cnmagic.c revision 1.3 1 /* $NetBSD: cnmagic.c,v 1.3 2001/11/12 15:25:01 lukem Exp $ */
2
3 /*
4 * Copyright (c) 2000 Eduardo Horvath
5 * 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 Eduardo Horvath.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: cnmagic.c,v 1.3 2001/11/12 15:25:01 lukem Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39
40 #define ENCODE_STATE(c, n) (short)(((c)&0x1ff)|(((n)&0x7f)<<9))
41
42 static unsigned short cn_magic[CNS_LEN];
43
44 /*
45 * Initialize a cnm_state_t.
46 */
47 void
48 cn_init_magic(cnm_state_t *cnm) {
49 cnm->cnm_state = 0;
50 cnm->cnm_magic = cn_magic;
51 }
52
53 /*
54 * Destroy a cnm_state_t.
55 */
56 void
57 cn_destroy_magic(cnm_state_t *cnm) {
58 cnm->cnm_state = 0;
59 cnm->cnm_magic = NULL;
60 }
61
62 /*
63 * Translate a magic string to a state
64 * machine table.
65 */
66 int
67 cn_set_magic(char *magic)
68 {
69 unsigned int i, c, n;
70 unsigned short m[CNS_LEN];
71
72 for (i=0; i<CNS_LEN; i++) {
73 c = (*magic++)&0xff;
74 n = *magic ? i+1 : CNS_TERM;
75 switch (c) {
76 case 0:
77 /* End of string */
78 if (i == 0) {
79 /* empty string? */
80 cn_magic[0] = 0;
81 #ifdef DEBUG
82 printf("cn_set_magic(): empty!\n");
83 #endif
84 return (0);
85 }
86 do {
87 cn_magic[i] = m[i];
88 } while (i--);
89 return(0);
90 case 0x27:
91 /* Escape sequence */
92 c = (*magic++)&0xff;
93 n = *magic ? i+1 : CNS_TERM;
94 switch (c) {
95 case 0x27:
96 break;
97 case 0x01:
98 /* BREAK */
99 c = CNC_BREAK;
100 break;
101 case 0x02:
102 /* NUL */
103 c = 0;
104 break;
105 }
106 /* FALLTHROUGH */
107 default:
108 /* Transition to the next state. */
109 #ifdef DEBUG
110 if (!cold)
111 printf("mag %d %x:%x\n", i, c, n);
112 #endif
113 m[i] = ENCODE_STATE(c, n);
114 break;
115 }
116 }
117 return (EINVAL);
118 }
119
120 /*
121 * Translatea state machine table back to
122 * a magic string.
123 */
124 int
125 cn_get_magic(char *magic, int maglen) {
126 unsigned int i, c;
127
128 for (i=0; i<CNS_LEN; i++) {
129 c = cn_magic[i];
130 /* Translate a character */
131 switch (CNS_MAGIC_VAL(c)) {
132 case CNC_BREAK:
133 *magic++ = 0x27;
134 *magic++ = 0x01;
135 break;
136 case 0:
137 *magic++ = 0x27;
138 *magic++ = 0x02;
139 break;
140 case 0x27:
141 *magic++ = 0x27;
142 *magic++ = 0x27;
143 break;
144 default:
145 *magic++ = (c&0x0ff);
146 break;
147 }
148 /* Now go to the next state */
149 i = CNS_MAGIC_NEXT(c);
150 if (i == CNS_TERM || i == 0) {
151 /* Either termination state or empty machine */
152 *magic++ = 0;
153 return (0);
154 }
155 }
156 return (EINVAL);
157 }
158
159