]>
git.saurik.com Git - wxWidgets.git/blob - utils/Install/sfxzip/unreduce.c
1 /*---------------------------------------------------------------------------
5 The Reducing algorithm is actually a combination of two distinct algorithms.
6 The first algorithm compresses repeated byte sequences, and the second al-
7 gorithm takes the compressed stream from the first algorithm and applies a
8 probabilistic compression method.
10 * Copyright 1989 Samuel H. Smith; All rights reserved
12 * Do not distribute modified versions without my permission.
13 * Do not remove or alter this notice or any other copyright notice.
14 * If you use this in your own program you must distribute source code.
15 * Do not use any of this in a commercial product.
17 See the accompanying file "COPYING" in UnZip source and binary distributions
18 for further information. This code is NOT used unless USE_SMITH_CODE is
19 explicitly defined (==> COPYRIGHT_CLEAN is not defined).
21 ---------------------------------------------------------------------------*/
24 #define UNZIP_INTERNAL
25 #include "unzip.h" /* defines COPYRIGHT_CLEAN by default */
28 #ifndef COPYRIGHT_CLEAN
30 /**************************************/
31 /* UnReduce Defines, Typedefs, etc. */
32 /**************************************/
36 typedef uch f_array
[64]; /* for followers[256][64] */
40 /******************************/
41 /* UnReduce Local Functions */
42 /******************************/
44 static void LoadFollowers
OF((__GPRO__ f_array
*followers
, uch
*Slen
));
48 /*******************************/
49 /* UnReduce Global Constants */
50 /*******************************/
52 static ZCONST shrint L_table
[] =
53 {0, 0x7f, 0x3f, 0x1f, 0x0f};
55 static ZCONST shrint D_shift
[] =
56 {0, 0x07, 0x06, 0x05, 0x04};
57 static ZCONST shrint D_mask
[] =
58 {0, 0x01, 0x03, 0x07, 0x0f};
60 static ZCONST shrint B_table
[] =
61 {8, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5,
62 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
63 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
64 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7,
65 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
66 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
67 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
68 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
69 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
70 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
71 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
72 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
73 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
74 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
81 /*************************/
82 /* Function unreduce() */
83 /*************************/
85 void unreduce(__G
) /* expand probabilistically reduced data */
88 register int lchar
= 0;
93 long s
= G
.ucsize
; /* number of bytes left to decompress */
94 unsigned w
= 0; /* position in output window slide[] */
95 unsigned u
= 1; /* true if slide[] unflushed */
98 f_array
*followers
= (f_array
*)(slide
+ 0x4000);
99 int factor
= G
.lrec
.compression_method
- 1;
101 LoadFollowers(__G__ followers
, Slen
);
103 while (s
> 0 /* && (!zipeof) */) {
104 if (Slen
[lchar
] == 0)
105 READBITS(8, nchar
) /* ; */
107 READBITS(1, nchar
) /* ; */
109 READBITS(8, nchar
) /* ; */
112 int bitsneeded
= B_table
[Slen
[lchar
]];
114 READBITS(bitsneeded
, follower
) /* ; */
115 nchar
= followers
[lchar
][follower
];
118 /* expand the resulting byte */
124 slide
[w
++] = (uch
)nchar
;
126 flush(__G__ slide
, (ulg
)w
, 0);
137 Len
= V
& L_table
[factor
];
138 if (Len
== L_table
[factor
])
147 flush(__G__ slide
, (ulg
)w
, 0);
162 register unsigned n
= Len
+ 3;
163 register unsigned d
= w
- ((((V
>> D_shift
[factor
]) &
164 D_mask
[factor
]) << 8) + nchar
+ 1);
168 n
-= (e
= (e
= 0x4000 - ((d
&= 0x3fff) > w
? d
: w
)) > n
?
172 memzero(slide
+ w
, e
);
177 if (w
- d
< e
) /* (assume unsigned comparison) */
178 do { /* slow to avoid memcpy() overlap */
179 slide
[w
++] = slide
[d
++];
183 memcpy(slide
+ w
, slide
+ d
, e
);
189 flush(__G__ slide
, (ulg
)w
, 0);
199 /* store character for next iteration */
203 /* flush out slide */
204 flush(__G__ slide
, (ulg
)w
, 0);
211 /******************************/
212 /* Function LoadFollowers() */
213 /******************************/
215 static void LoadFollowers(__G__ followers
, Slen
)
223 for (x
= 255; x
>= 0; x
--) {
224 READBITS(6, Slen
[x
]) /* ; */
225 for (i
= 0; (uch
)i
< Slen
[x
]; i
++)
226 READBITS(8, followers
[x
][i
]) /* ; */
230 #endif /* !COPYRIGHT_CLEAN */