Blame
Date:
Thu Oct 15 07:11:48 2020 UTC
Message:
up the debug a bit further while we're chasing a bug.
001
2014-11-21
pjp
/* $OpenBSD: base64.c,v 1.7 2013/12/31 02:32:56 tedu Exp $ */
002
2014-11-21
pjp
003
2014-11-21
pjp
/*
004
2014-11-21
pjp
* Copyright (c) 1996 by Internet Software Consortium.
005
2014-11-21
pjp
*
006
2014-11-21
pjp
* Permission to use, copy, modify, and distribute this software for any
007
2014-11-21
pjp
* purpose with or without fee is hereby granted, provided that the above
008
2014-11-21
pjp
* copyright notice and this permission notice appear in all copies.
009
2014-11-21
pjp
*
010
2014-11-21
pjp
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
011
2014-11-21
pjp
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
012
2014-11-21
pjp
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
013
2014-11-21
pjp
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
014
2014-11-21
pjp
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
015
2014-11-21
pjp
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
016
2014-11-21
pjp
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
017
2014-11-21
pjp
* SOFTWARE.
018
2014-11-21
pjp
*/
019
2014-11-21
pjp
020
2014-11-21
pjp
/*
021
2014-11-21
pjp
* Portions Copyright (c) 1995 by International Business Machines, Inc.
022
2014-11-21
pjp
*
023
2014-11-21
pjp
* International Business Machines, Inc. (hereinafter called IBM) grants
024
2014-11-21
pjp
* permission under its copyrights to use, copy, modify, and distribute this
025
2014-11-21
pjp
* Software with or without fee, provided that the above copyright notice and
026
2014-11-21
pjp
* all paragraphs of this notice appear in all copies, and that the name of IBM
027
2014-11-21
pjp
* not be used in connection with the marketing of any product incorporating
028
2014-11-21
pjp
* the Software or modifications thereof, without specific, written prior
029
2014-11-21
pjp
* permission.
030
2014-11-21
pjp
*
031
2014-11-21
pjp
* To the extent it has a right to do so, IBM grants an immunity from suit
032
2014-11-21
pjp
* under its patents, if any, for the use, sale or manufacture of products to
033
2014-11-21
pjp
* the extent that such products are used for performing Domain Name System
034
2014-11-21
pjp
* dynamic updates in TCP/IP networks by means of the Software. No immunity is
035
2014-11-21
pjp
* granted for any product per se or for any other function of any product.
036
2014-11-21
pjp
*
037
2014-11-21
pjp
* THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
038
2014-11-21
pjp
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
039
2014-11-21
pjp
* PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
040
2014-11-21
pjp
* DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
041
2014-11-21
pjp
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
042
2014-11-21
pjp
* IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
043
2014-11-21
pjp
*/
044
2014-11-21
pjp
045
2017-10-26
pjp
/*
046
2020-04-11
pjp
* $Id: base64.c,v 1.3 2020/04/11 06:00:07 pjp Exp $
047
2017-10-26
pjp
*/
048
2017-10-26
pjp
049
2014-11-21
pjp
#include <sys/types.h>
050
2014-11-21
pjp
#include <sys/socket.h>
051
2014-11-21
pjp
#include <netinet/in.h>
052
2014-11-21
pjp
#include <arpa/inet.h>
053
2014-11-21
pjp
#include <arpa/nameser.h>
054
2014-11-21
pjp
055
2014-11-21
pjp
#include <ctype.h>
056
2014-11-21
pjp
#include <resolv.h>
057
2014-11-21
pjp
#include <stdio.h>
058
2014-11-21
pjp
059
2014-11-21
pjp
#include <stdlib.h>
060
2014-11-21
pjp
#include <string.h>
061
2014-11-21
pjp
062
2014-11-21
pjp
static const char Base64[] =
063
2014-11-21
pjp
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
064
2014-11-21
pjp
static const char Pad64 = '=';
065
2014-11-21
pjp
066
2014-11-21
pjp
/* these prototype names shouldn't collide with anything */
067
2014-11-21
pjp
int mybase64_encode(u_char const *, size_t, char *, size_t);
068
2014-11-21
pjp
int mybase64_decode(char const *, u_char *, size_t);
069
2014-11-21
pjp
070
2014-11-21
pjp
/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
071
2014-11-21
pjp
The following encoding technique is taken from RFC 1521 by Borenstein
072
2014-11-21
pjp
and Freed. It is reproduced here in a slightly edited form for
073
2014-11-21
pjp
convenience.
074
2014-11-21
pjp
075
2014-11-21
pjp
A 65-character subset of US-ASCII is used, enabling 6 bits to be
076
2014-11-21
pjp
represented per printable character. (The extra 65th character, "=",
077
2014-11-21
pjp
is used to signify a special processing function.)
078
2014-11-21
pjp
079
2014-11-21
pjp
The encoding process represents 24-bit groups of input bits as output
080
2014-11-21
pjp
strings of 4 encoded characters. Proceeding from left to right, a
081
2014-11-21
pjp
24-bit input group is formed by concatenating 3 8-bit input groups.
082
2014-11-21
pjp
These 24 bits are then treated as 4 concatenated 6-bit groups, each
083
2014-11-21
pjp
of which is translated into a single digit in the base64 alphabet.
084
2014-11-21
pjp
085
2014-11-21
pjp
Each 6-bit group is used as an index into an array of 64 printable
086
2014-11-21
pjp
characters. The character referenced by the index is placed in the
087
2014-11-21
pjp
output string.
088
2014-11-21
pjp
089
2014-11-21
pjp
Table 1: The Base64 Alphabet
090
2014-11-21
pjp
091
2014-11-21
pjp
Value Encoding Value Encoding Value Encoding Value Encoding
092
2014-11-21
pjp
0 A 17 R 34 i 51 z
093
2014-11-21
pjp
1 B 18 S 35 j 52 0
094
2014-11-21
pjp
2 C 19 T 36 k 53 1
095
2014-11-21
pjp
3 D 20 U 37 l 54 2
096
2014-11-21
pjp
4 E 21 V 38 m 55 3
097
2014-11-21
pjp
5 F 22 W 39 n 56 4
098
2014-11-21
pjp
6 G 23 X 40 o 57 5
099
2014-11-21
pjp
7 H 24 Y 41 p 58 6
100
2014-11-21
pjp
8 I 25 Z 42 q 59 7
101
2014-11-21
pjp
9 J 26 a 43 r 60 8
102
2014-11-21
pjp
10 K 27 b 44 s 61 9
103
2014-11-21
pjp
11 L 28 c 45 t 62 +
104
2014-11-21
pjp
12 M 29 d 46 u 63 /
105
2014-11-21
pjp
13 N 30 e 47 v
106
2014-11-21
pjp
14 O 31 f 48 w (pad) =
107
2014-11-21
pjp
15 P 32 g 49 x
108
2014-11-21
pjp
16 Q 33 h 50 y
109
2014-11-21
pjp
110
2014-11-21
pjp
Special processing is performed if fewer than 24 bits are available
111
2014-11-21
pjp
at the end of the data being encoded. A full encoding quantum is
112
2014-11-21
pjp
always completed at the end of a quantity. When fewer than 24 input
113
2014-11-21
pjp
bits are available in an input group, zero bits are added (on the
114
2014-11-21
pjp
right) to form an integral number of 6-bit groups. Padding at the
115
2014-11-21
pjp
end of the data is performed using the '=' character.
116
2014-11-21
pjp
117
2014-11-21
pjp
Since all base64 input is an integral number of octets, only the
118
2014-11-21
pjp
-------------------------------------------------
119
2014-11-21
pjp
following cases can arise:
120
2014-11-21
pjp
121
2014-11-21
pjp
(1) the final quantum of encoding input is an integral
122
2014-11-21
pjp
multiple of 24 bits; here, the final unit of encoded
123
2014-11-21
pjp
output will be an integral multiple of 4 characters
124
2014-11-21
pjp
with no "=" padding,
125
2014-11-21
pjp
(2) the final quantum of encoding input is exactly 8 bits;
126
2014-11-21
pjp
here, the final unit of encoded output will be two
127
2014-11-21
pjp
characters followed by two "=" padding characters, or
128
2014-11-21
pjp
(3) the final quantum of encoding input is exactly 16 bits;
129
2014-11-21
pjp
here, the final unit of encoded output will be three
130
2014-11-21
pjp
characters followed by one "=" padding character.
131
2014-11-21
pjp
*/
132
2014-11-21
pjp
133
2014-11-21
pjp
int
134
2014-11-21
pjp
mybase64_encode(src, srclength, target, targsize)
135
2014-11-21
pjp
u_char const *src;
136
2014-11-21
pjp
size_t srclength;
137
2014-11-21
pjp
char *target;
138
2014-11-21
pjp
size_t targsize;
139
2014-11-21
pjp
{
140
2014-11-21
pjp
size_t datalength = 0;
141
2014-11-21
pjp
u_char input[3];
142
2014-11-21
pjp
u_char output[4];
143
2014-11-21
pjp
int i;
144
2014-11-21
pjp
145
2014-11-21
pjp
while (2 < srclength) {
146
2014-11-21
pjp
input[0] = *src++;
147
2014-11-21
pjp
input[1] = *src++;
148
2014-11-21
pjp
input[2] = *src++;
149
2014-11-21
pjp
srclength -= 3;
150
2014-11-21
pjp
151
2014-11-21
pjp
output[0] = input[0] >> 2;
152
2014-11-21
pjp
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
153
2014-11-21
pjp
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
154
2014-11-21
pjp
output[3] = input[2] & 0x3f;
155
2014-11-21
pjp
156
2014-11-21
pjp
if (datalength + 4 > targsize)
157
2014-11-21
pjp
return (-1);
158
2014-11-21
pjp
target[datalength++] = Base64[output[0]];
159
2014-11-21
pjp
target[datalength++] = Base64[output[1]];
160
2014-11-21
pjp
target[datalength++] = Base64[output[2]];
161
2014-11-21
pjp
target[datalength++] = Base64[output[3]];
162
2014-11-21
pjp
}
163
2014-11-21
pjp
164
2014-11-21
pjp
/* Now we worry about padding. */
165
2014-11-21
pjp
if (0 != srclength) {
166
2014-11-21
pjp
/* Get what's left. */
167
2014-11-21
pjp
input[0] = input[1] = input[2] = '\0';
168
2014-11-21
pjp
for (i = 0; i < srclength; i++)
169
2014-11-21
pjp
input[i] = *src++;
170
2014-11-21
pjp
171
2014-11-21
pjp
output[0] = input[0] >> 2;
172
2014-11-21
pjp
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
173
2014-11-21
pjp
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
174
2014-11-21
pjp
175
2014-11-21
pjp
if (datalength + 4 > targsize)
176
2014-11-21
pjp
return (-1);
177
2014-11-21
pjp
target[datalength++] = Base64[output[0]];
178
2014-11-21
pjp
target[datalength++] = Base64[output[1]];
179
2014-11-21
pjp
if (srclength == 1)
180
2014-11-21
pjp
target[datalength++] = Pad64;
181
2014-11-21
pjp
else
182
2014-11-21
pjp
target[datalength++] = Base64[output[2]];
183
2014-11-21
pjp
target[datalength++] = Pad64;
184
2014-11-21
pjp
}
185
2014-11-21
pjp
if (datalength >= targsize)
186
2014-11-21
pjp
return (-1);
187
2014-11-21
pjp
target[datalength] = '\0'; /* Returned value doesn't count \0. */
188
2014-11-21
pjp
return (datalength);
189
2014-11-21
pjp
}
190
2014-11-21
pjp
191
2014-11-21
pjp
/* skips all whitespace anywhere.
192
2014-11-21
pjp
converts characters, four at a time, starting at (or after)
193
2014-11-21
pjp
src from base - 64 numbers into three 8 bit bytes in the target area.
194
2014-11-21
pjp
it returns the number of data bytes stored at the target, or -1 on error.
195
2014-11-21
pjp
*/
196
2014-11-21
pjp
197
2014-11-21
pjp
int
198
2014-11-21
pjp
mybase64_decode(src, target, targsize)
199
2014-11-21
pjp
char const *src;
200
2014-11-21
pjp
u_char *target;
201
2014-11-21
pjp
size_t targsize;
202
2014-11-21
pjp
{
203
2014-11-21
pjp
int tarindex, state, ch;
204
2014-11-21
pjp
u_char nextbyte;
205
2014-11-21
pjp
char *pos;
206
2014-11-21
pjp
207
2014-11-21
pjp
state = 0;
208
2014-11-21
pjp
tarindex = 0;
209
2014-11-21
pjp
210
2014-11-21
pjp
while ((ch = (unsigned char)*src++) != '\0') {
211
2014-11-21
pjp
if (isspace(ch)) /* Skip whitespace anywhere. */
212
2014-11-21
pjp
continue;
213
2014-11-21
pjp
214
2014-11-21
pjp
if (ch == Pad64)
215
2014-11-21
pjp
break;
216
2014-11-21
pjp
217
2014-11-21
pjp
pos = strchr(Base64, ch);
218
2014-11-21
pjp
if (pos == 0) /* A non-base64 character. */
219
2014-11-21
pjp
return (-1);
220
2014-11-21
pjp
221
2014-11-21
pjp
switch (state) {
222
2014-11-21
pjp
case 0:
223
2014-11-21
pjp
if (target) {
224
2014-11-21
pjp
if (tarindex >= targsize)
225
2014-11-21
pjp
return (-1);
226
2014-11-21
pjp
target[tarindex] = (pos - Base64) << 2;
227
2014-11-21
pjp
}
228
2014-11-21
pjp
state = 1;
229
2014-11-21
pjp
break;
230
2014-11-21
pjp
case 1:
231
2014-11-21
pjp
if (target) {
232
2014-11-21
pjp
if (tarindex >= targsize)
233
2014-11-21
pjp
return (-1);
234
2014-11-21
pjp
target[tarindex] |= (pos - Base64) >> 4;
235
2014-11-21
pjp
nextbyte = ((pos - Base64) & 0x0f) << 4;
236
2014-11-21
pjp
if (tarindex + 1 < targsize)
237
2014-11-21
pjp
target[tarindex+1] = nextbyte;
238
2014-11-21
pjp
else if (nextbyte)
239
2014-11-21
pjp
return (-1);
240
2014-11-21
pjp
}
241
2014-11-21
pjp
tarindex++;
242
2014-11-21
pjp
state = 2;
243
2014-11-21
pjp
break;
244
2014-11-21
pjp
case 2:
245
2014-11-21
pjp
if (target) {
246
2014-11-21
pjp
if (tarindex >= targsize)
247
2014-11-21
pjp
return (-1);
248
2014-11-21
pjp
target[tarindex] |= (pos - Base64) >> 2;
249
2014-11-21
pjp
nextbyte = ((pos - Base64) & 0x03) << 6;
250
2014-11-21
pjp
if (tarindex + 1 < targsize)
251
2014-11-21
pjp
target[tarindex+1] = nextbyte;
252
2014-11-21
pjp
else if (nextbyte)
253
2014-11-21
pjp
return (-1);
254
2014-11-21
pjp
}
255
2014-11-21
pjp
tarindex++;
256
2014-11-21
pjp
state = 3;
257
2014-11-21
pjp
break;
258
2014-11-21
pjp
case 3:
259
2014-11-21
pjp
if (target) {
260
2014-11-21
pjp
if (tarindex >= targsize)
261
2014-11-21
pjp
return (-1);
262
2014-11-21
pjp
target[tarindex] |= (pos - Base64);
263
2014-11-21
pjp
}
264
2014-11-21
pjp
tarindex++;
265
2014-11-21
pjp
state = 0;
266
2014-11-21
pjp
break;
267
2014-11-21
pjp
}
268
2014-11-21
pjp
}
269
2014-11-21
pjp
270
2014-11-21
pjp
/*
271
2014-11-21
pjp
* We are done decoding Base-64 chars. Let's see if we ended
272
2014-11-21
pjp
* on a byte boundary, and/or with erroneous trailing characters.
273
2014-11-21
pjp
*/
274
2014-11-21
pjp
275
2014-11-21
pjp
if (ch == Pad64) { /* We got a pad char. */
276
2014-11-21
pjp
ch = (unsigned char)*src++; /* Skip it, get next. */
277
2014-11-21
pjp
switch (state) {
278
2014-11-21
pjp
case 0: /* Invalid = in first position */
279
2014-11-21
pjp
case 1: /* Invalid = in second position */
280
2014-11-21
pjp
return (-1);
281
2014-11-21
pjp
282
2014-11-21
pjp
case 2: /* Valid, means one byte of info */
283
2014-11-21
pjp
/* Skip any number of spaces. */
284
2014-11-21
pjp
for (; ch != '\0'; ch = (unsigned char)*src++)
285
2014-11-21
pjp
if (!isspace(ch))
286
2014-11-21
pjp
break;
287
2014-11-21
pjp
/* Make sure there is another trailing = sign. */
288
2014-11-21
pjp
if (ch != Pad64)
289
2014-11-21
pjp
return (-1);
290
2014-11-21
pjp
ch = (unsigned char)*src++; /* Skip the = */
291
2014-11-21
pjp
/* Fall through to "single trailing =" case. */
292
2014-11-21
pjp
/* FALLTHROUGH */
293
2014-11-21
pjp
294
2014-11-21
pjp
case 3: /* Valid, means two bytes of info */
295
2014-11-21
pjp
/*
296
2014-11-21
pjp
* We know this char is an =. Is there anything but
297
2014-11-21
pjp
* whitespace after it?
298
2014-11-21
pjp
*/
299
2014-11-21
pjp
for (; ch != '\0'; ch = (unsigned char)*src++)
300
2014-11-21
pjp
if (!isspace(ch))
301
2014-11-21
pjp
return (-1);
302
2014-11-21
pjp
303
2014-11-21
pjp
/*
304
2014-11-21
pjp
* Now make sure for cases 2 and 3 that the "extra"
305
2014-11-21
pjp
* bits that slopped past the last full byte were
306
2014-11-21
pjp
* zeros. If we don't check them, they become a
307
2014-11-21
pjp
* subliminal channel.
308
2014-11-21
pjp
*/
309
2014-11-21
pjp
if (target && tarindex < targsize &&
310
2014-11-21
pjp
target[tarindex] != 0)
311
2014-11-21
pjp
return (-1);
312
2014-11-21
pjp
}
313
2014-11-21
pjp
} else {
314
2014-11-21
pjp
/*
315
2014-11-21
pjp
* We ended by seeing the end of the string. Make sure we
316
2014-11-21
pjp
* have no partial bytes lying around.
317
2014-11-21
pjp
*/
318
2014-11-21
pjp
if (state != 0)
319
2014-11-21
pjp
return (-1);
320
2014-11-21
pjp
}
321
2014-11-21
pjp
322
2014-11-21
pjp
return (tarindex);
323
2014-11-21
pjp
}
repomaster@centroid.eu