OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 /* | 5 /* |
6 * Base64 decoding (ascii to binary). | 6 * Base64 decoding (ascii to binary). |
7 */ | 7 */ |
8 | 8 |
9 #include "nssb64.h" | 9 #include "nssb64.h" |
10 #include "nspr.h" | 10 #include "nspr.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 * and sometimes I left them when I thought the subroutines were at least | 58 * and sometimes I left them when I thought the subroutines were at least |
59 * self-consistent. | 59 * self-consistent. |
60 */ | 60 */ |
61 | 61 |
62 PR_BEGIN_EXTERN_C | 62 PR_BEGIN_EXTERN_C |
63 | 63 |
64 /* | 64 /* |
65 * Opaque object used by the decoder to store state. | 65 * Opaque object used by the decoder to store state. |
66 */ | 66 */ |
67 struct PLBase64DecoderStr { | 67 struct PLBase64DecoderStr { |
68 /* Current token (or portion, if token_size < 4) being decoded. */ | 68 /* Current token (or portion, if token_size < 4) being decoded. */ |
69 unsigned char token[4]; | 69 unsigned char token[4]; |
70 int token_size; | 70 int token_size; |
71 | 71 |
72 /* | 72 /* |
73 * Where to write the decoded data (used when streaming, not when | 73 * Where to write the decoded data (used when streaming, not when |
74 * doing all in-memory (buffer) operations). | 74 * doing all in-memory (buffer) operations). |
75 * | 75 * |
76 * Note that this definition is chosen to be compatible with PR_Write. | 76 * Note that this definition is chosen to be compatible with PR_Write. |
77 */ | 77 */ |
78 PRInt32 (*output_fn) (void *output_arg, const unsigned char *buf, | 78 PRInt32 (*output_fn)(void *output_arg, const unsigned char *buf, |
79 » » » PRInt32 size); | 79 PRInt32 size); |
80 void *output_arg; | 80 void *output_arg; |
81 | 81 |
82 /* | 82 /* |
83 * Where the decoded output goes -- either temporarily (in the streaming | 83 * Where the decoded output goes -- either temporarily (in the streaming |
84 * case, staged here before it goes to the output function) or what will | 84 * case, staged here before it goes to the output function) or what will |
85 * be the entire buffered result for users of the buffer version. | 85 * be the entire buffered result for users of the buffer version. |
86 */ | 86 */ |
87 unsigned char *output_buffer; | 87 unsigned char *output_buffer; |
88 PRUint32 output_buflen;» /* the total length of allocated buffer */ | 88 PRUint32 output_buflen; /* the total length of allocated buffer */ |
89 PRUint32 output_length;» /* the length that is currently populated */ | 89 PRUint32 output_length; /* the length that is currently populated */ |
90 }; | 90 }; |
91 | 91 |
92 PR_END_EXTERN_C | 92 PR_END_EXTERN_C |
93 | 93 |
94 | |
95 /* | 94 /* |
96 * Table to convert an ascii "code" to its corresponding binary value. | 95 * Table to convert an ascii "code" to its corresponding binary value. |
97 * For ease of use, the binary values in the table are the actual values | 96 * For ease of use, the binary values in the table are the actual values |
98 * PLUS ONE. This is so that the special value of zero can denote an | 97 * PLUS ONE. This is so that the special value of zero can denote an |
99 * invalid mapping; that was much easier than trying to fill in the other | 98 * invalid mapping; that was much easier than trying to fill in the other |
100 * values with some value other than zero, and to check for it. | 99 * values with some value other than zero, and to check for it. |
101 * Just remember to SUBTRACT ONE when using the value retrieved. | 100 * Just remember to SUBTRACT ONE when using the value retrieved. |
102 */ | 101 */ |
103 static unsigned char base64_codetovaluep1[256] = { | 102 static unsigned char base64_codetovaluep1[256] = { |
104 /* 0: */» 0,» 0,» 0,» 0,» 0,» 0,» 0,» 0, | 103 /* 0: */ 0, 0, 0, 0, 0, |
105 /* 8: */» 0,» 0,» 0,» 0,» 0,» 0,» 0,» 0, | 104 0, 0, 0, |
106 /* 16: */» 0,» 0,» 0,» 0,» 0,» 0,» 0,» 0, | 105 /* 8: */ 0, 0, 0, 0, 0, |
107 /* 24: */» 0,» 0,» 0,» 0,» 0,» 0,» 0,» 0, | 106 0, 0, 0, |
108 /* 32: */» 0,» 0,» 0,» 0,» 0,» 0,» 0,» 0, | 107 /* 16: */ 0, 0, 0, 0, 0, |
109 /* 40: */» 0,» 0,» 0,» 63,» 0,» 0,» 0,» 64, | 108 0, 0, 0, |
110 /* 48: */» 53,» 54,» 55,» 56,» 57,» 58,» 59,» 60, | 109 /* 24: */ 0, 0, 0, 0, 0, |
111 /* 56: */» 61,» 62,» 0,» 0,» 0,» 0,» 0,» 0, | 110 0, 0, 0, |
112 /* 64: */» 0,» 1,» 2,» 3,» 4,» 5,» 6,» 7, | 111 /* 32: */ 0, 0, 0, 0, 0, |
113 /* 72: */» 8,» 9,» 10,» 11,» 12,» 13,» 14,» 15, | 112 0, 0, 0, |
114 /* 80: */» 16,» 17,» 18,» 19,» 20,» 21,» 22,» 23, | 113 /* 40: */ 0, 0, 0, 63, 0, |
115 /* 88: */» 24,» 25,» 26,» 0,» 0,» 0,» 0,» 0, | 114 0, 0, 64, |
116 /* 96: */» 0,» 27,» 28,» 29,» 30,» 31,» 32,» 33, | 115 /* 48: */ 53, 54, 55, 56, 57, |
117 /* 104: */» 34,» 35,» 36,» 37,» 38,» 39,» 40,» 41, | 116 58, 59, 60, |
118 /* 112: */» 42,» 43,» 44,» 45,» 46,» 47,» 48,» 49, | 117 /* 56: */ 61, 62, 0, 0, 0, |
119 /* 120: */» 50,» 51,» 52,» 0,» 0,» 0,» 0,» 0, | 118 0, 0, 0, |
120 /* 128: */» 0,» 0,» 0,» 0,» 0,» 0,» 0,» 0 | 119 /* 64: */ 0, 1, 2, 3, 4, |
121 /* and rest are all zero as well */ | 120 5, 6, 7, |
| 121 /* 72: */ 8, 9, 10, 11, 12, |
| 122 13, 14, 15, |
| 123 /* 80: */ 16, 17, 18, 19, 20, |
| 124 21, 22, 23, |
| 125 /* 88: */ 24, 25, 26, 0, 0, |
| 126 0, 0, 0, |
| 127 /* 96: */ 0, 27, 28, 29, 30, |
| 128 31, 32, 33, |
| 129 /* 104: */ 34, 35, 36, 37, 38, |
| 130 39, 40, 41, |
| 131 /* 112: */ 42, 43, 44, 45, 46, |
| 132 47, 48, 49, |
| 133 /* 120: */ 50, 51, 52, 0, 0, |
| 134 0, 0, 0, |
| 135 /* 128: */ 0, 0, 0, 0, 0, |
| 136 0, 0, 0 |
| 137 /* and rest are all zero as well */ |
122 }; | 138 }; |
123 | 139 |
124 #define B64_PAD»'=' | 140 #define B64_PAD '=' |
125 | |
126 | 141 |
127 /* | 142 /* |
128 * Reads 4; writes 3 (known, or expected, to have no trailing padding). | 143 * Reads 4; writes 3 (known, or expected, to have no trailing padding). |
129 * Returns bytes written; -1 on error (unexpected character). | 144 * Returns bytes written; -1 on error (unexpected character). |
130 */ | 145 */ |
131 static int | 146 static int pl_base64_decode_4to3(const unsigned char *in, unsigned char *out) { |
132 pl_base64_decode_4to3 (const unsigned char *in, unsigned char *out) | 147 int j; |
133 { | 148 PRUint32 num = 0; |
134 int j; | 149 unsigned char bits; |
135 PRUint32 num = 0; | 150 |
136 unsigned char bits; | 151 for (j = 0; j < 4; j++) { |
137 | 152 bits = base64_codetovaluep1[in[j]]; |
138 for (j = 0; j < 4; j++) { | 153 if (bits == 0) return -1; |
139 » bits = base64_codetovaluep1[in[j]]; | 154 num = (num << 6) | (bits - 1); |
140 » if (bits == 0) | 155 } |
141 » return -1; | 156 |
142 » num = (num << 6) | (bits - 1); | 157 out[0] = (unsigned char)(num >> 16); |
143 } | 158 out[1] = (unsigned char)((num >> 8) & 0xFF); |
144 | 159 out[2] = (unsigned char)(num & 0xFF); |
145 out[0] = (unsigned char) (num >> 16); | 160 |
146 out[1] = (unsigned char) ((num >> 8) & 0xFF); | 161 return 3; |
147 out[2] = (unsigned char) (num & 0xFF); | |
148 | |
149 return 3; | |
150 } | 162 } |
151 | 163 |
152 /* | 164 /* |
153 * Reads 3; writes 2 (caller already confirmed EOF or trailing padding). | 165 * Reads 3; writes 2 (caller already confirmed EOF or trailing padding). |
154 * Returns bytes written; -1 on error (unexpected character). | 166 * Returns bytes written; -1 on error (unexpected character). |
155 */ | 167 */ |
156 static int | 168 static int pl_base64_decode_3to2(const unsigned char *in, unsigned char *out) { |
157 pl_base64_decode_3to2 (const unsigned char *in, unsigned char *out) | 169 PRUint32 num = 0; |
158 { | 170 unsigned char bits1, bits2, bits3; |
159 PRUint32 num = 0; | 171 |
160 unsigned char bits1, bits2, bits3; | 172 bits1 = base64_codetovaluep1[in[0]]; |
161 | 173 bits2 = base64_codetovaluep1[in[1]]; |
162 bits1 = base64_codetovaluep1[in[0]]; | 174 bits3 = base64_codetovaluep1[in[2]]; |
163 bits2 = base64_codetovaluep1[in[1]]; | 175 |
164 bits3 = base64_codetovaluep1[in[2]]; | 176 if ((bits1 == 0) || (bits2 == 0) || (bits3 == 0)) return -1; |
165 | 177 |
166 if ((bits1 == 0) || (bits2 == 0) || (bits3 == 0)) | 178 num = ((PRUint32)(bits1 - 1)) << 10; |
167 » return -1; | 179 num |= ((PRUint32)(bits2 - 1)) << 4; |
168 | 180 num |= ((PRUint32)(bits3 - 1)) >> 2; |
169 num = ((PRUint32)(bits1 - 1)) << 10; | 181 |
170 num |= ((PRUint32)(bits2 - 1)) << 4; | 182 out[0] = (unsigned char)(num >> 8); |
171 num |= ((PRUint32)(bits3 - 1)) >> 2; | 183 out[1] = (unsigned char)(num & 0xFF); |
172 | 184 |
173 out[0] = (unsigned char) (num >> 8); | 185 return 2; |
174 out[1] = (unsigned char) (num & 0xFF); | |
175 | |
176 return 2; | |
177 } | 186 } |
178 | 187 |
179 /* | 188 /* |
180 * Reads 2; writes 1 (caller already confirmed EOF or trailing padding). | 189 * Reads 2; writes 1 (caller already confirmed EOF or trailing padding). |
181 * Returns bytes written; -1 on error (unexpected character). | 190 * Returns bytes written; -1 on error (unexpected character). |
182 */ | 191 */ |
183 static int | 192 static int pl_base64_decode_2to1(const unsigned char *in, unsigned char *out) { |
184 pl_base64_decode_2to1 (const unsigned char *in, unsigned char *out) | 193 PRUint32 num = 0; |
185 { | 194 unsigned char bits1, bits2; |
186 PRUint32 num = 0; | 195 |
187 unsigned char bits1, bits2; | 196 bits1 = base64_codetovaluep1[in[0]]; |
188 | 197 bits2 = base64_codetovaluep1[in[1]]; |
189 bits1 = base64_codetovaluep1[in[0]]; | 198 |
190 bits2 = base64_codetovaluep1[in[1]]; | 199 if ((bits1 == 0) || (bits2 == 0)) return -1; |
191 | 200 |
192 if ((bits1 == 0) || (bits2 == 0)) | 201 num = ((PRUint32)(bits1 - 1)) << 2; |
193 » return -1; | 202 num |= ((PRUint32)(bits2 - 1)) >> 4; |
194 | 203 |
195 num = ((PRUint32)(bits1 - 1)) << 2; | 204 out[0] = (unsigned char)num; |
196 num |= ((PRUint32)(bits2 - 1)) >> 4; | 205 |
197 | 206 return 1; |
198 out[0] = (unsigned char) num; | |
199 | |
200 return 1; | |
201 } | 207 } |
202 | 208 |
203 /* | 209 /* |
204 * Reads 4; writes 0-3. Returns bytes written or -1 on error. | 210 * Reads 4; writes 0-3. Returns bytes written or -1 on error. |
205 * (Writes less than 3 only at (presumed) EOF.) | 211 * (Writes less than 3 only at (presumed) EOF.) |
206 */ | 212 */ |
207 static int | 213 static int pl_base64_decode_token(const unsigned char *in, unsigned char *out) { |
208 pl_base64_decode_token (const unsigned char *in, unsigned char *out) | 214 if (in[3] != B64_PAD) return pl_base64_decode_4to3(in, out); |
209 { | 215 |
210 if (in[3] != B64_PAD) | 216 if (in[2] == B64_PAD) return pl_base64_decode_2to1(in, out); |
211 » return pl_base64_decode_4to3 (in, out); | 217 |
212 | 218 return pl_base64_decode_3to2(in, out); |
213 if (in[2] == B64_PAD) | 219 } |
214 » return pl_base64_decode_2to1 (in, out); | 220 |
215 | 221 static PRStatus pl_base64_decode_buffer(PLBase64Decoder *data, |
216 return pl_base64_decode_3to2 (in, out); | 222 const unsigned char *in, |
217 } | 223 PRUint32 length) { |
218 | 224 unsigned char *out = data->output_buffer; |
219 static PRStatus | 225 unsigned char *token = data->token; |
220 pl_base64_decode_buffer (PLBase64Decoder *data, const unsigned char *in, | 226 int i, n = 0; |
221 » » » PRUint32 length) | 227 |
222 { | 228 i = data->token_size; |
223 unsigned char *out = data->output_buffer; | 229 data->token_size = 0; |
224 unsigned char *token = data->token; | 230 |
225 int i, n = 0; | 231 while (length > 0) { |
226 | 232 while (i < 4 && length > 0) { |
227 i = data->token_size; | 233 /* |
228 data->token_size = 0; | 234 * XXX Note that the following simply ignores any unexpected |
229 | 235 * characters. This is exactly what the original code in |
230 while (length > 0) { | 236 * libmime did, and I am leaving it. We certainly want to skip |
231 » while (i < 4 && length > 0) { | 237 * over whitespace (we must); this does much more than that. |
232 » /* | 238 * I am not confident changing it, and I don't want to slow |
233 » * XXX Note that the following simply ignores any unexpected | 239 * the processing down doing more complicated checking, but |
234 » * characters. This is exactly what the original code in | 240 * someone else might have different ideas in the future. |
235 » * libmime did, and I am leaving it. We certainly want to skip | 241 */ |
236 » * over whitespace (we must); this does much more than that. | 242 if (base64_codetovaluep1[*in] > 0 || *in == B64_PAD) token[i++] = *in; |
237 » * I am not confident changing it, and I don't want to slow | 243 in++; |
238 » * the processing down doing more complicated checking, but | 244 length--; |
239 » * someone else might have different ideas in the future. | |
240 » */ | |
241 » if (base64_codetovaluep1[*in] > 0 || *in == B64_PAD) | |
242 » » token[i++] = *in; | |
243 » in++; | |
244 » length--; | |
245 » } | |
246 | |
247 » if (i < 4) { | |
248 » /* Didn't get enough for a complete token. */ | |
249 » data->token_size = i; | |
250 » break; | |
251 » } | |
252 » i = 0; | |
253 | |
254 » PR_ASSERT((out - data->output_buffer + 3) <= data->output_buflen); | |
255 | |
256 » /* | |
257 » * Assume we are not at the end; the following function only works | |
258 » * for an internal token (no trailing padding characters) but is | |
259 » * faster that way. If it hits an invalid character (padding) it | |
260 » * will return an error; we break out of the loop and try again | |
261 » * calling the routine that will handle a final token. | |
262 » * Note that we intentionally do it this way rather than explicitly | |
263 » * add a check for padding here (because that would just slow down | |
264 » * the normal case) nor do we rely on checking whether we have more | |
265 » * input to process (because that would also slow it down but also | |
266 » * because we want to allow trailing garbage, especially white space | |
267 » * and cannot tell that without read-ahead, also a slow proposition). | |
268 » * Whew. Understand? | |
269 » */ | |
270 » n = pl_base64_decode_4to3 (token, out); | |
271 » if (n < 0) | |
272 » break; | |
273 | |
274 » /* Advance "out" by the number of bytes just written to it. */ | |
275 » out += n; | |
276 » n = 0; | |
277 } | 245 } |
278 | 246 |
| 247 if (i < 4) { |
| 248 /* Didn't get enough for a complete token. */ |
| 249 data->token_size = i; |
| 250 break; |
| 251 } |
| 252 i = 0; |
| 253 |
| 254 PR_ASSERT((out - data->output_buffer + 3) <= data->output_buflen); |
| 255 |
279 /* | 256 /* |
280 * See big comment above, before call to pl_base64_decode_4to3. | 257 * Assume we are not at the end; the following function only works |
281 * Here we check if we error'd out of loop, and allow for the case | 258 * for an internal token (no trailing padding characters) but is |
282 * that we are processing the last interesting token. If the routine | 259 * faster that way. If it hits an invalid character (padding) it |
283 * which should handle padding characters also fails, then we just | 260 * will return an error; we break out of the loop and try again |
284 * have bad input and give up. | 261 * calling the routine that will handle a final token. |
| 262 * Note that we intentionally do it this way rather than explicitly |
| 263 * add a check for padding here (because that would just slow down |
| 264 * the normal case) nor do we rely on checking whether we have more |
| 265 * input to process (because that would also slow it down but also |
| 266 * because we want to allow trailing garbage, especially white space |
| 267 * and cannot tell that without read-ahead, also a slow proposition). |
| 268 * Whew. Understand? |
285 */ | 269 */ |
286 if (n < 0) { | 270 n = pl_base64_decode_4to3(token, out); |
287 » n = pl_base64_decode_token (token, out); | 271 if (n < 0) break; |
288 » if (n < 0) | 272 |
289 » return PR_FAILURE; | 273 /* Advance "out" by the number of bytes just written to it. */ |
290 | 274 out += n; |
291 » out += n; | 275 n = 0; |
292 } | 276 } |
293 | 277 |
294 /* | 278 /* |
295 * As explained above, we can get here with more input remaining, but | 279 * See big comment above, before call to pl_base64_decode_4to3. |
296 * it should be all characters we do not care about (i.e. would be | 280 * Here we check if we error'd out of loop, and allow for the case |
297 * ignored when transferring from "in" to "token" in loop above, | 281 * that we are processing the last interesting token. If the routine |
298 * except here we choose to ignore extraneous pad characters, too). | 282 * which should handle padding characters also fails, then we just |
299 * Swallow it, performing that check. If we find more characters that | 283 * have bad input and give up. |
300 * we would expect to decode, something is wrong. | 284 */ |
301 */ | 285 if (n < 0) { |
302 while (length > 0) { | 286 n = pl_base64_decode_token(token, out); |
303 » if (base64_codetovaluep1[*in] > 0) | 287 if (n < 0) return PR_FAILURE; |
304 » return PR_FAILURE; | 288 |
305 » in++; | 289 out += n; |
306 » length--; | 290 } |
307 } | 291 |
308 | 292 /* |
309 /* Record the length of decoded data we have left in output_buffer. */ | 293 * As explained above, we can get here with more input remaining, but |
310 data->output_length = (PRUint32) (out - data->output_buffer); | 294 * it should be all characters we do not care about (i.e. would be |
311 return PR_SUCCESS; | 295 * ignored when transferring from "in" to "token" in loop above, |
| 296 * except here we choose to ignore extraneous pad characters, too). |
| 297 * Swallow it, performing that check. If we find more characters that |
| 298 * we would expect to decode, something is wrong. |
| 299 */ |
| 300 while (length > 0) { |
| 301 if (base64_codetovaluep1[*in] > 0) return PR_FAILURE; |
| 302 in++; |
| 303 length--; |
| 304 } |
| 305 |
| 306 /* Record the length of decoded data we have left in output_buffer. */ |
| 307 data->output_length = (PRUint32)(out - data->output_buffer); |
| 308 return PR_SUCCESS; |
312 } | 309 } |
313 | 310 |
314 /* | 311 /* |
315 * Flush any remaining buffered characters. Given well-formed input, | 312 * Flush any remaining buffered characters. Given well-formed input, |
316 * this will have nothing to do. If the input was missing the padding | 313 * this will have nothing to do. If the input was missing the padding |
317 * characters at the end, though, there could be 1-3 characters left | 314 * characters at the end, though, there could be 1-3 characters left |
318 * behind -- we will tolerate that by adding the padding for them. | 315 * behind -- we will tolerate that by adding the padding for them. |
319 */ | 316 */ |
320 static PRStatus | 317 static PRStatus pl_base64_decode_flush(PLBase64Decoder *data) { |
321 pl_base64_decode_flush (PLBase64Decoder *data) | 318 int count; |
322 { | 319 |
323 int count; | 320 /* |
324 | 321 * If no remaining characters, or all are padding (also not well-formed |
325 /* | 322 * input, but again, be tolerant), then nothing more to do. (And, that |
326 * If no remaining characters, or all are padding (also not well-formed | 323 * is considered successful.) |
327 * input, but again, be tolerant), then nothing more to do. (And, that | 324 */ |
328 * is considered successful.) | 325 if (data->token_size == 0 || data->token[0] == B64_PAD) return PR_SUCCESS; |
329 */ | 326 |
330 if (data->token_size == 0 || data->token[0] == B64_PAD) | 327 /* |
331 » return PR_SUCCESS; | 328 * Assume we have all the interesting input except for some expected |
332 | 329 * padding characters. Add them and decode the resulting token. |
333 /* | 330 */ |
334 * Assume we have all the interesting input except for some expected | 331 while (data->token_size < 4) data->token[data->token_size++] = B64_PAD; |
335 * padding characters. Add them and decode the resulting token. | 332 |
336 */ | 333 data->token_size = 0; /* so a subsequent flush call is a no-op */ |
337 while (data->token_size < 4) | 334 |
338 » data->token[data->token_size++] = B64_PAD; | 335 count = pl_base64_decode_token(data->token, |
339 | 336 data->output_buffer + data->output_length); |
340 data->token_size = 0;» /* so a subsequent flush call is a no-op */ | 337 if (count < 0) return PR_FAILURE; |
341 | 338 |
342 count = pl_base64_decode_token (data->token, | 339 /* |
343 » » » » data->output_buffer + data->output_length); | 340 * If there is an output function, call it with this last bit of data. |
344 if (count < 0) | 341 * Otherwise we are doing all buffered output, and the decoded bytes |
345 » return PR_FAILURE; | 342 * are now there, we just need to reflect that in the length. |
346 | 343 */ |
347 /* | 344 if (data->output_fn != NULL) { |
348 * If there is an output function, call it with this last bit of data. | 345 PRInt32 output_result; |
349 * Otherwise we are doing all buffered output, and the decoded bytes | 346 |
350 * are now there, we just need to reflect that in the length. | 347 PR_ASSERT(data->output_length == 0); |
351 */ | 348 output_result = |
352 if (data->output_fn != NULL) { | 349 data->output_fn(data->output_arg, data->output_buffer, (PRInt32)count); |
353 » PRInt32 output_result; | 350 if (output_result < 0) return PR_FAILURE; |
354 | 351 } else { |
355 » PR_ASSERT(data->output_length == 0); | 352 data->output_length += count; |
356 » output_result = data->output_fn (data->output_arg, | 353 } |
357 » » » » » data->output_buffer, | 354 |
358 » » » » » (PRInt32) count); | 355 return PR_SUCCESS; |
359 » if (output_result < 0) | 356 } |
360 » return PR_FAILURE; | |
361 } else { | |
362 » data->output_length += count; | |
363 } | |
364 | |
365 return PR_SUCCESS; | |
366 } | |
367 | |
368 | 357 |
369 /* | 358 /* |
370 * The maximum space needed to hold the output of the decoder given | 359 * The maximum space needed to hold the output of the decoder given |
371 * input data of length "size". | 360 * input data of length "size". |
372 */ | 361 */ |
373 static PRUint32 | 362 static PRUint32 PL_Base64MaxDecodedLength(PRUint32 size) { |
374 PL_Base64MaxDecodedLength (PRUint32 size) | 363 return ((size * 3) / 4); |
375 { | 364 } |
376 return ((size * 3) / 4); | |
377 } | |
378 | |
379 | 365 |
380 /* | 366 /* |
381 * A distinct internal creation function for the buffer version to use. | 367 * A distinct internal creation function for the buffer version to use. |
382 * (It does not want to specify an output_fn, and we want the normal | 368 * (It does not want to specify an output_fn, and we want the normal |
383 * Create function to require that.) If more common initialization | 369 * Create function to require that.) If more common initialization |
384 * of the decoding context needs to be done, it should be done *here*. | 370 * of the decoding context needs to be done, it should be done *here*. |
385 */ | 371 */ |
386 static PLBase64Decoder * | 372 static PLBase64Decoder *pl_base64_create_decoder(void) { |
387 pl_base64_create_decoder (void) | 373 return PR_NEWZAP(PLBase64Decoder); |
388 { | |
389 return PR_NEWZAP(PLBase64Decoder); | |
390 } | 374 } |
391 | 375 |
392 /* | 376 /* |
393 * Function to start a base64 decoding context. | 377 * Function to start a base64 decoding context. |
394 * An "output_fn" is required; the "output_arg" parameter to that is optional. | 378 * An "output_fn" is required; the "output_arg" parameter to that is optional. |
395 */ | 379 */ |
396 static PLBase64Decoder * | 380 static PLBase64Decoder *PL_CreateBase64Decoder( |
397 PL_CreateBase64Decoder (PRInt32 (*output_fn) (void *, const unsigned char *, | 381 PRInt32 (*output_fn)(void *, const unsigned char *, PRInt32), |
398 » » » » » PRInt32), | 382 void *output_arg) { |
399 » » » void *output_arg) | 383 PLBase64Decoder *data; |
400 { | 384 |
401 PLBase64Decoder *data; | 385 if (output_fn == NULL) { |
402 | 386 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
403 if (output_fn == NULL) { | 387 return NULL; |
404 » PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0); | 388 } |
405 » return NULL; | 389 |
406 } | 390 data = pl_base64_create_decoder(); |
407 | 391 if (data != NULL) { |
408 data = pl_base64_create_decoder (); | 392 data->output_fn = output_fn; |
409 if (data != NULL) { | 393 data->output_arg = output_arg; |
410 » data->output_fn = output_fn; | 394 } |
411 » data->output_arg = output_arg; | 395 return data; |
412 } | 396 } |
413 return data; | |
414 } | |
415 | |
416 | 397 |
417 /* | 398 /* |
418 * Push data through the decoder, causing the output_fn (provided to Create) | 399 * Push data through the decoder, causing the output_fn (provided to Create) |
419 * to be called with the decoded data. | 400 * to be called with the decoded data. |
420 */ | 401 */ |
421 static PRStatus | 402 static PRStatus PL_UpdateBase64Decoder(PLBase64Decoder *data, |
422 PL_UpdateBase64Decoder (PLBase64Decoder *data, const char *buffer, | 403 const char *buffer, PRUint32 size) { |
423 » » » PRUint32 size) | 404 PRUint32 need_length; |
424 { | 405 PRStatus status; |
425 PRUint32 need_length; | 406 |
426 PRStatus status; | 407 /* XXX Should we do argument checking only in debug build? */ |
427 | 408 if (data == NULL || buffer == NULL || size == 0) { |
428 /* XXX Should we do argument checking only in debug build? */ | 409 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
429 if (data == NULL || buffer == NULL || size == 0) { | 410 return PR_FAILURE; |
430 » PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0); | 411 } |
431 » return PR_FAILURE; | 412 |
432 } | 413 /* |
433 | 414 * How much space could this update need for decoding? |
434 /* | 415 */ |
435 * How much space could this update need for decoding? | 416 need_length = PL_Base64MaxDecodedLength(size + data->token_size); |
436 */ | 417 |
437 need_length = PL_Base64MaxDecodedLength (size + data->token_size); | 418 /* |
438 | 419 * Make sure we have at least that much. If not, (re-)allocate. |
439 /* | 420 */ |
440 * Make sure we have at least that much. If not, (re-)allocate. | 421 if (need_length > data->output_buflen) { |
441 */ | 422 unsigned char *output_buffer = data->output_buffer; |
442 if (need_length > data->output_buflen) { | 423 |
443 » unsigned char *output_buffer = data->output_buffer; | 424 if (output_buffer != NULL) |
444 | 425 output_buffer = (unsigned char *)PR_Realloc(output_buffer, need_length); |
445 » if (output_buffer != NULL) | 426 else |
446 » output_buffer = (unsigned char *) PR_Realloc(output_buffer, | 427 output_buffer = (unsigned char *)PR_Malloc(need_length); |
447 » » » » » » » need_length); | 428 |
448 » else | 429 if (output_buffer == NULL) return PR_FAILURE; |
449 » output_buffer = (unsigned char *) PR_Malloc(need_length); | 430 |
450 | 431 data->output_buffer = output_buffer; |
451 » if (output_buffer == NULL) | 432 data->output_buflen = need_length; |
452 » return PR_FAILURE; | 433 } |
453 | 434 |
454 » data->output_buffer = output_buffer; | 435 /* There should not have been any leftover output data in the buffer. */ |
455 » data->output_buflen = need_length; | 436 PR_ASSERT(data->output_length == 0); |
456 } | 437 data->output_length = 0; |
457 | 438 |
458 /* There should not have been any leftover output data in the buffer. */ | 439 status = pl_base64_decode_buffer(data, (const unsigned char *)buffer, size); |
459 PR_ASSERT(data->output_length == 0); | 440 |
460 data->output_length = 0; | 441 /* Now that we have some decoded data, write it. */ |
461 | 442 if (status == PR_SUCCESS && data->output_length > 0) { |
462 status = pl_base64_decode_buffer (data, (const unsigned char *) buffer, | 443 PRInt32 output_result; |
463 » » » » size); | 444 |
464 | 445 PR_ASSERT(data->output_fn != NULL); |
465 /* Now that we have some decoded data, write it. */ | 446 output_result = data->output_fn(data->output_arg, data->output_buffer, |
466 if (status == PR_SUCCESS && data->output_length > 0) { | 447 (PRInt32)data->output_length); |
467 » PRInt32 output_result; | 448 if (output_result < 0) status = PR_FAILURE; |
468 | 449 } |
469 » PR_ASSERT(data->output_fn != NULL); | 450 |
470 » output_result = data->output_fn (data->output_arg, | 451 data->output_length = 0; |
471 » » » » » data->output_buffer, | 452 return status; |
472 » » » » » (PRInt32) data->output_length); | 453 } |
473 » if (output_result < 0) | |
474 » status = PR_FAILURE; | |
475 } | |
476 | |
477 data->output_length = 0; | |
478 return status; | |
479 } | |
480 | |
481 | 454 |
482 /* | 455 /* |
483 * When you're done decoding, call this to free the data. If "abort_p" | 456 * When you're done decoding, call this to free the data. If "abort_p" |
484 * is false, then calling this may cause the output_fn to be called | 457 * is false, then calling this may cause the output_fn to be called |
485 * one last time (as the last buffered data is flushed out). | 458 * one last time (as the last buffered data is flushed out). |
486 */ | 459 */ |
487 static PRStatus | 460 static PRStatus PL_DestroyBase64Decoder(PLBase64Decoder *data, PRBool abort_p) { |
488 PL_DestroyBase64Decoder (PLBase64Decoder *data, PRBool abort_p) | 461 PRStatus status = PR_SUCCESS; |
489 { | 462 |
490 PRStatus status = PR_SUCCESS; | 463 /* XXX Should we do argument checking only in debug build? */ |
491 | 464 if (data == NULL) { |
492 /* XXX Should we do argument checking only in debug build? */ | 465 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
493 if (data == NULL) { | 466 return PR_FAILURE; |
494 » PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0); | 467 } |
495 » return PR_FAILURE; | 468 |
496 } | 469 /* Flush out the last few buffered characters. */ |
497 | 470 if (!abort_p) status = pl_base64_decode_flush(data); |
498 /* Flush out the last few buffered characters. */ | 471 |
499 if (!abort_p) | 472 if (data->output_buffer != NULL) PR_Free(data->output_buffer); |
500 » status = pl_base64_decode_flush (data); | 473 PR_Free(data); |
501 | 474 |
502 if (data->output_buffer != NULL) | 475 return status; |
503 » PR_Free(data->output_buffer); | 476 } |
504 PR_Free(data); | |
505 | |
506 return status; | |
507 } | |
508 | |
509 | 477 |
510 /* | 478 /* |
511 * Perform base64 decoding from an input buffer to an output buffer. | 479 * Perform base64 decoding from an input buffer to an output buffer. |
512 * The output buffer can be provided (as "dest"); you can also pass in | 480 * The output buffer can be provided (as "dest"); you can also pass in |
513 * a NULL and this function will allocate a buffer large enough for you, | 481 * a NULL and this function will allocate a buffer large enough for you, |
514 * and return it. If you do provide the output buffer, you must also | 482 * and return it. If you do provide the output buffer, you must also |
515 * provide the maximum length of that buffer (as "maxdestlen"). | 483 * provide the maximum length of that buffer (as "maxdestlen"). |
516 * The actual decoded length of output will be returned to you in | 484 * The actual decoded length of output will be returned to you in |
517 * "output_destlen". | 485 * "output_destlen". |
518 * | 486 * |
519 * Return value is NULL on error, the output buffer (allocated or provided) | 487 * Return value is NULL on error, the output buffer (allocated or provided) |
520 * otherwise. | 488 * otherwise. |
521 */ | 489 */ |
522 static unsigned char * | 490 static unsigned char *PL_Base64DecodeBuffer(const char *src, PRUint32 srclen, |
523 PL_Base64DecodeBuffer (const char *src, PRUint32 srclen, unsigned char *dest, | 491 unsigned char *dest, |
524 » » PRUint32 maxdestlen, PRUint32 *output_destlen) | 492 PRUint32 maxdestlen, |
525 { | 493 PRUint32 *output_destlen) { |
526 PRUint32 need_length; | 494 PRUint32 need_length; |
527 unsigned char *output_buffer = NULL; | 495 unsigned char *output_buffer = NULL; |
528 PLBase64Decoder *data = NULL; | 496 PLBase64Decoder *data = NULL; |
529 PRStatus status; | 497 PRStatus status; |
530 | 498 |
531 PR_ASSERT(srclen > 0); | 499 PR_ASSERT(srclen > 0); |
532 if (srclen == 0) { | 500 if (srclen == 0) { |
533 » PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); | 501 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
534 » return NULL; | 502 return NULL; |
| 503 } |
| 504 |
| 505 /* |
| 506 * How much space could we possibly need for decoding this input? |
| 507 */ |
| 508 need_length = PL_Base64MaxDecodedLength(srclen); |
| 509 |
| 510 /* |
| 511 * Make sure we have at least that much, if output buffer provided. |
| 512 * If no output buffer provided, then we allocate that much. |
| 513 */ |
| 514 if (dest != NULL) { |
| 515 PR_ASSERT(maxdestlen >= need_length); |
| 516 if (maxdestlen < need_length) { |
| 517 PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0); |
| 518 goto loser; |
535 } | 519 } |
536 | 520 output_buffer = dest; |
537 /* | 521 } else { |
538 * How much space could we possibly need for decoding this input? | 522 output_buffer = (unsigned char *)PR_Malloc(need_length); |
539 */ | 523 if (output_buffer == NULL) goto loser; |
540 need_length = PL_Base64MaxDecodedLength (srclen); | 524 maxdestlen = need_length; |
541 | 525 } |
542 /* | 526 |
543 * Make sure we have at least that much, if output buffer provided. | 527 data = pl_base64_create_decoder(); |
544 * If no output buffer provided, then we allocate that much. | 528 if (data == NULL) goto loser; |
545 */ | 529 |
546 if (dest != NULL) { | 530 data->output_buflen = maxdestlen; |
547 » PR_ASSERT(maxdestlen >= need_length); | 531 data->output_buffer = output_buffer; |
548 » if (maxdestlen < need_length) { | 532 |
549 » PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0); | 533 status = pl_base64_decode_buffer(data, (const unsigned char *)src, srclen); |
550 » goto loser; | 534 |
551 » } | 535 /* |
552 » output_buffer = dest; | 536 * We do not wait for Destroy to flush, because Destroy will also |
553 } else { | 537 * get rid of our decoder context, which we need to look at first! |
554 » output_buffer = (unsigned char *) PR_Malloc(need_length); | 538 */ |
555 » if (output_buffer == NULL) | 539 if (status == PR_SUCCESS) status = pl_base64_decode_flush(data); |
556 » goto loser; | 540 |
557 » maxdestlen = need_length; | 541 /* Must clear this or Destroy will free it. */ |
558 } | 542 data->output_buffer = NULL; |
559 | 543 |
560 data = pl_base64_create_decoder(); | 544 if (status == PR_SUCCESS) { |
561 if (data == NULL) | 545 *output_destlen = data->output_length; |
562 » goto loser; | 546 status = PL_DestroyBase64Decoder(data, PR_FALSE); |
563 | 547 data = NULL; |
564 data->output_buflen = maxdestlen; | 548 if (status == PR_FAILURE) goto loser; |
565 data->output_buffer = output_buffer; | 549 return output_buffer; |
566 | 550 } |
567 status = pl_base64_decode_buffer (data, (const unsigned char *) src, | |
568 » » » » srclen); | |
569 | |
570 /* | |
571 * We do not wait for Destroy to flush, because Destroy will also | |
572 * get rid of our decoder context, which we need to look at first! | |
573 */ | |
574 if (status == PR_SUCCESS) | |
575 » status = pl_base64_decode_flush (data); | |
576 | |
577 /* Must clear this or Destroy will free it. */ | |
578 data->output_buffer = NULL; | |
579 | |
580 if (status == PR_SUCCESS) { | |
581 » *output_destlen = data->output_length; | |
582 » status = PL_DestroyBase64Decoder (data, PR_FALSE); | |
583 » data = NULL; | |
584 » if (status == PR_FAILURE) | |
585 » goto loser; | |
586 » return output_buffer; | |
587 } | |
588 | 551 |
589 loser: | 552 loser: |
590 if (dest == NULL && output_buffer != NULL) | 553 if (dest == NULL && output_buffer != NULL) PR_Free(output_buffer); |
591 » PR_Free(output_buffer); | 554 if (data != NULL) (void)PL_DestroyBase64Decoder(data, PR_TRUE); |
592 if (data != NULL) | 555 return NULL; |
593 » (void) PL_DestroyBase64Decoder (data, PR_TRUE); | 556 } |
594 return NULL; | |
595 } | |
596 | |
597 | 557 |
598 /* | 558 /* |
599 * XXX End of base64 decoding code to be moved into NSPR. | 559 * XXX End of base64 decoding code to be moved into NSPR. |
600 ******************************************************** | 560 ******************************************************** |
601 */ | 561 */ |
602 | 562 |
603 /* | 563 /* |
604 * This is the beginning of the NSS cover functions. These will | 564 * This is the beginning of the NSS cover functions. These will |
605 * provide the interface we want to expose as NSS-ish. For example, | 565 * provide the interface we want to expose as NSS-ish. For example, |
606 * they will operate on our Items, do any special handling or checking | 566 * they will operate on our Items, do any special handling or checking |
607 * we want to do, etc. | 567 * we want to do, etc. |
608 */ | 568 */ |
609 | 569 |
610 | |
611 PR_BEGIN_EXTERN_C | 570 PR_BEGIN_EXTERN_C |
612 | 571 |
613 /* | 572 /* |
614 * A boring cover structure for now. Perhaps someday it will include | 573 * A boring cover structure for now. Perhaps someday it will include |
615 * some more interesting fields. | 574 * some more interesting fields. |
616 */ | 575 */ |
617 struct NSSBase64DecoderStr { | 576 struct NSSBase64DecoderStr { |
618 PLBase64Decoder *pl_data; | 577 PLBase64Decoder *pl_data; |
619 }; | 578 }; |
620 | 579 |
621 PR_END_EXTERN_C | 580 PR_END_EXTERN_C |
622 | 581 |
623 | |
624 /* | 582 /* |
625 * Function to start a base64 decoding context. | 583 * Function to start a base64 decoding context. |
626 */ | 584 */ |
627 NSSBase64Decoder * | 585 NSSBase64Decoder *NSSBase64Decoder_Create( |
628 NSSBase64Decoder_Create (PRInt32 (*output_fn) (void *, const unsigned char *, | 586 PRInt32 (*output_fn)(void *, const unsigned char *, PRInt32), |
629 » » » » » PRInt32), | 587 void *output_arg) { |
630 » » » void *output_arg) | 588 PLBase64Decoder *pl_data; |
631 { | 589 NSSBase64Decoder *nss_data; |
632 PLBase64Decoder *pl_data; | 590 |
633 NSSBase64Decoder *nss_data; | 591 nss_data = PORT_ZNew(NSSBase64Decoder); |
634 | 592 if (nss_data == NULL) return NULL; |
635 nss_data = PORT_ZNew(NSSBase64Decoder); | 593 |
636 if (nss_data == NULL) | 594 pl_data = PL_CreateBase64Decoder(output_fn, output_arg); |
637 » return NULL; | 595 if (pl_data == NULL) { |
638 | 596 PORT_Free(nss_data); |
639 pl_data = PL_CreateBase64Decoder (output_fn, output_arg); | 597 return NULL; |
640 if (pl_data == NULL) { | 598 } |
641 » PORT_Free(nss_data); | 599 |
642 » return NULL; | 600 nss_data->pl_data = pl_data; |
643 } | 601 return nss_data; |
644 | 602 } |
645 nss_data->pl_data = pl_data; | |
646 return nss_data; | |
647 } | |
648 | |
649 | 603 |
650 /* | 604 /* |
651 * Push data through the decoder, causing the output_fn (provided to Create) | 605 * Push data through the decoder, causing the output_fn (provided to Create) |
652 * to be called with the decoded data. | 606 * to be called with the decoded data. |
653 */ | 607 */ |
654 SECStatus | 608 SECStatus NSSBase64Decoder_Update(NSSBase64Decoder *data, const char *buffer, |
655 NSSBase64Decoder_Update (NSSBase64Decoder *data, const char *buffer, | 609 PRUint32 size) { |
656 » » » PRUint32 size) | 610 PRStatus pr_status; |
657 { | 611 |
658 PRStatus pr_status; | 612 /* XXX Should we do argument checking only in debug build? */ |
659 | 613 if (data == NULL) { |
660 /* XXX Should we do argument checking only in debug build? */ | 614 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
661 if (data == NULL) { | 615 return SECFailure; |
662 » PORT_SetError (SEC_ERROR_INVALID_ARGS); | 616 } |
663 » return SECFailure; | 617 |
664 } | 618 pr_status = PL_UpdateBase64Decoder(data->pl_data, buffer, size); |
665 | 619 if (pr_status == PR_FAILURE) return SECFailure; |
666 pr_status = PL_UpdateBase64Decoder (data->pl_data, buffer, size); | 620 |
667 if (pr_status == PR_FAILURE) | 621 return SECSuccess; |
668 » return SECFailure; | 622 } |
669 | |
670 return SECSuccess; | |
671 } | |
672 | |
673 | 623 |
674 /* | 624 /* |
675 * When you're done decoding, call this to free the data. If "abort_p" | 625 * When you're done decoding, call this to free the data. If "abort_p" |
676 * is false, then calling this may cause the output_fn to be called | 626 * is false, then calling this may cause the output_fn to be called |
677 * one last time (as the last buffered data is flushed out). | 627 * one last time (as the last buffered data is flushed out). |
678 */ | 628 */ |
679 SECStatus | 629 SECStatus NSSBase64Decoder_Destroy(NSSBase64Decoder *data, PRBool abort_p) { |
680 NSSBase64Decoder_Destroy (NSSBase64Decoder *data, PRBool abort_p) | 630 PRStatus pr_status; |
681 { | 631 |
682 PRStatus pr_status; | 632 /* XXX Should we do argument checking only in debug build? */ |
683 | 633 if (data == NULL) { |
684 /* XXX Should we do argument checking only in debug build? */ | 634 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
685 if (data == NULL) { | 635 return SECFailure; |
686 » PORT_SetError (SEC_ERROR_INVALID_ARGS); | 636 } |
687 » return SECFailure; | 637 |
688 } | 638 pr_status = PL_DestroyBase64Decoder(data->pl_data, abort_p); |
689 | 639 |
690 pr_status = PL_DestroyBase64Decoder (data->pl_data, abort_p); | 640 PORT_Free(data); |
691 | 641 |
692 PORT_Free(data); | 642 if (pr_status == PR_FAILURE) return SECFailure; |
693 | 643 |
694 if (pr_status == PR_FAILURE) | 644 return SECSuccess; |
695 » return SECFailure; | 645 } |
696 | |
697 return SECSuccess; | |
698 } | |
699 | |
700 | 646 |
701 /* | 647 /* |
702 * Perform base64 decoding from an ascii string "inStr" to an Item. | 648 * Perform base64 decoding from an ascii string "inStr" to an Item. |
703 * The length of the input must be provided as "inLen". The Item | 649 * The length of the input must be provided as "inLen". The Item |
704 * may be provided (as "outItemOpt"); you can also pass in a NULL | 650 * may be provided (as "outItemOpt"); you can also pass in a NULL |
705 * and the Item will be allocated for you. | 651 * and the Item will be allocated for you. |
706 * | 652 * |
707 * In any case, the data within the Item will be allocated for you. | 653 * In any case, the data within the Item will be allocated for you. |
708 * All allocation will happen out of the passed-in "arenaOpt", if non-NULL. | 654 * All allocation will happen out of the passed-in "arenaOpt", if non-NULL. |
709 * If "arenaOpt" is NULL, standard allocation (heap) will be used and | 655 * If "arenaOpt" is NULL, standard allocation (heap) will be used and |
710 * you will want to free the result via SECITEM_FreeItem. | 656 * you will want to free the result via SECITEM_FreeItem. |
711 * | 657 * |
712 * Return value is NULL on error, the Item (allocated or provided) otherwise. | 658 * Return value is NULL on error, the Item (allocated or provided) otherwise. |
713 */ | 659 */ |
714 SECItem * | 660 SECItem *NSSBase64_DecodeBuffer(PLArenaPool *arenaOpt, SECItem *outItemOpt, |
715 NSSBase64_DecodeBuffer (PLArenaPool *arenaOpt, SECItem *outItemOpt, | 661 const char *inStr, unsigned int inLen) { |
716 » » » const char *inStr, unsigned int inLen) | 662 SECItem *out_item = NULL; |
717 { | 663 PRUint32 max_out_len = 0; |
718 SECItem *out_item = NULL; | 664 PRUint32 out_len; |
719 PRUint32 max_out_len = 0; | 665 void *mark = NULL; |
720 PRUint32 out_len; | 666 unsigned char *dummy; |
721 void *mark = NULL; | 667 |
722 unsigned char *dummy; | 668 if ((outItemOpt != NULL && outItemOpt->data != NULL) || inLen == 0) { |
723 | 669 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
724 if ((outItemOpt != NULL && outItemOpt->data != NULL) || inLen == 0) { | 670 return NULL; |
725 » PORT_SetError (SEC_ERROR_INVALID_ARGS); | 671 } |
726 » return NULL; | 672 |
| 673 if (arenaOpt != NULL) mark = PORT_ArenaMark(arenaOpt); |
| 674 |
| 675 max_out_len = PL_Base64MaxDecodedLength(inLen); |
| 676 out_item = SECITEM_AllocItem(arenaOpt, outItemOpt, max_out_len); |
| 677 if (out_item == NULL) { |
| 678 if (arenaOpt != NULL) PORT_ArenaRelease(arenaOpt, mark); |
| 679 return NULL; |
| 680 } |
| 681 |
| 682 dummy = PL_Base64DecodeBuffer(inStr, inLen, out_item->data, max_out_len, |
| 683 &out_len); |
| 684 if (dummy == NULL) { |
| 685 if (arenaOpt != NULL) { |
| 686 PORT_ArenaRelease(arenaOpt, mark); |
| 687 if (outItemOpt != NULL) { |
| 688 outItemOpt->data = NULL; |
| 689 outItemOpt->len = 0; |
| 690 } |
| 691 } else { |
| 692 SECITEM_FreeItem(out_item, (outItemOpt == NULL) ? PR_TRUE : PR_FALSE); |
727 } | 693 } |
728 | 694 return NULL; |
729 if (arenaOpt != NULL) | 695 } |
730 » mark = PORT_ArenaMark (arenaOpt); | 696 |
731 | 697 if (arenaOpt != NULL) PORT_ArenaUnmark(arenaOpt, mark); |
732 max_out_len = PL_Base64MaxDecodedLength (inLen); | 698 out_item->len = out_len; |
733 out_item = SECITEM_AllocItem (arenaOpt, outItemOpt, max_out_len); | 699 return out_item; |
734 if (out_item == NULL) { | 700 } |
735 » if (arenaOpt != NULL) | |
736 » PORT_ArenaRelease (arenaOpt, mark); | |
737 » return NULL; | |
738 } | |
739 | |
740 dummy = PL_Base64DecodeBuffer (inStr, inLen, out_item->data, | |
741 » » » » max_out_len, &out_len); | |
742 if (dummy == NULL) { | |
743 » if (arenaOpt != NULL) { | |
744 » PORT_ArenaRelease (arenaOpt, mark); | |
745 » if (outItemOpt != NULL) { | |
746 » » outItemOpt->data = NULL; | |
747 » » outItemOpt->len = 0; | |
748 » } | |
749 » } else { | |
750 » SECITEM_FreeItem (out_item, | |
751 » » » (outItemOpt == NULL) ? PR_TRUE : PR_FALSE); | |
752 » } | |
753 » return NULL; | |
754 } | |
755 | |
756 if (arenaOpt != NULL) | |
757 » PORT_ArenaUnmark (arenaOpt, mark); | |
758 out_item->len = out_len; | |
759 return out_item; | |
760 } | |
761 | |
762 | 701 |
763 /* | 702 /* |
764 * XXX Everything below is deprecated. If you add new stuff, put it | 703 * XXX Everything below is deprecated. If you add new stuff, put it |
765 * *above*, not below. | 704 * *above*, not below. |
766 */ | 705 */ |
767 | 706 |
768 /* | 707 /* |
769 * XXX The following "ATOB" functions are provided for backward compatibility | 708 * XXX The following "ATOB" functions are provided for backward compatibility |
770 * with current code. They should be considered strongly deprecated. | 709 * with current code. They should be considered strongly deprecated. |
771 * When we can convert all our code over to using the new NSSBase64Decoder_ | 710 * When we can convert all our code over to using the new NSSBase64Decoder_ |
772 * functions defined above, we should get rid of these altogether. (Remove | 711 * functions defined above, we should get rid of these altogether. (Remove |
773 * protoypes from base64.h as well -- actually, remove that file completely). | 712 * protoypes from base64.h as well -- actually, remove that file completely). |
774 * If someone thinks either of these functions provides such a very useful | 713 * If someone thinks either of these functions provides such a very useful |
775 * interface (though, as shown, the same functionality can already be | 714 * interface (though, as shown, the same functionality can already be |
776 * obtained by calling NSSBase64_DecodeBuffer directly), fine -- but then | 715 * obtained by calling NSSBase64_DecodeBuffer directly), fine -- but then |
777 * that API should be provided with a nice new NSSFoo name and using | 716 * that API should be provided with a nice new NSSFoo name and using |
778 * appropriate types, etc. | 717 * appropriate types, etc. |
779 */ | 718 */ |
780 | 719 |
781 #include "base64.h" | 720 #include "base64.h" |
782 | 721 |
783 /* | 722 /* |
784 ** Return an PORT_Alloc'd string which is the base64 decoded version | 723 ** Return an PORT_Alloc'd string which is the base64 decoded version |
785 ** of the input string; set *lenp to the length of the returned data. | 724 ** of the input string; set *lenp to the length of the returned data. |
786 */ | 725 */ |
787 unsigned char * | 726 unsigned char *ATOB_AsciiToData(const char *string, unsigned int *lenp) { |
788 ATOB_AsciiToData(const char *string, unsigned int *lenp) | 727 SECItem binary_item, *dummy; |
789 { | |
790 SECItem binary_item, *dummy; | |
791 | 728 |
792 binary_item.data = NULL; | 729 binary_item.data = NULL; |
793 binary_item.len = 0; | 730 binary_item.len = 0; |
794 | 731 |
795 dummy = NSSBase64_DecodeBuffer (NULL, &binary_item, string, | 732 dummy = NSSBase64_DecodeBuffer(NULL, &binary_item, string, |
796 » » » » (PRUint32) PORT_Strlen(string)); | 733 (PRUint32)PORT_Strlen(string)); |
797 if (dummy == NULL) | 734 if (dummy == NULL) return NULL; |
798 » return NULL; | |
799 | 735 |
800 PORT_Assert(dummy == &binary_item); | 736 PORT_Assert(dummy == &binary_item); |
801 | 737 |
802 *lenp = dummy->len; | 738 *lenp = dummy->len; |
803 return dummy->data; | 739 return dummy->data; |
804 } | 740 } |
805 | 741 |
806 /* | 742 /* |
807 ** Convert from ascii to binary encoding of an item. | 743 ** Convert from ascii to binary encoding of an item. |
808 */ | 744 */ |
809 SECStatus | 745 SECStatus ATOB_ConvertAsciiToItem(SECItem *binary_item, const char *ascii) { |
810 ATOB_ConvertAsciiToItem(SECItem *binary_item, const char *ascii) | 746 SECItem *dummy; |
811 { | |
812 SECItem *dummy; | |
813 | 747 |
814 if (binary_item == NULL) { | 748 if (binary_item == NULL) { |
815 » PORT_SetError (SEC_ERROR_INVALID_ARGS); | 749 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
816 » return SECFailure; | 750 return SECFailure; |
817 } | 751 } |
818 | 752 |
819 /* | 753 /* |
820 * XXX Would prefer to assert here if data is non-null (actually, | 754 * XXX Would prefer to assert here if data is non-null (actually, |
821 * don't need to, just let NSSBase64_DecodeBuffer do it), so as to | 755 * don't need to, just let NSSBase64_DecodeBuffer do it), so as to |
822 * to catch unintended memory leaks, but callers are not clean in | 756 * to catch unintended memory leaks, but callers are not clean in |
823 * this respect so we need to explicitly clear here to avoid the | 757 * this respect so we need to explicitly clear here to avoid the |
824 * assert in NSSBase64_DecodeBuffer. | 758 * assert in NSSBase64_DecodeBuffer. |
825 */ | 759 */ |
826 binary_item->data = NULL; | 760 binary_item->data = NULL; |
827 binary_item->len = 0; | 761 binary_item->len = 0; |
828 | 762 |
829 dummy = NSSBase64_DecodeBuffer (NULL, binary_item, ascii, | 763 dummy = NSSBase64_DecodeBuffer(NULL, binary_item, ascii, |
830 » » » » (PRUint32) PORT_Strlen(ascii)); | 764 (PRUint32)PORT_Strlen(ascii)); |
831 | 765 |
832 if (dummy == NULL) | 766 if (dummy == NULL) return SECFailure; |
833 » return SECFailure; | |
834 | 767 |
835 return SECSuccess; | 768 return SECSuccess; |
836 } | 769 } |
OLD | NEW |