diff --git a/CMakeLists.txt b/CMakeLists.txt index e56b1f5..f75817f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.12) if (POLICY CMP0048) cmake_policy(SET CMP0048 NEW) endif (POLICY CMP0048) -project(base16384 VERSION 2.2.4) +project(base16384 VERSION 2.2.5) add_executable(base16384_b base16384.c) diff --git a/base1432.c b/base1432.c index 86d9080..ac09987 100644 --- a/base1432.c +++ b/base1432.c @@ -1,6 +1,6 @@ /* base1432.c * This file is part of the base16384 distribution (https://github.com/fumiama/base16384). - * Copyright (c) 2022 Fumiama Minamoto. + * Copyright (c) 2022-2023 Fumiama Minamoto. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -62,7 +62,7 @@ // #define DEBUG -int base16384_encode(const char* data, int dlen, char* buf, int blen) { +int base16384_encode(const char* data, int dlen, char* buf) { int outlen = dlen / 7 * 8; int offset = dlen % 7; switch(offset) { // 算上偏移标志字符占用的2字节 @@ -140,7 +140,7 @@ int base16384_encode(const char* data, int dlen, char* buf, int blen) { return outlen; } -int base16384_decode(const char* data, int dlen, char* buf, int blen) { +int base16384_decode(const char* data, int dlen, char* buf) { int outlen = dlen; int offset = 0; if(data[dlen-2] == '=') { diff --git a/base1464.c b/base1464.c index a29b002..73b0432 100644 --- a/base1464.c +++ b/base1464.c @@ -1,6 +1,6 @@ /* base1464.c * This file is part of the base16384 distribution (https://github.com/fumiama/base16384). - * Copyright (c) 2022 Fumiama Minamoto. + * Copyright (c) 2022-2023 Fumiama Minamoto. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -62,7 +62,7 @@ // #define DEBUG -int base16384_encode(const char* data, int dlen, char* buf, int blen) { +int base16384_encode(const char* data, int dlen, char* buf) { int outlen = dlen / 7 * 8; int offset = dlen % 7; switch(offset) { // 算上偏移标志字符占用的2字节 @@ -136,7 +136,7 @@ int base16384_encode(const char* data, int dlen, char* buf, int blen) { return outlen; } -int base16384_decode(const char* data, int dlen, char* buf, int blen) { +int base16384_decode(const char* data, int dlen, char* buf) { int outlen = dlen; int offset = 0; if(data[dlen-2] == '=') { diff --git a/base16384.1 b/base16384.1 index 7630b8c..c8e0921 100644 --- a/base16384.1 +++ b/base16384.1 @@ -3,7 +3,7 @@ base16384 \- Encode binary files to printable utf16be .SH SYNOPSIS .B base16384 --[e|d] <\fIinputfile\fR> <\fIoutputfile\fR> +-[e|d|t] <\fIinputfile\fR> <\fIoutputfile\fR> .SH DESCRIPTION .LP There are @@ -29,6 +29,12 @@ to \fB\-e\fR Read data from \fIinputfile\fR and encode them into \fIoutputfile\fR. .TP 0.5i +\fB\-d\fR +Read data from \fIinputfile\fR and decode them into \fIoutputfile\fR. +.TP 0.5i +\fB\-t\fR +Show spend time. +.TP 0.5i \fBinputfile\fR An absolute or relative file path. Specially, pass - to read from stdin. .TP 0.5i @@ -59,6 +65,9 @@ Map input file error. .TP 0.5i \fB7\fR Write file error in mmap. +.TP 0.5i +\fB8\fR +Invalid input/output filename. .SH "SEE ALSO" https://github.com/fumiama/base16384 .SH BUGS @@ -68,7 +77,7 @@ on github. .SH AUTHOR This manual page contributed by Fumiama Minamoto. .SH "COPYRIGHT" -Copyright \(co 2022, Fumiama Minamoto +Copyright \(co 2022-2023, Fumiama Minamoto This file is part of .IR "base16384" . .LP diff --git a/base16384.c b/base16384.c index e6e04b0..d3c4b7b 100644 --- a/base16384.c +++ b/base16384.c @@ -1,6 +1,6 @@ /* base16384.c * This file is part of the base16384 distribution (https://github.com/fumiama/base16384). - * Copyright (c) 2022 Fumiama Minamoto. + * Copyright (c) 2022-2023 Fumiama Minamoto. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,7 +39,7 @@ unsigned long get_start_ms() { #endif static void print_usage() { - puts("Copyright (c) 2022-2023 Fumiama Minamoto.\nBase16384 2.2.4 (July 23rd 2023). Usage:"); + puts("Copyright (c) 2022-2023 Fumiama Minamoto.\nBase16384 2.2.5 (August 25th 2023). Usage:"); puts("base16384 [-edt] [inputfile] [outputfile]"); puts(" -e\t\tencode"); puts(" -d\t\tdecode"); @@ -97,15 +97,18 @@ int main(int argc, char** argv) { printf("spend time: %lums\n", get_start_ms() - t); #endif } + #define print_base16384_err(n) case base16384_err_##n: perror("base16384_err_"#n); break if(exitstat) switch(exitstat) { - case base16384_err_get_file_size: perror("base16384_err_get_file_size"); break; - case base16384_err_fopen_output_file: perror("base16384_err_fopen_output_file"); break; - case base16384_err_fopen_input_file: perror("base16384_err_fopen_input_file"); break; - 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; + print_base16384_err(get_file_size); + print_base16384_err(fopen_output_file); + print_base16384_err(fopen_input_file); + print_base16384_err(write_file); + print_base16384_err(open_input_file); + print_base16384_err(map_input_file); + print_base16384_err(read_file); + print_base16384_err(invalid_file_name); default: perror("base16384"); break; } + #undef print_base16384_err return exitstat; } diff --git a/base16384.h b/base16384.h index 9faa14d..f4bb985 100644 --- a/base16384.h +++ b/base16384.h @@ -3,7 +3,7 @@ /* base16384.h * This file is part of the base16384 distribution (https://github.com/fumiama/base16384). - * Copyright (c) 2022 Fumiama Minamoto. + * Copyright (c) 2022-2023 Fumiama Minamoto. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,22 +24,27 @@ #include #endif +#define define_base16384_err_t(n) base16384_err_##n + // base16384_err_t is the return value of base16384_en/decode_file enum base16384_err_t { - base16384_err_ok, - base16384_err_get_file_size, - base16384_err_fopen_output_file, - base16384_err_fopen_input_file, - base16384_err_write_file, - base16384_err_open_input_file, - base16384_err_map_input_file, - base16384_err_read_file, + define_base16384_err_t(ok), + define_base16384_err_t(get_file_size), + define_base16384_err_t(fopen_output_file), + define_base16384_err_t(fopen_input_file), + define_base16384_err_t(write_file), + define_base16384_err_t(open_input_file), + define_base16384_err_t(map_input_file), + define_base16384_err_t(read_file), + define_base16384_err_t(invalid_file_name), }; // base16384_err_t is the return value of base16384_en/decode_file typedef enum base16384_err_t base16384_err_t; +#undef define_base16384_err_t + #define BASE16384_ENCBUFSZ (BUFSIZ*1024/7*7) -#define BASE16384_DECBUFSZ (BUFSIZ*1024/8*8+2) +#define BASE16384_DECBUFSZ (BUFSIZ*1024/8*8+16) // base16384_encode_len calc min buf size to fill encode result static inline int base16384_encode_len(int dlen) { @@ -75,10 +80,10 @@ static inline int base16384_decode_len(int dlen, int offset) { } // base16384_encode encodes data and write result into buf -int base16384_encode(const char* data, int dlen, char* buf, int blen); +int base16384_encode(const char* data, int dlen, char* buf); // base16384_decode decodes data and write result into buf -int base16384_decode(const char* data, int dlen, char* buf, int blen); +int base16384_decode(const char* data, int dlen, char* buf); // base16384_encode_file encodes input file to output file. // use `-` to specify stdin/stdout diff --git a/file.c b/file.c index ff5bc09..dc58810 100644 --- a/file.c +++ b/file.c @@ -1,6 +1,6 @@ /* file.c * This file is part of the base16384 distribution (https://github.com/fumiama/base16384). - * Copyright (c) 2022 Fumiama Minamoto. + * Copyright (c) 2022-2023 Fumiama Minamoto. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #ifdef _WIN32 @@ -48,6 +49,7 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha off_t inputsize; FILE* fp = NULL; FILE* fpo; + if(!input || !output || strlen(input) <= 0 || strlen(output) <= 0) return base16384_err_invalid_file_name; if(is_standard_io(input)) { // read from stdin inputsize = 0; fp = stdin; @@ -59,8 +61,8 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha if(!fpo) { return base16384_err_fopen_output_file; } - if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use encbuf & fread - inputsize = BUFSIZ*1024/7*7; + if(!inputsize || inputsize > BASE16384_ENCBUFSZ) { // stdin or big file, use encbuf & fread + inputsize = BASE16384_ENCBUFSZ/8*8; #ifdef _WIN32 } #endif @@ -69,12 +71,11 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha return base16384_err_fopen_input_file; } - int outputsize = base16384_encode_len(inputsize)+16; size_t cnt = 0; fputc(0xFE, fpo); fputc(0xFF, fpo); while((cnt = fread(encbuf, sizeof(char), inputsize, fp)) > 0) { - int n = base16384_encode(encbuf, cnt, decbuf, outputsize); + int n = base16384_encode(encbuf, cnt, decbuf); if(fwrite(decbuf, n, 1, fpo) <= 0) { return base16384_err_write_file; } @@ -87,14 +88,13 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha if(fd < 0) { return base16384_err_open_input_file; } - char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0); + char *input_file = mmap(NULL, (size_t)inputsize+16, PROT_READ, MAP_PRIVATE, fd, 0); if(input_file == MAP_FAILED) { return base16384_err_map_input_file; } - int outputsize = base16384_encode_len(inputsize)+16; fputc(0xFE, fpo); fputc(0xFF, fpo); - int n = base16384_encode(input_file, (int)inputsize, decbuf, outputsize); + int n = base16384_encode(input_file, (int)inputsize, decbuf); if(fwrite(decbuf, n, 1, fpo) <= 0) { return base16384_err_write_file; } @@ -113,13 +113,12 @@ base16384_err_t base16384_encode_fp(FILE* input, FILE* output, char* encbuf, cha if(!output) { return base16384_err_fopen_output_file; } - off_t inputsize = BUFSIZ*1024/7*7; - int outputsize = base16384_encode_len(inputsize)+16; + off_t inputsize = BASE16384_ENCBUFSZ/8*8; size_t cnt = 0; fputc(0xFE, output); fputc(0xFF, output); while((cnt = fread(encbuf, sizeof(char), inputsize, input)) > 0) { - int n = base16384_encode(encbuf, cnt, decbuf, outputsize); + int n = base16384_encode(encbuf, cnt, decbuf); if(fwrite(decbuf, n, 1, output) <= 0) { return base16384_err_write_file; } @@ -134,12 +133,11 @@ base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* d if(output < 0) { return base16384_err_fopen_output_file; } - off_t inputsize = BUFSIZ*1024/7*7; - int outputsize = base16384_encode_len(inputsize)+16; + off_t inputsize = BASE16384_ENCBUFSZ/8*8; size_t cnt = 0; write(output, "\xfe\xff", 2); while((cnt = read(input, encbuf, inputsize)) > 0) { - int n = base16384_encode(encbuf, cnt, decbuf, outputsize); + int n = base16384_encode(encbuf, cnt, decbuf); if(write(output, decbuf, n) < n) { return base16384_err_write_file; } @@ -167,6 +165,7 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha off_t inputsize; FILE* fp = NULL; FILE* fpo; + if(!input || !output || strlen(input) <= 0 || strlen(output) <= 0) return base16384_err_invalid_file_name; if(is_standard_io(input)) { // read from stdin inputsize = 0; fp = stdin; @@ -178,8 +177,8 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha if(!fpo) { return base16384_err_fopen_output_file; } - if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use decbuf & fread - inputsize = BUFSIZ*1024/8*8; + if(!inputsize || inputsize > BASE16384_DECBUFSZ) { // stdin or big file, use decbuf & fread + inputsize = BASE16384_DECBUFSZ/8*8; #ifdef _WIN32 } #endif @@ -187,7 +186,6 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha if(!fp) { return base16384_err_fopen_input_file; } - int outputsize = base16384_decode_len(inputsize, 0)+16; int cnt = 0; int end = 0; rm_head(fp); @@ -196,7 +194,7 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha decbuf[cnt++] = '='; decbuf[cnt++] = end; } - if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf, outputsize), 1, fpo) <= 0) { + if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf), 1, fpo) <= 0) { return base16384_err_write_file; } } @@ -208,13 +206,12 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha if(fd < 0) { return base16384_err_open_input_file; } - char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0); + char *input_file = mmap(NULL, (size_t)inputsize+16, PROT_READ, MAP_PRIVATE, fd, 0); if(input_file == MAP_FAILED) { return base16384_err_map_input_file; } - int outputsize = base16384_decode_len(inputsize, 0)+16; int off = skip_offset(input_file); - if(fwrite(encbuf, base16384_decode(input_file+off, inputsize-off, encbuf, outputsize), 1, fpo) <= 0) { + if(fwrite(encbuf, base16384_decode(input_file+off, inputsize-off, encbuf), 1, fpo) <= 0) { return base16384_err_write_file; } munmap(input_file, (size_t)inputsize); @@ -232,8 +229,7 @@ base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, cha if(!output) { return base16384_err_fopen_output_file; } - off_t inputsize = BUFSIZ*1024/8*8; - int outputsize = base16384_decode_len(inputsize, 0)+16; + off_t inputsize = BASE16384_DECBUFSZ/8*8; int cnt = 0; int end = 0; rm_head(input); @@ -242,7 +238,7 @@ base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, cha decbuf[cnt++] = '='; decbuf[cnt++] = end; } - if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf, outputsize), 1, output) <= 0) { + if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf), 1, output) <= 0) { return base16384_err_write_file; } } @@ -265,8 +261,7 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d if(output < 0) { return base16384_err_fopen_output_file; } - off_t inputsize = BUFSIZ*1024/8*8; - int outputsize = base16384_decode_len(inputsize, 0)+16; + off_t inputsize = BASE16384_DECBUFSZ/8*8; int cnt = 0; int end = 0; decbuf[0] = 0; @@ -283,7 +278,7 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d end = 0; } else end = 1; } else end = 0; - cnt = base16384_decode(decbuf, cnt, encbuf, outputsize); + cnt = base16384_decode(decbuf, cnt, encbuf); if(write(output, encbuf, cnt) < cnt) { return base16384_err_write_file; }