1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*---------------------------------------------------------------------------
unreduce.c
The Reducing algorithm is actually a combination of two distinct algorithms.
The first algorithm compresses repeated byte sequences, and the second al-
gorithm takes the compressed stream from the first algorithm and applies a
probabilistic compression method.
* Copyright 1989 Samuel H. Smith; All rights reserved
*
* Do not distribute modified versions without my permission.
* Do not remove or alter this notice or any other copyright notice.
* If you use this in your own program you must distribute source code.
* Do not use any of this in a commercial product.
See the accompanying file "COPYING" in UnZip source and binary distributions
for further information. This code is NOT used unless USE_SMITH_CODE is
explicitly defined (==> COPYRIGHT_CLEAN is not defined).
---------------------------------------------------------------------------*/
#define UNZIP_INTERNAL
#include "unzip.h" /* defines COPYRIGHT_CLEAN by default */
#ifndef COPYRIGHT_CLEAN
/**************************************/
/* UnReduce Defines, Typedefs, etc. */
/**************************************/
#define DLE 144
typedef uch f_array[64]; /* for followers[256][64] */
/******************************/
/* UnReduce Local Functions */
/******************************/
static void LoadFollowers OF((__GPRO__ f_array *followers, uch *Slen));
/*******************************/
/* UnReduce Global Constants */
/*******************************/
static ZCONST shrint L_table[] =
{0, 0x7f, 0x3f, 0x1f, 0x0f};
static ZCONST shrint D_shift[] =
{0, 0x07, 0x06, 0x05, 0x04};
static ZCONST shrint D_mask[] =
{0, 0x01, 0x03, 0x07, 0x0f};
static ZCONST shrint B_table[] =
{8, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8};
/*************************/
/* Function unreduce() */
/*************************/
void unreduce(__G) /* expand probabilistically reduced data */
__GDEF
{
register int lchar = 0;
shrint nchar;
shrint ExState = 0;
shrint V = 0;
shrint Len = 0;
long s = G.ucsize; /* number of bytes left to decompress */
unsigned w = 0; /* position in output window slide[] */
unsigned u = 1; /* true if slide[] unflushed */
uch Slen[256];
f_array *followers = (f_array *)(slide + 0x4000);
int factor = G.lrec.compression_method - 1;
LoadFollowers(__G__ followers, Slen);
while (s > 0 /* && (!zipeof) */) {
if (Slen[lchar] == 0)
READBITS(8, nchar) /* ; */
else {
READBITS(1, nchar) /* ; */
if (nchar != 0)
READBITS(8, nchar) /* ; */
else {
shrint follower;
int bitsneeded = B_table[Slen[lchar]];
READBITS(bitsneeded, follower) /* ; */
nchar = followers[lchar][follower];
}
}
/* expand the resulting byte */
switch (ExState) {
case 0:
if (nchar != DLE) {
s--;
slide[w++] = (uch)nchar;
if (w == 0x4000) {
flush(__G__ slide, (ulg)w, 0);
w = u = 0;
}
}
else
ExState = 1;
break;
case 1:
if (nchar != 0) {
V = nchar;
Len = V & L_table[factor];
if (Len == L_table[factor])
ExState = 2;
else
ExState = 3;
} else {
s--;
slide[w++] = DLE;
if (w == 0x4000)
{
flush(__G__ slide, (ulg)w, 0);
w = u = 0;
}
ExState = 0;
}
break;
case 2:{
Len += nchar;
ExState = 3;
}
break;
case 3:{
register unsigned e;
register unsigned n = Len + 3;
register unsigned d = w - ((((V >> D_shift[factor]) &
D_mask[factor]) << 8) + nchar + 1);
s -= n;
do {
n -= (e = (e = 0x4000 - ((d &= 0x3fff) > w ? d : w)) > n ?
n : e);
if (u && w <= d)
{
memzero(slide + w, e);
w += e;
d += e;
}
else
if (w - d < e) /* (assume unsigned comparison) */
do { /* slow to avoid memcpy() overlap */
slide[w++] = slide[d++];
} while (--e);
else
{
memcpy(slide + w, slide + d, e);
w += e;
d += e;
}
if (w == 0x4000)
{
flush(__G__ slide, (ulg)w, 0);
w = u = 0;
}
} while (n);
ExState = 0;
}
break;
}
/* store character for next iteration */
lchar = nchar;
}
/* flush out slide */
flush(__G__ slide, (ulg)w, 0);
}
/******************************/
/* Function LoadFollowers() */
/******************************/
static void LoadFollowers(__G__ followers, Slen)
__GDEF
f_array *followers;
uch *Slen;
{
register int x;
register int i;
for (x = 255; x >= 0; x--) {
READBITS(6, Slen[x]) /* ; */
for (i = 0; (uch)i < Slen[x]; i++)
READBITS(8, followers[x][i]) /* ; */
}
}
#endif /* !COPYRIGHT_CLEAN */
|