-
Notifications
You must be signed in to change notification settings - Fork 9
/
modp_b64w.c
236 lines (206 loc) · 5.72 KB
/
modp_b64w.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
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
/**
* \file modp_b64w.c
* <PRE>
* MODP_B64 - High performance base64 encoder/decoder
* https://github.com/client9/stringencoders
*
* Copyright © 2005-2016 Nick Galbreath
* Modified by spacewander
* All rights reserved.
* Released under MIT license. See LICENSE for details.
* </PRE>
*/
/*
* If you are ripping this out of the library, comment out the next
* line and uncomment the next lines as approrpiate
*/
//#include "config.h"
/* public header */
#include "modp_stdint.h"
#include "modp_b64w_data.h"
#undef DOPAD
#define BADCHAR 0x01FFFFFF
size_t modp_b64w_encode(char* dest, const char* str, size_t len)
{
size_t i = 0;
const uint8_t* s = (const uint8_t*)str;
uint8_t* p = (uint8_t*)dest;
/* unsigned here is important! */
/* uint8_t is fastest on G4, amd */
/* uint32_t is fastest on Intel */
uint32_t t1, t2, t3;
if (len > 2) {
for (i = 0; i < len - 2; i += 3) {
t1 = s[i];
t2 = s[i + 1];
t3 = s[i + 2];
*p++ = e0[t1];
*p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
*p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
*p++ = e2[t3];
}
}
switch (len - i) {
case 0:
break;
case 1:
t1 = s[i];
*p++ = e0[t1];
*p++ = e1[(t1 & 0x03) << 4];
break;
default: /* case 2 */
t1 = s[i];
t2 = s[i + 1];
*p++ = e0[t1];
*p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
*p++ = e2[(t2 & 0x0F) << 2];
}
return (size_t)(p - (uint8_t*)dest);
}
#ifdef WORDS_BIGENDIAN /* BIG ENDIAN -- SUN / IBM / MOTOROLA */
size_t modp_b64w_decode(char* dest, const char* src, size_t len)
{
size_t i;
if (len == 0)
return 0;
#ifdef DOPAD
/* if padding is used, then the message must be at least
4 chars and be a multiple of 4.
there can be at most 2 pad chars at the end */
if (len < 4 || (len % 4 != 0))
return -1;
if (src[len - 1] == CHARPAD) {
len--;
if (src[len - 1] == CHARPAD) {
len--;
}
}
#endif /* DOPAD */
size_t leftover = len % 4;
size_t chunks = (leftover == 0) ? len / 4 - 1 : len / 4;
uint8_t* p = (uint8_t*)dest;
uint32_t x = 0;
uint32_t* destInt = (uint32_t*)p;
uint32_t* srcInt = (uint32_t*)src;
uint32_t y = *srcInt++;
for (i = 0; i < chunks; ++i) {
x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] | d2[y >> 8 & 0xff] | d3[y & 0xff];
if (x >= BADCHAR)
return -1;
*destInt = x << 8;
p += 3;
destInt = (uint32_t*)p;
y = *srcInt++;
}
switch (leftover) {
case 0:
x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] | d2[y >> 8 & 0xff] | d3[y & 0xff];
if (x >= BADCHAR)
return -1;
*p++ = ((uint8_t*)&x)[1];
*p++ = ((uint8_t*)&x)[2];
*p = ((uint8_t*)&x)[3];
return (chunks + 1) * 3;
#ifndef DOPAD
case 1: /* with padding this is an impossible case */
x = d3[y >> 24];
*p = (uint8_t)x;
break;
#endif
case 2:
x = d3[y >> 24] * 64 + d3[(y >> 16) & 0xff];
*p = (uint8_t)(x >> 4);
break;
default: /* case 3 */
x = (d3[y >> 24] * 64 + d3[(y >> 16) & 0xff]) * 64 + d3[(y >> 8) & 0xff];
*p++ = (uint8_t)(x >> 10);
*p = (uint8_t)(x >> 2);
break;
}
if (x >= BADCHAR)
return -1;
return 3 * chunks + (6 * leftover) / 8;
}
#else /* LITTLE ENDIAN -- INTEL AND FRIENDS */
size_t modp_b64w_decode(char* dest, const char* src, size_t len)
{
size_t i;
size_t leftover;
size_t chunks;
uint8_t* p;
uint8_t* q;
uint32_t x;
uint32_t* destInt;
const uint32_t* srcInt;
uint32_t y;
if (len == 0)
return 0;
#ifdef DOPAD
/*
* if padding is used, then the message must be at least
* 4 chars and be a multiple of 4
*/
if (len < 4 || (len % 4 != 0)) {
return (size_t)-1; /* error */
}
/* there can be at most 2 pad chars at the end */
if (src[len - 1] == CHARPAD) {
len--;
if (src[len - 1] == CHARPAD) {
len--;
}
}
#endif
leftover = len % 4;
chunks = (leftover == 0) ? len / 4 - 1 : len / 4;
p = (uint8_t*)dest;
x = 0;
destInt = (uint32_t*)p;
srcInt = (const uint32_t*)src;
for (i = 0; i < chunks; ++i) {
y = *srcInt;
x = d0[y & 0xff] | d1[(y >> 8) & 0xff] | d2[(y >> 16) & 0xff] | d3[(y >> 24) & 0xff];
if (x >= BADCHAR) {
return (size_t)-1;
}
*destInt = x;
p += 3;
destInt = (uint32_t*)p;
srcInt++;
}
switch (leftover) {
case 0:
y = *srcInt;
x = d0[y & 0xff] | d1[(y >> 8) & 0xff] | d2[(y >> 16) & 0xff] | d3[(y >> 24) & 0xff];
if (x >= BADCHAR) {
return (size_t)-1;
}
*p++ = ((uint8_t*)(&x))[0];
*p++ = ((uint8_t*)(&x))[1];
*p = ((uint8_t*)(&x))[2];
return (chunks + 1) * 3;
#ifndef DOPAD
case 1: /* with padding this is an impossible case */
q = (uint8_t*)srcInt;
x = d0[*q];
*p = *((uint8_t*)(&x)); /* i.e. first char/byte in int */
break;
#endif
case 2: /* case 2, 1 output byte */
q = (uint8_t*)srcInt;
x = d0[*q] | d1[*(q+1)];
*p = *((uint8_t*)(&x)); /* i.e. first char */
break;
default: /* case 3, 2 output bytes */
q = (uint8_t*)srcInt;
x = d0[*q] | d1[*(q+1)] | d2[*(q+2)]; /* 0x3c */
*p++ = ((uint8_t*)(&x))[0];
*p = ((uint8_t*)(&x))[1];
break;
}
if (x >= BADCHAR) {
return (size_t)-1;
}
return 3 * chunks + (6 * leftover) / 8;
}
#endif /* if bigendian / else / endif */