1
0
mirror of https://github.com/fumiama/base16384.git synced 2026-06-06 02:30:31 +08:00
Files
base16384/file.c
源文雨 65512e2dcd sync (#22)
* fix typo

* fix: 32bit be decoding

* chore: add Rust alternative (#18)

* Update C# Link (#20)

* 尝试修复#19

- 移除未使用的参数 int blen
- 完善文档、注释
- 完善文件名错误检测
- 修复 encbuf, decbuf 处理逻辑

* fix: disable mmap in windows

* fix: encode bufsz (#19)

* v2.2.5

* Update Swift Link (#21)

---------

Co-authored-by: 忘忧北萱草 <wybxc@qq.com>
Co-authored-by: LC <64722907+lc6464@users.noreply.github.com>
Co-authored-by: oboard <oboard@outlook.com>
2024-03-26 01:52:27 +09:00

289 lines
7.9 KiB
C

/* file.c
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
* Copyright (c) 2022-2023 Fumiama Minamoto.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __cosmopolitan
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#include <process.h>
#else
#include <unistd.h>
#include <sys/mman.h>
#endif
#endif
#include "base16384.h"
#ifdef __cosmopolitan
#define get_file_size(filepath) ((off_t)GetFileSize(filepath))
#else
static inline off_t get_file_size(const char* filepath) {
struct stat statbuf;
return stat(filepath, &statbuf)?-1:statbuf.st_size;
}
#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(!input || !output || strlen(input) <= 0 || strlen(output) <= 0) return base16384_err_invalid_file_name;
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 = is_standard_io(output)?stdout:fopen(output, "wb");
if(!fpo) {
return base16384_err_fopen_output_file;
}
if(!inputsize || inputsize > BASE16384_ENCBUFSZ) { // stdin or big file, use encbuf & fread
inputsize = BASE16384_ENCBUFSZ-7;
#if defined _WIN32 || defined __cosmopolitan
}
#endif
if(!fp) fp = fopen(input, "rb");
if(!fp) {
return base16384_err_fopen_input_file;
}
size_t cnt = 0;
fputc(0xFE, fpo);
fputc(0xFF, fpo);
while((cnt = fread(encbuf, sizeof(char), inputsize, fp)) > 0) {
int n = base16384_encode(encbuf, cnt, decbuf);
if(fwrite(decbuf, n, 1, fpo) <= 0) {
return base16384_err_write_file;
}
}
if(!is_standard_io(output)) fclose(fpo);
if(!is_standard_io(input)) fclose(fp);
#if !defined _WIN32 && !defined __cosmopolitan
} else { // small file, use mmap & fwrite
int fd = open(input, O_RDONLY);
if(fd < 0) {
return base16384_err_open_input_file;
}
char *input_file = mmap(NULL, (size_t)inputsize+16, PROT_READ, MAP_PRIVATE, fd, 0);
if(input_file == MAP_FAILED) {
return base16384_err_map_input_file;
}
fputc(0xFE, fpo);
fputc(0xFF, fpo);
int n = base16384_encode(input_file, (int)inputsize, decbuf);
if(fwrite(decbuf, n, 1, fpo) <= 0) {
return base16384_err_write_file;
}
munmap(input_file, (size_t)inputsize);
if(!is_standard_io(output)) fclose(fpo);
close(fd);
}
#endif
return base16384_err_ok;
}
base16384_err_t base16384_encode_fp(FILE* input, FILE* output, char* encbuf, char* decbuf) {
if(!input) {
return base16384_err_fopen_input_file;
}
if(!output) {
return base16384_err_fopen_output_file;
}
off_t inputsize = BASE16384_ENCBUFSZ-7;
size_t cnt = 0;
fputc(0xFE, output);
fputc(0xFF, output);
while((cnt = fread(encbuf, sizeof(char), inputsize, input)) > 0) {
int n = base16384_encode(encbuf, cnt, decbuf);
if(fwrite(decbuf, n, 1, output) <= 0) {
return base16384_err_write_file;
}
}
return base16384_err_ok;
}
base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* decbuf) {
if(input < 0) {
return base16384_err_fopen_input_file;
}
if(output < 0) {
return base16384_err_fopen_output_file;
}
off_t inputsize = BASE16384_ENCBUFSZ-7;
size_t cnt = 0;
write(output, "\xfe\xff", 2);
while((cnt = read(input, encbuf, inputsize)) > 0) {
int n = base16384_encode(encbuf, cnt, decbuf);
if(write(output, decbuf, n) < n) {
return base16384_err_write_file;
}
}
return base16384_err_ok;
}
#define rm_head(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)
static inline int is_next_end(FILE* fp) {
int ch = fgetc(fp);
if(ch == EOF) return 0;
if(ch == '=') return fgetc(fp);
ungetc(ch, fp);
return 0;
}
base16384_err_t base16384_decode_file(const char* input, const char* output, char* encbuf, char* decbuf) {
off_t inputsize;
FILE* fp = NULL;
FILE* fpo;
if(!input || !output || strlen(input) <= 0 || strlen(output) <= 0) return base16384_err_invalid_file_name;
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 = is_standard_io(output)?stdout:fopen(output, "wb");
if(!fpo) {
return base16384_err_fopen_output_file;
}
if(!inputsize || inputsize > BASE16384_DECBUFSZ) { // stdin or big file, use decbuf & fread
inputsize = BASE16384_DECBUFSZ/8*8;
#if defined _WIN32 || defined __cosmopolitan
}
#endif
if(!fp) fp = fopen(input, "rb");
if(!fp) {
return base16384_err_fopen_input_file;
}
int cnt = 0;
int end = 0;
rm_head(fp);
while((cnt = fread(decbuf, sizeof(char), inputsize, fp)) > 0) {
if((end = is_next_end(fp))) {
decbuf[cnt++] = '=';
decbuf[cnt++] = end;
}
if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf), 1, fpo) <= 0) {
return base16384_err_write_file;
}
}
if(!is_standard_io(output)) fclose(fpo);
if(!is_standard_io(input)) fclose(fp);
#if !defined _WIN32 && !defined __cosmopolitan
} else { // small file, use mmap & fwrite
int fd = open(input, O_RDONLY);
if(fd < 0) {
return base16384_err_open_input_file;
}
char *input_file = mmap(NULL, (size_t)inputsize+16, PROT_READ, MAP_PRIVATE, fd, 0);
if(input_file == MAP_FAILED) {
return base16384_err_map_input_file;
}
int off = skip_offset(input_file);
if(fwrite(encbuf, base16384_decode(input_file+off, inputsize-off, encbuf), 1, fpo) <= 0) {
return base16384_err_write_file;
}
munmap(input_file, (size_t)inputsize);
if(!is_standard_io(output)) fclose(fpo);
close(fd);
}
#endif
return base16384_err_ok;
}
base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, char* decbuf) {
if(!input) {
return base16384_err_fopen_input_file;
}
if(!output) {
return base16384_err_fopen_output_file;
}
off_t inputsize = BASE16384_DECBUFSZ/8*8;
int cnt = 0;
int end = 0;
rm_head(input);
while((cnt = fread(decbuf, sizeof(char), inputsize, input)) > 0) {
if((end = is_next_end(input))) {
decbuf[cnt++] = '=';
decbuf[cnt++] = end;
}
if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf), 1, output) <= 0) {
return base16384_err_write_file;
}
}
return base16384_err_ok;
}
static inline int is_next_end_fd(int fd) {
char ch = 0;
read(fd, &ch, 1);
if(ch == '=') {
read(fd, &ch, 1);
}
return (int)ch;
}
base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* decbuf) {
if(input < 0) {
return base16384_err_fopen_input_file;
}
if(output < 0) {
return base16384_err_fopen_output_file;
}
off_t inputsize = BASE16384_DECBUFSZ/8*8;
int cnt = 0;
int end = 0;
decbuf[0] = 0;
if(read(input, decbuf, 2) < 2) {
return base16384_err_read_file;
}
if(decbuf[0] != (char)(0xfe)) cnt = 2;
while((end = read(input, decbuf+cnt, inputsize-cnt)) > 0 || cnt > 0) {
if(end > 0) {
cnt += end;
if((end = is_next_end_fd(input))) {
decbuf[cnt++] = '=';
decbuf[cnt++] = end;
end = 0;
} else end = 1;
} else end = 0;
cnt = base16384_decode(decbuf, cnt, encbuf);
if(write(output, encbuf, cnt) < cnt) {
return base16384_err_write_file;
}
cnt = end;
}
return base16384_err_ok;
}