~ubuntu-branches/ubuntu/precise/mysql-5.5/precise-201203300109

« back to all changes in this revision

Viewing changes to cmd-line-utils/libedit/np/unvis.c

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-02-14 23:59:22 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120214235922-cux5uek1e5l0hje9
Tags: 5.5.20-0ubuntu1
* New upstream release.
* d/mysql-server-5.5.mysql.upstart: Fix stop on to make sure mysql is
  fully stopped before shutdown commences. (LP: #688541) Also simplify
  start on as it is redundant.
* d/control: Depend on upstart version which has apparmor profile load
  script to prevent failure on upgrade from lucid to precise.
  (LP: #907465)
* d/apparmor-profile: need to allow /run since that is the true path
  of /var/run files. (LP: #917542)
* d/control: mysql-server-5.5 has files in it that used to be owned
  by libmysqlclient-dev, so it must break/replace it. (LP: #912487)
* d/rules, d/control: 5.5.20 Fixes segfault on tests with gcc 4.6,
  change compiler back to system default.
* d/rules: Turn off embedded libedit/readline.(Closes: #659566)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*      $NetBSD: unvis.c,v 1.28 2005/09/13 01:44:09 christos Exp $      */
 
1
/*      $NetBSD: unvis.c,v 1.36 2011/03/18 09:07:20 martin Exp $        */
2
2
 
3
3
/*-
4
4
 * Copyright (c) 1989, 1993
38
38
#endif
39
39
#endif /* LIBC_SCCS and not lint */
40
40
 
 
41
/* XXXMYSQL : Make compiler happy. */
 
42
#ifdef _LIBC
 
43
#include "namespace.h"
 
44
#endif
 
45
 
41
46
#include <sys/types.h>
42
47
 
43
48
#include <assert.h>
44
49
#include <ctype.h>
 
50
 
 
51
/* XXXMYSQL : stdint.h might not be available on older Solaris platforms. */
 
52
#if defined(__sun) || defined(__sun__)
 
53
#include <sys/inttypes.h>
 
54
#else
 
55
#include <stdint.h>
 
56
#endif
 
57
 
45
58
#include <stdio.h>
46
 
#ifdef HAVE_VIS_H
47
 
#include <vis.h>
48
 
#else
 
59
#include <errno.h>
 
60
/*
 
61
  XXXMYSQL : Due to different versions of vis.h available,
 
62
             use the one bundled with libedit.
 
63
*/
49
64
#include "np/vis.h"
50
 
#endif
51
65
 
52
66
#ifdef __weak_alias
53
 
__weak_alias(strunvis,_strunvis)
 
67
__weak_alias(strnunvisx,_strnunvisx)
54
68
#endif
55
69
 
56
70
#if !HAVE_VIS
64
78
#define S_CTRL          4       /* control char started (^) */
65
79
#define S_OCTAL2        5       /* octal digit 2 */
66
80
#define S_OCTAL3        6       /* octal digit 3 */
67
 
#define S_HEX1          7       /* hex digit */
68
 
#define S_HEX2          8       /* hex digit 2 */
 
81
#define S_HEX1          7       /* http hex digit */
 
82
#define S_HEX2          8       /* http hex digit 2 */
 
83
#define S_MIME1         9       /* mime hex digit 1 */
 
84
#define S_MIME2         10      /* mime hex digit 2 */
 
85
#define S_EATCRNL       11      /* mime eating CRNL */
 
86
#define S_AMP           12      /* seen & */
 
87
#define S_NUMBER        13      /* collecting number */
 
88
#define S_STRING        14      /* collecting string */
69
89
 
70
90
#define isoctal(c)      (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
71
91
#define xtod(c)         (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
 
92
#define XTOD(c)         (isdigit(c) ? (c - '0') : ((c - 'A') + 10))
 
93
 
 
94
/*
 
95
 * RFC 1866
 
96
 */
 
97
static const struct nv {
 
98
        const char *name;
 
99
        uint8_t value;
 
100
} nv[] = {
 
101
        { "AElig",      198 }, /* capital AE diphthong (ligature)  */
 
102
        { "Aacute",     193 }, /* capital A, acute accent  */
 
103
        { "Acirc",      194 }, /* capital A, circumflex accent  */
 
104
        { "Agrave",     192 }, /* capital A, grave accent  */
 
105
        { "Aring",      197 }, /* capital A, ring  */
 
106
        { "Atilde",     195 }, /* capital A, tilde  */
 
107
        { "Auml",       196 }, /* capital A, dieresis or umlaut mark  */
 
108
        { "Ccedil",     199 }, /* capital C, cedilla  */
 
109
        { "ETH",        208 }, /* capital Eth, Icelandic  */
 
110
        { "Eacute",     201 }, /* capital E, acute accent  */
 
111
        { "Ecirc",      202 }, /* capital E, circumflex accent  */
 
112
        { "Egrave",     200 }, /* capital E, grave accent  */
 
113
        { "Euml",       203 }, /* capital E, dieresis or umlaut mark  */
 
114
        { "Iacute",     205 }, /* capital I, acute accent  */
 
115
        { "Icirc",      206 }, /* capital I, circumflex accent  */
 
116
        { "Igrave",     204 }, /* capital I, grave accent  */
 
117
        { "Iuml",       207 }, /* capital I, dieresis or umlaut mark  */
 
118
        { "Ntilde",     209 }, /* capital N, tilde  */
 
119
        { "Oacute",     211 }, /* capital O, acute accent  */
 
120
        { "Ocirc",      212 }, /* capital O, circumflex accent  */
 
121
        { "Ograve",     210 }, /* capital O, grave accent  */
 
122
        { "Oslash",     216 }, /* capital O, slash  */
 
123
        { "Otilde",     213 }, /* capital O, tilde  */
 
124
        { "Ouml",       214 }, /* capital O, dieresis or umlaut mark  */
 
125
        { "THORN",      222 }, /* capital THORN, Icelandic  */
 
126
        { "Uacute",     218 }, /* capital U, acute accent  */
 
127
        { "Ucirc",      219 }, /* capital U, circumflex accent  */
 
128
        { "Ugrave",     217 }, /* capital U, grave accent  */
 
129
        { "Uuml",       220 }, /* capital U, dieresis or umlaut mark  */
 
130
        { "Yacute",     221 }, /* capital Y, acute accent  */
 
131
        { "aacute",     225 }, /* small a, acute accent  */
 
132
        { "acirc",      226 }, /* small a, circumflex accent  */
 
133
        { "acute",      180 }, /* acute accent  */
 
134
        { "aelig",      230 }, /* small ae diphthong (ligature)  */
 
135
        { "agrave",     224 }, /* small a, grave accent  */
 
136
        { "amp",         38 }, /* ampersand  */
 
137
        { "aring",      229 }, /* small a, ring  */
 
138
        { "atilde",     227 }, /* small a, tilde  */
 
139
        { "auml",       228 }, /* small a, dieresis or umlaut mark  */
 
140
        { "brvbar",     166 }, /* broken (vertical) bar  */
 
141
        { "ccedil",     231 }, /* small c, cedilla  */
 
142
        { "cedil",      184 }, /* cedilla  */
 
143
        { "cent",       162 }, /* cent sign  */
 
144
        { "copy",       169 }, /* copyright sign  */
 
145
        { "curren",     164 }, /* general currency sign  */
 
146
        { "deg",        176 }, /* degree sign  */
 
147
        { "divide",     247 }, /* divide sign  */
 
148
        { "eacute",     233 }, /* small e, acute accent  */
 
149
        { "ecirc",      234 }, /* small e, circumflex accent  */
 
150
        { "egrave",     232 }, /* small e, grave accent  */
 
151
        { "eth",        240 }, /* small eth, Icelandic  */
 
152
        { "euml",       235 }, /* small e, dieresis or umlaut mark  */
 
153
        { "frac12",     189 }, /* fraction one-half  */
 
154
        { "frac14",     188 }, /* fraction one-quarter  */
 
155
        { "frac34",     190 }, /* fraction three-quarters  */
 
156
        { "gt",          62 }, /* greater than  */
 
157
        { "iacute",     237 }, /* small i, acute accent  */
 
158
        { "icirc",      238 }, /* small i, circumflex accent  */
 
159
        { "iexcl",      161 }, /* inverted exclamation mark  */
 
160
        { "igrave",     236 }, /* small i, grave accent  */
 
161
        { "iquest",     191 }, /* inverted question mark  */
 
162
        { "iuml",       239 }, /* small i, dieresis or umlaut mark  */
 
163
        { "laquo",      171 }, /* angle quotation mark, left  */
 
164
        { "lt",          60 }, /* less than  */
 
165
        { "macr",       175 }, /* macron  */
 
166
        { "micro",      181 }, /* micro sign  */
 
167
        { "middot",     183 }, /* middle dot  */
 
168
        { "nbsp",       160 }, /* no-break space  */
 
169
        { "not",        172 }, /* not sign  */
 
170
        { "ntilde",     241 }, /* small n, tilde  */
 
171
        { "oacute",     243 }, /* small o, acute accent  */
 
172
        { "ocirc",      244 }, /* small o, circumflex accent  */
 
173
        { "ograve",     242 }, /* small o, grave accent  */
 
174
        { "ordf",       170 }, /* ordinal indicator, feminine  */
 
175
        { "ordm",       186 }, /* ordinal indicator, masculine  */
 
176
        { "oslash",     248 }, /* small o, slash  */
 
177
        { "otilde",     245 }, /* small o, tilde  */
 
178
        { "ouml",       246 }, /* small o, dieresis or umlaut mark  */
 
179
        { "para",       182 }, /* pilcrow (paragraph sign)  */
 
180
        { "plusmn",     177 }, /* plus-or-minus sign  */
 
181
        { "pound",      163 }, /* pound sterling sign  */
 
182
        { "quot",        34 }, /* double quote  */
 
183
        { "raquo",      187 }, /* angle quotation mark, right  */
 
184
        { "reg",        174 }, /* registered sign  */
 
185
        { "sect",       167 }, /* section sign  */
 
186
        { "shy",        173 }, /* soft hyphen  */
 
187
        { "sup1",       185 }, /* superscript one  */
 
188
        { "sup2",       178 }, /* superscript two  */
 
189
        { "sup3",       179 }, /* superscript three  */
 
190
        { "szlig",      223 }, /* small sharp s, German (sz ligature)  */
 
191
        { "thorn",      254 }, /* small thorn, Icelandic  */
 
192
        { "times",      215 }, /* multiply sign  */
 
193
        { "uacute",     250 }, /* small u, acute accent  */
 
194
        { "ucirc",      251 }, /* small u, circumflex accent  */
 
195
        { "ugrave",     249 }, /* small u, grave accent  */
 
196
        { "uml",        168 }, /* umlaut (dieresis)  */
 
197
        { "uuml",       252 }, /* small u, dieresis or umlaut mark  */
 
198
        { "yacute",     253 }, /* small y, acute accent  */
 
199
        { "yen",        165 }, /* yen sign  */
 
200
        { "yuml",       255 }, /* small y, dieresis or umlaut mark  */
 
201
};
72
202
 
73
203
/*
74
204
 * unvis - decode characters previously encoded by vis
75
205
 */
76
206
int
77
 
unvis(cp, c, astate, flag)
78
 
        char *cp;
79
 
        int c;
80
 
        int *astate, flag;
 
207
unvis(char *cp, int c, int *astate, int flag)
81
208
{
82
209
        unsigned char uc = (unsigned char)c;
 
210
        unsigned char st, ia, is, lc;
 
211
 
 
212
/*
 
213
 * Bottom 8 bits of astate hold the state machine state.
 
214
 * Top 8 bits hold the current character in the http 1866 nv string decoding
 
215
 */
 
216
#define GS(a)           ((a) & 0xff)
 
217
#define SS(a, b)        (((uint32_t)(a) << 24) | (b))
 
218
#define GI(a)           ((uint32_t)(a) >> 24)
83
219
 
84
220
        _DIAGASSERT(cp != NULL);
85
221
        _DIAGASSERT(astate != NULL);
 
222
        st = GS(*astate);
86
223
 
87
224
        if (flag & UNVIS_END) {
88
 
                if (*astate == S_OCTAL2 || *astate == S_OCTAL3
89
 
                    || *astate == S_HEX2) {
90
 
                        *astate = S_GROUND;
91
 
                        return (UNVIS_VALID);
 
225
                switch (st) {
 
226
                case S_OCTAL2:
 
227
                case S_OCTAL3:
 
228
                case S_HEX2:
 
229
                        *astate = SS(0, S_GROUND);
 
230
                        return UNVIS_VALID;
 
231
                case S_GROUND:
 
232
                        return UNVIS_NOCHAR;
 
233
                default:
 
234
                        return UNVIS_SYNBAD;
92
235
                }
93
 
                return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
94
236
        }
95
237
 
96
 
        switch (*astate) {
 
238
        switch (st) {
97
239
 
98
240
        case S_GROUND:
99
241
                *cp = 0;
100
 
                if (c == '\\') {
101
 
                        *astate = S_START;
102
 
                        return (0);
103
 
                }
104
 
                if ((flag & VIS_HTTPSTYLE) && c == '%') {
105
 
                        *astate = S_HEX1;
106
 
                        return (0);
 
242
                if ((flag & VIS_NOESCAPE) == 0 && c == '\\') {
 
243
                        *astate = SS(0, S_START);
 
244
                        return UNVIS_NOCHAR;
 
245
                }
 
246
                if ((flag & VIS_HTTP1808) && c == '%') {
 
247
                        *astate = SS(0, S_HEX1);
 
248
                        return UNVIS_NOCHAR;
 
249
                }
 
250
                if ((flag & VIS_HTTP1866) && c == '&') {
 
251
                        *astate = SS(0, S_AMP);
 
252
                        return UNVIS_NOCHAR;
 
253
                }
 
254
                if ((flag & VIS_MIMESTYLE) && c == '=') {
 
255
                        *astate = SS(0, S_MIME1);
 
256
                        return UNVIS_NOCHAR;
107
257
                }
108
258
                *cp = c;
109
 
                return (UNVIS_VALID);
 
259
                return UNVIS_VALID;
110
260
 
111
261
        case S_START:
112
262
                switch(c) {
113
263
                case '\\':
114
264
                        *cp = c;
115
 
                        *astate = S_GROUND;
116
 
                        return (UNVIS_VALID);
 
265
                        *astate = SS(0, S_GROUND);
 
266
                        return UNVIS_VALID;
117
267
                case '0': case '1': case '2': case '3':
118
268
                case '4': case '5': case '6': case '7':
119
269
                        *cp = (c - '0');
120
 
                        *astate = S_OCTAL2;
121
 
                        return (0);
 
270
                        *astate = SS(0, S_OCTAL2);
 
271
                        return UNVIS_NOCHAR;
122
272
                case 'M':
123
273
                        *cp = (char)0200;
124
 
                        *astate = S_META;
125
 
                        return (0);
 
274
                        *astate = SS(0, S_META);
 
275
                        return UNVIS_NOCHAR;
126
276
                case '^':
127
 
                        *astate = S_CTRL;
128
 
                        return (0);
 
277
                        *astate = SS(0, S_CTRL);
 
278
                        return UNVIS_NOCHAR;
129
279
                case 'n':
130
280
                        *cp = '\n';
131
 
                        *astate = S_GROUND;
132
 
                        return (UNVIS_VALID);
 
281
                        *astate = SS(0, S_GROUND);
 
282
                        return UNVIS_VALID;
133
283
                case 'r':
134
284
                        *cp = '\r';
135
 
                        *astate = S_GROUND;
136
 
                        return (UNVIS_VALID);
 
285
                        *astate = SS(0, S_GROUND);
 
286
                        return UNVIS_VALID;
137
287
                case 'b':
138
288
                        *cp = '\b';
139
 
                        *astate = S_GROUND;
140
 
                        return (UNVIS_VALID);
 
289
                        *astate = SS(0, S_GROUND);
 
290
                        return UNVIS_VALID;
141
291
                case 'a':
142
292
                        *cp = '\007';
143
 
                        *astate = S_GROUND;
144
 
                        return (UNVIS_VALID);
 
293
                        *astate = SS(0, S_GROUND);
 
294
                        return UNVIS_VALID;
145
295
                case 'v':
146
296
                        *cp = '\v';
147
 
                        *astate = S_GROUND;
148
 
                        return (UNVIS_VALID);
 
297
                        *astate = SS(0, S_GROUND);
 
298
                        return UNVIS_VALID;
149
299
                case 't':
150
300
                        *cp = '\t';
151
 
                        *astate = S_GROUND;
152
 
                        return (UNVIS_VALID);
 
301
                        *astate = SS(0, S_GROUND);
 
302
                        return UNVIS_VALID;
153
303
                case 'f':
154
304
                        *cp = '\f';
155
 
                        *astate = S_GROUND;
156
 
                        return (UNVIS_VALID);
 
305
                        *astate = SS(0, S_GROUND);
 
306
                        return UNVIS_VALID;
157
307
                case 's':
158
308
                        *cp = ' ';
159
 
                        *astate = S_GROUND;
160
 
                        return (UNVIS_VALID);
 
309
                        *astate = SS(0, S_GROUND);
 
310
                        return UNVIS_VALID;
161
311
                case 'E':
162
312
                        *cp = '\033';
163
 
                        *astate = S_GROUND;
164
 
                        return (UNVIS_VALID);
 
313
                        *astate = SS(0, S_GROUND);
 
314
                        return UNVIS_VALID;
165
315
                case '\n':
166
316
                        /*
167
317
                         * hidden newline
168
318
                         */
169
 
                        *astate = S_GROUND;
170
 
                        return (UNVIS_NOCHAR);
 
319
                        *astate = SS(0, S_GROUND);
 
320
                        return UNVIS_NOCHAR;
171
321
                case '$':
172
322
                        /*
173
323
                         * hidden marker
174
324
                         */
175
 
                        *astate = S_GROUND;
176
 
                        return (UNVIS_NOCHAR);
 
325
                        *astate = SS(0, S_GROUND);
 
326
                        return UNVIS_NOCHAR;
177
327
                }
178
 
                *astate = S_GROUND;
179
 
                return (UNVIS_SYNBAD);
 
328
                goto bad;
180
329
 
181
330
        case S_META:
182
331
                if (c == '-')
183
 
                        *astate = S_META1;
 
332
                        *astate = SS(0, S_META1);
184
333
                else if (c == '^')
185
 
                        *astate = S_CTRL;
186
 
                else {
187
 
                        *astate = S_GROUND;
188
 
                        return (UNVIS_SYNBAD);
189
 
                }
190
 
                return (0);
 
334
                        *astate = SS(0, S_CTRL);
 
335
                else 
 
336
                        goto bad;
 
337
                return UNVIS_NOCHAR;
191
338
 
192
339
        case S_META1:
193
 
                *astate = S_GROUND;
 
340
                *astate = SS(0, S_GROUND);
194
341
                *cp |= c;
195
 
                return (UNVIS_VALID);
 
342
                return UNVIS_VALID;
196
343
 
197
344
        case S_CTRL:
198
345
                if (c == '?')
199
346
                        *cp |= 0177;
200
347
                else
201
348
                        *cp |= c & 037;
202
 
                *astate = S_GROUND;
203
 
                return (UNVIS_VALID);
 
349
                *astate = SS(0, S_GROUND);
 
350
                return UNVIS_VALID;
204
351
 
205
352
        case S_OCTAL2:  /* second possible octal digit */
206
353
                if (isoctal(uc)) {
208
355
                         * yes - and maybe a third
209
356
                         */
210
357
                        *cp = (*cp << 3) + (c - '0');
211
 
                        *astate = S_OCTAL3;
212
 
                        return (0);
 
358
                        *astate = SS(0, S_OCTAL3);
 
359
                        return UNVIS_NOCHAR;
213
360
                }
214
361
                /*
215
362
                 * no - done with current sequence, push back passed char
216
363
                 */
217
 
                *astate = S_GROUND;
218
 
                return (UNVIS_VALIDPUSH);
 
364
                *astate = SS(0, S_GROUND);
 
365
                return UNVIS_VALIDPUSH;
219
366
 
220
367
        case S_OCTAL3:  /* third possible octal digit */
221
 
                *astate = S_GROUND;
 
368
                *astate = SS(0, S_GROUND);
222
369
                if (isoctal(uc)) {
223
370
                        *cp = (*cp << 3) + (c - '0');
224
 
                        return (UNVIS_VALID);
 
371
                        return UNVIS_VALID;
225
372
                }
226
373
                /*
227
374
                 * we were done, push back passed char
228
375
                 */
229
 
                return (UNVIS_VALIDPUSH);
 
376
                return UNVIS_VALIDPUSH;
230
377
 
231
378
        case S_HEX1:
232
379
                if (isxdigit(uc)) {
233
380
                        *cp = xtod(uc);
234
 
                        *astate = S_HEX2;
235
 
                        return (0);
 
381
                        *astate = SS(0, S_HEX2);
 
382
                        return UNVIS_NOCHAR;
236
383
                }
237
384
                /*
238
385
                 * no - done with current sequence, push back passed char
239
386
                 */
240
 
                *astate = S_GROUND;
241
 
                return (UNVIS_VALIDPUSH);
 
387
                *astate = SS(0, S_GROUND);
 
388
                return UNVIS_VALIDPUSH;
242
389
 
243
390
        case S_HEX2:
244
391
                *astate = S_GROUND;
245
392
                if (isxdigit(uc)) {
246
393
                        *cp = xtod(uc) | (*cp << 4);
247
 
                        return (UNVIS_VALID);
248
 
                }
249
 
                return (UNVIS_VALIDPUSH);
 
394
                        return UNVIS_VALID;
 
395
                }
 
396
                return UNVIS_VALIDPUSH;
 
397
 
 
398
        case S_MIME1:
 
399
                if (uc == '\n' || uc == '\r') {
 
400
                        *astate = SS(0, S_EATCRNL);
 
401
                        return UNVIS_NOCHAR;
 
402
                }
 
403
                if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
 
404
                        *cp = XTOD(uc);
 
405
                        *astate = SS(0, S_MIME2);
 
406
                        return UNVIS_NOCHAR;
 
407
                }
 
408
                goto bad;
 
409
 
 
410
        case S_MIME2:
 
411
                if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
 
412
                        *astate = SS(0, S_GROUND);
 
413
                        *cp = XTOD(uc) | (*cp << 4);
 
414
                        return UNVIS_VALID;
 
415
                }
 
416
                goto bad;
 
417
 
 
418
        case S_EATCRNL:
 
419
                switch (uc) {
 
420
                case '\r':
 
421
                case '\n':
 
422
                        return UNVIS_NOCHAR;
 
423
                case '=':
 
424
                        *astate = SS(0, S_MIME1);
 
425
                        return UNVIS_NOCHAR;
 
426
                default:
 
427
                        *cp = uc;
 
428
                        *astate = SS(0, S_GROUND);
 
429
                        return UNVIS_VALID;
 
430
                }
 
431
 
 
432
        case S_AMP:
 
433
                *cp = 0;
 
434
                if (uc == '#') {
 
435
                        *astate = SS(0, S_NUMBER);
 
436
                        return UNVIS_NOCHAR;
 
437
                }
 
438
                *astate = SS(0, S_STRING);
 
439
                /*FALLTHROUGH*/
 
440
 
 
441
        case S_STRING:
 
442
                ia = *cp;               /* index in the array */
 
443
                is = GI(*astate);       /* index in the string */
 
444
                lc = is == 0 ? 0 : nv[ia].name[is - 1]; /* last character */
 
445
 
 
446
                if (uc == ';')
 
447
                        uc = '\0';
 
448
 
 
449
                for (; ia < __arraycount(nv); ia++) {
 
450
                        if (is != 0 && nv[ia].name[is - 1] != lc)
 
451
                                goto bad;
 
452
                        if (nv[ia].name[is] == uc)
 
453
                                break;
 
454
                }
 
455
 
 
456
                if (ia == __arraycount(nv))
 
457
                        goto bad;
 
458
 
 
459
                if (uc != 0) {
 
460
                        *cp = ia;
 
461
                        *astate = SS(is + 1, S_STRING);
 
462
                        return UNVIS_NOCHAR;
 
463
                }
 
464
 
 
465
                *cp = nv[ia].value;
 
466
                *astate = SS(0, S_GROUND);
 
467
                return UNVIS_VALID;
 
468
 
 
469
        case S_NUMBER:
 
470
                if (uc == ';')
 
471
                        return UNVIS_VALID;
 
472
                if (!isdigit(uc))
 
473
                        goto bad;
 
474
                *cp += (*cp * 10) + uc - '0';
 
475
                return UNVIS_NOCHAR;
250
476
 
251
477
        default:
 
478
        bad:
252
479
                /*
253
480
                 * decoder in unknown state - (probably uninitialized)
254
481
                 */
255
 
                *astate = S_GROUND;
256
 
                return (UNVIS_SYNBAD);
 
482
                *astate = SS(0, S_GROUND);
 
483
                return UNVIS_SYNBAD;
257
484
        }
258
485
}
259
486
 
260
487
/*
261
 
 * strunvis - decode src into dst
 
488
 * strnunvisx - decode src into dst
262
489
 *
263
490
 *      Number of chars decoded into dst is returned, -1 on error.
264
491
 *      Dst is null terminated.
265
492
 */
266
493
 
267
494
int
268
 
strunvisx(dst, src, flag)
269
 
        char *dst;
270
 
        const char *src;
271
 
        int flag;
 
495
strnunvisx(char *dst, size_t dlen, const char *src, int flag)
272
496
{
273
497
        char c;
274
 
        char *start = dst;
 
498
        char t, *start = dst;
275
499
        int state = 0;
276
500
 
277
501
        _DIAGASSERT(src != NULL);
278
502
        _DIAGASSERT(dst != NULL);
 
503
#define CHECKSPACE() \
 
504
        do { \
 
505
                if (dlen-- == 0) { \
 
506
                        errno = ENOSPC; \
 
507
                        return -1; \
 
508
                } \
 
509
        } while (/*CONSTCOND*/0)
279
510
 
280
511
        while ((c = *src++) != '\0') {
281
512
 again:
282
 
                switch (unvis(dst, c, &state, flag)) {
 
513
                switch (unvis(&t, c, &state, flag)) {
283
514
                case UNVIS_VALID:
284
 
                        dst++;
 
515
                        CHECKSPACE();
 
516
                        *dst++ = t;
285
517
                        break;
286
518
                case UNVIS_VALIDPUSH:
287
 
                        dst++;
 
519
                        CHECKSPACE();
 
520
                        *dst++ = t;
288
521
                        goto again;
289
522
                case 0:
290
523
                case UNVIS_NOCHAR:
291
524
                        break;
 
525
                case UNVIS_SYNBAD:
 
526
                        errno = EINVAL;
 
527
                        return -1;
292
528
                default:
293
 
                        return (-1);
 
529
                        _DIAGASSERT(0);
 
530
                        errno = EINVAL;
 
531
                        return -1;
294
532
                }
295
533
        }
296
 
        if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
297
 
                dst++;
 
534
        if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) {
 
535
                CHECKSPACE();
 
536
                *dst++ = t;
 
537
        }
 
538
        CHECKSPACE();
298
539
        *dst = '\0';
299
 
        return (dst - start);
300
 
}
301
 
 
302
 
int
303
 
strunvis(dst, src)
304
 
        char *dst;
305
 
        const char *src;
306
 
{
307
 
        return strunvisx(dst, src, 0);
 
540
        return (int)(dst - start);
 
541
}
 
542
 
 
543
int
 
544
strunvisx(char *dst, const char *src, int flag)
 
545
{
 
546
        return strnunvisx(dst, (size_t)~0, src, flag);
 
547
}
 
548
 
 
549
int
 
550
strunvis(char *dst, const char *src)
 
551
{
 
552
        return strnunvisx(dst, (size_t)~0, src, 0);
 
553
}
 
554
 
 
555
int
 
556
strnunvis(char *dst, size_t dlen, const char *src)
 
557
{
 
558
        return strnunvisx(dst, dlen, src, 0);
308
559
}
309
560
#endif