📄 fdi.c
字号:
if (!pfdici) {
ERR("(!pfdici)!\n");
/* PFDI_INT(hfdi)->perf->erfOper = FDIERROR_NONE;
PFDI_INT(hfdi)->perf->erfType = ERROR_BAD_ARGUMENTS;
PFDI_INT(hfdi)->perf->fError = TRUE; */
SetLastError(ERROR_BAD_ARGUMENTS);
return FALSE;
}
rv = FDI_read_entries(hfdi, hf, pfdici, NULL);
if (rv)
pfdici->hasnext = FALSE; /* yuck. duplicate apparent cabinet.dll bug */
return rv;
}
/******************************************************************
* QTMfdi_initmodel (internal)
*
* Initialize a model which decodes symbols from [s] to [s]+[n]-1
*/
static void QTMfdi_initmodel(struct QTMmodel *m, struct QTMmodelsym *sym, int n, int s) {
int i;
m->shiftsleft = 4;
m->entries = n;
m->syms = sym;
memset(m->tabloc, 0xFF, sizeof(m->tabloc)); /* clear out look-up table */
for (i = 0; i < n; i++) {
m->tabloc[i+s] = i; /* set up a look-up entry for symbol */
m->syms[i].sym = i+s; /* actual symbol */
m->syms[i].cumfreq = n-i; /* current frequency of that symbol */
}
m->syms[n].cumfreq = 0;
}
/******************************************************************
* QTMfdi_init (internal)
*/
static int QTMfdi_init(int window, int level, fdi_decomp_state *decomp_state) {
unsigned int wndsize = 1 << window;
int msz = window * 2, i;
cab_ULONG j;
/* QTM supports window sizes of 2^10 (1Kb) through 2^21 (2Mb) */
/* if a previously allocated window is big enough, keep it */
if (window < 10 || window > 21) return DECR_DATAFORMAT;
if (QTM(actual_size) < wndsize) {
if (QTM(window)) PFDI_FREE(CAB(hfdi), QTM(window));
QTM(window) = NULL;
}
if (!QTM(window)) {
if (!(QTM(window) = PFDI_ALLOC(CAB(hfdi), wndsize))) return DECR_NOMEMORY;
QTM(actual_size) = wndsize;
}
QTM(window_size) = wndsize;
QTM(window_posn) = 0;
/* initialize static slot/extrabits tables */
for (i = 0, j = 0; i < 27; i++) {
CAB(q_length_extra)[i] = (i == 26) ? 0 : (i < 2 ? 0 : i - 2) >> 2;
CAB(q_length_base)[i] = j; j += 1 << ((i == 26) ? 5 : CAB(q_length_extra)[i]);
}
for (i = 0, j = 0; i < 42; i++) {
CAB(q_extra_bits)[i] = (i < 2 ? 0 : i-2) >> 1;
CAB(q_position_base)[i] = j; j += 1 << CAB(q_extra_bits)[i];
}
/* initialize arithmetic coding models */
QTMfdi_initmodel(&QTM(model7), &QTM(m7sym)[0], 7, 0);
QTMfdi_initmodel(&QTM(model00), &QTM(m00sym)[0], 0x40, 0x00);
QTMfdi_initmodel(&QTM(model40), &QTM(m40sym)[0], 0x40, 0x40);
QTMfdi_initmodel(&QTM(model80), &QTM(m80sym)[0], 0x40, 0x80);
QTMfdi_initmodel(&QTM(modelC0), &QTM(mC0sym)[0], 0x40, 0xC0);
/* model 4 depends on table size, ranges from 20 to 24 */
QTMfdi_initmodel(&QTM(model4), &QTM(m4sym)[0], (msz < 24) ? msz : 24, 0);
/* model 5 depends on table size, ranges from 20 to 36 */
QTMfdi_initmodel(&QTM(model5), &QTM(m5sym)[0], (msz < 36) ? msz : 36, 0);
/* model 6pos depends on table size, ranges from 20 to 42 */
QTMfdi_initmodel(&QTM(model6pos), &QTM(m6psym)[0], msz, 0);
QTMfdi_initmodel(&QTM(model6len), &QTM(m6lsym)[0], 27, 0);
return DECR_OK;
}
/************************************************************
* LZXfdi_init (internal)
*/
static int LZXfdi_init(int window, fdi_decomp_state *decomp_state) {
cab_ULONG wndsize = 1 << window;
int i, j, posn_slots;
/* LZX supports window sizes of 2^15 (32Kb) through 2^21 (2Mb) */
/* if a previously allocated window is big enough, keep it */
if (window < 15 || window > 21) return DECR_DATAFORMAT;
if (LZX(actual_size) < wndsize) {
if (LZX(window)) PFDI_FREE(CAB(hfdi), LZX(window));
LZX(window) = NULL;
}
if (!LZX(window)) {
if (!(LZX(window) = PFDI_ALLOC(CAB(hfdi), wndsize))) return DECR_NOMEMORY;
LZX(actual_size) = wndsize;
}
LZX(window_size) = wndsize;
/* initialize static tables */
for (i=0, j=0; i <= 50; i += 2) {
CAB(extra_bits)[i] = CAB(extra_bits)[i+1] = j; /* 0,0,0,0,1,1,2,2,3,3... */
if ((i != 0) && (j < 17)) j++; /* 0,0,1,2,3,4...15,16,17,17,17,17... */
}
for (i=0, j=0; i <= 50; i++) {
CAB(lzx_position_base)[i] = j; /* 0,1,2,3,4,6,8,12,16,24,32,... */
j += 1 << CAB(extra_bits)[i]; /* 1,1,1,1,2,2,4,4,8,8,16,16,32,32,... */
}
/* calculate required position slots */
if (window == 20) posn_slots = 42;
else if (window == 21) posn_slots = 50;
else posn_slots = window << 1;
/*posn_slots=i=0; while (i < wndsize) i += 1 << CAB(extra_bits)[posn_slots++]; */
LZX(R0) = LZX(R1) = LZX(R2) = 1;
LZX(main_elements) = LZX_NUM_CHARS + (posn_slots << 3);
LZX(header_read) = 0;
LZX(frames_read) = 0;
LZX(block_remaining) = 0;
LZX(block_type) = LZX_BLOCKTYPE_INVALID;
LZX(intel_curpos) = 0;
LZX(intel_started) = 0;
LZX(window_posn) = 0;
/* initialize tables to 0 (because deltas will be applied to them) */
for (i = 0; i < LZX_MAINTREE_MAXSYMBOLS; i++) LZX(MAINTREE_len)[i] = 0;
for (i = 0; i < LZX_LENGTH_MAXSYMBOLS; i++) LZX(LENGTH_len)[i] = 0;
return DECR_OK;
}
/****************************************************
* NONEfdi_decomp(internal)
*/
static int NONEfdi_decomp(int inlen, int outlen, fdi_decomp_state *decomp_state)
{
if (inlen != outlen) return DECR_ILLEGALDATA;
memcpy(CAB(outbuf), CAB(inbuf), (size_t) inlen);
return DECR_OK;
}
/********************************************************
* Ziphuft_free (internal)
*/
static void fdi_Ziphuft_free(HFDI hfdi, struct Ziphuft *t)
{
register struct Ziphuft *p, *q;
/* Go through linked list, freeing from the allocated (t[-1]) address. */
p = t;
while (p != (struct Ziphuft *)NULL)
{
q = (--p)->v.t;
PFDI_FREE(hfdi, p);
p = q;
}
}
/*********************************************************
* fdi_Ziphuft_build (internal)
*/
static cab_LONG fdi_Ziphuft_build(cab_ULONG *b, cab_ULONG n, cab_ULONG s, const cab_UWORD *d, const cab_UWORD *e,
struct Ziphuft **t, cab_LONG *m, fdi_decomp_state *decomp_state)
{
cab_ULONG a; /* counter for codes of length k */
cab_ULONG el; /* length of EOB code (value 256) */
cab_ULONG f; /* i repeats in table every f entries */
cab_LONG g; /* maximum code length */
cab_LONG h; /* table level */
register cab_ULONG i; /* counter, current code */
register cab_ULONG j; /* counter */
register cab_LONG k; /* number of bits in current code */
cab_LONG *l; /* stack of bits per table */
register cab_ULONG *p; /* pointer into ZIP(c)[],ZIP(b)[],ZIP(v)[] */
register struct Ziphuft *q; /* points to current table */
struct Ziphuft r; /* table entry for structure assignment */
register cab_LONG w; /* bits before this table == (l * h) */
cab_ULONG *xp; /* pointer into x */
cab_LONG y; /* number of dummy codes added */
cab_ULONG z; /* number of entries in current table */
l = ZIP(lx)+1;
/* Generate counts for each bit length */
el = n > 256 ? b[256] : ZIPBMAX; /* set length of EOB code, if any */
for(i = 0; i < ZIPBMAX+1; ++i)
ZIP(c)[i] = 0;
p = b; i = n;
do
{
ZIP(c)[*p]++; p++; /* assume all entries <= ZIPBMAX */
} while (--i);
if (ZIP(c)[0] == n) /* null input--all zero length codes */
{
*t = (struct Ziphuft *)NULL;
*m = 0;
return 0;
}
/* Find minimum and maximum length, bound *m by those */
for (j = 1; j <= ZIPBMAX; j++)
if (ZIP(c)[j])
break;
k = j; /* minimum code length */
if ((cab_ULONG)*m < j)
*m = j;
for (i = ZIPBMAX; i; i--)
if (ZIP(c)[i])
break;
g = i; /* maximum code length */
if ((cab_ULONG)*m > i)
*m = i;
/* Adjust last length count to fill out codes, if needed */
for (y = 1 << j; j < i; j++, y <<= 1)
if ((y -= ZIP(c)[j]) < 0)
return 2; /* bad input: more codes than bits */
if ((y -= ZIP(c)[i]) < 0)
return 2;
ZIP(c)[i] += y;
/* Generate starting offsets LONGo the value table for each length */
ZIP(x)[1] = j = 0;
p = ZIP(c) + 1; xp = ZIP(x) + 2;
while (--i)
{ /* note that i == g from above */
*xp++ = (j += *p++);
}
/* Make a table of values in order of bit lengths */
p = b; i = 0;
do{
if ((j = *p++) != 0)
ZIP(v)[ZIP(x)[j]++] = i;
} while (++i < n);
/* Generate the Huffman codes and for each, make the table entries */
ZIP(x)[0] = i = 0; /* first Huffman code is zero */
p = ZIP(v); /* grab values in bit order */
h = -1; /* no tables yet--level -1 */
w = l[-1] = 0; /* no bits decoded yet */
ZIP(u)[0] = (struct Ziphuft *)NULL; /* just to keep compilers happy */
q = (struct Ziphuft *)NULL; /* ditto */
z = 0; /* ditto */
/* go through the bit lengths (k already is bits in shortest code) */
for (; k <= g; k++)
{
a = ZIP(c)[k];
while (a--)
{
/* here i is the Huffman code of length k bits for value *p */
/* make tables up to required level */
while (k > w + l[h])
{
w += l[h++]; /* add bits already decoded */
/* compute minimum size table less than or equal to *m bits */
z = (z = g - w) > (cab_ULONG)*m ? *m : z; /* upper limit */
if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
{ /* too few codes for k-w bit table */
f -= a + 1; /* deduct codes from patterns left */
xp = ZIP(c) + k;
while (++j < z) /* try smaller tables up to z bits */
{
if ((f <<= 1) <= *++xp)
break; /* enough codes to use up j bits */
f -= *xp; /* else deduct codes from patterns */
}
}
if ((cab_ULONG)w + j > el && (cab_ULONG)w < el)
j = el - w; /* make EOB code end at table */
z = 1 << j; /* table entries for j-bit table */
l[h] = j; /* set table size in stack */
/* allocate and link in new table */
if (!(q = (struct Ziphuft *) PFDI_ALLOC(CAB(hfdi), (z + 1)*sizeof(struct Ziphuft))))
{
if(h)
fdi_Ziphuft_free(CAB(hfdi), ZIP(u)[0]);
return 3; /* not enough memory */
}
*t = q + 1; /* link to list for Ziphuft_free() */
*(t = &(q->v.t)) = (struct Ziphuft *)NULL;
ZIP(u)[h] = ++q; /* table starts after link */
/* connect to last table, if there is one */
if (h)
{
ZIP(x)[h] = i; /* save pattern for backing up */
r.b = (cab_UBYTE)l[h-1]; /* bits to dump before this table */
r.e = (cab_UBYTE)(16 + j); /* bits in this table */
r.v.t = q; /* pointer to this table */
j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
ZIP(u)[h-1][j] = r; /* connect to last table */
}
}
/* set up table entry in r */
r.b = (cab_UBYTE)(k - w);
if (p >= ZIP(v) + n)
r.e = 99; /* out of values--invalid code */
else if (*p < s)
{
r.e = (cab_UBYTE)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
r.v.n = *p++; /* simple code is just the value */
}
else
{
r.e = (cab_UBYTE)e[*p - s]; /* non-simple--look up in lists */
r.v.n = d[*p++ - s];
}
/* fill code-like entries with r */
f = 1 << (k - w);
for (j = i >> w; j < z; j += f)
q[j] = r;
/* backwards increment the k-bit code i */
for (j = 1 << (k - 1); i & j; j >>= 1)
i ^= j;
i ^= j;
/* backup over finished tables */
while ((i & ((1 << w) - 1)) != ZIP(x)[h])
w -= l[--h]; /* don't need to update q */
}
}
/* return actual size of base table */
*m = l[0];
/* Return true (1) if we were given an incomplete table */
return y != 0 && g != 1;
}
/*********************************************************
* fdi_Zipinflate_codes (internal)
*/
static cab_LONG fdi_Zipinflate_codes(struct Ziphuft *tl, struct Ziphuft *td,
cab_LONG bl, cab_LONG bd, fdi_decomp_state *decomp_state)
{
register cab_ULONG e; /* table entry flag/number of extra bits */
cab_ULONG n, d; /* length and index for copy */
cab_ULONG w; /* current window position */
struct Ziphuft *t; /* pointer to table entry */
cab_ULONG ml, md; /* masks for bl and bd bits */
register cab_ULONG b; /* bit buffer */
register cab_ULONG k; /* number of bits in bit buffer */
/* make local copies of globals */
b = ZIP(bb); /* initialize bit buffer */
k = ZIP(bk);
w = ZIP(window_posn); /* initialize window position */
/* inflate the coded data */
ml = Zipmask[bl]; /* precompute masks for speed */
md = Zipmask[bd];
for(;;)
{
ZIPNEEDBITS((cab_ULONG)bl)
if((e = (t = tl + ((cab_ULONG)b & ml))->e) > 16)
do
{
if (e == 99)
return 1;
ZIPDUMPBITS(t->b)
e -= 16;
ZIPNEEDBITS(e)
} while ((e = (t = t->v.t + ((cab_ULONG)b & Zipmask[e]))->e) > 16);
ZIPDUMPBITS(t->b)
if (e == 16) /* then it's a literal */
CAB(outbuf)[w++] = (cab_UBYTE)t->v.n;
else /* it's an EOB or a length */
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -