00001 /* $NetBSD: unvis.c,v 1.22 2002/03/23 17:38:27 christos Exp $ */ 00002 00003 /*- 00004 * Copyright (c) 1989, 1993 00005 * The Regents of the University of California. All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 3. All advertising materials mentioning features or use of this software 00016 * must display the following acknowledgement: 00017 * This product includes software developed by the University of 00018 * California, Berkeley and its contributors. 00019 * 4. Neither the name of the University nor the names of its contributors 00020 * may be used to endorse or promote products derived from this software 00021 * without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00028 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00029 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00032 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00033 * SUCH DAMAGE. 00034 */ 00035 00036 #include "config.h" 00037 #if defined(LIBC_SCCS) && !defined(lint) 00038 #if 0 00039 static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; 00040 #else 00041 __RCSID("$NetBSD: unvis.c,v 1.22 2002/03/23 17:38:27 christos Exp $"); 00042 #endif 00043 #endif /* LIBC_SCCS and not lint */ 00044 00045 #define __LIBC12_SOURCE__ 00046 00047 #include <sys/types.h> 00048 00049 #include <assert.h> 00050 #include <ctype.h> 00051 #include <stdio.h> 00052 #include "np/vis.h" 00053 00054 #ifdef __weak_alias 00055 __weak_alias(strunvis,_strunvis) 00056 __weak_alias(unvis,_unvis) 00057 #endif 00058 00059 #ifdef __warn_references 00060 __warn_references(unvis, 00061 "warning: reference to compatibility unvis(); include <vis.h> for correct reference") 00062 #endif 00063 00064 #if !HAVE_VIS_H 00065 /* 00066 * decode driven by state machine 00067 */ 00068 #define S_GROUND 0 /* haven't seen escape char */ 00069 #define S_START 1 /* start decoding special sequence */ 00070 #define S_META 2 /* metachar started (M) */ 00071 #define S_META1 3 /* metachar more, regular char (-) */ 00072 #define S_CTRL 4 /* control char started (^) */ 00073 #define S_OCTAL2 5 /* octal digit 2 */ 00074 #define S_OCTAL3 6 /* octal digit 3 */ 00075 #define S_HEX1 7 /* hex digit */ 00076 #define S_HEX2 8 /* hex digit 2 */ 00077 00078 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') 00079 #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10)) 00080 00081 int 00082 unvis(cp, c, astate, flag) 00083 char *cp; 00084 int c; 00085 int *astate, flag; 00086 { 00087 return __unvis13(cp, (int)c, astate, flag); 00088 } 00089 00090 /* 00091 * unvis - decode characters previously encoded by vis 00092 */ 00093 int 00094 __unvis13(cp, c, astate, flag) 00095 char *cp; 00096 int c; 00097 int *astate, flag; 00098 { 00099 00100 _DIAGASSERT(cp != NULL); 00101 _DIAGASSERT(astate != NULL); 00102 00103 if (flag & UNVIS_END) { 00104 if (*astate == S_OCTAL2 || *astate == S_OCTAL3 00105 || *astate == S_HEX2) { 00106 *astate = S_GROUND; 00107 return (UNVIS_VALID); 00108 } 00109 return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD); 00110 } 00111 00112 switch (*astate) { 00113 00114 case S_GROUND: 00115 *cp = 0; 00116 if (c == '\\') { 00117 *astate = S_START; 00118 return (0); 00119 } 00120 if ((flag & VIS_HTTPSTYLE) && c == '%') { 00121 *astate = S_HEX1; 00122 return (0); 00123 } 00124 *cp = c; 00125 return (UNVIS_VALID); 00126 00127 case S_START: 00128 switch(c) { 00129 case '\\': 00130 *cp = c; 00131 *astate = S_GROUND; 00132 return (UNVIS_VALID); 00133 case '0': case '1': case '2': case '3': 00134 case '4': case '5': case '6': case '7': 00135 *cp = (c - '0'); 00136 *astate = S_OCTAL2; 00137 return (0); 00138 case 'M': 00139 *cp = (char)0200; 00140 *astate = S_META; 00141 return (0); 00142 case '^': 00143 *astate = S_CTRL; 00144 return (0); 00145 case 'n': 00146 *cp = '\n'; 00147 *astate = S_GROUND; 00148 return (UNVIS_VALID); 00149 case 'r': 00150 *cp = '\r'; 00151 *astate = S_GROUND; 00152 return (UNVIS_VALID); 00153 case 'b': 00154 *cp = '\b'; 00155 *astate = S_GROUND; 00156 return (UNVIS_VALID); 00157 case 'a': 00158 *cp = '\007'; 00159 *astate = S_GROUND; 00160 return (UNVIS_VALID); 00161 case 'v': 00162 *cp = '\v'; 00163 *astate = S_GROUND; 00164 return (UNVIS_VALID); 00165 case 't': 00166 *cp = '\t'; 00167 *astate = S_GROUND; 00168 return (UNVIS_VALID); 00169 case 'f': 00170 *cp = '\f'; 00171 *astate = S_GROUND; 00172 return (UNVIS_VALID); 00173 case 's': 00174 *cp = ' '; 00175 *astate = S_GROUND; 00176 return (UNVIS_VALID); 00177 case 'E': 00178 *cp = '\033'; 00179 *astate = S_GROUND; 00180 return (UNVIS_VALID); 00181 case '\n': 00182 /* 00183 * hidden newline 00184 */ 00185 *astate = S_GROUND; 00186 return (UNVIS_NOCHAR); 00187 case '$': 00188 /* 00189 * hidden marker 00190 */ 00191 *astate = S_GROUND; 00192 return (UNVIS_NOCHAR); 00193 } 00194 *astate = S_GROUND; 00195 return (UNVIS_SYNBAD); 00196 00197 case S_META: 00198 if (c == '-') 00199 *astate = S_META1; 00200 else if (c == '^') 00201 *astate = S_CTRL; 00202 else { 00203 *astate = S_GROUND; 00204 return (UNVIS_SYNBAD); 00205 } 00206 return (0); 00207 00208 case S_META1: 00209 *astate = S_GROUND; 00210 *cp |= c; 00211 return (UNVIS_VALID); 00212 00213 case S_CTRL: 00214 if (c == '?') 00215 *cp |= 0177; 00216 else 00217 *cp |= c & 037; 00218 *astate = S_GROUND; 00219 return (UNVIS_VALID); 00220 00221 case S_OCTAL2: /* second possible octal digit */ 00222 if (isoctal(c)) { 00223 /* 00224 * yes - and maybe a third 00225 */ 00226 *cp = (*cp << 3) + (c - '0'); 00227 *astate = S_OCTAL3; 00228 return (0); 00229 } 00230 /* 00231 * no - done with current sequence, push back passed char 00232 */ 00233 *astate = S_GROUND; 00234 return (UNVIS_VALIDPUSH); 00235 00236 case S_OCTAL3: /* third possible octal digit */ 00237 *astate = S_GROUND; 00238 if (isoctal(c)) { 00239 *cp = (*cp << 3) + (c - '0'); 00240 return (UNVIS_VALID); 00241 } 00242 /* 00243 * we were done, push back passed char 00244 */ 00245 return (UNVIS_VALIDPUSH); 00246 case S_HEX1: 00247 if (isxdigit(c)) { 00248 *cp = xtod(c); 00249 *astate = S_HEX2; 00250 return (0); 00251 } 00252 /* 00253 * no - done with current sequence, push back passed char 00254 */ 00255 *astate = S_GROUND; 00256 return (UNVIS_VALIDPUSH); 00257 case S_HEX2: 00258 *astate = S_GROUND; 00259 if (isxdigit(c)) { 00260 *cp = xtod(c) | (*cp << 4); 00261 return (UNVIS_VALID); 00262 } 00263 return (UNVIS_VALIDPUSH); 00264 default: 00265 /* 00266 * decoder in unknown state - (probably uninitialized) 00267 */ 00268 *astate = S_GROUND; 00269 return (UNVIS_SYNBAD); 00270 } 00271 } 00272 00273 /* 00274 * strunvis - decode src into dst 00275 * 00276 * Number of chars decoded into dst is returned, -1 on error. 00277 * Dst is null terminated. 00278 */ 00279 00280 int 00281 strunvisx(dst, src, flag) 00282 char *dst; 00283 const char *src; 00284 int flag; 00285 { 00286 char c; 00287 char *start = dst; 00288 int state = 0; 00289 00290 _DIAGASSERT(src != NULL); 00291 _DIAGASSERT(dst != NULL); 00292 00293 while ((c = *src++) != '\0') { 00294 again: 00295 switch (__unvis13(dst, c, &state, flag)) { 00296 case UNVIS_VALID: 00297 dst++; 00298 break; 00299 case UNVIS_VALIDPUSH: 00300 dst++; 00301 goto again; 00302 case 0: 00303 case UNVIS_NOCHAR: 00304 break; 00305 default: 00306 return (-1); 00307 } 00308 } 00309 if (__unvis13(dst, c, &state, UNVIS_END) == UNVIS_VALID) 00310 dst++; 00311 *dst = '\0'; 00312 return (dst - start); 00313 } 00314 00315 int 00316 strunvis(dst, src) 00317 char *dst; 00318 const char *src; 00319 { 00320 return strunvisx(dst, src, 0); 00321 } 00322 #endif
1.4.7

