1 /* $NetBSD: unvis.c,v 1.44 2014/09/26 15:43:36 roy Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/types.h>
44 #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
48 * decode driven by state machine
50 #define S_GROUND 0 /* haven't seen escape char */
51 #define S_START 1 /* start decoding special sequence */
52 #define S_META 2 /* metachar started (M) */
53 #define S_META1 3 /* metachar more, regular char (-) */
54 #define S_CTRL 4 /* control char started (^) */
55 #define S_OCTAL2 5 /* octal digit 2 */
56 #define S_OCTAL3 6 /* octal digit 3 */
57 #define S_HEX 7 /* mandatory hex digit */
58 #define S_HEX1 8 /* http hex digit */
59 #define S_HEX2 9 /* http hex digit 2 */
61 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
62 #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
64 #define _VIS_END 0x0800 /* for unvis */
65 #define UNVIS_END _VIS_END
70 #define UNVIS_VALID 1 /* character valid */
71 #define UNVIS_VALIDPUSH 2 /* character valid, push back passed char */
72 #define UNVIS_NOCHAR 3 /* valid sequence, no character produced */
73 #define UNVIS_SYNBAD -1 /* unrecognized escape sequence */
74 #define UNVIS_ERROR -2 /* decoder in unknown state (unrecoverable) */
77 * unvis - decode characters previously encoded by vis
80 unvis(char *cp, char c, int *astate, int flag)
82 unsigned char uc = (unsigned char)c;
86 * Bottom 8 bits of astate hold the state machine state.
87 * Top 8 bits hold the current character in the http 1866 nv string decoding
89 #define GS(a) ((a) & 0xff)
90 #define SS(a, b) (((uint32_t)(a) << 24) | (b))
91 #define GI(a) ((uint32_t)(a) >> 24)
95 if (flag & UNVIS_END) {
100 *astate = SS(0, S_GROUND);
114 *astate = SS(0, S_START);
124 *astate = SS(0, S_GROUND);
126 case '0': case '1': case '2': case '3':
127 case '4': case '5': case '6': case '7':
129 *astate = SS(0, S_OCTAL2);
133 *astate = SS(0, S_META);
136 *astate = SS(0, S_CTRL);
140 *astate = SS(0, S_GROUND);
144 *astate = SS(0, S_GROUND);
148 *astate = SS(0, S_GROUND);
152 *astate = SS(0, S_GROUND);
156 *astate = SS(0, S_GROUND);
160 *astate = SS(0, S_GROUND);
164 *astate = SS(0, S_GROUND);
168 *astate = SS(0, S_GROUND);
172 *astate = SS(0, S_GROUND);
175 *astate = SS(0, S_HEX);
181 *astate = SS(0, S_GROUND);
187 *astate = SS(0, S_GROUND);
192 *astate = SS(0, S_GROUND);
200 *astate = SS(0, S_META1);
202 *astate = SS(0, S_CTRL);
208 *astate = SS(0, S_GROUND);
217 *astate = SS(0, S_GROUND);
220 case S_OCTAL2: /* second possible octal digit */
223 * yes - and maybe a third
225 *cp = (char)((*cp << 3) + (c - '0'));
226 *astate = SS(0, S_OCTAL3);
230 * no - done with current sequence, push back passed char
232 *astate = SS(0, S_GROUND);
233 return UNVIS_VALIDPUSH;
235 case S_OCTAL3: /* third possible octal digit */
236 *astate = SS(0, S_GROUND);
238 *cp = (char)((*cp << 3) + (c - '0'));
242 * we were done, push back passed char
244 return UNVIS_VALIDPUSH;
252 *cp = (char)xtod(uc);
253 *astate = SS(0, S_HEX2);
257 * no - done with current sequence, push back passed char
259 *astate = SS(0, S_GROUND);
260 return UNVIS_VALIDPUSH;
265 *cp = (char)(xtod(uc) | (*cp << 4));
268 return UNVIS_VALIDPUSH;
273 * decoder in unknown state - (probably uninitialized)
275 *astate = SS(0, S_GROUND);
281 * strnunvisx - decode src into dst
283 * Number of chars decoded into dst is returned, -1 on error.
284 * Dst is null terminated.
288 dhcpcd_strnunvis(char *dst, size_t dlen, const char *src)
291 char t = '\0', *start = dst;
294 #define CHECKSPACE() \
300 } while (/*CONSTCOND*/0)
302 while ((c = *src++) != '\0') {
304 switch (unvis(&t, c, &state, 0)) {
309 case UNVIS_VALIDPUSH:
324 if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) {
330 return (int)(dst - start);