1 /*- 2 * Copyright (c) 1985, 1986, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Diomidis Spinellis and James A. Woods, derived from original 7 * work by Spencer Thomas and Joseph Orost. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 40 __FBSDID("$FreeBSD$"); 41 42 #if defined(LIBC_SCCS) && !defined(lint) 43 static char sccsid[] = "@(#)zopen.c 8.1 (Berkeley) 6/27/93"; 44 #endif /* LIBC_SCCS and not lint */ 45 46 /*- 47 * fcompress.c - File compression ala IEEE Computer, June 1984. 48 * 49 * Compress authors: 50 * Spencer W. Thomas (decvax!utah-cs!thomas) 51 * Jim McKie (decvax!mcvax!jim) 52 * Steve Davies (decvax!vax135!petsd!peora!srd) 53 * Ken Turkowski (decvax!decwrl!turtlevax!ken) 54 * James A. Woods (decvax!ihnp4!ames!jaw) 55 * Joe Orost (decvax!vax135!petsd!joe) 56 * 57 * Cleaned up and converted to library returning I/O streams by 58 * Diomidis Spinellis <dds@doc.ic.ac.uk>. 59 * 60 * zopen(filename, mode, bits) 61 * Returns a FILE * that can be used for read or write. The modes 62 * supported are only "r" and "w". Seeking is not allowed. On 63 * reading the file is decompressed, on writing it is compressed. 64 * The output is compatible with compress(1) with 16 bit tables. 65 * Any file produced by compress(1) can be read. 66 */ 67 68 #include <sys/param.h> 69 #include <sys/stat.h> 70 71 #include <ctype.h> 72 #include <errno.h> 73 #include <signal.h> 74 #include <stdio.h> 75 #include <stdlib.h> 76 #include <string.h> 77 #include <unistd.h> 78 #include "zopen.h" 79 80 #define BITS 16 /* Default bits. */ 81 #define HSIZE 69001 /* 95% occupancy */ 82 83 /* A code_int must be able to hold 2**BITS values of type int, and also -1. */ 84 typedef long code_int; 85 typedef long count_int; 86 87 typedef u_char char_type; 88 static char_type magic_header[] = 89 {'\037', '\235'}; /* 1F 9D */ 90 91 #define BIT_MASK 0x1f /* Defines for third byte of header. */ 92 #define BLOCK_MASK 0x80 93 94 /* 95 * Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is 96 * a fourth header byte (for expansion). 97 */ 98 #define INIT_BITS 9 /* Initial number of bits/code. */ 99 100 #define MAXCODE(n_bits) ((1 << (n_bits)) - 1) 101 102 struct s_zstate { 103 FILE *zs_fp; /* File stream for I/O */ 104 char zs_mode; /* r or w */ 105 enum { 106 S_START, S_MIDDLE, S_EOF 107 } zs_state; /* State of computation */ 108 u_int zs_n_bits; /* Number of bits/code. */ 109 u_int zs_maxbits; /* User settable max # bits/code. */ 110 code_int zs_maxcode; /* Maximum code, given n_bits. */ 111 code_int zs_maxmaxcode; /* Should NEVER generate this code. */ 112 count_int zs_htab [HSIZE]; 113 u_short zs_codetab [HSIZE]; 114 code_int zs_hsize; /* For dynamic table sizing. */ 115 code_int zs_free_ent; /* First unused entry. */ 116 /* 117 * Block compression parameters -- after all codes are used up, 118 * and compression rate changes, start over. 119 */ 120 int zs_block_compress; 121 int zs_clear_flg; 122 long zs_ratio; 123 count_int zs_checkpoint; 124 u_int zs_offset; 125 long zs_in_count; /* Length of input. */ 126 long zs_bytes_out; /* Length of compressed output. */ 127 long zs_out_count; /* # of codes output (for debugging). */ 128 char_type zs_buf[BITS]; 129 union { 130 struct { 131 long zs_fcode; 132 code_int zs_ent; 133 code_int zs_hsize_reg; 134 int zs_hshift; 135 } w; /* Write paramenters */ 136 struct { 137 char_type *zs_stackp; 138 int zs_finchar; 139 code_int zs_code, zs_oldcode, zs_incode; 140 int zs_roffset, zs_size; 141 char_type zs_gbuf[BITS]; 142 } r; /* Read parameters */ 143 } u; 144 }; 145 146 /* Definitions to retain old variable names */ 147 #define fp zs->zs_fp 148 #define zmode zs->zs_mode 149 #define state zs->zs_state 150 #define n_bits zs->zs_n_bits 151 #define maxbits zs->zs_maxbits 152 #define maxcode zs->zs_maxcode 153 #define maxmaxcode zs->zs_maxmaxcode 154 #define htab zs->zs_htab 155 #define codetab zs->zs_codetab 156 #define hsize zs->zs_hsize 157 #define free_ent zs->zs_free_ent 158 #define block_compress zs->zs_block_compress 159 #define clear_flg zs->zs_clear_flg 160 #define ratio zs->zs_ratio 161 #define checkpoint zs->zs_checkpoint 162 #define offset zs->zs_offset 163 #define in_count zs->zs_in_count 164 #define bytes_out zs->zs_bytes_out 165 #define out_count zs->zs_out_count 166 #define buf zs->zs_buf 167 #define fcode zs->u.w.zs_fcode 168 #define hsize_reg zs->u.w.zs_hsize_reg 169 #define ent zs->u.w.zs_ent 170 #define hshift zs->u.w.zs_hshift 171 #define stackp zs->u.r.zs_stackp 172 #define finchar zs->u.r.zs_finchar 173 #define code zs->u.r.zs_code 174 #define oldcode zs->u.r.zs_oldcode 175 #define incode zs->u.r.zs_incode 176 #define roffset zs->u.r.zs_roffset 177 #define size zs->u.r.zs_size 178 #define gbuf zs->u.r.zs_gbuf 179 180 /* 181 * To save much memory, we overlay the table used by compress() with those 182 * used by decompress(). The tab_prefix table is the same size and type as 183 * the codetab. The tab_suffix table needs 2**BITS characters. We get this 184 * from the beginning of htab. The output stack uses the rest of htab, and 185 * contains characters. There is plenty of room for any possible stack 186 * (stack used to be 8000 characters). 187 */ 188 189 #define htabof(i) htab[i] 190 #define codetabof(i) codetab[i] 191 192 #define tab_prefixof(i) codetabof(i) 193 #define tab_suffixof(i) ((char_type *)(htab))[i] 194 #define de_stack ((char_type *)&tab_suffixof(1 << BITS)) 195 196 #define CHECK_GAP 10000 /* Ratio check interval. */ 197 198 /* 199 * the next two codes should not be changed lightly, as they must not 200 * lie within the contiguous general code space. 201 */ 202 #define FIRST 257 /* First free entry. */ 203 #define CLEAR 256 /* Table clear output code. */ 204 205 static int cl_block __P((struct s_zstate *)); 206 static void cl_hash __P((struct s_zstate *, count_int)); 207 static code_int getcode __P((struct s_zstate *)); 208 static int output __P((struct s_zstate *, code_int)); 209 static int zclose __P((void *)); 210 static int zread __P((void *, char *, int)); 211 static int zwrite __P((void *, const char *, int)); 212 213 /*- 214 * Algorithm from "A Technique for High Performance Data Compression", 215 * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19. 216 * 217 * Algorithm: 218 * Modified Lempel-Ziv method (LZW). Basically finds common 219 * substrings and replaces them with a variable size code. This is 220 * deterministic, and can be done on the fly. Thus, the decompression 221 * procedure needs no input table, but tracks the way the table was built. 222 */ 223 224 /*- 225 * compress write 226 * 227 * Algorithm: use open addressing double hashing (no chaining) on the 228 * prefix code / next character combination. We do a variant of Knuth's 229 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime 230 * secondary probe. Here, the modular division first probe is gives way 231 * to a faster exclusive-or manipulation. Also do block compression with 232 * an adaptive reset, whereby the code table is cleared when the compression 233 * ratio decreases, but after the table fills. The variable-length output 234 * codes are re-sized at this point, and a special CLEAR code is generated 235 * for the decompressor. Late addition: construct the table according to 236 * file size for noticeable speed improvement on small files. Please direct 237 * questions about this implementation to ames!jaw. 238 */ 239 static int 240 zwrite(cookie, wbp, num) 241 void *cookie; 242 const char *wbp; 243 int num; 244 { 245 code_int i; 246 int c, disp; 247 struct s_zstate *zs; 248 const u_char *bp; 249 u_char tmp; 250 int count; 251 252 if (num == 0) 253 return (0); 254 255 zs = cookie; 256 count = num; 257 bp = wbp; 258 if (state == S_MIDDLE) 259 goto middle; 260 state = S_MIDDLE; 261 262 maxmaxcode = 1L << maxbits; 263 if (fwrite(magic_header, 264 sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header)) 265 return (-1); 266 tmp = (u_char)((maxbits) | block_compress); 267 if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp)) 268 return (-1); 269 270 offset = 0; 271 bytes_out = 3; /* Includes 3-byte header mojo. */ 272 out_count = 0; 273 clear_flg = 0; 274 ratio = 0; 275 in_count = 1; 276 checkpoint = CHECK_GAP; 277 maxcode = MAXCODE(n_bits = INIT_BITS); 278 free_ent = ((block_compress) ? FIRST : 256); 279 280 ent = *bp++; 281 --count; 282 283 hshift = 0; 284 for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L) 285 hshift++; 286 hshift = 8 - hshift; /* Set hash code range bound. */ 287 288 hsize_reg = hsize; 289 cl_hash(zs, (count_int)hsize_reg); /* Clear hash table. */ 290 291 middle: for (i = 0; count--;) { 292 c = *bp++; 293 in_count++; 294 fcode = (long)(((long)c << maxbits) + ent); 295 i = ((c << hshift) ^ ent); /* Xor hashing. */ 296 297 if (htabof(i) == fcode) { 298 ent = codetabof(i); 299 continue; 300 } else if ((long)htabof(i) < 0) /* Empty slot. */ 301 goto nomatch; 302 disp = hsize_reg - i; /* Secondary hash (after G. Knott). */ 303 if (i == 0) 304 disp = 1; 305 probe: if ((i -= disp) < 0) 306 i += hsize_reg; 307 308 if (htabof(i) == fcode) { 309 ent = codetabof(i); 310 continue; 311 } 312 if ((long)htabof(i) >= 0) 313 goto probe; 314 nomatch: if (output(zs, (code_int) ent) == -1) 315 return (-1); 316 out_count++; 317 ent = c; 318 if (free_ent < maxmaxcode) { 319 codetabof(i) = free_ent++; /* code -> hashtable */ 320 htabof(i) = fcode; 321 } else if ((count_int)in_count >= 322 checkpoint && block_compress) { 323 if (cl_block(zs) == -1) 324 return (-1); 325 } 326 } 327 return (num); 328 } 329 330 static int 331 zclose(cookie) 332 void *cookie; 333 { 334 struct s_zstate *zs; 335 int rval; 336 337 zs = cookie; 338 if (zmode == 'w') { /* Put out the final code. */ 339 if (output(zs, (code_int) ent) == -1) { 340 (void)fclose(fp); 341 free(zs); 342 return (-1); 343 } 344 out_count++; 345 if (output(zs, (code_int) - 1) == -1) { 346 (void)fclose(fp); 347 free(zs); 348 return (-1); 349 } 350 } 351 rval = fclose(fp) == EOF ? -1 : 0; 352 free(zs); 353 return (rval); 354 } 355 356 /*- 357 * Output the given code. 358 * Inputs: 359 * code: A n_bits-bit integer. If == -1, then EOF. This assumes 360 * that n_bits =< (long)wordsize - 1. 361 * Outputs: 362 * Outputs code to the file. 363 * Assumptions: 364 * Chars are 8 bits long. 365 * Algorithm: 366 * Maintain a BITS character long buffer (so that 8 codes will 367 * fit in it exactly). Use the VAX insv instruction to insert each 368 * code in turn. When the buffer fills up empty it and start over. 369 */ 370 371 static char_type lmask[9] = 372 {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00}; 373 static char_type rmask[9] = 374 {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; 375 376 static int 377 output(zs, ocode) 378 struct s_zstate *zs; 379 code_int ocode; 380 { 381 int r_off; 382 u_int bits; 383 char_type *bp; 384 385 r_off = offset; 386 bits = n_bits; 387 bp = buf; 388 if (ocode >= 0) { 389 /* Get to the first byte. */ 390 bp += (r_off >> 3); 391 r_off &= 7; 392 /* 393 * Since ocode is always >= 8 bits, only need to mask the first 394 * hunk on the left. 395 */ 396 *bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]); 397 bp++; 398 bits -= (8 - r_off); 399 ocode >>= 8 - r_off; 400 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ 401 if (bits >= 8) { 402 *bp++ = ocode; 403 ocode >>= 8; 404 bits -= 8; 405 } 406 /* Last bits. */ 407 if (bits) 408 *bp = ocode; 409 offset += n_bits; 410 if (offset == (n_bits << 3)) { 411 bp = buf; 412 bits = n_bits; 413 bytes_out += bits; 414 if (fwrite(bp, sizeof(char), bits, fp) != bits) 415 return (-1); 416 bp += bits; 417 bits = 0; 418 offset = 0; 419 } 420 /* 421 * If the next entry is going to be too big for the ocode size, 422 * then increase it, if possible. 423 */ 424 if (free_ent > maxcode || (clear_flg > 0)) { 425 /* 426 * Write the whole buffer, because the input side won't 427 * discover the size increase until after it has read it. 428 */ 429 if (offset > 0) { 430 if (fwrite(buf, 1, n_bits, fp) != n_bits) 431 return (-1); 432 bytes_out += n_bits; 433 } 434 offset = 0; 435 436 if (clear_flg) { 437 maxcode = MAXCODE(n_bits = INIT_BITS); 438 clear_flg = 0; 439 } else { 440 n_bits++; 441 if (n_bits == maxbits) 442 maxcode = maxmaxcode; 443 else 444 maxcode = MAXCODE(n_bits); 445 } 446 } 447 } else { 448 /* At EOF, write the rest of the buffer. */ 449 if (offset > 0) { 450 offset = (offset + 7) / 8; 451 if (fwrite(buf, 1, offset, fp) != offset) 452 return (-1); 453 bytes_out += offset; 454 } 455 offset = 0; 456 } 457 return (0); 458 } 459 460 /* 461 * Decompress read. This routine adapts to the codes in the file building 462 * the "string" table on-the-fly; requiring no table to be stored in the 463 * compressed file. The tables used herein are shared with those of the 464 * compress() routine. See the definitions above. 465 */ 466 static int 467 zread(cookie, rbp, num) 468 void *cookie; 469 char *rbp; 470 int num; 471 { 472 u_int count; 473 struct s_zstate *zs; 474 u_char *bp, header[3]; 475 476 if (num == 0) 477 return (0); 478 479 zs = cookie; 480 count = num; 481 bp = (u_char *)rbp; 482 switch (state) { 483 case S_START: 484 state = S_MIDDLE; 485 break; 486 case S_MIDDLE: 487 goto middle; 488 case S_EOF: 489 goto eof; 490 } 491 492 /* Check the magic number */ 493 if (fread(header, 494 sizeof(char), sizeof(header), fp) != sizeof(header) || 495 memcmp(header, magic_header, sizeof(magic_header)) != 0) { 496 errno = EFTYPE; 497 return (-1); 498 } 499 maxbits = header[2]; /* Set -b from file. */ 500 block_compress = maxbits & BLOCK_MASK; 501 maxbits &= BIT_MASK; 502 maxmaxcode = 1L << maxbits; 503 if (maxbits > BITS) { 504 errno = EFTYPE; 505 return (-1); 506 } 507 /* As above, initialize the first 256 entries in the table. */ 508 maxcode = MAXCODE(n_bits = INIT_BITS); 509 for (code = 255; code >= 0; code--) { 510 tab_prefixof(code) = 0; 511 tab_suffixof(code) = (char_type) code; 512 } 513 free_ent = block_compress ? FIRST : 256; 514 515 finchar = oldcode = getcode(zs); 516 if (oldcode == -1) /* EOF already? */ 517 return (0); /* Get out of here */ 518 519 /* First code must be 8 bits = char. */ 520 *bp++ = (u_char)finchar; 521 count--; 522 stackp = de_stack; 523 524 while ((code = getcode(zs)) > -1) { 525 526 if ((code == CLEAR) && block_compress) { 527 for (code = 255; code >= 0; code--) 528 tab_prefixof(code) = 0; 529 clear_flg = 1; 530 free_ent = FIRST - 1; 531 if ((code = getcode(zs)) == -1) /* O, untimely death! */ 532 break; 533 } 534 incode = code; 535 536 /* Special case for KwKwK string. */ 537 if (code >= free_ent) { 538 *stackp++ = finchar; 539 code = oldcode; 540 } 541 542 /* Generate output characters in reverse order. */ 543 while (code >= 256) { 544 *stackp++ = tab_suffixof(code); 545 code = tab_prefixof(code); 546 } 547 *stackp++ = finchar = tab_suffixof(code); 548 549 /* And put them out in forward order. */ 550 middle: do { 551 if (count-- == 0) 552 return (num); 553 *bp++ = *--stackp; 554 } while (stackp > de_stack); 555 556 /* Generate the new entry. */ 557 if ((code = free_ent) < maxmaxcode) { 558 tab_prefixof(code) = (u_short) oldcode; 559 tab_suffixof(code) = finchar; 560 free_ent = code + 1; 561 } 562 563 /* Remember previous code. */ 564 oldcode = incode; 565 } 566 state = S_EOF; 567 eof: return (num - count); 568 } 569 570 /*- 571 * Read one code from the standard input. If EOF, return -1. 572 * Inputs: 573 * stdin 574 * Outputs: 575 * code or -1 is returned. 576 */ 577 static code_int 578 getcode(zs) 579 struct s_zstate *zs; 580 { 581 code_int gcode; 582 int r_off, bits; 583 char_type *bp; 584 585 bp = gbuf; 586 if (clear_flg > 0 || roffset >= size || free_ent > maxcode) { 587 /* 588 * If the next entry will be too big for the current gcode 589 * size, then we must increase the size. This implies reading 590 * a new buffer full, too. 591 */ 592 if (free_ent > maxcode) { 593 n_bits++; 594 if (n_bits == maxbits) /* Won't get any bigger now. */ 595 maxcode = maxmaxcode; 596 else 597 maxcode = MAXCODE(n_bits); 598 } 599 if (clear_flg > 0) { 600 maxcode = MAXCODE(n_bits = INIT_BITS); 601 clear_flg = 0; 602 } 603 size = fread(gbuf, 1, n_bits, fp); 604 if (size <= 0) /* End of file. */ 605 return (-1); 606 roffset = 0; 607 /* Round size down to integral number of codes. */ 608 size = (size << 3) - (n_bits - 1); 609 } 610 r_off = roffset; 611 bits = n_bits; 612 613 /* Get to the first byte. */ 614 bp += (r_off >> 3); 615 r_off &= 7; 616 617 /* Get first part (low order bits). */ 618 gcode = (*bp++ >> r_off); 619 bits -= (8 - r_off); 620 r_off = 8 - r_off; /* Now, roffset into gcode word. */ 621 622 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ 623 if (bits >= 8) { 624 gcode |= *bp++ << r_off; 625 r_off += 8; 626 bits -= 8; 627 } 628 629 /* High order bits. */ 630 gcode |= (*bp & rmask[bits]) << r_off; 631 roffset += n_bits; 632 633 return (gcode); 634 } 635 636 static int 637 cl_block(zs) /* Table clear for block compress. */ 638 struct s_zstate *zs; 639 { 640 long rat; 641 642 checkpoint = in_count + CHECK_GAP; 643 644 if (in_count > 0x007fffff) { /* Shift will overflow. */ 645 rat = bytes_out >> 8; 646 if (rat == 0) /* Don't divide by zero. */ 647 rat = 0x7fffffff; 648 else 649 rat = in_count / rat; 650 } else 651 rat = (in_count << 8) / bytes_out; /* 8 fractional bits. */ 652 if (rat > ratio) 653 ratio = rat; 654 else { 655 ratio = 0; 656 cl_hash(zs, (count_int) hsize); 657 free_ent = FIRST; 658 clear_flg = 1; 659 if (output(zs, (code_int) CLEAR) == -1) 660 return (-1); 661 } 662 return (0); 663 } 664 665 static void 666 cl_hash(zs, cl_hsize) /* Reset code table. */ 667 struct s_zstate *zs; 668 count_int cl_hsize; 669 { 670 count_int *htab_p; 671 long i, m1; 672 673 m1 = -1; 674 htab_p = htab + cl_hsize; 675 i = cl_hsize - 16; 676 do { /* Might use Sys V memset(3) here. */ 677 *(htab_p - 16) = m1; 678 *(htab_p - 15) = m1; 679 *(htab_p - 14) = m1; 680 *(htab_p - 13) = m1; 681 *(htab_p - 12) = m1; 682 *(htab_p - 11) = m1; 683 *(htab_p - 10) = m1; 684 *(htab_p - 9) = m1; 685 *(htab_p - 8) = m1; 686 *(htab_p - 7) = m1; 687 *(htab_p - 6) = m1; 688 *(htab_p - 5) = m1; 689 *(htab_p - 4) = m1; 690 *(htab_p - 3) = m1; 691 *(htab_p - 2) = m1; 692 *(htab_p - 1) = m1; 693 htab_p -= 16; 694 } while ((i -= 16) >= 0); 695 for (i += 16; i > 0; i--) 696 *--htab_p = m1; 697 } 698 699 FILE * 700 zopen(fname, mode, bits) 701 const char *fname, *mode; 702 int bits; 703 { 704 struct s_zstate *zs; 705 706 if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' || 707 bits < 0 || bits > BITS) { 708 errno = EINVAL; 709 return (NULL); 710 } 711 712 if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL) 713 return (NULL); 714 715 maxbits = bits ? bits : BITS; /* User settable max # bits/code. */ 716 maxmaxcode = 1L << maxbits; /* Should NEVER generate this code. */ 717 hsize = HSIZE; /* For dynamic table sizing. */ 718 free_ent = 0; /* First unused entry. */ 719 block_compress = BLOCK_MASK; 720 clear_flg = 0; 721 ratio = 0; 722 checkpoint = CHECK_GAP; 723 in_count = 1; /* Length of input. */ 724 out_count = 0; /* # of codes output (for debugging). */ 725 state = S_START; 726 roffset = 0; 727 size = 0; 728 729 /* 730 * Layering compress on top of stdio in order to provide buffering, 731 * and ensure that reads and write work with the data specified. 732 */ 733 if ((fp = fopen(fname, mode)) == NULL) { 734 free(zs); 735 return (NULL); 736 } 737 switch (*mode) { 738 case 'r': 739 zmode = 'r'; 740 return (funopen(zs, zread, NULL, NULL, zclose)); 741 case 'w': 742 zmode = 'w'; 743 return (funopen(zs, NULL, zwrite, NULL, zclose)); 744 } 745 /* NOTREACHED */ 746 return (NULL); 747 } 748