From 65635d3df37ad0ce8a6ed97624e5f8588e5dc399 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: Mon, 17 Oct 2022 10:39:54 +0800 Subject: [PATCH 1/4] fix file api --- file.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/file.c b/file.c index 3ab0885..85749c5 100644 --- a/file.c +++ b/file.c @@ -70,7 +70,7 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha size_t cnt = 0; fputc(0xFE, fpo); fputc(0xFF, fpo); - while((cnt = fread(encbuf, sizeof(char), inputsize, fp))) { + while((cnt = fread(encbuf, sizeof(char), inputsize, fp)) > 0) { int n = base16384_encode(encbuf, cnt, decbuf, outputsize); if(fwrite(decbuf, n, 1, fpo) <= 0) { return base16384_err_write_file; @@ -115,7 +115,7 @@ base16384_err_t base16384_encode_fp(FILE* input, FILE* output, char* encbuf, cha size_t cnt = 0; fputc(0xFE, output); fputc(0xFF, output); - while((cnt = fread(encbuf, sizeof(char), inputsize, input))) { + while((cnt = fread(encbuf, sizeof(char), inputsize, input)) > 0) { int n = base16384_encode(encbuf, cnt, decbuf, outputsize); if(fwrite(decbuf, n, 1, output) <= 0) { return base16384_err_write_file; @@ -125,17 +125,17 @@ base16384_err_t base16384_encode_fp(FILE* input, FILE* output, char* encbuf, cha } base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* decbuf) { - if(!input) { + if(input < 0) { return base16384_err_fopen_input_file; } - if(!output) { + if(output < 0) { return base16384_err_fopen_output_file; } off_t inputsize = BUFSIZ*1024/7*7; int outputsize = base16384_encode_len(inputsize)+16; size_t cnt = 0; write(output, "\xfe\xff", 2); - while((cnt = read(input, encbuf, inputsize))) { + while((cnt = read(input, encbuf, inputsize)) > 0) { int n = base16384_encode(encbuf, cnt, decbuf, outputsize); if(write(output, decbuf, n) < n) { return base16384_err_write_file; @@ -188,7 +188,7 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha int cnt = 0; int end = 0; rm_head(fp); - while((cnt = fread(decbuf, sizeof(char), inputsize, fp))) { + while((cnt = fread(decbuf, sizeof(char), inputsize, fp)) > 0) { if((end = is_next_end(fp))) { decbuf[cnt++] = '='; decbuf[cnt++] = end; @@ -234,7 +234,7 @@ base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, cha int cnt = 0; int end = 0; rm_head(input); - while((cnt = fread(decbuf, sizeof(char), inputsize, input))) { + while((cnt = fread(decbuf, sizeof(char), inputsize, input)) > 0) { if((end = is_next_end(input))) { decbuf[cnt++] = '='; decbuf[cnt++] = end; @@ -256,10 +256,10 @@ static int is_next_end_fd(int fd) { } base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* decbuf) { - if(!input) { + if(input < 0) { return base16384_err_fopen_input_file; } - if(!output) { + if(output < 0) { return base16384_err_fopen_output_file; } off_t inputsize = BUFSIZ*1024/8*8; @@ -269,17 +269,21 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d decbuf[0] = 0; read(input, decbuf, 2); if(decbuf[0] != (char)(0xfe)) cnt = 2; - while((cnt += read(input, decbuf+cnt, inputsize-cnt), cnt)) { - if((end = is_next_end_fd(input))) { - decbuf[cnt++] = '='; - decbuf[cnt++] = end; - return base16384_err_ok; - } + while((end = read(input, decbuf+cnt, inputsize-cnt), cnt) > 0 || cnt > 0) { + if(end > 0) { + cnt += end; + if((end = is_next_end_fd(input))) { + decbuf[cnt++] = '='; + decbuf[cnt++] = end; + return base16384_err_ok; + } + end = 1; + } else end = 0; cnt = base16384_decode(decbuf, cnt, encbuf, outputsize); if(write(output, encbuf, cnt) < cnt) { return base16384_err_write_file; } - cnt = 1; + cnt = end; } return base16384_err_ok; } From 6b5229c8022b013f7ed38349867e76ab589a7883 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: Mon, 17 Oct 2022 11:05:30 +0800 Subject: [PATCH 2/4] fix decode fp --- file.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/file.c b/file.c index 85749c5..6952d15 100644 --- a/file.c +++ b/file.c @@ -275,9 +275,8 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d if((end = is_next_end_fd(input))) { decbuf[cnt++] = '='; decbuf[cnt++] = end; - return base16384_err_ok; - } - end = 1; + end = 0; + } else end = 1; } else end = 0; cnt = base16384_decode(decbuf, cnt, encbuf, outputsize); if(write(output, encbuf, cnt) < cnt) { From 9cfa8ad1bc9802a329ac251fb98bf9e21980dca7 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: Mon, 17 Oct 2022 12:41:05 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=20include?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base16384.h | 1 + 1 file changed, 1 insertion(+) diff --git a/base16384.h b/base16384.h index a42d5af..21fe64a 100644 --- a/base16384.h +++ b/base16384.h @@ -20,6 +20,7 @@ #ifndef __cosmopolitan #include +#include #endif // base16384_err_t is the return value of base16384_en/decode_file From b9fcd2e1536437168a31d6dedfe567c6cd5b1e3d 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: Mon, 17 Oct 2022 12:56:55 +0800 Subject: [PATCH 4/4] fix base16384_decode_fd --- base16384.c | 1 + base16384.h | 1 + file.c | 12 +++++++----- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/base16384.c b/base16384.c index a2dd1f1..2e466de 100644 --- a/base16384.c +++ b/base16384.c @@ -103,6 +103,7 @@ int main(int argc, char** argv) { case base16384_err_write_file: perror("base16384_err_write_file"); break; case base16384_err_open_input_file: perror("base16384_err_open_input_file"); break; case base16384_err_map_input_file: perror("base16384_err_map_input_file"); break; + case base16384_err_read_file: perror("base16384_err_read_file"); break; default: perror("base16384"); break; } return exitstat; diff --git a/base16384.h b/base16384.h index 21fe64a..daaae49 100644 --- a/base16384.h +++ b/base16384.h @@ -32,6 +32,7 @@ enum base16384_err_t { base16384_err_write_file, base16384_err_open_input_file, base16384_err_map_input_file, + base16384_err_read_file, }; // base16384_err_t is the return value of base16384_en/decode_file typedef enum base16384_err_t base16384_err_t; diff --git a/file.c b/file.c index 6952d15..3d50442 100644 --- a/file.c +++ b/file.c @@ -35,7 +35,7 @@ #ifdef __cosmopolitan #define get_file_size(filepath) ((off_t)GetFileSize(filepath)) #else -static off_t get_file_size(const char* filepath) { +static inline off_t get_file_size(const char* filepath) { struct stat statbuf; return stat(filepath, &statbuf)?-1:statbuf.st_size; } @@ -152,7 +152,7 @@ base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* d #define skip_offset(input_file) ((input_file[0]==(char)0xFE)?2:0) -static int is_next_end(FILE* fp) { +static inline int is_next_end(FILE* fp) { int ch = fgetc(fp); if(ch == EOF) return 0; if(ch == '=') return fgetc(fp); @@ -246,7 +246,7 @@ base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, cha return base16384_err_ok; } -static int is_next_end_fd(int fd) { +static inline int is_next_end_fd(int fd) { char ch = 0; read(fd, &ch, 1); if(ch == '=') { @@ -267,9 +267,11 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d int cnt = 0; int end = 0; decbuf[0] = 0; - read(input, decbuf, 2); + if(read(input, decbuf, 2) < 2) { + return base16384_err_read_file; + } if(decbuf[0] != (char)(0xfe)) cnt = 2; - while((end = read(input, decbuf+cnt, inputsize-cnt), cnt) > 0 || cnt > 0) { + while((end = read(input, decbuf+cnt, inputsize-cnt)) > 0 || cnt > 0) { if(end > 0) { cnt += end; if((end = is_next_end_fd(input))) {