]>
git.saurik.com Git - wxWidgets.git/blob - utils/Install/sfxzip/unshrink.c
1 /*---------------------------------------------------------------------------
3 unshrink.c version 1.21 23 Nov 95
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.
13 THE INFO-ZIP GROUP DISCLAIMS ALL LIABILITY FOR USE OF THIS CODE
14 IN VIOLATION OF APPLICABLE PATENT LAW.
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
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,
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.
46 For large-memory machines, a second outbuf is allocated for translations,
47 but only if unshrinking and only if translations are required.
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
54 Copyright 1994, 1995 Greg Roelofs. See the accompanying file "COPYING"
55 in UnZip 5.20 (or later) source or binary distributions.
57 ---------------------------------------------------------------------------*/
60 #define UNZIP_INTERNAL
61 #include "unzip.h" /* defines LZW_CLEAN by default */
66 static void partial_clear
OF((__GPRO
));
70 if ((c)<32 || (c)>=127) pipeit("\\x%02x",(c)); else { }
75 /* HSIZE is defined as 2^13 (8192) in unzip.h */
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) */
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
87 /***********************/
88 /* Function unshrink() */
89 /***********************/
94 int offset
= (HSIZE
- 1);
95 uch
*stacktop
= stack
+ offset
;
97 int codesize
=9, len
, KwKwK
, error
;
98 shrint code
, oldcode
, freecode
, curcode
;
100 unsigned int outbufsiz
;
101 #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
102 /* Normally realbuf and outbuf will be the same. However, if the data
103 * are redirected to a large memory buffer, realbuf will point to the
104 * new location while outbuf will remain pointing to the malloc'd
106 uch
*realbuf
= G
.outbuf
;
108 # define realbuf G.outbuf
112 /*---------------------------------------------------------------------------
113 Initialize various variables.
114 ---------------------------------------------------------------------------*/
116 lastfreecode
= BOGUSCODE
;
118 #ifndef VMS /* VMS uses its own buffer scheme for textmode flush(). */
120 /* non-memory-limited machines: allocate second (large) buffer for
121 * textmode conversion in flush(), but only if needed */
122 if (G
.pInfo
->textmode
&& !G
.outbuf2
&&
123 (G
.outbuf2
= (uch
*)malloc(TRANSBUFSIZ
)) == (uch
*)NULL
)
128 for (code
= 0; code
< BOGUSCODE
; ++code
) {
129 Value
[code
] = (uch
)code
;
130 parent
[code
] = BOGUSCODE
;
132 for (code
= BOGUSCODE
+1; code
< HSIZE
; ++code
)
133 parent
[code
] = FREE_CODE
;
135 #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
136 if (G
.redirect_slide
) { /* use normal outbuf unless we're a DLL routine */
137 realbuf
= G
.redirect_buffer
;
138 outbufsiz
= G
.redirect_size
;
142 if (G
.pInfo
->textmode
&& !G
.redirect_data
)
144 if (G
.pInfo
->textmode
)
146 outbufsiz
= RAWBUFSIZ
;
148 outbufsiz
= OUTBUFSIZ
;
152 /*---------------------------------------------------------------------------
153 Get and output first code, then loop over remaining ones.
154 ---------------------------------------------------------------------------*/
156 READBITS(codesize
, oldcode
)
158 *G
.outptr
++ = (uch
)oldcode
;
164 READBITS(codesize
, code
)
167 if (code
== BOGUSCODE
) { /* possible to have consecutive escapes? */
168 READBITS(codesize
, code
)
171 Trace((stderr
, " (codesize now %d bits)\n", codesize
));
172 } else if (code
== 2) {
173 Trace((stderr
, " (partial clear code)\n"));
174 partial_clear(__G
); /* clear leafs (nodes with no children) */
175 Trace((stderr
, " (done with partial clear)\n"));
176 lastfreecode
= BOGUSCODE
; /* reset start of free-node search */
181 /*-----------------------------------------------------------------------
182 Translate code: traverse tree from leaf back to root.
183 -----------------------------------------------------------------------*/
188 if (parent
[curcode
] == FREE_CODE
) {
189 /* or (FLAG_BITS[curcode] & FREE_CODE)? */
191 Trace((stderr
, " (found a KwKwK code %d; oldcode = %d)\n", code
,
193 --newstr
; /* last character will be same as first character */
199 *newstr
-- = Value
[curcode
];
200 curcode
= (shrint
)(parent
[curcode
] & CODE_MASK
);
201 } while (curcode
!= BOGUSCODE
);
203 len
= (int)(stacktop
- newstr
++);
207 /*-----------------------------------------------------------------------
208 Write expanded string in reverse order to output buffer.
209 -----------------------------------------------------------------------*/
211 Trace((stderr
, "code %4d; oldcode %4d; char %3d (%c); string [", code
,
212 oldcode
, (int)(*newstr
), (*newstr
<32 || *newstr
>=127)? ' ':*newstr
));
217 for (p
= newstr
; p
< newstr
+len
; ++p
) {
220 if (++G
.outcnt
== outbufsiz
) {
221 Trace((stderr
, "doing flush(), outcnt = %lu\n", G
.outcnt
));
222 if ((error
= flush(__G__ realbuf
, G
.outcnt
, TRUE
)) != 0)
223 pipeit("unshrink: flush() error (%d)\n",
225 Trace((stderr
, "done with flush()\n"));
232 /*-----------------------------------------------------------------------
233 Add new leaf (first character of newstr) to tree as child of oldcode.
234 -----------------------------------------------------------------------*/
236 /* search for freecode */
237 freecode
= (shrint
)(lastfreecode
+ 1);
238 /* add if-test before loop for speed? */
239 while (parent
[freecode
] != FREE_CODE
)
241 lastfreecode
= freecode
;
242 Trace((stderr
, "]; newcode %d\n", freecode
));
244 Value
[freecode
] = *newstr
;
245 parent
[freecode
] = oldcode
;
250 /*---------------------------------------------------------------------------
251 Flush any remaining data and return to sender...
252 ---------------------------------------------------------------------------*/
255 Trace((stderr
, "doing final flush(), outcnt = %lu\n", G
.outcnt
));
256 if ((error
= flush(__G__ realbuf
, G
.outcnt
, TRUE
)) != 0)
257 pipeit("unshrink: flush() error (%d)\n", error
);
258 Trace((stderr
, "done with flush()\n"));
263 } /* end function unshrink() */
269 /****************************/
270 /* Function partial_clear() */ /* no longer recursive... */
271 /****************************/
273 static void partial_clear(__G
)
276 register shrint code
;
278 /* clear all nodes which have no children (i.e., leaf nodes only) */
280 /* first loop: mark each parent as such */
281 for (code
= BOGUSCODE
+1; code
< HSIZE
; ++code
) {
282 register shrint cparent
= (shrint
)(parent
[code
] & CODE_MASK
);
284 if (cparent
> BOGUSCODE
&& cparent
!= FREE_CODE
)
285 FLAG_BITS
[cparent
] |= HAS_CHILD
; /* set parent's child-bit */
288 /* second loop: clear all nodes *not* marked as parents; reset flag bits */
289 for (code
= BOGUSCODE
+1; code
< HSIZE
; ++code
) {
290 if (FLAG_BITS
[code
] & HAS_CHILD
) /* just clear child-bit */
291 FLAG_BITS
[code
] &= ~HAS_CHILD
;
292 else { /* leaf: lose it */
293 Trace((stderr
, "%d\n", code
));
294 parent
[code
] = FREE_CODE
;
301 #endif /* !LZW_CLEAN */