Roll src/third_party/WebKit 5bc05e9:3d59927 (svn 202625:202627)
[chromium-blink-merge.git] / base / big_endian.cc
blob9b69147a316e1f325e7e4304d8998750caca48f2
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"
9 namespace base {
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_)
16 return false;
17 ptr_ += len;
18 return true;
21 bool 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;
29 bool 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;
37 template<typename T>
38 bool 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;
46 bool BigEndianReader::ReadU8(uint8* value) {
47 return Read(value);
50 bool BigEndianReader::ReadU16(uint16* value) {
51 return Read(value);
54 bool BigEndianReader::ReadU32(uint32* value) {
55 return Read(value);
58 bool BigEndianReader::ReadU64(uint64* value) {
59 return Read(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_)
67 return false;
68 ptr_ += len;
69 return true;
72 bool 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;
80 template<typename T>
81 bool 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;
89 bool BigEndianWriter::WriteU8(uint8 value) {
90 return Write(value);
93 bool BigEndianWriter::WriteU16(uint16 value) {
94 return Write(value);
97 bool BigEndianWriter::WriteU32(uint32 value) {
98 return Write(value);
101 bool BigEndianWriter::WriteU64(uint64 value) {
102 return Write(value);
105 } // namespace base