| OLD | NEW |
| (Empty) |
| 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 #ifndef NET_SPDY_HPACK_HPACK_INPUT_STREAM_H_ | |
| 6 #define NET_SPDY_HPACK_HPACK_INPUT_STREAM_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include <utility> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "net/base/net_export.h" | |
| 15 #include "net/spdy/hpack/hpack_constants.h" | |
| 16 #include "net/spdy/hpack/hpack_huffman_table.h" | |
| 17 #include "net/spdy/platform/api/spdy_string.h" | |
| 18 #include "net/spdy/platform/api/spdy_string_piece.h" | |
| 19 | |
| 20 // All section references below are to | |
| 21 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-08 | |
| 22 | |
| 23 namespace net { | |
| 24 | |
| 25 namespace test { | |
| 26 class HpackInputStreamPeer; | |
| 27 } // namespace test | |
| 28 | |
| 29 typedef std::pair<size_t, uint32_t> InitialPeekResult; | |
| 30 | |
| 31 // An HpackInputStream handles all the low-level details of decoding | |
| 32 // header fields. | |
| 33 class NET_EXPORT_PRIVATE HpackInputStream { | |
| 34 public: | |
| 35 friend class test::HpackInputStreamPeer; | |
| 36 | |
| 37 explicit HpackInputStream(SpdyStringPiece buffer); | |
| 38 ~HpackInputStream(); | |
| 39 | |
| 40 // Returns whether or not there is more data to process. | |
| 41 bool HasMoreData() const; | |
| 42 | |
| 43 // If the next bits of input match |prefix|, consumes them and returns true. | |
| 44 // Otherwise, consumes nothing and returns false. | |
| 45 bool MatchPrefixAndConsume(HpackPrefix prefix); | |
| 46 | |
| 47 // The Decode* functions return true and fill in their arguments if | |
| 48 // decoding was successful, or false if an error was encountered. | |
| 49 | |
| 50 bool DecodeNextUint32(uint32_t* I); | |
| 51 bool DecodeNextIdentityString(SpdyStringPiece* str); | |
| 52 bool DecodeNextHuffmanString(SpdyString* str); | |
| 53 | |
| 54 // Stores input bits into the most-significant, unfilled bits of |out|. | |
| 55 // |peeked_count| is the number of filled bits in |out| which have been | |
| 56 // previously peeked. PeekBits() will fill some number of remaining bits, | |
| 57 // returning the new total number via |peeked_count|. Returns true if one | |
| 58 // or more additional bits were added to |out|, and false otherwise. | |
| 59 bool PeekBits(size_t* peeked_count, uint32_t* out) const; | |
| 60 | |
| 61 // Similar to PeekBits, but intended to be used when starting to decode a | |
| 62 // Huffman encoded string. Returns a pair containing the peeked_count and | |
| 63 // out values as described for PeekBits, with the bits from the first N bytes | |
| 64 // of buffer_, where N == min(4, buffer_.size()), starting with the high | |
| 65 // order bits. | |
| 66 // Should only be called when first peeking at bits from the input stream as | |
| 67 // it does not take peeked_count as an input, so doesn't know how many bits | |
| 68 // have already been returned by previous calls to InitializePeekBits and | |
| 69 // PeekBits. | |
| 70 InitialPeekResult InitializePeekBits(); | |
| 71 | |
| 72 // Consumes |count| bits of input. Generally paired with PeekBits(). | |
| 73 void ConsumeBits(size_t count); | |
| 74 | |
| 75 // If not currently on a byte boundary, consumes and discards | |
| 76 // remaining bits in the current byte. | |
| 77 void ConsumeByteRemainder(); | |
| 78 | |
| 79 // Return the total bytes that have been parsed SUCCESSFULLY. | |
| 80 uint32_t ParsedBytes() const; | |
| 81 | |
| 82 // When incrementally decode the header, need to remember the current | |
| 83 // position in the buffer after we successfully decode one opcode. | |
| 84 void MarkCurrentPosition(); | |
| 85 | |
| 86 // Returning true indicates this instance of HpackInputStream | |
| 87 // doesn't have enough data to parse the current opcode, and we | |
| 88 // are done with this instance. When more data arrive, a new | |
| 89 // HpackInputStream should be created to restart the parsing. | |
| 90 bool NeedMoreData() const; | |
| 91 | |
| 92 private: | |
| 93 SpdyStringPiece buffer_; | |
| 94 size_t bit_offset_; | |
| 95 // Total number of bytes parsed successfully. Only get updated when an | |
| 96 // opcode is parsed successfully. | |
| 97 uint32_t parsed_bytes_; | |
| 98 // Total number of bytes parsed currently. Get updated when an octet, | |
| 99 // a number or a string has been parsed successfully. Can point to the | |
| 100 // middle of an opcode. | |
| 101 uint32_t parsed_bytes_current_; | |
| 102 bool need_more_data_; | |
| 103 | |
| 104 bool PeekNextOctet(uint8_t* next_octet); | |
| 105 | |
| 106 bool DecodeNextOctet(uint8_t* next_octet); | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(HpackInputStream); | |
| 109 }; | |
| 110 | |
| 111 } // namespace net | |
| 112 | |
| 113 #endif // NET_SPDY_HPACK_HPACK_INPUT_STREAM_H_ | |
| OLD | NEW |