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