Fri Sep 26 12:59:28 2014 UTC ()
Decode any printable characters encoded with VIS_CSTYLE so
unvis(3) works with vis.c r1.25


(roy)
diff -r1.41 -r1.42 src/lib/libc/gen/unvis.c

cvs diff -r1.41 -r1.42 src/lib/libc/gen/unvis.c (switch to unified diff)

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