home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / compress / gnuzip_.zip / UNLZW.C < prev    next >
C/C++ Source or Header  |  1993-03-28  |  9KB  |  371 lines

  1. /* unlzw.c -- decompress files in LZW format.
  2.  * The code in this file is directly derived from the public domain 'compress'
  3.  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  4.  * Ken Turkowski, Dave Mack and Peter Jannesen.
  5.  *
  6.  * This is a temporary version which will be rewritten in some future version
  7.  * to accomodate in-memory decompression.
  8.  */
  9.  
  10. #ifndef lint
  11. static char rcsid[] = "$Id: unlzw.c,v 0.10 1993/02/10 16:07:22 jloup Exp $";
  12. #endif
  13.  
  14. #include "tailor.h"
  15. #include "gzip.h"
  16. #include "lzw.h"
  17.  
  18. #include <stdio.h>
  19.  
  20. #if HAVE_UNISTD_H
  21. #  include <sys/types.h>
  22. #  include <unistd.h>
  23. #endif
  24.  
  25. typedef    unsigned char    char_type;
  26. typedef          long   code_int;
  27. typedef unsigned long     count_int;
  28. typedef unsigned short    count_short;
  29. typedef unsigned long     cmp_code_int;
  30.  
  31. #define MAXCODE(n)    (1L << (n))
  32.     
  33. #ifndef    REGISTERS
  34. #    define    REGISTERS    2
  35. #endif
  36. #define    REG1    
  37. #define    REG2    
  38. #define    REG3    
  39. #define    REG4    
  40. #define    REG5    
  41. #define    REG6    
  42. #define    REG7    
  43. #define    REG8    
  44. #define    REG9    
  45. #define    REG10
  46. #define    REG11    
  47. #define    REG12    
  48. #define    REG13
  49. #define    REG14
  50. #define    REG15
  51. #define    REG16
  52. #if REGISTERS >= 1
  53. #    undef    REG1
  54. #    define    REG1    register
  55. #endif
  56. #if REGISTERS >= 2
  57. #    undef    REG2
  58. #    define    REG2    register
  59. #endif
  60. #if REGISTERS >= 3
  61. #    undef    REG3
  62. #    define    REG3    register
  63. #endif
  64. #if REGISTERS >= 4
  65. #    undef    REG4
  66. #    define    REG4    register
  67. #endif
  68. #if REGISTERS >= 5
  69. #    undef    REG5
  70. #    define    REG5    register
  71. #endif
  72. #if REGISTERS >= 6
  73. #    undef    REG6
  74. #    define    REG6    register
  75. #endif
  76. #if REGISTERS >= 7
  77. #    undef    REG7
  78. #    define    REG7    register
  79. #endif
  80. #if REGISTERS >= 8
  81. #    undef    REG8
  82. #    define    REG8    register
  83. #endif
  84. #if REGISTERS >= 9
  85. #    undef    REG9
  86. #    define    REG9    register
  87. #endif
  88. #if REGISTERS >= 10
  89. #    undef    REG10
  90. #    define    REG10    register
  91. #endif
  92. #if REGISTERS >= 11
  93. #    undef    REG11
  94. #    define    REG11    register
  95. #endif
  96. #if REGISTERS >= 12
  97. #    undef    REG12
  98. #    define    REG12    register
  99. #endif
  100. #if REGISTERS >= 13
  101. #    undef    REG13
  102. #    define    REG13    register
  103. #endif
  104. #if REGISTERS >= 14
  105. #    undef    REG14
  106. #    define    REG14    register
  107. #endif
  108. #if REGISTERS >= 15
  109. #    undef    REG15
  110. #    define    REG15    register
  111. #endif
  112. #if REGISTERS >= 16
  113. #    undef    REG16
  114. #    define    REG16    register
  115. #endif
  116.     
  117. #ifndef    BYTEORDER
  118. #    define    BYTEORDER    0000
  119. #endif
  120.     
  121. #ifndef    NOALLIGN
  122. #    define    NOALLIGN    0
  123. #endif
  124.  
  125.  
  126. union    bytes {
  127.     long  word;
  128.     struct {
  129. #if BYTEORDER == 4321
  130.     char_type    b1;
  131.     char_type    b2;
  132.     char_type    b3;
  133.     char_type    b4;
  134. #else
  135. #if BYTEORDER == 1234
  136.     char_type    b4;
  137.     char_type    b3;
  138.     char_type    b2;
  139.     char_type    b1;
  140. #else
  141. #    undef    BYTEORDER
  142.     int  dummy;
  143. #endif
  144. #endif
  145.     } bytes;
  146. };
  147.  
  148. #if BYTEORDER == 4321 && NOALLIGN == 1
  149. #  define input(b,o,c,n,m){ \
  150.      (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
  151.      (o) += (n); \
  152.    }
  153. #else
  154. #  define input(b,o,c,n,m){ \
  155.      REG1 char_type *p = &(b)[(o)>>3]; \
  156.      (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
  157.      ((long)(p[2])<<16))>>((o)&0x7))&(m); \
  158.      (o) += (n); \
  159.    }
  160. #endif
  161.  
  162. #ifndef MAXSEG_64K
  163.    /* DECLARE(ush, tab_prefix, (1<<BITS)); -- prefix code */
  164. #  define tab_prefixof(i) tab_prefix[i]
  165. #  define clear_tab_prefixof()    memzero(tab_prefix, 256);
  166. #else
  167.    /* DECLARE(ush, tab_prefix0, (1<<(BITS-1)); -- prefix for even codes */
  168.    /* DECLARE(ush, tab_prefix1, (1<<(BITS-1)); -- prefix for odd  codes */
  169.    ush *tab_prefix[2];
  170. #  define tab_prefixof(i) tab_prefix[(i)&1][(i)>>1]
  171. #  define clear_tab_prefixof()    \
  172.       memzero(tab_prefix0, 128), \
  173.       memzero(tab_prefix1, 128);
  174. #endif
  175. #define de_stack        ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
  176. #define tab_suffixof(i) tab_suffix[i]
  177.  
  178. int block_mode = BLOCK_MODE; /* block compress mode -C compatible with 2.0 */
  179.  
  180. /* ============================================================================
  181.  * Decompress in to out.  This routine adapts to the codes in the
  182.  * file building the "string" table on-the-fly; requiring no table to
  183.  * be stored in the compressed file.
  184.  * IN assertions: the buffer inbuf contains already the beginning of
  185.  *   the compressed data, from offsets iptr to insize-1 included.
  186.  *   The magic header has already been checked and skipped.
  187.  *   bytes_in and bytes_out have been initialized.
  188.  */
  189. void unlzw(in, out) 
  190.     int in, out;    /* input and output file descriptors */
  191. {
  192.     REG2   char_type  *stackp;
  193.     REG3   code_int   code;
  194.     REG4   int        finchar;
  195.     REG5   code_int   oldcode;
  196.     REG6   code_int   incode;
  197.     REG7   long       inbits;
  198.     REG8   long       posbits;
  199.     REG9   int        outpos;
  200. /*  REG10  int        insize; (global) */
  201.     REG11  unsigned   bitmask;
  202.     REG12  code_int   free_ent;
  203.     REG13  code_int   maxcode;
  204.     REG14  code_int   maxmaxcode;
  205.     REG15  int        n_bits;
  206.     REG16  int        rsize;
  207.     
  208. #ifdef MAXSEG_64K
  209.     tab_prefix[0] = tab_prefix0;
  210.     tab_prefix[1] = tab_prefix1;
  211. #endif
  212.     maxbits = get_byte();
  213.     block_mode = maxbits & BLOCK_MODE;
  214.     if ((maxbits & LZW_RESERVED) != 0) {
  215.     fprintf(stderr, "%s: warning, unknown flags 0x%x\n",
  216.         ifname, maxbits & LZW_RESERVED);
  217.     if (exit_code == OK) exit_code = WARNING;
  218.     }
  219.     maxbits &= BIT_MASK;
  220.     maxmaxcode = MAXCODE(maxbits);
  221.     
  222.     if (maxbits > BITS) {
  223.     fprintf(stderr,
  224.         "%s: compressed with %d bits, can only handle %d bits\n",
  225.         ifname, maxbits, BITS);
  226.     exit_code = ERROR;
  227.     return;
  228.     }
  229.     rsize = insize;
  230.     maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  231.     bitmask = (1<<n_bits)-1;
  232.     oldcode = -1;
  233.     finchar = 0;
  234.     outpos = 0;
  235.     posbits = inptr<<3;
  236.  
  237.     free_ent = ((block_mode) ? FIRST : 256);
  238.     
  239.     clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
  240.     
  241.     for (code = 255 ; code >= 0 ; --code) {
  242.     tab_suffixof(code) = (char_type)code;
  243.     }
  244.     do {
  245.     REG1 int i;
  246.     int  e;
  247.     int  o;
  248.     
  249.     resetbuf:
  250.     e = insize-(o = (posbits>>3));
  251.     
  252.     for (i = 0 ; i < e ; ++i) {
  253.         inbuf[i] = inbuf[i+o];
  254.     }
  255.     insize = e;
  256.     posbits = 0;
  257.     
  258.     if (insize < INBUF_EXTRA) {
  259.         if ((rsize = read(in, inbuf+insize, INBUFSIZ)) == EOF) {
  260.         read_error();
  261.         }
  262.         insize += rsize;
  263.     }
  264.     inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 : 
  265.           ((long)insize<<3)-(n_bits-1));
  266.     
  267.     while (inbits > posbits) {
  268.         if (free_ent > maxcode) {
  269.         posbits = ((posbits-1) +
  270.                ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  271.         ++n_bits;
  272.         if (n_bits == maxbits) {
  273.             maxcode = maxmaxcode;
  274.         } else {
  275.             maxcode = MAXCODE(n_bits)-1;
  276.         }
  277.         bitmask = (1<<n_bits)-1;
  278.         goto resetbuf;
  279.         }
  280.         input(inbuf,posbits,code,n_bits,bitmask);
  281.         
  282.         if (oldcode == -1) {
  283.         outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
  284.         continue;
  285.         }
  286.         if (code == CLEAR && block_mode) {
  287.         clear_tab_prefixof();
  288.         free_ent = FIRST - 1;
  289.         posbits = ((posbits-1) +
  290.                ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  291.         maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  292.         bitmask = (1<<n_bits)-1;
  293.         goto resetbuf;
  294.         }
  295.         incode = code;
  296.         stackp = de_stack;
  297.         
  298.         if (code >= free_ent) { /* Special case for KwKwK string. */
  299.         if (code > free_ent) {
  300.  
  301.             REG1 char_type *p;
  302.  
  303.             posbits -= n_bits;
  304.             p = &inbuf[posbits>>3];
  305.             
  306.             fprintf(stderr,
  307.                 "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
  308.                 code, free_ent, n_bits, insize);
  309.             fprintf(stderr,
  310.                 "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
  311.                 posbits, p[-1],p[0],p[1],p[2],p[3]);
  312.             if (!test && outpos > 0
  313.             && write(out, outbuf, outpos) != outpos) {
  314.             write_error();
  315.             }
  316.             error("Corrupt input. Use zcat to recover some data.");
  317.         }
  318.         *--stackp = (char_type)finchar;
  319.         code = oldcode;
  320.         }
  321.  
  322.         while ((cmp_code_int)code >= (cmp_code_int)256) {
  323.         /* Generate output characters in reverse order */
  324.         *--stackp = tab_suffixof(code);
  325.         code = tab_prefixof(code);
  326.         }
  327.         *--stackp =    (char_type)(finchar = tab_suffixof(code));
  328.         
  329.         /* And put them out in forward order */
  330.         {
  331.         REG1 int    i;
  332.         
  333.         if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
  334.             do {
  335.             if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
  336.  
  337.             if (i > 0) {
  338.                 memcpy(outbuf+outpos, stackp, i);
  339.                 outpos += i;
  340.             }
  341.             if (outpos >= OUTBUFSIZ) {
  342.                 if (!test && write(out, outbuf, outpos) != outpos){
  343.                 write_error();
  344.                 }
  345.                 outpos = 0;
  346.             }
  347.             stackp+= i;
  348.             } while ((i = (de_stack-stackp)) > 0);
  349.         } else {
  350.             memcpy(outbuf+outpos, stackp, i);
  351.             outpos += i;
  352.         }
  353.         }
  354.  
  355.         if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
  356.  
  357.         tab_prefixof(code) = (unsigned short)oldcode;
  358.         tab_suffixof(code) = (char_type)finchar;
  359.         free_ent = code+1;
  360.         } 
  361.         oldcode = incode;    /* Remember previous code.    */
  362.     }
  363.     bytes_in += rsize;
  364.  
  365.     } while (rsize != 0);
  366.     
  367.     if (!test && outpos > 0 && write(out, outbuf, outpos) != outpos) {
  368.     write_error();
  369.     }
  370. }
  371.