Skip to content

Commit 23556f1

Browse files
authored
arm64: fix base64 default_or_url tables accepting '[' (#1062)
In to_base64_mask, the default_or_url tables gave '+' and '_' the same bit, so '[' (lut_lo[0xB] & lut_hi[0xB] = 0xa2 & 0x12) was taken for a base64 character. The scalar tables reject it. With base64_default_or_url, '[' was decoded instead of reported as INVALID_BASE64_CHARACTER; with base64_default_or_url_accept_garbage, it was not skipped. '_' now uses bit 0x10 (lut_lo[0xF] = 0xb6, lut_hi[0xB] = 0x10). Reported-by: Dylan Conway <[email protected]>
1 parent e2a9551 commit 23556f1

2 files changed

Lines changed: 63 additions & 5 deletions

File tree

‎src/arm64/arm_base64.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ uint64_t to_base64_mask(block64 *b, bool *error) {
335335
if (default_or_url) {
336336
lut_lo =
337337
simdutf_make_uint8x16_t(0xa9, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
338-
0xf8, 0xf9, 0xf1, 0xa2, 0xa1, 0xa5, 0xa0, 0xa6);
338+
0xf8, 0xf9, 0xf1, 0xa2, 0xa1, 0xa5, 0xa0, 0xb6);
339339
} else if (base64_url) {
340340
lut_lo =
341341
simdutf_make_uint8x16_t(0xa9, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
@@ -348,7 +348,7 @@ uint64_t to_base64_mask(block64 *b, bool *error) {
348348
#else
349349
if (default_or_url) {
350350
lut_lo = uint8x16_t{0xa9, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
351-
0xf8, 0xf9, 0xf1, 0xa2, 0xa1, 0xa5, 0xa0, 0xa6};
351+
0xf8, 0xf9, 0xf1, 0xa2, 0xa1, 0xa5, 0xa0, 0xb6};
352352
} else if (base64_url) {
353353
lut_lo = uint8x16_t{0xa9, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
354354
0xf8, 0xf9, 0xf1, 0xa0, 0xa1, 0xa5, 0xa0, 0xa2};
@@ -366,10 +366,10 @@ uint64_t to_base64_mask(block64 *b, bool *error) {
366366
if (default_or_url) {
367367
lut_hi =
368368
simdutf_make_uint8x16_t(0x0, 0x1, 0x0, 0x0, 0x1, 0x6, 0x8, 0x8, 0x10,
369-
0x20, 0x20, 0x12, 0x40, 0x80, 0x80, 0x40);
369+
0x20, 0x20, 0x10, 0x40, 0x80, 0x80, 0x40);
370370
} else if (base64_url) {
371371
lut_hi =
372-
simdutf_make_uint8x16_t(0x0, 0x1, 0x0, 0x0, 0x1, 0x6, 0x8, 0x8, 0x10,
372+
simdutf_make_uint8x16_t(0x0, 0x1, 0x0, 0x0, 0x1, 0x4, 0x8, 0x8, 0x10,
373373
0x20, 0x20, 0x12, 0x40, 0x80, 0x80, 0x40);
374374
} else {
375375
lut_hi =
@@ -379,7 +379,7 @@ uint64_t to_base64_mask(block64 *b, bool *error) {
379379
#else
380380
if (default_or_url) {
381381
lut_hi = uint8x16_t{0x0, 0x1, 0x0, 0x0, 0x1, 0x6, 0x8, 0x8,
382-
0x10, 0x20, 0x20, 0x12, 0x40, 0x80, 0x80, 0x40};
382+
0x10, 0x20, 0x20, 0x10, 0x40, 0x80, 0x80, 0x40};
383383
} else if (base64_url) {
384384
lut_hi = uint8x16_t{0x0, 0x1, 0x0, 0x0, 0x1, 0x4, 0x8, 0x8,
385385
0x10, 0x20, 0x20, 0x12, 0x40, 0x80, 0x80, 0x40};

‎tests/base64_tests.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,64 @@ TEST(issue_node_anything_goes) {
340340
text);
341341
}
342342

343+
TEST(base64_every_byte_value_in_a_block) {
344+
const simdutf::base64_options options[] = {
345+
simdutf::base64_default,
346+
simdutf::base64_url,
347+
simdutf::base64_default_or_url,
348+
simdutf::base64_default_accept_garbage,
349+
simdutf::base64_url_accept_garbage,
350+
simdutf::base64_default_or_url_accept_garbage};
351+
for (simdutf::base64_options option : options) {
352+
const bool url = option == simdutf::base64_url ||
353+
option == simdutf::base64_url_accept_garbage;
354+
const bool both = option == simdutf::base64_default_or_url ||
355+
option == simdutf::base64_default_or_url_accept_garbage;
356+
const bool garbage =
357+
option == simdutf::base64_default_accept_garbage ||
358+
option == simdutf::base64_url_accept_garbage ||
359+
option == simdutf::base64_default_or_url_accept_garbage;
360+
for (int c = 0; c < 256; c++) {
361+
if (c == '=') {
362+
continue;
363+
}
364+
const bool alpha = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
365+
(c >= '0' && c <= '9');
366+
const bool extra = ((c == '+' || c == '/') && (!url || both)) ||
367+
((c == '-' || c == '_') && (url || both));
368+
const bool space =
369+
c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
370+
for (size_t pos : {size_t(0), size_t(31), size_t(63)}) {
371+
std::string input(128, 'A');
372+
input[pos] = char(c);
373+
std::vector<char> out(96);
374+
simdutf::result r = implementation.base64_to_binary(
375+
input.data(), input.size(), out.data(), option);
376+
if (alpha || extra) {
377+
ASSERT_EQUAL(r.error, simdutf::error_code::SUCCESS);
378+
ASSERT_EQUAL(r.count, 96);
379+
} else if (space || garbage) {
380+
ASSERT_EQUAL(r.error, simdutf::error_code::SUCCESS);
381+
ASSERT_EQUAL(r.count, 95);
382+
} else {
383+
ASSERT_EQUAL(r.error, simdutf::error_code::INVALID_BASE64_CHARACTER);
384+
ASSERT_EQUAL(r.count, pos);
385+
}
386+
}
387+
}
388+
}
389+
}
390+
391+
TEST(base64_default_or_url_only_garbage) {
392+
const std::string input = "[[" + std::string(62, '!');
393+
std::vector<char> out(48);
394+
simdutf::result r = implementation.base64_to_binary(
395+
input.data(), input.size(), out.data(),
396+
simdutf::base64_default_or_url_accept_garbage);
397+
ASSERT_EQUAL(r.error, simdutf::error_code::SUCCESS);
398+
ASSERT_EQUAL(r.count, 0);
399+
}
400+
343401
TEST(issue_dash) {
344402
const std::string input = "Iw==";
345403
std::vector<char> back(1);

0 commit comments

Comments
 (0)