1/*	$NetBSD: base64.c,v 1.1.1.1 2011/05/02 07:01:09 agc Exp $	*/
2
3/*-
4 * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31/*
32 * Adapted for kernel mode use with minimal changes from
33 * /usr/src/crypto/dist/heimdal/lib/roken/base64.c
34 *
35 * Original:
36 * Copyright (c) 1995-2001 Kungliga Tekniska H�gskolan
37 * (Royal Institute of Technology, Stockholm, Sweden).
38 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
44 * 1. Redistributions of source code must retain the above copyright
45 *    notice, this list of conditions and the following disclaimer.
46 *
47 * 2. Redistributions in binary form must reproduce the above copyright
48 *    notice, this list of conditions and the following disclaimer in the
49 *    documentation and/or other materials provided with the distribution.
50 *
51 * 3. Neither the name of the Institute nor the names of its contributors
52 *    may be used to endorse or promote products derived from this software
53 *    without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68#include "iscsi_globals.h"
69#include "base64.h"
70
71static char base64_chars[] =
72	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
73
74static int
75pos(char c)
76{
77	char *p;
78
79	for (p = base64_chars; *p; p++) {
80		if (*p == c) {
81			return (int)(p - base64_chars);
82		}
83	}
84	return -1;
85}
86
87int
88base64_encode(const void *data, int size, uint8_t * buffer)
89{
90	uint8_t *p;
91	int i;
92	int c;
93	const uint8_t *q;
94
95	p = buffer;
96	q = (const uint8_t *) data;
97	*p++ = '0';
98	*p++ = 'b';
99
100	i = 0;
101	for (i = 0; i < size;) {
102		c = q[i++];
103		c *= 256;
104		if (i < size) {
105			c += q[i];
106		}
107		i++;
108		c *= 256;
109		if (i < size) {
110			c += q[i];
111		}
112		i++;
113		p[0] = base64_chars[(c & 0x00fc0000) >> 18];
114		p[1] = base64_chars[(c & 0x0003f000) >> 12];
115		p[2] = base64_chars[(c & 0x00000fc0) >> 6];
116		p[3] = base64_chars[(c & 0x0000003f) >> 0];
117		if (i > size) {
118			p[3] = '=';
119		}
120		if (i > size + 1) {
121			p[2] = '=';
122		}
123		p += 4;
124	}
125	*p = 0;
126	return strlen(buffer);
127}
128
129#define DECODE_ERROR 0xffffffff
130
131static uint32_t
132token_decode(uint8_t * token)
133{
134	int i;
135	uint32_t val = 0;
136	int marker = 0;
137
138	if (strlen(token) < 4) {
139		return DECODE_ERROR;
140	}
141	for (i = 0; i < 4; i++) {
142		val *= 64;
143		if (token[i] == '=') {
144			marker++;
145		} else if (marker > 0) {
146			return DECODE_ERROR;
147		} else {
148			val += pos(token[i]);
149		}
150	}
151	if (marker > 2) {
152		return DECODE_ERROR;
153	}
154	return (marker << 24) | val;
155}
156
157
158uint8_t *
159base64_decode(uint8_t * str, void *data, int *datalen)
160{
161	uint8_t *p, *q;
162	uint32_t marker = 0;
163
164	q = data;
165	for (p = str; *p; p += 4) {
166		uint32_t val = token_decode(p);
167		marker = (val >> 24) & 0xff;
168		if (val == DECODE_ERROR) {
169			return NULL;
170		}
171		*q++ = (val >> 16) & 0xff;
172		if (marker < 2) {
173			*q++ = (val >> 8) & 0xff;
174		}
175		if (marker < 1) {
176			*q++ = val & 0xff;
177		}
178	}
179	*datalen = (int)(q - (uint8_t *) data);
180
181	return p - marker + 1;
182}
183