"Fossies" - the Fresh Open Source Software Archive

Member "s-nail-14.9.11/obs-lzw.c" (8 Aug 2018, 19765 Bytes) of package /linux/misc/s-nail-14.9.11.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "obs-lzw.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 14.9.10_vs_14.9.11.

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