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