BinaryTokenizer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SRC_PARSER_BINARYTOKENIZER_H
10 #define SQUID_SRC_PARSER_BINARYTOKENIZER_H
11 
12 #include "ip/forward.h"
13 #include "sbuf/SBuf.h"
14 
15 namespace Parser
16 {
17 
18 class BinaryTokenizer;
19 
22 {
23 public:
25  explicit BinaryTokenizerContext(BinaryTokenizer &tk, const char *aName);
27 
29  inline void close();
30 
32  inline void success();
33 
36  const char *const name;
37  uint64_t start;
38 };
39 
46 {
47 public:
48  class InsufficientInput {}; // thrown when a method runs out of data
49  typedef uint64_t size_type; // enough for the largest supported offset
50 
52  explicit BinaryTokenizer(const SBuf &data, const bool expectMore = false);
53 
56  void reset(const SBuf &data, const bool expectMore);
57 
60  void reinput(const SBuf &data, const bool expectMore) { data_ = data; expectMore_ = expectMore; }
61 
63  void commit();
64 
66  void rollback();
67 
69  bool atEnd() const;
70 
72  uint8_t uint8(const char *description);
73 
75  uint16_t uint16(const char *description);
76 
78  uint32_t uint24(const char *description);
79 
81  uint32_t uint32(const char *description);
82 
84  SBuf area(uint64_t size, const char *description);
85 
87  Ip::Address inet4(const char *description);
88 
90  Ip::Address inet6(const char *description);
91 
92  /*
93  * Variable-length arrays (a.k.a. Pascal or prefix strings).
94  * pstringN() extracts and returns N-bit length followed by length bytes
95  */
96  SBuf pstring8(const char *description);
97  SBuf pstring16(const char *description);
98  SBuf pstring24(const char *description);
99 
101  void skip(uint64_t size, const char *description);
102 
104  uint64_t parsed() const { return parsed_; }
105 
107  SBuf leftovers() const { return data_.substr(parsed_); }
108 
110  void got(uint64_t size, const char *description) const;
111 
113 
114 protected:
115  uint32_t octet();
116  void want(uint64_t size, const char *description) const;
117  void got(uint32_t value, uint64_t size, const char *description) const;
118  void got(const SBuf &value, uint64_t size, const char *description) const;
119  void got(const Ip::Address &value, uint64_t size, const char *description) const;
120  void skipped(uint64_t size, const char *description) const;
121 
122 private:
123  template <class InAddr>
124  Ip::Address inetAny(const char *description);
125 
127  uint64_t parsed_;
128  uint64_t syncPoint_;
129  bool expectMore_;
130 };
131 
132 /* BinaryTokenizerContext */
133 
134 inline
136  tokenizer(tk),
137  parent(tk.context),
138  name(aName),
139  start(tk.parsed())
140 {
141  tk.context = this;
142 }
143 
144 inline
145 void
148 }
149 
150 inline
151 void
154  close();
155 }
156 
157 } /* namespace Parser */
158 
159 #endif // SQUID_SRC_PARSER_BINARYTOKENIZER_H
160 
uint64_t syncPoint_
where to re-start the next parsing attempt
SBuf leftovers() const
yet unparsed bytes
bool expectMore_
whether more data bytes may arrive in the future
void close()
ends parsing named object; repeated calls OK
const char *const name
this context description or nullptr
Definition: SBuf.h:86
uint64_t parsed_
number of data bytes parsed or skipped
Generic protocol-agnostic parsing tools.
Definition: RequestParser.h:15
const BinaryTokenizerContext * context
debugging: thing being parsed
void success()
reports successful parsing of a named object and calls close()
unsigned short uint16
Definition: rfcnb-priv.h:38
void const char HLPCB void * data
Definition: stub_helper.cc:16
unsigned int uint32
Definition: md4.c:41
uint64_t parsed() const
the number of already parsed bytes
void reinput(const SBuf &data, const bool expectMore)
SBuf substr(size_type pos, size_type n=npos) const
Definition: SBuf.cc:586
void got(uint64_t size, const char *description) const
debugging helper for parsed multi-field structures
enables efficient debugging with concise field names: Hello.version.major
uint64_t start
context parsing begins at this tokenizer position
BinaryTokenizer & tokenizer
tokenizer being used for parsing
BinaryTokenizerContext(BinaryTokenizer &tk, const char *aName)
starts parsing named object
const BinaryTokenizerContext *const parent
enclosing context or nullptr
int size
Definition: ModDevPoll.cc:77

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors