big_endian.h 4.3 KB

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