if_iwm - Factor out firmware station handling into if_iwm_sta.c.
[dragonfly.git] / contrib / ldns / compat / b32_ntop.c
blob038ebdc95806e6521f71a4146b42642e790dd7e1
1 /*
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
15 * SOFTWARE.
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
27 * permission.
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 <sys/types.h>
45 #include <sys/param.h>
46 #ifdef HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
48 #endif
50 #ifdef HAVE_NETINET_IN_H
51 #include <netinet/in.h>
52 #endif
53 #ifdef HAVE_ARPA_INET_H
54 #include <arpa/inet.h>
55 #endif
57 #include <ctype.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
62 #include <assert.h>
64 static const char Base32[] =
65 "abcdefghijklmnopqrstuvwxyz234567";
66 /* "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";*/
67 /* 00000000001111111111222222222233
68 01234567890123456789012345678901*/
69 static const char Base32_extended_hex[] =
70 /* "0123456789ABCDEFGHIJKLMNOPQRSTUV";*/
71 "0123456789abcdefghijklmnopqrstuv";
72 static const char Pad32 = '=';
74 /* (From RFC3548 and draft-josefsson-rfc3548bis-00.txt)
75 5. Base 32 Encoding
77 The Base 32 encoding is designed to represent arbitrary sequences of
78 octets in a form that needs to be case insensitive but need not be
79 humanly readable.
81 A 33-character subset of US-ASCII is used, enabling 5 bits to be
82 represented per printable character. (The extra 33rd character, "=",
83 is used to signify a special processing function.)
85 The encoding process represents 40-bit groups of input bits as output
86 strings of 8 encoded characters. Proceeding from left to right, a
87 40-bit input group is formed by concatenating 5 8bit input groups.
88 These 40 bits are then treated as 8 concatenated 5-bit groups, each
89 of which is translated into a single digit in the base 32 alphabet.
90 When encoding a bit stream via the base 32 encoding, the bit stream
91 must be presumed to be ordered with the most-significant-bit first.
92 That is, the first bit in the stream will be the high-order bit in
93 the first 8bit byte, and the eighth bit will be the low-order bit in
94 the first 8bit byte, and so on.
96 Each 5-bit group is used as an index into an array of 32 printable
97 characters. The character referenced by the index is placed in the
98 output string. These characters, identified in Table 3, below, are
99 selected from US-ASCII digits and uppercase letters.
101 Table 3: The Base 32 Alphabet
103 Value Encoding Value Encoding Value Encoding Value Encoding
104 0 A 9 J 18 S 27 3
105 1 B 10 K 19 T 28 4
106 2 C 11 L 20 U 29 5
107 3 D 12 M 21 V 30 6
108 4 E 13 N 22 W 31 7
109 5 F 14 O 23 X
110 6 G 15 P 24 Y (pad) =
111 7 H 16 Q 25 Z
112 8 I 17 R 26 2
115 Special processing is performed if fewer than 40 bits are available
116 at the end of the data being encoded. A full encoding quantum is
117 always completed at the end of a body. When fewer than 40 input bits
118 are available in an input group, zero bits are added (on the right)
119 to form an integral number of 5-bit groups. Padding at the end of
120 the data is performed using the "=" character. Since all base 32
121 input is an integral number of octets, only the following cases can
122 arise:
124 (1) the final quantum of encoding input is an integral multiple of 40
125 bits; here, the final unit of encoded output will be an integral
126 multiple of 8 characters with no "=" padding,
128 (2) the final quantum of encoding input is exactly 8 bits; here, the
129 final unit of encoded output will be two characters followed by six
130 "=" padding characters,
132 (3) the final quantum of encoding input is exactly 16 bits; here, the
133 final unit of encoded output will be four characters followed by four
134 "=" padding characters,
136 (4) the final quantum of encoding input is exactly 24 bits; here, the
137 final unit of encoded output will be five characters followed by
138 three "=" padding characters, or
140 (5) the final quantum of encoding input is exactly 32 bits; here, the
141 final unit of encoded output will be seven characters followed by one
142 "=" padding character.
145 6. Base 32 Encoding with Extended Hex Alphabet
147 The following description of base 32 is due to [7]. This encoding
148 should not be regarded as the same as the "base32" encoding, and
149 should not be referred to as only "base32".
151 One property with this alphabet, that the base64 and base32 alphabet
152 lack, is that encoded data maintain its sort order when the encoded
153 data is compared bit-wise.
155 This encoding is identical to the previous one, except for the
156 alphabet. The new alphabet is found in table 4.
158 Table 4: The "Extended Hex" Base 32 Alphabet
160 Value Encoding Value Encoding Value Encoding Value Encoding
161 0 0 9 9 18 I 27 R
162 1 1 10 A 19 J 28 S
163 2 2 11 B 20 K 29 T
164 3 3 12 C 21 L 30 U
165 4 4 13 D 22 M 31 V
166 5 5 14 E 23 N
167 6 6 15 F 24 O (pad) =
168 7 7 16 G 25 P
169 8 8 17 H 26 Q
175 ldns_b32_ntop_ar(uint8_t const *src, size_t srclength, char *target, size_t targsize, const char B32_ar[]) {
176 size_t datalength = 0;
177 uint8_t input[5];
178 uint8_t output[8];
179 size_t i;
180 memset(output, 0, 8);
182 while (4 < srclength) {
183 input[0] = *src++;
184 input[1] = *src++;
185 input[2] = *src++;
186 input[3] = *src++;
187 input[4] = *src++;
188 srclength -= 5;
190 output[0] = (input[0] & 0xf8) >> 3;
191 output[1] = ((input[0] & 0x07) << 2) + ((input[1] & 0xc0) >> 6);
192 output[2] = (input[1] & 0x3e) >> 1;
193 output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4);
194 output[4] = ((input[2] & 0x0f) << 1) + ((input[3] & 0x80) >> 7);
195 output[5] = (input[3] & 0x7c) >> 2;
196 output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5);
197 output[7] = (input[4] & 0x1f);
199 assert(output[0] < 32);
200 assert(output[1] < 32);
201 assert(output[2] < 32);
202 assert(output[3] < 32);
203 assert(output[4] < 32);
204 assert(output[5] < 32);
205 assert(output[6] < 32);
206 assert(output[7] < 32);
208 if (datalength + 8 > targsize) {
209 return (-1);
211 target[datalength++] = B32_ar[output[0]];
212 target[datalength++] = B32_ar[output[1]];
213 target[datalength++] = B32_ar[output[2]];
214 target[datalength++] = B32_ar[output[3]];
215 target[datalength++] = B32_ar[output[4]];
216 target[datalength++] = B32_ar[output[5]];
217 target[datalength++] = B32_ar[output[6]];
218 target[datalength++] = B32_ar[output[7]];
221 /* Now we worry about padding. */
222 if (0 != srclength) {
223 /* Get what's left. */
224 input[0] = input[1] = input[2] = input[3] = input[4] = (uint8_t) '\0';
225 for (i = 0; i < srclength; i++)
226 input[i] = *src++;
228 output[0] = (input[0] & 0xf8) >> 3;
229 assert(output[0] < 32);
230 if (srclength >= 1) {
231 output[1] = ((input[0] & 0x07) << 2) + ((input[1] & 0xc0) >> 6);
232 assert(output[1] < 32);
233 output[2] = (input[1] & 0x3e) >> 1;
234 assert(output[2] < 32);
236 if (srclength >= 2) {
237 output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4);
238 assert(output[3] < 32);
240 if (srclength >= 3) {
241 output[4] = ((input[2] & 0x0f) << 1) + ((input[3] & 0x80) >> 7);
242 assert(output[4] < 32);
243 output[5] = (input[3] & 0x7c) >> 2;
244 assert(output[5] < 32);
246 if (srclength >= 4) {
247 output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5);
248 assert(output[6] < 32);
252 if (datalength + 1 > targsize) {
253 return (-2);
255 target[datalength++] = B32_ar[output[0]];
256 if (srclength >= 1) {
257 if (datalength + 1 > targsize) { return (-2); }
258 target[datalength++] = B32_ar[output[1]];
259 if (srclength == 1 && output[2] == 0) {
260 if (datalength + 1 > targsize) { return (-2); }
261 target[datalength++] = Pad32;
262 } else {
263 if (datalength + 1 > targsize) { return (-2); }
264 target[datalength++] = B32_ar[output[2]];
266 } else {
267 if (datalength + 1 > targsize) { return (-2); }
268 target[datalength++] = Pad32;
269 if (datalength + 1 > targsize) { return (-2); }
270 target[datalength++] = Pad32;
272 if (srclength >= 2) {
273 if (datalength + 1 > targsize) { return (-2); }
274 target[datalength++] = B32_ar[output[3]];
275 } else {
276 if (datalength + 1 > targsize) { return (-2); }
277 target[datalength++] = Pad32;
279 if (srclength >= 3) {
280 if (datalength + 1 > targsize) { return (-2); }
281 target[datalength++] = B32_ar[output[4]];
282 if (srclength == 3 && output[5] == 0) {
283 if (datalength + 1 > targsize) { return (-2); }
284 target[datalength++] = Pad32;
285 } else {
286 if (datalength + 1 > targsize) { return (-2); }
287 target[datalength++] = B32_ar[output[5]];
289 } else {
290 if (datalength + 1 > targsize) { return (-2); }
291 target[datalength++] = Pad32;
292 if (datalength + 1 > targsize) { return (-2); }
293 target[datalength++] = Pad32;
295 if (srclength >= 4) {
296 if (datalength + 1 > targsize) { return (-2); }
297 target[datalength++] = B32_ar[output[6]];
298 } else {
299 if (datalength + 1 > targsize) { return (-2); }
300 target[datalength++] = Pad32;
302 if (datalength + 1 > targsize) { return (-2); }
303 target[datalength++] = Pad32;
305 if (datalength+1 > targsize) {
306 return (int) (datalength);
308 target[datalength] = '\0'; /* Returned value doesn't count \0. */
309 return (int) (datalength);
313 ldns_b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
314 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32);
317 /* deprecated, here for backwards compatibility */
319 b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
320 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32);
324 ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
325 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32_extended_hex);
328 /* deprecated, here for backwards compatibility */
330 b32_ntop_extended_hex(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
331 return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32_extended_hex);