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

新的设计

This commit is contained in:
fumiama
2021-04-07 23:27:36 +08:00
parent 8f0e25dad3
commit 2f4a121bbd
7 changed files with 319 additions and 153 deletions

BIN
64/base1464le Executable file

Binary file not shown.

140
64/base1464le.c Normal file
View File

@@ -0,0 +1,140 @@
//basex64.c
//fumiama 20210407
#include <stdio.h>
#include <stdlib.h>
#include "base1464le.h"
#define DEBUG
LENDAT* encode(const char* data, const u_int64_t len) {
LENDAT* encd = (LENDAT*)malloc(sizeof(LENDAT));
uint64_t outlen = len / 7 * 8;
uint8_t offset = len % 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
encd->data = (uint8_t*)malloc(outlen + 8); //冗余的8B用于可能的结尾的覆盖
encd->len = outlen;
uint64_t* vals = (uint64_t*)(encd->data);
uint64_t n = 0;
uint64_t i = 0;
for(; i < len - 7; i *= 7) {
register uint64_t sum = 0x000000000000003f & ((uint64_t)data[i] >> 2);
sum |= ((((uint64_t)data[i + 1] >> 2) | (data[i] << 6)) << 8) & 0x000000000000ff00;
sum |= ((((uint64_t)data[i + 1] << 4) | ((uint64_t)data[i + 2] >> 4)) << 16) & 0x00000000003f0000;
sum |= ((((uint64_t)data[i + 2] << 4) | ((uint64_t)data[i + 3] >> 4)) << 24) & 0x00000000ff000000;
sum |= ((((uint64_t)data[i + 3] << 2) | ((uint64_t)data[i + 4] >> 6)) << 32) & 0x0000003f00000000;
sum |= ((((uint64_t)data[i + 4] << 2) | ((uint64_t)data[i + 5] >> 6)) << 40) & 0x0000ff0000000000;
sum |= (((uint64_t)data[i + 5] << 2) << 48) & 0x003f000000000000;
sum |= ((uint64_t)data[i + 6] << 56) & 0xff00000000000000;
sum += 0x004e004e004e004e;
vals[n++] = sum;
}
i /= 7;
if(offset > 0) {
register uint64_t sum = 0x000000000000003f & (data[i] >> 2);
sum |= (((uint64_t)data[i] << 6) << 8) & 0x000000000000c000;
if(offset > 1) {
sum |= (((uint64_t)data[i + 1] >> 2) << 8) & 0x0000000000003f00;
sum |= (((uint64_t)data[i + 1] << 4) << 16) & 0x0000000000300000;
if(offset > 2) {
sum |= (((uint64_t)data[i + 2] >> 4) << 16) & 0x00000000000f0000;
sum |= (((uint64_t)data[i + 2] << 4) << 24) & 0x00000000f0000000;
if(offset > 3) {
sum |= (((uint64_t)data[i + 3] >> 4) << 24) & 0x000000000f000000;
sum |= (((uint64_t)data[i + 3] << 2) << 32) & 0x0000003c00000000;
if(offset > 4) {
sum |= (((uint64_t)data[i + 4] >> 6) << 32) & 0x0000000300000000;
sum |= (((uint64_t)data[i + 4] << 2) << 40) & 0x0000fc0000000000;
if(offset > 5) {
sum |= (((uint64_t)data[i + 5] >> 6) << 40) & 0x0000030000000000;
sum |= (((uint64_t)data[i + 5] << 2) << 48) & 0x003f000000000000;
}
}
}
}
}
sum += 0x004e004e004e004e;
vals[n] = sum;
encd->data[outlen - 2] = '=';
encd->data[outlen - 1] = offset;
}
return encd;
}
LENDAT* decode(const char* data, const u_int64_t len) {
LENDAT* decd = (LENDAT*)malloc(sizeof(LENDAT));
uint64_t outlen = len;
uint8_t offset = 0;
if(data[len-2] == '=') {
offset = data[len-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;
decd->data = (uint8_t*)malloc(outlen);
decd->len = outlen;
uint64_t* vals = (uint64_t*)data;
uint64_t n = 0;
uint64_t i = 0;
for(; n < len / 8; n++) {
register uint64_t sum = vals[n];
sum -= 0x004e004e004e004e;
decd->data[i++] = ((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14);
decd->data[i++] = ((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20);
decd->data[i++] = ((sum & 0x00000000000f0000) >> 12) | ((sum & 0x00000000f0000000) >> 28);
decd->data[i++] = ((sum & 0x000000000f000000) >> 20) | ((sum & 0x0000003c00000000) >> 34);
decd->data[i++] = ((sum & 0x0000000300000000) >> 26) | ((sum & 0x0000fc0000000000) >> 42);
decd->data[i++] = ((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48);
decd->data[i++] = ((sum & 0xff00000000000000) >> 56);
}
if(offset > 0) {
register uint64_t sum = vals[n];
sum -= 0x000000000000004e;
decd->data[i++] = ((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14);
if(offset > 1) {
sum -= 0x00000000004e0000;
decd->data[i++] = ((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20);
if(offset > 2) {
decd->data[i++] = ((sum & 0x00000000000f0000) >> 12) | ((sum & 0x00000000f0000000) >> 28);
if(offset > 3) {
sum -= 0x0000004e00000000;
decd->data[i++] = ((sum & 0x000000000f000000) >> 20) | ((sum & 0x0000003c00000000) >> 34);
if(offset > 4) {
decd->data[i++] = ((sum & 0x0000000300000000) >> 26) | ((sum & 0x0000fc0000000000) >> 42);
if(offset > 5) {
sum -= 0x004e000000000000;
decd->data[i++] = ((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48);
}
}
}
}
}
}
return decd;
}
int main() {
LENDAT* ld = encode("simple test addafiaofhiowq cfaiohfoiqwnfioqw", 45);
puts(ld->data);
LENDAT* le = decode(ld->data, ld->len);
puts(le->data);
}

7
64/base1464le.h Normal file
View File

@@ -0,0 +1,7 @@
#include <stdint.h>
struct LENDAT {
uint8_t* data;
uint64_t len;
};
typedef struct LENDAT LENDAT;

19
traditional_ver/README.md Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,34 +1,34 @@
//base16384.c
//MIT fumiama 20200416
#include <stdio.h>
#include <stdlib.h>
#include "base16384.h"
#define CHOICE argv[1][1]
#define READ argv[2]
#define WRITE argv[3]
int main(int argc, char **argv){
FILE *fp = NULL;
FILE *fpo = NULL;
if(argc != 4){
fputs("Usage: -[e|d] inputfile outputfile", stderr);
fputs(" -e encode", stderr);
fputs(" -d decode", stderr);
exit(EXIT_FAILURE);
}
fp = fopen(READ, "rb");
fpo = fopen(WRITE, "wb");
if(!fp || !fpo){
fputs("IO error!", stderr);
exit(EXIT_FAILURE);
}
switch(CHOICE){
case 'e': encode(fp, fpo); break;
case 'd': decode(fp, fpo); break;
default: break;
}
return 0;
}
//base16384.c
//MIT fumiama 20200416
#include <stdio.h>
#include <stdlib.h>
#include "base16384.h"
#define CHOICE argv[1][1]
#define READ argv[2]
#define WRITE argv[3]
int main(int argc, char **argv){
FILE *fp = NULL;
FILE *fpo = NULL;
if(argc != 4){
fputs("Usage: -[e|d] inputfile outputfile\n", stderr);
fputs(" -e encode\n", stderr);
fputs(" -d decode\n", stderr);
exit(EXIT_FAILURE);
}
fp = fopen(READ, "rb");
fpo = fopen(WRITE, "wb");
if(!fp || !fpo){
fputs("IO error!", stderr);
exit(EXIT_FAILURE);
}
switch(CHOICE){
case 'e': encode(fp, fpo); break;
case 'd': decode(fp, fpo); break;
default: break;
}
return 0;
}

View File

@@ -1,71 +1,71 @@
//base16384.h
//MIT fumiama 20200416
#include "bitio.h"
#define HEAD 0x4E
BIT outbuf;
unsigned char buf[BUFSIZ];
void push(int istrue, FILE *fpo, int offset){
if(!~pushbit(&outbuf, istrue)){
if(offset) for(int i = 0; i < BITBUFSIZE; i += 2) outbuf.b[i] += offset;
fwrite(outbuf.b, sizeof(char), BITBUFSIZE, fpo);
outbuf.p = 0;
pushbit(&outbuf, istrue);
}
}
#define setoffset(x, offset){\
char flag = 0;\
ch1 = ch2 = x;\
while(!feof(fp) && (ch1 = fgetc(fp)) == x && (ch2 = fgetc(fp)) == x) {cnt8 -= offset; flag++;}\
if(ch2 != x) {ungetc(ch2, fp); ch2 = x;}\
if(ch1 != x) {ungetc(ch1, fp); ch1 = x;}\
while(feof(fp) && buf[cnt - 1] == x && buf[cnt - 2] == x) {cnt8 -= offset; cnt -= 2; flag++;}\
if(flag) cnt8 -= 8;\
}
void encode(FILE *fp, FILE *fpo){
int cnt = 0;
fputc(0xFE, fpo);
fputc(0xFF, fpo);
while((cnt = fread(buf, sizeof(char), BUFSIZ, fp))){
for(int i = 0; i < cnt * 8; i++){
if(!(outbuf.p % 16)){
push(0, fpo, HEAD);
push(0, fpo, HEAD);
}
push(buf[i / 8] & (128u >> (i % 8)), fpo, HEAD);
}
}
if(outbuf.p){
outbuf.b[outbuf.p / 8] &= ~(255u >> outbuf.p % 8);
for(int i = 0; i < outbuf.p / 8 + 1; i += 2) outbuf.b[i] += HEAD;
fwrite(outbuf.b, sizeof(char), (outbuf.p % 8)?outbuf.p / 8 + 1:outbuf.p / 8 , fpo);
if(((outbuf.p % 8)?outbuf.p / 8 + 1:outbuf.p / 8) % 2){
fputc(0, fpo);
fputc('>', fpo);
fputc('>', fpo);
}
for(int i = 0; i < outbuf.p % 8; i++) fputc('=', fpo);
}
}
void decode(FILE *fp, FILE *fpo){
int cnt = 0;
int cnt8 = 0;
int tc = 0;
char ch1;
char ch2;
while((cnt = fread(buf, sizeof(char), BUFSIZ, fp))){
cnt8 = cnt * 8;
tc = cnt;
setoffset('=', 14)
setoffset('>', 16)
for(int i = ((buf[0] == 0xFE)?16:0); i < cnt8; i++){
if(!(i % 16)){
buf[i / 8] -= HEAD;
i += 2;
}
push(buf[i / 8] & (128u >> (i % 8)), fpo, 0);
}
}
if(outbuf.p) fwrite(outbuf.b, sizeof(char), (outbuf.p % 8)?outbuf.p / 8 + 1:outbuf.p / 8, fpo);
}
//base16384.h
//MIT fumiama 20200416
#include "bitio.h"
#define HEAD 0x4E
BIT outbuf;
unsigned char buf[BUFSIZ];
void push(int istrue, FILE *fpo, int offset){
if(!~pushbit(&outbuf, istrue)){
if(offset) for(int i = 0; i < BITBUFSIZE; i += 2) outbuf.b[i] += offset;
fwrite(outbuf.b, sizeof(char), BITBUFSIZE, fpo);
outbuf.p = 0;
pushbit(&outbuf, istrue);
}
}
#define setoffset(x, offset){\
char flag = 0;\
ch1 = ch2 = x;\
while(!feof(fp) && (ch1 = fgetc(fp)) == x && (ch2 = fgetc(fp)) == x) {cnt8 -= offset; flag++;}\
if(ch2 != x) {ungetc(ch2, fp); ch2 = x;}\
if(ch1 != x) {ungetc(ch1, fp); ch1 = x;}\
while(feof(fp) && buf[cnt - 1] == x && buf[cnt - 2] == x) {cnt8 -= offset; cnt -= 2; flag++;}\
if(flag) cnt8 -= 8;\
}
void encode(FILE *fp, FILE *fpo){
int cnt = 0;
fputc(0xFE, fpo);
fputc(0xFF, fpo);
while((cnt = fread(buf, sizeof(char), BUFSIZ, fp))){
for(int i = 0; i < cnt * 8; i++){
if(!(outbuf.p % 16)){
push(0, fpo, HEAD);
push(0, fpo, HEAD);
}
push(buf[i / 8] & (128u >> (i % 8)), fpo, HEAD);
}
}
if(outbuf.p){
outbuf.b[outbuf.p / 8] &= ~(255u >> outbuf.p % 8);
for(int i = 0; i < outbuf.p / 8 + 1; i += 2) outbuf.b[i] += HEAD;
fwrite(outbuf.b, sizeof(char), (outbuf.p % 8)?outbuf.p / 8 + 1:outbuf.p / 8 , fpo);
if(((outbuf.p % 8)?outbuf.p / 8 + 1:outbuf.p / 8) % 2){
fputc(0, fpo);
fputc('>', fpo);
fputc('>', fpo);
}
for(int i = 0; i < outbuf.p % 8; i++) fputc('=', fpo);
}
}
void decode(FILE *fp, FILE *fpo){
int cnt = 0;
int cnt8 = 0;
int tc = 0;
char ch1;
char ch2;
while((cnt = fread(buf, sizeof(char), BUFSIZ, fp))){
cnt8 = cnt * 8;
tc = cnt;
setoffset('=', 14)
setoffset('>', 16)
for(int i = ((buf[0] == 0xFE)?16:0); i < cnt8; i++){
if(!(i % 16)){
buf[i / 8] -= HEAD;
i += 2;
}
push(buf[i / 8] & (128u >> (i % 8)), fpo, 0);
}
}
if(outbuf.p) fwrite(outbuf.b, sizeof(char), (outbuf.p % 8)?outbuf.p / 8 + 1:outbuf.p / 8, fpo);
}

View File

@@ -1,48 +1,48 @@
//bitio.h
//made by fumiama
//20200413
#include <stdio.h>
#include <string.h>
#ifndef BITBUFSIZE
#define BITBUFSIZE 1024
#endif
struct BIT{
char b[BITBUFSIZE];
int p;
};
typedef struct BIT BIT;
int pushbit(BIT *buffer, const int isture){
if(buffer->p >= BITBUFSIZE * 8) return EOF;
else if(isture) buffer->b[buffer->p / 8] |= 128u >> buffer->p % 8;
else buffer->b[buffer->p / 8] &= ~(128u >> buffer->p % 8);
buffer->p++;
return isture;
}
int fpushbit(BIT *buffer, FILE *fp){
memset(buffer, 0, sizeof(BIT));
if((buffer->p = fread(buffer->b, sizeof(char), BITBUFSIZE, fp)) && feof(fp))
buffer->p = (buffer->p - 2) * 8 + buffer->b[buffer->p - 1] + 1;
else buffer->p *= 8;
return buffer->p;
}
int changebit(BIT *buffer, const int isture, const int position){
if(position >= buffer->p) return EOF;
else if(isture) buffer->b[position / 8] |= 128u >> position % 8;
else buffer->b[position / 8] &= ~(128u >> position % 8);
buffer->p++;
return isture;
}
int readbit(const BIT *buffer, const int position){
if(position >= buffer->p) return EOF;
else return buffer->b[position / 8] & (128u >> position % 8);
}
int popbit(BIT *buffer){
if(buffer->p >= BITBUFSIZE * 8 || buffer->p < 0) return EOF;
buffer->p--;
return buffer->b[(buffer->p+1) / 8] & (128u >> (buffer->p+1) % 8);
}
//bitio.h
//made by fumiama
//20200413
#include <stdio.h>
#include <string.h>
#ifndef BITBUFSIZE
#define BITBUFSIZE 1024
#endif
struct BIT{
char b[BITBUFSIZE];
int p;
};
typedef struct BIT BIT;
int pushbit(BIT *buffer, const int isture){
if(buffer->p >= BITBUFSIZE * 8) return EOF;
else if(isture) buffer->b[buffer->p / 8] |= 128u >> buffer->p % 8;
else buffer->b[buffer->p / 8] &= ~(128u >> buffer->p % 8);
buffer->p++;
return isture;
}
int fpushbit(BIT *buffer, FILE *fp){
memset(buffer, 0, sizeof(BIT));
if((buffer->p = fread(buffer->b, sizeof(char), BITBUFSIZE, fp)) && feof(fp))
buffer->p = (buffer->p - 2) * 8 + buffer->b[buffer->p - 1] + 1;
else buffer->p *= 8;
return buffer->p;
}
int changebit(BIT *buffer, const int isture, const int position){
if(position >= buffer->p) return EOF;
else if(isture) buffer->b[position / 8] |= 128u >> position % 8;
else buffer->b[position / 8] &= ~(128u >> position % 8);
buffer->p++;
return isture;
}
int readbit(const BIT *buffer, const int position){
if(position >= buffer->p) return EOF;
else return buffer->b[position / 8] & (128u >> position % 8);
}
int popbit(BIT *buffer){
if(buffer->p >= BITBUFSIZE * 8 || buffer->p < 0) return EOF;
buffer->p--;
return buffer->b[(buffer->p+1) / 8] & (128u >> (buffer->p+1) % 8);
}