b64_ntop.c revision 238104
1238104Sdes/*
2238104Sdes * Copyright (c) 1996, 1998 by Internet Software Consortium.
3238104Sdes *
4238104Sdes * Permission to use, copy, modify, and distribute this software for any
5238104Sdes * purpose with or without fee is hereby granted, provided that the above
6238104Sdes * copyright notice and this permission notice appear in all copies.
7238104Sdes *
8238104Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9238104Sdes * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10238104Sdes * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11238104Sdes * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12238104Sdes * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13238104Sdes * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14238104Sdes * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15238104Sdes * SOFTWARE.
16238104Sdes */
17238104Sdes
18238104Sdes/*
19238104Sdes * Portions Copyright (c) 1995 by International Business Machines, Inc.
20238104Sdes *
21238104Sdes * International Business Machines, Inc. (hereinafter called IBM) grants
22238104Sdes * permission under its copyrights to use, copy, modify, and distribute this
23238104Sdes * Software with or without fee, provided that the above copyright notice and
24238104Sdes * all paragraphs of this notice appear in all copies, and that the name of IBM
25238104Sdes * not be used in connection with the marketing of any product incorporating
26238104Sdes * the Software or modifications thereof, without specific, written prior
27238104Sdes * permission.
28238104Sdes *
29238104Sdes * To the extent it has a right to do so, IBM grants an immunity from suit
30238104Sdes * under its patents, if any, for the use, sale or manufacture of products to
31238104Sdes * the extent that such products are used for performing Domain Name System
32238104Sdes * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
33238104Sdes * granted for any product per se or for any other function of any product.
34238104Sdes *
35238104Sdes * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
36238104Sdes * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37238104Sdes * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
38238104Sdes * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
39238104Sdes * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
40238104Sdes * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
41238104Sdes */
42238104Sdes#include <ldns/config.h>
43238104Sdes
44238104Sdes#include <sys/types.h>
45238104Sdes#include <sys/param.h>
46238104Sdes#ifdef HAVE_SYS_SOCKET_H
47238104Sdes#include <sys/socket.h>
48238104Sdes#endif
49238104Sdes
50238104Sdes#ifdef HAVE_NETINET_IN_H
51238104Sdes#include <netinet/in.h>
52238104Sdes#endif
53238104Sdes#ifdef HAVE_ARPA_INET_H
54238104Sdes#include <arpa/inet.h>
55238104Sdes#endif
56238104Sdes
57238104Sdes#include <ctype.h>
58238104Sdes#include <stdio.h>
59238104Sdes#include <stdlib.h>
60238104Sdes#include <string.h>
61238104Sdes
62238104Sdes#define Assert(Cond) if (!(Cond)) abort()
63238104Sdes
64238104Sdesstatic const char Base64[] =
65238104Sdes	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
66238104Sdesstatic const char Pad64 = '=';
67238104Sdes
68238104Sdes/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
69238104Sdes   The following encoding technique is taken from RFC 1521 by Borenstein
70238104Sdes   and Freed.  It is reproduced here in a slightly edited form for
71238104Sdes   convenience.
72238104Sdes
73238104Sdes   A 65-character subset of US-ASCII is used, enabling 6 bits to be
74238104Sdes   represented per printable character. (The extra 65th character, "=",
75238104Sdes   is used to signify a special processing function.)
76238104Sdes
77238104Sdes   The encoding process represents 24-bit groups of input bits as output
78238104Sdes   strings of 4 encoded characters. Proceeding from left to right, a
79238104Sdes   24-bit input group is formed by concatenating 3 8-bit input groups.
80238104Sdes   These 24 bits are then treated as 4 concatenated 6-bit groups, each
81238104Sdes   of which is translated into a single digit in the base64 alphabet.
82238104Sdes
83238104Sdes   Each 6-bit group is used as an index into an array of 64 printable
84238104Sdes   characters. The character referenced by the index is placed in the
85238104Sdes   output string.
86238104Sdes
87238104Sdes                         Table 1: The Base64 Alphabet
88238104Sdes
89238104Sdes      Value Encoding  Value Encoding  Value Encoding  Value Encoding
90238104Sdes          0 A            17 R            34 i            51 z
91238104Sdes          1 B            18 S            35 j            52 0
92238104Sdes          2 C            19 T            36 k            53 1
93238104Sdes          3 D            20 U            37 l            54 2
94238104Sdes          4 E            21 V            38 m            55 3
95238104Sdes          5 F            22 W            39 n            56 4
96238104Sdes          6 G            23 X            40 o            57 5
97238104Sdes          7 H            24 Y            41 p            58 6
98238104Sdes          8 I            25 Z            42 q            59 7
99238104Sdes          9 J            26 a            43 r            60 8
100238104Sdes         10 K            27 b            44 s            61 9
101238104Sdes         11 L            28 c            45 t            62 +
102238104Sdes         12 M            29 d            46 u            63 /
103238104Sdes         13 N            30 e            47 v
104238104Sdes         14 O            31 f            48 w         (pad) =
105238104Sdes         15 P            32 g            49 x
106238104Sdes         16 Q            33 h            50 y
107238104Sdes
108238104Sdes   Special processing is performed if fewer than 24 bits are available
109238104Sdes   at the end of the data being encoded.  A full encoding quantum is
110238104Sdes   always completed at the end of a quantity.  When fewer than 24 input
111238104Sdes   bits are available in an input group, zero bits are added (on the
112238104Sdes   right) to form an integral number of 6-bit groups.  Padding at the
113238104Sdes   end of the data is performed using the '=' character.
114238104Sdes
115238104Sdes   Since all base64 input is an integral number of octets, only the
116238104Sdes         -------------------------------------------------
117238104Sdes   following cases can arise:
118238104Sdes
119238104Sdes       (1) the final quantum of encoding input is an integral
120238104Sdes           multiple of 24 bits; here, the final unit of encoded
121238104Sdes	   output will be an integral multiple of 4 characters
122238104Sdes	   with no "=" padding,
123238104Sdes       (2) the final quantum of encoding input is exactly 8 bits;
124238104Sdes           here, the final unit of encoded output will be two
125238104Sdes	   characters followed by two "=" padding characters, or
126238104Sdes       (3) the final quantum of encoding input is exactly 16 bits;
127238104Sdes           here, the final unit of encoded output will be three
128238104Sdes	   characters followed by one "=" padding character.
129238104Sdes   */
130238104Sdes
131238104Sdesint
132238104Sdesldns_b64_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) {
133238104Sdes	size_t datalength = 0;
134238104Sdes	uint8_t input[3];
135238104Sdes	uint8_t output[4];
136238104Sdes	size_t i;
137238104Sdes
138238104Sdes	if (srclength == 0) {
139238104Sdes		if (targsize > 0) {
140238104Sdes			target[0] = '\0';
141238104Sdes			return 0;
142238104Sdes		} else {
143238104Sdes			return -1;
144238104Sdes		}
145238104Sdes	}
146238104Sdes
147238104Sdes	while (2 < srclength) {
148238104Sdes		input[0] = *src++;
149238104Sdes		input[1] = *src++;
150238104Sdes		input[2] = *src++;
151238104Sdes		srclength -= 3;
152238104Sdes
153238104Sdes		output[0] = input[0] >> 2;
154238104Sdes		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
155238104Sdes		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
156238104Sdes		output[3] = input[2] & 0x3f;
157238104Sdes		Assert(output[0] < 64);
158238104Sdes		Assert(output[1] < 64);
159238104Sdes		Assert(output[2] < 64);
160238104Sdes		Assert(output[3] < 64);
161238104Sdes
162238104Sdes		if (datalength + 4 > targsize) {
163238104Sdes			return (-1);
164238104Sdes		}
165238104Sdes		target[datalength++] = Base64[output[0]];
166238104Sdes		target[datalength++] = Base64[output[1]];
167238104Sdes		target[datalength++] = Base64[output[2]];
168238104Sdes		target[datalength++] = Base64[output[3]];
169238104Sdes	}
170238104Sdes
171238104Sdes	/* Now we worry about padding. */
172238104Sdes	if (0 != srclength) {
173238104Sdes		/* Get what's left. */
174238104Sdes		input[0] = input[1] = input[2] = (uint8_t) '\0';
175238104Sdes		for (i = 0; i < srclength; i++)
176238104Sdes			input[i] = *src++;
177238104Sdes
178238104Sdes		output[0] = input[0] >> 2;
179238104Sdes		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
180238104Sdes		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
181238104Sdes		Assert(output[0] < 64);
182238104Sdes		Assert(output[1] < 64);
183238104Sdes		Assert(output[2] < 64);
184238104Sdes
185238104Sdes		if (datalength + 4 > targsize) {
186238104Sdes			return (-2);
187238104Sdes		}
188238104Sdes		target[datalength++] = Base64[output[0]];
189238104Sdes		target[datalength++] = Base64[output[1]];
190238104Sdes		if (srclength == 1) {
191238104Sdes			target[datalength++] = Pad64;
192238104Sdes		} else {
193238104Sdes			target[datalength++] = Base64[output[2]];
194238104Sdes		}
195238104Sdes		target[datalength++] = Pad64;
196238104Sdes	}
197238104Sdes	if (datalength >= targsize) {
198238104Sdes		return (-3);
199238104Sdes	}
200238104Sdes	target[datalength] = '\0';	/* Returned value doesn't count \0. */
201238104Sdes	return (int) (datalength);
202238104Sdes}
203