QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [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 "net/third_party/quiche/src/spdy/core/spdy_prefixed_buffer_reader.h" |
| 6 | |
| 7 | #include <new> |
| 8 | |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_logging.h" |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 10 | |
| 11 | namespace spdy { |
| 12 | |
| 13 | SpdyPrefixedBufferReader::SpdyPrefixedBufferReader(const char* prefix, |
| 14 | size_t prefix_length, |
| 15 | const char* suffix, |
| 16 | size_t suffix_length) |
| 17 | : prefix_(prefix), |
| 18 | suffix_(suffix), |
| 19 | prefix_length_(prefix_length), |
| 20 | suffix_length_(suffix_length) {} |
| 21 | |
| 22 | size_t SpdyPrefixedBufferReader::Available() { |
| 23 | return prefix_length_ + suffix_length_; |
| 24 | } |
| 25 | |
| 26 | bool SpdyPrefixedBufferReader::ReadN(size_t count, char* out) { |
| 27 | if (Available() < count) { |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | if (prefix_length_ >= count) { |
| 32 | // Read is fully satisfied by the prefix. |
| 33 | std::copy(prefix_, prefix_ + count, out); |
| 34 | prefix_ += count; |
| 35 | prefix_length_ -= count; |
| 36 | return true; |
| 37 | } else if (prefix_length_ != 0) { |
| 38 | // Read is partially satisfied by the prefix. |
| 39 | out = std::copy(prefix_, prefix_ + prefix_length_, out); |
| 40 | count -= prefix_length_; |
| 41 | prefix_length_ = 0; |
| 42 | // Fallthrough to suffix read. |
| 43 | } |
| 44 | DCHECK(suffix_length_ >= count); |
| 45 | // Read is satisfied by the suffix. |
| 46 | std::copy(suffix_, suffix_ + count, out); |
| 47 | suffix_ += count; |
| 48 | suffix_length_ -= count; |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | bool SpdyPrefixedBufferReader::ReadN(size_t count, |
| 53 | SpdyPinnableBufferPiece* out) { |
| 54 | if (Available() < count) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | out->storage_.reset(); |
| 59 | out->length_ = count; |
| 60 | |
| 61 | if (prefix_length_ >= count) { |
| 62 | // Read is fully satisfied by the prefix. |
| 63 | out->buffer_ = prefix_; |
| 64 | prefix_ += count; |
| 65 | prefix_length_ -= count; |
| 66 | return true; |
| 67 | } else if (prefix_length_ != 0) { |
| 68 | // Read is only partially satisfied by the prefix. We need to allocate |
| 69 | // contiguous storage as the read spans the prefix & suffix. |
| 70 | out->storage_.reset(new char[count]); |
| 71 | out->buffer_ = out->storage_.get(); |
| 72 | ReadN(count, out->storage_.get()); |
| 73 | return true; |
| 74 | } else { |
| 75 | DCHECK(suffix_length_ >= count); |
| 76 | // Read is fully satisfied by the suffix. |
| 77 | out->buffer_ = suffix_; |
| 78 | suffix_ += count; |
| 79 | suffix_length_ -= count; |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | } // namespace spdy |