Skip to content

Commit 63548fb

Browse files
nodejs-github-botaduh95
authored andcommitted
deps: update nbytes to 0.1.5
PR-URL: #67688 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent bc8732f commit 63548fb

5 files changed

Lines changed: 70 additions & 16 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "0.1.4"
2+
".": "0.1.5"
33
}

‎deps/nbytes/CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [0.1.5](https://github.com/nodejs/nbytes/compare/v0.1.4...v0.1.5) (2026-11-16)
4+
5+
6+
### Bug Fixes
7+
8+
* **base64, hex:** enhance decoding to ignore non-Latin1 characters ([#17](https://github.com/nodejs/nbytes/issues/17)) ([c0328e6](https://github.com/nodejs/nbytes/commit/c0328e6cc55b1aaae982d626c2f5a01f51cd2134))
9+
310
## [0.1.4](https://github.com/nodejs/nbytes/compare/v0.1.3...v0.1.4) (2026-04-09)
411

512

‎deps/nbytes/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.28)
2-
project(nbytes VERSION 0.1.4) # x-release-please-version
2+
project(nbytes VERSION 0.1.5) # x-release-please-version
33

44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_CXX_STANDARD_REQUIRED True)

‎deps/nbytes/include/nbytes.h‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ bool SwapBytes64(char *data, size_t nbytes);
109109

110110
extern const int8_t unbase64_table[256];
111111

112+
// Code units above 0xFF are never base64 digits; do not truncate them.
113+
template <typename TypeName>
114+
inline int8_t Unbase64(TypeName c) {
115+
if constexpr (sizeof(TypeName) > 1) {
116+
if (c > 0xFF) return -1;
117+
}
118+
return unbase64_table[static_cast<uint8_t>(c)];
119+
}
120+
112121
template <typename TypeName>
113122
bool Base64DecodeGroupSlow(char *const dst, const size_t dstlen,
114123
const TypeName *const src, const size_t srclen,
@@ -117,8 +126,8 @@ bool Base64DecodeGroupSlow(char *const dst, const size_t dstlen,
117126
uint8_t lo;
118127
#define V(expr) \
119128
for (;;) { \
120-
const uint8_t c = static_cast<uint8_t>(src[*i]); \
121-
lo = unbase64_table[c]; \
129+
const TypeName c = src[*i]; \
130+
lo = Unbase64(c); \
122131
*i += 1; \
123132
if (lo < 64) break; /* Legal character. */ \
124133
if (c == '=' || *i >= srclen) return false; /* Stop decoding. */ \
@@ -179,14 +188,10 @@ size_t Base64DecodeFast(char *const dst, const size_t dstlen,
179188
size_t k = 0;
180189
while (i < max_i && k < max_k) {
181190
const unsigned char txt[] = {
182-
static_cast<unsigned char>(
183-
unbase64_table[static_cast<uint8_t>(src[i + 0])]),
184-
static_cast<unsigned char>(
185-
unbase64_table[static_cast<uint8_t>(src[i + 1])]),
186-
static_cast<unsigned char>(
187-
unbase64_table[static_cast<uint8_t>(src[i + 2])]),
188-
static_cast<unsigned char>(
189-
unbase64_table[static_cast<uint8_t>(src[i + 3])]),
191+
static_cast<unsigned char>(Unbase64(src[i + 0])),
192+
static_cast<unsigned char>(Unbase64(src[i + 1])),
193+
static_cast<unsigned char>(Unbase64(src[i + 2])),
194+
static_cast<unsigned char>(Unbase64(src[i + 3])),
190195
};
191196

192197
const uint32_t v = ReadUint32BE(txt);
@@ -224,13 +229,22 @@ size_t Base64Decode(char *const dst, const size_t dstlen,
224229

225230
extern const int8_t unhex_table[256];
226231

232+
// Code units above 0xFF are never hex digits; do not truncate them.
233+
template <typename TypeName>
234+
inline int8_t Unhex(TypeName c) {
235+
if constexpr (sizeof(TypeName) > 1) {
236+
if (c > 0xFF) return -1;
237+
}
238+
return unhex_table[static_cast<uint8_t>(c)];
239+
}
240+
227241
template <typename TypeName>
228242
static size_t HexDecode(char *buf, size_t len, const TypeName *src,
229243
const size_t srcLen) {
230244
size_t i;
231245
for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) {
232-
unsigned a = unhex_table[static_cast<uint8_t>(src[i * 2 + 0])];
233-
unsigned b = unhex_table[static_cast<uint8_t>(src[i * 2 + 1])];
246+
unsigned a = Unhex(src[i * 2 + 0]);
247+
unsigned b = Unhex(src[i * 2 + 1]);
234248
if (!~a || !~b) return i;
235249
buf[i] = (a << 4) | b;
236250
}
@@ -836,12 +850,12 @@ size_t SearchString(const char *haystack, size_t haystack_length,
836850

837851
// ============================================================================
838852
// Version metadata
839-
#define NBYTES_VERSION "0.1.4" // x-release-please-version
853+
#define NBYTES_VERSION "0.1.5" // x-release-please-version
840854

841855
enum {
842856
NBYTES_VERSION_MAJOR = 0, // x-release-please-major
843857
NBYTES_VERSION_MINOR = 1, // x-release-please-minor
844-
NBYTES_VERSION_REVISION = 4, // x-release-please-patch
858+
NBYTES_VERSION_REVISION = 5, // x-release-please-patch
845859
};
846860

847861
} // namespace nbytes

‎deps/nbytes/tests/basic.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,36 @@ TEST(basic, swap_bytes64) {
2727
EXPECT_EQ(input, expected);
2828
SUCCEED();
2929
}
30+
31+
TEST(base64, decode_two_byte_ignores_non_latin1) {
32+
// U+4E2D has the low byte 0x2D ('-'); it must not decode as a base64 digit.
33+
const char16_t input[] = u"QUJD\u4E2DREVG";
34+
char out[16] = {};
35+
size_t n = nbytes::Base64Decode(out, sizeof(out), input, 9);
36+
EXPECT_EQ(std::string(out, n), "ABCDEF");
37+
}
38+
39+
TEST(base64, decode_two_byte_does_not_stop_on_low_byte_padding) {
40+
// U+013D has the low byte 0x3D ('='); it must not end the decoding.
41+
const char16_t input[] = u"QUJD\u013DREVG";
42+
char out[16] = {};
43+
size_t n = nbytes::Base64Decode(out, sizeof(out), input, 9);
44+
EXPECT_EQ(std::string(out, n), "ABCDEF");
45+
}
46+
47+
TEST(base64, decode_two_byte_all_non_latin1_is_empty) {
48+
// Four U+4E2D units form a full group in the fast path; the output must
49+
// still be empty rather than the bytes of "----".
50+
const char16_t input[] = u"\u4E2D\u4E2D\u4E2D\u4E2D";
51+
char out[16] = {};
52+
size_t n = nbytes::Base64Decode(out, sizeof(out), input, 4);
53+
EXPECT_EQ(n, 0u);
54+
}
55+
56+
TEST(hex, decode_two_byte_rejects_non_latin1) {
57+
// U+0141 has the low byte 0x41 ('A'); it is not a hex digit.
58+
const char16_t input[] = u"\u0141\u0141";
59+
char out[4] = {};
60+
size_t n = nbytes::HexDecode(out, sizeof(out), input, 2);
61+
EXPECT_EQ(n, 0u);
62+
}

0 commit comments

Comments
 (0)