1/*	$NetBSD: base64.c,v 1.2 2017/04/18 17:28:18 maya 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	for (i = 0; i < size;) {
101		c = q[i++];
102		c *= 256;
103		if (i < size) {
104			c += q[i];
105		}
106		i++;
107		c *= 256;
108		if (i < size) {
109			c += q[i];
110		}
111		i++;
112		p[0] = base64_chars[(c & 0x00fc0000) >> 18];
113		p[1] = base64_chars[(c & 0x0003f000) >> 12];
114		p[2] = base64_chars[(c & 0x00000fc0) >> 6];
115		p[3] = base64_chars[(c & 0x0000003f) >> 0];
116		if (i > size) {
117			p[3] = '=';
118		}
119		if (i > size + 1) {
120			p[2] = '=';
121		}
122		p += 4;
123	}
124	*p = 0;
125	return strlen(buffer);
126}
127
128#define DECODE_ERROR 0xffffffff
129
130static uint32_t
131token_decode(uint8_t * token)
132{
133	int i;
134	uint32_t val = 0;
135	int marker = 0;
136
137	if (strlen(token) < 4) {
138		return DECODE_ERROR;
139	}
140	for (i = 0; i < 4; i++) {
141		val *= 64;
142		if (token[i] == '=') {
143			marker++;
144		} else if (marker > 0) {
145			return DECODE_ERROR;
146		} else {
147			val += pos(token[i]);
148		}
149	}
150	if (marker > 2) {
151		return DECODE_ERROR;
152	}
153	return (marker << 24) | val;
154}
155
156
157uint8_t *
158base64_decode(uint8_t * str, void *data, int *datalen)
159{
160	uint8_t *p, *q;
161	uint32_t marker = 0;
162
163	q = data;
164	for (p = str; *p; p += 4) {
165		uint32_t val = token_decode(p);
166		marker = (val >> 24) & 0xff;
167		if (val == DECODE_ERROR) {
168			return NULL;
169		}
170		*q++ = (val >> 16) & 0xff;
171		if (marker < 2) {
172			*q++ = (val >> 8) & 0xff;
173		}
174		if (marker < 1) {
175			*q++ = val & 0xff;
176		}
177	}
178	*datalen = (int)(q - (uint8_t *) data);
179
180	return p - marker + 1;
181}
182