📄 explode.c
字号:
{ if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0) { if (r == 1) huft_free(td); huft_free(tl); huft_free(tb); return r; } r = explode_lit4(tb, tl, td, bb, bl, bd); } huft_free(td); huft_free(tl); huft_free(tb); } else /* No literal tree--minimum match length is 2 */ { if ((r = get_tree(l, 64)) != 0) return r; if ((r = huft_build(l, 64, 0, cplen2, extra, &tl, &bl)) != 0) { if (r == 1) huft_free(tl); return r; } if ((r = get_tree(l, 64)) != 0) return r; if (pUnzip->cur_file_info.flag & 2) /* true if 8K */ { if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0) { if (r == 1) huft_free(td); huft_free(tl); return r; } r = explode_nolit8(tl, td, bl, bd); } else /* else 4K */ { if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0) { if (r == 1) huft_free(td); huft_free(tl); return r; } r = explode_nolit4(tl, td, bl, bd); } huft_free(td); huft_free(tl); }#ifdef DEBUG fprintf(stderr, "<%u > ", hufts);#endif /* DEBUG */ return r;}int ReadByte(x) UWORD *x;{ /* * read a byte; return 8 if byte available, 0 if not */ if (pfile_in_zip_read_info->stream.avail_in == 0) { unsigned int uReadThis = UNZ_BUFSIZE; if (pfile_in_zip_read_info->rest_read_compressed <= 0) return (0); if (pfile_in_zip_read_info->rest_read_compressed < uReadThis) uReadThis = (uInt) pfile_in_zip_read_info->rest_read_compressed; if (uReadThis == 0) return UNZ_EOF; if (fseek (pfile_in_zip_read_info->file, pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile, SEEK_SET) != 0) return UNZ_ERRNO; if (fread (pfile_in_zip_read_info->read_buffer, uReadThis, 1, pfile_in_zip_read_info->file) != 1) return UNZ_ERRNO; pfile_in_zip_read_info->pos_in_zipfile += uReadThis; pfile_in_zip_read_info->rest_read_compressed -= uReadThis; pfile_in_zip_read_info->stream.next_in = (Bytef *) pfile_in_zip_read_info->read_buffer; pfile_in_zip_read_info->stream.avail_in = (uInt) uReadThis; } *x = *pfile_in_zip_read_info->stream.next_in++; pfile_in_zip_read_info->stream.avail_in--; return 8;}/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */#define BMAX 16 /* maximum bit length of any code (16 for explode) */#define N_MAX 288 /* maximum number of codes in any set */unsigned hufts; /* track memory usage */int huft_build(b, n, s, d, e, t, m)unsigned *b; /* code lengths in bits (all assumed <= BMAX) */unsigned n; /* number of codes (assumed <= N_MAX) */unsigned s; /* number of simple-valued codes (0..s-1) */ush *d; /* list of base values for non-simple codes */ush *e; /* list of extra bits for non-simple codes */struct huft **t; /* result: starting table */int *m; /* maximum lookup bits, returns actual *//* Given a list of code lengths and a maximum table size, make a set of tables to decode that set of codes. Return zero on success, one if the given code set is incomplete (the tables are still built in this case), two if the input is invalid (all zero length codes or an oversubscribed set of lengths), and three if not enough memory. */{ unsigned a; /* counter for codes of length k */ unsigned c[BMAX+1]; /* bit length count table */ unsigned f; /* i repeats in table every f entries */ int g; /* maximum code length */ int h; /* table level */ register unsigned i; /* counter, current code */ register unsigned j; /* counter */ register int k; /* number of bits in current code */ int l; /* bits per table (returned in m) */ register unsigned *p; /* pointer into c[], b[], or v[] */ register struct huft *q; /* points to current table */ struct huft r; /* table entry for structure assignment */ struct huft *u[BMAX]; /* table stack */ unsigned v[N_MAX]; /* values in order of bit length */ register int w; /* bits before this table == (l * h) */ unsigned x[BMAX+1]; /* bit offsets, then code stack */ unsigned *xp; /* pointer into x */ int y; /* number of dummy codes added */ unsigned z; /* number of entries in current table */ /* Generate counts for each bit length */ memset(c, 0, sizeof(c)); p = b; i = n; do { c[*p++]++; /* assume all entries <= BMAX */ } while (--i); if (c[0] == n) /* null input--all zero length codes */ { *t = (struct huft *)NULL; *m = 0; return 0; } /* Find minimum and maximum length, bound *m by those */ l = *m; for (j = 1; j <= BMAX; j++) if (c[j]) break; k = j; /* minimum code length */ if ((unsigned)l < j) l = j; for (i = BMAX; i; i--) if (c[i]) break; g = i; /* maximum code length */ if ((unsigned)l > i) l = i; *m = l; /* Adjust last length count to fill out codes, if needed */ for (y = 1 << j; j < i; j++, y <<= 1) if ((y -= c[j]) < 0) return 2; /* bad input: more codes than bits */ if ((y -= c[i]) < 0) return 2; c[i] += y; /* Generate starting offsets into the value table for each length */ x[1] = j = 0; p = c + 1; xp = 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) v[x[j]++] = i; } while (++i < n); /* Generate the Huffman codes and for each, make the table entries */ x[0] = i = 0; /* first Huffman code is zero */ p = v; /* grab values in bit order */ h = -1; /* no tables yet--level -1 */ w = -l; /* bits decoded == (l * h) */ u[0] = (struct huft *)NULL; /* just to keep compilers happy */ q = (struct huft *)NULL; /* ditto */ z = 0; /* ditto */ /* go through the bit lengths (k already is bits in shortest code) */ for (; k <= g; k++) { a = 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; /* previous table always l bits */ /* compute minimum size table less than or equal to l bits */ z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */ 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 = 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 */ } } z = 1 << j; /* table entries for j-bit table */ /* allocate and link in new table */ if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == (struct huft *)NULL) { if (h) huft_free(u[0]); return 3; /* not enough memory */ } hufts += z + 1; /* track memory usage */ *t = q + 1; /* link to list for huft_free() */ *(t = &(q->v.t)) = (struct huft *)NULL; u[h] = ++q; /* table starts after link */ /* connect to last table, if there is one */ if (h) { x[h] = i; /* save pattern for backing up */ r.b = (uch)l; /* bits to dump before this table */ r.e = (uch)(16 + j); /* bits in this table */ r.v.t = q; /* pointer to this table */ j = i >> (w - l); /* (get around Turbo C bug) */ u[h-1][j] = r; /* connect to last table */ } } /* set up table entry in r */ r.b = (uch)(k - w); if (p >= v + n) r.e = 99; /* out of values--invalid code */ else if (*p < s) { r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */ r.v.n = *p++; /* simple code is just the value */ } else { r.e = (uch)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)) != x[h]) { h--; /* don't need to update q */ w -= l; } } } /* Return true (1) if we were given an incomplete table */ return y != 0 && g != 1;}int huft_free(t)struct huft *t; /* table to free *//* Free the malloc'ed tables built by huft_build(), which makes a linked list of the tables it made, with the links in a dummy first entry of each table. */{ register struct huft *p, *q; /* Go through linked list, freeing from the malloced (t[-1]) address. */ p = t; while (p != (struct huft *)NULL) { q = (--p)->v.t; free(p); p = q; } return 0;}void flush(w)unsigned w; /* number of bytes to flush *//* Do the equivalent of OUTB for the bytes slide[0..w-1]. */{ memmove (pfile_in_zip_read_info->stream.next_out, slide, w); pfile_in_zip_read_info->crc32 = crc32 (pfile_in_zip_read_info->crc32, pfile_in_zip_read_info->stream.next_out, w); pfile_in_zip_read_info->stream.next_out += w; pfile_in_zip_read_info->stream.avail_out -= w; pfile_in_zip_read_info->stream.total_out += w;}void flush_stack(w)unsigned w; /* number of bytes to flush *//* Do the equivalent of OUTB for the bytes slide[0..w-1]. */{ memmove (pfile_in_zip_read_info->stream.next_out, stack, w); pfile_in_zip_read_info->crc32 = crc32 (pfile_in_zip_read_info->crc32, pfile_in_zip_read_info->stream.next_out, w); pfile_in_zip_read_info->stream.next_out += w; pfile_in_zip_read_info->stream.avail_out -= w; pfile_in_zip_read_info->stream.total_out += w;}/****************************//* Function FillBitBuffer() *//****************************/int FillBitBuffer(){ /* * Fill bitbuf, which is 32 bits. This function is only used by the * READBIT and PEEKBIT macros (which are used by all of the uncompression * routines). */ UWORD temp; zipeof = 1; while (bits_left < 25 && ReadByte(&temp) == 8) { bitbuf |= (ULONG)temp << bits_left; bits_left += 8; zipeof = 0; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -