blob: 514581fe41595709cde742195fb9530a1f1667df [file] [log] [blame]
tfarina@chromium.org5e7e6732014-02-27 03:14:57 +09001// 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.
4
5#include "base/big_endian.h"
6
7#include "base/strings/string_piece.h"
8
9namespace base {
10
11BigEndianReader::BigEndianReader(const char* buf, size_t len)
12 : ptr_(buf), end_(ptr_ + len) {}
13
14bool BigEndianReader::Skip(size_t len) {
15 if (ptr_ + len > end_)
16 return false;
17 ptr_ += len;
18 return true;
19}
20
21bool BigEndianReader::ReadBytes(void* out, size_t len) {
22 if (ptr_ + len > end_)
23 return false;
24 memcpy(out, ptr_, len);
25 ptr_ += len;
26 return true;
27}
28
29bool BigEndianReader::ReadPiece(base::StringPiece* out, size_t len) {
30 if (ptr_ + len > end_)
31 return false;
32 *out = base::StringPiece(ptr_, len);
33 ptr_ += len;
34 return true;
35}
36
37template<typename T>
38bool BigEndianReader::Read(T* value) {
39 if (ptr_ + sizeof(T) > end_)
40 return false;
41 ReadBigEndian<T>(ptr_, value);
42 ptr_ += sizeof(T);
43 return true;
44}
45
avia6a6a682015-12-27 07:15:14 +090046bool BigEndianReader::ReadU8(uint8_t* value) {
tfarina@chromium.org5e7e6732014-02-27 03:14:57 +090047 return Read(value);
48}
49
avia6a6a682015-12-27 07:15:14 +090050bool BigEndianReader::ReadU16(uint16_t* value) {
tfarina@chromium.org5e7e6732014-02-27 03:14:57 +090051 return Read(value);
52}
53
avia6a6a682015-12-27 07:15:14 +090054bool BigEndianReader::ReadU32(uint32_t* value) {
tfarina@chromium.org5e7e6732014-02-27 03:14:57 +090055 return Read(value);
56}
57
avia6a6a682015-12-27 07:15:14 +090058bool BigEndianReader::ReadU64(uint64_t* value) {
benchan@chromium.orgc9993402014-07-31 08:24:48 +090059 return Read(value);
60}
61
tfarina@chromium.org5e7e6732014-02-27 03:14:57 +090062BigEndianWriter::BigEndianWriter(char* buf, size_t len)
63 : ptr_(buf), end_(ptr_ + len) {}
64
65bool BigEndianWriter::Skip(size_t len) {
66 if (ptr_ + len > end_)
67 return false;
68 ptr_ += len;
69 return true;
70}
71
72bool BigEndianWriter::WriteBytes(const void* buf, size_t len) {
73 if (ptr_ + len > end_)
74 return false;
75 memcpy(ptr_, buf, len);
76 ptr_ += len;
77 return true;
78}
79
80template<typename T>
81bool BigEndianWriter::Write(T value) {
82 if (ptr_ + sizeof(T) > end_)
83 return false;
84 WriteBigEndian<T>(ptr_, value);
85 ptr_ += sizeof(T);
86 return true;
87}
88
avia6a6a682015-12-27 07:15:14 +090089bool BigEndianWriter::WriteU8(uint8_t value) {
tfarina@chromium.org5e7e6732014-02-27 03:14:57 +090090 return Write(value);
91}
92
avia6a6a682015-12-27 07:15:14 +090093bool BigEndianWriter::WriteU16(uint16_t value) {
tfarina@chromium.org5e7e6732014-02-27 03:14:57 +090094 return Write(value);
95}
96
avia6a6a682015-12-27 07:15:14 +090097bool BigEndianWriter::WriteU32(uint32_t value) {
tfarina@chromium.org5e7e6732014-02-27 03:14:57 +090098 return Write(value);
99}
100
avia6a6a682015-12-27 07:15:14 +0900101bool BigEndianWriter::WriteU64(uint64_t value) {
benchan@chromium.orgc9993402014-07-31 08:24:48 +0900102 return Write(value);
103}
104
tfarina@chromium.org5e7e6732014-02-27 03:14:57 +0900105} // namespace base