VirtualBox

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

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

bugfix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 KB
Line 
1/* $Id: base64.cpp 51787 2014-07-01 20:18:21Z 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 if (u8 == BASE64_PAD)
308 {
309 unsigned ch;
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
329 /*
330 * Invalid char and no where to indicate where the
331 * Base64 text ends? Return failure.
332 */
333 if ( u8 == BASE64_INVALID
334 && !ppszEnd)
335 return VERR_INVALID_BASE64_ENCODING;
336
337 /*
338 * Check padding vs. pending sextets, if anything left to do finish it off.
339 */
340 if (c6Bits || cbPad)
341 {
342 if (c6Bits + cbPad != 4)
343 return VERR_INVALID_BASE64_ENCODING;
344
345 switch (c6Bits)
346 {
347 case 1:
348 u8Trio[1] = u8Trio[2] = 0;
349 break;
350 case 2:
351 u8Trio[2] = 0;
352 break;
353 case 3:
354 default:
355 break;
356 }
357 switch (3 - cbPad)
358 {
359 case 1:
360 if (cbData < 1)
361 return VERR_BUFFER_OVERFLOW;
362 cbData--;
363 pbData[0] = u8Trio[0];
364 pbData++;
365 break;
366
367 case 2:
368 if (cbData < 2)
369 return VERR_BUFFER_OVERFLOW;
370 cbData -= 2;
371 pbData[0] = u8Trio[0];
372 pbData[1] = u8Trio[1];
373 pbData += 2;
374 break;
375
376 default:
377 break;
378 }
379 }
380
381 /*
382 * Set optional return values and return successfully.
383 */
384 if (ppszEnd)
385 *ppszEnd = (char *)pszString;
386 if (pcbActual)
387 *pcbActual = pbData - (uint8_t *)pvData;
388 return VINF_SUCCESS;
389}
390RT_EXPORT_SYMBOL(RTBase64DecodeEx);
391
392
393RTDECL(int) RTBase64Decode(const char *pszString, void *pvData, size_t cbData, size_t *pcbActual, char **ppszEnd)
394{
395 return RTBase64DecodeEx(pszString, RTSTR_MAX, pvData, cbData, pcbActual, ppszEnd);
396}
397RT_EXPORT_SYMBOL(RTBase64Decode);
398
399
400/**
401 * Calculates the length of the Base64 encoding of a given number of bytes of
402 * data.
403 *
404 * This will assume line breaks every 64 chars. A RTBase64EncodedLengthEx
405 * function can be added if closer control over the output is found to be
406 * required.
407 *
408 * @returns The Base64 string length.
409 * @param cbData The number of bytes to encode.
410 */
411RTDECL(size_t) RTBase64EncodedLength(size_t cbData)
412{
413 if (cbData * 8 / 8 != cbData)
414 {
415 AssertReturn(sizeof(size_t) == sizeof(uint64_t), ~(size_t)0);
416 uint64_t cch = cbData * (uint64_t)8;
417 while (cch % 24)
418 cch += 8;
419 cch /= 6;
420
421 cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
422 return cch;
423 }
424
425 size_t cch = cbData * 8;
426 while (cch % 24)
427 cch += 8;
428 cch /= 6;
429
430 cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
431 return cch;
432}
433RT_EXPORT_SYMBOL(RTBase64EncodedLength);
434
435
436/**
437 * Encodes the specifed data into a Base64 string, the caller supplies the
438 * output buffer.
439 *
440 * This will make the same assumptions about line breaks and EOL size as
441 * RTBase64EncodedLength() does. A RTBase64EncodeEx function can be added if
442 * more strict control over the output formatting is found necessary.
443 *
444 * @returns IRPT status code.
445 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
446 * may contain an invalid Base64 string.
447 *
448 * @param pvData The data to encode.
449 * @param cbData The number of bytes to encode.
450 * @param pszBuf Where to put the Base64 string.
451 * @param cbBuf The size of the output buffer, including the terminator.
452 * @param pcchActual The actual number of characters returned.
453 */
454RTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual)
455{
456 /*
457 * Process whole "trios" of input data.
458 */
459 uint8_t u8A;
460 uint8_t u8B;
461 uint8_t u8C;
462 size_t cbLineFeed = cbBuf - RTBASE64_LINE_LEN;
463 const uint8_t *pbSrc = (const uint8_t *)pvData;
464 char *pchDst = pszBuf;
465 while (cbData >= 3)
466 {
467 if (cbBuf < 4 + 1)
468 return VERR_BUFFER_OVERFLOW;
469
470 /* encode */
471 u8A = pbSrc[0];
472 pchDst[0] = g_szValToChar[u8A >> 2];
473 u8B = pbSrc[1];
474 pchDst[1] = g_szValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)];
475 u8C = pbSrc[2];
476 pchDst[2] = g_szValToChar[((u8B << 2) & 0x3f) | (u8C >> 6)];
477 pchDst[3] = g_szValToChar[u8C & 0x3f];
478
479 /* advance */
480 cbBuf -= 4;
481 pchDst += 4;
482 cbData -= 3;
483 pbSrc += 3;
484
485 /* deal out linefeeds */
486 if (cbBuf == cbLineFeed && cbData)
487 {
488 if (cbBuf < RTBASE64_EOL_SIZE + 1)
489 return VERR_BUFFER_OVERFLOW;
490 cbBuf -= RTBASE64_EOL_SIZE;
491 if (RTBASE64_EOL_SIZE == 2)
492 *pchDst++ = '\r';
493 *pchDst++ = '\n';
494 cbLineFeed = cbBuf - RTBASE64_LINE_LEN;
495 }
496 }
497
498 /*
499 * Deal with the odd bytes and string termination.
500 */
501 if (cbData)
502 {
503 if (cbBuf < 4 + 1)
504 return VERR_BUFFER_OVERFLOW;
505 switch (cbData)
506 {
507 case 1:
508 u8A = pbSrc[0];
509 pchDst[0] = g_szValToChar[u8A >> 2];
510 pchDst[1] = g_szValToChar[(u8A << 4) & 0x3f];
511 pchDst[2] = '=';
512 pchDst[3] = '=';
513 break;
514 case 2:
515 u8A = pbSrc[0];
516 pchDst[0] = g_szValToChar[u8A >> 2];
517 u8B = pbSrc[1];
518 pchDst[1] = g_szValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)];
519 pchDst[2] = g_szValToChar[(u8B << 2) & 0x3f];
520 pchDst[3] = '=';
521 break;
522 }
523 pchDst += 4;
524 }
525
526 *pchDst = '\0';
527
528 if (pcchActual)
529 *pcchActual = pchDst - pszBuf;
530 return VINF_SUCCESS;
531}
532RT_EXPORT_SYMBOL(RTBase64Encode);
533
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