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

new no-malloc api

This commit is contained in:
源文雨
2022-03-19 19:26:25 +08:00
parent 8d490096fd
commit c65a2342e8
11 changed files with 225 additions and 212 deletions

View File

@@ -1,38 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#ifdef __WINNT__
#include <windows.h>
#endif
#include "base16384.h"
#include "base14.h"
static off_t get_file_size(const char* filepath) {
struct stat statbuf;
return stat(filepath, &statbuf)?-1:statbuf.st_size;
}
void encode_file(const char* input, const char* output) {
off_t inputsize = get_file_size(input);
if(inputsize < 0) {
puts("Get file size error!");
return;
}
FILE* fp = NULL;
fp = fopen(input, "rb");
if(fp) {
FILE* fpo = NULL;
fpo = fopen(output, "wb");
if(fpo) {
uint8_t* bufi = (uint8_t*)malloc(B14BUFSIZ/7*7);
if(bufi) {
int cnt = 0;
fputc(0xFE, fpo);
fputc(0xFF, fpo);
while((cnt = fread(bufi, sizeof(uint8_t), B14BUFSIZ/7*7, fp))) {
LENDAT* ld = encode(bufi, cnt);
if(fwrite(ld->data, ld->len, 1, fpo) <= 0) {
puts("Write file error!");
exit(EXIT_FAILURE);
}
free(ld->data);
free(ld);
}
free(bufi);
} else puts("Allocate input buffer error!");
fclose(fpo);
} else puts("Open output file error!");
if(!fp) {
puts("Open input file error!");
return;
}
FILE* fpo = NULL;
fpo = fopen(output, "wb");
if(!fpo) {
puts("Open output file error!");
return;
}
if(inputsize > BUFSIZ*1024) inputsize = BUFSIZ*1024/7*7; // big file
char* bufi = (char*)malloc(inputsize);
if(!bufi) {
puts("Allocate input buffer error!");
return;
}
int outputsize = encode_len(inputsize)+16;
char* bufo = (char*)malloc(outputsize);
if(!bufo) {
puts("Allocate output buffer error!");
return;
}
size_t cnt = 0;
fputc(0xFE, fpo);
fputc(0xFF, fpo);
while((cnt = fread(bufi, sizeof(char), inputsize, fp))) {
int n = encode(bufi, cnt, bufo, outputsize);
if(fwrite(bufo, n, 1, fpo) <= 0) {
puts("Write file error!");
return;
}
}
/* 由操作系统负责释放资源
free(bufo);
free(bufi);
fclose(fpo);
fclose(fp);
} else puts("Open input file error!");
以缩短程序运行时间 */
}
void rm_head(FILE* fp) {
@@ -51,36 +76,55 @@ static int is_next_end(FILE* fp) {
}
void decode_file(const char* input, const char* output) {
off_t inputsize = get_file_size(input);
if(inputsize < 0) {
puts("Get file size error!");
return;
}
FILE* fp = NULL;
fp = fopen(input, "rb");
if(fp) {
FILE* fpo = NULL;
fpo = fopen(output, "wb");
if(fpo) {
uint8_t* bufi = (uint8_t*)malloc(B14BUFSIZ/8*8 + 2); //+2避免漏检结束偏移标志
if(bufi) {
int cnt = 0;
int end = 0;
rm_head(fp);
while((cnt = fread(bufi, sizeof(uint8_t), B14BUFSIZ/8*8, fp))) {
if((end = is_next_end(fp))) {
bufi[cnt++] = '=';
bufi[cnt++] = end;
}
LENDAT* ld = decode(bufi, cnt);
if(fwrite(ld->data, ld->len, 1, fpo) <= 0) {
puts("Write file error!");
exit(EXIT_FAILURE);
}
free(ld->data);
free(ld);
}
free(bufi);
} else puts("Allocate input buffer error!");
fclose(fpo);
} else puts("Open output file error!");
if(!fp) {
puts("Open input file error!");
return;
}
FILE* fpo = NULL;
fpo = fopen(output, "wb");
if(!fpo) {
puts("Open output file error!");
return;
}
if(inputsize > BUFSIZ*1024) inputsize = BUFSIZ*1024/8*8; // big file
char* bufi = (char*)malloc(inputsize+2); // +2避免漏检结束偏移标志
if(!bufi) {
puts("Allocate input buffer error!");
return;
}
int outputsize = decode_len(inputsize, 0)+16;
char* bufo = (char*)malloc(outputsize);
if(!bufo) {
puts("Allocate output buffer error!");
return;
}
int cnt = 0;
int end = 0;
rm_head(fp);
while((cnt = fread(bufi, sizeof(char), inputsize, fp))) {
if((end = is_next_end(fp))) {
bufi[cnt++] = '=';
bufi[cnt++] = end;
}
int n = decode(bufi, cnt, bufo, outputsize);
if(fwrite(bufo, n, 1, fpo) <= 0) {
puts("Write file error!");
return;
}
}
/* 由操作系统负责释放资源
free(bufo);
free(bufi);
fclose(fpo);
fclose(fp);
} else puts("Open input file error!");
以缩短程序运行时间 */
}
#ifndef __WINNT__
@@ -93,27 +137,26 @@ unsigned long get_start_ms() {
#define CHOICE argv[1][1]
int main(int argc, char** argv) {
if(argc == 4) {
#if defined(__WINNT__)
clock_t t = clock();
#else
unsigned long t = get_start_ms();
#endif
switch(CHOICE){
case 'e': encode_file(argv[2], argv[3]); break;
case 'd': decode_file(argv[2], argv[3]); break;
default: break;
}
#if defined(__WINNT__)
printf("spend time: %lums\n", clock() - t);
#else
printf("spend time: %lums\n", get_start_ms() - t);
#endif
} else {
if(argc != 4) {
fputs("Usage: -[e|d] <inputfile> <outputfile>\n", stderr);
fputs("\t-e encode\n", stderr);
fputs("\t-d decode\n", stderr);
exit(EXIT_FAILURE);
}
#ifdef __WINNT__
clock_t t = clock();
#else
unsigned long t = get_start_ms();
#endif
switch(CHOICE) {
case 'e': encode_file(argv[2], argv[3]); break;
case 'd': decode_file(argv[2], argv[3]); break;
default: break;
}
#ifdef __WINNT__
printf("spend time: %lums\n", clock() - t);
#else
printf("spend time: %lums\n", get_start_ms() - t);
#endif
return 0;
}