cnmagic.c revision 1.2 1 /* $NetBSD: cnmagic.c,v 1.2 2000/12/19 04:39:19 mrg 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/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36
37 #define ENCODE_STATE(c, n) (short)(((c)&0x1ff)|(((n)&0x7f)<<9))
38
39 static unsigned short cn_magic[CNS_LEN];
40
41 /*
42 * Initialize a cnm_state_t.
43 */
44 void
45 cn_init_magic(cnm_state_t *cnm) {
46 cnm->cnm_state = 0;
47 cnm->cnm_magic = cn_magic;
48 }
49
50 /*
51 * Destroy a cnm_state_t.
52 */
53 void
54 cn_destroy_magic(cnm_state_t *cnm) {
55 cnm->cnm_state = 0;
56 cnm->cnm_magic = NULL;
57 }
58
59 /*
60 * Translate a magic string to a state
61 * machine table.
62 */
63 int
64 cn_set_magic(char *magic)
65 {
66 unsigned int i, c, n;
67 unsigned short m[CNS_LEN];
68
69 for (i=0; i<CNS_LEN; i++) {
70 c = (*magic++)&0xff;
71 n = *magic ? i+1 : CNS_TERM;
72 switch (c) {
73 case 0:
74 /* End of string */
75 if (i == 0) {
76 /* empty string? */
77 cn_magic[0] = 0;
78 #ifdef DEBUG
79 printf("cn_set_magic(): empty!\n");
80 #endif
81 return (0);
82 }
83 do {
84 cn_magic[i] = m[i];
85 } while (i--);
86 return(0);
87 case 0x27:
88 /* Escape sequence */
89 c = (*magic++)&0xff;
90 n = *magic ? i+1 : CNS_TERM;
91 switch (c) {
92 case 0x27:
93 break;
94 case 0x01:
95 /* BREAK */
96 c = CNC_BREAK;
97 break;
98 case 0x02:
99 /* NUL */
100 c = 0;
101 break;
102 }
103 /* FALLTHROUGH */
104 default:
105 /* Transition to the next state. */
106 #ifdef DEBUG
107 if (!cold)
108 printf("mag %d %x:%x\n", i, c, n);
109 #endif
110 m[i] = ENCODE_STATE(c, n);
111 break;
112 }
113 }
114 return (EINVAL);
115 }
116
117 /*
118 * Translatea state machine table back to
119 * a magic string.
120 */
121 int
122 cn_get_magic(char *magic, int maglen) {
123 unsigned int i, c;
124
125 for (i=0; i<CNS_LEN; i++) {
126 c = cn_magic[i];
127 /* Translate a character */
128 switch (CNS_MAGIC_VAL(c)) {
129 case CNC_BREAK:
130 *magic++ = 0x27;
131 *magic++ = 0x01;
132 break;
133 case 0:
134 *magic++ = 0x27;
135 *magic++ = 0x02;
136 break;
137 case 0x27:
138 *magic++ = 0x27;
139 *magic++ = 0x27;
140 break;
141 default:
142 *magic++ = (c&0x0ff);
143 break;
144 }
145 /* Now go to the next state */
146 i = CNS_MAGIC_NEXT(c);
147 if (i == CNS_TERM || i == 0) {
148 /* Either termination state or empty machine */
149 *magic++ = 0;
150 return (0);
151 }
152 }
153 return (EINVAL);
154 }
155
156