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