Fix big-endian encodings for fixed packed arrays

Previously the encoder would serialize the output buffer in one copy,
but this only works on little-endian systems. For big-endian systems, we
now iterate through the array and convert each value if necessary before
serializing it out.

Closes https://github.com/protocolbuffers/upb/issues/436
pull/13171/head
Stan Hu 3 years ago
parent 0cb7f72c02
commit 4e8267e7e3
No known key found for this signature in database
GPG Key ID: 8D3931AD39CC7A20
  1. 19
      upb/encode.c

@ -171,10 +171,23 @@ static void encode_fixedarray(upb_encstate *e, const upb_array *arr,
size_t bytes = arr->len * elem_size;
const char* data = _upb_array_constptr(arr);
const char* ptr = data + bytes - elem_size;
if (tag) {
if (tag || !_upb_isle()) {
while (true) {
encode_bytes(e, ptr, elem_size);
encode_varint(e, tag);
if (elem_size == 4) {
uint32_t val;
memcpy(&val, ptr, sizeof(val));
val = _upb_be_swap32(val);
encode_bytes(e, &val, elem_size);
} else {
UPB_ASSERT(elem_size == 8);
uint64_t val;
memcpy(&val, ptr, sizeof(val));
val = _upb_be_swap64(val);
encode_bytes(e, &val, elem_size);
}
if (tag) encode_varint(e, tag);
if (ptr == data) break;
ptr -= elem_size;
}

Loading…
Cancel
Save