8 #include "wvbackslash.h"
10 static const char *escapein =
"\a\b\f\n\r\t\v";
11 static const char *escapeout =
"abfnrtv";
13 static inline char tohex(
int digit,
char alphabase = (
'a' - 10))
15 return (digit < 10 ?
'0' : alphabase) + digit;
18 static inline int fromhex(
char digit)
22 if (digit >=
'A' && digit <=
'F')
23 return digit -
'A' + 10;
24 if (digit >=
'a' && digit <=
'f')
25 return digit -
'a' + 10;
29 static inline int fromoctal(
char digit)
31 if (digit >=
'0' && digit <=
'7')
48 size_t avail = outbuf.
free();
52 const unsigned char *datain = inbuf.
get(len);
53 for (
size_t i = 0; i < len; ++i)
60 const char *foundnasty = NULL;
61 const char *foundspecial = NULL;
64 foundnasty = strchr(nasties.
cstr(), c);
67 foundspecial = strchr(escapein, c);
68 if (! foundspecial && isprint(c))
80 if (foundnasty != NULL)
87 if (foundspecial != NULL)
90 outbuf.
putch(escapeout[foundspecial - escapein]);
99 outbuf.
putch(tohex(c >> 4));
100 outbuf.
putch(tohex(c & 15));
125 if (outbuf.
free() == 0)
126 return inbuf.
used() == 0;
127 if (! flushtmpbuf(outbuf))
133 const unsigned char *datain = inbuf.
get(len);
134 for (
size_t i = 0; i < len; ++i)
147 if (c >=
'0' && c <=
'3')
167 const char *found = strchr(escapeout, c);
170 c = escapein[found - escapeout];
179 int digit = fromhex(c);
190 value = (value << 4) | digit;
205 int digit = fromoctal(c);
208 value = (value << 3) | digit;
210 state = State(state + 1);
224 if (outbuf.
free() == 0)
226 inbuf.
unget(len - i);
233 if (inbuf.
used() != 0)
236 return flushtmpbuf(outbuf);
252 bool WvBackslashDecoder::flushtmpbuf(
WvBuf &outbuf)
254 if (state != Initial)
263 size_t len = tmpbuf.
used();
266 size_t avail = outbuf.
free();
269 outbuf.
merge(tmpbuf, avail);
virtual bool _reset()
Template method implementation of reset().
WvBackslashDecoder()
Creates a C-style backslash decoder.
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
void unget(size_t count)
Ungets exactly the specified number of elements by returning them to the buffer for subsequent reads.
void unalloc(size_t count)
Unallocates exactly the specified number of elements by removing them from the buffer and releasing t...
size_t free() const
Returns the number of elements that the buffer can currently accept for writing.
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
void putch(int ch)
Puts a single character into the buffer as an int.
const char * cstr() const
return a (const char *) for this string.
virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
Template method implementation of encode().
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
void zap()
Clears the buffer.
virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
Template method implementation of encode().
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
size_t used() const
Returns the number of elements in the buffer currently available for reading.
WvBackslashEncoder(WvStringParm _nasties="\\\"")
Creates a C-style backslash encoder.
void merge(Buffer &inbuf, size_t count)
Efficiently moves count bytes from the specified buffer into this one.
virtual bool _reset()
Template method implementation of reset().