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