-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.c
More file actions
174 lines (137 loc) · 5.03 KB
/
Copy pathbase64.c
File metadata and controls
174 lines (137 loc) · 5.03 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
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
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#define always_inline inline __attribute__((always_inline))
struct slice_t {
char * buf;
size_t len;
size_t cap;
};
/** Exported Functions **/
void b64encode(struct slice_t *out, const struct slice_t *src);
size_t b64decode(struct slice_t *out, const char *src, size_t nb);
/** Encoder Helper Functions **/
static const char TabEncodeCharsetStd[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static always_inline void encode(char *out, char *src, const char *tab) {
out[0] = tab[(src[0] >> 2)];
out[1] = tab[((src[0] << 4) & 0x30) | (src[1] >> 4)];
out[2] = tab[((src[1] << 2) & 0x3C) | (src[2] >> 6)];
out[3] = tab[src[2] & 0x3F];
}
static always_inline void encode_remainder(char *out, char *src, char *src_end, const char *tab) {
out[0] = tab[(src[0] >> 2)];
if (src+1 == src_end) { // 1 char left
out[1] = tab[(src[0] << 4) & 0x30];
out[2] = '=';
} else { // 2 char left
out[1] = tab[((src[0] << 4) & 0x30) | (src[1] >> 4)];
out[2] = tab[(src[1] << 2) & 0x3C];
}
out[3] = '=';
}
/** Function Implementations **/
void b64encode(struct slice_t *out, const struct slice_t *src) {
char * out_start = out->buf + out->len;
char * out_i = out->buf + out->len;
char * src_i = src->buf;
char * src_end = src->buf + src->len;
const char * st = TabEncodeCharsetStd;
/* check for empty string */
if (src->len == 0) {
return;
}
/* converting 3 bytes to 4 bytes */
while (src_i + 3 <= src_end) {
encode(out_i, src_i, st);
src_i += 3;
out_i += 4;
}
/* converting 1/2 bytes to 4 bytes */
if (src_i < src_end) {
encode_remainder(out_i, src_i, src_end, st);
src_i = src_end;
out_i += 4;
}
/* update slice */
out->len += out_i - out_start;
}
static const u_int8_t TabDecodeCharsetStd[128] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x3f,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
};
static always_inline void decode(char *out, char *src, const char *tab) {
out[0] = (tab[src[0]] << 2) | ((tab[src[1]] >> 4) & 0x03);
out[1] = (tab[src[1]] << 4) | (tab[src[2]] >> 2);
out[2] = (tab[src[2]] << 6) | tab[src[3]];
}
size_t b64decode(struct slice_t *out, const char *src, size_t nb) {
char * out_start = out->buf + out->len;
char * out_i = out->buf + out->len;
char * src_end = src + nb;
const char * st = TabDecodeCharsetStd;
/* check for empty string */
if (nb == 0) {
return 0;
}
/* converting 4 bytes to 3 bytes */
while (src + 4 <= src_end) {
decode(out_i, src, st);
src += 4;
out_i += 3;
}
return nb;
}
void test_encode() {
struct slice_t src, out;
char static_buf[] = "Hello";
src.buf = static_buf; // Указатель на строку
src.len = strlen(static_buf); // Длина строки
src.cap = strlen(static_buf); // sizeof Вместимость буфера (включая '\0')
const int cap = ((sizeof(static_buf)/sizeof(char)) / 3) * 4;
static char empty_buf[cap];
out.buf = &empty_buf[0];
out.len = 0;
out.cap = cap;
printf("src.Buffer: %s\n", src.buf);
printf("src.Length: %zu\n", src.len);
printf("src.Capacity: %zu\n", src.cap);
b64encode(&out, &src);
printf("Result: ");
for (const char *p = out.buf; p < out.buf + out.len; p++) {
printf("%c", *p);
}
printf("\n");
printf("out.Buffer: %s\n", out.buf);
printf("out.Length: %zu\n", out.len);
printf("out.Capacity: %zu\n", out.cap);
}
void test_decode() {
struct slice_t src, out;
char static_buf[] = "SGVsbG8=";
src.buf = static_buf;
src.len = strlen(static_buf);
src.cap = strlen(static_buf);
const int cap = ((sizeof(static_buf)/sizeof(char)) / 4) * 3;
char empty_buf[cap];
out.buf = &empty_buf[0];
out.len = 0;
out.cap = cap;
printf("Buffer: %s\n", src.buf);
printf("Length: %zu\n", src.len);
printf("Capacity: %zu\n", src.cap);
b64decode(&out, src.buf, src.len);
printf("Result: \n");
for (const char *p = out.buf; p < out.buf + out.cap; p++) {
printf("%c", *p);
}
}
void main() {
test_encode();
test_decode();
}