1 /* $OpenBSD: ap_base64.c,v 1.8 2005/03/28 21:03:33 niallo Exp $ */
2 
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  *    if any, must include the following acknowledgment:
23  *       "This product includes software developed by the
24  *        Apache Software Foundation (http://www.apache.org/)."
25  *    Alternately, this acknowledgment may appear in the software itself,
26  *    if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" must
29  *    not be used to endorse or promote products derived from this
30  *    software without prior written permission. For written
31  *    permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  *    nor may "Apache" appear in their name, without prior written
35  *    permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation.  For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  * Portions of this software are based upon public domain software
57  * originally written at the National Center for Supercomputing Applications,
58  * University of Illinois, Urbana-Champaign.
59  */
60 
61 /* base64 encoder/decoder. Originally part of main/util.c
62  * but moved here so that support/ab and ap_sha1.c could
63  * use it. This meant removing the ap_palloc()s and adding
64  * ugly 'len' functions, which is quite a nasty cost.
65  */
66 
67 #include <string.h>
68 
69 #include "ap_config.h"
70 #include "ap.h"
71 
72 
73 /* aaaack but it's fast and const should make it shared text page. */
74 static const unsigned char pr2six[256] =
75 {
76 	/* ASCII table */
77 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
78 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
79 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
80 	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
81 	64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
82 	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
83 	64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
84 	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
85 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
86 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
87 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
88 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
89 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
90 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
91 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
92 	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
93 };
94 
95 API_EXPORT(int)
ap_base64decode_len(const char * bufcoded)96 ap_base64decode_len(const char *bufcoded)
97 {
98 	int nbytesdecoded;
99 	register const unsigned char *bufin;
100 	register int nprbytes;
101 
102 	bufin = (const unsigned char *) bufcoded;
103 	while (pr2six[*(bufin++)] <= 63);
104 
105 	nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
106 	nbytesdecoded = ((nprbytes + 3) / 4) * 3;
107 
108 	return nbytesdecoded + 1;
109 }
110 
111 API_EXPORT(int)
ap_base64decode(char * bufplain,const char * bufcoded)112 ap_base64decode(char *bufplain, const char *bufcoded)
113 {
114 	int len;
115 
116 	len = ap_base64decode_binary((unsigned char *) bufplain, bufcoded);
117 	bufplain[len] = '\0';
118 	return len;
119 }
120 
121 /* This is the same as ap_base64udecode() except on EBCDIC machines, where
122  * the conversion of the output to ebcdic is left out.
123  */
124 API_EXPORT(int)
ap_base64decode_binary(unsigned char * bufplain,const char * bufcoded)125 ap_base64decode_binary(unsigned char *bufplain, const char *bufcoded)
126 {
127 	int nbytesdecoded;
128 	register const unsigned char *bufin;
129 	register unsigned char *bufout;
130 	register int nprbytes;
131 	bufin = (const unsigned char *) bufcoded;
132 	while (pr2six[*(bufin++)] <= 63);
133 	nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
134 	nbytesdecoded = ((nprbytes + 3) / 4) * 3;
135 
136 	bufout = (unsigned char *) bufplain;
137 	bufin = (const unsigned char *) bufcoded;
138 
139 	while (nprbytes > 4) {
140 		*(bufout++) = (unsigned char) (pr2six[*bufin] << 2
141 		    | pr2six[bufin[1]] >> 4);
142 		*(bufout++) = (unsigned char) (pr2six[bufin[1]] << 4
143 		    | pr2six[bufin[2]] >> 2);
144 		*(bufout++) = (unsigned char) (pr2six[bufin[2]] << 6
145 		    | pr2six[bufin[3]]);
146 		bufin += 4;
147 		nprbytes -= 4;
148 	}
149 
150 	/* Note: (nprbytes == 1) would be an error, so just ingore that case */
151 	if (nprbytes > 1)
152 		*(bufout++) = (unsigned char) (pr2six[*bufin] << 2
153 		    | pr2six[bufin[1]] >> 4);
154 	if (nprbytes > 2)
155 		*(bufout++) = (unsigned char) (pr2six[bufin[1]] << 4
156 		    | pr2six[bufin[2]] >> 2);
157 	if (nprbytes > 3)
158 		*(bufout++) = (unsigned char) (pr2six[bufin[2]] << 6
159 		    | pr2six[bufin[3]]);
160 
161 	nbytesdecoded -= (4 - nprbytes) & 3;
162 	return nbytesdecoded;
163 }
164 
165 static const char basis_64[] =
166 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
167 
168 API_EXPORT(int)
ap_base64encode_len(int len)169 ap_base64encode_len(int len)
170 {
171 	return ((len + 2) / 3 * 4) + 1;
172 }
173 
174 API_EXPORT(int)
ap_base64encode(char * encoded,const char * string,int len)175 ap_base64encode(char *encoded, const char *string, int len)
176 {
177 	return ap_base64encode_binary(encoded, (const unsigned char *) string,
178 	    len);
179 }
180 
181 /* This is the same as ap_base64encode() except on EBCDIC machines, where
182  * the conversion of the input to ascii is left out.
183  */
184 API_EXPORT(int)
ap_base64encode_binary(char * encoded,const unsigned char * string,int len)185 ap_base64encode_binary(char *encoded, const unsigned char *string, int len)
186 {
187 	int i;
188 	char *p;
189 
190 	p = encoded;
191 	for (i = 0; i < len - 2; i += 3) {
192 		*p++ = basis_64[(string[i] >> 2) & 0x3F];
193 		*p++ = basis_64[((string[i] & 0x3) << 4) |
194 		    ((int) (string[i + 1] & 0xF0) >> 4)];
195 		*p++ = basis_64[((string[i + 1] & 0xF) << 2) |
196 		    ((int) (string[i + 2] & 0xC0) >> 6)];
197 		*p++ = basis_64[string[i + 2] & 0x3F];
198 	}
199 	if (i < len) {
200 		*p++ = basis_64[(string[i] >> 2) & 0x3F];
201 		if (i == (len - 1)) {
202 			*p++ = basis_64[((string[i] & 0x3) << 4)];
203 			*p++ = '=';
204 		}
205 		else {
206 			*p++ = basis_64[((string[i] & 0x3) << 4) |
207 			    ((int) (string[i + 1] & 0xF0) >> 4)];
208 			*p++ = basis_64[((string[i + 1] & 0xF) << 2)];
209 		}
210 		*p++ = '=';
211 	}
212 
213 	*p++ = '\0';
214 	return p - encoded;
215 }
216