home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip532.zip / unshrink.c < prev    next >
C/C++ Source or Header  |  1997-10-21  |  11KB  |  290 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unshrink.c                     version 1.21                     23 Nov 95
  4.  
  5.  
  6.        NOTE:  This code may or may not infringe on the so-called "Welch
  7.        patent" owned by Unisys.  (From reading the patent, it appears
  8.        that a pure LZW decompressor is *not* covered, but this claim has
  9.        not been tested in court, and Unisys is reported to believe other-
  10.        wise.)  It is therefore the responsibility of the user to acquire
  11.        whatever license(s) may be required for legal use of this code.
  12.  
  13.        THE INFO-ZIP GROUP DISCLAIMS ALL LIABILITY FOR USE OF THIS CODE
  14.        IN VIOLATION OF APPLICABLE PATENT LAW.
  15.  
  16.  
  17.   Shrinking is basically a dynamic LZW algorithm with allowed code sizes of
  18.   up to 13 bits; in addition, there is provision for partial clearing of
  19.   leaf nodes.  PKWARE uses the special code 256 (decimal) to indicate a
  20.   change in code size or a partial clear of the code tree:  256,1 for the
  21.   former and 256,2 for the latter.  [Note that partial clearing can "orphan"
  22.   nodes:  the parent-to-be can be cleared before its new child is added,
  23.   but the child is added anyway (as an orphan, as though the parent still
  24.   existed).  When the tree fills up to the point where the parent node is
  25.   reused, the orphan is effectively "adopted."  Versions prior to 1.05 were
  26.   affected more due to greater use of pointers (to children and siblings
  27.   as well as parents).]
  28.  
  29.   This replacement version of unshrink.c was written from scratch.  It is
  30.   based only on the algorithms described in Mark Nelson's _The Data Compres-
  31.   sion Book_ and in Terry Welch's original paper in the June 1984 issue of
  32.   IEEE _Computer_; no existing source code, including any in Nelson's book,
  33.   was used.
  34.  
  35.   Memory requirements have been reduced in this version and are now no more
  36.   than the original Sam Smith code.  This is still larger than any of the
  37.   other algorithms:  at a minimum, 8K+8K+16K (stack+values+parents) assuming
  38.   16-bit short ints, and this does not even include the output buffer (the
  39.   other algorithms leave the uncompressed data in the work area, typically
  40.   called slide[]).  For machines with a 64KB data space this is a problem,
  41.   particularly when text conversion is required and line endings have more
  42.   than one character.  UnZip's solution is to use two roughly equal halves
  43.   of outbuf for the ASCII conversion in such a case; the "unshrink" argument
  44.   to flush() signals that this is the case.
  45.  
  46.   For large-memory machines, a second outbuf is allocated for translations,
  47.   but only if unshrinking and only if translations are required.
  48.  
  49.               | binary mode  |        text mode
  50.     ---------------------------------------------------
  51.     big mem   |  big outbuf  | big outbuf + big outbuf2  <- malloc'd here
  52.     small mem | small outbuf | half + half small outbuf
  53.  
  54.   Copyright 1994, 1995 Greg Roelofs.  See the accompanying file "COPYING"
  55.   in UnZip 5.20 (or later) source or binary distributions.
  56.  
  57.   ---------------------------------------------------------------------------*/
  58.  
  59.  
  60. #define UNZIP_INTERNAL
  61. #include "unzip.h"       /* defines LZW_CLEAN by default */
  62.  
  63.  
  64. #ifndef LZW_CLEAN
  65.  
  66. static void  partial_clear  OF((__GPRO));
  67.  
  68. #ifdef DEBUG
  69. #  define OUTDBG(c) \
  70.    if ((c)<32 || (c)>=127) fprintf(stderr,"\\x%02x",(c)); else putc((c),stderr);
  71. #else
  72. #  define OUTDBG(c)
  73. #endif
  74.  
  75. /* HSIZE is defined as 2^13 (8192) in unzip.h */
  76. #define BOGUSCODE  256
  77. #define FLAG_BITS  parent        /* upper bits of parent[] used as flag bits */
  78. #define CODE_MASK  (HSIZE - 1)   /* 0x1fff (lower bits are parent's index) */
  79. #define FREE_CODE  HSIZE         /* 0x2000 (code is unused or was cleared) */
  80. #define HAS_CHILD  (HSIZE << 1)  /* 0x4000 (code has a child--do not clear) */
  81.  
  82. #define parent G.area.shrink.Parent
  83. #define Value  G.area.shrink.value /* "value" conflicts with Pyramid ioctl.h */
  84. #define stack  G.area.shrink.Stack
  85.  
  86.  
  87. /***********************/
  88. /* Function unshrink() */
  89. /***********************/
  90.  
  91. int unshrink(__G)
  92.      __GDEF
  93. {
  94.     int offset = (HSIZE - 1);
  95.     uch *stacktop = stack + offset;
  96.     register uch *newstr;
  97.     int codesize=9, len, KwKwK, error;
  98.     shrint code, oldcode, freecode, curcode;
  99.     shrint lastfreecode;
  100.     unsigned int outbufsiz;
  101.  
  102.  
  103. /*---------------------------------------------------------------------------
  104.     Initialize various variables.
  105.   ---------------------------------------------------------------------------*/
  106.  
  107.     lastfreecode = BOGUSCODE;
  108.  
  109. #ifndef VMS     /* VMS uses its own buffer scheme for textmode flush(). */
  110. #ifndef SMALL_MEM
  111.     /* non-memory-limited machines:  allocate second (large) buffer for
  112.      * textmode conversion in flush(), but only if needed */
  113.     if (G.pInfo->textmode && !G.outbuf2 &&
  114.         (G.outbuf2 = (uch *)malloc(TRANSBUFSIZ)) == (uch *)NULL)
  115.         return PK_MEM3;
  116. #endif
  117. #endif /* !VMS */
  118.  
  119.     for (code = 0;  code < BOGUSCODE;  ++code) {
  120.         Value[code] = (uch)code;
  121.         parent[code] = BOGUSCODE;
  122.     }
  123.     for (code = BOGUSCODE+1;  code < HSIZE;  ++code)
  124.         parent[code] = FREE_CODE;
  125.  
  126.     G.realbuf = G.outbuf;   /* use normal outbuf unless we're a DLL routine */
  127. #ifdef DLL
  128.     if (G.redirect_data) {
  129.         G.realbuf = G.redirect_buffer;
  130.         outbufsiz = G.redirect_size;
  131.     } else
  132. #endif
  133.     if (G.pInfo->textmode)
  134.         outbufsiz = RAWBUFSIZ;
  135.     else
  136.         outbufsiz = OUTBUFSIZ;
  137.     G.outptr = G.realbuf;
  138.     G.outcnt = 0L;
  139.  
  140. /*---------------------------------------------------------------------------
  141.     Get and output first code, then loop over remaining ones.
  142.   ---------------------------------------------------------------------------*/
  143.  
  144.     READBITS(codesize, oldcode)
  145.     if (!G.zipeof) {
  146.         *G.outptr++ = (uch)oldcode;
  147.         OUTDBG((uch)oldcode)
  148.         ++G.outcnt;
  149.     }
  150.  
  151.     do {
  152.         READBITS(codesize, code)
  153.         if (G.zipeof)
  154.             break;
  155.         if (code == BOGUSCODE) {   /* possible to have consecutive escapes? */
  156.             READBITS(codesize, code)
  157.             if (code == 1) {
  158.                 ++codesize;
  159.                 Trace((stderr, " (codesize now %d bits)\n", codesize));
  160.             } else if (code == 2) {
  161.                 Trace((stderr, " (partial clear code)\n"));
  162.                 partial_clear(__G);   /* clear leafs (nodes with no children) */
  163.                 Trace((stderr, " (done with partial clear)\n"));
  164.                 lastfreecode = BOGUSCODE;  /* reset start of free-node search */
  165.             }
  166.             continue;
  167.         }
  168.  
  169.     /*-----------------------------------------------------------------------
  170.         Translate code:  traverse tree from leaf back to root.
  171.       -----------------------------------------------------------------------*/
  172.  
  173.         newstr = stacktop;
  174.         curcode = code;
  175.  
  176.         if (parent[curcode] == FREE_CODE) {
  177.             /* or (FLAG_BITS[curcode] & FREE_CODE)? */
  178.             KwKwK = TRUE;
  179.             Trace((stderr, " (found a KwKwK code %d; oldcode = %d)\n", code,
  180.               oldcode));
  181.             --newstr;   /* last character will be same as first character */
  182.             curcode = oldcode;
  183.         } else
  184.             KwKwK = FALSE;
  185.  
  186.         do {
  187.             *newstr-- = Value[curcode];
  188.             curcode = (shrint)(parent[curcode] & CODE_MASK);
  189.         } while (curcode != BOGUSCODE);
  190.  
  191.         len = (int)(stacktop - newstr++);
  192.         if (KwKwK)
  193.             *stacktop = *newstr;
  194.  
  195.     /*-----------------------------------------------------------------------
  196.         Write expanded string in reverse order to output buffer.
  197.       -----------------------------------------------------------------------*/
  198.  
  199.         Trace((stderr, "code %4d; oldcode %4d; char %3d (%c); string [", code,
  200.           oldcode, (int)(*newstr), (*newstr<32 || *newstr>=127)? ' ':*newstr));
  201.  
  202.         {
  203.             register uch *p;
  204.  
  205.             for (p = newstr;  p < newstr+len;  ++p) {
  206.                 *G.outptr++ = *p;
  207.                 OUTDBG(*p)
  208.                 if (++G.outcnt == outbufsiz) {
  209.                     Trace((stderr, "doing flush(), outcnt = %lu\n", G.outcnt));
  210.                     if ((error = flush(__G__ G.realbuf, G.outcnt, TRUE)) != 0)
  211.                         fprintf(stderr, "unshrink:  flush() error (%d)\n",
  212.                           error);
  213.                     Trace((stderr, "done with flush()\n"));
  214.                     G.outptr = G.realbuf;
  215.                     G.outcnt = 0L;
  216.                 }
  217.             }
  218.         }
  219.  
  220.     /*-----------------------------------------------------------------------
  221.         Add new leaf (first character of newstr) to tree as child of oldcode.
  222.       -----------------------------------------------------------------------*/
  223.  
  224.         /* search for freecode */
  225.         freecode = (shrint)(lastfreecode + 1);
  226.         /* add if-test before loop for speed? */
  227.         while (parent[freecode] != FREE_CODE)
  228.             ++freecode;
  229.         lastfreecode = freecode;
  230.         Trace((stderr, "]; newcode %d\n", freecode));
  231.  
  232.         Value[freecode] = *newstr;
  233.         parent[freecode] = oldcode;
  234.         oldcode = code;
  235.  
  236.     } while (!G.zipeof);
  237.  
  238. /*---------------------------------------------------------------------------
  239.     Flush any remaining data and return to sender...
  240.   ---------------------------------------------------------------------------*/
  241.  
  242.     if (G.outcnt > 0L) {
  243.         Trace((stderr, "doing final flush(), outcnt = %lu\n", G.outcnt));
  244.         if ((error = flush(__G__ G.realbuf, G.outcnt, TRUE)) != 0)
  245.             fprintf(stderr, "unshrink:  flush() error (%d)\n", error);
  246.         Trace((stderr, "done with flush()\n"));
  247.     }
  248.  
  249.     return PK_OK;
  250.  
  251. } /* end function unshrink() */
  252.  
  253.  
  254.  
  255.  
  256.  
  257. /****************************/
  258. /* Function partial_clear() */      /* no longer recursive... */
  259. /****************************/
  260.  
  261. static void partial_clear(__G)
  262.      __GDEF
  263. {
  264.     register shrint code;
  265.  
  266.     /* clear all nodes which have no children (i.e., leaf nodes only) */
  267.  
  268.     /* first loop:  mark each parent as such */
  269.     for (code = BOGUSCODE+1;  code < HSIZE;  ++code) {
  270.         register shrint cparent = (shrint)(parent[code] & CODE_MASK);
  271.  
  272.         if (cparent > BOGUSCODE && cparent != FREE_CODE)
  273.             FLAG_BITS[cparent] |= HAS_CHILD;   /* set parent's child-bit */
  274.     }
  275.  
  276.     /* second loop:  clear all nodes *not* marked as parents; reset flag bits */
  277.     for (code = BOGUSCODE+1;  code < HSIZE;  ++code) {
  278.         if (FLAG_BITS[code] & HAS_CHILD)    /* just clear child-bit */
  279.             FLAG_BITS[code] &= ~HAS_CHILD;
  280.         else {                              /* leaf:  lose it */
  281.             Trace((stderr, "%d\n", code));
  282.             parent[code] = FREE_CODE;
  283.         }
  284.     }
  285.  
  286.     return;
  287. }
  288.  
  289. #endif /* !LZW_CLEAN */
  290.