Skip to content

Commit 754e425

Browse files
committed
crypto: Add missing WriteBE16 function
Also add fuzz test, mimicing the WriteLE16 one.
1 parent 33adc75 commit 754e425

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/crypto/common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ uint64_t static inline ReadBE64(const unsigned char* ptr)
7070
return be64toh_internal(x);
7171
}
7272

73+
void static inline WriteBE16(unsigned char* ptr, uint16_t x)
74+
{
75+
uint16_t v = htobe16_internal(x);
76+
memcpy(ptr, &v, 2);
77+
}
78+
7379
void static inline WriteBE32(unsigned char* ptr, uint32_t x)
7480
{
7581
uint32_t v = htobe32_internal(x);

src/test/fuzz/crypto_common.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ FUZZ_TARGET(crypto_common)
3535
WriteLE64(writele64_arr.data(), random_u64);
3636
assert(ReadLE64(writele64_arr.data()) == random_u64);
3737

38+
std::array<uint8_t, 2> writebe16_arr;
39+
WriteBE16(writebe16_arr.data(), random_u16);
40+
assert(ReadBE16(writebe16_arr.data()) == random_u16);
41+
3842
std::array<uint8_t, 4> writebe32_arr;
3943
WriteBE32(writebe32_arr.data(), random_u32);
4044
assert(ReadBE32(writebe32_arr.data()) == random_u32);

0 commit comments

Comments
 (0)