mirror of
https://github.com/fumiama/base16384.git
synced 2026-06-06 10:40:30 +08:00
@@ -1,6 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.0.0)
|
||||
project(base16384 VERSION 2.2.0)
|
||||
SET(CMAKE_BUILD_TYPE "Release")
|
||||
project(base16384 VERSION 2.2.2)
|
||||
|
||||
add_executable(base16384_b base16384.c)
|
||||
|
||||
@@ -18,14 +17,6 @@ ELSE()
|
||||
add_library(base16384_s STATIC file.c base1432.c)
|
||||
ENDIF()
|
||||
|
||||
if (MSVC)
|
||||
# do nothing
|
||||
else()
|
||||
# add hardening
|
||||
target_compile_options(base16384 PUBLIC -Wl,-z,now -Wdate-time)
|
||||
add_definitions(-D_FORTIFY_SOURCE=2)
|
||||
endif()
|
||||
|
||||
set_target_properties(base16384_b PROPERTIES OUTPUT_NAME base16384)
|
||||
set_target_properties(base16384_s PROPERTIES OUTPUT_NAME base16384)
|
||||
set_target_properties(base16384 PROPERTIES VERSION ${CMAKE_PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
# define be16toh(x) betoh16(x)
|
||||
# define be32toh(x) betoh32(x)
|
||||
#endif
|
||||
#ifdef __MAC_10_0
|
||||
#ifdef __APPLE__
|
||||
# define be16toh(x) ntohs(x)
|
||||
# define be32toh(x) ntohl(x)
|
||||
# define htobe16(x) ntohs(x)
|
||||
@@ -192,7 +192,7 @@ int base16384_decode(const char* data, int dlen, char* buf, int blen) {
|
||||
if(offset--) {
|
||||
buf[i++] = ((sum & 0x000f0000) >> 12) | ((sum & 0xf0000000) >> 28);
|
||||
if(offset--) {
|
||||
buf[i++] = (sum & 0x0f000000) >> 20;
|
||||
buf[i] = (sum & 0x0f000000) >> 20;
|
||||
// 这里有读取越界
|
||||
sum = vals[n];
|
||||
sum -= 0x0000004e;
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
# define be32toh(x) betoh32(x)
|
||||
# define be64toh(x) betoh64(x)
|
||||
#endif
|
||||
#ifdef __MAC_10_0
|
||||
#ifdef __APPLE__
|
||||
# define be16toh(x) ntohs(x)
|
||||
# define be32toh(x) ntohl(x)
|
||||
# define be64toh(x) ntohll(x)
|
||||
|
||||
@@ -39,7 +39,7 @@ unsigned long get_start_ms() {
|
||||
#endif
|
||||
|
||||
static void print_usage() {
|
||||
puts("Copyright (c) 2022 Fumiama Minamoto.\nBase16384 2.2.0 (Oct 16th 2022). Usage:");
|
||||
puts("Copyright (c) 2022 Fumiama Minamoto.\nBase16384 2.2.2 (Dec 14th 2022). Usage:");
|
||||
puts("base16384 [-edt] [inputfile] [outputfile]");
|
||||
puts(" -e\t\tencode");
|
||||
puts(" -d\t\tdecode");
|
||||
|
||||
28
file.c
28
file.c
@@ -42,18 +42,20 @@ static inline off_t get_file_size(const char* filepath) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#define is_standard_io(filename) (*(uint16_t*)(filename) == *(uint16_t*)"-")
|
||||
|
||||
base16384_err_t base16384_encode_file(const char* input, const char* output, char* encbuf, char* decbuf) {
|
||||
off_t inputsize;
|
||||
FILE* fp = NULL;
|
||||
FILE* fpo;
|
||||
if(*(uint16_t*)input == *(uint16_t*)"-") { // read from stdin
|
||||
if(is_standard_io(input)) { // read from stdin
|
||||
inputsize = 0;
|
||||
fp = stdin;
|
||||
} else inputsize = get_file_size(input);
|
||||
if(inputsize < 0) {
|
||||
return base16384_err_get_file_size;
|
||||
}
|
||||
fpo = (*(uint16_t*)output == *(uint16_t*)"-")?stdout:fopen(output, "wb");
|
||||
fpo = is_standard_io(output)?stdout:fopen(output, "wb");
|
||||
if(!fpo) {
|
||||
return base16384_err_fopen_output_file;
|
||||
}
|
||||
@@ -77,12 +79,12 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha
|
||||
return base16384_err_write_file;
|
||||
}
|
||||
}
|
||||
fclose(fpo);
|
||||
fclose(fp);
|
||||
if(!is_standard_io(output)) fclose(fpo);
|
||||
if(!is_standard_io(input)) fclose(fp);
|
||||
#ifndef _WIN32
|
||||
} else { // small file, use mmap & fwrite
|
||||
int fd = open(input, O_RDONLY);
|
||||
if(fd <= 0) {
|
||||
if(fd < 0) {
|
||||
return base16384_err_open_input_file;
|
||||
}
|
||||
char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
@@ -97,7 +99,7 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha
|
||||
return base16384_err_write_file;
|
||||
}
|
||||
munmap(input_file, (size_t)inputsize);
|
||||
fclose(fpo);
|
||||
if(!is_standard_io(output)) fclose(fpo);
|
||||
close(fd);
|
||||
}
|
||||
#endif
|
||||
@@ -148,7 +150,7 @@ base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* d
|
||||
#define rm_head(fp) {\
|
||||
int ch = fgetc(fp);\
|
||||
if(ch == 0xFE) fgetc(fp);\
|
||||
else rewind(fp);\
|
||||
else ungetc(ch, fp);\
|
||||
}
|
||||
|
||||
#define skip_offset(input_file) ((input_file[0]==(char)0xFE)?2:0)
|
||||
@@ -165,14 +167,14 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha
|
||||
off_t inputsize;
|
||||
FILE* fp = NULL;
|
||||
FILE* fpo;
|
||||
if(*(uint16_t*)input == *(uint16_t*)"-") { // read from stdin
|
||||
if(is_standard_io(input)) { // read from stdin
|
||||
inputsize = 0;
|
||||
fp = stdin;
|
||||
} else inputsize = get_file_size(input);
|
||||
if(inputsize < 0) {
|
||||
return base16384_err_get_file_size;
|
||||
}
|
||||
fpo = (*(uint16_t*)output == *(uint16_t*)"-")?stdout:fopen(output, "wb");
|
||||
fpo = is_standard_io(output)?stdout:fopen(output, "wb");
|
||||
if(!fpo) {
|
||||
return base16384_err_fopen_output_file;
|
||||
}
|
||||
@@ -198,12 +200,12 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha
|
||||
return base16384_err_write_file;
|
||||
}
|
||||
}
|
||||
fclose(fpo);
|
||||
fclose(fp);
|
||||
if(!is_standard_io(output)) fclose(fpo);
|
||||
if(!is_standard_io(input)) fclose(fp);
|
||||
#ifndef _WIN32
|
||||
} else { // small file, use mmap & fwrite
|
||||
int fd = open(input, O_RDONLY);
|
||||
if(fd <= 0) {
|
||||
if(fd < 0) {
|
||||
return base16384_err_open_input_file;
|
||||
}
|
||||
char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
@@ -216,7 +218,7 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha
|
||||
return base16384_err_write_file;
|
||||
}
|
||||
munmap(input_file, (size_t)inputsize);
|
||||
fclose(fpo);
|
||||
if(!is_standard_io(output)) fclose(fpo);
|
||||
close(fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user