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