| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
] |
| 111 |
] |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
#ifdef _WIN32 |
| 295 |
|
| 296 |
|
| 297 |
#include <stdio.h> |
| 298 |
#include <stdlib.h> |
| 299 |
#include <windows.h> |
| 300 |
#ifdef DYNAMIC_ZLIB |
| 301 |
#include <zlib.h> |
| 302 |
#else |
| 303 |
#include "../zlib-1.3.1/zlib.h" |
| 304 |
#endif |
| 305 |
|
| 306 |
#include "pigz.h" |
| 307 |
|
| 308 |
#ifdef _WIN64 |
| 309 |
#define putc_unlocked _fputc_nolock |
| 310 |
#else |
| 311 |
#define putc_unlocked putc |
| 312 |
#endif |
| 313 |
|
| 314 |
void pigz_init(uint32_t setprocs) { |
| 315 |
return; |
| 316 |
} |
| 317 |
|
| 318 |
void parallel_compress(char* out_fname, unsigned char* overflow_buf, uint32_t do_append, uint32_t(* emitn)(uint32_t, unsigned char*)) { |
| 319 |
|
| 320 |
uint32_t overflow_ct = 0; |
| 321 |
gzFile gz_outfile = gzopen(out_fname, do_append? "ab": "wb"); |
| 322 |
unsigned char* write_ptr; |
| 323 |
uint32_t last_size; |
| 324 |
if (!gz_outfile) { |
| 325 |
putc_unlocked('\n', stdout); |
| 326 |
fflush(stdout); |
| 327 |
fprintf(stderr, "Error: Failed to open %s.\n", out_fname); |
| 328 |
exit(2); |
| 329 |
} |
| 330 |
do { |
| 331 |
last_size = emitn(overflow_ct, overflow_buf); |
| 332 |
if (last_size > PIGZ_BLOCK_SIZE) { |
| 333 |
overflow_ct = last_size - PIGZ_BLOCK_SIZE; |
| 334 |
last_size = PIGZ_BLOCK_SIZE; |
| 335 |
} else { |
| 336 |
overflow_ct = 0; |
| 337 |
} |
| 338 |
if (last_size) { |
| 339 |
if (!gzwrite(gz_outfile, overflow_buf, last_size)) { |
| 340 |
putc_unlocked('\n', stdout); |
| 341 |
fflush(stdout); |
| 342 |
fputs("Error: File write failure.\n", stderr); |
| 343 |
gzclose(gz_outfile); |
| 344 |
exit(6); |
| 345 |
} |
| 346 |
} |
| 347 |
if (overflow_ct) { |
| 348 |
write_ptr = &(overflow_buf[PIGZ_BLOCK_SIZE]); |
| 349 |
while (overflow_ct > PIGZ_BLOCK_SIZE) { |
| 350 |
if (!gzwrite(gz_outfile, write_ptr, PIGZ_BLOCK_SIZE)) { |
| 351 |
putc_unlocked('\n', stdout); |
| 352 |
fflush(stdout); |
| 353 |
fputs("Error: File write failure.\n", stderr); |
| 354 |
gzclose(gz_outfile); |
| 355 |
exit(6); |
| 356 |
} |
| 357 |
write_ptr = &(write_ptr[PIGZ_BLOCK_SIZE]); |
| 358 |
overflow_ct -= PIGZ_BLOCK_SIZE; |
| 359 |
} |
| 360 |
memcpy(overflow_buf, write_ptr, overflow_ct); |
| 361 |
} |
| 362 |
} while (last_size); |
| 363 |
if (gzclose(gz_outfile) != Z_OK) { |
| 364 |
putc_unlocked('\n', stdout); |
| 365 |
fflush(stdout); |
| 366 |
fputs("Error: File write failure.\n", stderr); |
| 367 |
exit(6); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
int32_t pzwrite_init(char* out_fname, unsigned char* overflow_buf, uint32_t do_append, Pigz_state* ps_ptr) { |
| 372 |
ps_ptr->outfile = fopen(out_fname, do_append? "ab" : "wb"); |
| 373 |
ps_ptr->gz_outfile = NULL; |
| 374 |
if (!ps_ptr->outfile) { |
| 375 |
putc_unlocked('\n', stdout); |
| 376 |
fflush(stdout); |
| 377 |
fprintf(stderr, "Error: Failed to open %s.\n", out_fname); |
| 378 |
return 2; |
| 379 |
} |
| 380 |
ps_ptr->overflow_buf = overflow_buf; |
| 381 |
return 0; |
| 382 |
} |
| 383 |
|
| 384 |
void compressed_pzwrite_init(char* out_fname, unsigned char* overflow_buf, uint32_t do_append, Pigz_state* ps_ptr) { |
| 385 |
ps_ptr->outfile = NULL; |
| 386 |
ps_ptr->gz_outfile = gzopen(out_fname, do_append? "ab" : "wb"); |
| 387 |
if (!ps_ptr->gz_outfile) { |
| 388 |
putc_unlocked('\n', stdout); |
| 389 |
fflush(stdout); |
| 390 |
fprintf(stderr, "Error: Failed to open %s.\n", out_fname); |
| 391 |
exit(2); |
| 392 |
} |
| 393 |
ps_ptr->overflow_buf = overflow_buf; |
| 394 |
} |
| 395 |
|
| 396 |
int32_t flex_pzwrite_init(uint32_t output_gz, char* out_fname, unsigned char* overflow_buf, uint32_t do_append, Pigz_state* ps_ptr) { |
| 397 |
if (!output_gz) { |
| 398 |
return pzwrite_init(out_fname, overflow_buf, do_append, ps_ptr); |
| 399 |
} else { |
| 400 |
compressed_pzwrite_init(out_fname, overflow_buf, do_append, ps_ptr); |
| 401 |
return 0; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
int32_t force_pzwrite(Pigz_state* ps_ptr, char** writep_ptr, uint32_t write_min) { |
| 406 |
unsigned char* writep = (unsigned char*)(*writep_ptr); |
| 407 |
if (ps_ptr->overflow_buf != writep) { |
| 408 |
if (!fwrite(ps_ptr->overflow_buf, writep - ps_ptr->overflow_buf, 1, ps_ptr->outfile)) { |
| 409 |
return 6; |
| 410 |
} |
| 411 |
*writep_ptr = (char*)(ps_ptr->overflow_buf); |
| 412 |
} |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
void force_compressed_pzwrite(Pigz_state* ps_ptr, char** writep_ptr, uint32_t write_min) { |
| 417 |
unsigned char* writep = (unsigned char*)(*writep_ptr); |
| 418 |
if (ps_ptr->overflow_buf != writep) { |
| 419 |
if (!gzwrite(ps_ptr->gz_outfile, ps_ptr->overflow_buf, writep - ps_ptr->overflow_buf)) { |
| 420 |
putc_unlocked('\n', stdout); |
| 421 |
fflush(stdout); |
| 422 |
fputs("Error: File write failure.\n", stderr); |
| 423 |
gzclose(ps_ptr->gz_outfile); |
| 424 |
exit(6); |
| 425 |
} |
| 426 |
*writep_ptr = (char*)(ps_ptr->overflow_buf); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
int32_t flex_pzputs_std(Pigz_state* ps_ptr, char** writep_ptr, char* ss, uint32_t sslen) { |
| 431 |
unsigned char* writep = (unsigned char*)(*writep_ptr); |
| 432 |
unsigned char* readp = (unsigned char*)ss; |
| 433 |
uint32_t cur_write_space = 2 * PIGZ_BLOCK_SIZE - ((uintptr_t)(writep - ps_ptr->overflow_buf)); |
| 434 |
while (sslen > cur_write_space) { |
| 435 |
memcpy(writep, readp, cur_write_space); |
| 436 |
if (is_uncompressed_pzwrite(ps_ptr)) { |
| 437 |
if (!fwrite(ps_ptr->overflow_buf, 2 * PIGZ_BLOCK_SIZE, 1, ps_ptr->outfile)) { |
| 438 |
return 6; |
| 439 |
} |
| 440 |
} else { |
| 441 |
if (!gzwrite(ps_ptr->gz_outfile, ps_ptr->overflow_buf, 2 * PIGZ_BLOCK_SIZE)) { |
| 442 |
putc_unlocked('\n', stdout); |
| 443 |
fflush(stdout); |
| 444 |
fputs("Error: File write failure.\n", stderr); |
| 445 |
gzclose(ps_ptr->gz_outfile); |
| 446 |
exit(6); |
| 447 |
} |
| 448 |
} |
| 449 |
writep = ps_ptr->overflow_buf; |
| 450 |
readp = &(readp[cur_write_space]); |
| 451 |
sslen -= cur_write_space; |
| 452 |
cur_write_space = 2 * PIGZ_BLOCK_SIZE; |
| 453 |
} |
| 454 |
memcpy(writep, readp, sslen); |
| 455 |
*writep_ptr = (char*)(&(writep[sslen])); |
| 456 |
return flex_pzwrite(ps_ptr, writep_ptr); |
| 457 |
} |
| 458 |
|
| 459 |
int32_t pzwrite_close_null(Pigz_state* ps_ptr, char* writep) { |
| 460 |
force_pzwrite(ps_ptr, &writep, 0); |
| 461 |
int32_t ii = ferror(ps_ptr->outfile); |
| 462 |
int32_t jj = fclose(ps_ptr->outfile); |
| 463 |
ps_ptr->overflow_buf = NULL; |
| 464 |
return ii || jj; |
| 465 |
} |
| 466 |
|
| 467 |
void compressed_pzwrite_close_null(Pigz_state* ps_ptr, char* writep) { |
| 468 |
force_compressed_pzwrite(ps_ptr, &writep, 0); |
| 469 |
ps_ptr->overflow_buf = NULL; |
| 470 |
if (gzclose(ps_ptr->gz_outfile) != Z_OK) { |
| 471 |
putc_unlocked('\n', stdout); |
| 472 |
fflush(stdout); |
| 473 |
fputs("Error: File write failure.\n", stderr); |
| 474 |
exit(6); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
int32_t flex_pzwrite_close_null(Pigz_state* ps_ptr, char* writep) { |
| 479 |
if (is_uncompressed_pzwrite(ps_ptr)) { |
| 480 |
return pzwrite_close_null(ps_ptr, writep); |
| 481 |
} else { |
| 482 |
compressed_pzwrite_close_null(ps_ptr, writep); |
| 483 |
return 0; |
| 484 |
} |
| 485 |
} |
| 486 |
#else |
| 487 |
|
| 488 |
#define VERSION "pigz 2.3\n" |
| 489 |
|
| 490 |
|
| 491 |
#define _FILE_OFFSET_BITS 64 |
| 492 |
|
| 493 |
|
| 494 |
#include <stdio.h> |
| 495 |
|
| 496 |
|
| 497 |
#include <stdlib.h> |
| 498 |
|
| 499 |
#include <stdarg.h> |
| 500 |
#include <string.h> |
| 501 |
|
| 502 |
#include <errno.h> |
| 503 |
#include <assert.h> |
| 504 |
#include <time.h> |
| 505 |
#include <signal.h> |
| 506 |
#include <sys/types.h> |
| 507 |
#include <sys/stat.h> |
| 508 |
|
| 509 |
#include <sys/time.h> |
| 510 |
#include <unistd.h> |
| 511 |
|
| 512 |
#include <fcntl.h> |
| 513 |
|
| 514 |
#include <dirent.h> |
| 515 |
|
| 516 |
#include <limits.h> |
| 517 |
#if __STDC_VERSION__-0 >= 199901L || __GNUC__-0 >= 3 |
| 518 |
# include <inttypes.h> |
| 519 |
#endif |
| 520 |
|
| 521 |
#ifdef __hpux |
| 522 |
# include <sys/param.h> |
| 523 |
# include <sys/pstat.h> |
| 524 |
#endif |
| 525 |
|
| 526 |
#ifdef DYNAMIC_ZLIB |
| 527 |
#include <zlib.h> |
| 528 |
#else |
| 529 |
#include "../zlib-1.3.1/zlib.h" |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
#endif |
| 536 |
#if !defined(ZLIB_VERNUM) || ZLIB_VERNUM < 0x1230 |
| 537 |
# error Need zlib version 1.2.3 or later |
| 538 |
#endif |
| 539 |
|
| 540 |
#ifndef NOTHREAD |
| 541 |
# include "yarn.h" |
| 542 |
|
| 543 |
|
| 544 |
#endif |
| 545 |
|
| 546 |
#include "pigz.h" |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
#define local static |
| 551 |
|
| 552 |
|
| 553 |
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) |
| 554 |
# include <io.h> |
| 555 |
# define SET_BINARY_MODE(fd) setmode(fd, O_BINARY) |
| 556 |
#else |
| 557 |
# define SET_BINARY_MODE(fd) |
| 558 |
#endif |
| 559 |
|
| 560 |
|
| 561 |
#define RELEASE(ptr) \ |
| 562 |
do { \ |
| 563 |
if ((ptr) != NULL) { \ |
| 564 |
free(ptr); \ |
| 565 |
ptr = NULL; \ |
| 566 |
} \ |
| 567 |
} while (0) |
| 568 |
|
| 569 |
|
| 570 |
#define DICT 32768U |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
#define MAXP2 (UINT_MAX - (UINT_MAX >> 1)) |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
#define RSYNCBITS 12 |
| 620 |
#define RSYNCMASK ((1U << RSYNCBITS) - 1) |
| 621 |
#define RSYNCHIT (RSYNCMASK >> 1) |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
#define INBUFS(p) (((p)<<1)+3) |
| 629 |
#define OUTPOOL(s) ((s)+((s)>>4)+DICT) |
| 630 |
|
| 631 |
|
| 632 |
#define BUF 32768U |
| 633 |
|
| 634 |
|
| 635 |
local struct { |
| 636 |
char *prog; |
| 637 |
int outd; |
| 638 |
char *outf; |
| 639 |
int verbosity; |
| 640 |
time_t mtime; |
| 641 |
int level; |
| 642 |
int procs; |
| 643 |
size_t block; |
| 644 |
int warned; |
| 645 |
} g; |
| 646 |
|
| 647 |
|
| 648 |
local int complain(const char *fmt, ...) |
| 649 |
{ |
| 650 |
va_list ap; |
| 651 |
|
| 652 |
if (g.verbosity > 0) { |
| 653 |
fprintf(stderr, "%s: ", g.prog); |
| 654 |
va_start(ap, fmt); |
| 655 |
vfprintf(stderr, fmt, ap); |
| 656 |
va_end(ap); |
| 657 |
putc_unlocked('\n', stderr); |
| 658 |
fflush(stderr); |
| 659 |
g.warned = 1; |
| 660 |
} |
| 661 |
return 0; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
local int bail(const char *why, const char *what) |
| 666 |
{ |
| 667 |
if (g.outd != -1 && g.outf != NULL) |
| 668 |
unlink(g.outf); |
| 669 |
complain("abort: %s%s", why, what); |
| 670 |
exit(1); |
| 671 |
return 0; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
local void writen(int desc, unsigned char *buf, size_t len) |
| 676 |
{ |
| 677 |
ssize_t ret; |
| 678 |
|
| 679 |
while (len) { |
| 680 |
ret = write(desc, buf, len); |
| 681 |
if (ret < 1) { |
| 682 |
complain("write error code %d", errno); |
| 683 |
bail("write error on ", g.outf); |
| 684 |
} |
| 685 |
buf += ret; |
| 686 |
len -= ret; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
#define PUT2L(a,b) (*(a)=(b)&0xff,(a)[1]=(b)>>8) |
| 692 |
#define PUT4L(a,b) (PUT2L(a,(b)&0xffff),PUT2L((a)+2,(b)>>16)) |
| 693 |
#define PUT4M(a,b) (*(a)=(b)>>24,(a)[1]=(b)>>16,(a)[2]=(b)>>8,(a)[3]=(b)) |
| 694 |
|
| 695 |
|
| 696 |
local unsigned long put_header(void) |
| 697 |
{ |
| 698 |
unsigned long len; |
| 699 |
unsigned char head[30]; |
| 700 |
|
| 701 |
head[0] = 31; |
| 702 |
head[1] = 139; |
| 703 |
head[2] = 8; |
| 704 |
head[3] = 0; |
| 705 |
PUT4L(head + 4, g.mtime); |
| 706 |
head[8] = g.level >= 9 ? 2 : (g.level == 1 ? 4 : 0); |
| 707 |
head[9] = 3; |
| 708 |
writen(g.outd, head, 10); |
| 709 |
len = 10; |
| 710 |
return len; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
local void put_trailer(unsigned long ulen, unsigned long clen, |
| 715 |
unsigned long check, unsigned long head) |
| 716 |
{ |
| 717 |
unsigned char tail[46]; |
| 718 |
|
| 719 |
PUT4L(tail, check); |
| 720 |
PUT4L(tail + 4, ulen); |
| 721 |
writen(g.outd, tail, 8); |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
#define CHECK(a,b,c) crc32(a,b,c) |
| 726 |
|
| 727 |
#ifndef NOTHREAD |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
#define COMB(a,b,c) crc32_comb(a,b,c) |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
local unsigned long gf2_matrix_times(unsigned long *mat, unsigned long vec) |
| 740 |
{ |
| 741 |
unsigned long sum; |
| 742 |
|
| 743 |
sum = 0; |
| 744 |
while (vec) { |
| 745 |
if (vec & 1) |
| 746 |
sum ^= *mat; |
| 747 |
vec >>= 1; |
| 748 |
mat++; |
| 749 |
} |
| 750 |
return sum; |
| 751 |
} |
| 752 |
|
| 753 |
local void gf2_matrix_square(unsigned long *square, unsigned long *mat) |
| 754 |
{ |
| 755 |
int n; |
| 756 |
|
| 757 |
for (n = 0; n < 32; n++) |
| 758 |
square[n] = gf2_matrix_times(mat, mat[n]); |
| 759 |
} |
| 760 |
|
| 761 |
local unsigned long crc32_comb(unsigned long crc1, unsigned long crc2, |
| 762 |
size_t len2) |
| 763 |
{ |
| 764 |
int n; |
| 765 |
unsigned long row; |
| 766 |
unsigned long even[32]; |
| 767 |
unsigned long odd[32]; |
| 768 |
|
| 769 |
|
| 770 |
if (len2 == 0) |
| 771 |
return crc1; |
| 772 |
|
| 773 |
|
| 774 |
odd[0] = 0xedb88320UL; |
| 775 |
row = 1; |
| 776 |
for (n = 1; n < 32; n++) { |
| 777 |
odd[n] = row; |
| 778 |
row <<= 1; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
gf2_matrix_square(even, odd); |
| 783 |
|
| 784 |
|
| 785 |
gf2_matrix_square(odd, even); |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
do { |
| 790 |
|
| 791 |
gf2_matrix_square(even, odd); |
| 792 |
if (len2 & 1) |
| 793 |
crc1 = gf2_matrix_times(even, crc1); |
| 794 |
len2 >>= 1; |
| 795 |
|
| 796 |
|
| 797 |
if (len2 == 0) |
| 798 |
break; |
| 799 |
|
| 800 |
|
| 801 |
gf2_matrix_square(odd, even); |
| 802 |
if (len2 & 1) |
| 803 |
crc1 = gf2_matrix_times(odd, crc1); |
| 804 |
len2 >>= 1; |
| 805 |
|
| 806 |
|
| 807 |
} while (len2 != 0); |
| 808 |
|
| 809 |
|
| 810 |
crc1 ^= crc2; |
| 811 |
return crc1; |
| 812 |
} |
| 813 |
|
| 814 |
#define BASE 65521U |
| 815 |
#define LOW16 0xffff |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
local void new_pool(struct pool *pool, size_t size, int limit) |
| 821 |
{ |
| 822 |
pool->have = new_lock(0); |
| 823 |
pool->head = NULL; |
| 824 |
pool->size = size; |
| 825 |
pool->limit = limit; |
| 826 |
pool->made = 0; |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
local struct space *get_space(struct pool *pool) |
| 832 |
{ |
| 833 |
struct space *space; |
| 834 |
|
| 835 |
|
| 836 |
possess(pool->have); |
| 837 |
if (pool->limit == 0) |
| 838 |
wait_for(pool->have, NOT_TO_BE, 0); |
| 839 |
|
| 840 |
|
| 841 |
if (pool->head != NULL) { |
| 842 |
space = pool->head; |
| 843 |
possess(space->use); |
| 844 |
pool->head = space->next; |
| 845 |
twist(pool->have, BY, -1); |
| 846 |
twist(space->use, TO, 1); |
| 847 |
space->len = 0; |
| 848 |
return space; |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
assert(pool->limit != 0); |
| 853 |
if (pool->limit > 0) |
| 854 |
pool->limit--; |
| 855 |
pool->made++; |
| 856 |
release(pool->have); |
| 857 |
space = (struct space*)malloc(sizeof(struct space)); |
| 858 |
if (space == NULL) |
| 859 |
bail("not enough memory", ""); |
| 860 |
space->use = new_lock(1); |
| 861 |
space->buf = (unsigned char*)malloc(pool->size); |
| 862 |
if (space->buf == NULL) |
| 863 |
bail("not enough memory", ""); |
| 864 |
space->size = pool->size; |
| 865 |
space->len = 0; |
| 866 |
space->pool = pool; |
| 867 |
return space; |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
local size_t grow(size_t size) |
| 874 |
{ |
| 875 |
size_t was, top; |
| 876 |
int shift; |
| 877 |
|
| 878 |
was = size; |
| 879 |
size += size >> 2; |
| 880 |
top = size; |
| 881 |
for (shift = 0; top > 7; shift++) |
| 882 |
top >>= 1; |
| 883 |
if (top == 7) |
| 884 |
size = (size_t)1 << (shift + 3); |
| 885 |
if (size < 16) |
| 886 |
size = 16; |
| 887 |
if (size <= was) |
| 888 |
size = (size_t)0 - 1; |
| 889 |
return size; |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
local void grow_space(struct space *space) |
| 894 |
{ |
| 895 |
size_t more; |
| 896 |
|
| 897 |
|
| 898 |
more = grow(space->size); |
| 899 |
if (more == space->size) |
| 900 |
bail("not enough memory", ""); |
| 901 |
|
| 902 |
|
| 903 |
space->buf = (unsigned char*)realloc(space->buf, more); |
| 904 |
if (space->buf == NULL) |
| 905 |
bail("not enough memory", ""); |
| 906 |
space->size = more; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
local void use_space(struct space *space) |
| 912 |
{ |
| 913 |
possess(space->use); |
| 914 |
twist(space->use, BY, +1); |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
local void drop_space(struct space *space) |
| 919 |
{ |
| 920 |
int use; |
| 921 |
struct pool *pool; |
| 922 |
|
| 923 |
possess(space->use); |
| 924 |
use = peek_lock(space->use); |
| 925 |
assert(use != 0); |
| 926 |
if (use == 1) { |
| 927 |
pool = space->pool; |
| 928 |
possess(pool->have); |
| 929 |
space->next = pool->head; |
| 930 |
pool->head = space; |
| 931 |
twist(pool->have, BY, +1); |
| 932 |
} |
| 933 |
twist(space->use, BY, -1); |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
local int free_pool(struct pool *pool) |
| 939 |
{ |
| 940 |
int count; |
| 941 |
struct space *space; |
| 942 |
|
| 943 |
possess(pool->have); |
| 944 |
count = 0; |
| 945 |
while ((space = pool->head) != NULL) { |
| 946 |
pool->head = space->next; |
| 947 |
free(space->buf); |
| 948 |
free_lock(space->use); |
| 949 |
free(space); |
| 950 |
count++; |
| 951 |
} |
| 952 |
assert(count == pool->made); |
| 953 |
release(pool->have); |
| 954 |
free_lock(pool->have); |
| 955 |
return count; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
local struct pool in_pool; |
| 960 |
local struct pool out_pool; |
| 961 |
local struct pool dict_pool; |
| 962 |
local struct pool lens_pool; |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
struct job { |
| 970 |
long seq; |
| 971 |
int more; |
| 972 |
struct space *in; |
| 973 |
struct space *out; |
| 974 |
struct space *lens; |
| 975 |
unsigned long check; |
| 976 |
lock *calc; |
| 977 |
struct job *next; |
| 978 |
}; |
| 979 |
|
| 980 |
|
| 981 |
local lock *compress_have = NULL; |
| 982 |
local struct job *compress_head, **compress_tail; |
| 983 |
|
| 984 |
|
| 985 |
local lock *write_first; |
| 986 |
local struct job *write_head; |
| 987 |
|
| 988 |
|
| 989 |
local int cthreads = 0; |
| 990 |
|
| 991 |
|
| 992 |
local thread *writeth = NULL; |
| 993 |
|
| 994 |
|
| 995 |
local void setup_jobs(void) |
| 996 |
{ |
| 997 |
|
| 998 |
if (compress_have != NULL) |
| 999 |
return; |
| 1000 |
|
| 1001 |
|
| 1002 |
compress_have = new_lock(0); |
| 1003 |
compress_head = NULL; |
| 1004 |
compress_tail = &compress_head; |
| 1005 |
write_first = new_lock(-1); |
| 1006 |
write_head = NULL; |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
new_pool(&in_pool, g.block, INBUFS(g.procs)); |
| 1012 |
new_pool(&out_pool, OUTPOOL(g.block), -1); |
| 1013 |
new_pool(&dict_pool, DICT, -1); |
| 1014 |
new_pool(&lens_pool, g.block >> (RSYNCBITS - 1), -1); |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
local void finish_jobs(void) |
| 1020 |
{ |
| 1021 |
struct job job; |
| 1022 |
int caught; |
| 1023 |
|
| 1024 |
|
| 1025 |
if (compress_have == NULL) |
| 1026 |
return; |
| 1027 |
|
| 1028 |
|
| 1029 |
possess(compress_have); |
| 1030 |
job.seq = -1; |
| 1031 |
job.next = NULL; |
| 1032 |
compress_head = &job; |
| 1033 |
compress_tail = &(job.next); |
| 1034 |
twist(compress_have, BY, +1); |
| 1035 |
|
| 1036 |
|
| 1037 |
caught = join_all(); |
| 1038 |
assert(caught == cthreads); |
| 1039 |
cthreads = 0; |
| 1040 |
|
| 1041 |
|
| 1042 |
caught = free_pool(&lens_pool); |
| 1043 |
caught = free_pool(&dict_pool); |
| 1044 |
caught = free_pool(&out_pool); |
| 1045 |
caught = free_pool(&in_pool); |
| 1046 |
free_lock(write_first); |
| 1047 |
free_lock(compress_have); |
| 1048 |
compress_have = NULL; |
| 1049 |
close(g.outd); |
| 1050 |
g.outd = -1; |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
local void deflate_engine(z_stream *strm, struct space *out, int flush) |
| 1058 |
{ |
| 1059 |
size_t room; |
| 1060 |
|
| 1061 |
do { |
| 1062 |
room = out->size - out->len; |
| 1063 |
if (room == 0) { |
| 1064 |
grow_space(out); |
| 1065 |
room = out->size - out->len; |
| 1066 |
} |
| 1067 |
strm->next_out = out->buf + out->len; |
| 1068 |
strm->avail_out = room < UINT_MAX ? (unsigned)room : UINT_MAX; |
| 1069 |
(void)deflate(strm, flush); |
| 1070 |
out->len = strm->next_out - out->buf; |
| 1071 |
} while (strm->avail_out == 0); |
| 1072 |
assert(strm->avail_in == 0); |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
local void compress_thread(void *dummy) |
| 1081 |
{ |
| 1082 |
struct job *job; |
| 1083 |
struct job *here, **prior; |
| 1084 |
unsigned long check; |
| 1085 |
unsigned char *next; |
| 1086 |
size_t left; |
| 1087 |
size_t len; |
| 1088 |
#if ZLIB_VERNUM >= 0x1260 |
| 1089 |
int bits; |
| 1090 |
#endif |
| 1091 |
z_stream strm; |
| 1092 |
|
| 1093 |
(void)dummy; |
| 1094 |
|
| 1095 |
|
| 1096 |
strm.zfree = Z_NULL; |
| 1097 |
strm.zalloc = Z_NULL; |
| 1098 |
strm.opaque = Z_NULL; |
| 1099 |
if (deflateInit2(&strm, 6, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) |
| 1100 |
bail("not enough memory", ""); |
| 1101 |
|
| 1102 |
|
| 1103 |
for (;;) { |
| 1104 |
|
| 1105 |
possess(compress_have); |
| 1106 |
wait_for(compress_have, NOT_TO_BE, 0); |
| 1107 |
job = compress_head; |
| 1108 |
assert(job != NULL); |
| 1109 |
if (job->seq == -1) |
| 1110 |
break; |
| 1111 |
compress_head = job->next; |
| 1112 |
if (job->next == NULL) |
| 1113 |
compress_tail = &compress_head; |
| 1114 |
twist(compress_have, BY, -1); |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
(void)deflateReset(&strm); |
| 1120 |
(void)deflateParams(&strm, g.level, Z_DEFAULT_STRATEGY); |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
if (job->out != NULL) { |
| 1125 |
len = job->out->len; |
| 1126 |
left = len < DICT ? len : DICT; |
| 1127 |
deflateSetDictionary(&strm, job->out->buf + (len - left), |
| 1128 |
left); |
| 1129 |
drop_space(job->out); |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
job->out = get_space(&out_pool); |
| 1134 |
strm.next_in = job->in->buf; |
| 1135 |
strm.next_out = job->out->buf; |
| 1136 |
|
| 1137 |
|
| 1138 |
next = job->lens == NULL ? NULL : job->lens->buf; |
| 1139 |
left = job->in->len; |
| 1140 |
job->out->len = 0; |
| 1141 |
do { |
| 1142 |
|
| 1143 |
len = next == NULL ? 128 : *next++; |
| 1144 |
if (len < 128) |
| 1145 |
len = (len << 8) + (*next++) + 64; |
| 1146 |
else if (len == 128) |
| 1147 |
len = left; |
| 1148 |
else if (len < 192) |
| 1149 |
len &= 0x3f; |
| 1150 |
else { |
| 1151 |
len = ((len & 0x3f) << 16) + (*next++ << 8) + 32832U; |
| 1152 |
len += *next++; |
| 1153 |
} |
| 1154 |
left -= len; |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
while (len > MAXP2) { |
| 1161 |
strm.avail_in = MAXP2; |
| 1162 |
deflate_engine(&strm, job->out, Z_NO_FLUSH); |
| 1163 |
len -= MAXP2; |
| 1164 |
} |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
strm.avail_in = (unsigned)len; |
| 1170 |
if (left || job->more) { |
| 1171 |
#if ZLIB_VERNUM >= 0x1260 |
| 1172 |
deflate_engine(&strm, job->out, Z_BLOCK); |
| 1173 |
|
| 1174 |
|
| 1175 |
(void)deflatePending(&strm, Z_NULL, &bits); |
| 1176 |
if (bits & 1) |
| 1177 |
deflate_engine(&strm, job->out, Z_SYNC_FLUSH); |
| 1178 |
else if (bits & 7) { |
| 1179 |
do { |
| 1180 |
bits = deflatePrime(&strm, 10, 2); |
| 1181 |
assert(bits == Z_OK); |
| 1182 |
(void)deflatePending(&strm, Z_NULL, &bits); |
| 1183 |
} while (bits & 7); |
| 1184 |
deflate_engine(&strm, job->out, Z_BLOCK); |
| 1185 |
} |
| 1186 |
#else |
| 1187 |
deflate_engine(&strm, job->out, Z_SYNC_FLUSH); |
| 1188 |
#endif |
| 1189 |
} |
| 1190 |
else |
| 1191 |
deflate_engine(&strm, job->out, Z_FINISH); |
| 1192 |
} while (left); |
| 1193 |
if (job->lens != NULL) { |
| 1194 |
drop_space(job->lens); |
| 1195 |
job->lens = NULL; |
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
use_space(job->in); |
| 1200 |
|
| 1201 |
|
| 1202 |
possess(write_first); |
| 1203 |
prior = &write_head; |
| 1204 |
while ((here = *prior) != NULL) { |
| 1205 |
if (here->seq > job->seq) |
| 1206 |
break; |
| 1207 |
prior = &(here->next); |
| 1208 |
} |
| 1209 |
job->next = here; |
| 1210 |
*prior = job; |
| 1211 |
twist(write_first, TO, write_head->seq); |
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
len = job->in->len; |
| 1217 |
next = job->in->buf; |
| 1218 |
check = CHECK(0L, Z_NULL, 0); |
| 1219 |
while (len > MAXP2) { |
| 1220 |
check = CHECK(check, next, MAXP2); |
| 1221 |
len -= MAXP2; |
| 1222 |
next += MAXP2; |
| 1223 |
} |
| 1224 |
check = CHECK(check, next, (unsigned)len); |
| 1225 |
drop_space(job->in); |
| 1226 |
job->check = check; |
| 1227 |
possess(job->calc); |
| 1228 |
twist(job->calc, TO, 1); |
| 1229 |
|
| 1230 |
|
| 1231 |
} |
| 1232 |
|
| 1233 |
|
| 1234 |
release(compress_have); |
| 1235 |
(void)deflateEnd(&strm); |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
local void write_thread(void *dummy) |
| 1242 |
{ |
| 1243 |
long seq; |
| 1244 |
struct job *job; |
| 1245 |
size_t len; |
| 1246 |
int more; |
| 1247 |
unsigned long head; |
| 1248 |
unsigned long ulen; |
| 1249 |
unsigned long clen; |
| 1250 |
unsigned long check; |
| 1251 |
|
| 1252 |
(void)dummy; |
| 1253 |
|
| 1254 |
|
| 1255 |
head = put_header(); |
| 1256 |
|
| 1257 |
|
| 1258 |
ulen = clen = 0; |
| 1259 |
check = CHECK(0L, Z_NULL, 0); |
| 1260 |
seq = 0; |
| 1261 |
do { |
| 1262 |
|
| 1263 |
possess(write_first); |
| 1264 |
wait_for(write_first, TO_BE, seq); |
| 1265 |
job = write_head; |
| 1266 |
write_head = job->next; |
| 1267 |
twist(write_first, TO, write_head == NULL ? -1 : write_head->seq); |
| 1268 |
|
| 1269 |
|
| 1270 |
more = job->more; |
| 1271 |
len = job->in->len; |
| 1272 |
drop_space(job->in); |
| 1273 |
ulen += (unsigned long)len; |
| 1274 |
clen += (unsigned long)(job->out->len); |
| 1275 |
|
| 1276 |
|
| 1277 |
writen(g.outd, job->out->buf, job->out->len); |
| 1278 |
drop_space(job->out); |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
possess(job->calc); |
| 1283 |
wait_for(job->calc, TO_BE, 1); |
| 1284 |
release(job->calc); |
| 1285 |
check = COMB(check, job->check, len); |
| 1286 |
|
| 1287 |
|
| 1288 |
free_lock(job->calc); |
| 1289 |
free(job); |
| 1290 |
|
| 1291 |
|
| 1292 |
seq++; |
| 1293 |
} while (more); |
| 1294 |
|
| 1295 |
|
| 1296 |
put_trailer(ulen, clen, check, head); |
| 1297 |
|
| 1298 |
|
| 1299 |
possess(compress_have); |
| 1300 |
assert(compress_head == NULL && peek_lock(compress_have) == 0); |
| 1301 |
release(compress_have); |
| 1302 |
possess(write_first); |
| 1303 |
assert(write_head == NULL); |
| 1304 |
twist(write_first, TO, -1); |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
void parallel_compress(char* out_fname, unsigned char* overflow_buf, uint32_t do_append, uint32_t(* emitn)(uint32_t, unsigned char*)) |
| 1312 |
{ |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
unsigned char* read_ptr = NULL; |
| 1318 |
|
| 1319 |
uint32_t overflow_ct; |
| 1320 |
long seq; |
| 1321 |
struct space *curr; |
| 1322 |
struct space *next; |
| 1323 |
struct space *dict; |
| 1324 |
struct job *job; |
| 1325 |
|
| 1326 |
int more; |
| 1327 |
size_t len; |
| 1328 |
uint32_t cur_len; |
| 1329 |
|
| 1330 |
g.outf = out_fname; |
| 1331 |
g.outd = open(g.outf, O_WRONLY | (do_append? O_APPEND : (O_CREAT | O_TRUNC)), 0644); |
| 1332 |
|
| 1333 |
|
| 1334 |
setup_jobs(); |
| 1335 |
|
| 1336 |
|
| 1337 |
writeth = launch(write_thread, NULL); |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
seq = 0; |
| 1342 |
next = get_space(&in_pool); |
| 1343 |
cur_len = emitn(0, overflow_buf); |
| 1344 |
if (cur_len > PIGZ_BLOCK_SIZE) { |
| 1345 |
memcpy(next->buf, overflow_buf, PIGZ_BLOCK_SIZE); |
| 1346 |
next->len = PIGZ_BLOCK_SIZE; |
| 1347 |
read_ptr = &(overflow_buf[PIGZ_BLOCK_SIZE]); |
| 1348 |
} else { |
| 1349 |
memcpy(next->buf, overflow_buf, cur_len); |
| 1350 |
next->len = cur_len; |
| 1351 |
} |
| 1352 |
overflow_ct = cur_len - next->len; |
| 1353 |
|
| 1354 |
dict = NULL; |
| 1355 |
do { |
| 1356 |
|
| 1357 |
job = (struct job*)malloc(sizeof(struct job)); |
| 1358 |
if (job == NULL) |
| 1359 |
bail("not enough memory", ""); |
| 1360 |
job->calc = new_lock(0); |
| 1361 |
|
| 1362 |
|
| 1363 |
curr = next; |
| 1364 |
|
| 1365 |
|
| 1366 |
next = get_space(&in_pool); |
| 1367 |
if (overflow_ct >= PIGZ_BLOCK_SIZE) { |
| 1368 |
|
| 1369 |
|
| 1370 |
memcpy(next->buf, read_ptr, PIGZ_BLOCK_SIZE); |
| 1371 |
next->len = PIGZ_BLOCK_SIZE; |
| 1372 |
read_ptr = &(read_ptr[PIGZ_BLOCK_SIZE]); |
| 1373 |
overflow_ct -= PIGZ_BLOCK_SIZE; |
| 1374 |
} else { |
| 1375 |
if (overflow_ct) { |
| 1376 |
memcpy(overflow_buf, read_ptr, overflow_ct); |
| 1377 |
} |
| 1378 |
cur_len = emitn(overflow_ct, overflow_buf); |
| 1379 |
if (cur_len > PIGZ_BLOCK_SIZE) { |
| 1380 |
memcpy(next->buf, overflow_buf, PIGZ_BLOCK_SIZE); |
| 1381 |
next->len = PIGZ_BLOCK_SIZE; |
| 1382 |
read_ptr = &(overflow_buf[PIGZ_BLOCK_SIZE]); |
| 1383 |
} else { |
| 1384 |
memcpy(next->buf, overflow_buf, cur_len); |
| 1385 |
next->len = cur_len; |
| 1386 |
} |
| 1387 |
overflow_ct = cur_len - next->len; |
| 1388 |
} |
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
job->lens = NULL; |
| 1393 |
|
| 1394 |
|
| 1395 |
job->in = curr; |
| 1396 |
|
| 1397 |
|
| 1398 |
more = next->len != 0; |
| 1399 |
job->more = more; |
| 1400 |
|
| 1401 |
|
| 1402 |
job->out = dict; |
| 1403 |
if (more) { |
| 1404 |
if (curr->len >= DICT || job->out == NULL) { |
| 1405 |
dict = curr; |
| 1406 |
use_space(dict); |
| 1407 |
} |
| 1408 |
else { |
| 1409 |
dict = get_space(&dict_pool); |
| 1410 |
len = DICT - curr->len; |
| 1411 |
memcpy(dict->buf, job->out->buf + (job->out->len - len), len); |
| 1412 |
memcpy(dict->buf + len, curr->buf, curr->len); |
| 1413 |
dict->len = DICT; |
| 1414 |
} |
| 1415 |
} |
| 1416 |
|
| 1417 |
|
| 1418 |
job->seq = seq; |
| 1419 |
if (++seq < 1) |
| 1420 |
bail("input too long: ", ""); |
| 1421 |
|
| 1422 |
|
| 1423 |
if (cthreads < seq && cthreads < g.procs) { |
| 1424 |
(void)launch(compress_thread, NULL); |
| 1425 |
cthreads++; |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
possess(compress_have); |
| 1430 |
job->next = NULL; |
| 1431 |
*compress_tail = job; |
| 1432 |
compress_tail = &(job->next); |
| 1433 |
twist(compress_have, BY, +1); |
| 1434 |
} while (more); |
| 1435 |
drop_space(next); |
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
join(writeth); |
| 1440 |
writeth = NULL; |
| 1441 |
finish_jobs(); |
| 1442 |
} |
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
int32_t pzwrite_init(char* out_fname, unsigned char* overflow_buf, uint32_t do_append, Pigz_state* ps_ptr) { |
| 1447 |
|
| 1448 |
ps_ptr->outd = open(out_fname, O_WRONLY | (do_append? O_APPEND : (O_CREAT | O_TRUNC)), 0644); |
| 1449 |
if (ps_ptr->outd == -1) { |
| 1450 |
putc_unlocked('\n', stdout); |
| 1451 |
fflush(stdout); |
| 1452 |
fprintf(stderr, "Error: Failed to open %s.\n", out_fname); |
| 1453 |
return 2; |
| 1454 |
} |
| 1455 |
ps_ptr->overflow_buf = overflow_buf; |
| 1456 |
return 0; |
| 1457 |
} |
| 1458 |
|
| 1459 |
void compressed_pzwrite_init(char* out_fname, unsigned char* overflow_buf, uint32_t do_append, Pigz_state* ps_ptr) { |
| 1460 |
ps_ptr->outd = -1; |
| 1461 |
g.outf = out_fname; |
| 1462 |
g.outd = open(g.outf, O_WRONLY | (do_append? O_APPEND : (O_CREAT | O_TRUNC)), 0644); |
| 1463 |
|
| 1464 |
|
| 1465 |
setup_jobs(); |
| 1466 |
|
| 1467 |
|
| 1468 |
writeth = launch(write_thread, NULL); |
| 1469 |
|
| 1470 |
ps_ptr->overflow_buf = overflow_buf; |
| 1471 |
ps_ptr->next = NULL; |
| 1472 |
} |
| 1473 |
|
| 1474 |
int32_t flex_pzwrite_init(uint32_t output_gz, char* out_fname, unsigned char* overflow_buf, uint32_t do_append, Pigz_state* ps_ptr) { |
| 1475 |
if (!output_gz) { |
| 1476 |
return pzwrite_init(out_fname, overflow_buf, do_append, ps_ptr); |
| 1477 |
} else { |
| 1478 |
compressed_pzwrite_init(out_fname, overflow_buf, do_append, ps_ptr); |
| 1479 |
return 0; |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
int32_t force_pzwrite(Pigz_state* ps_ptr, char** writep_ptr, uint32_t write_min) { |
| 1484 |
unsigned char* writep = (unsigned char*)(*writep_ptr); |
| 1485 |
unsigned char* buf = ps_ptr->overflow_buf; |
| 1486 |
uint32_t len = (uintptr_t)(writep - buf); |
| 1487 |
ssize_t ret; |
| 1488 |
while (len) { |
| 1489 |
ret = write(ps_ptr->outd, ps_ptr->overflow_buf, len); |
| 1490 |
if (ret < 1) { |
| 1491 |
return 6; |
| 1492 |
} |
| 1493 |
buf += ret; |
| 1494 |
len -= ret; |
| 1495 |
} |
| 1496 |
*writep_ptr = (char*)(ps_ptr->overflow_buf); |
| 1497 |
return 0; |
| 1498 |
} |
| 1499 |
|
| 1500 |
void force_compressed_pzwrite(Pigz_state* ps_ptr, char** writep_ptr, uint32_t write_min) { |
| 1501 |
|
| 1502 |
|
| 1503 |
unsigned char* writep = (unsigned char*)(*writep_ptr); |
| 1504 |
unsigned char* readp = ps_ptr->overflow_buf; |
| 1505 |
uint32_t cur_len = (uintptr_t)(writep - readp); |
| 1506 |
|
| 1507 |
struct space* curr; |
| 1508 |
struct job *job; |
| 1509 |
|
| 1510 |
int more; |
| 1511 |
size_t len; |
| 1512 |
if (!ps_ptr->next) { |
| 1513 |
ps_ptr->seq = 0; |
| 1514 |
ps_ptr->next = get_space(&in_pool); |
| 1515 |
if (cur_len > PIGZ_BLOCK_SIZE) { |
| 1516 |
memcpy(ps_ptr->next->buf, readp, PIGZ_BLOCK_SIZE); |
| 1517 |
ps_ptr->next->len = PIGZ_BLOCK_SIZE; |
| 1518 |
readp = &(readp[PIGZ_BLOCK_SIZE]); |
| 1519 |
cur_len -= PIGZ_BLOCK_SIZE; |
| 1520 |
} else { |
| 1521 |
memcpy(ps_ptr->next->buf, readp, cur_len); |
| 1522 |
ps_ptr->next->len = cur_len; |
| 1523 |
readp = writep; |
| 1524 |
cur_len = 0; |
| 1525 |
} |
| 1526 |
ps_ptr->dict = NULL; |
| 1527 |
if ((cur_len <= PIGZ_BLOCK_SIZE) && write_min) { |
| 1528 |
|
| 1529 |
if (cur_len) { |
| 1530 |
memcpy(ps_ptr->overflow_buf, readp, cur_len); |
| 1531 |
} |
| 1532 |
*writep_ptr = (char*)(&(ps_ptr->overflow_buf[cur_len])); |
| 1533 |
return; |
| 1534 |
} |
| 1535 |
} |
| 1536 |
|
| 1537 |
do { |
| 1538 |
|
| 1539 |
job = (struct job*)malloc(sizeof(struct job)); |
| 1540 |
if (job == NULL) { |
| 1541 |
bail("not enough memory", ""); |
| 1542 |
} |
| 1543 |
job->calc = new_lock(0); |
| 1544 |
curr = ps_ptr->next; |
| 1545 |
ps_ptr->next = get_space(&in_pool); |
| 1546 |
if (cur_len > PIGZ_BLOCK_SIZE) { |
| 1547 |
memcpy(ps_ptr->next->buf, readp, PIGZ_BLOCK_SIZE); |
| 1548 |
ps_ptr->next->len = PIGZ_BLOCK_SIZE; |
| 1549 |
readp = &(readp[PIGZ_BLOCK_SIZE]); |
| 1550 |
} else { |
| 1551 |
memcpy(ps_ptr->next->buf, readp, cur_len); |
| 1552 |
ps_ptr->next->len = cur_len; |
| 1553 |
readp = writep; |
| 1554 |
} |
| 1555 |
job->lens = NULL; |
| 1556 |
job->in = curr; |
| 1557 |
more = (cur_len != 0); |
| 1558 |
job->more = more; |
| 1559 |
job->out = ps_ptr->dict; |
| 1560 |
if (more) { |
| 1561 |
if (curr->len >= DICT || job->out == NULL) { |
| 1562 |
ps_ptr->dict = curr; |
| 1563 |
use_space(ps_ptr->dict); |
| 1564 |
} else { |
| 1565 |
ps_ptr->dict = get_space(&dict_pool); |
| 1566 |
len = DICT - curr->len; |
| 1567 |
memcpy(ps_ptr->dict->buf, job->out->buf + (job->out->len - len), len); |
| 1568 |
memcpy(ps_ptr->dict->buf + len, curr->buf, curr->len); |
| 1569 |
ps_ptr->dict->len = DICT; |
| 1570 |
} |
| 1571 |
} |
| 1572 |
job->seq = ps_ptr->seq; |
| 1573 |
if (++(ps_ptr->seq) < 1) { |
| 1574 |
bail("input too long: ", ""); |
| 1575 |
} |
| 1576 |
if (cthreads < ps_ptr->seq && cthreads < g.procs) { |
| 1577 |
(void)launch(compress_thread, NULL); |
| 1578 |
cthreads++; |
| 1579 |
} |
| 1580 |
possess(compress_have); |
| 1581 |
job->next = NULL; |
| 1582 |
*compress_tail = job; |
| 1583 |
compress_tail = &(job->next); |
| 1584 |
twist(compress_have, BY, +1); |
| 1585 |
cur_len = (uintptr_t)(writep - readp); |
| 1586 |
} while ((cur_len >= write_min) && more); |
| 1587 |
if (cur_len) { |
| 1588 |
memcpy(ps_ptr->overflow_buf, readp, cur_len); |
| 1589 |
} |
| 1590 |
*writep_ptr = (char*)(&(ps_ptr->overflow_buf[cur_len])); |
| 1591 |
} |
| 1592 |
|
| 1593 |
int32_t flex_pzputs_std(Pigz_state* ps_ptr, char** writep_ptr, char* ss, uint32_t sslen) { |
| 1594 |
unsigned char* writep = (unsigned char*)(*writep_ptr); |
| 1595 |
unsigned char* readp = (unsigned char*)ss; |
| 1596 |
uint32_t cur_write_pos = (uintptr_t)(writep - ps_ptr->overflow_buf); |
| 1597 |
uint32_t delta; |
| 1598 |
int32_t ii; |
| 1599 |
while ((sslen + cur_write_pos) > PIGZ_BLOCK_SIZE) { |
| 1600 |
if (cur_write_pos <= PIGZ_BLOCK_SIZE) { |
| 1601 |
delta = PIGZ_BLOCK_SIZE + 1 - cur_write_pos; |
| 1602 |
memcpy(writep, readp, delta); |
| 1603 |
writep = &(writep[delta]); |
| 1604 |
readp = &(readp[delta]); |
| 1605 |
sslen -= delta; |
| 1606 |
} |
| 1607 |
if (is_uncompressed_pzwrite(ps_ptr)) { |
| 1608 |
ii = force_pzwrite(ps_ptr, (char**)(&writep), PIGZ_BLOCK_SIZE + 1); |
| 1609 |
if (ii) { |
| 1610 |
return ii; |
| 1611 |
} |
| 1612 |
} else { |
| 1613 |
force_compressed_pzwrite(ps_ptr, (char**)(&writep), PIGZ_BLOCK_SIZE + 1); |
| 1614 |
} |
| 1615 |
cur_write_pos = (uintptr_t)(writep - ps_ptr->overflow_buf); |
| 1616 |
} |
| 1617 |
memcpy(writep, readp, sslen); |
| 1618 |
*writep_ptr = (char*)(&(writep[sslen])); |
| 1619 |
return flex_pzwrite(ps_ptr, writep_ptr); |
| 1620 |
} |
| 1621 |
|
| 1622 |
int32_t pzwrite_close_null(Pigz_state* ps_ptr, char* writep) { |
| 1623 |
int32_t ii = force_pzwrite(ps_ptr, &writep, 0); |
| 1624 |
int32_t jj = close(ps_ptr->outd); |
| 1625 |
ps_ptr->overflow_buf = NULL; |
| 1626 |
return ii || jj; |
| 1627 |
} |
| 1628 |
|
| 1629 |
void compressed_pzwrite_close_null(Pigz_state* ps_ptr, char* writep) { |
| 1630 |
force_compressed_pzwrite(ps_ptr, &writep, 0); |
| 1631 |
drop_space(ps_ptr->next); |
| 1632 |
|
| 1633 |
|
| 1634 |
join(writeth); |
| 1635 |
writeth = NULL; |
| 1636 |
finish_jobs(); |
| 1637 |
ps_ptr->overflow_buf = NULL; |
| 1638 |
} |
| 1639 |
|
| 1640 |
int32_t flex_pzwrite_close_null(Pigz_state* ps_ptr, char* writep) { |
| 1641 |
if (is_uncompressed_pzwrite(ps_ptr)) { |
| 1642 |
return pzwrite_close_null(ps_ptr, writep); |
| 1643 |
} else { |
| 1644 |
compressed_pzwrite_close_null(ps_ptr, writep); |
| 1645 |
return 0; |
| 1646 |
} |
| 1647 |
} |
| 1648 |
#endif |
| 1649 |
|
| 1650 |
|
| 1651 |
local void cut_short(int sig) |
| 1652 |
{ |
| 1653 |
(void)sig; |
| 1654 |
if (g.outd != -1 && g.outf != NULL) |
| 1655 |
unlink(g.outf); |
| 1656 |
putc_unlocked('\n', stdout); |
| 1657 |
_exit(1); |
| 1658 |
} |
| 1659 |
|
| 1660 |
|
| 1661 |
void pigz_init(uint32_t setprocs) |
| 1662 |
{ |
| 1663 |
signal(SIGINT, cut_short); |
| 1664 |
g.level = Z_DEFAULT_COMPRESSION; |
| 1665 |
#ifdef NOTHREAD |
| 1666 |
g.procs = 1; |
| 1667 |
#else |
| 1668 |
g.procs = setprocs; |
| 1669 |
|
| 1670 |
if (g.procs > 127) { |
| 1671 |
g.procs = 127; |
| 1672 |
} |
| 1673 |
#endif |
| 1674 |
yarn_prefix = g.prog; |
| 1675 |
yarn_abort = cut_short; |
| 1676 |
g.block = PIGZ_BLOCK_SIZE; |
| 1677 |
g.verbosity = 1; |
| 1678 |
} |
| 1679 |
#endif |
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
int32_t write_uncompressed(char* out_fname, unsigned char* overflow_buf, uint32_t do_append, uint32_t(* emitn)(uint32_t, unsigned char*)) { |
| 1684 |
uint32_t overflow_ct = 0; |
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
FILE* outfile = fopen(out_fname, do_append? "a" : "w"); |
| 1689 |
unsigned char* write_ptr; |
| 1690 |
uint32_t last_size; |
| 1691 |
if (!outfile) { |
| 1692 |
putc_unlocked('\n', stdout); |
| 1693 |
fflush(stdout); |
| 1694 |
fprintf(stderr, "Error: Failed to open %s.\n", out_fname); |
| 1695 |
return 2; |
| 1696 |
} |
| 1697 |
do { |
| 1698 |
last_size = emitn(overflow_ct, overflow_buf); |
| 1699 |
if (last_size > PIGZ_BLOCK_SIZE) { |
| 1700 |
overflow_ct = last_size - PIGZ_BLOCK_SIZE; |
| 1701 |
last_size = PIGZ_BLOCK_SIZE; |
| 1702 |
} else { |
| 1703 |
overflow_ct = 0; |
| 1704 |
} |
| 1705 |
if (last_size) { |
| 1706 |
if (!fwrite(overflow_buf, last_size, 1, outfile)) { |
| 1707 |
putc_unlocked('\n', stdout); |
| 1708 |
fflush(stdout); |
| 1709 |
fputs("Error: File write failure.\n", stderr); |
| 1710 |
fclose(outfile); |
| 1711 |
return 6; |
| 1712 |
} |
| 1713 |
} |
| 1714 |
if (overflow_ct) { |
| 1715 |
write_ptr = &(overflow_buf[PIGZ_BLOCK_SIZE]); |
| 1716 |
while (overflow_ct > PIGZ_BLOCK_SIZE) { |
| 1717 |
if (!fwrite(write_ptr, PIGZ_BLOCK_SIZE, 1, outfile)) { |
| 1718 |
putc_unlocked('\n', stdout); |
| 1719 |
fflush(stdout); |
| 1720 |
fputs("Error: File write failure.\n", stderr); |
| 1721 |
fclose(outfile); |
| 1722 |
return 6; |
| 1723 |
} |
| 1724 |
write_ptr = &(write_ptr[PIGZ_BLOCK_SIZE]); |
| 1725 |
overflow_ct -= PIGZ_BLOCK_SIZE; |
| 1726 |
} |
| 1727 |
memcpy(overflow_buf, write_ptr, overflow_ct); |
| 1728 |
} |
| 1729 |
} while (last_size); |
| 1730 |
if (fclose(outfile)) { |
| 1731 |
putc_unlocked('\n', stdout); |
| 1732 |
fflush(stdout); |
| 1733 |
fputs("Error: File write failure.\n", stderr); |
| 1734 |
return 6; |
| 1735 |
} |
| 1736 |
return 0; |
| 1737 |
} |