OLD | NEW |
(Empty) | |
| 1 /////////////////////////////////////////////////////////////////////////////// |
| 2 // |
| 3 /// \file coder.c |
| 4 /// \brief Compresses or uncompresses a file |
| 5 // |
| 6 // Author: Lasse Collin |
| 7 // |
| 8 // This file has been put into the public domain. |
| 9 // You can do whatever you want with this file. |
| 10 // |
| 11 /////////////////////////////////////////////////////////////////////////////// |
| 12 |
| 13 #include "private.h" |
| 14 |
| 15 |
| 16 /// Return value type for coder_init(). |
| 17 enum coder_init_ret { |
| 18 CODER_INIT_NORMAL, |
| 19 CODER_INIT_PASSTHRU, |
| 20 CODER_INIT_ERROR, |
| 21 }; |
| 22 |
| 23 |
| 24 enum operation_mode opt_mode = MODE_COMPRESS; |
| 25 enum format_type opt_format = FORMAT_AUTO; |
| 26 bool opt_auto_adjust = true; |
| 27 |
| 28 |
| 29 /// Stream used to communicate with liblzma |
| 30 static lzma_stream strm = LZMA_STREAM_INIT; |
| 31 |
| 32 /// Filters needed for all encoding all formats, and also decoding in raw data |
| 33 static lzma_filter filters[LZMA_FILTERS_MAX + 1]; |
| 34 |
| 35 /// Input and output buffers |
| 36 static io_buf in_buf; |
| 37 static io_buf out_buf; |
| 38 |
| 39 /// Number of filters. Zero indicates that we are using a preset. |
| 40 static size_t filters_count = 0; |
| 41 |
| 42 /// Number of the preset (0-9) |
| 43 static size_t preset_number = 6; |
| 44 |
| 45 /// Indicate if no preset has been explicitly given. In that case, if we need |
| 46 /// to auto-adjust for lower memory usage, we won't print a warning. |
| 47 static bool preset_default = true; |
| 48 |
| 49 /// If a preset is used (no custom filter chain) and preset_extreme is true, |
| 50 /// a significantly slower compression is used to achieve slightly better |
| 51 /// compression ratio. |
| 52 static bool preset_extreme = false; |
| 53 |
| 54 /// Integrity check type |
| 55 static lzma_check check; |
| 56 |
| 57 /// This becomes false if the --check=CHECK option is used. |
| 58 static bool check_default = true; |
| 59 |
| 60 |
| 61 extern void |
| 62 coder_set_check(lzma_check new_check) |
| 63 { |
| 64 check = new_check; |
| 65 check_default = false; |
| 66 return; |
| 67 } |
| 68 |
| 69 |
| 70 extern void |
| 71 coder_set_preset(size_t new_preset) |
| 72 { |
| 73 preset_number = new_preset; |
| 74 preset_default = false; |
| 75 return; |
| 76 } |
| 77 |
| 78 |
| 79 extern void |
| 80 coder_set_extreme(void) |
| 81 { |
| 82 preset_extreme = true; |
| 83 return; |
| 84 } |
| 85 |
| 86 |
| 87 extern void |
| 88 coder_add_filter(lzma_vli id, void *options) |
| 89 { |
| 90 if (filters_count == LZMA_FILTERS_MAX) |
| 91 message_fatal(_("Maximum number of filters is four")); |
| 92 |
| 93 filters[filters_count].id = id; |
| 94 filters[filters_count].options = options; |
| 95 ++filters_count; |
| 96 |
| 97 return; |
| 98 } |
| 99 |
| 100 |
| 101 static void lzma_attribute((noreturn)) |
| 102 memlimit_too_small(uint64_t memory_usage) |
| 103 { |
| 104 message(V_ERROR, _("Memory usage limit is too low for the given " |
| 105 "filter setup.")); |
| 106 message_mem_needed(V_ERROR, memory_usage); |
| 107 tuklib_exit(E_ERROR, E_ERROR, false); |
| 108 } |
| 109 |
| 110 |
| 111 extern void |
| 112 coder_set_compression_settings(void) |
| 113 { |
| 114 // Options for LZMA1 or LZMA2 in case we are using a preset. |
| 115 static lzma_options_lzma opt_lzma; |
| 116 |
| 117 if (filters_count == 0) { |
| 118 // We are using a preset. This is not a good idea in raw mode |
| 119 // except when playing around with things. Different versions |
| 120 // of this software may use different options in presets, and |
| 121 // thus make uncompressing the raw data difficult. |
| 122 if (opt_format == FORMAT_RAW) { |
| 123 // The message is shown only if warnings are allowed |
| 124 // but the exit status isn't changed. |
| 125 message(V_WARNING, _("Using a preset in raw mode " |
| 126 "is discouraged.")); |
| 127 message(V_WARNING, _("The exact options of the " |
| 128 "presets may vary between software " |
| 129 "versions.")); |
| 130 } |
| 131 |
| 132 // Get the preset for LZMA1 or LZMA2. |
| 133 if (preset_extreme) |
| 134 preset_number |= LZMA_PRESET_EXTREME; |
| 135 |
| 136 if (lzma_lzma_preset(&opt_lzma, preset_number)) |
| 137 message_bug(); |
| 138 |
| 139 // Use LZMA2 except with --format=lzma we use LZMA1. |
| 140 filters[0].id = opt_format == FORMAT_LZMA |
| 141 ? LZMA_FILTER_LZMA1 : LZMA_FILTER_LZMA2; |
| 142 filters[0].options = &opt_lzma; |
| 143 filters_count = 1; |
| 144 } else { |
| 145 preset_default = false; |
| 146 } |
| 147 |
| 148 // Terminate the filter options array. |
| 149 filters[filters_count].id = LZMA_VLI_UNKNOWN; |
| 150 |
| 151 // If we are using the .lzma format, allow exactly one filter |
| 152 // which has to be LZMA1. |
| 153 if (opt_format == FORMAT_LZMA && (filters_count != 1 |
| 154 || filters[0].id != LZMA_FILTER_LZMA1)) |
| 155 message_fatal(_("The .lzma format supports only " |
| 156 "the LZMA1 filter")); |
| 157 |
| 158 // If we are using the .xz format, make sure that there is no LZMA1 |
| 159 // filter to prevent LZMA_PROG_ERROR. |
| 160 if (opt_format == FORMAT_XZ) |
| 161 for (size_t i = 0; i < filters_count; ++i) |
| 162 if (filters[i].id == LZMA_FILTER_LZMA1) |
| 163 message_fatal(_("LZMA1 cannot be used " |
| 164 "with the .xz format")); |
| 165 |
| 166 // Print the selected filter chain. |
| 167 message_filters_show(V_DEBUG, filters); |
| 168 |
| 169 // If using --format=raw, we can be decoding. The memusage function |
| 170 // also validates the filter chain and the options used for the |
| 171 // filters. |
| 172 const uint64_t memory_limit = hardware_memlimit_get(); |
| 173 uint64_t memory_usage; |
| 174 if (opt_mode == MODE_COMPRESS) |
| 175 memory_usage = lzma_raw_encoder_memusage(filters); |
| 176 else |
| 177 memory_usage = lzma_raw_decoder_memusage(filters); |
| 178 |
| 179 if (memory_usage == UINT64_MAX) |
| 180 message_fatal(_("Unsupported filter chain or filter options")); |
| 181 |
| 182 // Print memory usage info before possible dictionary |
| 183 // size auto-adjusting. |
| 184 message_mem_needed(V_DEBUG, memory_usage); |
| 185 |
| 186 if (memory_usage > memory_limit) { |
| 187 // If --no-auto-adjust was used or we didn't find LZMA1 or |
| 188 // LZMA2 as the last filter, give an error immediately. |
| 189 // --format=raw implies --no-auto-adjust. |
| 190 if (!opt_auto_adjust || opt_format == FORMAT_RAW) |
| 191 memlimit_too_small(memory_usage); |
| 192 |
| 193 assert(opt_mode == MODE_COMPRESS); |
| 194 |
| 195 // Look for the last filter if it is LZMA2 or LZMA1, so |
| 196 // we can make it use less RAM. With other filters we don't |
| 197 // know what to do. |
| 198 size_t i = 0; |
| 199 while (filters[i].id != LZMA_FILTER_LZMA2 |
| 200 && filters[i].id != LZMA_FILTER_LZMA1) { |
| 201 if (filters[i].id == LZMA_VLI_UNKNOWN) |
| 202 memlimit_too_small(memory_usage); |
| 203 |
| 204 ++i; |
| 205 } |
| 206 |
| 207 // Decrease the dictionary size until we meet the memory |
| 208 // usage limit. First round down to full mebibytes. |
| 209 lzma_options_lzma *opt = filters[i].options; |
| 210 const uint32_t orig_dict_size = opt->dict_size; |
| 211 opt->dict_size &= ~((UINT32_C(1) << 20) - 1); |
| 212 while (true) { |
| 213 // If it is below 1 MiB, auto-adjusting failed. We |
| 214 // could be more sophisticated and scale it down even |
| 215 // more, but let's see if many complain about this |
| 216 // version. |
| 217 // |
| 218 // FIXME: Displays the scaled memory usage instead |
| 219 // of the original. |
| 220 if (opt->dict_size < (UINT32_C(1) << 20)) |
| 221 memlimit_too_small(memory_usage); |
| 222 |
| 223 memory_usage = lzma_raw_encoder_memusage(filters); |
| 224 if (memory_usage == UINT64_MAX) |
| 225 message_bug(); |
| 226 |
| 227 // Accept it if it is low enough. |
| 228 if (memory_usage <= memory_limit) |
| 229 break; |
| 230 |
| 231 // Otherwise 1 MiB down and try again. I hope this |
| 232 // isn't too slow method for cases where the original |
| 233 // dict_size is very big. |
| 234 opt->dict_size -= UINT32_C(1) << 20; |
| 235 } |
| 236 |
| 237 // Tell the user that we decreased the dictionary size. |
| 238 // However, omit the message if no preset or custom chain |
| 239 // was given. FIXME: Always warn? |
| 240 if (!preset_default) |
| 241 message(V_WARNING, _("Adjusted LZMA%c dictionary size " |
| 242 "from %s MiB to %s MiB to not exceed " |
| 243 "the memory usage limit of %s MiB"), |
| 244 filters[i].id == LZMA_FILTER_LZMA2 |
| 245 ? '2' : '1', |
| 246 uint64_to_str(orig_dict_size >> 20, 0), |
| 247 uint64_to_str(opt->dict_size >> 20, 1), |
| 248 uint64_to_str(round_up_to_mib( |
| 249 memory_limit), 2)); |
| 250 } |
| 251 |
| 252 /* |
| 253 // Limit the number of worker threads so that memory usage |
| 254 // limit isn't exceeded. |
| 255 assert(memory_usage > 0); |
| 256 size_t thread_limit = memory_limit / memory_usage; |
| 257 if (thread_limit == 0) |
| 258 thread_limit = 1; |
| 259 |
| 260 if (opt_threads > thread_limit) |
| 261 opt_threads = thread_limit; |
| 262 */ |
| 263 |
| 264 if (check_default) { |
| 265 // The default check type is CRC64, but fallback to CRC32 |
| 266 // if CRC64 isn't supported by the copy of liblzma we are |
| 267 // using. CRC32 is always supported. |
| 268 check = LZMA_CHECK_CRC64; |
| 269 if (!lzma_check_is_supported(check)) |
| 270 check = LZMA_CHECK_CRC32; |
| 271 } |
| 272 |
| 273 return; |
| 274 } |
| 275 |
| 276 |
| 277 /// Return true if the data in in_buf seems to be in the .xz format. |
| 278 static bool |
| 279 is_format_xz(void) |
| 280 { |
| 281 return strm.avail_in >= 6 && memcmp(in_buf.u8, "\3757zXZ", 6) == 0; |
| 282 } |
| 283 |
| 284 |
| 285 /// Return true if the data in in_buf seems to be in the .lzma format. |
| 286 static bool |
| 287 is_format_lzma(void) |
| 288 { |
| 289 // The .lzma header is 13 bytes. |
| 290 if (strm.avail_in < 13) |
| 291 return false; |
| 292 |
| 293 // Decode the LZMA1 properties. |
| 294 lzma_filter filter = { .id = LZMA_FILTER_LZMA1 }; |
| 295 if (lzma_properties_decode(&filter, NULL, in_buf.u8, 5) != LZMA_OK) |
| 296 return false; |
| 297 |
| 298 // A hack to ditch tons of false positives: We allow only dictionary |
| 299 // sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone |
| 300 // created only files with 2^n, but accepts any dictionary size. |
| 301 // If someone complains, this will be reconsidered. |
| 302 lzma_options_lzma *opt = filter.options; |
| 303 const uint32_t dict_size = opt->dict_size; |
| 304 free(opt); |
| 305 |
| 306 if (dict_size != UINT32_MAX) { |
| 307 uint32_t d = dict_size - 1; |
| 308 d |= d >> 2; |
| 309 d |= d >> 3; |
| 310 d |= d >> 4; |
| 311 d |= d >> 8; |
| 312 d |= d >> 16; |
| 313 ++d; |
| 314 if (d != dict_size || dict_size == 0) |
| 315 return false; |
| 316 } |
| 317 |
| 318 // Another hack to ditch false positives: Assume that if the |
| 319 // uncompressed size is known, it must be less than 256 GiB. |
| 320 // Again, if someone complains, this will be reconsidered. |
| 321 uint64_t uncompressed_size = 0; |
| 322 for (size_t i = 0; i < 8; ++i) |
| 323 uncompressed_size |= (uint64_t)(in_buf.u8[5 + i]) << (i * 8); |
| 324 |
| 325 if (uncompressed_size != UINT64_MAX |
| 326 && uncompressed_size > (UINT64_C(1) << 38)) |
| 327 return false; |
| 328 |
| 329 return true; |
| 330 } |
| 331 |
| 332 |
| 333 /// Detect the input file type (for now, this done only when decompressing), |
| 334 /// and initialize an appropriate coder. Return value indicates if a normal |
| 335 /// liblzma-based coder was initialized (CODER_INIT_NORMAL), if passthru |
| 336 /// mode should be used (CODER_INIT_PASSTHRU), or if an error occurred |
| 337 /// (CODER_INIT_ERROR). |
| 338 static enum coder_init_ret |
| 339 coder_init(file_pair *pair) |
| 340 { |
| 341 lzma_ret ret = LZMA_PROG_ERROR; |
| 342 |
| 343 if (opt_mode == MODE_COMPRESS) { |
| 344 switch (opt_format) { |
| 345 case FORMAT_AUTO: |
| 346 // args.c ensures this. |
| 347 assert(0); |
| 348 break; |
| 349 |
| 350 case FORMAT_XZ: |
| 351 ret = lzma_stream_encoder(&strm, filters, check); |
| 352 break; |
| 353 |
| 354 case FORMAT_LZMA: |
| 355 ret = lzma_alone_encoder(&strm, filters[0].options); |
| 356 break; |
| 357 |
| 358 case FORMAT_RAW: |
| 359 ret = lzma_raw_encoder(&strm, filters); |
| 360 break; |
| 361 } |
| 362 } else { |
| 363 const uint32_t flags = LZMA_TELL_UNSUPPORTED_CHECK |
| 364 | LZMA_CONCATENATED; |
| 365 |
| 366 // We abuse FORMAT_AUTO to indicate unknown file format, |
| 367 // for which we may consider passthru mode. |
| 368 enum format_type init_format = FORMAT_AUTO; |
| 369 |
| 370 switch (opt_format) { |
| 371 case FORMAT_AUTO: |
| 372 if (is_format_xz()) |
| 373 init_format = FORMAT_XZ; |
| 374 else if (is_format_lzma()) |
| 375 init_format = FORMAT_LZMA; |
| 376 break; |
| 377 |
| 378 case FORMAT_XZ: |
| 379 if (is_format_xz()) |
| 380 init_format = FORMAT_XZ; |
| 381 break; |
| 382 |
| 383 case FORMAT_LZMA: |
| 384 if (is_format_lzma()) |
| 385 init_format = FORMAT_LZMA; |
| 386 break; |
| 387 |
| 388 case FORMAT_RAW: |
| 389 init_format = FORMAT_RAW; |
| 390 break; |
| 391 } |
| 392 |
| 393 switch (init_format) { |
| 394 case FORMAT_AUTO: |
| 395 // Uknown file format. If --decompress --stdout |
| 396 // --force have been given, then we copy the input |
| 397 // as is to stdout. Checking for MODE_DECOMPRESS |
| 398 // is needed, because we don't want to do use |
| 399 // passthru mode with --test. |
| 400 if (opt_mode == MODE_DECOMPRESS |
| 401 && opt_stdout && opt_force) |
| 402 return CODER_INIT_PASSTHRU; |
| 403 |
| 404 ret = LZMA_FORMAT_ERROR; |
| 405 break; |
| 406 |
| 407 case FORMAT_XZ: |
| 408 ret = lzma_stream_decoder(&strm, |
| 409 hardware_memlimit_get(), flags); |
| 410 break; |
| 411 |
| 412 case FORMAT_LZMA: |
| 413 ret = lzma_alone_decoder(&strm, |
| 414 hardware_memlimit_get()); |
| 415 break; |
| 416 |
| 417 case FORMAT_RAW: |
| 418 // Memory usage has already been checked in |
| 419 // coder_set_compression_settings(). |
| 420 ret = lzma_raw_decoder(&strm, filters); |
| 421 break; |
| 422 } |
| 423 |
| 424 // Try to decode the headers. This will catch too low |
| 425 // memory usage limit in case it happens in the first |
| 426 // Block of the first Stream, which is where it very |
| 427 // probably will happen if it is going to happen. |
| 428 if (ret == LZMA_OK && init_format != FORMAT_RAW) { |
| 429 strm.next_out = NULL; |
| 430 strm.avail_out = 0; |
| 431 ret = lzma_code(&strm, LZMA_RUN); |
| 432 } |
| 433 } |
| 434 |
| 435 if (ret != LZMA_OK) { |
| 436 message_error("%s: %s", pair->src_name, message_strm(ret)); |
| 437 if (ret == LZMA_MEMLIMIT_ERROR) |
| 438 message_mem_needed(V_ERROR, lzma_memusage(&strm)); |
| 439 |
| 440 return CODER_INIT_ERROR; |
| 441 } |
| 442 |
| 443 return CODER_INIT_NORMAL; |
| 444 } |
| 445 |
| 446 |
| 447 /// Compress or decompress using liblzma. |
| 448 static bool |
| 449 coder_normal(file_pair *pair) |
| 450 { |
| 451 // Encoder needs to know when we have given all the input to it. |
| 452 // The decoders need to know it too when we are using |
| 453 // LZMA_CONCATENATED. We need to check for src_eof here, because |
| 454 // the first input chunk has been already read, and that may |
| 455 // have been the only chunk we will read. |
| 456 lzma_action action = pair->src_eof ? LZMA_FINISH : LZMA_RUN; |
| 457 |
| 458 lzma_ret ret; |
| 459 |
| 460 // Assume that something goes wrong. |
| 461 bool success = false; |
| 462 |
| 463 strm.next_out = out_buf.u8; |
| 464 strm.avail_out = IO_BUFFER_SIZE; |
| 465 |
| 466 while (!user_abort) { |
| 467 // Fill the input buffer if it is empty and we haven't reached |
| 468 // end of file yet. |
| 469 if (strm.avail_in == 0 && !pair->src_eof) { |
| 470 strm.next_in = in_buf.u8; |
| 471 strm.avail_in = io_read( |
| 472 pair, &in_buf, IO_BUFFER_SIZE); |
| 473 |
| 474 if (strm.avail_in == SIZE_MAX) |
| 475 break; |
| 476 |
| 477 if (pair->src_eof) |
| 478 action = LZMA_FINISH; |
| 479 } |
| 480 |
| 481 // Let liblzma do the actual work. |
| 482 ret = lzma_code(&strm, action); |
| 483 |
| 484 // Write out if the output buffer became full. |
| 485 if (strm.avail_out == 0) { |
| 486 if (opt_mode != MODE_TEST && io_write(pair, &out_buf, |
| 487 IO_BUFFER_SIZE - strm.avail_out)) |
| 488 break; |
| 489 |
| 490 strm.next_out = out_buf.u8; |
| 491 strm.avail_out = IO_BUFFER_SIZE; |
| 492 } |
| 493 |
| 494 if (ret != LZMA_OK) { |
| 495 // Determine if the return value indicates that we |
| 496 // won't continue coding. |
| 497 const bool stop = ret != LZMA_NO_CHECK |
| 498 && ret != LZMA_UNSUPPORTED_CHECK; |
| 499 |
| 500 if (stop) { |
| 501 // Write the remaining bytes even if something |
| 502 // went wrong, because that way the user gets |
| 503 // as much data as possible, which can be good |
| 504 // when trying to get at least some useful |
| 505 // data out of damaged files. |
| 506 if (opt_mode != MODE_TEST && io_write(pair, |
| 507 &out_buf, IO_BUFFER_SIZE |
| 508 - strm.avail_out)) |
| 509 break; |
| 510 } |
| 511 |
| 512 if (ret == LZMA_STREAM_END) { |
| 513 // Check that there is no trailing garbage. |
| 514 // This is needed for LZMA_Alone and raw |
| 515 // streams. |
| 516 if (strm.avail_in == 0 && !pair->src_eof) { |
| 517 // Try reading one more byte. |
| 518 // Hopefully we don't get any more |
| 519 // input, and thus pair->src_eof |
| 520 // becomes true. |
| 521 strm.avail_in = io_read( |
| 522 pair, &in_buf, 1); |
| 523 if (strm.avail_in == SIZE_MAX) |
| 524 break; |
| 525 |
| 526 assert(strm.avail_in == 0 |
| 527 || strm.avail_in == 1); |
| 528 } |
| 529 |
| 530 if (strm.avail_in == 0) { |
| 531 assert(pair->src_eof); |
| 532 success = true; |
| 533 break; |
| 534 } |
| 535 |
| 536 // We hadn't reached the end of the file. |
| 537 ret = LZMA_DATA_ERROR; |
| 538 assert(stop); |
| 539 } |
| 540 |
| 541 // If we get here and stop is true, something went |
| 542 // wrong and we print an error. Otherwise it's just |
| 543 // a warning and coding can continue. |
| 544 if (stop) { |
| 545 message_error("%s: %s", pair->src_name, |
| 546 message_strm(ret)); |
| 547 } else { |
| 548 message_warning("%s: %s", pair->src_name, |
| 549 message_strm(ret)); |
| 550 |
| 551 // When compressing, all possible errors set |
| 552 // stop to true. |
| 553 assert(opt_mode != MODE_COMPRESS); |
| 554 } |
| 555 |
| 556 if (ret == LZMA_MEMLIMIT_ERROR) { |
| 557 // Display how much memory it would have |
| 558 // actually needed. |
| 559 message_mem_needed(V_ERROR, |
| 560 lzma_memusage(&strm)); |
| 561 } |
| 562 |
| 563 if (stop) |
| 564 break; |
| 565 } |
| 566 |
| 567 // Show progress information under certain conditions. |
| 568 message_progress_update(); |
| 569 } |
| 570 |
| 571 return success; |
| 572 } |
| 573 |
| 574 |
| 575 /// Copy from input file to output file without processing the data in any |
| 576 /// way. This is used only when trying to decompress unrecognized files |
| 577 /// with --decompress --stdout --force, so the output is always stdout. |
| 578 static bool |
| 579 coder_passthru(file_pair *pair) |
| 580 { |
| 581 while (strm.avail_in != 0) { |
| 582 if (user_abort) |
| 583 return false; |
| 584 |
| 585 if (io_write(pair, &in_buf, strm.avail_in)) |
| 586 return false; |
| 587 |
| 588 strm.total_in += strm.avail_in; |
| 589 strm.total_out = strm.total_in; |
| 590 message_progress_update(); |
| 591 |
| 592 strm.avail_in = io_read(pair, &in_buf, IO_BUFFER_SIZE); |
| 593 if (strm.avail_in == SIZE_MAX) |
| 594 return false; |
| 595 } |
| 596 |
| 597 return true; |
| 598 } |
| 599 |
| 600 |
| 601 extern void |
| 602 coder_run(const char *filename) |
| 603 { |
| 604 // Set and possibly print the filename for the progress message. |
| 605 message_filename(filename); |
| 606 |
| 607 // Try to open the input file. |
| 608 file_pair *pair = io_open_src(filename); |
| 609 if (pair == NULL) |
| 610 return; |
| 611 |
| 612 // Assume that something goes wrong. |
| 613 bool success = false; |
| 614 |
| 615 // Read the first chunk of input data. This is needed to detect |
| 616 // the input file type (for now, only for decompression). |
| 617 strm.next_in = in_buf.u8; |
| 618 strm.avail_in = io_read(pair, &in_buf, IO_BUFFER_SIZE); |
| 619 |
| 620 if (strm.avail_in != SIZE_MAX) { |
| 621 // Initialize the coder. This will detect the file format |
| 622 // and, in decompression or testing mode, check the memory |
| 623 // usage of the first Block too. This way we don't try to |
| 624 // open the destination file if we see that coding wouldn't |
| 625 // work at all anyway. This also avoids deleting the old |
| 626 // "target" file if --force was used. |
| 627 const enum coder_init_ret init_ret = coder_init(pair); |
| 628 |
| 629 if (init_ret != CODER_INIT_ERROR && !user_abort) { |
| 630 // Don't open the destination file when --test |
| 631 // is used. |
| 632 if (opt_mode == MODE_TEST || !io_open_dest(pair)) { |
| 633 // Initialize the progress indicator. |
| 634 const uint64_t in_size |
| 635 = pair->src_st.st_size <= 0 |
| 636 ? 0 : pair->src_st.st_size; |
| 637 message_progress_start(&strm, in_size); |
| 638 |
| 639 // Do the actual coding or passthru. |
| 640 if (init_ret == CODER_INIT_NORMAL) |
| 641 success = coder_normal(pair); |
| 642 else |
| 643 success = coder_passthru(pair); |
| 644 |
| 645 message_progress_end(success); |
| 646 } |
| 647 } |
| 648 } |
| 649 |
| 650 // Close the file pair. It needs to know if coding was successful to |
| 651 // know if the source or target file should be unlinked. |
| 652 io_close(pair, success); |
| 653 |
| 654 return; |
| 655 } |
OLD | NEW |