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