diff --git a/base1432.c b/base1432.c index 9089ab0..faef239 100644 --- a/base1432.c +++ b/base1432.c @@ -30,7 +30,7 @@ typedef union { int base16384_encode_safe(const char* data, int dlen, char* buf) { int outlen = dlen / 7 * 8; int offset = dlen % 7; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen += 4; break; case 2: @@ -127,7 +127,7 @@ int base16384_encode_safe(const char* data, int dlen, char* buf) { int base16384_encode(const char* data, int dlen, char* buf) { int outlen = dlen / 7 * 8; int offset = dlen % 7; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen += 4; break; case 2: @@ -202,7 +202,7 @@ int base16384_encode(const char* data, int dlen, char* buf) { int base16384_encode_unsafe(const char* data, int dlen, char* buf) { int outlen = dlen / 7 * 8; int offset = dlen % 7; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen += 4; break; case 2: @@ -244,7 +244,7 @@ int base16384_decode_safe(const char* data, int dlen, char* buf) { int offset = 0; if(data[dlen-2] == '=') { offset = data[dlen-1]; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen -= 4; break; case 2: @@ -259,7 +259,7 @@ int base16384_decode_safe(const char* data, int dlen, char* buf) { const uint32_t* vals = (const uint32_t*)data; uint32_t n = 0; int32_t i = 0; - for(; i < outlen - 7; i+=7) { // n实际每次自增2 + for(; i < outlen - 7; i+=7) { // n += 2 in one loop register uint32_t sum = 0; register uint32_t shift = htobe32(vals[n++]) - 0x4e004e00; shift <<= 2; @@ -341,7 +341,7 @@ int base16384_decode(const char* data, int dlen, char* buf) { int offset = 0; if(data[dlen-2] == '=') { offset = data[dlen-1]; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen -= 4; break; case 2: @@ -356,7 +356,7 @@ int base16384_decode(const char* data, int dlen, char* buf) { const uint32_t* vals = (const uint32_t*)data; uint32_t n = 0; int32_t i = 0; - for(; i <= outlen - 7; i+=7) { // n实际每次自增2 + for(; i <= outlen - 7; i+=7) { // n += 2 in one loop register uint32_t sum = 0; register uint32_t shift = htobe32(vals[n++]) - 0x4e004e00; shift <<= 2; @@ -375,7 +375,7 @@ int base16384_decode(const char* data, int dlen, char* buf) { } if(*(uint8_t*)(&vals[n]) == '=') return outlen; if(offset--) { - // 这里有读取越界 + // here comes a read overlap #ifdef WORDS_BIGENDIAN register uint32_t sum = __builtin_bswap32(vals[n++]); #else @@ -391,7 +391,7 @@ int base16384_decode(const char* data, int dlen, char* buf) { if(offset--) { buf[i] = (sum & 0x0f000000) >> 20; if(*(uint8_t*)(&vals[n]) == '=') return outlen; - // 这里有读取越界 + // here comes a read overlap #ifdef WORDS_BIGENDIAN sum = __builtin_bswap32(vals[n]); #else @@ -418,7 +418,7 @@ int base16384_decode_unsafe(const char* data, int dlen, char* buf) { int offset = 0; if(data[dlen-2] == '=') { offset = data[dlen-1]; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen -= 4; break; case 2: @@ -433,7 +433,7 @@ int base16384_decode_unsafe(const char* data, int dlen, char* buf) { const uint32_t* vals = (const uint32_t*)data; uint32_t n = 0; int32_t i = 0; - for(; i < outlen-7; i+=7) { // n实际每次自增2 + for(; i < outlen-7; i+=7) { // n += 2 in one loop register uint32_t sum = 0; register uint32_t shift = htobe32(vals[n++]) - 0x4e004e00; shift <<= 2; diff --git a/base1464.c b/base1464.c index 1cfc786..80bc49c 100644 --- a/base1464.c +++ b/base1464.c @@ -30,7 +30,7 @@ typedef union { int base16384_encode_safe(const char* data, int dlen, char* buf) { int outlen = dlen / 7 * 8; int offset = dlen % 7; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen += 4; break; case 2: @@ -112,7 +112,7 @@ int base16384_encode_safe(const char* data, int dlen, char* buf) { int base16384_encode(const char* data, int dlen, char* buf) { int outlen = dlen / 7 * 8; int offset = dlen % 7; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen += 4; break; case 2: @@ -127,7 +127,7 @@ int base16384_encode(const char* data, int dlen, char* buf) { int64_t i = 0; for(; i <= dlen - 7; i += 7) { register uint64_t sum = 0; - register uint64_t shift = htobe64(*(uint64_t*)(data+i))>>2; // 这里有读取越界 + register uint64_t shift = htobe64(*(uint64_t*)(data+i))>>2; // here comes a read overlap sum |= shift & 0x3fff000000000000; shift >>= 2; sum |= shift & 0x00003fff00000000; @@ -177,7 +177,7 @@ int base16384_encode(const char* data, int dlen, char* buf) { int base16384_encode_unsafe(const char* data, int dlen, char* buf) { int outlen = dlen / 7 * 8; int offset = dlen % 7; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen += 4; break; case 2: @@ -192,7 +192,7 @@ int base16384_encode_unsafe(const char* data, int dlen, char* buf) { int64_t i = 0; for(; i < dlen; i += 7) { register uint64_t sum = 0; - register uint64_t shift = htobe64(*(uint64_t*)(data+i))>>2; // 这里有读取越界 + register uint64_t shift = htobe64(*(uint64_t*)(data+i))>>2; // here comes a read overlap sum |= shift & 0x3fff000000000000; shift >>= 2; sum |= shift & 0x00003fff00000000; @@ -215,7 +215,7 @@ int base16384_decode_safe(const char* data, int dlen, char* buf) { int offset = 0; if(data[dlen-2] == '=') { offset = data[dlen-1]; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen -= 4; break; case 2: @@ -292,7 +292,7 @@ int base16384_decode(const char* data, int dlen, char* buf) { int offset = 0; if(data[dlen-2] == '=') { offset = data[dlen-1]; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen -= 4; break; case 2: @@ -322,7 +322,7 @@ int base16384_decode(const char* data, int dlen, char* buf) { } if(*(uint8_t*)(&vals[n]) == '=') return outlen; if(offset--) { - // 这里有读取越界 + // here comes a read overlap #ifdef WORDS_BIGENDIAN register uint64_t sum = __builtin_bswap64(vals[n]) - 0x000000000000004e; #else @@ -356,7 +356,7 @@ int base16384_decode_unsafe(const char* data, int dlen, char* buf) { int offset = 0; if(data[dlen-2] == '=') { offset = data[dlen-1]; - switch(offset) { // 算上偏移标志字符占用的2字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen -= 4; break; case 2: diff --git a/base16384.h b/base16384.h index e0631a8..c24a05b 100644 --- a/base16384.h +++ b/base16384.h @@ -66,7 +66,7 @@ typedef enum base16384_err_t base16384_err_t; * @param count read bytes count * @return the size read */ -typedef ssize_t(*base16384_reader_t)(const void *client_data, void *buffer, size_t count); +typedef ssize_t (*base16384_reader_t)(const void *client_data, void *buffer, size_t count); /** * @brief custom writer function interface @@ -75,13 +75,16 @@ typedef ssize_t(*base16384_reader_t)(const void *client_data, void *buffer, size * @param count write bytes count * @return the size written */ -typedef ssize_t(*base16384_writer_t)(const void *client_data, const void *buffer, size_t count); +typedef ssize_t (*base16384_writer_t)(const void *client_data, const void *buffer, size_t count); + +union base16384_io_function_t { + base16384_reader_t reader; + base16384_writer_t writer; +}; +typedef union base16384_io_function_t base16384_io_function_t; struct base16384_stream_t { - const union { - base16384_reader_t reader; - base16384_writer_t writer; - } f; + const base16384_io_function_t f; const void *client_data; }; /** @@ -97,7 +100,7 @@ typedef struct base16384_stream_t base16384_stream_t; static inline int _base16384_encode_len(int dlen) { int outlen = dlen / 7 * 8; int offset = dlen % 7; - switch(offset) { // 算上偏移标志字符占用的 2 字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen += 4; break; case 2: @@ -127,7 +130,7 @@ static inline int base16384_encode_len(int dlen) { */ static inline int _base16384_decode_len(int dlen, int offset) { int outlen = dlen; - switch(offset) { // 算上偏移标志字符占用的 2 字节 + switch(offset) { // also count 0x3dxx case 0: break; case 1: outlen -= 4; break; case 2: