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