xref: /openbsd-src/usr.bin/locate/code/locate.code.c (revision ddce81d18ead1cead306cecb446175faf94d13e9)
1 /*
2  *	$OpenBSD: locate.code.c,v 1.20 2018/05/30 15:13:22 espie 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.20 2018/05/30 15:13:22 espie 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 <err.h>
82 #include <errno.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87 #include <limits.h>
88 
89 #include "locate.h"
90 
91 #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
92 
93 u_char buf1[PATH_MAX] = " ";
94 u_char buf2[PATH_MAX];
95 u_char bigrams[BGBUFSIZE + 1] = { 0 };
96 
97 #define LOOKUP 1 /* use a lookup array instead a function, 3x faster */
98 
99 #ifdef LOOKUP
100 #define BGINDEX(x) (big[(u_char)*x][(u_char)*(x + 1)])
101 typedef short bg_t;
102 bg_t big[UCHAR_MAX + 1][UCHAR_MAX + 1];
103 #else
104 #define BGINDEX(x) bgindex(x)
105 typedef int bg_t;
106 int	bgindex(char *);
107 #endif /* LOOKUP */
108 
109 
110 void	usage(void);
111 extern int optind;
112 extern int optopt;
113 
114 int
115 main(int argc, char *argv[])
116 {
117 	u_char *cp, *oldpath, *path;
118 	int ch, code, count, diffcount, oldcount;
119 	FILE *fp;
120 	int i, j;
121 
122 	if (pledge("stdio rpath", NULL) == -1)
123 		err(1, "pledge");
124 
125 	while ((ch = getopt(argc, argv, "")) != -1)
126 		switch (ch) {
127 		default:
128 			usage();
129 		}
130 	argc -= optind;
131 	argv += optind;
132 
133 	if (argc != 1)
134 		usage();
135 
136 	if ((fp = fopen(argv[0], "r")) == NULL)
137 		err(1, "%s", argv[0]);
138 
139 	if (pledge("stdio", NULL) == -1)
140 		err(1, "pledge");
141 
142 	/* First copy bigram array to stdout. */
143 	if (fgets(bigrams, sizeof(bigrams), fp) == NULL) {
144 		if (ferror(fp)) {
145 			err(1, "fgets on %s", argv[0]);
146 		} else {
147 			errx(1, "premature end of bigram file %s", argv[0]);
148 		}
149 	}
150 
151 	if (strlen(bigrams) != BGBUFSIZE)
152 		errx(1, "bigram array too small to build db, index more files");
153 
154 	if (fputs(bigrams, stdout) == EOF)
155 		err(1, "stdout");
156 	(void)fclose(fp);
157 
158 #ifdef LOOKUP
159 	/* init lookup table */
160 	for (i = 0; i < UCHAR_MAX + 1; i++)
161 	    	for (j = 0; j < UCHAR_MAX + 1; j++)
162 			big[i][j] = (bg_t)-1;
163 
164 	for (cp = bigrams, i = 0; *cp != '\0'; i += 2, cp += 2)
165 		big[(u_char)*cp][(u_char)*(cp + 1)] = (bg_t)i;
166 
167 #endif /* LOOKUP */
168 
169 	oldpath = buf1;
170 	path = buf2;
171 	oldcount = 0;
172 
173 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
174 
175 		/* skip empty lines */
176 		if (*path == '\n')
177 			continue;
178 
179 		/* remove newline */
180 		for (cp = path; *cp != '\0'; cp++) {
181 			/* chop newline */
182 			if (*cp == '\n')
183 				*cp = '\0';
184 		}
185 
186 		/* Skip longest common prefix. */
187 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
188 			if (*cp == '\0')
189 				break;
190 
191 		count = cp - path;
192 		diffcount = count - oldcount + OFFSET;
193 		oldcount = count;
194 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
195 			if (putchar(SWITCH) == EOF ||
196 			    putw(diffcount, stdout) == EOF)
197 				err(1, "stdout");
198 		} else
199 			if (putchar(diffcount) == EOF)
200 				err(1, "stdout");
201 
202 		while (*cp != '\0') {
203 			/* print *two* characters */
204 
205 			if ((code = BGINDEX(cp)) != (bg_t)-1) {
206 				/*
207 				 * print *one* as bigram
208 				 * Found, so mark byte with
209 				 *  parity bit.
210 				 */
211 				if (putchar((code / 2) | PARITY) == EOF)
212 					err(1, "stdout");
213 				cp += 2;
214 			} else {
215 				for (i = 0; i < 2; i++) {
216 					if (*cp == '\0')
217 						break;
218 
219 					/* print umlauts in file names */
220 					if (*cp < ASCII_MIN ||
221 					    *cp > ASCII_MAX) {
222 						if (putchar(UMLAUT) == EOF ||
223 						    putchar(*cp++) == EOF)
224 							err(1, "stdout");
225 					} else {
226 						/* normal character */
227 						if (putchar(*cp++) == EOF)
228 							err(1, "stdout");
229 					}
230 				}
231 
232 			}
233 		}
234 
235 		if (path == buf1) {		/* swap pointers */
236 			path = buf2;
237 			oldpath = buf1;
238 		} else {
239 			path = buf1;
240 			oldpath = buf2;
241 		}
242 	}
243 	/* Non-zero status if there were errors */
244 	if (fflush(stdout) != 0 || ferror(stdout))
245 		exit(1);
246 	exit(0);
247 }
248 
249 #ifndef LOOKUP
250 int
251 bgindex(char *bg)			/* Return location of bg in bigrams or -1. */
252 {
253 	char bg0, bg1, *p;
254 
255 	bg0 = bg[0];
256 	bg1 = bg[1];
257 	for (p = bigrams; *p != NULL; p++)
258 		if (*p++ == bg0 && *p == bg1)
259 			break;
260 	return (*p == NULL ? -1 : (--p - bigrams));
261 }
262 #endif /* !LOOKUP */
263 
264 void
265 usage(void)
266 {
267 	(void)fprintf(stderr,
268 	    "usage: locate.code common_bigrams < list > squozen_list\n");
269 	exit(1);
270 }
271