From f396f7cb9fc6d8fcd897f3d88c05721642db6b46 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Fri, 26 Jul 2024 11:41:38 -0700 Subject: [PATCH] Replace preprocessor macros with `ToHost` from endian PiperOrigin-RevId: 656475785 --- conformance/BUILD.bazel | 1 + src/google/protobuf/BUILD.bazel | 18 +++++++++++++++++- src/google/protobuf/io/BUILD.bazel | 1 + src/google/protobuf/io/coded_stream.h | 27 +++------------------------ 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/conformance/BUILD.bazel b/conformance/BUILD.bazel index 5a6c271856..e402498e37 100644 --- a/conformance/BUILD.bazel +++ b/conformance/BUILD.bazel @@ -143,6 +143,7 @@ cc_library( ":conformance_cc_proto", "//src/google/protobuf", "//src/google/protobuf:descriptor_legacy", + "//src/google/protobuf:endian", "//src/google/protobuf:protobuf_lite", "//src/google/protobuf/util:differencer", "//src/google/protobuf/util:json_util", diff --git a/src/google/protobuf/BUILD.bazel b/src/google/protobuf/BUILD.bazel index 38c088e092..0321694748 100644 --- a/src/google/protobuf/BUILD.bazel +++ b/src/google/protobuf/BUILD.bazel @@ -342,7 +342,7 @@ cc_library( "//src/google/protobuf:__subpackages__", ], deps = [ - "//src/google/protobuf:port", + ":port", "//src/google/protobuf/stubs:lite", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/log:absl_check", @@ -479,6 +479,7 @@ cc_library( "repeated_ptr_field.cc", "wire_format_lite.cc", ], + # TODO Fix ODR violations across BUILD.bazel files. hdrs = [ "any.h", "arena.h", @@ -527,6 +528,7 @@ cc_library( ":arena_align", ":arena_allocation_policy", ":arena_cleanup", + ":endian", ":internal_visibility", ":port", ":string_block", @@ -2066,3 +2068,17 @@ filegroup( ], visibility = ["//pkg:__pkg__"], ) + +cc_library( + name = "endian", + hdrs = ["endian.h"], + strip_include_prefix = "/src", + visibility = [ + "//conformance:__subpackages__", + "//src/google/protobuf:__subpackages__", + ], + deps = [ + ":port", + "@com_google_absl//absl/base:config", + ], +) diff --git a/src/google/protobuf/io/BUILD.bazel b/src/google/protobuf/io/BUILD.bazel index 096ebf71b8..e003c65f89 100644 --- a/src/google/protobuf/io/BUILD.bazel +++ b/src/google/protobuf/io/BUILD.bazel @@ -27,6 +27,7 @@ cc_library( deps = [ ":io_win32", "//src/google/protobuf:arena", + "//src/google/protobuf:endian", "//src/google/protobuf:port", "//src/google/protobuf/stubs:lite", "@com_google_absl//absl/base", diff --git a/src/google/protobuf/io/coded_stream.h b/src/google/protobuf/io/coded_stream.h index 4a5ca80cf8..3a40dedd80 100644 --- a/src/google/protobuf/io/coded_stream.h +++ b/src/google/protobuf/io/coded_stream.h @@ -111,9 +111,9 @@ #include "absl/numeric/bits.h" #include "absl/strings/cord.h" #include "absl/strings/string_view.h" +#include "google/protobuf/endian.h" #include "google/protobuf/port.h" - // Must be included last. #include "google/protobuf/port_def.inc" @@ -1324,37 +1324,16 @@ inline bool CodedInputStream::ReadVarintSizeAsInt(int* value) { // static inline const uint8_t* CodedInputStream::ReadLittleEndian32FromArray( const uint8_t* buffer, uint32_t* value) { -#if defined(ABSL_IS_LITTLE_ENDIAN) && \ - !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) memcpy(value, buffer, sizeof(*value)); + *value = google::protobuf::internal::little_endian::ToHost(*value); return buffer + sizeof(*value); -#else - *value = (static_cast(buffer[0])) | - (static_cast(buffer[1]) << 8) | - (static_cast(buffer[2]) << 16) | - (static_cast(buffer[3]) << 24); - return buffer + sizeof(*value); -#endif } // static inline const uint8_t* CodedInputStream::ReadLittleEndian64FromArray( const uint8_t* buffer, uint64_t* value) { -#if defined(ABSL_IS_LITTLE_ENDIAN) && \ - !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) memcpy(value, buffer, sizeof(*value)); + *value = google::protobuf::internal::little_endian::ToHost(*value); return buffer + sizeof(*value); -#else - uint32_t part0 = (static_cast(buffer[0])) | - (static_cast(buffer[1]) << 8) | - (static_cast(buffer[2]) << 16) | - (static_cast(buffer[3]) << 24); - uint32_t part1 = (static_cast(buffer[4])) | - (static_cast(buffer[5]) << 8) | - (static_cast(buffer[6]) << 16) | - (static_cast(buffer[7]) << 24); - *value = static_cast(part0) | (static_cast(part1) << 32); - return buffer + sizeof(*value); -#endif } inline bool CodedInputStream::ReadLittleEndian32(uint32_t* value) {