• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 7zMain.c - Test application for 7z Decoder
2 2023-04-04 : Igor Pavlov : Public domain */
3 
4 #include "Precomp.h"
5 
6 #include <stdio.h>
7 #include <string.h>
8 
9 #include "../../CpuArch.h"
10 
11 #include "../../7z.h"
12 #include "../../7zAlloc.h"
13 #include "../../7zBuf.h"
14 #include "../../7zCrc.h"
15 #include "../../7zFile.h"
16 #include "../../7zVersion.h"
17 
18 #ifndef USE_WINDOWS_FILE
19 /* for mkdir */
20 #ifdef _WIN32
21 #include <direct.h>
22 #else
23 #include <stdlib.h>
24 #include <time.h>
25 #ifdef __GNUC__
26 #include <sys/time.h>
27 #endif
28 #include <fcntl.h>
29 // #include <utime.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #endif
33 #endif
34 
35 #define kInputBufSize ((size_t)1 << 18)
36 
37 static const ISzAlloc g_Alloc = { SzAlloc, SzFree };
38 // static const ISzAlloc g_Alloc_temp = { SzAllocTemp, SzFreeTemp };
39 
40 
Print(const char * s)41 static void Print(const char *s)
42 {
43   fputs(s, stdout);
44 }
45 
46 
Buf_EnsureSize(CBuf * dest,size_t size)47 static int Buf_EnsureSize(CBuf *dest, size_t size)
48 {
49   if (dest->size >= size)
50     return 1;
51   Buf_Free(dest, &g_Alloc);
52   return Buf_Create(dest, size, &g_Alloc);
53 }
54 
55 #ifndef _WIN32
56 #define MY_USE_UTF8
57 #endif
58 
59 /* #define MY_USE_UTF8 */
60 
61 #ifdef MY_USE_UTF8
62 
63 #define MY_UTF8_START(n) (0x100 - (1 << (7 - (n))))
64 
65 #define MY_UTF8_RANGE(n) (((UInt32)1) << ((n) * 5 + 6))
66 
67 #define MY_UTF8_HEAD(n, val) ((Byte)(MY_UTF8_START(n) + (val >> (6 * (n)))))
68 #define MY_UTF8_CHAR(n, val) ((Byte)(0x80 + (((val) >> (6 * (n))) & 0x3F)))
69 
Utf16_To_Utf8_Calc(const UInt16 * src,const UInt16 * srcLim)70 static size_t Utf16_To_Utf8_Calc(const UInt16 *src, const UInt16 *srcLim)
71 {
72   size_t size = 0;
73   for (;;)
74   {
75     UInt32 val;
76     if (src == srcLim)
77       return size;
78 
79     size++;
80     val = *src++;
81 
82     if (val < 0x80)
83       continue;
84 
85     if (val < MY_UTF8_RANGE(1))
86     {
87       size++;
88       continue;
89     }
90 
91     if (val >= 0xD800 && val < 0xDC00 && src != srcLim)
92     {
93       const UInt32 c2 = *src;
94       if (c2 >= 0xDC00 && c2 < 0xE000)
95       {
96         src++;
97         size += 3;
98         continue;
99       }
100     }
101 
102     size += 2;
103   }
104 }
105 
Utf16_To_Utf8(Byte * dest,const UInt16 * src,const UInt16 * srcLim)106 static Byte *Utf16_To_Utf8(Byte *dest, const UInt16 *src, const UInt16 *srcLim)
107 {
108   for (;;)
109   {
110     UInt32 val;
111     if (src == srcLim)
112       return dest;
113 
114     val = *src++;
115 
116     if (val < 0x80)
117     {
118       *dest++ = (Byte)val;
119       continue;
120     }
121 
122     if (val < MY_UTF8_RANGE(1))
123     {
124       dest[0] = MY_UTF8_HEAD(1, val);
125       dest[1] = MY_UTF8_CHAR(0, val);
126       dest += 2;
127       continue;
128     }
129 
130     if (val >= 0xD800 && val < 0xDC00 && src != srcLim)
131     {
132       const UInt32 c2 = *src;
133       if (c2 >= 0xDC00 && c2 < 0xE000)
134       {
135         src++;
136         val = (((val - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000;
137         dest[0] = MY_UTF8_HEAD(3, val);
138         dest[1] = MY_UTF8_CHAR(2, val);
139         dest[2] = MY_UTF8_CHAR(1, val);
140         dest[3] = MY_UTF8_CHAR(0, val);
141         dest += 4;
142         continue;
143       }
144     }
145 
146     dest[0] = MY_UTF8_HEAD(2, val);
147     dest[1] = MY_UTF8_CHAR(1, val);
148     dest[2] = MY_UTF8_CHAR(0, val);
149     dest += 3;
150   }
151 }
152 
Utf16_To_Utf8Buf(CBuf * dest,const UInt16 * src,size_t srcLen)153 static SRes Utf16_To_Utf8Buf(CBuf *dest, const UInt16 *src, size_t srcLen)
154 {
155   size_t destLen = Utf16_To_Utf8_Calc(src, src + srcLen);
156   destLen += 1;
157   if (!Buf_EnsureSize(dest, destLen))
158     return SZ_ERROR_MEM;
159   *Utf16_To_Utf8(dest->data, src, src + srcLen) = 0;
160   return SZ_OK;
161 }
162 
163 #endif
164 
Utf16_To_Char(CBuf * buf,const UInt16 * s,UINT codePage)165 static SRes Utf16_To_Char(CBuf *buf, const UInt16 *s
166     #ifndef MY_USE_UTF8
167     , UINT codePage
168     #endif
169     )
170 {
171   unsigned len = 0;
172   for (len = 0; s[len] != 0; len++) {}
173 
174   #ifndef MY_USE_UTF8
175   {
176     const unsigned size = len * 3 + 100;
177     if (!Buf_EnsureSize(buf, size))
178       return SZ_ERROR_MEM;
179     {
180       buf->data[0] = 0;
181       if (len != 0)
182       {
183         const char defaultChar = '_';
184         BOOL defUsed;
185         const unsigned numChars = (unsigned)WideCharToMultiByte(
186             codePage, 0, (LPCWSTR)s, (int)len, (char *)buf->data, (int)size, &defaultChar, &defUsed);
187         if (numChars == 0 || numChars >= size)
188           return SZ_ERROR_FAIL;
189         buf->data[numChars] = 0;
190       }
191       return SZ_OK;
192     }
193   }
194   #else
195   return Utf16_To_Utf8Buf(buf, s, len);
196   #endif
197 }
198 
199 #ifdef _WIN32
200   #ifndef USE_WINDOWS_FILE
201     static UINT g_FileCodePage = CP_ACP;
202     #define MY_FILE_CODE_PAGE_PARAM ,g_FileCodePage
203   #endif
204 #else
205   #define MY_FILE_CODE_PAGE_PARAM
206 #endif
207 
MyCreateDir(const UInt16 * name)208 static WRes MyCreateDir(const UInt16 *name)
209 {
210   #ifdef USE_WINDOWS_FILE
211 
212   return CreateDirectoryW((LPCWSTR)name, NULL) ? 0 : GetLastError();
213 
214   #else
215 
216   CBuf buf;
217   WRes res;
218   Buf_Init(&buf);
219   RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM))
220 
221   res =
222   #ifdef _WIN32
223   _mkdir((const char *)buf.data)
224   #else
225   mkdir((const char *)buf.data, 0777)
226   #endif
227   == 0 ? 0 : errno;
228   Buf_Free(&buf, &g_Alloc);
229   return res;
230 
231   #endif
232 }
233 
OutFile_OpenUtf16(CSzFile * p,const UInt16 * name)234 static WRes OutFile_OpenUtf16(CSzFile *p, const UInt16 *name)
235 {
236   #ifdef USE_WINDOWS_FILE
237   return OutFile_OpenW(p, (LPCWSTR)name);
238   #else
239   CBuf buf;
240   WRes res;
241   Buf_Init(&buf);
242   RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM))
243   res = OutFile_Open(p, (const char *)buf.data);
244   Buf_Free(&buf, &g_Alloc);
245   return res;
246   #endif
247 }
248 
249 
PrintString(const UInt16 * s)250 static SRes PrintString(const UInt16 *s)
251 {
252   CBuf buf;
253   SRes res;
254   Buf_Init(&buf);
255   res = Utf16_To_Char(&buf, s
256       #ifndef MY_USE_UTF8
257       , CP_OEMCP
258       #endif
259       );
260   if (res == SZ_OK)
261     Print((const char *)buf.data);
262   Buf_Free(&buf, &g_Alloc);
263   return res;
264 }
265 
UInt64ToStr(UInt64 value,char * s,int numDigits)266 static void UInt64ToStr(UInt64 value, char *s, int numDigits)
267 {
268   char temp[32];
269   int pos = 0;
270   do
271   {
272     temp[pos++] = (char)('0' + (unsigned)(value % 10));
273     value /= 10;
274   }
275   while (value != 0);
276 
277   for (numDigits -= pos; numDigits > 0; numDigits--)
278     *s++ = ' ';
279 
280   do
281     *s++ = temp[--pos];
282   while (pos);
283   *s = '\0';
284 }
285 
UIntToStr(char * s,unsigned value,int numDigits)286 static char *UIntToStr(char *s, unsigned value, int numDigits)
287 {
288   char temp[16];
289   int pos = 0;
290   do
291     temp[pos++] = (char)('0' + (value % 10));
292   while (value /= 10);
293 
294   for (numDigits -= pos; numDigits > 0; numDigits--)
295     *s++ = '0';
296 
297   do
298     *s++ = temp[--pos];
299   while (pos);
300   *s = '\0';
301   return s;
302 }
303 
UIntToStr_2(char * s,unsigned value)304 static void UIntToStr_2(char *s, unsigned value)
305 {
306   s[0] = (char)('0' + (value / 10));
307   s[1] = (char)('0' + (value % 10));
308 }
309 
310 
311 #define PERIOD_4 (4 * 365 + 1)
312 #define PERIOD_100 (PERIOD_4 * 25 - 1)
313 #define PERIOD_400 (PERIOD_100 * 4 + 1)
314 
315 
316 
317 #ifndef _WIN32
318 
319 // MS uses long for BOOL, but long is 32-bit in MS. So we use int.
320 // typedef long BOOL;
321 typedef int BOOL;
322 
323 typedef struct _FILETIME
324 {
325   DWORD dwLowDateTime;
326   DWORD dwHighDateTime;
327 } FILETIME;
328 
TIME_GetBias()329 static LONG TIME_GetBias()
330 {
331   const time_t utc = time(NULL);
332   struct tm *ptm = localtime(&utc);
333   const int localdaylight = ptm->tm_isdst; /* daylight for local timezone */
334   ptm = gmtime(&utc);
335   ptm->tm_isdst = localdaylight; /* use local daylight, not that of Greenwich */
336   const LONG bias = (int)(mktime(ptm) - utc);
337   return bias;
338 }
339 
340 #define TICKS_PER_SEC 10000000
341 
342 #define GET_TIME_64(pft) ((pft)->dwLowDateTime | ((UInt64)(pft)->dwHighDateTime << 32))
343 
344 #define SET_FILETIME(ft, v64) \
345    (ft)->dwLowDateTime = (DWORD)v64; \
346    (ft)->dwHighDateTime = (DWORD)(v64 >> 32);
347 
348 #define WINAPI
349 #define TRUE 1
350 
FileTimeToLocalFileTime(const FILETIME * fileTime,FILETIME * localFileTime)351 static BOOL WINAPI FileTimeToLocalFileTime(const FILETIME *fileTime, FILETIME *localFileTime)
352 {
353   UInt64 v = GET_TIME_64(fileTime);
354   v = (UInt64)((Int64)v - (Int64)TIME_GetBias() * TICKS_PER_SEC);
355   SET_FILETIME(localFileTime, v)
356   return TRUE;
357 }
358 
359 static const UInt32 kNumTimeQuantumsInSecond = 10000000;
360 static const UInt32 kFileTimeStartYear = 1601;
361 static const UInt32 kUnixTimeStartYear = 1970;
362 static const UInt64 kUnixTimeOffset =
363     (UInt64)60 * 60 * 24 * (89 + 365 * (kUnixTimeStartYear - kFileTimeStartYear));
364 
Time_FileTimeToUnixTime64(const FILETIME * ft)365 static Int64 Time_FileTimeToUnixTime64(const FILETIME *ft)
366 {
367   const UInt64 winTime = GET_TIME_64(ft);
368   return (Int64)(winTime / kNumTimeQuantumsInSecond) - (Int64)kUnixTimeOffset;
369 }
370 
371 #if defined(_AIX)
372   #define MY_ST_TIMESPEC st_timespec
373 #else
374   #define MY_ST_TIMESPEC timespec
375 #endif
376 
FILETIME_To_timespec(const FILETIME * ft,struct MY_ST_TIMESPEC * ts)377 static void FILETIME_To_timespec(const FILETIME *ft, struct MY_ST_TIMESPEC *ts)
378 {
379   if (ft)
380   {
381     const Int64 sec = Time_FileTimeToUnixTime64(ft);
382     // time_t is long
383     const time_t sec2 = (time_t)sec;
384     if (sec2 == sec)
385     {
386       ts->tv_sec = sec2;
387       const UInt64 winTime = GET_TIME_64(ft);
388       ts->tv_nsec = (long)((winTime % 10000000) * 100);
389       return;
390     }
391   }
392   // else
393   {
394     ts->tv_sec = 0;
395     // ts.tv_nsec = UTIME_NOW; // set to the current time
396     ts->tv_nsec = UTIME_OMIT; // keep old timesptamp
397   }
398 }
399 
Set_File_FILETIME(const UInt16 * name,const FILETIME * mTime)400 static WRes Set_File_FILETIME(const UInt16 *name, const FILETIME *mTime)
401 {
402   struct timespec times[2];
403 
404   const int flags = 0; // follow link
405     // = AT_SYMLINK_NOFOLLOW; // don't follow link
406 
407   CBuf buf;
408   int res;
409   Buf_Init(&buf);
410   RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM))
411   FILETIME_To_timespec(NULL, &times[0]);
412   FILETIME_To_timespec(mTime, &times[1]);
413   res = utimensat(AT_FDCWD, (const char *)buf.data, times, flags);
414   Buf_Free(&buf, &g_Alloc);
415   if (res == 0)
416     return 0;
417   return errno;
418 }
419 
420 #endif
421 
NtfsFileTime_to_FILETIME(const CNtfsFileTime * t,FILETIME * ft)422 static void NtfsFileTime_to_FILETIME(const CNtfsFileTime *t, FILETIME *ft)
423 {
424   ft->dwLowDateTime = (DWORD)(t->Low);
425   ft->dwHighDateTime = (DWORD)(t->High);
426 }
427 
ConvertFileTimeToString(const CNtfsFileTime * nTime,char * s)428 static void ConvertFileTimeToString(const CNtfsFileTime *nTime, char *s)
429 {
430   unsigned year, mon, hour, min, sec;
431   Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
432   unsigned t;
433   UInt32 v;
434   // UInt64 v64 = nt->Low | ((UInt64)nt->High << 32);
435   UInt64 v64;
436   {
437     FILETIME fileTime, locTime;
438     NtfsFileTime_to_FILETIME(nTime, &fileTime);
439     if (!FileTimeToLocalFileTime(&fileTime, &locTime))
440     {
441       locTime.dwHighDateTime =
442       locTime.dwLowDateTime = 0;
443     }
444     v64 = locTime.dwLowDateTime | ((UInt64)locTime.dwHighDateTime << 32);
445   }
446   v64 /= 10000000;
447   sec = (unsigned)(v64 % 60); v64 /= 60;
448   min = (unsigned)(v64 % 60); v64 /= 60;
449   hour = (unsigned)(v64 % 24); v64 /= 24;
450 
451   v = (UInt32)v64;
452 
453   year = (unsigned)(1601 + v / PERIOD_400 * 400);
454   v %= PERIOD_400;
455 
456   t = v / PERIOD_100; if (t ==  4) t =  3; year += t * 100; v -= t * PERIOD_100;
457   t = v / PERIOD_4;   if (t == 25) t = 24; year += t * 4;   v -= t * PERIOD_4;
458   t = v / 365;        if (t ==  4) t =  3; year += t;       v -= t * 365;
459 
460   if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
461     ms[1] = 29;
462   for (mon = 0;; mon++)
463   {
464     const unsigned d = ms[mon];
465     if (v < d)
466       break;
467     v -= d;
468   }
469   s = UIntToStr(s, year, 4); *s++ = '-';
470   UIntToStr_2(s, mon + 1); s[2] = '-'; s += 3;
471   UIntToStr_2(s, (unsigned)v + 1); s[2] = ' '; s += 3;
472   UIntToStr_2(s, hour); s[2] = ':'; s += 3;
473   UIntToStr_2(s, min); s[2] = ':'; s += 3;
474   UIntToStr_2(s, sec); s[2] = 0;
475 }
476 
PrintLF(void)477 static void PrintLF(void)
478 {
479   Print("\n");
480 }
481 
PrintError(char * s)482 static void PrintError(char *s)
483 {
484   Print("\nERROR: ");
485   Print(s);
486   PrintLF();
487 }
488 
PrintError_WRes(const char * message,WRes wres)489 static void PrintError_WRes(const char *message, WRes wres)
490 {
491   Print("\nERROR: ");
492   Print(message);
493   PrintLF();
494   {
495     char s[32];
496     UIntToStr(s, (unsigned)wres, 1);
497     Print("System error code: ");
498     Print(s);
499   }
500   // sprintf(buffer + strlen(buffer), "\nSystem error code: %d", (unsigned)wres);
501   #ifdef _WIN32
502   {
503     char *s = NULL;
504     if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
505         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
506         NULL, wres, 0, (LPSTR) &s, 0, NULL) != 0 && s)
507     {
508       Print(" : ");
509       Print(s);
510       LocalFree(s);
511     }
512   }
513   #else
514   {
515     const char *s = strerror(wres);
516     if (s)
517     {
518       Print(" : ");
519       Print(s);
520     }
521   }
522   #endif
523   PrintLF();
524 }
525 
GetAttribString(UInt32 wa,BoolInt isDir,char * s)526 static void GetAttribString(UInt32 wa, BoolInt isDir, char *s)
527 {
528   #ifdef USE_WINDOWS_FILE
529   s[0] = (char)(((wa & FILE_ATTRIBUTE_DIRECTORY) != 0 || isDir) ? 'D' : '.');
530   s[1] = (char)(((wa & FILE_ATTRIBUTE_READONLY ) != 0) ? 'R': '.');
531   s[2] = (char)(((wa & FILE_ATTRIBUTE_HIDDEN   ) != 0) ? 'H': '.');
532   s[3] = (char)(((wa & FILE_ATTRIBUTE_SYSTEM   ) != 0) ? 'S': '.');
533   s[4] = (char)(((wa & FILE_ATTRIBUTE_ARCHIVE  ) != 0) ? 'A': '.');
534   s[5] = 0;
535   #else
536   s[0] = (char)(((wa & (1 << 4)) != 0 || isDir) ? 'D' : '.');
537   s[1] = 0;
538   #endif
539 }
540 
541 
542 // #define NUM_PARENTS_MAX 128
543 
main(int numargs,char * args[])544 int Z7_CDECL main(int numargs, char *args[])
545 {
546   ISzAlloc allocImp;
547   ISzAlloc allocTempImp;
548 
549   CFileInStream archiveStream;
550   CLookToRead2 lookStream;
551   CSzArEx db;
552   SRes res;
553   UInt16 *temp = NULL;
554   size_t tempSize = 0;
555   // UInt32 parents[NUM_PARENTS_MAX];
556 
557   Print("\n7z Decoder " MY_VERSION_CPU " : " MY_COPYRIGHT_DATE "\n\n");
558 
559   if (numargs == 1)
560   {
561     Print(
562       "Usage: 7zDec <command> <archive_name>\n\n"
563       "<Commands>\n"
564       "  e: Extract files from archive (without using directory names)\n"
565       "  l: List contents of archive\n"
566       "  t: Test integrity of archive\n"
567       "  x: eXtract files with full paths\n");
568     return 0;
569   }
570 
571   if (numargs < 3)
572   {
573     PrintError("incorrect command");
574     return 1;
575   }
576 
577   #if defined(_WIN32) && !defined(USE_WINDOWS_FILE) && !defined(UNDER_CE)
578   g_FileCodePage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
579   #endif
580 
581 
582   allocImp = g_Alloc;
583   allocTempImp = g_Alloc;
584   // allocTempImp = g_Alloc_temp;
585 
586   {
587     WRes wres =
588     #ifdef UNDER_CE
589       InFile_OpenW(&archiveStream.file, L"\test.7z"); // change it
590     #else
591       InFile_Open(&archiveStream.file, args[2]);
592     #endif
593     if (wres != 0)
594     {
595       PrintError_WRes("cannot open input file", wres);
596       return 1;
597     }
598   }
599 
600   FileInStream_CreateVTable(&archiveStream);
601   archiveStream.wres = 0;
602   LookToRead2_CreateVTable(&lookStream, False);
603   lookStream.buf = NULL;
604 
605   res = SZ_OK;
606 
607   {
608     lookStream.buf = (Byte *)ISzAlloc_Alloc(&allocImp, kInputBufSize);
609     if (!lookStream.buf)
610       res = SZ_ERROR_MEM;
611     else
612     {
613       lookStream.bufSize = kInputBufSize;
614       lookStream.realStream = &archiveStream.vt;
615       LookToRead2_INIT(&lookStream)
616     }
617   }
618 
619   CrcGenerateTable();
620 
621   SzArEx_Init(&db);
622 
623   if (res == SZ_OK)
624   {
625     res = SzArEx_Open(&db, &lookStream.vt, &allocImp, &allocTempImp);
626   }
627 
628   if (res == SZ_OK)
629   {
630     char *command = args[1];
631     int listCommand = 0, testCommand = 0, fullPaths = 0;
632 
633     if (strcmp(command, "l") == 0) listCommand = 1;
634     else if (strcmp(command, "t") == 0) testCommand = 1;
635     else if (strcmp(command, "e") == 0) { }
636     else if (strcmp(command, "x") == 0) { fullPaths = 1; }
637     else
638     {
639       PrintError("incorrect command");
640       res = SZ_ERROR_FAIL;
641     }
642 
643     if (res == SZ_OK)
644     {
645       UInt32 i;
646 
647       /*
648       if you need cache, use these 3 variables.
649       if you use external function, you can make these variable as static.
650       */
651       UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */
652       Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */
653       size_t outBufferSize = 0;  /* it can have any value before first call (if outBuffer = 0) */
654 
655       for (i = 0; i < db.NumFiles; i++)
656       {
657         size_t offset = 0;
658         size_t outSizeProcessed = 0;
659         // const CSzFileItem *f = db.Files + i;
660         size_t len;
661         const BoolInt isDir = SzArEx_IsDir(&db, i);
662         if (listCommand == 0 && isDir && !fullPaths)
663           continue;
664         len = SzArEx_GetFileNameUtf16(&db, i, NULL);
665         // len = SzArEx_GetFullNameLen(&db, i);
666 
667         if (len > tempSize)
668         {
669           SzFree(NULL, temp);
670           tempSize = len;
671           temp = (UInt16 *)SzAlloc(NULL, tempSize * sizeof(temp[0]));
672           if (!temp)
673           {
674             res = SZ_ERROR_MEM;
675             break;
676           }
677         }
678 
679         SzArEx_GetFileNameUtf16(&db, i, temp);
680         /*
681         if (SzArEx_GetFullNameUtf16_Back(&db, i, temp + len) != temp)
682         {
683           res = SZ_ERROR_FAIL;
684           break;
685         }
686         */
687 
688         if (listCommand)
689         {
690           char attr[8], s[32], t[32];
691           UInt64 fileSize;
692 
693           GetAttribString(SzBitWithVals_Check(&db.Attribs, i) ? db.Attribs.Vals[i] : 0, isDir, attr);
694 
695           fileSize = SzArEx_GetFileSize(&db, i);
696           UInt64ToStr(fileSize, s, 10);
697 
698           if (SzBitWithVals_Check(&db.MTime, i))
699             ConvertFileTimeToString(&db.MTime.Vals[i], t);
700           else
701           {
702             size_t j;
703             for (j = 0; j < 19; j++)
704               t[j] = ' ';
705             t[j] = '\0';
706           }
707 
708           Print(t);
709           Print(" ");
710           Print(attr);
711           Print(" ");
712           Print(s);
713           Print("  ");
714           res = PrintString(temp);
715           if (res != SZ_OK)
716             break;
717           if (isDir)
718             Print("/");
719           PrintLF();
720           continue;
721         }
722 
723         Print(testCommand ?
724             "T ":
725             "- ");
726         res = PrintString(temp);
727         if (res != SZ_OK)
728           break;
729 
730         if (isDir)
731           Print("/");
732         else
733         {
734           res = SzArEx_Extract(&db, &lookStream.vt, i,
735               &blockIndex, &outBuffer, &outBufferSize,
736               &offset, &outSizeProcessed,
737               &allocImp, &allocTempImp);
738           if (res != SZ_OK)
739             break;
740         }
741 
742         if (!testCommand)
743         {
744           CSzFile outFile;
745           size_t processedSize;
746           size_t j;
747           UInt16 *name = (UInt16 *)temp;
748           const UInt16 *destPath = (const UInt16 *)name;
749 
750           for (j = 0; name[j] != 0; j++)
751             if (name[j] == '/')
752             {
753               if (fullPaths)
754               {
755                 name[j] = 0;
756                 MyCreateDir(name);
757                 name[j] = CHAR_PATH_SEPARATOR;
758               }
759               else
760                 destPath = name + j + 1;
761             }
762 
763           if (isDir)
764           {
765             MyCreateDir(destPath);
766             PrintLF();
767             continue;
768           }
769           else
770           {
771             const WRes wres = OutFile_OpenUtf16(&outFile, destPath);
772             if (wres != 0)
773             {
774               PrintError_WRes("cannot open output file", wres);
775               res = SZ_ERROR_FAIL;
776               break;
777             }
778           }
779 
780           processedSize = outSizeProcessed;
781 
782           {
783             const WRes wres = File_Write(&outFile, outBuffer + offset, &processedSize);
784             if (wres != 0 || processedSize != outSizeProcessed)
785             {
786               PrintError_WRes("cannot write output file", wres);
787               res = SZ_ERROR_FAIL;
788               break;
789             }
790           }
791 
792           {
793             FILETIME mtime;
794             FILETIME *mtimePtr = NULL;
795 
796             #ifdef USE_WINDOWS_FILE
797             FILETIME ctime;
798             FILETIME *ctimePtr = NULL;
799             #endif
800 
801             if (SzBitWithVals_Check(&db.MTime, i))
802             {
803               const CNtfsFileTime *t = &db.MTime.Vals[i];
804               mtime.dwLowDateTime = (DWORD)(t->Low);
805               mtime.dwHighDateTime = (DWORD)(t->High);
806               mtimePtr = &mtime;
807             }
808 
809             #ifdef USE_WINDOWS_FILE
810             if (SzBitWithVals_Check(&db.CTime, i))
811             {
812               const CNtfsFileTime *t = &db.CTime.Vals[i];
813               ctime.dwLowDateTime = (DWORD)(t->Low);
814               ctime.dwHighDateTime = (DWORD)(t->High);
815               ctimePtr = &ctime;
816             }
817 
818             if (mtimePtr || ctimePtr)
819               SetFileTime(outFile.handle, ctimePtr, NULL, mtimePtr);
820             #endif
821 
822             {
823               const WRes wres = File_Close(&outFile);
824               if (wres != 0)
825               {
826                 PrintError_WRes("cannot close output file", wres);
827                 res = SZ_ERROR_FAIL;
828                 break;
829               }
830             }
831 
832             #ifndef USE_WINDOWS_FILE
833             #ifdef _WIN32
834             mtimePtr = mtimePtr;
835             #else
836             if (mtimePtr)
837               Set_File_FILETIME(destPath, mtimePtr);
838             #endif
839             #endif
840           }
841 
842           #ifdef USE_WINDOWS_FILE
843           if (SzBitWithVals_Check(&db.Attribs, i))
844           {
845             UInt32 attrib = db.Attribs.Vals[i];
846             /* p7zip stores posix attributes in high 16 bits and adds 0x8000 as marker.
847                We remove posix bits, if we detect posix mode field */
848             if ((attrib & 0xF0000000) != 0)
849               attrib &= 0x7FFF;
850             SetFileAttributesW((LPCWSTR)destPath, attrib);
851           }
852           #endif
853         }
854         PrintLF();
855       }
856       ISzAlloc_Free(&allocImp, outBuffer);
857     }
858   }
859 
860   SzFree(NULL, temp);
861   SzArEx_Free(&db, &allocImp);
862   ISzAlloc_Free(&allocImp, lookStream.buf);
863 
864   File_Close(&archiveStream.file);
865 
866   if (res == SZ_OK)
867   {
868     Print("\nEverything is Ok\n");
869     return 0;
870   }
871 
872   if (res == SZ_ERROR_UNSUPPORTED)
873     PrintError("decoder doesn't support this archive");
874   else if (res == SZ_ERROR_MEM)
875     PrintError("cannot allocate memory");
876   else if (res == SZ_ERROR_CRC)
877     PrintError("CRC error");
878   else if (res == SZ_ERROR_READ /* || archiveStream.Res != 0 */)
879     PrintError_WRes("Read Error", archiveStream.wres);
880   else
881   {
882     char s[32];
883     UInt64ToStr((unsigned)res, s, 0);
884     PrintError(s);
885   }
886 
887   return 1;
888 }
889