diff --git a/CMakeLists.txt b/CMakeLists.txt index ca4b762..9eec095 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ endif (POLICY CMP0048) project(base16384 VERSION 2.3.0) add_definitions(-DBASE16384_VERSION="${PROJECT_VERSION}") -add_definitions(-DBASE16384_VERSION_DATE="April 4th 2024") +add_definitions(-DBASE16384_VERSION_DATE="April 5th 2024") message(STATUS "Testing endian...") include(TestBigEndian) diff --git a/base16384.1 b/base16384.1 index 47c869a..d5dd9a5 100644 --- a/base16384.1 +++ b/base16384.1 @@ -1,4 +1,4 @@ -.TH BASE16384 1 "4 April 2024" "GNU" "User Commands" +.TH BASE16384 1 "5 April 2024" "GNU" "User Commands" .SH NAME base16384 \- Encode binary files to printable utf16be .SH SYNOPSIS @@ -40,16 +40,27 @@ Read data from \fIinputfile\fR and decode them into \fIoutputfile\fR. Show spend time. .TP 0.5i \fB\-n\fR -Do not write utf16be file header (0xFEFF) to the output. +Do not write utf16be file header +.B 0xFEFF +to the output. .TP 0.5i \fB\-c\fR -Embed or validate checksum in remainder when using stdin/stdout or inputsize > _BASE16384_ENCBUFSZ. +Embed or validate checksum in remainder when using \fIstdin\fR or \fIstdout\fR or inputsize > _BASE16384_ENCBUFSZ. +.TP 0.5i +\fB\-C\fR +Do +.B -c +forcely. .TP 0.5i \fBinputfile\fR -An absolute or relative file path. Specially, pass - to read from stdin. +An absolute or relative file path. Specially, pass +.B - +to read from \fIstdin\fR. .TP 0.5i \fBoutputfile\fR -An absolute or relative file path. Specially, pass - to write to stdout. +An absolute or relative file path. Specially, pass +.B - +to write to \fIstdout\fR. .SH "EXIT STATUS" .TP 0.5i \fB0\fR diff --git a/base16384.c b/base16384.c index f860f40..eb9102e 100644 --- a/base16384.c +++ b/base16384.c @@ -52,6 +52,7 @@ static base16384_err_t print_usage() { fputs(" -t\t\tshow spend time\n", stderr); fputs(" -n\t\tdon't write utf16be file header (0xFEFF)\n", stderr); fputs(" -c\t\tembed or validate checksum in remainder\n", stderr); + fputs(" -C\t\tdo -c forcely\n", stderr); fputs(" inputfile\tpass - to read from stdin\n", stderr); fputs(" outputfile\tpass - to write to stdout\n", stderr); return base16384_err_invalid_commandline_parameter; @@ -91,6 +92,9 @@ int main(int argc, char** argv) { case 'c': if(set_or_test_flag(use_checksum, 1)) return print_usage(); break; + case 'C': + if(set_or_test_flag(use_checksum, 2)) return print_usage(); + break; default: return print_usage(); break; @@ -111,7 +115,9 @@ int main(int argc, char** argv) { #define do_coding(method) base16384_##method##_file_detailed( \ argv[2], argv[3], encbuf, decbuf, \ - (no_header?BASE16384_FLAG_NOHEADER:0) | (use_checksum?BASE16384_FLAG_SUM_CHECK_ON_REMAIN:0) \ + (no_header?BASE16384_FLAG_NOHEADER:0) \ + | ((use_checksum&1)?BASE16384_FLAG_SUM_CHECK_ON_REMAIN:0) \ + | ((use_checksum&2)?BASE16384_FLAG_DO_SUM_CHECK_FORCELY:0) \ ) exitstat = is_encode?do_coding(encode):do_coding(decode); #undef do_coding diff --git a/base16384.h b/base16384.h index 9a8e6fc..2e1a34c 100644 --- a/base16384.h +++ b/base16384.h @@ -56,6 +56,8 @@ typedef enum base16384_err_t base16384_err_t; #define BASE16384_FLAG_NOHEADER (1<<0) // enable sum check when using stdin or inputsize > _BASE16384_ENCBUFSZ #define BASE16384_FLAG_SUM_CHECK_ON_REMAIN (1<<1) +// forcely do sumcheck without checking decoded data length +#define BASE16384_FLAG_DO_SUM_CHECK_FORCELY (1<<2) // initial sum value used in BASE16384_FLAG_SUM_CHECK_ON_REMAIN #define BASE16384_SIMPLE_SUM_INIT_VALUE (0x8e29c213) diff --git a/file.c b/file.c index e950305..b73601e 100644 --- a/file.c +++ b/file.c @@ -75,7 +75,7 @@ base16384_err_t base16384_encode_file_detailed(const char* input, const char* ou if(!fpo) { return base16384_err_fopen_output_file; } - if(inputsize >= _BASE16384_ENCBUFSZ) { // stdin or big file, use encbuf & fread + if(flag&BASE16384_FLAG_DO_SUM_CHECK_FORCELY || inputsize >= _BASE16384_ENCBUFSZ) { // stdin or big file, use encbuf & fread inputsize = _BASE16384_ENCBUFSZ; #if defined _WIN32 || defined __cosmopolitan } @@ -273,7 +273,7 @@ base16384_err_t base16384_decode_file_detailed(const char* input, const char* ou last_encbuf_cnt = cnt; } if(flag&BASE16384_FLAG_SUM_CHECK_ON_REMAIN - && total_decoded_len >= _BASE16384_ENCBUFSZ + && (flag&BASE16384_FLAG_DO_SUM_CHECK_FORCELY || total_decoded_len >= _BASE16384_ENCBUFSZ) && last_decbuf_cnt > 2 && decbuf[last_decbuf_cnt-2] == '=' && check_sum(sum, *(uint32_t*)(&encbuf[last_encbuf_cnt]), offset)) { @@ -350,7 +350,7 @@ base16384_err_t base16384_decode_fp_detailed(FILE* input, FILE* output, char* en last_encbuf_cnt = cnt; } if(flag&BASE16384_FLAG_SUM_CHECK_ON_REMAIN - && total_decoded_len >= _BASE16384_ENCBUFSZ + && (flag&BASE16384_FLAG_DO_SUM_CHECK_FORCELY || total_decoded_len >= _BASE16384_ENCBUFSZ) && last_decbuf_cnt > 2 && decbuf[last_decbuf_cnt-2] == '=' && check_sum(sum, *(uint32_t*)(&encbuf[last_encbuf_cnt]), offset)) { @@ -429,7 +429,7 @@ base16384_err_t base16384_decode_fd_detailed(int input, int output, char* encbuf last_encbuf_cnt = n; } if(flag&BASE16384_FLAG_SUM_CHECK_ON_REMAIN - && total_decoded_len >= _BASE16384_ENCBUFSZ + && (flag&BASE16384_FLAG_DO_SUM_CHECK_FORCELY || total_decoded_len >= _BASE16384_ENCBUFSZ) && last_decbuf_cnt > 2 && decbuf[last_decbuf_cnt-2] == '=' && check_sum(sum, *(uint32_t*)(&encbuf[last_encbuf_cnt]), offset)) { diff --git a/test/file_test.c b/test/file_test.c index 9382a81..b5ed8ed 100644 --- a/test/file_test.c +++ b/test/file_test.c @@ -130,13 +130,23 @@ char tstbuf[BASE16384_ENCBUFSZ]; #define test_detailed(name) \ test_##name##_detailed(0); \ - fputs("one test passed.\n", stderr); \ +\ test_##name##_detailed(BASE16384_FLAG_NOHEADER); \ - fputs("one test passed.\n", stderr); \ test_##name##_detailed(BASE16384_FLAG_SUM_CHECK_ON_REMAIN); \ - fputs("one test passed.\n", stderr); \ + test_##name##_detailed(BASE16384_FLAG_DO_SUM_CHECK_FORCELY); \ +\ test_##name##_detailed(BASE16384_FLAG_NOHEADER|BASE16384_FLAG_SUM_CHECK_ON_REMAIN); \ - fputs("one test passed.\n", stderr); + test_##name##_detailed(BASE16384_FLAG_NOHEADER|BASE16384_FLAG_DO_SUM_CHECK_FORCELY); \ +\ + test_##name##_detailed(BASE16384_FLAG_SUM_CHECK_ON_REMAIN|BASE16384_FLAG_DO_SUM_CHECK_FORCELY); \ +\ + test_##name##_detailed(BASE16384_FLAG_NOHEADER|BASE16384_FLAG_SUM_CHECK_ON_REMAIN|BASE16384_FLAG_DO_SUM_CHECK_FORCELY); + + +#define remove_test_files() \ + remove(TEST_INPUT_FILENAME); \ + remove(TEST_OUTPUT_FILENAME); \ + remove(TEST_VALIDATE_FILENAME); int main() { srand(time(NULL)); @@ -149,9 +159,7 @@ int main() { test_detailed(fp); test_detailed(fd); - remove(TEST_INPUT_FILENAME); - remove(TEST_OUTPUT_FILENAME); - remove(TEST_VALIDATE_FILENAME); + remove_test_files(); return 0; } diff --git a/test/wrap_test.c b/test/wrap_test.c index 095c40d..4d5df88 100644 --- a/test/wrap_test.c +++ b/test/wrap_test.c @@ -74,7 +74,6 @@ int main() { validate_result(); } - fputs("one test passed.\n", stderr); fputs("testing base16384_en/decode_fp...\n", stderr); init_input_file(); @@ -106,7 +105,6 @@ int main() { validate_result(); } - fputs("one test passed.\n", stderr); fputs("testing base16384_en/decode_fd...\n", stderr); init_input_file(); @@ -133,7 +131,6 @@ int main() { validate_result(); } - fputs("one test passed.\n", stderr); remove(TEST_INPUT_FILENAME); remove(TEST_OUTPUT_FILENAME);