unvis.c revision 244230
1321369Sdim/* $NetBSD: unvis.c,v 1.40 2012/12/14 21:31:01 christos Exp $ */ 2292932Sdim 3353358Sdim/*- 4353358Sdim * Copyright (c) 1989, 1993 5353358Sdim * The Regents of the University of California. All rights reserved. 6292932Sdim * 7292932Sdim * Redistribution and use in source and binary forms, with or without 8292932Sdim * modification, are permitted provided that the following conditions 9292932Sdim * are met: 10292932Sdim * 1. Redistributions of source code must retain the above copyright 11314564Sdim * notice, this list of conditions and the following disclaimer. 12314564Sdim * 2. Redistributions in binary form must reproduce the above copyright 13314564Sdim * notice, this list of conditions and the following disclaimer in the 14292932Sdim * documentation and/or other materials provided with the distribution. 15292932Sdim * 3. Neither the name of the University nor the names of its contributors 16292932Sdim * may be used to endorse or promote products derived from this software 17292932Sdim * without specific prior written permission. 18292932Sdim * 19292932Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20314564Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21314564Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22292932Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23292932Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24292932Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25321369Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26321369Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27321369Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28321369Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29292932Sdim * SUCH DAMAGE. 30353358Sdim */ 31321369Sdim 32321369Sdim#include <sys/cdefs.h> 33292932Sdim#if defined(LIBC_SCCS) && !defined(lint) 34292932Sdim#if 0 35292932Sdimstatic char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; 36292932Sdim#else 37314564Sdim__RCSID("$NetBSD: unvis.c,v 1.40 2012/12/14 21:31:01 christos Exp $"); 38292932Sdim#endif 39292932Sdim#endif /* LIBC_SCCS and not lint */ 40292932Sdim 41292932Sdim#include "namespace.h" 42292932Sdim#include <sys/types.h> 43353358Sdim 44353358Sdim#include <assert.h> 45353358Sdim#include <ctype.h> 46353358Sdim#include <stdint.h> 47341825Sdim#include <stdio.h> 48292932Sdim#include <errno.h> 49314564Sdim#include <vis.h> 50314564Sdim 51314564Sdim#ifdef __weak_alias 52314564Sdim__weak_alias(strnunvisx,_strnunvisx) 53292932Sdim#endif 54314564Sdim 55292932Sdim#if !HAVE_VIS 56314564Sdim/* 57292932Sdim * decode driven by state machine 58314564Sdim */ 59314564Sdim#define S_GROUND 0 /* haven't seen escape char */ 60314564Sdim#define S_START 1 /* start decoding special sequence */ 61314564Sdim#define S_META 2 /* metachar started (M) */ 62292932Sdim#define S_META1 3 /* metachar more, regular char (-) */ 63314564Sdim#define S_CTRL 4 /* control char started (^) */ 64314564Sdim#define S_OCTAL2 5 /* octal digit 2 */ 65314564Sdim#define S_OCTAL3 6 /* octal digit 3 */ 66314564Sdim#define S_HEX 7 /* mandatory hex digit */ 67292932Sdim#define S_HEX1 8 /* http hex digit */ 68314564Sdim#define S_HEX2 9 /* http hex digit 2 */ 69314564Sdim#define S_MIME1 10 /* mime hex digit 1 */ 70314564Sdim#define S_MIME2 11 /* mime hex digit 2 */ 71292932Sdim#define S_EATCRNL 12 /* mime eating CRNL */ 72344779Sdim#define S_AMP 13 /* seen & */ 73341825Sdim#define S_NUMBER 14 /* collecting number */ 74292932Sdim#define S_STRING 15 /* collecting string */ 75314564Sdim 76314564Sdim#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') 77314564Sdim#define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10)) 78314564Sdim#define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10)) 79314564Sdim 80292932Sdim/* 81360784Sdim * RFC 1866 82314564Sdim */ 83314564Sdimstatic const struct nv { 84314564Sdim const char name[7]; 85314564Sdim uint8_t value; 86314564Sdim} nv[] = { 87314564Sdim { "AElig", 198 }, /* capital AE diphthong (ligature) */ 88314564Sdim { "Aacute", 193 }, /* capital A, acute accent */ 89314564Sdim { "Acirc", 194 }, /* capital A, circumflex accent */ 90314564Sdim { "Agrave", 192 }, /* capital A, grave accent */ 91292932Sdim { "Aring", 197 }, /* capital A, ring */ 92314564Sdim { "Atilde", 195 }, /* capital A, tilde */ 93314564Sdim { "Auml", 196 }, /* capital A, dieresis or umlaut mark */ 94314564Sdim { "Ccedil", 199 }, /* capital C, cedilla */ 95292932Sdim { "ETH", 208 }, /* capital Eth, Icelandic */ 96314564Sdim { "Eacute", 201 }, /* capital E, acute accent */ 97314564Sdim { "Ecirc", 202 }, /* capital E, circumflex accent */ 98292932Sdim { "Egrave", 200 }, /* capital E, grave accent */ 99314564Sdim { "Euml", 203 }, /* capital E, dieresis or umlaut mark */ 100292932Sdim { "Iacute", 205 }, /* capital I, acute accent */ 101292932Sdim { "Icirc", 206 }, /* capital I, circumflex accent */ 102314564Sdim { "Igrave", 204 }, /* capital I, grave accent */ 103314564Sdim { "Iuml", 207 }, /* capital I, dieresis or umlaut mark */ 104314564Sdim { "Ntilde", 209 }, /* capital N, tilde */ 105314564Sdim { "Oacute", 211 }, /* capital O, acute accent */ 106314564Sdim { "Ocirc", 212 }, /* capital O, circumflex accent */ 107314564Sdim { "Ograve", 210 }, /* capital O, grave accent */ 108314564Sdim { "Oslash", 216 }, /* capital O, slash */ 109314564Sdim { "Otilde", 213 }, /* capital O, tilde */ 110292932Sdim { "Ouml", 214 }, /* capital O, dieresis or umlaut mark */ 111292932Sdim { "THORN", 222 }, /* capital THORN, Icelandic */ 112314564Sdim { "Uacute", 218 }, /* capital U, acute accent */ 113314564Sdim { "Ucirc", 219 }, /* capital U, circumflex accent */ 114314564Sdim { "Ugrave", 217 }, /* capital U, grave accent */ 115292932Sdim { "Uuml", 220 }, /* capital U, dieresis or umlaut mark */ 116314564Sdim { "Yacute", 221 }, /* capital Y, acute accent */ 117314564Sdim { "aacute", 225 }, /* small a, acute accent */ 118314564Sdim { "acirc", 226 }, /* small a, circumflex accent */ 119292932Sdim { "acute", 180 }, /* acute accent */ 120314564Sdim { "aelig", 230 }, /* small ae diphthong (ligature) */ 121314564Sdim { "agrave", 224 }, /* small a, grave accent */ 122292932Sdim { "amp", 38 }, /* ampersand */ 123314564Sdim { "aring", 229 }, /* small a, ring */ 124314564Sdim { "atilde", 227 }, /* small a, tilde */ 125292932Sdim { "auml", 228 }, /* small a, dieresis or umlaut mark */ 126314564Sdim { "brvbar", 166 }, /* broken (vertical) bar */ 127314564Sdim { "ccedil", 231 }, /* small c, cedilla */ 128314564Sdim { "cedil", 184 }, /* cedilla */ 129314564Sdim { "cent", 162 }, /* cent sign */ 130314564Sdim { "copy", 169 }, /* copyright sign */ 131314564Sdim { "curren", 164 }, /* general currency sign */ 132292932Sdim { "deg", 176 }, /* degree sign */ 133314564Sdim { "divide", 247 }, /* divide sign */ 134314564Sdim { "eacute", 233 }, /* small e, acute accent */ 135314564Sdim { "ecirc", 234 }, /* small e, circumflex accent */ 136314564Sdim { "egrave", 232 }, /* small e, grave accent */ 137292932Sdim { "eth", 240 }, /* small eth, Icelandic */ 138314564Sdim { "euml", 235 }, /* small e, dieresis or umlaut mark */ 139314564Sdim { "frac12", 189 }, /* fraction one-half */ 140314564Sdim { "frac14", 188 }, /* fraction one-quarter */ 141314564Sdim { "frac34", 190 }, /* fraction three-quarters */ 142314564Sdim { "gt", 62 }, /* greater than */ 143292932Sdim { "iacute", 237 }, /* small i, acute accent */ 144314564Sdim { "icirc", 238 }, /* small i, circumflex accent */ 145292932Sdim { "iexcl", 161 }, /* inverted exclamation mark */ 146314564Sdim { "igrave", 236 }, /* small i, grave accent */ 147292932Sdim { "iquest", 191 }, /* inverted question mark */ 148292932Sdim { "iuml", 239 }, /* small i, dieresis or umlaut mark */ 149314564Sdim { "laquo", 171 }, /* angle quotation mark, left */ 150314564Sdim { "lt", 60 }, /* less than */ 151314564Sdim { "macr", 175 }, /* macron */ 152314564Sdim { "micro", 181 }, /* micro sign */ 153314564Sdim { "middot", 183 }, /* middle dot */ 154314564Sdim { "nbsp", 160 }, /* no-break space */ 155292932Sdim { "not", 172 }, /* not sign */ 156292932Sdim { "ntilde", 241 }, /* small n, tilde */ 157314564Sdim { "oacute", 243 }, /* small o, acute accent */ 158314564Sdim { "ocirc", 244 }, /* small o, circumflex accent */ 159314564Sdim { "ograve", 242 }, /* small o, grave accent */ 160314564Sdim { "ordf", 170 }, /* ordinal indicator, feminine */ 161292932Sdim { "ordm", 186 }, /* ordinal indicator, masculine */ 162314564Sdim { "oslash", 248 }, /* small o, slash */ 163314564Sdim { "otilde", 245 }, /* small o, tilde */ 164314564Sdim { "ouml", 246 }, /* small o, dieresis or umlaut mark */ 165314564Sdim { "para", 182 }, /* pilcrow (paragraph sign) */ 166292932Sdim { "plusmn", 177 }, /* plus-or-minus sign */ 167314564Sdim { "pound", 163 }, /* pound sterling sign */ 168314564Sdim { "quot", 34 }, /* double quote */ 169314564Sdim { "raquo", 187 }, /* angle quotation mark, right */ 170314564Sdim { "reg", 174 }, /* registered sign */ 171292932Sdim { "sect", 167 }, /* section sign */ 172314564Sdim { "shy", 173 }, /* soft hyphen */ 173314564Sdim { "sup1", 185 }, /* superscript one */ 174314564Sdim { "sup2", 178 }, /* superscript two */ 175314564Sdim { "sup3", 179 }, /* superscript three */ 176314564Sdim { "szlig", 223 }, /* small sharp s, German (sz ligature) */ 177314564Sdim { "thorn", 254 }, /* small thorn, Icelandic */ 178314564Sdim { "times", 215 }, /* multiply sign */ 179314564Sdim { "uacute", 250 }, /* small u, acute accent */ 180292932Sdim { "ucirc", 251 }, /* small u, circumflex accent */ 181314564Sdim { "ugrave", 249 }, /* small u, grave accent */ 182314564Sdim { "uml", 168 }, /* umlaut (dieresis) */ 183314564Sdim { "uuml", 252 }, /* small u, dieresis or umlaut mark */ 184314564Sdim { "yacute", 253 }, /* small y, acute accent */ 185314564Sdim { "yen", 165 }, /* yen sign */ 186314564Sdim { "yuml", 255 }, /* small y, dieresis or umlaut mark */ 187292932Sdim}; 188314564Sdim 189314564Sdim/* 190314564Sdim * unvis - decode characters previously encoded by vis 191314564Sdim */ 192314564Sdimint 193314564Sdimunvis(char *cp, int c, int *astate, int flag) 194292932Sdim{ 195314564Sdim unsigned char uc = (unsigned char)c; 196341825Sdim unsigned char st, ia, is, lc; 197341825Sdim 198314564Sdim/* 199314564Sdim * Bottom 8 bits of astate hold the state machine state. 200314564Sdim * Top 8 bits hold the current character in the http 1866 nv string decoding 201314564Sdim */ 202314564Sdim#define GS(a) ((a) & 0xff) 203314564Sdim#define SS(a, b) (((uint32_t)(a) << 24) | (b)) 204314564Sdim#define GI(a) ((uint32_t)(a) >> 24) 205314564Sdim 206292932Sdim _DIAGASSERT(cp != NULL); 207314564Sdim _DIAGASSERT(astate != NULL); 208314564Sdim st = GS(*astate); 209292932Sdim 210314564Sdim if (flag & UNVIS_END) { 211314564Sdim switch (st) { 212314564Sdim case S_OCTAL2: 213314564Sdim case S_OCTAL3: 214314564Sdim case S_HEX2: 215314564Sdim *astate = SS(0, S_GROUND); 216314564Sdim return UNVIS_VALID; 217314564Sdim case S_GROUND: 218314564Sdim return UNVIS_NOCHAR; 219314564Sdim default: 220314564Sdim return UNVIS_SYNBAD; 221314564Sdim } 222314564Sdim } 223314564Sdim 224314564Sdim switch (st) { 225292932Sdim 226314564Sdim case S_GROUND: 227292932Sdim *cp = 0; 228314564Sdim if ((flag & VIS_NOESCAPE) == 0 && c == '\\') { 229292932Sdim *astate = SS(0, S_START); 230292932Sdim return UNVIS_NOCHAR; 231314564Sdim } 232314564Sdim if ((flag & VIS_HTTP1808) && c == '%') { 233292932Sdim *astate = SS(0, S_HEX1); 234314564Sdim return UNVIS_NOCHAR; 235314564Sdim } 236314564Sdim if ((flag & VIS_HTTP1866) && c == '&') { 237314564Sdim *astate = SS(0, S_AMP); 238314564Sdim return UNVIS_NOCHAR; 239292932Sdim } 240314564Sdim if ((flag & VIS_MIMESTYLE) && c == '=') { 241314564Sdim *astate = SS(0, S_MIME1); 242292932Sdim return UNVIS_NOCHAR; 243292932Sdim } 244321369Sdim *cp = c; 245321369Sdim return UNVIS_VALID; 246292932Sdim 247 case S_START: 248 switch(c) { 249 case '\\': 250 *cp = c; 251 *astate = SS(0, S_GROUND); 252 return UNVIS_VALID; 253 case '0': case '1': case '2': case '3': 254 case '4': case '5': case '6': case '7': 255 *cp = (c - '0'); 256 *astate = SS(0, S_OCTAL2); 257 return UNVIS_NOCHAR; 258 case 'M': 259 *cp = (char)0200; 260 *astate = SS(0, S_META); 261 return UNVIS_NOCHAR; 262 case '^': 263 *astate = SS(0, S_CTRL); 264 return UNVIS_NOCHAR; 265 case 'n': 266 *cp = '\n'; 267 *astate = SS(0, S_GROUND); 268 return UNVIS_VALID; 269 case 'r': 270 *cp = '\r'; 271 *astate = SS(0, S_GROUND); 272 return UNVIS_VALID; 273 case 'b': 274 *cp = '\b'; 275 *astate = SS(0, S_GROUND); 276 return UNVIS_VALID; 277 case 'a': 278 *cp = '\007'; 279 *astate = SS(0, S_GROUND); 280 return UNVIS_VALID; 281 case 'v': 282 *cp = '\v'; 283 *astate = SS(0, S_GROUND); 284 return UNVIS_VALID; 285 case 't': 286 *cp = '\t'; 287 *astate = SS(0, S_GROUND); 288 return UNVIS_VALID; 289 case 'f': 290 *cp = '\f'; 291 *astate = SS(0, S_GROUND); 292 return UNVIS_VALID; 293 case 's': 294 *cp = ' '; 295 *astate = SS(0, S_GROUND); 296 return UNVIS_VALID; 297 case 'E': 298 *cp = '\033'; 299 *astate = SS(0, S_GROUND); 300 return UNVIS_VALID; 301 case 'x': 302 *astate = SS(0, S_HEX); 303 return UNVIS_NOCHAR; 304 case '\n': 305 /* 306 * hidden newline 307 */ 308 *astate = SS(0, S_GROUND); 309 return UNVIS_NOCHAR; 310 case '$': 311 /* 312 * hidden marker 313 */ 314 *astate = SS(0, S_GROUND); 315 return UNVIS_NOCHAR; 316 } 317 goto bad; 318 319 case S_META: 320 if (c == '-') 321 *astate = SS(0, S_META1); 322 else if (c == '^') 323 *astate = SS(0, S_CTRL); 324 else 325 goto bad; 326 return UNVIS_NOCHAR; 327 328 case S_META1: 329 *astate = SS(0, S_GROUND); 330 *cp |= c; 331 return UNVIS_VALID; 332 333 case S_CTRL: 334 if (c == '?') 335 *cp |= 0177; 336 else 337 *cp |= c & 037; 338 *astate = SS(0, S_GROUND); 339 return UNVIS_VALID; 340 341 case S_OCTAL2: /* second possible octal digit */ 342 if (isoctal(uc)) { 343 /* 344 * yes - and maybe a third 345 */ 346 *cp = (*cp << 3) + (c - '0'); 347 *astate = SS(0, S_OCTAL3); 348 return UNVIS_NOCHAR; 349 } 350 /* 351 * no - done with current sequence, push back passed char 352 */ 353 *astate = SS(0, S_GROUND); 354 return UNVIS_VALIDPUSH; 355 356 case S_OCTAL3: /* third possible octal digit */ 357 *astate = SS(0, S_GROUND); 358 if (isoctal(uc)) { 359 *cp = (*cp << 3) + (c - '0'); 360 return UNVIS_VALID; 361 } 362 /* 363 * we were done, push back passed char 364 */ 365 return UNVIS_VALIDPUSH; 366 367 case S_HEX: 368 if (!isxdigit(uc)) 369 goto bad; 370 /*FALLTHROUGH*/ 371 case S_HEX1: 372 if (isxdigit(uc)) { 373 *cp = xtod(uc); 374 *astate = SS(0, S_HEX2); 375 return UNVIS_NOCHAR; 376 } 377 /* 378 * no - done with current sequence, push back passed char 379 */ 380 *astate = SS(0, S_GROUND); 381 return UNVIS_VALIDPUSH; 382 383 case S_HEX2: 384 *astate = S_GROUND; 385 if (isxdigit(uc)) { 386 *cp = xtod(uc) | (*cp << 4); 387 return UNVIS_VALID; 388 } 389 return UNVIS_VALIDPUSH; 390 391 case S_MIME1: 392 if (uc == '\n' || uc == '\r') { 393 *astate = SS(0, S_EATCRNL); 394 return UNVIS_NOCHAR; 395 } 396 if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) { 397 *cp = XTOD(uc); 398 *astate = SS(0, S_MIME2); 399 return UNVIS_NOCHAR; 400 } 401 goto bad; 402 403 case S_MIME2: 404 if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) { 405 *astate = SS(0, S_GROUND); 406 *cp = XTOD(uc) | (*cp << 4); 407 return UNVIS_VALID; 408 } 409 goto bad; 410 411 case S_EATCRNL: 412 switch (uc) { 413 case '\r': 414 case '\n': 415 return UNVIS_NOCHAR; 416 case '=': 417 *astate = SS(0, S_MIME1); 418 return UNVIS_NOCHAR; 419 default: 420 *cp = uc; 421 *astate = SS(0, S_GROUND); 422 return UNVIS_VALID; 423 } 424 425 case S_AMP: 426 *cp = 0; 427 if (uc == '#') { 428 *astate = SS(0, S_NUMBER); 429 return UNVIS_NOCHAR; 430 } 431 *astate = SS(0, S_STRING); 432 /*FALLTHROUGH*/ 433 434 case S_STRING: 435 ia = *cp; /* index in the array */ 436 is = GI(*astate); /* index in the string */ 437 lc = is == 0 ? 0 : nv[ia].name[is - 1]; /* last character */ 438 439 if (uc == ';') 440 uc = '\0'; 441 442 for (; ia < __arraycount(nv); ia++) { 443 if (is != 0 && nv[ia].name[is - 1] != lc) 444 goto bad; 445 if (nv[ia].name[is] == uc) 446 break; 447 } 448 449 if (ia == __arraycount(nv)) 450 goto bad; 451 452 if (uc != 0) { 453 *cp = ia; 454 *astate = SS(is + 1, S_STRING); 455 return UNVIS_NOCHAR; 456 } 457 458 *cp = nv[ia].value; 459 *astate = SS(0, S_GROUND); 460 return UNVIS_VALID; 461 462 case S_NUMBER: 463 if (uc == ';') 464 return UNVIS_VALID; 465 if (!isdigit(uc)) 466 goto bad; 467 *cp += (*cp * 10) + uc - '0'; 468 return UNVIS_NOCHAR; 469 470 default: 471 bad: 472 /* 473 * decoder in unknown state - (probably uninitialized) 474 */ 475 *astate = SS(0, S_GROUND); 476 return UNVIS_SYNBAD; 477 } 478} 479 480/* 481 * strnunvisx - decode src into dst 482 * 483 * Number of chars decoded into dst is returned, -1 on error. 484 * Dst is null terminated. 485 */ 486 487int 488strnunvisx(char *dst, size_t dlen, const char *src, int flag) 489{ 490 char c; 491 char t = '\0', *start = dst; 492 int state = 0; 493 494 _DIAGASSERT(src != NULL); 495 _DIAGASSERT(dst != NULL); 496#define CHECKSPACE() \ 497 do { \ 498 if (dlen-- == 0) { \ 499 errno = ENOSPC; \ 500 return -1; \ 501 } \ 502 } while (/*CONSTCOND*/0) 503 504 while ((c = *src++) != '\0') { 505 again: 506 switch (unvis(&t, c, &state, flag)) { 507 case UNVIS_VALID: 508 CHECKSPACE(); 509 *dst++ = t; 510 break; 511 case UNVIS_VALIDPUSH: 512 CHECKSPACE(); 513 *dst++ = t; 514 goto again; 515 case 0: 516 case UNVIS_NOCHAR: 517 break; 518 case UNVIS_SYNBAD: 519 errno = EINVAL; 520 return -1; 521 default: 522 _DIAGASSERT(/*CONSTCOND*/0); 523 errno = EINVAL; 524 return -1; 525 } 526 } 527 if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) { 528 CHECKSPACE(); 529 *dst++ = t; 530 } 531 CHECKSPACE(); 532 *dst = '\0'; 533 return (int)(dst - start); 534} 535 536int 537strunvisx(char *dst, const char *src, int flag) 538{ 539 return strnunvisx(dst, (size_t)~0, src, flag); 540} 541 542int 543strunvis(char *dst, const char *src) 544{ 545 return strnunvisx(dst, (size_t)~0, src, 0); 546} 547 548int 549strnunvis(char *dst, size_t dlen, const char *src) 550{ 551 return strnunvisx(dst, dlen, src, 0); 552} 553#endif 554