• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1   // LzmaAlone.cpp
2 
3 #include "StdAfx.h"
4 
5 // #include <stdio.h>
6 
7 #if (defined(_WIN32) || defined(OS2) || defined(MSDOS)) && !defined(UNDER_CE)
8 #include <fcntl.h>
9 #include <io.h>
10 #define MY_SET_BINARY_MODE(file) _setmode(_fileno(file), O_BINARY)
11 #else
12 #define MY_SET_BINARY_MODE(file)
13 #endif
14 
15 #include "../../../../C/CpuArch.h"
16 #include "../../../../C/7zVersion.h"
17 #include "../../../../C/Alloc.h"
18 #include "../../../../C/Lzma86.h"
19 
20 #include "../../../Common/MyWindows.h"
21 #include "../../../Common/MyInitGuid.h"
22 
23 #include "../../../Windows/NtCheck.h"
24 
25 #ifndef Z7_ST
26 #include "../../../Windows/System.h"
27 #endif
28 
29 #include "../../../Common/IntToString.h"
30 #include "../../../Common/CommandLineParser.h"
31 #include "../../../Common/StringConvert.h"
32 #include "../../../Common/StringToInt.h"
33 
34 #include "../../Common/FileStreams.h"
35 #include "../../Common/StreamUtils.h"
36 
37 #include "../../Compress/LzmaDecoder.h"
38 #include "../../Compress/LzmaEncoder.h"
39 
40 #include "../../UI/Console/BenchCon.h"
41 #include "../../UI/Console/ConsoleClose.h"
42 
43 extern
44 bool g_LargePagesMode;
45 bool g_LargePagesMode = false;
46 
47 using namespace NCommandLineParser;
48 
49 static const unsigned kDictSizeLog = 24;
50 
51 #define kCopyrightString "\nLZMA " MY_VERSION_CPU " : " MY_COPYRIGHT_DATE "\n\n"
52 
53 static const char * const kHelpString =
54     "Usage:  lzma <command> [inputFile] [outputFile] [<switches>...]\n"
55     "\n"
56     "<command>\n"
57     "  e : Encode file\n"
58     "  d : Decode file\n"
59     "  b : Benchmark\n"
60     "<switches>\n"
61     "  -a{N}  : set compression mode : [0, 1] : default = 1 (max)\n"
62     "  -d{N}  : set dictionary size : [12, 30] : default = 24 (16 MiB)\n"
63     "  -fb{N} : set number of fast bytes : [5, 273] : default = 128\n"
64     "  -mc{N} : set number of cycles for match finder\n"
65     "  -lc{N} : set number of literal context bits : [0, 8] : default = 3\n"
66     "  -lp{N} : set number of literal pos bits : [0, 4] : default = 0\n"
67     "  -pb{N} : set number of pos bits : [0, 4] : default = 2\n"
68     "  -mf{M} : set match finder: [hc4, bt2, bt3, bt4] : default = bt4\n"
69     "  -mt{N} : set number of CPU threads\n"
70     "  -eos   : write end of stream marker\n"
71     "  -si    : read data from stdin\n"
72     "  -so    : write data to stdout\n";
73 
74 
75 static const char * const kCantAllocate = "Cannot allocate memory";
76 static const char * const kReadError = "Read error";
77 static const char * const kWriteError = "Write error";
78 
79 
80 namespace NKey {
81 enum Enum
82 {
83   kHelp1 = 0,
84   kHelp2,
85   kMethod,
86   kLevel,
87   kAlgo,
88   kDict,
89   kFb,
90   kMc,
91   kLc,
92   kLp,
93   kPb,
94   kMatchFinder,
95   kMultiThread,
96   kEOS,
97   kStdIn,
98   kStdOut,
99   kFilter86
100 };
101 }
102 
103 #define SWFRM_3(t, mu, mi) t, mu, mi, NULL
104 
105 #define SWFRM_1(t) SWFRM_3(t, false, 0)
106 #define SWFRM_SIMPLE SWFRM_1(NSwitchType::kSimple)
107 #define SWFRM_STRING SWFRM_1(NSwitchType::kString)
108 
109 #define SWFRM_STRING_SINGL(mi) SWFRM_3(NSwitchType::kString, false, mi)
110 
111 static const CSwitchForm kSwitchForms[] =
112 {
113   { "?",  SWFRM_SIMPLE },
114   { "H",  SWFRM_SIMPLE },
115   { "MM", SWFRM_STRING_SINGL(1) },
116   { "X", SWFRM_STRING_SINGL(1) },
117   { "A", SWFRM_STRING_SINGL(1) },
118   { "D", SWFRM_STRING_SINGL(1) },
119   { "FB", SWFRM_STRING_SINGL(1) },
120   { "MC", SWFRM_STRING_SINGL(1) },
121   { "LC", SWFRM_STRING_SINGL(1) },
122   { "LP", SWFRM_STRING_SINGL(1) },
123   { "PB", SWFRM_STRING_SINGL(1) },
124   { "MF", SWFRM_STRING_SINGL(1) },
125   { "MT", SWFRM_STRING },
126   { "EOS", SWFRM_SIMPLE },
127   { "SI",  SWFRM_SIMPLE },
128   { "SO",  SWFRM_SIMPLE },
129   { "F86",  NSwitchType::kChar, false, 0, "+" }
130 };
131 
132 
Convert_UString_to_AString(const UString & s,AString & temp)133 static void Convert_UString_to_AString(const UString &s, AString &temp)
134 {
135   int codePage = CP_OEMCP;
136   /*
137   int g_CodePage = -1;
138   int codePage = g_CodePage;
139   if (codePage == -1)
140     codePage = CP_OEMCP;
141   if (codePage == CP_UTF8)
142     ConvertUnicodeToUTF8(s, temp);
143   else
144   */
145     UnicodeStringToMultiByte2(temp, s, (UINT)codePage);
146 }
147 
PrintErr(const char * s)148 static void PrintErr(const char *s)
149 {
150   fputs(s, stderr);
151 }
152 
PrintErr_LF(const char * s)153 static void PrintErr_LF(const char *s)
154 {
155   PrintErr(s);
156   fputc('\n', stderr);
157 }
158 
159 
PrintError(const char * s)160 static void PrintError(const char *s)
161 {
162   PrintErr("\nERROR: ");
163   PrintErr_LF(s);
164 }
165 
PrintError2(const char * s1,const UString & s2)166 static void PrintError2(const char *s1, const UString &s2)
167 {
168   PrintError(s1);
169   AString a;
170   Convert_UString_to_AString(s2, a);
171   PrintErr_LF(a);
172 }
173 
PrintError_int(const char * s,int code)174 static void PrintError_int(const char *s, int code)
175 {
176   PrintError(s);
177   char temp[32];
178   ConvertInt64ToString(code, temp);
179   PrintErr("Error code = ");
180   PrintErr_LF(temp);
181 }
182 
183 
184 
Print(const char * s)185 static void Print(const char *s)
186 {
187   fputs(s, stdout);
188 }
189 
Print_UInt64(UInt64 v)190 static void Print_UInt64(UInt64 v)
191 {
192   char temp[32];
193   ConvertUInt64ToString(v, temp);
194   Print(temp);
195 }
196 
Print_MB(UInt64 v)197 static void Print_MB(UInt64 v)
198 {
199   Print_UInt64(v);
200   Print(" MiB");
201 }
202 
Print_Size(const char * s,UInt64 v)203 static void Print_Size(const char *s, UInt64 v)
204 {
205   Print(s);
206   Print_UInt64(v);
207   Print(" (");
208   Print_MB(v >> 20);
209   Print(")\n");
210 }
211 
PrintTitle()212 static void PrintTitle()
213 {
214   Print(kCopyrightString);
215 }
216 
PrintHelp()217 static void PrintHelp()
218 {
219   PrintTitle();
220   Print(kHelpString);
221 }
222 
223 
224 Z7_CLASS_IMP_COM_1(
225   CProgressPrint,
226   ICompressProgressInfo
227 )
228   UInt64 _size1;
229   UInt64 _size2;
230 public:
231   CProgressPrint(): _size1(0), _size2(0) {}
232 
233   void ClosePrint();
234 };
235 
236 #define BACK_STR \
237 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
238 static const char * const kBackSpaces =
239 BACK_STR
240 "                                                                "
241 BACK_STR;
242 
243 
244 void CProgressPrint::ClosePrint()
245 {
246   Print(kBackSpaces);
247 }
248 
249 Z7_COM7F_IMF(CProgressPrint::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize))
250 {
251   if (NConsoleClose::TestBreakSignal())
252     return E_ABORT;
253   if (inSize)
254   {
255     UInt64 v1 = *inSize >> 20;
256     UInt64 v2 = _size2;
257     if (outSize)
258       v2 = *outSize >> 20;
259     if (v1 != _size1 || v2 != _size2)
260     {
261       _size1 = v1;
262       _size2 = v2;
263       ClosePrint();
264       Print_MB(_size1);
265       Print(" -> ");
266       Print_MB(_size2);
267     }
268   }
269   return S_OK;
270 }
271 
272 
273 Z7_ATTR_NORETURN
274 static void IncorrectCommand()
275 {
276   throw "Incorrect command";
277 }
278 
279 static UInt32 GetNumber(const wchar_t *s)
280 {
281   const wchar_t *end;
282   UInt32 v = ConvertStringToUInt32(s, &end);
283   if (*end != 0)
284     IncorrectCommand();
285   return v;
286 }
287 
288 static void ParseUInt32(const CParser &parser, unsigned index, UInt32 &res)
289 {
290   if (parser[index].ThereIs)
291     res = GetNumber(parser[index].PostStrings[0]);
292 }
293 
294 
295 static int Error_HRESULT(const char *s, HRESULT res)
296 {
297   if (res == E_ABORT)
298   {
299     Print("\n\nBreak signaled\n");
300     return 255;
301   }
302 
303   PrintError(s);
304 
305   if (res == E_OUTOFMEMORY)
306   {
307     PrintErr_LF(kCantAllocate);
308     return 8;
309   }
310   if (res == E_INVALIDARG)
311   {
312     PrintErr_LF("Ununsupported parameter");
313   }
314   else
315   {
316     char temp[32];
317     ConvertUInt32ToHex((UInt32)res, temp);
318     PrintErr("Error code = 0x");
319     PrintErr_LF(temp);
320   }
321   return 1;
322 }
323 
324 #if defined(_UNICODE) && !defined(_WIN64) && !defined(UNDER_CE)
325 #define NT_CHECK_FAIL_ACTION PrintError("Unsupported Windows version"); return 1;
326 #endif
327 
328 static void AddProp(CObjectVector<CProperty> &props2, const char *name, const wchar_t *val)
329 {
330   CProperty &prop = props2.AddNew();
331   prop.Name = name;
332   prop.Value = val;
333 }
334 
335 static int main2(int numArgs, const char *args[])
336 {
337   NT_CHECK
338 
339   if (numArgs == 1)
340   {
341     PrintHelp();
342     return 0;
343   }
344 
345   /*
346   bool unsupportedTypes = (sizeof(Byte) != 1 || sizeof(UInt32) < 4 || sizeof(UInt64) < 8);
347   if (unsupportedTypes)
348     throw "Unsupported base types. Edit Common/Types.h and recompile";
349   */
350 
351   UStringVector commandStrings;
352   for (int i = 1; i < numArgs; i++)
353     commandStrings.Add(MultiByteToUnicodeString(args[i]));
354 
355   CParser parser;
356   try
357   {
358     if (!parser.ParseStrings(kSwitchForms, Z7_ARRAY_SIZE(kSwitchForms), commandStrings))
359     {
360       PrintError2(parser.ErrorMessage, parser.ErrorLine);
361       return 1;
362     }
363   }
364   catch(...)
365   {
366     IncorrectCommand();
367   }
368 
369   if (parser[NKey::kHelp1].ThereIs || parser[NKey::kHelp2].ThereIs)
370   {
371     PrintHelp();
372     return 0;
373   }
374 
375   bool stdInMode = parser[NKey::kStdIn].ThereIs;
376   bool stdOutMode = parser[NKey::kStdOut].ThereIs;
377 
378   if (!stdOutMode)
379     PrintTitle();
380 
381   const UStringVector &params = parser.NonSwitchStrings;
382 
383   unsigned paramIndex = 0;
384   if (paramIndex >= params.Size())
385     IncorrectCommand();
386   const UString &command = params[paramIndex++];
387 
388   CObjectVector<CProperty> props2;
389   bool dictDefined = false;
390   UInt32 dict = (UInt32)(Int32)-1;
391 
392   if (parser[NKey::kDict].ThereIs)
393   {
394     UInt32 dictLog;
395     const UString &s = parser[NKey::kDict].PostStrings[0];
396     dictLog = GetNumber(s);
397     dict = 1 << dictLog;
398     dictDefined = true;
399     AddProp(props2, "d", s);
400   }
401 
402   if (parser[NKey::kLevel].ThereIs)
403   {
404     const UString &s = parser[NKey::kLevel].PostStrings[0];
405     /* UInt32 level = */ GetNumber(s);
406     AddProp(props2, "x", s);
407   }
408 
409   UString mf ("BT4");
410   if (parser[NKey::kMatchFinder].ThereIs)
411     mf = parser[NKey::kMatchFinder].PostStrings[0];
412 
413   UInt32 numThreads = (UInt32)(Int32)-1;
414 
415   #ifndef Z7_ST
416 
417   if (parser[NKey::kMultiThread].ThereIs)
418   {
419     const UString &s = parser[NKey::kMultiThread].PostStrings[0];
420     if (s.IsEmpty())
421       numThreads = NWindows::NSystem::GetNumberOfProcessors();
422     else
423       numThreads = GetNumber(s);
424     AddProp(props2, "mt", s);
425   }
426 
427   #endif
428 
429 
430   if (parser[NKey::kMethod].ThereIs)
431   {
432     const UString &s = parser[NKey::kMethod].PostStrings[0];
433     if (s.IsEmpty() || s[0] != '=')
434       IncorrectCommand();
435     AddProp(props2, "m", s.Ptr(1));
436   }
437 
438   if (StringsAreEqualNoCase_Ascii(command, "b"))
439   {
440     UInt32 numIterations = 1;
441     if (paramIndex < params.Size())
442       numIterations = GetNumber(params[paramIndex++]);
443     if (params.Size() != paramIndex)
444       IncorrectCommand();
445 
446     HRESULT res = BenchCon(props2, numIterations, stdout);
447 
448     if (res == S_OK)
449       return 0;
450     return Error_HRESULT("Benchmark error", res);
451   }
452 
453   {
454     UInt32 needParams = 3;
455     if (stdInMode) needParams--;
456     if (stdOutMode) needParams--;
457     if (needParams != params.Size())
458       IncorrectCommand();
459   }
460 
461   if (numThreads == (UInt32)(Int32)-1)
462     numThreads = 1;
463 
464   bool encodeMode = false;
465 
466   if (StringsAreEqualNoCase_Ascii(command, "e"))
467     encodeMode = true;
468   else if (!StringsAreEqualNoCase_Ascii(command, "d"))
469     IncorrectCommand();
470 
471   CMyComPtr<ISequentialInStream> inStream;
472   CInFileStream *inStreamSpec = NULL;
473 
474   if (stdInMode)
475   {
476     inStream = new CStdInFileStream;
477     MY_SET_BINARY_MODE(stdin);
478   }
479   else
480   {
481     const UString &inputName = params[paramIndex++];
482     inStreamSpec = new CInFileStream;
483     inStream = inStreamSpec;
484     if (!inStreamSpec->Open(us2fs(inputName)))
485     {
486       PrintError2("Cannot open input file", inputName);
487       return 1;
488     }
489   }
490 
491   CMyComPtr<ISequentialOutStream> outStream;
492   COutFileStream *outStreamSpec = NULL;
493 
494   if (stdOutMode)
495   {
496     outStream = new CStdOutFileStream;
497     MY_SET_BINARY_MODE(stdout);
498   }
499   else
500   {
501     const UString &outputName = params[paramIndex++];
502     outStreamSpec = new COutFileStream;
503     outStream = outStreamSpec;
504     if (!outStreamSpec->Create(us2fs(outputName), true))
505     {
506       PrintError2("Cannot open output file", outputName);
507       return 1;
508     }
509   }
510 
511   bool fileSizeDefined = false;
512   UInt64 fileSize = 0;
513 
514   if (inStreamSpec)
515   {
516     if (!inStreamSpec->GetLength(fileSize))
517       throw "Cannot get file length";
518     fileSizeDefined = true;
519     if (!stdOutMode)
520       Print_Size("Input size:  ", fileSize);
521   }
522 
523   if (encodeMode && !dictDefined)
524   {
525     dict = 1 << kDictSizeLog;
526     if (fileSizeDefined)
527     {
528       unsigned i;
529       for (i = 16; i < kDictSizeLog; i++)
530         if ((UInt32)((UInt32)1 << i) >= fileSize)
531           break;
532       dict = (UInt32)1 << i;
533     }
534   }
535 
536   if (parser[NKey::kFilter86].ThereIs)
537   {
538     /* -f86 switch is for x86 filtered mode: BCJ + LZMA.
539        It uses modified header format.
540        It's not recommended to use -f86 mode now.
541        You can use xz format instead, if you want to use filters */
542 
543     if (parser[NKey::kEOS].ThereIs || stdInMode)
544       throw "Cannot use stdin in this mode";
545 
546     size_t inSize = (size_t)fileSize;
547 
548     if (inSize != fileSize)
549       throw "File is too big";
550 
551     Byte *inBuffer = NULL;
552 
553     if (inSize != 0)
554     {
555       inBuffer = (Byte *)MyAlloc((size_t)inSize);
556       if (!inBuffer)
557         throw kCantAllocate;
558     }
559 
560     if (ReadStream_FAIL(inStream, inBuffer, inSize) != S_OK)
561       throw "Cannot read";
562 
563     Byte *outBuffer = NULL;
564     size_t outSize;
565 
566     if (encodeMode)
567     {
568       // we allocate 105% of original size for output buffer
569       UInt64 outSize64 = fileSize / 20 * 21 + (1 << 16);
570 
571       outSize = (size_t)outSize64;
572 
573       if (outSize != outSize64)
574         throw "File is too big";
575 
576       if (outSize != 0)
577       {
578         outBuffer = (Byte *)MyAlloc((size_t)outSize);
579         if (!outBuffer)
580           throw kCantAllocate;
581       }
582 
583       int res = Lzma86_Encode(outBuffer, &outSize, inBuffer, inSize,
584           5, dict, parser[NKey::kFilter86].PostCharIndex == 0 ? SZ_FILTER_YES : SZ_FILTER_AUTO);
585 
586       if (res != 0)
587       {
588         PrintError_int("Encode error", (int)res);
589         return 1;
590       }
591     }
592     else
593     {
594       UInt64 outSize64;
595 
596       if (Lzma86_GetUnpackSize(inBuffer, inSize, &outSize64) != 0)
597         throw "data error";
598 
599       outSize = (size_t)outSize64;
600       if (outSize != outSize64)
601         throw "Unpack size is too big";
602       if (outSize != 0)
603       {
604         outBuffer = (Byte *)MyAlloc(outSize);
605         if (!outBuffer)
606           throw kCantAllocate;
607       }
608 
609       int res = Lzma86_Decode(outBuffer, &outSize, inBuffer, &inSize);
610 
611       if (inSize != (size_t)fileSize)
612         throw "incorrect processed size";
613       if (res != 0)
614       {
615         PrintError_int("Decode error", (int)res);
616         return 1;
617       }
618     }
619 
620     if (WriteStream(outStream, outBuffer, outSize) != S_OK)
621       throw kWriteError;
622 
623     MyFree(outBuffer);
624     MyFree(inBuffer);
625   }
626   else
627   {
628 
629   CProgressPrint *progressSpec = NULL;
630   CMyComPtr<ICompressProgressInfo> progress;
631 
632   if (!stdOutMode)
633   {
634     progressSpec = new CProgressPrint;
635     progress = progressSpec;
636   }
637 
638   if (encodeMode)
639   {
640     NCompress::NLzma::CEncoder *encoderSpec = new NCompress::NLzma::CEncoder;
641     CMyComPtr<ICompressCoder> encoder = encoderSpec;
642 
643     UInt32 pb = 2;
644     UInt32 lc = 3; // = 0; for 32-bit data
645     UInt32 lp = 0; // = 2; for 32-bit data
646     UInt32 algo = 1;
647     UInt32 fb = 128;
648     UInt32 mc = 16 + fb / 2;
649     bool mcDefined = false;
650 
651     bool eos = parser[NKey::kEOS].ThereIs || stdInMode;
652 
653     ParseUInt32(parser, NKey::kAlgo, algo);
654     ParseUInt32(parser, NKey::kFb, fb);
655     ParseUInt32(parser, NKey::kLc, lc);
656     ParseUInt32(parser, NKey::kLp, lp);
657     ParseUInt32(parser, NKey::kPb, pb);
658 
659     mcDefined = parser[NKey::kMc].ThereIs;
660     if (mcDefined)
661       mc = GetNumber(parser[NKey::kMc].PostStrings[0]);
662 
663     const PROPID propIDs[] =
664     {
665       NCoderPropID::kDictionarySize,
666       NCoderPropID::kPosStateBits,
667       NCoderPropID::kLitContextBits,
668       NCoderPropID::kLitPosBits,
669       NCoderPropID::kAlgorithm,
670       NCoderPropID::kNumFastBytes,
671       NCoderPropID::kMatchFinder,
672       NCoderPropID::kEndMarker,
673       NCoderPropID::kNumThreads,
674       NCoderPropID::kMatchFinderCycles,
675     };
676 
677     const unsigned kNumPropsMax = Z7_ARRAY_SIZE(propIDs);
678 
679     PROPVARIANT props[kNumPropsMax];
680     for (int p = 0; p < 6; p++)
681       props[p].vt = VT_UI4;
682 
683     props[0].ulVal = (UInt32)dict;
684     props[1].ulVal = (UInt32)pb;
685     props[2].ulVal = (UInt32)lc;
686     props[3].ulVal = (UInt32)lp;
687     props[4].ulVal = (UInt32)algo;
688     props[5].ulVal = (UInt32)fb;
689 
690     props[6].vt = VT_BSTR;
691     props[6].bstrVal = const_cast<BSTR>((const wchar_t *)mf);
692 
693     props[7].vt = VT_BOOL;
694     props[7].boolVal = eos ? VARIANT_TRUE : VARIANT_FALSE;
695 
696     props[8].vt = VT_UI4;
697     props[8].ulVal = (UInt32)numThreads;
698 
699     // it must be last in property list
700     props[9].vt = VT_UI4;
701     props[9].ulVal = (UInt32)mc;
702 
703     unsigned numProps = kNumPropsMax;
704     if (!mcDefined)
705       numProps--;
706 
707     HRESULT res = encoderSpec->SetCoderProperties(propIDs, props, numProps);
708     if (res != S_OK)
709       return Error_HRESULT("incorrect encoder properties", res);
710 
711     if (encoderSpec->WriteCoderProperties(outStream) != S_OK)
712       throw kWriteError;
713 
714     bool fileSizeWasUsed = true;
715     if (eos || stdInMode)
716     {
717       fileSize = (UInt64)(Int64)-1;
718       fileSizeWasUsed = false;
719     }
720 
721     {
722       Byte temp[8];
723       for (int i = 0; i < 8; i++)
724         temp[i]= (Byte)(fileSize >> (8 * i));
725       if (WriteStream(outStream, temp, 8) != S_OK)
726         throw kWriteError;
727     }
728 
729     res = encoder->Code(inStream, outStream, NULL, NULL, progress);
730     if (progressSpec)
731       progressSpec->ClosePrint();
732 
733     if (res != S_OK)
734       return Error_HRESULT("Encoding error", res);
735 
736     UInt64 processedSize = encoderSpec->GetInputProcessedSize();
737 
738     if (fileSizeWasUsed && processedSize != fileSize)
739       throw "Incorrect size of processed data";
740   }
741   else
742   {
743     NCompress::NLzma::CDecoder *decoderSpec = new NCompress::NLzma::CDecoder;
744     CMyComPtr<ICompressCoder> decoder = decoderSpec;
745 
746     decoderSpec->FinishStream = true;
747 
748     const unsigned kPropertiesSize = 5;
749     Byte header[kPropertiesSize + 8];
750 
751     if (ReadStream_FALSE(inStream, header, kPropertiesSize + 8) != S_OK)
752       throw kReadError;
753 
754     if (decoderSpec->SetDecoderProperties2(header, kPropertiesSize) != S_OK)
755       throw "SetDecoderProperties error";
756 
757     UInt64 unpackSize = 0;
758     for (unsigned i = 0; i < 8; i++)
759       unpackSize |= ((UInt64)header[kPropertiesSize + i]) << (8 * i);
760 
761     bool unpackSizeDefined = (unpackSize != (UInt64)(Int64)-1);
762 
763     HRESULT res = decoder->Code(inStream, outStream, NULL, unpackSizeDefined ? &unpackSize : NULL, progress);
764     if (progressSpec)
765       progressSpec->ClosePrint();
766 
767     if (res != S_OK)
768     {
769       if (res == S_FALSE)
770       {
771         PrintError("Decoding error");
772         return 1;
773       }
774       return Error_HRESULT("Decoding error", res);
775     }
776 
777     if (unpackSizeDefined && unpackSize != decoderSpec->GetOutputProcessedSize())
778       throw "incorrect uncompressed size in header";
779   }
780   }
781 
782   if (outStreamSpec)
783   {
784     if (!stdOutMode)
785       Print_Size("Output size: ", outStreamSpec->ProcessedSize);
786     if (outStreamSpec->Close() != S_OK)
787       throw "File closing error";
788   }
789 
790   return 0;
791 }
792 
793 int Z7_CDECL main(int numArgs, const char *args[])
794 {
795   NConsoleClose::CCtrlHandlerSetter ctrlHandlerSetter;
796 
797   try { return main2(numArgs, args); }
798   catch (const char *s)
799   {
800     PrintError(s);
801     return 1;
802   }
803   catch(...)
804   {
805     PrintError("Unknown Error");
806     return 1;
807   }
808 }
809