| 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> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 // 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. |
| 17 // Note: this loop is unrolled with -O1 and above. | 17 // Note: this loop is unrolled with -O1 and above. |
| 18 // NOTE(szym): glibc dns-canon.c and SpdyFrameReader use | 18 // NOTE(szym): glibc dns-canon.c use ntohs(*(uint16_t*)ptr) which is |
| 19 // ntohs(*(uint16_t*)ptr) which is potentially unaligned. | 19 // potentially unaligned. |
| 20 // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M. | 20 // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M. |
| 21 template<typename T> | 21 template<typename T> |
| 22 inline void ReadBigEndian(const char buf[], T* out) { | 22 inline void ReadBigEndian(const char buf[], T* out) { |
| 23 *out = buf[0]; | 23 *out = buf[0]; |
| 24 for (size_t i = 1; i < sizeof(T); ++i) { | 24 for (size_t i = 1; i < sizeof(T); ++i) { |
| 25 *out <<= 8; | 25 *out <<= 8; |
| 26 // Must cast to uint8_t to avoid clobbering by sign extension. | 26 // Must cast to uint8_t to avoid clobbering by sign extension. |
| 27 *out |= static_cast<uint8_t>(buf[i]); | 27 *out |= static_cast<uint8_t>(buf[i]); |
| 28 } | 28 } |
| 29 } | 29 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 template<typename T> | 97 template<typename T> |
| 98 bool Write(T v); | 98 bool Write(T v); |
| 99 | 99 |
| 100 char* ptr_; | 100 char* ptr_; |
| 101 char* end_; | 101 char* end_; |
| 102 }; | 102 }; |
| 103 | 103 |
| 104 } // namespace base | 104 } // namespace base |
| 105 | 105 |
| 106 #endif // BASE_BIG_ENDIAN_H_ | 106 #endif // BASE_BIG_ENDIAN_H_ |
| OLD | NEW |