-
Notifications
You must be signed in to change notification settings - Fork 0
/
basilikum.c
157 lines (131 loc) · 2.87 KB
/
basilikum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
static const char* const alphabet =
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"-.";
static signed char unalpha[256];
typedef unsigned long long ULL;
void basilikum_init()
{
for(unsigned u=0; u<256; ++u)
unalpha[u] = -1;
for(unsigned u=0; u<=62; ++u)
{
unalpha[ (unsigned char)alphabet[u] ] = u;
}
}
static
int sidx(const char* input, size_t max_len, size_t idx)
{
const int c = idx<max_len ? input[idx] : 32;
return (c<32 || c>126) ? 94 : c-32;
}
static
size_t base62(char* out, uint64_t coll, unsigned length)
{
size_t written = 0;
while(length){
unsigned rem = coll % 62;
coll = coll / 62;
*out = alphabet[rem];
++out;
++written;
--length;
};
return written;
}
char* basilikum_encode(const char* input, const unsigned long long secret)
{
const size_t input_len = strlen(input);
const size_t input_pkt = input_len/9;
const size_t input_rem = input_len%9;
const size_t output_len = (input_pkt * 10) + (input_rem ? input_rem+1u : 0u);
char* output = calloc(1, output_len + 1);
// memset(output, '#', output_len);
char* optr = output;
unsigned long long rs = secret;
unsigned iidx = 0;
uint64_t factor = 1;
uint64_t coll = 0;
for(size_t i=0; i<input_len; ++i)
{
const int si = sidx(input, input_len, i);
coll += (si + (rs%89))%96 * factor;
// coll += (si * factor);
rs = rs / 89;
if(rs<101)
rs += secret;
factor *= 96;
if(++iidx == 9)
{
optr += base62(optr, coll, iidx+1);
iidx = 0;
factor = 1;
coll = 0;
}
}
if(iidx)
{
base62(optr, coll, iidx+1);
}
return output;
};
static
void unbase96(char* out, uint64_t coll, unsigned iidx, unsigned long long* rs, const unsigned long long secret)
{
while(--iidx)
{
*out = ' '+ (96 + (coll % 96) - *rs%89)%96;
coll = coll/96;
++out;
*rs = *rs / 89;
if(*rs<101)
*rs += secret;
}
}
char* basilikum_decode(const char* input, const unsigned long long secret)
{
const size_t input_len = strlen(input);
const size_t input_pkt = input_len/10;
const size_t input_rem = input_len%10;
const size_t output_len = (input_pkt * 9) + (input_rem ? input_rem-1u : 0u);
char* output = calloc(1, output_len + 1);
char* optr = output;
unsigned long long rs = secret;
unsigned iidx = 0;
uint64_t factor = 1;
uint64_t coll = 0;
for(size_t i=0; i<input_len; ++i)
{
const int val = unalpha[ (unsigned char) input[i] ];
if(val<0)
{
printf("!ERROR!\n"); fflush(stdout);
continue;
}
coll += val*factor;
++iidx;
factor *=62;
if(iidx==10)
{
unbase96(optr, coll, iidx, &rs, secret);
optr += iidx-1;
assert(optr-output <= output_len);
iidx = 0;
factor = 1;
coll=0;
}
}
if(iidx)
{
unbase96(optr, coll, iidx, &rs, secret);
optr += iidx-1;
assert(optr-output <= output_len);
}
return output;
}