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