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