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_t* value) {
50 bool BigEndianReader::ReadU16(uint16_t* value) {
54 bool BigEndianReader::ReadU32(uint32_t* value) {
58 bool BigEndianReader::ReadU64(uint64_t* value) {
62 BigEndianWriter::BigEndianWriter(char* buf, size_t len)
63 : ptr_(buf), end_(ptr_ + len) {}
65 bool BigEndianWriter::Skip(size_t len) {
66 if (ptr_ + len > end_)
72 bool BigEndianWriter::WriteBytes(const void* buf, size_t len) {
73 if (ptr_ + len > end_)
75 memcpy(ptr_, buf, len);
81 bool BigEndianWriter::Write(T value) {
82 if (ptr_ + sizeof(T) > end_)
84 WriteBigEndian<T>(ptr_, value);
89 bool BigEndianWriter::WriteU8(uint8_t value) {
93 bool BigEndianWriter::WriteU16(uint16_t value) {
97 bool BigEndianWriter::WriteU32(uint32_t value) {
101 bool BigEndianWriter::WriteU64(uint64_t value) {