📄 xzip.cpp
字号:
// levels >= 4.
unsigned good_match;
// Use a faster search when the previous match is longer than this
int nice_match; // Stop searching when current match exceeds this
};
typedef struct iztimes {
time_t atime,mtime,ctime;
} iztimes; // access, modify, create times
typedef struct zlist {
ush vem, ver, flg, how; // See central header in zipfile.c for what vem..off are
ulg tim, crc, siz, len;
extent nam, ext, cext, com; // offset of ext must be >= LOCHEAD
ush dsk, att, lflg; // offset of lflg must be >= LOCHEAD
ulg atx, off;
char name[MAX_PATH]; // File name in zip file
char *extra; // Extra field (set only if ext != 0)
char *cextra; // Extra in central (set only if cext != 0)
char *comment; // Comment (set only if com != 0)
char iname[MAX_PATH]; // Internal file name after cleanup
char zname[MAX_PATH]; // External version of internal name
int mark; // Marker for files to operate on
int trash; // Marker for files to delete
int dosflag; // Set to force MSDOS file attributes
struct zlist far *nxt; // Pointer to next header in list
} TZipFileInfo;
class TState;
typedef unsigned (*READFUNC)(TState &state, char *buf,unsigned size);
typedef unsigned (*FLUSHFUNC)(void *param, const char *buf, unsigned *size);
typedef unsigned (*WRITEFUNC)(void *param, const char *buf, unsigned size);
class TState
{
public:
TState() //+++1.2
{
param = 0;
level = 0;
seekable = FALSE;
readfunc = 0;
flush_outbuf = 0;
err = 0;
}
void *param;
int level;
bool seekable;
READFUNC readfunc;
FLUSHFUNC flush_outbuf;
TTreeState ts;
TBitState bs;
TDeflateState ds;
const char *err;
};
void Assert(TState &state,bool cond, const char *msg)
{ if (cond) return;
state.err=msg;
}
void __cdecl Trace(const char *x, ...) {va_list paramList; va_start(paramList, x); paramList; va_end(paramList);}
void __cdecl Tracec(bool ,const char *x, ...) {va_list paramList; va_start(paramList, x); paramList; va_end(paramList);}
// ===========================================================================
// Local (static) routines in this file.
//
void init_block (TState &);
void pqdownheap (TState &,ct_data *tree, int k);
void gen_bitlen (TState &,tree_desc *desc);
void gen_codes (TState &state,ct_data *tree, int max_code);
void build_tree (TState &,tree_desc *desc);
void scan_tree (TState &,ct_data *tree, int max_code);
void send_tree (TState &state,ct_data *tree, int max_code);
int build_bl_tree (TState &);
void send_all_trees (TState &state,int lcodes, int dcodes, int blcodes);
void compress_block (TState &state,ct_data *ltree, ct_data *dtree);
void set_file_type (TState &);
void send_bits (TState &state, int value, int length);
unsigned bi_reverse (unsigned code, int len);
void bi_windup (TState &state);
void copy_block (TState &state,char *buf, unsigned len, int header);
#define send_code(state, c, tree) send_bits(state, tree[c].fc.code, tree[c].dl.len)
// Send a code of the given tree. c and tree must not have side effects
// alternatively...
//#define send_code(state, c, tree)
// { if (state.verbose>1) fprintf(stderr,"\ncd %3d ",(c));
// send_bits(state, tree[c].fc.code, tree[c].dl.len); }
#define d_code(dist) ((dist) < 256 ? state.ts.dist_code[dist] : state.ts.dist_code[256+((dist)>>7)])
// Mapping from a distance to a distance code. dist is the distance - 1 and
// must not have side effects. dist_code[256] and dist_code[257] are never used.
#define Max(a,b) (a >= b ? a : b)
/* the arguments must not have side effects */
/* ===========================================================================
* Allocate the match buffer, initialize the various tables and save the
* location of the internal file attribute (ascii/binary) and method
* (DEFLATE/STORE).
*/
void ct_init(TState &state, ush *attr)
{
int n; /* iterates over tree elements */
int bits; /* bit counter */
int length; /* length value */
int code; /* code value */
int dist; /* distance index */
state.ts.file_type = attr;
//state.ts.file_method = method;
state.ts.cmpr_bytelen = state.ts.cmpr_len_bits = 0L;
state.ts.input_len = 0L;
if (state.ts.static_dtree[0].dl.len != 0) return; /* ct_init already called */
/* Initialize the mapping length (0..255) -> length code (0..28) */
length = 0;
for (code = 0; code < LENGTH_CODES-1; code++) {
state.ts.base_length[code] = length;
for (n = 0; n < (1<<extra_lbits[code]); n++) {
state.ts.length_code[length++] = (uch)code;
}
}
Assert(state,length == 256, "ct_init: length != 256");
/* Note that the length 255 (match length 258) can be represented
* in two different ways: code 284 + 5 bits or code 285, so we
* overwrite length_code[255] to use the best encoding:
*/
state.ts.length_code[length-1] = (uch)code;
/* Initialize the mapping dist (0..32K) -> dist code (0..29) */
dist = 0;
for (code = 0 ; code < 16; code++) {
state.ts.base_dist[code] = dist;
for (n = 0; n < (1<<extra_dbits[code]); n++) {
state.ts.dist_code[dist++] = (uch)code;
}
}
Assert(state,dist == 256, "ct_init: dist != 256");
dist >>= 7; /* from now on, all distances are divided by 128 */
for ( ; code < D_CODES; code++) {
state.ts.base_dist[code] = dist << 7;
for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
state.ts.dist_code[256 + dist++] = (uch)code;
}
}
Assert(state,dist == 256, "ct_init: 256+dist != 512");
/* Construct the codes of the static literal tree */
for (bits = 0; bits <= MAX_BITS; bits++) state.ts.bl_count[bits] = 0;
n = 0;
while (n <= 143) state.ts.static_ltree[n++].dl.len = 8, state.ts.bl_count[8]++;
while (n <= 255) state.ts.static_ltree[n++].dl.len = 9, state.ts.bl_count[9]++;
while (n <= 279) state.ts.static_ltree[n++].dl.len = 7, state.ts.bl_count[7]++;
while (n <= 287) state.ts.static_ltree[n++].dl.len = 8, state.ts.bl_count[8]++;
/* fc.codes 286 and 287 do not exist, but we must include them in the
* tree construction to get a canonical Huffman tree (longest code
* all ones)
*/
gen_codes(state,(ct_data *)state.ts.static_ltree, L_CODES+1);
/* The static distance tree is trivial: */
for (n = 0; n < D_CODES; n++) {
state.ts.static_dtree[n].dl.len = 5;
state.ts.static_dtree[n].fc.code = (ush)bi_reverse(n, 5);
}
/* Initialize the first block of the first file: */
init_block(state);
}
/* ===========================================================================
* Initialize a new block.
*/
void init_block(TState &state)
{
int n; /* iterates over tree elements */
/* Initialize the trees. */
for (n = 0; n < L_CODES; n++) state.ts.dyn_ltree[n].fc.freq = 0;
for (n = 0; n < D_CODES; n++) state.ts.dyn_dtree[n].fc.freq = 0;
for (n = 0; n < BL_CODES; n++) state.ts.bl_tree[n].fc.freq = 0;
state.ts.dyn_ltree[END_BLOCK].fc.freq = 1;
state.ts.opt_len = state.ts.static_len = 0L;
state.ts.last_lit = state.ts.last_dist = state.ts.last_flags = 0;
state.ts.flags = 0; state.ts.flag_bit = 1;
}
#define SMALLEST 1
/* Index within the heap array of least frequent node in the Huffman tree */
/* ===========================================================================
* Remove the smallest element from the heap and recreate the heap with
* one less element. Updates heap and heap_len.
*/
#define pqremove(tree, top) \
{\
top = state.ts.heap[SMALLEST]; \
state.ts.heap[SMALLEST] = state.ts.heap[state.ts.heap_len--]; \
pqdownheap(state,tree, SMALLEST); \
}
/* ===========================================================================
* Compares to subtrees, using the tree depth as tie breaker when
* the subtrees have equal frequency. This minimizes the worst case length.
*/
#define smaller(tree, n, m) \
(tree[n].fc.freq < tree[m].fc.freq || \
(tree[n].fc.freq == tree[m].fc.freq && state.ts.depth[n] <= state.ts.depth[m]))
/* ===========================================================================
* Restore the heap property by moving down the tree starting at node k,
* exchanging a node with the smallest of its two sons if necessary, stopping
* when the heap property is re-established (each father smaller than its
* two sons).
*/
void pqdownheap(TState &state,ct_data *tree, int k)
{
int v = state.ts.heap[k];
int j = k << 1; /* left son of k */
int htemp; /* required because of bug in SASC compiler */
while (j <= state.ts.heap_len) {
/* Set j to the smallest of the two sons: */
if (j < state.ts.heap_len && smaller(tree, state.ts.heap[j+1], state.ts.heap[j])) j++;
/* Exit if v is smaller than both sons */
htemp = state.ts.heap[j];
if (smaller(tree, v, htemp)) break;
/* Exchange v with the smallest son */
state.ts.heap[k] = htemp;
k = j;
/* And continue down the tree, setting j to the left son of k */
j <<= 1;
}
state.ts.heap[k] = v;
}
/* ===========================================================================
* Compute the optimal bit lengths for a tree and update the total bit length
* for the current block.
* IN assertion: the fields freq and dad are set, heap[heap_max] and
* above are the tree nodes sorted by increasing frequency.
* OUT assertions: the field len is set to the optimal bit length, the
* array bl_count contains the frequencies for each bit length.
* The length opt_len is updated; static_len is also updated if stree is
* not null.
*/
void gen_bitlen(TState &state,tree_desc *desc)
{
ct_data *tree = desc->dyn_tree;
const int *extra = desc->extra_bits;
int base = desc->extra_base;
int max_code = desc->max_code;
int max_length = desc->max_length;
ct_data *stree = desc->static_tree;
int h; /* heap index */
int n, m; /* iterate over the tree elements */
int bits; /* bit length */
int xbits; /* extra bits */
ush f; /* frequency */
int overflow = 0; /* number of elements with bit length too large */
for (bits = 0; bits <= MAX_BITS; bits++) state.ts.bl_count[bits] = 0;
/* In a first pass, compute the optimal bit lengths (which may
* overflow in the case of the bit length tree).
*/
tree[state.ts.heap[state.ts.heap_max]].dl.len = 0; /* root of the heap */
for (h = state.ts.heap_max+1; h < HEAP_SIZE; h++) {
n = state.ts.heap[h];
bits = tree[tree[n].dl.dad].dl.len + 1;
if (bits > max_length) bits = max_length, overflow++;
tree[n].dl.len = (ush)bits;
/* We overwrite tree[n].dl.dad which is no longer needed */
if (n > max_code) continue; /* not a leaf node */
state.ts.bl_count[bits]++;
xbits = 0;
if (n >= base) xbits = extra[n-base];
f = tree[n].fc.freq;
state.ts.opt_len += (ulg)f * (bits + xbits);
if (stree) state.ts.static_len += (ulg)f * (stree[n].dl.len + xbits);
}
if (overflow == 0) return;
Trace("\nbit length overflow\n");
/* This happens for example on obj2 and pic of the Calgary corpus */
/* Find the first bit length which could increase: */
do {
bits = max_length-1;
while (state.ts.bl_count[bits] == 0) bits--;
state.ts.bl_count[bits]--; /* move one leaf down the tree */
state.ts.bl_count[bits+1] += (ush)2; /* move one overflow item as its brother */
state.ts.bl_count[max_length]--;
/* The brother of the overflow item also moves one step up,
* but this does not affect bl_count[max_length]
*/
overflow -= 2;
} while (overflow > 0);
/* Now recompute all bit lengths, scanning in increasing frequency.
* h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
* lengths instead of fixing only the wrong ones. This idea is taken
* from 'ar' written by Haruhiko Okumura.)
*/
for (bits = max_length; bits != 0; bits--) {
n = state.ts.bl_count[bits];
while (n != 0) {
m = state.ts.heap[--h];
if (m > max_code) continue;
if (tree[m].dl.len != (ush)bits) {
Trace("code %d bits %d->%d\n", m, tree[m].dl.len, bits);
state.ts.opt_len += ((long)bits-(long)tree[m].dl.len)*(long)tree[m].fc.freq;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -