proxygen
proxygen::HPACKDecodeBuffer Class Reference

#include <HPACKDecodeBuffer.h>

Public Member Functions

 HPACKDecodeBuffer (folly::io::Cursor &cursorVal, uint32_t totalBytes, uint32_t maxLiteralSize)
 
 ~HPACKDecodeBuffer ()
 
void reset (folly::io::Cursor &cursorVal)
 
void reset (folly::io::Cursor &cursorVal, uint32_t totalBytes)
 
uint32_t consumedBytes () const
 
const folly::io::Cursorcursor () const
 
bool empty ()
 
uint8_t next ()
 
uint8_t peek ()
 
HPACK::DecodeError decodeInteger (uint8_t nbit, uint64_t &integer)
 
HPACK::DecodeError decodeInteger (uint64_t &integer)
 
HPACK::DecodeError decodeLiteral (folly::fbstring &literal)
 
HPACK::DecodeError decodeLiteral (uint8_t nbit, folly::fbstring &literal)
 

Private Attributes

folly::io::Cursorcursor_
 
uint32_t totalBytes_
 
uint32_t remainingBytes_
 
uint32_t maxLiteralSize_ {std::numeric_limits<uint32_t>::max()}
 

Detailed Description

Definition at line 21 of file HPACKDecodeBuffer.h.

Constructor & Destructor Documentation

proxygen::HPACKDecodeBuffer::HPACKDecodeBuffer ( folly::io::Cursor cursorVal,
uint32_t  totalBytes,
uint32_t  maxLiteralSize 
)
inlineexplicit

Definition at line 24 of file HPACKDecodeBuffer.h.

27  : cursor_(cursorVal),
28  totalBytes_(totalBytes),
29  remainingBytes_(totalBytes),
30  maxLiteralSize_(maxLiteralSize) {}
folly::io::Cursor & cursor_
proxygen::HPACKDecodeBuffer::~HPACKDecodeBuffer ( )
inline

Definition at line 32 of file HPACKDecodeBuffer.h.

32 {}

Member Function Documentation

const folly::io::Cursor& proxygen::HPACKDecodeBuffer::cursor ( ) const
inline

Definition at line 49 of file HPACKDecodeBuffer.h.

References cursor_, decodeInteger(), decodeLiteral(), empty(), next(), peek(), uint64_t, and uint8_t.

49  {
50  return cursor_;
51  }
folly::io::Cursor & cursor_
DecodeError proxygen::HPACKDecodeBuffer::decodeInteger ( uint8_t  nbit,
uint64_t integer 
)

decode an integer from the current position, given a nbit prefix. Ignores 8 - nbit bits in the first byte of the buffer.

Definition at line 101 of file HPACKDecodeBuffer.cpp.

References add, proxygen::ERROR, f, max, proxygen::HPACK::NBIT_MASKS, next(), proxygen::NONE, remainingBytes_, uint32_t, uint64_t, and uint8_t.

Referenced by cursor(), proxygen::QPACKEncoder::decodeHeaderAck(), proxygen::HPACKDecoder::decodeIndexedHeader(), proxygen::QPACKDecoder::decodeIndexedHeaderQ(), decodeInteger(), decodeLiteral(), proxygen::HPACKDecoder::decodeLiteralHeader(), proxygen::QPACKDecoder::decodeLiteralHeaderQ(), proxygen::QPACKDecoder::handleBaseIndex(), and proxygen::HPACKDecoderBase::handleTableSizeUpdate().

101  {
102  if (remainingBytes_ == 0) {
103  LOG(ERROR) << "remainingBytes_ == 0";
104  return DecodeError::BUFFER_UNDERFLOW;
105  }
106  uint8_t byte = next();
107  uint8_t mask = HPACK::NBIT_MASKS[nbit];
108  // remove the first (8 - nbit) bits
109  byte = byte & mask;
110  integer = byte;
111  if (byte != mask) {
112  // the value fit in one byte
113  return DecodeError::NONE;
114  }
115  uint64_t f = 1;
116  uint32_t fexp = 0;
117  do {
118  if (remainingBytes_ == 0) {
119  LOG(ERROR) << "remainingBytes_ == 0";
120  return DecodeError::BUFFER_UNDERFLOW;
121  }
122  byte = next();
123  if (fexp > 64) {
124  // overflow in factorizer, f > 2^64
125  LOG(ERROR) << "overflow fexp=" << fexp;
126  return DecodeError::INTEGER_OVERFLOW;
127  }
128  uint64_t add = (byte & 127) * f;
129  if (std::numeric_limits<uint64_t>::max() - integer < add) {
130  // overflow detected
131  LOG(ERROR) << "overflow integer=" << integer << " add=" << add;
132  return DecodeError::INTEGER_OVERFLOW;
133  }
134  integer += add;
135  f = f << 7;
136  fexp += 7;
137  } while (byte & 128);
138  return DecodeError::NONE;
139 }
auto f
LogLevel max
Definition: LogLevel.cpp:31
auto add
Definition: BaseTest.cpp:70
const uint8_t NBIT_MASKS[9]
DecodeError proxygen::HPACKDecodeBuffer::decodeInteger ( uint64_t integer)

As above but with no prefix

Definition at line 97 of file HPACKDecodeBuffer.cpp.

References decodeInteger().

97  {
98  return decodeInteger(8, integer);
99 }
HPACK::DecodeError decodeInteger(uint8_t nbit, uint64_t &integer)
DecodeError proxygen::HPACKDecodeBuffer::decodeLiteral ( folly::fbstring literal)

decode a literal starting from the current position

Definition at line 44 of file HPACKDecodeBuffer.cpp.

Referenced by cursor(), proxygen::HPACKDecoder::decodeLiteralHeader(), and proxygen::QPACKDecoder::decodeLiteralHeaderQ().

44  {
45  return decodeLiteral(7, literal);
46 }
HPACK::DecodeError decodeLiteral(folly::fbstring &literal)
DecodeError proxygen::HPACKDecodeBuffer::decodeLiteral ( uint8_t  nbit,
folly::fbstring literal 
)

Definition at line 48 of file HPACKDecodeBuffer.cpp.

References folly::basic_fbstring< E, T, A, Storage >::append(), folly::basic_fbstring< E, T, A, Storage >::clear(), cursor_, data, folly::io::detail::CursorBase< Derived, BufType >::data(), folly::IOBuf::data(), decodeInteger(), proxygen::ERROR, proxygen::huffman::huffTree(), folly::io::detail::CursorBase< Derived, BufType >::length(), maxLiteralSize_, proxygen::NONE, peek(), folly::io::detail::CursorBase< Derived, BufType >::pull(), remainingBytes_, folly::size(), folly::io::detail::CursorBase< Derived, BufType >::skip(), uint64_t, uint8_t, and folly::IOBuf::writableData().

49  {
50  literal.clear();
51  if (remainingBytes_ == 0) {
52  LOG(ERROR) << "remainingBytes_ == 0";
53  return DecodeError::BUFFER_UNDERFLOW;
54  }
55  auto byte = peek();
56  uint8_t huffmanCheck = uint8_t(1 << nbit);
57  bool huffman = byte & huffmanCheck;
58  // extract the size
59  uint64_t size;
60  DecodeError result = decodeInteger(nbit, size);
61  if (result != DecodeError::NONE) {
62  LOG(ERROR) << "Could not decode literal size";
63  return result;
64  }
65  if (size > remainingBytes_) {
66  LOG(ERROR) << "size > remainingBytes_ decoding literal size="
67  << size << " remainingBytes_=" << remainingBytes_;
68  return DecodeError::BUFFER_UNDERFLOW;
69  }
70  if (size > maxLiteralSize_) {
71  LOG(ERROR) << "Literal too large, size=" << size;
72  return DecodeError::LITERAL_TOO_LARGE;
73  }
74  const uint8_t* data;
75  unique_ptr<IOBuf> tmpbuf;
76  // handle the case where the buffer spans multiple buffers
77  if (cursor_.length() >= size) {
78  data = cursor_.data();
79  cursor_.skip(size);
80  } else {
81  // temporary buffer to pull the chunks together
82  tmpbuf = IOBuf::create(size);
83  // pull() will move the cursor
84  cursor_.pull(tmpbuf->writableData(), size);
85  data = tmpbuf->data();
86  }
87  if (huffman) {
88  static auto& huffmanTree = huffman::huffTree();
89  huffmanTree.decode(data, size, literal);
90  } else {
91  literal.append((const char *)data, size);
92  }
94  return DecodeError::NONE;
95 }
const uint8_t * data() const
Definition: Cursor.h:105
folly::io::Cursor & cursor_
const uint8_t * data() const
Definition: IOBuf.h:499
void pull(void *buf, size_t len)
Definition: Cursor.h:418
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
uint8_t * writableData()
Definition: IOBuf.h:509
size_t length() const
Definition: Cursor.h:118
void skip(size_t len)
Definition: Cursor.h:371
HPACK::DecodeError decodeInteger(uint8_t nbit, uint64_t &integer)
const HuffTree & huffTree()
Definition: Huffman.cpp:252
basic_fbstring & append(const basic_fbstring &str)
Definition: FBString.h:1953
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
bool proxygen::HPACKDecodeBuffer::empty ( )
Returns
true if there are no more bytes to decode. Calling this method might move the cursor from the current IOBuf to the next one

Definition at line 22 of file HPACKDecodeBuffer.cpp.

References remainingBytes_.

Referenced by cursor(), proxygen::HPACKDecoder::decodeStreaming(), proxygen::QPACKDecoder::decodeStreamingImpl(), and proxygen::QPACKDecoder::handleBaseIndex().

22  {
23  return remainingBytes_ == 0;
24 }
uint8_t proxygen::HPACKDecodeBuffer::next ( )

extracts one byte from the buffer and advances the cursor

Definition at line 26 of file HPACKDecodeBuffer.cpp.

References cursor_, peek(), remainingBytes_, folly::io::detail::CursorBase< Derived, BufType >::skip(), and uint8_t.

Referenced by cursor(), decodeInteger(), and proxygen::HPACKDecoder::decodeLiteralHeader().

26  {
27  CHECK_GT(remainingBytes_, 0);
28  // in case we are the end of an IOBuf, peek() will move to the next one
29  uint8_t byte = peek();
30  cursor_.skip(1);
32 
33  return byte;
34 }
folly::io::Cursor & cursor_
void skip(size_t len)
Definition: Cursor.h:371
void proxygen::HPACKDecodeBuffer::reset ( folly::io::Cursor cursorVal)
inline

Definition at line 34 of file HPACKDecodeBuffer.h.

References folly::io::detail::CursorBase< Derived, BufType >::totalLength().

34  {
35  reset(cursorVal, folly::to<uint32_t>(cursorVal.totalLength()));
36  }
void reset(folly::io::Cursor &cursorVal)
size_t totalLength() const
Definition: Cursor.h:126
void proxygen::HPACKDecodeBuffer::reset ( folly::io::Cursor cursorVal,
uint32_t  totalBytes 
)
inline

Definition at line 38 of file HPACKDecodeBuffer.h.

References cursor_, remainingBytes_, and totalBytes_.

39  {
40  cursor_ = cursorVal;
41  totalBytes_ = totalBytes;
42  remainingBytes_ = totalBytes;
43  }
folly::io::Cursor & cursor_

Member Data Documentation

folly::io::Cursor& proxygen::HPACKDecodeBuffer::cursor_
private

Definition at line 88 of file HPACKDecodeBuffer.h.

Referenced by cursor(), decodeLiteral(), next(), peek(), and reset().

uint32_t proxygen::HPACKDecodeBuffer::maxLiteralSize_ {std::numeric_limits<uint32_t>::max()}
private

Definition at line 91 of file HPACKDecodeBuffer.h.

Referenced by decodeLiteral().

uint32_t proxygen::HPACKDecodeBuffer::remainingBytes_
private

Definition at line 90 of file HPACKDecodeBuffer.h.

Referenced by consumedBytes(), decodeInteger(), decodeLiteral(), empty(), next(), peek(), and reset().

uint32_t proxygen::HPACKDecodeBuffer::totalBytes_
private

Definition at line 89 of file HPACKDecodeBuffer.h.

Referenced by consumedBytes(), and reset().


The documentation for this class was generated from the following files: