mirror of
https://github.com/fumiama/base16384.git
synced 2026-06-08 04:00:25 +08:00
feat: add param -n -t & optimize param parsing & tidy documents
This commit is contained in:
152
base1432.c
152
base1432.c
@@ -1,6 +1,6 @@
|
||||
/* base1432.c
|
||||
* This file is part of the base16384 distribution (https://github.com/fumiama/base16384).
|
||||
* Copyright (c) 2022-2023 Fumiama Minamoto.
|
||||
* Copyright (c) 2022-2024 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
|
||||
@@ -16,51 +16,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef __cosmopolitan // always le
|
||||
# define be16toh(x) bswap_16(x)
|
||||
# define be32toh(x) bswap_32(x)
|
||||
# define htobe16(x) bswap_16(x)
|
||||
# define htobe32(x) bswap_32(x)
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef __linux__
|
||||
# include <endian.h>
|
||||
#endif
|
||||
#ifdef __FreeBSD__
|
||||
# include <sys/endian.h>
|
||||
#endif
|
||||
#ifdef __NetBSD__
|
||||
# include <sys/endian.h>
|
||||
#endif
|
||||
#ifdef __OpenBSD__
|
||||
# include <sys/types.h>
|
||||
# define be16toh(x) betoh16(x)
|
||||
# define be32toh(x) betoh32(x)
|
||||
#endif
|
||||
#ifdef __APPLE__
|
||||
# define be16toh(x) ntohs(x)
|
||||
# define be32toh(x) ntohl(x)
|
||||
# define htobe16(x) ntohs(x)
|
||||
# define htobe32(x) htonl(x)
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
# define be16toh(x) (x)
|
||||
# define be32toh(x) (x)
|
||||
# define htobe16(x) (x)
|
||||
# define htobe32(x) (x)
|
||||
#else
|
||||
# define be16toh(x) _byteswap_ushort(x)
|
||||
# define be32toh(x) _byteswap_ulong(x)
|
||||
# define htobe16(x) _byteswap_ushort(x)
|
||||
# define htobe32(x) _byteswap_ulong(x)
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// #define DEBUG
|
||||
#include "binary.h"
|
||||
|
||||
int base16384_encode(const char* data, int dlen, char* buf) {
|
||||
int outlen = dlen / 7 * 8;
|
||||
@@ -140,6 +96,49 @@ int base16384_encode(const char* data, int dlen, char* buf) {
|
||||
return outlen;
|
||||
}
|
||||
|
||||
int base16384_encode_unsafe(const char* data, int dlen, char* buf) {
|
||||
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;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
printf("outlen: %llu, offset: %u, malloc: %llu\n", outlen, offset, outlen + 8);
|
||||
#endif
|
||||
uint32_t* vals = (uint32_t*)buf;
|
||||
uint32_t n = 0;
|
||||
int32_t i = 0;
|
||||
for(; i < dlen; i += 7) {
|
||||
register uint32_t sum = 0;
|
||||
register uint32_t shift = htobe32(*(uint32_t*)(data+i));
|
||||
sum |= (shift>>2) & 0x3fff0000;
|
||||
sum |= (shift>>4) & 0x00003fff;
|
||||
sum += 0x4e004e00;
|
||||
vals[n++] = be32toh(sum);
|
||||
shift <<= 26;
|
||||
shift &= 0x3c000000;
|
||||
sum = 0;
|
||||
shift |= (htobe32(*(uint32_t*)(data+i+4))>>6)&0x03fffffc;
|
||||
sum |= shift & 0x3fff0000;
|
||||
shift >>= 2;
|
||||
sum |= shift & 0x00003fff;
|
||||
sum += 0x4e004e00;
|
||||
vals[n++] = be32toh(sum);
|
||||
}
|
||||
if(offset) {
|
||||
buf[outlen - 2] = '=';
|
||||
buf[outlen - 1] = offset;
|
||||
}
|
||||
return outlen;
|
||||
}
|
||||
|
||||
int base16384_decode(const char* data, int dlen, char* buf) {
|
||||
int outlen = dlen;
|
||||
int offset = 0;
|
||||
@@ -214,3 +213,64 @@ int base16384_decode(const char* data, int dlen, char* buf) {
|
||||
}
|
||||
return outlen;
|
||||
}
|
||||
|
||||
int base16384_decode_unsafe(const char* data, int dlen, char* buf) {
|
||||
int outlen = dlen;
|
||||
int offset = 0;
|
||||
if(data[dlen-2] == '=') {
|
||||
offset = data[dlen-1];
|
||||
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;
|
||||
}
|
||||
}
|
||||
outlen = outlen / 8 * 7 + offset;
|
||||
uint32_t* vals = (uint32_t*)data;
|
||||
uint32_t n = 0;
|
||||
int32_t i = 0;
|
||||
for(; i < outlen-7; i+=7) { // n实际每次自增2
|
||||
register uint32_t sum = 0;
|
||||
register uint32_t shift = htobe32(vals[n++]) - 0x4e004e00;
|
||||
shift <<= 2;
|
||||
sum |= shift & 0xfffc0000;
|
||||
shift <<= 2;
|
||||
sum |= shift & 0x0003fff0;
|
||||
shift = htobe32(vals[n++]) - 0x4e004e00;
|
||||
sum |= shift >> 26;
|
||||
*(uint32_t*)(buf+i) = be32toh(sum);
|
||||
sum = 0;
|
||||
shift <<= 6;
|
||||
sum |= shift & 0xffc00000;
|
||||
shift <<= 2;
|
||||
sum |= shift & 0x003fff00;
|
||||
*(uint32_t*)(buf+i+4) = be32toh(sum);
|
||||
}
|
||||
register uint32_t sum = 0;
|
||||
register uint32_t shift = htobe32(vals[n++]);
|
||||
if(((shift>>24)&0xff) < 0x4e) shift |= 0xff000000;
|
||||
if(((shift>> 8)&0xff) < 0x4e) shift |= 0x0000ff00;
|
||||
shift -= 0x4e004e00;
|
||||
shift <<= 2;
|
||||
sum |= shift & 0xfffc0000;
|
||||
shift <<= 2;
|
||||
sum |= shift & 0x0003fff0;
|
||||
shift = htobe32(vals[n++]);
|
||||
if(((shift>>24)&0xff) < 0x4e) shift |= 0xff000000;
|
||||
if(((shift>> 8)&0xff) < 0x4e) shift |= 0x0000ff00;
|
||||
shift -= 0x4e004e00;
|
||||
sum |= shift >> 26;
|
||||
*(uint32_t*)(buf+i) = be32toh(sum);
|
||||
sum = 0;
|
||||
shift <<= 6;
|
||||
sum |= shift & 0xffc00000;
|
||||
shift <<= 2;
|
||||
sum |= shift & 0x003fff00;
|
||||
*(uint32_t*)(buf+i+4) = be32toh(sum);
|
||||
return outlen;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user