1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// LzmaAlone.cpp
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h"
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <stdio.h>
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#if (defined(_WIN32) || defined(OS2) || defined(MSDOS)) && !defined(UNDER_CE)
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <fcntl.h>
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <io.h>
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define MY_SET_BINARY_MODE(file) _setmode(_fileno(file), O_BINARY)
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#else
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define MY_SET_BINARY_MODE(file)
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// #include "../../../Common/MyWindows.h"
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../Common/MyInitGuid.h"
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../../C/7zVersion.h"
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../../C/Alloc.h"
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../../C/Lzma86.h"
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../Windows/NtCheck.h"
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _7ZIP_ST
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../Windows/System.h"
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../Common/CommandLineParser.h"
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../Common/StringConvert.h"
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../Common/StringToInt.h"
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../Common/FileStreams.h"
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../Common/StreamUtils.h"
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../Compress/LzmaDecoder.h"
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../Compress/LzmaEncoder.h"
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../UI/Console/BenchCon.h"
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
41baa3858d3f5d128a5c8466b700098109edcad5f2repo syncusing namespace NCommandLineParser;
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
43baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const char *kCantAllocate = "Can not allocate memory";
44baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const char *kReadError = "Read error";
45baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const char *kWriteError = "Write error";
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
47baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NKey {
48baa3858d3f5d128a5c8466b700098109edcad5f2repo syncenum Enum
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kHelp1 = 0,
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kHelp2,
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kAlgo,
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kDict,
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kFb,
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kMc,
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kLc,
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kLp,
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kPb,
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kMatchFinder,
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kMultiThread,
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kEOS,
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kStdIn,
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kStdOut,
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  kFilter86
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
68baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const CSwitchForm kSwitchForms[] =
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"?",  NSwitchType::kSimple, false },
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"H",  NSwitchType::kSimple, false },
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"A", NSwitchType::kUnLimitedPostString, false, 1 },
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"D", NSwitchType::kUnLimitedPostString, false, 1 },
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"FB", NSwitchType::kUnLimitedPostString, false, 1 },
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"MC", NSwitchType::kUnLimitedPostString, false, 1 },
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"LC", NSwitchType::kUnLimitedPostString, false, 1 },
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"LP", NSwitchType::kUnLimitedPostString, false, 1 },
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"PB", NSwitchType::kUnLimitedPostString, false, 1 },
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"MF", NSwitchType::kUnLimitedPostString, false, 1 },
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"MT", NSwitchType::kUnLimitedPostString, false, 0 },
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"EOS", NSwitchType::kSimple, false },
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"SI",  NSwitchType::kSimple, false },
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"SO",  NSwitchType::kSimple, false },
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { L"F86",  NSwitchType::kPostChar, false, 0, 0, L"+" }
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
87baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const int kNumSwitches = sizeof(kSwitchForms) / sizeof(kSwitchForms[0]);
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
89baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void PrintMessage(const char *s)
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fputs(s, stderr);
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
94baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void PrintHelp()
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintMessage("\nUsage:  LZMA <e|d> inputFile outputFile [<switches>...]\n"
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync             "  e: encode file\n"
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync             "  d: decode file\n"
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync             "  b: Benchmark\n"
100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "<Switches>\n"
101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -a{N}:  set compression mode - [0, 1], default: 1 (max)\n"
102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -d{N}:  set dictionary size - [12, 30], default: 23 (8MB)\n"
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -fb{N}: set number of fast bytes - [5, 273], default: 128\n"
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -mc{N}: set number of cycles for match finder\n"
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -lc{N}: set number of literal context bits - [0, 8], default: 3\n"
106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -lp{N}: set number of literal pos bits - [0, 4], default: 0\n"
107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -pb{N}: set number of pos bits - [0, 4], default: 2\n"
108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -mf{MF_ID}: set Match Finder: [bt2, bt3, bt4, hc4], default: bt4\n"
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -mt{N}: set number of CPU threads\n"
110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -eos:   write End Of Stream marker\n"
111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -si:    read data from stdin\n"
112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    "  -so:    write data to stdout\n"
113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    );
114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
116baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void PrintHelpAndExit(const char *s)
117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(stderr, "\nError: %s\n\n", s);
119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintHelp();
120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  throw -1;
121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
123baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void IncorrectCommand()
124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintHelpAndExit("Incorrect command");
126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
128baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void WriteArgumentsToStringList(int numArgs, const char *args[], UStringVector &strings)
129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (int i = 1; i < numArgs; i++)
131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    strings.Add(MultiByteToUnicodeString(args[i]));
132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
134baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic bool GetNumber(const wchar_t *s, UInt32 &value)
135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  value = 0;
137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (MyStringLen(s) == 0)
138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const wchar_t *end;
140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 res = ConvertStringToUInt64(s, &end);
141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (*end != L'\0')
142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res > 0xFFFFFFFF)
144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  value = UInt32(res);
146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return true;
147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
149baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void ParseUInt32(const CParser &parser, int index, UInt32 &res)
150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (parser[index].ThereIs)
152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!GetNumber(parser[index].PostStrings[0], res))
153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      IncorrectCommand();
154baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define NT_CHECK_FAIL_ACTION PrintMessage("Unsupported Windows version"); return 1;
157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
158baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint main2(int numArgs, const char *args[])
159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  NT_CHECK
161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintMessage("\nLZMA " MY_VERSION_COPYRIGHT_DATE "\n");
163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numArgs == 1)
165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
166baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintHelp();
167baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0;
168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool unsupportedTypes = (sizeof(Byte) != 1 || sizeof(UInt32) < 4 || sizeof(UInt64) < 4);
171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (unsupportedTypes)
172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
173baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintMessage("Unsupported base types. Edit Common/Types.h and recompile");
174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
175baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UStringVector commandStrings;
178baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  WriteArgumentsToStringList(numArgs, args, commandStrings);
179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CParser parser(kNumSwitches);
180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  try
181baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    parser.ParseStrings(kSwitchForms, commandStrings);
183baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
184baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  catch(...)
185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    IncorrectCommand();
187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
188baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
189baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if(parser[NKey::kHelp1].ThereIs || parser[NKey::kHelp2].ThereIs)
190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
191baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintHelp();
192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0;
193baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
194baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const UStringVector &nonSwitchStrings = parser.NonSwitchStrings;
195baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
196baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int paramIndex = 0;
197baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (paramIndex >= nonSwitchStrings.Size())
198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    IncorrectCommand();
199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const UString &command = nonSwitchStrings[paramIndex++];
200baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
201baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool dictDefined = false;
202baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 dict = (UInt32)-1;
203baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if(parser[NKey::kDict].ThereIs)
204baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
205baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 dicLog;
206baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!GetNumber(parser[NKey::kDict].PostStrings[0], dicLog))
207baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      IncorrectCommand();
208baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    dict = 1 << dicLog;
209baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    dictDefined = true;
210baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
211baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UString mf = L"BT4";
212baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (parser[NKey::kMatchFinder].ThereIs)
213baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    mf = parser[NKey::kMatchFinder].PostStrings[0];
214baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
215baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 numThreads = (UInt32)-1;
216baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
217baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
218baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (parser[NKey::kMultiThread].ThereIs)
219baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
220baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 numCPUs = NWindows::NSystem::GetNumberOfProcessors();
221baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const UString &s = parser[NKey::kMultiThread].PostStrings[0];
222baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (s.IsEmpty())
223baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      numThreads = numCPUs;
224baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
225baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (!GetNumber(s, numThreads))
226baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        IncorrectCommand();
227baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
228baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
229baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
230baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (command.CompareNoCase(L"b") == 0)
231baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
232baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const UInt32 kNumDefaultItereations = 1;
233baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 numIterations = kNumDefaultItereations;
234baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
235baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (paramIndex < nonSwitchStrings.Size())
236baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (!GetNumber(nonSwitchStrings[paramIndex++], numIterations))
237baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          numIterations = kNumDefaultItereations;
238baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
239baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return LzmaBenchCon(stderr, numIterations, numThreads, dict);
240baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
241baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
242baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numThreads == (UInt32)-1)
243baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numThreads = 1;
244baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
245baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool encodeMode = false;
246baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (command.CompareNoCase(L"e") == 0)
247baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    encodeMode = true;
248baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else if (command.CompareNoCase(L"d") == 0)
249baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    encodeMode = false;
250baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
251baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    IncorrectCommand();
252baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
253baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool stdInMode = parser[NKey::kStdIn].ThereIs;
254baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  bool stdOutMode = parser[NKey::kStdOut].ThereIs;
255baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
256baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CMyComPtr<ISequentialInStream> inStream;
257baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CInFileStream *inStreamSpec = 0;
258baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (stdInMode)
259baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
260baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    inStream = new CStdInFileStream;
261baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MY_SET_BINARY_MODE(stdin);
262baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
263baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
264baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
265baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (paramIndex >= nonSwitchStrings.Size())
266baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      IncorrectCommand();
267baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const UString &inputName = nonSwitchStrings[paramIndex++];
268baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    inStreamSpec = new CInFileStream;
269baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    inStream = inStreamSpec;
270baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!inStreamSpec->Open(GetSystemString(inputName)))
271baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
272baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fprintf(stderr, "\nError: can not open input file %s\n",
273baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          (const char *)GetOemString(inputName));
274baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
275baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
276baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
277baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
278baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CMyComPtr<ISequentialOutStream> outStream;
279baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  COutFileStream *outStreamSpec = NULL;
280baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (stdOutMode)
281baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
282baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    outStream = new CStdOutFileStream;
283baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MY_SET_BINARY_MODE(stdout);
284baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
285baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
286baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
287baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (paramIndex >= nonSwitchStrings.Size())
288baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      IncorrectCommand();
289baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const UString &outputName = nonSwitchStrings[paramIndex++];
290baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    outStreamSpec = new COutFileStream;
291baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    outStream = outStreamSpec;
292baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!outStreamSpec->Create(GetSystemString(outputName), true))
293baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
294baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fprintf(stderr, "\nError: can not open output file %s\n",
295baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        (const char *)GetOemString(outputName));
296baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
297baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
298baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
299baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
300baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (parser[NKey::kFilter86].ThereIs)
301baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
302baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    // -f86 switch is for x86 filtered mode: BCJ + LZMA.
303baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (parser[NKey::kEOS].ThereIs || stdInMode)
304baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      throw "Can not use stdin in this mode";
305baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt64 fileSize;
306baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    inStreamSpec->File.GetLength(fileSize);
307baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (fileSize > 0xF0000000)
308baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      throw "File is too big";
309baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    size_t inSize = (size_t)fileSize;
310baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Byte *inBuffer = 0;
311baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (inSize != 0)
312baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
313baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      inBuffer = (Byte *)MyAlloc((size_t)inSize);
314baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (inBuffer == 0)
315baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        throw kCantAllocate;
316baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
317baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
318baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (ReadStream_FAIL(inStream, inBuffer, inSize) != S_OK)
319baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      throw "Can not read";
320baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
321baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Byte *outBuffer = 0;
322baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    size_t outSize;
323baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (encodeMode)
324baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
325baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      // we allocate 105% of original size for output buffer
326baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      outSize = (size_t)fileSize / 20 * 21 + (1 << 16);
327baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (outSize != 0)
328baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
329baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        outBuffer = (Byte *)MyAlloc((size_t)outSize);
330baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (outBuffer == 0)
331baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          throw kCantAllocate;
332baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
333baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (!dictDefined)
334baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        dict = 1 << 23;
335baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      int res = Lzma86_Encode(outBuffer, &outSize, inBuffer, inSize,
336baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          5, dict, parser[NKey::kFilter86].PostCharIndex == 0 ? SZ_FILTER_YES : SZ_FILTER_AUTO);
337baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (res != 0)
338baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
339baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        fprintf(stderr, "\nEncoder error = %d\n", (int)res);
340baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        return 1;
341baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
342baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
343baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
344baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
345baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt64 outSize64;
346baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (Lzma86_GetUnpackSize(inBuffer, inSize, &outSize64) != 0)
347baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        throw "data error";
348baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      outSize = (size_t)outSize64;
349baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (outSize != outSize64)
350baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        throw "too big";
351baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (outSize != 0)
352baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
353baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        outBuffer = (Byte *)MyAlloc(outSize);
354baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (outBuffer == 0)
355baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          throw kCantAllocate;
356baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
357baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      int res = Lzma86_Decode(outBuffer, &outSize, inBuffer, &inSize);
358baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (inSize != (size_t)fileSize)
359baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        throw "incorrect processed size";
360baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (res != 0)
361baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        throw "LzmaDecoder error";
362baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
363baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (WriteStream(outStream, outBuffer, outSize) != S_OK)
364baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      throw kWriteError;
365baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MyFree(outBuffer);
366baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MyFree(inBuffer);
367baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0;
368baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
369baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
370baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
371baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 fileSize;
372baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (encodeMode)
373baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
374baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    NCompress::NLzma::CEncoder *encoderSpec = new NCompress::NLzma::CEncoder;
375baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    CMyComPtr<ICompressCoder> encoder = encoderSpec;
376baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
377baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!dictDefined)
378baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      dict = 1 << 23;
379baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
380baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 pb = 2;
381baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 lc = 3; // = 0; for 32-bit data
382baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 lp = 0; // = 2; for 32-bit data
383baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 algo = 1;
384baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 fb = 128;
385baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 mc = 16 + fb / 2;
386baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    bool mcDefined = false;
387baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
388baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    bool eos = parser[NKey::kEOS].ThereIs || stdInMode;
389baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
390baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ParseUInt32(parser, NKey::kAlgo, algo);
391baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ParseUInt32(parser, NKey::kFb, fb);
392baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ParseUInt32(parser, NKey::kLc, lc);
393baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ParseUInt32(parser, NKey::kLp, lp);
394baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ParseUInt32(parser, NKey::kPb, pb);
395baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
396baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    mcDefined = parser[NKey::kMc].ThereIs;
397baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (mcDefined)
398baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (!GetNumber(parser[NKey::kMc].PostStrings[0], mc))
399baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        IncorrectCommand();
400baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
401baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PROPID propIDs[] =
402baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
403baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kDictionarySize,
404baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kPosStateBits,
405baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kLitContextBits,
406baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kLitPosBits,
407baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kAlgorithm,
408baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kNumFastBytes,
409baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kMatchFinder,
410baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kEndMarker,
411baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kNumThreads,
412baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      NCoderPropID::kMatchFinderCycles,
413baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    };
414baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const int kNumPropsMax = sizeof(propIDs) / sizeof(propIDs[0]);
415baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
416baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PROPVARIANT props[kNumPropsMax];
417baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (int p = 0; p < 6; p++)
418baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      props[p].vt = VT_UI4;
419baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
420baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[0].ulVal = (UInt32)dict;
421baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[1].ulVal = (UInt32)pb;
422baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[2].ulVal = (UInt32)lc;
423baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[3].ulVal = (UInt32)lp;
424baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[4].ulVal = (UInt32)algo;
425baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[5].ulVal = (UInt32)fb;
426baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
427baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[6].vt = VT_BSTR;
428baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[6].bstrVal = const_cast<BSTR>((const wchar_t *)mf);
429baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
430baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[7].vt = VT_BOOL;
431baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[7].boolVal = eos ? VARIANT_TRUE : VARIANT_FALSE;
432baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
433baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[8].vt = VT_UI4;
434baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[8].ulVal = (UInt32)numThreads;
435baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
436baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    // it must be last in property list
437baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[9].vt = VT_UI4;
438baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    props[9].ulVal = (UInt32)mc;
439baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
440baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    int numProps = kNumPropsMax;
441baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!mcDefined)
442baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      numProps--;
443baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
444baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (encoderSpec->SetCoderProperties(propIDs, props, numProps) != S_OK)
445baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      IncorrectCommand();
446baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    encoderSpec->WriteCoderProperties(outStream);
447baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
448baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (eos || stdInMode)
449baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fileSize = (UInt64)(Int64)-1;
450baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
451baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      inStreamSpec->File.GetLength(fileSize);
452baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
453baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (int i = 0; i < 8; i++)
454baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
455baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      Byte b = Byte(fileSize >> (8 * i));
456baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (outStream->Write(&b, 1, 0) != S_OK)
457baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
458baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        PrintMessage(kWriteError);
459baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        return 1;
460baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
461baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
462baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    HRESULT result = encoder->Code(inStream, outStream, 0, 0, 0);
463baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (result == E_OUTOFMEMORY)
464baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
465baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      PrintMessage("\nError: Can not allocate memory\n");
466baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
467baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
468baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else if (result != S_OK)
469baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
470baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fprintf(stderr, "\nEncoder error = %X\n", (unsigned int)result);
471baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
472baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
473baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
474baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
475baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
476baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    NCompress::NLzma::CDecoder *decoderSpec = new NCompress::NLzma::CDecoder;
477baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    CMyComPtr<ICompressCoder> decoder = decoderSpec;
478baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    decoderSpec->FinishStream = true;
479baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const UInt32 kPropertiesSize = 5;
480baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Byte header[kPropertiesSize + 8];
481baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (ReadStream_FALSE(inStream, header, kPropertiesSize + 8) != S_OK)
482baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
483baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      PrintMessage(kReadError);
484baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
485baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
486baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (decoderSpec->SetDecoderProperties2(header, kPropertiesSize) != S_OK)
487baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
488baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      PrintMessage("SetDecoderProperties error");
489baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
490baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
491baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fileSize = 0;
492baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (int i = 0; i < 8; i++)
493baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fileSize |= ((UInt64)header[kPropertiesSize + i]) << (8 * i);
494baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
495baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (decoder->Code(inStream, outStream, 0, (fileSize == (UInt64)(Int64)-1) ? 0 : &fileSize, 0) != S_OK)
496baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
497baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      PrintMessage("Decoder error");
498baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
499baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
500baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
501baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (outStreamSpec != NULL)
502baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
503baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (outStreamSpec->Close() != S_OK)
504baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
505baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      PrintMessage("File closing error");
506baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 1;
507baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
508baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
509baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return 0;
510baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
511baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
512baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MY_CDECL main(int numArgs, const char *args[])
513baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
514baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  try { return main2(numArgs, args); }
515baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  catch(const char *s)
516baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
517baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fprintf(stderr, "\nError: %s\n", s);
518baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
519baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
520baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  catch(...)
521baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
522baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintMessage("\nError\n");
523baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
524baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
525baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
526