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