CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/data/headers/windows/base64.h
Views: 1904
1
/* from https://github.com/mdornseif/didentd */
2
/* public domain
3
* BASE64 on stdin -> converted data on stdout */
4
5
/* arbitrary data on stdin -> BASE64 data on stdout
6
* UNIX's newline convention is used, i.e. one ASCII control-j (10 decimal).
7
*
8
* public domain
9
*/
10
11
/* Hacked by [email protected] to be a library function working on memory blocks
12
*
13
*/
14
15
static unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
16
17
int base64decode(char *dest, const char *src, int l)
18
{
19
static char inalphabet[256], decoder[256];
20
static short table_initialized = 0;
21
int i, bits, c, char_count;
22
int rpos;
23
int wpos = 0;
24
25
if (!table_initialized) {
26
for (i = (sizeof alphabet) - 1; i >= 0; i--) {
27
inalphabet[alphabet[i]] = 1;
28
decoder[alphabet[i]] = i;
29
}
30
table_initialized = 1;
31
}
32
33
char_count = 0;
34
bits = 0;
35
for (rpos = 0; rpos < l; rpos++) {
36
c = src[rpos];
37
38
if (c == '=') {
39
break;
40
}
41
42
if (c > 255 || !inalphabet[c]) {
43
return -1;
44
}
45
46
bits += decoder[c];
47
char_count++;
48
if (char_count < 4) {
49
bits <<= 6;
50
} else {
51
dest[wpos++] = bits >> 16;
52
dest[wpos++] = (bits >> 8) & 0xff;
53
dest[wpos++] = bits & 0xff;
54
bits = 0;
55
char_count = 0;
56
}
57
}
58
59
switch (char_count) {
60
case 1:
61
return -1;
62
break;
63
case 2:
64
dest[wpos++] = bits >> 10;
65
break;
66
case 3:
67
dest[wpos++] = bits >> 16;
68
dest[wpos++] = (bits >> 8) & 0xff;
69
break;
70
}
71
72
return wpos;
73
}
74
75
int base64encode(char *dest, const char *src, int l)
76
{
77
int bits, c, char_count;
78
int rpos;
79
int wpos = 0;
80
81
char_count = 0;
82
bits = 0;
83
84
for (rpos = 0; rpos < l; rpos++) {
85
c = src[rpos];
86
87
bits += c;
88
char_count++;
89
if (char_count < 3) {
90
bits <<= 8;
91
} else {
92
dest[wpos++] = alphabet[bits >> 18];
93
dest[wpos++] = alphabet[(bits >> 12) & 0x3f];
94
dest[wpos++] = alphabet[(bits >> 6) & 0x3f];
95
dest[wpos++] = alphabet[bits & 0x3f];
96
bits = 0;
97
char_count = 0;
98
}
99
}
100
101
if (char_count != 0) {
102
bits <<= 16 - (8 * char_count);
103
dest[wpos++] = alphabet[bits >> 18];
104
dest[wpos++] = alphabet[(bits >> 12) & 0x3f];
105
if (char_count == 1) {
106
dest[wpos++] = '=';
107
dest[wpos++] = '=';
108
} else {
109
dest[wpos++] = alphabet[(bits >> 6) & 0x3f];
110
dest[wpos++] = '=';
111
}
112
}
113
return wpos;
114
}
115