File: wvbackslash.cc

package info (click to toggle)
wvstreams 4.0.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,420 kB
  • ctags: 6,518
  • sloc: cpp: 52,544; sh: 5,770; ansic: 810; makefile: 461; tcl: 114; perl: 18
file content (277 lines) | stat: -rw-r--r-- 7,082 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 2002 Net Integration Technologies, Inc.
 * 
 * Performs C-style backslash escaping and unescaping of strings.
 */
#include <ctype.h>
#include "wvbackslash.h"

static const char *escapein = "\a\b\f\n\r\t\v";
static const char *escapeout = "abfnrtv";

static inline char tohex(int digit, char alphabase = ('a' - 10))
{
    return (digit < 10 ? '0' : alphabase) + digit;
}

static inline int fromhex(char digit)
{
    if (isdigit(digit))
        return digit - '0';
    if (digit >= 'A' && digit <= 'F')
        return digit - 'A' + 10;
    if (digit >= 'a' && digit <= 'f')
        return digit - 'a' + 10;
    return -1;
}

static inline int fromoctal(char digit)
{
    if (digit >= '0' && digit <= '7')
        return digit - '0';
    return -1;
}


/***** WvBackslashEncoder *****/

WvBackslashEncoder::WvBackslashEncoder(WvStringParm _nasties) :
    nasties(_nasties)
{
}


bool WvBackslashEncoder::_encode(WvBuf &inbuf, WvBuf &outbuf,
    bool flush)
{
    size_t avail = outbuf.free();
    size_t len;
    while ((len = inbuf.optgettable()) != 0)
    {
        const unsigned char *datain = inbuf.get(len);
        for (size_t i = 0; i < len; ++i)
        {
            int c = datain[i];
            
            // handle 1 character escape sequences
            if (avail < 1)
                { outbuf.unget(len - i); return ! flush; }
            const char *foundnasty = NULL;
            const char *foundspecial = NULL;
            if (c != '\0')
            {
                foundnasty = strchr(nasties.cstr(), c);
                if (! foundnasty)
                {
                    foundspecial = strchr(escapein, c);
                    if (! foundspecial && isprint(c))
                    {
                        outbuf.putch(c);
                        avail -= 1;
                        continue;
                    }
                }
            }
            
            // handle 2 character escape sequences
            if (avail < 2)
                { outbuf.unget(len - i); return ! flush; }
            if (foundnasty != NULL)
            {
                outbuf.putch('\\');
                outbuf.putch(c);
                avail -= 2;
                continue;
            }
            if (foundspecial != NULL)
            {
                outbuf.putch('\\');
                outbuf.putch(escapeout[foundspecial - escapein]);
                avail -= 2;
                continue;
            }

            // handle 4 character escape sequences
            if (avail < 4)
                { outbuf.unget(len - i); return ! flush; }
            outbuf.put("\\x", 2);
            outbuf.putch(tohex(c >> 4));
            outbuf.putch(tohex(c & 15));
            avail -= 4;
        }
    }
    return true;
}


bool WvBackslashEncoder::_reset()
{
    return true;
}


/***** WvBackslashDecoder *****/

WvBackslashDecoder::WvBackslashDecoder() : tmpbuf(4)
{
    _reset();
}


bool WvBackslashDecoder::_encode(WvBuf &inbuf, WvBuf &outbuf,
    bool flush)
{
    if (outbuf.free() == 0)
        return inbuf.used() == 0;
    if (! flushtmpbuf(outbuf))
        return false;

    size_t len;
    while ((len = inbuf.optgettable()) != 0)
    {
        const unsigned char *datain = inbuf.get(len);
        for (size_t i = 0; i < len; ++i)
        {
            int c = datain[i];

            switch (state)
            {
                case Initial:
                    if (c == '\\')
                        state = Escape;
                    tmpbuf.putch(c);
                    break;
                
                case Escape:
                    if (c >= '0' && c <= '3')
                    {
                        tmpbuf.unalloc(1);
                        value = c - '0';
                        state = Octal1;
                    }
                    else if (c == 'x')
                    {
                        tmpbuf.putch(c);
                        state = Hex1;
                    }
                    else if (c == '\n')
                    {
                        // line continuation sequence
                        tmpbuf.unalloc(1);
                        tmpbuf.putch(' ');
                        state = Initial;
                    }
                    else
                    {
                        const char *found = strchr(escapeout, c);
                        tmpbuf.unalloc(1);
                        if (found != NULL)
                            c = escapein[found - escapeout];
                        // else we just drop the backslash
                        tmpbuf.putch(c);
                        state = Initial;
                    }
                    break;

                case Hex2:
                case Hex1: {
                    int digit = fromhex(c);
                    if (digit >= 0)
                    {
                        if (state == Hex1)
                        {
                            tmpbuf.unalloc(2);
                            value = digit;
                            state = Hex2;
                        }
                        else
                        {
                            value = (value << 4) | digit;
                            state = Initial;
                        }
                    }
                    else
                    {
                        i -= 1;
                        state = Initial;
                    }
                    break;
                }

                case Octal3:
                case Octal2:
                case Octal1: {
                    int digit = fromoctal(c);
                    if (digit >= 0)
                    {
                        value = (value << 3) | digit;
                        if (state != Octal3)
                            state = State(state + 1);
                        else
                            state = Initial;
                    }
                    else
                    {
                        i -= 1;
                        state = Initial;
                    }
                    break;
                }
            }

            flushtmpbuf(outbuf);
            if (outbuf.free() == 0)
            {
                inbuf.unget(len - i);
                break;
            }
        }
    }
    if (flush)
    {
        if (inbuf.used() != 0)
            return false;
        state = Initial;
        return flushtmpbuf(outbuf);
    }
    return true;

}


bool WvBackslashDecoder::_reset()
{
    state = Initial;
    value = -1;
    tmpbuf.zap();
    return true;
}


bool WvBackslashDecoder::flushtmpbuf(WvBuf &outbuf)
{
    if (state != Initial)
        return true;
        
    if (value != -1)
    {
        tmpbuf.putch(value);
        value = -1;
    }
    
    size_t len = tmpbuf.used();
    if (len == 0)
        return true;
    size_t avail = outbuf.free();
    if (avail > len)
        avail = len;
    outbuf.merge(tmpbuf, avail);
    len -= avail;
    if (len == 0)
    {
        tmpbuf.zap();
        return true;
    }
    return false;
}