Changeset 16766 in vbox
- Timestamp:
- Feb 14, 2009 9:32:04 AM (16 years ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/base64.cpp
r16765 r16766 44 44 * Defined Constants And Macros * 45 45 *******************************************************************************/ 46 /** The line length used for encoding. */ 47 #define RTBASE64_LINE_LEN 64 48 46 49 /** @name Special g_au8CharToVal values 47 50 * @{ */ 48 #define BASE64_SPACE 0xc049 #define BASE64_PAD 0xe050 #define BASE64_INVALID 0xff51 #define BASE64_SPACE 0xc0 52 #define BASE64_PAD 0xe0 53 #define BASE64_INVALID 0xff 51 54 /** @} */ 52 55 … … 252 255 AssertCompile(sizeof(char) == sizeof(uint8_t)); 253 256 254 const char *pszCurStart;255 257 for (;;) 256 258 { 257 pszCurStart = pszString;258 259 /* The first 6-bit group. */ 259 260 while ((u8 = g_au8CharToVal[ch = *pszString]) == BASE64_SPACE) … … 421 422 cch /= 6; 422 423 423 cch += (cch / 64) * RTBASE64_EOL_SIZE;424 cch += (cch / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE; 424 425 return cch; 425 426 } … … 430 431 cch /= 6; 431 432 432 cch += (cch / 64) * RTBASE64_EOL_SIZE;433 cch += (cch / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE; 433 434 return cch; 434 435 } … … 455 456 RTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual) 456 457 { 457 /** @todo implement RTBase64Encode. */ 458 return VERR_NOT_IMPLEMENTED; 458 /* 459 * Process whole "trios" of input data. 460 */ 461 uint8_t u8A; 462 uint8_t u8B; 463 uint8_t u8C; 464 size_t cbLineFeed = cbBuf - RTBASE64_LINE_LEN; 465 const uint8_t *pbSrc = (const uint8_t *)pvData; 466 char *pchDst = pszBuf; 467 while (cbData >= 3) 468 { 469 if (cbBuf < 4 + 1) 470 return VERR_BUFFER_OVERFLOW; 471 472 /* encode */ 473 u8A = pbSrc[0]; 474 pchDst[0] = g_szValToChar[u8A >> 2]; 475 u8B = pbSrc[1]; 476 pchDst[1] = g_szValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)]; 477 u8C = pbSrc[2]; 478 pchDst[2] = g_szValToChar[((u8B << 2) & 0x3f) | (u8C >> 6)]; 479 pchDst[3] = g_szValToChar[u8C & 0x3f]; 480 481 /* advance */ 482 cbBuf -= 4; 483 pchDst += 4; 484 cbData -= 3; 485 pbSrc += 3; 486 487 if (cbBuf == cbLineFeed) 488 { 489 if (cbBuf < RTBASE64_EOL_SIZE + 1) 490 return VERR_BUFFER_OVERFLOW; 491 cbBuf -= RTBASE64_EOL_SIZE; 492 if (RTBASE64_EOL_SIZE == 2) 493 *pchDst++ = '\r'; 494 *pchDst++ = '\n'; 495 cbLineFeed = cbBuf - RTBASE64_LINE_LEN; 496 } 497 } 498 499 /* 500 * Deal with the odd bytes and string termination. 501 */ 502 if (cbData) 503 { 504 if (cbBuf < 4 + 1) 505 return VERR_BUFFER_OVERFLOW; 506 switch (cbData) 507 { 508 case 1: 509 u8A = pbSrc[0]; 510 pchDst[0] = g_szValToChar[u8A >> 2]; 511 pchDst[1] = g_szValToChar[(u8A << 4) & 0x3f]; 512 pchDst[2] = '='; 513 pchDst[3] = '='; 514 break; 515 case 2: 516 u8A = pbSrc[0]; 517 pchDst[0] = g_szValToChar[u8A >> 2]; 518 u8B = pbSrc[1]; 519 pchDst[1] = g_szValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)]; 520 pchDst[2] = g_szValToChar[(u8B << 2) & 0x3f]; 521 pchDst[3] = '='; 522 break; 523 } 524 pchDst += 4; 525 } 526 527 *pchDst = '\0'; 528 529 if (pcchActual) 530 *pcchActual = pchDst - pszBuf; 531 return VINF_SUCCESS; 459 532 } 460 533 -
trunk/src/VBox/Runtime/testcase/tstBase64.cpp
r16765 r16766 38 38 #include <iprt/initterm.h> 39 39 40 /******************************************************************************* 41 * Defined Constants And Macros * 42 *******************************************************************************/ 43 #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS) 44 # define MY_NL "\r\n" 45 #else 46 # define MY_NL "\n" 47 #endif 48 40 49 41 50 int main() … … 61 70 TEST_ENTRY("Hey", "SGV5"), 62 71 TEST_ENTRY("Base64", "QmFzZTY0"), 63 TEST_ENTRY("Call me Ishmael.", "Q2FsbCBtZSBJc2htYWVsLg==") 72 TEST_ENTRY("Call me Ishmael.", "Q2FsbCBtZSBJc2htYWVsLg=="), 73 TEST_ENTRY( 74 "Man is distinguished, not only by his reason, but by this singular passion " 75 "from other animals, which is a lust of the mind, that by a perseverance of " 76 "delight in the continued and indefatigable generation of knowledge, exceeds " 77 "the short vehemence of any carnal pleasure." /* Thomas Hobbes's Leviathan */, 78 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1" MY_NL 79 "dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3" MY_NL 80 "aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFu" MY_NL 81 "Y2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxl" MY_NL 82 "IGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhl" MY_NL 83 "bWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" 84 ) 64 85 #undef TEST_ENTRY 65 86 }; … … 94 115 } 95 116 96 rc = RTBase64Encode(g_aTests[i].pszText, g_aTests[i].cchText, szOut, g_aTests[i].cchEnc + 1, &cchOut);117 rc = RTBase64Encode(g_aTests[i].pszText, g_aTests[i].cchText, szOut, g_aTests[i].cchEnc*2 + 1, &cchOut); 97 118 if (RT_FAILURE(rc)) 98 119 { … … 124 145 125 146 /* 126 * Bigger Test.147 * Try with some more junk in the encoding and different line length. 127 148 */ 128 149 static const char s_szText2[] = … … 133 154 134 155 static const char s_szEnc2[] = 135 " TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n"136 " IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n\r"137 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n"138 " 139 " ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n \n";156 " TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n" 157 " IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n\r\t\t\t\v" 158 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n" 159 "\tdWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n\r" 160 " ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n \n \r \n \t"; 140 161 141 162 int rc = RTBase64Decode(&s_szEnc2[0], szOut, sizeof(s_szText2), &cchOut, NULL);
Note:
See TracChangeset
for help on using the changeset viewer.