Skip to content

Commit

Permalink
feat(checksum): add flag BASE16384_FLAG_DO_SUM_CHECK_FORCELY
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Apr 5, 2024
1 parent e5f8c0f commit cd15e63
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions base16384.1
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion base16384.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions base16384.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down
22 changes: 15 additions & 7 deletions test/file_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}
3 changes: 0 additions & 3 deletions test/wrap_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -133,7 +131,6 @@ int main() {

validate_result();
}
fputs("one test passed.\n", stderr);

remove(TEST_INPUT_FILENAME);
remove(TEST_OUTPUT_FILENAME);
Expand Down

0 comments on commit cd15e63

Please sign in to comment.