/src/barebox/lib/base64.c
Line | Count | Source |
1 | | /* |
2 | | * Code based on busybox-1.23.2 |
3 | | * |
4 | | * Copyright 2003, Glenn McGrath |
5 | | * Copyright 2006, Rob Landley <rob@landley.net> |
6 | | * Copyright 2010, Denys Vlasenko |
7 | | * |
8 | | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
9 | | */ |
10 | | |
11 | | #include <common.h> |
12 | | #include <base64.h> |
13 | | |
14 | | /* Conversion table. for base 64 */ |
15 | | static const char uuenc_tbl_base64[65 + 1] = { |
16 | | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
17 | | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
18 | | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
19 | | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
20 | | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
21 | | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
22 | | 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
23 | | '4', '5', '6', '7', '8', '9', '+', '/', |
24 | | '=' /* termination character */, |
25 | | '\0' /* needed for uudecode.c only */ |
26 | | }; |
27 | | |
28 | | static char base64_trchr(char ch, bool url) |
29 | 34.3M | { |
30 | 34.3M | if (!url) |
31 | 0 | return ch; |
32 | | |
33 | 34.3M | switch (ch) { |
34 | 7 | case '+': |
35 | 7 | return '-'; |
36 | 7 | case '/': |
37 | 7 | return '_'; |
38 | 9.59k | case '-': |
39 | 9.59k | return '+'; |
40 | 1.11k | case '_': |
41 | 1.11k | return '/'; |
42 | 34.2M | default: |
43 | 34.2M | return ch; |
44 | 34.3M | } |
45 | 34.3M | } |
46 | | |
47 | | /* |
48 | | * Encode bytes at S of length LENGTH to uuencode or base64 format and place it |
49 | | * to STORE. STORE will be 0-terminated, and must point to a writable |
50 | | * buffer of at least 1+BASE64_LENGTH(length) bytes. |
51 | | * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3)) |
52 | | */ |
53 | | void uuencode(char *p, const char *src, int length) |
54 | 0 | { |
55 | 0 | const unsigned char *s = src; |
56 | 0 | const char *tbl = uuenc_tbl_base64; |
57 | | |
58 | | /* Transform the 3x8 bits to 4x6 bits */ |
59 | 0 | while (length > 0) { |
60 | 0 | unsigned s1, s2; |
61 | | |
62 | | /* Are s[1], s[2] valid or should be assumed 0? */ |
63 | 0 | s1 = s2 = 0; |
64 | 0 | length -= 3; /* can be >=0, -1, -2 */ |
65 | 0 | if (length >= -1) { |
66 | 0 | s1 = s[1]; |
67 | 0 | if (length >= 0) |
68 | 0 | s2 = s[2]; |
69 | 0 | } |
70 | 0 | *p++ = tbl[s[0] >> 2]; |
71 | 0 | *p++ = tbl[((s[0] & 3) << 4) + (s1 >> 4)]; |
72 | 0 | *p++ = tbl[((s1 & 0xf) << 2) + (s2 >> 6)]; |
73 | 0 | *p++ = tbl[s2 & 0x3f]; |
74 | 0 | s += 3; |
75 | 0 | } |
76 | | /* Zero-terminate */ |
77 | 0 | *p = '\0'; |
78 | | /* If length is -2 or -1, pad last char or two */ |
79 | 0 | while (length) { |
80 | 0 | *--p = tbl[64]; |
81 | 0 | length++; |
82 | 0 | } |
83 | 0 | } |
84 | | EXPORT_SYMBOL(uuencode); |
85 | | |
86 | | /* |
87 | | * Decode base64 encoded string. Stops on '\0'. |
88 | | * |
89 | | */ |
90 | | static int __decode_base64(char *p_dst, int dst_len, const char *src, bool url) |
91 | 2.42k | { |
92 | 2.42k | char *dst = p_dst; |
93 | 2.42k | int length = 0; |
94 | 2.42k | bool end_reached = false; |
95 | | |
96 | 11.2M | while (dst_len > 0 && !end_reached) { |
97 | 11.2M | unsigned char six_bit[4]; |
98 | 11.2M | int count = 0; |
99 | | |
100 | | /* Fetch up to four 6-bit values */ |
101 | 41.5M | while (count < 4) { |
102 | 34.3M | const char *table_ptr; |
103 | 34.3M | int ch; |
104 | | |
105 | | /* |
106 | | * Get next _valid_ character. |
107 | | * uuenc_tbl_base64[] contains this string: |
108 | | * 0 1 2 3 4 5 6 |
109 | | * 01234567890123456789012345678901234567890123456789012345678901234 |
110 | | * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" |
111 | | */ |
112 | 34.3M | do { |
113 | 34.3M | ch = *src; |
114 | 34.3M | if (ch == '\0') { |
115 | | /* |
116 | | * Example: |
117 | | * If we decode "QUJD <NUL>", we want |
118 | | * to return ptr to NUL, not to ' ', |
119 | | * because we did fully decode |
120 | | * the string (to "ABC"). |
121 | | */ |
122 | 1.14k | if (count != 0 && url) { |
123 | 1.06k | end_reached = true; |
124 | 1.06k | goto out; |
125 | 1.06k | } |
126 | | |
127 | 83 | goto ret; |
128 | 1.14k | } |
129 | 34.3M | src++; |
130 | 34.3M | table_ptr = strchr(uuenc_tbl_base64, base64_trchr(ch, url)); |
131 | 34.3M | } while (!table_ptr && !url); |
132 | | |
133 | 34.3M | if (!table_ptr) { |
134 | 1.24k | end_reached = true; |
135 | 1.24k | goto out; |
136 | 1.24k | } |
137 | | |
138 | | /* Convert encoded character to decimal */ |
139 | 34.3M | ch = table_ptr - uuenc_tbl_base64; |
140 | | |
141 | | /* ch is 64 if char was '=', otherwise 0..63 */ |
142 | 34.3M | if (ch == 64) |
143 | 3.94M | break; |
144 | | |
145 | 30.3M | six_bit[count] = ch; |
146 | 30.3M | count++; |
147 | 30.3M | } |
148 | 11.2M | out: |
149 | | |
150 | | /* |
151 | | * Transform 6-bit values to 8-bit ones. |
152 | | * count can be < 4 when we decode the tail: |
153 | | * "eQ==" -> "y", not "y NUL NUL". |
154 | | * Note that (count > 1) is always true, |
155 | | * "x===" encoding is not valid: |
156 | | * even a single zero byte encodes as "AA==". |
157 | | * However, with current logic we come here with count == 1 |
158 | | * when we decode "==" tail. |
159 | | */ |
160 | 11.2M | if (count > 1) |
161 | 7.67M | *dst++ = six_bit[0] << 2 | six_bit[1] >> 4; |
162 | 11.2M | if (count > 2 && dst_len > 1) |
163 | 7.62M | *dst++ = six_bit[1] << 4 | six_bit[2] >> 2; |
164 | 11.2M | if (count > 3 && dst_len > 2) |
165 | 7.26M | *dst++ = six_bit[2] << 6 | six_bit[3]; |
166 | | /* last character was "=" */ |
167 | 11.2M | if (count != 0) |
168 | 7.80M | length += min(count - 1, dst_len); |
169 | | /* |
170 | | * Note that if we decode "AA==" and ate first '=', |
171 | | * we just decoded one char (count == 2) and now we'll |
172 | | * do the loop once more to decode second '='. |
173 | | */ |
174 | 11.2M | dst_len -= count-1; |
175 | 11.2M | } |
176 | 2.42k | ret: |
177 | 2.42k | p_dst = dst; |
178 | | |
179 | 2.42k | return length; |
180 | 2.42k | } |
181 | | |
182 | | /* |
183 | | * Decode base64 encoded string. Stops on '\0'. |
184 | | * |
185 | | */ |
186 | | int decode_base64(char *p_dst, int dst_len, const char *src) |
187 | 0 | { |
188 | 0 | return __decode_base64(p_dst, dst_len, src, false); |
189 | 0 | } |
190 | | EXPORT_SYMBOL(decode_base64); |
191 | | |
192 | | /* |
193 | | * Decode base64url encoded string. Stops on '\0'. |
194 | | * |
195 | | */ |
196 | | int decode_base64url(char *p_dst, int dst_len, const char *src) |
197 | 2.42k | { |
198 | 2.42k | return __decode_base64(p_dst, dst_len, src, true); |
199 | 2.42k | } |
200 | | EXPORT_SYMBOL(decode_base64url); |