OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_BIG_ENDIAN_H_ | 5 #ifndef BASE_BIG_ENDIAN_H_ |
6 #define BASE_BIG_ENDIAN_H_ | 6 #define BASE_BIG_ENDIAN_H_ |
7 | 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
8 #include "base/base_export.h" | 11 #include "base/base_export.h" |
9 #include "base/basictypes.h" | |
10 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
11 | 13 |
12 namespace base { | 14 namespace base { |
13 | 15 |
14 // Read an integer (signed or unsigned) from |buf| in Big Endian order. | 16 // Read an integer (signed or unsigned) from |buf| in Big Endian order. |
15 // Note: this loop is unrolled with -O1 and above. | 17 // Note: this loop is unrolled with -O1 and above. |
16 // NOTE(szym): glibc dns-canon.c and SpdyFrameBuilder use | 18 // NOTE(szym): glibc dns-canon.c and SpdyFrameBuilder use |
17 // ntohs(*(uint16_t*)ptr) which is potentially unaligned. | 19 // ntohs(*(uint16_t*)ptr) which is potentially unaligned. |
18 // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M. | 20 // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M. |
19 template<typename T> | 21 template<typename T> |
20 inline void ReadBigEndian(const char buf[], T* out) { | 22 inline void ReadBigEndian(const char buf[], T* out) { |
21 *out = buf[0]; | 23 *out = buf[0]; |
22 for (size_t i = 1; i < sizeof(T); ++i) { | 24 for (size_t i = 1; i < sizeof(T); ++i) { |
23 *out <<= 8; | 25 *out <<= 8; |
24 // Must cast to uint8 to avoid clobbering by sign extension. | 26 // Must cast to uint8_t to avoid clobbering by sign extension. |
25 *out |= static_cast<uint8>(buf[i]); | 27 *out |= static_cast<uint8_t>(buf[i]); |
26 } | 28 } |
27 } | 29 } |
28 | 30 |
29 // Write an integer (signed or unsigned) |val| to |buf| in Big Endian order. | 31 // Write an integer (signed or unsigned) |val| to |buf| in Big Endian order. |
30 // Note: this loop is unrolled with -O1 and above. | 32 // Note: this loop is unrolled with -O1 and above. |
31 template<typename T> | 33 template<typename T> |
32 inline void WriteBigEndian(char buf[], T val) { | 34 inline void WriteBigEndian(char buf[], T val) { |
33 for (size_t i = 0; i < sizeof(T); ++i) { | 35 for (size_t i = 0; i < sizeof(T); ++i) { |
34 buf[sizeof(T)-i-1] = static_cast<char>(val & 0xFF); | 36 buf[sizeof(T)-i-1] = static_cast<char>(val & 0xFF); |
35 val >>= 8; | 37 val >>= 8; |
36 } | 38 } |
37 } | 39 } |
38 | 40 |
39 // Specializations to make clang happy about the (dead code) shifts above. | 41 // Specializations to make clang happy about the (dead code) shifts above. |
40 template<> | 42 template <> |
41 inline void ReadBigEndian<uint8>(const char buf[], uint8* out) { | 43 inline void ReadBigEndian<uint8_t>(const char buf[], uint8_t* out) { |
42 *out = buf[0]; | 44 *out = buf[0]; |
43 } | 45 } |
44 | 46 |
45 template<> | 47 template <> |
46 inline void WriteBigEndian<uint8>(char buf[], uint8 val) { | 48 inline void WriteBigEndian<uint8_t>(char buf[], uint8_t val) { |
47 buf[0] = static_cast<char>(val); | 49 buf[0] = static_cast<char>(val); |
48 } | 50 } |
49 | 51 |
50 // Allows reading integers in network order (big endian) while iterating over | 52 // Allows reading integers in network order (big endian) while iterating over |
51 // an underlying buffer. All the reading functions advance the internal pointer. | 53 // an underlying buffer. All the reading functions advance the internal pointer. |
52 class BASE_EXPORT BigEndianReader { | 54 class BASE_EXPORT BigEndianReader { |
53 public: | 55 public: |
54 BigEndianReader(const char* buf, size_t len); | 56 BigEndianReader(const char* buf, size_t len); |
55 | 57 |
56 const char* ptr() const { return ptr_; } | 58 const char* ptr() const { return ptr_; } |
57 int remaining() const { return end_ - ptr_; } | 59 int remaining() const { return end_ - ptr_; } |
58 | 60 |
59 bool Skip(size_t len); | 61 bool Skip(size_t len); |
60 bool ReadBytes(void* out, size_t len); | 62 bool ReadBytes(void* out, size_t len); |
61 // Creates a StringPiece in |out| that points to the underlying buffer. | 63 // Creates a StringPiece in |out| that points to the underlying buffer. |
62 bool ReadPiece(base::StringPiece* out, size_t len); | 64 bool ReadPiece(base::StringPiece* out, size_t len); |
63 bool ReadU8(uint8* value); | 65 bool ReadU8(uint8_t* value); |
64 bool ReadU16(uint16* value); | 66 bool ReadU16(uint16_t* value); |
65 bool ReadU32(uint32* value); | 67 bool ReadU32(uint32_t* value); |
66 bool ReadU64(uint64* value); | 68 bool ReadU64(uint64_t* value); |
67 | 69 |
68 private: | 70 private: |
69 // Hidden to promote type safety. | 71 // Hidden to promote type safety. |
70 template<typename T> | 72 template<typename T> |
71 bool Read(T* v); | 73 bool Read(T* v); |
72 | 74 |
73 const char* ptr_; | 75 const char* ptr_; |
74 const char* end_; | 76 const char* end_; |
75 }; | 77 }; |
76 | 78 |
77 // Allows writing integers in network order (big endian) while iterating over | 79 // Allows writing integers in network order (big endian) while iterating over |
78 // an underlying buffer. All the writing functions advance the internal pointer. | 80 // an underlying buffer. All the writing functions advance the internal pointer. |
79 class BASE_EXPORT BigEndianWriter { | 81 class BASE_EXPORT BigEndianWriter { |
80 public: | 82 public: |
81 BigEndianWriter(char* buf, size_t len); | 83 BigEndianWriter(char* buf, size_t len); |
82 | 84 |
83 char* ptr() const { return ptr_; } | 85 char* ptr() const { return ptr_; } |
84 int remaining() const { return end_ - ptr_; } | 86 int remaining() const { return end_ - ptr_; } |
85 | 87 |
86 bool Skip(size_t len); | 88 bool Skip(size_t len); |
87 bool WriteBytes(const void* buf, size_t len); | 89 bool WriteBytes(const void* buf, size_t len); |
88 bool WriteU8(uint8 value); | 90 bool WriteU8(uint8_t value); |
89 bool WriteU16(uint16 value); | 91 bool WriteU16(uint16_t value); |
90 bool WriteU32(uint32 value); | 92 bool WriteU32(uint32_t value); |
91 bool WriteU64(uint64 value); | 93 bool WriteU64(uint64_t value); |
92 | 94 |
93 private: | 95 private: |
94 // Hidden to promote type safety. | 96 // Hidden to promote type safety. |
95 template<typename T> | 97 template<typename T> |
96 bool Write(T v); | 98 bool Write(T v); |
97 | 99 |
98 char* ptr_; | 100 char* ptr_; |
99 char* end_; | 101 char* end_; |
100 }; | 102 }; |
101 | 103 |
102 } // namespace base | 104 } // namespace base |
103 | 105 |
104 #endif // BASE_BIG_ENDIAN_H_ | 106 #endif // BASE_BIG_ENDIAN_H_ |
OLD | NEW |