Replace preprocessor macros with `ToHost` from endian

PiperOrigin-RevId: 656475785
pull/17621/head
Protobuf Team Bot 4 months ago committed by Copybara-Service
parent 7141c30425
commit f396f7cb9f
  1. 1
      conformance/BUILD.bazel
  2. 18
      src/google/protobuf/BUILD.bazel
  3. 1
      src/google/protobuf/io/BUILD.bazel
  4. 27
      src/google/protobuf/io/coded_stream.h

@ -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",

@ -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",
],
)

@ -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",

@ -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<uint32_t>(buffer[0])) |
(static_cast<uint32_t>(buffer[1]) << 8) |
(static_cast<uint32_t>(buffer[2]) << 16) |
(static_cast<uint32_t>(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<uint32_t>(buffer[0])) |
(static_cast<uint32_t>(buffer[1]) << 8) |
(static_cast<uint32_t>(buffer[2]) << 16) |
(static_cast<uint32_t>(buffer[3]) << 24);
uint32_t part1 = (static_cast<uint32_t>(buffer[4])) |
(static_cast<uint32_t>(buffer[5]) << 8) |
(static_cast<uint32_t>(buffer[6]) << 16) |
(static_cast<uint32_t>(buffer[7]) << 24);
*value = static_cast<uint64_t>(part0) | (static_cast<uint64_t>(part1) << 32);
return buffer + sizeof(*value);
#endif
}
inline bool CodedInputStream::ReadLittleEndian32(uint32_t* value) {

Loading…
Cancel
Save