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