dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "common/quiche_data_writer.h" |
dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | #include <limits> |
| 9 | |
vasilvv | 6950cd8 | 2020-12-03 15:33:04 -0800 | [diff] [blame] | 10 | #include "absl/strings/str_cat.h" |
vasilvv | 652cd42 | 2020-10-09 15:50:02 -0700 | [diff] [blame] | 11 | #include "absl/strings/string_view.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 12 | #include "common/quiche_endian.h" |
dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 13 | |
| 14 | namespace quiche { |
| 15 | |
| 16 | QuicheDataWriter::QuicheDataWriter(size_t size, char* buffer) |
| 17 | : QuicheDataWriter(size, buffer, quiche::NETWORK_BYTE_ORDER) {} |
| 18 | |
| 19 | QuicheDataWriter::QuicheDataWriter(size_t size, |
| 20 | char* buffer, |
| 21 | quiche::Endianness endianness) |
| 22 | : buffer_(buffer), capacity_(size), length_(0), endianness_(endianness) {} |
| 23 | |
| 24 | QuicheDataWriter::~QuicheDataWriter() {} |
| 25 | |
| 26 | char* QuicheDataWriter::data() { |
| 27 | return buffer_; |
| 28 | } |
| 29 | |
| 30 | bool QuicheDataWriter::WriteUInt8(uint8_t value) { |
| 31 | return WriteBytes(&value, sizeof(value)); |
| 32 | } |
| 33 | |
| 34 | bool QuicheDataWriter::WriteUInt16(uint16_t value) { |
| 35 | if (endianness_ == quiche::NETWORK_BYTE_ORDER) { |
| 36 | value = quiche::QuicheEndian::HostToNet16(value); |
| 37 | } |
| 38 | return WriteBytes(&value, sizeof(value)); |
| 39 | } |
| 40 | |
| 41 | bool QuicheDataWriter::WriteUInt32(uint32_t value) { |
| 42 | if (endianness_ == quiche::NETWORK_BYTE_ORDER) { |
| 43 | value = quiche::QuicheEndian::HostToNet32(value); |
| 44 | } |
| 45 | return WriteBytes(&value, sizeof(value)); |
| 46 | } |
| 47 | |
| 48 | bool QuicheDataWriter::WriteUInt64(uint64_t value) { |
| 49 | if (endianness_ == quiche::NETWORK_BYTE_ORDER) { |
| 50 | value = quiche::QuicheEndian::HostToNet64(value); |
| 51 | } |
| 52 | return WriteBytes(&value, sizeof(value)); |
| 53 | } |
| 54 | |
| 55 | bool QuicheDataWriter::WriteBytesToUInt64(size_t num_bytes, uint64_t value) { |
| 56 | if (num_bytes > sizeof(value)) { |
| 57 | return false; |
| 58 | } |
| 59 | if (endianness_ == quiche::HOST_BYTE_ORDER) { |
| 60 | return WriteBytes(&value, num_bytes); |
| 61 | } |
| 62 | |
| 63 | value = quiche::QuicheEndian::HostToNet64(value); |
| 64 | return WriteBytes(reinterpret_cast<char*>(&value) + sizeof(value) - num_bytes, |
| 65 | num_bytes); |
| 66 | } |
| 67 | |
vasilvv | 652cd42 | 2020-10-09 15:50:02 -0700 | [diff] [blame] | 68 | bool QuicheDataWriter::WriteStringPiece16(absl::string_view val) { |
dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 69 | if (val.size() > std::numeric_limits<uint16_t>::max()) { |
| 70 | return false; |
| 71 | } |
| 72 | if (!WriteUInt16(static_cast<uint16_t>(val.size()))) { |
| 73 | return false; |
| 74 | } |
| 75 | return WriteBytes(val.data(), val.size()); |
| 76 | } |
| 77 | |
vasilvv | 652cd42 | 2020-10-09 15:50:02 -0700 | [diff] [blame] | 78 | bool QuicheDataWriter::WriteStringPiece(absl::string_view val) { |
dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 79 | return WriteBytes(val.data(), val.size()); |
| 80 | } |
| 81 | |
| 82 | char* QuicheDataWriter::BeginWrite(size_t length) { |
| 83 | if (length_ > capacity_) { |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
| 87 | if (capacity_ - length_ < length) { |
| 88 | return nullptr; |
| 89 | } |
| 90 | |
| 91 | #ifdef ARCH_CPU_64_BITS |
vasilvv | b38e023 | 2021-02-02 15:20:07 -0800 | [diff] [blame] | 92 | QUICHE_DCHECK_LE(length, std::numeric_limits<uint32_t>::max()); |
dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 93 | #endif |
| 94 | |
| 95 | return buffer_ + length_; |
| 96 | } |
| 97 | |
| 98 | bool QuicheDataWriter::WriteBytes(const void* data, size_t data_len) { |
| 99 | char* dest = BeginWrite(data_len); |
| 100 | if (!dest) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | memcpy(dest, data, data_len); |
| 105 | |
| 106 | length_ += data_len; |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | bool QuicheDataWriter::WriteRepeatedByte(uint8_t byte, size_t count) { |
| 111 | char* dest = BeginWrite(count); |
| 112 | if (!dest) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | memset(dest, byte, count); |
| 117 | |
| 118 | length_ += count; |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | void QuicheDataWriter::WritePadding() { |
vasilvv | b38e023 | 2021-02-02 15:20:07 -0800 | [diff] [blame] | 123 | QUICHE_DCHECK_LE(length_, capacity_); |
dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 124 | if (length_ > capacity_) { |
| 125 | return; |
| 126 | } |
| 127 | memset(buffer_ + length_, 0x00, capacity_ - length_); |
| 128 | length_ = capacity_; |
| 129 | } |
| 130 | |
| 131 | bool QuicheDataWriter::WritePaddingBytes(size_t count) { |
| 132 | return WriteRepeatedByte(0x00, count); |
| 133 | } |
| 134 | |
| 135 | bool QuicheDataWriter::WriteTag(uint32_t tag) { |
| 136 | return WriteBytes(&tag, sizeof(tag)); |
| 137 | } |
| 138 | |
| 139 | bool QuicheDataWriter::Seek(size_t length) { |
| 140 | if (!BeginWrite(length)) { |
| 141 | return false; |
| 142 | } |
| 143 | length_ += length; |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | std::string QuicheDataWriter::DebugString() const { |
vasilvv | 6950cd8 | 2020-12-03 15:33:04 -0800 | [diff] [blame] | 148 | return absl::StrCat(" { capacity: ", capacity_, ", length: ", length_, " }"); |
dmcardle | 2b64f50 | 2020-01-07 15:22:36 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | } // namespace quiche |