1 /* $OpenBSD: zopen.c,v 1.18 2011/09/22 10:41:04 deraadt 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/param.h> 62 #include <sys/stat.h> 63 64 #include <ctype.h> 65 #include <errno.h> 66 #include <signal.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <unistd.h> 71 #include <fcntl.h> 72 #include "compress.h" 73 74 #define BITS 16 /* Default bits. */ 75 #define HSIZE 69001 /* 95% occupancy */ 76 #define ZBUFSIZ 8192 /* I/O buffer size */ 77 78 /* A code_int must be able to hold 2**BITS values of type int, and also -1. */ 79 typedef long code_int; 80 typedef long count_int; 81 82 static const u_char z_magic[] = 83 {'\037', '\235'}; /* 1F 9D */ 84 85 #define BIT_MASK 0x1f /* Defines for third byte of header. */ 86 #define BLOCK_MASK 0x80 87 88 /* 89 * Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is 90 * a fourth header byte (for expansion). 91 */ 92 #define INIT_BITS 9 /* Initial number of bits/code. */ 93 94 #define MAXCODE(n_bits) ((1 << (n_bits)) - 1) 95 96 struct s_zstate { 97 int zs_fd; /* File stream for I/O */ 98 char zs_mode; /* r or w */ 99 enum { 100 S_START, S_MAGIC, S_MIDDLE, S_EOF 101 } zs_state; /* State of computation */ 102 int zs_n_bits; /* Number of bits/code. */ 103 int zs_maxbits; /* User settable max # bits/code. */ 104 code_int zs_maxcode; /* Maximum code, given n_bits. */ 105 code_int zs_maxmaxcode; /* Should NEVER generate this code. */ 106 count_int zs_htab[HSIZE]; 107 u_short zs_codetab[HSIZE]; 108 code_int zs_hsize; /* For dynamic table sizing. */ 109 code_int zs_free_ent; /* First unused entry. */ 110 /* 111 * Block compression parameters -- after all codes are used up, 112 * and compression rate changes, start over. 113 */ 114 int zs_block_compress; 115 int zs_clear_flg; 116 long zs_ratio; 117 count_int zs_checkpoint; 118 long zs_in_count; /* Length of input. */ 119 long zs_bytes_out; /* Length of output. */ 120 long zs_out_count; /* # of codes output (for debugging).*/ 121 u_char zs_buf[ZBUFSIZ]; /* I/O buffer */ 122 u_char *zs_bp; /* Current I/O window in the zs_buf */ 123 int zs_offset; /* Number of bits in the zs_buf */ 124 union { 125 struct { 126 long zs_fcode; 127 code_int zs_ent; 128 code_int zs_hsize_reg; 129 int zs_hshift; 130 } w; /* Write parameters */ 131 struct { 132 u_char *zs_stackp, *zs_ebp; 133 int zs_finchar; 134 code_int zs_code, zs_oldcode, zs_incode; 135 int zs_size; 136 } r; /* Read parameters */ 137 } u; 138 }; 139 140 /* Definitions to retain old variable names */ 141 #define zs_fcode u.w.zs_fcode 142 #define zs_ent u.w.zs_ent 143 #define zs_hsize_reg u.w.zs_hsize_reg 144 #define zs_hshift u.w.zs_hshift 145 #define zs_stackp u.r.zs_stackp 146 #define zs_finchar u.r.zs_finchar 147 #define zs_code u.r.zs_code 148 #define zs_oldcode u.r.zs_oldcode 149 #define zs_incode u.r.zs_incode 150 #define zs_size u.r.zs_size 151 #define zs_ebp u.r.zs_ebp 152 153 /* 154 * To save much memory, we overlay the table used by compress() with those 155 * used by decompress(). The tab_prefix table is the same size and type as 156 * the codetab. The tab_suffix table needs 2**BITS characters. We get this 157 * from the beginning of htab. The output stack uses the rest of htab, and 158 * contains characters. There is plenty of room for any possible stack 159 * (stack used to be 8000 characters). 160 */ 161 162 #define htabof(i) zs->zs_htab[i] 163 #define codetabof(i) zs->zs_codetab[i] 164 165 #define tab_prefixof(i) codetabof(i) 166 #define tab_suffixof(i) ((u_char *)(zs->zs_htab))[i] 167 #define de_stack ((u_char *)&tab_suffixof(1 << BITS)) 168 169 #define CHECK_GAP 10000 /* Ratio check interval. */ 170 171 /* 172 * the next two codes should not be changed lightly, as they must not 173 * lie within the contiguous general code space. 174 */ 175 #define FIRST 257 /* First free entry. */ 176 #define CLEAR 256 /* Table clear output code. */ 177 178 static int cl_block(struct s_zstate *); 179 static void cl_hash(struct s_zstate *, count_int); 180 static code_int getcode(struct s_zstate *); 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 #ifndef SAVECORE 338 setfile(name, zs->zs_fd, sb); 339 #endif 340 rval = close(zs->zs_fd); 341 free(zs); 342 return (rval); 343 } 344 345 static int 346 zclose(void *cookie) 347 { 348 return z_close(cookie, NULL, NULL, NULL); 349 } 350 351 /*- 352 * Output the given code. 353 * Inputs: 354 * code: A n_bits-bit integer. If == -1, then EOF. This assumes 355 * that n_bits =< (long)wordsize - 1. 356 * Outputs: 357 * Outputs code to the file. 358 * Assumptions: 359 * Chars are 8 bits long. 360 * Algorithm: 361 * Maintain a BITS character long buffer (so that 8 codes will 362 * fit in it exactly). Use the VAX insv instruction to insert each 363 * code in turn. When the buffer fills up empty it and start over. 364 */ 365 366 static const u_char lmask[9] = 367 {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00}; 368 static const u_char rmask[9] = 369 {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; 370 371 static int 372 output(struct s_zstate *zs, code_int ocode) 373 { 374 int bits; 375 376 if (ocode >= 0) { 377 int r_off; 378 u_char *bp; 379 380 /* Get to the first byte. */ 381 bp = zs->zs_bp + (zs->zs_offset >> 3); 382 r_off = zs->zs_offset & 7; 383 bits = zs->zs_n_bits; 384 385 /* 386 * Since ocode is always >= 8 bits, only need to mask the first 387 * hunk on the left. 388 */ 389 *bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]); 390 bp++; 391 bits -= (8 - r_off); 392 ocode >>= 8 - r_off; 393 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits) */ 394 if (bits >= 8) { 395 *bp++ = ocode; 396 ocode >>= 8; 397 bits -= 8; 398 } 399 /* Last bits. */ 400 if (bits) 401 *bp = ocode; 402 zs->zs_offset += zs->zs_n_bits; 403 if (zs->zs_offset == (zs->zs_n_bits << 3)) { 404 zs->zs_bp += zs->zs_n_bits; 405 zs->zs_offset = 0; 406 } 407 /* 408 * If the next entry is going to be too big for the ocode size, 409 * then increase it, if possible. 410 */ 411 if (zs->zs_free_ent > zs->zs_maxcode || 412 (zs->zs_clear_flg > 0)) { 413 /* 414 * Write the whole buffer, because the input side won't 415 * discover the size increase until after it has read it 416 */ 417 if (zs->zs_offset > 0) { 418 zs->zs_bp += zs->zs_n_bits; 419 zs->zs_offset = 0; 420 } 421 422 if (zs->zs_clear_flg) { 423 zs->zs_maxcode = 424 MAXCODE(zs->zs_n_bits = INIT_BITS); 425 zs->zs_clear_flg = 0; 426 } else { 427 zs->zs_n_bits++; 428 if (zs->zs_n_bits == zs->zs_maxbits) 429 zs->zs_maxcode = zs->zs_maxmaxcode; 430 else 431 zs->zs_maxcode = 432 MAXCODE(zs->zs_n_bits); 433 } 434 } 435 436 if (zs->zs_bp + zs->zs_n_bits > &zs->zs_buf[ZBUFSIZ]) { 437 bits = zs->zs_bp - zs->zs_buf; 438 if (write(zs->zs_fd, zs->zs_buf, bits) != bits) 439 return (-1); 440 zs->zs_bytes_out += bits; 441 if (zs->zs_offset > 0) 442 fprintf (stderr, "zs_offset != 0\n"); 443 zs->zs_bp = zs->zs_buf; 444 } 445 } else { 446 /* At EOF, write the rest of the buffer. */ 447 if (zs->zs_offset > 0) 448 zs->zs_bp += (zs->zs_offset + 7) / 8; 449 if (zs->zs_bp > zs->zs_buf) { 450 bits = zs->zs_bp - zs->zs_buf; 451 if (write(zs->zs_fd, zs->zs_buf, bits) != bits) 452 return (-1); 453 zs->zs_bytes_out += bits; 454 } 455 zs->zs_offset = 0; 456 zs->zs_bp = zs->zs_buf; 457 } 458 return (0); 459 } 460 461 /* 462 * Decompress read. This routine adapts to the codes in the file building 463 * the "string" table on-the-fly; requiring no table to be stored in the 464 * compressed file. The tables used herein are shared with those of the 465 * compress() routine. See the definitions above. 466 */ 467 int 468 zread(void *cookie, char *rbp, int num) 469 { 470 u_int count; 471 struct s_zstate *zs; 472 u_char *bp, header[3]; 473 474 if (num == 0) 475 return (0); 476 477 zs = cookie; 478 count = num; 479 bp = (u_char *)rbp; 480 switch (zs->zs_state) { 481 case S_START: 482 zs->zs_state = S_MIDDLE; 483 zs->zs_bp = zs->zs_buf; 484 header[0] = header[1] = header[2] = '\0'; 485 read(zs->zs_fd, header, sizeof(header)); 486 break; 487 case S_MAGIC: 488 zs->zs_state = S_MIDDLE; 489 zs->zs_bp = zs->zs_buf; 490 header[0] = z_magic[0]; 491 header[1] = z_magic[1]; 492 header[2] = '\0'; 493 read(zs->zs_fd, &header[2], 1); 494 break; 495 case S_MIDDLE: 496 goto middle; 497 case S_EOF: 498 goto eof; 499 } 500 501 /* Check the magic number */ 502 if (header[0] != z_magic[0] || header[1] != z_magic[1]) { 503 errno = EFTYPE; 504 return (-1); 505 } 506 zs->zs_maxbits = header[2]; /* Set -b from file. */ 507 zs->zs_in_count += sizeof(header); 508 zs->zs_block_compress = zs->zs_maxbits & BLOCK_MASK; 509 zs->zs_maxbits &= BIT_MASK; 510 zs->zs_maxmaxcode = 1L << zs->zs_maxbits; 511 if (zs->zs_maxbits > BITS) { 512 errno = EFTYPE; 513 return (-1); 514 } 515 /* As above, initialize the first 256 entries in the table. */ 516 zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS); 517 for (zs->zs_code = 255; zs->zs_code >= 0; zs->zs_code--) { 518 tab_prefixof(zs->zs_code) = 0; 519 tab_suffixof(zs->zs_code) = (u_char) zs->zs_code; 520 } 521 zs->zs_free_ent = zs->zs_block_compress ? FIRST : 256; 522 523 zs->zs_finchar = zs->zs_oldcode = getcode(zs); 524 if (zs->zs_oldcode == -1) /* EOF already? */ 525 return (0); /* Get out of here */ 526 527 /* First code must be 8 bits = char. */ 528 *bp++ = (u_char)zs->zs_finchar; 529 count--; 530 zs->zs_stackp = de_stack; 531 532 while ((zs->zs_code = getcode(zs)) > -1) { 533 534 if ((zs->zs_code == CLEAR) && zs->zs_block_compress) { 535 for (zs->zs_code = 255; zs->zs_code >= 0; 536 zs->zs_code--) 537 tab_prefixof(zs->zs_code) = 0; 538 zs->zs_clear_flg = 1; 539 zs->zs_free_ent = FIRST - 1; 540 if ((zs->zs_code = getcode(zs)) == -1) /* O, untimely death! */ 541 break; 542 } 543 zs->zs_incode = zs->zs_code; 544 545 /* Special case for KwKwK string. */ 546 if (zs->zs_code >= zs->zs_free_ent) { 547 *zs->zs_stackp++ = zs->zs_finchar; 548 zs->zs_code = zs->zs_oldcode; 549 } 550 551 /* Generate output characters in reverse order. */ 552 while (zs->zs_code >= 256) { 553 /* 554 * Bad input file may cause zs_stackp to overflow 555 * zs_htab; check here and abort decompression, 556 * that's better than dumping core. 557 */ 558 if (zs->zs_stackp >= (u_char *)&zs->zs_htab[HSIZE]) { 559 errno = EINVAL; 560 return (-1); 561 } 562 *zs->zs_stackp++ = tab_suffixof(zs->zs_code); 563 zs->zs_code = tab_prefixof(zs->zs_code); 564 } 565 *zs->zs_stackp++ = zs->zs_finchar = tab_suffixof(zs->zs_code); 566 567 /* And put them out in forward order. */ 568 middle: do { 569 if (count-- == 0) { 570 zs->zs_bytes_out += num; 571 return (num); 572 } 573 *bp++ = *--zs->zs_stackp; 574 } while (zs->zs_stackp > de_stack); 575 576 /* Generate the new entry. */ 577 if ((zs->zs_code = zs->zs_free_ent) < zs->zs_maxmaxcode) { 578 tab_prefixof(zs->zs_code) = (u_short) zs->zs_oldcode; 579 tab_suffixof(zs->zs_code) = zs->zs_finchar; 580 zs->zs_free_ent = zs->zs_code + 1; 581 } 582 583 /* Remember previous code. */ 584 zs->zs_oldcode = zs->zs_incode; 585 } 586 zs->zs_state = S_EOF; 587 zs->zs_bytes_out += num - count; 588 eof: return (num - count); 589 } 590 591 /*- 592 * Read one code from the standard input. If EOF, return -1. 593 * Inputs: 594 * stdin 595 * Outputs: 596 * code or -1 is returned. 597 */ 598 static code_int 599 getcode(struct s_zstate *zs) 600 { 601 code_int gcode; 602 int r_off, bits; 603 u_char *bp; 604 605 if (zs->zs_clear_flg > 0 || zs->zs_offset >= zs->zs_size || 606 zs->zs_free_ent > zs->zs_maxcode) { 607 608 zs->zs_bp += zs->zs_n_bits; 609 /* 610 * If the next entry will be too big for the current gcode 611 * size, then we must increase the size. This implies reading 612 * a new buffer full, too. 613 */ 614 if (zs->zs_free_ent > zs->zs_maxcode) { 615 zs->zs_n_bits++; 616 if (zs->zs_n_bits == zs->zs_maxbits) { 617 /* Won't get any bigger now. */ 618 zs->zs_maxcode = zs->zs_maxmaxcode; 619 } else 620 zs->zs_maxcode = MAXCODE(zs->zs_n_bits); 621 } 622 if (zs->zs_clear_flg > 0) { 623 zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS); 624 zs->zs_clear_flg = 0; 625 } 626 627 /* fill the buffer up to the neck */ 628 if (zs->zs_bp + zs->zs_n_bits > zs->zs_ebp) { 629 for (bp = zs->zs_buf; zs->zs_bp < zs->zs_ebp; 630 *bp++ = *zs->zs_bp++); 631 if ((bits = read(zs->zs_fd, bp, ZBUFSIZ - 632 (bp - zs->zs_buf))) < 0) 633 return -1; 634 zs->zs_in_count += bits; 635 zs->zs_bp = zs->zs_buf; 636 zs->zs_ebp = bp + bits; 637 } 638 zs->zs_offset = 0; 639 zs->zs_size = MIN(zs->zs_n_bits, zs->zs_ebp - zs->zs_bp); 640 if (zs->zs_size == 0) 641 return -1; 642 /* Round size down to integral number of codes. */ 643 zs->zs_size = (zs->zs_size << 3) - (zs->zs_n_bits - 1); 644 } 645 646 bp = zs->zs_bp; 647 r_off = zs->zs_offset; 648 bits = zs->zs_n_bits; 649 650 /* Get to the first byte. */ 651 bp += (r_off >> 3); 652 r_off &= 7; 653 654 /* Get first part (low order bits). */ 655 gcode = (*bp++ >> r_off); 656 bits -= (8 - r_off); 657 r_off = 8 - r_off; /* Now, roffset into gcode word. */ 658 659 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ 660 if (bits >= 8) { 661 gcode |= *bp++ << r_off; 662 r_off += 8; 663 bits -= 8; 664 } 665 666 /* High order bits. */ 667 gcode |= (*bp & rmask[bits]) << r_off; 668 zs->zs_offset += zs->zs_n_bits; 669 670 return (gcode); 671 } 672 673 /* Table clear for block compress. */ 674 static int 675 cl_block(struct s_zstate *zs) 676 { 677 long rat; 678 679 zs->zs_checkpoint = zs->zs_in_count + CHECK_GAP; 680 681 if (zs->zs_in_count > 0x007fffff) { /* Shift will overflow. */ 682 rat = zs->zs_bytes_out >> 8; 683 if (rat == 0) /* Don't divide by zero. */ 684 rat = 0x7fffffff; 685 else 686 rat = zs->zs_in_count / rat; 687 } else { 688 /* 8 fractional bits. */ 689 rat = (zs->zs_in_count << 8) / zs->zs_bytes_out; 690 } 691 if (rat > zs->zs_ratio) 692 zs->zs_ratio = rat; 693 else { 694 zs->zs_ratio = 0; 695 cl_hash(zs, (count_int) zs->zs_hsize); 696 zs->zs_free_ent = FIRST; 697 zs->zs_clear_flg = 1; 698 if (output(zs, (code_int) CLEAR) == -1) 699 return (-1); 700 } 701 return (0); 702 } 703 704 /* Reset code table. */ 705 static void 706 cl_hash(struct s_zstate *zs, count_int cl_hsize) 707 { 708 count_int *htab_p; 709 long i, m1; 710 711 m1 = -1; 712 htab_p = zs->zs_htab + cl_hsize; 713 i = cl_hsize - 16; 714 do { /* Might use Sys V memset(3) here. */ 715 *(htab_p - 16) = m1; 716 *(htab_p - 15) = m1; 717 *(htab_p - 14) = m1; 718 *(htab_p - 13) = m1; 719 *(htab_p - 12) = m1; 720 *(htab_p - 11) = m1; 721 *(htab_p - 10) = m1; 722 *(htab_p - 9) = m1; 723 *(htab_p - 8) = m1; 724 *(htab_p - 7) = m1; 725 *(htab_p - 6) = m1; 726 *(htab_p - 5) = m1; 727 *(htab_p - 4) = m1; 728 *(htab_p - 3) = m1; 729 *(htab_p - 2) = m1; 730 *(htab_p - 1) = m1; 731 htab_p -= 16; 732 } while ((i -= 16) >= 0); 733 for (i += 16; i > 0; i--) 734 *--htab_p = m1; 735 } 736 737 FILE * 738 zopen(const char *name, const char *mode, int bits) 739 { 740 int fd; 741 void *cookie; 742 if ((fd = open(name, (*mode=='r'? O_RDONLY:O_WRONLY|O_CREAT), 743 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) 744 return NULL; 745 if ((cookie = z_open(fd, mode, NULL, bits, 0, 0)) == NULL) { 746 close(fd); 747 return NULL; 748 } 749 return funopen(cookie, (*mode == 'r'?zread:NULL), 750 (*mode == 'w'?zwrite:NULL), NULL, zclose); 751 } 752 753 void * 754 z_open(int fd, const char *mode, char *name, int bits, 755 u_int32_t mtime, int gotmagic) 756 { 757 struct s_zstate *zs; 758 759 if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' || 760 bits < 0 || bits > BITS) { 761 errno = EINVAL; 762 return (NULL); 763 } 764 765 if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL) 766 return (NULL); 767 768 /* User settable max # bits/code. */ 769 zs->zs_maxbits = bits ? bits : BITS; 770 /* Should NEVER generate this code. */ 771 zs->zs_maxmaxcode = 1 << zs->zs_maxbits; 772 zs->zs_hsize = HSIZE; /* For dynamic table sizing. */ 773 zs->zs_free_ent = 0; /* First unused entry. */ 774 zs->zs_block_compress = BLOCK_MASK; 775 zs->zs_clear_flg = 0; 776 zs->zs_ratio = 0; 777 zs->zs_checkpoint = CHECK_GAP; 778 zs->zs_in_count = 0; /* Length of input. */ 779 zs->zs_out_count = 0; /* # of codes output (for debugging).*/ 780 zs->zs_state = gotmagic ? S_MAGIC : S_START; 781 zs->zs_offset = 0; 782 zs->zs_size = 0; 783 zs->zs_mode = mode[0]; 784 zs->zs_bp = zs->zs_ebp = zs->zs_buf; 785 786 zs->zs_fd = fd; 787 return zs; 788 } 789