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