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
33 * HEAVILY trimmed down for use only in dhcpcd.
34 * Please use the source in NetBSD for a fuller working copy.
37 #include <sys/types.h>
49 #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
53 * decode driven by state machine
55 #define S_GROUND 0 /* haven't seen escape char */
56 #define S_START 1 /* start decoding special sequence */
57 #define S_META 2 /* metachar started (M) */
58 #define S_META1 3 /* metachar more, regular char (-) */
59 #define S_CTRL 4 /* control char started (^) */
60 #define S_OCTAL2 5 /* octal digit 2 */
61 #define S_OCTAL3 6 /* octal digit 3 */
62 #define S_HEX 7 /* mandatory hex digit */
63 #define S_HEX1 8 /* http hex digit */
64 #define S_HEX2 9 /* http hex digit 2 */
66 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
67 #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
69 #define _VIS_END 0x0800 /* for unvis */
70 #define UNVIS_END _VIS_END
75 #define UNVIS_VALID 1 /* character valid */
76 #define UNVIS_VALIDPUSH 2 /* character valid, push back passed char */
77 #define UNVIS_NOCHAR 3 /* valid sequence, no character produced */
78 #define UNVIS_SYNBAD -1 /* unrecognized escape sequence */
79 #define UNVIS_ERROR -2 /* decoder in unknown state (unrecoverable) */
82 * unvis - decode characters previously encoded by vis
85 unvis(char *cp, int c, int *astate, int flag)
87 unsigned char uc = (unsigned char)c;
91 * Bottom 8 bits of astate hold the state machine state.
92 * Top 8 bits hold the current character in the http 1866 nv string decoding
94 #define GS(a) ((a) & 0xff)
95 #define SS(a, b) (((uint32_t)(a) << 24) | (b))
96 #define GI(a) ((uint32_t)(a) >> 24)
100 if (flag & UNVIS_END) {
105 *astate = SS(0, S_GROUND);
119 *astate = SS(0, S_START);
129 *astate = SS(0, S_GROUND);
131 case '0': case '1': case '2': case '3':
132 case '4': case '5': case '6': case '7':
133 *cp = (char)(c - '0');
134 *astate = SS(0, S_OCTAL2);
138 *astate = SS(0, S_META);
141 *astate = SS(0, S_CTRL);
145 *astate = SS(0, S_GROUND);
149 *astate = SS(0, S_GROUND);
153 *astate = SS(0, S_GROUND);
157 *astate = SS(0, S_GROUND);
161 *astate = SS(0, S_GROUND);
165 *astate = SS(0, S_GROUND);
169 *astate = SS(0, S_GROUND);
173 *astate = SS(0, S_GROUND);
177 *astate = SS(0, S_GROUND);
180 *astate = SS(0, S_HEX);
186 *astate = SS(0, S_GROUND);
192 *astate = SS(0, S_GROUND);
197 *astate = SS(0, S_GROUND);
205 *astate = SS(0, S_META1);
207 *astate = SS(0, S_CTRL);
213 *astate = SS(0, S_GROUND);
222 *astate = SS(0, S_GROUND);
225 case S_OCTAL2: /* second possible octal digit */
228 * yes - and maybe a third
230 *cp = (char)((*cp << 3) + (c - '0'));
231 *astate = SS(0, S_OCTAL3);
235 * no - done with current sequence, push back passed char
237 *astate = SS(0, S_GROUND);
238 return UNVIS_VALIDPUSH;
240 case S_OCTAL3: /* third possible octal digit */
241 *astate = SS(0, S_GROUND);
243 *cp = (char)((*cp << 3) + (c - '0'));
247 * we were done, push back passed char
249 return UNVIS_VALIDPUSH;
257 *cp = (char)xtod(uc);
258 *astate = SS(0, S_HEX2);
262 * no - done with current sequence, push back passed char
264 *astate = SS(0, S_GROUND);
265 return UNVIS_VALIDPUSH;
270 *cp = (char)(xtod(uc) | (*cp << 4));
273 return UNVIS_VALIDPUSH;
278 * decoder in unknown state - (probably uninitialized)
280 *astate = SS(0, S_GROUND);
286 * strnunvisx - decode src into dst
288 * Number of chars decoded into dst is returned, -1 on error.
289 * Dst is null terminated.
293 dhcpcd_strnunvis(char *dst, size_t dlen, const char *src)
296 char t = '\0', *start = dst;
299 #define CHECKSPACE() \
305 } while (/*CONSTCOND*/0)
307 while ((c = *src++) != '\0') {
309 switch (unvis(&t, c, &state, 0)) {
314 case UNVIS_VALIDPUSH:
329 if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) {
335 return (int)(dst - start);