forked from babenek/base91x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase91.h
305 lines (268 loc) · 6.72 KB
/
base91.h
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
MIT License
Copyright (c) 2017 Roman Babenko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <vector>
#include <string>
#if __CHAR_BIT__!=8
#error DESIGNED ONLY FOR 8 BIT BYTE (CHAR)
#endif
//------------------------------------------------------------------------------
/**
* Class base91 provides encoding and decoding statics methods
* using numeric system of base 91 with specific alphabet that does not require
* escaping any symbols in C, C++ string.
* The alphabet contains printable characters of ASCII except:
* " Quotation mark
* ' Apostrophe
* \ Backslash
* An encoded string might be used for JSON string if JSON does not require
* to escate / Slash.
* Encoded string size ~ 1.231 * original size.
* There is possibility to extend the algorithm to use 89 codes during decode.
*/
class base91
{
private:
/*
BASE91 JSON OPTIMIZED ALPHABET:
!~}|{zyxwvutsrqponmlkjihgfedcba`_^]#[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-+*)($&%
*/
/** Base of the numeric system is 91dec equals ASCII symbol [ */
static const char base = '[';
/** Bits in one byte. Should be 8 */
static const unsigned char_bit = __CHAR_BIT__;
/** Pair of base91 symbols might code 13 bits */
static const unsigned b91word_bit = 13;
/** 8192 possibly values for 13 bits */
static const unsigned b91word_size = 0x2000;
/** Mask for 13 bits */
static const unsigned b91word_mask = 0x1FFF;
protected:
/**
* Convert number 0 - 90 to symbol
* @param a[in] - number 0 - 90
* @return symbol from alphabet or -1 if input is out of alphabet
*/
static inline char
encodeSymbol(const char a)
{
switch (a)
{
case '\0': return '!';
break;
case '#': return '#';
break;
case 'X': return '$';
break;
default:
if ('\0' <= a and base > a)
{
return 0x7F ^ a;
}
break;
}
return -1;
}
/**
* Convert base91 symbol to decimal values
* @param a[in] - any symbol
* @return 0-90 if symbol is in alphabet -1 - in other case
*/
static inline char
decodeSymbol(const char a)
{
switch (a)
{
case '!':
{
return '\0';
}
break;
case '#':
{
return '#';
}
break;
case '$':
{
return 'X';
}
break;
default:
{
if ('!' <= a and '~' >= a and '\''!=a and '\"'!=a and '\\'!=a)
{
return 0x7F ^ a;
}
}
break;
}
return -1;
}
public:
/**
* Encode 8bit based container(vector) to string
* @param in - 8bit based std::vector
* @param out - std::string
* @param dummy - do not use
*/
template<typename Container>
static void
encode(const Container &in, std::string &out,
typename std::enable_if<sizeof(typename Container::value_type)==sizeof(char)>::type *
dummy = nullptr)
{
out.clear();
// reserved a bit more than necessary to avoid extra calculation
out.reserve(char_bit + ((2 * char_bit)*in.size()) / (b91word_bit));
auto HI = new char[b91word_size];
auto LO = new char[b91word_size];
{
// initialize encoder
char hi = 0;
char lo = 0;
unsigned n = 0;
while (n < b91word_size)
{
LO[n] = encodeSymbol(lo);
HI[n] = encodeSymbol(hi);
if (base==(++lo))
{
lo = 0;
hi++;
}
n++;
}
}
unsigned collector = 0;
unsigned bit_collected = 0;
for (auto &n : in)
{
collector |= static_cast<unsigned char>(n) << bit_collected;
bit_collected += char_bit;
while (b91word_bit <= bit_collected)
{
const unsigned cod = b91word_mask & collector;
out.push_back(LO[cod]);
out.push_back(HI[cod]);
collector >>= b91word_bit;
bit_collected -= b91word_bit;
}
}
if (0!=bit_collected)
{
const unsigned cod = b91word_mask & collector;
out.push_back(LO[cod]);
if (7 <= bit_collected)
{
out.push_back(HI[cod]);
}
}
delete[] LO;
delete[] HI;
return;
}
/**
* Decode string to binary std::vector
* @param in - std::string
* @param out - std::vector of 8bit elements
* @param dummy - do not use
*/
template<typename Container>
static void
decode(const std::string &in, Container &out,
typename std::enable_if<sizeof(typename Container::value_type)==sizeof(char)>::type *
dummy = nullptr)
{
out.clear();
// reserved a bit more than necessary to avoid extra calculation
out.reserve(char_bit + (b91word_bit*in.size()) / (2 * char_bit));
auto ZYX = new char[1 << char_bit];
for (unsigned n = 0; n < (1 << char_bit); ++n)
{
// fill reverse alphabet
ZYX[n] = decodeSymbol(n);
}
auto HILO = new short[base][base];
{
// fill reverse codes
unsigned hi = 0;
unsigned lo = 0;
for (unsigned n = 0; n < b91word_size; ++n)
{
HILO[hi][lo] = n;
if (base==(++lo))
{
lo = 0;
hi++;
}
}
// subzero values to optional purpose
for (int n = 2; n < static_cast<int>(base); ++n)
{
HILO[base - 1][n] = 0 - n;
}
}
unsigned collector = 0;
int bit_collected = 0;
char lower = -1;
for (auto &n : in)
{
const char digit = ZYX[n];
if (-1==digit)
{
continue;
}
if (-1==lower)
{
lower = digit;
continue;
}
const short cod = HILO[digit][lower];
if ((b91word_mask & cod)!=cod)
{
lower = -1;
continue;
// there is possibility to use 89 subzero values as codes
}
collector |= cod << bit_collected;
bit_collected += b91word_bit;
lower = -1;
while (char_bit <= bit_collected)
{
out.push_back(static_cast<char>(0xFF & collector));
collector >>= char_bit;
bit_collected -= char_bit;
}
}
if (-1!=lower)
{
collector |= HILO[0][lower] << bit_collected;
bit_collected += (char_bit - 1);
}
if (char_bit <= bit_collected)
{
out.push_back(static_cast<char>(0xFF & collector));
}
delete[] HILO;
delete[] ZYX;
}
};
//------------------------------------------------------------------------------