xref: /spdk/lib/util/base64.c (revision 712a3f69d32632bf6c862f00200f7f437d3f7529)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 #include "spdk/endian.h"
36 #include "spdk/base64.h"
37 
38 #ifdef __aarch64__
39 #include "base64_neon.c"
40 #endif
41 
42 #define BASE64_ENC_BITMASK 0x3FUL
43 #define BASE64_PADDING_CHAR '='
44 
45 static const char base64_enc_table[] =
46 	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 	"abcdefghijklmnopqrstuvwxyz"
48 	"0123456789+/";
49 
50 static const char base64_urfsafe_enc_table[] =
51 	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
52 	"abcdefghijklmnopqrstuvwxyz"
53 	"0123456789-_";
54 
55 static const uint8_t
56 base64_dec_table[] = {
57 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
58 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
59 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
60 	52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255, 255, 255, 255, 255,
61 	255,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
62 	15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
63 	255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
64 	41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51, 255, 255, 255, 255, 255,
65 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
66 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
67 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
68 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
69 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
70 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
71 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
72 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
73 };
74 
75 static const uint8_t
76 base64_urlsafe_dec_table[] = {
77 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
78 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
79 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  62, 255, 255,
80 	52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255, 255, 255, 255, 255,
81 	255,   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, 255, 255, 255, 255,  63,
83 	255,  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, 255, 255, 255, 255, 255,
85 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
86 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
87 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
88 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
89 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
90 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
91 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
92 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
93 };
94 
95 static int
96 _spdk_base64_encode(char *dst, const char *enc_table, const void *src, size_t src_len)
97 {
98 	uint32_t raw_u32;
99 
100 	if (!dst || !src || src_len <= 0) {
101 		return -EINVAL;
102 	}
103 
104 #ifdef __aarch64__
105 	_spdk_base64_encode_neon64(&dst, enc_table, &src, &src_len);
106 #endif
107 
108 	while (src_len >= 4) {
109 		raw_u32 = from_be32(src);
110 
111 		*dst++ = enc_table[(raw_u32 >> 26) & BASE64_ENC_BITMASK];
112 		*dst++ = enc_table[(raw_u32 >> 20) & BASE64_ENC_BITMASK];
113 		*dst++ = enc_table[(raw_u32 >> 14) & BASE64_ENC_BITMASK];
114 		*dst++ = enc_table[(raw_u32 >> 8) & BASE64_ENC_BITMASK];
115 
116 		src_len -= 3;
117 		src += 3;
118 	}
119 
120 	if (src_len == 0) {
121 		goto out;
122 	}
123 
124 	raw_u32 = 0;
125 	memcpy(&raw_u32, src, src_len);
126 	raw_u32 = from_be32(&raw_u32);
127 
128 	*dst++ = enc_table[(raw_u32 >> 26) & BASE64_ENC_BITMASK];
129 	*dst++ = enc_table[(raw_u32 >> 20) & BASE64_ENC_BITMASK];
130 	*dst++ = (src_len >= 2) ? enc_table[(raw_u32 >> 14) & BASE64_ENC_BITMASK] : BASE64_PADDING_CHAR;
131 	*dst++ = (src_len == 3) ? enc_table[(raw_u32 >> 8) & BASE64_ENC_BITMASK] : BASE64_PADDING_CHAR;
132 
133 out:
134 	*dst = '\0';
135 
136 	return 0;
137 }
138 
139 int
140 spdk_base64_encode(char *dst, const void *src, size_t src_len)
141 {
142 	return _spdk_base64_encode(dst, base64_enc_table, src, src_len);
143 }
144 
145 int
146 spdk_base64_urlsafe_encode(char *dst, const void *src, size_t src_len)
147 {
148 	return _spdk_base64_encode(dst, base64_urfsafe_enc_table, src, src_len);
149 }
150 
151 #ifdef __aarch64__
152 static int
153 _spdk_base64_decode(void *dst, size_t *_dst_len, const uint8_t *dec_table,
154 		    const uint8_t *dec_table_opt, const char *src)
155 #else
156 static int
157 _spdk_base64_decode(void *dst, size_t *_dst_len, const uint8_t *dec_table, const char *src)
158 #endif
159 {
160 	size_t src_strlen;
161 	size_t tail_len = 0;
162 	const uint8_t *src_in;
163 	uint32_t tmp[4];
164 	int i;
165 
166 	if (!src) {
167 		return -EINVAL;
168 	}
169 
170 	src_strlen = strlen(src);
171 
172 	/* strlen of src should be 4n */
173 	if (src_strlen == 0 || src_strlen % 4 != 0) {
174 		return -EINVAL;
175 	}
176 
177 	/* Consider Base64 padding, it at most has 2 padding characters. */
178 	for (i = 0; i < 2; i++) {
179 		if (src[src_strlen - 1] != BASE64_PADDING_CHAR) {
180 			break;
181 		}
182 		src_strlen--;
183 	}
184 
185 	/* strlen of src without padding shouldn't be 4n+1 */
186 	if (src_strlen == 0 || src_strlen % 4 == 1) {
187 		return -EINVAL;
188 	}
189 
190 	if (_dst_len) {
191 		*_dst_len = spdk_base64_get_decoded_len(src_strlen);
192 	}
193 
194 	/* If dst is NULL, the client is only concerned w/ _dst_len, return */
195 	if (!dst) {
196 		return 0;
197 	}
198 
199 	src_in = (const uint8_t *) src;
200 
201 #ifdef __aarch64__
202 	_spdk_base64_decode_neon64(&dst, dec_table_opt, &src_in, &src_strlen);
203 
204 	if (src_strlen == 0) {
205 		return 0;
206 	}
207 #endif
208 
209 	/* space of dst can be used by to_be32 */
210 	while (src_strlen > 4) {
211 		tmp[0] = dec_table[*src_in++];
212 		tmp[1] = dec_table[*src_in++];
213 		tmp[2] = dec_table[*src_in++];
214 		tmp[3] = dec_table[*src_in++];
215 
216 		if (tmp[0] == 255 || tmp[1] == 255 || tmp[2] == 255 || tmp[3] == 255) {
217 			return -EINVAL;
218 		}
219 
220 		to_be32(dst, tmp[3] << 8 | tmp[2] << 14 | tmp[1] << 20 | tmp[0] << 26);
221 
222 		dst += 3;
223 		src_strlen -= 4;
224 	}
225 
226 	/* space of dst is not enough to be used by to_be32 */
227 	tmp[0] = dec_table[src_in[0]];
228 	tmp[1] = dec_table[src_in[1]];
229 	tmp[2] = (src_strlen >= 3) ? dec_table[src_in[2]] : 0;
230 	tmp[3] = (src_strlen == 4) ? dec_table[src_in[3]] : 0;
231 	tail_len = src_strlen - 1;
232 
233 	if (tmp[0] == 255 || tmp[1] == 255 || tmp[2] == 255 || tmp[3] == 255) {
234 		return -EINVAL;
235 	}
236 
237 	to_be32(&tmp[3], tmp[3] << 8 | tmp[2] << 14 | tmp[1] << 20 | tmp[0] << 26);
238 	memcpy(dst, (uint8_t *)&tmp[3], tail_len);
239 
240 	return 0;
241 }
242 
243 int
244 spdk_base64_decode(void *dst, size_t *dst_len, const char *src)
245 {
246 #ifdef __aarch64__
247 	return _spdk_base64_decode(dst, dst_len, base64_dec_table, base64_dec_table_neon64, src);
248 #else
249 	return _spdk_base64_decode(dst, dst_len, base64_dec_table, src);
250 #endif
251 }
252 
253 int
254 spdk_base64_urlsafe_decode(void *dst, size_t *dst_len, const char *src)
255 {
256 #ifdef __aarch64__
257 	return _spdk_base64_decode(dst, dst_len, base64_urlsafe_dec_table, base64_urlsafe_dec_table_neon64,
258 				   src);
259 #else
260 	return _spdk_base64_decode(dst, dst_len, base64_urlsafe_dec_table, src);
261 #endif
262 }
263