VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/base64.cpp@ 51788

Last change on this file since 51788 was 51788, checked in by vboxsync, 11 years ago

More correct fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.2 KB
Line 
1/* $Id: base64.cpp 51788 2014-07-01 20:27:17Z vboxsync $ */
2/** @file
3 * IPRT - Base64, MIME content transfer encoding.
4 */
5
6/*
7 * Copyright (C) 2009-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/base64.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/err.h>
36#include <iprt/ctype.h>
37#include <iprt/string.h>
38#ifdef RT_STRICT
39# include <iprt/asm.h>
40#endif
41
42
43/*******************************************************************************
44* Defined Constants And Macros *
45*******************************************************************************/
46/** The line length used for encoding. */
47#define RTBASE64_LINE_LEN 64
48
49/** @name Special g_au8CharToVal values
50 * @{ */
51#define BASE64_SPACE 0xc0
52#define BASE64_PAD 0xe0
53#define BASE64_INVALID 0xff
54/** @} */
55
56
57/*******************************************************************************
58* Global Variables *
59*******************************************************************************/
60/** Base64 character to value. (RFC 2045)
61 * ASSUMES ASCII / UTF-8. */
62static const uint8_t g_au8CharToVal[256] =
63{
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xff, 0xff, /* 0x00..0x0f */
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x10..0x1f */
66 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 62, 0xff, 0xff, 0xff, 63, /* 0x20..0x2f */
67 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, /* 0x30..0x3f */
68 0xff, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40..0x4f */
69 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50..0x5f */
70 0xff, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60..0x6f */
71 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x70..0x7f */
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x80..0x8f */
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x90..0x9f */
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0xa0..0xaf */
75 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0xb0..0xbf */
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0xc0..0xcf */
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0xd0..0xdf */
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0xe0..0xef */
79 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* 0xf0..0xff */
80};
81
82/** Value to Base64 character. (RFC 2045) */
83static const char g_szValToChar[64+1] =
84 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
85
86
87#ifdef RT_STRICT
88/**
89 * Perform table sanity checks on the first call.
90 */
91static void rtBase64Sanity(void)
92{
93 static bool s_fSane = false;
94 if (RT_UNLIKELY(!s_fSane))
95 {
96 for (unsigned i = 0; i < 64; i++)
97 {
98 unsigned ch = g_szValToChar[i];
99 Assert(ch);
100 Assert(g_au8CharToVal[ch] == i);
101 }
102
103 for (unsigned i = 0; i < 256; i++)
104 {
105 uint8_t u8 = g_au8CharToVal[i];
106 Assert( ( u8 == BASE64_INVALID
107 && !RT_C_IS_ALNUM(i)
108 && !RT_C_IS_SPACE(i))
109 || ( u8 == BASE64_PAD
110 && i == '=')
111 || ( u8 == BASE64_SPACE
112 && ( RT_C_IS_SPACE(i)
113 || (i == '\r'))) /* Carriage return is handled as a space character as well. */
114 || ( u8 < 64
115 && (unsigned)g_szValToChar[u8] == i));
116 }
117 ASMAtomicWriteBool(&s_fSane, true);
118 }
119}
120#endif /* RT_STRICT */
121
122
123RTDECL(ssize_t) RTBase64DecodedSizeEx(const char *pszString, size_t cchStringMax, char **ppszEnd)
124{
125#ifdef RT_STRICT
126 rtBase64Sanity();
127#endif
128
129 /*
130 * Walk the string until a non-encoded or non-space character is encountered.
131 */
132 uint32_t c6Bits = 0;
133 uint8_t u8 = BASE64_INVALID;
134 unsigned ch;
135 AssertCompile(sizeof(char) == sizeof(uint8_t));
136
137 while (cchStringMax > 0 && (ch = *pszString))
138 {
139 u8 = g_au8CharToVal[ch];
140 if (u8 < 64)
141 c6Bits++;
142 else if (RT_UNLIKELY(u8 != BASE64_SPACE))
143 break;
144
145 /* advance */
146 pszString++;
147 cchStringMax--;
148 }
149
150 /*
151 * Padding can only be found at the end and there is
152 * only 1 or 2 padding chars. Deal with it first.
153 */
154 unsigned cbPad = 0;
155 if (u8 == BASE64_PAD)
156 {
157 cbPad = 1;
158 c6Bits++;
159 pszString++;
160 cchStringMax--;
161 while (cchStringMax > 0 && (ch = *pszString))
162 {
163 u8 = g_au8CharToVal[ch];
164 if (u8 != BASE64_SPACE)
165 {
166 if (u8 != BASE64_PAD)
167 break;
168 c6Bits++;
169 cbPad++;
170 }
171 pszString++;
172 cchStringMax--;
173 }
174 if (cbPad >= 3)
175 return -1;
176 }
177
178 /*
179 * Invalid char and no where to indicate where the
180 * Base64 text ends? Return failure.
181 */
182 if ( u8 == BASE64_INVALID
183 && !ppszEnd
184 && ch)
185 return -1;
186
187 /*
188 * Recalc 6-bit to 8-bit and adjust for padding.
189 */
190 size_t cb;
191 if (c6Bits * 3 / 3 == c6Bits)
192 {
193 if ((c6Bits * 3 % 4) != 0)
194 return -1;
195 cb = c6Bits * 3 / 4;
196 }
197 else
198 {
199 if ((c6Bits * (uint64_t)3 % 4) != 0)
200 return -1;
201 cb = c6Bits * (uint64_t)3 / 4;
202 }
203
204 if (cb < cbPad)
205 return -1;
206 cb -= cbPad;
207
208 if (ppszEnd)
209 *ppszEnd = (char *)pszString;
210 return cb;
211}
212RT_EXPORT_SYMBOL(RTBase64DecodedSizeEx);
213
214
215RTDECL(ssize_t) RTBase64DecodedSize(const char *pszString, char **ppszEnd)
216{
217 return RTBase64DecodedSizeEx(pszString, RTSTR_MAX, ppszEnd);
218}
219RT_EXPORT_SYMBOL(RTBase64DecodedSize);
220
221
222RTDECL(int) RTBase64DecodeEx(const char *pszString, size_t cchStringMax, void *pvData, size_t cbData,
223 size_t *pcbActual, char **ppszEnd)
224{
225#ifdef RT_STRICT
226 rtBase64Sanity();
227#endif
228
229 /*
230 * Process input in groups of 4 input / 3 output chars.
231 */
232 uint8_t u8Trio[3] = { 0, 0, 0 }; /* shuts up gcc */
233 uint8_t *pbData = (uint8_t *)pvData;
234 uint8_t u8;
235 unsigned c6Bits = 0;
236 AssertCompile(sizeof(char) == sizeof(uint8_t));
237
238 for (;;)
239 {
240 /* The first 6-bit group. */
241 while ((u8 = cchStringMax > 0 ? g_au8CharToVal[*pszString] : BASE64_INVALID) == BASE64_SPACE)
242 pszString++, cchStringMax--;
243 if (u8 >= 64)
244 {
245 c6Bits = 0;
246 break;
247 }
248 u8Trio[0] = u8 << 2;
249 pszString++;
250 cchStringMax--;
251
252 /* The second 6-bit group. */
253 while ((u8 = cchStringMax > 0 ? g_au8CharToVal[*pszString] : BASE64_INVALID) == BASE64_SPACE)
254 pszString++, cchStringMax--;
255 if (u8 >= 64)
256 {
257 c6Bits = 1;
258 break;
259 }
260 u8Trio[0] |= u8 >> 4;
261 u8Trio[1] = u8 << 4;
262 pszString++;
263 cchStringMax--;
264
265 /* The third 6-bit group. */
266 u8 = BASE64_INVALID;
267 while ((u8 = cchStringMax > 0 ? g_au8CharToVal[*pszString] : BASE64_INVALID) == BASE64_SPACE)
268 pszString++, cchStringMax--;
269 if (u8 >= 64)
270 {
271 c6Bits = 2;
272 break;
273 }
274 u8Trio[1] |= u8 >> 2;
275 u8Trio[2] = u8 << 6;
276 pszString++;
277 cchStringMax--;
278
279 /* The fourth 6-bit group. */
280 u8 = BASE64_INVALID;
281 while ((u8 = cchStringMax > 0 ? g_au8CharToVal[*pszString] : BASE64_INVALID) == BASE64_SPACE)
282 pszString++, cchStringMax--;
283 if (u8 >= 64)
284 {
285 c6Bits = 3;
286 break;
287 }
288 u8Trio[2] |= u8;
289 pszString++;
290 cchStringMax--;
291
292 /* flush the trio */
293 if (cbData < 3)
294 return VERR_BUFFER_OVERFLOW;
295 cbData -= 3;
296 pbData[0] = u8Trio[0];
297 pbData[1] = u8Trio[1];
298 pbData[2] = u8Trio[2];
299 pbData += 3;
300 }
301
302 /*
303 * Padding can only be found at the end and there is
304 * only 1 or 2 padding chars. Deal with it first.
305 */
306 unsigned cbPad = 0;
307 unsigned ch = 0;
308 if (u8 == BASE64_PAD)
309 {
310 cbPad = 1;
311 pszString++;
312 cchStringMax--;
313 while (cchStringMax > 0 && (ch = *pszString))
314 {
315 u8 = g_au8CharToVal[ch];
316 if (u8 != BASE64_SPACE)
317 {
318 if (u8 != BASE64_PAD)
319 break;
320 cbPad++;
321 }
322 pszString++;
323 cchStringMax--;
324 }
325 if (cbPad >= 3)
326 return VERR_INVALID_BASE64_ENCODING;
327 }
328 else if (cchStringMax > 0)
329 ch = *pszString;
330
331 /*
332 * Invalid char and no where to indicate where the
333 * Base64 text ends? Return failure.
334 */
335 if ( u8 == BASE64_INVALID
336 && !ppszEnd
337 && ch != '\0')
338 return VERR_INVALID_BASE64_ENCODING;
339
340 /*
341 * Check padding vs. pending sextets, if anything left to do finish it off.
342 */
343 if (c6Bits || cbPad)
344 {
345 if (c6Bits + cbPad != 4)
346 return VERR_INVALID_BASE64_ENCODING;
347
348 switch (c6Bits)
349 {
350 case 1:
351 u8Trio[1] = u8Trio[2] = 0;
352 break;
353 case 2:
354 u8Trio[2] = 0;
355 break;
356 case 3:
357 default:
358 break;
359 }
360 switch (3 - cbPad)
361 {
362 case 1:
363 if (cbData < 1)
364 return VERR_BUFFER_OVERFLOW;
365 cbData--;
366 pbData[0] = u8Trio[0];
367 pbData++;
368 break;
369
370 case 2:
371 if (cbData < 2)
372 return VERR_BUFFER_OVERFLOW;
373 cbData -= 2;
374 pbData[0] = u8Trio[0];
375 pbData[1] = u8Trio[1];
376 pbData += 2;
377 break;
378
379 default:
380 break;
381 }
382 }
383
384 /*
385 * Set optional return values and return successfully.
386 */
387 if (ppszEnd)
388 *ppszEnd = (char *)pszString;
389 if (pcbActual)
390 *pcbActual = pbData - (uint8_t *)pvData;
391 return VINF_SUCCESS;
392}
393RT_EXPORT_SYMBOL(RTBase64DecodeEx);
394
395
396RTDECL(int) RTBase64Decode(const char *pszString, void *pvData, size_t cbData, size_t *pcbActual, char **ppszEnd)
397{
398 return RTBase64DecodeEx(pszString, RTSTR_MAX, pvData, cbData, pcbActual, ppszEnd);
399}
400RT_EXPORT_SYMBOL(RTBase64Decode);
401
402
403/**
404 * Calculates the length of the Base64 encoding of a given number of bytes of
405 * data.
406 *
407 * This will assume line breaks every 64 chars. A RTBase64EncodedLengthEx
408 * function can be added if closer control over the output is found to be
409 * required.
410 *
411 * @returns The Base64 string length.
412 * @param cbData The number of bytes to encode.
413 */
414RTDECL(size_t) RTBase64EncodedLength(size_t cbData)
415{
416 if (cbData * 8 / 8 != cbData)
417 {
418 AssertReturn(sizeof(size_t) == sizeof(uint64_t), ~(size_t)0);
419 uint64_t cch = cbData * (uint64_t)8;
420 while (cch % 24)
421 cch += 8;
422 cch /= 6;
423
424 cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
425 return cch;
426 }
427
428 size_t cch = cbData * 8;
429 while (cch % 24)
430 cch += 8;
431 cch /= 6;
432
433 cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
434 return cch;
435}
436RT_EXPORT_SYMBOL(RTBase64EncodedLength);
437
438
439/**
440 * Encodes the specifed data into a Base64 string, the caller supplies the
441 * output buffer.
442 *
443 * This will make the same assumptions about line breaks and EOL size as
444 * RTBase64EncodedLength() does. A RTBase64EncodeEx function can be added if
445 * more strict control over the output formatting is found necessary.
446 *
447 * @returns IRPT status code.
448 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
449 * may contain an invalid Base64 string.
450 *
451 * @param pvData The data to encode.
452 * @param cbData The number of bytes to encode.
453 * @param pszBuf Where to put the Base64 string.
454 * @param cbBuf The size of the output buffer, including the terminator.
455 * @param pcchActual The actual number of characters returned.
456 */
457RTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual)
458{
459 /*
460 * Process whole "trios" of input data.
461 */
462 uint8_t u8A;
463 uint8_t u8B;
464 uint8_t u8C;
465 size_t cbLineFeed = cbBuf - RTBASE64_LINE_LEN;
466 const uint8_t *pbSrc = (const uint8_t *)pvData;
467 char *pchDst = pszBuf;
468 while (cbData >= 3)
469 {
470 if (cbBuf < 4 + 1)
471 return VERR_BUFFER_OVERFLOW;
472
473 /* encode */
474 u8A = pbSrc[0];
475 pchDst[0] = g_szValToChar[u8A >> 2];
476 u8B = pbSrc[1];
477 pchDst[1] = g_szValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)];
478 u8C = pbSrc[2];
479 pchDst[2] = g_szValToChar[((u8B << 2) & 0x3f) | (u8C >> 6)];
480 pchDst[3] = g_szValToChar[u8C & 0x3f];
481
482 /* advance */
483 cbBuf -= 4;
484 pchDst += 4;
485 cbData -= 3;
486 pbSrc += 3;
487
488 /* deal out linefeeds */
489 if (cbBuf == cbLineFeed && cbData)
490 {
491 if (cbBuf < RTBASE64_EOL_SIZE + 1)
492 return VERR_BUFFER_OVERFLOW;
493 cbBuf -= RTBASE64_EOL_SIZE;
494 if (RTBASE64_EOL_SIZE == 2)
495 *pchDst++ = '\r';
496 *pchDst++ = '\n';
497 cbLineFeed = cbBuf - RTBASE64_LINE_LEN;
498 }
499 }
500
501 /*
502 * Deal with the odd bytes and string termination.
503 */
504 if (cbData)
505 {
506 if (cbBuf < 4 + 1)
507 return VERR_BUFFER_OVERFLOW;
508 switch (cbData)
509 {
510 case 1:
511 u8A = pbSrc[0];
512 pchDst[0] = g_szValToChar[u8A >> 2];
513 pchDst[1] = g_szValToChar[(u8A << 4) & 0x3f];
514 pchDst[2] = '=';
515 pchDst[3] = '=';
516 break;
517 case 2:
518 u8A = pbSrc[0];
519 pchDst[0] = g_szValToChar[u8A >> 2];
520 u8B = pbSrc[1];
521 pchDst[1] = g_szValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)];
522 pchDst[2] = g_szValToChar[(u8B << 2) & 0x3f];
523 pchDst[3] = '=';
524 break;
525 }
526 pchDst += 4;
527 }
528
529 *pchDst = '\0';
530
531 if (pcchActual)
532 *pcchActual = pchDst - pszBuf;
533 return VINF_SUCCESS;
534}
535RT_EXPORT_SYMBOL(RTBase64Encode);
536
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette