BinaryTokenizer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 "parser/forward.h"
14 #include "sbuf/SBuf.h"
15 
16 namespace Parser
17 {
18 
19 class BinaryTokenizer;
20 
23 {
24 public:
26  explicit BinaryTokenizerContext(BinaryTokenizer &tk, const char *aName);
28 
30  inline void close();
31 
33  inline void success();
34 
37  const char *const name;
38  uint64_t start;
39 };
40 
47 {
48 public:
49  typedef ::Parser::InsufficientInput InsufficientInput;
50  typedef uint64_t size_type; // enough for the largest supported offset
51 
53  explicit BinaryTokenizer(const SBuf &data, const bool expectMore = false);
54 
57  void reset(const SBuf &data, const bool expectMore);
58 
61  void reinput(const SBuf &data, const bool expectMore) { data_ = data; expectMore_ = expectMore; }
62 
64  void commit();
65 
67  void rollback();
68 
70  bool atEnd() const;
71 
73  uint8_t uint8(const char *description);
74 
76  uint16_t uint16(const char *description);
77 
79  uint32_t uint24(const char *description);
80 
82  uint32_t uint32(const char *description);
83 
85  SBuf area(uint64_t size, const char *description);
86 
88  Ip::Address inet4(const char *description);
89 
91  Ip::Address inet6(const char *description);
92 
93  /*
94  * Variable-length arrays (a.k.a. Pascal or prefix strings).
95  * pstringN() extracts and returns N-bit length followed by length bytes
96  */
97  SBuf pstring8(const char *description);
98  SBuf pstring16(const char *description);
99  SBuf pstring24(const char *description);
100 
102  void skip(uint64_t size, const char *description);
103 
105  uint64_t parsed() const { return parsed_; }
106 
108  SBuf leftovers() const { return data_.substr(parsed_); }
109 
111  void got(uint64_t size, const char *description) const;
112 
114 
115 protected:
116  uint32_t octet();
117  void want(uint64_t size, const char *description) const;
118  void got(uint32_t value, uint64_t size, const char *description) const;
119  void got(const SBuf &value, uint64_t size, const char *description) const;
120  void got(const Ip::Address &value, uint64_t size, const char *description) const;
121  void skipped(uint64_t size, const char *description) const;
122 
123 private:
124  template <class InAddr>
125  Ip::Address inetAny(const char *description);
126 
128  uint64_t parsed_;
129  uint64_t syncPoint_;
130  bool expectMore_;
131 };
132 
133 /* BinaryTokenizerContext */
134 
135 inline
137  tokenizer(tk),
138  parent(tk.context),
139  name(aName),
140  start(tk.parsed())
141 {
142  tk.context = this;
143 }
144 
145 inline
146 void
149 }
150 
151 inline
152 void
155  close();
156 }
157 
158 } /* namespace Parser */
159 
160 #endif /* SQUID_SRC_PARSER_BINARYTOKENIZER_H */
161 
const BinaryTokenizerContext *const parent
enclosing context or nullptr
const BinaryTokenizerContext * context
debugging: thing being parsed
uint64_t parsed_
number of data bytes parsed or skipped
void reset(const SBuf &data, const bool expectMore)
uint64_t parsed() const
the number of already parsed bytes
SBuf area(uint64_t size, const char *description)
parse size consecutive bytes as an opaque blob
Ip::Address inet6(const char *description)
interpret the next 16 bytes as a raw in6_addr structure
::Parser::InsufficientInput InsufficientInput
Definition: SBuf.h:93
void commit()
make progress: future parsing failures will not rollback beyond this point
uint32_t uint24(const char *description)
parse a three-byte unsigned integer (returned as uint32_t)
void close()
ends parsing named object; repeated calls OK
const char *const name
this context description or nullptr
SBuf pstring24(const char *description)
up to 16 MiB-long p-string!
Ip::Address inetAny(const char *description)
SBuf substr(size_type pos, size_type n=npos) const
Definition: SBuf.cc:576
Ip::Address inet4(const char *description)
interpret the next 4 bytes as a raw in_addr structure
void rollback()
resume [incremental] parsing from the last commit point
void skip(uint64_t size, const char *description)
ignore the next size bytes
uint64_t start
context parsing begins at this tokenizer position
int size
Definition: ModDevPoll.cc:69
uint8_t uint8(const char *description)
parse a single-byte unsigned integer
Generic protocol-agnostic parsing tools.
Definition: RequestParser.h:15
void success()
reports successful parsing of a named object and calls close()
bool expectMore_
whether more data bytes may arrive in the future
void skipped(uint64_t size, const char *description) const
debugging helper for skipped fields
bool atEnd() const
no more bytes to parse or skip
SBuf leftovers() const
yet unparsed bytes
enables efficient debugging with concise field names: Hello.version.major
void got(uint64_t size, const char *description) const
debugging helper for parsed multi-field structures
void reinput(const SBuf &data, const bool expectMore)
BinaryTokenizer & tokenizer
tokenizer being used for parsing
SBuf pstring8(const char *description)
up to 255 byte-long p-string
BinaryTokenizerContext(BinaryTokenizer &tk, const char *aName)
starts parsing named object
void want(uint64_t size, const char *description) const
logs and throws if fewer than size octets remain; no other side effects
uint16_t uint16(const char *description)
parse a two-byte unsigned integer
uint64_t syncPoint_
where to re-start the next parsing attempt
uint32_t uint32(const char *description)
parse a four-byte unsigned integer
SBuf pstring16(const char *description)
up to 64 KiB-long p-string

 

Introduction

Documentation

Support

Miscellaneous