1
0
mirror of https://github.com/fumiama/base16384.git synced 2026-06-10 05:10:24 +08:00

优化错误输出,新增-t参数,新增文件API

This commit is contained in:
源文雨
2022-10-16 11:47:14 +08:00
parent 022e132ef5
commit fd2cc4a899
6 changed files with 296 additions and 219 deletions

View File

@@ -1,5 +1,5 @@
#ifndef _BASE14_H_
#define _BASE14_H_
#ifndef _BASE16384_H_
#define _BASE16384_H_
/* base16384.h
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
@@ -18,8 +18,26 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// encode_len calc min buf size to fill encode result
int encode_len(int dlen) {
#include <stdint.h>
// base16384_err_t is the return value of base16384_en/decode_file
enum base16384_err_t {
base16384_err_ok,
base16384_err_get_file_size,
base16384_err_fopen_output_file,
base16384_err_fopen_input_file,
base16384_err_write_file,
base16384_err_open_input_file,
base16384_err_map_input_file,
};
// base16384_err_t is the return value of base16384_en/decode_file
typedef enum base16384_err_t base16384_err_t;
#define BASE16384_ENCBUFSZ (BUFSIZ*1024/7*7)
#define BASE16384_DECBUFSZ (BUFSIZ*1024/8*8+2)
// base16384_encode_len calc min buf size to fill encode result
static inline int base16384_encode_len(int dlen) {
int outlen = dlen / 7 * 8;
int offset = dlen % 7;
switch(offset) { // 算上偏移标志字符占用的2字节
@@ -35,8 +53,8 @@ int encode_len(int dlen) {
return outlen + 8; // 冗余的8B用于可能的结尾的覆盖
}
// decode_len calc min buf size to fill decode result
int decode_len(int dlen, int offset) {
// base16384_decode_len calc min buf size to fill decode result
static inline int base16384_decode_len(int dlen, int offset) {
int outlen = dlen;
switch(offset) { // 算上偏移标志字符占用的2字节
case 0: break;
@@ -51,9 +69,20 @@ int decode_len(int dlen, int offset) {
return outlen / 8 * 7 + offset + 1; // 多出1字节用于循环覆盖
}
// 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);
// base16384_encode encodes data and write result into buf
int base16384_encode(const char* data, int dlen, char* buf, int blen);
#endif
// base16384_decode decodes data and write result into buf
int base16384_decode(const char* data, int dlen, char* buf, int blen);
// base16384_encode_file encodes input file to output file.
// use `-` to specify stdin/stdout
// encbuf & decbuf must be no less than BASE16384_ENCBUFSZ & BASE16384_DECBUFSZ
base16384_err_t base16384_encode_file(const char* input, const char* output, char* encbuf, char* decbuf);
// base16384_decode_file decodes input file to output file.
// use `-` to specify stdin/stdout
// encbuf & decbuf must be no less than BASE16384_ENCBUFSZ & BASE16384_DECBUFSZ
base16384_err_t base16384_decode_file(const char* input, const char* output, char* encbuf, char* decbuf);
#endif