34 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] =
"@(#)unvis.c 8.1 (Berkeley) 6/4/93";
43 #include "namespace.h"
46 #include <sys/types.h>
52 #if defined(__sun) || defined(__sun__)
53 #include <sys/inttypes.h>
67 __weak_alias(strnunvisx,_strnunvisx)
91 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
93 #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
94 #define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10))
99 static const struct nv {
209 unvis(
char *cp,
int c,
int *astate,
int flag)
211 unsigned char uc = (
unsigned char)c;
212 unsigned char st, ia, is, lc;
218 #define GS(a) ((a) & 0xff)
219 #define SS(a, b) (((uint32_t)(a) << 24) | (b))
220 #define GI(a) ((uint32_t)(a) >> 24)
222 _DIAGASSERT(cp != NULL);
223 _DIAGASSERT(astate != NULL);
226 if (flag & UNVIS_END) {
231 *astate = SS(0, S_GROUND);
244 if ((flag & VIS_NOESCAPE) == 0 && c ==
'\\') {
245 *astate = SS(0, S_START);
248 if ((flag & VIS_HTTP1808) && c ==
'%') {
249 *astate = SS(0, S_HEX1);
252 if ((flag & VIS_HTTP1866) && c ==
'&') {
253 *astate = SS(0, S_AMP);
256 if ((flag & VIS_MIMESTYLE) && c ==
'=') {
257 *astate = SS(0, S_MIME1);
267 *astate = SS(0, S_GROUND);
269 case '0':
case '1':
case '2':
case '3':
270 case '4':
case '5':
case '6':
case '7':
272 *astate = SS(0, S_OCTAL2);
276 *astate = SS(0, S_META);
279 *astate = SS(0, S_CTRL);
283 *astate = SS(0, S_GROUND);
287 *astate = SS(0, S_GROUND);
291 *astate = SS(0, S_GROUND);
295 *astate = SS(0, S_GROUND);
299 *astate = SS(0, S_GROUND);
303 *astate = SS(0, S_GROUND);
307 *astate = SS(0, S_GROUND);
311 *astate = SS(0, S_GROUND);
315 *astate = SS(0, S_GROUND);
321 *astate = SS(0, S_GROUND);
327 *astate = SS(0, S_GROUND);
334 *astate = SS(0, S_META1);
336 *astate = SS(0, S_CTRL);
342 *astate = SS(0, S_GROUND);
351 *astate = SS(0, S_GROUND);
359 *cp = (*cp << 3) + (c -
'0');
360 *astate = SS(0, S_OCTAL3);
366 *astate = SS(0, S_GROUND);
367 return UNVIS_VALIDPUSH;
370 *astate = SS(0, S_GROUND);
372 *cp = (*cp << 3) + (c -
'0');
378 return UNVIS_VALIDPUSH;
383 *astate = SS(0, S_HEX2);
389 *astate = SS(0, S_GROUND);
390 return UNVIS_VALIDPUSH;
395 *cp = xtod(uc) | (*cp << 4);
398 return UNVIS_VALIDPUSH;
401 if (uc ==
'\n' || uc ==
'\r') {
402 *astate = SS(0, S_EATCRNL);
405 if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
407 *astate = SS(0, S_MIME2);
413 if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
414 *astate = SS(0, S_GROUND);
415 *cp = XTOD(uc) | (*cp << 4);
426 *astate = SS(0, S_MIME1);
430 *astate = SS(0, S_GROUND);
437 *astate = SS(0, S_NUMBER);
440 *astate = SS(0, S_STRING);
446 lc = is == 0 ? 0 : nv[ia].name[is - 1];
451 for (; ia < __arraycount(nv); ia++) {
452 if (is != 0 && nv[ia].
name[is - 1] != lc)
454 if (nv[ia].
name[is] == uc)
458 if (ia == __arraycount(nv))
463 *astate = SS(is + 1, S_STRING);
468 *astate = SS(0, S_GROUND);
476 *cp += (*cp * 10) + uc -
'0';
484 *astate = SS(0, S_GROUND);
497 strnunvisx(
char *dst,
size_t dlen,
const char *src,
int flag)
500 char t, *start = dst;
503 _DIAGASSERT(src != NULL);
504 _DIAGASSERT(dst != NULL);
505 #define CHECKSPACE() \
513 while ((c = *src++) !=
'\0') {
515 switch (unvis(&t, c, &state, flag)) {
520 case UNVIS_VALIDPUSH:
536 if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) {
542 return (
int)(dst - start);
546 strunvisx(
char *dst,
const char *src,
int flag)
548 return strnunvisx(dst, (
size_t)~0, src, flag);
552 strunvis(
char *dst,
const char *src)
554 return strnunvisx(dst, (
size_t)~0, src, 0);
558 strnunvis(
char *dst,
size_t dlen,
const char *src)
560 return strnunvisx(dst, dlen, src, 0);