1 /* $NetBSD: cnmagic.c,v 1.9 2006/11/01 10:17:58 yamt Exp $ */
4 * Copyright (c) 2000 Eduardo Horvath
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: cnmagic.c,v 1.9 2006/11/01 10:17:58 yamt Exp $");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #define ENCODE_STATE(c, n) (short)(((c)&0x1ff)|(((n)&0x7f)<<9))
37 static unsigned short cn_magic
[CNS_LEN
];
40 * Initialize a cnm_state_t.
43 cn_init_magic(cnm_state_t
*cnm
)
46 cnm
->cnm_magic
= cn_magic
;
50 * Destroy a cnm_state_t.
53 cn_destroy_magic(cnm_state_t
*cnm
)
56 cnm
->cnm_magic
= NULL
;
60 * Translate a magic string to a state
64 cn_set_magic(const char *magic
)
67 unsigned short m
[CNS_LEN
];
69 for (i
= 0; i
< CNS_LEN
; i
++) {
70 c
= (*magic
++) & 0xff;
71 n
= *magic
? i
+1 : CNS_TERM
;
79 printf("cn_set_magic(): empty!\n");
89 c
= (*magic
++) & 0xff;
90 n
= *magic
? i
+1 : CNS_TERM
;
105 /* Transition to the next state. */
108 printf("mag %d %x:%x\n", i
, c
, n
);
110 m
[i
] = ENCODE_STATE(c
, n
);
118 * Translatea state machine table back to
122 cn_get_magic(char *magic
, size_t maglen
)
126 for (i
= 0; i
< CNS_LEN
;) {
128 /* Translate a character */
129 switch (CNS_MAGIC_VAL(c
)) {
143 *magic
++ = (c
& 0x0ff);
146 /* Now go to the next state */
147 i
= CNS_MAGIC_NEXT(c
);
148 if (i
== CNS_TERM
|| i
== 0) {
149 /* Either termination state or empty machine */