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_NTOP
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 <assert.h>
64
65#include <ldns/util.h>
66
67static const char Base32[] =
68	"abcdefghijklmnopqrstuvwxyz234567";
69/*	"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";*/
70/*       00000000001111111111222222222233
71         01234567890123456789012345678901*/
72static const char Base32_extended_hex[] =
73/*	"0123456789ABCDEFGHIJKLMNOPQRSTUV";*/
74	"0123456789abcdefghijklmnopqrstuv";
75static const char Pad32 = '=';
76
77/* (From RFC3548 and draft-josefsson-rfc3548bis-00.txt)
785.  Base 32 Encoding
79
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
82   humanly readable.
83
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.)
87
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.
98
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.
103
104                      Table 3: The Base 32 Alphabet
105
106         Value Encoding  Value Encoding  Value Encoding  Value Encoding
107             0 A             9 J            18 S            27 3
108             1 B            10 K            19 T            28 4
109             2 C            11 L            20 U            29 5
110             3 D            12 M            21 V            30 6
111             4 E            13 N            22 W            31 7
112             5 F            14 O            23 X
113             6 G            15 P            24 Y         (pad) =
114             7 H            16 Q            25 Z
115             8 I            17 R            26 2
116
117
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
125   arise:
126
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,
130
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,
134
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,
138
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
142
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.
146
147
1486.  Base 32 Encoding with Extended Hex Alphabet
149
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".
153
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.
157
158   This encoding is identical to the previous one, except for the
159   alphabet.  The new alphabet is found in table 4.
160
161                     Table 4: The "Extended Hex" Base 32 Alphabet
162
163         Value Encoding  Value Encoding  Value Encoding  Value Encoding
164             0 0             9 9            18 I            27 R
165             1 1            10 A            19 J            28 S
166             2 2            11 B            20 K            29 T
167             3 3            12 C            21 L            30 U
168             4 4            13 D            22 M            31 V
169             5 5            14 E            23 N
170             6 6            15 F            24 O         (pad) =
171             7 7            16 G            25 P
172             8 8            17 H            26 Q
173
174*/
175
176
177static int
178ldns_b32_ntop_ar(uint8_t const *src, size_t srclength, char *target, size_t targsize, const char B32_ar[]) {
179	size_t datalength = 0;
180	uint8_t input[5];
181	uint8_t output[8];
182	size_t i;
183        memset(output, 0, 8);
184
185	while (4 < srclength) {
186		input[0] = *src++;
187		input[1] = *src++;
188		input[2] = *src++;
189		input[3] = *src++;
190		input[4] = *src++;
191		srclength -= 5;
192
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);
201
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);
210
211		if (datalength + 8 > targsize) {
212			return (-1);
213		}
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]];
222	}
223
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++)
229			input[i] = *src++;
230
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);
238		}
239		if (srclength >= 2) {
240			output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4);
241			assert(output[3] < 32);
242		}
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);
248		}
249		if (srclength >= 4) {
250			output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5);
251			assert(output[6] < 32);
252		}
253
254
255		if (datalength + 1 > targsize) {
256			return (-2);
257		}
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;
265			} else {
266				if (datalength + 1 > targsize) { return (-2); }
267				target[datalength++] = B32_ar[output[2]];
268			}
269		} else {
270			if (datalength + 1 > targsize) { return (-2); }
271			target[datalength++] = Pad32;
272			if (datalength + 1 > targsize) { return (-2); }
273			target[datalength++] = Pad32;
274		}
275		if (srclength >= 2) {
276			if (datalength + 1 > targsize) { return (-2); }
277			target[datalength++] = B32_ar[output[3]];
278		} else {
279			if (datalength + 1 > targsize) { return (-2); }
280			target[datalength++] = Pad32;
281		}
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;
288			} else {
289				if (datalength + 1 > targsize) { return (-2); }
290				target[datalength++] = B32_ar[output[5]];
291			}
292		} else {
293			if (datalength + 1 > targsize) { return (-2); }
294			target[datalength++] = Pad32;
295			if (datalength + 1 > targsize) { return (-2); }
296			target[datalength++] = Pad32;
297		}
298		if (srclength >= 4) {
299			if (datalength + 1 > targsize) { return (-2); }
300			target[datalength++] = B32_ar[output[6]];
301		} else {
302			if (datalength + 1 > targsize) { return (-2); }
303			target[datalength++] = Pad32;
304		}
305		if (datalength + 1 > targsize) { return (-2); }
306		target[datalength++] = Pad32;
307	}
308	if (datalength+1 > targsize) {
309		return (int) (datalength);
310	}
311	target[datalength] = '\0';	/* Returned value doesn't count \0. */
312	return (int) (datalength);
313}
314
315int
316ldns_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);
318}
319
320/* deprecated, here for backwards compatibility */
321int
322b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
323	return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32);
324}
325
326int
327ldns_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);
329}
330
331/* deprecated, here for backwards compatibility */
332int
333b32_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);
335}
336
337#endif /* !HAVE_B32_NTOP */
338