]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/parser/BinaryTokenizer.h
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / parser / BinaryTokenizer.h
index c94077f3bbafc72f19a19bffe8419dbada245c5d..850075521dabc49be9e1cad75c9fa540be78e7da 100644 (file)
@@ -1,14 +1,16 @@
 /*
- * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
  *
  * Squid software is distributed under GPLv2+ license and includes
  * contributions from numerous individuals and organizations.
  * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
-#ifndef SQUID_PARSER_BINARY_TOKENIZER_H
-#define SQUID_PARSER_BINARY_TOKENIZER_H
+#ifndef SQUID_SRC_PARSER_BINARYTOKENIZER_H
+#define SQUID_SRC_PARSER_BINARYTOKENIZER_H
 
+#include "ip/forward.h"
+#include "parser/forward.h"
 #include "sbuf/SBuf.h"
 
 namespace Parser
@@ -21,7 +23,7 @@ class BinaryTokenizerContext
 {
 public:
     /// starts parsing named object
-    inline explicit BinaryTokenizerContext(BinaryTokenizer &tk, const char *aName);
+    explicit BinaryTokenizerContext(BinaryTokenizer &tk, const char *aName);
     ~BinaryTokenizerContext() { close(); }
 
     /// ends parsing named object; repeated calls OK
@@ -31,19 +33,20 @@ public:
     inline void success();
 
     BinaryTokenizer &tokenizer; ///< tokenizer being used for parsing
-    const BinaryTokenizerContext *parent; ///< enclosing context or nullptr
-    const char *name; ///< this context description or nullptr
+    const BinaryTokenizerContext * const parent; ///< enclosing context or nullptr
+    const char *const name; ///< this context description or nullptr
     uint64_t start; ///< context parsing begins at this tokenizer position
 };
 
 /// Safely extracts byte-oriented (i.e., non-textual) fields from raw input.
+/// Assume that the integers are stored in network byte order.
 /// Supports commit points for atomic incremental parsing of multi-part fields.
 /// Throws InsufficientInput when more input is needed to parse the next field.
 /// Throws on errors.
 class BinaryTokenizer
 {
 public:
-    class InsufficientInput {}; // thrown when a method runs out of data
+    typedef ::Parser::InsufficientInput InsufficientInput;
     typedef uint64_t size_type; // enough for the largest supported offset
 
     BinaryTokenizer();
@@ -69,18 +72,24 @@ public:
     /// parse a single-byte unsigned integer
     uint8_t uint8(const char *description);
 
-    // parse a two-byte unsigned integer
+    /// parse a two-byte unsigned integer
     uint16_t uint16(const char *description);
 
-    // parse a three-byte unsigned integer (returned as uint32_t)
+    /// parse a three-byte unsigned integer (returned as uint32_t)
     uint32_t uint24(const char *description);
 
-    // parse a four-byte unsigned integer
+    /// parse a four-byte unsigned integer
     uint32_t uint32(const char *description);
 
     /// parse size consecutive bytes as an opaque blob
     SBuf area(uint64_t size, const char *description);
 
+    /// interpret the next 4 bytes as a raw in_addr structure
+    Ip::Address inet4(const char *description);
+
+    /// interpret the next 16 bytes as a raw in6_addr structure
+    Ip::Address inet6(const char *description);
+
     /*
      * Variable-length arrays (a.k.a. Pascal or prefix strings).
      * pstringN() extracts and returns N-bit length followed by length bytes
@@ -108,9 +117,13 @@ protected:
     void want(uint64_t size, const char *description) const;
     void got(uint32_t value, uint64_t size, const char *description) const;
     void got(const SBuf &value, uint64_t size, const char *description) const;
+    void got(const Ip::Address &value, uint64_t size, const char *description) const;
     void skipped(uint64_t size, const char *description) const;
 
 private:
+    template <class InAddr>
+    Ip::Address inetAny(const char *description);
+
     SBuf data_;
     uint64_t parsed_; ///< number of data bytes parsed or skipped
     uint64_t syncPoint_; ///< where to re-start the next parsing attempt
@@ -144,4 +157,5 @@ BinaryTokenizerContext::success() {
 
 } /* namespace Parser */
 
-#endif // SQUID_PARSER_BINARY_TOKENIZER_H
+#endif // SQUID_SRC_PARSER_BINARYTOKENIZER_H
+