Changeset 84296 in vbox for trunk/src/VBox/Runtime/common/string
- Timestamp:
- May 13, 2020 4:46:27 PM (5 years ago)
- svn:sync-xref-src-repo-rev:
- 137938
- Location:
- trunk/src/VBox/Runtime/common/string
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/base64-utf16.cpp
r84293 r84296 49 49 DECL_FORCE_INLINE(uint8_t) rtBase64TranslateUtf16(RTUTF16 wc) 50 50 { 51 if (wc < RT_ELEMENTS(g_au8 RTBase64CharToVal))52 return g_au8 RTBase64CharToVal[wc];51 if (wc < RT_ELEMENTS(g_au8rtBase64CharToVal)) 52 return g_au8rtBase64CharToVal[wc]; 53 53 if (RTUniCpIsSpace(wc)) 54 54 return BASE64_SPACE; … … 62 62 if (cwcStringMax > 0) 63 63 return rtBase64TranslateUtf16(*pwszString); 64 return BASE64_ INVALID;64 return BASE64_NULL; 65 65 } 66 66 … … 68 68 /* 69 69 * Mostly the same as RTBase64DecodedSizeEx, except for the wider character 70 * type and therefore more careful handling of g_sz RTBase64ValToChar and additional70 * type and therefore more careful handling of g_szrtBase64ValToChar and additional 71 71 * space characters. Fixes must be applied to both copies of the code. 72 72 */ … … 81 81 */ 82 82 uint32_t c6Bits = 0; 83 uint8_t u8 = BASE64_INVALID; 84 RTUTF16 wc = 0; 85 86 while (cwcStringMax > 0 && (wc = *pwszString)) 87 { 88 u8 = rtBase64TranslateUtf16(wc); 83 uint8_t u8; 84 85 while ((u8 = rtBase64TranslateNextUtf16(pwszString, cwcStringMax)) != BASE64_NULL) 86 { 89 87 if (u8 < 64) 90 88 c6Bits++; … … 108 106 pwszString++; 109 107 cwcStringMax--; 110 while (cwcStringMax > 0 && (wc = *pwszString)) 111 { 112 u8 = rtBase64TranslateUtf16(wc); 108 while ((u8 = rtBase64TranslateNextUtf16(pwszString, cwcStringMax)) != BASE64_NULL) 109 { 113 110 if (u8 != BASE64_SPACE) 114 111 { … … 129 126 * Base64 text ends? Return failure. 130 127 */ 131 if ( u8 == BASE64_INVALID 132 && !ppwszEnd 133 && wc) 128 if ( u8 == BASE64_INVALID 129 && !ppwszEnd) 134 130 return -1; 135 131 … … 165 161 uint8_t u8; 166 162 unsigned c6Bits = 0; 167 AssertCompile(sizeof(char) == sizeof(uint8_t));168 163 169 164 for (;;) … … 241 236 pwszString++; 242 237 cwcStringMax--; 243 RTUTF16 wc; 244 while (cwcStringMax > 0 && (wc = *pwszString)) 245 { 246 u8 = rtBase64TranslateUtf16(wc); 238 while ((u8 = rtBase64TranslateNextUtf16(pwszString, cwcStringMax)) != BASE64_NULL) 239 { 247 240 if (u8 != BASE64_SPACE) 248 241 { … … 262 255 * Base64 text ends? Return failure. 263 256 */ 264 if ( u8 == BASE64_INVALID 265 && !ppwszEnd 266 && cwcStringMax != 0 267 && *pwszString != '\0') 257 if ( u8 == BASE64_INVALID 258 && !ppwszEnd) 268 259 return VERR_INVALID_BASE64_ENCODING; 269 260 … … 361 352 { 362 353 /* Expand the EOL style flags: */ 363 size_t const cchEol = g_acch RTBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK];364 char const chEol0 = g_aach RTBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][0];365 char const chEol1 = g_aach RTBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][1];354 size_t const cchEol = g_acchrtBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK]; 355 char const chEol0 = g_aachrtBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][0]; 356 char const chEol1 = g_aachrtBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][1]; 366 357 Assert(cchEol == (chEol0 != '\0' ? 1U : 0U) + (chEol1 != '\0' ? 1U : 0U)); 367 358 … … 382 373 /* encode */ 383 374 u8A = pbSrc[0]; 384 pwcDst[0] = g_sz RTBase64ValToChar[u8A >> 2];375 pwcDst[0] = g_szrtBase64ValToChar[u8A >> 2]; 385 376 u8B = pbSrc[1]; 386 pwcDst[1] = g_sz RTBase64ValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)];377 pwcDst[1] = g_szrtBase64ValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)]; 387 378 u8C = pbSrc[2]; 388 pwcDst[2] = g_sz RTBase64ValToChar[((u8B << 2) & 0x3f) | (u8C >> 6)];389 pwcDst[3] = g_sz RTBase64ValToChar[u8C & 0x3f];379 pwcDst[2] = g_szrtBase64ValToChar[((u8B << 2) & 0x3f) | (u8C >> 6)]; 380 pwcDst[3] = g_szrtBase64ValToChar[u8C & 0x3f]; 390 381 391 382 /* advance */ … … 419 410 case 1: 420 411 u8A = pbSrc[0]; 421 pwcDst[0] = g_sz RTBase64ValToChar[u8A >> 2];422 pwcDst[1] = g_sz RTBase64ValToChar[(u8A << 4) & 0x3f];412 pwcDst[0] = g_szrtBase64ValToChar[u8A >> 2]; 413 pwcDst[1] = g_szrtBase64ValToChar[(u8A << 4) & 0x3f]; 423 414 pwcDst[2] = '='; 424 415 pwcDst[3] = '='; … … 426 417 case 2: 427 418 u8A = pbSrc[0]; 428 pwcDst[0] = g_sz RTBase64ValToChar[u8A >> 2];419 pwcDst[0] = g_szrtBase64ValToChar[u8A >> 2]; 429 420 u8B = pbSrc[1]; 430 pwcDst[1] = g_sz RTBase64ValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)];431 pwcDst[2] = g_sz RTBase64ValToChar[(u8B << 2) & 0x3f];421 pwcDst[1] = g_szrtBase64ValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)]; 422 pwcDst[2] = g_szrtBase64ValToChar[(u8B << 2) & 0x3f]; 432 423 pwcDst[3] = '='; 433 424 break; -
trunk/src/VBox/Runtime/common/string/base64.cpp
r84293 r84296 51 51 /** Base64 character to value. (RFC 2045) 52 52 * ASSUMES ASCII / UTF-8. */ 53 DECL_HIDDEN_CONST(const uint8_t) g_au8 RTBase64CharToVal[256] =54 { 55 0xf f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xff, 0xff, /* 0x00..0x0f */53 DECL_HIDDEN_CONST(const uint8_t) g_au8rtBase64CharToVal[256] = 54 { 55 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xff, 0xff, /* 0x00..0x0f */ 56 56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x10..0x1f */ 57 57 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 62, 0xff, 0xff, 0xff, 63, /* 0x20..0x2f */ … … 72 72 73 73 /** Value to Base64 character. (RFC 2045) */ 74 DECL_HIDDEN_CONST(const char) g_sz RTBase64ValToChar[64+1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";74 DECL_HIDDEN_CONST(const char) g_szrtBase64ValToChar[64+1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 75 75 76 76 /** The end-of-line lengths (indexed by style flag value). */ 77 DECL_HIDDEN_CONST(const size_t) g_acch RTBase64EolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1] =77 DECL_HIDDEN_CONST(const size_t) g_acchrtBase64EolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1] = 78 78 { 79 79 /*[RTBASE64_FLAGS_EOL_NATIVE ]:*/ RTBASE64_EOL_SIZE, … … 84 84 85 85 /** The end-of-line characters (zero, one or two). */ 86 DECL_HIDDEN_CONST(const char) g_aach RTBase64EolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1][2] =86 DECL_HIDDEN_CONST(const char) g_aachrtBase64EolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1][2] = 87 87 { 88 88 /*[RTBASE64_FLAGS_EOL_NATIVE ]:*/ { RTBASE64_EOL_SIZE == 1 ? '\n' : '\r', RTBASE64_EOL_SIZE == 1 ? '\0' : '\n', }, … … 105 105 for (unsigned i = 0; i < 64; i++) 106 106 { 107 unsigned ch = g_sz RTBase64ValToChar[i];107 unsigned ch = g_szrtBase64ValToChar[i]; 108 108 Assert(ch); 109 Assert(g_au8 RTBase64CharToVal[ch] == i);109 Assert(g_au8rtBase64CharToVal[ch] == i); 110 110 } 111 111 112 112 for (unsigned i = 0; i < 256; i++) 113 113 { 114 uint8_t u8 = g_au8 RTBase64CharToVal[i];114 uint8_t u8 = g_au8rtBase64CharToVal[i]; 115 115 Assert( ( u8 == BASE64_INVALID 116 116 && !RT_C_IS_ALNUM(i) … … 121 121 && RT_C_IS_SPACE(i)) 122 122 || ( u8 < 64 123 && (unsigned)g_szRTBase64ValToChar[u8] == i)); 123 && (unsigned)g_szrtBase64ValToChar[u8] == i) 124 || ( u8 == BASE64_NULL 125 && i == 0) ); 124 126 } 125 127 ASMAtomicWriteBool(&s_fSane, true); … … 127 129 } 128 130 #endif /* RT_STRICT */ 131 132 133 134 /** Fetched the next character in the string and translates it. */ 135 DECL_FORCE_INLINE(uint8_t) rtBase64TranslateNext(const char *pszString, size_t cchStringMax) 136 { 137 AssertCompile(sizeof(unsigned char) == sizeof(uint8_t)); 138 if (cchStringMax > 0) 139 return g_au8rtBase64CharToVal[(unsigned char)*pszString]; 140 return BASE64_NULL; 141 } 129 142 130 143 … … 143 156 */ 144 157 uint32_t c6Bits = 0; 145 uint8_t u8 = BASE64_INVALID; 146 unsigned ch = 0; 147 AssertCompile(sizeof(char) == sizeof(uint8_t)); 148 149 while (cchStringMax > 0 && (ch = *pszString)) 150 { 151 u8 = g_au8RTBase64CharToVal[ch]; 158 uint8_t u8; 159 160 while ((u8 = rtBase64TranslateNext(pszString, cchStringMax)) != BASE64_NULL) 161 { 152 162 if (u8 < 64) 153 163 c6Bits++; … … 171 181 pszString++; 172 182 cchStringMax--; 173 while (cchStringMax > 0 && (ch = *pszString)) 174 { 175 u8 = g_au8RTBase64CharToVal[ch]; 183 while ((u8 = rtBase64TranslateNext(pszString, cchStringMax)) != BASE64_NULL) 184 { 176 185 if (u8 != BASE64_SPACE) 177 186 { … … 192 201 * Base64 text ends? Return failure. 193 202 */ 194 if ( u8 == BASE64_INVALID 195 && !ppszEnd 196 && ch) 203 if ( u8 == BASE64_INVALID 204 && !ppszEnd) 197 205 return -1; 198 206 … … 226 234 uint8_t u8Trio[3] = { 0, 0, 0 }; /* shuts up gcc */ 227 235 uint8_t *pbData = (uint8_t *)pvData; 228 unsigned ch;229 236 uint8_t u8; 230 237 unsigned c6Bits = 0; 231 AssertCompile(sizeof(char) == sizeof(uint8_t));232 238 233 239 for (;;) 234 240 { 235 241 /* The first 6-bit group. */ 236 while ((u8 = g_au8RTBase64CharToVal[ch = cchStringMax > 0 ? (uint8_t)*pszString : 0]) == BASE64_SPACE)242 while ((u8 = rtBase64TranslateNext(pszString, cchStringMax)) == BASE64_SPACE) 237 243 pszString++, cchStringMax--; 238 244 if (u8 >= 64) … … 246 252 247 253 /* The second 6-bit group. */ 248 while ((u8 = g_au8RTBase64CharToVal[ch = cchStringMax > 0 ? (uint8_t)*pszString : 0]) == BASE64_SPACE)254 while ((u8 = rtBase64TranslateNext(pszString, cchStringMax)) == BASE64_SPACE) 249 255 pszString++, cchStringMax--; 250 256 if (u8 >= 64) … … 260 266 /* The third 6-bit group. */ 261 267 u8 = BASE64_INVALID; 262 while ((u8 = g_au8RTBase64CharToVal[ch = cchStringMax > 0 ? (uint8_t)*pszString : 0]) == BASE64_SPACE)268 while ((u8 = rtBase64TranslateNext(pszString, cchStringMax)) == BASE64_SPACE) 263 269 pszString++, cchStringMax--; 264 270 if (u8 >= 64) … … 274 280 /* The fourth 6-bit group. */ 275 281 u8 = BASE64_INVALID; 276 while ((u8 = g_au8RTBase64CharToVal[ch = cchStringMax > 0 ? (uint8_t)*pszString : 0]) == BASE64_SPACE)282 while ((u8 = rtBase64TranslateNext(pszString, cchStringMax)) == BASE64_SPACE) 277 283 pszString++, cchStringMax--; 278 284 if (u8 >= 64) … … 305 311 pszString++; 306 312 cchStringMax--; 307 while (cchStringMax > 0 && (ch = (uint8_t)*pszString)) 308 { 309 u8 = g_au8RTBase64CharToVal[ch]; 313 while ((u8 = rtBase64TranslateNext(pszString, cchStringMax)) != BASE64_NULL) 314 { 310 315 if (u8 != BASE64_SPACE) 311 316 { … … 325 330 * Base64 text ends? Return failure. 326 331 */ 327 if ( u8 == BASE64_INVALID 328 && !ppszEnd 329 && ch != '\0') 332 if ( u8 == BASE64_INVALID 333 && !ppszEnd) 330 334 return VERR_INVALID_BASE64_ENCODING; 331 335 … … 402 406 RTDECL(size_t) RTBase64EncodedLengthEx(size_t cbData, uint32_t fFlags) 403 407 { 404 size_t const cchEol = g_acch RTBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK];408 size_t const cchEol = g_acchrtBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK]; 405 409 406 410 if (cbData * 8 / 8 != cbData) … … 441 445 { 442 446 /* Expand the EOL style flags: */ 443 size_t const cchEol = g_acch RTBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK];444 char const chEol0 = g_aach RTBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][0];445 char const chEol1 = g_aach RTBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][1];447 size_t const cchEol = g_acchrtBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK]; 448 char const chEol0 = g_aachrtBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][0]; 449 char const chEol1 = g_aachrtBase64EolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][1]; 446 450 Assert(cchEol == (chEol0 != '\0' ? 1U : 0U) + (chEol1 != '\0' ? 1U : 0U)); 447 451 … … 462 466 /* encode */ 463 467 u8A = pbSrc[0]; 464 pchDst[0] = g_sz RTBase64ValToChar[u8A >> 2];468 pchDst[0] = g_szrtBase64ValToChar[u8A >> 2]; 465 469 u8B = pbSrc[1]; 466 pchDst[1] = g_sz RTBase64ValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)];470 pchDst[1] = g_szrtBase64ValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)]; 467 471 u8C = pbSrc[2]; 468 pchDst[2] = g_sz RTBase64ValToChar[((u8B << 2) & 0x3f) | (u8C >> 6)];469 pchDst[3] = g_sz RTBase64ValToChar[u8C & 0x3f];472 pchDst[2] = g_szrtBase64ValToChar[((u8B << 2) & 0x3f) | (u8C >> 6)]; 473 pchDst[3] = g_szrtBase64ValToChar[u8C & 0x3f]; 470 474 471 475 /* advance */ … … 499 503 case 1: 500 504 u8A = pbSrc[0]; 501 pchDst[0] = g_sz RTBase64ValToChar[u8A >> 2];502 pchDst[1] = g_sz RTBase64ValToChar[(u8A << 4) & 0x3f];505 pchDst[0] = g_szrtBase64ValToChar[u8A >> 2]; 506 pchDst[1] = g_szrtBase64ValToChar[(u8A << 4) & 0x3f]; 503 507 pchDst[2] = '='; 504 508 pchDst[3] = '='; … … 506 510 case 2: 507 511 u8A = pbSrc[0]; 508 pchDst[0] = g_sz RTBase64ValToChar[u8A >> 2];512 pchDst[0] = g_szrtBase64ValToChar[u8A >> 2]; 509 513 u8B = pbSrc[1]; 510 pchDst[1] = g_sz RTBase64ValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)];511 pchDst[2] = g_sz RTBase64ValToChar[(u8B << 2) & 0x3f];514 pchDst[1] = g_szrtBase64ValToChar[((u8A << 4) & 0x3f) | (u8B >> 4)]; 515 pchDst[2] = g_szrtBase64ValToChar[(u8B << 2) & 0x3f]; 512 516 pchDst[3] = '='; 513 517 break; -
trunk/src/VBox/Runtime/common/string/base64.h
r84294 r84296 32 32 #define RTBASE64_LINE_LEN 64 33 33 34 /** @name Special g_au8 RTBase64CharToVal values34 /** @name Special g_au8rtBase64CharToVal values 35 35 * @{ */ 36 36 #define BASE64_SPACE 0xc0 37 37 #define BASE64_PAD 0xe0 38 #define BASE64_NULL 0xfe 38 39 #define BASE64_INVALID 0xff 39 40 /** @} */ … … 43 44 * Global Variables * 44 45 *********************************************************************************************************************************/ 45 extern DECLHIDDEN(const uint8_t) g_au8 RTBase64CharToVal[256];46 extern DECLHIDDEN(const char) g_sz RTBase64ValToChar[64+1];47 extern DECLHIDDEN(const size_t) g_acch RTBase64EolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1];48 extern DECLHIDDEN(const char) g_aach RTBase64EolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1][2];46 extern DECLHIDDEN(const uint8_t) g_au8rtBase64CharToVal[256]; 47 extern DECLHIDDEN(const char) g_szrtBase64ValToChar[64+1]; 48 extern DECLHIDDEN(const size_t) g_acchrtBase64EolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1]; 49 extern DECLHIDDEN(const char) g_aachrtBase64EolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1][2]; 49 50 50 51
Note:
See TracChangeset
for help on using the changeset viewer.