upb: lock down the internal headers for upb:wire_reader

PiperOrigin-RevId: 595002227
pull/15239/head
Eric Salo 11 months ago committed by Copybara-Service
parent cb8a31ee3d
commit 4f1eba857a
  1. 1
      upb/base/BUILD
  2. 21
      upb/base/internal/endian.h
  3. 1
      upb/util/BUILD
  4. 6
      upb/util/compare_test.cc
  5. 5
      upb/wire/BUILD
  6. 8
      upb/wire/decode.c
  7. 14
      upb/wire/encode.c
  8. 11
      upb/wire/internal/decode_fast.h
  9. 6
      upb/wire/reader.h

@ -29,6 +29,7 @@ cc_library(
cc_library(
name = "internal",
hdrs = [
"internal/endian.h",
"internal/log2.h",
],
copts = UPB_DEFAULT_COPTS,

@ -5,8 +5,8 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_WIRE_INTERNAL_ENDIAN_H_
#define UPB_WIRE_INTERNAL_ENDIAN_H_
#ifndef UPB_BASE_INTERNAL_ENDIAN_H_
#define UPB_BASE_INTERNAL_ENDIAN_H_
#include <stdint.h>
@ -17,23 +17,24 @@
extern "C" {
#endif
UPB_INLINE bool UPB_PRIVATE(_upb_IsLittleEndian)(void) {
UPB_INLINE bool upb_IsLittleEndian(void) {
const int x = 1;
return *(char*)&x == 1;
}
UPB_INLINE uint32_t UPB_PRIVATE(_upb_BigEndian32)(uint32_t val) {
if (UPB_PRIVATE(_upb_IsLittleEndian)()) return val;
UPB_INLINE uint32_t upb_BigEndian32(uint32_t val) {
if (upb_IsLittleEndian()) return val;
return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
}
UPB_INLINE uint64_t UPB_PRIVATE(_upb_BigEndian64)(uint64_t val) {
if (UPB_PRIVATE(_upb_IsLittleEndian)()) return val;
UPB_INLINE uint64_t upb_BigEndian64(uint64_t val) {
if (upb_IsLittleEndian()) return val;
return ((uint64_t)UPB_PRIVATE(_upb_BigEndian32)((uint32_t)val) << 32) |
UPB_PRIVATE(_upb_BigEndian32)((uint32_t)(val >> 32));
const uint64_t hi = ((uint64_t)upb_BigEndian32((uint32_t)val)) << 32;
const uint64_t lo = upb_BigEndian32((uint32_t)(val >> 32));
return hi | lo;
}
#ifdef __cplusplus
@ -42,4 +43,4 @@ UPB_INLINE uint64_t UPB_PRIVATE(_upb_BigEndian64)(uint64_t val) {
#include "upb/port/undef.inc"
#endif /* UPB_WIRE_INTERNAL_ENDIAN_H_ */
#endif /* UPB_BASE_INTERNAL_ENDIAN_H_ */

@ -165,6 +165,7 @@ cc_test(
":compare",
"//upb:port",
"//upb:wire_reader",
"//upb/base:internal",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",

@ -15,7 +15,7 @@
#include <vector>
#include <gtest/gtest.h>
#include "upb/wire/internal/endian.h"
#include "upb/base/internal/endian.h"
#include "upb/wire/types.h"
// Must be last.
@ -84,11 +84,11 @@ std::string ToBinaryPayload(const UnknownFields& fields) {
ret.append(val->val);
} else if (const auto* val = std::get_if<Fixed64>(&field.value)) {
EncodeVarint(field.field_number << 3 | kUpb_WireType_64Bit, &ret);
uint64_t swapped = UPB_PRIVATE(_upb_BigEndian64)(val->val);
uint64_t swapped = upb_BigEndian64(val->val);
ret.append(reinterpret_cast<const char*>(&swapped), sizeof(swapped));
} else if (const auto* val = std::get_if<Fixed32>(&field.value)) {
EncodeVarint(field.field_number << 3 | kUpb_WireType_32Bit, &ret);
uint32_t swapped = UPB_PRIVATE(_upb_BigEndian32)(val->val);
uint32_t swapped = upb_BigEndian32(val->val);
ret.append(reinterpret_cast<const char*>(&swapped), sizeof(swapped));
} else if (const auto* val = std::get_if<Group>(&field.value)) {
EncodeVarint(field.field_number << 3 | kUpb_WireType_StartGroup, &ret);

@ -33,6 +33,7 @@ cc_library(
"//upb:message_accessors",
"//upb:mini_table",
"//upb:port",
"//upb/base:internal",
"//upb/hash",
"//upb/mem:internal",
"//upb/message:internal",
@ -43,11 +44,10 @@ cc_library(
cc_library(
name = "reader",
srcs = [
"internal/reader.h",
"reader.c",
],
hdrs = [
"internal/endian.h",
"internal/reader.h",
"reader.h",
"types.h",
],
@ -55,6 +55,7 @@ cc_library(
deps = [
":eps_copy_input_stream",
"//upb:port",
"//upb/base:internal",
],
)

@ -14,6 +14,7 @@
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/mem/arena.h"
@ -42,7 +43,6 @@
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/internal/constants.h"
#include "upb/wire/internal/decoder.h"
#include "upb/wire/internal/endian.h"
#include "upb/wire/reader.h"
// Must be last.
@ -212,7 +212,7 @@ static const char* upb_Decoder_DecodeSize(upb_Decoder* d, const char* ptr,
}
static void _upb_Decoder_MungeInt32(wireval* val) {
if (!UPB_PRIVATE(_upb_IsLittleEndian)()) {
if (!upb_IsLittleEndian()) {
/* The next stage will memcpy(dst, &val, 4) */
val->uint32_val = val->uint64_val;
}
@ -434,7 +434,7 @@ static const char* _upb_Decoder_DecodeFixedPacked(
arr->UPB_PRIVATE(size) += count;
// Note: if/when the decoder supports multi-buffer input, we will need to
// handle buffer seams here.
if (UPB_PRIVATE(_upb_IsLittleEndian)()) {
if (upb_IsLittleEndian()) {
ptr = upb_EpsCopyInputStream_Copy(&d->input, ptr, mem, val->size);
} else {
int delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size);
@ -758,7 +758,7 @@ const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
}
uint64_t msg_head;
memcpy(&msg_head, msg, 8);
msg_head = UPB_PRIVATE(_upb_BigEndian64)(msg_head);
msg_head = upb_BigEndian64(msg_head);
if (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~msg_head) {
d->missing_required = true;
}

@ -15,6 +15,7 @@
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/hash/str_table.h"
@ -37,7 +38,6 @@
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/wire/internal/constants.h"
#include "upb/wire/internal/endian.h"
#include "upb/wire/types.h"
// Must be last.
@ -130,12 +130,12 @@ static void encode_bytes(upb_encstate* e, const void* data, size_t len) {
}
static void encode_fixed64(upb_encstate* e, uint64_t val) {
val = UPB_PRIVATE(_upb_BigEndian64)(val);
val = upb_BigEndian64(val);
encode_bytes(e, &val, sizeof(uint64_t));
}
static void encode_fixed32(upb_encstate* e, uint32_t val) {
val = UPB_PRIVATE(_upb_BigEndian32)(val);
val = upb_BigEndian32(val);
encode_bytes(e, &val, sizeof(uint32_t));
}
@ -186,18 +186,18 @@ static void encode_fixedarray(upb_encstate* e, const upb_Array* arr,
const char* data = _upb_array_constptr(arr);
const char* ptr = data + bytes - elem_size;
if (tag || !UPB_PRIVATE(_upb_IsLittleEndian)()) {
if (tag || !upb_IsLittleEndian()) {
while (true) {
if (elem_size == 4) {
uint32_t val;
memcpy(&val, ptr, sizeof(val));
val = UPB_PRIVATE(_upb_BigEndian32)(val);
val = upb_BigEndian32(val);
encode_bytes(e, &val, elem_size);
} else {
UPB_ASSERT(elem_size == 8);
uint64_t val;
memcpy(&val, ptr, sizeof(val));
val = UPB_PRIVATE(_upb_BigEndian64)(val);
val = upb_BigEndian64(val);
encode_bytes(e, &val, elem_size);
}
@ -552,7 +552,7 @@ static void encode_message(upb_encstate* e, const upb_Message* msg,
m->UPB_PRIVATE(required_count)) {
uint64_t msg_head;
memcpy(&msg_head, msg, 8);
msg_head = UPB_PRIVATE(_upb_BigEndian64)(msg_head);
msg_head = upb_BigEndian64(msg_head);
if (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~msg_head) {
encode_err(e, kUpb_EncodeStatus_MissingRequired);
}

@ -39,8 +39,8 @@
// - '1' for one-byte tags (field numbers 1-15)
// - '2' for two-byte tags (field numbers 16-2048)
#ifndef UPB_WIRE_DECODE_INTERNAL_FAST_H_
#define UPB_WIRE_DECODE_INTERNAL_FAST_H_
#ifndef UPB_WIRE_INTERNAL_DECODE_FAST_H_
#define UPB_WIRE_INTERNAL_DECODE_FAST_H_
#include "upb/message/message.h"
@ -110,6 +110,7 @@ TAGBYTES(o)
TAGBYTES(r)
#undef F
#undef UTF8
#undef TAGBYTES
/* sub-message fields *********************************************************/
@ -132,9 +133,9 @@ TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
#undef TAGBYTES
#undef SIZES
#undef F
#undef SIZES
#undef TAGBYTES
#undef UPB_PARSE_PARAMS
@ -144,4 +145,4 @@ TAGBYTES(r)
#include "upb/port/undef.inc"
#endif /* UPB_WIRE_DECODE_INTERNAL_FAST_H_ */
#endif /* UPB_WIRE_INTERNAL_DECODE_FAST_H_ */

@ -8,8 +8,8 @@
#ifndef UPB_WIRE_READER_H_
#define UPB_WIRE_READER_H_
#include "upb/base/internal/endian.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/internal/endian.h"
#include "upb/wire/internal/reader.h"
#include "upb/wire/types.h" // IWYU pragma: export
@ -89,7 +89,7 @@ UPB_INLINE const char* upb_WireReader_ReadSize(const char* ptr, int* size) {
UPB_INLINE const char* upb_WireReader_ReadFixed32(const char* ptr, void* val) {
uint32_t uval;
memcpy(&uval, ptr, 4);
uval = UPB_PRIVATE(_upb_BigEndian32)(uval);
uval = upb_BigEndian32(uval);
memcpy(val, &uval, 4);
return ptr + 4;
}
@ -102,7 +102,7 @@ UPB_INLINE const char* upb_WireReader_ReadFixed32(const char* ptr, void* val) {
UPB_INLINE const char* upb_WireReader_ReadFixed64(const char* ptr, void* val) {
uint64_t uval;
memcpy(&uval, ptr, 8);
uval = UPB_PRIVATE(_upb_BigEndian64)(uval);
uval = upb_BigEndian64(uval);
memcpy(val, &uval, 8);
return ptr + 8;
}

Loading…
Cancel
Save