mirror of
https://github.com/fumiama/base16384.git
synced 2026-06-05 02:00:31 +08:00
fix(file): base16384_decode_fd_detailed
This commit is contained in:
@@ -15,6 +15,10 @@ if (${isBigEndian})
|
|||||||
add_definitions(-DWORDS_BIGENDIAN)
|
add_definitions(-DWORDS_BIGENDIAN)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
|
if (BUILD STREQUAL "test")
|
||||||
|
add_definitions(-DBASE16384_BUFSZ_FACTOR=1)
|
||||||
|
endif ()
|
||||||
|
|
||||||
add_executable(base16384_b base16384.c)
|
add_executable(base16384_b base16384.c)
|
||||||
|
|
||||||
IF ((NOT FORCE_32BIT) AND CMAKE_SIZEOF_VOID_P EQUAL 8)
|
IF ((NOT FORCE_32BIT) AND CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||||
|
|||||||
@@ -42,8 +42,12 @@ enum base16384_err_t {
|
|||||||
*/
|
*/
|
||||||
typedef enum base16384_err_t base16384_err_t;
|
typedef enum base16384_err_t base16384_err_t;
|
||||||
|
|
||||||
#define _BASE16384_ENCBUFSZ (BUFSIZ*1024/7*7)
|
#ifndef BASE16384_BUFSZ_FACTOR
|
||||||
#define _BASE16384_DECBUFSZ (BUFSIZ*1024/8*8)
|
#define BASE16384_BUFSZ_FACTOR (1024)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define _BASE16384_ENCBUFSZ ((BUFSIZ*BASE16384_BUFSZ_FACTOR)/7*7)
|
||||||
|
#define _BASE16384_DECBUFSZ ((BUFSIZ*BASE16384_BUFSZ_FACTOR)/8*8)
|
||||||
|
|
||||||
#define BASE16384_ENCBUFSZ (_BASE16384_ENCBUFSZ+16)
|
#define BASE16384_ENCBUFSZ (_BASE16384_ENCBUFSZ+16)
|
||||||
#define BASE16384_DECBUFSZ (_BASE16384_DECBUFSZ+16)
|
#define BASE16384_DECBUFSZ (_BASE16384_DECBUFSZ+16)
|
||||||
|
|||||||
34
file.c
34
file.c
@@ -393,40 +393,44 @@ base16384_err_t base16384_decode_fd_detailed(int input, int output, char* encbuf
|
|||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return base16384_err_fopen_output_file;
|
return base16384_err_fopen_output_file;
|
||||||
}
|
}
|
||||||
off_t inputsize = _BASE16384_DECBUFSZ-1;
|
off_t inputsize = _BASE16384_DECBUFSZ;
|
||||||
int cnt = 0;
|
int p = 0, n;
|
||||||
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
|
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
|
||||||
|
uint8_t remains[8];
|
||||||
decbuf[0] = 0;
|
decbuf[0] = 0;
|
||||||
if(read(input, decbuf, 2) != 2) {
|
if(read(input, remains, 2) != 2) {
|
||||||
return base16384_err_read_file;
|
return base16384_err_read_file;
|
||||||
}
|
}
|
||||||
if(decbuf[0] != (char)(0xfe)) {
|
if(remains[0] != (uint8_t)(0xfe)) p = 2;
|
||||||
cnt = read(input, decbuf+2, inputsize-2)+2;
|
while((n = read(input, decbuf+p, inputsize-p)) > 0) {
|
||||||
} else {
|
if(p) {
|
||||||
cnt = read(input, decbuf, inputsize);
|
memcpy(decbuf, remains, p);
|
||||||
}
|
n += p;
|
||||||
if(cnt > 0) do {
|
p = 0;
|
||||||
|
}
|
||||||
uint16_t next = is_next_end_fd(input);
|
uint16_t next = is_next_end_fd(input);
|
||||||
if(errno) {
|
if(errno) {
|
||||||
return base16384_err_read_file;
|
return base16384_err_read_file;
|
||||||
}
|
}
|
||||||
if((uint16_t)(~next)) {
|
if((uint16_t)(~next)) {
|
||||||
if(next&0xff00) decbuf[cnt++] = '=';
|
if(next&0xff00) {
|
||||||
decbuf[cnt++] = (char)(next&0x00ff);
|
decbuf[n++] = '=';
|
||||||
|
decbuf[n++] = (char)(next&0x00ff);
|
||||||
|
} else remains[p++] = (char)(next&0x00ff);
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf(stderr, "decode chunk: %d, last2: %c %02x\n", cnt, decbuf[cnt-2], (uint8_t)decbuf[cnt-1]);
|
fprintf(stderr, "decode chunk: %d, last2: %c %02x\n", cnt, decbuf[cnt-2], (uint8_t)decbuf[cnt-1]);
|
||||||
#endif
|
#endif
|
||||||
cnt = base16384_decode_unsafe(decbuf, cnt, encbuf);
|
n = base16384_decode_unsafe(decbuf, n, encbuf);
|
||||||
if(cnt && write(output, encbuf, cnt) != cnt) {
|
if(n && write(output, encbuf, n) != n) {
|
||||||
return base16384_err_write_file;
|
return base16384_err_write_file;
|
||||||
}
|
}
|
||||||
if(flag&BASE16384_FLAG_SUM_CHECK_ON_REMAIN) {
|
if(flag&BASE16384_FLAG_SUM_CHECK_ON_REMAIN) {
|
||||||
if (calc_and_check_sum(&sum, cnt, encbuf)) {
|
if (calc_and_check_sum(&sum, n, encbuf)) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return base16384_err_invalid_decoding_checksum;
|
return base16384_err_invalid_decoding_checksum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while((cnt = read(input, decbuf, inputsize)) > 0);
|
}
|
||||||
return base16384_err_ok;
|
return base16384_err_ok;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ char tstbuf[BASE16384_ENCBUFSZ];
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define test_fp_detailed(flag) \
|
#define test_fp_detailed(flag) \
|
||||||
fputs("testing base16384_en/decode_fp...\n", stderr); \
|
fputs("testing base16384_en/decode_fp with flag "#flag"...\n", stderr); \
|
||||||
init_input_file(); \
|
init_input_file(); \
|
||||||
for(i = TEST_SIZE; i > 0; i--) { \
|
for(i = TEST_SIZE; i > 0; i--) { \
|
||||||
reset_and_truncate(fd, i); \
|
reset_and_truncate(fd, i); \
|
||||||
@@ -137,7 +137,7 @@ char tstbuf[BASE16384_ENCBUFSZ];
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define test_fd_detailed(flag) \
|
#define test_fd_detailed(flag) \
|
||||||
fputs("testing base16384_en/decode_fd...\n", stderr); \
|
fputs("testing base16384_en/decode_fd with flag "#flag"...\n", stderr); \
|
||||||
init_input_file(); \
|
init_input_file(); \
|
||||||
for(i = TEST_SIZE; i > 0; i--) { \
|
for(i = TEST_SIZE; i > 0; i--) { \
|
||||||
reset_and_truncate(fd, i); \
|
reset_and_truncate(fd, i); \
|
||||||
|
|||||||
Reference in New Issue
Block a user