xref: /illumos-gate/usr/src/cmd/spell/hashlook.c (revision 2a8bcb4e)
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 #include <stdlib.h>
31 #include <stdio.h>
32 #include "hash.h"
33 #include "huff.h"
34 
35 unsigned *table;
36 int hindex[NI];
37 
38 #define	B (BYTE * sizeof (unsigned))
39 #define	L (BYTE * sizeof (long)-1)
40 #define	MASK (~((unsigned long)1L<<L))
41 
42 #ifdef pdp11	/* sizeof (unsigned)==sizeof(long)/2 */
43 #define	fetch(wp, bp)\
44 	(((((long)wp[0]<<B)|wp[1])<<(B-bp))|(wp[2]>>bp))
45 #else 		/* sizeof (unsigned)==sizeof(long) */
46 #define	fetch(wp, bp) ((wp[0] << (B - bp)) | (wp[1] >> bp))
47 #endif
48 
49 int
hashlook(char * s)50 hashlook(char *s)
51 {
52 	unsigned long h;
53 	unsigned long t;
54 	int bp;
55 	unsigned *wp;
56 	long sum;
57 	unsigned *tp;
58 
59 	h = hash(s);
60 	t = h>>(HASHWIDTH-INDEXWIDTH);
61 	wp = &table[hindex[t]];
62 	tp = &table[hindex[t+1]];
63 	bp = B;
64 	sum = (long)t<<(HASHWIDTH-INDEXWIDTH);
65 	for (;;) {
66 		{
67 			/*
68 			 * this block is equivalent to:
69 			 * bp -= decode((fetch(wp, bp) >> 1) & MASK, &t);
70 			 */
71 			long y;
72 			long v;
73 
74 			/*
75 			 * shift 32 on those machines leaves destination
76 			 * unchanged
77 			 */
78 			if (bp == 0)
79 				y = 0;
80 			else
81 				y = wp[0] << (B - bp);
82 			if (bp < 32)
83 				y |= (wp[1] >> bp);
84 			y = (y >> 1) & MASK;
85 			if (y < cs) {
86 				t = y >> (long) (L+1-w);
87 				bp -= w-1;
88 			} else {
89 				for (bp -= w, v = v0; y >= qcs;
90 				    y = (y << 1) & MASK, v += n)
91 					bp -= 1;
92 				t = v + (y>> (long)(L-w));
93 			}
94 		}
95 		while (bp <= 0) {
96 			bp += B;
97 			wp++;
98 		}
99 		if (wp >= tp && (wp > tp||bp < B))
100 			return (0);
101 		sum += t;
102 		if (sum < h)
103 			continue;
104 		return (sum == h);
105 	}
106 }
107 
108 
109 int
prime(char * file)110 prime(char *file)
111 {
112 	FILE *f;
113 
114 #ifdef pdp11	/* because of insufficient address space for buffers */
115 	fd = dup(0);
116 	close(0);
117 	if (open(file, 0) != 0)
118 		return (0);
119 	f = stdin;
120 	if (rhuff(f) == 0 || read(fileno(f), (char *)hindex,
121 	    NI * sizeof (*hindex)) != NI * sizeof (*hindex) ||
122 	    (table = (unsigned *)malloc(hindex[NI-1] * sizeof (*table))) == 0 ||
123 	    read(fileno(f), (char *)table, sizeof (*table) * hindex[NI-1]) !=
124 	    hindex[NI-1] * sizeof (*table))
125 		return (0);
126 	close(0);
127 	if (dup(fd) != 0)
128 		return (0);
129 	close(fd);
130 #else
131 	if ((f = fopen(file, "r")) == NULL)
132 		return (0);
133 	if (rhuff(f) == 0 ||
134 	    fread((char *)hindex, sizeof (*hindex),  NI, f) != NI ||
135 	    (table = (unsigned *)malloc(hindex[NI-1] * sizeof (*table))) == 0 ||
136 	    fread((char *)table, sizeof (*table), hindex[NI-1], f) !=
137 	    hindex[NI-1])
138 		return (0);
139 	(void) fclose(f);
140 #endif
141 	hashinit();
142 	return (1);
143 }
144