1
0
mirror of https://github.com/fumiama/base16384.git synced 2026-06-19 13:20:25 +08:00

add streamed io and edit README

This commit is contained in:
源文雨
2022-04-22 22:51:49 +08:00
parent 4cf01ede2a
commit 76c72b2e90
2 changed files with 52 additions and 40 deletions

File diff suppressed because one or more lines are too long

View File

@@ -25,33 +25,33 @@ char encbuf[BUFSIZ*1024/7*7];
char decbuf[BUFSIZ*1024/8*8+2]; char decbuf[BUFSIZ*1024/8*8+2];
void encode_file(const char* input, const char* output) { void encode_file(const char* input, const char* output) {
off_t inputsize = get_file_size(input); off_t inputsize;
if(inputsize <= 0) { FILE* fp = NULL;
FILE* fpo;
if(*(uint16_t*)input == *(uint16_t*)"-") { // read from stdin
inputsize = 0;
fp = stdin;
} else inputsize = get_file_size(input);
if(inputsize < 0) {
perror("Get file size error: "); perror("Get file size error: ");
return; return;
} }
if(inputsize > (1u<<31)-1) { fpo = (*(uint16_t*)output == *(uint16_t*)"-")?stdout:fopen(output, "wb");
puts("Input file too big!");
return;
}
FILE* fpo = NULL;
fpo = fopen(output, "wb");
if(!fpo) { if(!fpo) {
perror("Fopen output file error: "); perror("Fopen output file error: ");
return; return;
} }
if(inputsize > BUFSIZ*1024) { // big file, use encbuf & fread if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use encbuf & fread
inputsize = BUFSIZ*1024/7*7; inputsize = BUFSIZ*1024/7*7;
#ifdef _WIN32 #ifdef _WIN32
} }
#endif #endif
FILE* fp = NULL; if(!fp) fp = fopen(input, "rb");
fp = fopen(input, "rb");
if(!fp) { if(!fp) {
perror("Fopen input file error: "); perror("Fopen input file error: ");
return; return;
} }
int outputsize = encode_len(inputsize)+16; int outputsize = encode_len(inputsize)+16;
size_t cnt = 0; size_t cnt = 0;
fputc(0xFE, fpo); fputc(0xFE, fpo);
@@ -112,28 +112,28 @@ static int is_next_end(FILE* fp) {
} }
void decode_file(const char* input, const char* output) { void decode_file(const char* input, const char* output) {
off_t inputsize = get_file_size(input); off_t inputsize;
if(inputsize <= 0) { FILE* fp = NULL;
FILE* fpo;
if(*(uint16_t*)input == *(uint16_t*)"-") { // read from stdin
inputsize = 0;
fp = stdin;
} else inputsize = get_file_size(input);
if(inputsize < 0) {
perror("Get file size error: "); perror("Get file size error: ");
return; return;
} }
if(inputsize > (1u<<31)-1) { fpo = (*(uint16_t*)output == *(uint16_t*)"-")?stdout:fopen(output, "wb");
puts("Input file too big!");
return;
}
FILE* fpo = NULL;
fpo = fopen(output, "wb");
if(!fpo) { if(!fpo) {
perror("Fopen output file error: "); perror("Fopen output file error: ");
return; return;
} }
if(inputsize > BUFSIZ*1024) { // big file, use decbuf & fread if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use decbuf & fread
inputsize = BUFSIZ*1024/8*8; inputsize = BUFSIZ*1024/8*8;
#ifdef _WIN32 #ifdef _WIN32
} }
#endif #endif
FILE* fp = NULL; if(!fp) fp = fopen(input, "rb");
fp = fopen(input, "rb");
if(!fp) { if(!fp) {
perror("Fopen input file error: "); perror("Fopen input file error: ");
return; return;
@@ -191,28 +191,31 @@ unsigned long get_start_ms() {
} }
#endif #endif
#define CHOICE argv[1][1]
int main(int argc, char** argv) { int main(int argc, char** argv) {
if(argc != 4) { if(argc != 4) {
fputs("Usage: -[e|d] <inputfile> <outputfile>\n", stderr); puts("Usage: -[e|d] <inputfile> <outputfile>");
fputs("\t-e encode\n", stderr); puts("\t-e encode");
fputs("\t-d decode\n", stderr); puts("\t-d decode");
exit(EXIT_FAILURE); puts("\t<inputfile> pass - to read from stdin");
puts("\t<outputfile> pass - to write to stdout");
exit(EXIT_SUCCESS);
} }
#ifdef _WIN32 #ifdef _WIN32
clock_t t = clock(); clock_t t = clock();
#else #else
unsigned long t = get_start_ms(); unsigned long t = get_start_ms();
#endif #endif
switch(CHOICE) { switch(argv[1][1]) {
case 'e': encode_file(argv[2], argv[3]); break; case 'e': encode_file(argv[2], argv[3]); break;
case 'd': decode_file(argv[2], argv[3]); break; case 'd': decode_file(argv[2], argv[3]); break;
default: break; default: break;
} }
#ifdef _WIN32 if(*(uint16_t*)(argv[3]) != *(uint16_t*)"-") {
printf("spend time: %lums\n", clock() - t); #ifdef _WIN32
#else printf("spend time: %lums\n", clock() - t);
printf("spend time: %lums\n", get_start_ms() - t); #else
#endif printf("spend time: %lums\n", get_start_ms() - t);
#endif
}
return 0; return 0;
} }