xref: /openbsd-src/usr.bin/locate/code/locate.code.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*
2  *	$OpenBSD: locate.code.c,v 1.16 2009/10/27 23:59:39 deraadt Exp $
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * James A. Woods.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * 	$Id: locate.code.c,v 1.16 2009/10/27 23:59:39 deraadt Exp $
35  */
36 
37 /*
38  * PURPOSE:	sorted list compressor (works with a modified 'find'
39  *		to encode/decode a filename database)
40  *
41  * USAGE:	bigram < list > bigrams
42  *		process bigrams (see updatedb) > common_bigrams
43  *		code common_bigrams < list > squozen_list
44  *
45  * METHOD:	Uses 'front compression' (see ";login:", Volume 8, Number 1
46  *		February/March 1983, p. 8).  Output format is, per line, an
47  *		offset differential count byte followed by a partially bigram-
48  *		encoded ascii residue.  A bigram is a two-character sequence,
49  *		the first 128 most common of which are encoded in one byte.
50  *
51  * EXAMPLE:	For simple front compression with no bigram encoding,
52  *		if the input is...		then the output is...
53  *
54  *		/usr/src			 0 /usr/src
55  *		/usr/src/cmd/aardvark.c		 8 /cmd/aardvark.c
56  *		/usr/src/cmd/armadillo.c	14 armadillo.c
57  *		/usr/tmp/zoo			 5 tmp/zoo
58  *
59  *	The codes are:
60  *
61  *	0-28	likeliest differential counts + offset to make nonnegative
62  *	30	switch code for out-of-range count to follow in next word
63  *      31      an 8 bit char followed
64  *	128-255 bigram codes (128 most common, as determined by 'updatedb')
65  *	32-127  single character (printable) ascii residue (ie, literal)
66  *
67  * The locate database store any character except newline ('\n')
68  * and NUL ('\0'). The 8-bit character support don't wast extra
69  * space until you have characters in file names less than 32
70  * or greater than 127.
71  *
72  *
73  * SEE ALSO:	updatedb.sh, ../bigram/locate.bigram.c
74  *
75  * AUTHOR:	James A. Woods, Informatics General Corp.,
76  *		NASA Ames Research Center, 10/82
77  *              8-bit file names characters:
78  *              	Wolfram Schneider, Berlin September 1996
79  */
80 
81 #include <sys/param.h>
82 #include <err.h>
83 #include <errno.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <stdio.h>
87 #include "locate.h"
88 
89 #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
90 
91 u_char buf1[MAXPATHLEN] = " ";
92 u_char buf2[MAXPATHLEN];
93 u_char bigrams[BGBUFSIZE + 1] = { 0 };
94 
95 #define LOOKUP 1 /* use a lookup array instead a function, 3x faster */
96 
97 #ifdef LOOKUP
98 #define BGINDEX(x) (big[(u_char)*x][(u_char)*(x + 1)])
99 typedef short bg_t;
100 bg_t big[UCHAR_MAX + 1][UCHAR_MAX + 1];
101 #else
102 #define BGINDEX(x) bgindex(x)
103 typedef int bg_t;
104 int	bgindex(char *);
105 #endif /* LOOKUP */
106 
107 
108 void	usage(void);
109 extern int optind;
110 extern int optopt;
111 
112 int
113 main(int argc, char *argv[])
114 {
115 	u_char *cp, *oldpath, *path;
116 	int ch, code, count, diffcount, oldcount;
117 	FILE *fp;
118 	int i, j;
119 
120 	while ((ch = getopt(argc, argv, "")) != -1)
121 		switch (ch) {
122 		default:
123 			usage();
124 		}
125 	argc -= optind;
126 	argv += optind;
127 
128 	if (argc != 1)
129 		usage();
130 
131 	if ((fp = fopen(argv[0], "r")) == NULL)
132 		err(1, "%s", argv[0]);
133 
134 	/* First copy bigram array to stdout. */
135 	if (fgets(bigrams, sizeof(bigrams), fp) == NULL)
136 		err(1, "fgets");
137 
138 	if (strlen(bigrams) != BGBUFSIZE)
139 		errx(1, "bigram array too small to build db, index more files");
140 
141 	if (fputs(bigrams, stdout) == EOF)
142 		err(1, "stdout");
143 	(void)fclose(fp);
144 
145 #ifdef LOOKUP
146 	/* init lookup table */
147 	for (i = 0; i < UCHAR_MAX + 1; i++)
148 	    	for (j = 0; j < UCHAR_MAX + 1; j++)
149 			big[i][j] = (bg_t)-1;
150 
151 	for (cp = bigrams, i = 0; *cp != '\0'; i += 2, cp += 2)
152 		big[(u_char)*cp][(u_char)*(cp + 1)] = (bg_t)i;
153 
154 #endif /* LOOKUP */
155 
156 	oldpath = buf1;
157 	path = buf2;
158 	oldcount = 0;
159 
160 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
161 
162 		/* skip empty lines */
163 		if (*path == '\n')
164 			continue;
165 
166 		/* remove newline */
167 		for (cp = path; *cp != '\0'; cp++) {
168 			/* chop newline */
169 			if (*cp == '\n')
170 				*cp = '\0';
171 		}
172 
173 		/* Skip longest common prefix. */
174 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
175 			if (*cp == '\0')
176 				break;
177 
178 		count = cp - path;
179 		diffcount = count - oldcount + OFFSET;
180 		oldcount = count;
181 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
182 			if (putchar(SWITCH) == EOF ||
183 			    putw(diffcount, stdout) == EOF)
184 				err(1, "stdout");
185 		} else
186 			if (putchar(diffcount) == EOF)
187 				err(1, "stdout");
188 
189 		while (*cp != '\0') {
190 			/* print *two* characters */
191 
192 			if ((code = BGINDEX(cp)) != (bg_t)-1) {
193 				/*
194 				 * print *one* as bigram
195 				 * Found, so mark byte with
196 				 *  parity bit.
197 				 */
198 				if (putchar((code / 2) | PARITY) == EOF)
199 					err(1, "stdout");
200 				cp += 2;
201 			} else {
202 				for (i = 0; i < 2; i++) {
203 					if (*cp == '\0')
204 						break;
205 
206 					/* print umlauts in file names */
207 					if (*cp < ASCII_MIN ||
208 					    *cp > ASCII_MAX) {
209 						if (putchar(UMLAUT) == EOF ||
210 						    putchar(*cp++) == EOF)
211 							err(1, "stdout");
212 					} else {
213 						/* normal character */
214 						if (putchar(*cp++) == EOF)
215 							err(1, "stdout");
216 					}
217 				}
218 
219 			}
220 		}
221 
222 		if (path == buf1) {		/* swap pointers */
223 			path = buf2;
224 			oldpath = buf1;
225 		} else {
226 			path = buf1;
227 			oldpath = buf2;
228 		}
229 	}
230 	/* Non-zero status if there were errors */
231 	if (fflush(stdout) != 0 || ferror(stdout))
232 		exit(1);
233 	exit(0);
234 }
235 
236 #ifndef LOOKUP
237 int
238 bgindex(char *bg)			/* Return location of bg in bigrams or -1. */
239 {
240 	char bg0, bg1, *p;
241 
242 	bg0 = bg[0];
243 	bg1 = bg[1];
244 	for (p = bigrams; *p != NULL; p++)
245 		if (*p++ == bg0 && *p == bg1)
246 			break;
247 	return (*p == NULL ? -1 : (--p - bigrams));
248 }
249 #endif /* !LOOKUP */
250 
251 void
252 usage(void)
253 {
254 	(void)fprintf(stderr,
255 	    "usage: locate.code common_bigrams < list > squozen_list\n");
256 	exit(1);
257 }
258