Skip to content

Commit ff4cf81

Browse files
juanarboladdaleax
authored andcommitted
src: create helper for reading Uint32BE
Fixes: #34827 PR-URL: #34944 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent a880653 commit ff4cf81

File tree

5 files changed

+110
-88
lines changed

5 files changed

+110
-88
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@
668668
'src/base_object.h',
669669
'src/base_object-inl.h',
670670
'src/base64.h',
671+
'src/base64-inl.h',
671672
'src/callback_queue.h',
672673
'src/callback_queue-inl.h',
673674
'src/connect_wrap.h',

src/base64-inl.h

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef SRC_BASE64_INL_H_
2+
#define SRC_BASE64_INL_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "util.h"
7+
8+
namespace node {
9+
10+
extern const int8_t unbase64_table[256];
11+
12+
13+
inline static int8_t unbase64(uint8_t x) {
14+
return unbase64_table[x];
15+
}
16+
17+
18+
inline uint32_t ReadUint32BE(const unsigned char* p) {
19+
return static_cast<uint32_t>(p[0] << 24U) |
20+
static_cast<uint32_t>(p[1] << 16U) |
21+
static_cast<uint32_t>(p[2] << 8U) |
22+
static_cast<uint32_t>(p[3]);
23+
}
24+
25+
26+
template <typename TypeName>
27+
bool base64_decode_group_slow(char* const dst, const size_t dstlen,
28+
const TypeName* const src, const size_t srclen,
29+
size_t* const i, size_t* const k) {
30+
uint8_t hi;
31+
uint8_t lo;
32+
#define V(expr) \
33+
for (;;) { \
34+
const uint8_t c = src[*i]; \
35+
lo = unbase64(c); \
36+
*i += 1; \
37+
if (lo < 64) \
38+
break; /* Legal character. */ \
39+
if (c == '=' || *i >= srclen) \
40+
return false; /* Stop decoding. */ \
41+
} \
42+
expr; \
43+
if (*i >= srclen) \
44+
return false; \
45+
if (*k >= dstlen) \
46+
return false; \
47+
hi = lo;
48+
V(/* Nothing. */);
49+
V(dst[(*k)++] = ((hi & 0x3F) << 2) | ((lo & 0x30) >> 4));
50+
V(dst[(*k)++] = ((hi & 0x0F) << 4) | ((lo & 0x3C) >> 2));
51+
V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
52+
#undef V
53+
return true; // Continue decoding.
54+
}
55+
56+
57+
template <typename TypeName>
58+
size_t base64_decode_fast(char* const dst, const size_t dstlen,
59+
const TypeName* const src, const size_t srclen,
60+
const size_t decoded_size) {
61+
const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
62+
const size_t max_k = available / 3 * 3;
63+
size_t max_i = srclen / 4 * 4;
64+
size_t i = 0;
65+
size_t k = 0;
66+
while (i < max_i && k < max_k) {
67+
const unsigned char txt[] = {
68+
static_cast<unsigned char>(unbase64(src[i + 0])),
69+
static_cast<unsigned char>(unbase64(src[i + 1])),
70+
static_cast<unsigned char>(unbase64(src[i + 2])),
71+
static_cast<unsigned char>(unbase64(src[i + 3])),
72+
};
73+
74+
const uint32_t v = ReadUint32BE(txt);
75+
// If MSB is set, input contains whitespace or is not valid base64.
76+
if (v & 0x80808080) {
77+
if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k))
78+
return k;
79+
max_i = i + (srclen - i) / 4 * 4; // Align max_i again.
80+
} else {
81+
dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
82+
dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
83+
dst[k + 2] = ((v >> 2) & 0xC0) | ((v >> 0) & 0x3F);
84+
i += 4;
85+
k += 3;
86+
}
87+
}
88+
if (i < srclen && k < dstlen) {
89+
base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
90+
}
91+
return k;
92+
}
93+
94+
} // namespace node
95+
96+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
97+
98+
#endif // SRC_BASE64_INL_H_

src/base64.h

+3-75
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include "util.h"
7+
#include "base64-inl.h"
78

89
#include <cstddef>
910
#include <cstdint>
@@ -20,6 +21,8 @@ static inline constexpr size_t base64_decoded_size_fast(size_t size) {
2021
return size > 1 ? (size / 4) * 3 + (size % 4 + 1) / 2 : 0;
2122
}
2223

24+
inline uint32_t ReadUint32BE(const unsigned char* p);
25+
2326
template <typename TypeName>
2427
size_t base64_decoded_size(const TypeName* src, size_t size) {
2528
// 1-byte input cannot be decoded
@@ -34,81 +37,6 @@ size_t base64_decoded_size(const TypeName* src, size_t size) {
3437
return base64_decoded_size_fast(size);
3538
}
3639

37-
38-
extern const int8_t unbase64_table[256];
39-
40-
41-
inline static int8_t unbase64(uint8_t x) {
42-
return unbase64_table[x];
43-
}
44-
45-
46-
template <typename TypeName>
47-
bool base64_decode_group_slow(char* const dst, const size_t dstlen,
48-
const TypeName* const src, const size_t srclen,
49-
size_t* const i, size_t* const k) {
50-
uint8_t hi;
51-
uint8_t lo;
52-
#define V(expr) \
53-
for (;;) { \
54-
const uint8_t c = src[*i]; \
55-
lo = unbase64(c); \
56-
*i += 1; \
57-
if (lo < 64) \
58-
break; /* Legal character. */ \
59-
if (c == '=' || *i >= srclen) \
60-
return false; /* Stop decoding. */ \
61-
} \
62-
expr; \
63-
if (*i >= srclen) \
64-
return false; \
65-
if (*k >= dstlen) \
66-
return false; \
67-
hi = lo;
68-
V(/* Nothing. */);
69-
V(dst[(*k)++] = ((hi & 0x3F) << 2) | ((lo & 0x30) >> 4));
70-
V(dst[(*k)++] = ((hi & 0x0F) << 4) | ((lo & 0x3C) >> 2));
71-
V(dst[(*k)++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));
72-
#undef V
73-
return true; // Continue decoding.
74-
}
75-
76-
77-
template <typename TypeName>
78-
size_t base64_decode_fast(char* const dst, const size_t dstlen,
79-
const TypeName* const src, const size_t srclen,
80-
const size_t decoded_size) {
81-
const size_t available = dstlen < decoded_size ? dstlen : decoded_size;
82-
const size_t max_k = available / 3 * 3;
83-
size_t max_i = srclen / 4 * 4;
84-
size_t i = 0;
85-
size_t k = 0;
86-
while (i < max_i && k < max_k) {
87-
const uint32_t v =
88-
unbase64(src[i + 0]) << 24 |
89-
unbase64(src[i + 1]) << 16 |
90-
unbase64(src[i + 2]) << 8 |
91-
unbase64(src[i + 3]);
92-
// If MSB is set, input contains whitespace or is not valid base64.
93-
if (v & 0x80808080) {
94-
if (!base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k))
95-
return k;
96-
max_i = i + (srclen - i) / 4 * 4; // Align max_i again.
97-
} else {
98-
dst[k + 0] = ((v >> 22) & 0xFC) | ((v >> 20) & 0x03);
99-
dst[k + 1] = ((v >> 12) & 0xF0) | ((v >> 10) & 0x0F);
100-
dst[k + 2] = ((v >> 2) & 0xC0) | ((v >> 0) & 0x3F);
101-
i += 4;
102-
k += 3;
103-
}
104-
}
105-
if (i < srclen && k < dstlen) {
106-
base64_decode_group_slow(dst, dstlen, src, srclen, &i, &k);
107-
}
108-
return k;
109-
}
110-
111-
11240
template <typename TypeName>
11341
size_t base64_decode(char* const dst, const size_t dstlen,
11442
const TypeName* const src, const size_t srclen) {

src/cares_wrap.cc

+6-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define CARES_STATICLIB
2323
#include "ares.h"
2424
#include "async_wrap-inl.h"
25+
#include "base64-inl.h"
2526
#include "env-inl.h"
2627
#include "memory_tracker-inl.h"
2728
#include "node.h"
@@ -79,13 +80,6 @@ inline uint16_t cares_get_16bit(const unsigned char* p) {
7980
return static_cast<uint32_t>(p[0] << 8U) | (static_cast<uint32_t>(p[1]));
8081
}
8182

82-
inline uint32_t cares_get_32bit(const unsigned char* p) {
83-
return static_cast<uint32_t>(p[0] << 24U) |
84-
static_cast<uint32_t>(p[1] << 16U) |
85-
static_cast<uint32_t>(p[2] << 8U) |
86-
static_cast<uint32_t>(p[3]);
87-
}
88-
8983
const int ns_t_cname_or_a = -1;
9084

9185
#define DNS_ESETSRVPENDING -1000
@@ -1127,11 +1121,11 @@ int ParseSoaReply(Environment* env,
11271121
return ARES_EBADRESP;
11281122
}
11291123

1130-
const unsigned int serial = cares_get_32bit(ptr + 0 * 4);
1131-
const unsigned int refresh = cares_get_32bit(ptr + 1 * 4);
1132-
const unsigned int retry = cares_get_32bit(ptr + 2 * 4);
1133-
const unsigned int expire = cares_get_32bit(ptr + 3 * 4);
1134-
const unsigned int minttl = cares_get_32bit(ptr + 4 * 4);
1124+
const unsigned int serial = ReadUint32BE(ptr + 0 * 4);
1125+
const unsigned int refresh = ReadUint32BE(ptr + 1 * 4);
1126+
const unsigned int retry = ReadUint32BE(ptr + 2 * 4);
1127+
const unsigned int expire = ReadUint32BE(ptr + 3 * 4);
1128+
const unsigned int minttl = ReadUint32BE(ptr + 4 * 4);
11351129

11361130
Local<Object> soa_record = Object::New(env->isolate());
11371131
soa_record->Set(context,

src/node_sockaddr.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "node_sockaddr-inl.h" // NOLINT(build/include)
22
#include "env-inl.h"
3+
#include "base64-inl.h"
34
#include "base_object-inl.h"
45
#include "memory_tracker-inl.h"
56
#include "uv.h"
@@ -302,7 +303,7 @@ bool in_network_ipv6_ipv4(
302303
return false;
303304

304305
ptr += sizeof(mask);
305-
uint32_t check = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
306+
uint32_t check = ReadUint32BE(ptr);
306307

307308
return (check & m) == (htonl(net_in->sin_addr.s_addr) & m);
308309
}

0 commit comments

Comments
 (0)