1
0
mirror of https://github.com/fumiama/base16384.git synced 2026-06-23 17:10:18 +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,10 +0,0 @@
cmake_minimum_required(VERSION 3.0.0)
project(base1432 VERSION 2.0)
SET(CMAKE_BUILD_TYPE "Release")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
add_library(base1432 STATIC base14.c)
add_library(base14 SHARED base14.c)
INSTALL(TARGETS base14 LIBRARY DESTINATION lib)
INSTALL(FILES base14.h DESTINATION include)

View File

@@ -1,13 +0,0 @@
//base1432le.h
//fumiama 20210408
#include <stdint.h>
#define B14BUFSIZ 1024*1024 // 1M
struct LENDAT {
uint8_t* data;
int32_t len;
};
typedef struct LENDAT LENDAT;
LENDAT* encode(const uint8_t* data, const int32_t len);
LENDAT* decode(const uint8_t* data, const int32_t len);

View File

@@ -1,10 +0,0 @@
cmake_minimum_required(VERSION 3.0.0)
project(base1464 VERSION 2.0)
SET(CMAKE_BUILD_TYPE "Release")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
add_library(base1464 STATIC base14.c)
add_library(base14 SHARED base14.c)
INSTALL(TARGETS base14 LIBRARY DESTINATION lib)
INSTALL(FILES base14.h DESTINATION include)

View File

@@ -1,13 +0,0 @@
//base1464le.h
//fumiama 20210408
#include <stdint.h>
#define B14BUFSIZ 1024*1024 // 1M
struct LENDAT {
uint8_t* data;
int64_t len;
};
typedef struct LENDAT LENDAT;
LENDAT* encode(const uint8_t* data, const int64_t len);
LENDAT* decode(const uint8_t* data, const int64_t len);

View File

@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.0.0) cmake_minimum_required(VERSION 3.0.0)
project(base16384 VERSION 2.0) project(base16384 VERSION 2.0)
SET(CMAKE_BUILD_TYPE "Release") SET(CMAKE_BUILD_TYPE "Release")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
add_executable(base16384 base16384.c) add_executable(base16384 base16384.c)
@@ -11,13 +12,15 @@ if (${isBigEndian})
endif() endif()
IF(CMAKE_SIZEOF_VOID_P EQUAL 8) IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
add_definitions("-DCPUBIT64") add_library(b1464 STATIC base1464.c base14.c)
add_subdirectory("./64") add_library(b14 SHARED base1464.c base14.c)
target_link_libraries(base16384 base1464) target_link_libraries(base16384 b1464)
ELSE() ELSE()
add_definitions("-DCPUBIT32") add_library(b1432 STATIC base1432.c base14.c)
add_subdirectory("./32") add_library(b14 SHARED base1432.c base14.c)
target_link_libraries(base16384 base1432) target_link_libraries(base16384 b1432)
ENDIF() ENDIF()
INSTALL(TARGETS base16384 RUNTIME DESTINATION bin) INSTALL(TARGETS base16384 RUNTIME DESTINATION bin)
INSTALL(TARGETS b14 LIBRARY DESTINATION lib)
INSTALL(FILES base14.h DESTINATION include)

32
base14.c Normal file
View File

@@ -0,0 +1,32 @@
#include "base14.h"
int encode_len(int dlen) {
int outlen = dlen / 7 * 8;
int offset = dlen % 7;
switch(offset) { // 算上偏移标志字符占用的2字节
case 0: break;
case 1: outlen += 4; break;
case 2:
case 3: outlen += 6; break;
case 4:
case 5: outlen += 8; break;
case 6: outlen += 10; break;
default: break;
}
return outlen + 8; // 冗余的8B用于可能的结尾的覆盖
}
int decode_len(int dlen, int offset) {
int outlen = dlen;
switch(offset) { // 算上偏移标志字符占用的2字节
case 0: break;
case 1: outlen -= 4; break;
case 2:
case 3: outlen -= 6; break;
case 4:
case 5: outlen -= 8; break;
case 6: outlen -= 10; break;
default: break;
}
return outlen / 8 * 7 + offset + 1; // 多出1字节用于循环覆盖
}

12
base14.h Normal file
View File

@@ -0,0 +1,12 @@
// base14.h
// fumiama 20220319
// encode_len calc min buf size to fill encode result
int encode_len(int dlen);
// decode_len calc min buf size to fill decode result
int decode_len(int dlen, int offset);
// encode data and write result into buf
int encode(const char* data, int dlen, char* buf, int blen);
// decode data and write result into buf
int decode(const char* data, int dlen, char* buf, int blen);

View File

@@ -1,5 +1,5 @@
//base1432le.c // base1432.c
//fumiama 20210408 // fumiama 20220319
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef __linux__ #ifdef __linux__
@@ -37,17 +37,12 @@
#endif #endif
#include "base14.h" #include "base14.h"
//#define DEBUG // #define DEBUG
#ifndef new int encode(const char* data, int dlen, char* buf, int blen) {
#define new malloc int outlen = dlen / 7 * 8;
#endif int offset = dlen % 7;
switch(offset) { // 算上偏移标志字符占用的2字节
LENDAT* encode(const uint8_t* data, const int32_t len) {
LENDAT* encd = (LENDAT*)new(sizeof(LENDAT));
int32_t outlen = len / 7 * 8;
uint8_t offset = len % 7;
switch(offset) { //算上偏移标志字符占用的2字节
case 0: break; case 0: break;
case 1: outlen += 4; break; case 1: outlen += 4; break;
case 2: case 2:
@@ -60,12 +55,10 @@ LENDAT* encode(const uint8_t* data, const int32_t len) {
#ifdef DEBUG #ifdef DEBUG
printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8); printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8);
#endif #endif
encd->data = (uint8_t*)new(outlen + 8); //冗余的8B用于可能的结尾的覆盖 uint32_t* vals = (uint32_t*)buf;
encd->len = outlen;
uint32_t* vals = (uint32_t*)(encd->data);
uint32_t n = 0; uint32_t n = 0;
int32_t i = 0; int32_t i = 0;
for(; i <= len - 7; i += 7) { for(; i <= dlen - 7; i += 7) {
register uint32_t sum = 0; register uint32_t sum = 0;
register uint32_t shift = htobe32(*(uint32_t*)(data+i)); register uint32_t shift = htobe32(*(uint32_t*)(data+i));
sum |= (shift>>2) & 0x3fff0000; sum |= (shift>>2) & 0x3fff0000;
@@ -118,19 +111,18 @@ LENDAT* encode(const uint8_t* data, const int32_t len) {
#else #else
vals[n] = sum; vals[n] = sum;
#endif #endif
encd->data[outlen - 2] = '='; buf[outlen - 2] = '=';
encd->data[outlen - 1] = offset; buf[outlen - 1] = offset;
} }
return encd; return outlen;
} }
LENDAT* decode(const uint8_t* data, const int32_t len) { int decode(const char* data, int dlen, char* buf, int blen) {
LENDAT* decd = (LENDAT*)new(sizeof(LENDAT)); int outlen = dlen;
int32_t outlen = len; int offset = 0;
uint8_t offset = 0; if(data[dlen-2] == '=') {
if(data[len-2] == '=') { offset = data[dlen-1];
offset = data[len-1]; switch(offset) { // 算上偏移标志字符占用的2字节
switch(offset) { //算上偏移标志字符占用的2字节
case 0: break; case 0: break;
case 1: outlen -= 4; break; case 1: outlen -= 4; break;
case 2: case 2:
@@ -142,12 +134,10 @@ LENDAT* decode(const uint8_t* data, const int32_t len) {
} }
} }
outlen = outlen / 8 * 7 + offset; outlen = outlen / 8 * 7 + offset;
decd->data = (uint8_t*)new(outlen+1); //多出1字节用于循环覆盖
decd->len = outlen;
uint32_t* vals = (uint32_t*)data; uint32_t* vals = (uint32_t*)data;
uint32_t n = 0; uint32_t n = 0;
int32_t i = 0; int32_t i = 0;
for(; i <= outlen - 7; i+=7) { //n实际每次自增2 for(; i <= outlen - 7; i+=7) { // n实际每次自增2
register uint32_t sum = 0; register uint32_t sum = 0;
register uint32_t shift = htobe32(vals[n++]) - 0x4e004e00; register uint32_t shift = htobe32(vals[n++]) - 0x4e004e00;
shift <<= 2; shift <<= 2;
@@ -156,44 +146,44 @@ LENDAT* decode(const uint8_t* data, const int32_t len) {
sum |= shift & 0x0003fff0; sum |= shift & 0x0003fff0;
shift = htobe32(vals[n++]) - 0x4e004e00; shift = htobe32(vals[n++]) - 0x4e004e00;
sum |= shift >> 26; sum |= shift >> 26;
*(uint32_t*)(decd->data+i) = be32toh(sum); *(uint32_t*)(buf+i) = be32toh(sum);
sum = 0; sum = 0;
shift <<= 6; shift <<= 6;
sum |= shift & 0xffc00000; sum |= shift & 0xffc00000;
shift <<= 2; shift <<= 2;
sum |= shift & 0x003fff00; sum |= shift & 0x003fff00;
*(uint32_t*)(decd->data+i+4) = be32toh(sum); *(uint32_t*)(buf+i+4) = be32toh(sum);
} }
if(offset--) { if(offset--) {
//这里有读取越界 // 这里有读取越界
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
register uint32_t sum = __builtin_bswap32(vals[n++]); register uint32_t sum = __builtin_bswap32(vals[n++]);
#else #else
register uint32_t sum = vals[n++]; register uint32_t sum = vals[n++];
#endif #endif
sum -= 0x0000004e; sum -= 0x0000004e;
decd->data[i++] = ((sum & 0x0000003f) << 2) | ((sum & 0x0000c000) >> 14); buf[i++] = ((sum & 0x0000003f) << 2) | ((sum & 0x0000c000) >> 14);
if(offset--) { if(offset--) {
sum -= 0x004e0000; sum -= 0x004e0000;
decd->data[i++] = ((sum & 0x00003f00) >> 6) | ((sum & 0x00300000) >> 20); buf[i++] = ((sum & 0x00003f00) >> 6) | ((sum & 0x00300000) >> 20);
if(offset--) { if(offset--) {
decd->data[i++] = ((sum & 0x000f0000) >> 12) | ((sum & 0xf0000000) >> 28); buf[i++] = ((sum & 0x000f0000) >> 12) | ((sum & 0xf0000000) >> 28);
if(offset--) { if(offset--) {
decd->data[i++] = (sum & 0x0f000000) >> 20; buf[i++] = (sum & 0x0f000000) >> 20;
//这里有读取越界 // 这里有读取越界
sum = vals[n]; sum = vals[n];
sum -= 0x0000004e; sum -= 0x0000004e;
decd->data[i++] |= (sum & 0x0000003c) >> 2; buf[i++] |= (sum & 0x0000003c) >> 2;
if(offset--) { if(offset--) {
decd->data[i++] = ((sum & 0x00000003) << 6) | ((sum & 0x0000fc00) >> 10); buf[i++] = ((sum & 0x00000003) << 6) | ((sum & 0x0000fc00) >> 10);
if(offset--) { if(offset--) {
sum -= 0x004e0000; sum -= 0x004e0000;
decd->data[i] = ((sum & 0x00000300) >> 2) | ((sum & 0x003f0000) >> 16); buf[i] = ((sum & 0x00000300) >> 2) | ((sum & 0x003f0000) >> 16);
} }
} }
} }
} }
} }
} }
return decd; return outlen;
} }

View File

@@ -1,5 +1,5 @@
//base14.c // base1464.c
//fumiama 20211029 // fumiama 20211029
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef __linux__ #ifdef __linux__
@@ -44,17 +44,12 @@
#endif #endif
#include "base14.h" #include "base14.h"
//#define DEBUG // #define DEBUG
#ifndef new int encode(const char* data, int dlen, char* buf, int blen) {
#define new malloc int outlen = dlen / 7 * 8;
#endif int offset = dlen % 7;
switch(offset) { // 算上偏移标志字符占用的2字节
LENDAT* encode(const uint8_t* data, const int64_t len) {
LENDAT* encd = (LENDAT*)new(sizeof(LENDAT));
int64_t outlen = len / 7 * 8;
uint8_t offset = len % 7;
switch(offset) { //算上偏移标志字符占用的2字节
case 0: break; case 0: break;
case 1: outlen += 4; break; case 1: outlen += 4; break;
case 2: case 2:
@@ -67,14 +62,12 @@ LENDAT* encode(const uint8_t* data, const int64_t len) {
#ifdef DEBUG #ifdef DEBUG
printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8); printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8);
#endif #endif
encd->data = (uint8_t*)new(outlen + 8); //冗余的8B用于可能的结尾的覆盖 uint64_t* vals = (uint64_t*)buf;
encd->len = outlen;
uint64_t* vals = (uint64_t*)(encd->data);
uint64_t n = 0; uint64_t n = 0;
int64_t i = 0; int64_t i = 0;
for(; i <= len - 7; i += 7) { for(; i <= dlen - 7; i += 7) {
register uint64_t sum = 0; register uint64_t sum = 0;
register uint64_t shift = htobe64(*(uint64_t*)(data+i))>>2; //这里有读取越界 register uint64_t shift = htobe64(*(uint64_t*)(data+i))>>2; // 这里有读取越界
sum |= shift & 0x3fff000000000000; sum |= shift & 0x3fff000000000000;
shift >>= 2; shift >>= 2;
sum |= shift & 0x00003fff00000000; sum |= shift & 0x00003fff00000000;
@@ -88,7 +81,7 @@ LENDAT* encode(const uint8_t* data, const int64_t len) {
printf("i: %llu, add sum: %016llx\n", i, sum); printf("i: %llu, add sum: %016llx\n", i, sum);
#endif #endif
} }
uint8_t o = offset; int o = offset;
if(o--) { if(o--) {
register uint64_t sum = 0x000000000000003f & (data[i] >> 2); register uint64_t sum = 0x000000000000003f & (data[i] >> 2);
sum |= ((uint64_t)data[i] << 14) & 0x000000000000c000; sum |= ((uint64_t)data[i] << 14) & 0x000000000000c000;
@@ -121,19 +114,18 @@ LENDAT* encode(const uint8_t* data, const int64_t len) {
#ifdef DEBUG #ifdef DEBUG
printf("i: %llu, add sum: %016llx\n", i, sum); printf("i: %llu, add sum: %016llx\n", i, sum);
#endif #endif
encd->data[outlen - 2] = '='; buf[outlen - 2] = '=';
encd->data[outlen - 1] = offset; buf[outlen - 1] = offset;
} }
return encd; return outlen;
} }
LENDAT* decode(const uint8_t* data, const int64_t len) { int decode(const char* data, int dlen, char* buf, int blen) {
LENDAT* decd = (LENDAT*)new(sizeof(LENDAT)); int outlen = dlen;
int64_t outlen = len; int offset = 0;
uint8_t offset = 0; if(data[dlen-2] == '=') {
if(data[len-2] == '=') { offset = data[dlen-1];
offset = data[len-1]; switch(offset) { // 算上偏移标志字符占用的2字节
switch(offset) { //算上偏移标志字符占用的2字节
case 0: break; case 0: break;
case 1: outlen -= 4; break; case 1: outlen -= 4; break;
case 2: case 2:
@@ -145,8 +137,6 @@ LENDAT* decode(const uint8_t* data, const int64_t len) {
} }
} }
outlen = outlen / 8 * 7 + offset; outlen = outlen / 8 * 7 + offset;
decd->data = (uint8_t*)new(outlen+1); //多出1字节用于循环覆盖
decd->len = outlen;
uint64_t* vals = (uint64_t*)data; uint64_t* vals = (uint64_t*)data;
uint64_t n = 0; uint64_t n = 0;
int64_t i = 0; int64_t i = 0;
@@ -161,37 +151,37 @@ LENDAT* decode(const uint8_t* data, const int64_t len) {
sum |= shift & 0x0000000fffc00000; sum |= shift & 0x0000000fffc00000;
shift <<= 2; shift <<= 2;
sum |= shift & 0x00000000003fff00; sum |= shift & 0x00000000003fff00;
*(uint64_t*)(decd->data+i) = be64toh(sum); *(uint64_t*)(buf+i) = be64toh(sum);
#ifdef DEBUG #ifdef DEBUG
printf("i: %llu, add sum: %016llx\n", i, sum); printf("i: %llu, add sum: %016llx\n", i, sum);
#endif #endif
} }
if(offset--) { if(offset--) {
//这里有读取越界 // 这里有读取越界
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
register uint64_t sum = __builtin_bswap64(vals[n]) - 0x000000000000004e; register uint64_t sum = __builtin_bswap64(vals[n]) - 0x000000000000004e;
#else #else
register uint64_t sum = vals[n] - 0x000000000000004e; register uint64_t sum = vals[n] - 0x000000000000004e;
#endif #endif
decd->data[i++] = ((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14); buf[i++] = ((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14);
if(offset--) { if(offset--) {
sum -= 0x00000000004e0000; sum -= 0x00000000004e0000;
decd->data[i++] = ((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20); buf[i++] = ((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20);
if(offset--) { if(offset--) {
decd->data[i++] = ((sum & 0x00000000000f0000) >> 12) | ((sum & 0x00000000f0000000) >> 28); buf[i++] = ((sum & 0x00000000000f0000) >> 12) | ((sum & 0x00000000f0000000) >> 28);
if(offset--) { if(offset--) {
sum -= 0x0000004e00000000; sum -= 0x0000004e00000000;
decd->data[i++] = ((sum & 0x000000000f000000) >> 20) | ((sum & 0x0000003c00000000) >> 34); buf[i++] = ((sum & 0x000000000f000000) >> 20) | ((sum & 0x0000003c00000000) >> 34);
if(offset--) { if(offset--) {
decd->data[i++] = ((sum & 0x0000000300000000) >> 26) | ((sum & 0x0000fc0000000000) >> 42); buf[i++] = ((sum & 0x0000000300000000) >> 26) | ((sum & 0x0000fc0000000000) >> 42);
if(offset--) { if(offset--) {
sum -= 0x004e000000000000; sum -= 0x004e000000000000;
decd->data[i] = ((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48); buf[i] = ((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48);
} }
} }
} }
} }
} }
} }
return decd; return outlen;
} }

View File

@@ -1,38 +1,63 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#include <time.h> #include <time.h>
#ifdef __WINNT__ #ifdef __WINNT__
#include <windows.h> #include <windows.h>
#endif #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) { 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; FILE* fp = NULL;
fp = fopen(input, "rb"); fp = fopen(input, "rb");
if(fp) { if(!fp) {
FILE* fpo = NULL; puts("Open input file error!");
fpo = fopen(output, "wb"); return;
if(fpo) { }
uint8_t* bufi = (uint8_t*)malloc(B14BUFSIZ/7*7); FILE* fpo = NULL;
if(bufi) { fpo = fopen(output, "wb");
int cnt = 0; if(!fpo) {
fputc(0xFE, fpo); puts("Open output file error!");
fputc(0xFF, fpo); return;
while((cnt = fread(bufi, sizeof(uint8_t), B14BUFSIZ/7*7, fp))) { }
LENDAT* ld = encode(bufi, cnt); if(inputsize > BUFSIZ*1024) inputsize = BUFSIZ*1024/7*7; // big file
if(fwrite(ld->data, ld->len, 1, fpo) <= 0) { char* bufi = (char*)malloc(inputsize);
puts("Write file error!"); if(!bufi) {
exit(EXIT_FAILURE); puts("Allocate input buffer error!");
} return;
free(ld->data); }
free(ld); int outputsize = encode_len(inputsize)+16;
} char* bufo = (char*)malloc(outputsize);
free(bufi); if(!bufo) {
} else puts("Allocate input buffer error!"); puts("Allocate output buffer error!");
fclose(fpo); return;
} else puts("Open output file error!"); }
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); fclose(fp);
} else puts("Open input file error!"); 以缩短程序运行时间 */
} }
void rm_head(FILE* fp) { 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) { 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; FILE* fp = NULL;
fp = fopen(input, "rb"); fp = fopen(input, "rb");
if(fp) { if(!fp) {
FILE* fpo = NULL; puts("Open input file error!");
fpo = fopen(output, "wb"); return;
if(fpo) { }
uint8_t* bufi = (uint8_t*)malloc(B14BUFSIZ/8*8 + 2); //+2避免漏检结束偏移标志 FILE* fpo = NULL;
if(bufi) { fpo = fopen(output, "wb");
int cnt = 0; if(!fpo) {
int end = 0; puts("Open output file error!");
rm_head(fp); return;
while((cnt = fread(bufi, sizeof(uint8_t), B14BUFSIZ/8*8, fp))) { }
if((end = is_next_end(fp))) { if(inputsize > BUFSIZ*1024) inputsize = BUFSIZ*1024/8*8; // big file
bufi[cnt++] = '='; char* bufi = (char*)malloc(inputsize+2); // +2避免漏检结束偏移标志
bufi[cnt++] = end; if(!bufi) {
} puts("Allocate input buffer error!");
LENDAT* ld = decode(bufi, cnt); return;
if(fwrite(ld->data, ld->len, 1, fpo) <= 0) { }
puts("Write file error!"); int outputsize = decode_len(inputsize, 0)+16;
exit(EXIT_FAILURE); char* bufo = (char*)malloc(outputsize);
} if(!bufo) {
free(ld->data); puts("Allocate output buffer error!");
free(ld); return;
} }
free(bufi); int cnt = 0;
} else puts("Allocate input buffer error!"); int end = 0;
fclose(fpo); rm_head(fp);
} else puts("Open output file error!"); 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); fclose(fp);
} else puts("Open input file error!"); 以缩短程序运行时间 */
} }
#ifndef __WINNT__ #ifndef __WINNT__
@@ -93,27 +137,26 @@ unsigned long get_start_ms() {
#define CHOICE argv[1][1] #define CHOICE argv[1][1]
int main(int argc, char** argv) { int main(int argc, char** argv) {
if(argc == 4) { 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 {
fputs("Usage: -[e|d] <inputfile> <outputfile>\n", stderr); fputs("Usage: -[e|d] <inputfile> <outputfile>\n", stderr);
fputs("\t-e encode\n", stderr); fputs("\t-e encode\n", stderr);
fputs("\t-d decode\n", stderr); fputs("\t-d decode\n", stderr);
exit(EXIT_FAILURE); 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; return 0;
} }

View File

@@ -1,11 +0,0 @@
#ifndef CPUBIT32
#ifndef CPUBIT64
#define CPUBIT32
#endif
#endif
#ifdef CPUBIT32
#include "./32/base14.h"
#endif
#ifdef CPUBIT64
#include "./64/base14.h"
#endif