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