| 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 |
#include "deflate.h" |
| 34 |
|
| 35 |
#ifdef ZLIB_DEBUG |
| 36 |
# include <ctype.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#define MAX_BL_BITS 7 |
| 44 |
|
| 45 |
|
| 46 |
#define END_BLOCK 256 |
| 47 |
|
| 48 |
|
| 49 |
#define REP_3_6 16 |
| 50 |
|
| 51 |
|
| 52 |
#define REPZ_3_10 17 |
| 53 |
|
| 54 |
|
| 55 |
#define REPZ_11_138 18 |
| 56 |
|
| 57 |
|
| 58 |
local const int extra_lbits[LENGTH_CODES] |
| 59 |
= {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,16,0}; |
| 60 |
|
| 61 |
|
| 62 |
local const int extra_dbits[D_CODES] |
| 63 |
= {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14}; |
| 64 |
|
| 65 |
|
| 66 |
local const int extra_blbits[BL_CODES] |
| 67 |
= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; |
| 68 |
|
| 69 |
local const uch bl_order[BL_CODES] |
| 70 |
= {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#define DIST_CODE_LEN 768 |
| 80 |
|
| 81 |
local int base_length[LENGTH_CODES]; |
| 82 |
|
| 83 |
|
| 84 |
uch length_code[BASE_MATCH-MIN_MATCH+1]; |
| 85 |
|
| 86 |
|
| 87 |
#if defined(GEN_TREES_H) || !defined(STDC) |
| 88 |
|
| 89 |
|
| 90 |
local ct_data static_ltree[L_CODES+2]; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
local ct_data static_dtree[D_CODES]; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
uch _dist_code[DIST_CODE_LEN]; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
local int base_dist[D_CODES]; |
| 109 |
|
| 110 |
|
| 111 |
#else |
| 112 |
# include "deflate_tree.h" |
| 113 |
#endif |
| 114 |
|
| 115 |
struct static_tree_desc_s { |
| 116 |
const ct_data *static_tree; |
| 117 |
const int FAR *extra_bits; |
| 118 |
int extra_base; |
| 119 |
int elems; |
| 120 |
int max_length; |
| 121 |
}; |
| 122 |
|
| 123 |
local const static_tree_desc static_l_desc = |
| 124 |
{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; |
| 125 |
|
| 126 |
local const static_tree_desc static_d_desc = |
| 127 |
{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; |
| 128 |
|
| 129 |
local const static_tree_desc static_bl_desc = |
| 130 |
{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
local void tr_static_init OF((void)); |
| 137 |
local void init_block OF((deflate_state *s)); |
| 138 |
local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); |
| 139 |
local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); |
| 140 |
local void gen_codes OF((ct_data *tree, int max_code, ush FAR *bl_count)); |
| 141 |
local void build_tree OF((deflate_state *s, tree_desc *desc)); |
| 142 |
local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); |
| 143 |
local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); |
| 144 |
local int build_bl_tree OF((deflate_state *s)); |
| 145 |
local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, |
| 146 |
int blcodes)); |
| 147 |
local void compress_block OF((deflate_state *s, const ct_data *ltree, |
| 148 |
const ct_data *dtree)); |
| 149 |
local int detect_data_type OF((deflate_state *s)); |
| 150 |
local unsigned bi_reverse OF((unsigned code, int len)); |
| 151 |
local void bi_windup OF((deflate_state *s)); |
| 152 |
local void bi_flush OF((deflate_state *s)); |
| 153 |
|
| 154 |
#ifdef GEN_TREES_H |
| 155 |
local void gen_trees_header OF((void)); |
| 156 |
#endif |
| 157 |
|
| 158 |
#ifndef ZLIB_DEBUG |
| 159 |
# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) |
| 160 |
|
| 161 |
|
| 162 |
#else |
| 163 |
# define send_code(s, c, tree) \ |
| 164 |
{ if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ |
| 165 |
send_bits(s, tree[c].Code, tree[c].Len); } |
| 166 |
#endif |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
#define put_short(s, w) { \ |
| 173 |
put_byte(s, (uch)((w) & 0xff)); \ |
| 174 |
put_byte(s, (uch)((ush)(w) >> 8)); \ |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
#ifdef ZLIB_DEBUG |
| 182 |
local void send_bits OF((deflate_state *s, int value, int length)); |
| 183 |
|
| 184 |
local void send_bits(s, value, length) |
| 185 |
deflate_state *s; |
| 186 |
int value; |
| 187 |
int length; |
| 188 |
{ |
| 189 |
Tracevv((stderr," l %2d v %4x ", length, value)); |
| 190 |
Assert(length > 0 && length <= 16, "invalid length"); |
| 191 |
s->bits_sent += (ulg)length; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if (s->bi_valid > (int)Buf_size - length) { |
| 198 |
s->bi_buf |= (ush)value << s->bi_valid; |
| 199 |
put_short(s, s->bi_buf); |
| 200 |
s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); |
| 201 |
s->bi_valid += length - Buf_size; |
| 202 |
} else { |
| 203 |
s->bi_buf |= (ush)value << s->bi_valid; |
| 204 |
s->bi_valid += length; |
| 205 |
} |
| 206 |
} |
| 207 |
#else |
| 208 |
|
| 209 |
#define send_bits(s, value, length) \ |
| 210 |
{ int len = length;\ |
| 211 |
if (s->bi_valid > (int)Buf_size - len) {\ |
| 212 |
int val = (int)value;\ |
| 213 |
s->bi_buf |= (ush)val << s->bi_valid;\ |
| 214 |
put_short(s, s->bi_buf);\ |
| 215 |
s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ |
| 216 |
s->bi_valid += len - Buf_size;\ |
| 217 |
} else {\ |
| 218 |
s->bi_buf |= (ush)(value) << s->bi_valid;\ |
| 219 |
s->bi_valid += len;\ |
| 220 |
}\ |
| 221 |
} |
| 222 |
#endif |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
local void tr_static_init() |
| 231 |
{ |
| 232 |
|
| 233 |
#if defined(GEN_TREES_H) || !defined(STDC) |
| 234 |
static int static_init_done = 0; |
| 235 |
int n; |
| 236 |
int bits; |
| 237 |
int length; |
| 238 |
int code; |
| 239 |
int dist; |
| 240 |
ush bl_count[MAX_BITS+1]; |
| 241 |
|
| 242 |
|
| 243 |
if (static_init_done) return; |
| 244 |
|
| 245 |
|
| 246 |
#ifdef NO_INIT_GLOBAL_POINTERS |
| 247 |
static_l_desc.static_tree = static_ltree; |
| 248 |
static_l_desc.extra_bits = extra_lbits; |
| 249 |
static_d_desc.static_tree = static_dtree; |
| 250 |
static_d_desc.extra_bits = extra_dbits; |
| 251 |
static_bl_desc.extra_bits = extra_blbits; |
| 252 |
#endif |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
length = 0; |
| 257 |
for (code = 0; code < LENGTH_CODES-2; code++) { |
| 258 |
base_length[code] = length; |
| 259 |
for (n = 0; n < (1<<extra_lbits[code]); n++) { |
| 260 |
length_code[length++] = (uch)code; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
base_length[LENGTH_CODES-2] = 0; |
| 265 |
Assert (length == BASE_MATCH-MINMATCH+1, "tr_static_init: length != 256"); |
| 266 |
|
| 267 |
|
| 268 |
dist = 0; |
| 269 |
for (code = 0 ; code < 16; code++) { |
| 270 |
base_dist[code] = dist; |
| 271 |
for (n = 0; n < (1<<extra_dbits[code]); n++) { |
| 272 |
_dist_code[dist++] = (uch)code; |
| 273 |
} |
| 274 |
} |
| 275 |
Assert (dist == 256, "tr_static_init: dist != 256"); |
| 276 |
dist >>= 7; |
| 277 |
for ( ; code < D_CODES; code++) { |
| 278 |
base_dist[code] = dist << 7; |
| 279 |
for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { |
| 280 |
_dist_code[256 + dist++] = (uch)code; |
| 281 |
} |
| 282 |
} |
| 283 |
Assert (dist == 512, "tr_static_init: 256+dist != 768"); |
| 284 |
|
| 285 |
|
| 286 |
for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; |
| 287 |
n = 0; |
| 288 |
while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; |
| 289 |
while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; |
| 290 |
while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; |
| 291 |
while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); |
| 297 |
|
| 298 |
|
| 299 |
for (n = 0; n < D_CODES; n++) { |
| 300 |
static_dtree[n].Len = 5; |
| 301 |
static_dtree[n].Code = bi_reverse((unsigned)n, 5); |
| 302 |
} |
| 303 |
static_init_done = 1; |
| 304 |
|
| 305 |
#endif |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
#ifdef GEN_TREES_H |
| 312 |
# ifndef ZLIB_DEBUG |
| 313 |
# include <stdio.h> |
| 314 |
# endif |
| 315 |
|
| 316 |
# define SEPARATOR(i, last, width) \ |
| 317 |
((i) == (last)? "\n};\n\n" : \ |
| 318 |
((i) % (width) == (width)-1 ? ",\n" : ", ")) |
| 319 |
|
| 320 |
void make_deflate_tree() |
| 321 |
{ |
| 322 |
FILE *header = fopen("deflate_tree.h", "w"); |
| 323 |
int i; |
| 324 |
|
| 325 |
tr_static_init(); |
| 326 |
|
| 327 |
Assert (header != NULL, "Can't open trees.h"); |
| 328 |
fprintf(header, |
| 329 |
"/* header created automatically with -DGEN_TREES_H */\n\n"); |
| 330 |
|
| 331 |
fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); |
| 332 |
for (i = 0; i < L_CODES+2; i++) { |
| 333 |
fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, |
| 334 |
static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); |
| 335 |
} |
| 336 |
|
| 337 |
fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); |
| 338 |
for (i = 0; i < D_CODES; i++) { |
| 339 |
fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, |
| 340 |
static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); |
| 341 |
} |
| 342 |
|
| 343 |
fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n"); |
| 344 |
for (i = 0; i < DIST_CODE_LEN; i++) { |
| 345 |
fprintf(header, "%2u%s", _dist_code[i], |
| 346 |
SEPARATOR(i, DIST_CODE_LEN-1, 20)); |
| 347 |
} |
| 348 |
|
| 349 |
fprintf(header, |
| 350 |
"const uch ZLIB_INTERNAL length_code[BASE_MATCH-MIN_MATCH+1]= {\n"); |
| 351 |
for (i = 0; i < BASE_MATCH-MIN_MATCH+1; i++) { |
| 352 |
fprintf(header, "%2u%s", length_code[i], |
| 353 |
SEPARATOR(i, BASE_MATCH-MIN_MATCH, 20)); |
| 354 |
} |
| 355 |
|
| 356 |
fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); |
| 357 |
for (i = 0; i < LENGTH_CODES; i++) { |
| 358 |
fprintf(header, "%1u%s", base_length[i], |
| 359 |
SEPARATOR(i, LENGTH_CODES-1, 20)); |
| 360 |
} |
| 361 |
|
| 362 |
fprintf(header, "local const int base_dist[D_CODES] = {\n"); |
| 363 |
for (i = 0; i < D_CODES; i++) { |
| 364 |
fprintf(header, "%5u%s", base_dist[i], |
| 365 |
SEPARATOR(i, D_CODES-1, 10)); |
| 366 |
} |
| 367 |
|
| 368 |
fclose(header); |
| 369 |
} |
| 370 |
#endif |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
void ZLIB_INTERNAL _tr_init(s) |
| 376 |
deflate_state *s; |
| 377 |
{ |
| 378 |
tr_static_init(); |
| 379 |
|
| 380 |
s->l_desc.dyn_tree = s->dyn_ltree; |
| 381 |
s->l_desc.stat_desc = &static_l_desc; |
| 382 |
|
| 383 |
s->d_desc.dyn_tree = s->dyn_dtree; |
| 384 |
s->d_desc.stat_desc = &static_d_desc; |
| 385 |
|
| 386 |
s->bl_desc.dyn_tree = s->bl_tree; |
| 387 |
s->bl_desc.stat_desc = &static_bl_desc; |
| 388 |
|
| 389 |
s->bi_buf = 0; |
| 390 |
s->bi_valid = 0; |
| 391 |
#ifdef ZLIB_DEBUG |
| 392 |
s->compressed_len = 0L; |
| 393 |
s->bits_sent = 0L; |
| 394 |
#endif |
| 395 |
|
| 396 |
|
| 397 |
init_block(s); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
local void init_block(s) |
| 404 |
deflate_state *s; |
| 405 |
{ |
| 406 |
int n; |
| 407 |
|
| 408 |
|
| 409 |
for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; |
| 410 |
for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; |
| 411 |
for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; |
| 412 |
|
| 413 |
s->dyn_ltree[END_BLOCK].Freq = 1; |
| 414 |
s->opt_len = s->static_len = 0L; |
| 415 |
s->sym_next = s->matches = 0; |
| 416 |
} |
| 417 |
|
| 418 |
#define SMALLEST 1 |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
#define pqremove(s, tree, top) \ |
| 427 |
{\ |
| 428 |
top = s->heap[SMALLEST]; \ |
| 429 |
s->heap[SMALLEST] = s->heap[s->heap_len--]; \ |
| 430 |
pqdownheap(s, tree, SMALLEST); \ |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
#define smaller(tree, n, m, depth) \ |
| 438 |
(tree[n].Freq < tree[m].Freq || \ |
| 439 |
(tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
local void pqdownheap(s, tree, k) |
| 448 |
deflate_state *s; |
| 449 |
ct_data *tree; |
| 450 |
int k; |
| 451 |
{ |
| 452 |
int v = s->heap[k]; |
| 453 |
int j = k << 1; |
| 454 |
while (j <= s->heap_len) { |
| 455 |
|
| 456 |
if (j < s->heap_len && |
| 457 |
smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { |
| 458 |
j++; |
| 459 |
} |
| 460 |
|
| 461 |
if (smaller(tree, v, s->heap[j], s->depth)) break; |
| 462 |
|
| 463 |
|
| 464 |
s->heap[k] = s->heap[j]; k = j; |
| 465 |
|
| 466 |
|
| 467 |
j <<= 1; |
| 468 |
} |
| 469 |
s->heap[k] = v; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
local void gen_bitlen(s, desc) |
| 483 |
deflate_state *s; |
| 484 |
tree_desc *desc; |
| 485 |
{ |
| 486 |
ct_data *tree = desc->dyn_tree; |
| 487 |
int max_code = desc->max_code; |
| 488 |
const ct_data *stree = desc->stat_desc->static_tree; |
| 489 |
const int FAR *extra = desc->stat_desc->extra_bits; |
| 490 |
int base = desc->stat_desc->extra_base; |
| 491 |
int max_length = desc->stat_desc->max_length; |
| 492 |
int h; |
| 493 |
int n, m; |
| 494 |
int bits; |
| 495 |
int xbits; |
| 496 |
ush f; |
| 497 |
int overflow = 0; |
| 498 |
|
| 499 |
for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
tree[s->heap[s->heap_max]].Len = 0; |
| 505 |
|
| 506 |
for (h = s->heap_max+1; h < HEAP_SIZE; h++) { |
| 507 |
n = s->heap[h]; |
| 508 |
bits = tree[tree[n].Dad].Len + 1; |
| 509 |
if (bits > max_length) bits = max_length, overflow++; |
| 510 |
tree[n].Len = (ush)bits; |
| 511 |
|
| 512 |
|
| 513 |
if (n > max_code) continue; |
| 514 |
|
| 515 |
s->bl_count[bits]++; |
| 516 |
xbits = 0; |
| 517 |
if (n >= base) xbits = extra[n-base]; |
| 518 |
f = tree[n].Freq; |
| 519 |
s->opt_len += (ulg)f * (unsigned)(bits + xbits); |
| 520 |
if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits); |
| 521 |
} |
| 522 |
if (overflow == 0) return; |
| 523 |
|
| 524 |
Tracev((stderr,"\nbit length overflow\n")); |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
do { |
| 529 |
bits = max_length-1; |
| 530 |
while (s->bl_count[bits] == 0) bits--; |
| 531 |
s->bl_count[bits]--; |
| 532 |
s->bl_count[bits+1] += 2; |
| 533 |
s->bl_count[max_length]--; |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
overflow -= 2; |
| 538 |
} while (overflow > 0); |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
for (bits = max_length; bits != 0; bits--) { |
| 546 |
n = s->bl_count[bits]; |
| 547 |
while (n != 0) { |
| 548 |
m = s->heap[--h]; |
| 549 |
if (m > max_code) continue; |
| 550 |
if ((unsigned) tree[m].Len != (unsigned) bits) { |
| 551 |
Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); |
| 552 |
s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq; |
| 553 |
tree[m].Len = (ush)bits; |
| 554 |
} |
| 555 |
n--; |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
local void gen_codes (tree, max_code, bl_count) |
| 569 |
ct_data *tree; |
| 570 |
int max_code; |
| 571 |
ush FAR *bl_count; |
| 572 |
{ |
| 573 |
ush next_code[MAX_BITS+1]; |
| 574 |
unsigned code = 0; |
| 575 |
int bits; |
| 576 |
int n; |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
for (bits = 1; bits <= MAX_BITS; bits++) { |
| 582 |
code = (code + bl_count[bits-1]) << 1; |
| 583 |
next_code[bits] = (ush)code; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1, |
| 589 |
"inconsistent bit counts"); |
| 590 |
Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); |
| 591 |
|
| 592 |
for (n = 0; n <= max_code; n++) { |
| 593 |
int len = tree[n].Len; |
| 594 |
if (len == 0) continue; |
| 595 |
|
| 596 |
tree[n].Code = (ush)bi_reverse(next_code[len]++, len); |
| 597 |
|
| 598 |
Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %04x (%x) ", |
| 599 |
n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1)); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
local void build_tree(s, desc) |
| 612 |
deflate_state *s; |
| 613 |
tree_desc *desc; |
| 614 |
{ |
| 615 |
ct_data *tree = desc->dyn_tree; |
| 616 |
const ct_data *stree = desc->stat_desc->static_tree; |
| 617 |
int elems = desc->stat_desc->elems; |
| 618 |
int n, m; |
| 619 |
int max_code = -1; |
| 620 |
int node; |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
s->heap_len = 0, s->heap_max = HEAP_SIZE; |
| 627 |
|
| 628 |
for (n = 0; n < elems; n++) { |
| 629 |
if (tree[n].Freq != 0) { |
| 630 |
s->heap[++(s->heap_len)] = max_code = n; |
| 631 |
s->depth[n] = 0; |
| 632 |
} else { |
| 633 |
tree[n].Len = 0; |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
while (s->heap_len < 2) { |
| 643 |
node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); |
| 644 |
tree[node].Freq = 1; |
| 645 |
s->depth[node] = 0; |
| 646 |
s->opt_len--; if (stree) s->static_len -= stree[node].Len; |
| 647 |
|
| 648 |
} |
| 649 |
desc->max_code = max_code; |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
node = elems; |
| 660 |
do { |
| 661 |
pqremove(s, tree, n); |
| 662 |
m = s->heap[SMALLEST]; |
| 663 |
|
| 664 |
s->heap[--(s->heap_max)] = n; |
| 665 |
s->heap[--(s->heap_max)] = m; |
| 666 |
|
| 667 |
|
| 668 |
tree[node].Freq = tree[n].Freq + tree[m].Freq; |
| 669 |
s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ? |
| 670 |
s->depth[n] : s->depth[m]) + 1); |
| 671 |
tree[n].Dad = tree[m].Dad = (ush)node; |
| 672 |
#ifdef DUMP_BL_TREE |
| 673 |
if (tree == s->bl_tree) { |
| 674 |
fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", |
| 675 |
node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); |
| 676 |
} |
| 677 |
#endif |
| 678 |
|
| 679 |
s->heap[SMALLEST] = node++; |
| 680 |
pqdownheap(s, tree, SMALLEST); |
| 681 |
|
| 682 |
} while (s->heap_len >= 2); |
| 683 |
|
| 684 |
s->heap[--(s->heap_max)] = s->heap[SMALLEST]; |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
gen_bitlen(s, (tree_desc *)desc); |
| 690 |
|
| 691 |
|
| 692 |
gen_codes ((ct_data *)tree, max_code, s->bl_count); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
local void scan_tree (s, tree, max_code) |
| 700 |
deflate_state *s; |
| 701 |
ct_data *tree; |
| 702 |
int max_code; |
| 703 |
{ |
| 704 |
int n; |
| 705 |
int prevlen = -1; |
| 706 |
int curlen; |
| 707 |
int nextlen = tree[0].Len; |
| 708 |
int count = 0; |
| 709 |
int max_count = 7; |
| 710 |
int min_count = 4; |
| 711 |
|
| 712 |
if (nextlen == 0) max_count = 138, min_count = 3; |
| 713 |
tree[max_code+1].Len = (ush)0xffff; |
| 714 |
|
| 715 |
for (n = 0; n <= max_code; n++) { |
| 716 |
curlen = nextlen; nextlen = tree[n+1].Len; |
| 717 |
if (++count < max_count && curlen == nextlen) { |
| 718 |
continue; |
| 719 |
} else if (count < min_count) { |
| 720 |
s->bl_tree[curlen].Freq += count; |
| 721 |
} else if (curlen != 0) { |
| 722 |
if (curlen != prevlen) s->bl_tree[curlen].Freq++; |
| 723 |
s->bl_tree[REP_3_6].Freq++; |
| 724 |
} else if (count <= 10) { |
| 725 |
s->bl_tree[REPZ_3_10].Freq++; |
| 726 |
} else { |
| 727 |
s->bl_tree[REPZ_11_138].Freq++; |
| 728 |
} |
| 729 |
count = 0; prevlen = curlen; |
| 730 |
if (nextlen == 0) { |
| 731 |
max_count = 138, min_count = 3; |
| 732 |
} else if (curlen == nextlen) { |
| 733 |
max_count = 6, min_count = 3; |
| 734 |
} else { |
| 735 |
max_count = 7, min_count = 4; |
| 736 |
} |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
local void send_tree (s, tree, max_code) |
| 745 |
deflate_state *s; |
| 746 |
ct_data *tree; |
| 747 |
int max_code; |
| 748 |
{ |
| 749 |
int n; |
| 750 |
int prevlen = -1; |
| 751 |
int curlen; |
| 752 |
int nextlen = tree[0].Len; |
| 753 |
int count = 0; |
| 754 |
int max_count = 7; |
| 755 |
int min_count = 4; |
| 756 |
|
| 757 |
|
| 758 |
if (nextlen == 0) max_count = 138, min_count = 3; |
| 759 |
|
| 760 |
for (n = 0; n <= max_code; n++) { |
| 761 |
curlen = nextlen; nextlen = tree[n+1].Len; |
| 762 |
if (++count < max_count && curlen == nextlen) { |
| 763 |
continue; |
| 764 |
} else if (count < min_count) { |
| 765 |
do { send_code(s, curlen, s->bl_tree); } while (--count != 0); |
| 766 |
|
| 767 |
} else if (curlen != 0) { |
| 768 |
if (curlen != prevlen) { |
| 769 |
send_code(s, curlen, s->bl_tree); count--; |
| 770 |
} |
| 771 |
Assert(count >= 3 && count <= 6, " 3_6?"); |
| 772 |
send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); |
| 773 |
|
| 774 |
} else if (count <= 10) { |
| 775 |
send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3); |
| 776 |
|
| 777 |
} else { |
| 778 |
send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7); |
| 779 |
} |
| 780 |
count = 0; prevlen = curlen; |
| 781 |
if (nextlen == 0) { |
| 782 |
max_count = 138, min_count = 3; |
| 783 |
} else if (curlen == nextlen) { |
| 784 |
max_count = 6, min_count = 3; |
| 785 |
} else { |
| 786 |
max_count = 7, min_count = 4; |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
local int build_bl_tree(s) |
| 796 |
deflate_state *s; |
| 797 |
{ |
| 798 |
int max_blindex; |
| 799 |
|
| 800 |
|
| 801 |
scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); |
| 802 |
scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); |
| 803 |
|
| 804 |
|
| 805 |
build_tree(s, (tree_desc *)(&(s->bl_desc))); |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { |
| 815 |
if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; |
| 816 |
} |
| 817 |
|
| 818 |
s->opt_len += 3*((ulg)max_blindex+1) + 5+5+4; |
| 819 |
Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", |
| 820 |
s->opt_len, s->static_len)); |
| 821 |
|
| 822 |
return max_blindex; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
local void send_all_trees(s, lcodes, dcodes, blcodes) |
| 831 |
deflate_state *s; |
| 832 |
int lcodes, dcodes, blcodes; |
| 833 |
{ |
| 834 |
int rank; |
| 835 |
|
| 836 |
Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); |
| 837 |
Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, |
| 838 |
"too many codes"); |
| 839 |
Tracev((stderr, "\nbl counts: ")); |
| 840 |
send_bits(s, lcodes-257, 5); |
| 841 |
send_bits(s, dcodes-1, 5); |
| 842 |
send_bits(s, blcodes-4, 4); |
| 843 |
for (rank = 0; rank < blcodes; rank++) { |
| 844 |
Tracev((stderr, "\nbl code %2d ", bl_order[rank])); |
| 845 |
send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); |
| 846 |
} |
| 847 |
Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); |
| 848 |
|
| 849 |
send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); |
| 850 |
Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); |
| 851 |
|
| 852 |
send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); |
| 853 |
Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) |
| 860 |
deflate_state *s; |
| 861 |
char FAR *buf; |
| 862 |
ulg stored_len; |
| 863 |
int last; |
| 864 |
{ |
| 865 |
send_bits(s, (STORED_BLOCK<<1)+last, 3); |
| 866 |
bi_windup(s); |
| 867 |
put_short(s, (ush)stored_len); |
| 868 |
put_short(s, (ush)~stored_len); |
| 869 |
if (stored_len) |
| 870 |
zmemcpy(s->pending_buf + s->pending, (Byte FAR *)buf, stored_len); |
| 871 |
s->pending += stored_len; |
| 872 |
#ifdef ZLIB_DEBUG |
| 873 |
s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; |
| 874 |
s->compressed_len += (stored_len + 4) << 3; |
| 875 |
s->bits_sent += 2*16; |
| 876 |
s->bits_sent += stored_len<<3; |
| 877 |
#endif |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
void ZLIB_INTERNAL _tr_flush_bits(s) |
| 884 |
deflate_state *s; |
| 885 |
{ |
| 886 |
bi_flush(s); |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) |
| 894 |
deflate_state *s; |
| 895 |
char FAR *buf; |
| 896 |
ulg stored_len; |
| 897 |
int last; |
| 898 |
{ |
| 899 |
ulg opt_lenb, static_lenb; |
| 900 |
int max_blindex = 0; |
| 901 |
|
| 902 |
|
| 903 |
if (s->strm->data_type == Z_UNKNOWN) |
| 904 |
s->strm->data_type = detect_data_type(s); |
| 905 |
|
| 906 |
|
| 907 |
build_tree(s, (tree_desc *)(&(s->l_desc))); |
| 908 |
Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, |
| 909 |
s->static_len)); |
| 910 |
|
| 911 |
build_tree(s, (tree_desc *)(&(s->d_desc))); |
| 912 |
Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, |
| 913 |
s->static_len)); |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
max_blindex = build_bl_tree(s); |
| 922 |
|
| 923 |
|
| 924 |
opt_lenb = (s->opt_len+3+7)>>3; |
| 925 |
static_lenb = (s->static_len+3+7)>>3; |
| 926 |
|
| 927 |
Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", |
| 928 |
opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, |
| 929 |
s->sym_next / 3)); |
| 930 |
|
| 931 |
if (static_lenb <= opt_lenb) opt_lenb = static_lenb; |
| 932 |
|
| 933 |
#ifdef FORCE_STORED |
| 934 |
if (buf != (char*)0) { |
| 935 |
#else |
| 936 |
if (stored_len+4 <= opt_lenb && buf != (char*)0) { |
| 937 |
|
| 938 |
#endif |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
_tr_stored_block(s, buf, stored_len, last); |
| 946 |
|
| 947 |
#ifdef FORCE_STATIC |
| 948 |
} else if (static_lenb >= 0) { |
| 949 |
#else |
| 950 |
} else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { |
| 951 |
#endif |
| 952 |
send_bits(s, (STATIC_TREES<<1)+last, 3); |
| 953 |
compress_block(s, (const ct_data *)static_ltree, |
| 954 |
(const ct_data *)static_dtree); |
| 955 |
#ifdef ZLIB_DEBUG |
| 956 |
s->compressed_len += 3 + s->static_len; |
| 957 |
#endif |
| 958 |
} else { |
| 959 |
send_bits(s, (DYN_TREES<<1)+last, 3); |
| 960 |
send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, |
| 961 |
max_blindex+1); |
| 962 |
compress_block(s, (const ct_data *)s->dyn_ltree, |
| 963 |
(const ct_data *)s->dyn_dtree); |
| 964 |
#ifdef ZLIB_DEBUG |
| 965 |
s->compressed_len += 3 + s->opt_len; |
| 966 |
#endif |
| 967 |
} |
| 968 |
Assert (s->compressed_len == s->bits_sent, "bad compressed size"); |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
init_block(s); |
| 973 |
|
| 974 |
if (last) { |
| 975 |
bi_windup(s); |
| 976 |
#ifdef ZLIB_DEBUG |
| 977 |
s->compressed_len += 7; |
| 978 |
#endif |
| 979 |
} |
| 980 |
Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, |
| 981 |
s->compressed_len-7*last)); |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
int ZLIB_INTERNAL _tr_tally (s, dist, lc) |
| 989 |
deflate_state *s; |
| 990 |
unsigned dist; |
| 991 |
unsigned lc; |
| 992 |
{ |
| 993 |
s->sym_buf[s->sym_next++] = dist; |
| 994 |
s->sym_buf[s->sym_next++] = dist >> 8; |
| 995 |
s->sym_buf[s->sym_next++] = lc; |
| 996 |
s->sym_buf[s->sym_next++] = lc >> 8; |
| 997 |
if (dist == 0) { |
| 998 |
|
| 999 |
s->dyn_ltree[lc].Freq++; |
| 1000 |
} else { |
| 1001 |
s->matches++; |
| 1002 |
|
| 1003 |
dist--; |
| 1004 |
Assert((ush)dist < (ush)MAX_DIST(s) && |
| 1005 |
(ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && |
| 1006 |
(ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); |
| 1007 |
|
| 1008 |
s->dyn_ltree[l_code(lc) + LITERALS + 1].Freq++; |
| 1009 |
s->dyn_dtree[d_code(dist)].Freq++; |
| 1010 |
} |
| 1011 |
return (s->sym_next == s->sym_end); |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
local void compress_block(s, ltree, dtree) |
| 1018 |
deflate_state *s; |
| 1019 |
const ct_data *ltree; |
| 1020 |
const ct_data *dtree; |
| 1021 |
{ |
| 1022 |
ush dist; |
| 1023 |
ush lc; |
| 1024 |
unsigned sx = 0; |
| 1025 |
unsigned code; |
| 1026 |
int extra; |
| 1027 |
|
| 1028 |
if (s->sym_next != 0) do { |
| 1029 |
dist = s->sym_buf[sx++] & 0xff; |
| 1030 |
dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8; |
| 1031 |
lc = s->sym_buf[sx++] & 0xff; |
| 1032 |
lc += (unsigned)(s->sym_buf[sx++] & 0xff) << 8; |
| 1033 |
if (dist == 0) { |
| 1034 |
send_code(s, lc, ltree); |
| 1035 |
Tracecv(isgraph(lc), (stderr," '%c' ", lc)); |
| 1036 |
} else { |
| 1037 |
|
| 1038 |
code = l_code(lc); |
| 1039 |
send_code(s, code+LITERALS+1, ltree); |
| 1040 |
extra = extra_lbits[code]; |
| 1041 |
if (extra != 0) { |
| 1042 |
lc -= base_length[code]; |
| 1043 |
send_bits(s, lc, extra); |
| 1044 |
} |
| 1045 |
dist--; |
| 1046 |
code = d_code(dist); |
| 1047 |
Assert (code < D_CODES, "bad d_code"); |
| 1048 |
|
| 1049 |
send_code(s, code, dtree); |
| 1050 |
extra = extra_dbits[code]; |
| 1051 |
if (extra != 0) { |
| 1052 |
dist -= (unsigned)base_dist[code]; |
| 1053 |
send_bits(s, dist, extra); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow"); |
| 1059 |
|
| 1060 |
} while (sx < s->sym_next); |
| 1061 |
|
| 1062 |
send_code(s, END_BLOCK, ltree); |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
local int detect_data_type(s) |
| 1079 |
deflate_state *s; |
| 1080 |
{ |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
unsigned long block_mask = 0xf3ffc07fUL; |
| 1086 |
int n; |
| 1087 |
|
| 1088 |
|
| 1089 |
for (n = 0; n <= 31; n++, block_mask >>= 1) |
| 1090 |
if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0)) |
| 1091 |
return Z_BINARY; |
| 1092 |
|
| 1093 |
|
| 1094 |
if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 |
| 1095 |
|| s->dyn_ltree[13].Freq != 0) |
| 1096 |
return Z_TEXT; |
| 1097 |
for (n = 32; n < LITERALS; n++) |
| 1098 |
if (s->dyn_ltree[n].Freq != 0) |
| 1099 |
return Z_TEXT; |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
return Z_BINARY; |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
local unsigned bi_reverse(code, len) |
| 1113 |
unsigned code; |
| 1114 |
int len; |
| 1115 |
{ |
| 1116 |
register unsigned res = 0; |
| 1117 |
do { |
| 1118 |
res |= code & 1; |
| 1119 |
code >>= 1, res <<= 1; |
| 1120 |
} while (--len > 0); |
| 1121 |
return res >> 1; |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
local void bi_flush(s) |
| 1128 |
deflate_state *s; |
| 1129 |
{ |
| 1130 |
if (s->bi_valid == 16) { |
| 1131 |
put_short(s, s->bi_buf); |
| 1132 |
s->bi_buf = 0; |
| 1133 |
s->bi_valid = 0; |
| 1134 |
} else if (s->bi_valid >= 8) { |
| 1135 |
put_byte(s, (Byte)s->bi_buf); |
| 1136 |
s->bi_buf >>= 8; |
| 1137 |
s->bi_valid -= 8; |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
local void bi_windup(s) |
| 1145 |
deflate_state *s; |
| 1146 |
{ |
| 1147 |
if (s->bi_valid > 8) { |
| 1148 |
put_short(s, s->bi_buf); |
| 1149 |
} else if (s->bi_valid > 0) { |
| 1150 |
put_byte(s, (Byte)s->bi_buf); |
| 1151 |
} |
| 1152 |
s->bi_buf = 0; |
| 1153 |
s->bi_valid = 0; |
| 1154 |
#ifdef ZLIB_DEBUG |
| 1155 |
s->bits_sent = (s->bits_sent+7) & ~7; |
| 1156 |
#endif |
| 1157 |
} |