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