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>
66 static const char Base32[] =
67 "abcdefghijklmnopqrstuvwxyz234567";
68 /* "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";*/
69 /* 00000000001111111111222222222233
70 01234567890123456789012345678901*/
71 static const char Base32_extended_hex[] =
72 /* "0123456789ABCDEFGHIJKLMNOPQRSTUV";*/
73 "0123456789abcdefghijklmnopqrstuv";
74 static const char Pad32 = '=';
76 /* (From RFC3548 and draft-josefsson-rfc3548bis-00.txt)
79 The Base 32 encoding is designed to represent arbitrary sequences of
80 octets in a form that needs to be case insensitive but need not be
83 A 33-character subset of US-ASCII is used, enabling 5 bits to be
84 represented per printable character. (The extra 33rd character, "=",
85 is used to signify a special processing function.)
87 The encoding process represents 40-bit groups of input bits as output
88 strings of 8 encoded characters. Proceeding from left to right, a
89 40-bit input group is formed by concatenating 5 8bit input groups.
90 These 40 bits are then treated as 8 concatenated 5-bit groups, each
91 of which is translated into a single digit in the base 32 alphabet.
92 When encoding a bit stream via the base 32 encoding, the bit stream
93 must be presumed to be ordered with the most-significant-bit first.
94 That is, the first bit in the stream will be the high-order bit in
95 the first 8bit byte, and the eighth bit will be the low-order bit in
96 the first 8bit byte, and so on.
98 Each 5-bit group is used as an index into an array of 32 printable
99 characters. The character referenced by the index is placed in the
100 output string. These characters, identified in Table 3, below, are
101 selected from US-ASCII digits and uppercase letters.
103 Table 3: The Base 32 Alphabet
105 Value Encoding Value Encoding Value Encoding Value Encoding
112 6 G 15 P 24 Y (pad) =
117 Special processing is performed if fewer than 40 bits are available
118 at the end of the data being encoded. A full encoding quantum is
119 always completed at the end of a body. When fewer than 40 input bits
120 are available in an input group, zero bits are added (on the right)
121 to form an integral number of 5-bit groups. Padding at the end of
122 the data is performed using the "=" character. Since all base 32
123 input is an integral number of octets, only the following cases can
126 (1) the final quantum of encoding input is an integral multiple of 40
127 bits; here, the final unit of encoded output will be an integral
128 multiple of 8 characters with no "=" padding,
130 (2) the final quantum of encoding input is exactly 8 bits; here, the
131 final unit of encoded output will be two characters followed by six
132 "=" padding characters,
134 (3) the final quantum of encoding input is exactly 16 bits; here, the
135 final unit of encoded output will be four characters followed by four
136 "=" padding characters,
138 (4) the final quantum of encoding input is exactly 24 bits; here, the
139 final unit of encoded output will be five characters followed by
140 three "=" padding characters, or
142 (5) the final quantum of encoding input is exactly 32 bits; here, the
143 final unit of encoded output will be seven characters followed by one
144 "=" padding character.
147 6. Base 32 Encoding with Extended Hex Alphabet
149 The following description of base 32 is due to [7]. This encoding
150 should not be regarded as the same as the "base32" encoding, and
151 should not be referred to as only "base32".
153 One property with this alphabet, that the base64 and base32 alphabet
154 lack, is that encoded data maintain its sort order when the encoded
155 data is compared bit-wise.
157 This encoding is identical to the previous one, except for the
158 alphabet. The new alphabet is found in table 4.
160 Table 4: The "Extended Hex" Base 32 Alphabet
162 Value Encoding Value Encoding Value Encoding Value Encoding
169 6 6 15 F 24 O (pad) =
177 ldns_b32_ntop_ar(uint8_t const *src, size_t srclength, char *target, size_t targsize, const char B32_ar[]) {
178 size_t datalength = 0;
182 memset(output, 0, 8);
184 while (4 < srclength) {
192 output[0] = (input[0] & 0xf8) >> 3;
193 output[1] = ((input[0] & 0x07) << 2) + ((input[1] & 0xc0) >> 6);
194 output[2] = (input[1] & 0x3e) >> 1;
195 output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4);
196 output[4] = ((input[2] & 0x0f) << 1) + ((input[3] & 0x80) >> 7);
197 output[5] = (input[3] & 0x7c) >> 2;
198 output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5);
199 output[7] = (input[4] & 0x1f);
201 assert(output[0] < 32);
202 assert(output[1] < 32);
203 assert(output[2] < 32);
204 assert(output[3] < 32);
205 assert(output[4] < 32);
206 assert(output[5] < 32);
207 assert(output[6] < 32);
208 assert(output[7] < 32);
210 if (datalength + 8 > targsize) {
213 target[datalength++] = B32_ar[output[0]];
214 target[datalength++] = B32_ar[output[1]];
215 target[datalength++] = B32_ar[output[2]];
216 target[datalength++] = B32_ar[output[3]];
217 target[datalength++] = B32_ar[output[4]];
218 target[datalength++] = B32_ar[output[5]];
219 target[datalength++] = B32_ar[output[6]];
220 target[datalength++] = B32_ar[output[7]];
223 /* Now we worry about padding. */
224 if (0 != srclength) {
225 /* Get what's left. */
226 input[0] = input[1] = input[2] = input[3] = input[4] = (uint8_t) '\0';
227 for (i = 0; i < srclength; i++)
230 output[0] = (input[0] & 0xf8) >> 3;
231 assert(output[0] < 32);
232 if (srclength >= 1) {
233 output[1] = ((input[0] & 0x07) << 2) + ((input[1] & 0xc0) >> 6);
234 assert(output[1] < 32);
235 output[2] = (input[1] & 0x3e) >> 1;
236 assert(output[2] < 32);
238 if (srclength >= 2) {
239 output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4);
240 assert(output[3] < 32);
242 if (srclength >= 3) {
243 output[4] = ((input[2] & 0x0f) << 1) + ((input[3] & 0x80) >> 7);
244 assert(output[4] < 32);
245 output[5] = (input[3] & 0x7c) >> 2;
246 assert(output[5] < 32);
248 if (srclength >= 4) {
249 output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5);
250 assert(output[6] < 32);
254 if (datalength + 1 > targsize) {
257 target[datalength++] = B32_ar[output[0]];
258 if (srclength >= 1) {
259 target[datalength++] = B32_ar[output[1]];
260 if (srclength == 1 && output[2] == 0) {
261 target[datalength++] = Pad32;
263 target[datalength++] = B32_ar[output[2]];
266 target[datalength++] = Pad32;
267 target[datalength++] = Pad32;
269 if (srclength >= 2) {
270 target[datalength++] = B32_ar[output[3]];
272 target[datalength++] = Pad32;
274 if (srclength >= 3) {
275 target[datalength++] = B32_ar[output[4]];
276 if (srclength == 3 && output[5] == 0) {
277 target[datalength++] = Pad32;
279 target[datalength++] = B32_ar[output[5]];
282 target[datalength++] = Pad32;
283 target[datalength++] = Pad32;
285 if (srclength >= 4) {
286 target[datalength++] = B32_ar[output[6]];
288 target[datalength++] = Pad32;
290 target[datalength++] = Pad32;
292 if (datalength > targsize) {
295 target[datalength] = '\0'; /* Returned value doesn't count \0. */
296 return (int) (datalength);
300 ldns_b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
301 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32);
304 /* deprecated, here for backwards compatibility */
306 b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
307 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32);
311 ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
312 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32_extended_hex);
315 /* deprecated, here for backwards compatibility */
317 b32_ntop_extended_hex(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
318 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32_extended_hex);