]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/ldns/compat/b32_pton.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / ldns / compat / b32_pton.c
1 /*
2  * Copyright (c) 1996, 1998 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17
18 /*
19  * Portions Copyright (c) 1995 by International Business Machines, Inc.
20  *
21  * International Business Machines, Inc. (hereinafter called IBM) grants
22  * permission under its copyrights to use, copy, modify, and distribute this
23  * Software with or without fee, provided that the above copyright notice and
24  * all paragraphs of this notice appear in all copies, and that the name of IBM
25  * not be used in connection with the marketing of any product incorporating
26  * the Software or modifications thereof, without specific, written prior
27  * permission.
28  *
29  * To the extent it has a right to do so, IBM grants an immunity from suit
30  * under its patents, if any, for the use, sale or manufacture of products to
31  * the extent that such products are used for performing Domain Name System
32  * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
33  * granted for any product per se or for any other function of any product.
34  *
35  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
36  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
38  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
39  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
40  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
41  */
42 #include <ldns/config.h>
43 #ifndef HAVE_B32_PTON
44
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #ifdef HAVE_SYS_SOCKET_H
48 #include <sys/socket.h>
49 #endif
50
51 #ifdef HAVE_NETINET_IN_H
52 #include <netinet/in.h>
53 #endif
54 #ifdef HAVE_ARPA_INET_H
55 #include <arpa/inet.h>
56 #endif
57
58 #include <ctype.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #include <ldns/util.h>
64
65 /*      "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";*/
66 static const char Base32[] =
67         "abcdefghijklmnopqrstuvwxyz234567";
68 /*      "0123456789ABCDEFGHIJKLMNOPQRSTUV";*/
69 static const char Base32_extended_hex[] =
70         "0123456789abcdefghijklmnopqrstuv";
71 static const char Pad32 = '=';
72
73 /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
74 5.  Base 32 Encoding
75
76    The Base 32 encoding is designed to represent arbitrary sequences of
77    octets in a form that needs to be case insensitive but need not be
78    humanly readable.
79
80    A 33-character subset of US-ASCII is used, enabling 5 bits to be
81    represented per printable character.  (The extra 33rd character, "=",
82    is used to signify a special processing function.)
83
84    The encoding process represents 40-bit groups of input bits as output
85    strings of 8 encoded characters.  Proceeding from left to right, a
86    40-bit input group is formed by concatenating 5 8bit input groups.
87    These 40 bits are then treated as 8 concatenated 5-bit groups, each
88    of which is translated into a single digit in the base 32 alphabet.
89    When encoding a bit stream via the base 32 encoding, the bit stream
90    must be presumed to be ordered with the most-significant-bit first.
91    That is, the first bit in the stream will be the high-order bit in
92    the first 8bit byte, and the eighth bit will be the low-order bit in
93    the first 8bit byte, and so on.
94
95    Each 5-bit group is used as an index into an array of 32 printable
96    characters.  The character referenced by the index is placed in the
97    output string.  These characters, identified in Table 3, below, are
98    selected from US-ASCII digits and uppercase letters.
99
100                       Table 3: The Base 32 Alphabet
101
102          Value Encoding  Value Encoding  Value Encoding  Value Encoding
103              0 A             9 J            18 S            27 3
104              1 B            10 K            19 T            28 4
105              2 C            11 L            20 U            29 5
106              3 D            12 M            21 V            30 6
107              4 E            13 N            22 W            31 7
108              5 F            14 O            23 X
109              6 G            15 P            24 Y         (pad) =
110              7 H            16 Q            25 Z
111              8 I            17 R            26 2
112
113
114    Special processing is performed if fewer than 40 bits are available
115    at the end of the data being encoded.  A full encoding quantum is
116    always completed at the end of a body.  When fewer than 40 input bits
117    are available in an input group, zero bits are added (on the right)
118    to form an integral number of 5-bit groups.  Padding at the end of
119    the data is performed using the "=" character.  Since all base 32
120    input is an integral number of octets, only the following cases can
121    arise:
122
123    (1) the final quantum of encoding input is an integral multiple of 40
124    bits; here, the final unit of encoded output will be an integral
125    multiple of 8 characters with no "=" padding,
126
127    (2) the final quantum of encoding input is exactly 8 bits; here, the
128    final unit of encoded output will be two characters followed by six
129    "=" padding characters,
130
131    (3) the final quantum of encoding input is exactly 16 bits; here, the
132    final unit of encoded output will be four characters followed by four
133    "=" padding characters,
134
135    (4) the final quantum of encoding input is exactly 24 bits; here, the
136    final unit of encoded output will be five characters followed by
137    three "=" padding characters, or
138
139    (5) the final quantum of encoding input is exactly 32 bits; here, the
140    final unit of encoded output will be seven characters followed by one
141    "=" padding character.
142
143
144 6.  Base 32 Encoding with Extended Hex Alphabet
145
146    The following description of base 32 is due to [7].  This encoding
147    should not be regarded as the same as the "base32" encoding, and
148    should not be referred to as only "base32".
149
150    One property with this alphabet, that the base32 and base32 alphabet
151    lack, is that encoded data maintain its sort order when the encoded
152    data is compared bit-wise.
153
154    This encoding is identical to the previous one, except for the
155    alphabet.  The new alphabet is found in table 4.
156
157                      Table 4: The "Extended Hex" Base 32 Alphabet
158
159          Value Encoding  Value Encoding  Value Encoding  Value Encoding
160              0 0             9 9            18 I            27 R
161              1 1            10 A            19 J            28 S
162              2 2            11 B            20 K            29 T
163              3 3            12 C            21 L            30 U
164              4 4            13 D            22 M            31 V
165              5 5            14 E            23 N
166              6 6            15 F            24 O         (pad) =
167              7 7            16 G            25 P
168              8 8            17 H            26 Q
169
170
171
172
173 */
174 /* skips all whitespace anywhere.
175    converts characters, four at a time, starting at (or after)
176    src from base - 32 numbers into three 8 bit bytes in the target area.
177    it returns the number of data bytes stored at the target, or -1 on error.
178  */
179
180 static int
181 ldns_b32_pton_ar(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize, const char B32_ar[])
182 {
183         int tarindex, state, ch;
184         char *pos;
185         int i = 0;
186
187         state = 0;
188         tarindex = 0;
189         
190         while ((ch = *src++) != '\0' && (i == 0 || i < (int) hashed_owner_str_len)) {
191                 i++;
192                 ch = tolower(ch);
193                 if (isspace((unsigned char)ch))        /* Skip whitespace anywhere. */
194                         continue;
195
196                 if (ch == Pad32)
197                         break;
198
199                 pos = strchr(B32_ar, ch);
200                 if (pos == 0) {
201                         /* A non-base32 character. */
202                         return (-ch);
203                 }
204
205                 switch (state) {
206                 case 0:
207                         if (target) {
208                                 if ((size_t)tarindex >= targsize) {
209                                         return (-2);
210                                 }
211                                 target[tarindex] = (pos - B32_ar) << 3;
212                         }
213                         state = 1;
214                         break;
215                 case 1:
216                         if (target) {
217                                 if ((size_t)tarindex + 1 >= targsize) {
218                                         return (-3);
219                                 }
220                                 target[tarindex]   |=  (pos - B32_ar) >> 2;
221                                 target[tarindex+1]  = ((pos - B32_ar) & 0x03)
222                                                         << 6 ;
223                         }
224                         tarindex++;
225                         state = 2;
226                         break;
227                 case 2:
228                         if (target) {
229                                 if ((size_t)tarindex + 1 >= targsize) {
230                                         return (-4);
231                                 }
232                                 target[tarindex]   |=  (pos - B32_ar) << 1;
233                         }
234                         /*tarindex++;*/
235                         state = 3;
236                         break;
237                 case 3:
238                         if (target) {
239                                 if ((size_t)tarindex + 1 >= targsize) {
240                                         return (-5);
241                                 }
242                                 target[tarindex]   |=  (pos - B32_ar) >> 4;
243                                 target[tarindex+1]  = ((pos - B32_ar) & 0x0f) << 4 ;
244                         }
245                         tarindex++;
246                         state = 4;
247                         break;
248                 case 4:
249                         if (target) {
250                                 if ((size_t)tarindex + 1 >= targsize) {
251                                         return (-6);
252                                 }
253                                 target[tarindex]   |=  (pos - B32_ar) >> 1;
254                                 target[tarindex+1]  = ((pos - B32_ar) & 0x01)
255                                                         << 7 ;
256                         }
257                         tarindex++;
258                         state = 5;
259                         break;
260                 case 5:
261                         if (target) {
262                                 if ((size_t)tarindex + 1 >= targsize) {
263                                         return (-7);
264                                 }
265                                 target[tarindex]   |=  (pos - B32_ar) << 2;
266                         }
267                         state = 6;
268                         break;
269                 case 6:
270                         if (target) {
271                                 if ((size_t)tarindex + 1 >= targsize) {
272                                         return (-8);
273                                 }
274                                 target[tarindex]   |=  (pos - B32_ar) >> 3;
275                                 target[tarindex+1]  = ((pos - B32_ar) & 0x07)
276                                                         << 5 ;
277                         }
278                         tarindex++;
279                         state = 7;
280                         break;
281                 case 7:
282                         if (target) {
283                                 if ((size_t)tarindex + 1 >= targsize) {
284                                         return (-9);
285                                 }
286                                 target[tarindex]   |=  (pos - B32_ar);
287                         }
288                         tarindex++;
289                         state = 0;
290                         break;
291                 default:
292                         abort();
293                 }
294         }
295
296         /*
297          * We are done decoding Base-32 chars.  Let's see if we ended
298          * on a byte boundary, and/or with erroneous trailing characters.
299          */
300
301         if (ch == Pad32) {              /* We got a pad char. */
302                 ch = *src++;            /* Skip it, get next. */
303                 switch (state) {
304                 case 0:         /* Invalid = in first position */
305                 case 1:         /* Invalid = in second position */
306                         return (-10);
307
308                 case 2:         /* Valid, means one byte of info */
309                 case 3:
310                         /* Skip any number of spaces. */
311                         for ((void)NULL; ch != '\0'; ch = *src++)
312                                 if (!isspace((unsigned char)ch))
313                                         break;
314                         /* Make sure there is another trailing = sign. */
315                         if (ch != Pad32) {
316                                 return (-11);
317                         }
318                         ch = *src++;            /* Skip the = */
319                         /* Fall through to "single trailing =" case. */
320                         /* FALLTHROUGH */
321
322                 case 4:         /* Valid, means two bytes of info */
323                 case 5:
324                 case 6:
325                         /*
326                          * We know this char is an =.  Is there anything but
327                          * whitespace after it?
328                          */
329                         for ((void)NULL; ch != '\0'; ch = *src++)
330                                 if (!(isspace((unsigned char)ch) || ch == '=')) {
331                                         return (-12);
332                                 }
333
334                 case 7:         /* Valid, means three bytes of info */
335                         /*
336                          * We know this char is an =.  Is there anything but
337                          * whitespace after it?
338                          */
339                         for ((void)NULL; ch != '\0'; ch = *src++)
340                                 if (!isspace((unsigned char)ch)) {
341                                         return (-13);
342                                 }
343
344                         /*
345                          * Now make sure for cases 2 and 3 that the "extra"
346                          * bits that slopped past the last full byte were
347                          * zeros.  If we don't check them, they become a
348                          * subliminal channel.
349                          */
350                         if (target && target[tarindex] != 0) {
351                                 return (-14);
352                         }
353                 }
354         } else {
355                 /*
356                  * We ended by seeing the end of the string.  Make sure we
357                  * have no partial bytes lying around.
358                  */
359                 if (state != 0)
360                         return (-15);
361         }
362
363         return (tarindex);
364 }
365
366 int
367 ldns_b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize)
368 {
369         return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32);
370 }
371
372 /* deprecated, here for backwards compatibility */
373 int
374 b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize)
375 {
376         return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32);
377 }
378
379 int
380 ldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize)
381 {
382         return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32_extended_hex);
383 }
384
385 /* deprecated, here for backwards compatibility */
386 int
387 b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize)
388 {
389         return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32_extended_hex);
390 }
391
392 #endif /* !HAVE_B32_PTON */