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.
5 #ifndef BASE_BIG_ENDIAN_H_
6 #define BASE_BIG_ENDIAN_H_
10 #include <type_traits>
12 #include "base/base_export.h"
13 #include "base/strings/string_piece.h"
17 // Read an integer (signed or unsigned) from |buf| in Big Endian order.
18 // Note: this loop is unrolled with -O1 and above.
19 // NOTE(szym): glibc dns-canon.c use ntohs(*(uint16_t*)ptr) which is
20 // potentially unaligned.
21 // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M.
23 inline void ReadBigEndian(const char buf[], T* out) {
24 static_assert(std::is_integral<T>::value, "T has to be an integral type.");
26 for (size_t i = 1; i < sizeof(T); ++i) {
28 // Must cast to uint8_t to avoid clobbering by sign extension.
29 *out |= static_cast<uint8_t>(buf[i]);
33 // Write an integer (signed or unsigned) |val| to |buf| in Big Endian order.
34 // Note: this loop is unrolled with -O1 and above.
36 inline void WriteBigEndian(char buf[], T val) {
37 static_assert(std::is_integral<T>::value, "T has to be an integral type.");
38 for (size_t i = 0; i < sizeof(T); ++i) {
39 buf[sizeof(T)-i-1] = static_cast<char>(val & 0xFF);
44 // Specializations to make clang happy about the (dead code) shifts above.
46 inline void ReadBigEndian<uint8_t>(const char buf[], uint8_t* out) {
51 inline void WriteBigEndian<uint8_t>(char buf[], uint8_t val) {
52 buf[0] = static_cast<char>(val);
55 // Allows reading integers in network order (big endian) while iterating over
56 // an underlying buffer. All the reading functions advance the internal pointer.
57 class BASE_EXPORT BigEndianReader {
59 BigEndianReader(const char* buf, size_t len);
61 const char* ptr() const { return ptr_; }
62 size_t remaining() const { return end_ - ptr_; }
64 bool Skip(size_t len);
65 bool ReadBytes(void* out, size_t len);
66 // Creates a StringPiece in |out| that points to the underlying buffer.
67 bool ReadPiece(base::StringPiece* out, size_t len);
68 bool ReadU8(uint8_t* value);
69 bool ReadU16(uint16_t* value);
70 bool ReadU32(uint32_t* value);
71 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.
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.
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);
88 // Hidden to promote type safety.
92 bool ReadLengthPrefixed(base::StringPiece* out);
98 // Allows writing integers in network order (big endian) while iterating over
99 // an underlying buffer. All the writing functions advance the internal pointer.
100 class BASE_EXPORT BigEndianWriter {
102 BigEndianWriter(char* buf, size_t len);
104 char* ptr() const { return ptr_; }
105 size_t remaining() const { return end_ - ptr_; }
107 bool Skip(size_t len);
108 bool WriteBytes(const void* buf, size_t len);
109 bool WriteU8(uint8_t value);
110 bool WriteU16(uint16_t value);
111 bool WriteU32(uint32_t value);
112 bool WriteU64(uint64_t value);
115 // Hidden to promote type safety.
125 #endif // BASE_BIG_ENDIAN_H_