coder.c revision 262754
1207753Smm/////////////////////////////////////////////////////////////////////////////// 2207753Smm// 3207753Smm/// \file coder.c 4207753Smm/// \brief Compresses or uncompresses a file 5207753Smm// 6207753Smm// Author: Lasse Collin 7207753Smm// 8207753Smm// This file has been put into the public domain. 9207753Smm// You can do whatever you want with this file. 10207753Smm// 11207753Smm/////////////////////////////////////////////////////////////////////////////// 12207753Smm 13207753Smm#include "private.h" 14207753Smm 15207753Smm 16207753Smm/// Return value type for coder_init(). 17207753Smmenum coder_init_ret { 18207753Smm CODER_INIT_NORMAL, 19207753Smm CODER_INIT_PASSTHRU, 20207753Smm CODER_INIT_ERROR, 21207753Smm}; 22207753Smm 23207753Smm 24207753Smmenum operation_mode opt_mode = MODE_COMPRESS; 25207753Smmenum format_type opt_format = FORMAT_AUTO; 26213700Smmbool opt_auto_adjust = true; 27207753Smm 28207753Smm 29207753Smm/// Stream used to communicate with liblzma 30207753Smmstatic lzma_stream strm = LZMA_STREAM_INIT; 31207753Smm 32207753Smm/// Filters needed for all encoding all formats, and also decoding in raw data 33207753Smmstatic lzma_filter filters[LZMA_FILTERS_MAX + 1]; 34207753Smm 35207753Smm/// Input and output buffers 36207753Smmstatic io_buf in_buf; 37207753Smmstatic io_buf out_buf; 38207753Smm 39207753Smm/// Number of filters. Zero indicates that we are using a preset. 40262754Sdelphijstatic uint32_t filters_count = 0; 41207753Smm 42207753Smm/// Number of the preset (0-9) 43262754Sdelphijstatic uint32_t preset_number = LZMA_PRESET_DEFAULT; 44207753Smm 45207753Smm/// Integrity check type 46207753Smmstatic lzma_check check; 47207753Smm 48207753Smm/// This becomes false if the --check=CHECK option is used. 49207753Smmstatic bool check_default = true; 50207753Smm 51207753Smm 52207753Smmextern void 53207753Smmcoder_set_check(lzma_check new_check) 54207753Smm{ 55207753Smm check = new_check; 56207753Smm check_default = false; 57207753Smm return; 58207753Smm} 59207753Smm 60207753Smm 61262754Sdelphijstatic void 62262754Sdelphijforget_filter_chain(void) 63207753Smm{ 64213700Smm // Setting a preset makes us forget a possibly defined custom 65213700Smm // filter chain. 66213700Smm while (filters_count > 0) { 67213700Smm --filters_count; 68213700Smm free(filters[filters_count].options); 69213700Smm filters[filters_count].options = NULL; 70213700Smm } 71213700Smm 72207753Smm return; 73207753Smm} 74207753Smm 75207753Smm 76207753Smmextern void 77262754Sdelphijcoder_set_preset(uint32_t new_preset) 78262754Sdelphij{ 79262754Sdelphij preset_number &= ~LZMA_PRESET_LEVEL_MASK; 80262754Sdelphij preset_number |= new_preset; 81262754Sdelphij forget_filter_chain(); 82262754Sdelphij return; 83262754Sdelphij} 84262754Sdelphij 85262754Sdelphij 86262754Sdelphijextern void 87207753Smmcoder_set_extreme(void) 88207753Smm{ 89262754Sdelphij preset_number |= LZMA_PRESET_EXTREME; 90262754Sdelphij forget_filter_chain(); 91207753Smm return; 92207753Smm} 93207753Smm 94207753Smm 95207753Smmextern void 96207753Smmcoder_add_filter(lzma_vli id, void *options) 97207753Smm{ 98207753Smm if (filters_count == LZMA_FILTERS_MAX) 99207753Smm message_fatal(_("Maximum number of filters is four")); 100207753Smm 101207753Smm filters[filters_count].id = id; 102207753Smm filters[filters_count].options = options; 103207753Smm ++filters_count; 104207753Smm 105262754Sdelphij // Setting a custom filter chain makes us forget the preset options. 106262754Sdelphij // This makes a difference if one specifies e.g. "xz -9 --lzma2 -e" 107262754Sdelphij // where the custom filter chain resets the preset level back to 108262754Sdelphij // the default 6, making the example equivalent to "xz -6e". 109262754Sdelphij preset_number = LZMA_PRESET_DEFAULT; 110262754Sdelphij 111207753Smm return; 112207753Smm} 113207753Smm 114207753Smm 115223935Smmstatic void lzma_attribute((__noreturn__)) 116207753Smmmemlimit_too_small(uint64_t memory_usage) 117207753Smm{ 118207753Smm message(V_ERROR, _("Memory usage limit is too low for the given " 119207753Smm "filter setup.")); 120207753Smm message_mem_needed(V_ERROR, memory_usage); 121207753Smm tuklib_exit(E_ERROR, E_ERROR, false); 122207753Smm} 123207753Smm 124207753Smm 125207753Smmextern void 126207753Smmcoder_set_compression_settings(void) 127207753Smm{ 128207753Smm // Options for LZMA1 or LZMA2 in case we are using a preset. 129207753Smm static lzma_options_lzma opt_lzma; 130207753Smm 131207753Smm if (filters_count == 0) { 132207753Smm // We are using a preset. This is not a good idea in raw mode 133207753Smm // except when playing around with things. Different versions 134207753Smm // of this software may use different options in presets, and 135207753Smm // thus make uncompressing the raw data difficult. 136207753Smm if (opt_format == FORMAT_RAW) { 137207753Smm // The message is shown only if warnings are allowed 138207753Smm // but the exit status isn't changed. 139207753Smm message(V_WARNING, _("Using a preset in raw mode " 140207753Smm "is discouraged.")); 141207753Smm message(V_WARNING, _("The exact options of the " 142207753Smm "presets may vary between software " 143207753Smm "versions.")); 144207753Smm } 145207753Smm 146207753Smm // Get the preset for LZMA1 or LZMA2. 147207753Smm if (lzma_lzma_preset(&opt_lzma, preset_number)) 148207753Smm message_bug(); 149207753Smm 150207753Smm // Use LZMA2 except with --format=lzma we use LZMA1. 151207753Smm filters[0].id = opt_format == FORMAT_LZMA 152207753Smm ? LZMA_FILTER_LZMA1 : LZMA_FILTER_LZMA2; 153207753Smm filters[0].options = &opt_lzma; 154207753Smm filters_count = 1; 155207753Smm } 156207753Smm 157207753Smm // Terminate the filter options array. 158207753Smm filters[filters_count].id = LZMA_VLI_UNKNOWN; 159207753Smm 160207753Smm // If we are using the .lzma format, allow exactly one filter 161207753Smm // which has to be LZMA1. 162207753Smm if (opt_format == FORMAT_LZMA && (filters_count != 1 163207753Smm || filters[0].id != LZMA_FILTER_LZMA1)) 164207753Smm message_fatal(_("The .lzma format supports only " 165207753Smm "the LZMA1 filter")); 166207753Smm 167207753Smm // If we are using the .xz format, make sure that there is no LZMA1 168207753Smm // filter to prevent LZMA_PROG_ERROR. 169207753Smm if (opt_format == FORMAT_XZ) 170207753Smm for (size_t i = 0; i < filters_count; ++i) 171207753Smm if (filters[i].id == LZMA_FILTER_LZMA1) 172207753Smm message_fatal(_("LZMA1 cannot be used " 173207753Smm "with the .xz format")); 174207753Smm 175207753Smm // Print the selected filter chain. 176213700Smm message_filters_show(V_DEBUG, filters); 177207753Smm 178207753Smm // If using --format=raw, we can be decoding. The memusage function 179207753Smm // also validates the filter chain and the options used for the 180207753Smm // filters. 181213700Smm const uint64_t memory_limit = hardware_memlimit_get(opt_mode); 182207753Smm uint64_t memory_usage; 183207753Smm if (opt_mode == MODE_COMPRESS) 184207753Smm memory_usage = lzma_raw_encoder_memusage(filters); 185207753Smm else 186207753Smm memory_usage = lzma_raw_decoder_memusage(filters); 187207753Smm 188207753Smm if (memory_usage == UINT64_MAX) 189207753Smm message_fatal(_("Unsupported filter chain or filter options")); 190207753Smm 191207753Smm // Print memory usage info before possible dictionary 192207753Smm // size auto-adjusting. 193207753Smm message_mem_needed(V_DEBUG, memory_usage); 194213700Smm if (opt_mode == MODE_COMPRESS) { 195213700Smm const uint64_t decmem = lzma_raw_decoder_memusage(filters); 196213700Smm if (decmem != UINT64_MAX) 197213700Smm message(V_DEBUG, _("Decompression will need " 198213700Smm "%s MiB of memory."), uint64_to_str( 199213700Smm round_up_to_mib(decmem), 0)); 200213700Smm } 201207753Smm 202207753Smm if (memory_usage > memory_limit) { 203207753Smm // If --no-auto-adjust was used or we didn't find LZMA1 or 204207753Smm // LZMA2 as the last filter, give an error immediately. 205207753Smm // --format=raw implies --no-auto-adjust. 206213700Smm if (!opt_auto_adjust || opt_format == FORMAT_RAW) 207207753Smm memlimit_too_small(memory_usage); 208207753Smm 209207753Smm assert(opt_mode == MODE_COMPRESS); 210207753Smm 211207753Smm // Look for the last filter if it is LZMA2 or LZMA1, so 212207753Smm // we can make it use less RAM. With other filters we don't 213207753Smm // know what to do. 214207753Smm size_t i = 0; 215207753Smm while (filters[i].id != LZMA_FILTER_LZMA2 216207753Smm && filters[i].id != LZMA_FILTER_LZMA1) { 217207753Smm if (filters[i].id == LZMA_VLI_UNKNOWN) 218207753Smm memlimit_too_small(memory_usage); 219207753Smm 220207753Smm ++i; 221207753Smm } 222207753Smm 223207753Smm // Decrease the dictionary size until we meet the memory 224207753Smm // usage limit. First round down to full mebibytes. 225207753Smm lzma_options_lzma *opt = filters[i].options; 226207753Smm const uint32_t orig_dict_size = opt->dict_size; 227207753Smm opt->dict_size &= ~((UINT32_C(1) << 20) - 1); 228207753Smm while (true) { 229207753Smm // If it is below 1 MiB, auto-adjusting failed. We 230207753Smm // could be more sophisticated and scale it down even 231207753Smm // more, but let's see if many complain about this 232207753Smm // version. 233207753Smm // 234207753Smm // FIXME: Displays the scaled memory usage instead 235207753Smm // of the original. 236207753Smm if (opt->dict_size < (UINT32_C(1) << 20)) 237207753Smm memlimit_too_small(memory_usage); 238207753Smm 239207753Smm memory_usage = lzma_raw_encoder_memusage(filters); 240207753Smm if (memory_usage == UINT64_MAX) 241207753Smm message_bug(); 242207753Smm 243207753Smm // Accept it if it is low enough. 244207753Smm if (memory_usage <= memory_limit) 245207753Smm break; 246207753Smm 247207753Smm // Otherwise 1 MiB down and try again. I hope this 248207753Smm // isn't too slow method for cases where the original 249207753Smm // dict_size is very big. 250207753Smm opt->dict_size -= UINT32_C(1) << 20; 251207753Smm } 252207753Smm 253207753Smm // Tell the user that we decreased the dictionary size. 254213700Smm message(V_WARNING, _("Adjusted LZMA%c dictionary size " 255213700Smm "from %s MiB to %s MiB to not exceed " 256213700Smm "the memory usage limit of %s MiB"), 257213700Smm filters[i].id == LZMA_FILTER_LZMA2 258213700Smm ? '2' : '1', 259213700Smm uint64_to_str(orig_dict_size >> 20, 0), 260213700Smm uint64_to_str(opt->dict_size >> 20, 1), 261213700Smm uint64_to_str(round_up_to_mib( 262213700Smm memory_limit), 2)); 263207753Smm } 264207753Smm 265207753Smm/* 266207753Smm // Limit the number of worker threads so that memory usage 267207753Smm // limit isn't exceeded. 268207753Smm assert(memory_usage > 0); 269207753Smm size_t thread_limit = memory_limit / memory_usage; 270207753Smm if (thread_limit == 0) 271207753Smm thread_limit = 1; 272207753Smm 273207753Smm if (opt_threads > thread_limit) 274207753Smm opt_threads = thread_limit; 275207753Smm*/ 276207753Smm 277207753Smm if (check_default) { 278207753Smm // The default check type is CRC64, but fallback to CRC32 279207753Smm // if CRC64 isn't supported by the copy of liblzma we are 280207753Smm // using. CRC32 is always supported. 281207753Smm check = LZMA_CHECK_CRC64; 282207753Smm if (!lzma_check_is_supported(check)) 283207753Smm check = LZMA_CHECK_CRC32; 284207753Smm } 285207753Smm 286207753Smm return; 287207753Smm} 288207753Smm 289207753Smm 290207753Smm/// Return true if the data in in_buf seems to be in the .xz format. 291207753Smmstatic bool 292207753Smmis_format_xz(void) 293207753Smm{ 294244601Smm // Specify the magic as hex to be compatible with EBCDIC systems. 295244601Smm static const uint8_t magic[6] = { 0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00 }; 296244601Smm return strm.avail_in >= sizeof(magic) 297244601Smm && memcmp(in_buf.u8, magic, sizeof(magic)) == 0; 298207753Smm} 299207753Smm 300207753Smm 301207753Smm/// Return true if the data in in_buf seems to be in the .lzma format. 302207753Smmstatic bool 303207753Smmis_format_lzma(void) 304207753Smm{ 305207753Smm // The .lzma header is 13 bytes. 306207753Smm if (strm.avail_in < 13) 307207753Smm return false; 308207753Smm 309207753Smm // Decode the LZMA1 properties. 310207753Smm lzma_filter filter = { .id = LZMA_FILTER_LZMA1 }; 311207753Smm if (lzma_properties_decode(&filter, NULL, in_buf.u8, 5) != LZMA_OK) 312207753Smm return false; 313207753Smm 314207753Smm // A hack to ditch tons of false positives: We allow only dictionary 315207753Smm // sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone 316207753Smm // created only files with 2^n, but accepts any dictionary size. 317207753Smm // If someone complains, this will be reconsidered. 318207753Smm lzma_options_lzma *opt = filter.options; 319207753Smm const uint32_t dict_size = opt->dict_size; 320207753Smm free(opt); 321207753Smm 322207753Smm if (dict_size != UINT32_MAX) { 323207753Smm uint32_t d = dict_size - 1; 324207753Smm d |= d >> 2; 325207753Smm d |= d >> 3; 326207753Smm d |= d >> 4; 327207753Smm d |= d >> 8; 328207753Smm d |= d >> 16; 329207753Smm ++d; 330207753Smm if (d != dict_size || dict_size == 0) 331207753Smm return false; 332207753Smm } 333207753Smm 334207753Smm // Another hack to ditch false positives: Assume that if the 335207753Smm // uncompressed size is known, it must be less than 256 GiB. 336207753Smm // Again, if someone complains, this will be reconsidered. 337207753Smm uint64_t uncompressed_size = 0; 338207753Smm for (size_t i = 0; i < 8; ++i) 339207753Smm uncompressed_size |= (uint64_t)(in_buf.u8[5 + i]) << (i * 8); 340207753Smm 341207753Smm if (uncompressed_size != UINT64_MAX 342207753Smm && uncompressed_size > (UINT64_C(1) << 38)) 343207753Smm return false; 344207753Smm 345207753Smm return true; 346207753Smm} 347207753Smm 348207753Smm 349207753Smm/// Detect the input file type (for now, this done only when decompressing), 350207753Smm/// and initialize an appropriate coder. Return value indicates if a normal 351207753Smm/// liblzma-based coder was initialized (CODER_INIT_NORMAL), if passthru 352207753Smm/// mode should be used (CODER_INIT_PASSTHRU), or if an error occurred 353207753Smm/// (CODER_INIT_ERROR). 354207753Smmstatic enum coder_init_ret 355207753Smmcoder_init(file_pair *pair) 356207753Smm{ 357207753Smm lzma_ret ret = LZMA_PROG_ERROR; 358207753Smm 359207753Smm if (opt_mode == MODE_COMPRESS) { 360207753Smm switch (opt_format) { 361207753Smm case FORMAT_AUTO: 362207753Smm // args.c ensures this. 363207753Smm assert(0); 364207753Smm break; 365207753Smm 366207753Smm case FORMAT_XZ: 367207753Smm ret = lzma_stream_encoder(&strm, filters, check); 368207753Smm break; 369207753Smm 370207753Smm case FORMAT_LZMA: 371207753Smm ret = lzma_alone_encoder(&strm, filters[0].options); 372207753Smm break; 373207753Smm 374207753Smm case FORMAT_RAW: 375207753Smm ret = lzma_raw_encoder(&strm, filters); 376207753Smm break; 377207753Smm } 378207753Smm } else { 379207753Smm const uint32_t flags = LZMA_TELL_UNSUPPORTED_CHECK 380207753Smm | LZMA_CONCATENATED; 381207753Smm 382207753Smm // We abuse FORMAT_AUTO to indicate unknown file format, 383207753Smm // for which we may consider passthru mode. 384207753Smm enum format_type init_format = FORMAT_AUTO; 385207753Smm 386207753Smm switch (opt_format) { 387207753Smm case FORMAT_AUTO: 388207753Smm if (is_format_xz()) 389207753Smm init_format = FORMAT_XZ; 390207753Smm else if (is_format_lzma()) 391207753Smm init_format = FORMAT_LZMA; 392207753Smm break; 393207753Smm 394207753Smm case FORMAT_XZ: 395207753Smm if (is_format_xz()) 396207753Smm init_format = FORMAT_XZ; 397207753Smm break; 398207753Smm 399207753Smm case FORMAT_LZMA: 400207753Smm if (is_format_lzma()) 401207753Smm init_format = FORMAT_LZMA; 402207753Smm break; 403207753Smm 404207753Smm case FORMAT_RAW: 405207753Smm init_format = FORMAT_RAW; 406207753Smm break; 407207753Smm } 408207753Smm 409207753Smm switch (init_format) { 410207753Smm case FORMAT_AUTO: 411207753Smm // Uknown file format. If --decompress --stdout 412207753Smm // --force have been given, then we copy the input 413207753Smm // as is to stdout. Checking for MODE_DECOMPRESS 414207753Smm // is needed, because we don't want to do use 415207753Smm // passthru mode with --test. 416207753Smm if (opt_mode == MODE_DECOMPRESS 417207753Smm && opt_stdout && opt_force) 418207753Smm return CODER_INIT_PASSTHRU; 419207753Smm 420207753Smm ret = LZMA_FORMAT_ERROR; 421207753Smm break; 422207753Smm 423207753Smm case FORMAT_XZ: 424207753Smm ret = lzma_stream_decoder(&strm, 425213700Smm hardware_memlimit_get( 426213700Smm MODE_DECOMPRESS), flags); 427207753Smm break; 428207753Smm 429207753Smm case FORMAT_LZMA: 430207753Smm ret = lzma_alone_decoder(&strm, 431213700Smm hardware_memlimit_get( 432213700Smm MODE_DECOMPRESS)); 433207753Smm break; 434207753Smm 435207753Smm case FORMAT_RAW: 436207753Smm // Memory usage has already been checked in 437207753Smm // coder_set_compression_settings(). 438207753Smm ret = lzma_raw_decoder(&strm, filters); 439207753Smm break; 440207753Smm } 441207753Smm 442207753Smm // Try to decode the headers. This will catch too low 443207753Smm // memory usage limit in case it happens in the first 444207753Smm // Block of the first Stream, which is where it very 445207753Smm // probably will happen if it is going to happen. 446207753Smm if (ret == LZMA_OK && init_format != FORMAT_RAW) { 447207753Smm strm.next_out = NULL; 448207753Smm strm.avail_out = 0; 449207753Smm ret = lzma_code(&strm, LZMA_RUN); 450207753Smm } 451207753Smm } 452207753Smm 453207753Smm if (ret != LZMA_OK) { 454207753Smm message_error("%s: %s", pair->src_name, message_strm(ret)); 455207753Smm if (ret == LZMA_MEMLIMIT_ERROR) 456207753Smm message_mem_needed(V_ERROR, lzma_memusage(&strm)); 457207753Smm 458207753Smm return CODER_INIT_ERROR; 459207753Smm } 460207753Smm 461207753Smm return CODER_INIT_NORMAL; 462207753Smm} 463207753Smm 464207753Smm 465207753Smm/// Compress or decompress using liblzma. 466207753Smmstatic bool 467207753Smmcoder_normal(file_pair *pair) 468207753Smm{ 469207753Smm // Encoder needs to know when we have given all the input to it. 470207753Smm // The decoders need to know it too when we are using 471207753Smm // LZMA_CONCATENATED. We need to check for src_eof here, because 472207753Smm // the first input chunk has been already read, and that may 473207753Smm // have been the only chunk we will read. 474207753Smm lzma_action action = pair->src_eof ? LZMA_FINISH : LZMA_RUN; 475207753Smm 476207753Smm lzma_ret ret; 477207753Smm 478207753Smm // Assume that something goes wrong. 479207753Smm bool success = false; 480207753Smm 481207753Smm strm.next_out = out_buf.u8; 482207753Smm strm.avail_out = IO_BUFFER_SIZE; 483207753Smm 484207753Smm while (!user_abort) { 485207753Smm // Fill the input buffer if it is empty and we haven't reached 486207753Smm // end of file yet. 487207753Smm if (strm.avail_in == 0 && !pair->src_eof) { 488207753Smm strm.next_in = in_buf.u8; 489207753Smm strm.avail_in = io_read( 490207753Smm pair, &in_buf, IO_BUFFER_SIZE); 491207753Smm 492207753Smm if (strm.avail_in == SIZE_MAX) 493207753Smm break; 494207753Smm 495207753Smm if (pair->src_eof) 496207753Smm action = LZMA_FINISH; 497207753Smm } 498207753Smm 499207753Smm // Let liblzma do the actual work. 500207753Smm ret = lzma_code(&strm, action); 501207753Smm 502207753Smm // Write out if the output buffer became full. 503207753Smm if (strm.avail_out == 0) { 504207753Smm if (opt_mode != MODE_TEST && io_write(pair, &out_buf, 505207753Smm IO_BUFFER_SIZE - strm.avail_out)) 506207753Smm break; 507207753Smm 508207753Smm strm.next_out = out_buf.u8; 509207753Smm strm.avail_out = IO_BUFFER_SIZE; 510207753Smm } 511207753Smm 512207753Smm if (ret != LZMA_OK) { 513207753Smm // Determine if the return value indicates that we 514207753Smm // won't continue coding. 515207753Smm const bool stop = ret != LZMA_NO_CHECK 516207753Smm && ret != LZMA_UNSUPPORTED_CHECK; 517207753Smm 518207753Smm if (stop) { 519207753Smm // Write the remaining bytes even if something 520207753Smm // went wrong, because that way the user gets 521207753Smm // as much data as possible, which can be good 522207753Smm // when trying to get at least some useful 523207753Smm // data out of damaged files. 524207753Smm if (opt_mode != MODE_TEST && io_write(pair, 525207753Smm &out_buf, IO_BUFFER_SIZE 526207753Smm - strm.avail_out)) 527207753Smm break; 528207753Smm } 529207753Smm 530207753Smm if (ret == LZMA_STREAM_END) { 531207753Smm // Check that there is no trailing garbage. 532207753Smm // This is needed for LZMA_Alone and raw 533207753Smm // streams. 534207753Smm if (strm.avail_in == 0 && !pair->src_eof) { 535207753Smm // Try reading one more byte. 536207753Smm // Hopefully we don't get any more 537207753Smm // input, and thus pair->src_eof 538207753Smm // becomes true. 539207753Smm strm.avail_in = io_read( 540207753Smm pair, &in_buf, 1); 541207753Smm if (strm.avail_in == SIZE_MAX) 542207753Smm break; 543207753Smm 544207753Smm assert(strm.avail_in == 0 545207753Smm || strm.avail_in == 1); 546207753Smm } 547207753Smm 548207753Smm if (strm.avail_in == 0) { 549207753Smm assert(pair->src_eof); 550207753Smm success = true; 551207753Smm break; 552207753Smm } 553207753Smm 554207753Smm // We hadn't reached the end of the file. 555207753Smm ret = LZMA_DATA_ERROR; 556207753Smm assert(stop); 557207753Smm } 558207753Smm 559207753Smm // If we get here and stop is true, something went 560207753Smm // wrong and we print an error. Otherwise it's just 561207753Smm // a warning and coding can continue. 562207753Smm if (stop) { 563207753Smm message_error("%s: %s", pair->src_name, 564207753Smm message_strm(ret)); 565207753Smm } else { 566207753Smm message_warning("%s: %s", pair->src_name, 567207753Smm message_strm(ret)); 568207753Smm 569207753Smm // When compressing, all possible errors set 570207753Smm // stop to true. 571207753Smm assert(opt_mode != MODE_COMPRESS); 572207753Smm } 573207753Smm 574207753Smm if (ret == LZMA_MEMLIMIT_ERROR) { 575207753Smm // Display how much memory it would have 576207753Smm // actually needed. 577207753Smm message_mem_needed(V_ERROR, 578207753Smm lzma_memusage(&strm)); 579207753Smm } 580207753Smm 581207753Smm if (stop) 582207753Smm break; 583207753Smm } 584207753Smm 585207753Smm // Show progress information under certain conditions. 586207753Smm message_progress_update(); 587207753Smm } 588207753Smm 589207753Smm return success; 590207753Smm} 591207753Smm 592207753Smm 593207753Smm/// Copy from input file to output file without processing the data in any 594207753Smm/// way. This is used only when trying to decompress unrecognized files 595207753Smm/// with --decompress --stdout --force, so the output is always stdout. 596207753Smmstatic bool 597207753Smmcoder_passthru(file_pair *pair) 598207753Smm{ 599207753Smm while (strm.avail_in != 0) { 600207753Smm if (user_abort) 601207753Smm return false; 602207753Smm 603207753Smm if (io_write(pair, &in_buf, strm.avail_in)) 604207753Smm return false; 605207753Smm 606207753Smm strm.total_in += strm.avail_in; 607207753Smm strm.total_out = strm.total_in; 608207753Smm message_progress_update(); 609207753Smm 610207753Smm strm.avail_in = io_read(pair, &in_buf, IO_BUFFER_SIZE); 611207753Smm if (strm.avail_in == SIZE_MAX) 612207753Smm return false; 613207753Smm } 614207753Smm 615207753Smm return true; 616207753Smm} 617207753Smm 618207753Smm 619207753Smmextern void 620207753Smmcoder_run(const char *filename) 621207753Smm{ 622207753Smm // Set and possibly print the filename for the progress message. 623207753Smm message_filename(filename); 624207753Smm 625207753Smm // Try to open the input file. 626207753Smm file_pair *pair = io_open_src(filename); 627207753Smm if (pair == NULL) 628207753Smm return; 629207753Smm 630207753Smm // Assume that something goes wrong. 631207753Smm bool success = false; 632207753Smm 633207753Smm // Read the first chunk of input data. This is needed to detect 634207753Smm // the input file type (for now, only for decompression). 635207753Smm strm.next_in = in_buf.u8; 636207753Smm strm.avail_in = io_read(pair, &in_buf, IO_BUFFER_SIZE); 637207753Smm 638207753Smm if (strm.avail_in != SIZE_MAX) { 639207753Smm // Initialize the coder. This will detect the file format 640207753Smm // and, in decompression or testing mode, check the memory 641207753Smm // usage of the first Block too. This way we don't try to 642207753Smm // open the destination file if we see that coding wouldn't 643207753Smm // work at all anyway. This also avoids deleting the old 644207753Smm // "target" file if --force was used. 645207753Smm const enum coder_init_ret init_ret = coder_init(pair); 646207753Smm 647207753Smm if (init_ret != CODER_INIT_ERROR && !user_abort) { 648207753Smm // Don't open the destination file when --test 649207753Smm // is used. 650207753Smm if (opt_mode == MODE_TEST || !io_open_dest(pair)) { 651207753Smm // Initialize the progress indicator. 652207753Smm const uint64_t in_size 653207753Smm = pair->src_st.st_size <= 0 654207753Smm ? 0 : pair->src_st.st_size; 655207753Smm message_progress_start(&strm, in_size); 656207753Smm 657207753Smm // Do the actual coding or passthru. 658207753Smm if (init_ret == CODER_INIT_NORMAL) 659207753Smm success = coder_normal(pair); 660207753Smm else 661207753Smm success = coder_passthru(pair); 662207753Smm 663207753Smm message_progress_end(success); 664207753Smm } 665207753Smm } 666207753Smm } 667207753Smm 668207753Smm // Close the file pair. It needs to know if coding was successful to 669207753Smm // know if the source or target file should be unlinked. 670207753Smm io_close(pair, success); 671207753Smm 672207753Smm return; 673207753Smm} 674