xref: /openbsd-src/usr.bin/locate/code/locate.code.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*
2  *	$OpenBSD: locate.code.c,v 1.11 2003/07/07 21:36:16 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.11 2003/07/07 21:36:16 deraadt Exp $
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)locate.code.c	8.1 (Berkeley) 6/6/93";
46 #else
47 static char rcsid[] = "$OpenBSD: locate.code.c,v 1.11 2003/07/07 21:36:16 deraadt Exp $";
48 #endif
49 #endif /* not lint */
50 
51 /*
52  * PURPOSE:	sorted list compressor (works with a modified 'find'
53  *		to encode/decode a filename database)
54  *
55  * USAGE:	bigram < list > bigrams
56  *		process bigrams (see updatedb) > common_bigrams
57  *		code common_bigrams < list > squozen_list
58  *
59  * METHOD:	Uses 'front compression' (see ";login:", Volume 8, Number 1
60  *		February/March 1983, p. 8).  Output format is, per line, an
61  *		offset differential count byte followed by a partially bigram-
62  *		encoded ascii residue.  A bigram is a two-character sequence,
63  *		the first 128 most common of which are encoded in one byte.
64  *
65  * EXAMPLE:	For simple front compression with no bigram encoding,
66  *		if the input is...		then the output is...
67  *
68  *		/usr/src			 0 /usr/src
69  *		/usr/src/cmd/aardvark.c		 8 /cmd/aardvark.c
70  *		/usr/src/cmd/armadillo.c	14 armadillo.c
71  *		/usr/tmp/zoo			 5 tmp/zoo
72  *
73  *	The codes are:
74  *
75  *	0-28	likeliest differential counts + offset to make nonnegative
76  *	30	switch code for out-of-range count to follow in next word
77  *      31      an 8 bit char followed
78  *	128-255 bigram codes (128 most common, as determined by 'updatedb')
79  *	32-127  single character (printable) ascii residue (ie, literal)
80  *
81  * The locate database store any character except newline ('\n')
82  * and NUL ('\0'). The 8-bit character support don't wast extra
83  * space until you have characters in file names less than 32
84  * or greather than 127.
85  *
86  *
87  * SEE ALSO:	updatedb.sh, ../bigram/locate.bigram.c
88  *
89  * AUTHOR:	James A. Woods, Informatics General Corp.,
90  *		NASA Ames Research Center, 10/82
91  *              8-bit file names characters:
92  *              	Wolfram Schneider, Berlin September 1996
93  */
94 
95 #include <sys/param.h>
96 #include <err.h>
97 #include <errno.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <stdio.h>
101 #include "locate.h"
102 
103 #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
104 
105 u_char buf1[MAXPATHLEN] = " ";
106 u_char buf2[MAXPATHLEN];
107 u_char bigrams[BGBUFSIZE + 1] = { 0 };
108 
109 #define LOOKUP 1 /* use a lookup array instead a function, 3x faster */
110 
111 #ifdef LOOKUP
112 #define BGINDEX(x) (big[(u_char)*x][(u_char)*(x + 1)])
113 typedef short bg_t;
114 bg_t big[UCHAR_MAX + 1][UCHAR_MAX + 1];
115 #else
116 #define BGINDEX(x) bgindex(x)
117 typedef int bg_t;
118 int	bgindex(char *);
119 #endif /* LOOKUP */
120 
121 
122 void	usage(void);
123 extern int optind;
124 extern int optopt;
125 
126 int
127 main(int argc, char *argv[])
128 {
129 	u_char *cp, *oldpath, *path;
130 	int ch, code, count, diffcount, oldcount;
131 	FILE *fp;
132 	int i, j;
133 
134 	while ((ch = getopt(argc, argv, "")) != -1)
135 		switch(ch) {
136 		default:
137 			usage();
138 		}
139 	argc -= optind;
140 	argv += optind;
141 
142 	if (argc != 1)
143 		usage();
144 
145 	if ((fp = fopen(argv[0], "r")) == NULL)
146 		err(1, "%s", argv[0]);
147 
148 	/* First copy bigram array to stdout. */
149 	(void)fgets(bigrams, BGBUFSIZE + 1, fp);
150 
151 	if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
152 		err(1, "stdout");
153 	(void)fclose(fp);
154 
155 #ifdef LOOKUP
156 	/* init lookup table */
157 	for (i = 0; i < UCHAR_MAX + 1; i++)
158 	    	for (j = 0; j < UCHAR_MAX + 1; j++)
159 			big[i][j] = (bg_t)-1;
160 
161 	for (cp = bigrams, i = 0; *cp != '\0'; i += 2, cp += 2)
162 	        big[(u_char)*cp][(u_char)*(cp + 1)] = (bg_t)i;
163 
164 #endif /* LOOKUP */
165 
166 	oldpath = buf1;
167 	path = buf2;
168 	oldcount = 0;
169 
170 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
171 
172 		/* skip empty lines */
173 		if (*path == '\n')
174 			continue;
175 
176 		/* remove newline */
177 		for (cp = path; *cp != '\0'; cp++) {
178 			/* chop newline */
179 			if (*cp == '\n')
180 				*cp = '\0';
181 		}
182 
183 		/* Skip longest common prefix. */
184 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
185 			if (*cp == '\0')
186 				break;
187 
188 		count = cp - path;
189 		diffcount = count - oldcount + OFFSET;
190 		oldcount = count;
191 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
192 			if (putchar(SWITCH) == EOF ||
193 			    putw(diffcount, stdout) == EOF)
194 				err(1, "stdout");
195 		} else
196 			if (putchar(diffcount) == EOF)
197 				err(1, "stdout");
198 
199 		while (*cp != '\0') {
200 			/* print *two* characters */
201 
202 			if ((code = BGINDEX(cp)) != (bg_t)-1) {
203 				/*
204 				 * print *one* as bigram
205 				 * Found, so mark byte with
206 				 *  parity bit.
207 				 */
208 				if (putchar((code / 2) | PARITY) == EOF)
209 					err(1, "stdout");
210 				cp += 2;
211 			}
212 
213 			else {
214 				for (i = 0; i < 2; i++) {
215 					if (*cp == '\0')
216 						break;
217 
218 					/* print umlauts in file names */
219 					if (*cp < ASCII_MIN ||
220 					    *cp > ASCII_MAX) {
221 						if (putchar(UMLAUT) == EOF ||
222 						    putchar(*cp++) == EOF)
223 							err(1, "stdout");
224 					}
225 
226 					else {
227 						/* normal character */
228 						if(putchar(*cp++) == EOF)
229 							err(1, "stdout");
230 					}
231 				}
232 
233 			}
234 		}
235 
236 		if (path == buf1) {		/* swap pointers */
237 			path = buf2;
238 			oldpath = buf1;
239 		} else {
240 			path = buf1;
241 			oldpath = buf2;
242 		}
243 	}
244 	/* Non-zero status if there were errors */
245 	if (fflush(stdout) != 0 || ferror(stdout))
246 		exit(1);
247 	exit(0);
248 }
249 
250 #ifndef LOOKUP
251 int
252 bgindex(char *bg)			/* Return location of bg in bigrams or -1. */
253 {
254 	char bg0, bg1, *p;
255 
256 	bg0 = bg[0];
257 	bg1 = bg[1];
258 	for (p = bigrams; *p != NULL; p++)
259 		if (*p++ == bg0 && *p == bg1)
260 			break;
261 	return (*p == NULL ? -1 : (--p - bigrams));
262 }
263 #endif /* !LOOKUP */
264 
265 void
266 usage(void)
267 {
268 	(void)fprintf(stderr,
269 	    "usage: locate.code common_bigrams < list > squozen_list\n");
270 	exit(1);
271 }
272