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