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 #include "base/big_endian.h"
7 #include "base/strings/string_piece.h"
11 BigEndianReader::BigEndianReader(const char* buf
, size_t len
)
12 : ptr_(buf
), end_(ptr_
+ len
) {}
14 bool BigEndianReader::Skip(size_t len
) {
15 if (ptr_
+ len
> end_
)
21 bool BigEndianReader::ReadBytes(void* out
, size_t len
) {
22 if (ptr_
+ len
> end_
)
24 memcpy(out
, ptr_
, len
);
29 bool BigEndianReader::ReadPiece(base::StringPiece
* out
, size_t len
) {
30 if (ptr_
+ len
> end_
)
32 *out
= base::StringPiece(ptr_
, len
);
38 bool BigEndianReader::Read(T
* value
) {
39 if (ptr_
+ sizeof(T
) > end_
)
41 ReadBigEndian
<T
>(ptr_
, value
);
46 bool BigEndianReader::ReadU8(uint8
* value
) {
50 bool BigEndianReader::ReadU16(uint16
* value
) {
54 bool BigEndianReader::ReadU32(uint32
* value
) {
58 BigEndianWriter::BigEndianWriter(char* buf
, size_t len
)
59 : ptr_(buf
), end_(ptr_
+ len
) {}
61 bool BigEndianWriter::Skip(size_t len
) {
62 if (ptr_
+ len
> end_
)
68 bool BigEndianWriter::WriteBytes(const void* buf
, size_t len
) {
69 if (ptr_
+ len
> end_
)
71 memcpy(ptr_
, buf
, len
);
77 bool BigEndianWriter::Write(T value
) {
78 if (ptr_
+ sizeof(T
) > end_
)
80 WriteBigEndian
<T
>(ptr_
, value
);
85 bool BigEndianWriter::WriteU8(uint8 value
) {
89 bool BigEndianWriter::WriteU16(uint16 value
) {
93 bool BigEndianWriter::WriteU32(uint32 value
) {