xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/roken/base64.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: base64.c,v 1.2 2017/01/28 21:31:50 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 1995-2001 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include <config.h>
37ca1c9b0cSelric 
38ca1c9b0cSelric #include <stdlib.h>
39ca1c9b0cSelric #include <string.h>
40ca1c9b0cSelric #include <limits.h>
41ca1c9b0cSelric #include <krb5/base64.h>
42ca1c9b0cSelric 
43ca1c9b0cSelric static const char base64_chars[] =
44ca1c9b0cSelric     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45ca1c9b0cSelric 
46ca1c9b0cSelric static int
pos(char c)47ca1c9b0cSelric pos(char c)
48ca1c9b0cSelric {
49ca1c9b0cSelric     const char *p;
50ca1c9b0cSelric     for (p = base64_chars; *p; p++)
51ca1c9b0cSelric 	if (*p == c)
52ca1c9b0cSelric 	    return p - base64_chars;
53ca1c9b0cSelric     return -1;
54ca1c9b0cSelric }
55ca1c9b0cSelric 
56ca1c9b0cSelric ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_base64_encode(const void * data,int size,char ** str)57b9d004c6Schristos rk_base64_encode(const void *data, int size, char **str)
58ca1c9b0cSelric {
59ca1c9b0cSelric     char *s, *p;
60ca1c9b0cSelric     int i;
61ca1c9b0cSelric     int c;
62ca1c9b0cSelric     const unsigned char *q;
63ca1c9b0cSelric 
64ca1c9b0cSelric     if (size > INT_MAX/4 || size < 0) {
65ca1c9b0cSelric 	*str = NULL;
66ca1c9b0cSelric 	return -1;
67ca1c9b0cSelric     }
68ca1c9b0cSelric 
69ca1c9b0cSelric     p = s = (char *) malloc(size * 4 / 3 + 4);
70ca1c9b0cSelric     if (p == NULL) {
71ca1c9b0cSelric         *str = NULL;
72ca1c9b0cSelric 	return -1;
73ca1c9b0cSelric     }
74ca1c9b0cSelric     q = (const unsigned char *) data;
75ca1c9b0cSelric 
76ca1c9b0cSelric     for (i = 0; i < size;) {
77ca1c9b0cSelric 	c = q[i++];
78ca1c9b0cSelric 	c *= 256;
79ca1c9b0cSelric 	if (i < size)
80ca1c9b0cSelric 	    c += q[i];
81ca1c9b0cSelric 	i++;
82ca1c9b0cSelric 	c *= 256;
83ca1c9b0cSelric 	if (i < size)
84ca1c9b0cSelric 	    c += q[i];
85ca1c9b0cSelric 	i++;
86ca1c9b0cSelric 	p[0] = base64_chars[(c & 0x00fc0000) >> 18];
87ca1c9b0cSelric 	p[1] = base64_chars[(c & 0x0003f000) >> 12];
88ca1c9b0cSelric 	p[2] = base64_chars[(c & 0x00000fc0) >> 6];
89ca1c9b0cSelric 	p[3] = base64_chars[(c & 0x0000003f) >> 0];
90ca1c9b0cSelric 	if (i > size)
91ca1c9b0cSelric 	    p[3] = '=';
92ca1c9b0cSelric 	if (i > size + 1)
93ca1c9b0cSelric 	    p[2] = '=';
94ca1c9b0cSelric 	p += 4;
95ca1c9b0cSelric     }
96ca1c9b0cSelric     *p = 0;
97ca1c9b0cSelric     *str = s;
98ca1c9b0cSelric     return (int) strlen(s);
99ca1c9b0cSelric }
100ca1c9b0cSelric 
101ca1c9b0cSelric #define DECODE_ERROR 0xffffffff
102ca1c9b0cSelric 
103ca1c9b0cSelric static unsigned int
token_decode(const char * token)104ca1c9b0cSelric token_decode(const char *token)
105ca1c9b0cSelric {
106ca1c9b0cSelric     int i;
107ca1c9b0cSelric     unsigned int val = 0;
108ca1c9b0cSelric     int marker = 0;
109ca1c9b0cSelric     if (strlen(token) < 4)
110ca1c9b0cSelric 	return DECODE_ERROR;
111ca1c9b0cSelric     for (i = 0; i < 4; i++) {
112ca1c9b0cSelric 	val *= 64;
113ca1c9b0cSelric 	if (token[i] == '=')
114ca1c9b0cSelric 	    marker++;
115ca1c9b0cSelric 	else if (marker > 0)
116ca1c9b0cSelric 	    return DECODE_ERROR;
117ca1c9b0cSelric 	else
118ca1c9b0cSelric 	    val += pos(token[i]);
119ca1c9b0cSelric     }
120ca1c9b0cSelric     if (marker > 2)
121ca1c9b0cSelric 	return DECODE_ERROR;
122ca1c9b0cSelric     return (marker << 24) | val;
123ca1c9b0cSelric }
124ca1c9b0cSelric 
125ca1c9b0cSelric ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_base64_decode(const char * str,void * data)126b9d004c6Schristos rk_base64_decode(const char *str, void *data)
127ca1c9b0cSelric {
128ca1c9b0cSelric     const char *p;
129ca1c9b0cSelric     unsigned char *q;
130ca1c9b0cSelric 
131ca1c9b0cSelric     q = data;
132ca1c9b0cSelric     for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
133ca1c9b0cSelric 	unsigned int val = token_decode(p);
134ca1c9b0cSelric 	unsigned int marker = (val >> 24) & 0xff;
135ca1c9b0cSelric 	if (val == DECODE_ERROR)
136ca1c9b0cSelric 	    return -1;
137ca1c9b0cSelric 	*q++ = (val >> 16) & 0xff;
138ca1c9b0cSelric 	if (marker < 2)
139ca1c9b0cSelric 	    *q++ = (val >> 8) & 0xff;
140ca1c9b0cSelric 	if (marker < 1)
141ca1c9b0cSelric 	    *q++ = val & 0xff;
142ca1c9b0cSelric     }
143ca1c9b0cSelric     return q - (unsigned char *) data;
144ca1c9b0cSelric }
145