tfarina@chromium.org | 5e7e673 | 2014-02-27 03:14:57 +0900 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/big_endian.h" |
| 6 | |
| 7 | #include "base/strings/string_piece.h" |
| 8 | |
| 9 | namespace base { |
| 10 | |
| 11 | BigEndianReader::BigEndianReader(const char* buf, size_t len) |
| 12 | : ptr_(buf), end_(ptr_ + len) {} |
| 13 | |
| 14 | bool BigEndianReader::Skip(size_t len) { |
| 15 | if (ptr_ + len > end_) |
| 16 | return false; |
| 17 | ptr_ += len; |
| 18 | return true; |
| 19 | } |
| 20 | |
| 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; |
| 27 | } |
| 28 | |
| 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; |
| 35 | } |
| 36 | |
| 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; |
| 44 | } |
| 45 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 46 | bool BigEndianReader::ReadU8(uint8_t* value) { |
tfarina@chromium.org | 5e7e673 | 2014-02-27 03:14:57 +0900 | [diff] [blame] | 47 | return Read(value); |
| 48 | } |
| 49 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 50 | bool BigEndianReader::ReadU16(uint16_t* value) { |
tfarina@chromium.org | 5e7e673 | 2014-02-27 03:14:57 +0900 | [diff] [blame] | 51 | return Read(value); |
| 52 | } |
| 53 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 54 | bool BigEndianReader::ReadU32(uint32_t* value) { |
tfarina@chromium.org | 5e7e673 | 2014-02-27 03:14:57 +0900 | [diff] [blame] | 55 | return Read(value); |
| 56 | } |
| 57 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 58 | bool BigEndianReader::ReadU64(uint64_t* value) { |
benchan@chromium.org | c999340 | 2014-07-31 08:24:48 +0900 | [diff] [blame] | 59 | return Read(value); |
| 60 | } |
| 61 | |
tfarina@chromium.org | 5e7e673 | 2014-02-27 03:14:57 +0900 | [diff] [blame] | 62 | BigEndianWriter::BigEndianWriter(char* buf, size_t len) |
| 63 | : ptr_(buf), end_(ptr_ + len) {} |
| 64 | |
| 65 | bool BigEndianWriter::Skip(size_t len) { |
| 66 | if (ptr_ + len > end_) |
| 67 | return false; |
| 68 | ptr_ += len; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 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; |
| 78 | } |
| 79 | |
| 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; |
| 87 | } |
| 88 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 89 | bool BigEndianWriter::WriteU8(uint8_t value) { |
tfarina@chromium.org | 5e7e673 | 2014-02-27 03:14:57 +0900 | [diff] [blame] | 90 | return Write(value); |
| 91 | } |
| 92 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 93 | bool BigEndianWriter::WriteU16(uint16_t value) { |
tfarina@chromium.org | 5e7e673 | 2014-02-27 03:14:57 +0900 | [diff] [blame] | 94 | return Write(value); |
| 95 | } |
| 96 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 97 | bool BigEndianWriter::WriteU32(uint32_t value) { |
tfarina@chromium.org | 5e7e673 | 2014-02-27 03:14:57 +0900 | [diff] [blame] | 98 | return Write(value); |
| 99 | } |
| 100 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 101 | bool BigEndianWriter::WriteU64(uint64_t value) { |
benchan@chromium.org | c999340 | 2014-07-31 08:24:48 +0900 | [diff] [blame] | 102 | return Write(value); |
| 103 | } |
| 104 | |
tfarina@chromium.org | 5e7e673 | 2014-02-27 03:14:57 +0900 | [diff] [blame] | 105 | } // namespace base |