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