7 static unsigned long extract __P((char *s,int start,int length));
8 static void standard __P((char *word));
9 static void insert __P((char *s, int x, int start, int length));
10 static int wsrch __P((char *w,int low,int high));
12 /* Dictionary for integer-word translations */
13 static char Wp[2048][4] = {
2064 /* Encode 8 bytes in 'c' as a string of English words.
2065 * Returns a pointer to a static buffer
2071 char cp[9]; /* add in room for the parity 2 bits*/
2076 /* compute parity */
2077 for(p = 0,i = 0; i < 64;i += 2)
2078 p += extract(cp,i,2);
2080 cp[8] = (char)p << 6;
2081 strncat(engout,&Wp[extract(cp, 0,11)][0],4);
2083 strncat(engout,&Wp[extract(cp,11,11)][0],4);
2085 strncat(engout,&Wp[extract(cp,22,11)][0],4);
2087 strncat(engout,&Wp[extract(cp,33,11)][0],4);
2089 strncat(engout,&Wp[extract(cp,44,11)][0],4);
2091 strncat(engout,&Wp[extract(cp,55,11)][0],4);
2093 printf("engout is %s\n\r",engout);
2098 /* convert English to binary
2099 * returns 1 OK - all good words and parity is OK
2100 * 0 word not in data base
2101 * -1 badly formed in put ie > 4 char word
2102 * -2 words OK but parity is wrong
2110 int i, v,l, low,high;
2118 strncpy(input,e,sizeof(input));
2120 memset(b, 0, sizeof(b));
2122 for(i=0,p=0;i<6;i++,p+=11){
2123 while ((word = strsep(&cp, " ")) != NULL && *word == '\0')
2138 if( (v = wsrch(word,low,high)) < 0 )
2140 insert(b,v,(int)p,11);
2143 /* now check the parity of what we got */
2144 for(p = 0, i = 0; i < 64; i +=2)
2145 p += extract(b, i, 2);
2147 if( (p & 3) != extract(b, 64,2) )
2154 /* Display 8 bytes as a series of 16-bit hex digits */
2160 sprintf(out,"%02X%02X %02X%02X %02X%02X %02X%02X",
2161 s[0] & 0xff,s[1] & 0xff,s[2] & 0xff,
2162 s[3] & 0xff,s[4] & 0xff,s[5] & 0xff,
2163 s[6] & 0xff,s[7] & 0xff);
2167 /* Encode 8 bytes in 'cp' as stream of ascii letters.
2168 * Provided as a possible alternative to btoe()
2175 static char out[31];
2177 /* code out put by characters 6 bits each added to 0x21 (!)*/
2178 for(i=0;i <= 10;i++){
2179 /* last one is only 4 bits not 6*/
2180 out[i] = '!'+ extract(cp,6*i,i >= 10 ? 4:6);
2187 /* Internal subroutines for word encoding/decoding */
2189 /* Dictionary binary search */
2199 if((j = strncmp(w,Wp[i],4)) == 0)
2200 return i; /* Found it */
2202 /* Avoid effects of integer truncation in /2 */
2203 if(strncmp(w,Wp[high],4) == 0)
2209 return -1; /* I don't *think* this can happen...*/
2211 high = i; /* Search lower half */
2213 low = i; /* Search upper half */
2217 insert(s, x, start, length)
2228 assert(length <= 11);
2230 assert(length >= 0);
2231 assert(start +length <= 66);
2233 shift = ((8 -(( start + length) % 8))%8);
2234 y = (long) x << shift;
2235 cl = (y >> 16) & 0xff;
2236 cc = (y >> 8) & 0xff;
2238 if(shift + length > 16){
2240 s[start/8 +1] |= cc;
2241 s[start/8 +2] |= cr;
2242 } else if(shift +length > 8){
2244 s[start/8 + 1] |= cr;
2252 register char *word;
2258 *word = toupper(*word);
2269 /* Extract 'length' bits from the char array 's' starting with bit 'start' */
2270 static unsigned long
2271 extract(s, start, length)
2280 assert(length <= 11);
2282 assert(length >= 0);
2283 assert(start +length <= 66);
2288 x = ((long)(cl<<8 | cc) <<8 | cr) ;
2289 x = x >> (24 - (length + (start %8)));
2290 x =( x & (0xffff >> (16-length) ) );