diff options
Diffstat (limited to 'C/Util/7z/7zMain.c')
-rw-r--r-- | C/Util/7z/7zMain.c | 85 |
1 files changed, 43 insertions, 42 deletions
diff --git a/C/Util/7z/7zMain.c b/C/Util/7z/7zMain.c index 9d55509..547920a 100644 --- a/C/Util/7z/7zMain.c +++ b/C/Util/7z/7zMain.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* 7zMain.c - Test application for 7z Decoder | 1 | /* 7zMain.c - Test application for 7z Decoder |
2 | 2021-04-29 : Igor Pavlov : Public domain */ | 2 | 2023-04-04 : Igor Pavlov : Public domain */ |
3 | 3 | ||
4 | #include "Precomp.h" | 4 | #include "Precomp.h" |
5 | 5 | ||
@@ -32,10 +32,10 @@ | |||
32 | #endif | 32 | #endif |
33 | #endif | 33 | #endif |
34 | 34 | ||
35 | |||
36 | #define kInputBufSize ((size_t)1 << 18) | 35 | #define kInputBufSize ((size_t)1 << 18) |
37 | 36 | ||
38 | static const ISzAlloc g_Alloc = { SzAlloc, SzFree }; | 37 | static const ISzAlloc g_Alloc = { SzAlloc, SzFree }; |
38 | // static const ISzAlloc g_Alloc_temp = { SzAllocTemp, SzFreeTemp }; | ||
39 | 39 | ||
40 | 40 | ||
41 | static void Print(const char *s) | 41 | static void Print(const char *s) |
@@ -53,19 +53,19 @@ static int Buf_EnsureSize(CBuf *dest, size_t size) | |||
53 | } | 53 | } |
54 | 54 | ||
55 | #ifndef _WIN32 | 55 | #ifndef _WIN32 |
56 | #define _USE_UTF8 | 56 | #define MY_USE_UTF8 |
57 | #endif | 57 | #endif |
58 | 58 | ||
59 | /* #define _USE_UTF8 */ | 59 | /* #define MY_USE_UTF8 */ |
60 | 60 | ||
61 | #ifdef _USE_UTF8 | 61 | #ifdef MY_USE_UTF8 |
62 | 62 | ||
63 | #define _UTF8_START(n) (0x100 - (1 << (7 - (n)))) | 63 | #define MY_UTF8_START(n) (0x100 - (1 << (7 - (n)))) |
64 | 64 | ||
65 | #define _UTF8_RANGE(n) (((UInt32)1) << ((n) * 5 + 6)) | 65 | #define MY_UTF8_RANGE(n) (((UInt32)1) << ((n) * 5 + 6)) |
66 | 66 | ||
67 | #define _UTF8_HEAD(n, val) ((Byte)(_UTF8_START(n) + (val >> (6 * (n))))) | 67 | #define MY_UTF8_HEAD(n, val) ((Byte)(MY_UTF8_START(n) + (val >> (6 * (n))))) |
68 | #define _UTF8_CHAR(n, val) ((Byte)(0x80 + (((val) >> (6 * (n))) & 0x3F))) | 68 | #define MY_UTF8_CHAR(n, val) ((Byte)(0x80 + (((val) >> (6 * (n))) & 0x3F))) |
69 | 69 | ||
70 | static size_t 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 | { | 71 | { |
@@ -82,7 +82,7 @@ static size_t Utf16_To_Utf8_Calc(const UInt16 *src, const UInt16 *srcLim) | |||
82 | if (val < 0x80) | 82 | if (val < 0x80) |
83 | continue; | 83 | continue; |
84 | 84 | ||
85 | if (val < _UTF8_RANGE(1)) | 85 | if (val < MY_UTF8_RANGE(1)) |
86 | { | 86 | { |
87 | size++; | 87 | size++; |
88 | continue; | 88 | continue; |
@@ -90,7 +90,7 @@ static size_t Utf16_To_Utf8_Calc(const UInt16 *src, const UInt16 *srcLim) | |||
90 | 90 | ||
91 | if (val >= 0xD800 && val < 0xDC00 && src != srcLim) | 91 | if (val >= 0xD800 && val < 0xDC00 && src != srcLim) |
92 | { | 92 | { |
93 | UInt32 c2 = *src; | 93 | const UInt32 c2 = *src; |
94 | if (c2 >= 0xDC00 && c2 < 0xE000) | 94 | if (c2 >= 0xDC00 && c2 < 0xE000) |
95 | { | 95 | { |
96 | src++; | 96 | src++; |
@@ -119,33 +119,33 @@ static Byte *Utf16_To_Utf8(Byte *dest, const UInt16 *src, const UInt16 *srcLim) | |||
119 | continue; | 119 | continue; |
120 | } | 120 | } |
121 | 121 | ||
122 | if (val < _UTF8_RANGE(1)) | 122 | if (val < MY_UTF8_RANGE(1)) |
123 | { | 123 | { |
124 | dest[0] = _UTF8_HEAD(1, val); | 124 | dest[0] = MY_UTF8_HEAD(1, val); |
125 | dest[1] = _UTF8_CHAR(0, val); | 125 | dest[1] = MY_UTF8_CHAR(0, val); |
126 | dest += 2; | 126 | dest += 2; |
127 | continue; | 127 | continue; |
128 | } | 128 | } |
129 | 129 | ||
130 | if (val >= 0xD800 && val < 0xDC00 && src != srcLim) | 130 | if (val >= 0xD800 && val < 0xDC00 && src != srcLim) |
131 | { | 131 | { |
132 | UInt32 c2 = *src; | 132 | const UInt32 c2 = *src; |
133 | if (c2 >= 0xDC00 && c2 < 0xE000) | 133 | if (c2 >= 0xDC00 && c2 < 0xE000) |
134 | { | 134 | { |
135 | src++; | 135 | src++; |
136 | val = (((val - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000; | 136 | val = (((val - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000; |
137 | dest[0] = _UTF8_HEAD(3, val); | 137 | dest[0] = MY_UTF8_HEAD(3, val); |
138 | dest[1] = _UTF8_CHAR(2, val); | 138 | dest[1] = MY_UTF8_CHAR(2, val); |
139 | dest[2] = _UTF8_CHAR(1, val); | 139 | dest[2] = MY_UTF8_CHAR(1, val); |
140 | dest[3] = _UTF8_CHAR(0, val); | 140 | dest[3] = MY_UTF8_CHAR(0, val); |
141 | dest += 4; | 141 | dest += 4; |
142 | continue; | 142 | continue; |
143 | } | 143 | } |
144 | } | 144 | } |
145 | 145 | ||
146 | dest[0] = _UTF8_HEAD(2, val); | 146 | dest[0] = MY_UTF8_HEAD(2, val); |
147 | dest[1] = _UTF8_CHAR(1, val); | 147 | dest[1] = MY_UTF8_CHAR(1, val); |
148 | dest[2] = _UTF8_CHAR(0, val); | 148 | dest[2] = MY_UTF8_CHAR(0, val); |
149 | dest += 3; | 149 | dest += 3; |
150 | } | 150 | } |
151 | } | 151 | } |
@@ -163,7 +163,7 @@ static SRes Utf16_To_Utf8Buf(CBuf *dest, const UInt16 *src, size_t srcLen) | |||
163 | #endif | 163 | #endif |
164 | 164 | ||
165 | static SRes Utf16_To_Char(CBuf *buf, const UInt16 *s | 165 | static SRes Utf16_To_Char(CBuf *buf, const UInt16 *s |
166 | #ifndef _USE_UTF8 | 166 | #ifndef MY_USE_UTF8 |
167 | , UINT codePage | 167 | , UINT codePage |
168 | #endif | 168 | #endif |
169 | ) | 169 | ) |
@@ -171,7 +171,7 @@ static SRes Utf16_To_Char(CBuf *buf, const UInt16 *s | |||
171 | unsigned len = 0; | 171 | unsigned len = 0; |
172 | for (len = 0; s[len] != 0; len++) {} | 172 | for (len = 0; s[len] != 0; len++) {} |
173 | 173 | ||
174 | #ifndef _USE_UTF8 | 174 | #ifndef MY_USE_UTF8 |
175 | { | 175 | { |
176 | const unsigned size = len * 3 + 100; | 176 | const unsigned size = len * 3 + 100; |
177 | if (!Buf_EnsureSize(buf, size)) | 177 | if (!Buf_EnsureSize(buf, size)) |
@@ -216,7 +216,7 @@ static WRes MyCreateDir(const UInt16 *name) | |||
216 | CBuf buf; | 216 | CBuf buf; |
217 | WRes res; | 217 | WRes res; |
218 | Buf_Init(&buf); | 218 | Buf_Init(&buf); |
219 | RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM)); | 219 | RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM)) |
220 | 220 | ||
221 | res = | 221 | res = |
222 | #ifdef _WIN32 | 222 | #ifdef _WIN32 |
@@ -239,7 +239,7 @@ static WRes OutFile_OpenUtf16(CSzFile *p, const UInt16 *name) | |||
239 | CBuf buf; | 239 | CBuf buf; |
240 | WRes res; | 240 | WRes res; |
241 | Buf_Init(&buf); | 241 | Buf_Init(&buf); |
242 | RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM)); | 242 | RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM)) |
243 | res = OutFile_Open(p, (const char *)buf.data); | 243 | res = OutFile_Open(p, (const char *)buf.data); |
244 | Buf_Free(&buf, &g_Alloc); | 244 | Buf_Free(&buf, &g_Alloc); |
245 | return res; | 245 | return res; |
@@ -253,7 +253,7 @@ static SRes PrintString(const UInt16 *s) | |||
253 | SRes res; | 253 | SRes res; |
254 | Buf_Init(&buf); | 254 | Buf_Init(&buf); |
255 | res = Utf16_To_Char(&buf, s | 255 | res = Utf16_To_Char(&buf, s |
256 | #ifndef _USE_UTF8 | 256 | #ifndef MY_USE_UTF8 |
257 | , CP_OEMCP | 257 | , CP_OEMCP |
258 | #endif | 258 | #endif |
259 | ); | 259 | ); |
@@ -328,12 +328,12 @@ typedef struct _FILETIME | |||
328 | 328 | ||
329 | static LONG TIME_GetBias() | 329 | static LONG TIME_GetBias() |
330 | { | 330 | { |
331 | time_t utc = time(NULL); | 331 | const time_t utc = time(NULL); |
332 | struct tm *ptm = localtime(&utc); | 332 | struct tm *ptm = localtime(&utc); |
333 | int localdaylight = ptm->tm_isdst; /* daylight for local timezone */ | 333 | const int localdaylight = ptm->tm_isdst; /* daylight for local timezone */ |
334 | ptm = gmtime(&utc); | 334 | ptm = gmtime(&utc); |
335 | ptm->tm_isdst = localdaylight; /* use local daylight, not that of Greenwich */ | 335 | ptm->tm_isdst = localdaylight; /* use local daylight, not that of Greenwich */ |
336 | LONG bias = (int)(mktime(ptm)-utc); | 336 | const LONG bias = (int)(mktime(ptm) - utc); |
337 | return bias; | 337 | return bias; |
338 | } | 338 | } |
339 | 339 | ||
@@ -352,7 +352,7 @@ static BOOL WINAPI FileTimeToLocalFileTime(const FILETIME *fileTime, FILETIME *l | |||
352 | { | 352 | { |
353 | UInt64 v = GET_TIME_64(fileTime); | 353 | UInt64 v = GET_TIME_64(fileTime); |
354 | v = (UInt64)((Int64)v - (Int64)TIME_GetBias() * TICKS_PER_SEC); | 354 | v = (UInt64)((Int64)v - (Int64)TIME_GetBias() * TICKS_PER_SEC); |
355 | SET_FILETIME(localFileTime, v); | 355 | SET_FILETIME(localFileTime, v) |
356 | return TRUE; | 356 | return TRUE; |
357 | } | 357 | } |
358 | 358 | ||
@@ -364,7 +364,7 @@ static const UInt64 kUnixTimeOffset = | |||
364 | 364 | ||
365 | static Int64 Time_FileTimeToUnixTime64(const FILETIME *ft) | 365 | static Int64 Time_FileTimeToUnixTime64(const FILETIME *ft) |
366 | { | 366 | { |
367 | UInt64 winTime = GET_TIME_64(ft); | 367 | const UInt64 winTime = GET_TIME_64(ft); |
368 | return (Int64)(winTime / kNumTimeQuantumsInSecond) - (Int64)kUnixTimeOffset; | 368 | return (Int64)(winTime / kNumTimeQuantumsInSecond) - (Int64)kUnixTimeOffset; |
369 | } | 369 | } |
370 | 370 | ||
@@ -384,8 +384,8 @@ static void FILETIME_To_timespec(const FILETIME *ft, struct MY_ST_TIMESPEC *ts) | |||
384 | if (sec2 == sec) | 384 | if (sec2 == sec) |
385 | { | 385 | { |
386 | ts->tv_sec = sec2; | 386 | ts->tv_sec = sec2; |
387 | UInt64 winTime = GET_TIME_64(ft); | 387 | const UInt64 winTime = GET_TIME_64(ft); |
388 | ts->tv_nsec = (long)((winTime % 10000000) * 100);; | 388 | ts->tv_nsec = (long)((winTime % 10000000) * 100); |
389 | return; | 389 | return; |
390 | } | 390 | } |
391 | } | 391 | } |
@@ -407,7 +407,7 @@ static WRes Set_File_FILETIME(const UInt16 *name, const FILETIME *mTime) | |||
407 | CBuf buf; | 407 | CBuf buf; |
408 | int res; | 408 | int res; |
409 | Buf_Init(&buf); | 409 | Buf_Init(&buf); |
410 | RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM)); | 410 | RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM)) |
411 | FILETIME_To_timespec(NULL, ×[0]); | 411 | FILETIME_To_timespec(NULL, ×[0]); |
412 | FILETIME_To_timespec(mTime, ×[1]); | 412 | FILETIME_To_timespec(mTime, ×[1]); |
413 | res = utimensat(AT_FDCWD, (const char *)buf.data, times, flags); | 413 | res = utimensat(AT_FDCWD, (const char *)buf.data, times, flags); |
@@ -461,7 +461,7 @@ static void ConvertFileTimeToString(const CNtfsFileTime *nTime, char *s) | |||
461 | ms[1] = 29; | 461 | ms[1] = 29; |
462 | for (mon = 0;; mon++) | 462 | for (mon = 0;; mon++) |
463 | { | 463 | { |
464 | unsigned d = ms[mon]; | 464 | const unsigned d = ms[mon]; |
465 | if (v < d) | 465 | if (v < d) |
466 | break; | 466 | break; |
467 | v -= d; | 467 | v -= d; |
@@ -474,7 +474,7 @@ static void ConvertFileTimeToString(const CNtfsFileTime *nTime, char *s) | |||
474 | UIntToStr_2(s, sec); s[2] = 0; | 474 | UIntToStr_2(s, sec); s[2] = 0; |
475 | } | 475 | } |
476 | 476 | ||
477 | static void PrintLF() | 477 | static void PrintLF(void) |
478 | { | 478 | { |
479 | Print("\n"); | 479 | Print("\n"); |
480 | } | 480 | } |
@@ -541,7 +541,7 @@ static void GetAttribString(UInt32 wa, BoolInt isDir, char *s) | |||
541 | 541 | ||
542 | // #define NUM_PARENTS_MAX 128 | 542 | // #define NUM_PARENTS_MAX 128 |
543 | 543 | ||
544 | int MY_CDECL main(int numargs, char *args[]) | 544 | int Z7_CDECL main(int numargs, char *args[]) |
545 | { | 545 | { |
546 | ISzAlloc allocImp; | 546 | ISzAlloc allocImp; |
547 | ISzAlloc allocTempImp; | 547 | ISzAlloc allocTempImp; |
@@ -581,6 +581,7 @@ int MY_CDECL main(int numargs, char *args[]) | |||
581 | 581 | ||
582 | allocImp = g_Alloc; | 582 | allocImp = g_Alloc; |
583 | allocTempImp = g_Alloc; | 583 | allocTempImp = g_Alloc; |
584 | // allocTempImp = g_Alloc_temp; | ||
584 | 585 | ||
585 | { | 586 | { |
586 | WRes wres = | 587 | WRes wres = |
@@ -611,7 +612,7 @@ int MY_CDECL main(int numargs, char *args[]) | |||
611 | { | 612 | { |
612 | lookStream.bufSize = kInputBufSize; | 613 | lookStream.bufSize = kInputBufSize; |
613 | lookStream.realStream = &archiveStream.vt; | 614 | lookStream.realStream = &archiveStream.vt; |
614 | LookToRead2_Init(&lookStream); | 615 | LookToRead2_INIT(&lookStream) |
615 | } | 616 | } |
616 | } | 617 | } |
617 | 618 | ||
@@ -767,7 +768,7 @@ int MY_CDECL main(int numargs, char *args[]) | |||
767 | } | 768 | } |
768 | else | 769 | else |
769 | { | 770 | { |
770 | WRes wres = OutFile_OpenUtf16(&outFile, destPath); | 771 | const WRes wres = OutFile_OpenUtf16(&outFile, destPath); |
771 | if (wres != 0) | 772 | if (wres != 0) |
772 | { | 773 | { |
773 | PrintError_WRes("cannot open output file", wres); | 774 | PrintError_WRes("cannot open output file", wres); |
@@ -779,7 +780,7 @@ int MY_CDECL main(int numargs, char *args[]) | |||
779 | processedSize = outSizeProcessed; | 780 | processedSize = outSizeProcessed; |
780 | 781 | ||
781 | { | 782 | { |
782 | WRes wres = File_Write(&outFile, outBuffer + offset, &processedSize); | 783 | const WRes wres = File_Write(&outFile, outBuffer + offset, &processedSize); |
783 | if (wres != 0 || processedSize != outSizeProcessed) | 784 | if (wres != 0 || processedSize != outSizeProcessed) |
784 | { | 785 | { |
785 | PrintError_WRes("cannot write output file", wres); | 786 | PrintError_WRes("cannot write output file", wres); |
@@ -819,7 +820,7 @@ int MY_CDECL main(int numargs, char *args[]) | |||
819 | #endif | 820 | #endif |
820 | 821 | ||
821 | { | 822 | { |
822 | WRes wres = File_Close(&outFile); | 823 | const WRes wres = File_Close(&outFile); |
823 | if (wres != 0) | 824 | if (wres != 0) |
824 | { | 825 | { |
825 | PrintError_WRes("cannot close output file", wres); | 826 | PrintError_WRes("cannot close output file", wres); |