Move quic::QuicEndian to quiche::QuicheEndian
This change will enable us to reuse the endian library in new third_party repos without introducing a dependency on //third_party/quic.
gfe-relnote: Refactor of QUIC Endian library
PiperOrigin-RevId: 281406510
Change-Id: If1b72425a799bad7469281f32a12d6630fe787c4
diff --git a/common/platform/api/quiche_endian.h b/common/platform/api/quiche_endian.h
new file mode 100644
index 0000000..f8a9ee6
--- /dev/null
+++ b/common/platform/api/quiche_endian.h
@@ -0,0 +1,55 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef QUICHE_COMMON_PLATFORM_API_QUICHE_ENDIAN_H_
+#define QUICHE_COMMON_PLATFORM_API_QUICHE_ENDIAN_H_
+
+#include "net/third_party/quiche/src/common/platform/api/quiche_export.h"
+#include "net/quiche/common/platform/impl/quiche_endian_impl.h"
+
+namespace quiche {
+
+enum Endianness {
+ NETWORK_BYTE_ORDER, // big endian
+ HOST_BYTE_ORDER // little endian
+};
+
+// Provide utility functions that convert from/to network order (big endian)
+// to/from host order (can be either little or big endian depending on the
+// platform).
+class QUICHE_EXPORT_PRIVATE QuicheEndian {
+ public:
+ // Convert |x| from host order (can be either little or big endian depending
+ // on the platform) to network order (big endian).
+ static uint16_t HostToNet16(uint16_t x) {
+ return QuicheEndianImpl::HostToNet16(x);
+ }
+ static uint32_t HostToNet32(uint32_t x) {
+ return QuicheEndianImpl::HostToNet32(x);
+ }
+ static uint64_t HostToNet64(uint64_t x) {
+ return QuicheEndianImpl::HostToNet64(x);
+ }
+
+ // Convert |x| from network order (big endian) to host order (can be either
+ // little or big endian depending on the platform).
+ static uint16_t NetToHost16(uint16_t x) {
+ return QuicheEndianImpl::NetToHost16(x);
+ }
+ static uint32_t NetToHost32(uint32_t x) {
+ return QuicheEndianImpl::NetToHost32(x);
+ }
+ static uint64_t NetToHost64(uint64_t x) {
+ return QuicheEndianImpl::NetToHost64(x);
+ }
+
+ // Returns true if current host order is little endian.
+ static bool HostIsLittleEndian() {
+ return QuicheEndianImpl::HostIsLittleEndian();
+ }
+};
+
+} // namespace quiche
+
+#endif // QUICHE_COMMON_PLATFORM_API_QUICHE_ENDIAN_H_
diff --git a/common/platform/api/quiche_endian_test.cc b/common/platform/api/quiche_endian_test.cc
new file mode 100644
index 0000000..98e16fe
--- /dev/null
+++ b/common/platform/api/quiche_endian_test.cc
@@ -0,0 +1,59 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/third_party/quiche/src/common/platform/api/quiche_endian.h"
+#include "net/third_party/quiche/src/common/platform/api/quiche_test.h"
+
+namespace quiche {
+namespace test {
+namespace {
+
+const uint16_t k16BitTestData = 0xaabb;
+const uint16_t k16BitSwappedTestData = 0xbbaa;
+const uint32_t k32BitTestData = 0xaabbccdd;
+const uint32_t k32BitSwappedTestData = 0xddccbbaa;
+const uint64_t k64BitTestData = 0xaabbccdd44332211;
+const uint64_t k64BitSwappedTestData = 0x11223344ddccbbaa;
+
+class QuicheEndianTest : public QuicheTest {};
+
+TEST_F(QuicheEndianTest, HostToNet) {
+ if (quiche::QuicheEndian::HostIsLittleEndian()) {
+ EXPECT_EQ(k16BitSwappedTestData,
+ quiche::QuicheEndian::HostToNet16(k16BitTestData));
+ EXPECT_EQ(k32BitSwappedTestData,
+ quiche::QuicheEndian::HostToNet32(k32BitTestData));
+ EXPECT_EQ(k64BitSwappedTestData,
+ quiche::QuicheEndian::HostToNet64(k64BitTestData));
+ } else {
+ EXPECT_EQ(k16BitTestData,
+ quiche::QuicheEndian::HostToNet16(k16BitTestData));
+ EXPECT_EQ(k32BitTestData,
+ quiche::QuicheEndian::HostToNet32(k32BitTestData));
+ EXPECT_EQ(k64BitTestData,
+ quiche::QuicheEndian::HostToNet64(k64BitTestData));
+ }
+}
+
+TEST_F(QuicheEndianTest, NetToHost) {
+ if (quiche::QuicheEndian::HostIsLittleEndian()) {
+ EXPECT_EQ(k16BitTestData,
+ quiche::QuicheEndian::NetToHost16(k16BitSwappedTestData));
+ EXPECT_EQ(k32BitTestData,
+ quiche::QuicheEndian::NetToHost32(k32BitSwappedTestData));
+ EXPECT_EQ(k64BitTestData,
+ quiche::QuicheEndian::NetToHost64(k64BitSwappedTestData));
+ } else {
+ EXPECT_EQ(k16BitSwappedTestData,
+ quiche::QuicheEndian::NetToHost16(k16BitSwappedTestData));
+ EXPECT_EQ(k32BitSwappedTestData,
+ quiche::QuicheEndian::NetToHost32(k32BitSwappedTestData));
+ EXPECT_EQ(k64BitSwappedTestData,
+ quiche::QuicheEndian::NetToHost64(k64BitSwappedTestData));
+ }
+}
+
+} // namespace
+} // namespace test
+} // namespace quiche
diff --git a/common/platform/api/quiche_export.h b/common/platform/api/quiche_export.h
new file mode 100644
index 0000000..22cc1f9
--- /dev/null
+++ b/common/platform/api/quiche_export.h
@@ -0,0 +1,17 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef THIRD_PARTY_QUICHE_PLATFORM_API_QUICHE_EXPORT_H_
+#define THIRD_PARTY_QUICHE_PLATFORM_API_QUICHE_EXPORT_H_
+
+#include "net/quiche/common/platform/impl/quiche_export_impl.h"
+
+// quiche_export_impl.h defines the following macros:
+// - QUICHE_EXPORT is not meant to be used.
+// - QUICHE_EXPORT_PRIVATE is meant for QUICHE functionality that is built in
+// Chromium as part of //net, and not fully contained in headers.
+// - QUICHE_NO_EXPORT is meant for QUICHE functionality that is either fully
+// defined in a header, or is built in Chromium as part of tests or tools.
+
+#endif // THIRD_PARTY_QUICHE_PLATFORM_API_QUICHE_EXPORT_H_