From cde0cacd07b9ef0c8f437354cb14d93e5e1b9227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Fri, 5 Apr 2024 00:48:13 +0900 Subject: [PATCH] fix(file): base16384_decode_fd_detailed --- CMakeLists.txt | 4 ++++ base16384.h | 8 ++++++-- file.c | 34 +++++++++++++++++++--------------- test/file_test.c | 4 ++-- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0600cec..ca4b762 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/base16384.h b/base16384.h index 5707ca3..339dabb 100644 --- a/base16384.h +++ b/base16384.h @@ -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) diff --git a/file.c b/file.c index 038dcd4..b095b44 100644 --- a/file.c +++ b/file.c @@ -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; } diff --git a/test/file_test.c b/test/file_test.c index b3565d3..ce720fa 100644 --- a/test/file_test.c +++ b/test/file_test.c @@ -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); \