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