This repository was archived by the owner on Jul 27, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathBaseEncoding.cpp
More file actions
110 lines (103 loc) · 2.72 KB
/
Copy pathBaseEncoding.cpp
File metadata and controls
110 lines (103 loc) · 2.72 KB
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
#include "BaseEncoding.h"
#include"algo_ext.h"
using namespace std;
static const string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static const string base32_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"234567";
static const string base16_chars =
"0123456789"
"ABCDEF";
static string enmap(string &arr, const int targ_bit) {
int fillbit = 0;
string ret, unitbuf;
int unit_bits = lcm(targ_bit, 8);
int orig_unit = unit_bits / 8;
int mapd_unit = unit_bits / targ_bit;
while (arr.size() % orig_unit)arr += '\0', fillbit += 8;
for (int i = 0; i < arr.size(); i += orig_unit) {
int step = 0;
unitbuf = ""; unitbuf.resize(mapd_unit);
for (int j = 0; j < mapd_unit; j++) {
for (int k = 0; k < targ_bit; k++, step++)
unitbuf[j] |=
((arr[i + step / 8] >> (7 - step % 8)) & 1) << (targ_bit - k - 1);
}
ret += unitbuf;
}
while (fillbit > targ_bit)
ret.pop_back(), fillbit -= targ_bit;
return ret;
}
static string demap(string &arr, const int orig_bit) {
string ret, unitbuf;
int unit_bits = lcm(orig_bit, 8);
int orig_unit = unit_bits / 8;
int mapd_unit = unit_bits / orig_bit;
for (int i = 0; i < arr.size(); i += mapd_unit) {
int step = 0;
unitbuf = ""; unitbuf.resize(orig_unit);
for (int j = 0; j < orig_unit; j++) {
for (int k = 0; k < 8; k++, step++)
unitbuf[j] |=
((arr[i + step / orig_bit] >> (orig_bit - 1 - step % orig_bit) & 1) << (7 - k));
}
ret += unitbuf;
}
while (ret[ret.size() - 1] == 0)
ret.pop_back();
return ret;
}
string base64::b64encode(string txt) {
txt = enmap(txt, 6);
for (int i = 0; i < txt.size(); i++)
txt[i] = base64_chars[txt[i]];
while (txt.size() % 4)txt += '=';
return txt;
}
string base64::b64decode(string enc) {
for (int i = 0; i < enc.size(); i++) {
if (enc[i] == '=')enc[i] = 0;
else enc[i] = base64_chars.find(enc[i]);
if (enc[i] == -1)
return "not base64 encoded string";
}
enc = demap(enc, 6);
return enc;
}
string base64::b32encode(string txt) {
txt = enmap(txt, 5);
for (int i = 0; i < txt.size(); i++)
txt[i] = base32_chars[txt[i]];
while (txt.size() % 8)txt += '=';
return txt;
}
string base64::b32decode(string enc) {
for (int i = 0; i < enc.size(); i++) {
if (enc[i] == '=')enc[i] = 0;
else enc[i] = base32_chars.find(enc[i]);
if (enc[i] == -1)
return "not base64 encoded string";
}
enc = demap(enc, 5);
return enc;
}
string base64::b16encode(string txt) {
txt = enmap(txt, 4);
for (int i = 0; i < txt.size(); i++)
txt = base16_chars[txt[i]];
// no need to append =
return txt;
}
string base64::b16decode(string enc) {
for (int i = 0; i < enc.size(); i++) {
enc[i] = base16_chars.find(enc[i]);
if (enc[i] == -1)
return "not base64 encoded string";
}
enc = demap(enc, 4);
return enc;
}