Skip to content

Commit

Permalink
upb: tighten up and lock down upb/wire/
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 593821827
  • Loading branch information
ericsalo authored and copybara-github committed Dec 26, 2023
1 parent 16afb4d commit 2fb0b93
Show file tree
Hide file tree
Showing 21 changed files with 142 additions and 150 deletions.
2 changes: 1 addition & 1 deletion protos/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cc_library(
"//upb:message_types",
"//upb:mini_table",
"//upb:wire",
"//upb:wire_types",
"//upb:wire_reader",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
Expand Down
1 change: 0 additions & 1 deletion python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ py_extension(
"//upb:reflection",
"//upb:text",
"//upb:wire_reader",
"//upb:wire_types",
"//upb/util:compare",
"//upb/util:def_to_proto",
"//upb/util:required_fields",
Expand Down
19 changes: 0 additions & 19 deletions upb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ cc_library(
":mini_descriptor",
":mini_table",
":wire",
":wire_internal",
],
)

Expand Down Expand Up @@ -327,24 +326,12 @@ alias(
visibility = ["//visibility:public"],
)

alias(
name = "wire_internal",
actual = "//upb/wire:internal",
visibility = ["//visibility:public"],
)

alias(
name = "wire_reader",
actual = "//upb/wire:reader",
visibility = ["//visibility:public"],
)

alias(
name = "wire_types",
actual = "//upb/wire:types",
visibility = ["//visibility:public"],
)

alias(
name = "eps_copy_input_stream",
actual = "//upb/wire:eps_copy_input_stream",
Expand Down Expand Up @@ -407,9 +394,7 @@ upb_amalgamation(
":reflection",
":reflection_internal",
":wire",
":wire_internal",
":wire_reader",
":wire_types",
],
strip_import_prefix = ["src"],
)
Expand Down Expand Up @@ -455,9 +440,7 @@ upb_amalgamation(
":reflection",
":reflection_internal",
":wire",
":wire_internal",
":wire_reader",
":wire_types",
],
prefix = "php-",
strip_import_prefix = ["src"],
Expand Down Expand Up @@ -504,9 +487,7 @@ upb_amalgamation(
":reflection",
":reflection_internal",
":wire",
":wire_internal",
":wire_reader",
":wire_types",
],
prefix = "ruby-",
strip_import_prefix = ["src"],
Expand Down
2 changes: 1 addition & 1 deletion upb/generated_code_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/wire/decode.h"
#include "upb/wire/decode_fast.h"
#include "upb/wire/encode.h"
#include "upb/wire/internal/decode_fast.h"
// IWYU pragma: end_exports

#endif // UPB_GENERATED_CODE_SUPPORT_H_
2 changes: 0 additions & 2 deletions upb/message/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ cc_library(
":message",
":tagged_ptr",
"//upb:base",
"//upb:eps_copy_input_stream",
"//upb:mem",
"//upb:message_types",
"//upb:mini_table",
Expand Down Expand Up @@ -181,7 +180,6 @@ cc_library(
"//upb:mini_table",
"//upb:port",
"//upb:wire",
"//upb:wire_internal",
"//upb:wire_reader",
],
)
Expand Down
1 change: 0 additions & 1 deletion upb/mini_table/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ cc_library(
"//upb:base",
"//upb:hash",
"//upb:mem",
"//upb:message_types",
"//upb:port",
],
)
Expand Down
4 changes: 2 additions & 2 deletions upb/mini_table/internal/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <stdint.h>

#include "upb/message/types.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/sub.h"
#include "upb/mini_table/types.h"
Expand All @@ -19,8 +18,9 @@
#include "upb/port/def.inc"

struct upb_Decoder;
struct upb_Message;
typedef const char* _upb_FieldParser(struct upb_Decoder* d, const char* ptr,
upb_Message* msg, intptr_t table,
struct upb_Message* msg, intptr_t table,
uint64_t hasbits, uint64_t data);
typedef struct {
uint64_t field_data;
Expand Down
1 change: 0 additions & 1 deletion upb/text/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ cc_library(
"//upb:reflection",
"//upb:wire",
"//upb:wire_reader",
"//upb:wire_types",
],
)

Expand Down
5 changes: 2 additions & 3 deletions upb/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ cc_library(
"//upb:mem",
"//upb:port",
"//upb:wire_reader",
"//upb:wire_types",
],
)

Expand All @@ -163,8 +162,8 @@ cc_test(
deps = [
":compare",
"@com_google_googletest//:gtest_main",
"//upb:wire_internal",
"//upb:wire_types",
"//upb:port",
"//upb:wire_reader",
"@com_google_absl//absl/strings",
],
)
Expand Down
9 changes: 6 additions & 3 deletions upb/util/compare_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
#include <vector>

#include <gtest/gtest.h>
#include "upb/wire/internal/swap.h"
#include "upb/wire/internal/endian.h"
#include "upb/wire/types.h"

// Must be last.
#include "upb/port/def.inc"

struct UnknownField;

using UnknownFields = std::vector<UnknownField>;
Expand Down Expand Up @@ -81,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_BigEndian_Swap64(val->val);
uint64_t swapped = UPB_PRIVATE(_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_BigEndian_Swap32(val->val);
uint32_t swapped = UPB_PRIVATE(_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);
Expand Down
46 changes: 11 additions & 35 deletions upb/wire/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,23 @@ load("//bazel:build_defs.bzl", "UPB_DEFAULT_COPTS")

cc_library(
name = "wire",
srcs = [
],
hdrs = [
"decode.h",
"encode.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":internal",
":types",
"//upb:mem",
"//upb:message",
"//upb:mini_table",
"//upb:port",
],
)

cc_library(
name = "internal",
srcs = [
"decode.c",
"decode.h",
"decode_fast.c",
"encode.c",
"encode.h",
"internal/decode_fast.c",
],
hdrs = [
"decode_fast.h",
"decode.h",
"encode.h",
"internal/constants.h",
"internal/decode.h",
"internal/swap.h",
"internal/decode_fast.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":eps_copy_input_stream",
":reader",
":types",
"//upb:base",
"//upb:hash",
"//upb:mem",
Expand All @@ -56,6 +34,7 @@ cc_library(
"//upb:message_internal",
"//upb:message_internal_types",
"//upb:message_tagged_ptr",
"//upb:message_types",
"//upb:mini_table",
"//upb:port",
"//third_party/utf8_range",
Expand All @@ -65,24 +44,21 @@ cc_library(
cc_library(
name = "reader",
srcs = [
"internal/swap.h",
"reader.c",
],
hdrs = ["reader.h"],
hdrs = [
"internal/endian.h",
"internal/reader.h",
"reader.h",
"types.h",
],
visibility = ["//visibility:public"],
deps = [
":eps_copy_input_stream",
":types",
"//upb:port",
],
)

cc_library(
name = "types",
hdrs = ["types.h"],
visibility = ["//visibility:public"],
)

cc_library(
name = "eps_copy_input_stream",
srcs = ["eps_copy_input_stream.c"],
Expand Down
9 changes: 5 additions & 4 deletions upb/wire/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "upb/mini_table/extension.h"
#include "upb/mini_table/extension_registry.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/size_log2.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
Expand All @@ -39,7 +40,7 @@
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/internal/constants.h"
#include "upb/wire/internal/decode.h"
#include "upb/wire/internal/swap.h"
#include "upb/wire/internal/endian.h"
#include "upb/wire/reader.h"

// Must be last.
Expand Down Expand Up @@ -209,7 +210,7 @@ static const char* upb_Decoder_DecodeSize(upb_Decoder* d, const char* ptr,
}

static void _upb_Decoder_MungeInt32(wireval* val) {
if (!_upb_IsLittleEndian()) {
if (!UPB_PRIVATE(_upb_IsLittleEndian)()) {
/* The next stage will memcpy(dst, &val, 4) */
val->uint32_val = val->uint64_val;
}
Expand Down Expand Up @@ -429,7 +430,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_IsLittleEndian()) {
if (UPB_PRIVATE(_upb_IsLittleEndian)()) {
ptr = upb_EpsCopyInputStream_Copy(&d->input, ptr, mem, val->size);
} else {
int delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size);
Expand Down Expand Up @@ -753,7 +754,7 @@ const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
}
uint64_t msg_head;
memcpy(&msg_head, msg, 8);
msg_head = _upb_BigEndian_Swap64(msg_head);
msg_head = UPB_PRIVATE(_upb_BigEndian64)(msg_head);
if (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~msg_head) {
d->missing_required = true;
}
Expand Down
15 changes: 8 additions & 7 deletions upb/wire/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
#include "upb/message/tagged_ptr.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/wire/internal/constants.h"
#include "upb/wire/internal/swap.h"
#include "upb/wire/internal/endian.h"
#include "upb/wire/types.h"

// Must be last.
Expand Down Expand Up @@ -127,12 +128,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_BigEndian_Swap64(val);
val = UPB_PRIVATE(_upb_BigEndian64)(val);
encode_bytes(e, &val, sizeof(uint64_t));
}

static void encode_fixed32(upb_encstate* e, uint32_t val) {
val = _upb_BigEndian_Swap32(val);
val = UPB_PRIVATE(_upb_BigEndian32)(val);
encode_bytes(e, &val, sizeof(uint32_t));
}

Expand Down Expand Up @@ -183,18 +184,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_IsLittleEndian()) {
if (tag || !UPB_PRIVATE(_upb_IsLittleEndian)()) {
while (true) {
if (elem_size == 4) {
uint32_t val;
memcpy(&val, ptr, sizeof(val));
val = _upb_BigEndian_Swap32(val);
val = UPB_PRIVATE(_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_BigEndian_Swap64(val);
val = UPB_PRIVATE(_upb_BigEndian64)(val);
encode_bytes(e, &val, elem_size);
}

Expand Down Expand Up @@ -548,7 +549,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_BigEndian_Swap64(msg_head);
msg_head = UPB_PRIVATE(_upb_BigEndian64)(msg_head);
if (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~msg_head) {
encode_err(e, kUpb_EncodeStatus_MissingRequired);
}
Expand Down
1 change: 1 addition & 0 deletions upb/wire/encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <stdint.h>

#include "upb/mem/arena.h"
#include "upb/message/types.h"
#include "upb/mini_table/message.h"

// Must be last.
Expand Down
2 changes: 1 addition & 1 deletion upb/wire/decode_fast.c → upb/wire/internal/decode_fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// field type (eg. oneof boolean field with a 1 byte tag) and then dispatch
// to the specialized function as quickly as possible.

#include "upb/wire/decode_fast.h"
#include "upb/wire/internal/decode_fast.h"

#include "upb/message/array.h"
#include "upb/message/internal/array.h"
Expand Down
Loading

0 comments on commit 2fb0b93

Please sign in to comment.