1 /* $OpenBSD: unvis.c,v 1.15 2011/03/13 22:21:32 guenther Exp $ */ 2 /*- 3 * Copyright (c) 1989, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/types.h> 32 #include <ctype.h> 33 #include <vis.h> 34 35 /* 36 * decode driven by state machine 37 */ 38 #define S_GROUND 0 /* haven't seen escape char */ 39 #define S_START 1 /* start decoding special sequence */ 40 #define S_META 2 /* metachar started (M) */ 41 #define S_META1 3 /* metachar more, regular char (-) */ 42 #define S_CTRL 4 /* control char started (^) */ 43 #define S_OCTAL2 5 /* octal digit 2 */ 44 #define S_OCTAL3 6 /* octal digit 3 */ 45 46 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') 47 48 /* 49 * unvis - decode characters previously encoded by vis 50 */ 51 int 52 unvis(char *cp, char c, int *astate, int flag) 53 { 54 55 if (flag & UNVIS_END) { 56 if (*astate == S_OCTAL2 || *astate == S_OCTAL3) { 57 *astate = S_GROUND; 58 return (UNVIS_VALID); 59 } 60 return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD); 61 } 62 63 switch (*astate) { 64 65 case S_GROUND: 66 *cp = 0; 67 if (c == '\\') { 68 *astate = S_START; 69 return (0); 70 } 71 *cp = c; 72 return (UNVIS_VALID); 73 74 case S_START: 75 switch(c) { 76 case '-': 77 *cp = 0; 78 *astate = S_GROUND; 79 return (0); 80 case '\\': 81 *cp = c; 82 *astate = S_GROUND; 83 return (UNVIS_VALID); 84 case '0': case '1': case '2': case '3': 85 case '4': case '5': case '6': case '7': 86 *cp = (c - '0'); 87 *astate = S_OCTAL2; 88 return (0); 89 case 'M': 90 *cp = (char) 0200; 91 *astate = S_META; 92 return (0); 93 case '^': 94 *astate = S_CTRL; 95 return (0); 96 case 'n': 97 *cp = '\n'; 98 *astate = S_GROUND; 99 return (UNVIS_VALID); 100 case 'r': 101 *cp = '\r'; 102 *astate = S_GROUND; 103 return (UNVIS_VALID); 104 case 'b': 105 *cp = '\b'; 106 *astate = S_GROUND; 107 return (UNVIS_VALID); 108 case 'a': 109 *cp = '\007'; 110 *astate = S_GROUND; 111 return (UNVIS_VALID); 112 case 'v': 113 *cp = '\v'; 114 *astate = S_GROUND; 115 return (UNVIS_VALID); 116 case 't': 117 *cp = '\t'; 118 *astate = S_GROUND; 119 return (UNVIS_VALID); 120 case 'f': 121 *cp = '\f'; 122 *astate = S_GROUND; 123 return (UNVIS_VALID); 124 case 's': 125 *cp = ' '; 126 *astate = S_GROUND; 127 return (UNVIS_VALID); 128 case 'E': 129 *cp = '\033'; 130 *astate = S_GROUND; 131 return (UNVIS_VALID); 132 case '\n': 133 /* 134 * hidden newline 135 */ 136 *astate = S_GROUND; 137 return (UNVIS_NOCHAR); 138 case '$': 139 /* 140 * hidden marker 141 */ 142 *astate = S_GROUND; 143 return (UNVIS_NOCHAR); 144 } 145 *astate = S_GROUND; 146 return (UNVIS_SYNBAD); 147 148 case S_META: 149 if (c == '-') 150 *astate = S_META1; 151 else if (c == '^') 152 *astate = S_CTRL; 153 else { 154 *astate = S_GROUND; 155 return (UNVIS_SYNBAD); 156 } 157 return (0); 158 159 case S_META1: 160 *astate = S_GROUND; 161 *cp |= c; 162 return (UNVIS_VALID); 163 164 case S_CTRL: 165 if (c == '?') 166 *cp |= 0177; 167 else 168 *cp |= c & 037; 169 *astate = S_GROUND; 170 return (UNVIS_VALID); 171 172 case S_OCTAL2: /* second possible octal digit */ 173 if (isoctal(c)) { 174 /* 175 * yes - and maybe a third 176 */ 177 *cp = (*cp << 3) + (c - '0'); 178 *astate = S_OCTAL3; 179 return (0); 180 } 181 /* 182 * no - done with current sequence, push back passed char 183 */ 184 *astate = S_GROUND; 185 return (UNVIS_VALIDPUSH); 186 187 case S_OCTAL3: /* third possible octal digit */ 188 *astate = S_GROUND; 189 if (isoctal(c)) { 190 *cp = (*cp << 3) + (c - '0'); 191 return (UNVIS_VALID); 192 } 193 /* 194 * we were done, push back passed char 195 */ 196 return (UNVIS_VALIDPUSH); 197 198 default: 199 /* 200 * decoder in unknown state - (probably uninitialized) 201 */ 202 *astate = S_GROUND; 203 return (UNVIS_SYNBAD); 204 } 205 } 206 207 /* 208 * strunvis - decode src into dst 209 * 210 * Number of chars decoded into dst is returned, -1 on error. 211 * Dst is null terminated. 212 */ 213 214 int 215 strunvis(char *dst, const char *src) 216 { 217 char c; 218 char *start = dst; 219 int state = 0; 220 221 while ((c = *src++)) { 222 again: 223 switch (unvis(dst, c, &state, 0)) { 224 case UNVIS_VALID: 225 dst++; 226 break; 227 case UNVIS_VALIDPUSH: 228 dst++; 229 goto again; 230 case 0: 231 case UNVIS_NOCHAR: 232 break; 233 default: 234 *dst = '\0'; 235 return (-1); 236 } 237 } 238 if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID) 239 dst++; 240 *dst = '\0'; 241 return (dst - start); 242 } 243 244 ssize_t 245 strnunvis(char *dst, const char *src, size_t sz) 246 { 247 char c, p; 248 char *start = dst, *end = dst + sz - 1; 249 int state = 0; 250 251 if (sz > 0) 252 *end = '\0'; 253 while ((c = *src++)) { 254 again: 255 switch (unvis(&p, c, &state, 0)) { 256 case UNVIS_VALID: 257 if (dst < end) 258 *dst = p; 259 dst++; 260 break; 261 case UNVIS_VALIDPUSH: 262 if (dst < end) 263 *dst = p; 264 dst++; 265 goto again; 266 case 0: 267 case UNVIS_NOCHAR: 268 break; 269 default: 270 if (dst <= end) 271 *dst = '\0'; 272 return (-1); 273 } 274 } 275 if (unvis(&p, c, &state, UNVIS_END) == UNVIS_VALID) { 276 if (dst < end) 277 *dst = p; 278 dst++; 279 } 280 if (dst <= end) 281 *dst = '\0'; 282 return (dst - start); 283 } 284 285