From a768996479cda61980b1fa0bdb8287e09eb59793 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: Wed, 27 Apr 2022 12:47:31 +0800 Subject: [PATCH] add manpage --- CMakeLists.txt | 1 + base16384.1 | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ base16384.c | 43 +++++++++++++------------ 3 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 base16384.1 diff --git a/CMakeLists.txt b/CMakeLists.txt index 09a1a9c..c1812c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,3 +27,4 @@ target_link_libraries(base16384 libbase16384) INSTALL(TARGETS base16384 RUNTIME DESTINATION bin) INSTALL(TARGETS libbase16384 LIBRARY DESTINATION lib) INSTALL(FILES base16384.h DESTINATION include) +INSTALL(FILES base16384.1 DESTINATION share/man/man1) \ No newline at end of file diff --git a/base16384.1 b/base16384.1 new file mode 100644 index 0000000..64aedfc --- /dev/null +++ b/base16384.1 @@ -0,0 +1,86 @@ +.TH BASE16384 1 "27 April 2022" "GNU" "User Commands" +.SH NAME +base16384 \- Encode binary files to printable utf16be +.SH SYNOPSIS +.B base16384 +-[e|d] <\fIinputfile\fR> <\fIoutputfile\fR> +.SH DESCRIPTION +.LP +There are +.B 16384 +Chinese characters ( from +.B 4E00 +to +.B 8DFF +) being selected as the +.I "alphabet" +, just like what base64 did. If length of the data has a remainder after mod 7, an unicode Chinese character +.B 3Dxx +will be appended to present it with +.I xx +ranging from +.I 01 +to +.I 06 +. +.SH OPTIONS +.sp 1 +.TP 0.5i +\fB\-e\fR +Read data from \fIinputfile\fR and encode them into \fIoutputfile\fR. +.TP 0.5i +\fBinputfile\fR +An absolute or relative file path. Specially, pass - to read from stdin. +.TP 0.5i +\fBoutputfile\fR +An absolute or relative file path. Specially, pass - to write to stdout. +.SH "EXIT STATUS" +.TP 0.5i +\fB0\fR +Exit on success. +.TP 0.5i +\fB1\fR +Get file size error. +.TP 0.5i +\fB2\fR +Fopen output file error. +.TP 0.5i +\fB3\fR +Fopen input file error. +.TP 0.5i +\fB4\fR +Write file error. +.TP 0.5i +\fB5\fR +Open input file error. +.TP 0.5i +\fB6\fR +Map input file error. +.TP 0.5i +\fB7\fR +Write file error in mmap. +.SH "SEE ALSO" +https://github.com/fumiama/base16384 +.SH BUGS +There are currently no known bugs in this application. If you have found them, please contact the +.B author +on github. +.SH AUTHOR +This manual page contributed by fumiama. +.SH "COPYRIGHT" +Copyright \(co 1992-1993, 1996-2022 Free Software Foundation, Inc. +This file is part of +.IR "base16384" . +.LP +Base16384 is free software; you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. +.LP +Base16384 is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. See the GNU General Public License for more details. +.LP +You should have received a copy of the GNU General Public License along with +this program. If not, see +.IR http://www.gnu.org/licenses/ . diff --git a/base16384.c b/base16384.c index e0da532..e5052a0 100644 --- a/base16384.c +++ b/base16384.c @@ -25,7 +25,7 @@ static off_t get_file_size(const char* filepath) { char encbuf[BUFSIZ*1024/7*7]; char decbuf[BUFSIZ*1024/8*8+2]; -void encode_file(const char* input, const char* output) { +static int encode_file(const char* input, const char* output) { off_t inputsize; FILE* fp = NULL; FILE* fpo; @@ -35,12 +35,12 @@ void encode_file(const char* input, const char* output) { } else inputsize = get_file_size(input); if(inputsize < 0) { perror("Get file size error: "); - return; + return 1; } fpo = (*(uint16_t*)output == *(uint16_t*)"-")?stdout:fopen(output, "wb"); if(!fpo) { perror("Fopen output file error: "); - return; + return 2; } if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use encbuf & fread inputsize = BUFSIZ*1024/7*7; @@ -50,7 +50,7 @@ void encode_file(const char* input, const char* output) { if(!fp) fp = fopen(input, "rb"); if(!fp) { perror("Fopen input file error: "); - return; + return 3; } int outputsize = encode_len(inputsize)+16; @@ -61,7 +61,7 @@ void encode_file(const char* input, const char* output) { int n = encode(encbuf, cnt, decbuf, outputsize); if(fwrite(decbuf, n, 1, fpo) <= 0) { perror("Write file error: "); - return; + return 4; } } /* 由操作系统负责释放资源 @@ -73,12 +73,12 @@ void encode_file(const char* input, const char* output) { int fd = open(input, O_RDONLY); if(fd <= 0) { perror("Open input file error: "); - return; + return 5; } char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0); if(input_file == MAP_FAILED) { perror("Map input file error: "); - return; + return 6; } int outputsize = encode_len(inputsize)+16; fputc(0xFE, fpo); @@ -86,7 +86,7 @@ void encode_file(const char* input, const char* output) { int n = encode(input_file, (int)inputsize, decbuf, outputsize); if(fwrite(decbuf, n, 1, fpo) <= 0) { perror("Write file error: "); - return; + return 7; } munmap(input_file, (size_t)inputsize); /* 由操作系统负责释放资源 @@ -95,6 +95,7 @@ void encode_file(const char* input, const char* output) { 以缩短程序运行时间 */ } #endif + return 0; } #define rm_head(fp) {\ @@ -112,7 +113,7 @@ static int is_next_end(FILE* fp) { return 0; } -void decode_file(const char* input, const char* output) { +static int decode_file(const char* input, const char* output) { off_t inputsize; FILE* fp = NULL; FILE* fpo; @@ -122,12 +123,12 @@ void decode_file(const char* input, const char* output) { } else inputsize = get_file_size(input); if(inputsize < 0) { perror("Get file size error: "); - return; + return 1; } fpo = (*(uint16_t*)output == *(uint16_t*)"-")?stdout:fopen(output, "wb"); if(!fpo) { perror("Fopen output file error: "); - return; + return 2; } if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use decbuf & fread inputsize = BUFSIZ*1024/8*8; @@ -137,7 +138,7 @@ void decode_file(const char* input, const char* output) { if(!fp) fp = fopen(input, "rb"); if(!fp) { perror("Fopen input file error: "); - return; + return 3; } int outputsize = decode_len(inputsize, 0)+16; int cnt = 0; @@ -150,7 +151,7 @@ void decode_file(const char* input, const char* output) { } if(fwrite(encbuf, decode(decbuf, cnt, encbuf, outputsize), 1, fpo) <= 0) { perror("Write file error: "); - return; + return 4; } } /* 由操作系统负责释放资源 @@ -162,18 +163,18 @@ void decode_file(const char* input, const char* output) { int fd = open(input, O_RDONLY); if(fd <= 0) { perror("Open input file error: "); - return; + return 5; } char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0); if(input_file == MAP_FAILED) { perror("Map input file error: "); - return; + return 6; } int outputsize = decode_len(inputsize, 0)+16; int off = skip_offset(input_file); if(fwrite(encbuf, decode(input_file+off, inputsize-off, encbuf, outputsize), 1, fpo) <= 0) { perror("Write file error: "); - return; + return 7; } munmap(input_file, (size_t)inputsize); /* 由操作系统负责释放资源 @@ -182,6 +183,7 @@ void decode_file(const char* input, const char* output) { 以缩短程序运行时间 */ } #endif + return 0; } #ifndef _WIN32 @@ -206,17 +208,18 @@ int main(int argc, char** argv) { #else unsigned long t = get_start_ms(); #endif + int exitstat = 0; switch(argv[1][1]) { - case 'e': encode_file(argv[2], argv[3]); break; - case 'd': decode_file(argv[2], argv[3]); break; + case 'e': exitstat = encode_file(argv[2], argv[3]); break; + case 'd': exitstat = decode_file(argv[2], argv[3]); break; default: break; } - if(*(uint16_t*)(argv[3]) != *(uint16_t*)"-") { + if(!exitstat && *(uint16_t*)(argv[3]) != *(uint16_t*)"-") { #ifdef _WIN32 printf("spend time: %lums\n", clock() - t); #else printf("spend time: %lums\n", get_start_ms() - t); #endif } - return 0; + return exitstat; }