reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ParseBuffer.hxx
Go to the documentation of this file.
1#if !defined(RESIP_PARSEBUFFER_HXX)
2#define RESIP_PARSEBUFFER_HXX
3
4#include "rutil/Data.hxx"
6
7namespace resip
8{
9
18{
19 public:
20 // does NOT OWN the buffer memory
21 ParseBuffer(const char* buff, size_t len,
22 const Data& errorContext = Data::Empty);
23
24 explicit ParseBuffer(const char* buff,
25 const Data& errorContext = Data::Empty);
26
27 explicit ParseBuffer(const Data& data,
28 const Data& errorContext = Data::Empty);
29
30 ParseBuffer(const ParseBuffer& other);
31
33 // .bwc. Backwards-compatibility hack; ParseException used to be
34 // a full inner class of ParseBuffer, and we also had a separate
35 // ParseException class that other things used. For consistency, we have
36 // moved everything over to use ParseException, but there is app-code
37 // out there that still expects ParseException to exist.
38 typedef ParseException Exception;
40
41 private:
48 class CurrentPosition
49 {
50 public:
51 inline explicit CurrentPosition(const ParseBuffer& pb) :
52 mPb(pb)
53 {}
54
55 operator const char*() const
56 {
57 return mPb.mPosition;
58 }
59
60 const char& operator*() const
61 {
62 mPb.assertNotEof();
63 return *mPb.mPosition;
64 }
65
66 const ParseBuffer& mPb;
67 };
68
74 class Pointer
75 {
76 public:
77 Pointer(const ParseBuffer& pb,
78 const char* position,
79 bool atEof);
80 Pointer(const CurrentPosition& pos);
81
82 operator const char*() const
83 {
84 return mPosition;
85 }
86
87 const char& operator*() const;
88 private:
89 const ParseBuffer& mPb;
90 const char* mPosition;
91 const bool mIsValid;
92 static const Data msg;
93 };
94
95 public:
96 const Data& getContext() const {return mErrorContext;}
97
98 // allow the buffer to be rolled back
99 ParseBuffer& operator=(const ParseBuffer& other);
100 void reset(const char* pos)
101 {
102 resip_assert( mBuff <= mEnd);
103 resip_assert( (pos >= mBuff) && (pos <= mEnd) );
104 mPosition = pos;
105 }
106
107 // abcdef
108 // ^ ^
109 // begin end
110 bool eof() const { return mPosition >= mEnd;}
111 bool bof() const { return mPosition <= mBuff;}
112 bool valid() const {return (!eof()) && (!bof());}
113 Pointer start() const { return Pointer(*this, mBuff, eof()); }
114 CurrentPosition position() const { return CurrentPosition(*this); }
115 Pointer end() const { return Pointer(*this, mEnd, true); }
116 size_t lengthRemaining() { return mEnd - mPosition; }
117
118 CurrentPosition skipChar()
119 {
120 if (eof())
121 {
122 fail(__FILE__, __LINE__,"skipped over eof");
123 }
124 ++mPosition;
125 return CurrentPosition(*this);
126 }
127
128 CurrentPosition skipChar(char c);
129 CurrentPosition skipChars(const char* cs);
130 CurrentPosition skipChars(const Data& cs);
131 CurrentPosition skipNonWhitespace();
132 CurrentPosition skipWhitespace();
133 CurrentPosition skipLWS();
134 CurrentPosition skipToTermCRLF();
135 CurrentPosition skipToChar(char c)
136 {
137 mPosition = (const char*)memchr(mPosition, c, mEnd-mPosition);
138 if(!mPosition)
139 {
140 mPosition=mEnd;
141 }
142 return CurrentPosition(*this);
143 }
144 CurrentPosition skipToChars(const char* cs);
145 CurrentPosition skipToChars(const Data& cs); // ?dlb? case sensitivity arg?
146 CurrentPosition skipToOneOf(const char* cs);
147 CurrentPosition skipToOneOf(const char* cs1, const char* cs2);
148 CurrentPosition skipToOneOf(const Data& cs);
149 CurrentPosition skipToOneOf(const Data& cs1, const Data& cs2);
150
151 // std::bitset based parse function
152 CurrentPosition skipChars(const std::bitset<256>& cs)
153 {
154 while (mPosition < mEnd)
155 {
156 if (cs.test((unsigned char)(*mPosition)))
157 {
158 mPosition++;
159 }
160 else
161 {
162 return CurrentPosition(*this);
163 }
164 }
165 return CurrentPosition(*this);
166 }
167
168 CurrentPosition skipToOneOf(const std::bitset<256>& cs)
169 {
170 while (mPosition < mEnd)
171 {
172 if (cs.test((unsigned char)(*mPosition)))
173 {
174 return CurrentPosition(*this);
175 }
176 else
177 {
178 mPosition++;
179 }
180 }
181 return CurrentPosition(*this);
182 }
183
184 const char* skipToEndQuote(char quote = '"');
185 CurrentPosition skipN(size_t count)
186 {
187 mPosition += count;
188 if (mPosition > mEnd)
189 {
190 fail(__FILE__, __LINE__, "skipped eof");
191 }
192 return CurrentPosition(*this);
193 }
194
195 CurrentPosition skipToEnd()
196 {
197 mPosition = mEnd;
198 return CurrentPosition(*this);
199 }
200
201 // inverse of skipChar() -- end up at char not before it
202 const char* skipBackChar();
203 const char* skipBackWhitespace();
204 const char* skipBackN(int count)
205 {
206 mPosition -= count;
207 if (bof())
208 {
209 fail(__FILE__, __LINE__,"backed over beginning of buffer");
210 }
211 return mPosition;
212 }
213
214 const char* skipBackChar(char c);
215 const char* skipBackToChar(char c);
216 const char* skipBackToOneOf(const char* cs);
217
218 void assertEof() const
219 {
220 if (!eof())
221 {
222 fail(__FILE__, __LINE__,"expected eof");
223 }
224 }
225
226 void assertNotEof() const
227 {
228 if (eof())
229 {
230 fail(__FILE__, __LINE__,"unexpected eof");
231 }
232 }
233
234 void fail(const char* file, unsigned int line,
235 const Data& errmsg = Data::Empty) const;
236
238 void data(Data& data, const char* start) const;
239
240 Data data(const char* start) const;
241
242 void dataUnescaped(Data& data, const char* start) const;
243
244 int integer();
245
246
247 uint8_t uInt8();
250
252 RESIP_DEPRECATED(unsigned long unsignedInteger()){return uInt32();}
253
254#ifndef RESIP_FIXED_POINT
255 float floatVal();
256#endif
257 int qVal();
258
259 static bool oneOf(char c, const char* cs);
260 static bool oneOf(char c, const Data& cs);
261 static const char* Whitespace;
262 static const char* ParamTerm;
263 private:
265 const char* mBuff;
266 const char* mPosition;
267 const char* mEnd;
268 const Data& mErrorContext;
269};
270
271}
272
273#endif
274
275/* ====================================================================
276 * The Vovida Software License, Version 1.0
277 *
278 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
279 *
280 * Redistribution and use in source and binary forms, with or without
281 * modification, are permitted provided that the following conditions
282 * are met:
283 *
284 * 1. Redistributions of source code must retain the above copyright
285 * notice, this list of conditions and the following disclaimer.
286 *
287 * 2. Redistributions in binary form must reproduce the above copyright
288 * notice, this list of conditions and the following disclaimer in
289 * the documentation and/or other materials provided with the
290 * distribution.
291 *
292 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
293 * and "Vovida Open Communication Application Library (VOCAL)" must
294 * not be used to endorse or promote products derived from this
295 * software without prior written permission. For written
296 * permission, please contact vocal@vovida.org.
297 *
298 * 4. Products derived from this software may not be called "VOCAL", nor
299 * may "VOCAL" appear in their name, without prior written
300 * permission of Vovida Networks, Inc.
301 *
302 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
303 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
304 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
305 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
306 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
307 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
308 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
309 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
310 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
311 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
312 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
313 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
314 * DAMAGE.
315 *
316 * ====================================================================
317 *
318 * This software consists of voluntary contributions made by Vovida
319 * Networks, Inc. and many individuals on behalf of Vovida Networks,
320 * Inc. For more information on Vovida Networks, Inc., please see
321 * <http://www.vovida.org/>.
322 *
323 */
#define resip_assert(x)
This file needs to work for both C and C++ code.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
static const Data Empty
Constant that represents a zero-length data.
Definition Data.hxx:882
Provides string-parsing functionality with protection from buffer overflow.
CurrentPosition skipLWS()
CurrentPosition skipNonWhitespace()
void dataUnescaped(Data &data, const char *start) const
const char * skipBackToChar(char c)
uint32_t uInt32()
dcm! – merge these, ask about length checks
Pointer start() const
CurrentPosition skipToEnd()
ParseBuffer & operator=(const ParseBuffer &other)
Pointer end() const
static const char * ParamTerm
const char * skipBackChar()
CurrentPosition skipWhitespace()
CurrentPosition skipToOneOf(const std::bitset< 256 > &cs)
CurrentPosition skipToChars(const char *cs)
unsigned long unsignedInteger()
CurrentPosition skipToChar(char c)
CurrentPosition position() const
static bool oneOf(char c, const char *cs)
void reset(const char *pos)
CurrentPosition skipChars(const char *cs)
const Data & getContext() const
void assertNotEof() const
const char * skipBackN(int count)
CurrentPosition skipToOneOf(const char *cs)
CurrentPosition skipN(size_t count)
CurrentPosition skipChar()
CurrentPosition skipToTermCRLF()
void fail(const char *file, unsigned int line, const Data &errmsg=Data::Empty) const
void data(Data &data, const char *start) const
make the passed in data share memory with the buffer (uses Data::Share)
CurrentPosition skipChars(const std::bitset< 256 > &cs)
friend class ParseBuffer::CurrentPosition
const char * skipToEndQuote(char quote='"')
uint64_t unsignedLongLong()
const char * skipBackWhitespace()
const char * skipBackToOneOf(const char *cs)
static const char * Whitespace
void assertEof() const
BaseException used to indicate parse failures.
#define RESIP_DEPRECATED(x)
Definition compat.hxx:334
dcm! – add UnusedChecking(_enum) below;
unsigned long long uint64_t
Definition Stun.hxx:39
unsigned int uint32_t
Definition Stun.hxx:34
unsigned char uint8_t
Definition Stun.hxx:29