| 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 "zbuild.h" |
| 34 |
#include "deflate.h" |
| 35 |
#include "trees.h" |
| 36 |
#include "trees_emit.h" |
| 37 |
#include "trees_tbl.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
struct static_tree_desc_s { |
| 48 |
const ct_data *static_tree; |
| 49 |
const int *extra_bits; |
| 50 |
int extra_base; |
| 51 |
int elems; |
| 52 |
unsigned int max_length; |
| 53 |
}; |
| 54 |
|
| 55 |
static const static_tree_desc static_l_desc = |
| 56 |
{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; |
| 57 |
|
| 58 |
static const static_tree_desc static_d_desc = |
| 59 |
{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; |
| 60 |
|
| 61 |
static const static_tree_desc static_bl_desc = |
| 62 |
{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static void init_block (deflate_state *s); |
| 69 |
static void pqdownheap (deflate_state *s, ct_data *tree, int k); |
| 70 |
static void gen_bitlen (deflate_state *s, tree_desc *desc); |
| 71 |
static void build_tree (deflate_state *s, tree_desc *desc); |
| 72 |
static void scan_tree (deflate_state *s, ct_data *tree, int max_code); |
| 73 |
static void send_tree (deflate_state *s, ct_data *tree, int max_code); |
| 74 |
static int build_bl_tree (deflate_state *s); |
| 75 |
static void send_all_trees (deflate_state *s, int lcodes, int dcodes, int blcodes); |
| 76 |
static void compress_block (deflate_state *s, const ct_data *ltree, const ct_data *dtree); |
| 77 |
static int detect_data_type (deflate_state *s); |
| 78 |
static void bi_flush (deflate_state *s); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
void Z_INTERNAL zng_tr_init(deflate_state *s) { |
| 84 |
s->l_desc.dyn_tree = s->dyn_ltree; |
| 85 |
s->l_desc.stat_desc = &static_l_desc; |
| 86 |
|
| 87 |
s->d_desc.dyn_tree = s->dyn_dtree; |
| 88 |
s->d_desc.stat_desc = &static_d_desc; |
| 89 |
|
| 90 |
s->bl_desc.dyn_tree = s->bl_tree; |
| 91 |
s->bl_desc.stat_desc = &static_bl_desc; |
| 92 |
|
| 93 |
s->bi_buf = 0; |
| 94 |
s->bi_valid = 0; |
| 95 |
#ifdef ZLIB_DEBUG |
| 96 |
s->compressed_len = 0L; |
| 97 |
s->bits_sent = 0L; |
| 98 |
#endif |
| 99 |
|
| 100 |
|
| 101 |
init_block(s); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static void init_block(deflate_state *s) { |
| 108 |
int n; |
| 109 |
|
| 110 |
|
| 111 |
for (n = 0; n < L_CODES; n++) |
| 112 |
s->dyn_ltree[n].Freq = 0; |
| 113 |
for (n = 0; n < D_CODES; n++) |
| 114 |
s->dyn_dtree[n].Freq = 0; |
| 115 |
for (n = 0; n < BL_CODES; n++) |
| 116 |
s->bl_tree[n].Freq = 0; |
| 117 |
|
| 118 |
s->dyn_ltree[END_BLOCK].Freq = 1; |
| 119 |
s->opt_len = s->static_len = 0L; |
| 120 |
s->sym_next = s->matches = 0; |
| 121 |
} |
| 122 |
|
| 123 |
#define SMALLEST 1 |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#define pqremove(s, tree, top) \ |
| 132 |
{\ |
| 133 |
top = s->heap[SMALLEST]; \ |
| 134 |
s->heap[SMALLEST] = s->heap[s->heap_len--]; \ |
| 135 |
pqdownheap(s, tree, SMALLEST); \ |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
#define smaller(tree, n, m, depth) \ |
| 143 |
(tree[n].Freq < tree[m].Freq || \ |
| 144 |
(tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static void pqdownheap(deflate_state *s, ct_data *tree, int k) { |
| 153 |
|
| 154 |
|
| 155 |
int v = s->heap[k]; |
| 156 |
int j = k << 1; |
| 157 |
while (j <= s->heap_len) { |
| 158 |
|
| 159 |
if (j < s->heap_len && smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { |
| 160 |
j++; |
| 161 |
} |
| 162 |
|
| 163 |
if (smaller(tree, v, s->heap[j], s->depth)) |
| 164 |
break; |
| 165 |
|
| 166 |
|
| 167 |
s->heap[k] = s->heap[j]; |
| 168 |
k = j; |
| 169 |
|
| 170 |
|
| 171 |
j <<= 1; |
| 172 |
} |
| 173 |
s->heap[k] = v; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
static void gen_bitlen(deflate_state *s, tree_desc *desc) { |
| 187 |
|
| 188 |
ct_data *tree = desc->dyn_tree; |
| 189 |
int max_code = desc->max_code; |
| 190 |
const ct_data *stree = desc->stat_desc->static_tree; |
| 191 |
const int *extra = desc->stat_desc->extra_bits; |
| 192 |
int base = desc->stat_desc->extra_base; |
| 193 |
unsigned int max_length = desc->stat_desc->max_length; |
| 194 |
int h; |
| 195 |
int n, m; |
| 196 |
unsigned int bits; |
| 197 |
int xbits; |
| 198 |
uint16_t f; |
| 199 |
int overflow = 0; |
| 200 |
|
| 201 |
for (bits = 0; bits <= MAX_BITS; bits++) |
| 202 |
s->bl_count[bits] = 0; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
tree[s->heap[s->heap_max]].Len = 0; |
| 208 |
|
| 209 |
for (h = s->heap_max + 1; h < HEAP_SIZE; h++) { |
| 210 |
n = s->heap[h]; |
| 211 |
bits = tree[tree[n].Dad].Len + 1u; |
| 212 |
if (bits > max_length){ |
| 213 |
bits = max_length; |
| 214 |
overflow++; |
| 215 |
} |
| 216 |
tree[n].Len = (uint16_t)bits; |
| 217 |
|
| 218 |
|
| 219 |
if (n > max_code) |
| 220 |
continue; |
| 221 |
|
| 222 |
s->bl_count[bits]++; |
| 223 |
xbits = 0; |
| 224 |
if (n >= base) |
| 225 |
xbits = extra[n-base]; |
| 226 |
f = tree[n].Freq; |
| 227 |
s->opt_len += (unsigned long)f * (unsigned int)(bits + xbits); |
| 228 |
if (stree) |
| 229 |
s->static_len += (unsigned long)f * (unsigned int)(stree[n].Len + xbits); |
| 230 |
} |
| 231 |
if (overflow == 0) |
| 232 |
return; |
| 233 |
|
| 234 |
Tracev((stderr, "\nbit length overflow\n")); |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
do { |
| 239 |
bits = max_length - 1; |
| 240 |
while (s->bl_count[bits] == 0) |
| 241 |
bits--; |
| 242 |
s->bl_count[bits]--; |
| 243 |
s->bl_count[bits+1] += 2u; |
| 244 |
s->bl_count[max_length]--; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
overflow -= 2; |
| 249 |
} while (overflow > 0); |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
for (bits = max_length; bits != 0; bits--) { |
| 257 |
n = s->bl_count[bits]; |
| 258 |
while (n != 0) { |
| 259 |
m = s->heap[--h]; |
| 260 |
if (m > max_code) |
| 261 |
continue; |
| 262 |
if (tree[m].Len != bits) { |
| 263 |
Tracev((stderr, "code %d bits %d->%u\n", m, tree[m].Len, bits)); |
| 264 |
s->opt_len += (unsigned long)(bits * tree[m].Freq); |
| 265 |
s->opt_len -= (unsigned long)(tree[m].Len * tree[m].Freq); |
| 266 |
tree[m].Len = (uint16_t)bits; |
| 267 |
} |
| 268 |
n--; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
Z_INTERNAL void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) { |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
uint16_t next_code[MAX_BITS+1]; |
| 286 |
unsigned int code = 0; |
| 287 |
int bits; |
| 288 |
int n; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
for (bits = 1; bits <= MAX_BITS; bits++) { |
| 294 |
code = (code + bl_count[bits-1]) << 1; |
| 295 |
next_code[bits] = (uint16_t)code; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
Assert(code + bl_count[MAX_BITS]-1 == (1 << MAX_BITS)-1, "inconsistent bit counts"); |
| 301 |
Tracev((stderr, "\ngen_codes: max_code %d ", max_code)); |
| 302 |
|
| 303 |
for (n = 0; n <= max_code; n++) { |
| 304 |
int len = tree[n].Len; |
| 305 |
if (len == 0) |
| 306 |
continue; |
| 307 |
|
| 308 |
tree[n].Code = PREFIX(bi_reverse)(next_code[len]++, len); |
| 309 |
|
| 310 |
Tracecv(tree != static_ltree, (stderr, "\nn %3d %c l %2d c %4x (%x) ", |
| 311 |
n, (isgraph(n & 0xff) ? n : ' '), len, tree[n].Code, next_code[len]-1)); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
static void build_tree(deflate_state *s, tree_desc *desc) { |
| 324 |
|
| 325 |
ct_data *tree = desc->dyn_tree; |
| 326 |
const ct_data *stree = desc->stat_desc->static_tree; |
| 327 |
int elems = desc->stat_desc->elems; |
| 328 |
int n, m; |
| 329 |
int max_code = -1; |
| 330 |
int node; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
s->heap_len = 0; |
| 337 |
s->heap_max = HEAP_SIZE; |
| 338 |
|
| 339 |
for (n = 0; n < elems; n++) { |
| 340 |
if (tree[n].Freq != 0) { |
| 341 |
s->heap[++(s->heap_len)] = max_code = n; |
| 342 |
s->depth[n] = 0; |
| 343 |
} else { |
| 344 |
tree[n].Len = 0; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
while (s->heap_len < 2) { |
| 354 |
node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); |
| 355 |
tree[node].Freq = 1; |
| 356 |
s->depth[node] = 0; |
| 357 |
s->opt_len--; |
| 358 |
if (stree) |
| 359 |
s->static_len -= stree[node].Len; |
| 360 |
|
| 361 |
} |
| 362 |
desc->max_code = max_code; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
for (n = s->heap_len/2; n >= 1; n--) |
| 368 |
pqdownheap(s, tree, n); |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
node = elems; |
| 374 |
do { |
| 375 |
pqremove(s, tree, n); |
| 376 |
m = s->heap[SMALLEST]; |
| 377 |
|
| 378 |
s->heap[--(s->heap_max)] = n; |
| 379 |
s->heap[--(s->heap_max)] = m; |
| 380 |
|
| 381 |
|
| 382 |
tree[node].Freq = tree[n].Freq + tree[m].Freq; |
| 383 |
s->depth[node] = (unsigned char)((s->depth[n] >= s->depth[m] ? |
| 384 |
s->depth[n] : s->depth[m]) + 1); |
| 385 |
tree[n].Dad = tree[m].Dad = (uint16_t)node; |
| 386 |
#ifdef DUMP_BL_TREE |
| 387 |
if (tree == s->bl_tree) { |
| 388 |
fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)", |
| 389 |
node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); |
| 390 |
} |
| 391 |
#endif |
| 392 |
|
| 393 |
s->heap[SMALLEST] = node++; |
| 394 |
pqdownheap(s, tree, SMALLEST); |
| 395 |
} while (s->heap_len >= 2); |
| 396 |
|
| 397 |
s->heap[--(s->heap_max)] = s->heap[SMALLEST]; |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
gen_bitlen(s, (tree_desc *)desc); |
| 403 |
|
| 404 |
|
| 405 |
gen_codes((ct_data *)tree, max_code, s->bl_count); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
static void scan_tree(deflate_state *s, ct_data *tree, int max_code) { |
| 413 |
|
| 414 |
|
| 415 |
int n; |
| 416 |
int prevlen = -1; |
| 417 |
int curlen; |
| 418 |
int nextlen = tree[0].Len; |
| 419 |
uint16_t count = 0; |
| 420 |
uint16_t max_count = 7; |
| 421 |
uint16_t min_count = 4; |
| 422 |
|
| 423 |
if (nextlen == 0) |
| 424 |
max_count = 138, min_count = 3; |
| 425 |
|
| 426 |
tree[max_code+1].Len = (uint16_t)0xffff; |
| 427 |
|
| 428 |
for (n = 0; n <= max_code; n++) { |
| 429 |
curlen = nextlen; |
| 430 |
nextlen = tree[n+1].Len; |
| 431 |
if (++count < max_count && curlen == nextlen) { |
| 432 |
continue; |
| 433 |
} else if (count < min_count) { |
| 434 |
s->bl_tree[curlen].Freq += count; |
| 435 |
} else if (curlen != 0) { |
| 436 |
if (curlen != prevlen) |
| 437 |
s->bl_tree[curlen].Freq++; |
| 438 |
s->bl_tree[REP_3_6].Freq++; |
| 439 |
} else if (count <= 10) { |
| 440 |
s->bl_tree[REPZ_3_10].Freq++; |
| 441 |
} else { |
| 442 |
s->bl_tree[REPZ_11_138].Freq++; |
| 443 |
} |
| 444 |
count = 0; |
| 445 |
prevlen = curlen; |
| 446 |
if (nextlen == 0) { |
| 447 |
max_count = 138, min_count = 3; |
| 448 |
} else if (curlen == nextlen) { |
| 449 |
max_count = 6, min_count = 3; |
| 450 |
} else { |
| 451 |
max_count = 7, min_count = 4; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
static void send_tree(deflate_state *s, ct_data *tree, int max_code) { |
| 461 |
|
| 462 |
|
| 463 |
int n; |
| 464 |
int prevlen = -1; |
| 465 |
int curlen; |
| 466 |
int nextlen = tree[0].Len; |
| 467 |
int count = 0; |
| 468 |
int max_count = 7; |
| 469 |
int min_count = 4; |
| 470 |
|
| 471 |
|
| 472 |
if (nextlen == 0) |
| 473 |
max_count = 138, min_count = 3; |
| 474 |
|
| 475 |
|
| 476 |
uint32_t bi_valid = s->bi_valid; |
| 477 |
uint64_t bi_buf = s->bi_buf; |
| 478 |
|
| 479 |
for (n = 0; n <= max_code; n++) { |
| 480 |
curlen = nextlen; |
| 481 |
nextlen = tree[n+1].Len; |
| 482 |
if (++count < max_count && curlen == nextlen) { |
| 483 |
continue; |
| 484 |
} else if (count < min_count) { |
| 485 |
do { |
| 486 |
send_code(s, curlen, s->bl_tree, bi_buf, bi_valid); |
| 487 |
} while (--count != 0); |
| 488 |
|
| 489 |
} else if (curlen != 0) { |
| 490 |
if (curlen != prevlen) { |
| 491 |
send_code(s, curlen, s->bl_tree, bi_buf, bi_valid); |
| 492 |
count--; |
| 493 |
} |
| 494 |
Assert(count >= 3 && count <= 6, " 3_6?"); |
| 495 |
send_code(s, REP_3_6, s->bl_tree, bi_buf, bi_valid); |
| 496 |
send_bits(s, count-3, 2, bi_buf, bi_valid); |
| 497 |
|
| 498 |
} else if (count <= 10) { |
| 499 |
send_code(s, REPZ_3_10, s->bl_tree, bi_buf, bi_valid); |
| 500 |
send_bits(s, count-3, 3, bi_buf, bi_valid); |
| 501 |
|
| 502 |
} else { |
| 503 |
send_code(s, REPZ_11_138, s->bl_tree, bi_buf, bi_valid); |
| 504 |
send_bits(s, count-11, 7, bi_buf, bi_valid); |
| 505 |
} |
| 506 |
count = 0; |
| 507 |
prevlen = curlen; |
| 508 |
if (nextlen == 0) { |
| 509 |
max_count = 138, min_count = 3; |
| 510 |
} else if (curlen == nextlen) { |
| 511 |
max_count = 6, min_count = 3; |
| 512 |
} else { |
| 513 |
max_count = 7, min_count = 4; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
s->bi_buf = bi_buf; |
| 519 |
s->bi_valid = bi_valid; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
static int build_bl_tree(deflate_state *s) { |
| 527 |
int max_blindex; |
| 528 |
|
| 529 |
|
| 530 |
scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); |
| 531 |
scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); |
| 532 |
|
| 533 |
|
| 534 |
build_tree(s, (tree_desc *)(&(s->bl_desc))); |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { |
| 544 |
if (s->bl_tree[bl_order[max_blindex]].Len != 0) |
| 545 |
break; |
| 546 |
} |
| 547 |
|
| 548 |
s->opt_len += 3*((unsigned long)max_blindex+1) + 5+5+4; |
| 549 |
Tracev((stderr, "\ndyn trees: dyn %lu, stat %lu", s->opt_len, s->static_len)); |
| 550 |
|
| 551 |
return max_blindex; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
static void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes) { |
| 560 |
int rank; |
| 561 |
|
| 562 |
Assert(lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); |
| 563 |
Assert(lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, "too many codes"); |
| 564 |
|
| 565 |
|
| 566 |
uint32_t bi_valid = s->bi_valid; |
| 567 |
uint64_t bi_buf = s->bi_buf; |
| 568 |
|
| 569 |
Tracev((stderr, "\nbl counts: ")); |
| 570 |
send_bits(s, lcodes-257, 5, bi_buf, bi_valid); |
| 571 |
send_bits(s, dcodes-1, 5, bi_buf, bi_valid); |
| 572 |
send_bits(s, blcodes-4, 4, bi_buf, bi_valid); |
| 573 |
for (rank = 0; rank < blcodes; rank++) { |
| 574 |
Tracev((stderr, "\nbl code %2u ", bl_order[rank])); |
| 575 |
send_bits(s, s->bl_tree[bl_order[rank]].Len, 3, bi_buf, bi_valid); |
| 576 |
} |
| 577 |
Tracev((stderr, "\nbl tree: sent %lu", s->bits_sent)); |
| 578 |
|
| 579 |
|
| 580 |
s->bi_buf = bi_buf; |
| 581 |
s->bi_valid = bi_valid; |
| 582 |
|
| 583 |
send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); |
| 584 |
Tracev((stderr, "\nlit tree: sent %lu", s->bits_sent)); |
| 585 |
|
| 586 |
send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); |
| 587 |
Tracev((stderr, "\ndist tree: sent %lu", s->bits_sent)); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
void Z_INTERNAL zng_tr_stored_block(deflate_state *s, char *buf, uint32_t stored_len, int last) { |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
zng_tr_emit_tree(s, STORED_BLOCK, last); |
| 598 |
zng_tr_emit_align(s); |
| 599 |
cmpr_bits_align(s); |
| 600 |
put_short(s, (uint16_t)stored_len); |
| 601 |
put_short(s, (uint16_t)~stored_len); |
| 602 |
cmpr_bits_add(s, 32); |
| 603 |
sent_bits_add(s, 32); |
| 604 |
if (stored_len) { |
| 605 |
memcpy(s->pending_buf + s->pending, (unsigned char *)buf, stored_len); |
| 606 |
s->pending += stored_len; |
| 607 |
cmpr_bits_add(s, stored_len << 3); |
| 608 |
sent_bits_add(s, stored_len << 3); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
void Z_INTERNAL zng_tr_flush_bits(deflate_state *s) { |
| 616 |
bi_flush(s); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
void Z_INTERNAL zng_tr_align(deflate_state *s) { |
| 624 |
zng_tr_emit_tree(s, STATIC_TREES, 0); |
| 625 |
zng_tr_emit_end_block(s, static_ltree, 0); |
| 626 |
bi_flush(s); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
void Z_INTERNAL zng_tr_flush_block(deflate_state *s, char *buf, uint32_t stored_len, int last) { |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
unsigned long opt_lenb, static_lenb; |
| 638 |
int max_blindex = 0; |
| 639 |
|
| 640 |
|
| 641 |
if (UNLIKELY(s->sym_next == 0)) { |
| 642 |
|
| 643 |
opt_lenb = static_lenb = 0; |
| 644 |
s->static_len = 7; |
| 645 |
} else if (s->level > 0) { |
| 646 |
|
| 647 |
if (s->strm->data_type == Z_UNKNOWN) |
| 648 |
s->strm->data_type = detect_data_type(s); |
| 649 |
|
| 650 |
|
| 651 |
build_tree(s, (tree_desc *)(&(s->l_desc))); |
| 652 |
Tracev((stderr, "\nlit data: dyn %lu, stat %lu", s->opt_len, s->static_len)); |
| 653 |
|
| 654 |
build_tree(s, (tree_desc *)(&(s->d_desc))); |
| 655 |
Tracev((stderr, "\ndist data: dyn %lu, stat %lu", s->opt_len, s->static_len)); |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
max_blindex = build_bl_tree(s); |
| 664 |
|
| 665 |
|
| 666 |
opt_lenb = (s->opt_len+3+7) >> 3; |
| 667 |
static_lenb = (s->static_len+3+7) >> 3; |
| 668 |
|
| 669 |
Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %u lit %u ", |
| 670 |
opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, |
| 671 |
s->sym_next / 3)); |
| 672 |
|
| 673 |
if (static_lenb <= opt_lenb) |
| 674 |
opt_lenb = static_lenb; |
| 675 |
|
| 676 |
} else { |
| 677 |
Assert(buf != NULL, "lost buf"); |
| 678 |
opt_lenb = static_lenb = stored_len + 5; |
| 679 |
} |
| 680 |
|
| 681 |
if (stored_len+4 <= opt_lenb && buf != NULL) { |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
zng_tr_stored_block(s, buf, stored_len, last); |
| 690 |
|
| 691 |
} else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { |
| 692 |
zng_tr_emit_tree(s, STATIC_TREES, last); |
| 693 |
compress_block(s, (const ct_data *)static_ltree, (const ct_data *)static_dtree); |
| 694 |
cmpr_bits_add(s, s->static_len); |
| 695 |
} else { |
| 696 |
zng_tr_emit_tree(s, DYN_TREES, last); |
| 697 |
send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, max_blindex+1); |
| 698 |
compress_block(s, (const ct_data *)s->dyn_ltree, (const ct_data *)s->dyn_dtree); |
| 699 |
cmpr_bits_add(s, s->opt_len); |
| 700 |
} |
| 701 |
Assert(s->compressed_len == s->bits_sent, "bad compressed size"); |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
init_block(s); |
| 706 |
|
| 707 |
if (last) { |
| 708 |
zng_tr_emit_align(s); |
| 709 |
} |
| 710 |
Tracev((stderr, "\ncomprlen %lu(%lu) ", s->compressed_len>>3, s->compressed_len-7*last)); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
static void compress_block(deflate_state *s, const ct_data *ltree, const ct_data *dtree) { |
| 717 |
|
| 718 |
|
| 719 |
unsigned dist; |
| 720 |
int lc; |
| 721 |
unsigned sx = 0; |
| 722 |
|
| 723 |
if (s->sym_next != 0) { |
| 724 |
do { |
| 725 |
dist = s->sym_buf[sx++] & 0xff; |
| 726 |
dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8; |
| 727 |
lc = s->sym_buf[sx++]; |
| 728 |
if (dist == 0) { |
| 729 |
zng_emit_lit(s, ltree, lc); |
| 730 |
} else { |
| 731 |
zng_emit_dist(s, ltree, dtree, lc, dist); |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
Assert(s->pending < s->lit_bufsize + sx, "pending_buf overflow"); |
| 736 |
} while (sx < s->sym_next); |
| 737 |
} |
| 738 |
|
| 739 |
zng_emit_end_block(s, ltree, 0); |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
static int detect_data_type(deflate_state *s) { |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
unsigned long black_mask = 0xf3ffc07fUL; |
| 761 |
int n; |
| 762 |
|
| 763 |
|
| 764 |
for (n = 0; n <= 31; n++, black_mask >>= 1) |
| 765 |
if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0)) |
| 766 |
return Z_BINARY; |
| 767 |
|
| 768 |
|
| 769 |
if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 || s->dyn_ltree[13].Freq != 0) |
| 770 |
return Z_TEXT; |
| 771 |
for (n = 32; n < LITERALS; n++) |
| 772 |
if (s->dyn_ltree[n].Freq != 0) |
| 773 |
return Z_TEXT; |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
return Z_BINARY; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
static void bi_flush(deflate_state *s) { |
| 785 |
if (s->bi_valid == 64) { |
| 786 |
put_uint64(s, s->bi_buf); |
| 787 |
s->bi_buf = 0; |
| 788 |
s->bi_valid = 0; |
| 789 |
} else { |
| 790 |
if (s->bi_valid >= 32) { |
| 791 |
put_uint32(s, (uint32_t)s->bi_buf); |
| 792 |
s->bi_buf >>= 32; |
| 793 |
s->bi_valid -= 32; |
| 794 |
} |
| 795 |
if (s->bi_valid >= 16) { |
| 796 |
put_short(s, (uint16_t)s->bi_buf); |
| 797 |
s->bi_buf >>= 16; |
| 798 |
s->bi_valid -= 16; |
| 799 |
} |
| 800 |
if (s->bi_valid >= 8) { |
| 801 |
put_byte(s, s->bi_buf); |
| 802 |
s->bi_buf >>= 8; |
| 803 |
s->bi_valid -= 8; |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
Z_INTERNAL uint16_t PREFIX(bi_reverse)(unsigned code, int len) { |
| 812 |
|
| 813 |
|
| 814 |
Assert(len >= 1 && len <= 15, "code length must be 1-15"); |
| 815 |
#define bitrev8(b) \ |
| 816 |
(uint8_t)((((uint8_t)(b) * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32) |
| 817 |
return (bitrev8(code >> 8) | (uint16_t)bitrev8(code) << 8) >> (16 - len); |
| 818 |
} |