1df930be7Sderaadt /*
2*47f32dc2Stedu * $OpenBSD: locate.code.c,v 1.21 2019/01/17 06:15:44 tedu Exp $
30f3d8120Smichaels *
4df930be7Sderaadt * Copyright (c) 1989, 1993
5df930be7Sderaadt * The Regents of the University of California. All rights reserved.
6df930be7Sderaadt *
7df930be7Sderaadt * This code is derived from software contributed to Berkeley by
8df930be7Sderaadt * James A. Woods.
9df930be7Sderaadt *
10df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
11df930be7Sderaadt * modification, are permitted provided that the following conditions
12df930be7Sderaadt * are met:
13df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
14df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
15df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
16df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
17df930be7Sderaadt * documentation and/or other materials provided with the distribution.
18f75387cbSmillert * 3. Neither the name of the University nor the names of its contributors
19df930be7Sderaadt * may be used to endorse or promote products derived from this software
20df930be7Sderaadt * without specific prior written permission.
21df930be7Sderaadt *
22df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df930be7Sderaadt * SUCH DAMAGE.
33df930be7Sderaadt */
34df930be7Sderaadt
35df930be7Sderaadt /*
36df930be7Sderaadt * PURPOSE: sorted list compressor (works with a modified 'find'
37df930be7Sderaadt * to encode/decode a filename database)
38df930be7Sderaadt *
39df930be7Sderaadt * USAGE: bigram < list > bigrams
40df930be7Sderaadt * process bigrams (see updatedb) > common_bigrams
41df930be7Sderaadt * code common_bigrams < list > squozen_list
42df930be7Sderaadt *
43df930be7Sderaadt * METHOD: Uses 'front compression' (see ";login:", Volume 8, Number 1
44df930be7Sderaadt * February/March 1983, p. 8). Output format is, per line, an
45df930be7Sderaadt * offset differential count byte followed by a partially bigram-
46df930be7Sderaadt * encoded ascii residue. A bigram is a two-character sequence,
47df930be7Sderaadt * the first 128 most common of which are encoded in one byte.
48df930be7Sderaadt *
49df930be7Sderaadt * EXAMPLE: For simple front compression with no bigram encoding,
50df930be7Sderaadt * if the input is... then the output is...
51df930be7Sderaadt *
52df930be7Sderaadt * /usr/src 0 /usr/src
53df930be7Sderaadt * /usr/src/cmd/aardvark.c 8 /cmd/aardvark.c
54df930be7Sderaadt * /usr/src/cmd/armadillo.c 14 armadillo.c
55df930be7Sderaadt * /usr/tmp/zoo 5 tmp/zoo
56df930be7Sderaadt *
57df930be7Sderaadt * The codes are:
58df930be7Sderaadt *
59df930be7Sderaadt * 0-28 likeliest differential counts + offset to make nonnegative
60df930be7Sderaadt * 30 switch code for out-of-range count to follow in next word
610f3d8120Smichaels * 31 an 8 bit char followed
62df930be7Sderaadt * 128-255 bigram codes (128 most common, as determined by 'updatedb')
63df930be7Sderaadt * 32-127 single character (printable) ascii residue (ie, literal)
64df930be7Sderaadt *
650f3d8120Smichaels * The locate database store any character except newline ('\n')
660f3d8120Smichaels * and NUL ('\0'). The 8-bit character support don't wast extra
670f3d8120Smichaels * space until you have characters in file names less than 32
688fa21293Sotto * or greater than 127.
690f3d8120Smichaels *
700f3d8120Smichaels *
710f3d8120Smichaels * SEE ALSO: updatedb.sh, ../bigram/locate.bigram.c
72df930be7Sderaadt *
73df930be7Sderaadt * AUTHOR: James A. Woods, Informatics General Corp.,
74df930be7Sderaadt * NASA Ames Research Center, 10/82
750f3d8120Smichaels * 8-bit file names characters:
760f3d8120Smichaels * Wolfram Schneider, Berlin September 1996
77df930be7Sderaadt */
78df930be7Sderaadt
79df930be7Sderaadt #include <err.h>
80df930be7Sderaadt #include <errno.h>
8131e6af90Sokan #include <stdio.h>
82df930be7Sderaadt #include <stdlib.h>
83df930be7Sderaadt #include <string.h>
8431e6af90Sokan #include <unistd.h>
85b9fc9a72Sderaadt #include <limits.h>
8631e6af90Sokan
87df930be7Sderaadt #include "locate.h"
88df930be7Sderaadt
89df930be7Sderaadt #define BGBUFSIZE (NBG * 2) /* size of bigram buffer */
90df930be7Sderaadt
91b9fc9a72Sderaadt u_char buf1[PATH_MAX] = " ";
92b9fc9a72Sderaadt u_char buf2[PATH_MAX];
930f3d8120Smichaels u_char bigrams[BGBUFSIZE + 1] = { 0 };
94df930be7Sderaadt
955dc1102eSmichaels #define LOOKUP 1 /* use a lookup array instead a function, 3x faster */
965dc1102eSmichaels
97c41e3a31Smichaels #ifdef LOOKUP
980f3d8120Smichaels #define BGINDEX(x) (big[(u_char)*x][(u_char)*(x + 1)])
990f3d8120Smichaels typedef short bg_t;
1000f3d8120Smichaels bg_t big[UCHAR_MAX + 1][UCHAR_MAX + 1];
101c41e3a31Smichaels #else
102c41e3a31Smichaels #define BGINDEX(x) bgindex(x)
103c41e3a31Smichaels typedef int bg_t;
104c72b5b24Smillert int bgindex(char *);
1055dc1102eSmichaels #endif /* LOOKUP */
1065dc1102eSmichaels
1075dc1102eSmichaels
108c72b5b24Smillert void usage(void);
109c41e3a31Smichaels extern int optind;
110c41e3a31Smichaels extern int optopt;
111df930be7Sderaadt
112df930be7Sderaadt int
main(int argc,char * argv[])113d33b10b9Sderaadt main(int argc, char *argv[])
114df930be7Sderaadt {
115c0932ef1Smpech u_char *cp, *oldpath, *path;
116df930be7Sderaadt int ch, code, count, diffcount, oldcount;
117df930be7Sderaadt FILE *fp;
118c0932ef1Smpech int i, j;
119df930be7Sderaadt
120a5a729f3Sderaadt if (pledge("stdio rpath", NULL) == -1)
121a5a729f3Sderaadt err(1, "pledge");
122a5a729f3Sderaadt
12372799b18Smillert while ((ch = getopt(argc, argv, "")) != -1)
124df930be7Sderaadt switch (ch) {
125df930be7Sderaadt default:
126df930be7Sderaadt usage();
127df930be7Sderaadt }
128df930be7Sderaadt argc -= optind;
129df930be7Sderaadt argv += optind;
130df930be7Sderaadt
131df930be7Sderaadt if (argc != 1)
132df930be7Sderaadt usage();
133df930be7Sderaadt
134df930be7Sderaadt if ((fp = fopen(argv[0], "r")) == NULL)
135df930be7Sderaadt err(1, "%s", argv[0]);
136df930be7Sderaadt
137a5a729f3Sderaadt if (pledge("stdio", NULL) == -1)
138a5a729f3Sderaadt err(1, "pledge");
139a5a729f3Sderaadt
140df930be7Sderaadt /* First copy bigram array to stdout. */
141dafef299Sespie if (fgets(bigrams, sizeof(bigrams), fp) == NULL) {
142dafef299Sespie if (ferror(fp)) {
143dafef299Sespie err(1, "fgets on %s", argv[0]);
144dafef299Sespie } else {
145dafef299Sespie errx(1, "premature end of bigram file %s", argv[0]);
146dafef299Sespie }
147dafef299Sespie }
1485dc1102eSmichaels
149b2817e1eSotto if (strlen(bigrams) != BGBUFSIZE)
150b2817e1eSotto errx(1, "bigram array too small to build db, index more files");
151b2817e1eSotto
152999d1a1cSray if (fputs(bigrams, stdout) == EOF)
153df930be7Sderaadt err(1, "stdout");
154df930be7Sderaadt (void)fclose(fp);
155df930be7Sderaadt
156c41e3a31Smichaels #ifdef LOOKUP
157c41e3a31Smichaels /* init lookup table */
1580f3d8120Smichaels for (i = 0; i < UCHAR_MAX + 1; i++)
1590f3d8120Smichaels for (j = 0; j < UCHAR_MAX + 1; j++)
160c41e3a31Smichaels big[i][j] = (bg_t)-1;
161c41e3a31Smichaels
1625dc1102eSmichaels for (cp = bigrams, i = 0; *cp != '\0'; i += 2, cp += 2)
1630f3d8120Smichaels big[(u_char)*cp][(u_char)*(cp + 1)] = (bg_t)i;
1640f3d8120Smichaels
1655dc1102eSmichaels #endif /* LOOKUP */
166c41e3a31Smichaels
167df930be7Sderaadt oldpath = buf1;
168df930be7Sderaadt path = buf2;
169df930be7Sderaadt oldcount = 0;
1705dc1102eSmichaels
171df930be7Sderaadt while (fgets(path, sizeof(buf2), stdin) != NULL) {
172c41e3a31Smichaels
173c41e3a31Smichaels /* skip empty lines */
174c41e3a31Smichaels if (*path == '\n')
175c41e3a31Smichaels continue;
176df930be7Sderaadt
1770f3d8120Smichaels /* remove newline */
1785dc1102eSmichaels for (cp = path; *cp != '\0'; cp++) {
179c41e3a31Smichaels /* chop newline */
180c41e3a31Smichaels if (*cp == '\n')
1815dc1102eSmichaels *cp = '\0';
182df930be7Sderaadt }
183df930be7Sderaadt
184df930be7Sderaadt /* Skip longest common prefix. */
1850f3d8120Smichaels for (cp = path; *cp == *oldpath; cp++, oldpath++)
1860f3d8120Smichaels if (*cp == '\0')
1870f3d8120Smichaels break;
188c41e3a31Smichaels
189df930be7Sderaadt count = cp - path;
190df930be7Sderaadt diffcount = count - oldcount + OFFSET;
191df930be7Sderaadt oldcount = count;
192df930be7Sderaadt if (diffcount < 0 || diffcount > 2 * OFFSET) {
193df930be7Sderaadt if (putchar(SWITCH) == EOF ||
194df930be7Sderaadt putw(diffcount, stdout) == EOF)
195df930be7Sderaadt err(1, "stdout");
196df930be7Sderaadt } else
197df930be7Sderaadt if (putchar(diffcount) == EOF)
198df930be7Sderaadt err(1, "stdout");
199df930be7Sderaadt
2005dc1102eSmichaels while (*cp != '\0') {
2010f3d8120Smichaels /* print *two* characters */
2020f3d8120Smichaels
2030f3d8120Smichaels if ((code = BGINDEX(cp)) != (bg_t)-1) {
2040f3d8120Smichaels /*
2050f3d8120Smichaels * print *one* as bigram
2060f3d8120Smichaels * Found, so mark byte with
2070f3d8120Smichaels * parity bit.
2080f3d8120Smichaels */
209df930be7Sderaadt if (putchar((code / 2) | PARITY) == EOF)
210df930be7Sderaadt err(1, "stdout");
211df930be7Sderaadt cp += 2;
21252123fd5Sderaadt } else {
2130f3d8120Smichaels for (i = 0; i < 2; i++) {
2140f3d8120Smichaels if (*cp == '\0')
2150f3d8120Smichaels break;
2160f3d8120Smichaels
2170f3d8120Smichaels /* print umlauts in file names */
2180f3d8120Smichaels if (*cp < ASCII_MIN ||
2190f3d8120Smichaels *cp > ASCII_MAX) {
2200f3d8120Smichaels if (putchar(UMLAUT) == EOF ||
2210f3d8120Smichaels putchar(*cp++) == EOF)
2220f3d8120Smichaels err(1, "stdout");
22352123fd5Sderaadt } else {
2240f3d8120Smichaels /* normal character */
2250f3d8120Smichaels if (putchar(*cp++) == EOF)
2260f3d8120Smichaels err(1, "stdout");
2270f3d8120Smichaels }
2280f3d8120Smichaels }
2290f3d8120Smichaels
2300f3d8120Smichaels }
2310f3d8120Smichaels }
2320f3d8120Smichaels
233df930be7Sderaadt if (path == buf1) { /* swap pointers */
234df930be7Sderaadt path = buf2;
235df930be7Sderaadt oldpath = buf1;
236df930be7Sderaadt } else {
237df930be7Sderaadt path = buf1;
238df930be7Sderaadt oldpath = buf2;
239df930be7Sderaadt }
240df930be7Sderaadt }
241df930be7Sderaadt /* Non-zero status if there were errors */
242df930be7Sderaadt if (fflush(stdout) != 0 || ferror(stdout))
243df930be7Sderaadt exit(1);
2445dc1102eSmichaels exit(0);
245df930be7Sderaadt }
246df930be7Sderaadt
247c41e3a31Smichaels #ifndef LOOKUP
248df930be7Sderaadt int
bgindex(char * bg)249d33b10b9Sderaadt bgindex(char *bg) /* Return location of bg in bigrams or -1. */
250df930be7Sderaadt {
251c0932ef1Smpech char bg0, bg1, *p;
252df930be7Sderaadt
253df930be7Sderaadt bg0 = bg[0];
254df930be7Sderaadt bg1 = bg[1];
2555dc1102eSmichaels for (p = bigrams; *p != NULL; p++)
256df930be7Sderaadt if (*p++ == bg0 && *p == bg1)
257df930be7Sderaadt break;
2580f3d8120Smichaels return (*p == NULL ? -1 : (--p - bigrams));
259df930be7Sderaadt }
260c41e3a31Smichaels #endif /* !LOOKUP */
261df930be7Sderaadt
262df930be7Sderaadt void
usage(void)263d33b10b9Sderaadt usage(void)
264df930be7Sderaadt {
265df930be7Sderaadt (void)fprintf(stderr,
266df930be7Sderaadt "usage: locate.code common_bigrams < list > squozen_list\n");
267df930be7Sderaadt exit(1);
268df930be7Sderaadt }
269