2 * Copyright (c) 1996, 1998 by Internet Software Consortium.
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.
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
19 * Portions Copyright (c) 1995 by International Business Machines, Inc.
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
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.
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.
42 #include <ldns/config.h>
44 #include <ldns/ldns.h>
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
52 #ifdef HAVE_NETINET_IN_H
53 #include <netinet/in.h>
55 #ifdef HAVE_ARPA_INET_H
56 #include <arpa/inet.h>
64 /* "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";*/
65 static const char Base32[] =
66 "abcdefghijklmnopqrstuvwxyz234567";
67 /* "0123456789ABCDEFGHIJKLMNOPQRSTUV";*/
68 static const char Base32_extended_hex[] =
69 "0123456789abcdefghijklmnopqrstuv";
70 static const char Pad32 = '=';
72 /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
75 The Base 32 encoding is designed to represent arbitrary sequences of
76 octets in a form that needs to be case insensitive but need not be
79 A 33-character subset of US-ASCII is used, enabling 5 bits to be
80 represented per printable character. (The extra 33rd character, "=",
81 is used to signify a special processing function.)
83 The encoding process represents 40-bit groups of input bits as output
84 strings of 8 encoded characters. Proceeding from left to right, a
85 40-bit input group is formed by concatenating 5 8bit input groups.
86 These 40 bits are then treated as 8 concatenated 5-bit groups, each
87 of which is translated into a single digit in the base 32 alphabet.
88 When encoding a bit stream via the base 32 encoding, the bit stream
89 must be presumed to be ordered with the most-significant-bit first.
90 That is, the first bit in the stream will be the high-order bit in
91 the first 8bit byte, and the eighth bit will be the low-order bit in
92 the first 8bit byte, and so on.
94 Each 5-bit group is used as an index into an array of 32 printable
95 characters. The character referenced by the index is placed in the
96 output string. These characters, identified in Table 3, below, are
97 selected from US-ASCII digits and uppercase letters.
99 Table 3: The Base 32 Alphabet
101 Value Encoding Value Encoding Value Encoding Value Encoding
108 6 G 15 P 24 Y (pad) =
113 Special processing is performed if fewer than 40 bits are available
114 at the end of the data being encoded. A full encoding quantum is
115 always completed at the end of a body. When fewer than 40 input bits
116 are available in an input group, zero bits are added (on the right)
117 to form an integral number of 5-bit groups. Padding at the end of
118 the data is performed using the "=" character. Since all base 32
119 input is an integral number of octets, only the following cases can
122 (1) the final quantum of encoding input is an integral multiple of 40
123 bits; here, the final unit of encoded output will be an integral
124 multiple of 8 characters with no "=" padding,
126 (2) the final quantum of encoding input is exactly 8 bits; here, the
127 final unit of encoded output will be two characters followed by six
128 "=" padding characters,
130 (3) the final quantum of encoding input is exactly 16 bits; here, the
131 final unit of encoded output will be four characters followed by four
132 "=" padding characters,
134 (4) the final quantum of encoding input is exactly 24 bits; here, the
135 final unit of encoded output will be five characters followed by
136 three "=" padding characters, or
138 (5) the final quantum of encoding input is exactly 32 bits; here, the
139 final unit of encoded output will be seven characters followed by one
140 "=" padding character.
143 6. Base 32 Encoding with Extended Hex Alphabet
145 The following description of base 32 is due to [7]. This encoding
146 should not be regarded as the same as the "base32" encoding, and
147 should not be referred to as only "base32".
149 One property with this alphabet, that the base32 and base32 alphabet
150 lack, is that encoded data maintain its sort order when the encoded
151 data is compared bit-wise.
153 This encoding is identical to the previous one, except for the
154 alphabet. The new alphabet is found in table 4.
156 Table 4: The "Extended Hex" Base 32 Alphabet
158 Value Encoding Value Encoding Value Encoding Value Encoding
165 6 6 15 F 24 O (pad) =
173 /* skips all whitespace anywhere.
174 converts characters, four at a time, starting at (or after)
175 src from base - 32 numbers into three 8 bit bytes in the target area.
176 it returns the number of data bytes stored at the target, or -1 on error.
180 ldns_b32_pton_ar(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize, const char B32_ar[])
182 int tarindex, state, ch;
189 while ((ch = *src++) != '\0' && (i == 0 || i < (int) hashed_owner_str_len)) {
192 if (isspace((unsigned char)ch)) /* Skip whitespace anywhere. */
198 pos = strchr(B32_ar, ch);
200 /* A non-base32 character. */
207 if ((size_t)tarindex >= targsize) {
210 target[tarindex] = (pos - B32_ar) << 3;
216 if ((size_t)tarindex + 1 >= targsize) {
219 target[tarindex] |= (pos - B32_ar) >> 2;
220 target[tarindex+1] = ((pos - B32_ar) & 0x03)
228 if ((size_t)tarindex + 1 >= targsize) {
231 target[tarindex] |= (pos - B32_ar) << 1;
238 if ((size_t)tarindex + 1 >= targsize) {
241 target[tarindex] |= (pos - B32_ar) >> 4;
242 target[tarindex+1] = ((pos - B32_ar) & 0x0f) << 4 ;
249 if ((size_t)tarindex + 1 >= targsize) {
252 target[tarindex] |= (pos - B32_ar) >> 1;
253 target[tarindex+1] = ((pos - B32_ar) & 0x01)
261 if ((size_t)tarindex + 1 >= targsize) {
264 target[tarindex] |= (pos - B32_ar) << 2;
270 if ((size_t)tarindex + 1 >= targsize) {
273 target[tarindex] |= (pos - B32_ar) >> 3;
274 target[tarindex+1] = ((pos - B32_ar) & 0x07)
282 if ((size_t)tarindex + 1 >= targsize) {
285 target[tarindex] |= (pos - B32_ar);
296 * We are done decoding Base-32 chars. Let's see if we ended
297 * on a byte boundary, and/or with erroneous trailing characters.
300 if (ch == Pad32) { /* We got a pad char. */
301 ch = *src++; /* Skip it, get next. */
303 case 0: /* Invalid = in first position */
304 case 1: /* Invalid = in second position */
307 case 2: /* Valid, means one byte of info */
309 /* Skip any number of spaces. */
310 for ((void)NULL; ch != '\0'; ch = *src++)
311 if (!isspace((unsigned char)ch))
313 /* Make sure there is another trailing = sign. */
317 ch = *src++; /* Skip the = */
318 /* Fall through to "single trailing =" case. */
321 case 4: /* Valid, means two bytes of info */
325 * We know this char is an =. Is there anything but
326 * whitespace after it?
328 for ((void)NULL; ch != '\0'; ch = *src++)
329 if (!(isspace((unsigned char)ch) || ch == '=')) {
333 case 7: /* Valid, means three bytes of info */
335 * We know this char is an =. Is there anything but
336 * whitespace after it?
338 for ((void)NULL; ch != '\0'; ch = *src++)
339 if (!isspace((unsigned char)ch)) {
344 * Now make sure for cases 2 and 3 that the "extra"
345 * bits that slopped past the last full byte were
346 * zeros. If we don't check them, they become a
347 * subliminal channel.
349 if (target && target[tarindex] != 0) {
355 * We ended by seeing the end of the string. Make sure we
356 * have no partial bytes lying around.
366 ldns_b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize)
368 return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32);
371 /* deprecated, here for backwards compatibility */
373 b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize)
375 return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32);
379 ldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize)
381 return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32_extended_hex);
384 /* deprecated, here for backwards compatibility */
386 b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize)
388 return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32_extended_hex);