1 /* $NetBSD: unvis.c,v 1.27 2005/05/16 11:42:04 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; 36 #else 37 __RCSID("$NetBSD: unvis.c,v 1.27 2005/05/16 11:42:04 lukem Exp $"); 38 #endif 39 #endif /* LIBC_SCCS and not lint */ 40 41 #define __LIBC12_SOURCE__ 42 43 #include "namespace.h" 44 #include <sys/types.h> 45 46 #include <assert.h> 47 #include <ctype.h> 48 #include <stdio.h> 49 #include <vis.h> 50 51 #ifdef __weak_alias 52 __weak_alias(strunvis,_strunvis) 53 __weak_alias(unvis,_unvis) 54 #endif 55 56 #ifdef __warn_references 57 __warn_references(unvis, 58 "warning: reference to compatibility unvis(); include <vis.h> for correct reference") 59 #endif 60 61 #if !HAVE_VIS 62 /* 63 * decode driven by state machine 64 */ 65 #define S_GROUND 0 /* haven't seen escape char */ 66 #define S_START 1 /* start decoding special sequence */ 67 #define S_META 2 /* metachar started (M) */ 68 #define S_META1 3 /* metachar more, regular char (-) */ 69 #define S_CTRL 4 /* control char started (^) */ 70 #define S_OCTAL2 5 /* octal digit 2 */ 71 #define S_OCTAL3 6 /* octal digit 3 */ 72 #define S_HEX1 7 /* hex digit */ 73 #define S_HEX2 8 /* hex digit 2 */ 74 75 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') 76 #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10)) 77 78 int 79 unvis(cp, c, astate, flag) 80 char *cp; 81 int c; 82 int *astate, flag; 83 { 84 return __unvis13(cp, c, astate, flag); 85 } 86 87 /* 88 * unvis - decode characters previously encoded by vis 89 */ 90 int 91 __unvis13(cp, c, astate, flag) 92 char *cp; 93 int c; 94 int *astate, flag; 95 { 96 unsigned char uc = (unsigned char)c; 97 98 _DIAGASSERT(cp != NULL); 99 _DIAGASSERT(astate != NULL); 100 101 if (flag & UNVIS_END) { 102 if (*astate == S_OCTAL2 || *astate == S_OCTAL3 103 || *astate == S_HEX2) { 104 *astate = S_GROUND; 105 return (UNVIS_VALID); 106 } 107 return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD); 108 } 109 110 switch (*astate) { 111 112 case S_GROUND: 113 *cp = 0; 114 if (c == '\\') { 115 *astate = S_START; 116 return (0); 117 } 118 if ((flag & VIS_HTTPSTYLE) && c == '%') { 119 *astate = S_HEX1; 120 return (0); 121 } 122 *cp = c; 123 return (UNVIS_VALID); 124 125 case S_START: 126 switch(c) { 127 case '\\': 128 *cp = c; 129 *astate = S_GROUND; 130 return (UNVIS_VALID); 131 case '0': case '1': case '2': case '3': 132 case '4': case '5': case '6': case '7': 133 *cp = (c - '0'); 134 *astate = S_OCTAL2; 135 return (0); 136 case 'M': 137 *cp = (char)0200; 138 *astate = S_META; 139 return (0); 140 case '^': 141 *astate = S_CTRL; 142 return (0); 143 case 'n': 144 *cp = '\n'; 145 *astate = S_GROUND; 146 return (UNVIS_VALID); 147 case 'r': 148 *cp = '\r'; 149 *astate = S_GROUND; 150 return (UNVIS_VALID); 151 case 'b': 152 *cp = '\b'; 153 *astate = S_GROUND; 154 return (UNVIS_VALID); 155 case 'a': 156 *cp = '\007'; 157 *astate = S_GROUND; 158 return (UNVIS_VALID); 159 case 'v': 160 *cp = '\v'; 161 *astate = S_GROUND; 162 return (UNVIS_VALID); 163 case 't': 164 *cp = '\t'; 165 *astate = S_GROUND; 166 return (UNVIS_VALID); 167 case 'f': 168 *cp = '\f'; 169 *astate = S_GROUND; 170 return (UNVIS_VALID); 171 case 's': 172 *cp = ' '; 173 *astate = S_GROUND; 174 return (UNVIS_VALID); 175 case 'E': 176 *cp = '\033'; 177 *astate = S_GROUND; 178 return (UNVIS_VALID); 179 case '\n': 180 /* 181 * hidden newline 182 */ 183 *astate = S_GROUND; 184 return (UNVIS_NOCHAR); 185 case '$': 186 /* 187 * hidden marker 188 */ 189 *astate = S_GROUND; 190 return (UNVIS_NOCHAR); 191 } 192 *astate = S_GROUND; 193 return (UNVIS_SYNBAD); 194 195 case S_META: 196 if (c == '-') 197 *astate = S_META1; 198 else if (c == '^') 199 *astate = S_CTRL; 200 else { 201 *astate = S_GROUND; 202 return (UNVIS_SYNBAD); 203 } 204 return (0); 205 206 case S_META1: 207 *astate = S_GROUND; 208 *cp |= c; 209 return (UNVIS_VALID); 210 211 case S_CTRL: 212 if (c == '?') 213 *cp |= 0177; 214 else 215 *cp |= c & 037; 216 *astate = S_GROUND; 217 return (UNVIS_VALID); 218 219 case S_OCTAL2: /* second possible octal digit */ 220 if (isoctal(uc)) { 221 /* 222 * yes - and maybe a third 223 */ 224 *cp = (*cp << 3) + (c - '0'); 225 *astate = S_OCTAL3; 226 return (0); 227 } 228 /* 229 * no - done with current sequence, push back passed char 230 */ 231 *astate = S_GROUND; 232 return (UNVIS_VALIDPUSH); 233 234 case S_OCTAL3: /* third possible octal digit */ 235 *astate = S_GROUND; 236 if (isoctal(uc)) { 237 *cp = (*cp << 3) + (c - '0'); 238 return (UNVIS_VALID); 239 } 240 /* 241 * we were done, push back passed char 242 */ 243 return (UNVIS_VALIDPUSH); 244 245 case S_HEX1: 246 if (isxdigit(uc)) { 247 *cp = xtod(uc); 248 *astate = S_HEX2; 249 return (0); 250 } 251 /* 252 * no - done with current sequence, push back passed char 253 */ 254 *astate = S_GROUND; 255 return (UNVIS_VALIDPUSH); 256 257 case S_HEX2: 258 *astate = S_GROUND; 259 if (isxdigit(uc)) { 260 *cp = xtod(uc) | (*cp << 4); 261 return (UNVIS_VALID); 262 } 263 return (UNVIS_VALIDPUSH); 264 265 default: 266 /* 267 * decoder in unknown state - (probably uninitialized) 268 */ 269 *astate = S_GROUND; 270 return (UNVIS_SYNBAD); 271 } 272 } 273 274 /* 275 * strunvis - decode src into dst 276 * 277 * Number of chars decoded into dst is returned, -1 on error. 278 * Dst is null terminated. 279 */ 280 281 int 282 strunvisx(dst, src, flag) 283 char *dst; 284 const char *src; 285 int flag; 286 { 287 char c; 288 char *start = dst; 289 int state = 0; 290 291 _DIAGASSERT(src != NULL); 292 _DIAGASSERT(dst != NULL); 293 294 while ((c = *src++) != '\0') { 295 again: 296 switch (__unvis13(dst, c, &state, flag)) { 297 case UNVIS_VALID: 298 dst++; 299 break; 300 case UNVIS_VALIDPUSH: 301 dst++; 302 goto again; 303 case 0: 304 case UNVIS_NOCHAR: 305 break; 306 default: 307 return (-1); 308 } 309 } 310 if (__unvis13(dst, c, &state, UNVIS_END) == UNVIS_VALID) 311 dst++; 312 *dst = '\0'; 313 return (dst - start); 314 } 315 316 int 317 strunvis(dst, src) 318 char *dst; 319 const char *src; 320 { 321 return strunvisx(dst, src, 0); 322 } 323 #endif 324