1
0
mirror of https://github.com/fumiama/base16384.git synced 2026-06-09 12:40:27 +08:00
* 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>
This commit is contained in:
源文雨
2024-03-26 01:52:27 +09:00
committed by GitHub
parent d2e6426c58
commit 65512e2dcd
8 changed files with 82 additions and 66 deletions

View File

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.12)
if (POLICY CMP0048) if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW) cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048) endif (POLICY CMP0048)
project(base16384 VERSION 2.2.3) project(base16384 VERSION 2.2.5)
add_executable(base16384_b base16384.c) add_executable(base16384_b base16384.c)

View File

@@ -1,7 +1,7 @@
# base16384 # base16384
> **Note**: This project used the awesome cross-platform binary compiling tool [cosmopolitan](https://github.com/jart/cosmopolitan) > **Note**: This project used the awesome cross-platform binary compiling tool [cosmopolitan](https://github.com/jart/cosmopolitan)
> Alternatives: [Go](https://github.com/fumiama/go-base16384), [Python](https://github.com/synodriver/pybase16384), [Android](https://github.com/fumiama/android-base16384), [TypeScript](https://github.com/shigma/base16384.js), [Lua(binding)](https://github.com/synodriver/lua-base16384), [Lua(pure)](https://github.com/Yiwen-Chan/base16384), [C#](https://github.com/lc6464/Base16384Coder-Console) > Alternatives: [Go](https://github.com/fumiama/go-base16384), [Python](https://github.com/synodriver/pybase16384), [Android](https://github.com/fumiama/android-base16384), [TypeScript](https://github.com/shigma/base16384.js), [Lua(binding)](https://github.com/synodriver/lua-base16384), [Lua(pure)](https://github.com/Yiwen-Chan/base16384), [C#](https://github.com/lc6464/Base16384.Net), [Rust](https://github.com/Wybxc/base16384-rs), [Swift](https://github.com/oboard/swift-ui-base16384)
Encode binary file to printable utf16be, and vice versa. Encode binary file to printable utf16be, and vice versa.
@@ -86,7 +86,7 @@ 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
``` ```

View File

@@ -1,6 +1,6 @@
/* base1432.c /* base1432.c
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384). * This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
* Copyright (c) 2022 Fumiama Minamoto. * Copyright (c) 2022-2023 Fumiama Minamoto.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -62,7 +62,7 @@
// #define DEBUG // #define DEBUG
int base16384_encode(const char* data, int dlen, char* buf, int blen) { int base16384_encode(const char* data, int dlen, char* buf) {
int outlen = dlen / 7 * 8; int outlen = dlen / 7 * 8;
int offset = dlen % 7; int offset = dlen % 7;
switch(offset) { // 算上偏移标志字符占用的2字节 switch(offset) { // 算上偏移标志字符占用的2字节
@@ -140,7 +140,7 @@ int base16384_encode(const char* data, int dlen, char* buf, int blen) {
return outlen; return outlen;
} }
int base16384_decode(const char* data, int dlen, char* buf, int blen) { int base16384_decode(const char* data, int dlen, char* buf) {
int outlen = dlen; int outlen = dlen;
int offset = 0; int offset = 0;
if(data[dlen-2] == '=') { if(data[dlen-2] == '=') {
@@ -194,7 +194,11 @@ int base16384_decode(const char* data, int dlen, char* buf, int blen) {
if(offset--) { if(offset--) {
buf[i] = (sum & 0x0f000000) >> 20; buf[i] = (sum & 0x0f000000) >> 20;
// 这里有读取越界 // 这里有读取越界
sum = vals[n]; #ifdef WORDS_BIGENDIAN
sum = __builtin_bswap32(vals[n]);
#else
sum = vals[n];
#endif
sum -= 0x0000004e; sum -= 0x0000004e;
buf[i++] |= (sum & 0x0000003c) >> 2; buf[i++] |= (sum & 0x0000003c) >> 2;
if(offset--) { if(offset--) {

View File

@@ -1,6 +1,6 @@
/* base1464.c /* base1464.c
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384). * This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
* Copyright (c) 2022 Fumiama Minamoto. * Copyright (c) 2022-2023 Fumiama Minamoto.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -62,7 +62,7 @@
// #define DEBUG // #define DEBUG
int base16384_encode(const char* data, int dlen, char* buf, int blen) { int base16384_encode(const char* data, int dlen, char* buf) {
int outlen = dlen / 7 * 8; int outlen = dlen / 7 * 8;
int offset = dlen % 7; int offset = dlen % 7;
switch(offset) { // 算上偏移标志字符占用的2字节 switch(offset) { // 算上偏移标志字符占用的2字节
@@ -136,7 +136,7 @@ int base16384_encode(const char* data, int dlen, char* buf, int blen) {
return outlen; return outlen;
} }
int base16384_decode(const char* data, int dlen, char* buf, int blen) { int base16384_decode(const char* data, int dlen, char* buf) {
int outlen = dlen; int outlen = dlen;
int offset = 0; int offset = 0;
if(data[dlen-2] == '=') { if(data[dlen-2] == '=') {

View File

@@ -1,9 +1,9 @@
.TH BASE16384 1 "27 April 2022" "GNU" "User Commands" .TH BASE16384 1 "26 August 2023" "GNU" "User Commands"
.SH NAME .SH NAME
base16384 \- Encode binary files to printable utf16be base16384 \- Encode binary files to printable utf16be
.SH SYNOPSIS .SH SYNOPSIS
.B base16384 .B base16384
-[e|d] <\fIinputfile\fR> <\fIoutputfile\fR> -[e|d|t] <\fIinputfile\fR> <\fIoutputfile\fR>
.SH DESCRIPTION .SH DESCRIPTION
.LP .LP
There are There are
@@ -29,6 +29,12 @@ to
\fB\-e\fR \fB\-e\fR
Read data from \fIinputfile\fR and encode them into \fIoutputfile\fR. Read data from \fIinputfile\fR and encode them into \fIoutputfile\fR.
.TP 0.5i .TP 0.5i
\fB\-d\fR
Read data from \fIinputfile\fR and decode them into \fIoutputfile\fR.
.TP 0.5i
\fB\-t\fR
Show spend time.
.TP 0.5i
\fBinputfile\fR \fBinputfile\fR
An absolute or relative file path. Specially, pass - to read from stdin. An absolute or relative file path. Specially, pass - to read from stdin.
.TP 0.5i .TP 0.5i
@@ -59,6 +65,9 @@ Map input file error.
.TP 0.5i .TP 0.5i
\fB7\fR \fB7\fR
Write file error in mmap. Write file error in mmap.
.TP 0.5i
\fB8\fR
Invalid input/output filename.
.SH "SEE ALSO" .SH "SEE ALSO"
https://github.com/fumiama/base16384 https://github.com/fumiama/base16384
.SH BUGS .SH BUGS
@@ -68,7 +77,7 @@ on github.
.SH AUTHOR .SH AUTHOR
This manual page contributed by Fumiama Minamoto. This manual page contributed by Fumiama Minamoto.
.SH "COPYRIGHT" .SH "COPYRIGHT"
Copyright \(co 2022, Fumiama Minamoto Copyright \(co 2022-2023, Fumiama Minamoto
This file is part of This file is part of
.IR "base16384" . .IR "base16384" .
.LP .LP

View File

@@ -1,6 +1,6 @@
/* base16384.c /* base16384.c
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384). * This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
* Copyright (c) 2022 Fumiama Minamoto. * Copyright (c) 2022-2023 Fumiama Minamoto.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -39,7 +39,7 @@ unsigned long get_start_ms() {
#endif #endif
static void print_usage() { static void print_usage() {
puts("Copyright (c) 2022-2023 Fumiama Minamoto.\nBase16384 2.2.3 (May 18th 2023). Usage:"); puts("Copyright (c) 2022-2023 Fumiama Minamoto.\nBase16384 2.2.5 (August 26th 2023). Usage:");
puts("base16384 [-edt] [inputfile] [outputfile]"); puts("base16384 [-edt] [inputfile] [outputfile]");
puts(" -e\t\tencode"); puts(" -e\t\tencode");
puts(" -d\t\tdecode"); puts(" -d\t\tdecode");
@@ -97,15 +97,18 @@ int main(int argc, char** argv) {
printf("spend time: %lums\n", get_start_ms() - t); printf("spend time: %lums\n", get_start_ms() - t);
#endif #endif
} }
#define print_base16384_err(n) case base16384_err_##n: perror("base16384_err_"#n); break
if(exitstat) switch(exitstat) { if(exitstat) switch(exitstat) {
case base16384_err_get_file_size: perror("base16384_err_get_file_size"); break; print_base16384_err(get_file_size);
case base16384_err_fopen_output_file: perror("base16384_err_fopen_output_file"); break; print_base16384_err(fopen_output_file);
case base16384_err_fopen_input_file: perror("base16384_err_fopen_input_file"); break; print_base16384_err(fopen_input_file);
case base16384_err_write_file: perror("base16384_err_write_file"); break; print_base16384_err(write_file);
case base16384_err_open_input_file: perror("base16384_err_open_input_file"); break; print_base16384_err(open_input_file);
case base16384_err_map_input_file: perror("base16384_err_map_input_file"); break; print_base16384_err(map_input_file);
case base16384_err_read_file: perror("base16384_err_read_file"); break; print_base16384_err(read_file);
print_base16384_err(invalid_file_name);
default: perror("base16384"); break; default: perror("base16384"); break;
} }
#undef print_base16384_err
return exitstat; return exitstat;
} }

View File

@@ -3,7 +3,7 @@
/* base16384.h /* base16384.h
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384). * This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
* Copyright (c) 2022 Fumiama Minamoto. * Copyright (c) 2022-2023 Fumiama Minamoto.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -24,22 +24,27 @@
#include <stdio.h> #include <stdio.h>
#endif #endif
#define define_base16384_err_t(n) base16384_err_##n
// base16384_err_t is the return value of base16384_en/decode_file // base16384_err_t is the return value of base16384_en/decode_file
enum base16384_err_t { enum base16384_err_t {
base16384_err_ok, define_base16384_err_t(ok),
base16384_err_get_file_size, define_base16384_err_t(get_file_size),
base16384_err_fopen_output_file, define_base16384_err_t(fopen_output_file),
base16384_err_fopen_input_file, define_base16384_err_t(fopen_input_file),
base16384_err_write_file, define_base16384_err_t(write_file),
base16384_err_open_input_file, define_base16384_err_t(open_input_file),
base16384_err_map_input_file, define_base16384_err_t(map_input_file),
base16384_err_read_file, define_base16384_err_t(read_file),
define_base16384_err_t(invalid_file_name),
}; };
// base16384_err_t is the return value of base16384_en/decode_file // base16384_err_t is the return value of base16384_en/decode_file
typedef enum base16384_err_t base16384_err_t; typedef enum base16384_err_t base16384_err_t;
#define BASE16384_ENCBUFSZ (BUFSIZ*1024/7*7) #undef define_base16384_err_t
#define BASE16384_DECBUFSZ (BUFSIZ*1024/8*8+2)
#define BASE16384_ENCBUFSZ (BUFSIZ*1024/7*7+7)
#define BASE16384_DECBUFSZ (BUFSIZ*1024/8*8+16)
// base16384_encode_len calc min buf size to fill encode result // base16384_encode_len calc min buf size to fill encode result
static inline int base16384_encode_len(int dlen) { static inline int base16384_encode_len(int dlen) {
@@ -75,10 +80,10 @@ static inline int base16384_decode_len(int dlen, int offset) {
} }
// base16384_encode encodes data and write result into buf // base16384_encode encodes data and write result into buf
int base16384_encode(const char* data, int dlen, char* buf, int blen); int base16384_encode(const char* data, int dlen, char* buf);
// base16384_decode decodes data and write result into buf // base16384_decode decodes data and write result into buf
int base16384_decode(const char* data, int dlen, char* buf, int blen); int base16384_decode(const char* data, int dlen, char* buf);
// base16384_encode_file encodes input file to output file. // base16384_encode_file encodes input file to output file.
// use `-` to specify stdin/stdout // use `-` to specify stdin/stdout

57
file.c
View File

@@ -1,6 +1,6 @@
/* file.c /* file.c
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384). * This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
* Copyright (c) 2022 Fumiama Minamoto. * Copyright (c) 2022-2023 Fumiama Minamoto.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#ifdef _WIN32 #ifdef _WIN32
@@ -48,6 +49,7 @@ base16384_err_t base16384_encode_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(!input || !output || strlen(input) <= 0 || strlen(output) <= 0) return base16384_err_invalid_file_name;
if(is_standard_io(input)) { // read from stdin if(is_standard_io(input)) { // read from stdin
inputsize = 0; inputsize = 0;
fp = stdin; fp = stdin;
@@ -59,9 +61,9 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha
if(!fpo) { if(!fpo) {
return base16384_err_fopen_output_file; return base16384_err_fopen_output_file;
} }
if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use encbuf & fread if(!inputsize || inputsize > BASE16384_ENCBUFSZ) { // stdin or big file, use encbuf & fread
inputsize = BUFSIZ*1024/7*7; inputsize = BASE16384_ENCBUFSZ-7;
#ifdef _WIN32 #if defined _WIN32 || defined __cosmopolitan
} }
#endif #endif
if(!fp) fp = fopen(input, "rb"); if(!fp) fp = fopen(input, "rb");
@@ -69,32 +71,30 @@ base16384_err_t base16384_encode_file(const char* input, const char* output, cha
return base16384_err_fopen_input_file; return base16384_err_fopen_input_file;
} }
int outputsize = base16384_encode_len(inputsize)+16;
size_t cnt = 0; size_t cnt = 0;
fputc(0xFE, fpo); fputc(0xFE, fpo);
fputc(0xFF, fpo); fputc(0xFF, fpo);
while((cnt = fread(encbuf, sizeof(char), inputsize, fp)) > 0) { while((cnt = fread(encbuf, sizeof(char), inputsize, fp)) > 0) {
int n = base16384_encode(encbuf, cnt, decbuf, outputsize); int n = base16384_encode(encbuf, cnt, decbuf);
if(fwrite(decbuf, n, 1, fpo) <= 0) { if(fwrite(decbuf, n, 1, fpo) <= 0) {
return base16384_err_write_file; return base16384_err_write_file;
} }
} }
if(!is_standard_io(output)) fclose(fpo); if(!is_standard_io(output)) fclose(fpo);
if(!is_standard_io(input)) fclose(fp); if(!is_standard_io(input)) fclose(fp);
#ifndef _WIN32 #if !defined _WIN32 && !defined __cosmopolitan
} 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+16, PROT_READ, MAP_PRIVATE, fd, 0);
if(input_file == MAP_FAILED) { if(input_file == MAP_FAILED) {
return base16384_err_map_input_file; return base16384_err_map_input_file;
} }
int outputsize = base16384_encode_len(inputsize)+16;
fputc(0xFE, fpo); fputc(0xFE, fpo);
fputc(0xFF, fpo); fputc(0xFF, fpo);
int n = base16384_encode(input_file, (int)inputsize, decbuf, outputsize); int n = base16384_encode(input_file, (int)inputsize, decbuf);
if(fwrite(decbuf, n, 1, fpo) <= 0) { if(fwrite(decbuf, n, 1, fpo) <= 0) {
return base16384_err_write_file; return base16384_err_write_file;
} }
@@ -113,13 +113,12 @@ base16384_err_t base16384_encode_fp(FILE* input, FILE* output, char* encbuf, cha
if(!output) { if(!output) {
return base16384_err_fopen_output_file; return base16384_err_fopen_output_file;
} }
off_t inputsize = BUFSIZ*1024/7*7; off_t inputsize = BASE16384_ENCBUFSZ-7;
int outputsize = base16384_encode_len(inputsize)+16;
size_t cnt = 0; size_t cnt = 0;
fputc(0xFE, output); fputc(0xFE, output);
fputc(0xFF, output); fputc(0xFF, output);
while((cnt = fread(encbuf, sizeof(char), inputsize, input)) > 0) { while((cnt = fread(encbuf, sizeof(char), inputsize, input)) > 0) {
int n = base16384_encode(encbuf, cnt, decbuf, outputsize); int n = base16384_encode(encbuf, cnt, decbuf);
if(fwrite(decbuf, n, 1, output) <= 0) { if(fwrite(decbuf, n, 1, output) <= 0) {
return base16384_err_write_file; return base16384_err_write_file;
} }
@@ -134,12 +133,11 @@ base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* d
if(output < 0) { if(output < 0) {
return base16384_err_fopen_output_file; return base16384_err_fopen_output_file;
} }
off_t inputsize = BUFSIZ*1024/7*7; off_t inputsize = BASE16384_ENCBUFSZ-7;
int outputsize = base16384_encode_len(inputsize)+16;
size_t cnt = 0; size_t cnt = 0;
write(output, "\xfe\xff", 2); write(output, "\xfe\xff", 2);
while((cnt = read(input, encbuf, inputsize)) > 0) { while((cnt = read(input, encbuf, inputsize)) > 0) {
int n = base16384_encode(encbuf, cnt, decbuf, outputsize); int n = base16384_encode(encbuf, cnt, decbuf);
if(write(output, decbuf, n) < n) { if(write(output, decbuf, n) < n) {
return base16384_err_write_file; return base16384_err_write_file;
} }
@@ -167,6 +165,7 @@ 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(!input || !output || strlen(input) <= 0 || strlen(output) <= 0) return base16384_err_invalid_file_name;
if(is_standard_io(input)) { // read from stdin if(is_standard_io(input)) { // read from stdin
inputsize = 0; inputsize = 0;
fp = stdin; fp = stdin;
@@ -178,16 +177,15 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha
if(!fpo) { if(!fpo) {
return base16384_err_fopen_output_file; return base16384_err_fopen_output_file;
} }
if(!inputsize || inputsize > BUFSIZ*1024) { // stdin or big file, use decbuf & fread if(!inputsize || inputsize > BASE16384_DECBUFSZ) { // stdin or big file, use decbuf & fread
inputsize = BUFSIZ*1024/8*8; inputsize = BASE16384_DECBUFSZ/8*8;
#ifdef _WIN32 #if defined _WIN32 || defined __cosmopolitan
} }
#endif #endif
if(!fp) fp = fopen(input, "rb"); if(!fp) fp = fopen(input, "rb");
if(!fp) { if(!fp) {
return base16384_err_fopen_input_file; return base16384_err_fopen_input_file;
} }
int outputsize = base16384_decode_len(inputsize, 0)+16;
int cnt = 0; int cnt = 0;
int end = 0; int end = 0;
rm_head(fp); rm_head(fp);
@@ -196,25 +194,24 @@ base16384_err_t base16384_decode_file(const char* input, const char* output, cha
decbuf[cnt++] = '='; decbuf[cnt++] = '=';
decbuf[cnt++] = end; decbuf[cnt++] = end;
} }
if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf, outputsize), 1, fpo) <= 0) { if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf), 1, fpo) <= 0) {
return base16384_err_write_file; return base16384_err_write_file;
} }
} }
if(!is_standard_io(output)) fclose(fpo); if(!is_standard_io(output)) fclose(fpo);
if(!is_standard_io(input)) fclose(fp); if(!is_standard_io(input)) fclose(fp);
#ifndef _WIN32 #if !defined _WIN32 && !defined __cosmopolitan
} 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+16, PROT_READ, MAP_PRIVATE, fd, 0);
if(input_file == MAP_FAILED) { if(input_file == MAP_FAILED) {
return base16384_err_map_input_file; return base16384_err_map_input_file;
} }
int outputsize = base16384_decode_len(inputsize, 0)+16;
int off = skip_offset(input_file); int off = skip_offset(input_file);
if(fwrite(encbuf, base16384_decode(input_file+off, inputsize-off, encbuf, outputsize), 1, fpo) <= 0) { if(fwrite(encbuf, base16384_decode(input_file+off, inputsize-off, encbuf), 1, fpo) <= 0) {
return base16384_err_write_file; return base16384_err_write_file;
} }
munmap(input_file, (size_t)inputsize); munmap(input_file, (size_t)inputsize);
@@ -232,8 +229,7 @@ base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, cha
if(!output) { if(!output) {
return base16384_err_fopen_output_file; return base16384_err_fopen_output_file;
} }
off_t inputsize = BUFSIZ*1024/8*8; off_t inputsize = BASE16384_DECBUFSZ/8*8;
int outputsize = base16384_decode_len(inputsize, 0)+16;
int cnt = 0; int cnt = 0;
int end = 0; int end = 0;
rm_head(input); rm_head(input);
@@ -242,7 +238,7 @@ base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, cha
decbuf[cnt++] = '='; decbuf[cnt++] = '=';
decbuf[cnt++] = end; decbuf[cnt++] = end;
} }
if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf, outputsize), 1, output) <= 0) { if(fwrite(encbuf, base16384_decode(decbuf, cnt, encbuf), 1, output) <= 0) {
return base16384_err_write_file; return base16384_err_write_file;
} }
} }
@@ -265,8 +261,7 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d
if(output < 0) { if(output < 0) {
return base16384_err_fopen_output_file; return base16384_err_fopen_output_file;
} }
off_t inputsize = BUFSIZ*1024/8*8; off_t inputsize = BASE16384_DECBUFSZ/8*8;
int outputsize = base16384_decode_len(inputsize, 0)+16;
int cnt = 0; int cnt = 0;
int end = 0; int end = 0;
decbuf[0] = 0; decbuf[0] = 0;
@@ -283,7 +278,7 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d
end = 0; end = 0;
} else end = 1; } else end = 1;
} else end = 0; } else end = 0;
cnt = base16384_decode(decbuf, cnt, encbuf, outputsize); cnt = base16384_decode(decbuf, cnt, encbuf);
if(write(output, encbuf, cnt) < cnt) { if(write(output, encbuf, cnt) < cnt) {
return base16384_err_write_file; return base16384_err_write_file;
} }