crypto: Add missing WriteBE16 function

Also add fuzz test, mimicing the WriteLE16 one.
This commit is contained in:
laanwj 2024-04-28 21:39:20 +02:00
parent 33adc7521c
commit 754e425438
2 changed files with 10 additions and 0 deletions

View File

@ -70,6 +70,12 @@ uint64_t static inline ReadBE64(const unsigned char* ptr)
return be64toh_internal(x);
}
void static inline WriteBE16(unsigned char* ptr, uint16_t x)
{
uint16_t v = htobe16_internal(x);
memcpy(ptr, &v, 2);
}
void static inline WriteBE32(unsigned char* ptr, uint32_t x)
{
uint32_t v = htobe32_internal(x);

View File

@ -35,6 +35,10 @@ FUZZ_TARGET(crypto_common)
WriteLE64(writele64_arr.data(), random_u64);
assert(ReadLE64(writele64_arr.data()) == random_u64);
std::array<uint8_t, 2> writebe16_arr;
WriteBE16(writebe16_arr.data(), random_u16);
assert(ReadBE16(writebe16_arr.data()) == random_u16);
std::array<uint8_t, 4> writebe32_arr;
WriteBE32(writebe32_arr.data(), random_u32);
assert(ReadBE32(writebe32_arr.data()) == random_u32);