1
0
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:
源文雨
2024-04-05 00:48:13 +09:00
parent 7c8080d115
commit cde0cacd07
4 changed files with 31 additions and 19 deletions

View File

@@ -15,6 +15,10 @@ if (${isBigEndian})
add_definitions(-DWORDS_BIGENDIAN)
endif ()
if (BUILD STREQUAL "test")
add_definitions(-DBASE16384_BUFSZ_FACTOR=1)
endif ()
add_executable(base16384_b base16384.c)
IF ((NOT FORCE_32BIT) AND CMAKE_SIZEOF_VOID_P EQUAL 8)

View File

@@ -42,8 +42,12 @@ enum base16384_err_t {
*/
typedef enum base16384_err_t base16384_err_t;
#define _BASE16384_ENCBUFSZ (BUFSIZ*1024/7*7)
#define _BASE16384_DECBUFSZ (BUFSIZ*1024/8*8)
#ifndef BASE16384_BUFSZ_FACTOR
#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_DECBUFSZ (_BASE16384_DECBUFSZ+16)

34
file.c
View File

@@ -393,40 +393,44 @@ base16384_err_t base16384_decode_fd_detailed(int input, int output, char* encbuf
errno = EINVAL;
return base16384_err_fopen_output_file;
}
off_t inputsize = _BASE16384_DECBUFSZ-1;
int cnt = 0;
off_t inputsize = _BASE16384_DECBUFSZ;
int p = 0, n;
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
uint8_t remains[8];
decbuf[0] = 0;
if(read(input, decbuf, 2) != 2) {
if(read(input, remains, 2) != 2) {
return base16384_err_read_file;
}
if(decbuf[0] != (char)(0xfe)) {
cnt = read(input, decbuf+2, inputsize-2)+2;
} else {
cnt = read(input, decbuf, inputsize);
}
if(cnt > 0) do {
if(remains[0] != (uint8_t)(0xfe)) p = 2;
while((n = read(input, decbuf+p, inputsize-p)) > 0) {
if(p) {
memcpy(decbuf, remains, p);
n += p;
p = 0;
}
uint16_t next = is_next_end_fd(input);
if(errno) {
return base16384_err_read_file;
}
if((uint16_t)(~next)) {
if(next&0xff00) decbuf[cnt++] = '=';
decbuf[cnt++] = (char)(next&0x00ff);
if(next&0xff00) {
decbuf[n++] = '=';
decbuf[n++] = (char)(next&0x00ff);
} else remains[p++] = (char)(next&0x00ff);
}
#ifdef DEBUG
fprintf(stderr, "decode chunk: %d, last2: %c %02x\n", cnt, decbuf[cnt-2], (uint8_t)decbuf[cnt-1]);
#endif
cnt = base16384_decode_unsafe(decbuf, cnt, encbuf);
if(cnt && write(output, encbuf, cnt) != cnt) {
n = base16384_decode_unsafe(decbuf, n, encbuf);
if(n && write(output, encbuf, n) != n) {
return base16384_err_write_file;
}
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;
return base16384_err_invalid_decoding_checksum;
}
}
} while((cnt = read(input, decbuf, inputsize)) > 0);
}
return base16384_err_ok;
}

View File

@@ -105,7 +105,7 @@ char tstbuf[BASE16384_ENCBUFSZ];
}
#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(); \
for(i = TEST_SIZE; i > 0; i--) { \
reset_and_truncate(fd, i); \
@@ -137,7 +137,7 @@ char tstbuf[BASE16384_ENCBUFSZ];
}
#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(); \
for(i = TEST_SIZE; i > 0; i--) { \
reset_and_truncate(fd, i); \