Blame


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