1225845Sdelphij/* $NetBSD: zuncompress.c,v 1.11 2011/08/16 13:55:02 joerg Exp $ */ 2166255Sdelphij 3166255Sdelphij/*- 4166255Sdelphij * Copyright (c) 1985, 1986, 1992, 1993 5166255Sdelphij * The Regents of the University of California. All rights reserved. 6166255Sdelphij * 7166255Sdelphij * This code is derived from software contributed to Berkeley by 8166255Sdelphij * Diomidis Spinellis and James A. Woods, derived from original 9166255Sdelphij * work by Spencer Thomas and Joseph Orost. 10166255Sdelphij * 11166255Sdelphij * Redistribution and use in source and binary forms, with or without 12166255Sdelphij * modification, are permitted provided that the following conditions 13166255Sdelphij * are met: 14166255Sdelphij * 1. Redistributions of source code must retain the above copyright 15166255Sdelphij * notice, this list of conditions and the following disclaimer. 16166255Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 17166255Sdelphij * notice, this list of conditions and the following disclaimer in the 18166255Sdelphij * documentation and/or other materials provided with the distribution. 19166255Sdelphij * 3. Neither the name of the University nor the names of its contributors 20166255Sdelphij * may be used to endorse or promote products derived from this software 21166255Sdelphij * without specific prior written permission. 22166255Sdelphij * 23166255Sdelphij * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24166255Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25166255Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26166255Sdelphij * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27166255Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28166255Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29166255Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30166255Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31166255Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32166255Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33166255Sdelphij * SUCH DAMAGE. 34166255Sdelphij * 35166255Sdelphij * from: NetBSD: zopen.c,v 1.8 2003/08/07 11:13:29 agc Exp 36166255Sdelphij * $FreeBSD$ 37166255Sdelphij */ 38166255Sdelphij 39166255Sdelphij/* This file is #included by gzip.c */ 40166255Sdelphij 41166255Sdelphijstatic int zread(void *, char *, int); 42166255Sdelphij 43166255Sdelphij#define tab_prefixof(i) (zs->zs_codetab[i]) 44166255Sdelphij#define tab_suffixof(i) ((char_type *)(zs->zs_htab))[i] 45166255Sdelphij#define de_stack ((char_type *)&tab_suffixof(1 << BITS)) 46166255Sdelphij 47166255Sdelphij#define BITS 16 /* Default bits. */ 48166255Sdelphij#define HSIZE 69001 /* 95% occupancy */ /* XXX may not need HSIZE */ 49166255Sdelphij#define BIT_MASK 0x1f /* Defines for third byte of header. */ 50166255Sdelphij#define BLOCK_MASK 0x80 51166255Sdelphij#define CHECK_GAP 10000 /* Ratio check interval. */ 52166255Sdelphij#define BUFSIZE (64 * 1024) 53166255Sdelphij 54166255Sdelphij/* 55166255Sdelphij * Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is 56166255Sdelphij * a fourth header byte (for expansion). 57166255Sdelphij */ 58166255Sdelphij#define INIT_BITS 9 /* Initial number of bits/code. */ 59166255Sdelphij 60166255Sdelphij/* 61166255Sdelphij * the next two codes should not be changed lightly, as they must not 62166255Sdelphij * lie within the contiguous general code space. 63166255Sdelphij */ 64166255Sdelphij#define FIRST 257 /* First free entry. */ 65166255Sdelphij#define CLEAR 256 /* Table clear output code. */ 66166255Sdelphij 67166255Sdelphij 68166255Sdelphij#define MAXCODE(n_bits) ((1 << (n_bits)) - 1) 69166255Sdelphij 70166255Sdelphijtypedef long code_int; 71166255Sdelphijtypedef long count_int; 72166255Sdelphijtypedef u_char char_type; 73166255Sdelphij 74166255Sdelphijstatic char_type magic_header[] = 75166255Sdelphij {'\037', '\235'}; /* 1F 9D */ 76166255Sdelphij 77166255Sdelphijstatic char_type rmask[9] = 78166255Sdelphij {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; 79166255Sdelphij 80241737Sedstatic off_t total_compressed_bytes; 81241737Sedstatic size_t compressed_prelen; 82241737Sedstatic char *compressed_pre; 83166255Sdelphij 84166255Sdelphijstruct s_zstate { 85166255Sdelphij FILE *zs_fp; /* File stream for I/O */ 86166255Sdelphij char zs_mode; /* r or w */ 87166255Sdelphij enum { 88166255Sdelphij S_START, S_MIDDLE, S_EOF 89166255Sdelphij } zs_state; /* State of computation */ 90166255Sdelphij int zs_n_bits; /* Number of bits/code. */ 91166255Sdelphij int zs_maxbits; /* User settable max # bits/code. */ 92166255Sdelphij code_int zs_maxcode; /* Maximum code, given n_bits. */ 93166255Sdelphij code_int zs_maxmaxcode; /* Should NEVER generate this code. */ 94166255Sdelphij count_int zs_htab [HSIZE]; 95166255Sdelphij u_short zs_codetab [HSIZE]; 96166255Sdelphij code_int zs_hsize; /* For dynamic table sizing. */ 97166255Sdelphij code_int zs_free_ent; /* First unused entry. */ 98166255Sdelphij /* 99166255Sdelphij * Block compression parameters -- after all codes are used up, 100166255Sdelphij * and compression rate changes, start over. 101166255Sdelphij */ 102166255Sdelphij int zs_block_compress; 103166255Sdelphij int zs_clear_flg; 104166255Sdelphij long zs_ratio; 105166255Sdelphij count_int zs_checkpoint; 106166255Sdelphij int zs_offset; 107166255Sdelphij long zs_in_count; /* Length of input. */ 108166255Sdelphij long zs_bytes_out; /* Length of compressed output. */ 109166255Sdelphij long zs_out_count; /* # of codes output (for debugging). */ 110166255Sdelphij char_type zs_buf[BITS]; 111166255Sdelphij union { 112166255Sdelphij struct { 113166255Sdelphij long zs_fcode; 114166255Sdelphij code_int zs_ent; 115166255Sdelphij code_int zs_hsize_reg; 116166255Sdelphij int zs_hshift; 117213927Sbcr } w; /* Write parameters */ 118166255Sdelphij struct { 119166255Sdelphij char_type *zs_stackp; 120166255Sdelphij int zs_finchar; 121166255Sdelphij code_int zs_code, zs_oldcode, zs_incode; 122166255Sdelphij int zs_roffset, zs_size; 123166255Sdelphij char_type zs_gbuf[BITS]; 124166255Sdelphij } r; /* Read parameters */ 125166255Sdelphij } u; 126166255Sdelphij}; 127166255Sdelphij 128166255Sdelphijstatic code_int getcode(struct s_zstate *zs); 129166255Sdelphij 130166255Sdelphijstatic off_t 131166255Sdelphijzuncompress(FILE *in, FILE *out, char *pre, size_t prelen, 132166255Sdelphij off_t *compressed_bytes) 133166255Sdelphij{ 134166255Sdelphij off_t bin, bout = 0; 135166255Sdelphij char *buf; 136166255Sdelphij 137166255Sdelphij buf = malloc(BUFSIZE); 138166255Sdelphij if (buf == NULL) 139166255Sdelphij return -1; 140166255Sdelphij 141166255Sdelphij /* XXX */ 142166255Sdelphij compressed_prelen = prelen; 143166255Sdelphij if (prelen != 0) 144166255Sdelphij compressed_pre = pre; 145166255Sdelphij else 146166255Sdelphij compressed_pre = NULL; 147166255Sdelphij 148268516Sdelphij while ((bin = fread(buf, 1, BUFSIZE, in)) != 0) { 149194916Sdelphij if (tflag == 0 && (off_t)fwrite(buf, 1, bin, out) != bin) { 150166255Sdelphij free(buf); 151166255Sdelphij return -1; 152166255Sdelphij } 153166255Sdelphij bout += bin; 154166255Sdelphij } 155166255Sdelphij 156166255Sdelphij if (compressed_bytes) 157166255Sdelphij *compressed_bytes = total_compressed_bytes; 158166255Sdelphij 159166255Sdelphij free(buf); 160166255Sdelphij return bout; 161166255Sdelphij} 162166255Sdelphij 163166255Sdelphijstatic int 164166255Sdelphijzclose(void *zs) 165166255Sdelphij{ 166166255Sdelphij free(zs); 167166255Sdelphij /* We leave the caller to close the fd passed to zdopen() */ 168166255Sdelphij return 0; 169166255Sdelphij} 170166255Sdelphij 171166255SdelphijFILE * 172166255Sdelphijzdopen(int fd) 173166255Sdelphij{ 174166255Sdelphij struct s_zstate *zs; 175166255Sdelphij 176166255Sdelphij if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL) 177166255Sdelphij return (NULL); 178166255Sdelphij 179166255Sdelphij zs->zs_state = S_START; 180166255Sdelphij 181166255Sdelphij /* XXX we can get rid of some of these */ 182166255Sdelphij zs->zs_hsize = HSIZE; /* For dynamic table sizing. */ 183166255Sdelphij zs->zs_free_ent = 0; /* First unused entry. */ 184166255Sdelphij zs->zs_block_compress = BLOCK_MASK; 185166255Sdelphij zs->zs_clear_flg = 0; /* XXX we calloc()'d this structure why = 0? */ 186166255Sdelphij zs->zs_ratio = 0; 187166255Sdelphij zs->zs_checkpoint = CHECK_GAP; 188166255Sdelphij zs->zs_in_count = 1; /* Length of input. */ 189166255Sdelphij zs->zs_out_count = 0; /* # of codes output (for debugging). */ 190166255Sdelphij zs->u.r.zs_roffset = 0; 191166255Sdelphij zs->u.r.zs_size = 0; 192166255Sdelphij 193166255Sdelphij /* 194166255Sdelphij * Layering compress on top of stdio in order to provide buffering, 195166255Sdelphij * and ensure that reads and write work with the data specified. 196166255Sdelphij */ 197166255Sdelphij if ((zs->zs_fp = fdopen(fd, "r")) == NULL) { 198166255Sdelphij free(zs); 199166255Sdelphij return NULL; 200166255Sdelphij } 201166255Sdelphij 202166255Sdelphij return funopen(zs, zread, NULL, NULL, zclose); 203166255Sdelphij} 204166255Sdelphij 205166255Sdelphij/* 206166255Sdelphij * Decompress read. This routine adapts to the codes in the file building 207166255Sdelphij * the "string" table on-the-fly; requiring no table to be stored in the 208166255Sdelphij * compressed file. The tables used herein are shared with those of the 209166255Sdelphij * compress() routine. See the definitions above. 210166255Sdelphij */ 211166255Sdelphijstatic int 212166255Sdelphijzread(void *cookie, char *rbp, int num) 213166255Sdelphij{ 214166255Sdelphij u_int count, i; 215166255Sdelphij struct s_zstate *zs; 216166255Sdelphij u_char *bp, header[3]; 217166255Sdelphij 218166255Sdelphij if (num == 0) 219166255Sdelphij return (0); 220166255Sdelphij 221166255Sdelphij zs = cookie; 222166255Sdelphij count = num; 223166255Sdelphij bp = (u_char *)rbp; 224166255Sdelphij switch (zs->zs_state) { 225166255Sdelphij case S_START: 226166255Sdelphij zs->zs_state = S_MIDDLE; 227166255Sdelphij break; 228166255Sdelphij case S_MIDDLE: 229166255Sdelphij goto middle; 230166255Sdelphij case S_EOF: 231166255Sdelphij goto eof; 232166255Sdelphij } 233166255Sdelphij 234166255Sdelphij /* Check the magic number */ 235166255Sdelphij for (i = 0; i < 3 && compressed_prelen; i++, compressed_prelen--) 236166255Sdelphij header[i] = *compressed_pre++; 237166255Sdelphij 238166255Sdelphij if (fread(header + i, 1, sizeof(header) - i, zs->zs_fp) != 239166255Sdelphij sizeof(header) - i || 240166255Sdelphij memcmp(header, magic_header, sizeof(magic_header)) != 0) { 241166255Sdelphij errno = EFTYPE; 242166255Sdelphij return (-1); 243166255Sdelphij } 244166255Sdelphij total_compressed_bytes = 0; 245166255Sdelphij zs->zs_maxbits = header[2]; /* Set -b from file. */ 246166255Sdelphij zs->zs_block_compress = zs->zs_maxbits & BLOCK_MASK; 247166255Sdelphij zs->zs_maxbits &= BIT_MASK; 248166255Sdelphij zs->zs_maxmaxcode = 1L << zs->zs_maxbits; 249225827Sbz if (zs->zs_maxbits > BITS || zs->zs_maxbits < 12) { 250166255Sdelphij errno = EFTYPE; 251166255Sdelphij return (-1); 252166255Sdelphij } 253166255Sdelphij /* As above, initialize the first 256 entries in the table. */ 254166255Sdelphij zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS); 255166255Sdelphij for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; zs->u.r.zs_code--) { 256166255Sdelphij tab_prefixof(zs->u.r.zs_code) = 0; 257166255Sdelphij tab_suffixof(zs->u.r.zs_code) = (char_type) zs->u.r.zs_code; 258166255Sdelphij } 259166255Sdelphij zs->zs_free_ent = zs->zs_block_compress ? FIRST : 256; 260166255Sdelphij 261225827Sbz zs->u.r.zs_oldcode = -1; 262166255Sdelphij zs->u.r.zs_stackp = de_stack; 263166255Sdelphij 264166255Sdelphij while ((zs->u.r.zs_code = getcode(zs)) > -1) { 265166255Sdelphij 266166255Sdelphij if ((zs->u.r.zs_code == CLEAR) && zs->zs_block_compress) { 267166255Sdelphij for (zs->u.r.zs_code = 255; zs->u.r.zs_code >= 0; 268166255Sdelphij zs->u.r.zs_code--) 269166255Sdelphij tab_prefixof(zs->u.r.zs_code) = 0; 270166255Sdelphij zs->zs_clear_flg = 1; 271225827Sbz zs->zs_free_ent = FIRST; 272225827Sbz zs->u.r.zs_oldcode = -1; 273225827Sbz continue; 274166255Sdelphij } 275166255Sdelphij zs->u.r.zs_incode = zs->u.r.zs_code; 276166255Sdelphij 277166255Sdelphij /* Special case for KwKwK string. */ 278166255Sdelphij if (zs->u.r.zs_code >= zs->zs_free_ent) { 279225827Sbz if (zs->u.r.zs_code > zs->zs_free_ent || 280225827Sbz zs->u.r.zs_oldcode == -1) { 281225827Sbz /* Bad stream. */ 282225827Sbz errno = EINVAL; 283225827Sbz return (-1); 284225827Sbz } 285166255Sdelphij *zs->u.r.zs_stackp++ = zs->u.r.zs_finchar; 286166255Sdelphij zs->u.r.zs_code = zs->u.r.zs_oldcode; 287166255Sdelphij } 288225827Sbz /* 289225827Sbz * The above condition ensures that code < free_ent. 290225827Sbz * The construction of tab_prefixof in turn guarantees that 291225827Sbz * each iteration decreases code and therefore stack usage is 292225827Sbz * bound by 1 << BITS - 256. 293225827Sbz */ 294166255Sdelphij 295166255Sdelphij /* Generate output characters in reverse order. */ 296166255Sdelphij while (zs->u.r.zs_code >= 256) { 297166255Sdelphij *zs->u.r.zs_stackp++ = tab_suffixof(zs->u.r.zs_code); 298166255Sdelphij zs->u.r.zs_code = tab_prefixof(zs->u.r.zs_code); 299166255Sdelphij } 300166255Sdelphij *zs->u.r.zs_stackp++ = zs->u.r.zs_finchar = tab_suffixof(zs->u.r.zs_code); 301166255Sdelphij 302166255Sdelphij /* And put them out in forward order. */ 303166255Sdelphijmiddle: do { 304166255Sdelphij if (count-- == 0) 305166255Sdelphij return (num); 306166255Sdelphij *bp++ = *--zs->u.r.zs_stackp; 307166255Sdelphij } while (zs->u.r.zs_stackp > de_stack); 308166255Sdelphij 309166255Sdelphij /* Generate the new entry. */ 310225827Sbz if ((zs->u.r.zs_code = zs->zs_free_ent) < zs->zs_maxmaxcode && 311225827Sbz zs->u.r.zs_oldcode != -1) { 312166255Sdelphij tab_prefixof(zs->u.r.zs_code) = (u_short) zs->u.r.zs_oldcode; 313166255Sdelphij tab_suffixof(zs->u.r.zs_code) = zs->u.r.zs_finchar; 314166255Sdelphij zs->zs_free_ent = zs->u.r.zs_code + 1; 315166255Sdelphij } 316166255Sdelphij 317166255Sdelphij /* Remember previous code. */ 318166255Sdelphij zs->u.r.zs_oldcode = zs->u.r.zs_incode; 319166255Sdelphij } 320166255Sdelphij zs->zs_state = S_EOF; 321166255Sdelphijeof: return (num - count); 322166255Sdelphij} 323166255Sdelphij 324166255Sdelphij/*- 325166255Sdelphij * Read one code from the standard input. If EOF, return -1. 326166255Sdelphij * Inputs: 327166255Sdelphij * stdin 328166255Sdelphij * Outputs: 329166255Sdelphij * code or -1 is returned. 330166255Sdelphij */ 331166255Sdelphijstatic code_int 332166255Sdelphijgetcode(struct s_zstate *zs) 333166255Sdelphij{ 334166255Sdelphij code_int gcode; 335166255Sdelphij int r_off, bits, i; 336166255Sdelphij char_type *bp; 337166255Sdelphij 338166255Sdelphij bp = zs->u.r.zs_gbuf; 339166255Sdelphij if (zs->zs_clear_flg > 0 || zs->u.r.zs_roffset >= zs->u.r.zs_size || 340166255Sdelphij zs->zs_free_ent > zs->zs_maxcode) { 341166255Sdelphij /* 342166255Sdelphij * If the next entry will be too big for the current gcode 343166255Sdelphij * size, then we must increase the size. This implies reading 344166255Sdelphij * a new buffer full, too. 345166255Sdelphij */ 346166255Sdelphij if (zs->zs_free_ent > zs->zs_maxcode) { 347166255Sdelphij zs->zs_n_bits++; 348166255Sdelphij if (zs->zs_n_bits == zs->zs_maxbits) /* Won't get any bigger now. */ 349166255Sdelphij zs->zs_maxcode = zs->zs_maxmaxcode; 350166255Sdelphij else 351166255Sdelphij zs->zs_maxcode = MAXCODE(zs->zs_n_bits); 352166255Sdelphij } 353166255Sdelphij if (zs->zs_clear_flg > 0) { 354166255Sdelphij zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS); 355166255Sdelphij zs->zs_clear_flg = 0; 356166255Sdelphij } 357166255Sdelphij /* XXX */ 358166255Sdelphij for (i = 0; i < zs->zs_n_bits && compressed_prelen; i++, compressed_prelen--) 359166255Sdelphij zs->u.r.zs_gbuf[i] = *compressed_pre++; 360166255Sdelphij zs->u.r.zs_size = fread(zs->u.r.zs_gbuf + i, 1, zs->zs_n_bits - i, zs->zs_fp); 361166255Sdelphij zs->u.r.zs_size += i; 362166255Sdelphij if (zs->u.r.zs_size <= 0) /* End of file. */ 363166255Sdelphij return (-1); 364166255Sdelphij zs->u.r.zs_roffset = 0; 365166255Sdelphij 366166255Sdelphij total_compressed_bytes += zs->u.r.zs_size; 367166255Sdelphij 368166255Sdelphij /* Round size down to integral number of codes. */ 369166255Sdelphij zs->u.r.zs_size = (zs->u.r.zs_size << 3) - (zs->zs_n_bits - 1); 370166255Sdelphij } 371166255Sdelphij r_off = zs->u.r.zs_roffset; 372166255Sdelphij bits = zs->zs_n_bits; 373166255Sdelphij 374166255Sdelphij /* Get to the first byte. */ 375166255Sdelphij bp += (r_off >> 3); 376166255Sdelphij r_off &= 7; 377166255Sdelphij 378166255Sdelphij /* Get first part (low order bits). */ 379166255Sdelphij gcode = (*bp++ >> r_off); 380166255Sdelphij bits -= (8 - r_off); 381166255Sdelphij r_off = 8 - r_off; /* Now, roffset into gcode word. */ 382166255Sdelphij 383166255Sdelphij /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ 384166255Sdelphij if (bits >= 8) { 385166255Sdelphij gcode |= *bp++ << r_off; 386166255Sdelphij r_off += 8; 387166255Sdelphij bits -= 8; 388166255Sdelphij } 389166255Sdelphij 390166255Sdelphij /* High order bits. */ 391166255Sdelphij gcode |= (*bp & rmask[bits]) << r_off; 392166255Sdelphij zs->u.r.zs_roffset += zs->zs_n_bits; 393166255Sdelphij 394166255Sdelphij return (gcode); 395166255Sdelphij} 396166255Sdelphij 397