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>
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #ifdef HAVE_SYS_SOCKET_H
48 #include <sys/socket.h>
51 #ifdef HAVE_NETINET_IN_H
52 #include <netinet/in.h>
54 #ifdef HAVE_ARPA_INET_H
55 #include <arpa/inet.h>
65 #include <ldns/util.h>
67 static const char Base32[] =
68 "abcdefghijklmnopqrstuvwxyz234567";
69 /* "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";*/
70 /* 00000000001111111111222222222233
71 01234567890123456789012345678901*/
72 static const char Base32_extended_hex[] =
73 /* "0123456789ABCDEFGHIJKLMNOPQRSTUV";*/
74 "0123456789abcdefghijklmnopqrstuv";
75 static const char Pad32 = '=';
77 /* (From RFC3548 and draft-josefsson-rfc3548bis-00.txt)
80 The Base 32 encoding is designed to represent arbitrary sequences of
81 octets in a form that needs to be case insensitive but need not be
84 A 33-character subset of US-ASCII is used, enabling 5 bits to be
85 represented per printable character. (The extra 33rd character, "=",
86 is used to signify a special processing function.)
88 The encoding process represents 40-bit groups of input bits as output
89 strings of 8 encoded characters. Proceeding from left to right, a
90 40-bit input group is formed by concatenating 5 8bit input groups.
91 These 40 bits are then treated as 8 concatenated 5-bit groups, each
92 of which is translated into a single digit in the base 32 alphabet.
93 When encoding a bit stream via the base 32 encoding, the bit stream
94 must be presumed to be ordered with the most-significant-bit first.
95 That is, the first bit in the stream will be the high-order bit in
96 the first 8bit byte, and the eighth bit will be the low-order bit in
97 the first 8bit byte, and so on.
99 Each 5-bit group is used as an index into an array of 32 printable
100 characters. The character referenced by the index is placed in the
101 output string. These characters, identified in Table 3, below, are
102 selected from US-ASCII digits and uppercase letters.
104 Table 3: The Base 32 Alphabet
106 Value Encoding Value Encoding Value Encoding Value Encoding
113 6 G 15 P 24 Y (pad) =
118 Special processing is performed if fewer than 40 bits are available
119 at the end of the data being encoded. A full encoding quantum is
120 always completed at the end of a body. When fewer than 40 input bits
121 are available in an input group, zero bits are added (on the right)
122 to form an integral number of 5-bit groups. Padding at the end of
123 the data is performed using the "=" character. Since all base 32
124 input is an integral number of octets, only the following cases can
127 (1) the final quantum of encoding input is an integral multiple of 40
128 bits; here, the final unit of encoded output will be an integral
129 multiple of 8 characters with no "=" padding,
131 (2) the final quantum of encoding input is exactly 8 bits; here, the
132 final unit of encoded output will be two characters followed by six
133 "=" padding characters,
135 (3) the final quantum of encoding input is exactly 16 bits; here, the
136 final unit of encoded output will be four characters followed by four
137 "=" padding characters,
139 (4) the final quantum of encoding input is exactly 24 bits; here, the
140 final unit of encoded output will be five characters followed by
141 three "=" padding characters, or
143 (5) the final quantum of encoding input is exactly 32 bits; here, the
144 final unit of encoded output will be seven characters followed by one
145 "=" padding character.
148 6. Base 32 Encoding with Extended Hex Alphabet
150 The following description of base 32 is due to [7]. This encoding
151 should not be regarded as the same as the "base32" encoding, and
152 should not be referred to as only "base32".
154 One property with this alphabet, that the base64 and base32 alphabet
155 lack, is that encoded data maintain its sort order when the encoded
156 data is compared bit-wise.
158 This encoding is identical to the previous one, except for the
159 alphabet. The new alphabet is found in table 4.
161 Table 4: The "Extended Hex" Base 32 Alphabet
163 Value Encoding Value Encoding Value Encoding Value Encoding
170 6 6 15 F 24 O (pad) =
178 ldns_b32_ntop_ar(uint8_t const *src, size_t srclength, char *target, size_t targsize, const char B32_ar[]) {
179 size_t datalength = 0;
183 memset(output, 0, 8);
185 while (4 < srclength) {
193 output[0] = (input[0] & 0xf8) >> 3;
194 output[1] = ((input[0] & 0x07) << 2) + ((input[1] & 0xc0) >> 6);
195 output[2] = (input[1] & 0x3e) >> 1;
196 output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4);
197 output[4] = ((input[2] & 0x0f) << 1) + ((input[3] & 0x80) >> 7);
198 output[5] = (input[3] & 0x7c) >> 2;
199 output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5);
200 output[7] = (input[4] & 0x1f);
202 assert(output[0] < 32);
203 assert(output[1] < 32);
204 assert(output[2] < 32);
205 assert(output[3] < 32);
206 assert(output[4] < 32);
207 assert(output[5] < 32);
208 assert(output[6] < 32);
209 assert(output[7] < 32);
211 if (datalength + 8 > targsize) {
214 target[datalength++] = B32_ar[output[0]];
215 target[datalength++] = B32_ar[output[1]];
216 target[datalength++] = B32_ar[output[2]];
217 target[datalength++] = B32_ar[output[3]];
218 target[datalength++] = B32_ar[output[4]];
219 target[datalength++] = B32_ar[output[5]];
220 target[datalength++] = B32_ar[output[6]];
221 target[datalength++] = B32_ar[output[7]];
224 /* Now we worry about padding. */
225 if (0 != srclength) {
226 /* Get what's left. */
227 input[0] = input[1] = input[2] = input[3] = input[4] = (uint8_t) '\0';
228 for (i = 0; i < srclength; i++)
231 output[0] = (input[0] & 0xf8) >> 3;
232 assert(output[0] < 32);
233 if (srclength >= 1) {
234 output[1] = ((input[0] & 0x07) << 2) + ((input[1] & 0xc0) >> 6);
235 assert(output[1] < 32);
236 output[2] = (input[1] & 0x3e) >> 1;
237 assert(output[2] < 32);
239 if (srclength >= 2) {
240 output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4);
241 assert(output[3] < 32);
243 if (srclength >= 3) {
244 output[4] = ((input[2] & 0x0f) << 1) + ((input[3] & 0x80) >> 7);
245 assert(output[4] < 32);
246 output[5] = (input[3] & 0x7c) >> 2;
247 assert(output[5] < 32);
249 if (srclength >= 4) {
250 output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5);
251 assert(output[6] < 32);
255 if (datalength + 1 > targsize) {
258 target[datalength++] = B32_ar[output[0]];
259 if (srclength >= 1) {
260 if (datalength + 1 > targsize) { return (-2); }
261 target[datalength++] = B32_ar[output[1]];
262 if (srclength == 1 && output[2] == 0) {
263 if (datalength + 1 > targsize) { return (-2); }
264 target[datalength++] = Pad32;
266 if (datalength + 1 > targsize) { return (-2); }
267 target[datalength++] = B32_ar[output[2]];
270 if (datalength + 1 > targsize) { return (-2); }
271 target[datalength++] = Pad32;
272 if (datalength + 1 > targsize) { return (-2); }
273 target[datalength++] = Pad32;
275 if (srclength >= 2) {
276 if (datalength + 1 > targsize) { return (-2); }
277 target[datalength++] = B32_ar[output[3]];
279 if (datalength + 1 > targsize) { return (-2); }
280 target[datalength++] = Pad32;
282 if (srclength >= 3) {
283 if (datalength + 1 > targsize) { return (-2); }
284 target[datalength++] = B32_ar[output[4]];
285 if (srclength == 3 && output[5] == 0) {
286 if (datalength + 1 > targsize) { return (-2); }
287 target[datalength++] = Pad32;
289 if (datalength + 1 > targsize) { return (-2); }
290 target[datalength++] = B32_ar[output[5]];
293 if (datalength + 1 > targsize) { return (-2); }
294 target[datalength++] = Pad32;
295 if (datalength + 1 > targsize) { return (-2); }
296 target[datalength++] = Pad32;
298 if (srclength >= 4) {
299 if (datalength + 1 > targsize) { return (-2); }
300 target[datalength++] = B32_ar[output[6]];
302 if (datalength + 1 > targsize) { return (-2); }
303 target[datalength++] = Pad32;
305 if (datalength + 1 > targsize) { return (-2); }
306 target[datalength++] = Pad32;
308 if (datalength+1 > targsize) {
309 return (int) (datalength);
311 target[datalength] = '\0'; /* Returned value doesn't count \0. */
312 return (int) (datalength);
316 ldns_b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
317 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32);
320 /* deprecated, here for backwards compatibility */
322 b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
323 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32);
327 ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
328 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32_extended_hex);
331 /* deprecated, here for backwards compatibility */
333 b32_ntop_extended_hex(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
334 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32_extended_hex);
337 #endif /* !HAVE_B32_NTOP */