big_endian.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_BIG_ENDIAN_H_
  5. #define BASE_BIG_ENDIAN_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <type_traits>
  9. #include "base/base_export.h"
  10. #include "base/containers/span.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/strings/string_piece.h"
  13. namespace base {
  14. // Read an integer (signed or unsigned) from |buf| in Big Endian order.
  15. // Note: this loop is unrolled with -O1 and above.
  16. // NOTE(szym): glibc dns-canon.c use ntohs(*(uint16_t*)ptr) which is
  17. // potentially unaligned.
  18. // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M.
  19. template <typename T>
  20. inline void ReadBigEndian(const uint8_t buf[], T* out) {
  21. static_assert(std::is_integral<T>::value, "T has to be an integral type.");
  22. // Make an unsigned version of the output type to make shift possible
  23. // without UB.
  24. typename std::make_unsigned<T>::type unsigned_result = buf[0];
  25. for (size_t i = 1; i < sizeof(T); ++i) {
  26. unsigned_result <<= 8;
  27. // Must cast to uint8_t to avoid clobbering by sign extension.
  28. unsigned_result |= buf[i];
  29. }
  30. *out = unsigned_result;
  31. }
  32. // Write an integer (signed or unsigned) |val| to |buf| in Big Endian order.
  33. // Note: this loop is unrolled with -O1 and above.
  34. template<typename T>
  35. inline void WriteBigEndian(char buf[], T val) {
  36. static_assert(std::is_integral<T>::value, "T has to be an integral type.");
  37. auto unsigned_val = static_cast<typename std::make_unsigned<T>::type>(val);
  38. for (size_t i = 0; i < sizeof(T); ++i) {
  39. buf[sizeof(T) - i - 1] = static_cast<char>(unsigned_val & 0xFF);
  40. unsigned_val >>= 8;
  41. }
  42. }
  43. // Specializations to make clang happy about the (dead code) shifts above.
  44. template <>
  45. inline void ReadBigEndian<uint8_t>(const uint8_t buf[], uint8_t* out) {
  46. *out = buf[0];
  47. }
  48. template <>
  49. inline void WriteBigEndian<uint8_t>(char buf[], uint8_t val) {
  50. buf[0] = static_cast<char>(val);
  51. }
  52. template <>
  53. inline void ReadBigEndian<int8_t>(const uint8_t buf[], int8_t* out) {
  54. *out = static_cast<int8_t>(buf[0]);
  55. }
  56. template <>
  57. inline void WriteBigEndian<int8_t>(char buf[], int8_t val) {
  58. buf[0] = static_cast<char>(val);
  59. }
  60. // Allows reading integers in network order (big endian) while iterating over
  61. // an underlying buffer. All the reading functions advance the internal pointer.
  62. class BASE_EXPORT BigEndianReader {
  63. public:
  64. static BigEndianReader FromStringPiece(base::StringPiece string_piece);
  65. BigEndianReader(const uint8_t* buf, size_t len);
  66. explicit BigEndianReader(base::span<const uint8_t> buf);
  67. const uint8_t* ptr() const { return ptr_; }
  68. size_t remaining() const { return static_cast<size_t>(end_ - ptr_); }
  69. bool Skip(size_t len);
  70. bool ReadBytes(void* out, size_t len);
  71. // Creates a StringPiece in |out| that points to the underlying buffer.
  72. bool ReadPiece(base::StringPiece* out, size_t len);
  73. bool ReadSpan(base::span<const uint8_t>* out, size_t len);
  74. bool ReadU8(uint8_t* value);
  75. bool ReadU16(uint16_t* value);
  76. bool ReadU32(uint32_t* value);
  77. bool ReadU64(uint64_t* value);
  78. // Reads a length-prefixed region:
  79. // 1. reads a big-endian length L from the buffer;
  80. // 2. sets |*out| to a StringPiece over the next L many bytes
  81. // of the buffer (beyond the end of the bytes encoding the length); and
  82. // 3. skips the main reader past this L-byte substring.
  83. //
  84. // Fails if reading a U8 or U16 fails, or if the parsed length is greater
  85. // than the number of bytes remaining in the stream.
  86. //
  87. // On failure, leaves the stream at the same position
  88. // as before the call.
  89. bool ReadU8LengthPrefixed(base::StringPiece* out);
  90. bool ReadU16LengthPrefixed(base::StringPiece* out);
  91. private:
  92. // Hidden to promote type safety.
  93. template<typename T>
  94. bool Read(T* v);
  95. template <typename T>
  96. bool ReadLengthPrefixed(base::StringPiece* out);
  97. const uint8_t* ptr_;
  98. const uint8_t* end_;
  99. };
  100. // Allows writing integers in network order (big endian) while iterating over
  101. // an underlying buffer. All the writing functions advance the internal pointer.
  102. class BASE_EXPORT BigEndianWriter {
  103. public:
  104. BigEndianWriter(char* buf, size_t len);
  105. char* ptr() const { return ptr_; }
  106. size_t remaining() const { return static_cast<size_t>(end_ - ptr_); }
  107. bool Skip(size_t len);
  108. bool WriteBytes(const void* buf, size_t len);
  109. bool WriteU8(uint8_t value);
  110. bool WriteU16(uint16_t value);
  111. bool WriteU32(uint32_t value);
  112. bool WriteU64(uint64_t value);
  113. private:
  114. // Hidden to promote type safety.
  115. template<typename T>
  116. bool Write(T v);
  117. // TODO(crbug.com/1298696): Breaks net_unittests.
  118. raw_ptr<char, DanglingUntriagedDegradeToNoOpWhenMTE> ptr_;
  119. raw_ptr<char, DanglingUntriagedDegradeToNoOpWhenMTE> end_;
  120. };
  121. } // namespace base
  122. #endif // BASE_BIG_ENDIAN_H_