1 /* 7zMain.c - Test application for 7z Decoder
2 2024-02-28 : Igor Pavlov : Public domain */
3
4 #include "Precomp.h"
5
6 #include <stdio.h>
7 #include <string.h>
8
9 #ifndef USE_WINDOWS_FILE
10 /* for mkdir */
11 #ifdef _WIN32
12 #include <direct.h>
13 #else
14 #include <stdlib.h>
15 #include <time.h>
16 #ifdef __GNUC__
17 #include <sys/time.h>
18 #endif
19 #include <fcntl.h>
20 // #include <utime.h>
21 #include <sys/stat.h>
22 #include <errno.h>
23 #endif
24 #endif
25
26 #include "../../7zFile.h"
27 #include "../../7z.h"
28 #include "../../7zAlloc.h"
29 #include "../../7zBuf.h"
30 #include "../../7zCrc.h"
31 #include "../../7zVersion.h"
32
33 #include "../../CpuArch.h"
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 size_t len = 0;
172 for (len = 0; s[len] != 0; len++) {}
173
174 #ifndef MY_USE_UTF8
175 {
176 const size_t 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
324 {
325 DWORD dwLowDateTime;
326 DWORD dwHighDateTime;
327 } FILETIME;
328
TIME_GetBias(void)329 static LONG TIME_GetBias(void)
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 return (int)(mktime(ptm) - utc);
337 }
338
339 #define TICKS_PER_SEC 10000000
340
341 #define GET_TIME_64(pft) ((pft)->dwLowDateTime | ((UInt64)(pft)->dwHighDateTime << 32))
342
343 #define SET_FILETIME(ft, v64) \
344 (ft)->dwLowDateTime = (DWORD)v64; \
345 (ft)->dwHighDateTime = (DWORD)(v64 >> 32);
346
347 #define WINAPI
348 #define TRUE 1
349
FileTimeToLocalFileTime(const FILETIME * fileTime,FILETIME * localFileTime)350 static BOOL WINAPI FileTimeToLocalFileTime(const FILETIME *fileTime, FILETIME *localFileTime)
351 {
352 UInt64 v = GET_TIME_64(fileTime);
353 v = (UInt64)((Int64)v - (Int64)TIME_GetBias() * TICKS_PER_SEC);
354 SET_FILETIME(localFileTime, v)
355 return TRUE;
356 }
357
358 static const UInt32 kNumTimeQuantumsInSecond = 10000000;
359 static const UInt32 kFileTimeStartYear = 1601;
360 static const UInt32 kUnixTimeStartYear = 1970;
361
Time_FileTimeToUnixTime64(const FILETIME * ft)362 static Int64 Time_FileTimeToUnixTime64(const FILETIME *ft)
363 {
364 const UInt64 kUnixTimeOffset =
365 (UInt64)60 * 60 * 24 * (89 + 365 * (kUnixTimeStartYear - kFileTimeStartYear));
366 const UInt64 winTime = GET_TIME_64(ft);
367 return (Int64)(winTime / kNumTimeQuantumsInSecond) - (Int64)kUnixTimeOffset;
368 }
369
370 #if defined(_AIX)
371 #define MY_ST_TIMESPEC st_timespec
372 #else
373 #define MY_ST_TIMESPEC timespec
374 #endif
375
FILETIME_To_timespec(const FILETIME * ft,struct MY_ST_TIMESPEC * ts)376 static void FILETIME_To_timespec(const FILETIME *ft, struct MY_ST_TIMESPEC *ts)
377 {
378 if (ft)
379 {
380 const Int64 sec = Time_FileTimeToUnixTime64(ft);
381 // time_t is long
382 const time_t sec2 = (time_t)sec;
383 if (sec2 == sec)
384 {
385 ts->tv_sec = sec2;
386 {
387 const UInt64 winTime = GET_TIME_64(ft);
388 ts->tv_nsec = (long)((winTime % 10000000) * 100);
389 }
390 return;
391 }
392 }
393 // else
394 {
395 ts->tv_sec = 0;
396 // ts.tv_nsec = UTIME_NOW; // set to the current time
397 ts->tv_nsec = UTIME_OMIT; // keep old timesptamp
398 }
399 }
400
Set_File_FILETIME(const UInt16 * name,const FILETIME * mTime)401 static WRes Set_File_FILETIME(const UInt16 *name, const FILETIME *mTime)
402 {
403 struct timespec times[2];
404
405 const int flags = 0; // follow link
406 // = AT_SYMLINK_NOFOLLOW; // don't follow link
407
408 CBuf buf;
409 int res;
410 Buf_Init(&buf);
411 RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM))
412 FILETIME_To_timespec(NULL, ×[0]);
413 FILETIME_To_timespec(mTime, ×[1]);
414 res = utimensat(AT_FDCWD, (const char *)buf.data, times, flags);
415 Buf_Free(&buf, &g_Alloc);
416 if (res == 0)
417 return 0;
418 return errno;
419 }
420
421 #endif
422
NtfsFileTime_to_FILETIME(const CNtfsFileTime * t,FILETIME * ft)423 static void NtfsFileTime_to_FILETIME(const CNtfsFileTime *t, FILETIME *ft)
424 {
425 ft->dwLowDateTime = (DWORD)(t->Low);
426 ft->dwHighDateTime = (DWORD)(t->High);
427 }
428
ConvertFileTimeToString(const CNtfsFileTime * nTime,char * s)429 static void ConvertFileTimeToString(const CNtfsFileTime *nTime, char *s)
430 {
431 unsigned year, mon, hour, min, sec;
432 Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
433 UInt32 t;
434 UInt32 v;
435 // UInt64 v64 = nt->Low | ((UInt64)nt->High << 32);
436 UInt64 v64;
437 {
438 FILETIME fileTime, locTime;
439 NtfsFileTime_to_FILETIME(nTime, &fileTime);
440 if (!FileTimeToLocalFileTime(&fileTime, &locTime))
441 {
442 locTime.dwHighDateTime =
443 locTime.dwLowDateTime = 0;
444 }
445 v64 = locTime.dwLowDateTime | ((UInt64)locTime.dwHighDateTime << 32);
446 }
447 v64 /= 10000000;
448 sec = (unsigned)(v64 % 60); v64 /= 60;
449 min = (unsigned)(v64 % 60); v64 /= 60;
450 hour = (unsigned)(v64 % 24); v64 /= 24;
451
452 v = (UInt32)v64;
453
454 year = (unsigned)(1601 + v / PERIOD_400 * 400);
455 v %= PERIOD_400;
456
457 t = v / PERIOD_100; if (t == 4) t = 3; year += t * 100; v -= t * PERIOD_100;
458 t = v / PERIOD_4; if (t == 25) t = 24; year += t * 4; v -= t * PERIOD_4;
459 t = v / 365; if (t == 4) t = 3; year += t; v -= t * 365;
460
461 if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
462 ms[1] = 29;
463 for (mon = 0;; mon++)
464 {
465 const UInt32 d = ms[mon];
466 if (v < d)
467 break;
468 v -= d;
469 }
470 s = UIntToStr(s, year, 4); *s++ = '-';
471 UIntToStr_2(s, mon + 1); s[2] = '-'; s += 3;
472 UIntToStr_2(s, (unsigned)v + 1); s[2] = ' '; s += 3;
473 UIntToStr_2(s, hour); s[2] = ':'; s += 3;
474 UIntToStr_2(s, min); s[2] = ':'; s += 3;
475 UIntToStr_2(s, sec); s[2] = 0;
476 }
477
PrintLF(void)478 static void PrintLF(void)
479 {
480 Print("\n");
481 }
482
PrintError(char * s)483 static void PrintError(char *s)
484 {
485 Print("\nERROR: ");
486 Print(s);
487 PrintLF();
488 }
489
PrintError_WRes(const char * message,WRes wres)490 static void PrintError_WRes(const char *message, WRes wres)
491 {
492 Print("\nERROR: ");
493 Print(message);
494 PrintLF();
495 {
496 char s[32];
497 UIntToStr(s, (unsigned)wres, 1);
498 Print("System error code: ");
499 Print(s);
500 }
501 // sprintf(buffer + strlen(buffer), "\nSystem error code: %d", (unsigned)wres);
502 #ifdef _WIN32
503 {
504 char *s = NULL;
505 if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
506 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
507 NULL, wres, 0, (LPSTR) &s, 0, NULL) != 0 && s)
508 {
509 Print(" : ");
510 Print(s);
511 LocalFree(s);
512 }
513 }
514 #else
515 {
516 const char *s = strerror(wres);
517 if (s)
518 {
519 Print(" : ");
520 Print(s);
521 }
522 }
523 #endif
524 PrintLF();
525 }
526
GetAttribString(UInt32 wa,BoolInt isDir,char * s)527 static void GetAttribString(UInt32 wa, BoolInt isDir, char *s)
528 {
529 #ifdef USE_WINDOWS_FILE
530 s[0] = (char)(((wa & FILE_ATTRIBUTE_DIRECTORY) != 0 || isDir) ? 'D' : '.');
531 s[1] = (char)(((wa & FILE_ATTRIBUTE_READONLY ) != 0) ? 'R': '.');
532 s[2] = (char)(((wa & FILE_ATTRIBUTE_HIDDEN ) != 0) ? 'H': '.');
533 s[3] = (char)(((wa & FILE_ATTRIBUTE_SYSTEM ) != 0) ? 'S': '.');
534 s[4] = (char)(((wa & FILE_ATTRIBUTE_ARCHIVE ) != 0) ? 'A': '.');
535 s[5] = 0;
536 #else
537 s[0] = (char)(((wa & (1 << 4)) != 0 || isDir) ? 'D' : '.');
538 s[1] = 0;
539 #endif
540 }
541
542
543 // #define NUM_PARENTS_MAX 128
544
main(int numargs,char * args[])545 int Z7_CDECL main(int numargs, char *args[])
546 {
547 ISzAlloc allocImp;
548 ISzAlloc allocTempImp;
549
550 CFileInStream archiveStream;
551 CLookToRead2 lookStream;
552 CSzArEx db;
553 SRes res;
554 UInt16 *temp = NULL;
555 size_t tempSize = 0;
556 // UInt32 parents[NUM_PARENTS_MAX];
557
558 Print("\n7z Decoder " MY_VERSION_CPU " : " MY_COPYRIGHT_DATE "\n\n");
559
560 if (numargs == 1)
561 {
562 Print(
563 "Usage: 7zDec <command> <archive_name>\n\n"
564 "<Commands>\n"
565 " e: Extract files from archive (without using directory names)\n"
566 " l: List contents of archive\n"
567 " t: Test integrity of archive\n"
568 " x: eXtract files with full paths\n");
569 return 0;
570 }
571
572 if (numargs < 3)
573 {
574 PrintError("incorrect command");
575 return 1;
576 }
577
578 #if defined(_WIN32) && !defined(USE_WINDOWS_FILE) && !defined(UNDER_CE)
579 g_FileCodePage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
580 #endif
581
582
583 allocImp = g_Alloc;
584 allocTempImp = g_Alloc;
585 // allocTempImp = g_Alloc_temp;
586
587 {
588 WRes wres =
589 #ifdef UNDER_CE
590 InFile_OpenW(&archiveStream.file, L"\test.7z"); // change it
591 #else
592 InFile_Open(&archiveStream.file, args[2]);
593 #endif
594 if (wres != 0)
595 {
596 PrintError_WRes("cannot open input file", wres);
597 return 1;
598 }
599 }
600
601 FileInStream_CreateVTable(&archiveStream);
602 archiveStream.wres = 0;
603 LookToRead2_CreateVTable(&lookStream, False);
604 lookStream.buf = NULL;
605
606 res = SZ_OK;
607
608 {
609 lookStream.buf = (Byte *)ISzAlloc_Alloc(&allocImp, kInputBufSize);
610 if (!lookStream.buf)
611 res = SZ_ERROR_MEM;
612 else
613 {
614 lookStream.bufSize = kInputBufSize;
615 lookStream.realStream = &archiveStream.vt;
616 LookToRead2_INIT(&lookStream)
617 }
618 }
619
620 CrcGenerateTable();
621
622 SzArEx_Init(&db);
623
624 if (res == SZ_OK)
625 {
626 res = SzArEx_Open(&db, &lookStream.vt, &allocImp, &allocTempImp);
627 }
628
629 if (res == SZ_OK)
630 {
631 char *command = args[1];
632 int listCommand = 0, testCommand = 0, fullPaths = 0;
633
634 if (strcmp(command, "l") == 0) listCommand = 1;
635 else if (strcmp(command, "t") == 0) testCommand = 1;
636 else if (strcmp(command, "e") == 0) { }
637 else if (strcmp(command, "x") == 0) { fullPaths = 1; }
638 else
639 {
640 PrintError("incorrect command");
641 res = SZ_ERROR_FAIL;
642 }
643
644 if (res == SZ_OK)
645 {
646 UInt32 i;
647
648 /*
649 if you need cache, use these 3 variables.
650 if you use external function, you can make these variable as static.
651 */
652 UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */
653 Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */
654 size_t outBufferSize = 0; /* it can have any value before first call (if outBuffer = 0) */
655
656 for (i = 0; i < db.NumFiles; i++)
657 {
658 size_t offset = 0;
659 size_t outSizeProcessed = 0;
660 // const CSzFileItem *f = db.Files + i;
661 size_t len;
662 const BoolInt isDir = SzArEx_IsDir(&db, i);
663 if (listCommand == 0 && isDir && !fullPaths)
664 continue;
665 len = SzArEx_GetFileNameUtf16(&db, i, NULL);
666 // len = SzArEx_GetFullNameLen(&db, i);
667
668 if (len > tempSize)
669 {
670 SzFree(NULL, temp);
671 tempSize = len;
672 temp = (UInt16 *)SzAlloc(NULL, tempSize * sizeof(temp[0]));
673 if (!temp)
674 {
675 res = SZ_ERROR_MEM;
676 break;
677 }
678 }
679
680 SzArEx_GetFileNameUtf16(&db, i, temp);
681 /*
682 if (SzArEx_GetFullNameUtf16_Back(&db, i, temp + len) != temp)
683 {
684 res = SZ_ERROR_FAIL;
685 break;
686 }
687 */
688
689 if (listCommand)
690 {
691 char attr[8], s[32], t[32];
692 UInt64 fileSize;
693
694 GetAttribString(SzBitWithVals_Check(&db.Attribs, i) ? db.Attribs.Vals[i] : 0, isDir, attr);
695
696 fileSize = SzArEx_GetFileSize(&db, i);
697 UInt64ToStr(fileSize, s, 10);
698
699 if (SzBitWithVals_Check(&db.MTime, i))
700 ConvertFileTimeToString(&db.MTime.Vals[i], t);
701 else
702 {
703 size_t j;
704 for (j = 0; j < 19; j++)
705 t[j] = ' ';
706 t[j] = '\0';
707 }
708
709 Print(t);
710 Print(" ");
711 Print(attr);
712 Print(" ");
713 Print(s);
714 Print(" ");
715 res = PrintString(temp);
716 if (res != SZ_OK)
717 break;
718 if (isDir)
719 Print("/");
720 PrintLF();
721 continue;
722 }
723
724 Print(testCommand ?
725 "T ":
726 "- ");
727 res = PrintString(temp);
728 if (res != SZ_OK)
729 break;
730
731 if (isDir)
732 Print("/");
733 else
734 {
735 res = SzArEx_Extract(&db, &lookStream.vt, i,
736 &blockIndex, &outBuffer, &outBufferSize,
737 &offset, &outSizeProcessed,
738 &allocImp, &allocTempImp);
739 if (res != SZ_OK)
740 break;
741 }
742
743 if (!testCommand)
744 {
745 CSzFile outFile;
746 size_t processedSize;
747 size_t j;
748 UInt16 *name = (UInt16 *)temp;
749 const UInt16 *destPath = (const UInt16 *)name;
750
751 for (j = 0; name[j] != 0; j++)
752 if (name[j] == '/')
753 {
754 if (fullPaths)
755 {
756 name[j] = 0;
757 MyCreateDir(name);
758 name[j] = CHAR_PATH_SEPARATOR;
759 }
760 else
761 destPath = name + j + 1;
762 }
763
764 if (isDir)
765 {
766 MyCreateDir(destPath);
767 PrintLF();
768 continue;
769 }
770 else
771 {
772 const WRes wres = OutFile_OpenUtf16(&outFile, destPath);
773 if (wres != 0)
774 {
775 PrintError_WRes("cannot open output file", wres);
776 res = SZ_ERROR_FAIL;
777 break;
778 }
779 }
780
781 processedSize = outSizeProcessed;
782
783 {
784 const WRes wres = File_Write(&outFile, outBuffer + offset, &processedSize);
785 if (wres != 0 || processedSize != outSizeProcessed)
786 {
787 PrintError_WRes("cannot write output file", wres);
788 res = SZ_ERROR_FAIL;
789 break;
790 }
791 }
792
793 {
794 FILETIME mtime;
795 FILETIME *mtimePtr = NULL;
796
797 #ifdef USE_WINDOWS_FILE
798 FILETIME ctime;
799 FILETIME *ctimePtr = NULL;
800 #endif
801
802 if (SzBitWithVals_Check(&db.MTime, i))
803 {
804 const CNtfsFileTime *t = &db.MTime.Vals[i];
805 mtime.dwLowDateTime = (DWORD)(t->Low);
806 mtime.dwHighDateTime = (DWORD)(t->High);
807 mtimePtr = &mtime;
808 }
809
810 #ifdef USE_WINDOWS_FILE
811 if (SzBitWithVals_Check(&db.CTime, i))
812 {
813 const CNtfsFileTime *t = &db.CTime.Vals[i];
814 ctime.dwLowDateTime = (DWORD)(t->Low);
815 ctime.dwHighDateTime = (DWORD)(t->High);
816 ctimePtr = &ctime;
817 }
818
819 if (mtimePtr || ctimePtr)
820 SetFileTime(outFile.handle, ctimePtr, NULL, mtimePtr);
821 #endif
822
823 {
824 const WRes wres = File_Close(&outFile);
825 if (wres != 0)
826 {
827 PrintError_WRes("cannot close output file", wres);
828 res = SZ_ERROR_FAIL;
829 break;
830 }
831 }
832
833 #ifndef USE_WINDOWS_FILE
834 #ifdef _WIN32
835 mtimePtr = mtimePtr;
836 #else
837 if (mtimePtr)
838 Set_File_FILETIME(destPath, mtimePtr);
839 #endif
840 #endif
841 }
842
843 #ifdef USE_WINDOWS_FILE
844 if (SzBitWithVals_Check(&db.Attribs, i))
845 {
846 UInt32 attrib = db.Attribs.Vals[i];
847 /* p7zip stores posix attributes in high 16 bits and adds 0x8000 as marker.
848 We remove posix bits, if we detect posix mode field */
849 if ((attrib & 0xF0000000) != 0)
850 attrib &= 0x7FFF;
851 SetFileAttributesW((LPCWSTR)destPath, attrib);
852 }
853 #endif
854 }
855 PrintLF();
856 }
857 ISzAlloc_Free(&allocImp, outBuffer);
858 }
859 }
860
861 SzFree(NULL, temp);
862 SzArEx_Free(&db, &allocImp);
863 ISzAlloc_Free(&allocImp, lookStream.buf);
864
865 File_Close(&archiveStream.file);
866
867 if (res == SZ_OK)
868 {
869 Print("\nEverything is Ok\n");
870 return 0;
871 }
872
873 if (res == SZ_ERROR_UNSUPPORTED)
874 PrintError("decoder doesn't support this archive");
875 else if (res == SZ_ERROR_MEM)
876 PrintError("cannot allocate memory");
877 else if (res == SZ_ERROR_CRC)
878 PrintError("CRC error");
879 else if (res == SZ_ERROR_READ /* || archiveStream.Res != 0 */)
880 PrintError_WRes("Read Error", archiveStream.wres);
881 else
882 {
883 char s[32];
884 UInt64ToStr((unsigned)res, s, 0);
885 PrintError(s);
886 }
887
888 return 1;
889 }
890