1
0
mirror of https://github.com/fumiama/base16384.git synced 2026-06-11 14:31:56 +08:00

Merge branch 'master' of https://github.com/fumiama/base16384 into debian

This commit is contained in:
源文雨
2024-04-08 00:42:36 +09:00
14 changed files with 439 additions and 106 deletions

168
file.c
View File

@@ -16,6 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#endif
#ifndef __cosmopolitan
#include <stdio.h>
#include <stdint.h>
@@ -57,14 +63,14 @@ static inline off_t get_file_size(const char* filepath) {
#define do_sum_check(flag) ((flag)&(BASE16384_FLAG_DO_SUM_CHECK_FORCELY|BASE16384_FLAG_SUM_CHECK_ON_REMAIN))
base16384_err_t base16384_encode_file_detailed(const char* input, const char* output, char* encbuf, char* decbuf, int flag) {
off_t inputsize;
FILE *fp = NULL, *fpo;
int errnobak = 0, is_stdin = is_standard_io(input);
base16384_err_t retval = base16384_err_ok;
if(!input || !output || strlen(input) <= 0 || strlen(output) <= 0) {
errno = EINVAL;
return base16384_err_invalid_file_name;
}
base16384_err_t retval = base16384_err_ok;
off_t inputsize;
FILE *fp = NULL, *fpo;
int errnobak = 0, is_stdin = is_standard_io(input);
if(is_stdin) { // read from stdin
inputsize = _BASE16384_ENCBUFSZ;
fp = stdin;
@@ -91,7 +97,11 @@ base16384_err_t base16384_encode_file_detailed(const char* input, const char* ou
fputc(0xFE, fpo);
fputc(0xFF, fpo);
}
size_t cnt;
#ifdef _MSC_VER
int cnt;
#else
size_t cnt;
#endif
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
while((cnt = fread(encbuf, sizeof(char), inputsize, fp)) > 0) {
int n;
@@ -159,7 +169,11 @@ base16384_err_t base16384_encode_fp_detailed(FILE* input, FILE* output, char* en
}
off_t inputsize = _BASE16384_ENCBUFSZ;
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
size_t cnt;
#ifdef _MSC_VER
int cnt;
#else
size_t cnt;
#endif
while((cnt = fread(encbuf, sizeof(char), inputsize, input)) > 0) {
int n;
while(cnt%7) {
@@ -213,10 +227,45 @@ base16384_err_t base16384_encode_fd_detailed(int input, int output, char* encbuf
return base16384_err_ok;
}
#define rm_head(fp) {\
int ch = fgetc(fp);\
if(ch == 0xFE) fgetc(fp);\
else ungetc(ch, fp);\
#define call_reader(cd, buf, n) (input->f.reader((cd)->client_data, (buf), (n)))
#define call_writer(cd, buf, n) (output->f.writer((cd)->client_data, (buf), (n)))
base16384_err_t base16384_encode_stream_detailed(base16384_stream_t* input, base16384_stream_t* output, char* encbuf, char* decbuf, int flag) {
if(!input || !input->f.reader) {
return base16384_err_fopen_input_file;
}
if(!output || !output->f.writer) {
return base16384_err_fopen_output_file;
}
off_t inputsize = _BASE16384_ENCBUFSZ;
size_t cnt = 0;
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
if(!(flag&BASE16384_FLAG_NOHEADER)) call_writer(output, "\xfe\xff", 2);
while((cnt = call_reader(input, encbuf, inputsize)) > 0) {
ssize_t n;
while(cnt%7) {
n = call_reader(input, encbuf+cnt, sizeof(char));
if(n > 0) cnt++;
else break;
}
if(do_sum_check(flag)) {
sum = calc_sum(sum, cnt, encbuf);
if(cnt%7) { // last encode
*(uint32_t*)(&encbuf[cnt]) = htobe32(sum);
}
}
n = base16384_encode_unsafe(encbuf, cnt, decbuf);
if(n && call_writer(output, decbuf, n) < n) {
return base16384_err_write_file;
}
}
return base16384_err_ok;
}
static inline void rm_head(FILE* fp) {
int ch = fgetc(fp);
if(ch == 0xFE) fgetc(fp);
else ungetc(ch, fp);
}
#define skip_offset(input_file) ((input_file[0]==(char)0xFE)?2:0)
@@ -230,16 +279,16 @@ static inline int is_next_end(FILE* fp) {
}
base16384_err_t base16384_decode_file_detailed(const char* input, const char* output, char* encbuf, char* decbuf, int flag) {
if(!input || !output || strlen(input) <= 0 || strlen(output) <= 0) {
errno = EINVAL;
return base16384_err_invalid_file_name;
}
off_t inputsize;
FILE* fp = NULL;
FILE* fpo;
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
base16384_err_t retval = base16384_err_ok;
int errnobak = 0, is_stdin = is_standard_io(input);
if(!input || !output || strlen(input) <= 0 || strlen(output) <= 0) {
errno = EINVAL;
return base16384_err_invalid_file_name;
}
if(is_stdin) { // read from stdin
inputsize = _BASE16384_DECBUFSZ;
fp = stdin;
@@ -264,9 +313,11 @@ base16384_err_t base16384_decode_file_detailed(const char* input, const char* ou
goto_base16384_file_detailed_cleanup(decode, base16384_err_fopen_input_file, {});
}
rm_head(fp);
#ifndef _WIN32 // windows is crazy and always throws EINVAL
if(errno) {
goto_base16384_file_detailed_cleanup(decode, base16384_err_read_file, {});
}
#endif
int cnt, last_encbuf_cnt = 0, last_decbuf_cnt = 0, offset = 0;
size_t total_decoded_len = 0;
while((cnt = fread(decbuf, sizeof(char), inputsize, fp)) > 0) {
@@ -281,7 +332,9 @@ base16384_err_t base16384_decode_file_detailed(const char* input, const char* ou
decbuf[cnt++] = '=';
decbuf[cnt++] = end;
}
#ifndef _WIN32 // windows is crazy and always throws EINVAL
if(errno) goto_base16384_file_detailed_cleanup(decode, base16384_err_read_file, {});
#endif
offset = decbuf[cnt-1];
last_decbuf_cnt = cnt;
cnt = base16384_decode_unsafe(decbuf, cnt, encbuf);
@@ -341,9 +394,11 @@ base16384_err_t base16384_decode_fp_detailed(FILE* input, FILE* output, char* en
off_t inputsize = _BASE16384_DECBUFSZ;
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
rm_head(input);
#ifndef _WIN32 // windows is crazy and always throws EINVAL
if(errno) {
return base16384_err_read_file;
}
#endif
int cnt, last_encbuf_cnt = 0, last_decbuf_cnt = 0, offset = 0;
size_t total_decoded_len = 0;
while((cnt = fread(decbuf, sizeof(char), inputsize, input)) > 0) {
@@ -358,7 +413,9 @@ base16384_err_t base16384_decode_fp_detailed(FILE* input, FILE* output, char* en
decbuf[cnt++] = '=';
decbuf[cnt++] = end;
}
#ifndef _WIN32 // windows is crazy and always throws EINVAL
if(errno) return base16384_err_read_file;
#endif
offset = decbuf[cnt-1];
last_decbuf_cnt = cnt;
cnt = base16384_decode_unsafe(decbuf, cnt, encbuf);
@@ -429,9 +486,11 @@ base16384_err_t base16384_decode_fd_detailed(int input, int output, char* encbuf
else break;
}
uint16_t next = is_next_end_fd(input);
#ifndef _WIN32 // windows is crazy and always throws EINVAL
if(errno) {
return base16384_err_read_file;
}
#endif
if((uint16_t)(~next)) {
if(next&0xff00) {
decbuf[n++] = '=';
@@ -458,3 +517,84 @@ base16384_err_t base16384_decode_fd_detailed(int input, int output, char* encbuf
}
return base16384_err_ok;
}
static inline uint16_t is_next_end_stream(base16384_stream_t* input) {
uint8_t ch = 0;
if(call_reader(input, &ch, 1) != 1) return (uint16_t)EOF;
uint16_t ret = (uint16_t)ch & 0x00ff;
if(ch == '=') {
if(call_reader(input, &ch, 1) != 1) return (uint16_t)EOF;
ret <<= 8;
ret |= (uint16_t)ch & 0x00ff;
}
return ret;
}
base16384_err_t base16384_decode_stream_detailed(base16384_stream_t* input, base16384_stream_t* output, char* encbuf, char* decbuf, int flag) {
if(!input || !input->f.reader) {
errno = EINVAL;
return base16384_err_fopen_input_file;
}
if(!output || !output->f.writer) {
errno = EINVAL;
return base16384_err_fopen_output_file;
}
off_t inputsize = _BASE16384_DECBUFSZ;
uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE;
uint8_t remains[8];
decbuf[0] = 0;
if(call_reader(input, remains, 2) != 2) {
return base16384_err_read_file;
}
int p = 0;
if(remains[0] != (uint8_t)(0xfe)) p = 2;
int n, last_encbuf_cnt = 0, last_decbuf_cnt = 0, offset = 0;
size_t total_decoded_len = 0;
while((n = call_reader(input, decbuf+p, inputsize-p)) > 0) {
if(p) {
memcpy(decbuf, remains, p);
n += p;
p = 0;
}
int x;
while(n%8) {
x = call_reader(input, decbuf+n, sizeof(char));
if(x > 0) n++;
else break;
}
uint16_t next = is_next_end_stream(input);
#ifndef _WIN32 // windows is crazy and always throws EINVAL
if(errno) {
return base16384_err_read_file;
}
#endif
if((uint16_t)(~next)) {
if(next&0xff00) {
decbuf[n++] = '=';
decbuf[n++] = (char)(next&0x00ff);
} else remains[p++] = (char)(next&0x00ff);
}
offset = decbuf[n-1];
last_decbuf_cnt = n;
n = base16384_decode_unsafe(decbuf, n, encbuf);
if(n && call_writer(output, encbuf, n) != n) {
return base16384_err_write_file;
}
total_decoded_len += n;
if(do_sum_check(flag)) sum = calc_sum(sum, n, encbuf);
last_encbuf_cnt = n;
}
if(do_sum_check(flag)
&& (flag&BASE16384_FLAG_DO_SUM_CHECK_FORCELY || total_decoded_len >= _BASE16384_ENCBUFSZ)
&& last_decbuf_cnt > 2
&& decbuf[last_decbuf_cnt-2] == '='
&& check_sum(sum, *(uint32_t*)(&encbuf[last_encbuf_cnt]), offset)) {
errno = EINVAL;
return base16384_err_invalid_decoding_checksum;
}
return base16384_err_ok;
}