1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* 7zMain.c - Test application for 7z Decoder
2cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky2015-01-02 : Igor Pavlov : Public domain */
3cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
4cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#include "Precomp.h"
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <stdio.h>
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <string.h>
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../7z.h"
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../7zAlloc.h"
11cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#include "../../7zBuf.h"
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../7zCrc.h"
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../7zFile.h"
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../7zVersion.h"
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef USE_WINDOWS_FILE
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/* for mkdir */
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifdef _WIN32
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <direct.h>
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#else
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <sys/stat.h>
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <errno.h>
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
26baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic ISzAlloc g_Alloc = { SzAlloc, SzFree };
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
28baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic int Buf_EnsureSize(CBuf *dest, size_t size)
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (dest->size >= size)
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Buf_Free(dest, &g_Alloc);
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return Buf_Create(dest, size, &g_Alloc);
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _WIN32
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
38baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
40baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic Bool Utf16_To_Utf8(Byte *dest, size_t *destLen, const UInt16 *src, size_t srcLen)
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  size_t destPos = 0, srcPos = 0;
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (;;)
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    unsigned numAdds;
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt32 value;
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (srcPos == srcLen)
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      *destLen = destPos;
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return True;
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    value = src[srcPos++];
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (value < 0x80)
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (dest)
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        dest[destPos] = (char)value;
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      destPos++;
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      continue;
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (value >= 0xD800 && value < 0xE000)
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 c2;
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (value >= 0xDC00 || srcPos == srcLen)
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        break;
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      c2 = src[srcPos++];
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (c2 < 0xDC00 || c2 >= 0xE000)
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        break;
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      value = (((value - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000;
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (numAdds = 1; numAdds < 5; numAdds++)
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (value < (((UInt32)1) << (numAdds * 5 + 6)))
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        break;
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (dest)
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      dest[destPos] = (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds)));
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    destPos++;
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    do
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      numAdds--;
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (dest)
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        dest[destPos] = (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F));
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      destPos++;
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    while (numAdds != 0);
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *destLen = destPos;
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return False;
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
89baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes Utf16_To_Utf8Buf(CBuf *dest, const UInt16 *src, size_t srcLen)
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  size_t destLen = 0;
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Bool res;
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Utf16_To_Utf8(NULL, &destLen, src, srcLen);
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  destLen += 1;
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!Buf_EnsureSize(dest, destLen))
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return SZ_ERROR_MEM;
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res = Utf16_To_Utf8(dest->data, &destLen, src, srcLen);
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  dest->data[destLen] = 0;
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return res ? SZ_OK : SZ_ERROR_FAIL;
100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
101cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
104cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckystatic SRes Utf16_To_Char(CBuf *buf, const UInt16 *s
105cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    #ifdef _WIN32
106cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    , UINT codePage
107cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    #endif
108cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    )
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
110cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  unsigned len = 0;
111cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  for (len = 0; s[len] != 0; len++);
112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef _WIN32
114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
115cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    unsigned size = len * 3 + 100;
116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (!Buf_EnsureSize(buf, size))
117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return SZ_ERROR_MEM;
118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
119cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      buf->data[0] = 0;
120cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      if (len != 0)
121cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      {
122cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        char defaultChar = '_';
123cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        BOOL defUsed;
124cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        unsigned numChars = 0;
125cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        numChars = WideCharToMultiByte(codePage, 0, s, len, (char *)buf->data, size, &defaultChar, &defUsed);
126cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        if (numChars == 0 || numChars >= size)
127cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          return SZ_ERROR_FAIL;
128cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        buf->data[numChars] = 0;
129cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      }
130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return SZ_OK;
131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return Utf16_To_Utf8Buf(buf, s, len);
135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
138cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#ifdef _WIN32
139cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #ifndef USE_WINDOWS_FILE
140cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    static UINT g_FileCodePage = CP_ACP;
141cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #endif
142cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #define MY_FILE_CODE_PAGE_PARAM ,g_FileCodePage
143cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#else
144cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #define MY_FILE_CODE_PAGE_PARAM
145cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#endif
146cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
147baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic WRes MyCreateDir(const UInt16 *name)
148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef USE_WINDOWS_FILE
150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return CreateDirectoryW(name, NULL) ? 0 : GetLastError();
152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
154baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CBuf buf;
156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  WRes res;
157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Buf_Init(&buf);
158cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM));
159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res =
161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef _WIN32
162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _mkdir((const char *)buf.data)
163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  mkdir((const char *)buf.data, 0777)
165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
166baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  == 0 ? 0 : errno;
167baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Buf_Free(&buf, &g_Alloc);
168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return res;
169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
173baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic WRes OutFile_OpenUtf16(CSzFile *p, const UInt16 *name)
174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
175baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef USE_WINDOWS_FILE
176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return OutFile_OpenW(p, name);
177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
178baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CBuf buf;
179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  WRes res;
180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Buf_Init(&buf);
181cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM));
182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res = OutFile_Open(p, (const char *)buf.data);
183baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Buf_Free(&buf, &g_Alloc);
184baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return res;
185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
188baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic SRes PrintString(const UInt16 *s)
189baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CBuf buf;
191baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SRes res;
192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Buf_Init(&buf);
193cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  res = Utf16_To_Char(&buf, s
194cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      #ifdef _WIN32
195cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      , CP_OEMCP
196cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      #endif
197cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      );
198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res == SZ_OK)
199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fputs((const char *)buf.data, stdout);
200baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Buf_Free(&buf, &g_Alloc);
201baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return res;
202baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
203baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
204baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void UInt64ToStr(UInt64 value, char *s)
205baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
206baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  char temp[32];
207baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int pos = 0;
208baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
209baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
210baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    temp[pos++] = (char)('0' + (unsigned)(value % 10));
211baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    value /= 10;
212baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
213baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (value != 0);
214baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
215baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *s++ = temp[--pos];
216baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (pos);
217baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *s = '\0';
218baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
219baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
220baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic char *UIntToStr(char *s, unsigned value, int numDigits)
221baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
222baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  char temp[16];
223baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int pos = 0;
224baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
225baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    temp[pos++] = (char)('0' + (value % 10));
226baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (value /= 10);
227baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (numDigits -= pos; numDigits > 0; numDigits--)
228baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *s++ = '0';
229baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  do
230baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    *s++ = temp[--pos];
231baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (pos);
232baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *s = '\0';
233baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return s;
234baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
235baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
236cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckystatic void UIntToStr_2(char *s, unsigned value)
237cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky{
238cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  s[0] = (char)('0' + (value / 10));
239cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  s[1] = (char)('0' + (value % 10));
240cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky}
241cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
242baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PERIOD_4 (4 * 365 + 1)
243baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PERIOD_100 (PERIOD_4 * 25 - 1)
244baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define PERIOD_400 (PERIOD_100 * 4 + 1)
245baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
246cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckystatic void ConvertFileTimeToString(const CNtfsFileTime *nt, char *s)
247baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
248cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  unsigned year, mon, hour, min, sec;
249baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
250baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  unsigned t;
251baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 v;
252cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  UInt64 v64 = nt->Low | ((UInt64)nt->High << 32);
253cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  v64 /= 10000000;
254baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  sec = (unsigned)(v64 % 60); v64 /= 60;
255baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  min = (unsigned)(v64 % 60); v64 /= 60;
256baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  hour = (unsigned)(v64 % 24); v64 /= 24;
257baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
258baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  v = (UInt32)v64;
259baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
260baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  year = (unsigned)(1601 + v / PERIOD_400 * 400);
261baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  v %= PERIOD_400;
262baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
263baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  t = v / PERIOD_100; if (t ==  4) t =  3; year += t * 100; v -= t * PERIOD_100;
264baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  t = v / PERIOD_4;   if (t == 25) t = 24; year += t * 4;   v -= t * PERIOD_4;
265baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  t = v / 365;        if (t ==  4) t =  3; year += t;       v -= t * 365;
266baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
267baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
268baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ms[1] = 29;
269cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  for (mon = 0;; mon++)
270baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
271cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    unsigned s = ms[mon];
272baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (v < s)
273baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      break;
274baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    v -= s;
275baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
276baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  s = UIntToStr(s, year, 4); *s++ = '-';
277cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  UIntToStr_2(s, mon + 1); s[2] = '-'; s += 3;
278cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  UIntToStr_2(s, (unsigned)v + 1); s[2] = ' '; s += 3;
279cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  UIntToStr_2(s, hour); s[2] = ':'; s += 3;
280cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  UIntToStr_2(s, min); s[2] = ':'; s += 3;
281cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  UIntToStr_2(s, sec); s[2] = 0;
282baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
283baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
284cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckyvoid PrintError(const char *sz)
285baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
286baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  printf("\nERROR: %s\n", sz);
287baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
288baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
289baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifdef USE_WINDOWS_FILE
290baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void GetAttribString(UInt32 wa, Bool isDir, char *s)
291baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
292cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  s[0] = (char)(((wa & FILE_ATTRIBUTE_DIRECTORY) != 0 || isDir) ? 'D' : '.');
293cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  s[1] = (char)(((wa & FILE_ATTRIBUTE_READONLY ) != 0) ? 'R': '.');
294cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  s[2] = (char)(((wa & FILE_ATTRIBUTE_HIDDEN   ) != 0) ? 'H': '.');
295cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  s[3] = (char)(((wa & FILE_ATTRIBUTE_SYSTEM   ) != 0) ? 'S': '.');
296cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  s[4] = (char)(((wa & FILE_ATTRIBUTE_ARCHIVE  ) != 0) ? 'A': '.');
297baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  s[5] = '\0';
298baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
299baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#else
300baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void GetAttribString(UInt32, Bool, char *s)
301baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
302baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  s[0] = '\0';
303baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
304baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
305baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
306cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky// #define NUM_PARENTS_MAX 128
307cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
308baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MY_CDECL main(int numargs, char *args[])
309baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
310baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CFileInStream archiveStream;
311baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CLookToRead lookStream;
312baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CSzArEx db;
313baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SRes res;
314baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ISzAlloc allocImp;
315baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ISzAlloc allocTempImp;
316baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt16 *temp = NULL;
317baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  size_t tempSize = 0;
318cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  // UInt32 parents[NUM_PARENTS_MAX];
319baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
320baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  printf("\n7z ANSI-C Decoder " MY_VERSION_COPYRIGHT_DATE "\n\n");
321baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numargs == 1)
322baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
323baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    printf(
324baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      "Usage: 7zDec <command> <archive_name>\n\n"
325baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      "<Commands>\n"
326baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      "  e: Extract files from archive (without using directory names)\n"
327baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      "  l: List contents of archive\n"
328baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      "  t: Test integrity of archive\n"
329baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      "  x: eXtract files with full paths\n");
330baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0;
331baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
332baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numargs < 3)
333baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
334baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintError("incorrect command");
335baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
336baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
337baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
338cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #if defined(_WIN32) && !defined(USE_WINDOWS_FILE) && !defined(UNDER_CE)
339cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  g_FileCodePage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
340cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #endif
341cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
342baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  allocImp.Alloc = SzAlloc;
343baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  allocImp.Free = SzFree;
344baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
345baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  allocTempImp.Alloc = SzAllocTemp;
346baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  allocTempImp.Free = SzFreeTemp;
347baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
348cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #ifdef UNDER_CE
349cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  if (InFile_OpenW(&archiveStream.file, L"\test.7z"))
350cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #else
351baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (InFile_Open(&archiveStream.file, args[2]))
352cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  #endif
353baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
354baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintError("can not open input file");
355baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 1;
356baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
357baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
358baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  FileInStream_CreateVTable(&archiveStream);
359baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LookToRead_CreateVTable(&lookStream, False);
360baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
361baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  lookStream.realStream = &archiveStream.s;
362baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LookToRead_Init(&lookStream);
363baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
364baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CrcGenerateTable();
365baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
366baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SzArEx_Init(&db);
367baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp);
368baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res == SZ_OK)
369baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
370baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    char *command = args[1];
371cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    int listCommand = 0, testCommand = 0, fullPaths = 0;
372baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (strcmp(command, "l") == 0) listCommand = 1;
373baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else if (strcmp(command, "t") == 0) testCommand = 1;
374cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    else if (strcmp(command, "e") == 0) { }
375cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    else if (strcmp(command, "x") == 0) { fullPaths = 1; }
376baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    else
377baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
378baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      PrintError("incorrect command");
379baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      res = SZ_ERROR_FAIL;
380baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
381baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
382baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (res == SZ_OK)
383baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
384baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 i;
385baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
386baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      /*
387baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if you need cache, use these 3 variables.
388baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if you use external function, you can make these variable as static.
389baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      */
390baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */
391baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */
392baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      size_t outBufferSize = 0;  /* it can have any value before first call (if outBuffer = 0) */
393baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
394cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky      for (i = 0; i < db.NumFiles; i++)
395baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
396baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        size_t offset = 0;
397baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        size_t outSizeProcessed = 0;
398cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        // const CSzFileItem *f = db.Files + i;
399baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        size_t len;
400cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        int isDir = SzArEx_IsDir(&db, i);
401cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        if (listCommand == 0 && isDir && !fullPaths)
402baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          continue;
403baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        len = SzArEx_GetFileNameUtf16(&db, i, NULL);
404cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        // len = SzArEx_GetFullNameLen(&db, i);
405baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
406baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (len > tempSize)
407baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
408baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          SzFree(NULL, temp);
409baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          tempSize = len;
410baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          temp = (UInt16 *)SzAlloc(NULL, tempSize * sizeof(temp[0]));
411cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          if (!temp)
412baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
413baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            res = SZ_ERROR_MEM;
414baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            break;
415baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
416baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
417baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
418baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        SzArEx_GetFileNameUtf16(&db, i, temp);
419cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        /*
420cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        if (SzArEx_GetFullNameUtf16_Back(&db, i, temp + len) != temp)
421cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        {
422cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          res = SZ_ERROR_FAIL;
423cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          break;
424cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        }
425cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        */
426cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky
427baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (listCommand)
428baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
429baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          char attr[8], s[32], t[32];
430cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          UInt64 fileSize;
431baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
432cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          GetAttribString(SzBitWithVals_Check(&db.Attribs, i) ? db.Attribs.Vals[i] : 0, isDir, attr);
433baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
434cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          fileSize = SzArEx_GetFileSize(&db, i);
435cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          UInt64ToStr(fileSize, s);
436cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          if (SzBitWithVals_Check(&db.MTime, i))
437cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky            ConvertFileTimeToString(&db.MTime.Vals[i], t);
438baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          else
439baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
440baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            size_t j;
441baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            for (j = 0; j < 19; j++)
442baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              t[j] = ' ';
443baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            t[j] = '\0';
444baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
445baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
446baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          printf("%s %s %10s  ", t, attr, s);
447baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          res = PrintString(temp);
448baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (res != SZ_OK)
449baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            break;
450cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          if (isDir)
451baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            printf("/");
452baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          printf("\n");
453baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          continue;
454baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
455baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        fputs(testCommand ?
456baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            "Testing    ":
457baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            "Extracting ",
458baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            stdout);
459baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        res = PrintString(temp);
460baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (res != SZ_OK)
461baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          break;
462cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky        if (isDir)
463baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          printf("/");
464baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        else
465baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
466baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          res = SzArEx_Extract(&db, &lookStream.s, i,
467baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              &blockIndex, &outBuffer, &outBufferSize,
468baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              &offset, &outSizeProcessed,
469baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              &allocImp, &allocTempImp);
470baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (res != SZ_OK)
471baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            break;
472baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
473baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (!testCommand)
474baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        {
475baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          CSzFile outFile;
476baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          size_t processedSize;
477baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          size_t j;
478baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          UInt16 *name = (UInt16 *)temp;
479baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          const UInt16 *destPath = (const UInt16 *)name;
480baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          for (j = 0; name[j] != 0; j++)
481baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            if (name[j] == '/')
482baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            {
483baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              if (fullPaths)
484baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              {
485baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                name[j] = 0;
486baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                MyCreateDir(name);
487baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                name[j] = CHAR_PATH_SEPARATOR;
488baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              }
489baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              else
490baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                destPath = name + j + 1;
491baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            }
492baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
493cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          if (isDir)
494baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
495baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            MyCreateDir(destPath);
496baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            printf("\n");
497baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            continue;
498baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
499baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          else if (OutFile_OpenUtf16(&outFile, destPath))
500baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
501baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            PrintError("can not open output file");
502baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            res = SZ_ERROR_FAIL;
503baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            break;
504baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
505baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          processedSize = outSizeProcessed;
506baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (File_Write(&outFile, outBuffer + offset, &processedSize) != 0 || processedSize != outSizeProcessed)
507baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
508baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            PrintError("can not write output file");
509baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            res = SZ_ERROR_FAIL;
510baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            break;
511baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
512baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          if (File_Close(&outFile))
513baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          {
514baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            PrintError("can not close output file");
515baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            res = SZ_ERROR_FAIL;
516baa3858d3f5d128a5c8466b700098109edcad5f2repo sync            break;
517baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          }
518baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          #ifdef USE_WINDOWS_FILE
519cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky          if (SzBitWithVals_Check(&db.Attribs, i))
520cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky            SetFileAttributesW(destPath, db.Attribs.Vals[i]);
521baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          #endif
522baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        }
523baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        printf("\n");
524baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
525baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      IAlloc_Free(&allocImp, outBuffer);
526baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
527baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
528baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SzArEx_Free(&db, &allocImp);
529baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  SzFree(NULL, temp);
530baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
531baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  File_Close(&archiveStream.file);
532baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res == SZ_OK)
533baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
534baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    printf("\nEverything is Ok\n");
535baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0;
536baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
537baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res == SZ_ERROR_UNSUPPORTED)
538baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintError("decoder doesn't support this archive");
539baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else if (res == SZ_ERROR_MEM)
540baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintError("can not allocate memory");
541baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else if (res == SZ_ERROR_CRC)
542baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintError("CRC error");
543baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else
544baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    printf("\nERROR #%d\n", res);
545baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return 1;
546baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
547