blob: 914767f4286e0959b2d5112e101c830289ae68d0 [file] [log] [blame]
[email protected]d9806a92014-02-26 18:14:571// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]7556ea22011-12-08 19:29:152// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d9806a92014-02-26 18:14:575#ifndef BASE_BIG_ENDIAN_H_
6#define BASE_BIG_ENDIAN_H_
[email protected]7556ea22011-12-08 19:29:157
[email protected]d9806a92014-02-26 18:14:578#include "base/base_export.h"
[email protected]7556ea22011-12-08 19:29:159#include "base/basictypes.h"
[email protected]d069c112013-04-13 00:01:5510#include "base/strings/string_piece.h"
[email protected]7556ea22011-12-08 19:29:1511
[email protected]d9806a92014-02-26 18:14:5712namespace base {
[email protected]7556ea22011-12-08 19:29:1513
14// Read an integer (signed or unsigned) from |buf| in Big Endian order.
15// Note: this loop is unrolled with -O1 and above.
16// NOTE(szym): glibc dns-canon.c and SpdyFrameBuilder use
17// ntohs(*(uint16_t*)ptr) which is potentially unaligned.
18// This would cause SIGBUS on ARMv5 or earlier and ARMv6-M.
19template<typename T>
20inline void ReadBigEndian(const char buf[], T* out) {
21 *out = buf[0];
22 for (size_t i = 1; i < sizeof(T); ++i) {
23 *out <<= 8;
24 // Must cast to uint8 to avoid clobbering by sign extension.
25 *out |= static_cast<uint8>(buf[i]);
26 }
27}
28
29// Write an integer (signed or unsigned) |val| to |buf| in Big Endian order.
30// Note: this loop is unrolled with -O1 and above.
31template<typename T>
32inline void WriteBigEndian(char buf[], T val) {
33 for (size_t i = 0; i < sizeof(T); ++i) {
34 buf[sizeof(T)-i-1] = static_cast<char>(val & 0xFF);
35 val >>= 8;
36 }
37}
38
39// Specializations to make clang happy about the (dead code) shifts above.
40template<>
41inline void ReadBigEndian<uint8>(const char buf[], uint8* out) {
42 *out = buf[0];
43}
44
45template<>
46inline void WriteBigEndian<uint8>(char buf[], uint8 val) {
47 buf[0] = static_cast<char>(val);
48}
49
50// Allows reading integers in network order (big endian) while iterating over
51// an underlying buffer. All the reading functions advance the internal pointer.
[email protected]d9806a92014-02-26 18:14:5752class BASE_EXPORT BigEndianReader {
[email protected]7556ea22011-12-08 19:29:1553 public:
[email protected]d9806a92014-02-26 18:14:5754 BigEndianReader(const char* buf, size_t len);
[email protected]7556ea22011-12-08 19:29:1555
56 const char* ptr() const { return ptr_; }
57 int remaining() const { return end_ - ptr_; }
58
59 bool Skip(size_t len);
60 bool ReadBytes(void* out, size_t len);
61 // Creates a StringPiece in |out| that points to the underlying buffer.
62 bool ReadPiece(base::StringPiece* out, size_t len);
63 bool ReadU8(uint8* value);
64 bool ReadU16(uint16* value);
65 bool ReadU32(uint32* value);
[email protected]0d5590f2014-07-30 23:24:4866 bool ReadU64(uint64* value);
[email protected]7556ea22011-12-08 19:29:1567
68 private:
69 // Hidden to promote type safety.
70 template<typename T>
71 bool Read(T* v);
72
73 const char* ptr_;
74 const char* end_;
75};
76
77// Allows writing integers in network order (big endian) while iterating over
78// an underlying buffer. All the writing functions advance the internal pointer.
[email protected]d9806a92014-02-26 18:14:5779class BASE_EXPORT BigEndianWriter {
[email protected]7556ea22011-12-08 19:29:1580 public:
[email protected]d9806a92014-02-26 18:14:5781 BigEndianWriter(char* buf, size_t len);
[email protected]7556ea22011-12-08 19:29:1582
83 char* ptr() const { return ptr_; }
84 int remaining() const { return end_ - ptr_; }
85
86 bool Skip(size_t len);
87 bool WriteBytes(const void* buf, size_t len);
88 bool WriteU8(uint8 value);
89 bool WriteU16(uint16 value);
90 bool WriteU32(uint32 value);
[email protected]0d5590f2014-07-30 23:24:4891 bool WriteU64(uint64 value);
[email protected]7556ea22011-12-08 19:29:1592
93 private:
94 // Hidden to promote type safety.
95 template<typename T>
96 bool Write(T v);
97
98 char* ptr_;
99 char* end_;
100};
101
[email protected]d9806a92014-02-26 18:14:57102} // namespace base
[email protected]7556ea22011-12-08 19:29:15103
[email protected]d9806a92014-02-26 18:14:57104#endif // BASE_BIG_ENDIAN_H_