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