mirror of
https://github.com/fumiama/base16384.git
synced 2026-06-05 02:00:31 +08:00
59 lines
1.8 KiB
C
59 lines
1.8 KiB
C
#ifndef _BASE14_H_
|
|
#define _BASE14_H_
|
|
|
|
/* base16384.h
|
|
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
|
|
* Copyright (c) 2022 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, version 3.
|
|
*
|
|
* 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/>.
|
|
*/
|
|
|
|
// encode_len calc min buf size to fill encode result
|
|
int encode_len(int dlen) {
|
|
int outlen = dlen / 7 * 8;
|
|
int offset = dlen % 7;
|
|
switch(offset) { // 算上偏移标志字符占用的2字节
|
|
case 0: break;
|
|
case 1: outlen += 4; break;
|
|
case 2:
|
|
case 3: outlen += 6; break;
|
|
case 4:
|
|
case 5: outlen += 8; break;
|
|
case 6: outlen += 10; break;
|
|
default: break;
|
|
}
|
|
return outlen + 8; // 冗余的8B用于可能的结尾的覆盖
|
|
}
|
|
|
|
// decode_len calc min buf size to fill decode result
|
|
int decode_len(int dlen, int offset) {
|
|
int outlen = dlen;
|
|
switch(offset) { // 算上偏移标志字符占用的2字节
|
|
case 0: break;
|
|
case 1: outlen -= 4; break;
|
|
case 2:
|
|
case 3: outlen -= 6; break;
|
|
case 4:
|
|
case 5: outlen -= 8; break;
|
|
case 6: outlen -= 10; break;
|
|
default: break;
|
|
}
|
|
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);
|
|
|
|
#endif |