mirror of
https://github.com/fumiama/base16384.git
synced 2026-06-18 20:50:23 +08:00
fix: data mismatch on stream decode with BOM
This commit is contained in:
@@ -76,21 +76,21 @@ base16384 [-edt] [inputfile] [outputfile]
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Examples 用例
|
## Examples 用例
|
||||||
1. Encode simple text
|
1. Encode simple text 简单文本编码
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo -n "1234567" | base16384 -e - - | iconv -f utf-16be -t utf-8
|
echo -n "1234567" | base16384 -e - - | iconv -f utf-16be -t utf-8
|
||||||
婌焳廔萷
|
婌焳廔萷
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Decode simple text
|
3. Decode simple text 简单文本解码
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo -n "婌焳廔萷" | iconv -f utf-8 -t utf-16be | ./base16384 -d - -
|
echo -n "婌焳廔萷" | iconv -f utf-8 -t utf-16be | ./base16384 -d - -
|
||||||
1234567
|
1234567
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Encode file
|
3. Encode file 编码文件
|
||||||
|
|
||||||
The text below is the encoding of the base16384 itself on MacOS 12.6 arm64. It is clear to see the strucutre of the binary file.
|
The text below is the encoding of the base16384 itself on MacOS 12.6 arm64. It is clear to see the strucutre of the binary file.
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
# define be16toh(x) betoh16(x)
|
# define be16toh(x) betoh16(x)
|
||||||
# define be32toh(x) betoh32(x)
|
# define be32toh(x) betoh32(x)
|
||||||
#endif
|
#endif
|
||||||
#ifdef __MAC_10_0
|
#ifdef __APPLE__
|
||||||
# define be16toh(x) ntohs(x)
|
# define be16toh(x) ntohs(x)
|
||||||
# define be32toh(x) ntohl(x)
|
# define be32toh(x) ntohl(x)
|
||||||
# define htobe16(x) ntohs(x)
|
# define htobe16(x) ntohs(x)
|
||||||
|
|||||||
@@ -34,7 +34,7 @@
|
|||||||
# define be32toh(x) betoh32(x)
|
# define be32toh(x) betoh32(x)
|
||||||
# define be64toh(x) betoh64(x)
|
# define be64toh(x) betoh64(x)
|
||||||
#endif
|
#endif
|
||||||
#ifdef __MAC_10_0
|
#ifdef __APPLE__
|
||||||
# define be16toh(x) ntohs(x)
|
# define be16toh(x) ntohs(x)
|
||||||
# define be32toh(x) ntohl(x)
|
# define be32toh(x) ntohl(x)
|
||||||
# define be64toh(x) ntohll(x)
|
# define be64toh(x) ntohll(x)
|
||||||
|
|||||||
28
file.c
28
file.c
@@ -42,18 +42,20 @@ static inline off_t get_file_size(const char* filepath) {
|
|||||||
}
|
}
|
||||||
#endif
|
#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) {
|
base16384_err_t base16384_encode_file(const char* input, const char* output, char* encbuf, char* decbuf) {
|
||||||
off_t inputsize;
|
off_t inputsize;
|
||||||
FILE* fp = NULL;
|
FILE* fp = NULL;
|
||||||
FILE* fpo;
|
FILE* fpo;
|
||||||
if(*(uint16_t*)input == *(uint16_t*)"-") { // read from stdin
|
if(is_standard_io(input)) { // read from stdin
|
||||||
inputsize = 0;
|
inputsize = 0;
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else inputsize = get_file_size(input);
|
} else inputsize = get_file_size(input);
|
||||||
if(inputsize < 0) {
|
if(inputsize < 0) {
|
||||||
return base16384_err_get_file_size;
|
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) {
|
if(!fpo) {
|
||||||
return base16384_err_fopen_output_file;
|
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;
|
return base16384_err_write_file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fpo);
|
if(!is_standard_io(output)) fclose(fpo);
|
||||||
fclose(fp);
|
if(!is_standard_io(input)) fclose(fp);
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
} else { // small file, use mmap & fwrite
|
} else { // small file, use mmap & fwrite
|
||||||
int fd = open(input, O_RDONLY);
|
int fd = open(input, O_RDONLY);
|
||||||
if(fd <= 0) {
|
if(fd < 0) {
|
||||||
return base16384_err_open_input_file;
|
return base16384_err_open_input_file;
|
||||||
}
|
}
|
||||||
char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0);
|
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;
|
return base16384_err_write_file;
|
||||||
}
|
}
|
||||||
munmap(input_file, (size_t)inputsize);
|
munmap(input_file, (size_t)inputsize);
|
||||||
fclose(fpo);
|
if(!is_standard_io(output)) fclose(fpo);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -148,7 +150,7 @@ base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* d
|
|||||||
#define rm_head(fp) {\
|
#define rm_head(fp) {\
|
||||||
int ch = fgetc(fp);\
|
int ch = fgetc(fp);\
|
||||||
if(ch == 0xFE) 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)
|
#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;
|
off_t inputsize;
|
||||||
FILE* fp = NULL;
|
FILE* fp = NULL;
|
||||||
FILE* fpo;
|
FILE* fpo;
|
||||||
if(*(uint16_t*)input == *(uint16_t*)"-") { // read from stdin
|
if(is_standard_io(input)) { // read from stdin
|
||||||
inputsize = 0;
|
inputsize = 0;
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else inputsize = get_file_size(input);
|
} else inputsize = get_file_size(input);
|
||||||
if(inputsize < 0) {
|
if(inputsize < 0) {
|
||||||
return base16384_err_get_file_size;
|
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) {
|
if(!fpo) {
|
||||||
return base16384_err_fopen_output_file;
|
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;
|
return base16384_err_write_file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fpo);
|
if(!is_standard_io(output)) fclose(fpo);
|
||||||
fclose(fp);
|
if(!is_standard_io(input)) fclose(fp);
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
} else { // small file, use mmap & fwrite
|
} else { // small file, use mmap & fwrite
|
||||||
int fd = open(input, O_RDONLY);
|
int fd = open(input, O_RDONLY);
|
||||||
if(fd <= 0) {
|
if(fd < 0) {
|
||||||
return base16384_err_open_input_file;
|
return base16384_err_open_input_file;
|
||||||
}
|
}
|
||||||
char *input_file = mmap(NULL, (size_t)inputsize, PROT_READ, MAP_PRIVATE, fd, 0);
|
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;
|
return base16384_err_write_file;
|
||||||
}
|
}
|
||||||
munmap(input_file, (size_t)inputsize);
|
munmap(input_file, (size_t)inputsize);
|
||||||
fclose(fpo);
|
if(!is_standard_io(output)) fclose(fpo);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user