Developer Area

root/tools/iphone-sdk/trunk/ldns_sources/b32_ntop.c @ 32

Revision 32, 11.0 kB (checked in by kjaleel, 6 years ago)

unzipped the DotTel?.zip and moved it into trunk

Line 
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
44#include "ldns.h"
45
46#include <sys/types.h>
47#include <sys/param.h>
48#ifdef HAVE_SYS_SOCKET_H
49#include <sys/socket.h>
50#endif
51
52#ifdef HAVE_NETINET_IN_H
53#include <netinet/in.h>
54#endif
55#ifdef HAVE_ARPA_INET_H
56#include <arpa/inet.h>
57#endif
58
59#include <ctype.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63
64#include <assert.h>
65
66static const char Base32[] =
67        "abcdefghijklmnopqrstuvwxyz234567";
68/*      "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";*/
69/*       00000000001111111111222222222233
70         01234567890123456789012345678901*/
71static const char Base32_extended_hex[] =
72/*      "0123456789ABCDEFGHIJKLMNOPQRSTUV";*/
73        "0123456789abcdefghijklmnopqrstuv";
74static const char Pad32 = '=';
75
76/* (From RFC3548 and draft-josefsson-rfc3548bis-00.txt)
775.  Base 32 Encoding
78
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
81   humanly readable.
82
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.)
86
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.
97
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.
102
103                      Table 3: The Base 32 Alphabet
104
105         Value Encoding  Value Encoding  Value Encoding  Value Encoding
106             0 A             9 J            18 S            27 3
107             1 B            10 K            19 T            28 4
108             2 C            11 L            20 U            29 5
109             3 D            12 M            21 V            30 6
110             4 E            13 N            22 W            31 7
111             5 F            14 O            23 X
112             6 G            15 P            24 Y         (pad) =
113             7 H            16 Q            25 Z
114             8 I            17 R            26 2
115
116
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
124   arise:
125
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,
129
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,
133
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,
137
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
141
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.
145
146
1476.  Base 32 Encoding with Extended Hex Alphabet
148
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".
152
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.
156
157   This encoding is identical to the previous one, except for the
158   alphabet.  The new alphabet is found in table 4.
159
160                     Table 4: The "Extended Hex" Base 32 Alphabet
161
162         Value Encoding  Value Encoding  Value Encoding  Value Encoding
163             0 0             9 9            18 I            27 R
164             1 1            10 A            19 J            28 S
165             2 2            11 B            20 K            29 T
166             3 3            12 C            21 L            30 U
167             4 4            13 D            22 M            31 V
168             5 5            14 E            23 N
169             6 6            15 F            24 O         (pad) =
170             7 7            16 G            25 P
171             8 8            17 H            26 Q
172
173*/
174
175
176int
177b32_ntop_ar(uint8_t const *src, size_t srclength, char *target, size_t targsize, const char B32_ar[]) {
178        size_t datalength = 0;
179        uint8_t input[5];
180        uint8_t output[8];
181        size_t i;
182        memset(output, 0, 8);
183
184        while (4 < srclength) {
185                input[0] = *src++;
186                input[1] = *src++;
187                input[2] = *src++;
188                input[3] = *src++;
189                input[4] = *src++;
190                srclength -= 5;
191
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);
200
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);
209
210                if (datalength + 8 > targsize) {
211                        return (-1);
212                }
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]];
221        }
222   
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++)
228                        input[i] = *src++;
229       
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);
237                }
238                if (srclength >= 2) {
239                        output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4);
240                        assert(output[3] < 32);
241                }
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);
247                }
248                if (srclength >= 4) {
249                        output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5);
250                        assert(output[6] < 32);
251                }
252
253
254                if (datalength + 1 > targsize) {
255                        return (-2);
256                }
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;
262                        } else {
263                                target[datalength++] = B32_ar[output[2]];
264                        }
265                } else {
266                        target[datalength++] = Pad32;
267                        target[datalength++] = Pad32;
268                }
269                if (srclength >= 2) {
270                        target[datalength++] = B32_ar[output[3]];
271                } else {
272                        target[datalength++] = Pad32;
273                }
274                if (srclength >= 3) {
275                        target[datalength++] = B32_ar[output[4]];
276                        if (srclength == 3 && output[5] == 0) {
277                                target[datalength++] = Pad32;
278                        } else {
279                                target[datalength++] = B32_ar[output[5]];
280                        }
281                } else {
282                        target[datalength++] = Pad32;
283                        target[datalength++] = Pad32;
284                }
285                if (srclength >= 4) {
286                        target[datalength++] = B32_ar[output[6]];
287                } else {
288                        target[datalength++] = Pad32;
289                }
290                target[datalength++] = Pad32;
291        }
292        if (datalength > targsize) {
293                return (-3);
294        }
295        target[datalength] = '\0';      /* Returned value doesn't count \0. */
296        return (int) (datalength);
297}
298
299int
300b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
301        return b32_ntop_ar(src, srclength, target, targsize, Base32);
302}
303
304int
305b32_ntop_extended_hex(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
306        return b32_ntop_ar(src, srclength, target, targsize, Base32_extended_hex);
307}
Note: See TracBrowser for help on using the browser.
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us