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_INPUT_STREAM_H_
6#define NET_SPDY_HPACK_INPUT_STREAM_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/macros.h"
12#include "base/strings/string_piece.h"
13#include "net/base/net_export.h"
14#include "net/spdy/hpack_constants.h"
15#include "net/spdy/hpack_huffman_table.h"
16
17// All section references below are to
18// http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-08
19
20namespace net {
21
22// An HpackInputStream handles all the low-level details of decoding
23// header fields.
24class NET_EXPORT_PRIVATE HpackInputStream {
25 public:
26  // |max_string_literal_size| is the largest that any one string
27  // literal (header name or header value) can be.
28  HpackInputStream(uint32 max_string_literal_size, base::StringPiece buffer);
29  ~HpackInputStream();
30
31  // Returns whether or not there is more data to process.
32  bool HasMoreData() const;
33
34  // If the next bits of input match |prefix|, consumes them and returns true.
35  // Otherwise, consumes nothing and returns false.
36  bool MatchPrefixAndConsume(HpackPrefix prefix);
37
38  // The Decode* functions return true and fill in their arguments if
39  // decoding was successful, or false if an error was encountered.
40
41  bool DecodeNextUint32(uint32* I);
42  bool DecodeNextIdentityString(base::StringPiece* str);
43  bool DecodeNextHuffmanString(const HpackHuffmanTable& table,
44                               std::string* str);
45
46  // Stores input bits into the most-significant, unfilled bits of |out|.
47  // |peeked_count| is the number of filled bits in |out| which have been
48  // previously peeked. PeekBits() will fill some number of remaining bits,
49  // returning the new total number via |peeked_count|. Returns true if one
50  // or more additional bits could be peeked, and false otherwise.
51  bool PeekBits(size_t* peeked_count, uint32* out);
52
53  // Consumes |count| bits of input. Generally paired with PeekBits().
54  void ConsumeBits(size_t count);
55
56  // If not currently on a byte boundary, consumes and discards
57  // remaining bits in the current byte.
58  void ConsumeByteRemainder();
59
60  // Accessors for testing.
61
62  void SetBitOffsetForTest(size_t bit_offset) {
63    bit_offset_ = bit_offset;
64  }
65
66 private:
67  const uint32 max_string_literal_size_;
68  base::StringPiece buffer_;
69  size_t bit_offset_;
70
71  bool PeekNextOctet(uint8* next_octet);
72
73  bool DecodeNextOctet(uint8* next_octet);
74
75  DISALLOW_COPY_AND_ASSIGN(HpackInputStream);
76};
77
78}  // namespace net
79
80#endif  // NET_SPDY_HPACK_INPUT_STREAM_H_
81