OpenVPN 3 Core Library
Loading...
Searching...
No Matches
lex.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// A set of lexical analyzer classes. These classes can be combined
13// with the methods in split.hpp to create parsers.
14
15#pragma once
16
18
19namespace openvpn {
20
21// This class (or others that define an is_space method) is used as a
22// template parameter to methods in split.hpp.
24{
25 static bool is_space(char c)
26 {
27 return string::is_space(c);
28 }
29};
30
39{
40 public:
45 bool in_quote() const
46 {
47 return in_squote | in_dquote;
48 }
49
50 protected:
60 bool handle_quote(char c)
61 {
62 if ((c == '\"') && (!in_squote))
63 {
65 return true;
66 }
67
68 if ((c == '\'') && (!in_dquote))
69 {
71 return true;
72 }
73
74 return false;
75 }
76
77 private:
78 bool in_squote = false;
79 bool in_dquote = false;
80};
81
82// A basic lexical analyzer that understands quoting
84{
85 public:
86 void put(char c)
87 {
88 in_backslash_ = false;
89 if (backslash_)
90 {
91 ch = c;
92 backslash_ = false;
93 in_backslash_ = true;
94 }
95 else if (c == '\\')
96 {
97 backslash_ = true;
98 ch = -1;
99 }
100 else if (handle_quote(c))
101 {
102 ch = -1;
103 }
104 else
105 ch = c;
106 }
107
108 bool available() const
109 {
110 return ch != -1;
111 }
112 int get() const
113 {
114 return ch;
115 }
116 void reset()
117 {
118 ch = -1;
119 }
120 bool in_backslash() const
121 {
122 return in_backslash_;
123 }
124
125 private:
126 bool backslash_ = false;
127 bool in_backslash_ = false;
128 int ch = -1;
129};
130
131// A null lexical analyzer has no special understanding
132// of any particular string character.
134{
135 public:
136 void put(char c)
137 {
138 ch = c;
139 }
140 bool available() const
141 {
142 return ch != -1;
143 }
144 int get() const
145 {
146 return ch;
147 }
148 void reset()
149 {
150 ch = -1;
151 }
152 bool in_quote() const
153 {
154 return false;
155 }
156 bool in_backslash() const
157 {
158 return false;
159 }
160
161 private:
162 int ch = -1;
163};
164
165} // namespace openvpn
Helper class to handle quote processing.
Definition lex.hpp:39
bool in_quote() const
Check if currently inside a quote.
Definition lex.hpp:45
bool handle_quote(char c)
Handle a character as a potential quote.
Definition lex.hpp:60
bool in_backslash() const
Definition lex.hpp:156
int get() const
Definition lex.hpp:144
bool in_quote() const
Definition lex.hpp:152
void put(char c)
Definition lex.hpp:136
bool available() const
Definition lex.hpp:140
void reset()
Definition lex.hpp:148
void put(char c)
Definition lex.hpp:86
bool in_backslash() const
Definition lex.hpp:120
bool available() const
Definition lex.hpp:108
int get() const
Definition lex.hpp:112
bool is_space(const char c)
Definition string.hpp:279
static bool is_space(char c)
Definition lex.hpp:25