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);