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