xref: /illumos-gate/usr/src/cmd/spell/hashcheck.c (revision 0d8b5334)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <locale.h>
36 #include "hash.h"
37 #include "huff.h"
38 
39 int	decode(long, long *);
40 
41 int hindex[NI];
42 unsigned *table;
43 unsigned wp;
44 int bp;
45 #define	U (BYTE*sizeof (unsigned))
46 #define	L (BYTE*sizeof (long))
47 
48 static long
49 fetch(void)
50 {
51 	long w1;
52 	long y = 0;
53 	int empty = L;
54 	int i = bp;
55 	int tp = wp;
56 	while (empty >= i) {
57 		empty -= i;
58 		i = U;
59 		y |= (long)table[tp++] << empty;
60 	}
61 	if (empty > 0)
62 		y |= table[tp]>>i-empty;
63 	i = decode((y >> 1) &
64 	    (((unsigned long)1 << (BYTE * sizeof (y) - 1)) - 1), &w1);
65 	bp -= i;
66 	while (bp <= 0) {
67 		bp += U;
68 		wp++;
69 	}
70 	return (w1);
71 }
72 
73 
74 /* ARGSUSED */
75 int
76 main(int argc, char **argv)
77 {
78 	int i;
79 	long v;
80 	long a;
81 
82 	/* Set locale environment variables local definitions */
83 	(void) setlocale(LC_ALL, "");
84 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
85 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
86 #endif
87 	(void) textdomain(TEXT_DOMAIN);
88 
89 	(void) rhuff(stdin);
90 	(void) fread((char *)hindex, sizeof (*hindex), NI, stdin);
91 	table = (unsigned *)malloc(hindex[NI-1]*sizeof (*table));
92 	(void) fread((char *)table, sizeof (*table), hindex[NI-1], stdin);
93 	for (i = 0; i < NI-1; i++) {
94 		bp = U;
95 		v = (long)i<<(HASHWIDTH-INDEXWIDTH);
96 		for (wp = hindex[i]; wp < hindex[i+1]; ) {
97 			if (wp == hindex[i] && bp == U)
98 				a = fetch();
99 			else {
100 				a = fetch();
101 				if (a == 0)
102 					break;
103 			}
104 			if (wp > hindex[i+1] ||
105 				wp == hindex[i+1] && bp < U)
106 				break;
107 			v += a;
108 			(void) printf("%.9lo\n", v);
109 		}
110 	}
111 	return (0);
112 }
113