1 /* $OpenBSD: zopen.c,v 1.4 2017/01/22 01:55:08 krw Exp $ */ 2 /* $NetBSD: zopen.c,v 1.5 1995/03/26 09:44:53 glass Exp $ */ 3 4 /*- 5 * Copyright (c) 1985, 1986, 1992, 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 * Diomidis Spinellis and James A. Woods, derived from original 10 * work by Spencer Thomas and Joseph Orost. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * From: @(#)zopen.c 8.1 (Berkeley) 6/27/93 37 */ 38 39 /*- 40 * fcompress.c - File compression ala IEEE Computer, June 1984. 41 * 42 * Compress authors: 43 * Spencer W. Thomas (decvax!utah-cs!thomas) 44 * Jim McKie (decvax!mcvax!jim) 45 * Steve Davies (decvax!vax135!petsd!peora!srd) 46 * Ken Turkowski (decvax!decwrl!turtlevax!ken) 47 * James A. Woods (decvax!ihnp4!ames!jaw) 48 * Joe Orost (decvax!vax135!petsd!joe) 49 * 50 * Cleaned up and converted to library returning I/O streams by 51 * Diomidis Spinellis <dds@doc.ic.ac.uk>. 52 * 53 * zopen(filename, mode, bits) 54 * Returns a FILE * that can be used for read or write. The modes 55 * supported are only "r" and "w". Seeking is not allowed. On 56 * reading the file is decompressed, on writing it is compressed. 57 * The output is compatible with compress(1) with 16 bit tables. 58 * Any file produced by compress(1) can be read. 59 */ 60 61 #include <sys/stat.h> 62 63 #include <ctype.h> 64 #include <errno.h> 65 #include <signal.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <string.h> 69 #include <unistd.h> 70 #include <fcntl.h> 71 #include "compress.h" 72 73 #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) 74 75 #define BITS 16 /* Default bits. */ 76 #define HSIZE 69001 /* 95% occupancy */ 77 #define ZBUFSIZ 8192 /* I/O buffer size */ 78 79 /* A code_int must be able to hold 2**BITS values of type int, and also -1. */ 80 typedef long code_int; 81 typedef long count_int; 82 83 static const u_char z_magic[] = 84 {'\037', '\235'}; /* 1F 9D */ 85 86 #define BIT_MASK 0x1f /* Defines for third byte of header. */ 87 #define BLOCK_MASK 0x80 88 89 /* 90 * Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is 91 * a fourth header byte (for expansion). 92 */ 93 #define INIT_BITS 9 /* Initial number of bits/code. */ 94 95 #define MAXCODE(n_bits) ((1 << (n_bits)) - 1) 96 97 struct s_zstate { 98 int zs_fd; /* File stream for I/O */ 99 char zs_mode; /* r or w */ 100 enum { 101 S_START, S_MAGIC, S_MIDDLE, S_EOF 102 } zs_state; /* State of computation */ 103 int zs_n_bits; /* Number of bits/code. */ 104 int zs_maxbits; /* User settable max # bits/code. */ 105 code_int zs_maxcode; /* Maximum code, given n_bits. */ 106 code_int zs_maxmaxcode; /* Should NEVER generate this code. */ 107 count_int zs_htab[HSIZE]; 108 u_short zs_codetab[HSIZE]; 109 code_int zs_hsize; /* For dynamic table sizing. */ 110 code_int zs_free_ent; /* First unused entry. */ 111 /* 112 * Block compression parameters -- after all codes are used up, 113 * and compression rate changes, start over. 114 */ 115 int zs_block_compress; 116 int zs_clear_flg; 117 long zs_ratio; 118 count_int zs_checkpoint; 119 long zs_in_count; /* Length of input. */ 120 long zs_bytes_out; /* Length of output. */ 121 long zs_out_count; /* # of codes output (for debugging).*/ 122 u_char zs_buf[ZBUFSIZ]; /* I/O buffer */ 123 u_char *zs_bp; /* Current I/O window in the zs_buf */ 124 int zs_offset; /* Number of bits in the zs_buf */ 125 union { 126 struct { 127 long zs_fcode; 128 code_int zs_ent; 129 code_int zs_hsize_reg; 130 int zs_hshift; 131 } w; /* Write parameters */ 132 struct { 133 u_char *zs_stackp, *zs_ebp; 134 int zs_finchar; 135 code_int zs_code, zs_oldcode, zs_incode; 136 int zs_size; 137 } r; /* Read parameters */ 138 } u; 139 }; 140 141 /* Definitions to retain old variable names */ 142 #define zs_fcode u.w.zs_fcode 143 #define zs_ent u.w.zs_ent 144 #define zs_hsize_reg u.w.zs_hsize_reg 145 #define zs_hshift u.w.zs_hshift 146 #define zs_stackp u.r.zs_stackp 147 #define zs_finchar u.r.zs_finchar 148 #define zs_code u.r.zs_code 149 #define zs_oldcode u.r.zs_oldcode 150 #define zs_incode u.r.zs_incode 151 #define zs_size u.r.zs_size 152 #define zs_ebp u.r.zs_ebp 153 154 /* 155 * To save much memory, we overlay the table used by compress() with those 156 * used by decompress(). The tab_prefix table is the same size and type as 157 * the codetab. The tab_suffix table needs 2**BITS characters. We get this 158 * from the beginning of htab. The output stack uses the rest of htab, and 159 * contains characters. There is plenty of room for any possible stack 160 * (stack used to be 8000 characters). 161 */ 162 163 #define htabof(i) zs->zs_htab[i] 164 #define codetabof(i) zs->zs_codetab[i] 165 166 #define tab_prefixof(i) codetabof(i) 167 #define tab_suffixof(i) ((u_char *)(zs->zs_htab))[i] 168 #define de_stack ((u_char *)&tab_suffixof(1 << BITS)) 169 170 #define CHECK_GAP 10000 /* Ratio check interval. */ 171 172 /* 173 * the next two codes should not be changed lightly, as they must not 174 * lie within the contiguous general code space. 175 */ 176 #define FIRST 257 /* First free entry. */ 177 #define CLEAR 256 /* Table clear output code. */ 178 179 static int cl_block(struct s_zstate *); 180 static void cl_hash(struct s_zstate *, count_int); 181 static int output(struct s_zstate *, code_int); 182 183 /*- 184 * Algorithm from "A Technique for High Performance Data Compression", 185 * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19. 186 * 187 * Algorithm: 188 * Modified Lempel-Ziv method (LZW). Basically finds common 189 * substrings and replaces them with a variable size code. This is 190 * deterministic, and can be done on the fly. Thus, the decompression 191 * procedure needs no input table, but tracks the way the table was built. 192 */ 193 194 /*- 195 * compress write 196 * 197 * Algorithm: use open addressing double hashing (no chaining) on the 198 * prefix code / next character combination. We do a variant of Knuth's 199 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime 200 * secondary probe. Here, the modular division first probe is gives way 201 * to a faster exclusive-or manipulation. Also do block compression with 202 * an adaptive reset, whereby the code table is cleared when the compression 203 * ratio decreases, but after the table fills. The variable-length output 204 * codes are re-sized at this point, and a special CLEAR code is generated 205 * for the decompressor. Late addition: construct the table according to 206 * file size for noticeable speed improvement on small files. Please direct 207 * questions about this implementation to ames!jaw. 208 */ 209 int 210 zwrite(void *cookie, const char *wbp, int num) 211 { 212 code_int i; 213 int c, disp; 214 struct s_zstate *zs; 215 const u_char *bp; 216 u_char tmp; 217 int count; 218 219 zs = cookie; 220 count = num; 221 bp = (u_char *)wbp; 222 switch (zs->zs_state) { 223 case S_MAGIC: 224 return -1; 225 case S_EOF: 226 return 0; 227 case S_START: 228 zs->zs_state = S_MIDDLE; 229 230 zs->zs_maxmaxcode = 1L << zs->zs_maxbits; 231 if (write(zs->zs_fd, z_magic, sizeof(z_magic)) != 232 sizeof(z_magic)) 233 return (-1); 234 tmp = (u_char)(zs->zs_maxbits | zs->zs_block_compress); 235 if (write(zs->zs_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) 236 return (-1); 237 238 zs->zs_bp = zs->zs_buf; 239 zs->zs_offset = 0; 240 zs->zs_bytes_out = 3; /* Includes 3-byte header mojo. */ 241 zs->zs_out_count = 0; 242 zs->zs_clear_flg = 0; 243 zs->zs_ratio = 0; 244 zs->zs_in_count = 1; 245 zs->zs_checkpoint = CHECK_GAP; 246 zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS); 247 zs->zs_free_ent = ((zs->zs_block_compress) ? FIRST : 256); 248 249 zs->zs_ent = *bp++; 250 --count; 251 252 zs->zs_hshift = 0; 253 for (zs->zs_fcode = (long)zs->zs_hsize; zs->zs_fcode < 65536L; 254 zs->zs_fcode *= 2L) 255 zs->zs_hshift++; 256 /* Set hash code range bound. */ 257 zs->zs_hshift = 8 - zs->zs_hshift; 258 259 zs->zs_hsize_reg = zs->zs_hsize; 260 /* Clear hash table. */ 261 cl_hash(zs, (count_int)zs->zs_hsize_reg); 262 263 case S_MIDDLE: 264 for (i = 0; count-- > 0;) { 265 c = *bp++; 266 zs->zs_in_count++; 267 zs->zs_fcode = (long)(((long)c << zs->zs_maxbits) + 268 zs->zs_ent); 269 /* Xor hashing. */ 270 i = ((c << zs->zs_hshift) ^ zs->zs_ent); 271 272 if (htabof(i) == zs->zs_fcode) { 273 zs->zs_ent = codetabof(i); 274 continue; 275 } else if ((long)htabof(i) < 0) /* Empty slot. */ 276 goto nomatch; 277 /* Secondary hash (after G. Knott). */ 278 disp = zs->zs_hsize_reg - i; 279 if (i == 0) 280 disp = 1; 281 probe: if ((i -= disp) < 0) 282 i += zs->zs_hsize_reg; 283 284 if (htabof(i) == zs->zs_fcode) { 285 zs->zs_ent = codetabof(i); 286 continue; 287 } 288 if ((long)htabof(i) >= 0) 289 goto probe; 290 nomatch: if (output(zs, (code_int) zs->zs_ent) == -1) 291 return (-1); 292 zs->zs_out_count++; 293 zs->zs_ent = c; 294 if (zs->zs_free_ent < zs->zs_maxmaxcode) { 295 /* code -> hashtable */ 296 codetabof(i) = zs->zs_free_ent++; 297 htabof(i) = zs->zs_fcode; 298 } else if ((count_int)zs->zs_in_count >= 299 zs->zs_checkpoint && zs->zs_block_compress) { 300 if (cl_block(zs) == -1) 301 return (-1); 302 } 303 } 304 } 305 return (num); 306 } 307 308 int 309 z_close(void *cookie, struct z_info *info, const char *name, struct stat *sb) 310 { 311 struct s_zstate *zs; 312 int rval; 313 314 zs = cookie; 315 if (zs->zs_mode == 'w') { /* Put out the final code. */ 316 if (output(zs, (code_int) zs->zs_ent) == -1) { 317 (void)close(zs->zs_fd); 318 free(zs); 319 return (-1); 320 } 321 zs->zs_out_count++; 322 if (output(zs, (code_int) - 1) == -1) { 323 (void)close(zs->zs_fd); 324 free(zs); 325 return (-1); 326 } 327 } 328 329 if (info != NULL) { 330 info->mtime = 0; 331 info->crc = (u_int32_t)-1; 332 info->hlen = 0; 333 info->total_in = (off_t)zs->zs_in_count; 334 info->total_out = (off_t)zs->zs_bytes_out; 335 } 336 337 rval = close(zs->zs_fd); 338 free(zs); 339 return (rval); 340 } 341 342 static int 343 zclose(void *cookie) 344 { 345 return z_close(cookie, NULL, NULL, NULL); 346 } 347 348 /*- 349 * Output the given code. 350 * Inputs: 351 * code: A n_bits-bit integer. If == -1, then EOF. This assumes 352 * that n_bits =< (long)wordsize - 1. 353 * Outputs: 354 * Outputs code to the file. 355 * Assumptions: 356 * Chars are 8 bits long. 357 * Algorithm: 358 * Maintain a BITS character long buffer (so that 8 codes will 359 * fit in it exactly). Use the VAX insv instruction to insert each 360 * code in turn. When the buffer fills up empty it and start over. 361 */ 362 363 static const u_char lmask[9] = 364 {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00}; 365 static const u_char rmask[9] = 366 {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; 367 368 static int 369 output(struct s_zstate *zs, code_int ocode) 370 { 371 int bits; 372 373 if (ocode >= 0) { 374 int r_off; 375 u_char *bp; 376 377 /* Get to the first byte. */ 378 bp = zs->zs_bp + (zs->zs_offset >> 3); 379 r_off = zs->zs_offset & 7; 380 bits = zs->zs_n_bits; 381 382 /* 383 * Since ocode is always >= 8 bits, only need to mask the first 384 * hunk on the left. 385 */ 386 *bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]); 387 bp++; 388 bits -= (8 - r_off); 389 ocode >>= 8 - r_off; 390 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits) */ 391 if (bits >= 8) { 392 *bp++ = ocode; 393 ocode >>= 8; 394 bits -= 8; 395 } 396 /* Last bits. */ 397 if (bits) 398 *bp = ocode; 399 zs->zs_offset += zs->zs_n_bits; 400 if (zs->zs_offset == (zs->zs_n_bits << 3)) { 401 zs->zs_bp += zs->zs_n_bits; 402 zs->zs_offset = 0; 403 } 404 /* 405 * If the next entry is going to be too big for the ocode size, 406 * then increase it, if possible. 407 */ 408 if (zs->zs_free_ent > zs->zs_maxcode || 409 (zs->zs_clear_flg > 0)) { 410 /* 411 * Write the whole buffer, because the input side won't 412 * discover the size increase until after it has read it 413 */ 414 if (zs->zs_offset > 0) { 415 zs->zs_bp += zs->zs_n_bits; 416 zs->zs_offset = 0; 417 } 418 419 if (zs->zs_clear_flg) { 420 zs->zs_maxcode = 421 MAXCODE(zs->zs_n_bits = INIT_BITS); 422 zs->zs_clear_flg = 0; 423 } else { 424 zs->zs_n_bits++; 425 if (zs->zs_n_bits == zs->zs_maxbits) 426 zs->zs_maxcode = zs->zs_maxmaxcode; 427 else 428 zs->zs_maxcode = 429 MAXCODE(zs->zs_n_bits); 430 } 431 } 432 433 if (zs->zs_bp + zs->zs_n_bits > &zs->zs_buf[ZBUFSIZ]) { 434 bits = zs->zs_bp - zs->zs_buf; 435 if (write(zs->zs_fd, zs->zs_buf, bits) != bits) 436 return (-1); 437 zs->zs_bytes_out += bits; 438 if (zs->zs_offset > 0) 439 fprintf (stderr, "zs_offset != 0\n"); 440 zs->zs_bp = zs->zs_buf; 441 } 442 } else { 443 /* At EOF, write the rest of the buffer. */ 444 if (zs->zs_offset > 0) 445 zs->zs_bp += (zs->zs_offset + 7) / 8; 446 if (zs->zs_bp > zs->zs_buf) { 447 bits = zs->zs_bp - zs->zs_buf; 448 if (write(zs->zs_fd, zs->zs_buf, bits) != bits) 449 return (-1); 450 zs->zs_bytes_out += bits; 451 } 452 zs->zs_offset = 0; 453 zs->zs_bp = zs->zs_buf; 454 } 455 return (0); 456 } 457 458 /* Table clear for block compress. */ 459 static int 460 cl_block(struct s_zstate *zs) 461 { 462 long rat; 463 464 zs->zs_checkpoint = zs->zs_in_count + CHECK_GAP; 465 466 if (zs->zs_in_count > 0x007fffff) { /* Shift will overflow. */ 467 rat = zs->zs_bytes_out >> 8; 468 if (rat == 0) /* Don't divide by zero. */ 469 rat = 0x7fffffff; 470 else 471 rat = zs->zs_in_count / rat; 472 } else { 473 /* 8 fractional bits. */ 474 rat = (zs->zs_in_count << 8) / zs->zs_bytes_out; 475 } 476 if (rat > zs->zs_ratio) 477 zs->zs_ratio = rat; 478 else { 479 zs->zs_ratio = 0; 480 cl_hash(zs, (count_int) zs->zs_hsize); 481 zs->zs_free_ent = FIRST; 482 zs->zs_clear_flg = 1; 483 if (output(zs, (code_int) CLEAR) == -1) 484 return (-1); 485 } 486 return (0); 487 } 488 489 /* Reset code table. */ 490 static void 491 cl_hash(struct s_zstate *zs, count_int cl_hsize) 492 { 493 count_int *htab_p; 494 long i, m1; 495 496 m1 = -1; 497 htab_p = zs->zs_htab + cl_hsize; 498 i = cl_hsize - 16; 499 do { /* Might use Sys V memset(3) here. */ 500 *(htab_p - 16) = m1; 501 *(htab_p - 15) = m1; 502 *(htab_p - 14) = m1; 503 *(htab_p - 13) = m1; 504 *(htab_p - 12) = m1; 505 *(htab_p - 11) = m1; 506 *(htab_p - 10) = m1; 507 *(htab_p - 9) = m1; 508 *(htab_p - 8) = m1; 509 *(htab_p - 7) = m1; 510 *(htab_p - 6) = m1; 511 *(htab_p - 5) = m1; 512 *(htab_p - 4) = m1; 513 *(htab_p - 3) = m1; 514 *(htab_p - 2) = m1; 515 *(htab_p - 1) = m1; 516 htab_p -= 16; 517 } while ((i -= 16) >= 0); 518 for (i += 16; i > 0; i--) 519 *--htab_p = m1; 520 } 521 522 FILE * 523 zopen(const char *name, const char *mode, int bits) 524 { 525 FILE *fp; 526 int fd; 527 void *cookie; 528 if ((fd = open(name, (*mode=='r'? O_RDONLY:O_WRONLY|O_CREAT), 529 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) 530 return NULL; 531 if ((cookie = z_open(fd, mode, NULL, bits, 0, 0)) == NULL) { 532 close(fd); 533 return NULL; 534 } 535 if ((fp = funopen(cookie, NULL, 536 (*mode == 'w'?zwrite:NULL), NULL, zclose)) == NULL) { 537 close(fd); 538 free(cookie); 539 return NULL; 540 } 541 return fp; 542 } 543 544 void * 545 z_open(int fd, const char *mode, char *name, int bits, 546 u_int32_t mtime, int gotmagic) 547 { 548 struct s_zstate *zs; 549 550 if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' || 551 bits < 0 || bits > BITS) { 552 errno = EINVAL; 553 return (NULL); 554 } 555 556 if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL) 557 return (NULL); 558 559 /* User settable max # bits/code. */ 560 zs->zs_maxbits = bits ? bits : BITS; 561 /* Should NEVER generate this code. */ 562 zs->zs_maxmaxcode = 1 << zs->zs_maxbits; 563 zs->zs_hsize = HSIZE; /* For dynamic table sizing. */ 564 zs->zs_free_ent = 0; /* First unused entry. */ 565 zs->zs_block_compress = BLOCK_MASK; 566 zs->zs_clear_flg = 0; 567 zs->zs_ratio = 0; 568 zs->zs_checkpoint = CHECK_GAP; 569 zs->zs_in_count = 0; /* Length of input. */ 570 zs->zs_out_count = 0; /* # of codes output (for debugging).*/ 571 zs->zs_state = gotmagic ? S_MAGIC : S_START; 572 zs->zs_offset = 0; 573 zs->zs_size = 0; 574 zs->zs_mode = mode[0]; 575 zs->zs_bp = zs->zs_ebp = zs->zs_buf; 576 577 zs->zs_fd = fd; 578 return zs; 579 } 580