OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_data_writer.h" | 5 #include "net/quic/quic_data_writer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "net/quic/quic_protocol.h" | 12 #include "net/quic/quic_protocol.h" |
13 | 13 |
| 14 using base::StringPiece; |
14 using std::numeric_limits; | 15 using std::numeric_limits; |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 | 18 |
18 QuicDataWriter::QuicDataWriter(size_t size) | 19 QuicDataWriter::QuicDataWriter(size_t size) |
19 : buffer_(new char[size]), | 20 : buffer_(new char[size]), |
20 capacity_(size), | 21 capacity_(size), |
21 length_(0) { | 22 length_(0) { |
22 } | 23 } |
23 | 24 |
24 QuicDataWriter::~QuicDataWriter() { | 25 QuicDataWriter::~QuicDataWriter() { |
25 delete[] buffer_; | 26 delete[] buffer_; |
26 } | 27 } |
27 | 28 |
| 29 char* QuicDataWriter::take() { |
| 30 char* rv = buffer_; |
| 31 buffer_ = NULL; |
| 32 capacity_ = 0; |
| 33 length_ = 0; |
| 34 return rv; |
| 35 } |
| 36 |
| 37 bool QuicDataWriter::WriteUInt8(uint8 value) { |
| 38 return WriteBytes(&value, sizeof(value)); |
| 39 } |
| 40 |
| 41 bool QuicDataWriter::WriteUInt16(uint16 value) { |
| 42 return WriteBytes(&value, sizeof(value)); |
| 43 } |
| 44 |
| 45 bool QuicDataWriter::WriteUInt32(uint32 value) { |
| 46 return WriteBytes(&value, sizeof(value)); |
| 47 } |
| 48 |
| 49 bool QuicDataWriter::WriteUInt48(uint64 value) { |
| 50 uint32 hi = value >> 32; |
| 51 uint32 lo = value & GG_UINT64_C(0x00000000FFFFFFFF); |
| 52 return WriteUInt32(lo) && WriteUInt16(hi); |
| 53 } |
| 54 |
| 55 bool QuicDataWriter::WriteUInt64(uint64 value) { |
| 56 return WriteBytes(&value, sizeof(value)); |
| 57 } |
| 58 |
| 59 bool QuicDataWriter::WriteUInt128(uint128 value) { |
| 60 return WriteUInt64(value.lo) && WriteUInt64(value.hi); |
| 61 } |
| 62 |
| 63 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { |
| 64 if (val.length() > numeric_limits<uint16>::max()) { |
| 65 return false; |
| 66 } |
| 67 if (!WriteUInt16(val.size())) { |
| 68 return false; |
| 69 } |
| 70 return WriteBytes(val.data(), val.size()); |
| 71 } |
| 72 |
28 char* QuicDataWriter::BeginWrite(size_t length) { | 73 char* QuicDataWriter::BeginWrite(size_t length) { |
29 if (capacity_ - length_ < length) { | 74 if (capacity_ - length_ < length) { |
30 return NULL; | 75 return NULL; |
31 } | 76 } |
32 | 77 |
33 #ifdef ARCH_CPU_64_BITS | 78 #ifdef ARCH_CPU_64_BITS |
34 DCHECK_LE(length, numeric_limits<uint32>::max()); | 79 DCHECK_LE(length, numeric_limits<uint32>::max()); |
35 #endif | 80 #endif |
36 | 81 |
37 return buffer_ + length_; | 82 return buffer_ + length_; |
38 } | 83 } |
39 | 84 |
40 bool QuicDataWriter::AdvancePointer(uint32 len) { | |
41 if (!BeginWrite(len)) { | |
42 return false; | |
43 } | |
44 length_ += len; | |
45 return true; | |
46 } | |
47 | |
48 bool QuicDataWriter::WriteBytes(const void* data, uint32 data_len) { | 85 bool QuicDataWriter::WriteBytes(const void* data, uint32 data_len) { |
49 char* dest = BeginWrite(data_len); | 86 char* dest = BeginWrite(data_len); |
50 if (!dest) { | 87 if (!dest) { |
51 return false; | 88 return false; |
52 } | 89 } |
53 | 90 |
54 memcpy(dest, data, data_len); | 91 memcpy(dest, data, data_len); |
55 | 92 |
56 length_ += data_len; | 93 length_ += data_len; |
57 return true; | 94 return true; |
58 } | 95 } |
59 | 96 |
60 void QuicDataWriter::WriteUint64ToBuffer(uint64 value, char* buffer) { | 97 void QuicDataWriter::WriteUint64ToBuffer(uint64 value, char* buffer) { |
61 memcpy(buffer, &value, sizeof(value)); | 98 memcpy(buffer, &value, sizeof(value)); |
62 } | 99 } |
63 | 100 |
64 void QuicDataWriter::WriteUint128ToBuffer(uint128 value, char* buffer) { | 101 void QuicDataWriter::WriteUint128ToBuffer(uint128 value, char* buffer) { |
65 WriteUint64ToBuffer(value.lo, buffer); | 102 WriteUint64ToBuffer(value.lo, buffer); |
66 WriteUint64ToBuffer(value.hi, buffer + sizeof(value.lo)); | 103 WriteUint64ToBuffer(value.hi, buffer + sizeof(value.lo)); |
67 } | 104 } |
68 | 105 |
69 } // namespace net | 106 } // namespace net |
OLD | NEW |