aboutsummaryrefslogtreecommitdiff
path: root/C
diff options
context:
space:
mode:
authorIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-06-21 00:00:00 +0000
committerIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-12-17 14:59:19 +0500
commit5b39dc76f1bc82f941d5c800ab9f34407a06b53a (patch)
treefe5e17420300b715021a76328444088d32047963 /C
parent93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (diff)
download7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.gz
7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.bz2
7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.zip
23.0123.01
Diffstat (limited to 'C')
-rw-r--r--C/7z.h12
-rw-r--r--C/7zAlloc.c69
-rw-r--r--C/7zAlloc.h6
-rw-r--r--C/7zArcIn.c405
-rw-r--r--C/7zBuf.h6
-rw-r--r--C/7zCrc.c130
-rw-r--r--C/7zCrc.h14
-rw-r--r--C/7zCrcOpt.c16
-rw-r--r--C/7zDec.c172
-rw-r--r--C/7zFile.c33
-rw-r--r--C/7zFile.h9
-rw-r--r--C/7zStream.c67
-rw-r--r--C/7zTypes.h262
-rw-r--r--C/7zVersion.h8
-rw-r--r--C/7zWindows.h101
-rw-r--r--C/7zip_gcc_c.mak75
-rw-r--r--C/Aes.c108
-rw-r--r--C/Aes.h36
-rw-r--r--C/AesOpt.c348
-rw-r--r--C/Alloc.c192
-rw-r--r--C/Alloc.h19
-rw-r--r--C/Bcj2.c319
-rw-r--r--C/Bcj2.h270
-rw-r--r--C/Bcj2Enc.c559
-rw-r--r--C/Blake2.h6
-rw-r--r--C/Blake2s.c34
-rw-r--r--C/Bra.c496
-rw-r--r--C/Bra.h115
-rw-r--r--C/Bra86.c221
-rw-r--r--C/BraIA64.c57
-rw-r--r--C/BwtSort.c13
-rw-r--r--C/BwtSort.h6
-rw-r--r--C/Compiler.h162
-rw-r--r--C/CpuArch.c795
-rw-r--r--C/CpuArch.h218
-rw-r--r--C/Delta.h6
-rw-r--r--C/DllSecur.c127
-rw-r--r--C/DllSecur.h6
-rw-r--r--C/HuffEnc.c10
-rw-r--r--C/HuffEnc.h6
-rw-r--r--C/LzFind.c519
-rw-r--r--C/LzFind.h53
-rw-r--r--C/LzFindMt.c70
-rw-r--r--C/LzFindMt.h10
-rw-r--r--C/LzFindOpt.c14
-rw-r--r--C/LzHash.h8
-rw-r--r--C/Lzma2Dec.c12
-rw-r--r--C/Lzma2Dec.h15
-rw-r--r--C/Lzma2DecMt.c155
-rw-r--r--C/Lzma2DecMt.h20
-rw-r--r--C/Lzma2Enc.c174
-rw-r--r--C/Lzma2Enc.h20
-rw-r--r--C/Lzma86.h6
-rw-r--r--C/Lzma86Dec.c7
-rw-r--r--C/Lzma86Enc.c7
-rw-r--r--C/LzmaDec.c190
-rw-r--r--C/LzmaDec.h17
-rw-r--r--C/LzmaEnc.c380
-rw-r--r--C/LzmaEnc.h23
-rw-r--r--C/LzmaLib.c8
-rw-r--r--C/LzmaLib.h12
-rw-r--r--C/MtCoder.c104
-rw-r--r--C/MtCoder.h50
-rw-r--r--C/MtDec.c101
-rw-r--r--C/MtDec.h34
-rw-r--r--C/Ppmd.h12
-rw-r--r--C/Ppmd7.c186
-rw-r--r--C/Ppmd7.h10
-rw-r--r--C/Ppmd7Dec.c57
-rw-r--r--C/Ppmd7Enc.c87
-rw-r--r--C/Ppmd7aDec.c54
-rw-r--r--C/Ppmd8.c248
-rw-r--r--C/Ppmd8.h10
-rw-r--r--C/Ppmd8Dec.c58
-rw-r--r--C/Ppmd8Enc.c86
-rw-r--r--C/Precomp.h6
-rw-r--r--C/RotateDefs.h28
-rw-r--r--C/Sha1.c165
-rw-r--r--C/Sha1.h14
-rw-r--r--C/Sha1Opt.c151
-rw-r--r--C/Sha256.c146
-rw-r--r--C/Sha256.h12
-rw-r--r--C/Sha256Opt.c129
-rw-r--r--C/Sort.h6
-rw-r--r--C/SwapBytes.c800
-rw-r--r--C/SwapBytes.h17
-rw-r--r--C/Threads.c86
-rw-r--r--C/Threads.h50
-rw-r--r--C/Util/7z/7z.dsp8
-rw-r--r--C/Util/7z/7zMain.c85
-rw-r--r--C/Util/7z/Precomp.h10
-rw-r--r--C/Util/7z/makefile2
-rw-r--r--C/Util/7z/makefile.gcc4
-rw-r--r--C/Util/7zipInstall/7zipInstall.c135
-rw-r--r--C/Util/7zipInstall/7zipInstall.dsp8
-rw-r--r--C/Util/7zipInstall/Precomp.h11
-rw-r--r--C/Util/7zipInstall/makefile14
-rw-r--r--C/Util/7zipUninstall/7zipUninstall.c128
-rw-r--r--C/Util/7zipUninstall/7zipUninstall.dsp8
-rw-r--r--C/Util/7zipUninstall/Precomp.h11
-rw-r--r--C/Util/7zipUninstall/makefile4
-rw-r--r--C/Util/Lzma/LzmaUtil.c145
-rw-r--r--C/Util/Lzma/LzmaUtil.dsp16
-rw-r--r--C/Util/Lzma/Precomp.h14
-rw-r--r--C/Util/LzmaLib/LzmaLib.dsp20
-rw-r--r--C/Util/LzmaLib/LzmaLibExports.c13
-rw-r--r--C/Util/LzmaLib/Precomp.c4
-rw-r--r--C/Util/LzmaLib/Precomp.h14
-rw-r--r--C/Util/LzmaLib/makefile22
-rw-r--r--C/Util/SfxSetup/Precomp.h10
-rw-r--r--C/Util/SfxSetup/SfxSetup.c46
-rw-r--r--C/Util/SfxSetup/makefile3
-rw-r--r--C/Util/SfxSetup/makefile_con4
-rw-r--r--C/Xz.c4
-rw-r--r--C/Xz.h56
-rw-r--r--C/XzCrc64.c32
-rw-r--r--C/XzCrc64.h12
-rw-r--r--C/XzCrc64Opt.c28
-rw-r--r--C/XzDec.c300
-rw-r--r--C/XzEnc.c270
-rw-r--r--C/XzEnc.h23
-rw-r--r--C/XzIn.c75
-rw-r--r--C/warn_clang.mak38
-rw-r--r--C/warn_clang_mac.mak38
124 files changed, 7333 insertions, 4223 deletions
diff --git a/C/7z.h b/C/7z.h
index 304f75f..9e27c01 100644
--- a/C/7z.h
+++ b/C/7z.h
@@ -1,8 +1,8 @@
1/* 7z.h -- 7z interface 1/* 7z.h -- 7z interface
22018-07-02 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_H 4#ifndef ZIP7_INC_7Z_H
5#define __7Z_H 5#define ZIP7_INC_7Z_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -98,7 +98,7 @@ typedef struct
98UInt64 SzAr_GetFolderUnpackSize(const CSzAr *p, UInt32 folderIndex); 98UInt64 SzAr_GetFolderUnpackSize(const CSzAr *p, UInt32 folderIndex);
99 99
100SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex, 100SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex,
101 ILookInStream *stream, UInt64 startPos, 101 ILookInStreamPtr stream, UInt64 startPos,
102 Byte *outBuffer, size_t outSize, 102 Byte *outBuffer, size_t outSize,
103 ISzAllocPtr allocMain); 103 ISzAllocPtr allocMain);
104 104
@@ -174,7 +174,7 @@ UInt16 *SzArEx_GetFullNameUtf16_Back(const CSzArEx *p, size_t fileIndex, UInt16
174 174
175SRes SzArEx_Extract( 175SRes SzArEx_Extract(
176 const CSzArEx *db, 176 const CSzArEx *db,
177 ILookInStream *inStream, 177 ILookInStreamPtr inStream,
178 UInt32 fileIndex, /* index of file */ 178 UInt32 fileIndex, /* index of file */
179 UInt32 *blockIndex, /* index of solid block */ 179 UInt32 *blockIndex, /* index of solid block */
180 Byte **outBuffer, /* pointer to pointer to output buffer (allocated with allocMain) */ 180 Byte **outBuffer, /* pointer to pointer to output buffer (allocated with allocMain) */
@@ -196,7 +196,7 @@ SZ_ERROR_INPUT_EOF
196SZ_ERROR_FAIL 196SZ_ERROR_FAIL
197*/ 197*/
198 198
199SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream, 199SRes SzArEx_Open(CSzArEx *p, ILookInStreamPtr inStream,
200 ISzAllocPtr allocMain, ISzAllocPtr allocTemp); 200 ISzAllocPtr allocMain, ISzAllocPtr allocTemp);
201 201
202EXTERN_C_END 202EXTERN_C_END
diff --git a/C/7zAlloc.c b/C/7zAlloc.c
index c924a52..2f0659a 100644
--- a/C/7zAlloc.c
+++ b/C/7zAlloc.c
@@ -1,5 +1,5 @@
1/* 7zAlloc.c -- Allocation functions 1/* 7zAlloc.c -- Allocation functions for 7z processing
22017-04-03 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -7,74 +7,83 @@
7 7
8#include "7zAlloc.h" 8#include "7zAlloc.h"
9 9
10/* #define _SZ_ALLOC_DEBUG */ 10/* #define SZ_ALLOC_DEBUG */
11/* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ 11/* use SZ_ALLOC_DEBUG to debug alloc/free operations */
12 12
13#ifdef _SZ_ALLOC_DEBUG 13#ifdef SZ_ALLOC_DEBUG
14 14
15/*
15#ifdef _WIN32 16#ifdef _WIN32
16#include <windows.h> 17#include "7zWindows.h"
17#endif 18#endif
19*/
18 20
19#include <stdio.h> 21#include <stdio.h>
20int g_allocCount = 0; 22static int g_allocCount = 0;
21int g_allocCountTemp = 0; 23static int g_allocCountTemp = 0;
22 24
25static void Print_Alloc(const char *s, size_t size, int *counter)
26{
27 const unsigned size2 = (unsigned)size;
28 fprintf(stderr, "\n%s count = %10d : %10u bytes; ", s, *counter, size2);
29 (*counter)++;
30}
31static void Print_Free(const char *s, int *counter)
32{
33 (*counter)--;
34 fprintf(stderr, "\n%s count = %10d", s, *counter);
35}
23#endif 36#endif
24 37
25void *SzAlloc(ISzAllocPtr p, size_t size) 38void *SzAlloc(ISzAllocPtr p, size_t size)
26{ 39{
27 UNUSED_VAR(p); 40 UNUSED_VAR(p)
28 if (size == 0) 41 if (size == 0)
29 return 0; 42 return 0;
30 #ifdef _SZ_ALLOC_DEBUG 43 #ifdef SZ_ALLOC_DEBUG
31 fprintf(stderr, "\nAlloc %10u bytes; count = %10d", (unsigned)size, g_allocCount); 44 Print_Alloc("Alloc", size, &g_allocCount);
32 g_allocCount++;
33 #endif 45 #endif
34 return malloc(size); 46 return malloc(size);
35} 47}
36 48
37void SzFree(ISzAllocPtr p, void *address) 49void SzFree(ISzAllocPtr p, void *address)
38{ 50{
39 UNUSED_VAR(p); 51 UNUSED_VAR(p)
40 #ifdef _SZ_ALLOC_DEBUG 52 #ifdef SZ_ALLOC_DEBUG
41 if (address != 0) 53 if (address)
42 { 54 Print_Free("Free ", &g_allocCount);
43 g_allocCount--;
44 fprintf(stderr, "\nFree; count = %10d", g_allocCount);
45 }
46 #endif 55 #endif
47 free(address); 56 free(address);
48} 57}
49 58
50void *SzAllocTemp(ISzAllocPtr p, size_t size) 59void *SzAllocTemp(ISzAllocPtr p, size_t size)
51{ 60{
52 UNUSED_VAR(p); 61 UNUSED_VAR(p)
53 if (size == 0) 62 if (size == 0)
54 return 0; 63 return 0;
55 #ifdef _SZ_ALLOC_DEBUG 64 #ifdef SZ_ALLOC_DEBUG
56 fprintf(stderr, "\nAlloc_temp %10u bytes; count = %10d", (unsigned)size, g_allocCountTemp); 65 Print_Alloc("Alloc_temp", size, &g_allocCountTemp);
57 g_allocCountTemp++; 66 /*
58 #ifdef _WIN32 67 #ifdef _WIN32
59 return HeapAlloc(GetProcessHeap(), 0, size); 68 return HeapAlloc(GetProcessHeap(), 0, size);
60 #endif 69 #endif
70 */
61 #endif 71 #endif
62 return malloc(size); 72 return malloc(size);
63} 73}
64 74
65void SzFreeTemp(ISzAllocPtr p, void *address) 75void SzFreeTemp(ISzAllocPtr p, void *address)
66{ 76{
67 UNUSED_VAR(p); 77 UNUSED_VAR(p)
68 #ifdef _SZ_ALLOC_DEBUG 78 #ifdef SZ_ALLOC_DEBUG
69 if (address != 0) 79 if (address)
70 { 80 Print_Free("Free_temp ", &g_allocCountTemp);
71 g_allocCountTemp--; 81 /*
72 fprintf(stderr, "\nFree_temp; count = %10d", g_allocCountTemp);
73 }
74 #ifdef _WIN32 82 #ifdef _WIN32
75 HeapFree(GetProcessHeap(), 0, address); 83 HeapFree(GetProcessHeap(), 0, address);
76 return; 84 return;
77 #endif 85 #endif
86 */
78 #endif 87 #endif
79 free(address); 88 free(address);
80} 89}
diff --git a/C/7zAlloc.h b/C/7zAlloc.h
index 44778f9..b2b8b0c 100644
--- a/C/7zAlloc.h
+++ b/C/7zAlloc.h
@@ -1,8 +1,8 @@
1/* 7zAlloc.h -- Allocation functions 1/* 7zAlloc.h -- Allocation functions
22017-04-03 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_ALLOC_H 4#ifndef ZIP7_INC_7Z_ALLOC_H
5#define __7Z_ALLOC_H 5#define ZIP7_INC_7Z_ALLOC_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
diff --git a/C/7zArcIn.c b/C/7zArcIn.c
index 0d9dec4..43fa7c2 100644
--- a/C/7zArcIn.c
+++ b/C/7zArcIn.c
@@ -1,5 +1,5 @@
1/* 7zArcIn.c -- 7z Input functions 1/* 7zArcIn.c -- 7z Input functions
22021-02-09 : Igor Pavlov : Public domain */ 22023-05-11 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -10,10 +10,11 @@
10#include "7zCrc.h" 10#include "7zCrc.h"
11#include "CpuArch.h" 11#include "CpuArch.h"
12 12
13#define MY_ALLOC(T, p, size, alloc) { \ 13#define MY_ALLOC(T, p, size, alloc) \
14 if ((p = (T *)ISzAlloc_Alloc(alloc, (size) * sizeof(T))) == NULL) return SZ_ERROR_MEM; } 14 { if ((p = (T *)ISzAlloc_Alloc(alloc, (size) * sizeof(T))) == NULL) return SZ_ERROR_MEM; }
15 15
16#define MY_ALLOC_ZE(T, p, size, alloc) { if ((size) == 0) p = NULL; else MY_ALLOC(T, p, size, alloc) } 16#define MY_ALLOC_ZE(T, p, size, alloc) \
17 { if ((size) == 0) p = NULL; else MY_ALLOC(T, p, size, alloc) }
17 18
18#define MY_ALLOC_AND_CPY(to, size, from, alloc) \ 19#define MY_ALLOC_AND_CPY(to, size, from, alloc) \
19 { MY_ALLOC(Byte, to, size, alloc); memcpy(to, from, size); } 20 { MY_ALLOC(Byte, to, size, alloc); memcpy(to, from, size); }
@@ -58,7 +59,7 @@ enum EIdEnum
58 59
59const Byte k7zSignature[k7zSignatureSize] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C}; 60const Byte k7zSignature[k7zSignatureSize] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
60 61
61#define SzBitUi32s_Init(p) { (p)->Defs = NULL; (p)->Vals = NULL; } 62#define SzBitUi32s_INIT(p) { (p)->Defs = NULL; (p)->Vals = NULL; }
62 63
63static SRes SzBitUi32s_Alloc(CSzBitUi32s *p, size_t num, ISzAllocPtr alloc) 64static SRes SzBitUi32s_Alloc(CSzBitUi32s *p, size_t num, ISzAllocPtr alloc)
64{ 65{
@@ -69,8 +70,8 @@ static SRes SzBitUi32s_Alloc(CSzBitUi32s *p, size_t num, ISzAllocPtr alloc)
69 } 70 }
70 else 71 else
71 { 72 {
72 MY_ALLOC(Byte, p->Defs, (num + 7) >> 3, alloc); 73 MY_ALLOC(Byte, p->Defs, (num + 7) >> 3, alloc)
73 MY_ALLOC(UInt32, p->Vals, num, alloc); 74 MY_ALLOC(UInt32, p->Vals, num, alloc)
74 } 75 }
75 return SZ_OK; 76 return SZ_OK;
76} 77}
@@ -81,7 +82,7 @@ static void SzBitUi32s_Free(CSzBitUi32s *p, ISzAllocPtr alloc)
81 ISzAlloc_Free(alloc, p->Vals); p->Vals = NULL; 82 ISzAlloc_Free(alloc, p->Vals); p->Vals = NULL;
82} 83}
83 84
84#define SzBitUi64s_Init(p) { (p)->Defs = NULL; (p)->Vals = NULL; } 85#define SzBitUi64s_INIT(p) { (p)->Defs = NULL; (p)->Vals = NULL; }
85 86
86static void SzBitUi64s_Free(CSzBitUi64s *p, ISzAllocPtr alloc) 87static void SzBitUi64s_Free(CSzBitUi64s *p, ISzAllocPtr alloc)
87{ 88{
@@ -96,7 +97,7 @@ static void SzAr_Init(CSzAr *p)
96 p->NumFolders = 0; 97 p->NumFolders = 0;
97 98
98 p->PackPositions = NULL; 99 p->PackPositions = NULL;
99 SzBitUi32s_Init(&p->FolderCRCs); 100 SzBitUi32s_INIT(&p->FolderCRCs)
100 101
101 p->FoCodersOffsets = NULL; 102 p->FoCodersOffsets = NULL;
102 p->FoStartPackStreamIndex = NULL; 103 p->FoStartPackStreamIndex = NULL;
@@ -142,11 +143,11 @@ void SzArEx_Init(CSzArEx *p)
142 p->FileNameOffsets = NULL; 143 p->FileNameOffsets = NULL;
143 p->FileNames = NULL; 144 p->FileNames = NULL;
144 145
145 SzBitUi32s_Init(&p->CRCs); 146 SzBitUi32s_INIT(&p->CRCs)
146 SzBitUi32s_Init(&p->Attribs); 147 SzBitUi32s_INIT(&p->Attribs)
147 // SzBitUi32s_Init(&p->Parents); 148 // SzBitUi32s_INIT(&p->Parents)
148 SzBitUi64s_Init(&p->MTime); 149 SzBitUi64s_INIT(&p->MTime)
149 SzBitUi64s_Init(&p->CTime); 150 SzBitUi64s_INIT(&p->CTime)
150} 151}
151 152
152void SzArEx_Free(CSzArEx *p, ISzAllocPtr alloc) 153void SzArEx_Free(CSzArEx *p, ISzAllocPtr alloc)
@@ -180,11 +181,20 @@ static int TestSignatureCandidate(const Byte *testBytes)
180 return 1; 181 return 1;
181} 182}
182 183
183#define SzData_Clear(p) { (p)->Data = NULL; (p)->Size = 0; } 184#define SzData_CLEAR(p) { (p)->Data = NULL; (p)->Size = 0; }
185
186#define SZ_READ_BYTE_SD_NOCHECK(_sd_, dest) \
187 (_sd_)->Size--; dest = *(_sd_)->Data++;
188
189#define SZ_READ_BYTE_SD(_sd_, dest) \
190 if ((_sd_)->Size == 0) return SZ_ERROR_ARCHIVE; \
191 SZ_READ_BYTE_SD_NOCHECK(_sd_, dest)
184 192
185#define SZ_READ_BYTE_SD(_sd_, dest) if ((_sd_)->Size == 0) return SZ_ERROR_ARCHIVE; (_sd_)->Size--; dest = *(_sd_)->Data++;
186#define SZ_READ_BYTE(dest) SZ_READ_BYTE_SD(sd, dest) 193#define SZ_READ_BYTE(dest) SZ_READ_BYTE_SD(sd, dest)
187#define SZ_READ_BYTE_2(dest) if (sd.Size == 0) return SZ_ERROR_ARCHIVE; sd.Size--; dest = *sd.Data++; 194
195#define SZ_READ_BYTE_2(dest) \
196 if (sd.Size == 0) return SZ_ERROR_ARCHIVE; \
197 sd.Size--; dest = *sd.Data++;
188 198
189#define SKIP_DATA(sd, size) { sd->Size -= (size_t)(size); sd->Data += (size_t)(size); } 199#define SKIP_DATA(sd, size) { sd->Size -= (size_t)(size); sd->Data += (size_t)(size); }
190#define SKIP_DATA2(sd, size) { sd.Size -= (size_t)(size); sd.Data += (size_t)(size); } 200#define SKIP_DATA2(sd, size) { sd.Size -= (size_t)(size); sd.Data += (size_t)(size); }
@@ -192,25 +202,25 @@ static int TestSignatureCandidate(const Byte *testBytes)
192#define SZ_READ_32(dest) if (sd.Size < 4) return SZ_ERROR_ARCHIVE; \ 202#define SZ_READ_32(dest) if (sd.Size < 4) return SZ_ERROR_ARCHIVE; \
193 dest = GetUi32(sd.Data); SKIP_DATA2(sd, 4); 203 dest = GetUi32(sd.Data); SKIP_DATA2(sd, 4);
194 204
195static MY_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value) 205static Z7_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value)
196{ 206{
197 Byte firstByte, mask; 207 Byte firstByte, mask;
198 unsigned i; 208 unsigned i;
199 UInt32 v; 209 UInt32 v;
200 210
201 SZ_READ_BYTE(firstByte); 211 SZ_READ_BYTE(firstByte)
202 if ((firstByte & 0x80) == 0) 212 if ((firstByte & 0x80) == 0)
203 { 213 {
204 *value = firstByte; 214 *value = firstByte;
205 return SZ_OK; 215 return SZ_OK;
206 } 216 }
207 SZ_READ_BYTE(v); 217 SZ_READ_BYTE(v)
208 if ((firstByte & 0x40) == 0) 218 if ((firstByte & 0x40) == 0)
209 { 219 {
210 *value = (((UInt32)firstByte & 0x3F) << 8) | v; 220 *value = (((UInt32)firstByte & 0x3F) << 8) | v;
211 return SZ_OK; 221 return SZ_OK;
212 } 222 }
213 SZ_READ_BYTE(mask); 223 SZ_READ_BYTE(mask)
214 *value = v | ((UInt32)mask << 8); 224 *value = v | ((UInt32)mask << 8);
215 mask = 0x20; 225 mask = 0x20;
216 for (i = 2; i < 8; i++) 226 for (i = 2; i < 8; i++)
@@ -218,11 +228,11 @@ static MY_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value)
218 Byte b; 228 Byte b;
219 if ((firstByte & mask) == 0) 229 if ((firstByte & mask) == 0)
220 { 230 {
221 UInt64 highPart = (unsigned)firstByte & (unsigned)(mask - 1); 231 const UInt64 highPart = (unsigned)firstByte & (unsigned)(mask - 1);
222 *value |= (highPart << (8 * i)); 232 *value |= (highPart << (8 * i));
223 return SZ_OK; 233 return SZ_OK;
224 } 234 }
225 SZ_READ_BYTE(b); 235 SZ_READ_BYTE(b)
226 *value |= ((UInt64)b << (8 * i)); 236 *value |= ((UInt64)b << (8 * i));
227 mask >>= 1; 237 mask >>= 1;
228 } 238 }
@@ -230,7 +240,7 @@ static MY_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value)
230} 240}
231 241
232 242
233static MY_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value) 243static Z7_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value)
234{ 244{
235 Byte firstByte; 245 Byte firstByte;
236 UInt64 value64; 246 UInt64 value64;
@@ -244,7 +254,7 @@ static MY_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value)
244 sd->Size--; 254 sd->Size--;
245 return SZ_OK; 255 return SZ_OK;
246 } 256 }
247 RINOK(ReadNumber(sd, &value64)); 257 RINOK(ReadNumber(sd, &value64))
248 if (value64 >= (UInt32)0x80000000 - 1) 258 if (value64 >= (UInt32)0x80000000 - 1)
249 return SZ_ERROR_UNSUPPORTED; 259 return SZ_ERROR_UNSUPPORTED;
250 if (value64 >= ((UInt64)(1) << ((sizeof(size_t) - 1) * 8 + 4))) 260 if (value64 >= ((UInt64)(1) << ((sizeof(size_t) - 1) * 8 + 4)))
@@ -258,10 +268,10 @@ static MY_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value)
258static SRes SkipData(CSzData *sd) 268static SRes SkipData(CSzData *sd)
259{ 269{
260 UInt64 size; 270 UInt64 size;
261 RINOK(ReadNumber(sd, &size)); 271 RINOK(ReadNumber(sd, &size))
262 if (size > sd->Size) 272 if (size > sd->Size)
263 return SZ_ERROR_ARCHIVE; 273 return SZ_ERROR_ARCHIVE;
264 SKIP_DATA(sd, size); 274 SKIP_DATA(sd, size)
265 return SZ_OK; 275 return SZ_OK;
266} 276}
267 277
@@ -270,22 +280,22 @@ static SRes WaitId(CSzData *sd, UInt32 id)
270 for (;;) 280 for (;;)
271 { 281 {
272 UInt64 type; 282 UInt64 type;
273 RINOK(ReadID(sd, &type)); 283 RINOK(ReadID(sd, &type))
274 if (type == id) 284 if (type == id)
275 return SZ_OK; 285 return SZ_OK;
276 if (type == k7zIdEnd) 286 if (type == k7zIdEnd)
277 return SZ_ERROR_ARCHIVE; 287 return SZ_ERROR_ARCHIVE;
278 RINOK(SkipData(sd)); 288 RINOK(SkipData(sd))
279 } 289 }
280} 290}
281 291
282static SRes RememberBitVector(CSzData *sd, UInt32 numItems, const Byte **v) 292static SRes RememberBitVector(CSzData *sd, UInt32 numItems, const Byte **v)
283{ 293{
284 UInt32 numBytes = (numItems + 7) >> 3; 294 const UInt32 numBytes = (numItems + 7) >> 3;
285 if (numBytes > sd->Size) 295 if (numBytes > sd->Size)
286 return SZ_ERROR_ARCHIVE; 296 return SZ_ERROR_ARCHIVE;
287 *v = sd->Data; 297 *v = sd->Data;
288 SKIP_DATA(sd, numBytes); 298 SKIP_DATA(sd, numBytes)
289 return SZ_OK; 299 return SZ_OK;
290} 300}
291 301
@@ -307,48 +317,48 @@ static UInt32 CountDefinedBits(const Byte *bits, UInt32 numItems)
307 return sum; 317 return sum;
308} 318}
309 319
310static MY_NO_INLINE SRes ReadBitVector(CSzData *sd, UInt32 numItems, Byte **v, ISzAllocPtr alloc) 320static Z7_NO_INLINE SRes ReadBitVector(CSzData *sd, UInt32 numItems, Byte **v, ISzAllocPtr alloc)
311{ 321{
312 Byte allAreDefined; 322 Byte allAreDefined;
313 Byte *v2; 323 Byte *v2;
314 UInt32 numBytes = (numItems + 7) >> 3; 324 const UInt32 numBytes = (numItems + 7) >> 3;
315 *v = NULL; 325 *v = NULL;
316 SZ_READ_BYTE(allAreDefined); 326 SZ_READ_BYTE(allAreDefined)
317 if (numBytes == 0) 327 if (numBytes == 0)
318 return SZ_OK; 328 return SZ_OK;
319 if (allAreDefined == 0) 329 if (allAreDefined == 0)
320 { 330 {
321 if (numBytes > sd->Size) 331 if (numBytes > sd->Size)
322 return SZ_ERROR_ARCHIVE; 332 return SZ_ERROR_ARCHIVE;
323 MY_ALLOC_AND_CPY(*v, numBytes, sd->Data, alloc); 333 MY_ALLOC_AND_CPY(*v, numBytes, sd->Data, alloc)
324 SKIP_DATA(sd, numBytes); 334 SKIP_DATA(sd, numBytes)
325 return SZ_OK; 335 return SZ_OK;
326 } 336 }
327 MY_ALLOC(Byte, *v, numBytes, alloc); 337 MY_ALLOC(Byte, *v, numBytes, alloc)
328 v2 = *v; 338 v2 = *v;
329 memset(v2, 0xFF, (size_t)numBytes); 339 memset(v2, 0xFF, (size_t)numBytes);
330 { 340 {
331 unsigned numBits = (unsigned)numItems & 7; 341 const unsigned numBits = (unsigned)numItems & 7;
332 if (numBits != 0) 342 if (numBits != 0)
333 v2[(size_t)numBytes - 1] = (Byte)((((UInt32)1 << numBits) - 1) << (8 - numBits)); 343 v2[(size_t)numBytes - 1] = (Byte)((((UInt32)1 << numBits) - 1) << (8 - numBits));
334 } 344 }
335 return SZ_OK; 345 return SZ_OK;
336} 346}
337 347
338static MY_NO_INLINE SRes ReadUi32s(CSzData *sd2, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc) 348static Z7_NO_INLINE SRes ReadUi32s(CSzData *sd2, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc)
339{ 349{
340 UInt32 i; 350 UInt32 i;
341 CSzData sd; 351 CSzData sd;
342 UInt32 *vals; 352 UInt32 *vals;
343 const Byte *defs; 353 const Byte *defs;
344 MY_ALLOC_ZE(UInt32, crcs->Vals, numItems, alloc); 354 MY_ALLOC_ZE(UInt32, crcs->Vals, numItems, alloc)
345 sd = *sd2; 355 sd = *sd2;
346 defs = crcs->Defs; 356 defs = crcs->Defs;
347 vals = crcs->Vals; 357 vals = crcs->Vals;
348 for (i = 0; i < numItems; i++) 358 for (i = 0; i < numItems; i++)
349 if (SzBitArray_Check(defs, i)) 359 if (SzBitArray_Check(defs, i))
350 { 360 {
351 SZ_READ_32(vals[i]); 361 SZ_READ_32(vals[i])
352 } 362 }
353 else 363 else
354 vals[i] = 0; 364 vals[i] = 0;
@@ -359,7 +369,7 @@ static MY_NO_INLINE SRes ReadUi32s(CSzData *sd2, UInt32 numItems, CSzBitUi32s *c
359static SRes ReadBitUi32s(CSzData *sd, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc) 369static SRes ReadBitUi32s(CSzData *sd, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc)
360{ 370{
361 SzBitUi32s_Free(crcs, alloc); 371 SzBitUi32s_Free(crcs, alloc);
362 RINOK(ReadBitVector(sd, numItems, &crcs->Defs, alloc)); 372 RINOK(ReadBitVector(sd, numItems, &crcs->Defs, alloc))
363 return ReadUi32s(sd, numItems, crcs, alloc); 373 return ReadUi32s(sd, numItems, crcs, alloc);
364} 374}
365 375
@@ -367,36 +377,36 @@ static SRes SkipBitUi32s(CSzData *sd, UInt32 numItems)
367{ 377{
368 Byte allAreDefined; 378 Byte allAreDefined;
369 UInt32 numDefined = numItems; 379 UInt32 numDefined = numItems;
370 SZ_READ_BYTE(allAreDefined); 380 SZ_READ_BYTE(allAreDefined)
371 if (!allAreDefined) 381 if (!allAreDefined)
372 { 382 {
373 size_t numBytes = (numItems + 7) >> 3; 383 const size_t numBytes = (numItems + 7) >> 3;
374 if (numBytes > sd->Size) 384 if (numBytes > sd->Size)
375 return SZ_ERROR_ARCHIVE; 385 return SZ_ERROR_ARCHIVE;
376 numDefined = CountDefinedBits(sd->Data, numItems); 386 numDefined = CountDefinedBits(sd->Data, numItems);
377 SKIP_DATA(sd, numBytes); 387 SKIP_DATA(sd, numBytes)
378 } 388 }
379 if (numDefined > (sd->Size >> 2)) 389 if (numDefined > (sd->Size >> 2))
380 return SZ_ERROR_ARCHIVE; 390 return SZ_ERROR_ARCHIVE;
381 SKIP_DATA(sd, (size_t)numDefined * 4); 391 SKIP_DATA(sd, (size_t)numDefined * 4)
382 return SZ_OK; 392 return SZ_OK;
383} 393}
384 394
385static SRes ReadPackInfo(CSzAr *p, CSzData *sd, ISzAllocPtr alloc) 395static SRes ReadPackInfo(CSzAr *p, CSzData *sd, ISzAllocPtr alloc)
386{ 396{
387 RINOK(SzReadNumber32(sd, &p->NumPackStreams)); 397 RINOK(SzReadNumber32(sd, &p->NumPackStreams))
388 398
389 RINOK(WaitId(sd, k7zIdSize)); 399 RINOK(WaitId(sd, k7zIdSize))
390 MY_ALLOC(UInt64, p->PackPositions, (size_t)p->NumPackStreams + 1, alloc); 400 MY_ALLOC(UInt64, p->PackPositions, (size_t)p->NumPackStreams + 1, alloc)
391 { 401 {
392 UInt64 sum = 0; 402 UInt64 sum = 0;
393 UInt32 i; 403 UInt32 i;
394 UInt32 numPackStreams = p->NumPackStreams; 404 const UInt32 numPackStreams = p->NumPackStreams;
395 for (i = 0; i < numPackStreams; i++) 405 for (i = 0; i < numPackStreams; i++)
396 { 406 {
397 UInt64 packSize; 407 UInt64 packSize;
398 p->PackPositions[i] = sum; 408 p->PackPositions[i] = sum;
399 RINOK(ReadNumber(sd, &packSize)); 409 RINOK(ReadNumber(sd, &packSize))
400 sum += packSize; 410 sum += packSize;
401 if (sum < packSize) 411 if (sum < packSize)
402 return SZ_ERROR_ARCHIVE; 412 return SZ_ERROR_ARCHIVE;
@@ -407,16 +417,16 @@ static SRes ReadPackInfo(CSzAr *p, CSzData *sd, ISzAllocPtr alloc)
407 for (;;) 417 for (;;)
408 { 418 {
409 UInt64 type; 419 UInt64 type;
410 RINOK(ReadID(sd, &type)); 420 RINOK(ReadID(sd, &type))
411 if (type == k7zIdEnd) 421 if (type == k7zIdEnd)
412 return SZ_OK; 422 return SZ_OK;
413 if (type == k7zIdCRC) 423 if (type == k7zIdCRC)
414 { 424 {
415 /* CRC of packed streams is unused now */ 425 /* CRC of packed streams is unused now */
416 RINOK(SkipBitUi32s(sd, p->NumPackStreams)); 426 RINOK(SkipBitUi32s(sd, p->NumPackStreams))
417 continue; 427 continue;
418 } 428 }
419 RINOK(SkipData(sd)); 429 RINOK(SkipData(sd))
420 } 430 }
421} 431}
422 432
@@ -442,7 +452,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd)
442 f->NumPackStreams = 0; 452 f->NumPackStreams = 0;
443 f->UnpackStream = 0; 453 f->UnpackStream = 0;
444 454
445 RINOK(SzReadNumber32(sd, &numCoders)); 455 RINOK(SzReadNumber32(sd, &numCoders))
446 if (numCoders == 0 || numCoders > SZ_NUM_CODERS_IN_FOLDER_MAX) 456 if (numCoders == 0 || numCoders > SZ_NUM_CODERS_IN_FOLDER_MAX)
447 return SZ_ERROR_UNSUPPORTED; 457 return SZ_ERROR_UNSUPPORTED;
448 458
@@ -453,7 +463,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd)
453 unsigned idSize, j; 463 unsigned idSize, j;
454 UInt64 id; 464 UInt64 id;
455 465
456 SZ_READ_BYTE(mainByte); 466 SZ_READ_BYTE(mainByte)
457 if ((mainByte & 0xC0) != 0) 467 if ((mainByte & 0xC0) != 0)
458 return SZ_ERROR_UNSUPPORTED; 468 return SZ_ERROR_UNSUPPORTED;
459 469
@@ -481,12 +491,12 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd)
481 { 491 {
482 UInt32 numStreams; 492 UInt32 numStreams;
483 493
484 RINOK(SzReadNumber32(sd, &numStreams)); 494 RINOK(SzReadNumber32(sd, &numStreams))
485 if (numStreams > k_NumCodersStreams_in_Folder_MAX) 495 if (numStreams > k_NumCodersStreams_in_Folder_MAX)
486 return SZ_ERROR_UNSUPPORTED; 496 return SZ_ERROR_UNSUPPORTED;
487 coder->NumStreams = (Byte)numStreams; 497 coder->NumStreams = (Byte)numStreams;
488 498
489 RINOK(SzReadNumber32(sd, &numStreams)); 499 RINOK(SzReadNumber32(sd, &numStreams))
490 if (numStreams != 1) 500 if (numStreams != 1)
491 return SZ_ERROR_UNSUPPORTED; 501 return SZ_ERROR_UNSUPPORTED;
492 } 502 }
@@ -499,7 +509,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd)
499 if ((mainByte & 0x20) != 0) 509 if ((mainByte & 0x20) != 0)
500 { 510 {
501 UInt32 propsSize = 0; 511 UInt32 propsSize = 0;
502 RINOK(SzReadNumber32(sd, &propsSize)); 512 RINOK(SzReadNumber32(sd, &propsSize))
503 if (propsSize > sd->Size) 513 if (propsSize > sd->Size)
504 return SZ_ERROR_ARCHIVE; 514 return SZ_ERROR_ARCHIVE;
505 if (propsSize >= 0x80) 515 if (propsSize >= 0x80)
@@ -549,12 +559,12 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd)
549 { 559 {
550 CSzBond *bp = f->Bonds + i; 560 CSzBond *bp = f->Bonds + i;
551 561
552 RINOK(SzReadNumber32(sd, &bp->InIndex)); 562 RINOK(SzReadNumber32(sd, &bp->InIndex))
553 if (bp->InIndex >= numInStreams || streamUsed[bp->InIndex]) 563 if (bp->InIndex >= numInStreams || streamUsed[bp->InIndex])
554 return SZ_ERROR_ARCHIVE; 564 return SZ_ERROR_ARCHIVE;
555 streamUsed[bp->InIndex] = True; 565 streamUsed[bp->InIndex] = True;
556 566
557 RINOK(SzReadNumber32(sd, &bp->OutIndex)); 567 RINOK(SzReadNumber32(sd, &bp->OutIndex))
558 if (bp->OutIndex >= numCoders || coderUsed[bp->OutIndex]) 568 if (bp->OutIndex >= numCoders || coderUsed[bp->OutIndex])
559 return SZ_ERROR_ARCHIVE; 569 return SZ_ERROR_ARCHIVE;
560 coderUsed[bp->OutIndex] = True; 570 coderUsed[bp->OutIndex] = True;
@@ -584,7 +594,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd)
584 for (i = 0; i < numPackStreams; i++) 594 for (i = 0; i < numPackStreams; i++)
585 { 595 {
586 UInt32 index; 596 UInt32 index;
587 RINOK(SzReadNumber32(sd, &index)); 597 RINOK(SzReadNumber32(sd, &index))
588 if (index >= numInStreams || streamUsed[index]) 598 if (index >= numInStreams || streamUsed[index])
589 return SZ_ERROR_ARCHIVE; 599 return SZ_ERROR_ARCHIVE;
590 streamUsed[index] = True; 600 streamUsed[index] = True;
@@ -598,7 +608,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd)
598} 608}
599 609
600 610
601static MY_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num) 611static Z7_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num)
602{ 612{
603 CSzData sd; 613 CSzData sd;
604 sd = *sd2; 614 sd = *sd2;
@@ -606,7 +616,7 @@ static MY_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num)
606 { 616 {
607 Byte firstByte, mask; 617 Byte firstByte, mask;
608 unsigned i; 618 unsigned i;
609 SZ_READ_BYTE_2(firstByte); 619 SZ_READ_BYTE_2(firstByte)
610 if ((firstByte & 0x80) == 0) 620 if ((firstByte & 0x80) == 0)
611 continue; 621 continue;
612 if ((firstByte & 0x40) == 0) 622 if ((firstByte & 0x40) == 0)
@@ -622,7 +632,7 @@ static MY_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num)
622 mask >>= 1; 632 mask >>= 1;
623 if (i > sd.Size) 633 if (i > sd.Size)
624 return SZ_ERROR_ARCHIVE; 634 return SZ_ERROR_ARCHIVE;
625 SKIP_DATA2(sd, i); 635 SKIP_DATA2(sd, i)
626 } 636 }
627 *sd2 = sd; 637 *sd2 = sd;
628 return SZ_OK; 638 return SZ_OK;
@@ -645,30 +655,30 @@ static SRes ReadUnpackInfo(CSzAr *p,
645 const Byte *startBufPtr; 655 const Byte *startBufPtr;
646 Byte external; 656 Byte external;
647 657
648 RINOK(WaitId(sd2, k7zIdFolder)); 658 RINOK(WaitId(sd2, k7zIdFolder))
649 659
650 RINOK(SzReadNumber32(sd2, &numFolders)); 660 RINOK(SzReadNumber32(sd2, &numFolders))
651 if (numFolders > numFoldersMax) 661 if (numFolders > numFoldersMax)
652 return SZ_ERROR_UNSUPPORTED; 662 return SZ_ERROR_UNSUPPORTED;
653 p->NumFolders = numFolders; 663 p->NumFolders = numFolders;
654 664
655 SZ_READ_BYTE_SD(sd2, external); 665 SZ_READ_BYTE_SD(sd2, external)
656 if (external == 0) 666 if (external == 0)
657 sd = *sd2; 667 sd = *sd2;
658 else 668 else
659 { 669 {
660 UInt32 index; 670 UInt32 index;
661 RINOK(SzReadNumber32(sd2, &index)); 671 RINOK(SzReadNumber32(sd2, &index))
662 if (index >= numTempBufs) 672 if (index >= numTempBufs)
663 return SZ_ERROR_ARCHIVE; 673 return SZ_ERROR_ARCHIVE;
664 sd.Data = tempBufs[index].data; 674 sd.Data = tempBufs[index].data;
665 sd.Size = tempBufs[index].size; 675 sd.Size = tempBufs[index].size;
666 } 676 }
667 677
668 MY_ALLOC(size_t, p->FoCodersOffsets, (size_t)numFolders + 1, alloc); 678 MY_ALLOC(size_t, p->FoCodersOffsets, (size_t)numFolders + 1, alloc)
669 MY_ALLOC(UInt32, p->FoStartPackStreamIndex, (size_t)numFolders + 1, alloc); 679 MY_ALLOC(UInt32, p->FoStartPackStreamIndex, (size_t)numFolders + 1, alloc)
670 MY_ALLOC(UInt32, p->FoToCoderUnpackSizes, (size_t)numFolders + 1, alloc); 680 MY_ALLOC(UInt32, p->FoToCoderUnpackSizes, (size_t)numFolders + 1, alloc)
671 MY_ALLOC_ZE(Byte, p->FoToMainUnpackSizeIndex, (size_t)numFolders, alloc); 681 MY_ALLOC_ZE(Byte, p->FoToMainUnpackSizeIndex, (size_t)numFolders, alloc)
672 682
673 startBufPtr = sd.Data; 683 startBufPtr = sd.Data;
674 684
@@ -681,7 +691,7 @@ static SRes ReadUnpackInfo(CSzAr *p,
681 691
682 p->FoCodersOffsets[fo] = (size_t)(sd.Data - startBufPtr); 692 p->FoCodersOffsets[fo] = (size_t)(sd.Data - startBufPtr);
683 693
684 RINOK(SzReadNumber32(&sd, &numCoders)); 694 RINOK(SzReadNumber32(&sd, &numCoders))
685 if (numCoders == 0 || numCoders > k_Scan_NumCoders_MAX) 695 if (numCoders == 0 || numCoders > k_Scan_NumCoders_MAX)
686 return SZ_ERROR_UNSUPPORTED; 696 return SZ_ERROR_UNSUPPORTED;
687 697
@@ -691,7 +701,7 @@ static SRes ReadUnpackInfo(CSzAr *p,
691 unsigned idSize; 701 unsigned idSize;
692 UInt32 coderInStreams; 702 UInt32 coderInStreams;
693 703
694 SZ_READ_BYTE_2(mainByte); 704 SZ_READ_BYTE_2(mainByte)
695 if ((mainByte & 0xC0) != 0) 705 if ((mainByte & 0xC0) != 0)
696 return SZ_ERROR_UNSUPPORTED; 706 return SZ_ERROR_UNSUPPORTED;
697 idSize = (mainByte & 0xF); 707 idSize = (mainByte & 0xF);
@@ -699,15 +709,15 @@ static SRes ReadUnpackInfo(CSzAr *p,
699 return SZ_ERROR_UNSUPPORTED; 709 return SZ_ERROR_UNSUPPORTED;
700 if (idSize > sd.Size) 710 if (idSize > sd.Size)
701 return SZ_ERROR_ARCHIVE; 711 return SZ_ERROR_ARCHIVE;
702 SKIP_DATA2(sd, idSize); 712 SKIP_DATA2(sd, idSize)
703 713
704 coderInStreams = 1; 714 coderInStreams = 1;
705 715
706 if ((mainByte & 0x10) != 0) 716 if ((mainByte & 0x10) != 0)
707 { 717 {
708 UInt32 coderOutStreams; 718 UInt32 coderOutStreams;
709 RINOK(SzReadNumber32(&sd, &coderInStreams)); 719 RINOK(SzReadNumber32(&sd, &coderInStreams))
710 RINOK(SzReadNumber32(&sd, &coderOutStreams)); 720 RINOK(SzReadNumber32(&sd, &coderOutStreams))
711 if (coderInStreams > k_Scan_NumCodersStreams_in_Folder_MAX || coderOutStreams != 1) 721 if (coderInStreams > k_Scan_NumCodersStreams_in_Folder_MAX || coderOutStreams != 1)
712 return SZ_ERROR_UNSUPPORTED; 722 return SZ_ERROR_UNSUPPORTED;
713 } 723 }
@@ -717,10 +727,10 @@ static SRes ReadUnpackInfo(CSzAr *p,
717 if ((mainByte & 0x20) != 0) 727 if ((mainByte & 0x20) != 0)
718 { 728 {
719 UInt32 propsSize; 729 UInt32 propsSize;
720 RINOK(SzReadNumber32(&sd, &propsSize)); 730 RINOK(SzReadNumber32(&sd, &propsSize))
721 if (propsSize > sd.Size) 731 if (propsSize > sd.Size)
722 return SZ_ERROR_ARCHIVE; 732 return SZ_ERROR_ARCHIVE;
723 SKIP_DATA2(sd, propsSize); 733 SKIP_DATA2(sd, propsSize)
724 } 734 }
725 } 735 }
726 736
@@ -734,7 +744,7 @@ static SRes ReadUnpackInfo(CSzAr *p,
734 Byte coderUsed[k_Scan_NumCoders_MAX]; 744 Byte coderUsed[k_Scan_NumCoders_MAX];
735 745
736 UInt32 i; 746 UInt32 i;
737 UInt32 numBonds = numCoders - 1; 747 const UInt32 numBonds = numCoders - 1;
738 if (numInStreams < numBonds) 748 if (numInStreams < numBonds)
739 return SZ_ERROR_ARCHIVE; 749 return SZ_ERROR_ARCHIVE;
740 750
@@ -750,12 +760,12 @@ static SRes ReadUnpackInfo(CSzAr *p,
750 { 760 {
751 UInt32 index; 761 UInt32 index;
752 762
753 RINOK(SzReadNumber32(&sd, &index)); 763 RINOK(SzReadNumber32(&sd, &index))
754 if (index >= numInStreams || streamUsed[index]) 764 if (index >= numInStreams || streamUsed[index])
755 return SZ_ERROR_ARCHIVE; 765 return SZ_ERROR_ARCHIVE;
756 streamUsed[index] = True; 766 streamUsed[index] = True;
757 767
758 RINOK(SzReadNumber32(&sd, &index)); 768 RINOK(SzReadNumber32(&sd, &index))
759 if (index >= numCoders || coderUsed[index]) 769 if (index >= numCoders || coderUsed[index])
760 return SZ_ERROR_ARCHIVE; 770 return SZ_ERROR_ARCHIVE;
761 coderUsed[index] = True; 771 coderUsed[index] = True;
@@ -767,7 +777,7 @@ static SRes ReadUnpackInfo(CSzAr *p,
767 for (i = 0; i < numPackStreams; i++) 777 for (i = 0; i < numPackStreams; i++)
768 { 778 {
769 UInt32 index; 779 UInt32 index;
770 RINOK(SzReadNumber32(&sd, &index)); 780 RINOK(SzReadNumber32(&sd, &index))
771 if (index >= numInStreams || streamUsed[index]) 781 if (index >= numInStreams || streamUsed[index])
772 return SZ_ERROR_ARCHIVE; 782 return SZ_ERROR_ARCHIVE;
773 streamUsed[index] = True; 783 streamUsed[index] = True;
@@ -802,7 +812,7 @@ static SRes ReadUnpackInfo(CSzAr *p,
802 const size_t dataSize = (size_t)(sd.Data - startBufPtr); 812 const size_t dataSize = (size_t)(sd.Data - startBufPtr);
803 p->FoStartPackStreamIndex[fo] = packStreamIndex; 813 p->FoStartPackStreamIndex[fo] = packStreamIndex;
804 p->FoCodersOffsets[fo] = dataSize; 814 p->FoCodersOffsets[fo] = dataSize;
805 MY_ALLOC_ZE_AND_CPY(p->CodersData, dataSize, startBufPtr, alloc); 815 MY_ALLOC_ZE_AND_CPY(p->CodersData, dataSize, startBufPtr, alloc)
806 } 816 }
807 817
808 if (external != 0) 818 if (external != 0)
@@ -812,21 +822,21 @@ static SRes ReadUnpackInfo(CSzAr *p,
812 sd = *sd2; 822 sd = *sd2;
813 } 823 }
814 824
815 RINOK(WaitId(&sd, k7zIdCodersUnpackSize)); 825 RINOK(WaitId(&sd, k7zIdCodersUnpackSize))
816 826
817 MY_ALLOC_ZE(UInt64, p->CoderUnpackSizes, (size_t)numCodersOutStreams, alloc); 827 MY_ALLOC_ZE(UInt64, p->CoderUnpackSizes, (size_t)numCodersOutStreams, alloc)
818 { 828 {
819 UInt32 i; 829 UInt32 i;
820 for (i = 0; i < numCodersOutStreams; i++) 830 for (i = 0; i < numCodersOutStreams; i++)
821 { 831 {
822 RINOK(ReadNumber(&sd, p->CoderUnpackSizes + i)); 832 RINOK(ReadNumber(&sd, p->CoderUnpackSizes + i))
823 } 833 }
824 } 834 }
825 835
826 for (;;) 836 for (;;)
827 { 837 {
828 UInt64 type; 838 UInt64 type;
829 RINOK(ReadID(&sd, &type)); 839 RINOK(ReadID(&sd, &type))
830 if (type == k7zIdEnd) 840 if (type == k7zIdEnd)
831 { 841 {
832 *sd2 = sd; 842 *sd2 = sd;
@@ -834,10 +844,10 @@ static SRes ReadUnpackInfo(CSzAr *p,
834 } 844 }
835 if (type == k7zIdCRC) 845 if (type == k7zIdCRC)
836 { 846 {
837 RINOK(ReadBitUi32s(&sd, numFolders, &p->FolderCRCs, alloc)); 847 RINOK(ReadBitUi32s(&sd, numFolders, &p->FolderCRCs, alloc))
838 continue; 848 continue;
839 } 849 }
840 RINOK(SkipData(&sd)); 850 RINOK(SkipData(&sd))
841 } 851 }
842} 852}
843 853
@@ -862,13 +872,13 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi)
862{ 872{
863 UInt64 type = 0; 873 UInt64 type = 0;
864 UInt32 numSubDigests = 0; 874 UInt32 numSubDigests = 0;
865 UInt32 numFolders = p->NumFolders; 875 const UInt32 numFolders = p->NumFolders;
866 UInt32 numUnpackStreams = numFolders; 876 UInt32 numUnpackStreams = numFolders;
867 UInt32 numUnpackSizesInData = 0; 877 UInt32 numUnpackSizesInData = 0;
868 878
869 for (;;) 879 for (;;)
870 { 880 {
871 RINOK(ReadID(sd, &type)); 881 RINOK(ReadID(sd, &type))
872 if (type == k7zIdNumUnpackStream) 882 if (type == k7zIdNumUnpackStream)
873 { 883 {
874 UInt32 i; 884 UInt32 i;
@@ -878,7 +888,7 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi)
878 for (i = 0; i < numFolders; i++) 888 for (i = 0; i < numFolders; i++)
879 { 889 {
880 UInt32 numStreams; 890 UInt32 numStreams;
881 RINOK(SzReadNumber32(sd, &numStreams)); 891 RINOK(SzReadNumber32(sd, &numStreams))
882 if (numUnpackStreams > numUnpackStreams + numStreams) 892 if (numUnpackStreams > numUnpackStreams + numStreams)
883 return SZ_ERROR_UNSUPPORTED; 893 return SZ_ERROR_UNSUPPORTED;
884 numUnpackStreams += numStreams; 894 numUnpackStreams += numStreams;
@@ -892,7 +902,7 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi)
892 } 902 }
893 if (type == k7zIdCRC || type == k7zIdSize || type == k7zIdEnd) 903 if (type == k7zIdCRC || type == k7zIdSize || type == k7zIdEnd)
894 break; 904 break;
895 RINOK(SkipData(sd)); 905 RINOK(SkipData(sd))
896 } 906 }
897 907
898 if (!ssi->sdNumSubStreams.Data) 908 if (!ssi->sdNumSubStreams.Data)
@@ -908,9 +918,9 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi)
908 if (type == k7zIdSize) 918 if (type == k7zIdSize)
909 { 919 {
910 ssi->sdSizes.Data = sd->Data; 920 ssi->sdSizes.Data = sd->Data;
911 RINOK(SkipNumbers(sd, numUnpackSizesInData)); 921 RINOK(SkipNumbers(sd, numUnpackSizesInData))
912 ssi->sdSizes.Size = (size_t)(sd->Data - ssi->sdSizes.Data); 922 ssi->sdSizes.Size = (size_t)(sd->Data - ssi->sdSizes.Data);
913 RINOK(ReadID(sd, &type)); 923 RINOK(ReadID(sd, &type))
914 } 924 }
915 925
916 for (;;) 926 for (;;)
@@ -920,14 +930,14 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi)
920 if (type == k7zIdCRC) 930 if (type == k7zIdCRC)
921 { 931 {
922 ssi->sdCRCs.Data = sd->Data; 932 ssi->sdCRCs.Data = sd->Data;
923 RINOK(SkipBitUi32s(sd, numSubDigests)); 933 RINOK(SkipBitUi32s(sd, numSubDigests))
924 ssi->sdCRCs.Size = (size_t)(sd->Data - ssi->sdCRCs.Data); 934 ssi->sdCRCs.Size = (size_t)(sd->Data - ssi->sdCRCs.Data);
925 } 935 }
926 else 936 else
927 { 937 {
928 RINOK(SkipData(sd)); 938 RINOK(SkipData(sd))
929 } 939 }
930 RINOK(ReadID(sd, &type)); 940 RINOK(ReadID(sd, &type))
931 } 941 }
932} 942}
933 943
@@ -940,31 +950,31 @@ static SRes SzReadStreamsInfo(CSzAr *p,
940{ 950{
941 UInt64 type; 951 UInt64 type;
942 952
943 SzData_Clear(&ssi->sdSizes); 953 SzData_CLEAR(&ssi->sdSizes)
944 SzData_Clear(&ssi->sdCRCs); 954 SzData_CLEAR(&ssi->sdCRCs)
945 SzData_Clear(&ssi->sdNumSubStreams); 955 SzData_CLEAR(&ssi->sdNumSubStreams)
946 956
947 *dataOffset = 0; 957 *dataOffset = 0;
948 RINOK(ReadID(sd, &type)); 958 RINOK(ReadID(sd, &type))
949 if (type == k7zIdPackInfo) 959 if (type == k7zIdPackInfo)
950 { 960 {
951 RINOK(ReadNumber(sd, dataOffset)); 961 RINOK(ReadNumber(sd, dataOffset))
952 if (*dataOffset > p->RangeLimit) 962 if (*dataOffset > p->RangeLimit)
953 return SZ_ERROR_ARCHIVE; 963 return SZ_ERROR_ARCHIVE;
954 RINOK(ReadPackInfo(p, sd, alloc)); 964 RINOK(ReadPackInfo(p, sd, alloc))
955 if (p->PackPositions[p->NumPackStreams] > p->RangeLimit - *dataOffset) 965 if (p->PackPositions[p->NumPackStreams] > p->RangeLimit - *dataOffset)
956 return SZ_ERROR_ARCHIVE; 966 return SZ_ERROR_ARCHIVE;
957 RINOK(ReadID(sd, &type)); 967 RINOK(ReadID(sd, &type))
958 } 968 }
959 if (type == k7zIdUnpackInfo) 969 if (type == k7zIdUnpackInfo)
960 { 970 {
961 RINOK(ReadUnpackInfo(p, sd, numFoldersMax, tempBufs, numTempBufs, alloc)); 971 RINOK(ReadUnpackInfo(p, sd, numFoldersMax, tempBufs, numTempBufs, alloc))
962 RINOK(ReadID(sd, &type)); 972 RINOK(ReadID(sd, &type))
963 } 973 }
964 if (type == k7zIdSubStreamsInfo) 974 if (type == k7zIdSubStreamsInfo)
965 { 975 {
966 RINOK(ReadSubStreamsInfo(p, sd, ssi)); 976 RINOK(ReadSubStreamsInfo(p, sd, ssi))
967 RINOK(ReadID(sd, &type)); 977 RINOK(ReadID(sd, &type))
968 } 978 }
969 else 979 else
970 { 980 {
@@ -976,7 +986,7 @@ static SRes SzReadStreamsInfo(CSzAr *p,
976} 986}
977 987
978static SRes SzReadAndDecodePackedStreams( 988static SRes SzReadAndDecodePackedStreams(
979 ILookInStream *inStream, 989 ILookInStreamPtr inStream,
980 CSzData *sd, 990 CSzData *sd,
981 CBuf *tempBufs, 991 CBuf *tempBufs,
982 UInt32 numFoldersMax, 992 UInt32 numFoldersMax,
@@ -988,7 +998,7 @@ static SRes SzReadAndDecodePackedStreams(
988 UInt32 fo; 998 UInt32 fo;
989 CSubStreamInfo ssi; 999 CSubStreamInfo ssi;
990 1000
991 RINOK(SzReadStreamsInfo(p, sd, numFoldersMax, NULL, 0, &dataStartPos, &ssi, allocTemp)); 1001 RINOK(SzReadStreamsInfo(p, sd, numFoldersMax, NULL, 0, &dataStartPos, &ssi, allocTemp))
992 1002
993 dataStartPos += baseOffset; 1003 dataStartPos += baseOffset;
994 if (p->NumFolders == 0) 1004 if (p->NumFolders == 0)
@@ -1000,7 +1010,7 @@ static SRes SzReadAndDecodePackedStreams(
1000 for (fo = 0; fo < p->NumFolders; fo++) 1010 for (fo = 0; fo < p->NumFolders; fo++)
1001 { 1011 {
1002 CBuf *tempBuf = tempBufs + fo; 1012 CBuf *tempBuf = tempBufs + fo;
1003 UInt64 unpackSize = SzAr_GetFolderUnpackSize(p, fo); 1013 const UInt64 unpackSize = SzAr_GetFolderUnpackSize(p, fo);
1004 if ((size_t)unpackSize != unpackSize) 1014 if ((size_t)unpackSize != unpackSize)
1005 return SZ_ERROR_MEM; 1015 return SZ_ERROR_MEM;
1006 if (!Buf_Create(tempBuf, (size_t)unpackSize, allocTemp)) 1016 if (!Buf_Create(tempBuf, (size_t)unpackSize, allocTemp))
@@ -1010,8 +1020,8 @@ static SRes SzReadAndDecodePackedStreams(
1010 for (fo = 0; fo < p->NumFolders; fo++) 1020 for (fo = 0; fo < p->NumFolders; fo++)
1011 { 1021 {
1012 const CBuf *tempBuf = tempBufs + fo; 1022 const CBuf *tempBuf = tempBufs + fo;
1013 RINOK(LookInStream_SeekTo(inStream, dataStartPos)); 1023 RINOK(LookInStream_SeekTo(inStream, dataStartPos))
1014 RINOK(SzAr_DecodeFolder(p, fo, inStream, dataStartPos, tempBuf->data, tempBuf->size, allocTemp)); 1024 RINOK(SzAr_DecodeFolder(p, fo, inStream, dataStartPos, tempBuf->data, tempBuf->size, allocTemp))
1015 } 1025 }
1016 1026
1017 return SZ_OK; 1027 return SZ_OK;
@@ -1046,7 +1056,7 @@ static SRes SzReadFileNames(const Byte *data, size_t size, UInt32 numFiles, size
1046 return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE; 1056 return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
1047} 1057}
1048 1058
1049static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num, 1059static Z7_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num,
1050 CSzData *sd2, 1060 CSzData *sd2,
1051 const CBuf *tempBufs, UInt32 numTempBufs, 1061 const CBuf *tempBufs, UInt32 numTempBufs,
1052 ISzAllocPtr alloc) 1062 ISzAllocPtr alloc)
@@ -1057,22 +1067,22 @@ static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num,
1057 Byte *defs; 1067 Byte *defs;
1058 Byte external; 1068 Byte external;
1059 1069
1060 RINOK(ReadBitVector(sd2, num, &p->Defs, alloc)); 1070 RINOK(ReadBitVector(sd2, num, &p->Defs, alloc))
1061 1071
1062 SZ_READ_BYTE_SD(sd2, external); 1072 SZ_READ_BYTE_SD(sd2, external)
1063 if (external == 0) 1073 if (external == 0)
1064 sd = *sd2; 1074 sd = *sd2;
1065 else 1075 else
1066 { 1076 {
1067 UInt32 index; 1077 UInt32 index;
1068 RINOK(SzReadNumber32(sd2, &index)); 1078 RINOK(SzReadNumber32(sd2, &index))
1069 if (index >= numTempBufs) 1079 if (index >= numTempBufs)
1070 return SZ_ERROR_ARCHIVE; 1080 return SZ_ERROR_ARCHIVE;
1071 sd.Data = tempBufs[index].data; 1081 sd.Data = tempBufs[index].data;
1072 sd.Size = tempBufs[index].size; 1082 sd.Size = tempBufs[index].size;
1073 } 1083 }
1074 1084
1075 MY_ALLOC_ZE(CNtfsFileTime, p->Vals, num, alloc); 1085 MY_ALLOC_ZE(CNtfsFileTime, p->Vals, num, alloc)
1076 vals = p->Vals; 1086 vals = p->Vals;
1077 defs = p->Defs; 1087 defs = p->Defs;
1078 for (i = 0; i < num; i++) 1088 for (i = 0; i < num; i++)
@@ -1082,7 +1092,7 @@ static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num,
1082 return SZ_ERROR_ARCHIVE; 1092 return SZ_ERROR_ARCHIVE;
1083 vals[i].Low = GetUi32(sd.Data); 1093 vals[i].Low = GetUi32(sd.Data);
1084 vals[i].High = GetUi32(sd.Data + 4); 1094 vals[i].High = GetUi32(sd.Data + 4);
1085 SKIP_DATA2(sd, 8); 1095 SKIP_DATA2(sd, 8)
1086 } 1096 }
1087 else 1097 else
1088 vals[i].High = vals[i].Low = 0; 1098 vals[i].High = vals[i].Low = 0;
@@ -1100,7 +1110,7 @@ static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num,
1100static SRes SzReadHeader2( 1110static SRes SzReadHeader2(
1101 CSzArEx *p, /* allocMain */ 1111 CSzArEx *p, /* allocMain */
1102 CSzData *sd, 1112 CSzData *sd,
1103 ILookInStream *inStream, 1113 ILookInStreamPtr inStream,
1104 CBuf *tempBufs, UInt32 *numTempBufs, 1114 CBuf *tempBufs, UInt32 *numTempBufs,
1105 ISzAllocPtr allocMain, 1115 ISzAllocPtr allocMain,
1106 ISzAllocPtr allocTemp 1116 ISzAllocPtr allocTemp
@@ -1111,26 +1121,26 @@ static SRes SzReadHeader2(
1111{ 1121{
1112 UInt64 type; 1122 UInt64 type;
1113 1123
1114 SzData_Clear(&ssi.sdSizes); 1124 SzData_CLEAR(&ssi.sdSizes)
1115 SzData_Clear(&ssi.sdCRCs); 1125 SzData_CLEAR(&ssi.sdCRCs)
1116 SzData_Clear(&ssi.sdNumSubStreams); 1126 SzData_CLEAR(&ssi.sdNumSubStreams)
1117 1127
1118 ssi.NumSubDigests = 0; 1128 ssi.NumSubDigests = 0;
1119 ssi.NumTotalSubStreams = 0; 1129 ssi.NumTotalSubStreams = 0;
1120 1130
1121 RINOK(ReadID(sd, &type)); 1131 RINOK(ReadID(sd, &type))
1122 1132
1123 if (type == k7zIdArchiveProperties) 1133 if (type == k7zIdArchiveProperties)
1124 { 1134 {
1125 for (;;) 1135 for (;;)
1126 { 1136 {
1127 UInt64 type2; 1137 UInt64 type2;
1128 RINOK(ReadID(sd, &type2)); 1138 RINOK(ReadID(sd, &type2))
1129 if (type2 == k7zIdEnd) 1139 if (type2 == k7zIdEnd)
1130 break; 1140 break;
1131 RINOK(SkipData(sd)); 1141 RINOK(SkipData(sd))
1132 } 1142 }
1133 RINOK(ReadID(sd, &type)); 1143 RINOK(ReadID(sd, &type))
1134 } 1144 }
1135 1145
1136 if (type == k7zIdAdditionalStreamsInfo) 1146 if (type == k7zIdAdditionalStreamsInfo)
@@ -1148,15 +1158,15 @@ static SRes SzReadHeader2(
1148 1158
1149 if (res != SZ_OK) 1159 if (res != SZ_OK)
1150 return res; 1160 return res;
1151 RINOK(ReadID(sd, &type)); 1161 RINOK(ReadID(sd, &type))
1152 } 1162 }
1153 1163
1154 if (type == k7zIdMainStreamsInfo) 1164 if (type == k7zIdMainStreamsInfo)
1155 { 1165 {
1156 RINOK(SzReadStreamsInfo(&p->db, sd, (UInt32)1 << 30, tempBufs, *numTempBufs, 1166 RINOK(SzReadStreamsInfo(&p->db, sd, (UInt32)1 << 30, tempBufs, *numTempBufs,
1157 &p->dataPos, &ssi, allocMain)); 1167 &p->dataPos, &ssi, allocMain))
1158 p->dataPos += p->startPosAfterHeader; 1168 p->dataPos += p->startPosAfterHeader;
1159 RINOK(ReadID(sd, &type)); 1169 RINOK(ReadID(sd, &type))
1160 } 1170 }
1161 1171
1162 if (type == k7zIdEnd) 1172 if (type == k7zIdEnd)
@@ -1174,23 +1184,23 @@ static SRes SzReadHeader2(
1174 const Byte *emptyStreams = NULL; 1184 const Byte *emptyStreams = NULL;
1175 const Byte *emptyFiles = NULL; 1185 const Byte *emptyFiles = NULL;
1176 1186
1177 RINOK(SzReadNumber32(sd, &numFiles)); 1187 RINOK(SzReadNumber32(sd, &numFiles))
1178 p->NumFiles = numFiles; 1188 p->NumFiles = numFiles;
1179 1189
1180 for (;;) 1190 for (;;)
1181 { 1191 {
1182 UInt64 type; 1192 UInt64 type;
1183 UInt64 size; 1193 UInt64 size;
1184 RINOK(ReadID(sd, &type)); 1194 RINOK(ReadID(sd, &type))
1185 if (type == k7zIdEnd) 1195 if (type == k7zIdEnd)
1186 break; 1196 break;
1187 RINOK(ReadNumber(sd, &size)); 1197 RINOK(ReadNumber(sd, &size))
1188 if (size > sd->Size) 1198 if (size > sd->Size)
1189 return SZ_ERROR_ARCHIVE; 1199 return SZ_ERROR_ARCHIVE;
1190 1200
1191 if (type >= ((UInt32)1 << 8)) 1201 if (type >= ((UInt32)1 << 8))
1192 { 1202 {
1193 SKIP_DATA(sd, size); 1203 SKIP_DATA(sd, size)
1194 } 1204 }
1195 else switch ((unsigned)type) 1205 else switch ((unsigned)type)
1196 { 1206 {
@@ -1200,7 +1210,7 @@ static SRes SzReadHeader2(
1200 const Byte *namesData; 1210 const Byte *namesData;
1201 Byte external; 1211 Byte external;
1202 1212
1203 SZ_READ_BYTE(external); 1213 SZ_READ_BYTE(external)
1204 if (external == 0) 1214 if (external == 0)
1205 { 1215 {
1206 namesSize = (size_t)size - 1; 1216 namesSize = (size_t)size - 1;
@@ -1209,7 +1219,7 @@ static SRes SzReadHeader2(
1209 else 1219 else
1210 { 1220 {
1211 UInt32 index; 1221 UInt32 index;
1212 RINOK(SzReadNumber32(sd, &index)); 1222 RINOK(SzReadNumber32(sd, &index))
1213 if (index >= *numTempBufs) 1223 if (index >= *numTempBufs)
1214 return SZ_ERROR_ARCHIVE; 1224 return SZ_ERROR_ARCHIVE;
1215 namesData = (tempBufs)[index].data; 1225 namesData = (tempBufs)[index].data;
@@ -1218,25 +1228,25 @@ static SRes SzReadHeader2(
1218 1228
1219 if ((namesSize & 1) != 0) 1229 if ((namesSize & 1) != 0)
1220 return SZ_ERROR_ARCHIVE; 1230 return SZ_ERROR_ARCHIVE;
1221 MY_ALLOC(size_t, p->FileNameOffsets, numFiles + 1, allocMain); 1231 MY_ALLOC(size_t, p->FileNameOffsets, numFiles + 1, allocMain)
1222 MY_ALLOC_ZE_AND_CPY(p->FileNames, namesSize, namesData, allocMain); 1232 MY_ALLOC_ZE_AND_CPY(p->FileNames, namesSize, namesData, allocMain)
1223 RINOK(SzReadFileNames(p->FileNames, namesSize, numFiles, p->FileNameOffsets)) 1233 RINOK(SzReadFileNames(p->FileNames, namesSize, numFiles, p->FileNameOffsets))
1224 if (external == 0) 1234 if (external == 0)
1225 { 1235 {
1226 SKIP_DATA(sd, namesSize); 1236 SKIP_DATA(sd, namesSize)
1227 } 1237 }
1228 break; 1238 break;
1229 } 1239 }
1230 case k7zIdEmptyStream: 1240 case k7zIdEmptyStream:
1231 { 1241 {
1232 RINOK(RememberBitVector(sd, numFiles, &emptyStreams)); 1242 RINOK(RememberBitVector(sd, numFiles, &emptyStreams))
1233 numEmptyStreams = CountDefinedBits(emptyStreams, numFiles); 1243 numEmptyStreams = CountDefinedBits(emptyStreams, numFiles);
1234 emptyFiles = NULL; 1244 emptyFiles = NULL;
1235 break; 1245 break;
1236 } 1246 }
1237 case k7zIdEmptyFile: 1247 case k7zIdEmptyFile:
1238 { 1248 {
1239 RINOK(RememberBitVector(sd, numEmptyStreams, &emptyFiles)); 1249 RINOK(RememberBitVector(sd, numEmptyStreams, &emptyFiles))
1240 break; 1250 break;
1241 } 1251 }
1242 case k7zIdWinAttrib: 1252 case k7zIdWinAttrib:
@@ -1245,22 +1255,22 @@ static SRes SzReadHeader2(
1245 CSzData sdSwitch; 1255 CSzData sdSwitch;
1246 CSzData *sdPtr; 1256 CSzData *sdPtr;
1247 SzBitUi32s_Free(&p->Attribs, allocMain); 1257 SzBitUi32s_Free(&p->Attribs, allocMain);
1248 RINOK(ReadBitVector(sd, numFiles, &p->Attribs.Defs, allocMain)); 1258 RINOK(ReadBitVector(sd, numFiles, &p->Attribs.Defs, allocMain))
1249 1259
1250 SZ_READ_BYTE(external); 1260 SZ_READ_BYTE(external)
1251 if (external == 0) 1261 if (external == 0)
1252 sdPtr = sd; 1262 sdPtr = sd;
1253 else 1263 else
1254 { 1264 {
1255 UInt32 index; 1265 UInt32 index;
1256 RINOK(SzReadNumber32(sd, &index)); 1266 RINOK(SzReadNumber32(sd, &index))
1257 if (index >= *numTempBufs) 1267 if (index >= *numTempBufs)
1258 return SZ_ERROR_ARCHIVE; 1268 return SZ_ERROR_ARCHIVE;
1259 sdSwitch.Data = (tempBufs)[index].data; 1269 sdSwitch.Data = (tempBufs)[index].data;
1260 sdSwitch.Size = (tempBufs)[index].size; 1270 sdSwitch.Size = (tempBufs)[index].size;
1261 sdPtr = &sdSwitch; 1271 sdPtr = &sdSwitch;
1262 } 1272 }
1263 RINOK(ReadUi32s(sdPtr, numFiles, &p->Attribs, allocMain)); 1273 RINOK(ReadUi32s(sdPtr, numFiles, &p->Attribs, allocMain))
1264 break; 1274 break;
1265 } 1275 }
1266 /* 1276 /*
@@ -1273,11 +1283,11 @@ static SRes SzReadHeader2(
1273 break; 1283 break;
1274 } 1284 }
1275 */ 1285 */
1276 case k7zIdMTime: RINOK(ReadTime(&p->MTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)); break; 1286 case k7zIdMTime: RINOK(ReadTime(&p->MTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)) break;
1277 case k7zIdCTime: RINOK(ReadTime(&p->CTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)); break; 1287 case k7zIdCTime: RINOK(ReadTime(&p->CTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)) break;
1278 default: 1288 default:
1279 { 1289 {
1280 SKIP_DATA(sd, size); 1290 SKIP_DATA(sd, size)
1281 } 1291 }
1282 } 1292 }
1283 } 1293 }
@@ -1288,10 +1298,10 @@ static SRes SzReadHeader2(
1288 for (;;) 1298 for (;;)
1289 { 1299 {
1290 UInt64 type; 1300 UInt64 type;
1291 RINOK(ReadID(sd, &type)); 1301 RINOK(ReadID(sd, &type))
1292 if (type == k7zIdEnd) 1302 if (type == k7zIdEnd)
1293 break; 1303 break;
1294 RINOK(SkipData(sd)); 1304 RINOK(SkipData(sd))
1295 } 1305 }
1296 1306
1297 { 1307 {
@@ -1303,40 +1313,37 @@ static SRes SzReadHeader2(
1303 UInt64 unpackPos = 0; 1313 UInt64 unpackPos = 0;
1304 const Byte *digestsDefs = NULL; 1314 const Byte *digestsDefs = NULL;
1305 const Byte *digestsVals = NULL; 1315 const Byte *digestsVals = NULL;
1306 UInt32 digestsValsIndex = 0; 1316 UInt32 digestIndex = 0;
1307 UInt32 digestIndex;
1308 Byte allDigestsDefined = 0;
1309 Byte isDirMask = 0; 1317 Byte isDirMask = 0;
1310 Byte crcMask = 0; 1318 Byte crcMask = 0;
1311 Byte mask = 0x80; 1319 Byte mask = 0x80;
1312 1320
1313 MY_ALLOC(UInt32, p->FolderToFile, p->db.NumFolders + 1, allocMain); 1321 MY_ALLOC(UInt32, p->FolderToFile, p->db.NumFolders + 1, allocMain)
1314 MY_ALLOC_ZE(UInt32, p->FileToFolder, p->NumFiles, allocMain); 1322 MY_ALLOC_ZE(UInt32, p->FileToFolder, p->NumFiles, allocMain)
1315 MY_ALLOC(UInt64, p->UnpackPositions, p->NumFiles + 1, allocMain); 1323 MY_ALLOC(UInt64, p->UnpackPositions, p->NumFiles + 1, allocMain)
1316 MY_ALLOC_ZE(Byte, p->IsDirs, (p->NumFiles + 7) >> 3, allocMain); 1324 MY_ALLOC_ZE(Byte, p->IsDirs, (p->NumFiles + 7) >> 3, allocMain)
1317 1325
1318 RINOK(SzBitUi32s_Alloc(&p->CRCs, p->NumFiles, allocMain)); 1326 RINOK(SzBitUi32s_Alloc(&p->CRCs, p->NumFiles, allocMain))
1319 1327
1320 if (ssi.sdCRCs.Size != 0) 1328 if (ssi.sdCRCs.Size != 0)
1321 { 1329 {
1322 SZ_READ_BYTE_SD(&ssi.sdCRCs, allDigestsDefined); 1330 Byte allDigestsDefined = 0;
1331 SZ_READ_BYTE_SD_NOCHECK(&ssi.sdCRCs, allDigestsDefined)
1323 if (allDigestsDefined) 1332 if (allDigestsDefined)
1324 digestsVals = ssi.sdCRCs.Data; 1333 digestsVals = ssi.sdCRCs.Data;
1325 else 1334 else
1326 { 1335 {
1327 size_t numBytes = (ssi.NumSubDigests + 7) >> 3; 1336 const size_t numBytes = (ssi.NumSubDigests + 7) >> 3;
1328 digestsDefs = ssi.sdCRCs.Data; 1337 digestsDefs = ssi.sdCRCs.Data;
1329 digestsVals = digestsDefs + numBytes; 1338 digestsVals = digestsDefs + numBytes;
1330 } 1339 }
1331 } 1340 }
1332 1341
1333 digestIndex = 0;
1334
1335 for (i = 0; i < numFiles; i++, mask >>= 1) 1342 for (i = 0; i < numFiles; i++, mask >>= 1)
1336 { 1343 {
1337 if (mask == 0) 1344 if (mask == 0)
1338 { 1345 {
1339 UInt32 byteIndex = (i - 1) >> 3; 1346 const UInt32 byteIndex = (i - 1) >> 3;
1340 p->IsDirs[byteIndex] = isDirMask; 1347 p->IsDirs[byteIndex] = isDirMask;
1341 p->CRCs.Defs[byteIndex] = crcMask; 1348 p->CRCs.Defs[byteIndex] = crcMask;
1342 isDirMask = 0; 1349 isDirMask = 0;
@@ -1374,18 +1381,17 @@ static SRes SzReadHeader2(
1374 numSubStreams = 1; 1381 numSubStreams = 1;
1375 if (ssi.sdNumSubStreams.Data) 1382 if (ssi.sdNumSubStreams.Data)
1376 { 1383 {
1377 RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams)); 1384 RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams))
1378 } 1385 }
1379 remSubStreams = numSubStreams; 1386 remSubStreams = numSubStreams;
1380 if (numSubStreams != 0) 1387 if (numSubStreams != 0)
1381 break; 1388 break;
1382 { 1389 {
1383 UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex); 1390 const UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
1384 unpackPos += folderUnpackSize; 1391 unpackPos += folderUnpackSize;
1385 if (unpackPos < folderUnpackSize) 1392 if (unpackPos < folderUnpackSize)
1386 return SZ_ERROR_ARCHIVE; 1393 return SZ_ERROR_ARCHIVE;
1387 } 1394 }
1388
1389 folderIndex++; 1395 folderIndex++;
1390 } 1396 }
1391 } 1397 }
@@ -1397,47 +1403,44 @@ static SRes SzReadHeader2(
1397 1403
1398 if (--remSubStreams == 0) 1404 if (--remSubStreams == 0)
1399 { 1405 {
1400 UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex); 1406 const UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
1401 UInt64 startFolderUnpackPos = p->UnpackPositions[p->FolderToFile[folderIndex]]; 1407 const UInt64 startFolderUnpackPos = p->UnpackPositions[p->FolderToFile[folderIndex]];
1402 if (folderUnpackSize < unpackPos - startFolderUnpackPos) 1408 if (folderUnpackSize < unpackPos - startFolderUnpackPos)
1403 return SZ_ERROR_ARCHIVE; 1409 return SZ_ERROR_ARCHIVE;
1404 unpackPos = startFolderUnpackPos + folderUnpackSize; 1410 unpackPos = startFolderUnpackPos + folderUnpackSize;
1405 if (unpackPos < folderUnpackSize) 1411 if (unpackPos < folderUnpackSize)
1406 return SZ_ERROR_ARCHIVE; 1412 return SZ_ERROR_ARCHIVE;
1407 1413
1408 if (numSubStreams == 1 && SzBitWithVals_Check(&p->db.FolderCRCs, i)) 1414 if (numSubStreams == 1 && SzBitWithVals_Check(&p->db.FolderCRCs, folderIndex))
1409 { 1415 {
1410 p->CRCs.Vals[i] = p->db.FolderCRCs.Vals[folderIndex]; 1416 p->CRCs.Vals[i] = p->db.FolderCRCs.Vals[folderIndex];
1411 crcMask |= mask; 1417 crcMask |= mask;
1412 } 1418 }
1413 else if (allDigestsDefined || (digestsDefs && SzBitArray_Check(digestsDefs, digestIndex)))
1414 {
1415 p->CRCs.Vals[i] = GetUi32(digestsVals + (size_t)digestsValsIndex * 4);
1416 digestsValsIndex++;
1417 crcMask |= mask;
1418 }
1419
1420 folderIndex++; 1419 folderIndex++;
1421 } 1420 }
1422 else 1421 else
1423 { 1422 {
1424 UInt64 v; 1423 UInt64 v;
1425 RINOK(ReadNumber(&ssi.sdSizes, &v)); 1424 RINOK(ReadNumber(&ssi.sdSizes, &v))
1426 unpackPos += v; 1425 unpackPos += v;
1427 if (unpackPos < v) 1426 if (unpackPos < v)
1428 return SZ_ERROR_ARCHIVE; 1427 return SZ_ERROR_ARCHIVE;
1429 if (allDigestsDefined || (digestsDefs && SzBitArray_Check(digestsDefs, digestIndex))) 1428 }
1429 if ((crcMask & mask) == 0 && digestsVals)
1430 {
1431 if (!digestsDefs || SzBitArray_Check(digestsDefs, digestIndex))
1430 { 1432 {
1431 p->CRCs.Vals[i] = GetUi32(digestsVals + (size_t)digestsValsIndex * 4); 1433 p->CRCs.Vals[i] = GetUi32(digestsVals);
1432 digestsValsIndex++; 1434 digestsVals += 4;
1433 crcMask |= mask; 1435 crcMask |= mask;
1434 } 1436 }
1437 digestIndex++;
1435 } 1438 }
1436 } 1439 }
1437 1440
1438 if (mask != 0x80) 1441 if (mask != 0x80)
1439 { 1442 {
1440 UInt32 byteIndex = (i - 1) >> 3; 1443 const UInt32 byteIndex = (i - 1) >> 3;
1441 p->IsDirs[byteIndex] = isDirMask; 1444 p->IsDirs[byteIndex] = isDirMask;
1442 p->CRCs.Defs[byteIndex] = crcMask; 1445 p->CRCs.Defs[byteIndex] = crcMask;
1443 } 1446 }
@@ -1454,7 +1457,7 @@ static SRes SzReadHeader2(
1454 break; 1457 break;
1455 if (!ssi.sdNumSubStreams.Data) 1458 if (!ssi.sdNumSubStreams.Data)
1456 return SZ_ERROR_ARCHIVE; 1459 return SZ_ERROR_ARCHIVE;
1457 RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams)); 1460 RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams))
1458 if (numSubStreams != 0) 1461 if (numSubStreams != 0)
1459 return SZ_ERROR_ARCHIVE; 1462 return SZ_ERROR_ARCHIVE;
1460 /* 1463 /*
@@ -1479,7 +1482,7 @@ static SRes SzReadHeader2(
1479static SRes SzReadHeader( 1482static SRes SzReadHeader(
1480 CSzArEx *p, 1483 CSzArEx *p,
1481 CSzData *sd, 1484 CSzData *sd,
1482 ILookInStream *inStream, 1485 ILookInStreamPtr inStream,
1483 ISzAllocPtr allocMain, 1486 ISzAllocPtr allocMain,
1484 ISzAllocPtr allocTemp) 1487 ISzAllocPtr allocTemp)
1485{ 1488{
@@ -1498,7 +1501,7 @@ static SRes SzReadHeader(
1498 for (i = 0; i < NUM_ADDITIONAL_STREAMS_MAX; i++) 1501 for (i = 0; i < NUM_ADDITIONAL_STREAMS_MAX; i++)
1499 Buf_Free(tempBufs + i, allocTemp); 1502 Buf_Free(tempBufs + i, allocTemp);
1500 1503
1501 RINOK(res); 1504 RINOK(res)
1502 1505
1503 if (sd->Size != 0) 1506 if (sd->Size != 0)
1504 return SZ_ERROR_FAIL; 1507 return SZ_ERROR_FAIL;
@@ -1508,7 +1511,7 @@ static SRes SzReadHeader(
1508 1511
1509static SRes SzArEx_Open2( 1512static SRes SzArEx_Open2(
1510 CSzArEx *p, 1513 CSzArEx *p,
1511 ILookInStream *inStream, 1514 ILookInStreamPtr inStream,
1512 ISzAllocPtr allocMain, 1515 ISzAllocPtr allocMain,
1513 ISzAllocPtr allocTemp) 1516 ISzAllocPtr allocTemp)
1514{ 1517{
@@ -1521,9 +1524,9 @@ static SRes SzArEx_Open2(
1521 SRes res; 1524 SRes res;
1522 1525
1523 startArcPos = 0; 1526 startArcPos = 0;
1524 RINOK(ILookInStream_Seek(inStream, &startArcPos, SZ_SEEK_CUR)); 1527 RINOK(ILookInStream_Seek(inStream, &startArcPos, SZ_SEEK_CUR))
1525 1528
1526 RINOK(LookInStream_Read2(inStream, header, k7zStartHeaderSize, SZ_ERROR_NO_ARCHIVE)); 1529 RINOK(LookInStream_Read2(inStream, header, k7zStartHeaderSize, SZ_ERROR_NO_ARCHIVE))
1527 1530
1528 if (!TestSignatureCandidate(header)) 1531 if (!TestSignatureCandidate(header))
1529 return SZ_ERROR_NO_ARCHIVE; 1532 return SZ_ERROR_NO_ARCHIVE;
@@ -1552,14 +1555,14 @@ static SRes SzArEx_Open2(
1552 1555
1553 { 1556 {
1554 Int64 pos = 0; 1557 Int64 pos = 0;
1555 RINOK(ILookInStream_Seek(inStream, &pos, SZ_SEEK_END)); 1558 RINOK(ILookInStream_Seek(inStream, &pos, SZ_SEEK_END))
1556 if ((UInt64)pos < (UInt64)startArcPos + nextHeaderOffset || 1559 if ((UInt64)pos < (UInt64)startArcPos + nextHeaderOffset ||
1557 (UInt64)pos < (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset || 1560 (UInt64)pos < (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset ||
1558 (UInt64)pos < (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset + nextHeaderSize) 1561 (UInt64)pos < (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset + nextHeaderSize)
1559 return SZ_ERROR_INPUT_EOF; 1562 return SZ_ERROR_INPUT_EOF;
1560 } 1563 }
1561 1564
1562 RINOK(LookInStream_SeekTo(inStream, (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset)); 1565 RINOK(LookInStream_SeekTo(inStream, (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset))
1563 1566
1564 if (!Buf_Create(&buf, nextHeaderSizeT, allocTemp)) 1567 if (!Buf_Create(&buf, nextHeaderSizeT, allocTemp))
1565 return SZ_ERROR_MEM; 1568 return SZ_ERROR_MEM;
@@ -1634,10 +1637,10 @@ static SRes SzArEx_Open2(
1634} 1637}
1635 1638
1636 1639
1637SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream, 1640SRes SzArEx_Open(CSzArEx *p, ILookInStreamPtr inStream,
1638 ISzAllocPtr allocMain, ISzAllocPtr allocTemp) 1641 ISzAllocPtr allocMain, ISzAllocPtr allocTemp)
1639{ 1642{
1640 SRes res = SzArEx_Open2(p, inStream, allocMain, allocTemp); 1643 const SRes res = SzArEx_Open2(p, inStream, allocMain, allocTemp);
1641 if (res != SZ_OK) 1644 if (res != SZ_OK)
1642 SzArEx_Free(p, allocMain); 1645 SzArEx_Free(p, allocMain);
1643 return res; 1646 return res;
@@ -1646,7 +1649,7 @@ SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream,
1646 1649
1647SRes SzArEx_Extract( 1650SRes SzArEx_Extract(
1648 const CSzArEx *p, 1651 const CSzArEx *p,
1649 ILookInStream *inStream, 1652 ILookInStreamPtr inStream,
1650 UInt32 fileIndex, 1653 UInt32 fileIndex,
1651 UInt32 *blockIndex, 1654 UInt32 *blockIndex,
1652 Byte **tempBuf, 1655 Byte **tempBuf,
@@ -1656,7 +1659,7 @@ SRes SzArEx_Extract(
1656 ISzAllocPtr allocMain, 1659 ISzAllocPtr allocMain,
1657 ISzAllocPtr allocTemp) 1660 ISzAllocPtr allocTemp)
1658{ 1661{
1659 UInt32 folderIndex = p->FileToFolder[fileIndex]; 1662 const UInt32 folderIndex = p->FileToFolder[fileIndex];
1660 SRes res = SZ_OK; 1663 SRes res = SZ_OK;
1661 1664
1662 *offset = 0; 1665 *offset = 0;
@@ -1673,13 +1676,13 @@ SRes SzArEx_Extract(
1673 1676
1674 if (*tempBuf == NULL || *blockIndex != folderIndex) 1677 if (*tempBuf == NULL || *blockIndex != folderIndex)
1675 { 1678 {
1676 UInt64 unpackSizeSpec = SzAr_GetFolderUnpackSize(&p->db, folderIndex); 1679 const UInt64 unpackSizeSpec = SzAr_GetFolderUnpackSize(&p->db, folderIndex);
1677 /* 1680 /*
1678 UInt64 unpackSizeSpec = 1681 UInt64 unpackSizeSpec =
1679 p->UnpackPositions[p->FolderToFile[(size_t)folderIndex + 1]] - 1682 p->UnpackPositions[p->FolderToFile[(size_t)folderIndex + 1]] -
1680 p->UnpackPositions[p->FolderToFile[folderIndex]]; 1683 p->UnpackPositions[p->FolderToFile[folderIndex]];
1681 */ 1684 */
1682 size_t unpackSize = (size_t)unpackSizeSpec; 1685 const size_t unpackSize = (size_t)unpackSizeSpec;
1683 1686
1684 if (unpackSize != unpackSizeSpec) 1687 if (unpackSize != unpackSizeSpec)
1685 return SZ_ERROR_MEM; 1688 return SZ_ERROR_MEM;
@@ -1707,7 +1710,7 @@ SRes SzArEx_Extract(
1707 1710
1708 if (res == SZ_OK) 1711 if (res == SZ_OK)
1709 { 1712 {
1710 UInt64 unpackPos = p->UnpackPositions[fileIndex]; 1713 const UInt64 unpackPos = p->UnpackPositions[fileIndex];
1711 *offset = (size_t)(unpackPos - p->UnpackPositions[p->FolderToFile[folderIndex]]); 1714 *offset = (size_t)(unpackPos - p->UnpackPositions[p->FolderToFile[folderIndex]]);
1712 *outSizeProcessed = (size_t)(p->UnpackPositions[(size_t)fileIndex + 1] - unpackPos); 1715 *outSizeProcessed = (size_t)(p->UnpackPositions[(size_t)fileIndex + 1] - unpackPos);
1713 if (*offset + *outSizeProcessed > *outBufferSize) 1716 if (*offset + *outSizeProcessed > *outBufferSize)
@@ -1723,8 +1726,8 @@ SRes SzArEx_Extract(
1723 1726
1724size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest) 1727size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest)
1725{ 1728{
1726 size_t offs = p->FileNameOffsets[fileIndex]; 1729 const size_t offs = p->FileNameOffsets[fileIndex];
1727 size_t len = p->FileNameOffsets[fileIndex + 1] - offs; 1730 const size_t len = p->FileNameOffsets[fileIndex + 1] - offs;
1728 if (dest != 0) 1731 if (dest != 0)
1729 { 1732 {
1730 size_t i; 1733 size_t i;
diff --git a/C/7zBuf.h b/C/7zBuf.h
index 81d1b5b..c0ba8a7 100644
--- a/C/7zBuf.h
+++ b/C/7zBuf.h
@@ -1,8 +1,8 @@
1/* 7zBuf.h -- Byte Buffer 1/* 7zBuf.h -- Byte Buffer
22017-04-03 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_BUF_H 4#ifndef ZIP7_INC_7Z_BUF_H
5#define __7Z_BUF_H 5#define ZIP7_INC_7Z_BUF_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
diff --git a/C/7zCrc.c b/C/7zCrc.c
index f186324..c995a8b 100644
--- a/C/7zCrc.c
+++ b/C/7zCrc.c
@@ -1,5 +1,5 @@
1/* 7zCrc.c -- CRC32 init 1/* 7zCrc.c -- CRC32 calculation and init
22021-04-01 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -13,22 +13,20 @@
13#else 13#else
14 #define CRC_NUM_TABLES 9 14 #define CRC_NUM_TABLES 9
15 15
16 #define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | (v << 24)) 16 UInt32 Z7_FASTCALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
17 17 UInt32 Z7_FASTCALL CrcUpdateT1_BeT8(UInt32 v, const void *data, size_t size, const UInt32 *table);
18 UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
19 UInt32 MY_FAST_CALL CrcUpdateT1_BeT8(UInt32 v, const void *data, size_t size, const UInt32 *table);
20#endif 18#endif
21 19
22#ifndef MY_CPU_BE 20#ifndef MY_CPU_BE
23 UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table); 21 UInt32 Z7_FASTCALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
24 UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table); 22 UInt32 Z7_FASTCALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table);
25#endif 23#endif
26 24
27typedef UInt32 (MY_FAST_CALL *CRC_FUNC)(UInt32 v, const void *data, size_t size, const UInt32 *table); 25/*
28
29extern 26extern
30CRC_FUNC g_CrcUpdateT4; 27CRC_FUNC g_CrcUpdateT4;
31CRC_FUNC g_CrcUpdateT4; 28CRC_FUNC g_CrcUpdateT4;
29*/
32extern 30extern
33CRC_FUNC g_CrcUpdateT8; 31CRC_FUNC g_CrcUpdateT8;
34CRC_FUNC g_CrcUpdateT8; 32CRC_FUNC g_CrcUpdateT8;
@@ -44,20 +42,22 @@ CRC_FUNC g_CrcUpdate;
44 42
45UInt32 g_CrcTable[256 * CRC_NUM_TABLES]; 43UInt32 g_CrcTable[256 * CRC_NUM_TABLES];
46 44
47UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size) 45UInt32 Z7_FASTCALL CrcUpdate(UInt32 v, const void *data, size_t size)
48{ 46{
49 return g_CrcUpdate(v, data, size, g_CrcTable); 47 return g_CrcUpdate(v, data, size, g_CrcTable);
50} 48}
51 49
52UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size) 50UInt32 Z7_FASTCALL CrcCalc(const void *data, size_t size)
53{ 51{
54 return g_CrcUpdate(CRC_INIT_VAL, data, size, g_CrcTable) ^ CRC_INIT_VAL; 52 return g_CrcUpdate(CRC_INIT_VAL, data, size, g_CrcTable) ^ CRC_INIT_VAL;
55} 53}
56 54
55#if CRC_NUM_TABLES < 4 \
56 || (CRC_NUM_TABLES == 4 && defined(MY_CPU_BE)) \
57 || (!defined(MY_CPU_LE) && !defined(MY_CPU_BE))
57#define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) 58#define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
58 59UInt32 Z7_FASTCALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const UInt32 *table);
59UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const UInt32 *table); 60UInt32 Z7_FASTCALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const UInt32 *table)
60UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const UInt32 *table)
61{ 61{
62 const Byte *p = (const Byte *)data; 62 const Byte *p = (const Byte *)data;
63 const Byte *pEnd = p + size; 63 const Byte *pEnd = p + size;
@@ -65,7 +65,7 @@ UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const U
65 v = CRC_UPDATE_BYTE_2(v, *p); 65 v = CRC_UPDATE_BYTE_2(v, *p);
66 return v; 66 return v;
67} 67}
68 68#endif
69 69
70/* ---------- hardware CRC ---------- */ 70/* ---------- hardware CRC ---------- */
71 71
@@ -78,16 +78,29 @@ UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const U
78 #if defined(_MSC_VER) 78 #if defined(_MSC_VER)
79 #if defined(MY_CPU_ARM64) 79 #if defined(MY_CPU_ARM64)
80 #if (_MSC_VER >= 1910) 80 #if (_MSC_VER >= 1910)
81 #ifndef __clang__
81 #define USE_ARM64_CRC 82 #define USE_ARM64_CRC
83 #include <intrin.h>
84 #endif
82 #endif 85 #endif
83 #endif 86 #endif
84 #elif (defined(__clang__) && (__clang_major__ >= 3)) \ 87 #elif (defined(__clang__) && (__clang_major__ >= 3)) \
85 || (defined(__GNUC__) && (__GNUC__ > 4)) 88 || (defined(__GNUC__) && (__GNUC__ > 4))
86 #if !defined(__ARM_FEATURE_CRC32) 89 #if !defined(__ARM_FEATURE_CRC32)
87 #define __ARM_FEATURE_CRC32 1 90 #define __ARM_FEATURE_CRC32 1
88 #if (!defined(__clang__) || (__clang_major__ > 3)) // fix these numbers 91 #if defined(__clang__)
92 #if defined(MY_CPU_ARM64)
93 #define ATTRIB_CRC __attribute__((__target__("crc")))
94 #else
95 #define ATTRIB_CRC __attribute__((__target__("armv8-a,crc")))
96 #endif
97 #else
98 #if defined(MY_CPU_ARM64)
99 #define ATTRIB_CRC __attribute__((__target__("+crc")))
100 #else
89 #define ATTRIB_CRC __attribute__((__target__("arch=armv8-a+crc"))) 101 #define ATTRIB_CRC __attribute__((__target__("arch=armv8-a+crc")))
90 #endif 102 #endif
103 #endif
91 #endif 104 #endif
92 #if defined(__ARM_FEATURE_CRC32) 105 #if defined(__ARM_FEATURE_CRC32)
93 #define USE_ARM64_CRC 106 #define USE_ARM64_CRC
@@ -105,7 +118,7 @@ UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const U
105 118
106#pragma message("ARM64 CRC emulation") 119#pragma message("ARM64 CRC emulation")
107 120
108MY_FORCE_INLINE 121Z7_FORCE_INLINE
109UInt32 __crc32b(UInt32 v, UInt32 data) 122UInt32 __crc32b(UInt32 v, UInt32 data)
110{ 123{
111 const UInt32 *table = g_CrcTable; 124 const UInt32 *table = g_CrcTable;
@@ -113,7 +126,7 @@ UInt32 __crc32b(UInt32 v, UInt32 data)
113 return v; 126 return v;
114} 127}
115 128
116MY_FORCE_INLINE 129Z7_FORCE_INLINE
117UInt32 __crc32w(UInt32 v, UInt32 data) 130UInt32 __crc32w(UInt32 v, UInt32 data)
118{ 131{
119 const UInt32 *table = g_CrcTable; 132 const UInt32 *table = g_CrcTable;
@@ -124,7 +137,7 @@ UInt32 __crc32w(UInt32 v, UInt32 data)
124 return v; 137 return v;
125} 138}
126 139
127MY_FORCE_INLINE 140Z7_FORCE_INLINE
128UInt32 __crc32d(UInt32 v, UInt64 data) 141UInt32 __crc32d(UInt32 v, UInt64 data)
129{ 142{
130 const UInt32 *table = g_CrcTable; 143 const UInt32 *table = g_CrcTable;
@@ -156,9 +169,9 @@ UInt32 __crc32d(UInt32 v, UInt64 data)
156// #pragma message("USE ARM HW CRC") 169// #pragma message("USE ARM HW CRC")
157 170
158ATTRIB_CRC 171ATTRIB_CRC
159UInt32 MY_FAST_CALL CrcUpdateT0_32(UInt32 v, const void *data, size_t size, const UInt32 *table); 172UInt32 Z7_FASTCALL CrcUpdateT0_32(UInt32 v, const void *data, size_t size, const UInt32 *table);
160ATTRIB_CRC 173ATTRIB_CRC
161UInt32 MY_FAST_CALL CrcUpdateT0_32(UInt32 v, const void *data, size_t size, const UInt32 *table) 174UInt32 Z7_FASTCALL CrcUpdateT0_32(UInt32 v, const void *data, size_t size, const UInt32 *table)
162{ 175{
163 const Byte *p = (const Byte *)data; 176 const Byte *p = (const Byte *)data;
164 UNUSED_VAR(table); 177 UNUSED_VAR(table);
@@ -188,9 +201,9 @@ UInt32 MY_FAST_CALL CrcUpdateT0_32(UInt32 v, const void *data, size_t size, cons
188} 201}
189 202
190ATTRIB_CRC 203ATTRIB_CRC
191UInt32 MY_FAST_CALL CrcUpdateT0_64(UInt32 v, const void *data, size_t size, const UInt32 *table); 204UInt32 Z7_FASTCALL CrcUpdateT0_64(UInt32 v, const void *data, size_t size, const UInt32 *table);
192ATTRIB_CRC 205ATTRIB_CRC
193UInt32 MY_FAST_CALL CrcUpdateT0_64(UInt32 v, const void *data, size_t size, const UInt32 *table) 206UInt32 Z7_FASTCALL CrcUpdateT0_64(UInt32 v, const void *data, size_t size, const UInt32 *table)
194{ 207{
195 const Byte *p = (const Byte *)data; 208 const Byte *p = (const Byte *)data;
196 UNUSED_VAR(table); 209 UNUSED_VAR(table);
@@ -219,6 +232,9 @@ UInt32 MY_FAST_CALL CrcUpdateT0_64(UInt32 v, const void *data, size_t size, cons
219 return v; 232 return v;
220} 233}
221 234
235#undef T0_32_UNROLL_BYTES
236#undef T0_64_UNROLL_BYTES
237
222#endif // defined(USE_ARM64_CRC) || defined(USE_CRC_EMU) 238#endif // defined(USE_ARM64_CRC) || defined(USE_CRC_EMU)
223 239
224#endif // MY_CPU_LE 240#endif // MY_CPU_LE
@@ -226,7 +242,7 @@ UInt32 MY_FAST_CALL CrcUpdateT0_64(UInt32 v, const void *data, size_t size, cons
226 242
227 243
228 244
229void MY_FAST_CALL CrcGenerateTable() 245void Z7_FASTCALL CrcGenerateTable(void)
230{ 246{
231 UInt32 i; 247 UInt32 i;
232 for (i = 0; i < 256; i++) 248 for (i = 0; i < 256; i++)
@@ -239,64 +255,62 @@ void MY_FAST_CALL CrcGenerateTable()
239 } 255 }
240 for (i = 256; i < 256 * CRC_NUM_TABLES; i++) 256 for (i = 256; i < 256 * CRC_NUM_TABLES; i++)
241 { 257 {
242 UInt32 r = g_CrcTable[(size_t)i - 256]; 258 const UInt32 r = g_CrcTable[(size_t)i - 256];
243 g_CrcTable[i] = g_CrcTable[r & 0xFF] ^ (r >> 8); 259 g_CrcTable[i] = g_CrcTable[r & 0xFF] ^ (r >> 8);
244 } 260 }
245 261
246 #if CRC_NUM_TABLES < 4 262 #if CRC_NUM_TABLES < 4
247 263 g_CrcUpdate = CrcUpdateT1;
248 g_CrcUpdate = CrcUpdateT1; 264 #elif defined(MY_CPU_LE)
249 265 // g_CrcUpdateT4 = CrcUpdateT4;
250 #else 266 #if CRC_NUM_TABLES < 8
251 267 g_CrcUpdate = CrcUpdateT4;
252 #ifdef MY_CPU_LE 268 #else // CRC_NUM_TABLES >= 8
253
254 g_CrcUpdateT4 = CrcUpdateT4;
255 g_CrcUpdate = CrcUpdateT4;
256
257 #if CRC_NUM_TABLES >= 8
258 g_CrcUpdateT8 = CrcUpdateT8; 269 g_CrcUpdateT8 = CrcUpdateT8;
259 270 /*
260 #ifdef MY_CPU_X86_OR_AMD64 271 #ifdef MY_CPU_X86_OR_AMD64
261 if (!CPU_Is_InOrder()) 272 if (!CPU_Is_InOrder())
262 #endif 273 #endif
263 g_CrcUpdate = CrcUpdateT8; 274 */
275 g_CrcUpdate = CrcUpdateT8;
264 #endif 276 #endif
265
266 #else 277 #else
267 { 278 {
268 #ifndef MY_CPU_BE 279 #ifndef MY_CPU_BE
269 UInt32 k = 0x01020304; 280 UInt32 k = 0x01020304;
270 const Byte *p = (const Byte *)&k; 281 const Byte *p = (const Byte *)&k;
271 if (p[0] == 4 && p[1] == 3) 282 if (p[0] == 4 && p[1] == 3)
272 { 283 {
273 g_CrcUpdateT4 = CrcUpdateT4; 284 #if CRC_NUM_TABLES < 8
274 g_CrcUpdate = CrcUpdateT4; 285 // g_CrcUpdateT4 = CrcUpdateT4;
275 #if CRC_NUM_TABLES >= 8 286 g_CrcUpdate = CrcUpdateT4;
276 g_CrcUpdateT8 = CrcUpdateT8; 287 #else // CRC_NUM_TABLES >= 8
277 g_CrcUpdate = CrcUpdateT8; 288 g_CrcUpdateT8 = CrcUpdateT8;
289 g_CrcUpdate = CrcUpdateT8;
278 #endif 290 #endif
279 } 291 }
280 else if (p[0] != 1 || p[1] != 2) 292 else if (p[0] != 1 || p[1] != 2)
281 g_CrcUpdate = CrcUpdateT1; 293 g_CrcUpdate = CrcUpdateT1;
282 else 294 else
283 #endif 295 #endif // MY_CPU_BE
284 { 296 {
285 for (i = 256 * CRC_NUM_TABLES - 1; i >= 256; i--) 297 for (i = 256 * CRC_NUM_TABLES - 1; i >= 256; i--)
286 { 298 {
287 UInt32 x = g_CrcTable[(size_t)i - 256]; 299 const UInt32 x = g_CrcTable[(size_t)i - 256];
288 g_CrcTable[i] = CRC_UINT32_SWAP(x); 300 g_CrcTable[i] = Z7_BSWAP32(x);
289 } 301 }
290 g_CrcUpdateT4 = CrcUpdateT1_BeT4; 302 #if CRC_NUM_TABLES <= 4
291 g_CrcUpdate = CrcUpdateT1_BeT4; 303 g_CrcUpdate = CrcUpdateT1;
292 #if CRC_NUM_TABLES >= 8 304 #elif CRC_NUM_TABLES <= 8
293 g_CrcUpdateT8 = CrcUpdateT1_BeT8; 305 // g_CrcUpdateT4 = CrcUpdateT1_BeT4;
294 g_CrcUpdate = CrcUpdateT1_BeT8; 306 g_CrcUpdate = CrcUpdateT1_BeT4;
307 #else // CRC_NUM_TABLES > 8
308 g_CrcUpdateT8 = CrcUpdateT1_BeT8;
309 g_CrcUpdate = CrcUpdateT1_BeT8;
295 #endif 310 #endif
296 } 311 }
297 } 312 }
298 #endif 313 #endif // CRC_NUM_TABLES < 4
299 #endif
300 314
301 #ifdef MY_CPU_LE 315 #ifdef MY_CPU_LE
302 #ifdef USE_ARM64_CRC 316 #ifdef USE_ARM64_CRC
@@ -320,3 +334,7 @@ void MY_FAST_CALL CrcGenerateTable()
320 #endif 334 #endif
321 #endif 335 #endif
322} 336}
337
338#undef kCrcPoly
339#undef CRC64_NUM_TABLES
340#undef CRC_UPDATE_BYTE_2
diff --git a/C/7zCrc.h b/C/7zCrc.h
index 8fd5795..4afaeae 100644
--- a/C/7zCrc.h
+++ b/C/7zCrc.h
@@ -1,8 +1,8 @@
1/* 7zCrc.h -- CRC32 calculation 1/* 7zCrc.h -- CRC32 calculation
22013-01-18 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_CRC_H 4#ifndef ZIP7_INC_7Z_CRC_H
5#define __7Z_CRC_H 5#define ZIP7_INC_7Z_CRC_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -11,14 +11,16 @@ EXTERN_C_BEGIN
11extern UInt32 g_CrcTable[]; 11extern UInt32 g_CrcTable[];
12 12
13/* Call CrcGenerateTable one time before other CRC functions */ 13/* Call CrcGenerateTable one time before other CRC functions */
14void MY_FAST_CALL CrcGenerateTable(void); 14void Z7_FASTCALL CrcGenerateTable(void);
15 15
16#define CRC_INIT_VAL 0xFFFFFFFF 16#define CRC_INIT_VAL 0xFFFFFFFF
17#define CRC_GET_DIGEST(crc) ((crc) ^ CRC_INIT_VAL) 17#define CRC_GET_DIGEST(crc) ((crc) ^ CRC_INIT_VAL)
18#define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) 18#define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
19 19
20UInt32 MY_FAST_CALL CrcUpdate(UInt32 crc, const void *data, size_t size); 20UInt32 Z7_FASTCALL CrcUpdate(UInt32 crc, const void *data, size_t size);
21UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size); 21UInt32 Z7_FASTCALL CrcCalc(const void *data, size_t size);
22
23typedef UInt32 (Z7_FASTCALL *CRC_FUNC)(UInt32 v, const void *data, size_t size, const UInt32 *table);
22 24
23EXTERN_C_END 25EXTERN_C_END
24 26
diff --git a/C/7zCrcOpt.c b/C/7zCrcOpt.c
index 69fad9c..9c64929 100644
--- a/C/7zCrcOpt.c
+++ b/C/7zCrcOpt.c
@@ -1,5 +1,5 @@
1/* 7zCrcOpt.c -- CRC32 calculation 1/* 7zCrcOpt.c -- CRC32 calculation
22021-02-09 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -9,8 +9,8 @@
9 9
10#define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) 10#define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
11 11
12UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table); 12UInt32 Z7_FASTCALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
13UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table) 13UInt32 Z7_FASTCALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table)
14{ 14{
15 const Byte *p = (const Byte *)data; 15 const Byte *p = (const Byte *)data;
16 for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++) 16 for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
@@ -29,8 +29,8 @@ UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const U
29 return v; 29 return v;
30} 30}
31 31
32UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table); 32UInt32 Z7_FASTCALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table);
33UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table) 33UInt32 Z7_FASTCALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table)
34{ 34{
35 const Byte *p = (const Byte *)data; 35 const Byte *p = (const Byte *)data;
36 for (; size > 0 && ((unsigned)(ptrdiff_t)p & 7) != 0; size--, p++) 36 for (; size > 0 && ((unsigned)(ptrdiff_t)p & 7) != 0; size--, p++)
@@ -61,11 +61,11 @@ UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const U
61 61
62#ifndef MY_CPU_LE 62#ifndef MY_CPU_LE
63 63
64#define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | (v << 24)) 64#define CRC_UINT32_SWAP(v) Z7_BSWAP32(v)
65 65
66#define CRC_UPDATE_BYTE_2_BE(crc, b) (table[(((crc) >> 24) ^ (b))] ^ ((crc) << 8)) 66#define CRC_UPDATE_BYTE_2_BE(crc, b) (table[(((crc) >> 24) ^ (b))] ^ ((crc) << 8))
67 67
68UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table) 68UInt32 Z7_FASTCALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table)
69{ 69{
70 const Byte *p = (const Byte *)data; 70 const Byte *p = (const Byte *)data;
71 table += 0x100; 71 table += 0x100;
@@ -86,7 +86,7 @@ UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, co
86 return CRC_UINT32_SWAP(v); 86 return CRC_UINT32_SWAP(v);
87} 87}
88 88
89UInt32 MY_FAST_CALL CrcUpdateT1_BeT8(UInt32 v, const void *data, size_t size, const UInt32 *table) 89UInt32 Z7_FASTCALL CrcUpdateT1_BeT8(UInt32 v, const void *data, size_t size, const UInt32 *table)
90{ 90{
91 const Byte *p = (const Byte *)data; 91 const Byte *p = (const Byte *)data;
92 table += 0x100; 92 table += 0x100;
diff --git a/C/7zDec.c b/C/7zDec.c
index fbfd016..96c6035 100644
--- a/C/7zDec.c
+++ b/C/7zDec.c
@@ -1,11 +1,11 @@
1/* 7zDec.c -- Decoding from 7z folder 1/* 7zDec.c -- Decoding from 7z folder
22021-02-09 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#include <string.h> 6#include <string.h>
7 7
8/* #define _7ZIP_PPMD_SUPPPORT */ 8/* #define Z7_PPMD_SUPPORT */
9 9
10#include "7z.h" 10#include "7z.h"
11#include "7zCrc.h" 11#include "7zCrc.h"
@@ -16,27 +16,49 @@
16#include "Delta.h" 16#include "Delta.h"
17#include "LzmaDec.h" 17#include "LzmaDec.h"
18#include "Lzma2Dec.h" 18#include "Lzma2Dec.h"
19#ifdef _7ZIP_PPMD_SUPPPORT 19#ifdef Z7_PPMD_SUPPORT
20#include "Ppmd7.h" 20#include "Ppmd7.h"
21#endif 21#endif
22 22
23#define k_Copy 0 23#define k_Copy 0
24#ifndef _7Z_NO_METHOD_LZMA2 24#ifndef Z7_NO_METHOD_LZMA2
25#define k_LZMA2 0x21 25#define k_LZMA2 0x21
26#endif 26#endif
27#define k_LZMA 0x30101 27#define k_LZMA 0x30101
28#define k_BCJ2 0x303011B 28#define k_BCJ2 0x303011B
29#ifndef _7Z_NO_METHODS_FILTERS 29
30#if !defined(Z7_NO_METHODS_FILTERS)
31#define Z7_USE_BRANCH_FILTER
32#endif
33
34#if !defined(Z7_NO_METHODS_FILTERS) || \
35 defined(Z7_USE_NATIVE_BRANCH_FILTER) && defined(MY_CPU_ARM64)
36#define Z7_USE_FILTER_ARM64
37#ifndef Z7_USE_BRANCH_FILTER
38#define Z7_USE_BRANCH_FILTER
39#endif
40#define k_ARM64 0xa
41#endif
42
43#if !defined(Z7_NO_METHODS_FILTERS) || \
44 defined(Z7_USE_NATIVE_BRANCH_FILTER) && defined(MY_CPU_ARMT)
45#define Z7_USE_FILTER_ARMT
46#ifndef Z7_USE_BRANCH_FILTER
47#define Z7_USE_BRANCH_FILTER
48#endif
49#define k_ARMT 0x3030701
50#endif
51
52#ifndef Z7_NO_METHODS_FILTERS
30#define k_Delta 3 53#define k_Delta 3
31#define k_BCJ 0x3030103 54#define k_BCJ 0x3030103
32#define k_PPC 0x3030205 55#define k_PPC 0x3030205
33#define k_IA64 0x3030401 56#define k_IA64 0x3030401
34#define k_ARM 0x3030501 57#define k_ARM 0x3030501
35#define k_ARMT 0x3030701
36#define k_SPARC 0x3030805 58#define k_SPARC 0x3030805
37#endif 59#endif
38 60
39#ifdef _7ZIP_PPMD_SUPPPORT 61#ifdef Z7_PPMD_SUPPORT
40 62
41#define k_PPMD 0x30401 63#define k_PPMD 0x30401
42 64
@@ -49,12 +71,12 @@ typedef struct
49 UInt64 processed; 71 UInt64 processed;
50 BoolInt extra; 72 BoolInt extra;
51 SRes res; 73 SRes res;
52 const ILookInStream *inStream; 74 ILookInStreamPtr inStream;
53} CByteInToLook; 75} CByteInToLook;
54 76
55static Byte ReadByte(const IByteIn *pp) 77static Byte ReadByte(IByteInPtr pp)
56{ 78{
57 CByteInToLook *p = CONTAINER_FROM_VTBL(pp, CByteInToLook, vt); 79 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CByteInToLook)
58 if (p->cur != p->end) 80 if (p->cur != p->end)
59 return *p->cur++; 81 return *p->cur++;
60 if (p->res == SZ_OK) 82 if (p->res == SZ_OK)
@@ -67,13 +89,13 @@ static Byte ReadByte(const IByteIn *pp)
67 p->cur = p->begin; 89 p->cur = p->begin;
68 p->end = p->begin + size; 90 p->end = p->begin + size;
69 if (size != 0) 91 if (size != 0)
70 return *p->cur++;; 92 return *p->cur++;
71 } 93 }
72 p->extra = True; 94 p->extra = True;
73 return 0; 95 return 0;
74} 96}
75 97
76static SRes SzDecodePpmd(const Byte *props, unsigned propsSize, UInt64 inSize, const ILookInStream *inStream, 98static SRes SzDecodePpmd(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStreamPtr inStream,
77 Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain) 99 Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain)
78{ 100{
79 CPpmd7 ppmd; 101 CPpmd7 ppmd;
@@ -138,14 +160,14 @@ static SRes SzDecodePpmd(const Byte *props, unsigned propsSize, UInt64 inSize, c
138#endif 160#endif
139 161
140 162
141static SRes SzDecodeLzma(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStream *inStream, 163static SRes SzDecodeLzma(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStreamPtr inStream,
142 Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain) 164 Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain)
143{ 165{
144 CLzmaDec state; 166 CLzmaDec state;
145 SRes res = SZ_OK; 167 SRes res = SZ_OK;
146 168
147 LzmaDec_Construct(&state); 169 LzmaDec_CONSTRUCT(&state)
148 RINOK(LzmaDec_AllocateProbs(&state, props, propsSize, allocMain)); 170 RINOK(LzmaDec_AllocateProbs(&state, props, propsSize, allocMain))
149 state.dic = outBuffer; 171 state.dic = outBuffer;
150 state.dicBufSize = outSize; 172 state.dicBufSize = outSize;
151 LzmaDec_Init(&state); 173 LzmaDec_Init(&state);
@@ -196,18 +218,18 @@ static SRes SzDecodeLzma(const Byte *props, unsigned propsSize, UInt64 inSize, I
196} 218}
197 219
198 220
199#ifndef _7Z_NO_METHOD_LZMA2 221#ifndef Z7_NO_METHOD_LZMA2
200 222
201static SRes SzDecodeLzma2(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStream *inStream, 223static SRes SzDecodeLzma2(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStreamPtr inStream,
202 Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain) 224 Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain)
203{ 225{
204 CLzma2Dec state; 226 CLzma2Dec state;
205 SRes res = SZ_OK; 227 SRes res = SZ_OK;
206 228
207 Lzma2Dec_Construct(&state); 229 Lzma2Dec_CONSTRUCT(&state)
208 if (propsSize != 1) 230 if (propsSize != 1)
209 return SZ_ERROR_DATA; 231 return SZ_ERROR_DATA;
210 RINOK(Lzma2Dec_AllocateProbs(&state, props[0], allocMain)); 232 RINOK(Lzma2Dec_AllocateProbs(&state, props[0], allocMain))
211 state.decoder.dic = outBuffer; 233 state.decoder.dic = outBuffer;
212 state.decoder.dicBufSize = outSize; 234 state.decoder.dicBufSize = outSize;
213 Lzma2Dec_Init(&state); 235 Lzma2Dec_Init(&state);
@@ -257,7 +279,7 @@ static SRes SzDecodeLzma2(const Byte *props, unsigned propsSize, UInt64 inSize,
257#endif 279#endif
258 280
259 281
260static SRes SzDecodeCopy(UInt64 inSize, ILookInStream *inStream, Byte *outBuffer) 282static SRes SzDecodeCopy(UInt64 inSize, ILookInStreamPtr inStream, Byte *outBuffer)
261{ 283{
262 while (inSize > 0) 284 while (inSize > 0)
263 { 285 {
@@ -265,13 +287,13 @@ static SRes SzDecodeCopy(UInt64 inSize, ILookInStream *inStream, Byte *outBuffer
265 size_t curSize = (1 << 18); 287 size_t curSize = (1 << 18);
266 if (curSize > inSize) 288 if (curSize > inSize)
267 curSize = (size_t)inSize; 289 curSize = (size_t)inSize;
268 RINOK(ILookInStream_Look(inStream, &inBuf, &curSize)); 290 RINOK(ILookInStream_Look(inStream, &inBuf, &curSize))
269 if (curSize == 0) 291 if (curSize == 0)
270 return SZ_ERROR_INPUT_EOF; 292 return SZ_ERROR_INPUT_EOF;
271 memcpy(outBuffer, inBuf, curSize); 293 memcpy(outBuffer, inBuf, curSize);
272 outBuffer += curSize; 294 outBuffer += curSize;
273 inSize -= curSize; 295 inSize -= curSize;
274 RINOK(ILookInStream_Skip(inStream, curSize)); 296 RINOK(ILookInStream_Skip(inStream, curSize))
275 } 297 }
276 return SZ_OK; 298 return SZ_OK;
277} 299}
@@ -282,12 +304,12 @@ static BoolInt IS_MAIN_METHOD(UInt32 m)
282 { 304 {
283 case k_Copy: 305 case k_Copy:
284 case k_LZMA: 306 case k_LZMA:
285 #ifndef _7Z_NO_METHOD_LZMA2 307 #ifndef Z7_NO_METHOD_LZMA2
286 case k_LZMA2: 308 case k_LZMA2:
287 #endif 309 #endif
288 #ifdef _7ZIP_PPMD_SUPPPORT 310 #ifdef Z7_PPMD_SUPPORT
289 case k_PPMD: 311 case k_PPMD:
290 #endif 312 #endif
291 return True; 313 return True;
292 } 314 }
293 return False; 315 return False;
@@ -317,7 +339,7 @@ static SRes CheckSupportedFolder(const CSzFolder *f)
317 } 339 }
318 340
319 341
320 #ifndef _7Z_NO_METHODS_FILTERS 342 #if defined(Z7_USE_BRANCH_FILTER)
321 343
322 if (f->NumCoders == 2) 344 if (f->NumCoders == 2)
323 { 345 {
@@ -333,13 +355,20 @@ static SRes CheckSupportedFolder(const CSzFolder *f)
333 return SZ_ERROR_UNSUPPORTED; 355 return SZ_ERROR_UNSUPPORTED;
334 switch ((UInt32)c->MethodID) 356 switch ((UInt32)c->MethodID)
335 { 357 {
358 #if !defined(Z7_NO_METHODS_FILTERS)
336 case k_Delta: 359 case k_Delta:
337 case k_BCJ: 360 case k_BCJ:
338 case k_PPC: 361 case k_PPC:
339 case k_IA64: 362 case k_IA64:
340 case k_SPARC: 363 case k_SPARC:
341 case k_ARM: 364 case k_ARM:
365 #endif
366 #ifdef Z7_USE_FILTER_ARM64
367 case k_ARM64:
368 #endif
369 #ifdef Z7_USE_FILTER_ARMT
342 case k_ARMT: 370 case k_ARMT:
371 #endif
343 break; 372 break;
344 default: 373 default:
345 return SZ_ERROR_UNSUPPORTED; 374 return SZ_ERROR_UNSUPPORTED;
@@ -372,15 +401,16 @@ static SRes CheckSupportedFolder(const CSzFolder *f)
372 return SZ_ERROR_UNSUPPORTED; 401 return SZ_ERROR_UNSUPPORTED;
373} 402}
374 403
375#ifndef _7Z_NO_METHODS_FILTERS 404
376#define CASE_BRA_CONV(isa) case k_ ## isa: isa ## _Convert(outBuffer, outSize, 0, 0); break; 405
377#endif 406
407
378 408
379static SRes SzFolder_Decode2(const CSzFolder *folder, 409static SRes SzFolder_Decode2(const CSzFolder *folder,
380 const Byte *propsData, 410 const Byte *propsData,
381 const UInt64 *unpackSizes, 411 const UInt64 *unpackSizes,
382 const UInt64 *packPositions, 412 const UInt64 *packPositions,
383 ILookInStream *inStream, UInt64 startPos, 413 ILookInStreamPtr inStream, UInt64 startPos,
384 Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain, 414 Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain,
385 Byte *tempBuf[]) 415 Byte *tempBuf[])
386{ 416{
@@ -389,7 +419,7 @@ static SRes SzFolder_Decode2(const CSzFolder *folder,
389 SizeT tempSize3 = 0; 419 SizeT tempSize3 = 0;
390 Byte *tempBuf3 = 0; 420 Byte *tempBuf3 = 0;
391 421
392 RINOK(CheckSupportedFolder(folder)); 422 RINOK(CheckSupportedFolder(folder))
393 423
394 for (ci = 0; ci < folder->NumCoders; ci++) 424 for (ci = 0; ci < folder->NumCoders; ci++)
395 { 425 {
@@ -404,8 +434,8 @@ static SRes SzFolder_Decode2(const CSzFolder *folder,
404 SizeT outSizeCur = outSize; 434 SizeT outSizeCur = outSize;
405 if (folder->NumCoders == 4) 435 if (folder->NumCoders == 4)
406 { 436 {
407 UInt32 indices[] = { 3, 2, 0 }; 437 const UInt32 indices[] = { 3, 2, 0 };
408 UInt64 unpackSize = unpackSizes[ci]; 438 const UInt64 unpackSize = unpackSizes[ci];
409 si = indices[ci]; 439 si = indices[ci];
410 if (ci < 2) 440 if (ci < 2)
411 { 441 {
@@ -431,37 +461,37 @@ static SRes SzFolder_Decode2(const CSzFolder *folder,
431 } 461 }
432 offset = packPositions[si]; 462 offset = packPositions[si];
433 inSize = packPositions[(size_t)si + 1] - offset; 463 inSize = packPositions[(size_t)si + 1] - offset;
434 RINOK(LookInStream_SeekTo(inStream, startPos + offset)); 464 RINOK(LookInStream_SeekTo(inStream, startPos + offset))
435 465
436 if (coder->MethodID == k_Copy) 466 if (coder->MethodID == k_Copy)
437 { 467 {
438 if (inSize != outSizeCur) /* check it */ 468 if (inSize != outSizeCur) /* check it */
439 return SZ_ERROR_DATA; 469 return SZ_ERROR_DATA;
440 RINOK(SzDecodeCopy(inSize, inStream, outBufCur)); 470 RINOK(SzDecodeCopy(inSize, inStream, outBufCur))
441 } 471 }
442 else if (coder->MethodID == k_LZMA) 472 else if (coder->MethodID == k_LZMA)
443 { 473 {
444 RINOK(SzDecodeLzma(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain)); 474 RINOK(SzDecodeLzma(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain))
445 } 475 }
446 #ifndef _7Z_NO_METHOD_LZMA2 476 #ifndef Z7_NO_METHOD_LZMA2
447 else if (coder->MethodID == k_LZMA2) 477 else if (coder->MethodID == k_LZMA2)
448 { 478 {
449 RINOK(SzDecodeLzma2(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain)); 479 RINOK(SzDecodeLzma2(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain))
450 } 480 }
451 #endif 481 #endif
452 #ifdef _7ZIP_PPMD_SUPPPORT 482 #ifdef Z7_PPMD_SUPPORT
453 else if (coder->MethodID == k_PPMD) 483 else if (coder->MethodID == k_PPMD)
454 { 484 {
455 RINOK(SzDecodePpmd(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain)); 485 RINOK(SzDecodePpmd(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain))
456 } 486 }
457 #endif 487 #endif
458 else 488 else
459 return SZ_ERROR_UNSUPPORTED; 489 return SZ_ERROR_UNSUPPORTED;
460 } 490 }
461 else if (coder->MethodID == k_BCJ2) 491 else if (coder->MethodID == k_BCJ2)
462 { 492 {
463 UInt64 offset = packPositions[1]; 493 const UInt64 offset = packPositions[1];
464 UInt64 s3Size = packPositions[2] - offset; 494 const UInt64 s3Size = packPositions[2] - offset;
465 495
466 if (ci != 3) 496 if (ci != 3)
467 return SZ_ERROR_UNSUPPORTED; 497 return SZ_ERROR_UNSUPPORTED;
@@ -473,8 +503,8 @@ static SRes SzFolder_Decode2(const CSzFolder *folder,
473 if (!tempBuf[2] && tempSizes[2] != 0) 503 if (!tempBuf[2] && tempSizes[2] != 0)
474 return SZ_ERROR_MEM; 504 return SZ_ERROR_MEM;
475 505
476 RINOK(LookInStream_SeekTo(inStream, startPos + offset)); 506 RINOK(LookInStream_SeekTo(inStream, startPos + offset))
477 RINOK(SzDecodeCopy(s3Size, inStream, tempBuf[2])); 507 RINOK(SzDecodeCopy(s3Size, inStream, tempBuf[2]))
478 508
479 if ((tempSizes[0] & 3) != 0 || 509 if ((tempSizes[0] & 3) != 0 ||
480 (tempSizes[1] & 3) != 0 || 510 (tempSizes[1] & 3) != 0 ||
@@ -493,26 +523,22 @@ static SRes SzFolder_Decode2(const CSzFolder *folder,
493 p.destLim = outBuffer + outSize; 523 p.destLim = outBuffer + outSize;
494 524
495 Bcj2Dec_Init(&p); 525 Bcj2Dec_Init(&p);
496 RINOK(Bcj2Dec_Decode(&p)); 526 RINOK(Bcj2Dec_Decode(&p))
497 527
498 { 528 {
499 unsigned i; 529 unsigned i;
500 for (i = 0; i < 4; i++) 530 for (i = 0; i < 4; i++)
501 if (p.bufs[i] != p.lims[i]) 531 if (p.bufs[i] != p.lims[i])
502 return SZ_ERROR_DATA; 532 return SZ_ERROR_DATA;
503 533 if (p.dest != p.destLim || !Bcj2Dec_IsMaybeFinished(&p))
504 if (!Bcj2Dec_IsFinished(&p))
505 return SZ_ERROR_DATA;
506
507 if (p.dest != p.destLim
508 || p.state != BCJ2_STREAM_MAIN)
509 return SZ_ERROR_DATA; 534 return SZ_ERROR_DATA;
510 } 535 }
511 } 536 }
512 } 537 }
513 #ifndef _7Z_NO_METHODS_FILTERS 538 #if defined(Z7_USE_BRANCH_FILTER)
514 else if (ci == 1) 539 else if (ci == 1)
515 { 540 {
541 #if !defined(Z7_NO_METHODS_FILTERS)
516 if (coder->MethodID == k_Delta) 542 if (coder->MethodID == k_Delta)
517 { 543 {
518 if (coder->PropsSize != 1) 544 if (coder->PropsSize != 1)
@@ -522,31 +548,53 @@ static SRes SzFolder_Decode2(const CSzFolder *folder,
522 Delta_Init(state); 548 Delta_Init(state);
523 Delta_Decode(state, (unsigned)(propsData[coder->PropsOffset]) + 1, outBuffer, outSize); 549 Delta_Decode(state, (unsigned)(propsData[coder->PropsOffset]) + 1, outBuffer, outSize);
524 } 550 }
551 continue;
525 } 552 }
526 else 553 #endif
554
555 #ifdef Z7_USE_FILTER_ARM64
556 if (coder->MethodID == k_ARM64)
557 {
558 UInt32 pc = 0;
559 if (coder->PropsSize == 4)
560 pc = GetUi32(propsData + coder->PropsOffset);
561 else if (coder->PropsSize != 0)
562 return SZ_ERROR_UNSUPPORTED;
563 z7_BranchConv_ARM64_Dec(outBuffer, outSize, pc);
564 continue;
565 }
566 #endif
567
568 #if !defined(Z7_NO_METHODS_FILTERS) || defined(Z7_USE_FILTER_ARMT)
527 { 569 {
528 if (coder->PropsSize != 0) 570 if (coder->PropsSize != 0)
529 return SZ_ERROR_UNSUPPORTED; 571 return SZ_ERROR_UNSUPPORTED;
572 #define CASE_BRA_CONV(isa) case k_ ## isa: Z7_BRANCH_CONV_DEC(isa)(outBuffer, outSize, 0); break; // pc = 0;
530 switch (coder->MethodID) 573 switch (coder->MethodID)
531 { 574 {
575 #if !defined(Z7_NO_METHODS_FILTERS)
532 case k_BCJ: 576 case k_BCJ:
533 { 577 {
534 UInt32 state; 578 UInt32 state = Z7_BRANCH_CONV_ST_X86_STATE_INIT_VAL;
535 x86_Convert_Init(state); 579 z7_BranchConvSt_X86_Dec(outBuffer, outSize, 0, &state); // pc = 0
536 x86_Convert(outBuffer, outSize, 0, &state, 0);
537 break; 580 break;
538 } 581 }
539 CASE_BRA_CONV(PPC) 582 CASE_BRA_CONV(PPC)
540 CASE_BRA_CONV(IA64) 583 CASE_BRA_CONV(IA64)
541 CASE_BRA_CONV(SPARC) 584 CASE_BRA_CONV(SPARC)
542 CASE_BRA_CONV(ARM) 585 CASE_BRA_CONV(ARM)
586 #endif
587 #if !defined(Z7_NO_METHODS_FILTERS) || defined(Z7_USE_FILTER_ARMT)
543 CASE_BRA_CONV(ARMT) 588 CASE_BRA_CONV(ARMT)
589 #endif
544 default: 590 default:
545 return SZ_ERROR_UNSUPPORTED; 591 return SZ_ERROR_UNSUPPORTED;
546 } 592 }
593 continue;
547 } 594 }
548 } 595 #endif
549 #endif 596 } // (c == 1)
597 #endif
550 else 598 else
551 return SZ_ERROR_UNSUPPORTED; 599 return SZ_ERROR_UNSUPPORTED;
552 } 600 }
@@ -556,7 +604,7 @@ static SRes SzFolder_Decode2(const CSzFolder *folder,
556 604
557 605
558SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex, 606SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex,
559 ILookInStream *inStream, UInt64 startPos, 607 ILookInStreamPtr inStream, UInt64 startPos,
560 Byte *outBuffer, size_t outSize, 608 Byte *outBuffer, size_t outSize,
561 ISzAllocPtr allocMain) 609 ISzAllocPtr allocMain)
562{ 610{
diff --git a/C/7zFile.c b/C/7zFile.c
index 13d2efa..ba5daa1 100644
--- a/C/7zFile.c
+++ b/C/7zFile.c
@@ -1,5 +1,5 @@
1/* 7zFile.c -- File IO 1/* 7zFile.c -- File IO
22021-04-29 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -268,7 +268,7 @@ WRes File_Write(CSzFile *p, const void *data, size_t *size)
268 return errno; 268 return errno;
269 if (processed == 0) 269 if (processed == 0)
270 break; 270 break;
271 data = (void *)((Byte *)data + (size_t)processed); 271 data = (const void *)((const Byte *)data + (size_t)processed);
272 originalSize -= (size_t)processed; 272 originalSize -= (size_t)processed;
273 *size += (size_t)processed; 273 *size += (size_t)processed;
274 } 274 }
@@ -287,7 +287,8 @@ WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin)
287 DWORD moveMethod; 287 DWORD moveMethod;
288 UInt32 low = (UInt32)*pos; 288 UInt32 low = (UInt32)*pos;
289 LONG high = (LONG)((UInt64)*pos >> 16 >> 16); /* for case when UInt64 is 32-bit only */ 289 LONG high = (LONG)((UInt64)*pos >> 16 >> 16); /* for case when UInt64 is 32-bit only */
290 switch (origin) 290 // (int) to eliminate clang warning
291 switch ((int)origin)
291 { 292 {
292 case SZ_SEEK_SET: moveMethod = FILE_BEGIN; break; 293 case SZ_SEEK_SET: moveMethod = FILE_BEGIN; break;
293 case SZ_SEEK_CUR: moveMethod = FILE_CURRENT; break; 294 case SZ_SEEK_CUR: moveMethod = FILE_CURRENT; break;
@@ -308,7 +309,7 @@ WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin)
308 309
309 int moveMethod; // = origin; 310 int moveMethod; // = origin;
310 311
311 switch (origin) 312 switch ((int)origin)
312 { 313 {
313 case SZ_SEEK_SET: moveMethod = SEEK_SET; break; 314 case SZ_SEEK_SET: moveMethod = SEEK_SET; break;
314 case SZ_SEEK_CUR: moveMethod = SEEK_CUR; break; 315 case SZ_SEEK_CUR: moveMethod = SEEK_CUR; break;
@@ -387,10 +388,10 @@ WRes File_GetLength(CSzFile *p, UInt64 *length)
387 388
388/* ---------- FileSeqInStream ---------- */ 389/* ---------- FileSeqInStream ---------- */
389 390
390static SRes FileSeqInStream_Read(const ISeqInStream *pp, void *buf, size_t *size) 391static SRes FileSeqInStream_Read(ISeqInStreamPtr pp, void *buf, size_t *size)
391{ 392{
392 CFileSeqInStream *p = CONTAINER_FROM_VTBL(pp, CFileSeqInStream, vt); 393 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CFileSeqInStream)
393 WRes wres = File_Read(&p->file, buf, size); 394 const WRes wres = File_Read(&p->file, buf, size);
394 p->wres = wres; 395 p->wres = wres;
395 return (wres == 0) ? SZ_OK : SZ_ERROR_READ; 396 return (wres == 0) ? SZ_OK : SZ_ERROR_READ;
396} 397}
@@ -403,18 +404,18 @@ void FileSeqInStream_CreateVTable(CFileSeqInStream *p)
403 404
404/* ---------- FileInStream ---------- */ 405/* ---------- FileInStream ---------- */
405 406
406static SRes FileInStream_Read(const ISeekInStream *pp, void *buf, size_t *size) 407static SRes FileInStream_Read(ISeekInStreamPtr pp, void *buf, size_t *size)
407{ 408{
408 CFileInStream *p = CONTAINER_FROM_VTBL(pp, CFileInStream, vt); 409 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CFileInStream)
409 WRes wres = File_Read(&p->file, buf, size); 410 const WRes wres = File_Read(&p->file, buf, size);
410 p->wres = wres; 411 p->wres = wres;
411 return (wres == 0) ? SZ_OK : SZ_ERROR_READ; 412 return (wres == 0) ? SZ_OK : SZ_ERROR_READ;
412} 413}
413 414
414static SRes FileInStream_Seek(const ISeekInStream *pp, Int64 *pos, ESzSeek origin) 415static SRes FileInStream_Seek(ISeekInStreamPtr pp, Int64 *pos, ESzSeek origin)
415{ 416{
416 CFileInStream *p = CONTAINER_FROM_VTBL(pp, CFileInStream, vt); 417 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CFileInStream)
417 WRes wres = File_Seek(&p->file, pos, origin); 418 const WRes wres = File_Seek(&p->file, pos, origin);
418 p->wres = wres; 419 p->wres = wres;
419 return (wres == 0) ? SZ_OK : SZ_ERROR_READ; 420 return (wres == 0) ? SZ_OK : SZ_ERROR_READ;
420} 421}
@@ -428,10 +429,10 @@ void FileInStream_CreateVTable(CFileInStream *p)
428 429
429/* ---------- FileOutStream ---------- */ 430/* ---------- FileOutStream ---------- */
430 431
431static size_t FileOutStream_Write(const ISeqOutStream *pp, const void *data, size_t size) 432static size_t FileOutStream_Write(ISeqOutStreamPtr pp, const void *data, size_t size)
432{ 433{
433 CFileOutStream *p = CONTAINER_FROM_VTBL(pp, CFileOutStream, vt); 434 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CFileOutStream)
434 WRes wres = File_Write(&p->file, data, &size); 435 const WRes wres = File_Write(&p->file, data, &size);
435 p->wres = wres; 436 p->wres = wres;
436 return size; 437 return size;
437} 438}
diff --git a/C/7zFile.h b/C/7zFile.h
index 788abb6..f5069cd 100644
--- a/C/7zFile.h
+++ b/C/7zFile.h
@@ -1,8 +1,8 @@
1/* 7zFile.h -- File IO 1/* 7zFile.h -- File IO
22021-02-15 : Igor Pavlov : Public domain */ 22023-03-05 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_FILE_H 4#ifndef ZIP7_INC_FILE_H
5#define __7Z_FILE_H 5#define ZIP7_INC_FILE_H
6 6
7#ifdef _WIN32 7#ifdef _WIN32
8#define USE_WINDOWS_FILE 8#define USE_WINDOWS_FILE
@@ -10,7 +10,8 @@
10#endif 10#endif
11 11
12#ifdef USE_WINDOWS_FILE 12#ifdef USE_WINDOWS_FILE
13#include <windows.h> 13#include "7zWindows.h"
14
14#else 15#else
15// note: USE_FOPEN mode is limited to 32-bit file size 16// note: USE_FOPEN mode is limited to 32-bit file size
16// #define USE_FOPEN 17// #define USE_FOPEN
diff --git a/C/7zStream.c b/C/7zStream.c
index 28a1460..74e75b6 100644
--- a/C/7zStream.c
+++ b/C/7zStream.c
@@ -1,5 +1,5 @@
1/* 7zStream.c -- 7z Stream functions 1/* 7zStream.c -- 7z Stream functions
22021-02-09 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -7,12 +7,33 @@
7 7
8#include "7zTypes.h" 8#include "7zTypes.h"
9 9
10SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType) 10
11SRes SeqInStream_ReadMax(ISeqInStreamPtr stream, void *buf, size_t *processedSize)
12{
13 size_t size = *processedSize;
14 *processedSize = 0;
15 while (size != 0)
16 {
17 size_t cur = size;
18 const SRes res = ISeqInStream_Read(stream, buf, &cur);
19 *processedSize += cur;
20 buf = (void *)((Byte *)buf + cur);
21 size -= cur;
22 if (res != SZ_OK)
23 return res;
24 if (cur == 0)
25 return SZ_OK;
26 }
27 return SZ_OK;
28}
29
30/*
31SRes SeqInStream_Read2(ISeqInStreamPtr stream, void *buf, size_t size, SRes errorType)
11{ 32{
12 while (size != 0) 33 while (size != 0)
13 { 34 {
14 size_t processed = size; 35 size_t processed = size;
15 RINOK(ISeqInStream_Read(stream, buf, &processed)); 36 RINOK(ISeqInStream_Read(stream, buf, &processed))
16 if (processed == 0) 37 if (processed == 0)
17 return errorType; 38 return errorType;
18 buf = (void *)((Byte *)buf + processed); 39 buf = (void *)((Byte *)buf + processed);
@@ -21,42 +42,44 @@ SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes
21 return SZ_OK; 42 return SZ_OK;
22} 43}
23 44
24SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size) 45SRes SeqInStream_Read(ISeqInStreamPtr stream, void *buf, size_t size)
25{ 46{
26 return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); 47 return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
27} 48}
49*/
50
28 51
29SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf) 52SRes SeqInStream_ReadByte(ISeqInStreamPtr stream, Byte *buf)
30{ 53{
31 size_t processed = 1; 54 size_t processed = 1;
32 RINOK(ISeqInStream_Read(stream, buf, &processed)); 55 RINOK(ISeqInStream_Read(stream, buf, &processed))
33 return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF; 56 return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
34} 57}
35 58
36 59
37 60
38SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset) 61SRes LookInStream_SeekTo(ILookInStreamPtr stream, UInt64 offset)
39{ 62{
40 Int64 t = (Int64)offset; 63 Int64 t = (Int64)offset;
41 return ILookInStream_Seek(stream, &t, SZ_SEEK_SET); 64 return ILookInStream_Seek(stream, &t, SZ_SEEK_SET);
42} 65}
43 66
44SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size) 67SRes LookInStream_LookRead(ILookInStreamPtr stream, void *buf, size_t *size)
45{ 68{
46 const void *lookBuf; 69 const void *lookBuf;
47 if (*size == 0) 70 if (*size == 0)
48 return SZ_OK; 71 return SZ_OK;
49 RINOK(ILookInStream_Look(stream, &lookBuf, size)); 72 RINOK(ILookInStream_Look(stream, &lookBuf, size))
50 memcpy(buf, lookBuf, *size); 73 memcpy(buf, lookBuf, *size);
51 return ILookInStream_Skip(stream, *size); 74 return ILookInStream_Skip(stream, *size);
52} 75}
53 76
54SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType) 77SRes LookInStream_Read2(ILookInStreamPtr stream, void *buf, size_t size, SRes errorType)
55{ 78{
56 while (size != 0) 79 while (size != 0)
57 { 80 {
58 size_t processed = size; 81 size_t processed = size;
59 RINOK(ILookInStream_Read(stream, buf, &processed)); 82 RINOK(ILookInStream_Read(stream, buf, &processed))
60 if (processed == 0) 83 if (processed == 0)
61 return errorType; 84 return errorType;
62 buf = (void *)((Byte *)buf + processed); 85 buf = (void *)((Byte *)buf + processed);
@@ -65,16 +88,16 @@ SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRe
65 return SZ_OK; 88 return SZ_OK;
66} 89}
67 90
68SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size) 91SRes LookInStream_Read(ILookInStreamPtr stream, void *buf, size_t size)
69{ 92{
70 return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); 93 return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
71} 94}
72 95
73 96
74 97
75#define GET_LookToRead2 CLookToRead2 *p = CONTAINER_FROM_VTBL(pp, CLookToRead2, vt); 98#define GET_LookToRead2 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CLookToRead2)
76 99
77static SRes LookToRead2_Look_Lookahead(const ILookInStream *pp, const void **buf, size_t *size) 100static SRes LookToRead2_Look_Lookahead(ILookInStreamPtr pp, const void **buf, size_t *size)
78{ 101{
79 SRes res = SZ_OK; 102 SRes res = SZ_OK;
80 GET_LookToRead2 103 GET_LookToRead2
@@ -93,7 +116,7 @@ static SRes LookToRead2_Look_Lookahead(const ILookInStream *pp, const void **buf
93 return res; 116 return res;
94} 117}
95 118
96static SRes LookToRead2_Look_Exact(const ILookInStream *pp, const void **buf, size_t *size) 119static SRes LookToRead2_Look_Exact(ILookInStreamPtr pp, const void **buf, size_t *size)
97{ 120{
98 SRes res = SZ_OK; 121 SRes res = SZ_OK;
99 GET_LookToRead2 122 GET_LookToRead2
@@ -113,14 +136,14 @@ static SRes LookToRead2_Look_Exact(const ILookInStream *pp, const void **buf, si
113 return res; 136 return res;
114} 137}
115 138
116static SRes LookToRead2_Skip(const ILookInStream *pp, size_t offset) 139static SRes LookToRead2_Skip(ILookInStreamPtr pp, size_t offset)
117{ 140{
118 GET_LookToRead2 141 GET_LookToRead2
119 p->pos += offset; 142 p->pos += offset;
120 return SZ_OK; 143 return SZ_OK;
121} 144}
122 145
123static SRes LookToRead2_Read(const ILookInStream *pp, void *buf, size_t *size) 146static SRes LookToRead2_Read(ILookInStreamPtr pp, void *buf, size_t *size)
124{ 147{
125 GET_LookToRead2 148 GET_LookToRead2
126 size_t rem = p->size - p->pos; 149 size_t rem = p->size - p->pos;
@@ -134,7 +157,7 @@ static SRes LookToRead2_Read(const ILookInStream *pp, void *buf, size_t *size)
134 return SZ_OK; 157 return SZ_OK;
135} 158}
136 159
137static SRes LookToRead2_Seek(const ILookInStream *pp, Int64 *pos, ESzSeek origin) 160static SRes LookToRead2_Seek(ILookInStreamPtr pp, Int64 *pos, ESzSeek origin)
138{ 161{
139 GET_LookToRead2 162 GET_LookToRead2
140 p->pos = p->size = 0; 163 p->pos = p->size = 0;
@@ -153,9 +176,9 @@ void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead)
153 176
154 177
155 178
156static SRes SecToLook_Read(const ISeqInStream *pp, void *buf, size_t *size) 179static SRes SecToLook_Read(ISeqInStreamPtr pp, void *buf, size_t *size)
157{ 180{
158 CSecToLook *p = CONTAINER_FROM_VTBL(pp, CSecToLook, vt); 181 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSecToLook)
159 return LookInStream_LookRead(p->realStream, buf, size); 182 return LookInStream_LookRead(p->realStream, buf, size);
160} 183}
161 184
@@ -164,9 +187,9 @@ void SecToLook_CreateVTable(CSecToLook *p)
164 p->vt.Read = SecToLook_Read; 187 p->vt.Read = SecToLook_Read;
165} 188}
166 189
167static SRes SecToRead_Read(const ISeqInStream *pp, void *buf, size_t *size) 190static SRes SecToRead_Read(ISeqInStreamPtr pp, void *buf, size_t *size)
168{ 191{
169 CSecToRead *p = CONTAINER_FROM_VTBL(pp, CSecToRead, vt); 192 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSecToRead)
170 return ILookInStream_Read(p->realStream, buf, size); 193 return ILookInStream_Read(p->realStream, buf, size);
171} 194}
172 195
diff --git a/C/7zTypes.h b/C/7zTypes.h
index f7d7071..1fcb247 100644
--- a/C/7zTypes.h
+++ b/C/7zTypes.h
@@ -1,8 +1,8 @@
1/* 7zTypes.h -- Basic types 1/* 7zTypes.h -- Basic types
22022-04-01 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_TYPES_H 4#ifndef ZIP7_7Z_TYPES_H
5#define __7Z_TYPES_H 5#define ZIP7_7Z_TYPES_H
6 6
7#ifdef _WIN32 7#ifdef _WIN32
8/* #include <windows.h> */ 8/* #include <windows.h> */
@@ -52,6 +52,11 @@ typedef int SRes;
52 #define MY_ALIGN(n) 52 #define MY_ALIGN(n)
53 #endif 53 #endif
54#else 54#else
55 /*
56 // C11/C++11:
57 #include <stdalign.h>
58 #define MY_ALIGN(n) alignas(n)
59 */
55 #define MY_ALIGN(n) __attribute__ ((aligned(n))) 60 #define MY_ALIGN(n) __attribute__ ((aligned(n)))
56#endif 61#endif
57 62
@@ -62,7 +67,7 @@ typedef int SRes;
62typedef unsigned WRes; 67typedef unsigned WRes;
63#define MY_SRes_HRESULT_FROM_WRes(x) HRESULT_FROM_WIN32(x) 68#define MY_SRes_HRESULT_FROM_WRes(x) HRESULT_FROM_WIN32(x)
64 69
65// #define MY_HRES_ERROR__INTERNAL_ERROR MY_SRes_HRESULT_FROM_WRes(ERROR_INTERNAL_ERROR) 70// #define MY_HRES_ERROR_INTERNAL_ERROR MY_SRes_HRESULT_FROM_WRes(ERROR_INTERNAL_ERROR)
66 71
67#else // _WIN32 72#else // _WIN32
68 73
@@ -70,13 +75,13 @@ typedef unsigned WRes;
70typedef int WRes; 75typedef int WRes;
71 76
72// (FACILITY_ERRNO = 0x800) is 7zip's FACILITY constant to represent (errno) errors in HRESULT 77// (FACILITY_ERRNO = 0x800) is 7zip's FACILITY constant to represent (errno) errors in HRESULT
73#define MY__FACILITY_ERRNO 0x800 78#define MY_FACILITY_ERRNO 0x800
74#define MY__FACILITY_WIN32 7 79#define MY_FACILITY_WIN32 7
75#define MY__FACILITY__WRes MY__FACILITY_ERRNO 80#define MY_FACILITY_WRes MY_FACILITY_ERRNO
76 81
77#define MY_HRESULT_FROM_errno_CONST_ERROR(x) ((HRESULT)( \ 82#define MY_HRESULT_FROM_errno_CONST_ERROR(x) ((HRESULT)( \
78 ( (HRESULT)(x) & 0x0000FFFF) \ 83 ( (HRESULT)(x) & 0x0000FFFF) \
79 | (MY__FACILITY__WRes << 16) \ 84 | (MY_FACILITY_WRes << 16) \
80 | (HRESULT)0x80000000 )) 85 | (HRESULT)0x80000000 ))
81 86
82#define MY_SRes_HRESULT_FROM_WRes(x) \ 87#define MY_SRes_HRESULT_FROM_WRes(x) \
@@ -120,17 +125,17 @@ typedef int WRes;
120#define ERROR_INVALID_REPARSE_DATA ((HRESULT)0x80071128L) 125#define ERROR_INVALID_REPARSE_DATA ((HRESULT)0x80071128L)
121#define ERROR_REPARSE_TAG_INVALID ((HRESULT)0x80071129L) 126#define ERROR_REPARSE_TAG_INVALID ((HRESULT)0x80071129L)
122 127
123// if (MY__FACILITY__WRes != FACILITY_WIN32), 128// if (MY_FACILITY_WRes != FACILITY_WIN32),
124// we use FACILITY_WIN32 for COM errors: 129// we use FACILITY_WIN32 for COM errors:
125#define E_OUTOFMEMORY ((HRESULT)0x8007000EL) 130#define E_OUTOFMEMORY ((HRESULT)0x8007000EL)
126#define E_INVALIDARG ((HRESULT)0x80070057L) 131#define E_INVALIDARG ((HRESULT)0x80070057L)
127#define MY__E_ERROR_NEGATIVE_SEEK ((HRESULT)0x80070083L) 132#define MY_E_ERROR_NEGATIVE_SEEK ((HRESULT)0x80070083L)
128 133
129/* 134/*
130// we can use FACILITY_ERRNO for some COM errors, that have errno equivalents: 135// we can use FACILITY_ERRNO for some COM errors, that have errno equivalents:
131#define E_OUTOFMEMORY MY_HRESULT_FROM_errno_CONST_ERROR(ENOMEM) 136#define E_OUTOFMEMORY MY_HRESULT_FROM_errno_CONST_ERROR(ENOMEM)
132#define E_INVALIDARG MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL) 137#define E_INVALIDARG MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL)
133#define MY__E_ERROR_NEGATIVE_SEEK MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL) 138#define MY_E_ERROR_NEGATIVE_SEEK MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL)
134*/ 139*/
135 140
136#define TEXT(quote) quote 141#define TEXT(quote) quote
@@ -156,18 +161,18 @@ typedef int WRes;
156 161
157 162
158#ifndef RINOK 163#ifndef RINOK
159#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; } 164#define RINOK(x) { const int _result_ = (x); if (_result_ != 0) return _result_; }
160#endif 165#endif
161 166
162#ifndef RINOK_WRes 167#ifndef RINOK_WRes
163#define RINOK_WRes(x) { WRes __result__ = (x); if (__result__ != 0) return __result__; } 168#define RINOK_WRes(x) { const WRes _result_ = (x); if (_result_ != 0) return _result_; }
164#endif 169#endif
165 170
166typedef unsigned char Byte; 171typedef unsigned char Byte;
167typedef short Int16; 172typedef short Int16;
168typedef unsigned short UInt16; 173typedef unsigned short UInt16;
169 174
170#ifdef _LZMA_UINT32_IS_ULONG 175#ifdef Z7_DECL_Int32_AS_long
171typedef long Int32; 176typedef long Int32;
172typedef unsigned long UInt32; 177typedef unsigned long UInt32;
173#else 178#else
@@ -206,37 +211,51 @@ typedef size_t SIZE_T;
206#endif // _WIN32 211#endif // _WIN32
207 212
208 213
209#define MY_HRES_ERROR__INTERNAL_ERROR ((HRESULT)0x8007054FL) 214#define MY_HRES_ERROR_INTERNAL_ERROR ((HRESULT)0x8007054FL)
210 215
211 216
212#ifdef _SZ_NO_INT_64 217#ifdef Z7_DECL_Int64_AS_long
213
214/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
215 NOTES: Some code will work incorrectly in that case! */
216 218
217typedef long Int64; 219typedef long Int64;
218typedef unsigned long UInt64; 220typedef unsigned long UInt64;
219 221
220#else 222#else
221 223
222#if defined(_MSC_VER) || defined(__BORLANDC__) 224#if (defined(_MSC_VER) || defined(__BORLANDC__)) && !defined(__clang__)
223typedef __int64 Int64; 225typedef __int64 Int64;
224typedef unsigned __int64 UInt64; 226typedef unsigned __int64 UInt64;
225#define UINT64_CONST(n) n 227#else
228#if defined(__clang__) || defined(__GNUC__)
229#include <stdint.h>
230typedef int64_t Int64;
231typedef uint64_t UInt64;
226#else 232#else
227typedef long long int Int64; 233typedef long long int Int64;
228typedef unsigned long long int UInt64; 234typedef unsigned long long int UInt64;
229#define UINT64_CONST(n) n ## ULL 235// #define UINT64_CONST(n) n ## ULL
236#endif
230#endif 237#endif
231 238
232#endif 239#endif
233 240
234#ifdef _LZMA_NO_SYSTEM_SIZE_T 241#define UINT64_CONST(n) n
235typedef UInt32 SizeT; 242
243
244#ifdef Z7_DECL_SizeT_AS_unsigned_int
245typedef unsigned int SizeT;
236#else 246#else
237typedef size_t SizeT; 247typedef size_t SizeT;
238#endif 248#endif
239 249
250/*
251#if (defined(_MSC_VER) && _MSC_VER <= 1200)
252typedef size_t MY_uintptr_t;
253#else
254#include <stdint.h>
255typedef uintptr_t MY_uintptr_t;
256#endif
257*/
258
240typedef int BoolInt; 259typedef int BoolInt;
241/* typedef BoolInt Bool; */ 260/* typedef BoolInt Bool; */
242#define True 1 261#define True 1
@@ -244,23 +263,23 @@ typedef int BoolInt;
244 263
245 264
246#ifdef _WIN32 265#ifdef _WIN32
247#define MY_STD_CALL __stdcall 266#define Z7_STDCALL __stdcall
248#else 267#else
249#define MY_STD_CALL 268#define Z7_STDCALL
250#endif 269#endif
251 270
252#ifdef _MSC_VER 271#ifdef _MSC_VER
253 272
254#if _MSC_VER >= 1300 273#if _MSC_VER >= 1300
255#define MY_NO_INLINE __declspec(noinline) 274#define Z7_NO_INLINE __declspec(noinline)
256#else 275#else
257#define MY_NO_INLINE 276#define Z7_NO_INLINE
258#endif 277#endif
259 278
260#define MY_FORCE_INLINE __forceinline 279#define Z7_FORCE_INLINE __forceinline
261 280
262#define MY_CDECL __cdecl 281#define Z7_CDECL __cdecl
263#define MY_FAST_CALL __fastcall 282#define Z7_FASTCALL __fastcall
264 283
265#else // _MSC_VER 284#else // _MSC_VER
266 285
@@ -268,27 +287,25 @@ typedef int BoolInt;
268 || (defined(__clang__) && (__clang_major__ >= 4)) \ 287 || (defined(__clang__) && (__clang_major__ >= 4)) \
269 || defined(__INTEL_COMPILER) \ 288 || defined(__INTEL_COMPILER) \
270 || defined(__xlC__) 289 || defined(__xlC__)
271#define MY_NO_INLINE __attribute__((noinline)) 290#define Z7_NO_INLINE __attribute__((noinline))
272// #define MY_FORCE_INLINE __attribute__((always_inline)) inline 291#define Z7_FORCE_INLINE __attribute__((always_inline)) inline
273#else 292#else
274#define MY_NO_INLINE 293#define Z7_NO_INLINE
294#define Z7_FORCE_INLINE
275#endif 295#endif
276 296
277#define MY_FORCE_INLINE 297#define Z7_CDECL
278
279
280#define MY_CDECL
281 298
282#if defined(_M_IX86) \ 299#if defined(_M_IX86) \
283 || defined(__i386__) 300 || defined(__i386__)
284// #define MY_FAST_CALL __attribute__((fastcall)) 301// #define Z7_FASTCALL __attribute__((fastcall))
285// #define MY_FAST_CALL __attribute__((cdecl)) 302// #define Z7_FASTCALL __attribute__((cdecl))
286#define MY_FAST_CALL 303#define Z7_FASTCALL
287#elif defined(MY_CPU_AMD64) 304#elif defined(MY_CPU_AMD64)
288// #define MY_FAST_CALL __attribute__((ms_abi)) 305// #define Z7_FASTCALL __attribute__((ms_abi))
289#define MY_FAST_CALL 306#define Z7_FASTCALL
290#else 307#else
291#define MY_FAST_CALL 308#define Z7_FASTCALL
292#endif 309#endif
293 310
294#endif // _MSC_VER 311#endif // _MSC_VER
@@ -296,41 +313,49 @@ typedef int BoolInt;
296 313
297/* The following interfaces use first parameter as pointer to structure */ 314/* The following interfaces use first parameter as pointer to structure */
298 315
299typedef struct IByteIn IByteIn; 316// #define Z7_C_IFACE_CONST_QUAL
300struct IByteIn 317#define Z7_C_IFACE_CONST_QUAL const
318
319#define Z7_C_IFACE_DECL(a) \
320 struct a ## _; \
321 typedef Z7_C_IFACE_CONST_QUAL struct a ## _ * a ## Ptr; \
322 typedef struct a ## _ a; \
323 struct a ## _
324
325
326Z7_C_IFACE_DECL (IByteIn)
301{ 327{
302 Byte (*Read)(const IByteIn *p); /* reads one byte, returns 0 in case of EOF or error */ 328 Byte (*Read)(IByteInPtr p); /* reads one byte, returns 0 in case of EOF or error */
303}; 329};
304#define IByteIn_Read(p) (p)->Read(p) 330#define IByteIn_Read(p) (p)->Read(p)
305 331
306 332
307typedef struct IByteOut IByteOut; 333Z7_C_IFACE_DECL (IByteOut)
308struct IByteOut
309{ 334{
310 void (*Write)(const IByteOut *p, Byte b); 335 void (*Write)(IByteOutPtr p, Byte b);
311}; 336};
312#define IByteOut_Write(p, b) (p)->Write(p, b) 337#define IByteOut_Write(p, b) (p)->Write(p, b)
313 338
314 339
315typedef struct ISeqInStream ISeqInStream; 340Z7_C_IFACE_DECL (ISeqInStream)
316struct ISeqInStream
317{ 341{
318 SRes (*Read)(const ISeqInStream *p, void *buf, size_t *size); 342 SRes (*Read)(ISeqInStreamPtr p, void *buf, size_t *size);
319 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. 343 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
320 (output(*size) < input(*size)) is allowed */ 344 (output(*size) < input(*size)) is allowed */
321}; 345};
322#define ISeqInStream_Read(p, buf, size) (p)->Read(p, buf, size) 346#define ISeqInStream_Read(p, buf, size) (p)->Read(p, buf, size)
323 347
348/* try to read as much as avail in stream and limited by (*processedSize) */
349SRes SeqInStream_ReadMax(ISeqInStreamPtr stream, void *buf, size_t *processedSize);
324/* it can return SZ_ERROR_INPUT_EOF */ 350/* it can return SZ_ERROR_INPUT_EOF */
325SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size); 351// SRes SeqInStream_Read(ISeqInStreamPtr stream, void *buf, size_t size);
326SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType); 352// SRes SeqInStream_Read2(ISeqInStreamPtr stream, void *buf, size_t size, SRes errorType);
327SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf); 353SRes SeqInStream_ReadByte(ISeqInStreamPtr stream, Byte *buf);
328 354
329 355
330typedef struct ISeqOutStream ISeqOutStream; 356Z7_C_IFACE_DECL (ISeqOutStream)
331struct ISeqOutStream
332{ 357{
333 size_t (*Write)(const ISeqOutStream *p, const void *buf, size_t size); 358 size_t (*Write)(ISeqOutStreamPtr p, const void *buf, size_t size);
334 /* Returns: result - the number of actually written bytes. 359 /* Returns: result - the number of actually written bytes.
335 (result < size) means error */ 360 (result < size) means error */
336}; 361};
@@ -344,29 +369,26 @@ typedef enum
344} ESzSeek; 369} ESzSeek;
345 370
346 371
347typedef struct ISeekInStream ISeekInStream; 372Z7_C_IFACE_DECL (ISeekInStream)
348struct ISeekInStream
349{ 373{
350 SRes (*Read)(const ISeekInStream *p, void *buf, size_t *size); /* same as ISeqInStream::Read */ 374 SRes (*Read)(ISeekInStreamPtr p, void *buf, size_t *size); /* same as ISeqInStream::Read */
351 SRes (*Seek)(const ISeekInStream *p, Int64 *pos, ESzSeek origin); 375 SRes (*Seek)(ISeekInStreamPtr p, Int64 *pos, ESzSeek origin);
352}; 376};
353#define ISeekInStream_Read(p, buf, size) (p)->Read(p, buf, size) 377#define ISeekInStream_Read(p, buf, size) (p)->Read(p, buf, size)
354#define ISeekInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) 378#define ISeekInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin)
355 379
356 380
357typedef struct ILookInStream ILookInStream; 381Z7_C_IFACE_DECL (ILookInStream)
358struct ILookInStream
359{ 382{
360 SRes (*Look)(const ILookInStream *p, const void **buf, size_t *size); 383 SRes (*Look)(ILookInStreamPtr p, const void **buf, size_t *size);
361 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. 384 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
362 (output(*size) > input(*size)) is not allowed 385 (output(*size) > input(*size)) is not allowed
363 (output(*size) < input(*size)) is allowed */ 386 (output(*size) < input(*size)) is allowed */
364 SRes (*Skip)(const ILookInStream *p, size_t offset); 387 SRes (*Skip)(ILookInStreamPtr p, size_t offset);
365 /* offset must be <= output(*size) of Look */ 388 /* offset must be <= output(*size) of Look */
366 389 SRes (*Read)(ILookInStreamPtr p, void *buf, size_t *size);
367 SRes (*Read)(const ILookInStream *p, void *buf, size_t *size);
368 /* reads directly (without buffer). It's same as ISeqInStream::Read */ 390 /* reads directly (without buffer). It's same as ISeqInStream::Read */
369 SRes (*Seek)(const ILookInStream *p, Int64 *pos, ESzSeek origin); 391 SRes (*Seek)(ILookInStreamPtr p, Int64 *pos, ESzSeek origin);
370}; 392};
371 393
372#define ILookInStream_Look(p, buf, size) (p)->Look(p, buf, size) 394#define ILookInStream_Look(p, buf, size) (p)->Look(p, buf, size)
@@ -375,19 +397,18 @@ struct ILookInStream
375#define ILookInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) 397#define ILookInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin)
376 398
377 399
378SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size); 400SRes LookInStream_LookRead(ILookInStreamPtr stream, void *buf, size_t *size);
379SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset); 401SRes LookInStream_SeekTo(ILookInStreamPtr stream, UInt64 offset);
380 402
381/* reads via ILookInStream::Read */ 403/* reads via ILookInStream::Read */
382SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType); 404SRes LookInStream_Read2(ILookInStreamPtr stream, void *buf, size_t size, SRes errorType);
383SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size); 405SRes LookInStream_Read(ILookInStreamPtr stream, void *buf, size_t size);
384
385 406
386 407
387typedef struct 408typedef struct
388{ 409{
389 ILookInStream vt; 410 ILookInStream vt;
390 const ISeekInStream *realStream; 411 ISeekInStreamPtr realStream;
391 412
392 size_t pos; 413 size_t pos;
393 size_t size; /* it's data size */ 414 size_t size; /* it's data size */
@@ -399,13 +420,13 @@ typedef struct
399 420
400void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead); 421void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead);
401 422
402#define LookToRead2_Init(p) { (p)->pos = (p)->size = 0; } 423#define LookToRead2_INIT(p) { (p)->pos = (p)->size = 0; }
403 424
404 425
405typedef struct 426typedef struct
406{ 427{
407 ISeqInStream vt; 428 ISeqInStream vt;
408 const ILookInStream *realStream; 429 ILookInStreamPtr realStream;
409} CSecToLook; 430} CSecToLook;
410 431
411void SecToLook_CreateVTable(CSecToLook *p); 432void SecToLook_CreateVTable(CSecToLook *p);
@@ -415,20 +436,19 @@ void SecToLook_CreateVTable(CSecToLook *p);
415typedef struct 436typedef struct
416{ 437{
417 ISeqInStream vt; 438 ISeqInStream vt;
418 const ILookInStream *realStream; 439 ILookInStreamPtr realStream;
419} CSecToRead; 440} CSecToRead;
420 441
421void SecToRead_CreateVTable(CSecToRead *p); 442void SecToRead_CreateVTable(CSecToRead *p);
422 443
423 444
424typedef struct ICompressProgress ICompressProgress; 445Z7_C_IFACE_DECL (ICompressProgress)
425
426struct ICompressProgress
427{ 446{
428 SRes (*Progress)(const ICompressProgress *p, UInt64 inSize, UInt64 outSize); 447 SRes (*Progress)(ICompressProgressPtr p, UInt64 inSize, UInt64 outSize);
429 /* Returns: result. (result != SZ_OK) means break. 448 /* Returns: result. (result != SZ_OK) means break.
430 Value (UInt64)(Int64)-1 for size means unknown value. */ 449 Value (UInt64)(Int64)-1 for size means unknown value. */
431}; 450};
451
432#define ICompressProgress_Progress(p, inSize, outSize) (p)->Progress(p, inSize, outSize) 452#define ICompressProgress_Progress(p, inSize, outSize) (p)->Progress(p, inSize, outSize)
433 453
434 454
@@ -466,13 +486,13 @@ struct ISzAlloc
466 486
467 487
468 488
469#ifndef MY_container_of 489#ifndef Z7_container_of
470 490
471/* 491/*
472#define MY_container_of(ptr, type, m) container_of(ptr, type, m) 492#define Z7_container_of(ptr, type, m) container_of(ptr, type, m)
473#define MY_container_of(ptr, type, m) CONTAINING_RECORD(ptr, type, m) 493#define Z7_container_of(ptr, type, m) CONTAINING_RECORD(ptr, type, m)
474#define MY_container_of(ptr, type, m) ((type *)((char *)(ptr) - offsetof(type, m))) 494#define Z7_container_of(ptr, type, m) ((type *)((char *)(ptr) - offsetof(type, m)))
475#define MY_container_of(ptr, type, m) (&((type *)0)->m == (ptr), ((type *)(((char *)(ptr)) - MY_offsetof(type, m)))) 495#define Z7_container_of(ptr, type, m) (&((type *)0)->m == (ptr), ((type *)(((char *)(ptr)) - MY_offsetof(type, m))))
476*/ 496*/
477 497
478/* 498/*
@@ -481,24 +501,64 @@ struct ISzAlloc
481 GCC 4.8.1 : classes with non-public variable members" 501 GCC 4.8.1 : classes with non-public variable members"
482*/ 502*/
483 503
484#define MY_container_of(ptr, type, m) ((type *)(void *)((char *)(void *)(1 ? (ptr) : &((type *)0)->m) - MY_offsetof(type, m))) 504#define Z7_container_of(ptr, type, m) \
505 ((type *)(void *)((char *)(void *) \
506 (1 ? (ptr) : &((type *)NULL)->m) - MY_offsetof(type, m)))
485 507
486#endif 508#define Z7_container_of_CONST(ptr, type, m) \
487 509 ((const type *)(const void *)((const char *)(const void *) \
488#define CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) ((type *)(void *)(ptr)) 510 (1 ? (ptr) : &((type *)NULL)->m) - MY_offsetof(type, m)))
489 511
490/* 512/*
491#define CONTAINER_FROM_VTBL(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) 513#define Z7_container_of_NON_CONST_FROM_CONST(ptr, type, m) \
514 ((type *)(void *)(const void *)((const char *)(const void *) \
515 (1 ? (ptr) : &((type *)NULL)->m) - MY_offsetof(type, m)))
492*/ 516*/
493#define CONTAINER_FROM_VTBL(ptr, type, m) MY_container_of(ptr, type, m)
494 517
495#define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) 518#endif
519
520#define Z7_CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) ((type *)(void *)(ptr))
521
522// #define Z7_CONTAINER_FROM_VTBL(ptr, type, m) Z7_CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m)
523#define Z7_CONTAINER_FROM_VTBL(ptr, type, m) Z7_container_of(ptr, type, m)
524// #define Z7_CONTAINER_FROM_VTBL(ptr, type, m) Z7_container_of_NON_CONST_FROM_CONST(ptr, type, m)
525
526#define Z7_CONTAINER_FROM_VTBL_CONST(ptr, type, m) Z7_container_of_CONST(ptr, type, m)
527
528#define Z7_CONTAINER_FROM_VTBL_CLS(ptr, type, m) Z7_CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m)
496/* 529/*
497#define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL(ptr, type, m) 530#define Z7_CONTAINER_FROM_VTBL_CLS(ptr, type, m) Z7_CONTAINER_FROM_VTBL(ptr, type, m)
498*/ 531*/
532#if defined (__clang__) || defined(__GNUC__)
533#define Z7_DIAGNOSCTIC_IGNORE_BEGIN_CAST_QUAL \
534 _Pragma("GCC diagnostic push") \
535 _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
536#define Z7_DIAGNOSCTIC_IGNORE_END_CAST_QUAL \
537 _Pragma("GCC diagnostic pop")
538#else
539#define Z7_DIAGNOSCTIC_IGNORE_BEGIN_CAST_QUAL
540#define Z7_DIAGNOSCTIC_IGNORE_END_CAST_QUAL
541#endif
542
543#define Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR(ptr, type, m, p) \
544 Z7_DIAGNOSCTIC_IGNORE_BEGIN_CAST_QUAL \
545 type *p = Z7_CONTAINER_FROM_VTBL(ptr, type, m); \
546 Z7_DIAGNOSCTIC_IGNORE_END_CAST_QUAL
547
548#define Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(type) \
549 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR(pp, type, vt, p)
499 550
500 551
501#define MY_memset_0_ARRAY(a) memset((a), 0, sizeof(a)) 552// #define ZIP7_DECLARE_HANDLE(name) typedef void *name;
553#define Z7_DECLARE_HANDLE(name) struct name##_dummy{int unused;}; typedef struct name##_dummy *name;
554
555
556#define Z7_memset_0_ARRAY(a) memset((a), 0, sizeof(a))
557
558#ifndef Z7_ARRAY_SIZE
559#define Z7_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
560#endif
561
502 562
503#ifdef _WIN32 563#ifdef _WIN32
504 564
@@ -527,3 +587,11 @@ struct ISzAlloc
527EXTERN_C_END 587EXTERN_C_END
528 588
529#endif 589#endif
590
591/*
592#ifndef Z7_ST
593#ifdef _7ZIP_ST
594#define Z7_ST
595#endif
596#endif
597*/
diff --git a/C/7zVersion.h b/C/7zVersion.h
index 49ea81d..7549239 100644
--- a/C/7zVersion.h
+++ b/C/7zVersion.h
@@ -1,7 +1,7 @@
1#define MY_VER_MAJOR 22 1#define MY_VER_MAJOR 23
2#define MY_VER_MINOR 01 2#define MY_VER_MINOR 01
3#define MY_VER_BUILD 0 3#define MY_VER_BUILD 0
4#define MY_VERSION_NUMBERS "22.01" 4#define MY_VERSION_NUMBERS "23.01"
5#define MY_VERSION MY_VERSION_NUMBERS 5#define MY_VERSION MY_VERSION_NUMBERS
6 6
7#ifdef MY_CPU_NAME 7#ifdef MY_CPU_NAME
@@ -10,12 +10,12 @@
10 #define MY_VERSION_CPU MY_VERSION 10 #define MY_VERSION_CPU MY_VERSION
11#endif 11#endif
12 12
13#define MY_DATE "2022-07-15" 13#define MY_DATE "2023-06-20"
14#undef MY_COPYRIGHT 14#undef MY_COPYRIGHT
15#undef MY_VERSION_COPYRIGHT_DATE 15#undef MY_VERSION_COPYRIGHT_DATE
16#define MY_AUTHOR_NAME "Igor Pavlov" 16#define MY_AUTHOR_NAME "Igor Pavlov"
17#define MY_COPYRIGHT_PD "Igor Pavlov : Public domain" 17#define MY_COPYRIGHT_PD "Igor Pavlov : Public domain"
18#define MY_COPYRIGHT_CR "Copyright (c) 1999-2022 Igor Pavlov" 18#define MY_COPYRIGHT_CR "Copyright (c) 1999-2023 Igor Pavlov"
19 19
20#ifdef USE_COPYRIGHT_CR 20#ifdef USE_COPYRIGHT_CR
21 #define MY_COPYRIGHT MY_COPYRIGHT_CR 21 #define MY_COPYRIGHT MY_COPYRIGHT_CR
diff --git a/C/7zWindows.h b/C/7zWindows.h
new file mode 100644
index 0000000..42c6db8
--- /dev/null
+++ b/C/7zWindows.h
@@ -0,0 +1,101 @@
1/* 7zWindows.h -- StdAfx
22023-04-02 : Igor Pavlov : Public domain */
3
4#ifndef ZIP7_INC_7Z_WINDOWS_H
5#define ZIP7_INC_7Z_WINDOWS_H
6
7#ifdef _WIN32
8
9#if defined(__clang__)
10# pragma clang diagnostic push
11#endif
12
13#if defined(_MSC_VER)
14
15#pragma warning(push)
16#pragma warning(disable : 4668) // '_WIN32_WINNT' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
17
18#if _MSC_VER == 1900
19// for old kit10 versions
20// #pragma warning(disable : 4255) // winuser.h(13979): warning C4255: 'GetThreadDpiAwarenessContext':
21#endif
22// win10 Windows Kit:
23#endif // _MSC_VER
24
25#if defined(_MSC_VER) && _MSC_VER <= 1200 && !defined(_WIN64)
26// for msvc6 without sdk2003
27#define RPC_NO_WINDOWS_H
28#endif
29
30#if defined(__MINGW32__) || defined(__MINGW64__)
31// #if defined(__GNUC__) && !defined(__clang__)
32#include <windows.h>
33#else
34#include <Windows.h>
35#endif
36// #include <basetsd.h>
37// #include <wtypes.h>
38
39// but if precompiled with clang-cl then we need
40// #include <windows.h>
41#if defined(_MSC_VER)
42#pragma warning(pop)
43#endif
44
45#if defined(__clang__)
46# pragma clang diagnostic pop
47#endif
48
49#if defined(_MSC_VER) && _MSC_VER <= 1200 && !defined(_WIN64)
50#ifndef _W64
51
52typedef long LONG_PTR, *PLONG_PTR;
53typedef unsigned long ULONG_PTR, *PULONG_PTR;
54typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
55
56#define Z7_OLD_WIN_SDK
57#endif // _W64
58#endif // _MSC_VER == 1200
59
60#ifdef Z7_OLD_WIN_SDK
61
62#ifndef INVALID_FILE_ATTRIBUTES
63#define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
64#endif
65#ifndef INVALID_SET_FILE_POINTER
66#define INVALID_SET_FILE_POINTER ((DWORD)-1)
67#endif
68#ifndef FILE_SPECIAL_ACCESS
69#define FILE_SPECIAL_ACCESS (FILE_ANY_ACCESS)
70#endif
71
72// ShlObj.h:
73// #define BIF_NEWDIALOGSTYLE 0x0040
74
75#pragma warning(disable : 4201)
76// #pragma warning(disable : 4115)
77
78#undef VARIANT_TRUE
79#define VARIANT_TRUE ((VARIANT_BOOL)-1)
80#endif
81
82#endif // Z7_OLD_WIN_SDK
83
84#ifdef UNDER_CE
85#undef VARIANT_TRUE
86#define VARIANT_TRUE ((VARIANT_BOOL)-1)
87#endif
88
89
90#if defined(_MSC_VER)
91#if _MSC_VER >= 1400 && _MSC_VER <= 1600
92 // BaseTsd.h(148) : 'HandleToULong' : unreferenced inline function has been removed
93 // string.h
94 // #pragma warning(disable : 4514)
95#endif
96#endif
97
98
99/* #include "7zTypes.h" */
100
101#endif
diff --git a/C/7zip_gcc_c.mak b/C/7zip_gcc_c.mak
index 24505f3..f19a99b 100644
--- a/C/7zip_gcc_c.mak
+++ b/C/7zip_gcc_c.mak
@@ -4,15 +4,28 @@ MY_ARCH_2 = $(MY_ARCH)
4MY_ASM = jwasm 4MY_ASM = jwasm
5MY_ASM = asmc 5MY_ASM = asmc
6 6
7ifndef RC
8#RC=windres.exe --target=pe-x86-64
9#RC=windres.exe -F pe-i386
10RC=windres.exe
11endif
12
7PROGPATH = $(O)/$(PROG) 13PROGPATH = $(O)/$(PROG)
8PROGPATH_STATIC = $(O)/$(PROG)s 14PROGPATH_STATIC = $(O)/$(PROG)s
9 15
16ifneq ($(CC), xlc)
17CFLAGS_WARN_WALL = -Wall -Werror -Wextra
18endif
10 19
11# for object file 20# for object file
12CFLAGS_BASE_LIST = -c 21CFLAGS_BASE_LIST = -c
13# for ASM file 22# for ASM file
14# CFLAGS_BASE_LIST = -S 23# CFLAGS_BASE_LIST = -S
15CFLAGS_BASE = $(MY_ARCH_2) -O2 $(CFLAGS_BASE_LIST) -Wall -Werror -Wextra $(CFLAGS_WARN) \ 24
25FLAGS_FLTO =
26FLAGS_FLTO = -flto
27
28CFLAGS_BASE = $(MY_ARCH_2) -O2 $(CFLAGS_BASE_LIST) $(CFLAGS_WARN_WALL) $(CFLAGS_WARN) \
16 -DNDEBUG -D_REENTRANT -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE 29 -DNDEBUG -D_REENTRANT -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
17 30
18 31
@@ -93,9 +106,9 @@ DEL_OBJ_EXE = -$(RM) $(O)\*.o $(O)\$(PROG).exe $(O)\$(PROG).dll
93endif 106endif
94 107
95 108
96LIB2 = -lOle32 -loleaut32 -luuid -ladvapi32 -lUser32 109LIB2 = -lOle32 -loleaut32 -luuid -ladvapi32 -lUser32 -lShell32
97 110
98CXXFLAGS_EXTRA = -DUNICODE -D_UNICODE 111CFLAGS_EXTRA = -DUNICODE -D_UNICODE
99# -Wno-delete-non-virtual-dtor 112# -Wno-delete-non-virtual-dtor
100 113
101 114
@@ -103,8 +116,8 @@ else
103 116
104RM = rm -f 117RM = rm -f
105MY_MKDIR=mkdir -p 118MY_MKDIR=mkdir -p
106# CFLAGS_BASE := $(CFLAGS_BASE) -D_7ZIP_ST 119# CFLAGS_BASE := $(CFLAGS_BASE) -DZ7_ST
107# CXXFLAGS_EXTRA = -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE 120# CFLAGS_EXTRA = -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
108 121
109# LOCAL_LIBS=-lpthread 122# LOCAL_LIBS=-lpthread
110# LOCAL_LIBS_DLL=$(LOCAL_LIBS) -ldl 123# LOCAL_LIBS_DLL=$(LOCAL_LIBS) -ldl
@@ -115,10 +128,6 @@ DEL_OBJ_EXE = -$(RM) $(PROGPATH) $(PROGPATH_STATIC) $(OBJS)
115endif 128endif
116 129
117 130
118
119CFLAGS = $(LOCAL_FLAGS) $(CFLAGS_BASE2) $(CFLAGS_BASE) $(CC_SHARED) -o $@
120
121
122ifdef IS_X64 131ifdef IS_X64
123AFLAGS_ABI = -elf64 -DABI_LINUX 132AFLAGS_ABI = -elf64 -DABI_LINUX
124else 133else
@@ -129,12 +138,9 @@ AFLAGS_ABI = -elf -DABI_LINUX -DABI_CDECL
129endif 138endif
130AFLAGS = $(AFLAGS_ABI) -Fo$(O)/ 139AFLAGS = $(AFLAGS_ABI) -Fo$(O)/
131 140
141C_WARN_FLAGS =
132 142
133CXX_WARN_FLAGS = 143CFLAGS = $(LOCAL_FLAGS) $(CFLAGS_BASE2) $(CFLAGS_BASE) $(CFLAGS_EXTRA) $(C_WARN_FLAGS) $(FLAGS_FLTO) $(CC_SHARED) -o $@
134#-Wno-invalid-offsetof
135#-Wno-reorder
136
137CXXFLAGS = $(LOCAL_FLAGS) $(CXXFLAGS_BASE2) $(CFLAGS_BASE) $(CXXFLAGS_EXTRA) $(CC_SHARED) -o $@ $(CXX_WARN_FLAGS)
138 144
139STATIC_TARGET= 145STATIC_TARGET=
140ifdef COMPL_STATIC 146ifdef COMPL_STATIC
@@ -147,18 +153,27 @@ all: $(O) $(PROGPATH) $(STATIC_TARGET)
147$(O): 153$(O):
148 $(MY_MKDIR) $(O) 154 $(MY_MKDIR) $(O)
149 155
150LFLAGS_ALL = -s $(MY_ARCH_2) $(LDFLAGS) $(LD_arch) $(OBJS) $(MY_LIBS) $(LIB2) 156ifneq ($(CC), $(CROSS_COMPILE)clang)
157LFLAGS_STRIP = -s
158endif
159
160LFLAGS_ALL = $(LFLAGS_STRIP) $(MY_ARCH_2) $(LDFLAGS) $(FLAGS_FLTO) $(LD_arch) $(OBJS) $(MY_LIBS) $(LIB2)
151$(PROGPATH): $(OBJS) 161$(PROGPATH): $(OBJS)
152 $(CXX) -o $(PROGPATH) $(LFLAGS_ALL) 162 $(CC) -o $(PROGPATH) $(LFLAGS_ALL)
153 163
154$(PROGPATH_STATIC): $(OBJS) 164$(PROGPATH_STATIC): $(OBJS)
155 $(CXX) -static -o $(PROGPATH_STATIC) $(LFLAGS_ALL) 165 $(CC) -static -o $(PROGPATH_STATIC) $(LFLAGS_ALL)
156 166
157 167
158ifndef NO_DEFAULT_RES 168ifndef NO_DEFAULT_RES
169# old mingw without -FO
170# windres.exe $(RFLAGS) resource.rc $O/resource.o
159$O/resource.o: resource.rc 171$O/resource.o: resource.rc
160 windres.exe $(RFLAGS) resource.rc $O/resource.o 172 $(RC) $(RFLAGS) resource.rc $(O)/resource.o
161endif 173endif
174# windres.exe $(RFLAGS) resource.rc $(O)\resource.o
175# windres.exe $(RFLAGS) resource.rc -FO $(O)/resource.o
176# $(RC) $(RFLAGS) resource.rc -FO $(O)/resource.o
162 177
163 178
164 179
@@ -256,10 +271,18 @@ $O/Sha256.o: ../../../C/Sha256.c
256 $(CC) $(CFLAGS) $< 271 $(CC) $(CFLAGS) $<
257$O/Sort.o: ../../../C/Sort.c 272$O/Sort.o: ../../../C/Sort.c
258 $(CC) $(CFLAGS) $< 273 $(CC) $(CFLAGS) $<
274$O/SwapBytes.o: ../../../C/SwapBytes.c
275 $(CC) $(CFLAGS) $<
259$O/Xz.o: ../../../C/Xz.c 276$O/Xz.o: ../../../C/Xz.c
260 $(CC) $(CFLAGS) $< 277 $(CC) $(CFLAGS) $<
261$O/XzCrc64.o: ../../../C/XzCrc64.c 278$O/XzCrc64.o: ../../../C/XzCrc64.c
262 $(CC) $(CFLAGS) $< 279 $(CC) $(CFLAGS) $<
280$O/XzDec.o: ../../../C/XzDec.c
281 $(CC) $(CFLAGS) $<
282$O/XzEnc.o: ../../../C/XzEnc.c
283 $(CC) $(CFLAGS) $<
284$O/XzIn.o: ../../../C/XzIn.c
285 $(CC) $(CFLAGS) $<
263 286
264 287
265ifdef USE_ASM 288ifdef USE_ASM
@@ -310,7 +333,7 @@ $O/LzmaDecOpt.o: ../../../Asm/arm64/LzmaDecOpt.S ../../../Asm/arm64/7zAsm.S
310endif 333endif
311 334
312$O/LzmaDec.o: ../../LzmaDec.c 335$O/LzmaDec.o: ../../LzmaDec.c
313 $(CC) $(CFLAGS) -D_LZMA_DEC_OPT $< 336 $(CC) $(CFLAGS) -DZ7_LZMA_DEC_OPT $<
314 337
315else 338else
316 339
@@ -321,22 +344,16 @@ endif
321 344
322 345
323 346
324$O/XzDec.o: ../../../C/XzDec.c
325 $(CC) $(CFLAGS) $<
326$O/XzEnc.o: ../../../C/XzEnc.c
327 $(CC) $(CFLAGS) $<
328$O/XzIn.o: ../../../C/XzIn.c
329 $(CC) $(CFLAGS) $<
330
331
332$O/7zMain.o: ../../../C/Util/7z/7zMain.c 347$O/7zMain.o: ../../../C/Util/7z/7zMain.c
333 $(CC) $(CFLAGS) $< 348 $(CC) $(CFLAGS) $<
334$O/LzmaUtil.o: ../../../C/Util/Lzma/LzmaUtil.c
335 $(CC) $(CFLAGS) $<
336$O/7zipInstall.o: ../../../C/Util/7zipInstall/7zipInstall.c 349$O/7zipInstall.o: ../../../C/Util/7zipInstall/7zipInstall.c
337 $(CC) $(CFLAGS) $< 350 $(CC) $(CFLAGS) $<
338$O/7zipUninstall.o: ../../../C/Util/7zipUninstall/7zipUninstall.c 351$O/7zipUninstall.o: ../../../C/Util/7zipUninstall/7zipUninstall.c
339 $(CC) $(CFLAGS) $< 352 $(CC) $(CFLAGS) $<
353$O/LzmaUtil.o: ../../../C/Util/Lzma/LzmaUtil.c
354 $(CC) $(CFLAGS) $<
355$O/XzUtil.o: ../../../C/Util/Xz/XzUtil.c
356 $(CC) $(CFLAGS) $<
340 357
341 358
342clean: 359clean:
diff --git a/C/Aes.c b/C/Aes.c
index 27e32e6..bcaafab 100644
--- a/C/Aes.c
+++ b/C/Aes.c
@@ -1,5 +1,5 @@
1/* Aes.c -- AES encryption / decryption 1/* Aes.c -- AES encryption / decryption
22021-05-13 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -7,7 +7,7 @@
7#include "Aes.h" 7#include "Aes.h"
8 8
9AES_CODE_FUNC g_AesCbc_Decode; 9AES_CODE_FUNC g_AesCbc_Decode;
10#ifndef _SFX 10#ifndef Z7_SFX
11AES_CODE_FUNC g_AesCbc_Encode; 11AES_CODE_FUNC g_AesCbc_Encode;
12AES_CODE_FUNC g_AesCtr_Code; 12AES_CODE_FUNC g_AesCtr_Code;
13UInt32 g_Aes_SupportedFunctions_Flags; 13UInt32 g_Aes_SupportedFunctions_Flags;
@@ -51,7 +51,7 @@ static Byte InvS[256];
51#define DD(x) (D + (x << 8)) 51#define DD(x) (D + (x << 8))
52 52
53 53
54// #define _SHOW_AES_STATUS 54// #define Z7_SHOW_AES_STATUS
55 55
56#ifdef MY_CPU_X86_OR_AMD64 56#ifdef MY_CPU_X86_OR_AMD64
57 #define USE_HW_AES 57 #define USE_HW_AES
@@ -72,11 +72,11 @@ static Byte InvS[256];
72#endif 72#endif
73 73
74#ifdef USE_HW_AES 74#ifdef USE_HW_AES
75#ifdef _SHOW_AES_STATUS 75#ifdef Z7_SHOW_AES_STATUS
76#include <stdio.h> 76#include <stdio.h>
77#define _PRF(x) x 77#define PRF(x) x
78#else 78#else
79#define _PRF(x) 79#define PRF(x)
80#endif 80#endif
81#endif 81#endif
82 82
@@ -90,23 +90,23 @@ void AesGenTables(void)
90 for (i = 0; i < 256; i++) 90 for (i = 0; i < 256; i++)
91 { 91 {
92 { 92 {
93 UInt32 a1 = Sbox[i]; 93 const UInt32 a1 = Sbox[i];
94 UInt32 a2 = xtime(a1); 94 const UInt32 a2 = xtime(a1);
95 UInt32 a3 = a2 ^ a1; 95 const UInt32 a3 = a2 ^ a1;
96 TT(0)[i] = Ui32(a2, a1, a1, a3); 96 TT(0)[i] = Ui32(a2, a1, a1, a3);
97 TT(1)[i] = Ui32(a3, a2, a1, a1); 97 TT(1)[i] = Ui32(a3, a2, a1, a1);
98 TT(2)[i] = Ui32(a1, a3, a2, a1); 98 TT(2)[i] = Ui32(a1, a3, a2, a1);
99 TT(3)[i] = Ui32(a1, a1, a3, a2); 99 TT(3)[i] = Ui32(a1, a1, a3, a2);
100 } 100 }
101 { 101 {
102 UInt32 a1 = InvS[i]; 102 const UInt32 a1 = InvS[i];
103 UInt32 a2 = xtime(a1); 103 const UInt32 a2 = xtime(a1);
104 UInt32 a4 = xtime(a2); 104 const UInt32 a4 = xtime(a2);
105 UInt32 a8 = xtime(a4); 105 const UInt32 a8 = xtime(a4);
106 UInt32 a9 = a8 ^ a1; 106 const UInt32 a9 = a8 ^ a1;
107 UInt32 aB = a8 ^ a2 ^ a1; 107 const UInt32 aB = a8 ^ a2 ^ a1;
108 UInt32 aD = a8 ^ a4 ^ a1; 108 const UInt32 aD = a8 ^ a4 ^ a1;
109 UInt32 aE = a8 ^ a4 ^ a2; 109 const UInt32 aE = a8 ^ a4 ^ a2;
110 DD(0)[i] = Ui32(aE, a9, aD, aB); 110 DD(0)[i] = Ui32(aE, a9, aD, aB);
111 DD(1)[i] = Ui32(aB, aE, a9, aD); 111 DD(1)[i] = Ui32(aB, aE, a9, aD);
112 DD(2)[i] = Ui32(aD, aB, aE, a9); 112 DD(2)[i] = Ui32(aD, aB, aE, a9);
@@ -116,7 +116,7 @@ void AesGenTables(void)
116 116
117 { 117 {
118 AES_CODE_FUNC d = AesCbc_Decode; 118 AES_CODE_FUNC d = AesCbc_Decode;
119 #ifndef _SFX 119 #ifndef Z7_SFX
120 AES_CODE_FUNC e = AesCbc_Encode; 120 AES_CODE_FUNC e = AesCbc_Encode;
121 AES_CODE_FUNC c = AesCtr_Code; 121 AES_CODE_FUNC c = AesCtr_Code;
122 UInt32 flags = 0; 122 UInt32 flags = 0;
@@ -126,10 +126,10 @@ void AesGenTables(void)
126 if (CPU_IsSupported_AES()) 126 if (CPU_IsSupported_AES())
127 { 127 {
128 // #pragma message ("AES HW") 128 // #pragma message ("AES HW")
129 _PRF(printf("\n===AES HW\n")); 129 PRF(printf("\n===AES HW\n"));
130 d = AesCbc_Decode_HW; 130 d = AesCbc_Decode_HW;
131 131
132 #ifndef _SFX 132 #ifndef Z7_SFX
133 e = AesCbc_Encode_HW; 133 e = AesCbc_Encode_HW;
134 c = AesCtr_Code_HW; 134 c = AesCtr_Code_HW;
135 flags = k_Aes_SupportedFunctions_HW; 135 flags = k_Aes_SupportedFunctions_HW;
@@ -138,9 +138,9 @@ void AesGenTables(void)
138 #ifdef MY_CPU_X86_OR_AMD64 138 #ifdef MY_CPU_X86_OR_AMD64
139 if (CPU_IsSupported_VAES_AVX2()) 139 if (CPU_IsSupported_VAES_AVX2())
140 { 140 {
141 _PRF(printf("\n===vaes avx2\n")); 141 PRF(printf("\n===vaes avx2\n"));
142 d = AesCbc_Decode_HW_256; 142 d = AesCbc_Decode_HW_256;
143 #ifndef _SFX 143 #ifndef Z7_SFX
144 c = AesCtr_Code_HW_256; 144 c = AesCtr_Code_HW_256;
145 flags |= k_Aes_SupportedFunctions_HW_256; 145 flags |= k_Aes_SupportedFunctions_HW_256;
146 #endif 146 #endif
@@ -150,7 +150,7 @@ void AesGenTables(void)
150 #endif 150 #endif
151 151
152 g_AesCbc_Decode = d; 152 g_AesCbc_Decode = d;
153 #ifndef _SFX 153 #ifndef Z7_SFX
154 g_AesCbc_Encode = e; 154 g_AesCbc_Encode = e;
155 g_AesCtr_Code = c; 155 g_AesCtr_Code = c;
156 g_Aes_SupportedFunctions_Flags = flags; 156 g_Aes_SupportedFunctions_Flags = flags;
@@ -194,7 +194,7 @@ void AesGenTables(void)
194#define FD(i, x) InvS[gb(x, m[(i - x) & 3])] 194#define FD(i, x) InvS[gb(x, m[(i - x) & 3])]
195#define FD4(i) dest[i] = Ui32(FD(i, 0), FD(i, 1), FD(i, 2), FD(i, 3)) ^ w[i]; 195#define FD4(i) dest[i] = Ui32(FD(i, 0), FD(i, 1), FD(i, 2), FD(i, 3)) ^ w[i];
196 196
197void MY_FAST_CALL Aes_SetKey_Enc(UInt32 *w, const Byte *key, unsigned keySize) 197void Z7_FASTCALL Aes_SetKey_Enc(UInt32 *w, const Byte *key, unsigned keySize)
198{ 198{
199 unsigned i, m; 199 unsigned i, m;
200 const UInt32 *wLim; 200 const UInt32 *wLim;
@@ -230,7 +230,7 @@ void MY_FAST_CALL Aes_SetKey_Enc(UInt32 *w, const Byte *key, unsigned keySize)
230 while (++w != wLim); 230 while (++w != wLim);
231} 231}
232 232
233void MY_FAST_CALL Aes_SetKey_Dec(UInt32 *w, const Byte *key, unsigned keySize) 233void Z7_FASTCALL Aes_SetKey_Dec(UInt32 *w, const Byte *key, unsigned keySize)
234{ 234{
235 unsigned i, num; 235 unsigned i, num;
236 Aes_SetKey_Enc(w, key, keySize); 236 Aes_SetKey_Enc(w, key, keySize);
@@ -251,7 +251,7 @@ void MY_FAST_CALL Aes_SetKey_Dec(UInt32 *w, const Byte *key, unsigned keySize)
251 src and dest are pointers to 4 UInt32 words. 251 src and dest are pointers to 4 UInt32 words.
252 src and dest can point to same block */ 252 src and dest can point to same block */
253 253
254// MY_FORCE_INLINE 254// Z7_FORCE_INLINE
255static void Aes_Encode(const UInt32 *w, UInt32 *dest, const UInt32 *src) 255static void Aes_Encode(const UInt32 *w, UInt32 *dest, const UInt32 *src)
256{ 256{
257 UInt32 s[4]; 257 UInt32 s[4];
@@ -265,17 +265,20 @@ static void Aes_Encode(const UInt32 *w, UInt32 *dest, const UInt32 *src)
265 w += 4; 265 w += 4;
266 for (;;) 266 for (;;)
267 { 267 {
268 HT16(m, s, 0); 268 HT16(m, s, 0)
269 if (--numRounds2 == 0) 269 if (--numRounds2 == 0)
270 break; 270 break;
271 HT16(s, m, 4); 271 HT16(s, m, 4)
272 w += 8; 272 w += 8;
273 } 273 }
274 w += 4; 274 w += 4;
275 FT4(0); FT4(1); FT4(2); FT4(3); 275 FT4(0)
276 FT4(1)
277 FT4(2)
278 FT4(3)
276} 279}
277 280
278MY_FORCE_INLINE 281Z7_FORCE_INLINE
279static void Aes_Decode(const UInt32 *w, UInt32 *dest, const UInt32 *src) 282static void Aes_Decode(const UInt32 *w, UInt32 *dest, const UInt32 *src)
280{ 283{
281 UInt32 s[4]; 284 UInt32 s[4];
@@ -289,12 +292,15 @@ static void Aes_Decode(const UInt32 *w, UInt32 *dest, const UInt32 *src)
289 for (;;) 292 for (;;)
290 { 293 {
291 w -= 8; 294 w -= 8;
292 HD16(m, s, 4); 295 HD16(m, s, 4)
293 if (--numRounds2 == 0) 296 if (--numRounds2 == 0)
294 break; 297 break;
295 HD16(s, m, 0); 298 HD16(s, m, 0)
296 } 299 }
297 FD4(0); FD4(1); FD4(2); FD4(3); 300 FD4(0)
301 FD4(1)
302 FD4(2)
303 FD4(3)
298} 304}
299 305
300void AesCbc_Init(UInt32 *p, const Byte *iv) 306void AesCbc_Init(UInt32 *p, const Byte *iv)
@@ -304,7 +310,7 @@ void AesCbc_Init(UInt32 *p, const Byte *iv)
304 p[i] = GetUi32(iv + i * 4); 310 p[i] = GetUi32(iv + i * 4);
305} 311}
306 312
307void MY_FAST_CALL AesCbc_Encode(UInt32 *p, Byte *data, size_t numBlocks) 313void Z7_FASTCALL AesCbc_Encode(UInt32 *p, Byte *data, size_t numBlocks)
308{ 314{
309 for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE) 315 for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE)
310 { 316 {
@@ -315,14 +321,14 @@ void MY_FAST_CALL AesCbc_Encode(UInt32 *p, Byte *data, size_t numBlocks)
315 321
316 Aes_Encode(p + 4, p, p); 322 Aes_Encode(p + 4, p, p);
317 323
318 SetUi32(data, p[0]); 324 SetUi32(data, p[0])
319 SetUi32(data + 4, p[1]); 325 SetUi32(data + 4, p[1])
320 SetUi32(data + 8, p[2]); 326 SetUi32(data + 8, p[2])
321 SetUi32(data + 12, p[3]); 327 SetUi32(data + 12, p[3])
322 } 328 }
323} 329}
324 330
325void MY_FAST_CALL AesCbc_Decode(UInt32 *p, Byte *data, size_t numBlocks) 331void Z7_FASTCALL AesCbc_Decode(UInt32 *p, Byte *data, size_t numBlocks)
326{ 332{
327 UInt32 in[4], out[4]; 333 UInt32 in[4], out[4];
328 for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE) 334 for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE)
@@ -334,10 +340,10 @@ void MY_FAST_CALL AesCbc_Decode(UInt32 *p, Byte *data, size_t numBlocks)
334 340
335 Aes_Decode(p + 4, out, in); 341 Aes_Decode(p + 4, out, in);
336 342
337 SetUi32(data, p[0] ^ out[0]); 343 SetUi32(data, p[0] ^ out[0])
338 SetUi32(data + 4, p[1] ^ out[1]); 344 SetUi32(data + 4, p[1] ^ out[1])
339 SetUi32(data + 8, p[2] ^ out[2]); 345 SetUi32(data + 8, p[2] ^ out[2])
340 SetUi32(data + 12, p[3] ^ out[3]); 346 SetUi32(data + 12, p[3] ^ out[3])
341 347
342 p[0] = in[0]; 348 p[0] = in[0];
343 p[1] = in[1]; 349 p[1] = in[1];
@@ -346,7 +352,7 @@ void MY_FAST_CALL AesCbc_Decode(UInt32 *p, Byte *data, size_t numBlocks)
346 } 352 }
347} 353}
348 354
349void MY_FAST_CALL AesCtr_Code(UInt32 *p, Byte *data, size_t numBlocks) 355void Z7_FASTCALL AesCtr_Code(UInt32 *p, Byte *data, size_t numBlocks)
350{ 356{
351 for (; numBlocks != 0; numBlocks--) 357 for (; numBlocks != 0; numBlocks--)
352 { 358 {
@@ -360,7 +366,7 @@ void MY_FAST_CALL AesCtr_Code(UInt32 *p, Byte *data, size_t numBlocks)
360 366
361 for (i = 0; i < 4; i++, data += 4) 367 for (i = 0; i < 4; i++, data += 4)
362 { 368 {
363 UInt32 t = temp[i]; 369 const UInt32 t = temp[i];
364 370
365 #ifdef MY_CPU_LE_UNALIGN 371 #ifdef MY_CPU_LE_UNALIGN
366 *((UInt32 *)(void *)data) ^= t; 372 *((UInt32 *)(void *)data) ^= t;
@@ -373,3 +379,15 @@ void MY_FAST_CALL AesCtr_Code(UInt32 *p, Byte *data, size_t numBlocks)
373 } 379 }
374 } 380 }
375} 381}
382
383#undef xtime
384#undef Ui32
385#undef gb0
386#undef gb1
387#undef gb2
388#undef gb3
389#undef gb
390#undef TT
391#undef DD
392#undef USE_HW_AES
393#undef PRF
diff --git a/C/Aes.h b/C/Aes.h
index 2aa2256..7f0182a 100644
--- a/C/Aes.h
+++ b/C/Aes.h
@@ -1,8 +1,8 @@
1/* Aes.h -- AES encryption / decryption 1/* Aes.h -- AES encryption / decryption
22018-04-28 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __AES_H 4#ifndef ZIP7_INC_AES_H
5#define __AES_H 5#define ZIP7_INC_AES_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -20,19 +20,19 @@ void AesGenTables(void);
20 20
21/* aes - 16-byte aligned pointer to keyMode+roundKeys sequence */ 21/* aes - 16-byte aligned pointer to keyMode+roundKeys sequence */
22/* keySize = 16 or 24 or 32 (bytes) */ 22/* keySize = 16 or 24 or 32 (bytes) */
23typedef void (MY_FAST_CALL *AES_SET_KEY_FUNC)(UInt32 *aes, const Byte *key, unsigned keySize); 23typedef void (Z7_FASTCALL *AES_SET_KEY_FUNC)(UInt32 *aes, const Byte *key, unsigned keySize);
24void MY_FAST_CALL Aes_SetKey_Enc(UInt32 *aes, const Byte *key, unsigned keySize); 24void Z7_FASTCALL Aes_SetKey_Enc(UInt32 *aes, const Byte *key, unsigned keySize);
25void MY_FAST_CALL Aes_SetKey_Dec(UInt32 *aes, const Byte *key, unsigned keySize); 25void Z7_FASTCALL Aes_SetKey_Dec(UInt32 *aes, const Byte *key, unsigned keySize);
26 26
27/* ivAes - 16-byte aligned pointer to iv+keyMode+roundKeys sequence: UInt32[AES_NUM_IVMRK_WORDS] */ 27/* ivAes - 16-byte aligned pointer to iv+keyMode+roundKeys sequence: UInt32[AES_NUM_IVMRK_WORDS] */
28void AesCbc_Init(UInt32 *ivAes, const Byte *iv); /* iv size is AES_BLOCK_SIZE */ 28void AesCbc_Init(UInt32 *ivAes, const Byte *iv); /* iv size is AES_BLOCK_SIZE */
29 29
30/* data - 16-byte aligned pointer to data */ 30/* data - 16-byte aligned pointer to data */
31/* numBlocks - the number of 16-byte blocks in data array */ 31/* numBlocks - the number of 16-byte blocks in data array */
32typedef void (MY_FAST_CALL *AES_CODE_FUNC)(UInt32 *ivAes, Byte *data, size_t numBlocks); 32typedef void (Z7_FASTCALL *AES_CODE_FUNC)(UInt32 *ivAes, Byte *data, size_t numBlocks);
33 33
34extern AES_CODE_FUNC g_AesCbc_Decode; 34extern AES_CODE_FUNC g_AesCbc_Decode;
35#ifndef _SFX 35#ifndef Z7_SFX
36extern AES_CODE_FUNC g_AesCbc_Encode; 36extern AES_CODE_FUNC g_AesCbc_Encode;
37extern AES_CODE_FUNC g_AesCtr_Code; 37extern AES_CODE_FUNC g_AesCtr_Code;
38#define k_Aes_SupportedFunctions_HW (1 << 2) 38#define k_Aes_SupportedFunctions_HW (1 << 2)
@@ -41,19 +41,19 @@ extern UInt32 g_Aes_SupportedFunctions_Flags;
41#endif 41#endif
42 42
43 43
44#define DECLARE__AES_CODE_FUNC(funcName) \ 44#define Z7_DECLARE_AES_CODE_FUNC(funcName) \
45 void MY_FAST_CALL funcName(UInt32 *ivAes, Byte *data, size_t numBlocks); 45 void Z7_FASTCALL funcName(UInt32 *ivAes, Byte *data, size_t numBlocks);
46 46
47DECLARE__AES_CODE_FUNC (AesCbc_Encode) 47Z7_DECLARE_AES_CODE_FUNC (AesCbc_Encode)
48DECLARE__AES_CODE_FUNC (AesCbc_Decode) 48Z7_DECLARE_AES_CODE_FUNC (AesCbc_Decode)
49DECLARE__AES_CODE_FUNC (AesCtr_Code) 49Z7_DECLARE_AES_CODE_FUNC (AesCtr_Code)
50 50
51DECLARE__AES_CODE_FUNC (AesCbc_Encode_HW) 51Z7_DECLARE_AES_CODE_FUNC (AesCbc_Encode_HW)
52DECLARE__AES_CODE_FUNC (AesCbc_Decode_HW) 52Z7_DECLARE_AES_CODE_FUNC (AesCbc_Decode_HW)
53DECLARE__AES_CODE_FUNC (AesCtr_Code_HW) 53Z7_DECLARE_AES_CODE_FUNC (AesCtr_Code_HW)
54 54
55DECLARE__AES_CODE_FUNC (AesCbc_Decode_HW_256) 55Z7_DECLARE_AES_CODE_FUNC (AesCbc_Decode_HW_256)
56DECLARE__AES_CODE_FUNC (AesCtr_Code_HW_256) 56Z7_DECLARE_AES_CODE_FUNC (AesCtr_Code_HW_256)
57 57
58EXTERN_C_END 58EXTERN_C_END
59 59
diff --git a/C/AesOpt.c b/C/AesOpt.c
index 8be8ff6..cfa6413 100644
--- a/C/AesOpt.c
+++ b/C/AesOpt.c
@@ -1,39 +1,33 @@
1/* AesOpt.c -- AES optimized code for x86 AES hardware instructions 1/* AesOpt.c -- AES optimized code for x86 AES hardware instructions
22021-04-01 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#include "Aes.h"
6#include "CpuArch.h" 7#include "CpuArch.h"
7 8
8#ifdef MY_CPU_X86_OR_AMD64 9#ifdef MY_CPU_X86_OR_AMD64
9 10
10 #if defined(__clang__) 11 #if defined(__INTEL_COMPILER)
11 #if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 8)
12 #define USE_INTEL_AES
13 #define ATTRIB_AES __attribute__((__target__("aes")))
14 #if (__clang_major__ >= 8)
15 #define USE_INTEL_VAES
16 #define ATTRIB_VAES __attribute__((__target__("aes,vaes,avx2")))
17 #endif
18 #endif
19 #elif defined(__GNUC__)
20 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
21 #define USE_INTEL_AES
22 #ifndef __AES__
23 #define ATTRIB_AES __attribute__((__target__("aes")))
24 #endif
25 #if (__GNUC__ >= 8)
26 #define USE_INTEL_VAES
27 #define ATTRIB_VAES __attribute__((__target__("aes,vaes,avx2")))
28 #endif
29 #endif
30 #elif defined(__INTEL_COMPILER)
31 #if (__INTEL_COMPILER >= 1110) 12 #if (__INTEL_COMPILER >= 1110)
32 #define USE_INTEL_AES 13 #define USE_INTEL_AES
33 #if (__INTEL_COMPILER >= 1900) 14 #if (__INTEL_COMPILER >= 1900)
34 #define USE_INTEL_VAES 15 #define USE_INTEL_VAES
35 #endif 16 #endif
36 #endif 17 #endif
18 #elif defined(__clang__) && (__clang_major__ > 3 || __clang_major__ == 3 && __clang_minor__ >= 8) \
19 || defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 4)
20 #define USE_INTEL_AES
21 #if !defined(__AES__)
22 #define ATTRIB_AES __attribute__((__target__("aes")))
23 #endif
24 #if defined(__clang__) && (__clang_major__ >= 8) \
25 || defined(__GNUC__) && (__GNUC__ >= 8)
26 #define USE_INTEL_VAES
27 #if !defined(__AES__) || !defined(__VAES__) || !defined(__AVX__) || !defined(__AVX2__)
28 #define ATTRIB_VAES __attribute__((__target__("aes,vaes,avx,avx2")))
29 #endif
30 #endif
37 #elif defined(_MSC_VER) 31 #elif defined(_MSC_VER)
38 #if (_MSC_VER > 1500) || (_MSC_FULL_VER >= 150030729) 32 #if (_MSC_VER > 1500) || (_MSC_FULL_VER >= 150030729)
39 #define USE_INTEL_AES 33 #define USE_INTEL_AES
@@ -56,12 +50,15 @@
56#include <wmmintrin.h> 50#include <wmmintrin.h>
57 51
58#ifndef USE_INTEL_VAES 52#ifndef USE_INTEL_VAES
59#define AES_TYPE_keys __m128i 53#define AES_TYPE_keys UInt32
60#define AES_TYPE_data __m128i 54#define AES_TYPE_data Byte
55// #define AES_TYPE_keys __m128i
56// #define AES_TYPE_data __m128i
61#endif 57#endif
62 58
63#define AES_FUNC_START(name) \ 59#define AES_FUNC_START(name) \
64 void MY_FAST_CALL name(__m128i *p, __m128i *data, size_t numBlocks) 60 void Z7_FASTCALL name(UInt32 *ivAes, Byte *data8, size_t numBlocks)
61 // void Z7_FASTCALL name(__m128i *p, __m128i *data, size_t numBlocks)
65 62
66#define AES_FUNC_START2(name) \ 63#define AES_FUNC_START2(name) \
67AES_FUNC_START (name); \ 64AES_FUNC_START (name); \
@@ -69,14 +66,16 @@ ATTRIB_AES \
69AES_FUNC_START (name) 66AES_FUNC_START (name)
70 67
71#define MM_OP(op, dest, src) dest = op(dest, src); 68#define MM_OP(op, dest, src) dest = op(dest, src);
72#define MM_OP_m(op, src) MM_OP(op, m, src); 69#define MM_OP_m(op, src) MM_OP(op, m, src)
73 70
74#define MM_XOR( dest, src) MM_OP(_mm_xor_si128, dest, src); 71#define MM_XOR( dest, src) MM_OP(_mm_xor_si128, dest, src)
75#define AVX_XOR(dest, src) MM_OP(_mm256_xor_si256, dest, src); 72#define AVX_XOR(dest, src) MM_OP(_mm256_xor_si256, dest, src)
76 73
77 74
78AES_FUNC_START2 (AesCbc_Encode_HW) 75AES_FUNC_START2 (AesCbc_Encode_HW)
79{ 76{
77 __m128i *p = (__m128i *)(void *)ivAes;
78 __m128i *data = (__m128i *)(void *)data8;
80 __m128i m = *p; 79 __m128i m = *p;
81 const __m128i k0 = p[2]; 80 const __m128i k0 = p[2];
82 const __m128i k1 = p[3]; 81 const __m128i k1 = p[3];
@@ -86,17 +85,17 @@ AES_FUNC_START2 (AesCbc_Encode_HW)
86 UInt32 r = numRounds2; 85 UInt32 r = numRounds2;
87 const __m128i *w = p + 4; 86 const __m128i *w = p + 4;
88 __m128i temp = *data; 87 __m128i temp = *data;
89 MM_XOR (temp, k0); 88 MM_XOR (temp, k0)
90 MM_XOR (m, temp); 89 MM_XOR (m, temp)
91 MM_OP_m (_mm_aesenc_si128, k1); 90 MM_OP_m (_mm_aesenc_si128, k1)
92 do 91 do
93 { 92 {
94 MM_OP_m (_mm_aesenc_si128, w[0]); 93 MM_OP_m (_mm_aesenc_si128, w[0])
95 MM_OP_m (_mm_aesenc_si128, w[1]); 94 MM_OP_m (_mm_aesenc_si128, w[1])
96 w += 2; 95 w += 2;
97 } 96 }
98 while (--r); 97 while (--r);
99 MM_OP_m (_mm_aesenclast_si128, w[0]); 98 MM_OP_m (_mm_aesenclast_si128, w[0])
100 *data = m; 99 *data = m;
101 } 100 }
102 *p = m; 101 *p = m;
@@ -104,14 +103,14 @@ AES_FUNC_START2 (AesCbc_Encode_HW)
104 103
105 104
106#define WOP_1(op) 105#define WOP_1(op)
107#define WOP_2(op) WOP_1 (op) op (m1, 1); 106#define WOP_2(op) WOP_1 (op) op (m1, 1)
108#define WOP_3(op) WOP_2 (op) op (m2, 2); 107#define WOP_3(op) WOP_2 (op) op (m2, 2)
109#define WOP_4(op) WOP_3 (op) op (m3, 3); 108#define WOP_4(op) WOP_3 (op) op (m3, 3)
110#ifdef MY_CPU_AMD64 109#ifdef MY_CPU_AMD64
111#define WOP_5(op) WOP_4 (op) op (m4, 4); 110#define WOP_5(op) WOP_4 (op) op (m4, 4)
112#define WOP_6(op) WOP_5 (op) op (m5, 5); 111#define WOP_6(op) WOP_5 (op) op (m5, 5)
113#define WOP_7(op) WOP_6 (op) op (m6, 6); 112#define WOP_7(op) WOP_6 (op) op (m6, 6)
114#define WOP_8(op) WOP_7 (op) op (m7, 7); 113#define WOP_8(op) WOP_7 (op) op (m7, 7)
115#endif 114#endif
116/* 115/*
117#define WOP_9(op) WOP_8 (op) op (m8, 8); 116#define WOP_9(op) WOP_8 (op) op (m8, 8);
@@ -130,20 +129,20 @@ AES_FUNC_START2 (AesCbc_Encode_HW)
130 #define WOP_M1 WOP_4 129 #define WOP_M1 WOP_4
131#endif 130#endif
132 131
133#define WOP(op) op (m0, 0); WOP_M1(op) 132#define WOP(op) op (m0, 0) WOP_M1(op)
134 133
135 134
136#define DECLARE_VAR(reg, ii) __m128i reg 135#define DECLARE_VAR(reg, ii) __m128i reg;
137#define LOAD_data( reg, ii) reg = data[ii]; 136#define LOAD_data( reg, ii) reg = data[ii];
138#define STORE_data( reg, ii) data[ii] = reg; 137#define STORE_data( reg, ii) data[ii] = reg;
139#if (NUM_WAYS > 1) 138#if (NUM_WAYS > 1)
140#define XOR_data_M1(reg, ii) MM_XOR (reg, data[ii- 1]); 139#define XOR_data_M1(reg, ii) MM_XOR (reg, data[ii- 1])
141#endif 140#endif
142 141
143#define AVX__DECLARE_VAR(reg, ii) __m256i reg 142#define AVX_DECLARE_VAR(reg, ii) __m256i reg;
144#define AVX__LOAD_data( reg, ii) reg = ((const __m256i *)(const void *)data)[ii]; 143#define AVX_LOAD_data( reg, ii) reg = ((const __m256i *)(const void *)data)[ii];
145#define AVX__STORE_data( reg, ii) ((__m256i *)(void *)data)[ii] = reg; 144#define AVX_STORE_data( reg, ii) ((__m256i *)(void *)data)[ii] = reg;
146#define AVX__XOR_data_M1(reg, ii) AVX_XOR (reg, (((const __m256i *)(const void *)(data - 1))[ii])); 145#define AVX_XOR_data_M1(reg, ii) AVX_XOR (reg, (((const __m256i *)(const void *)(data - 1))[ii]))
147 146
148#define MM_OP_key(op, reg) MM_OP(op, reg, key); 147#define MM_OP_key(op, reg) MM_OP(op, reg, key);
149 148
@@ -154,23 +153,23 @@ AES_FUNC_START2 (AesCbc_Encode_HW)
154#define AES_XOR( reg, ii) MM_OP_key (_mm_xor_si128, reg) 153#define AES_XOR( reg, ii) MM_OP_key (_mm_xor_si128, reg)
155 154
156 155
157#define AVX__AES_DEC( reg, ii) MM_OP_key (_mm256_aesdec_epi128, reg) 156#define AVX_AES_DEC( reg, ii) MM_OP_key (_mm256_aesdec_epi128, reg)
158#define AVX__AES_DEC_LAST( reg, ii) MM_OP_key (_mm256_aesdeclast_epi128, reg) 157#define AVX_AES_DEC_LAST( reg, ii) MM_OP_key (_mm256_aesdeclast_epi128, reg)
159#define AVX__AES_ENC( reg, ii) MM_OP_key (_mm256_aesenc_epi128, reg) 158#define AVX_AES_ENC( reg, ii) MM_OP_key (_mm256_aesenc_epi128, reg)
160#define AVX__AES_ENC_LAST( reg, ii) MM_OP_key (_mm256_aesenclast_epi128, reg) 159#define AVX_AES_ENC_LAST( reg, ii) MM_OP_key (_mm256_aesenclast_epi128, reg)
161#define AVX__AES_XOR( reg, ii) MM_OP_key (_mm256_xor_si256, reg) 160#define AVX_AES_XOR( reg, ii) MM_OP_key (_mm256_xor_si256, reg)
162 161
163#define CTR_START(reg, ii) MM_OP (_mm_add_epi64, ctr, one); reg = ctr; 162#define CTR_START(reg, ii) MM_OP (_mm_add_epi64, ctr, one) reg = ctr;
164#define CTR_END( reg, ii) MM_XOR (data[ii], reg); 163#define CTR_END( reg, ii) MM_XOR (data[ii], reg)
165 164
166#define AVX__CTR_START(reg, ii) MM_OP (_mm256_add_epi64, ctr2, two); reg = _mm256_xor_si256(ctr2, key); 165#define AVX_CTR_START(reg, ii) MM_OP (_mm256_add_epi64, ctr2, two) reg = _mm256_xor_si256(ctr2, key);
167#define AVX__CTR_END( reg, ii) AVX_XOR (((__m256i *)(void *)data)[ii], reg); 166#define AVX_CTR_END( reg, ii) AVX_XOR (((__m256i *)(void *)data)[ii], reg)
168 167
169#define WOP_KEY(op, n) { \ 168#define WOP_KEY(op, n) { \
170 const __m128i key = w[n]; \ 169 const __m128i key = w[n]; \
171 WOP(op); } 170 WOP(op); }
172 171
173#define AVX__WOP_KEY(op, n) { \ 172#define AVX_WOP_KEY(op, n) { \
174 const __m256i key = w[n]; \ 173 const __m256i key = w[n]; \
175 WOP(op); } 174 WOP(op); }
176 175
@@ -218,6 +217,8 @@ AES_FUNC_START2 (AesCbc_Encode_HW)
218 217
219AES_FUNC_START2 (AesCbc_Decode_HW) 218AES_FUNC_START2 (AesCbc_Decode_HW)
220{ 219{
220 __m128i *p = (__m128i *)(void *)ivAes;
221 __m128i *data = (__m128i *)(void *)data8;
221 __m128i iv = *p; 222 __m128i iv = *p;
222 const __m128i *wStart = p + *(const UInt32 *)(p + 1) * 2 + 2 - 1; 223 const __m128i *wStart = p + *(const UInt32 *)(p + 1) * 2 + 2 - 1;
223 const __m128i *dataEnd; 224 const __m128i *dataEnd;
@@ -228,7 +229,7 @@ AES_FUNC_START2 (AesCbc_Decode_HW)
228 const __m128i *w = wStart; 229 const __m128i *w = wStart;
229 230
230 WOP (DECLARE_VAR) 231 WOP (DECLARE_VAR)
231 WOP (LOAD_data); 232 WOP (LOAD_data)
232 WOP_KEY (AES_XOR, 1) 233 WOP_KEY (AES_XOR, 1)
233 234
234 do 235 do
@@ -239,10 +240,10 @@ AES_FUNC_START2 (AesCbc_Decode_HW)
239 while (w != p); 240 while (w != p);
240 WOP_KEY (AES_DEC_LAST, 0) 241 WOP_KEY (AES_DEC_LAST, 0)
241 242
242 MM_XOR (m0, iv); 243 MM_XOR (m0, iv)
243 WOP_M1 (XOR_data_M1) 244 WOP_M1 (XOR_data_M1)
244 iv = data[NUM_WAYS - 1]; 245 iv = data[NUM_WAYS - 1];
245 WOP (STORE_data); 246 WOP (STORE_data)
246 } 247 }
247 WIDE_LOOP_END 248 WIDE_LOOP_END
248 249
@@ -252,15 +253,15 @@ AES_FUNC_START2 (AesCbc_Decode_HW)
252 __m128i m = _mm_xor_si128 (w[2], *data); 253 __m128i m = _mm_xor_si128 (w[2], *data);
253 do 254 do
254 { 255 {
255 MM_OP_m (_mm_aesdec_si128, w[1]); 256 MM_OP_m (_mm_aesdec_si128, w[1])
256 MM_OP_m (_mm_aesdec_si128, w[0]); 257 MM_OP_m (_mm_aesdec_si128, w[0])
257 w -= 2; 258 w -= 2;
258 } 259 }
259 while (w != p); 260 while (w != p);
260 MM_OP_m (_mm_aesdec_si128, w[1]); 261 MM_OP_m (_mm_aesdec_si128, w[1])
261 MM_OP_m (_mm_aesdeclast_si128, w[0]); 262 MM_OP_m (_mm_aesdeclast_si128, w[0])
262 263
263 MM_XOR (m, iv); 264 MM_XOR (m, iv)
264 iv = *data; 265 iv = *data;
265 *data = m; 266 *data = m;
266 } 267 }
@@ -271,6 +272,8 @@ AES_FUNC_START2 (AesCbc_Decode_HW)
271 272
272AES_FUNC_START2 (AesCtr_Code_HW) 273AES_FUNC_START2 (AesCtr_Code_HW)
273{ 274{
275 __m128i *p = (__m128i *)(void *)ivAes;
276 __m128i *data = (__m128i *)(void *)data8;
274 __m128i ctr = *p; 277 __m128i ctr = *p;
275 UInt32 numRoundsMinus2 = *(const UInt32 *)(p + 1) * 2 - 1; 278 UInt32 numRoundsMinus2 = *(const UInt32 *)(p + 1) * 2 - 1;
276 const __m128i *dataEnd; 279 const __m128i *dataEnd;
@@ -283,7 +286,7 @@ AES_FUNC_START2 (AesCtr_Code_HW)
283 const __m128i *w = p; 286 const __m128i *w = p;
284 UInt32 r = numRoundsMinus2; 287 UInt32 r = numRoundsMinus2;
285 WOP (DECLARE_VAR) 288 WOP (DECLARE_VAR)
286 WOP (CTR_START); 289 WOP (CTR_START)
287 WOP_KEY (AES_XOR, 0) 290 WOP_KEY (AES_XOR, 0)
288 w += 1; 291 w += 1;
289 do 292 do
@@ -294,7 +297,7 @@ AES_FUNC_START2 (AesCtr_Code_HW)
294 while (--r); 297 while (--r);
295 WOP_KEY (AES_ENC_LAST, 0) 298 WOP_KEY (AES_ENC_LAST, 0)
296 299
297 WOP (CTR_END); 300 WOP (CTR_END)
298 } 301 }
299 WIDE_LOOP_END 302 WIDE_LOOP_END
300 303
@@ -303,19 +306,19 @@ AES_FUNC_START2 (AesCtr_Code_HW)
303 UInt32 numRounds2 = *(const UInt32 *)(p - 2 + 1) - 1; 306 UInt32 numRounds2 = *(const UInt32 *)(p - 2 + 1) - 1;
304 const __m128i *w = p; 307 const __m128i *w = p;
305 __m128i m; 308 __m128i m;
306 MM_OP (_mm_add_epi64, ctr, one); 309 MM_OP (_mm_add_epi64, ctr, one)
307 m = _mm_xor_si128 (ctr, p[0]); 310 m = _mm_xor_si128 (ctr, p[0]);
308 w += 1; 311 w += 1;
309 do 312 do
310 { 313 {
311 MM_OP_m (_mm_aesenc_si128, w[0]); 314 MM_OP_m (_mm_aesenc_si128, w[0])
312 MM_OP_m (_mm_aesenc_si128, w[1]); 315 MM_OP_m (_mm_aesenc_si128, w[1])
313 w += 2; 316 w += 2;
314 } 317 }
315 while (--numRounds2); 318 while (--numRounds2);
316 MM_OP_m (_mm_aesenc_si128, w[0]); 319 MM_OP_m (_mm_aesenc_si128, w[0])
317 MM_OP_m (_mm_aesenclast_si128, w[1]); 320 MM_OP_m (_mm_aesenclast_si128, w[1])
318 MM_XOR (*data, m); 321 MM_XOR (*data, m)
319 } 322 }
320 323
321 p[-2] = ctr; 324 p[-2] = ctr;
@@ -325,17 +328,58 @@ AES_FUNC_START2 (AesCtr_Code_HW)
325 328
326#ifdef USE_INTEL_VAES 329#ifdef USE_INTEL_VAES
327 330
331/*
332GCC before 2013-Jun:
333 <immintrin.h>:
334 #ifdef __AVX__
335 #include <avxintrin.h>
336 #endif
337GCC after 2013-Jun:
338 <immintrin.h>:
339 #include <avxintrin.h>
340CLANG 3.8+:
341{
342 <immintrin.h>:
343 #if !defined(_MSC_VER) || defined(__AVX__)
344 #include <avxintrin.h>
345 #endif
346
347 if (the compiler is clang for Windows and if global arch is not set for __AVX__)
348 [ if (defined(_MSC_VER) && !defined(__AVX__)) ]
349 {
350 <immintrin.h> doesn't include <avxintrin.h>
351 and we have 2 ways to fix it:
352 1) we can define required __AVX__ before <immintrin.h>
353 or
354 2) we can include <avxintrin.h> after <immintrin.h>
355 }
356}
357
358If we include <avxintrin.h> manually for GCC/CLANG, it's
359required that <immintrin.h> must be included before <avxintrin.h>.
360*/
361
362/*
328#if defined(__clang__) && defined(_MSC_VER) 363#if defined(__clang__) && defined(_MSC_VER)
329#define __SSE4_2__
330#define __AES__
331#define __AVX__ 364#define __AVX__
332#define __AVX2__ 365#define __AVX2__
333#define __VAES__ 366#define __VAES__
334#define __AVX512F__
335#define __AVX512VL__
336#endif 367#endif
368*/
337 369
338#include <immintrin.h> 370#include <immintrin.h>
371#if defined(__clang__) && defined(_MSC_VER)
372 #if !defined(__AVX__)
373 #include <avxintrin.h>
374 #endif
375 #if !defined(__AVX2__)
376 #include <avx2intrin.h>
377 #endif
378 #if !defined(__VAES__)
379 #include <vaesintrin.h>
380 #endif
381#endif // __clang__ && _MSC_VER
382
339 383
340#define VAES_FUNC_START2(name) \ 384#define VAES_FUNC_START2(name) \
341AES_FUNC_START (name); \ 385AES_FUNC_START (name); \
@@ -344,6 +388,8 @@ AES_FUNC_START (name)
344 388
345VAES_FUNC_START2 (AesCbc_Decode_HW_256) 389VAES_FUNC_START2 (AesCbc_Decode_HW_256)
346{ 390{
391 __m128i *p = (__m128i *)(void *)ivAes;
392 __m128i *data = (__m128i *)(void *)data8;
347 __m128i iv = *p; 393 __m128i iv = *p;
348 const __m128i *dataEnd; 394 const __m128i *dataEnd;
349 UInt32 numRounds = *(const UInt32 *)(p + 1) * 2 + 1; 395 UInt32 numRounds = *(const UInt32 *)(p + 1) * 2 + 1;
@@ -353,22 +399,22 @@ VAES_FUNC_START2 (AesCbc_Decode_HW_256)
353 { 399 {
354 const __m256i *w = keys + numRounds - 2; 400 const __m256i *w = keys + numRounds - 2;
355 401
356 WOP (AVX__DECLARE_VAR) 402 WOP (AVX_DECLARE_VAR)
357 WOP (AVX__LOAD_data); 403 WOP (AVX_LOAD_data)
358 AVX__WOP_KEY (AVX__AES_XOR, 1) 404 AVX_WOP_KEY (AVX_AES_XOR, 1)
359 405
360 do 406 do
361 { 407 {
362 AVX__WOP_KEY (AVX__AES_DEC, 0) 408 AVX_WOP_KEY (AVX_AES_DEC, 0)
363 w--; 409 w--;
364 } 410 }
365 while (w != keys); 411 while (w != keys);
366 AVX__WOP_KEY (AVX__AES_DEC_LAST, 0) 412 AVX_WOP_KEY (AVX_AES_DEC_LAST, 0)
367 413
368 AVX_XOR (m0, _mm256_setr_m128i(iv, data[0])); 414 AVX_XOR (m0, _mm256_setr_m128i(iv, data[0]))
369 WOP_M1 (AVX__XOR_data_M1) 415 WOP_M1 (AVX_XOR_data_M1)
370 iv = data[NUM_WAYS * 2 - 1]; 416 iv = data[NUM_WAYS * 2 - 1];
371 WOP (AVX__STORE_data); 417 WOP (AVX_STORE_data)
372 } 418 }
373 WIDE_LOOP_END_AVX(;) 419 WIDE_LOOP_END_AVX(;)
374 420
@@ -378,15 +424,15 @@ VAES_FUNC_START2 (AesCbc_Decode_HW_256)
378 __m128i m = _mm_xor_si128 (w[2], *data); 424 __m128i m = _mm_xor_si128 (w[2], *data);
379 do 425 do
380 { 426 {
381 MM_OP_m (_mm_aesdec_si128, w[1]); 427 MM_OP_m (_mm_aesdec_si128, w[1])
382 MM_OP_m (_mm_aesdec_si128, w[0]); 428 MM_OP_m (_mm_aesdec_si128, w[0])
383 w -= 2; 429 w -= 2;
384 } 430 }
385 while (w != p); 431 while (w != p);
386 MM_OP_m (_mm_aesdec_si128, w[1]); 432 MM_OP_m (_mm_aesdec_si128, w[1])
387 MM_OP_m (_mm_aesdeclast_si128, w[0]); 433 MM_OP_m (_mm_aesdeclast_si128, w[0])
388 434
389 MM_XOR (m, iv); 435 MM_XOR (m, iv)
390 iv = *data; 436 iv = *data;
391 *data = m; 437 *data = m;
392 } 438 }
@@ -403,18 +449,20 @@ AVX2: _mm256_add_epi64 : vpaddq ymm, ymm, ymm
403 _mm256_broadcastsi128_si256 : vbroadcasti128 449 _mm256_broadcastsi128_si256 : vbroadcasti128
404*/ 450*/
405 451
406#define AVX__CTR_LOOP_START \ 452#define AVX_CTR_LOOP_START \
407 ctr2 = _mm256_setr_m128i(_mm_sub_epi64(ctr, one), ctr); \ 453 ctr2 = _mm256_setr_m128i(_mm_sub_epi64(ctr, one), ctr); \
408 two = _mm256_setr_m128i(one, one); \ 454 two = _mm256_setr_m128i(one, one); \
409 two = _mm256_add_epi64(two, two); \ 455 two = _mm256_add_epi64(two, two); \
410 456
411// two = _mm256_setr_epi64x(2, 0, 2, 0); 457// two = _mm256_setr_epi64x(2, 0, 2, 0);
412 458
413#define AVX__CTR_LOOP_ENC \ 459#define AVX_CTR_LOOP_ENC \
414 ctr = _mm256_extracti128_si256 (ctr2, 1); \ 460 ctr = _mm256_extracti128_si256 (ctr2, 1); \
415 461
416VAES_FUNC_START2 (AesCtr_Code_HW_256) 462VAES_FUNC_START2 (AesCtr_Code_HW_256)
417{ 463{
464 __m128i *p = (__m128i *)(void *)ivAes;
465 __m128i *data = (__m128i *)(void *)data8;
418 __m128i ctr = *p; 466 __m128i ctr = *p;
419 UInt32 numRounds = *(const UInt32 *)(p + 1) * 2 + 1; 467 UInt32 numRounds = *(const UInt32 *)(p + 1) * 2 + 1;
420 const __m128i *dataEnd; 468 const __m128i *dataEnd;
@@ -422,44 +470,44 @@ VAES_FUNC_START2 (AesCtr_Code_HW_256)
422 __m256i ctr2, two; 470 __m256i ctr2, two;
423 p += 2; 471 p += 2;
424 472
425 WIDE_LOOP_START_AVX (AVX__CTR_LOOP_START) 473 WIDE_LOOP_START_AVX (AVX_CTR_LOOP_START)
426 { 474 {
427 const __m256i *w = keys; 475 const __m256i *w = keys;
428 UInt32 r = numRounds - 2; 476 UInt32 r = numRounds - 2;
429 WOP (AVX__DECLARE_VAR) 477 WOP (AVX_DECLARE_VAR)
430 AVX__WOP_KEY (AVX__CTR_START, 0); 478 AVX_WOP_KEY (AVX_CTR_START, 0)
431 479
432 w += 1; 480 w += 1;
433 do 481 do
434 { 482 {
435 AVX__WOP_KEY (AVX__AES_ENC, 0) 483 AVX_WOP_KEY (AVX_AES_ENC, 0)
436 w += 1; 484 w += 1;
437 } 485 }
438 while (--r); 486 while (--r);
439 AVX__WOP_KEY (AVX__AES_ENC_LAST, 0) 487 AVX_WOP_KEY (AVX_AES_ENC_LAST, 0)
440 488
441 WOP (AVX__CTR_END); 489 WOP (AVX_CTR_END)
442 } 490 }
443 WIDE_LOOP_END_AVX (AVX__CTR_LOOP_ENC) 491 WIDE_LOOP_END_AVX (AVX_CTR_LOOP_ENC)
444 492
445 SINGLE_LOOP 493 SINGLE_LOOP
446 { 494 {
447 UInt32 numRounds2 = *(const UInt32 *)(p - 2 + 1) - 1; 495 UInt32 numRounds2 = *(const UInt32 *)(p - 2 + 1) - 1;
448 const __m128i *w = p; 496 const __m128i *w = p;
449 __m128i m; 497 __m128i m;
450 MM_OP (_mm_add_epi64, ctr, one); 498 MM_OP (_mm_add_epi64, ctr, one)
451 m = _mm_xor_si128 (ctr, p[0]); 499 m = _mm_xor_si128 (ctr, p[0]);
452 w += 1; 500 w += 1;
453 do 501 do
454 { 502 {
455 MM_OP_m (_mm_aesenc_si128, w[0]); 503 MM_OP_m (_mm_aesenc_si128, w[0])
456 MM_OP_m (_mm_aesenc_si128, w[1]); 504 MM_OP_m (_mm_aesenc_si128, w[1])
457 w += 2; 505 w += 2;
458 } 506 }
459 while (--numRounds2); 507 while (--numRounds2);
460 MM_OP_m (_mm_aesenc_si128, w[0]); 508 MM_OP_m (_mm_aesenc_si128, w[0])
461 MM_OP_m (_mm_aesenclast_si128, w[1]); 509 MM_OP_m (_mm_aesenclast_si128, w[1])
462 MM_XOR (*data, m); 510 MM_XOR (*data, m)
463 } 511 }
464 512
465 p[-2] = ctr; 513 p[-2] = ctr;
@@ -477,7 +525,7 @@ VAES_FUNC_START2 (AesCtr_Code_HW_256)
477#define AES_TYPE_data Byte 525#define AES_TYPE_data Byte
478 526
479#define AES_FUNC_START(name) \ 527#define AES_FUNC_START(name) \
480 void MY_FAST_CALL name(UInt32 *p, Byte *data, size_t numBlocks) \ 528 void Z7_FASTCALL name(UInt32 *p, Byte *data, size_t numBlocks) \
481 529
482#define AES_COMPAT_STUB(name) \ 530#define AES_COMPAT_STUB(name) \
483 AES_FUNC_START(name); \ 531 AES_FUNC_START(name); \
@@ -496,8 +544,8 @@ AES_COMPAT_STUB (AesCtr_Code)
496#pragma message("VAES HW_SW stub was used") 544#pragma message("VAES HW_SW stub was used")
497 545
498#define VAES_COMPAT_STUB(name) \ 546#define VAES_COMPAT_STUB(name) \
499 void MY_FAST_CALL name ## _256(UInt32 *p, Byte *data, size_t numBlocks); \ 547 void Z7_FASTCALL name ## _256(UInt32 *p, Byte *data, size_t numBlocks); \
500 void MY_FAST_CALL name ## _256(UInt32 *p, Byte *data, size_t numBlocks) \ 548 void Z7_FASTCALL name ## _256(UInt32 *p, Byte *data, size_t numBlocks) \
501 { name((AES_TYPE_keys *)(void *)p, (AES_TYPE_data *)(void *)data, numBlocks); } 549 { name((AES_TYPE_keys *)(void *)p, (AES_TYPE_data *)(void *)data, numBlocks); }
502 550
503VAES_COMPAT_STUB (AesCbc_Decode_HW) 551VAES_COMPAT_STUB (AesCbc_Decode_HW)
@@ -551,7 +599,8 @@ VAES_COMPAT_STUB (AesCtr_Code_HW)
551typedef uint8x16_t v128; 599typedef uint8x16_t v128;
552 600
553#define AES_FUNC_START(name) \ 601#define AES_FUNC_START(name) \
554 void MY_FAST_CALL name(v128 *p, v128 *data, size_t numBlocks) 602 void Z7_FASTCALL name(UInt32 *ivAes, Byte *data8, size_t numBlocks)
603 // void Z7_FASTCALL name(v128 *p, v128 *data, size_t numBlocks)
555 604
556#define AES_FUNC_START2(name) \ 605#define AES_FUNC_START2(name) \
557AES_FUNC_START (name); \ 606AES_FUNC_START (name); \
@@ -559,18 +608,20 @@ ATTRIB_AES \
559AES_FUNC_START (name) 608AES_FUNC_START (name)
560 609
561#define MM_OP(op, dest, src) dest = op(dest, src); 610#define MM_OP(op, dest, src) dest = op(dest, src);
562#define MM_OP_m(op, src) MM_OP(op, m, src); 611#define MM_OP_m(op, src) MM_OP(op, m, src)
563#define MM_OP1_m(op) m = op(m); 612#define MM_OP1_m(op) m = op(m);
564 613
565#define MM_XOR( dest, src) MM_OP(veorq_u8, dest, src); 614#define MM_XOR( dest, src) MM_OP(veorq_u8, dest, src)
566#define MM_XOR_m( src) MM_XOR(m, src); 615#define MM_XOR_m( src) MM_XOR(m, src)
567 616
568#define AES_E_m(k) MM_OP_m (vaeseq_u8, k); 617#define AES_E_m(k) MM_OP_m (vaeseq_u8, k)
569#define AES_E_MC_m(k) AES_E_m (k); MM_OP1_m(vaesmcq_u8); 618#define AES_E_MC_m(k) AES_E_m (k) MM_OP1_m(vaesmcq_u8)
570 619
571 620
572AES_FUNC_START2 (AesCbc_Encode_HW) 621AES_FUNC_START2 (AesCbc_Encode_HW)
573{ 622{
623 v128 *p = (v128*)(void*)ivAes;
624 v128 *data = (v128*)(void*)data8;
574 v128 m = *p; 625 v128 m = *p;
575 const v128 k0 = p[2]; 626 const v128 k0 = p[2];
576 const v128 k1 = p[3]; 627 const v128 k1 = p[3];
@@ -608,7 +659,7 @@ AES_FUNC_START2 (AesCbc_Encode_HW)
608 AES_E_MC_m (p[14]) 659 AES_E_MC_m (p[14])
609 } 660 }
610 } 661 }
611 AES_E_m (k_z1); 662 AES_E_m (k_z1)
612 MM_XOR_m (k_z0); 663 MM_XOR_m (k_z0);
613 *data = m; 664 *data = m;
614 } 665 }
@@ -617,44 +668,44 @@ AES_FUNC_START2 (AesCbc_Encode_HW)
617 668
618 669
619#define WOP_1(op) 670#define WOP_1(op)
620#define WOP_2(op) WOP_1 (op) op (m1, 1); 671#define WOP_2(op) WOP_1 (op) op (m1, 1)
621#define WOP_3(op) WOP_2 (op) op (m2, 2); 672#define WOP_3(op) WOP_2 (op) op (m2, 2)
622#define WOP_4(op) WOP_3 (op) op (m3, 3); 673#define WOP_4(op) WOP_3 (op) op (m3, 3)
623#define WOP_5(op) WOP_4 (op) op (m4, 4); 674#define WOP_5(op) WOP_4 (op) op (m4, 4)
624#define WOP_6(op) WOP_5 (op) op (m5, 5); 675#define WOP_6(op) WOP_5 (op) op (m5, 5)
625#define WOP_7(op) WOP_6 (op) op (m6, 6); 676#define WOP_7(op) WOP_6 (op) op (m6, 6)
626#define WOP_8(op) WOP_7 (op) op (m7, 7); 677#define WOP_8(op) WOP_7 (op) op (m7, 7)
627 678
628 #define NUM_WAYS 8 679 #define NUM_WAYS 8
629 #define WOP_M1 WOP_8 680 #define WOP_M1 WOP_8
630 681
631#define WOP(op) op (m0, 0); WOP_M1(op) 682#define WOP(op) op (m0, 0) WOP_M1(op)
632 683
633#define DECLARE_VAR(reg, ii) v128 reg 684#define DECLARE_VAR(reg, ii) v128 reg;
634#define LOAD_data( reg, ii) reg = data[ii]; 685#define LOAD_data( reg, ii) reg = data[ii];
635#define STORE_data( reg, ii) data[ii] = reg; 686#define STORE_data( reg, ii) data[ii] = reg;
636#if (NUM_WAYS > 1) 687#if (NUM_WAYS > 1)
637#define XOR_data_M1(reg, ii) MM_XOR (reg, data[ii- 1]); 688#define XOR_data_M1(reg, ii) MM_XOR (reg, data[ii- 1])
638#endif 689#endif
639 690
640#define MM_OP_key(op, reg) MM_OP (op, reg, key); 691#define MM_OP_key(op, reg) MM_OP (op, reg, key)
641 692
642#define AES_D_m(k) MM_OP_m (vaesdq_u8, k); 693#define AES_D_m(k) MM_OP_m (vaesdq_u8, k)
643#define AES_D_IMC_m(k) AES_D_m (k); MM_OP1_m (vaesimcq_u8); 694#define AES_D_IMC_m(k) AES_D_m (k) MM_OP1_m (vaesimcq_u8)
644 695
645#define AES_XOR( reg, ii) MM_OP_key (veorq_u8, reg) 696#define AES_XOR( reg, ii) MM_OP_key (veorq_u8, reg)
646#define AES_D( reg, ii) MM_OP_key (vaesdq_u8, reg) 697#define AES_D( reg, ii) MM_OP_key (vaesdq_u8, reg)
647#define AES_E( reg, ii) MM_OP_key (vaeseq_u8, reg) 698#define AES_E( reg, ii) MM_OP_key (vaeseq_u8, reg)
648 699
649#define AES_D_IMC( reg, ii) AES_D (reg, ii); reg = vaesimcq_u8(reg) 700#define AES_D_IMC( reg, ii) AES_D (reg, ii) reg = vaesimcq_u8(reg);
650#define AES_E_MC( reg, ii) AES_E (reg, ii); reg = vaesmcq_u8(reg) 701#define AES_E_MC( reg, ii) AES_E (reg, ii) reg = vaesmcq_u8(reg);
651 702
652#define CTR_START(reg, ii) MM_OP (vaddq_u64, ctr, one); reg = vreinterpretq_u8_u64(ctr); 703#define CTR_START(reg, ii) MM_OP (vaddq_u64, ctr, one) reg = vreinterpretq_u8_u64(ctr);
653#define CTR_END( reg, ii) MM_XOR (data[ii], reg); 704#define CTR_END( reg, ii) MM_XOR (data[ii], reg)
654 705
655#define WOP_KEY(op, n) { \ 706#define WOP_KEY(op, n) { \
656 const v128 key = w[n]; \ 707 const v128 key = w[n]; \
657 WOP(op); } 708 WOP(op) }
658 709
659#define WIDE_LOOP_START \ 710#define WIDE_LOOP_START \
660 dataEnd = data + numBlocks; \ 711 dataEnd = data + numBlocks; \
@@ -672,6 +723,8 @@ AES_FUNC_START2 (AesCbc_Encode_HW)
672 723
673AES_FUNC_START2 (AesCbc_Decode_HW) 724AES_FUNC_START2 (AesCbc_Decode_HW)
674{ 725{
726 v128 *p = (v128*)(void*)ivAes;
727 v128 *data = (v128*)(void*)data8;
675 v128 iv = *p; 728 v128 iv = *p;
676 const v128 *wStart = p + ((size_t)*(const UInt32 *)(p + 1)) * 2; 729 const v128 *wStart = p + ((size_t)*(const UInt32 *)(p + 1)) * 2;
677 const v128 *dataEnd; 730 const v128 *dataEnd;
@@ -681,7 +734,7 @@ AES_FUNC_START2 (AesCbc_Decode_HW)
681 { 734 {
682 const v128 *w = wStart; 735 const v128 *w = wStart;
683 WOP (DECLARE_VAR) 736 WOP (DECLARE_VAR)
684 WOP (LOAD_data); 737 WOP (LOAD_data)
685 WOP_KEY (AES_D_IMC, 2) 738 WOP_KEY (AES_D_IMC, 2)
686 do 739 do
687 { 740 {
@@ -695,7 +748,7 @@ AES_FUNC_START2 (AesCbc_Decode_HW)
695 MM_XOR (m0, iv); 748 MM_XOR (m0, iv);
696 WOP_M1 (XOR_data_M1) 749 WOP_M1 (XOR_data_M1)
697 iv = data[NUM_WAYS - 1]; 750 iv = data[NUM_WAYS - 1];
698 WOP (STORE_data); 751 WOP (STORE_data)
699 } 752 }
700 WIDE_LOOP_END 753 WIDE_LOOP_END
701 754
@@ -724,6 +777,8 @@ AES_FUNC_START2 (AesCbc_Decode_HW)
724 777
725AES_FUNC_START2 (AesCtr_Code_HW) 778AES_FUNC_START2 (AesCtr_Code_HW)
726{ 779{
780 v128 *p = (v128*)(void*)ivAes;
781 v128 *data = (v128*)(void*)data8;
727 uint64x2_t ctr = vreinterpretq_u64_u8(*p); 782 uint64x2_t ctr = vreinterpretq_u64_u8(*p);
728 const v128 *wEnd = p + ((size_t)*(const UInt32 *)(p + 1)) * 2; 783 const v128 *wEnd = p + ((size_t)*(const UInt32 *)(p + 1)) * 2;
729 const v128 *dataEnd; 784 const v128 *dataEnd;
@@ -735,7 +790,7 @@ AES_FUNC_START2 (AesCtr_Code_HW)
735 { 790 {
736 const v128 *w = p; 791 const v128 *w = p;
737 WOP (DECLARE_VAR) 792 WOP (DECLARE_VAR)
738 WOP (CTR_START); 793 WOP (CTR_START)
739 do 794 do
740 { 795 {
741 WOP_KEY (AES_E_MC, 0) 796 WOP_KEY (AES_E_MC, 0)
@@ -746,7 +801,7 @@ AES_FUNC_START2 (AesCtr_Code_HW)
746 WOP_KEY (AES_E_MC, 0) 801 WOP_KEY (AES_E_MC, 0)
747 WOP_KEY (AES_E, 1) 802 WOP_KEY (AES_E, 1)
748 WOP_KEY (AES_XOR, 2) 803 WOP_KEY (AES_XOR, 2)
749 WOP (CTR_END); 804 WOP (CTR_END)
750 } 805 }
751 WIDE_LOOP_END 806 WIDE_LOOP_END
752 807
@@ -762,10 +817,10 @@ AES_FUNC_START2 (AesCtr_Code_HW)
762 w += 2; 817 w += 2;
763 } 818 }
764 while (w != wEnd); 819 while (w != wEnd);
765 AES_E_MC_m (w[0]); 820 AES_E_MC_m (w[0])
766 AES_E_m (w[1]); 821 AES_E_m (w[1])
767 MM_XOR_m (w[2]); 822 MM_XOR_m (w[2])
768 CTR_END (m, 0); 823 CTR_END (m, 0)
769 } 824 }
770 825
771 p[-2] = vreinterpretq_u8_u64(ctr); 826 p[-2] = vreinterpretq_u8_u64(ctr);
@@ -774,3 +829,12 @@ AES_FUNC_START2 (AesCtr_Code_HW)
774#endif // USE_HW_AES 829#endif // USE_HW_AES
775 830
776#endif // MY_CPU_ARM_OR_ARM64 831#endif // MY_CPU_ARM_OR_ARM64
832
833#undef NUM_WAYS
834#undef WOP_M1
835#undef WOP
836#undef DECLARE_VAR
837#undef LOAD_data
838#undef STORE_data
839#undef USE_INTEL_AES
840#undef USE_HW_AES
diff --git a/C/Alloc.c b/C/Alloc.c
index d1af76c..d841bf2 100644
--- a/C/Alloc.c
+++ b/C/Alloc.c
@@ -1,38 +1,54 @@
1/* Alloc.c -- Memory allocation functions 1/* Alloc.c -- Memory allocation functions
22021-07-13 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#include <stdio.h>
7
8#ifdef _WIN32 6#ifdef _WIN32
9#include <Windows.h> 7#include "7zWindows.h"
10#endif 8#endif
11#include <stdlib.h> 9#include <stdlib.h>
12 10
13#include "Alloc.h" 11#include "Alloc.h"
14 12
15/* #define _SZ_ALLOC_DEBUG */ 13#ifdef _WIN32
14#ifdef Z7_LARGE_PAGES
15#if defined(__clang__) || defined(__GNUC__)
16typedef void (*Z7_voidFunction)(void);
17#define MY_CAST_FUNC (Z7_voidFunction)
18#elif defined(_MSC_VER) && _MSC_VER > 1920
19#define MY_CAST_FUNC (void *)
20// #pragma warning(disable : 4191) // 'type cast': unsafe conversion from 'FARPROC' to 'void (__cdecl *)()'
21#else
22#define MY_CAST_FUNC
23#endif
24#endif // Z7_LARGE_PAGES
25#endif // _WIN32
26
27// #define SZ_ALLOC_DEBUG
28/* #define SZ_ALLOC_DEBUG */
16 29
17/* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ 30/* use SZ_ALLOC_DEBUG to debug alloc/free operations */
18#ifdef _SZ_ALLOC_DEBUG 31#ifdef SZ_ALLOC_DEBUG
19 32
33#include <string.h>
20#include <stdio.h> 34#include <stdio.h>
21int g_allocCount = 0; 35static int g_allocCount = 0;
22int g_allocCountMid = 0; 36#ifdef _WIN32
23int g_allocCountBig = 0; 37static int g_allocCountMid = 0;
38static int g_allocCountBig = 0;
39#endif
24 40
25 41
26#define CONVERT_INT_TO_STR(charType, tempSize) \ 42#define CONVERT_INT_TO_STR(charType, tempSize) \
27 unsigned char temp[tempSize]; unsigned i = 0; \ 43 char temp[tempSize]; unsigned i = 0; \
28 while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \ 44 while (val >= 10) { temp[i++] = (char)('0' + (unsigned)(val % 10)); val /= 10; } \
29 *s++ = (charType)('0' + (unsigned)val); \ 45 *s++ = (charType)('0' + (unsigned)val); \
30 while (i != 0) { i--; *s++ = temp[i]; } \ 46 while (i != 0) { i--; *s++ = temp[i]; } \
31 *s = 0; 47 *s = 0;
32 48
33static void ConvertUInt64ToString(UInt64 val, char *s) 49static void ConvertUInt64ToString(UInt64 val, char *s)
34{ 50{
35 CONVERT_INT_TO_STR(char, 24); 51 CONVERT_INT_TO_STR(char, 24)
36} 52}
37 53
38#define GET_HEX_CHAR(t) ((char)(((t < 10) ? ('0' + t) : ('A' + (t - 10))))) 54#define GET_HEX_CHAR(t) ((char)(((t < 10) ? ('0' + t) : ('A' + (t - 10)))))
@@ -77,7 +93,7 @@ static void PrintAligned(const char *s, size_t align)
77 Print(s); 93 Print(s);
78} 94}
79 95
80static void PrintLn() 96static void PrintLn(void)
81{ 97{
82 Print("\n"); 98 Print("\n");
83} 99}
@@ -89,10 +105,10 @@ static void PrintHex(UInt64 v, size_t align)
89 PrintAligned(s, align); 105 PrintAligned(s, align);
90} 106}
91 107
92static void PrintDec(UInt64 v, size_t align) 108static void PrintDec(int v, size_t align)
93{ 109{
94 char s[32]; 110 char s[32];
95 ConvertUInt64ToString(v, s); 111 ConvertUInt64ToString((unsigned)v, s);
96 PrintAligned(s, align); 112 PrintAligned(s, align);
97} 113}
98 114
@@ -102,12 +118,19 @@ static void PrintAddr(void *p)
102} 118}
103 119
104 120
105#define PRINT_ALLOC(name, cnt, size, ptr) \ 121#define PRINT_REALLOC(name, cnt, size, ptr) { \
122 Print(name " "); \
123 if (!ptr) PrintDec(cnt++, 10); \
124 PrintHex(size, 10); \
125 PrintAddr(ptr); \
126 PrintLn(); }
127
128#define PRINT_ALLOC(name, cnt, size, ptr) { \
106 Print(name " "); \ 129 Print(name " "); \
107 PrintDec(cnt++, 10); \ 130 PrintDec(cnt++, 10); \
108 PrintHex(size, 10); \ 131 PrintHex(size, 10); \
109 PrintAddr(ptr); \ 132 PrintAddr(ptr); \
110 PrintLn(); 133 PrintLn(); }
111 134
112#define PRINT_FREE(name, cnt, ptr) if (ptr) { \ 135#define PRINT_FREE(name, cnt, ptr) if (ptr) { \
113 Print(name " "); \ 136 Print(name " "); \
@@ -117,7 +140,9 @@ static void PrintAddr(void *p)
117 140
118#else 141#else
119 142
143#ifdef _WIN32
120#define PRINT_ALLOC(name, cnt, size, ptr) 144#define PRINT_ALLOC(name, cnt, size, ptr)
145#endif
121#define PRINT_FREE(name, cnt, ptr) 146#define PRINT_FREE(name, cnt, ptr)
122#define Print(s) 147#define Print(s)
123#define PrintLn() 148#define PrintLn()
@@ -127,16 +152,31 @@ static void PrintAddr(void *p)
127#endif 152#endif
128 153
129 154
155/*
156by specification:
157 malloc(non_NULL, 0) : returns NULL or a unique pointer value that can later be successfully passed to free()
158 realloc(NULL, size) : the call is equivalent to malloc(size)
159 realloc(non_NULL, 0) : the call is equivalent to free(ptr)
160
161in main compilers:
162 malloc(0) : returns non_NULL
163 realloc(NULL, 0) : returns non_NULL
164 realloc(non_NULL, 0) : returns NULL
165*/
166
130 167
131void *MyAlloc(size_t size) 168void *MyAlloc(size_t size)
132{ 169{
133 if (size == 0) 170 if (size == 0)
134 return NULL; 171 return NULL;
135 PRINT_ALLOC("Alloc ", g_allocCount, size, NULL); 172 // PRINT_ALLOC("Alloc ", g_allocCount, size, NULL)
136 #ifdef _SZ_ALLOC_DEBUG 173 #ifdef SZ_ALLOC_DEBUG
137 { 174 {
138 void *p = malloc(size); 175 void *p = malloc(size);
139 // PRINT_ALLOC("Alloc ", g_allocCount, size, p); 176 if (p)
177 {
178 PRINT_ALLOC("Alloc ", g_allocCount, size, p)
179 }
140 return p; 180 return p;
141 } 181 }
142 #else 182 #else
@@ -146,33 +186,64 @@ void *MyAlloc(size_t size)
146 186
147void MyFree(void *address) 187void MyFree(void *address)
148{ 188{
149 PRINT_FREE("Free ", g_allocCount, address); 189 PRINT_FREE("Free ", g_allocCount, address)
150 190
151 free(address); 191 free(address);
152} 192}
153 193
194void *MyRealloc(void *address, size_t size)
195{
196 if (size == 0)
197 {
198 MyFree(address);
199 return NULL;
200 }
201 // PRINT_REALLOC("Realloc ", g_allocCount, size, address)
202 #ifdef SZ_ALLOC_DEBUG
203 {
204 void *p = realloc(address, size);
205 if (p)
206 {
207 PRINT_REALLOC("Realloc ", g_allocCount, size, address)
208 }
209 return p;
210 }
211 #else
212 return realloc(address, size);
213 #endif
214}
215
216
154#ifdef _WIN32 217#ifdef _WIN32
155 218
156void *MidAlloc(size_t size) 219void *MidAlloc(size_t size)
157{ 220{
158 if (size == 0) 221 if (size == 0)
159 return NULL; 222 return NULL;
160 223 #ifdef SZ_ALLOC_DEBUG
161 PRINT_ALLOC("Alloc-Mid", g_allocCountMid, size, NULL); 224 {
162 225 void *p = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
226 if (p)
227 {
228 PRINT_ALLOC("Alloc-Mid", g_allocCountMid, size, p)
229 }
230 return p;
231 }
232 #else
163 return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); 233 return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
234 #endif
164} 235}
165 236
166void MidFree(void *address) 237void MidFree(void *address)
167{ 238{
168 PRINT_FREE("Free-Mid", g_allocCountMid, address); 239 PRINT_FREE("Free-Mid", g_allocCountMid, address)
169 240
170 if (!address) 241 if (!address)
171 return; 242 return;
172 VirtualFree(address, 0, MEM_RELEASE); 243 VirtualFree(address, 0, MEM_RELEASE);
173} 244}
174 245
175#ifdef _7ZIP_LARGE_PAGES 246#ifdef Z7_LARGE_PAGES
176 247
177#ifdef MEM_LARGE_PAGES 248#ifdef MEM_LARGE_PAGES
178 #define MY__MEM_LARGE_PAGES MEM_LARGE_PAGES 249 #define MY__MEM_LARGE_PAGES MEM_LARGE_PAGES
@@ -183,34 +254,35 @@ void MidFree(void *address)
183extern 254extern
184SIZE_T g_LargePageSize; 255SIZE_T g_LargePageSize;
185SIZE_T g_LargePageSize = 0; 256SIZE_T g_LargePageSize = 0;
186typedef SIZE_T (WINAPI *GetLargePageMinimumP)(VOID); 257typedef SIZE_T (WINAPI *Func_GetLargePageMinimum)(VOID);
187 258
188#endif // _7ZIP_LARGE_PAGES 259void SetLargePageSize(void)
189
190void SetLargePageSize()
191{ 260{
192 #ifdef _7ZIP_LARGE_PAGES 261 #ifdef Z7_LARGE_PAGES
193 SIZE_T size; 262 SIZE_T size;
194 GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) 263 const
195 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum"); 264 Func_GetLargePageMinimum fn =
196 if (!largePageMinimum) 265 (Func_GetLargePageMinimum) MY_CAST_FUNC GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
266 "GetLargePageMinimum");
267 if (!fn)
197 return; 268 return;
198 size = largePageMinimum(); 269 size = fn();
199 if (size == 0 || (size & (size - 1)) != 0) 270 if (size == 0 || (size & (size - 1)) != 0)
200 return; 271 return;
201 g_LargePageSize = size; 272 g_LargePageSize = size;
202 #endif 273 #endif
203} 274}
204 275
276#endif // Z7_LARGE_PAGES
205 277
206void *BigAlloc(size_t size) 278void *BigAlloc(size_t size)
207{ 279{
208 if (size == 0) 280 if (size == 0)
209 return NULL; 281 return NULL;
210 282
211 PRINT_ALLOC("Alloc-Big", g_allocCountBig, size, NULL); 283 PRINT_ALLOC("Alloc-Big", g_allocCountBig, size, NULL)
212 284
213 #ifdef _7ZIP_LARGE_PAGES 285 #ifdef Z7_LARGE_PAGES
214 { 286 {
215 SIZE_T ps = g_LargePageSize; 287 SIZE_T ps = g_LargePageSize;
216 if (ps != 0 && ps <= (1 << 30) && size > (ps / 2)) 288 if (ps != 0 && ps <= (1 << 30) && size > (ps / 2))
@@ -220,38 +292,38 @@ void *BigAlloc(size_t size)
220 size2 = (size + ps) & ~ps; 292 size2 = (size + ps) & ~ps;
221 if (size2 >= size) 293 if (size2 >= size)
222 { 294 {
223 void *res = VirtualAlloc(NULL, size2, MEM_COMMIT | MY__MEM_LARGE_PAGES, PAGE_READWRITE); 295 void *p = VirtualAlloc(NULL, size2, MEM_COMMIT | MY__MEM_LARGE_PAGES, PAGE_READWRITE);
224 if (res) 296 if (p)
225 return res; 297 {
298 PRINT_ALLOC("Alloc-BM ", g_allocCountMid, size2, p)
299 return p;
300 }
226 } 301 }
227 } 302 }
228 } 303 }
229 #endif 304 #endif
230 305
231 return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); 306 return MidAlloc(size);
232} 307}
233 308
234void BigFree(void *address) 309void BigFree(void *address)
235{ 310{
236 PRINT_FREE("Free-Big", g_allocCountBig, address); 311 PRINT_FREE("Free-Big", g_allocCountBig, address)
237 312 MidFree(address);
238 if (!address)
239 return;
240 VirtualFree(address, 0, MEM_RELEASE);
241} 313}
242 314
243#endif 315#endif // _WIN32
244 316
245 317
246static void *SzAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p); return MyAlloc(size); } 318static void *SzAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p) return MyAlloc(size); }
247static void SzFree(ISzAllocPtr p, void *address) { UNUSED_VAR(p); MyFree(address); } 319static void SzFree(ISzAllocPtr p, void *address) { UNUSED_VAR(p) MyFree(address); }
248const ISzAlloc g_Alloc = { SzAlloc, SzFree }; 320const ISzAlloc g_Alloc = { SzAlloc, SzFree };
249 321
250#ifdef _WIN32 322#ifdef _WIN32
251static void *SzMidAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p); return MidAlloc(size); } 323static void *SzMidAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p) return MidAlloc(size); }
252static void SzMidFree(ISzAllocPtr p, void *address) { UNUSED_VAR(p); MidFree(address); } 324static void SzMidFree(ISzAllocPtr p, void *address) { UNUSED_VAR(p) MidFree(address); }
253static void *SzBigAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p); return BigAlloc(size); } 325static void *SzBigAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p) return BigAlloc(size); }
254static void SzBigFree(ISzAllocPtr p, void *address) { UNUSED_VAR(p); BigFree(address); } 326static void SzBigFree(ISzAllocPtr p, void *address) { UNUSED_VAR(p) BigFree(address); }
255const ISzAlloc g_MidAlloc = { SzMidAlloc, SzMidFree }; 327const ISzAlloc g_MidAlloc = { SzMidAlloc, SzMidFree };
256const ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree }; 328const ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree };
257#endif 329#endif
@@ -334,7 +406,7 @@ static void *SzAlignedAlloc(ISzAllocPtr pp, size_t size)
334 void *p; 406 void *p;
335 void *pAligned; 407 void *pAligned;
336 size_t newSize; 408 size_t newSize;
337 UNUSED_VAR(pp); 409 UNUSED_VAR(pp)
338 410
339 /* also we can allocate additional dummy ALLOC_ALIGN_SIZE bytes after aligned 411 /* also we can allocate additional dummy ALLOC_ALIGN_SIZE bytes after aligned
340 block to prevent cache line sharing with another allocated blocks */ 412 block to prevent cache line sharing with another allocated blocks */
@@ -362,7 +434,7 @@ static void *SzAlignedAlloc(ISzAllocPtr pp, size_t size)
362 #else 434 #else
363 435
364 void *p; 436 void *p;
365 UNUSED_VAR(pp); 437 UNUSED_VAR(pp)
366 if (posix_memalign(&p, ALLOC_ALIGN_SIZE, size)) 438 if (posix_memalign(&p, ALLOC_ALIGN_SIZE, size))
367 return NULL; 439 return NULL;
368 440
@@ -377,7 +449,7 @@ static void *SzAlignedAlloc(ISzAllocPtr pp, size_t size)
377 449
378static void SzAlignedFree(ISzAllocPtr pp, void *address) 450static void SzAlignedFree(ISzAllocPtr pp, void *address)
379{ 451{
380 UNUSED_VAR(pp); 452 UNUSED_VAR(pp)
381 #ifndef USE_posix_memalign 453 #ifndef USE_posix_memalign
382 if (address) 454 if (address)
383 MyFree(((void **)address)[-1]); 455 MyFree(((void **)address)[-1]);
@@ -401,7 +473,7 @@ const ISzAlloc g_AlignedAlloc = { SzAlignedAlloc, SzAlignedFree };
401 473
402static void *AlignOffsetAlloc_Alloc(ISzAllocPtr pp, size_t size) 474static void *AlignOffsetAlloc_Alloc(ISzAllocPtr pp, size_t size)
403{ 475{
404 CAlignOffsetAlloc *p = CONTAINER_FROM_VTBL(pp, CAlignOffsetAlloc, vt); 476 const CAlignOffsetAlloc *p = Z7_CONTAINER_FROM_VTBL_CONST(pp, CAlignOffsetAlloc, vt);
405 void *adr; 477 void *adr;
406 void *pAligned; 478 void *pAligned;
407 size_t newSize; 479 size_t newSize;
@@ -447,7 +519,7 @@ static void AlignOffsetAlloc_Free(ISzAllocPtr pp, void *address)
447{ 519{
448 if (address) 520 if (address)
449 { 521 {
450 CAlignOffsetAlloc *p = CONTAINER_FROM_VTBL(pp, CAlignOffsetAlloc, vt); 522 const CAlignOffsetAlloc *p = Z7_CONTAINER_FROM_VTBL_CONST(pp, CAlignOffsetAlloc, vt);
451 PrintLn(); 523 PrintLn();
452 Print("- Aligned Free: "); 524 Print("- Aligned Free: ");
453 PrintLn(); 525 PrintLn();
diff --git a/C/Alloc.h b/C/Alloc.h
index 3be2041..fac5b62 100644
--- a/C/Alloc.h
+++ b/C/Alloc.h
@@ -1,19 +1,32 @@
1/* Alloc.h -- Memory allocation functions 1/* Alloc.h -- Memory allocation functions
22021-07-13 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#ifndef __COMMON_ALLOC_H 4#ifndef ZIP7_INC_ALLOC_H
5#define __COMMON_ALLOC_H 5#define ZIP7_INC_ALLOC_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
9EXTERN_C_BEGIN 9EXTERN_C_BEGIN
10 10
11/*
12 MyFree(NULL) : is allowed, as free(NULL)
13 MyAlloc(0) : returns NULL : but malloc(0) is allowed to return NULL or non_NULL
14 MyRealloc(NULL, 0) : returns NULL : but realloc(NULL, 0) is allowed to return NULL or non_NULL
15MyRealloc() is similar to realloc() for the following cases:
16 MyRealloc(non_NULL, 0) : returns NULL and always calls MyFree(ptr)
17 MyRealloc(NULL, non_ZERO) : returns NULL, if allocation failed
18 MyRealloc(non_NULL, non_ZERO) : returns NULL, if reallocation failed
19*/
20
11void *MyAlloc(size_t size); 21void *MyAlloc(size_t size);
12void MyFree(void *address); 22void MyFree(void *address);
23void *MyRealloc(void *address, size_t size);
13 24
14#ifdef _WIN32 25#ifdef _WIN32
15 26
27#ifdef Z7_LARGE_PAGES
16void SetLargePageSize(void); 28void SetLargePageSize(void);
29#endif
17 30
18void *MidAlloc(size_t size); 31void *MidAlloc(size_t size);
19void MidFree(void *address); 32void MidFree(void *address);
diff --git a/C/Bcj2.c b/C/Bcj2.c
index c7b9567..7cb57ad 100644
--- a/C/Bcj2.c
+++ b/C/Bcj2.c
@@ -1,29 +1,24 @@
1/* Bcj2.c -- BCJ2 Decoder (Converter for x86 code) 1/* Bcj2.c -- BCJ2 Decoder (Converter for x86 code)
22021-02-09 : Igor Pavlov : Public domain */ 22023-03-01 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#include "Bcj2.h" 6#include "Bcj2.h"
7#include "CpuArch.h" 7#include "CpuArch.h"
8 8
9#define CProb UInt16
10
11#define kTopValue ((UInt32)1 << 24) 9#define kTopValue ((UInt32)1 << 24)
12#define kNumModelBits 11 10#define kNumBitModelTotalBits 11
13#define kBitModelTotal (1 << kNumModelBits) 11#define kBitModelTotal (1 << kNumBitModelTotalBits)
14#define kNumMoveBits 5 12#define kNumMoveBits 5
15 13
16#define _IF_BIT_0 ttt = *prob; bound = (p->range >> kNumModelBits) * ttt; if (p->code < bound) 14// UInt32 bcj2_stats[256 + 2][2];
17#define _UPDATE_0 p->range = bound; *prob = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
18#define _UPDATE_1 p->range -= bound; p->code -= bound; *prob = (CProb)(ttt - (ttt >> kNumMoveBits));
19 15
20void Bcj2Dec_Init(CBcj2Dec *p) 16void Bcj2Dec_Init(CBcj2Dec *p)
21{ 17{
22 unsigned i; 18 unsigned i;
23 19 p->state = BCJ2_STREAM_RC; // BCJ2_DEC_STATE_OK;
24 p->state = BCJ2_DEC_STATE_OK;
25 p->ip = 0; 20 p->ip = 0;
26 p->temp[3] = 0; 21 p->temp = 0;
27 p->range = 0; 22 p->range = 0;
28 p->code = 0; 23 p->code = 0;
29 for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++) 24 for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++)
@@ -32,217 +27,248 @@ void Bcj2Dec_Init(CBcj2Dec *p)
32 27
33SRes Bcj2Dec_Decode(CBcj2Dec *p) 28SRes Bcj2Dec_Decode(CBcj2Dec *p)
34{ 29{
30 UInt32 v = p->temp;
31 // const Byte *src;
35 if (p->range <= 5) 32 if (p->range <= 5)
36 { 33 {
37 p->state = BCJ2_DEC_STATE_OK; 34 UInt32 code = p->code;
35 p->state = BCJ2_DEC_STATE_ERROR; /* for case if we return SZ_ERROR_DATA; */
38 for (; p->range != 5; p->range++) 36 for (; p->range != 5; p->range++)
39 { 37 {
40 if (p->range == 1 && p->code != 0) 38 if (p->range == 1 && code != 0)
41 return SZ_ERROR_DATA; 39 return SZ_ERROR_DATA;
42
43 if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC]) 40 if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
44 { 41 {
45 p->state = BCJ2_STREAM_RC; 42 p->state = BCJ2_STREAM_RC;
46 return SZ_OK; 43 return SZ_OK;
47 } 44 }
48 45 code = (code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
49 p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++; 46 p->code = code;
50 } 47 }
51 48 if (code == 0xffffffff)
52 if (p->code == 0xFFFFFFFF)
53 return SZ_ERROR_DATA; 49 return SZ_ERROR_DATA;
54 50 p->range = 0xffffffff;
55 p->range = 0xFFFFFFFF;
56 } 51 }
57 else if (p->state >= BCJ2_DEC_STATE_ORIG_0) 52 // else
58 { 53 {
59 while (p->state <= BCJ2_DEC_STATE_ORIG_3) 54 unsigned state = p->state;
55 // we check BCJ2_IS_32BIT_STREAM() here instead of check in the main loop
56 if (BCJ2_IS_32BIT_STREAM(state))
60 { 57 {
61 Byte *dest = p->dest; 58 const Byte *cur = p->bufs[state];
62 if (dest == p->destLim) 59 if (cur == p->lims[state])
63 return SZ_OK; 60 return SZ_OK;
64 *dest = p->temp[(size_t)p->state - BCJ2_DEC_STATE_ORIG_0]; 61 p->bufs[state] = cur + 4;
65 p->state++; 62 {
66 p->dest = dest + 1; 63 const UInt32 ip = p->ip + 4;
64 v = GetBe32a(cur) - ip;
65 p->ip = ip;
66 }
67 state = BCJ2_DEC_STATE_ORIG_0;
67 } 68 }
68 } 69 if ((unsigned)(state - BCJ2_DEC_STATE_ORIG_0) < 4)
69
70 /*
71 if (BCJ2_IS_32BIT_STREAM(p->state))
72 {
73 const Byte *cur = p->bufs[p->state];
74 if (cur == p->lims[p->state])
75 return SZ_OK;
76 p->bufs[p->state] = cur + 4;
77
78 { 70 {
79 UInt32 val; 71 Byte *dest = p->dest;
80 Byte *dest; 72 for (;;)
81 SizeT rem;
82
83 p->ip += 4;
84 val = GetBe32(cur) - p->ip;
85 dest = p->dest;
86 rem = p->destLim - dest;
87 if (rem < 4)
88 { 73 {
89 SizeT i; 74 if (dest == p->destLim)
90 SetUi32(p->temp, val); 75 {
91 for (i = 0; i < rem; i++) 76 p->state = state;
92 dest[i] = p->temp[i]; 77 p->temp = v;
93 p->dest = dest + rem; 78 return SZ_OK;
94 p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem; 79 }
95 return SZ_OK; 80 *dest++ = (Byte)v;
81 p->dest = dest;
82 if (++state == BCJ2_DEC_STATE_ORIG_3 + 1)
83 break;
84 v >>= 8;
96 } 85 }
97 SetUi32(dest, val);
98 p->temp[3] = (Byte)(val >> 24);
99 p->dest = dest + 4;
100 p->state = BCJ2_DEC_STATE_OK;
101 } 86 }
102 } 87 }
103 */
104 88
89 // src = p->bufs[BCJ2_STREAM_MAIN];
105 for (;;) 90 for (;;)
106 { 91 {
92 /*
107 if (BCJ2_IS_32BIT_STREAM(p->state)) 93 if (BCJ2_IS_32BIT_STREAM(p->state))
108 p->state = BCJ2_DEC_STATE_OK; 94 p->state = BCJ2_DEC_STATE_OK;
109 else 95 else
96 */
110 { 97 {
111 if (p->range < kTopValue) 98 if (p->range < kTopValue)
112 { 99 {
113 if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC]) 100 if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
114 { 101 {
115 p->state = BCJ2_STREAM_RC; 102 p->state = BCJ2_STREAM_RC;
103 p->temp = v;
116 return SZ_OK; 104 return SZ_OK;
117 } 105 }
118 p->range <<= 8; 106 p->range <<= 8;
119 p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++; 107 p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
120 } 108 }
121
122 { 109 {
123 const Byte *src = p->bufs[BCJ2_STREAM_MAIN]; 110 const Byte *src = p->bufs[BCJ2_STREAM_MAIN];
124 const Byte *srcLim; 111 const Byte *srcLim;
125 Byte *dest; 112 Byte *dest = p->dest;
126 SizeT num = (SizeT)(p->lims[BCJ2_STREAM_MAIN] - src);
127
128 if (num == 0)
129 { 113 {
130 p->state = BCJ2_STREAM_MAIN; 114 const SizeT rem = (SizeT)(p->lims[BCJ2_STREAM_MAIN] - src);
131 return SZ_OK; 115 SizeT num = (SizeT)(p->destLim - dest);
116 if (num >= rem)
117 num = rem;
118 #define NUM_ITERS 4
119 #if (NUM_ITERS & (NUM_ITERS - 1)) == 0
120 num &= ~((SizeT)NUM_ITERS - 1); // if (NUM_ITERS == (1 << x))
121 #else
122 num -= num % NUM_ITERS; // if (NUM_ITERS != (1 << x))
123 #endif
124 srcLim = src + num;
132 } 125 }
133 126
134 dest = p->dest; 127 #define NUM_SHIFT_BITS 24
135 if (num > (SizeT)(p->destLim - dest)) 128 #define ONE_ITER(indx) { \
129 const unsigned b = src[indx]; \
130 *dest++ = (Byte)b; \
131 v = (v << NUM_SHIFT_BITS) | b; \
132 if (((b + (0x100 - 0xe8)) & 0xfe) == 0) break; \
133 if (((v - (((UInt32)0x0f << (NUM_SHIFT_BITS)) + 0x80)) & \
134 ((((UInt32)1 << (4 + NUM_SHIFT_BITS)) - 0x1) << 4)) == 0) break; \
135 /* ++dest */; /* v = b; */ }
136
137 if (src != srcLim)
138 for (;;)
136 { 139 {
137 num = (SizeT)(p->destLim - dest); 140 /* The dependency chain of 2-cycle for (v) calculation is not big problem here.
138 if (num == 0) 141 But we can remove dependency chain with v = b in the end of loop. */
139 { 142 ONE_ITER(0)
140 p->state = BCJ2_DEC_STATE_ORIG; 143 #if (NUM_ITERS > 1)
141 return SZ_OK; 144 ONE_ITER(1)
142 } 145 #if (NUM_ITERS > 2)
146 ONE_ITER(2)
147 #if (NUM_ITERS > 3)
148 ONE_ITER(3)
149 #if (NUM_ITERS > 4)
150 ONE_ITER(4)
151 #if (NUM_ITERS > 5)
152 ONE_ITER(5)
153 #if (NUM_ITERS > 6)
154 ONE_ITER(6)
155 #if (NUM_ITERS > 7)
156 ONE_ITER(7)
157 #endif
158 #endif
159 #endif
160 #endif
161 #endif
162 #endif
163 #endif
164
165 src += NUM_ITERS;
166 if (src == srcLim)
167 break;
143 } 168 }
144
145 srcLim = src + num;
146 169
147 if (p->temp[3] == 0x0F && (src[0] & 0xF0) == 0x80) 170 if (src == srcLim)
148 *dest = src[0]; 171 #if (NUM_ITERS > 1)
149 else for (;;) 172 for (;;)
173 #endif
150 { 174 {
151 Byte b = *src; 175 #if (NUM_ITERS > 1)
152 *dest = b; 176 if (src == p->lims[BCJ2_STREAM_MAIN] || dest == p->destLim)
153 if (b != 0x0F) 177 #endif
154 { 178 {
155 if ((b & 0xFE) == 0xE8) 179 const SizeT num = (SizeT)(src - p->bufs[BCJ2_STREAM_MAIN]);
156 break; 180 p->bufs[BCJ2_STREAM_MAIN] = src;
157 dest++; 181 p->dest = dest;
158 if (++src != srcLim) 182 p->ip += (UInt32)num;
159 continue; 183 /* state BCJ2_STREAM_MAIN has more priority than BCJ2_STATE_ORIG */
160 break; 184 p->state =
185 src == p->lims[BCJ2_STREAM_MAIN] ?
186 (unsigned)BCJ2_STREAM_MAIN :
187 (unsigned)BCJ2_DEC_STATE_ORIG;
188 p->temp = v;
189 return SZ_OK;
161 } 190 }
162 dest++; 191 #if (NUM_ITERS > 1)
163 if (++src == srcLim) 192 ONE_ITER(0)
164 break; 193 src++;
165 if ((*src & 0xF0) != 0x80) 194 #endif
166 continue;
167 *dest = *src;
168 break;
169 } 195 }
170 196
171 num = (SizeT)(src - p->bufs[BCJ2_STREAM_MAIN]);
172
173 if (src == srcLim)
174 { 197 {
175 p->temp[3] = src[-1]; 198 const SizeT num = (SizeT)(dest - p->dest);
176 p->bufs[BCJ2_STREAM_MAIN] = src; 199 p->dest = dest; // p->dest += num;
200 p->bufs[BCJ2_STREAM_MAIN] += num; // = src;
177 p->ip += (UInt32)num; 201 p->ip += (UInt32)num;
178 p->dest += num;
179 p->state =
180 p->bufs[BCJ2_STREAM_MAIN] ==
181 p->lims[BCJ2_STREAM_MAIN] ?
182 (unsigned)BCJ2_STREAM_MAIN :
183 (unsigned)BCJ2_DEC_STATE_ORIG;
184 return SZ_OK;
185 } 202 }
186
187 { 203 {
188 UInt32 bound, ttt; 204 UInt32 bound, ttt;
189 CProb *prob; 205 CBcj2Prob *prob; // unsigned index;
190 Byte b = src[0]; 206 /*
191 Byte prev = (Byte)(num == 0 ? p->temp[3] : src[-1]); 207 prob = p->probs + (unsigned)((Byte)v == 0xe8 ?
192 208 2 + (Byte)(v >> 8) :
193 p->temp[3] = b; 209 ((v >> 5) & 1)); // ((Byte)v < 0xe8 ? 0 : 1));
194 p->bufs[BCJ2_STREAM_MAIN] = src + 1; 210 */
195 num++;
196 p->ip += (UInt32)num;
197 p->dest += num;
198
199 prob = p->probs + (unsigned)(b == 0xE8 ? 2 + (unsigned)prev : (b == 0xE9 ? 1 : 0));
200
201 _IF_BIT_0
202 { 211 {
203 _UPDATE_0 212 const unsigned c = ((v + 0x17) >> 6) & 1;
213 prob = p->probs + (unsigned)
214 (((0 - c) & (Byte)(v >> NUM_SHIFT_BITS)) + c + ((v >> 5) & 1));
215 // (Byte)
216 // 8x->0 : e9->1 : xxe8->xx+2
217 // 8x->0x100 : e9->0x101 : xxe8->xx
218 // (((0x100 - (e & ~v)) & (0x100 | (v >> 8))) + (e & v));
219 // (((0x101 + (~e | v)) & (0x100 | (v >> 8))) + (e & v));
220 }
221 ttt = *prob;
222 bound = (p->range >> kNumBitModelTotalBits) * ttt;
223 if (p->code < bound)
224 {
225 // bcj2_stats[prob - p->probs][0]++;
226 p->range = bound;
227 *prob = (CBcj2Prob)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
204 continue; 228 continue;
205 } 229 }
206 _UPDATE_1 230 {
207 231 // bcj2_stats[prob - p->probs][1]++;
232 p->range -= bound;
233 p->code -= bound;
234 *prob = (CBcj2Prob)(ttt - (ttt >> kNumMoveBits));
235 }
208 } 236 }
209 } 237 }
210 } 238 }
211
212 { 239 {
213 UInt32 val; 240 /* (v == 0xe8 ? 0 : 1) uses setcc instruction with additional zero register usage in x64 MSVC. */
214 unsigned cj = (p->temp[3] == 0xE8) ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP; 241 // const unsigned cj = ((Byte)v == 0xe8) ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP;
242 const unsigned cj = (((v + 0x57) >> 6) & 1) + BCJ2_STREAM_CALL;
215 const Byte *cur = p->bufs[cj]; 243 const Byte *cur = p->bufs[cj];
216 Byte *dest; 244 Byte *dest;
217 SizeT rem; 245 SizeT rem;
218
219 if (cur == p->lims[cj]) 246 if (cur == p->lims[cj])
220 { 247 {
221 p->state = cj; 248 p->state = cj;
222 break; 249 break;
223 } 250 }
224 251 v = GetBe32a(cur);
225 val = GetBe32(cur);
226 p->bufs[cj] = cur + 4; 252 p->bufs[cj] = cur + 4;
227 253 {
228 p->ip += 4; 254 const UInt32 ip = p->ip + 4;
229 val -= p->ip; 255 v -= ip;
256 p->ip = ip;
257 }
230 dest = p->dest; 258 dest = p->dest;
231 rem = (SizeT)(p->destLim - dest); 259 rem = (SizeT)(p->destLim - dest);
232
233 if (rem < 4) 260 if (rem < 4)
234 { 261 {
235 p->temp[0] = (Byte)val; if (rem > 0) dest[0] = (Byte)val; val >>= 8; 262 if ((unsigned)rem > 0) { dest[0] = (Byte)v; v >>= 8;
236 p->temp[1] = (Byte)val; if (rem > 1) dest[1] = (Byte)val; val >>= 8; 263 if ((unsigned)rem > 1) { dest[1] = (Byte)v; v >>= 8;
237 p->temp[2] = (Byte)val; if (rem > 2) dest[2] = (Byte)val; val >>= 8; 264 if ((unsigned)rem > 2) { dest[2] = (Byte)v; v >>= 8; }}}
238 p->temp[3] = (Byte)val; 265 p->temp = v;
239 p->dest = dest + rem; 266 p->dest = dest + rem;
240 p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem; 267 p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
241 break; 268 break;
242 } 269 }
243 270 SetUi32(dest, v)
244 SetUi32(dest, val); 271 v >>= 24;
245 p->temp[3] = (Byte)(val >> 24);
246 p->dest = dest + 4; 272 p->dest = dest + 4;
247 } 273 }
248 } 274 }
@@ -252,6 +278,13 @@ SRes Bcj2Dec_Decode(CBcj2Dec *p)
252 p->range <<= 8; 278 p->range <<= 8;
253 p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++; 279 p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
254 } 280 }
255
256 return SZ_OK; 281 return SZ_OK;
257} 282}
283
284#undef NUM_ITERS
285#undef ONE_ITER
286#undef NUM_SHIFT_BITS
287#undef kTopValue
288#undef kNumBitModelTotalBits
289#undef kBitModelTotal
290#undef kNumMoveBits
diff --git a/C/Bcj2.h b/C/Bcj2.h
index 8824080..4575545 100644
--- a/C/Bcj2.h
+++ b/C/Bcj2.h
@@ -1,8 +1,8 @@
1/* Bcj2.h -- BCJ2 Converter for x86 code 1/* Bcj2.h -- BCJ2 converter for x86 code (Branch CALL/JUMP variant2)
22014-11-10 : Igor Pavlov : Public domain */ 22023-03-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __BCJ2_H 4#ifndef ZIP7_INC_BCJ2_H
5#define __BCJ2_H 5#define ZIP7_INC_BCJ2_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -26,37 +26,68 @@ enum
26 BCJ2_DEC_STATE_ORIG_3, 26 BCJ2_DEC_STATE_ORIG_3,
27 27
28 BCJ2_DEC_STATE_ORIG, 28 BCJ2_DEC_STATE_ORIG,
29 BCJ2_DEC_STATE_OK 29 BCJ2_DEC_STATE_ERROR /* after detected data error */
30}; 30};
31 31
32enum 32enum
33{ 33{
34 BCJ2_ENC_STATE_ORIG = BCJ2_NUM_STREAMS, 34 BCJ2_ENC_STATE_ORIG = BCJ2_NUM_STREAMS,
35 BCJ2_ENC_STATE_OK 35 BCJ2_ENC_STATE_FINISHED /* it's state after fully encoded stream */
36}; 36};
37 37
38 38
39#define BCJ2_IS_32BIT_STREAM(s) ((s) == BCJ2_STREAM_CALL || (s) == BCJ2_STREAM_JUMP) 39/* #define BCJ2_IS_32BIT_STREAM(s) ((s) == BCJ2_STREAM_CALL || (s) == BCJ2_STREAM_JUMP) */
40#define BCJ2_IS_32BIT_STREAM(s) ((unsigned)((unsigned)(s) - (unsigned)BCJ2_STREAM_CALL) < 2)
40 41
41/* 42/*
42CBcj2Dec / CBcj2Enc 43CBcj2Dec / CBcj2Enc
43bufs sizes: 44bufs sizes:
44 BUF_SIZE(n) = lims[n] - bufs[n] 45 BUF_SIZE(n) = lims[n] - bufs[n]
45bufs sizes for BCJ2_STREAM_CALL and BCJ2_STREAM_JUMP must be mutliply of 4: 46bufs sizes for BCJ2_STREAM_CALL and BCJ2_STREAM_JUMP must be multiply of 4:
46 (BUF_SIZE(BCJ2_STREAM_CALL) & 3) == 0 47 (BUF_SIZE(BCJ2_STREAM_CALL) & 3) == 0
47 (BUF_SIZE(BCJ2_STREAM_JUMP) & 3) == 0 48 (BUF_SIZE(BCJ2_STREAM_JUMP) & 3) == 0
48*/ 49*/
49 50
51// typedef UInt32 CBcj2Prob;
52typedef UInt16 CBcj2Prob;
53
54/*
55BCJ2 encoder / decoder internal requirements:
56 - If last bytes of stream contain marker (e8/e8/0f8x), then
57 there is also encoded symbol (0 : no conversion) in RC stream.
58 - One case of overlapped instructions is supported,
59 if last byte of converted instruction is (0f) and next byte is (8x):
60 marker [xx xx xx 0f] 8x
61 then the pair (0f 8x) is treated as marker.
62*/
63
64/* ---------- BCJ2 Decoder ---------- */
65
50/* 66/*
51CBcj2Dec: 67CBcj2Dec:
52dest is allowed to overlap with bufs[BCJ2_STREAM_MAIN], with the following conditions: 68(dest) is allowed to overlap with bufs[BCJ2_STREAM_MAIN], with the following conditions:
53 bufs[BCJ2_STREAM_MAIN] >= dest && 69 bufs[BCJ2_STREAM_MAIN] >= dest &&
54 bufs[BCJ2_STREAM_MAIN] - dest >= tempReserv + 70 bufs[BCJ2_STREAM_MAIN] - dest >=
55 BUF_SIZE(BCJ2_STREAM_CALL) + 71 BUF_SIZE(BCJ2_STREAM_CALL) +
56 BUF_SIZE(BCJ2_STREAM_JUMP) 72 BUF_SIZE(BCJ2_STREAM_JUMP)
57 tempReserv = 0 : for first call of Bcj2Dec_Decode 73 reserve = bufs[BCJ2_STREAM_MAIN] - dest -
58 tempReserv = 4 : for any other calls of Bcj2Dec_Decode 74 ( BUF_SIZE(BCJ2_STREAM_CALL) +
59 overlap with offset = 1 is not allowed 75 BUF_SIZE(BCJ2_STREAM_JUMP) )
76 and additional conditions:
77 if (it's first call of Bcj2Dec_Decode() after Bcj2Dec_Init())
78 {
79 (reserve != 1) : if (ver < v23.00)
80 }
81 else // if there are more than one calls of Bcj2Dec_Decode() after Bcj2Dec_Init())
82 {
83 (reserve >= 6) : if (ver < v23.00)
84 (reserve >= 4) : if (ver >= v23.00)
85 We need that (reserve) because after first call of Bcj2Dec_Decode(),
86 CBcj2Dec::temp can contain up to 4 bytes for writing to (dest).
87 }
88 (reserve == 0) is allowed, if we decode full stream via single call of Bcj2Dec_Decode().
89 (reserve == 0) also is allowed in case of multi-call, if we use fixed buffers,
90 and (reserve) is calculated from full (final) sizes of all streams before first call.
60*/ 91*/
61 92
62typedef struct 93typedef struct
@@ -68,21 +99,65 @@ typedef struct
68 99
69 unsigned state; /* BCJ2_STREAM_MAIN has more priority than BCJ2_STATE_ORIG */ 100 unsigned state; /* BCJ2_STREAM_MAIN has more priority than BCJ2_STATE_ORIG */
70 101
71 UInt32 ip; 102 UInt32 ip; /* property of starting base for decoding */
72 Byte temp[4]; 103 UInt32 temp; /* Byte temp[4]; */
73 UInt32 range; 104 UInt32 range;
74 UInt32 code; 105 UInt32 code;
75 UInt16 probs[2 + 256]; 106 CBcj2Prob probs[2 + 256];
76} CBcj2Dec; 107} CBcj2Dec;
77 108
109
110/* Note:
111 Bcj2Dec_Init() sets (CBcj2Dec::ip = 0)
112 if (ip != 0) property is required, the caller must set CBcj2Dec::ip after Bcj2Dec_Init()
113*/
78void Bcj2Dec_Init(CBcj2Dec *p); 114void Bcj2Dec_Init(CBcj2Dec *p);
79 115
80/* Returns: SZ_OK or SZ_ERROR_DATA */ 116
117/* Bcj2Dec_Decode():
118 returns:
119 SZ_OK
120 SZ_ERROR_DATA : if data in 5 starting bytes of BCJ2_STREAM_RC stream are not correct
121*/
81SRes Bcj2Dec_Decode(CBcj2Dec *p); 122SRes Bcj2Dec_Decode(CBcj2Dec *p);
82 123
83#define Bcj2Dec_IsFinished(_p_) ((_p_)->code == 0) 124/* To check that decoding was finished you can compare
125 sizes of processed streams with sizes known from another sources.
126 You must do at least one mandatory check from the two following options:
127 - the check for size of processed output (ORIG) stream.
128 - the check for size of processed input (MAIN) stream.
129 additional optional checks:
130 - the checks for processed sizes of all input streams (MAIN, CALL, JUMP, RC)
131 - the checks Bcj2Dec_IsMaybeFinished*()
132 also before actual decoding you can check that the
133 following condition is met for stream sizes:
134 ( size(ORIG) == size(MAIN) + size(CALL) + size(JUMP) )
135*/
84 136
137/* (state == BCJ2_STREAM_MAIN) means that decoder is ready for
138 additional input data in BCJ2_STREAM_MAIN stream.
139 Note that (state == BCJ2_STREAM_MAIN) is allowed for non-finished decoding.
140*/
141#define Bcj2Dec_IsMaybeFinished_state_MAIN(_p_) ((_p_)->state == BCJ2_STREAM_MAIN)
85 142
143/* if the stream decoding was finished correctly, then range decoder
144 part of CBcj2Dec also was finished, and then (CBcj2Dec::code == 0).
145 Note that (CBcj2Dec::code == 0) is allowed for non-finished decoding.
146*/
147#define Bcj2Dec_IsMaybeFinished_code(_p_) ((_p_)->code == 0)
148
149/* use Bcj2Dec_IsMaybeFinished() only as additional check
150 after at least one mandatory check from the two following options:
151 - the check for size of processed output (ORIG) stream.
152 - the check for size of processed input (MAIN) stream.
153*/
154#define Bcj2Dec_IsMaybeFinished(_p_) ( \
155 Bcj2Dec_IsMaybeFinished_state_MAIN(_p_) && \
156 Bcj2Dec_IsMaybeFinished_code(_p_))
157
158
159
160/* ---------- BCJ2 Encoder ---------- */
86 161
87typedef enum 162typedef enum
88{ 163{
@@ -91,6 +166,91 @@ typedef enum
91 BCJ2_ENC_FINISH_MODE_END_STREAM 166 BCJ2_ENC_FINISH_MODE_END_STREAM
92} EBcj2Enc_FinishMode; 167} EBcj2Enc_FinishMode;
93 168
169/*
170 BCJ2_ENC_FINISH_MODE_CONTINUE:
171 process non finished encoding.
172 It notifies the encoder that additional further calls
173 can provide more input data (src) than provided by current call.
174 In that case the CBcj2Enc encoder still can move (src) pointer
175 up to (srcLim), but CBcj2Enc encoder can store some of the last
176 processed bytes (up to 4 bytes) from src to internal CBcj2Enc::temp[] buffer.
177 at return:
178 (CBcj2Enc::src will point to position that includes
179 processed data and data copied to (temp[]) buffer)
180 That data from (temp[]) buffer will be used in further calls.
181
182 BCJ2_ENC_FINISH_MODE_END_BLOCK:
183 finish encoding of current block (ended at srcLim) without RC flushing.
184 at return: if (CBcj2Enc::state == BCJ2_ENC_STATE_ORIG) &&
185 CBcj2Enc::src == CBcj2Enc::srcLim)
186 : it shows that block encoding was finished. And the encoder is
187 ready for new (src) data or for stream finish operation.
188 finished block means
189 {
190 CBcj2Enc has completed block encoding up to (srcLim).
191 (1 + 4 bytes) or (2 + 4 bytes) CALL/JUMP cortages will
192 not cross block boundary at (srcLim).
193 temporary CBcj2Enc buffer for (ORIG) src data is empty.
194 3 output uncompressed streams (MAIN, CALL, JUMP) were flushed.
195 RC stream was not flushed. And RC stream will cross block boundary.
196 }
197 Note: some possible implementation of BCJ2 encoder could
198 write branch marker (e8/e8/0f8x) in one call of Bcj2Enc_Encode(),
199 and it could calculate symbol for RC in another call of Bcj2Enc_Encode().
200 BCJ2 encoder uses ip/fileIp/fileSize/relatLimit values to calculate RC symbol.
201 And these CBcj2Enc variables can have different values in different Bcj2Enc_Encode() calls.
202 So caller must finish each block with BCJ2_ENC_FINISH_MODE_END_BLOCK
203 to ensure that RC symbol is calculated and written in proper block.
204
205 BCJ2_ENC_FINISH_MODE_END_STREAM
206 finish encoding of stream (ended at srcLim) fully including RC flushing.
207 at return: if (CBcj2Enc::state == BCJ2_ENC_STATE_FINISHED)
208 : it shows that stream encoding was finished fully,
209 and all output streams were flushed fully.
210 also Bcj2Enc_IsFinished() can be called.
211*/
212
213
214/*
215 32-bit relative offset in JUMP/CALL commands is
216 - (mod 4 GiB) for 32-bit x86 code
217 - signed Int32 for 64-bit x86-64 code
218 BCJ2 encoder also does internal relative to absolute address conversions.
219 And there are 2 possible ways to do it:
220 before v23: we used 32-bit variables and (mod 4 GiB) conversion
221 since v23: we use 64-bit variables and (signed Int32 offset) conversion.
222 The absolute address condition for conversion in v23:
223 ((UInt64)((Int64)ip64 - (Int64)fileIp64 + 5 + (Int32)offset) < (UInt64)fileSize64)
224 note that if (fileSize64 > 2 GiB). there is difference between
225 old (mod 4 GiB) way (v22) and new (signed Int32 offset) way (v23).
226 And new (v23) way is more suitable to encode 64-bit x86-64 code for (fileSize64 > 2 GiB) cases.
227*/
228
229/*
230// for old (v22) way for conversion:
231typedef UInt32 CBcj2Enc_ip_unsigned;
232typedef Int32 CBcj2Enc_ip_signed;
233#define BCJ2_ENC_FileSize_MAX ((UInt32)1 << 31)
234*/
235typedef UInt64 CBcj2Enc_ip_unsigned;
236typedef Int64 CBcj2Enc_ip_signed;
237
238/* maximum size of file that can be used for conversion condition */
239#define BCJ2_ENC_FileSize_MAX ((CBcj2Enc_ip_unsigned)0 - 2)
240
241/* default value of fileSize64_minus1 variable that means
242 that absolute address limitation will not be used */
243#define BCJ2_ENC_FileSizeField_UNLIMITED ((CBcj2Enc_ip_unsigned)0 - 1)
244
245/* calculate value that later can be set to CBcj2Enc::fileSize64_minus1 */
246#define BCJ2_ENC_GET_FileSizeField_VAL_FROM_FileSize(fileSize) \
247 ((CBcj2Enc_ip_unsigned)(fileSize) - 1)
248
249/* set CBcj2Enc::fileSize64_minus1 variable from size of file */
250#define Bcj2Enc_SET_FileSize(p, fileSize) \
251 (p)->fileSize64_minus1 = BCJ2_ENC_GET_FileSizeField_VAL_FROM_FileSize(fileSize);
252
253
94typedef struct 254typedef struct
95{ 255{
96 Byte *bufs[BCJ2_NUM_STREAMS]; 256 Byte *bufs[BCJ2_NUM_STREAMS];
@@ -101,45 +261,71 @@ typedef struct
101 unsigned state; 261 unsigned state;
102 EBcj2Enc_FinishMode finishMode; 262 EBcj2Enc_FinishMode finishMode;
103 263
104 Byte prevByte; 264 Byte context;
265 Byte flushRem;
266 Byte isFlushState;
105 267
106 Byte cache; 268 Byte cache;
107 UInt32 range; 269 UInt32 range;
108 UInt64 low; 270 UInt64 low;
109 UInt64 cacheSize; 271 UInt64 cacheSize;
272
273 // UInt32 context; // for marker version, it can include marker flag.
110 274
111 UInt32 ip; 275 /* (ip64) and (fileIp64) correspond to virtual source stream position
112 276 that doesn't include data in temp[] */
113 /* 32-bit ralative offset in JUMP/CALL commands is 277 CBcj2Enc_ip_unsigned ip64; /* current (ip) position */
114 - (mod 4 GB) in 32-bit mode 278 CBcj2Enc_ip_unsigned fileIp64; /* start (ip) position of current file */
115 - signed Int32 in 64-bit mode 279 CBcj2Enc_ip_unsigned fileSize64_minus1; /* size of current file (for conversion limitation) */
116 We use (mod 4 GB) check for fileSize. 280 UInt32 relatLimit; /* (relatLimit <= ((UInt32)1 << 31)) : 0 means disable_conversion */
117 Use fileSize up to 2 GB, if you want to support 32-bit and 64-bit code conversion. */ 281 // UInt32 relatExcludeBits;
118 UInt32 fileIp;
119 UInt32 fileSize; /* (fileSize <= ((UInt32)1 << 31)), 0 means no_limit */
120 UInt32 relatLimit; /* (relatLimit <= ((UInt32)1 << 31)), 0 means desable_conversion */
121 282
122 UInt32 tempTarget; 283 UInt32 tempTarget;
123 unsigned tempPos; 284 unsigned tempPos; /* the number of bytes that were copied to temp[] buffer
124 Byte temp[4 * 2]; 285 (tempPos <= 4) outside of Bcj2Enc_Encode() */
125 286 // Byte temp[4]; // for marker version
126 unsigned flushPos; 287 Byte temp[8];
127 288 CBcj2Prob probs[2 + 256];
128 UInt16 probs[2 + 256];
129} CBcj2Enc; 289} CBcj2Enc;
130 290
131void Bcj2Enc_Init(CBcj2Enc *p); 291void Bcj2Enc_Init(CBcj2Enc *p);
132void Bcj2Enc_Encode(CBcj2Enc *p);
133 292
134#define Bcj2Enc_Get_InputData_Size(p) ((SizeT)((p)->srcLim - (p)->src) + (p)->tempPos)
135#define Bcj2Enc_IsFinished(p) ((p)->flushPos == 5)
136 293
294/*
295Bcj2Enc_Encode(): at exit:
296 p->State < BCJ2_NUM_STREAMS : we need more buffer space for output stream
297 (bufs[p->State] == lims[p->State])
298 p->State == BCJ2_ENC_STATE_ORIG : we need more data in input src stream
299 (src == srcLim)
300 p->State == BCJ2_ENC_STATE_FINISHED : after fully encoded stream
301*/
302void Bcj2Enc_Encode(CBcj2Enc *p);
137 303
138#define BCJ2_RELAT_LIMIT_NUM_BITS 26 304/* Bcj2Enc encoder can look ahead for up 4 bytes of source stream.
139#define BCJ2_RELAT_LIMIT ((UInt32)1 << BCJ2_RELAT_LIMIT_NUM_BITS) 305 CBcj2Enc::tempPos : is the number of bytes that were copied from input stream to temp[] buffer.
306 (CBcj2Enc::src) after Bcj2Enc_Encode() is starting position after
307 fully processed data and after data copied to temp buffer.
308 So if the caller needs to get real number of fully processed input
309 bytes (without look ahead data in temp buffer),
310 the caller must subtruct (CBcj2Enc::tempPos) value from processed size
311 value that is calculated based on current (CBcj2Enc::src):
312 cur_processed_pos = Calc_Big_Processed_Pos(enc.src)) -
313 Bcj2Enc_Get_AvailInputSize_in_Temp(&enc);
314*/
315/* get the size of input data that was stored in temp[] buffer: */
316#define Bcj2Enc_Get_AvailInputSize_in_Temp(p) ((p)->tempPos)
140 317
141/* limit for CBcj2Enc::fileSize variable */ 318#define Bcj2Enc_IsFinished(p) ((p)->flushRem == 0)
142#define BCJ2_FileSize_MAX ((UInt32)1 << 31) 319
320/* Note : the decoder supports overlapping of marker (0f 80).
321 But we can eliminate such overlapping cases by setting
322 the limit for relative offset conversion as
323 CBcj2Enc::relatLimit <= (0x0f << 24) == (240 MiB)
324*/
325/* default value for CBcj2Enc::relatLimit */
326#define BCJ2_ENC_RELAT_LIMIT_DEFAULT ((UInt32)0x0f << 24)
327#define BCJ2_ENC_RELAT_LIMIT_MAX ((UInt32)1 << 31)
328// #define BCJ2_RELAT_EXCLUDE_NUM_BITS 5
143 329
144EXTERN_C_END 330EXTERN_C_END
145 331
diff --git a/C/Bcj2Enc.c b/C/Bcj2Enc.c
index 682362a..79460bb 100644
--- a/C/Bcj2Enc.c
+++ b/C/Bcj2Enc.c
@@ -1,60 +1,62 @@
1/* Bcj2Enc.c -- BCJ2 Encoder (Converter for x86 code) 1/* Bcj2Enc.c -- BCJ2 Encoder converter for x86 code (Branch CALL/JUMP variant2)
22021-02-09 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6/* #define SHOW_STAT */ 6/* #define SHOW_STAT */
7
8#ifdef SHOW_STAT 7#ifdef SHOW_STAT
9#include <stdio.h> 8#include <stdio.h>
10#define PRF(x) x 9#define PRF2(s) printf("%s ip=%8x tempPos=%d src= %8x\n", s, (unsigned)p->ip64, p->tempPos, (unsigned)(p->srcLim - p->src));
11#else 10#else
12#define PRF(x) 11#define PRF2(s)
13#endif 12#endif
14 13
15#include <string.h>
16
17#include "Bcj2.h" 14#include "Bcj2.h"
18#include "CpuArch.h" 15#include "CpuArch.h"
19 16
20#define CProb UInt16
21
22#define kTopValue ((UInt32)1 << 24) 17#define kTopValue ((UInt32)1 << 24)
23#define kNumModelBits 11 18#define kNumBitModelTotalBits 11
24#define kBitModelTotal (1 << kNumModelBits) 19#define kBitModelTotal (1 << kNumBitModelTotalBits)
25#define kNumMoveBits 5 20#define kNumMoveBits 5
26 21
27void Bcj2Enc_Init(CBcj2Enc *p) 22void Bcj2Enc_Init(CBcj2Enc *p)
28{ 23{
29 unsigned i; 24 unsigned i;
30 25 p->state = BCJ2_ENC_STATE_ORIG;
31 p->state = BCJ2_ENC_STATE_OK;
32 p->finishMode = BCJ2_ENC_FINISH_MODE_CONTINUE; 26 p->finishMode = BCJ2_ENC_FINISH_MODE_CONTINUE;
33 27 p->context = 0;
34 p->prevByte = 0; 28 p->flushRem = 5;
35 29 p->isFlushState = 0;
36 p->cache = 0; 30 p->cache = 0;
37 p->range = 0xFFFFFFFF; 31 p->range = 0xffffffff;
38 p->low = 0; 32 p->low = 0;
39 p->cacheSize = 1; 33 p->cacheSize = 1;
40 34 p->ip64 = 0;
41 p->ip = 0; 35 p->fileIp64 = 0;
42 36 p->fileSize64_minus1 = BCJ2_ENC_FileSizeField_UNLIMITED;
43 p->fileIp = 0; 37 p->relatLimit = BCJ2_ENC_RELAT_LIMIT_DEFAULT;
44 p->fileSize = 0; 38 // p->relatExcludeBits = 0;
45 p->relatLimit = BCJ2_RELAT_LIMIT;
46
47 p->tempPos = 0; 39 p->tempPos = 0;
48
49 p->flushPos = 0;
50
51 for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++) 40 for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++)
52 p->probs[i] = kBitModelTotal >> 1; 41 p->probs[i] = kBitModelTotal >> 1;
53} 42}
54 43
55static BoolInt MY_FAST_CALL RangeEnc_ShiftLow(CBcj2Enc *p) 44// Z7_NO_INLINE
45Z7_FORCE_INLINE
46static BoolInt Bcj2_RangeEnc_ShiftLow(CBcj2Enc *p)
56{ 47{
57 if ((UInt32)p->low < (UInt32)0xFF000000 || (UInt32)(p->low >> 32) != 0) 48 const UInt32 low = (UInt32)p->low;
49 const unsigned high = (unsigned)
50 #if defined(Z7_MSC_VER_ORIGINAL) \
51 && defined(MY_CPU_X86) \
52 && defined(MY_CPU_LE) \
53 && !defined(MY_CPU_64BIT)
54 // we try to rid of __aullshr() call in MSVS-x86
55 (((const UInt32 *)&p->low)[1]); // [1] : for little-endian only
56 #else
57 (p->low >> 32);
58 #endif
59 if (low < (UInt32)0xff000000 || high != 0)
58 { 60 {
59 Byte *buf = p->bufs[BCJ2_STREAM_RC]; 61 Byte *buf = p->bufs[BCJ2_STREAM_RC];
60 do 62 do
@@ -65,247 +67,440 @@ static BoolInt MY_FAST_CALL RangeEnc_ShiftLow(CBcj2Enc *p)
65 p->bufs[BCJ2_STREAM_RC] = buf; 67 p->bufs[BCJ2_STREAM_RC] = buf;
66 return True; 68 return True;
67 } 69 }
68 *buf++ = (Byte)(p->cache + (Byte)(p->low >> 32)); 70 *buf++ = (Byte)(p->cache + high);
69 p->cache = 0xFF; 71 p->cache = 0xff;
70 } 72 }
71 while (--p->cacheSize); 73 while (--p->cacheSize);
72 p->bufs[BCJ2_STREAM_RC] = buf; 74 p->bufs[BCJ2_STREAM_RC] = buf;
73 p->cache = (Byte)((UInt32)p->low >> 24); 75 p->cache = (Byte)(low >> 24);
74 } 76 }
75 p->cacheSize++; 77 p->cacheSize++;
76 p->low = (UInt32)p->low << 8; 78 p->low = low << 8;
77 return False; 79 return False;
78} 80}
79 81
80static void Bcj2Enc_Encode_2(CBcj2Enc *p) 82
81{ 83/*
82 if (BCJ2_IS_32BIT_STREAM(p->state)) 84We can use 2 alternative versions of code:
851) non-marker version:
86 Byte CBcj2Enc::context
87 Byte temp[8];
88 Last byte of marker (e8/e9/[0f]8x) can be written to temp[] buffer.
89 Encoder writes last byte of marker (e8/e9/[0f]8x) to dest, only in conjunction
90 with writing branch symbol to range coder in same Bcj2Enc_Encode_2() call.
91
922) marker version:
93 UInt32 CBcj2Enc::context
94 Byte CBcj2Enc::temp[4];
95 MARKER_FLAG in CBcj2Enc::context shows that CBcj2Enc::context contains finded marker.
96 it's allowed that
97 one call of Bcj2Enc_Encode_2() writes last byte of marker (e8/e9/[0f]8x) to dest,
98 and another call of Bcj2Enc_Encode_2() does offset conversion.
99 So different values of (fileIp) and (fileSize) are possible
100 in these different Bcj2Enc_Encode_2() calls.
101
102Also marker version requires additional if((v & MARKER_FLAG) == 0) check in main loop.
103So we use non-marker version.
104*/
105
106/*
107 Corner cases with overlap in multi-block.
108 before v23: there was one corner case, where converted instruction
109 could start in one sub-stream and finish in next sub-stream.
110 If multi-block (solid) encoding is used,
111 and BCJ2_ENC_FINISH_MODE_END_BLOCK is used for each sub-stream.
112 and (0f) is last byte of previous sub-stream
113 and (8x) is first byte of current sub-stream
114 then (0f 8x) pair is treated as marker by BCJ2 encoder and decoder.
115 BCJ2 encoder can converts 32-bit offset for that (0f 8x) cortage,
116 if that offset meets limit requirements.
117 If encoder allows 32-bit offset conversion for such overlap case,
118 then the data in 3 uncompressed BCJ2 streams for some sub-stream
119 can depend from data of previous sub-stream.
120 That corner case is not big problem, and it's rare case.
121 Since v23.00 we do additional check to prevent conversions in such overlap cases.
122*/
123
124/*
125 Bcj2Enc_Encode_2() output variables at exit:
83 { 126 {
84 Byte *cur = p->bufs[p->state]; 127 if (Bcj2Enc_Encode_2() exits with (p->state == BCJ2_ENC_STATE_ORIG))
85 if (cur == p->lims[p->state]) 128 {
86 return; 129 it means that encoder needs more input data.
87 SetBe32(cur, p->tempTarget); 130 if (p->srcLim == p->src) at exit, then
88 p->bufs[p->state] = cur + 4; 131 {
132 (p->finishMode != BCJ2_ENC_FINISH_MODE_END_STREAM)
133 all input data were read and processed, and we are ready for
134 new input data.
135 }
136 else
137 {
138 (p->srcLim != p->src)
139 (p->finishMode == BCJ2_ENC_FINISH_MODE_CONTINUE)
140 The encoder have found e8/e9/0f_8x marker,
141 and p->src points to last byte of that marker,
142 Bcj2Enc_Encode_2() needs more input data to get totally
143 5 bytes (last byte of marker and 32-bit branch offset)
144 as continuous array starting from p->src.
145 (p->srcLim - p->src < 5) requirement is met after exit.
146 So non-processed resedue from p->src to p->srcLim is always less than 5 bytes.
147 }
148 }
89 } 149 }
150*/
90 151
91 p->state = BCJ2_ENC_STATE_ORIG; 152Z7_NO_INLINE
92 153static void Bcj2Enc_Encode_2(CBcj2Enc *p)
93 for (;;) 154{
155 if (!p->isFlushState)
94 { 156 {
95 if (p->range < kTopValue) 157 const Byte *src;
158 UInt32 v;
96 { 159 {
97 if (RangeEnc_ShiftLow(p)) 160 const unsigned state = p->state;
98 return; 161 if (BCJ2_IS_32BIT_STREAM(state))
99 p->range <<= 8; 162 {
163 Byte *cur = p->bufs[state];
164 if (cur == p->lims[state])
165 return;
166 SetBe32a(cur, p->tempTarget)
167 p->bufs[state] = cur + 4;
168 }
100 } 169 }
170 p->state = BCJ2_ENC_STATE_ORIG; // for main reason of exit
171 src = p->src;
172 v = p->context;
173
174 // #define WRITE_CONTEXT p->context = v; // for marker version
175 #define WRITE_CONTEXT p->context = (Byte)v;
176 #define WRITE_CONTEXT_AND_SRC p->src = src; WRITE_CONTEXT
101 177
178 for (;;)
102 { 179 {
180 // const Byte *src;
181 // UInt32 v;
182 CBcj2Enc_ip_unsigned ip;
183 if (p->range < kTopValue)
184 {
185 // to reduce register pressure and code size: we save and restore local variables.
186 WRITE_CONTEXT_AND_SRC
187 if (Bcj2_RangeEnc_ShiftLow(p))
188 return;
189 p->range <<= 8;
190 src = p->src;
191 v = p->context;
192 }
193 // src = p->src;
194 // #define MARKER_FLAG ((UInt32)1 << 17)
195 // if ((v & MARKER_FLAG) == 0) // for marker version
103 { 196 {
104 const Byte *src = p->src;
105 const Byte *srcLim; 197 const Byte *srcLim;
106 Byte *dest; 198 Byte *dest = p->bufs[BCJ2_STREAM_MAIN];
107 SizeT num = (SizeT)(p->srcLim - src);
108
109 if (p->finishMode == BCJ2_ENC_FINISH_MODE_CONTINUE)
110 { 199 {
111 if (num <= 4) 200 const SizeT remSrc = (SizeT)(p->srcLim - src);
112 return; 201 SizeT rem = (SizeT)(p->lims[BCJ2_STREAM_MAIN] - dest);
113 num -= 4; 202 if (rem >= remSrc)
203 rem = remSrc;
204 srcLim = src + rem;
114 } 205 }
115 else if (num == 0) 206 /* p->context contains context of previous byte:
116 break; 207 bits [0 : 7] : src[-1], if (src) was changed in this call
117 208 bits [8 : 31] : are undefined for non-marker version
118 dest = p->bufs[BCJ2_STREAM_MAIN]; 209 */
119 if (num > (SizeT)(p->lims[BCJ2_STREAM_MAIN] - dest)) 210 // v = p->context;
211 #define NUM_SHIFT_BITS 24
212 #define CONV_FLAG ((UInt32)1 << 16)
213 #define ONE_ITER { \
214 b = src[0]; \
215 *dest++ = (Byte)b; \
216 v = (v << NUM_SHIFT_BITS) | b; \
217 if (((b + (0x100 - 0xe8)) & 0xfe) == 0) break; \
218 if (((v - (((UInt32)0x0f << (NUM_SHIFT_BITS)) + 0x80)) & \
219 ((((UInt32)1 << (4 + NUM_SHIFT_BITS)) - 0x1) << 4)) == 0) break; \
220 src++; if (src == srcLim) { break; } }
221
222 if (src != srcLim)
223 for (;;)
120 { 224 {
121 num = (SizeT)(p->lims[BCJ2_STREAM_MAIN] - dest); 225 /* clang can generate ineffective code with setne instead of two jcc instructions.
122 if (num == 0) 226 we can use 2 iterations and external (unsigned b) to avoid that ineffective code genaration. */
123 { 227 unsigned b;
124 p->state = BCJ2_STREAM_MAIN; 228 ONE_ITER
125 return; 229 ONE_ITER
126 }
127 } 230 }
128 231
129 srcLim = src + num; 232 ip = p->ip64 + (CBcj2Enc_ip_unsigned)(SizeT)(dest - p->bufs[BCJ2_STREAM_MAIN]);
233 p->bufs[BCJ2_STREAM_MAIN] = dest;
234 p->ip64 = ip;
130 235
131 if (p->prevByte == 0x0F && (src[0] & 0xF0) == 0x80) 236 if (src == srcLim)
132 *dest = src[0];
133 else for (;;)
134 { 237 {
135 Byte b = *src; 238 WRITE_CONTEXT_AND_SRC
136 *dest = b; 239 if (src != p->srcLim)
137 if (b != 0x0F)
138 { 240 {
139 if ((b & 0xFE) == 0xE8) 241 p->state = BCJ2_STREAM_MAIN;
140 break; 242 return;
141 dest++;
142 if (++src != srcLim)
143 continue;
144 break;
145 } 243 }
146 dest++; 244 /* (p->src == p->srcLim)
147 if (++src == srcLim) 245 (p->state == BCJ2_ENC_STATE_ORIG) */
148 break; 246 if (p->finishMode != BCJ2_ENC_FINISH_MODE_END_STREAM)
149 if ((*src & 0xF0) != 0x80) 247 return;
150 continue; 248 /* (p->finishMode == BCJ2_ENC_FINISH_MODE_END_STREAM */
151 *dest = *src; 249 // (p->flushRem == 5);
250 p->isFlushState = 1;
152 break; 251 break;
153 } 252 }
154 253 src++;
155 num = (SizeT)(src - p->src); 254 // p->src = src;
156 255 }
157 if (src == srcLim) 256 // ip = p->ip; // for marker version
158 { 257 /* marker was found */
159 p->prevByte = src[-1]; 258 /* (v) contains marker that was found:
160 p->bufs[BCJ2_STREAM_MAIN] = dest; 259 bits [NUM_SHIFT_BITS : NUM_SHIFT_BITS + 7]
161 p->src = src; 260 : value of src[-2] : xx/xx/0f
162 p->ip += (UInt32)num; 261 bits [0 : 7] : value of src[-1] : e8/e9/8x
163 continue; 262 */
164 } 263 {
165
166 { 264 {
167 Byte context = (Byte)(num == 0 ? p->prevByte : src[-1]); 265 #if NUM_SHIFT_BITS != 24
168 BoolInt needConvert; 266 v &= ~(UInt32)CONV_FLAG;
169 267 #endif
170 p->bufs[BCJ2_STREAM_MAIN] = dest + 1; 268 // UInt32 relat = 0;
171 p->ip += (UInt32)num + 1;
172 src++;
173
174 needConvert = False;
175
176 if ((SizeT)(p->srcLim - src) >= 4) 269 if ((SizeT)(p->srcLim - src) >= 4)
177 { 270 {
178 UInt32 relatVal = GetUi32(src); 271 /*
179 if ((p->fileSize == 0 || (UInt32)(p->ip + 4 + relatVal - p->fileIp) < p->fileSize) 272 if (relat != 0 || (Byte)v != 0xe8)
180 && ((relatVal + p->relatLimit) >> 1) < p->relatLimit) 273 BoolInt isBigOffset = True;
181 needConvert = True; 274 */
275 const UInt32 relat = GetUi32(src);
276 /*
277 #define EXCLUDE_FLAG ((UInt32)1 << 4)
278 #define NEED_CONVERT(rel) ((((rel) + EXCLUDE_FLAG) & (0 - EXCLUDE_FLAG * 2)) != 0)
279 if (p->relatExcludeBits != 0)
280 {
281 const UInt32 flag = (UInt32)1 << (p->relatExcludeBits - 1);
282 isBigOffset = (((relat + flag) & (0 - flag * 2)) != 0);
283 }
284 // isBigOffset = False; // for debug
285 */
286 ip -= p->fileIp64;
287 // Use the following if check, if (ip) is 64-bit:
288 if (ip > (((v + 0x20) >> 5) & 1)) // 23.00 : we eliminate milti-block overlap for (Of 80) and (e8/e9)
289 if ((CBcj2Enc_ip_unsigned)((CBcj2Enc_ip_signed)ip + 4 + (Int32)relat) <= p->fileSize64_minus1)
290 if (((UInt32)(relat + p->relatLimit) >> 1) < p->relatLimit)
291 v |= CONV_FLAG;
182 } 292 }
183 293 else if (p->finishMode == BCJ2_ENC_FINISH_MODE_CONTINUE)
184 { 294 {
185 UInt32 bound; 295 // (p->srcLim - src < 4)
186 unsigned ttt; 296 // /*
187 Byte b = src[-1]; 297 // for non-marker version
188 CProb *prob = p->probs + (unsigned)(b == 0xE8 ? 2 + (unsigned)context : (b == 0xE9 ? 1 : 0)); 298 p->ip64--; // p->ip = ip - 1;
189 299 p->bufs[BCJ2_STREAM_MAIN]--;
190 ttt = *prob; 300 src--;
191 bound = (p->range >> kNumModelBits) * ttt; 301 v >>= NUM_SHIFT_BITS;
192 302 // (0 < p->srcLim - p->src <= 4)
193 if (!needConvert) 303 // */
304 // v |= MARKER_FLAG; // for marker version
305 /* (p->state == BCJ2_ENC_STATE_ORIG) */
306 WRITE_CONTEXT_AND_SRC
307 return;
308 }
309 {
310 const unsigned c = ((v + 0x17) >> 6) & 1;
311 CBcj2Prob *prob = p->probs + (unsigned)
312 (((0 - c) & (Byte)(v >> NUM_SHIFT_BITS)) + c + ((v >> 5) & 1));
313 /*
314 ((Byte)v == 0xe8 ? 2 + ((Byte)(v >> 8)) :
315 ((Byte)v < 0xe8 ? 0 : 1)); // ((v >> 5) & 1));
316 */
317 const unsigned ttt = *prob;
318 const UInt32 bound = (p->range >> kNumBitModelTotalBits) * ttt;
319 if ((v & CONV_FLAG) == 0)
194 { 320 {
321 // static int yyy = 0; yyy++; printf("\n!needConvert = %d\n", yyy);
322 // v = (Byte)v; // for marker version
195 p->range = bound; 323 p->range = bound;
196 *prob = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); 324 *prob = (CBcj2Prob)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
197 p->src = src; 325 // WRITE_CONTEXT_AND_SRC
198 p->prevByte = b;
199 continue; 326 continue;
200 } 327 }
201
202 p->low += bound; 328 p->low += bound;
203 p->range -= bound; 329 p->range -= bound;
204 *prob = (CProb)(ttt - (ttt >> kNumMoveBits)); 330 *prob = (CBcj2Prob)(ttt - (ttt >> kNumMoveBits));
205 331 }
332 // p->context = src[3];
333 {
334 // const unsigned cj = ((Byte)v == 0xe8 ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP);
335 const unsigned cj = (((v + 0x57) >> 6) & 1) + BCJ2_STREAM_CALL;
336 ip = p->ip64;
337 v = GetUi32(src); // relat
338 ip += 4;
339 p->ip64 = ip;
340 src += 4;
341 // p->src = src;
206 { 342 {
207 UInt32 relatVal = GetUi32(src); 343 const UInt32 absol = (UInt32)ip + v;
208 UInt32 absVal; 344 Byte *cur = p->bufs[cj];
209 p->ip += 4; 345 v >>= 24;
210 absVal = p->ip + relatVal; 346 // WRITE_CONTEXT
211 p->prevByte = src[3]; 347 if (cur == p->lims[cj])
212 src += 4;
213 p->src = src;
214 { 348 {
215 unsigned cj = (b == 0xE8) ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP; 349 p->state = cj;
216 Byte *cur = p->bufs[cj]; 350 p->tempTarget = absol;
217 if (cur == p->lims[cj]) 351 WRITE_CONTEXT_AND_SRC
218 { 352 return;
219 p->state = cj;
220 p->tempTarget = absVal;
221 return;
222 }
223 SetBe32(cur, absVal);
224 p->bufs[cj] = cur + 4;
225 } 353 }
354 SetBe32a(cur, absol)
355 p->bufs[cj] = cur + 4;
226 } 356 }
227 } 357 }
228 } 358 }
229 } 359 }
230 } 360 } // end of loop
231 } 361 }
232 362
233 if (p->finishMode != BCJ2_ENC_FINISH_MODE_END_STREAM) 363 for (; p->flushRem != 0; p->flushRem--)
234 return; 364 if (Bcj2_RangeEnc_ShiftLow(p))
235
236 for (; p->flushPos < 5; p->flushPos++)
237 if (RangeEnc_ShiftLow(p))
238 return; 365 return;
239 p->state = BCJ2_ENC_STATE_OK; 366 p->state = BCJ2_ENC_STATE_FINISHED;
240} 367}
241 368
242 369
370/*
371BCJ2 encoder needs look ahead for up to 4 bytes in (src) buffer.
372So base function Bcj2Enc_Encode_2()
373 in BCJ2_ENC_FINISH_MODE_CONTINUE mode can return with
374 (p->state == BCJ2_ENC_STATE_ORIG && p->src < p->srcLim)
375Bcj2Enc_Encode() solves that look ahead problem by using p->temp[] buffer.
376 so if (p->state == BCJ2_ENC_STATE_ORIG) after Bcj2Enc_Encode(),
377 then (p->src == p->srcLim).
378 And the caller's code is simpler with Bcj2Enc_Encode().
379*/
380
381Z7_NO_INLINE
243void Bcj2Enc_Encode(CBcj2Enc *p) 382void Bcj2Enc_Encode(CBcj2Enc *p)
244{ 383{
245 PRF(printf("\n")); 384 PRF2("\n----")
246 PRF(printf("---- ip = %8d tempPos = %8d src = %8d\n", p->ip, p->tempPos, p->srcLim - p->src));
247
248 if (p->tempPos != 0) 385 if (p->tempPos != 0)
249 { 386 {
387 /* extra: number of bytes that were copied from (src) to (temp) buffer in this call */
250 unsigned extra = 0; 388 unsigned extra = 0;
251 389 /* We will touch only minimal required number of bytes in input (src) stream.
390 So we will add input bytes from (src) stream to temp[] with step of 1 byte.
391 We don't add new bytes to temp[] before Bcj2Enc_Encode_2() call
392 in first loop iteration because
393 - previous call of Bcj2Enc_Encode() could use another (finishMode),
394 - previous call could finish with (p->state != BCJ2_ENC_STATE_ORIG).
395 the case with full temp[] buffer (p->tempPos == 4) is possible here.
396 */
252 for (;;) 397 for (;;)
253 { 398 {
399 // (0 < p->tempPos <= 5) // in non-marker version
400 /* p->src : the current src data position including extra bytes
401 that were copied to temp[] buffer in this call */
254 const Byte *src = p->src; 402 const Byte *src = p->src;
255 const Byte *srcLim = p->srcLim; 403 const Byte *srcLim = p->srcLim;
256 EBcj2Enc_FinishMode finishMode = p->finishMode; 404 const EBcj2Enc_FinishMode finishMode = p->finishMode;
257
258 p->src = p->temp;
259 p->srcLim = p->temp + p->tempPos;
260 if (src != srcLim) 405 if (src != srcLim)
406 {
407 /* if there are some src data after the data copied to temp[],
408 then we use MODE_CONTINUE for temp data */
261 p->finishMode = BCJ2_ENC_FINISH_MODE_CONTINUE; 409 p->finishMode = BCJ2_ENC_FINISH_MODE_CONTINUE;
262 410 }
263 PRF(printf(" ip = %8d tempPos = %8d src = %8d\n", p->ip, p->tempPos, p->srcLim - p->src)); 411 p->src = p->temp;
264 412 p->srcLim = p->temp + p->tempPos;
413 PRF2(" ")
265 Bcj2Enc_Encode_2(p); 414 Bcj2Enc_Encode_2(p);
266
267 { 415 {
268 unsigned num = (unsigned)(p->src - p->temp); 416 const unsigned num = (unsigned)(p->src - p->temp);
269 unsigned tempPos = p->tempPos - num; 417 const unsigned tempPos = p->tempPos - num;
270 unsigned i; 418 unsigned i;
271 p->tempPos = tempPos; 419 p->tempPos = tempPos;
272 for (i = 0; i < tempPos; i++) 420 for (i = 0; i < tempPos; i++)
273 p->temp[i] = p->temp[(size_t)i + num]; 421 p->temp[i] = p->temp[(SizeT)i + num];
274 422 // tempPos : number of bytes in temp buffer
275 p->src = src; 423 p->src = src;
276 p->srcLim = srcLim; 424 p->srcLim = srcLim;
277 p->finishMode = finishMode; 425 p->finishMode = finishMode;
278 426 if (p->state != BCJ2_ENC_STATE_ORIG)
279 if (p->state != BCJ2_ENC_STATE_ORIG || src == srcLim) 427 {
428 // (p->tempPos <= 4) // in non-marker version
429 /* if (the reason of exit from Bcj2Enc_Encode_2()
430 is not BCJ2_ENC_STATE_ORIG),
431 then we exit from Bcj2Enc_Encode() with same reason */
432 // optional code begin : we rollback (src) and tempPos, if it's possible:
433 if (extra >= tempPos)
434 extra = tempPos;
435 p->src = src - extra;
436 p->tempPos = tempPos - extra;
437 // optional code end : rollback of (src) and tempPos
280 return; 438 return;
281 439 }
440 /* (p->tempPos <= 4)
441 (p->state == BCJ2_ENC_STATE_ORIG)
442 so encoder needs more data than in temp[] */
443 if (src == srcLim)
444 return; // src buffer has no more input data.
445 /* (src != srcLim)
446 so we can provide more input data from src for Bcj2Enc_Encode_2() */
282 if (extra >= tempPos) 447 if (extra >= tempPos)
283 { 448 {
284 p->src = src - tempPos; 449 /* (extra >= tempPos) means that temp buffer contains
450 only data from src buffer of this call.
451 So now we can encode without temp buffer */
452 p->src = src - tempPos; // rollback (src)
285 p->tempPos = 0; 453 p->tempPos = 0;
286 break; 454 break;
287 } 455 }
288 456 // we append one additional extra byte from (src) to temp[] buffer:
289 p->temp[tempPos] = src[0]; 457 p->temp[tempPos] = *src;
290 p->tempPos = tempPos + 1; 458 p->tempPos = tempPos + 1;
459 // (0 < p->tempPos <= 5) // in non-marker version
291 p->src = src + 1; 460 p->src = src + 1;
292 extra++; 461 extra++;
293 } 462 }
294 } 463 }
295 } 464 }
296 465
297 PRF(printf("++++ ip = %8d tempPos = %8d src = %8d\n", p->ip, p->tempPos, p->srcLim - p->src)); 466 PRF2("++++")
298 467 // (p->tempPos == 0)
299 Bcj2Enc_Encode_2(p); 468 Bcj2Enc_Encode_2(p);
469 PRF2("====")
300 470
301 if (p->state == BCJ2_ENC_STATE_ORIG) 471 if (p->state == BCJ2_ENC_STATE_ORIG)
302 { 472 {
303 const Byte *src = p->src; 473 const Byte *src = p->src;
304 unsigned rem = (unsigned)(p->srcLim - src); 474 const Byte *srcLim = p->srcLim;
305 unsigned i; 475 const unsigned rem = (unsigned)(srcLim - src);
306 for (i = 0; i < rem; i++) 476 /* (rem <= 4) here.
307 p->temp[i] = src[i]; 477 if (p->src != p->srcLim), then
308 p->tempPos = rem; 478 - we copy non-processed bytes from (p->src) to temp[] buffer,
309 p->src = src + rem; 479 - we set p->src equal to p->srcLim.
480 */
481 if (rem)
482 {
483 unsigned i = 0;
484 p->src = srcLim;
485 p->tempPos = rem;
486 // (0 < p->tempPos <= 4)
487 do
488 p->temp[i] = src[i];
489 while (++i != rem);
490 }
491 // (p->tempPos <= 4)
492 // (p->src == p->srcLim)
310 } 493 }
311} 494}
495
496#undef PRF2
497#undef CONV_FLAG
498#undef MARKER_FLAG
499#undef WRITE_CONTEXT
500#undef WRITE_CONTEXT_AND_SRC
501#undef ONE_ITER
502#undef NUM_SHIFT_BITS
503#undef kTopValue
504#undef kNumBitModelTotalBits
505#undef kBitModelTotal
506#undef kNumMoveBits
diff --git a/C/Blake2.h b/C/Blake2.h
index 14f3cb6..7235235 100644
--- a/C/Blake2.h
+++ b/C/Blake2.h
@@ -1,9 +1,9 @@
1/* Blake2.h -- BLAKE2 Hash 1/* Blake2.h -- BLAKE2 Hash
22015-06-30 : Igor Pavlov : Public domain 22023-03-04 : Igor Pavlov : Public domain
32015 : Samuel Neves : Public domain */ 32015 : Samuel Neves : Public domain */
4 4
5#ifndef __BLAKE2_H 5#ifndef ZIP7_INC_BLAKE2_H
6#define __BLAKE2_H 6#define ZIP7_INC_BLAKE2_H
7 7
8#include "7zTypes.h" 8#include "7zTypes.h"
9 9
diff --git a/C/Blake2s.c b/C/Blake2s.c
index 3c56a8b..2a84b57 100644
--- a/C/Blake2s.c
+++ b/C/Blake2s.c
@@ -1,7 +1,9 @@
1/* Blake2s.c -- BLAKE2s and BLAKE2sp Hash 1/* Blake2s.c -- BLAKE2s and BLAKE2sp Hash
22021-02-09 : Igor Pavlov : Public domain 22023-03-04 : Igor Pavlov : Public domain
32015 : Samuel Neves : Public domain */ 32015 : Samuel Neves : Public domain */
4 4
5#include "Precomp.h"
6
5#include <string.h> 7#include <string.h>
6 8
7#include "Blake2.h" 9#include "Blake2.h"
@@ -78,21 +80,21 @@ static void Blake2s_Compress(CBlake2s *p)
78 a += b + m[sigma[2*i+1]]; d ^= a; d = rotr32(d, 8); c += d; b ^= c; b = rotr32(b, 7); \ 80 a += b + m[sigma[2*i+1]]; d ^= a; d = rotr32(d, 8); c += d; b ^= c; b = rotr32(b, 7); \
79 81
80 #define R(r) \ 82 #define R(r) \
81 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \ 83 G(r,0,v[ 0],v[ 4],v[ 8],v[12]) \
82 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \ 84 G(r,1,v[ 1],v[ 5],v[ 9],v[13]) \
83 G(r,2,v[ 2],v[ 6],v[10],v[14]); \ 85 G(r,2,v[ 2],v[ 6],v[10],v[14]) \
84 G(r,3,v[ 3],v[ 7],v[11],v[15]); \ 86 G(r,3,v[ 3],v[ 7],v[11],v[15]) \
85 G(r,4,v[ 0],v[ 5],v[10],v[15]); \ 87 G(r,4,v[ 0],v[ 5],v[10],v[15]) \
86 G(r,5,v[ 1],v[ 6],v[11],v[12]); \ 88 G(r,5,v[ 1],v[ 6],v[11],v[12]) \
87 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \ 89 G(r,6,v[ 2],v[ 7],v[ 8],v[13]) \
88 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \ 90 G(r,7,v[ 3],v[ 4],v[ 9],v[14]) \
89 91
90 { 92 {
91 unsigned r; 93 unsigned r;
92 for (r = 0; r < BLAKE2S_NUM_ROUNDS; r++) 94 for (r = 0; r < BLAKE2S_NUM_ROUNDS; r++)
93 { 95 {
94 const Byte *sigma = k_Blake2s_Sigma[r]; 96 const Byte *sigma = k_Blake2s_Sigma[r];
95 R(r); 97 R(r)
96 } 98 }
97 /* R(0); R(1); R(2); R(3); R(4); R(5); R(6); R(7); R(8); R(9); */ 99 /* R(0); R(1); R(2); R(3); R(4); R(5); R(6); R(7); R(8); R(9); */
98 } 100 }
@@ -130,7 +132,7 @@ static void Blake2s_Update(CBlake2s *p, const Byte *data, size_t size)
130 } 132 }
131 133
132 memcpy(p->buf + pos, data, rem); 134 memcpy(p->buf + pos, data, rem);
133 Blake2s_Increment_Counter(S, BLAKE2S_BLOCK_SIZE); 135 Blake2s_Increment_Counter(S, BLAKE2S_BLOCK_SIZE)
134 Blake2s_Compress(p); 136 Blake2s_Compress(p);
135 p->bufPos = 0; 137 p->bufPos = 0;
136 data += rem; 138 data += rem;
@@ -143,13 +145,15 @@ static void Blake2s_Final(CBlake2s *p, Byte *digest)
143{ 145{
144 unsigned i; 146 unsigned i;
145 147
146 Blake2s_Increment_Counter(S, (UInt32)p->bufPos); 148 Blake2s_Increment_Counter(S, (UInt32)p->bufPos)
147 Blake2s_Set_LastBlock(p); 149 Blake2s_Set_LastBlock(p)
148 memset(p->buf + p->bufPos, 0, BLAKE2S_BLOCK_SIZE - p->bufPos); 150 memset(p->buf + p->bufPos, 0, BLAKE2S_BLOCK_SIZE - p->bufPos);
149 Blake2s_Compress(p); 151 Blake2s_Compress(p);
150 152
151 for (i = 0; i < 8; i++) 153 for (i = 0; i < 8; i++)
152 SetUi32(digest + sizeof(p->h[i]) * i, p->h[i]); 154 {
155 SetUi32(digest + sizeof(p->h[i]) * i, p->h[i])
156 }
153} 157}
154 158
155 159
@@ -242,3 +246,5 @@ void Blake2sp_Final(CBlake2sp *p, Byte *digest)
242 246
243 Blake2s_Final(&R, digest); 247 Blake2s_Final(&R, digest);
244} 248}
249
250#undef rotr32
diff --git a/C/Bra.c b/C/Bra.c
index 3b854d9..22e0e47 100644
--- a/C/Bra.c
+++ b/C/Bra.c
@@ -1,230 +1,420 @@
1/* Bra.c -- Converters for RISC code 1/* Bra.c -- Branch converters for RISC code
22021-02-09 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#include "CpuArch.h"
7#include "Bra.h" 6#include "Bra.h"
7#include "CpuArch.h"
8#include "RotateDefs.h"
9
10#if defined(MY_CPU_SIZEOF_POINTER) \
11 && ( MY_CPU_SIZEOF_POINTER == 4 \
12 || MY_CPU_SIZEOF_POINTER == 8)
13 #define BR_CONV_USE_OPT_PC_PTR
14#endif
15
16#ifdef BR_CONV_USE_OPT_PC_PTR
17#define BR_PC_INIT pc -= (UInt32)(SizeT)p;
18#define BR_PC_GET (pc + (UInt32)(SizeT)p)
19#else
20#define BR_PC_INIT pc += (UInt32)size;
21#define BR_PC_GET (pc - (UInt32)(SizeT)(lim - p))
22// #define BR_PC_INIT
23// #define BR_PC_GET (pc + (UInt32)(SizeT)(p - data))
24#endif
25
26#define BR_CONVERT_VAL(v, c) if (encoding) v += c; else v -= c;
27// #define BR_CONVERT_VAL(v, c) if (!encoding) c = (UInt32)0 - c; v += c;
28
29#define Z7_BRANCH_CONV(name) z7_BranchConv_ ## name
30
31#define Z7_BRANCH_FUNC_MAIN(name) \
32static \
33Z7_FORCE_INLINE \
34Z7_ATTRIB_NO_VECTOR \
35Byte *Z7_BRANCH_CONV(name)(Byte *p, SizeT size, UInt32 pc, int encoding)
8 36
9SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 37#define Z7_BRANCH_FUNC_IMP(name, m, encoding) \
38Z7_NO_INLINE \
39Z7_ATTRIB_NO_VECTOR \
40Byte *m(name)(Byte *data, SizeT size, UInt32 pc) \
41 { return Z7_BRANCH_CONV(name)(data, size, pc, encoding); } \
42
43#ifdef Z7_EXTRACT_ONLY
44#define Z7_BRANCH_FUNCS_IMP(name) \
45 Z7_BRANCH_FUNC_IMP(name, Z7_BRANCH_CONV_DEC, 0)
46#else
47#define Z7_BRANCH_FUNCS_IMP(name) \
48 Z7_BRANCH_FUNC_IMP(name, Z7_BRANCH_CONV_DEC, 0) \
49 Z7_BRANCH_FUNC_IMP(name, Z7_BRANCH_CONV_ENC, 1)
50#endif
51
52#if defined(__clang__)
53#define BR_EXTERNAL_FOR
54#define BR_NEXT_ITERATION continue;
55#else
56#define BR_EXTERNAL_FOR for (;;)
57#define BR_NEXT_ITERATION break;
58#endif
59
60#if defined(__clang__) && (__clang_major__ >= 8) \
61 || defined(__GNUC__) && (__GNUC__ >= 1000) \
62 // GCC is not good for __builtin_expect() here
63 /* || defined(_MSC_VER) && (_MSC_VER >= 1920) */
64 // #define Z7_unlikely [[unlikely]]
65 // #define Z7_LIKELY(x) (__builtin_expect((x), 1))
66 #define Z7_UNLIKELY(x) (__builtin_expect((x), 0))
67 // #define Z7_likely [[likely]]
68#else
69 // #define Z7_LIKELY(x) (x)
70 #define Z7_UNLIKELY(x) (x)
71 // #define Z7_likely
72#endif
73
74
75Z7_BRANCH_FUNC_MAIN(ARM64)
10{ 76{
11 Byte *p; 77 // Byte *p = data;
12 const Byte *lim; 78 const Byte *lim;
13 size &= ~(size_t)3; 79 const UInt32 flag = (UInt32)1 << (24 - 4);
14 ip += 4; 80 const UInt32 mask = ((UInt32)1 << 24) - (flag << 1);
15 p = data; 81 size &= ~(SizeT)3;
16 lim = data + size; 82 // if (size == 0) return p;
83 lim = p + size;
84 BR_PC_INIT
85 pc -= 4; // because (p) will point to next instruction
86
87 BR_EXTERNAL_FOR
88 {
89 // Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
90 for (;;)
91 {
92 UInt32 v;
93 if Z7_UNLIKELY(p == lim)
94 return p;
95 v = GetUi32a(p);
96 p += 4;
97 if Z7_UNLIKELY(((v - 0x94000000) & 0xfc000000) == 0)
98 {
99 UInt32 c = BR_PC_GET >> 2;
100 BR_CONVERT_VAL(v, c)
101 v &= 0x03ffffff;
102 v |= 0x94000000;
103 SetUi32a(p - 4, v)
104 BR_NEXT_ITERATION
105 }
106 // v = rotlFixed(v, 8); v += (flag << 8) - 0x90; if Z7_UNLIKELY((v & ((mask << 8) + 0x9f)) == 0)
107 v -= 0x90000000; if Z7_UNLIKELY((v & 0x9f000000) == 0)
108 {
109 UInt32 z, c;
110 // v = rotrFixed(v, 8);
111 v += flag; if Z7_UNLIKELY(v & mask) continue;
112 z = (v & 0xffffffe0) | (v >> 26);
113 c = (BR_PC_GET >> (12 - 3)) & ~(UInt32)7;
114 BR_CONVERT_VAL(z, c)
115 v &= 0x1f;
116 v |= 0x90000000;
117 v |= z << 26;
118 v |= 0x00ffffe0 & ((z & (((flag << 1) - 1))) - flag);
119 SetUi32a(p - 4, v)
120 }
121 }
122 }
123}
124Z7_BRANCH_FUNCS_IMP(ARM64)
17 125
18 if (encoding)
19 126
127Z7_BRANCH_FUNC_MAIN(ARM)
128{
129 // Byte *p = data;
130 const Byte *lim;
131 size &= ~(SizeT)3;
132 lim = p + size;
133 BR_PC_INIT
134 /* in ARM: branch offset is relative to the +2 instructions from current instruction.
135 (p) will point to next instruction */
136 pc += 8 - 4;
137
20 for (;;) 138 for (;;)
21 { 139 {
22 for (;;) 140 for (;;)
23 { 141 {
24 if (p >= lim) 142 if Z7_UNLIKELY(p >= lim) { return p; } p += 4; if Z7_UNLIKELY(p[-1] == 0xeb) break;
25 return (SizeT)(p - data); 143 if Z7_UNLIKELY(p >= lim) { return p; } p += 4; if Z7_UNLIKELY(p[-1] == 0xeb) break;
26 p += 4;
27 if (p[-1] == 0xEB)
28 break;
29 } 144 }
30 { 145 {
31 UInt32 v = GetUi32(p - 4); 146 UInt32 v = GetUi32a(p - 4);
32 v <<= 2; 147 UInt32 c = BR_PC_GET >> 2;
33 v += ip + (UInt32)(p - data); 148 BR_CONVERT_VAL(v, c)
34 v >>= 2; 149 v &= 0x00ffffff;
35 v &= 0x00FFFFFF; 150 v |= 0xeb000000;
36 v |= 0xEB000000; 151 SetUi32a(p - 4, v)
37 SetUi32(p - 4, v);
38 } 152 }
39 } 153 }
154}
155Z7_BRANCH_FUNCS_IMP(ARM)
156
40 157
158Z7_BRANCH_FUNC_MAIN(PPC)
159{
160 // Byte *p = data;
161 const Byte *lim;
162 size &= ~(SizeT)3;
163 lim = p + size;
164 BR_PC_INIT
165 pc -= 4; // because (p) will point to next instruction
166
41 for (;;) 167 for (;;)
42 { 168 {
169 UInt32 v;
43 for (;;) 170 for (;;)
44 { 171 {
45 if (p >= lim) 172 if Z7_UNLIKELY(p == lim)
46 return (SizeT)(p - data); 173 return p;
174 // v = GetBe32a(p);
175 v = *(UInt32 *)(void *)p;
47 p += 4; 176 p += 4;
48 if (p[-1] == 0xEB) 177 // if ((v & 0xfc000003) == 0x48000001) break;
49 break; 178 // if ((p[-4] & 0xFC) == 0x48 && (p[-1] & 3) == 1) break;
179 if Z7_UNLIKELY(
180 ((v - Z7_CONV_BE_TO_NATIVE_CONST32(0x48000001))
181 & Z7_CONV_BE_TO_NATIVE_CONST32(0xfc000003)) == 0) break;
50 } 182 }
51 { 183 {
52 UInt32 v = GetUi32(p - 4); 184 v = Z7_CONV_NATIVE_TO_BE_32(v);
53 v <<= 2; 185 {
54 v -= ip + (UInt32)(p - data); 186 UInt32 c = BR_PC_GET;
55 v >>= 2; 187 BR_CONVERT_VAL(v, c)
56 v &= 0x00FFFFFF; 188 }
57 v |= 0xEB000000; 189 v &= 0x03ffffff;
58 SetUi32(p - 4, v); 190 v |= 0x48000000;
191 SetBe32a(p - 4, v)
59 } 192 }
60 } 193 }
61} 194}
195Z7_BRANCH_FUNCS_IMP(PPC)
62 196
63 197
64SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 198#ifdef Z7_CPU_FAST_ROTATE_SUPPORTED
199#define BR_SPARC_USE_ROTATE
200#endif
201
202Z7_BRANCH_FUNC_MAIN(SPARC)
65{ 203{
66 Byte *p; 204 // Byte *p = data;
67 const Byte *lim; 205 const Byte *lim;
68 size &= ~(size_t)1; 206 const UInt32 flag = (UInt32)1 << 22;
69 p = data; 207 size &= ~(SizeT)3;
70 lim = data + size - 4; 208 lim = p + size;
71 209 BR_PC_INIT
72 if (encoding) 210 pc -= 4; // because (p) will point to next instruction
73
74 for (;;) 211 for (;;)
75 { 212 {
76 UInt32 b1; 213 UInt32 v;
77 for (;;) 214 for (;;)
78 { 215 {
79 UInt32 b3; 216 if Z7_UNLIKELY(p == lim)
80 if (p > lim) 217 return p;
81 return (SizeT)(p - data); 218 /* // the code without GetBe32a():
82 b1 = p[1]; 219 { const UInt32 v = GetUi16a(p) & 0xc0ff; p += 4; if (v == 0x40 || v == 0xc07f) break; }
83 b3 = p[3]; 220 */
84 p += 2; 221 v = GetBe32a(p);
85 b1 ^= 8; 222 p += 4;
86 if ((b3 & b1) >= 0xF8) 223 #ifdef BR_SPARC_USE_ROTATE
224 v = rotlFixed(v, 2);
225 v += (flag << 2) - 1;
226 if Z7_UNLIKELY((v & (3 - (flag << 3))) == 0)
227 #else
228 v += (UInt32)5 << 29;
229 v ^= (UInt32)7 << 29;
230 v += flag;
231 if Z7_UNLIKELY((v & (0 - (flag << 1))) == 0)
232 #endif
87 break; 233 break;
88 } 234 }
89 { 235 {
90 UInt32 v = 236 // UInt32 v = GetBe32a(p - 4);
91 ((UInt32)b1 << 19) 237 #ifndef BR_SPARC_USE_ROTATE
92 + (((UInt32)p[1] & 0x7) << 8) 238 v <<= 2;
93 + (((UInt32)p[-2] << 11)) 239 #endif
94 + (p[0]);
95
96 p += 2;
97 { 240 {
98 UInt32 cur = (ip + (UInt32)(p - data)) >> 1; 241 UInt32 c = BR_PC_GET;
99 v += cur; 242 BR_CONVERT_VAL(v, c)
100 } 243 }
101 244 v &= (flag << 3) - 1;
102 p[-4] = (Byte)(v >> 11); 245 #ifdef BR_SPARC_USE_ROTATE
103 p[-3] = (Byte)(0xF0 | ((v >> 19) & 0x7)); 246 v -= (flag << 2) - 1;
104 p[-2] = (Byte)v; 247 v = rotrFixed(v, 2);
105 p[-1] = (Byte)(0xF8 | (v >> 8)); 248 #else
249 v -= (flag << 2);
250 v >>= 2;
251 v |= (UInt32)1 << 30;
252 #endif
253 SetBe32a(p - 4, v)
106 } 254 }
107 } 255 }
256}
257Z7_BRANCH_FUNCS_IMP(SPARC)
258
259
260Z7_BRANCH_FUNC_MAIN(ARMT)
261{
262 // Byte *p = data;
263 Byte *lim;
264 size &= ~(SizeT)1;
265 // if (size == 0) return p;
266 if (size <= 2) return p;
267 size -= 2;
268 lim = p + size;
269 BR_PC_INIT
270 /* in ARM: branch offset is relative to the +2 instructions from current instruction.
271 (p) will point to the +2 instructions from current instruction */
272 // pc += 4 - 4;
273 // if (encoding) pc -= 0xf800 << 1; else pc += 0xf800 << 1;
274 // #define ARMT_TAIL_PROC { goto armt_tail; }
275 #define ARMT_TAIL_PROC { return p; }
108 276
109 for (;;) 277 do
110 { 278 {
111 UInt32 b1; 279 /* in MSVC 32-bit x86 compilers:
280 UInt32 version : it loads value from memory with movzx
281 Byte version : it loads value to 8-bit register (AL/CL)
282 movzx version is slightly faster in some cpus
283 */
284 unsigned b1;
285 // Byte / unsigned
286 b1 = p[1];
287 // optimized version to reduce one (p >= lim) check:
288 // unsigned a1 = p[1]; b1 = p[3]; p += 2; if Z7_LIKELY((b1 & (a1 ^ 8)) < 0xf8)
112 for (;;) 289 for (;;)
113 { 290 {
114 UInt32 b3; 291 unsigned b3; // Byte / UInt32
115 if (p > lim) 292 /* (Byte)(b3) normalization can use low byte computations in MSVC.
116 return (SizeT)(p - data); 293 It gives smaller code, and no loss of speed in some compilers/cpus.
117 b1 = p[1]; 294 But new MSVC 32-bit x86 compilers use more slow load
118 b3 = p[3]; 295 from memory to low byte register in that case.
119 p += 2; 296 So we try to use full 32-bit computations for faster code.
120 b1 ^= 8; 297 */
121 if ((b3 & b1) >= 0xF8) 298 // if (p >= lim) { ARMT_TAIL_PROC } b3 = b1 + 8; b1 = p[3]; p += 2; if ((b3 & b1) >= 0xf8) break;
122 break; 299 if Z7_UNLIKELY(p >= lim) { ARMT_TAIL_PROC } b3 = p[3]; p += 2; if Z7_UNLIKELY((b3 & (b1 ^ 8)) >= 0xf8) break;
300 if Z7_UNLIKELY(p >= lim) { ARMT_TAIL_PROC } b1 = p[3]; p += 2; if Z7_UNLIKELY((b1 & (b3 ^ 8)) >= 0xf8) break;
123 } 301 }
124 { 302 {
303 /* we can adjust pc for (0xf800) to rid of (& 0x7FF) operation.
304 But gcc/clang for arm64 can use bfi instruction for full code here */
125 UInt32 v = 305 UInt32 v =
126 ((UInt32)b1 << 19) 306 ((UInt32)GetUi16a(p - 2) << 11) |
307 ((UInt32)GetUi16a(p) & 0x7FF);
308 /*
309 UInt32 v =
310 ((UInt32)p[1 - 2] << 19)
127 + (((UInt32)p[1] & 0x7) << 8) 311 + (((UInt32)p[1] & 0x7) << 8)
128 + (((UInt32)p[-2] << 11)) 312 + (((UInt32)p[-2] << 11))
129 + (p[0]); 313 + (p[0]);
130 314 */
131 p += 2; 315 p += 2;
132 { 316 {
133 UInt32 cur = (ip + (UInt32)(p - data)) >> 1; 317 UInt32 c = BR_PC_GET >> 1;
134 v -= cur; 318 BR_CONVERT_VAL(v, c)
135 } 319 }
136 320 SetUi16a(p - 4, (UInt16)(((v >> 11) & 0x7ff) | 0xf000))
321 SetUi16a(p - 2, (UInt16)(v | 0xf800))
137 /* 322 /*
138 SetUi16(p - 4, (UInt16)(((v >> 11) & 0x7FF) | 0xF000));
139 SetUi16(p - 2, (UInt16)(v | 0xF800));
140 */
141
142 p[-4] = (Byte)(v >> 11); 323 p[-4] = (Byte)(v >> 11);
143 p[-3] = (Byte)(0xF0 | ((v >> 19) & 0x7)); 324 p[-3] = (Byte)(0xf0 | ((v >> 19) & 0x7));
144 p[-2] = (Byte)v; 325 p[-2] = (Byte)v;
145 p[-1] = (Byte)(0xF8 | (v >> 8)); 326 p[-1] = (Byte)(0xf8 | (v >> 8));
327 */
146 } 328 }
147 } 329 }
330 while (p < lim);
331 return p;
332 // armt_tail:
333 // if ((Byte)((lim[1] & 0xf8)) != 0xf0) { lim += 2; } return lim;
334 // return (Byte *)(lim + ((Byte)((lim[1] ^ 0xf0) & 0xf8) == 0 ? 0 : 2));
335 // return (Byte *)(lim + (((lim[1] ^ ~0xfu) & ~7u) == 0 ? 0 : 2));
336 // return (Byte *)(lim + 2 - (((((unsigned)lim[1] ^ 8) + 8) >> 7) & 2));
148} 337}
338Z7_BRANCH_FUNCS_IMP(ARMT)
149 339
150 340
151SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 341// #define BR_IA64_NO_INLINE
152{
153 Byte *p;
154 const Byte *lim;
155 size &= ~(size_t)3;
156 ip -= 4;
157 p = data;
158 lim = data + size;
159
160 for (;;)
161 {
162 for (;;)
163 {
164 if (p >= lim)
165 return (SizeT)(p - data);
166 p += 4;
167 /* if ((v & 0xFC000003) == 0x48000001) */
168 if ((p[-4] & 0xFC) == 0x48 && (p[-1] & 3) == 1)
169 break;
170 }
171 {
172 UInt32 v = GetBe32(p - 4);
173 if (encoding)
174 v += ip + (UInt32)(p - data);
175 else
176 v -= ip + (UInt32)(p - data);
177 v &= 0x03FFFFFF;
178 v |= 0x48000000;
179 SetBe32(p - 4, v);
180 }
181 }
182}
183
184 342
185SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 343Z7_BRANCH_FUNC_MAIN(IA64)
186{ 344{
187 Byte *p; 345 // Byte *p = data;
188 const Byte *lim; 346 const Byte *lim;
189 size &= ~(size_t)3; 347 size &= ~(SizeT)15;
190 ip -= 4; 348 lim = p + size;
191 p = data; 349 pc -= 1 << 4;
192 lim = data + size; 350 pc >>= 4 - 1;
193 351 // pc -= 1 << 1;
352
194 for (;;) 353 for (;;)
195 { 354 {
355 unsigned m;
196 for (;;) 356 for (;;)
197 { 357 {
198 if (p >= lim) 358 if Z7_UNLIKELY(p == lim)
199 return (SizeT)(p - data); 359 return p;
200 /* 360 m = (unsigned)((UInt32)0x334b0000 >> (*p & 0x1e));
201 v = GetBe32(p); 361 p += 16;
202 p += 4; 362 pc += 1 << 1;
203 m = v + ((UInt32)5 << 29); 363 if (m &= 3)
204 m ^= (UInt32)7 << 29;
205 m += (UInt32)1 << 22;
206 if ((m & ((UInt32)0x1FF << 23)) == 0)
207 break;
208 */
209 p += 4;
210 if ((p[-4] == 0x40 && (p[-3] & 0xC0) == 0) ||
211 (p[-4] == 0x7F && (p[-3] >= 0xC0)))
212 break; 364 break;
213 } 365 }
214 { 366 {
215 UInt32 v = GetBe32(p - 4); 367 p += (ptrdiff_t)m * 5 - 20; // negative value is expected here.
216 v <<= 2; 368 do
217 if (encoding) 369 {
218 v += ip + (UInt32)(p - data); 370 const UInt32 t =
219 else 371 #if defined(MY_CPU_X86_OR_AMD64)
220 v -= ip + (UInt32)(p - data); 372 // we use 32-bit load here to reduce code size on x86:
221 373 GetUi32(p);
222 v &= 0x01FFFFFF; 374 #else
223 v -= (UInt32)1 << 24; 375 GetUi16(p);
224 v ^= 0xFF000000; 376 #endif
225 v >>= 2; 377 UInt32 z = GetUi32(p + 1) >> m;
226 v |= 0x40000000; 378 p += 5;
227 SetBe32(p - 4, v); 379 if (((t >> m) & (0x70 << 1)) == 0
380 && ((z - (0x5000000 << 1)) & (0xf000000 << 1)) == 0)
381 {
382 UInt32 v = (UInt32)((0x8fffff << 1) | 1) & z;
383 z ^= v;
384 #ifdef BR_IA64_NO_INLINE
385 v |= (v & ((UInt32)1 << (23 + 1))) >> 3;
386 {
387 UInt32 c = pc;
388 BR_CONVERT_VAL(v, c)
389 }
390 v &= (0x1fffff << 1) | 1;
391 #else
392 {
393 if (encoding)
394 {
395 // pc &= ~(0xc00000 << 1); // we just need to clear at least 2 bits
396 pc &= (0x1fffff << 1) | 1;
397 v += pc;
398 }
399 else
400 {
401 // pc |= 0xc00000 << 1; // we need to set at least 2 bits
402 pc |= ~(UInt32)((0x1fffff << 1) | 1);
403 v -= pc;
404 }
405 }
406 v &= ~(UInt32)(0x600000 << 1);
407 #endif
408 v += (0x700000 << 1);
409 v &= (0x8fffff << 1) | 1;
410 z |= v;
411 z <<= m;
412 SetUi32(p + 1 - 5, z)
413 }
414 m++;
415 }
416 while (m &= 3); // while (m < 4);
228 } 417 }
229 } 418 }
230} 419}
420Z7_BRANCH_FUNCS_IMP(IA64)
diff --git a/C/Bra.h b/C/Bra.h
index 855e37a..a4ee568 100644
--- a/C/Bra.h
+++ b/C/Bra.h
@@ -1,64 +1,99 @@
1/* Bra.h -- Branch converters for executables 1/* Bra.h -- Branch converters for executables
22013-01-18 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __BRA_H 4#ifndef ZIP7_INC_BRA_H
5#define __BRA_H 5#define ZIP7_INC_BRA_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
9EXTERN_C_BEGIN 9EXTERN_C_BEGIN
10 10
11#define Z7_BRANCH_CONV_DEC(name) z7_BranchConv_ ## name ## _Dec
12#define Z7_BRANCH_CONV_ENC(name) z7_BranchConv_ ## name ## _Enc
13#define Z7_BRANCH_CONV_ST_DEC(name) z7_BranchConvSt_ ## name ## _Dec
14#define Z7_BRANCH_CONV_ST_ENC(name) z7_BranchConvSt_ ## name ## _Enc
15
16#define Z7_BRANCH_CONV_DECL(name) Byte * name(Byte *data, SizeT size, UInt32 pc)
17#define Z7_BRANCH_CONV_ST_DECL(name) Byte * name(Byte *data, SizeT size, UInt32 pc, UInt32 *state)
18
19typedef Z7_BRANCH_CONV_DECL( (*z7_Func_BranchConv));
20typedef Z7_BRANCH_CONV_ST_DECL((*z7_Func_BranchConvSt));
21
22#define Z7_BRANCH_CONV_ST_X86_STATE_INIT_VAL 0
23Z7_BRANCH_CONV_ST_DECL(Z7_BRANCH_CONV_ST_DEC(X86));
24Z7_BRANCH_CONV_ST_DECL(Z7_BRANCH_CONV_ST_ENC(X86));
25
26#define Z7_BRANCH_FUNCS_DECL(name) \
27Z7_BRANCH_CONV_DECL(Z7_BRANCH_CONV_DEC(name)); \
28Z7_BRANCH_CONV_DECL(Z7_BRANCH_CONV_ENC(name));
29
30Z7_BRANCH_FUNCS_DECL(ARM64)
31Z7_BRANCH_FUNCS_DECL(ARM)
32Z7_BRANCH_FUNCS_DECL(ARMT)
33Z7_BRANCH_FUNCS_DECL(PPC)
34Z7_BRANCH_FUNCS_DECL(SPARC)
35Z7_BRANCH_FUNCS_DECL(IA64)
36
11/* 37/*
12These functions convert relative addresses to absolute addresses 38These functions convert data that contain CPU instructions.
13in CALL instructions to increase the compression ratio. 39Each such function converts relative addresses to absolute addresses in some
14 40branch instructions: CALL (in all converters) and JUMP (X86 converter only).
15 In: 41Such conversion allows to increase compression ratio, if we compress that data.
16 data - data buffer 42
17 size - size of data 43There are 2 types of converters:
18 ip - current virtual Instruction Pinter (IP) value 44 Byte * Conv_RISC (Byte *data, SizeT size, UInt32 pc);
19 state - state variable for x86 converter 45 Byte * ConvSt_X86(Byte *data, SizeT size, UInt32 pc, UInt32 *state);
20 encoding - 0 (for decoding), 1 (for encoding) 46Each Converter supports 2 versions: one for encoding
21 47and one for decoding (_Enc/_Dec postfixes in function name).
22 Out:
23 state - state variable for x86 converter
24 48
25 Returns: 49In params:
26 The number of processed bytes. If you call these functions with multiple calls, 50 data : data buffer
27 you must start next call with first byte after block of processed bytes. 51 size : size of data
52 pc : current virtual Program Counter (Instruction Pinter) value
53In/Out param:
54 state : pointer to state variable (for X86 converter only)
55
56Return:
57 The pointer to position in (data) buffer after last byte that was processed.
58 If the caller calls converter again, it must call it starting with that position.
59 But the caller is allowed to move data in buffer. so pointer to
60 current processed position also will be changed for next call.
61 Also the caller must increase internal (pc) value for next call.
28 62
63Each converter has some characteristics: Endian, Alignment, LookAhead.
29 Type Endian Alignment LookAhead 64 Type Endian Alignment LookAhead
30 65
31 x86 little 1 4 66 X86 little 1 4
32 ARMT little 2 2 67 ARMT little 2 2
33 ARM little 4 0 68 ARM little 4 0
69 ARM64 little 4 0
34 PPC big 4 0 70 PPC big 4 0
35 SPARC big 4 0 71 SPARC big 4 0
36 IA64 little 16 0 72 IA64 little 16 0
37 73
38 size must be >= Alignment + LookAhead, if it's not last block. 74 (data) must be aligned for (Alignment).
39 If (size < Alignment + LookAhead), converter returns 0. 75 processed size can be calculated as:
40 76 SizeT processed = Conv(data, size, pc) - data;
41 Example: 77 if (processed == 0)
78 it means that converter needs more data for processing.
79 If (size < Alignment + LookAhead)
80 then (processed == 0) is allowed.
42 81
43 UInt32 ip = 0; 82Example code for conversion in loop:
44 for () 83 UInt32 pc = 0;
45 { 84 size = 0;
46 ; size must be >= Alignment + LookAhead, if it's not last block 85 for (;;)
47 SizeT processed = Convert(data, size, ip, 1); 86 {
48 data += processed; 87 size += Load_more_input_data(data + size);
49 size -= processed; 88 SizeT processed = Conv(data, size, pc) - data;
50 ip += processed; 89 if (processed == 0 && no_more_input_data_after_size)
51 } 90 break; // we stop convert loop
91 data += processed;
92 size -= processed;
93 pc += processed;
94 }
52*/ 95*/
53 96
54#define x86_Convert_Init(state) { state = 0; }
55SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding);
56SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
57SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
58SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
59SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
60SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
61
62EXTERN_C_END 97EXTERN_C_END
63 98
64#endif 99#endif
diff --git a/C/Bra86.c b/C/Bra86.c
index 10a0fbd..d81f392 100644
--- a/C/Bra86.c
+++ b/C/Bra86.c
@@ -1,82 +1,187 @@
1/* Bra86.c -- Converter for x86 code (BCJ) 1/* Bra86.c -- Branch converter for X86 code (BCJ)
22021-02-09 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#include "Bra.h" 6#include "Bra.h"
7#include "CpuArch.h"
7 8
8#define Test86MSByte(b) ((((b) + 1) & 0xFE) == 0)
9 9
10SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding) 10#if defined(MY_CPU_SIZEOF_POINTER) \
11 && ( MY_CPU_SIZEOF_POINTER == 4 \
12 || MY_CPU_SIZEOF_POINTER == 8)
13 #define BR_CONV_USE_OPT_PC_PTR
14#endif
15
16#ifdef BR_CONV_USE_OPT_PC_PTR
17#define BR_PC_INIT pc -= (UInt32)(SizeT)p; // (MY_uintptr_t)
18#define BR_PC_GET (pc + (UInt32)(SizeT)p)
19#else
20#define BR_PC_INIT pc += (UInt32)size;
21#define BR_PC_GET (pc - (UInt32)(SizeT)(lim - p))
22// #define BR_PC_INIT
23// #define BR_PC_GET (pc + (UInt32)(SizeT)(p - data))
24#endif
25
26#define BR_CONVERT_VAL(v, c) if (encoding) v += c; else v -= c;
27// #define BR_CONVERT_VAL(v, c) if (!encoding) c = (UInt32)0 - c; v += c;
28
29#define Z7_BRANCH_CONV_ST(name) z7_BranchConvSt_ ## name
30
31#define BR86_NEED_CONV_FOR_MS_BYTE(b) ((((b) + 1) & 0xfe) == 0)
32
33#ifdef MY_CPU_LE_UNALIGN
34 #define BR86_PREPARE_BCJ_SCAN const UInt32 v = GetUi32(p) ^ 0xe8e8e8e8;
35 #define BR86_IS_BCJ_BYTE(n) ((v & ((UInt32)0xfe << (n) * 8)) == 0)
36#else
37 #define BR86_PREPARE_BCJ_SCAN
38 // bad for MSVC X86 (partial write to byte reg):
39 #define BR86_IS_BCJ_BYTE(n) ((p[n - 4] & 0xfe) == 0xe8)
40 // bad for old MSVC (partial write to byte reg):
41 // #define BR86_IS_BCJ_BYTE(n) (((*p ^ 0xe8) & 0xfe) == 0)
42#endif
43
44static
45Z7_FORCE_INLINE
46Z7_ATTRIB_NO_VECTOR
47Byte *Z7_BRANCH_CONV_ST(X86)(Byte *p, SizeT size, UInt32 pc, UInt32 *state, int encoding)
11{ 48{
12 SizeT pos = 0;
13 UInt32 mask = *state & 7;
14 if (size < 5) 49 if (size < 5)
15 return 0; 50 return p;
16 size -= 4; 51 {
17 ip += 5; 52 // Byte *p = data;
53 const Byte *lim = p + size - 4;
54 unsigned mask = (unsigned)*state; // & 7;
55#ifdef BR_CONV_USE_OPT_PC_PTR
56 /* if BR_CONV_USE_OPT_PC_PTR is defined: we need to adjust (pc) for (+4),
57 because call/jump offset is relative to the next instruction.
58 if BR_CONV_USE_OPT_PC_PTR is not defined : we don't need to adjust (pc) for (+4),
59 because BR_PC_GET uses (pc - (lim - p)), and lim was adjusted for (-4) before.
60 */
61 pc += 4;
62#endif
63 BR_PC_INIT
64 goto start;
18 65
19 for (;;) 66 for (;; mask |= 4)
20 { 67 {
21 Byte *p = data + pos; 68 // cont: mask |= 4;
22 const Byte *limit = data + size; 69 start:
23 for (; p < limit; p++) 70 if (p >= lim)
24 if ((*p & 0xFE) == 0xE8) 71 goto fin;
25 break;
26
27 { 72 {
28 SizeT d = (SizeT)(p - data) - pos; 73 BR86_PREPARE_BCJ_SCAN
29 pos = (SizeT)(p - data); 74 p += 4;
30 if (p >= limit) 75 if (BR86_IS_BCJ_BYTE(0)) { goto m0; } mask >>= 1;
31 { 76 if (BR86_IS_BCJ_BYTE(1)) { goto m1; } mask >>= 1;
32 *state = (d > 2 ? 0 : mask >> (unsigned)d); 77 if (BR86_IS_BCJ_BYTE(2)) { goto m2; } mask = 0;
33 return pos; 78 if (BR86_IS_BCJ_BYTE(3)) { goto a3; }
34 }
35 if (d > 2)
36 mask = 0;
37 else
38 {
39 mask >>= (unsigned)d;
40 if (mask != 0 && (mask > 4 || mask == 3 || Test86MSByte(p[(size_t)(mask >> 1) + 1])))
41 {
42 mask = (mask >> 1) | 4;
43 pos++;
44 continue;
45 }
46 }
47 } 79 }
80 goto main_loop;
48 81
49 if (Test86MSByte(p[4])) 82 m0: p--;
83 m1: p--;
84 m2: p--;
85 if (mask == 0)
86 goto a3;
87 if (p > lim)
88 goto fin_p;
89
90 // if (((0x17u >> mask) & 1) == 0)
91 if (mask > 4 || mask == 3)
92 {
93 mask >>= 1;
94 continue; // goto cont;
95 }
96 mask >>= 1;
97 if (BR86_NEED_CONV_FOR_MS_BYTE(p[mask]))
98 continue; // goto cont;
99 // if (!BR86_NEED_CONV_FOR_MS_BYTE(p[3])) continue; // goto cont;
50 { 100 {
51 UInt32 v = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]); 101 UInt32 v = GetUi32(p);
52 UInt32 cur = ip + (UInt32)pos; 102 UInt32 c;
53 pos += 5; 103 v += (1 << 24); if (v & 0xfe000000) continue; // goto cont;
54 if (encoding) 104 c = BR_PC_GET;
55 v += cur; 105 BR_CONVERT_VAL(v, c)
56 else
57 v -= cur;
58 if (mask != 0)
59 { 106 {
60 unsigned sh = (mask & 6) << 2; 107 mask <<= 3;
61 if (Test86MSByte((Byte)(v >> sh))) 108 if (BR86_NEED_CONV_FOR_MS_BYTE(v >> mask))
62 { 109 {
63 v ^= (((UInt32)0x100 << sh) - 1); 110 v ^= (((UInt32)0x100 << mask) - 1);
64 if (encoding) 111 #ifdef MY_CPU_X86
65 v += cur; 112 // for X86 : we can recalculate (c) to reduce register pressure
66 else 113 c = BR_PC_GET;
67 v -= cur; 114 #endif
115 BR_CONVERT_VAL(v, c)
68 } 116 }
69 mask = 0; 117 mask = 0;
70 } 118 }
71 p[1] = (Byte)v; 119 // v = (v & ((1 << 24) - 1)) - (v & (1 << 24));
72 p[2] = (Byte)(v >> 8); 120 v &= (1 << 25) - 1; v -= (1 << 24);
73 p[3] = (Byte)(v >> 16); 121 SetUi32(p, v)
74 p[4] = (Byte)(0 - ((v >> 24) & 1)); 122 p += 4;
123 goto main_loop;
75 } 124 }
76 else 125
126 main_loop:
127 if (p >= lim)
128 goto fin;
129 for (;;)
77 { 130 {
78 mask = (mask >> 1) | 4; 131 BR86_PREPARE_BCJ_SCAN
79 pos++; 132 p += 4;
133 if (BR86_IS_BCJ_BYTE(0)) { goto a0; }
134 if (BR86_IS_BCJ_BYTE(1)) { goto a1; }
135 if (BR86_IS_BCJ_BYTE(2)) { goto a2; }
136 if (BR86_IS_BCJ_BYTE(3)) { goto a3; }
137 if (p >= lim)
138 goto fin;
139 }
140
141 a0: p--;
142 a1: p--;
143 a2: p--;
144 a3:
145 if (p > lim)
146 goto fin_p;
147 // if (!BR86_NEED_CONV_FOR_MS_BYTE(p[3])) continue; // goto cont;
148 {
149 UInt32 v = GetUi32(p);
150 UInt32 c;
151 v += (1 << 24); if (v & 0xfe000000) continue; // goto cont;
152 c = BR_PC_GET;
153 BR_CONVERT_VAL(v, c)
154 // v = (v & ((1 << 24) - 1)) - (v & (1 << 24));
155 v &= (1 << 25) - 1; v -= (1 << 24);
156 SetUi32(p, v)
157 p += 4;
158 goto main_loop;
80 } 159 }
81 } 160 }
161
162fin_p:
163 p--;
164fin:
165 // the following processing for tail is optional and can be commented
166 /*
167 lim += 4;
168 for (; p < lim; p++, mask >>= 1)
169 if ((*p & 0xfe) == 0xe8)
170 break;
171 */
172 *state = (UInt32)mask;
173 return p;
174 }
82} 175}
176
177
178#define Z7_BRANCH_CONV_ST_FUNC_IMP(name, m, encoding) \
179Z7_NO_INLINE \
180Z7_ATTRIB_NO_VECTOR \
181Byte *m(name)(Byte *data, SizeT size, UInt32 pc, UInt32 *state) \
182 { return Z7_BRANCH_CONV_ST(name)(data, size, pc, state, encoding); }
183
184Z7_BRANCH_CONV_ST_FUNC_IMP(X86, Z7_BRANCH_CONV_ST_DEC, 0)
185#ifndef Z7_EXTRACT_ONLY
186Z7_BRANCH_CONV_ST_FUNC_IMP(X86, Z7_BRANCH_CONV_ST_ENC, 1)
187#endif
diff --git a/C/BraIA64.c b/C/BraIA64.c
index d1dbc62..9dfe3e2 100644
--- a/C/BraIA64.c
+++ b/C/BraIA64.c
@@ -1,53 +1,14 @@
1/* BraIA64.c -- Converter for IA-64 code 1/* BraIA64.c -- Converter for IA-64 code
22017-01-26 : Igor Pavlov : Public domain */ 22023-02-20 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#include "CpuArch.h" 6// the code was moved to Bra.c
7#include "Bra.h"
8 7
9SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 8#ifdef _MSC_VER
10{ 9#pragma warning(disable : 4206) // nonstandard extension used : translation unit is empty
11 SizeT i; 10#endif
12 if (size < 16) 11
13 return 0; 12#if defined(__clang__)
14 size -= 16; 13#pragma GCC diagnostic ignored "-Wempty-translation-unit"
15 i = 0; 14#endif
16 do
17 {
18 unsigned m = ((UInt32)0x334B0000 >> (data[i] & 0x1E)) & 3;
19 if (m)
20 {
21 m++;
22 do
23 {
24 Byte *p = data + (i + (size_t)m * 5 - 8);
25 if (((p[3] >> m) & 15) == 5
26 && (((p[-1] | ((UInt32)p[0] << 8)) >> m) & 0x70) == 0)
27 {
28 unsigned raw = GetUi32(p);
29 unsigned v = raw >> m;
30 v = (v & 0xFFFFF) | ((v & (1 << 23)) >> 3);
31
32 v <<= 4;
33 if (encoding)
34 v += ip + (UInt32)i;
35 else
36 v -= ip + (UInt32)i;
37 v >>= 4;
38
39 v &= 0x1FFFFF;
40 v += 0x700000;
41 v &= 0x8FFFFF;
42 raw &= ~((UInt32)0x8FFFFF << m);
43 raw |= (v << m);
44 SetUi32(p, raw);
45 }
46 }
47 while (++m <= 4);
48 }
49 i += 16;
50 }
51 while (i <= size);
52 return i;
53}
diff --git a/C/BwtSort.c b/C/BwtSort.c
index 3eb57ef..05ad6de 100644
--- a/C/BwtSort.c
+++ b/C/BwtSort.c
@@ -1,5 +1,5 @@
1/* BwtSort.c -- BWT block sorting 1/* BwtSort.c -- BWT block sorting
22021-04-01 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -8,8 +8,6 @@
8 8
9/* #define BLOCK_SORT_USE_HEAP_SORT */ 9/* #define BLOCK_SORT_USE_HEAP_SORT */
10 10
11#define NO_INLINE MY_FAST_CALL
12
13/* Don't change it !!! */ 11/* Don't change it !!! */
14#define kNumHashBytes 2 12#define kNumHashBytes 2
15#define kNumHashValues (1 << (kNumHashBytes * 8)) 13#define kNumHashValues (1 << (kNumHashBytes * 8))
@@ -60,7 +58,10 @@ SortGroup - is recursive Range-Sort function with HeapSort optimization for smal
60returns: 1 - if there are groups, 0 - no more groups 58returns: 1 - if there are groups, 0 - no more groups
61*/ 59*/
62 60
63static UInt32 NO_INLINE SortGroup(UInt32 BlockSize, UInt32 NumSortedBytes, UInt32 groupOffset, UInt32 groupSize, int NumRefBits, UInt32 *Indices 61static
62UInt32
63Z7_FASTCALL
64SortGroup(UInt32 BlockSize, UInt32 NumSortedBytes, UInt32 groupOffset, UInt32 groupSize, int NumRefBits, UInt32 *Indices
64 #ifndef BLOCK_SORT_USE_HEAP_SORT 65 #ifndef BLOCK_SORT_USE_HEAP_SORT
65 , UInt32 left, UInt32 range 66 , UInt32 left, UInt32 range
66 #endif 67 #endif
@@ -72,7 +73,7 @@ static UInt32 NO_INLINE SortGroup(UInt32 BlockSize, UInt32 NumSortedBytes, UInt3
72 { 73 {
73 /* 74 /*
74 #ifndef BLOCK_SORT_EXTERNAL_FLAGS 75 #ifndef BLOCK_SORT_EXTERNAL_FLAGS
75 SetFinishedGroupSize(ind2, 1); 76 SetFinishedGroupSize(ind2, 1)
76 #endif 77 #endif
77 */ 78 */
78 return 0; 79 return 0;
@@ -463,7 +464,7 @@ UInt32 BlockSort(UInt32 *Indices, const Byte *data, UInt32 blockSize)
463 Indices[(size_t)(i - finishedGroupSize) + 1] &= kIndexMask; 464 Indices[(size_t)(i - finishedGroupSize) + 1] &= kIndexMask;
464 { 465 {
465 UInt32 newGroupSize = groupSize + finishedGroupSize; 466 UInt32 newGroupSize = groupSize + finishedGroupSize;
466 SetFinishedGroupSize(Indices + i - finishedGroupSize, newGroupSize); 467 SetFinishedGroupSize(Indices + i - finishedGroupSize, newGroupSize)
467 finishedGroupSize = newGroupSize; 468 finishedGroupSize = newGroupSize;
468 } 469 }
469 i += groupSize; 470 i += groupSize;
diff --git a/C/BwtSort.h b/C/BwtSort.h
index 7e989a9..a34b243 100644
--- a/C/BwtSort.h
+++ b/C/BwtSort.h
@@ -1,8 +1,8 @@
1/* BwtSort.h -- BWT block sorting 1/* BwtSort.h -- BWT block sorting
22013-01-18 : Igor Pavlov : Public domain */ 22023-03-03 : Igor Pavlov : Public domain */
3 3
4#ifndef __BWT_SORT_H 4#ifndef ZIP7_INC_BWT_SORT_H
5#define __BWT_SORT_H 5#define ZIP7_INC_BWT_SORT_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
diff --git a/C/Compiler.h b/C/Compiler.h
index a9816fa..185a52d 100644
--- a/C/Compiler.h
+++ b/C/Compiler.h
@@ -1,12 +1,37 @@
1/* Compiler.h 1/* Compiler.h : Compiler specific defines and pragmas
22021-01-05 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_COMPILER_H 4#ifndef ZIP7_INC_COMPILER_H
5#define __7Z_COMPILER_H 5#define ZIP7_INC_COMPILER_H
6
7#if defined(__clang__)
8# define Z7_CLANG_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
9#endif
10#if defined(__clang__) && defined(__apple_build_version__)
11# define Z7_APPLE_CLANG_VERSION Z7_CLANG_VERSION
12#elif defined(__clang__)
13# define Z7_LLVM_CLANG_VERSION Z7_CLANG_VERSION
14#elif defined(__GNUC__)
15# define Z7_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
16#endif
17
18#ifdef _MSC_VER
19#if !defined(__clang__) && !defined(__GNUC__)
20#define Z7_MSC_VER_ORIGINAL _MSC_VER
21#endif
22#endif
23
24#if defined(__MINGW32__) || defined(__MINGW64__)
25#define Z7_MINGW
26#endif
27
28// #pragma GCC diagnostic ignored "-Wunknown-pragmas"
29
30#ifdef __clang__
31// padding size of '' with 4 bytes to alignment boundary
32#pragma GCC diagnostic ignored "-Wpadded"
33#endif
6 34
7 #ifdef __clang__
8 #pragma clang diagnostic ignored "-Wunused-private-field"
9 #endif
10 35
11#ifdef _MSC_VER 36#ifdef _MSC_VER
12 37
@@ -17,24 +42,115 @@
17 #pragma warning(disable : 4214) // nonstandard extension used : bit field types other than int 42 #pragma warning(disable : 4214) // nonstandard extension used : bit field types other than int
18 #endif 43 #endif
19 44
20 #if _MSC_VER >= 1300 45#if defined(_MSC_VER) && _MSC_VER >= 1800
21 #pragma warning(disable : 4996) // This function or variable may be unsafe 46#pragma warning(disable : 4464) // relative include path contains '..'
22 #else 47#endif
23 #pragma warning(disable : 4511) // copy constructor could not be generated 48
24 #pragma warning(disable : 4512) // assignment operator could not be generated 49// == 1200 : -O1 : for __forceinline
25 #pragma warning(disable : 4514) // unreferenced inline function has been removed 50// >= 1900 : -O1 : for printf
26 #pragma warning(disable : 4702) // unreachable code 51#pragma warning(disable : 4710) // function not inlined
27 #pragma warning(disable : 4710) // not inlined 52
28 #pragma warning(disable : 4714) // function marked as __forceinline not inlined 53#if _MSC_VER < 1900
29 #pragma warning(disable : 4786) // identifier was truncated to '255' characters in the debug information 54// winnt.h: 'Int64ShllMod32'
30 #endif 55#pragma warning(disable : 4514) // unreferenced inline function has been removed
56#endif
57
58#if _MSC_VER < 1300
59// #pragma warning(disable : 4702) // unreachable code
60// Bra.c : -O1:
61#pragma warning(disable : 4714) // function marked as __forceinline not inlined
62#endif
63
64/*
65#if _MSC_VER > 1400 && _MSC_VER <= 1900
66// strcat: This function or variable may be unsafe
67// sysinfoapi.h: kit10: GetVersion was declared deprecated
68#pragma warning(disable : 4996)
69#endif
70*/
71
72#if _MSC_VER > 1200
73// -Wall warnings
74
75#pragma warning(disable : 4711) // function selected for automatic inline expansion
76#pragma warning(disable : 4820) // '2' bytes padding added after data member
77
78#if _MSC_VER >= 1400 && _MSC_VER < 1920
79// 1400: string.h: _DBG_MEMCPY_INLINE_
80// 1600 - 191x : smmintrin.h __cplusplus'
81// is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
82#pragma warning(disable : 4668)
83
84// 1400 - 1600 : WinDef.h : 'FARPROC' :
85// 1900 - 191x : immintrin.h: _readfsbase_u32
86// no function prototype given : converting '()' to '(void)'
87#pragma warning(disable : 4255)
88#endif
89
90#if _MSC_VER >= 1914
91// Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
92#pragma warning(disable : 5045)
93#endif
94
95#endif // _MSC_VER > 1200
96#endif // _MSC_VER
97
98
99#if defined(__clang__) && (__clang_major__ >= 4)
100 #define Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE \
101 _Pragma("clang loop unroll(disable)") \
102 _Pragma("clang loop vectorize(disable)")
103 #define Z7_ATTRIB_NO_VECTORIZE
104#elif defined(__GNUC__) && (__GNUC__ >= 5)
105 #define Z7_ATTRIB_NO_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
106 // __attribute__((optimize("no-unroll-loops")));
107 #define Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
108#elif defined(_MSC_VER) && (_MSC_VER >= 1920)
109 #define Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE \
110 _Pragma("loop( no_vector )")
111 #define Z7_ATTRIB_NO_VECTORIZE
112#else
113 #define Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
114 #define Z7_ATTRIB_NO_VECTORIZE
115#endif
116
117#if defined(MY_CPU_X86_OR_AMD64) && ( \
118 defined(__clang__) && (__clang_major__ >= 4) \
119 || defined(__GNUC__) && (__GNUC__ >= 5))
120 #define Z7_ATTRIB_NO_SSE __attribute__((__target__("no-sse")))
121#else
122 #define Z7_ATTRIB_NO_SSE
123#endif
124
125#define Z7_ATTRIB_NO_VECTOR \
126 Z7_ATTRIB_NO_VECTORIZE \
127 Z7_ATTRIB_NO_SSE
128
129
130#if defined(__clang__) && (__clang_major__ >= 8) \
131 || defined(__GNUC__) && (__GNUC__ >= 1000) \
132 /* || defined(_MSC_VER) && (_MSC_VER >= 1920) */
133 // GCC is not good for __builtin_expect()
134 #define Z7_LIKELY(x) (__builtin_expect((x), 1))
135 #define Z7_UNLIKELY(x) (__builtin_expect((x), 0))
136 // #define Z7_unlikely [[unlikely]]
137 // #define Z7_likely [[likely]]
138#else
139 #define Z7_LIKELY(x) (x)
140 #define Z7_UNLIKELY(x) (x)
141 // #define Z7_likely
142#endif
31 143
32 #ifdef __clang__
33 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
34 #pragma clang diagnostic ignored "-Wmicrosoft-exception-spec"
35 // #pragma clang diagnostic ignored "-Wreserved-id-macro"
36 #endif
37 144
145#if (defined(Z7_CLANG_VERSION) && (Z7_CLANG_VERSION >= 36000))
146#define Z7_DIAGNOSCTIC_IGNORE_BEGIN_RESERVED_MACRO_IDENTIFIER \
147 _Pragma("GCC diagnostic push") \
148 _Pragma("GCC diagnostic ignored \"-Wreserved-macro-identifier\"")
149#define Z7_DIAGNOSCTIC_IGNORE_END_RESERVED_MACRO_IDENTIFIER \
150 _Pragma("GCC diagnostic pop")
151#else
152#define Z7_DIAGNOSCTIC_IGNORE_BEGIN_RESERVED_MACRO_IDENTIFIER
153#define Z7_DIAGNOSCTIC_IGNORE_END_RESERVED_MACRO_IDENTIFIER
38#endif 154#endif
39 155
40#define UNUSED_VAR(x) (void)x; 156#define UNUSED_VAR(x) (void)x;
diff --git a/C/CpuArch.c b/C/CpuArch.c
index fa9afe3..33f8a3a 100644
--- a/C/CpuArch.c
+++ b/C/CpuArch.c
@@ -1,187 +1,318 @@
1/* CpuArch.c -- CPU specific code 1/* CpuArch.c -- CPU specific code
22021-07-13 : Igor Pavlov : Public domain */ 22023-05-18 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6// #include <stdio.h>
7
6#include "CpuArch.h" 8#include "CpuArch.h"
7 9
8#ifdef MY_CPU_X86_OR_AMD64 10#ifdef MY_CPU_X86_OR_AMD64
9 11
10#if (defined(_MSC_VER) && !defined(MY_CPU_AMD64)) || defined(__GNUC__) 12#undef NEED_CHECK_FOR_CPUID
11#define USE_ASM 13#if !defined(MY_CPU_AMD64)
14#define NEED_CHECK_FOR_CPUID
12#endif 15#endif
13 16
14#if !defined(USE_ASM) && _MSC_VER >= 1500 17/*
15#include <intrin.h> 18 cpuid instruction supports (subFunction) parameter in ECX,
19 that is used only with some specific (function) parameter values.
20 But we always use only (subFunction==0).
21*/
22/*
23 __cpuid(): MSVC and GCC/CLANG use same function/macro name
24 but parameters are different.
25 We use MSVC __cpuid() parameters style for our z7_x86_cpuid() function.
26*/
27
28#if defined(__GNUC__) /* && (__GNUC__ >= 10) */ \
29 || defined(__clang__) /* && (__clang_major__ >= 10) */
30
31/* there was some CLANG/GCC compilers that have issues with
32 rbx(ebx) handling in asm blocks in -fPIC mode (__PIC__ is defined).
33 compiler's <cpuid.h> contains the macro __cpuid() that is similar to our code.
34 The history of __cpuid() changes in CLANG/GCC:
35 GCC:
36 2007: it preserved ebx for (__PIC__ && __i386__)
37 2013: it preserved rbx and ebx for __PIC__
38 2014: it doesn't preserves rbx and ebx anymore
39 we suppose that (__GNUC__ >= 5) fixed that __PIC__ ebx/rbx problem.
40 CLANG:
41 2014+: it preserves rbx, but only for 64-bit code. No __PIC__ check.
42 Why CLANG cares about 64-bit mode only, and doesn't care about ebx (in 32-bit)?
43 Do we need __PIC__ test for CLANG or we must care about rbx even if
44 __PIC__ is not defined?
45*/
46
47#define ASM_LN "\n"
48
49#if defined(MY_CPU_AMD64) && defined(__PIC__) \
50 && ((defined (__GNUC__) && (__GNUC__ < 5)) || defined(__clang__))
51
52#define x86_cpuid_MACRO(p, func) { \
53 __asm__ __volatile__ ( \
54 ASM_LN "mov %%rbx, %q1" \
55 ASM_LN "cpuid" \
56 ASM_LN "xchg %%rbx, %q1" \
57 : "=a" ((p)[0]), "=&r" ((p)[1]), "=c" ((p)[2]), "=d" ((p)[3]) : "0" (func), "2"(0)); }
58
59 /* "=&r" selects free register. It can select even rbx, if that register is free.
60 "=&D" for (RDI) also works, but the code can be larger with "=&D"
61 "2"(0) means (subFunction = 0),
62 2 is (zero-based) index in the output constraint list "=c" (ECX). */
63
64#elif defined(MY_CPU_X86) && defined(__PIC__) \
65 && ((defined (__GNUC__) && (__GNUC__ < 5)) || defined(__clang__))
66
67#define x86_cpuid_MACRO(p, func) { \
68 __asm__ __volatile__ ( \
69 ASM_LN "mov %%ebx, %k1" \
70 ASM_LN "cpuid" \
71 ASM_LN "xchg %%ebx, %k1" \
72 : "=a" ((p)[0]), "=&r" ((p)[1]), "=c" ((p)[2]), "=d" ((p)[3]) : "0" (func), "2"(0)); }
73
74#else
75
76#define x86_cpuid_MACRO(p, func) { \
77 __asm__ __volatile__ ( \
78 ASM_LN "cpuid" \
79 : "=a" ((p)[0]), "=b" ((p)[1]), "=c" ((p)[2]), "=d" ((p)[3]) : "0" (func), "2"(0)); }
80
16#endif 81#endif
17 82
18#if defined(USE_ASM) && !defined(MY_CPU_AMD64) 83
19static UInt32 CheckFlag(UInt32 flag) 84void Z7_FASTCALL z7_x86_cpuid(UInt32 p[4], UInt32 func)
20{ 85{
21 #ifdef _MSC_VER 86 x86_cpuid_MACRO(p, func)
22 __asm pushfd; 87}
23 __asm pop EAX; 88
24 __asm mov EDX, EAX; 89
25 __asm xor EAX, flag; 90Z7_NO_INLINE
26 __asm push EAX; 91UInt32 Z7_FASTCALL z7_x86_cpuid_GetMaxFunc(void)
27 __asm popfd; 92{
28 __asm pushfd; 93 #if defined(NEED_CHECK_FOR_CPUID)
29 __asm pop EAX; 94 #define EFALGS_CPUID_BIT 21
30 __asm xor EAX, EDX; 95 UInt32 a;
31 __asm push EDX;
32 __asm popfd;
33 __asm and flag, EAX;
34 #else
35 __asm__ __volatile__ ( 96 __asm__ __volatile__ (
36 "pushf\n\t" 97 ASM_LN "pushf"
37 "pop %%EAX\n\t" 98 ASM_LN "pushf"
38 "movl %%EAX,%%EDX\n\t" 99 ASM_LN "pop %0"
39 "xorl %0,%%EAX\n\t" 100 // ASM_LN "movl %0, %1"
40 "push %%EAX\n\t" 101 // ASM_LN "xorl $0x200000, %0"
41 "popf\n\t" 102 ASM_LN "btc %1, %0"
42 "pushf\n\t" 103 ASM_LN "push %0"
43 "pop %%EAX\n\t" 104 ASM_LN "popf"
44 "xorl %%EDX,%%EAX\n\t" 105 ASM_LN "pushf"
45 "push %%EDX\n\t" 106 ASM_LN "pop %0"
46 "popf\n\t" 107 ASM_LN "xorl (%%esp), %0"
47 "andl %%EAX, %0\n\t": 108
48 "=c" (flag) : "c" (flag) : 109 ASM_LN "popf"
49 "%eax", "%edx"); 110 ASM_LN
111 : "=&r" (a) // "=a"
112 : "i" (EFALGS_CPUID_BIT)
113 );
114 if ((a & (1 << EFALGS_CPUID_BIT)) == 0)
115 return 0;
116 #endif
117 {
118 UInt32 p[4];
119 x86_cpuid_MACRO(p, 0)
120 return p[0];
121 }
122}
123
124#undef ASM_LN
125
126#elif !defined(_MSC_VER)
127
128/*
129// for gcc/clang and other: we can try to use __cpuid macro:
130#include <cpuid.h>
131void Z7_FASTCALL z7_x86_cpuid(UInt32 p[4], UInt32 func)
132{
133 __cpuid(func, p[0], p[1], p[2], p[3]);
134}
135UInt32 Z7_FASTCALL z7_x86_cpuid_GetMaxFunc(void)
136{
137 return (UInt32)__get_cpuid_max(0, NULL);
138}
139*/
140// for unsupported cpuid:
141void Z7_FASTCALL z7_x86_cpuid(UInt32 p[4], UInt32 func)
142{
143 UNUSED_VAR(func)
144 p[0] = p[1] = p[2] = p[3] = 0;
145}
146UInt32 Z7_FASTCALL z7_x86_cpuid_GetMaxFunc(void)
147{
148 return 0;
149}
150
151#else // _MSC_VER
152
153#if !defined(MY_CPU_AMD64)
154
155UInt32 __declspec(naked) Z7_FASTCALL z7_x86_cpuid_GetMaxFunc(void)
156{
157 #if defined(NEED_CHECK_FOR_CPUID)
158 #define EFALGS_CPUID_BIT 21
159 __asm pushfd
160 __asm pushfd
161 /*
162 __asm pop eax
163 // __asm mov edx, eax
164 __asm btc eax, EFALGS_CPUID_BIT
165 __asm push eax
166 */
167 __asm btc dword ptr [esp], EFALGS_CPUID_BIT
168 __asm popfd
169 __asm pushfd
170 __asm pop eax
171 // __asm xor eax, edx
172 __asm xor eax, [esp]
173 // __asm push edx
174 __asm popfd
175 __asm and eax, (1 shl EFALGS_CPUID_BIT)
176 __asm jz end_func
177 #endif
178 __asm push ebx
179 __asm xor eax, eax // func
180 __asm xor ecx, ecx // subFunction (optional) for (func == 0)
181 __asm cpuid
182 __asm pop ebx
183 #if defined(NEED_CHECK_FOR_CPUID)
184 end_func:
50 #endif 185 #endif
51 return flag; 186 __asm ret 0
52} 187}
53#define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
54#else
55#define CHECK_CPUID_IS_SUPPORTED
56#endif
57 188
58#ifndef USE_ASM 189void __declspec(naked) Z7_FASTCALL z7_x86_cpuid(UInt32 p[4], UInt32 func)
59 #ifdef _MSC_VER 190{
191 UNUSED_VAR(p)
192 UNUSED_VAR(func)
193 __asm push ebx
194 __asm push edi
195 __asm mov edi, ecx // p
196 __asm mov eax, edx // func
197 __asm xor ecx, ecx // subfunction (optional) for (func == 0)
198 __asm cpuid
199 __asm mov [edi ], eax
200 __asm mov [edi + 4], ebx
201 __asm mov [edi + 8], ecx
202 __asm mov [edi + 12], edx
203 __asm pop edi
204 __asm pop ebx
205 __asm ret 0
206}
207
208#else // MY_CPU_AMD64
209
60 #if _MSC_VER >= 1600 210 #if _MSC_VER >= 1600
61 #define MY__cpuidex __cpuidex 211 #include <intrin.h>
212 #define MY_cpuidex __cpuidex
62 #else 213 #else
63
64/* 214/*
65 __cpuid (function == 4) requires subfunction number in ECX. 215 __cpuid (func == (0 or 7)) requires subfunction number in ECX.
66 MSDN: The __cpuid intrinsic clears the ECX register before calling the cpuid instruction. 216 MSDN: The __cpuid intrinsic clears the ECX register before calling the cpuid instruction.
67 __cpuid() in new MSVC clears ECX. 217 __cpuid() in new MSVC clears ECX.
68 __cpuid() in old MSVC (14.00) doesn't clear ECX 218 __cpuid() in old MSVC (14.00) x64 doesn't clear ECX
69 We still can use __cpuid for low (function) values that don't require ECX, 219 We still can use __cpuid for low (func) values that don't require ECX,
70 but __cpuid() in old MSVC will be incorrect for some function values: (function == 4). 220 but __cpuid() in old MSVC will be incorrect for some func values: (func == 7).
71 So here we use the hack for old MSVC to send (subFunction) in ECX register to cpuid instruction, 221 So here we use the hack for old MSVC to send (subFunction) in ECX register to cpuid instruction,
72 where ECX value is first parameter for FAST_CALL / NO_INLINE function, 222 where ECX value is first parameter for FASTCALL / NO_INLINE func,
73 So the caller of MY__cpuidex_HACK() sets ECX as subFunction, and 223 So the caller of MY_cpuidex_HACK() sets ECX as subFunction, and
74 old MSVC for __cpuid() doesn't change ECX and cpuid instruction gets (subFunction) value. 224 old MSVC for __cpuid() doesn't change ECX and cpuid instruction gets (subFunction) value.
75 225
76 DON'T remove MY_NO_INLINE and MY_FAST_CALL for MY__cpuidex_HACK() !!! 226DON'T remove Z7_NO_INLINE and Z7_FASTCALL for MY_cpuidex_HACK(): !!!
77*/ 227*/
78
79static 228static
80MY_NO_INLINE 229Z7_NO_INLINE void Z7_FASTCALL MY_cpuidex_HACK(UInt32 subFunction, UInt32 func, int *CPUInfo)
81void MY_FAST_CALL MY__cpuidex_HACK(UInt32 subFunction, int *CPUInfo, UInt32 function)
82{ 230{
83 UNUSED_VAR(subFunction); 231 UNUSED_VAR(subFunction)
84 __cpuid(CPUInfo, function); 232 __cpuid(CPUInfo, func);
85} 233}
86 234 #define MY_cpuidex(info, func, func2) MY_cpuidex_HACK(func2, func, info)
87 #define MY__cpuidex(info, func, func2) MY__cpuidex_HACK(func2, info, func) 235 #pragma message("======== MY_cpuidex_HACK WAS USED ========")
88 #pragma message("======== MY__cpuidex_HACK WAS USED ========") 236 #endif // _MSC_VER >= 1600
89 #endif 237
90 #else 238#if !defined(MY_CPU_AMD64)
91 #define MY__cpuidex(info, func, func2) __cpuid(info, func) 239/* inlining for __cpuid() in MSVC x86 (32-bit) produces big ineffective code,
92 #pragma message("======== (INCORRECT ?) cpuid WAS USED ========") 240 so we disable inlining here */
93 #endif 241Z7_NO_INLINE
94#endif 242#endif
95 243void Z7_FASTCALL z7_x86_cpuid(UInt32 p[4], UInt32 func)
96
97
98
99void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d)
100{ 244{
101 #ifdef USE_ASM 245 MY_cpuidex((int *)p, (int)func, 0);
102 246}
103 #ifdef _MSC_VER
104
105 UInt32 a2, b2, c2, d2;
106 __asm xor EBX, EBX;
107 __asm xor ECX, ECX;
108 __asm xor EDX, EDX;
109 __asm mov EAX, function;
110 __asm cpuid;
111 __asm mov a2, EAX;
112 __asm mov b2, EBX;
113 __asm mov c2, ECX;
114 __asm mov d2, EDX;
115
116 *a = a2;
117 *b = b2;
118 *c = c2;
119 *d = d2;
120
121 #else
122
123 __asm__ __volatile__ (
124 #if defined(MY_CPU_AMD64) && defined(__PIC__)
125 "mov %%rbx, %%rdi;"
126 "cpuid;"
127 "xchg %%rbx, %%rdi;"
128 : "=a" (*a) ,
129 "=D" (*b) ,
130 #elif defined(MY_CPU_X86) && defined(__PIC__)
131 "mov %%ebx, %%edi;"
132 "cpuid;"
133 "xchgl %%ebx, %%edi;"
134 : "=a" (*a) ,
135 "=D" (*b) ,
136 #else
137 "cpuid"
138 : "=a" (*a) ,
139 "=b" (*b) ,
140 #endif
141 "=c" (*c) ,
142 "=d" (*d)
143 : "0" (function), "c"(0) ) ;
144
145 #endif
146
147 #else
148 247
149 int CPUInfo[4]; 248Z7_NO_INLINE
249UInt32 Z7_FASTCALL z7_x86_cpuid_GetMaxFunc(void)
250{
251 int a[4];
252 MY_cpuidex(a, 0, 0);
253 return a[0];
254}
150 255
151 MY__cpuidex(CPUInfo, (int)function, 0); 256#endif // MY_CPU_AMD64
257#endif // _MSC_VER
152 258
153 *a = (UInt32)CPUInfo[0]; 259#if defined(NEED_CHECK_FOR_CPUID)
154 *b = (UInt32)CPUInfo[1]; 260#define CHECK_CPUID_IS_SUPPORTED { if (z7_x86_cpuid_GetMaxFunc() == 0) return 0; }
155 *c = (UInt32)CPUInfo[2]; 261#else
156 *d = (UInt32)CPUInfo[3]; 262#define CHECK_CPUID_IS_SUPPORTED
263#endif
264#undef NEED_CHECK_FOR_CPUID
157 265
158 #endif
159}
160 266
161BoolInt x86cpuid_CheckAndRead(Cx86cpuid *p) 267static
268BoolInt x86cpuid_Func_1(UInt32 *p)
162{ 269{
163 CHECK_CPUID_IS_SUPPORTED 270 CHECK_CPUID_IS_SUPPORTED
164 MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]); 271 z7_x86_cpuid(p, 1);
165 MyCPUID(1, &p->ver, &p->b, &p->c, &p->d);
166 return True; 272 return True;
167} 273}
168 274
169static const UInt32 kVendors[][3] = 275/*
276static const UInt32 kVendors[][1] =
170{ 277{
171 { 0x756E6547, 0x49656E69, 0x6C65746E}, 278 { 0x756E6547 }, // , 0x49656E69, 0x6C65746E },
172 { 0x68747541, 0x69746E65, 0x444D4163}, 279 { 0x68747541 }, // , 0x69746E65, 0x444D4163 },
173 { 0x746E6543, 0x48727561, 0x736C7561} 280 { 0x746E6543 } // , 0x48727561, 0x736C7561 }
174}; 281};
282*/
283
284/*
285typedef struct
286{
287 UInt32 maxFunc;
288 UInt32 vendor[3];
289 UInt32 ver;
290 UInt32 b;
291 UInt32 c;
292 UInt32 d;
293} Cx86cpuid;
294
295enum
296{
297 CPU_FIRM_INTEL,
298 CPU_FIRM_AMD,
299 CPU_FIRM_VIA
300};
301int x86cpuid_GetFirm(const Cx86cpuid *p);
302#define x86cpuid_ver_GetFamily(ver) (((ver >> 16) & 0xff0) | ((ver >> 8) & 0xf))
303#define x86cpuid_ver_GetModel(ver) (((ver >> 12) & 0xf0) | ((ver >> 4) & 0xf))
304#define x86cpuid_ver_GetStepping(ver) (ver & 0xf)
175 305
176int x86cpuid_GetFirm(const Cx86cpuid *p) 306int x86cpuid_GetFirm(const Cx86cpuid *p)
177{ 307{
178 unsigned i; 308 unsigned i;
179 for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++) 309 for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[0]); i++)
180 { 310 {
181 const UInt32 *v = kVendors[i]; 311 const UInt32 *v = kVendors[i];
182 if (v[0] == p->vendor[0] && 312 if (v[0] == p->vendor[0]
183 v[1] == p->vendor[1] && 313 // && v[1] == p->vendor[1]
184 v[2] == p->vendor[2]) 314 // && v[2] == p->vendor[2]
315 )
185 return (int)i; 316 return (int)i;
186 } 317 }
187 return -1; 318 return -1;
@@ -190,41 +321,55 @@ int x86cpuid_GetFirm(const Cx86cpuid *p)
190BoolInt CPU_Is_InOrder() 321BoolInt CPU_Is_InOrder()
191{ 322{
192 Cx86cpuid p; 323 Cx86cpuid p;
193 int firm;
194 UInt32 family, model; 324 UInt32 family, model;
195 if (!x86cpuid_CheckAndRead(&p)) 325 if (!x86cpuid_CheckAndRead(&p))
196 return True; 326 return True;
197 327
198 family = x86cpuid_GetFamily(p.ver); 328 family = x86cpuid_ver_GetFamily(p.ver);
199 model = x86cpuid_GetModel(p.ver); 329 model = x86cpuid_ver_GetModel(p.ver);
200
201 firm = x86cpuid_GetFirm(&p);
202 330
203 switch (firm) 331 switch (x86cpuid_GetFirm(&p))
204 { 332 {
205 case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && ( 333 case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && (
206 /* In-Order Atom CPU */ 334 // In-Order Atom CPU
207 model == 0x1C /* 45 nm, N4xx, D4xx, N5xx, D5xx, 230, 330 */ 335 model == 0x1C // 45 nm, N4xx, D4xx, N5xx, D5xx, 230, 330
208 || model == 0x26 /* 45 nm, Z6xx */ 336 || model == 0x26 // 45 nm, Z6xx
209 || model == 0x27 /* 32 nm, Z2460 */ 337 || model == 0x27 // 32 nm, Z2460
210 || model == 0x35 /* 32 nm, Z2760 */ 338 || model == 0x35 // 32 nm, Z2760
211 || model == 0x36 /* 32 nm, N2xxx, D2xxx */ 339 || model == 0x36 // 32 nm, N2xxx, D2xxx
212 ))); 340 )));
213 case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA))); 341 case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA)));
214 case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF)); 342 case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF));
215 } 343 }
216 return True; 344 return False; // v23 : unknown processors are not In-Order
217} 345}
346*/
347
348#ifdef _WIN32
349#include "7zWindows.h"
350#endif
218 351
219#if !defined(MY_CPU_AMD64) && defined(_WIN32) 352#if !defined(MY_CPU_AMD64) && defined(_WIN32)
220#include <Windows.h> 353
221static BoolInt CPU_Sys_Is_SSE_Supported() 354/* for legacy SSE ia32: there is no user-space cpu instruction to check
355 that OS supports SSE register storing/restoring on context switches.
356 So we need some OS-specific function to check that it's safe to use SSE registers.
357*/
358
359Z7_FORCE_INLINE
360static BoolInt CPU_Sys_Is_SSE_Supported(void)
222{ 361{
223 OSVERSIONINFO vi; 362#ifdef _MSC_VER
224 vi.dwOSVersionInfoSize = sizeof(vi); 363 #pragma warning(push)
225 if (!GetVersionEx(&vi)) 364 #pragma warning(disable : 4996) // `GetVersion': was declared deprecated
226 return False; 365#endif
227 return (vi.dwMajorVersion >= 5); 366 /* low byte is major version of Windows
367 We suppose that any Windows version since
368 Windows2000 (major == 5) supports SSE registers */
369 return (Byte)GetVersion() >= 5;
370#if defined(_MSC_VER)
371 #pragma warning(pop)
372#endif
228} 373}
229#define CHECK_SYS_SSE_SUPPORT if (!CPU_Sys_Is_SSE_Supported()) return False; 374#define CHECK_SYS_SSE_SUPPORT if (!CPU_Sys_Is_SSE_Supported()) return False;
230#else 375#else
@@ -232,94 +377,300 @@ static BoolInt CPU_Sys_Is_SSE_Supported()
232#endif 377#endif
233 378
234 379
235static UInt32 X86_CPUID_ECX_Get_Flags() 380#if !defined(MY_CPU_AMD64)
381
382BoolInt CPU_IsSupported_CMOV(void)
236{ 383{
237 Cx86cpuid p; 384 UInt32 a[4];
385 if (!x86cpuid_Func_1(&a[0]))
386 return 0;
387 return (a[3] >> 15) & 1;
388}
389
390BoolInt CPU_IsSupported_SSE(void)
391{
392 UInt32 a[4];
238 CHECK_SYS_SSE_SUPPORT 393 CHECK_SYS_SSE_SUPPORT
239 if (!x86cpuid_CheckAndRead(&p)) 394 if (!x86cpuid_Func_1(&a[0]))
395 return 0;
396 return (a[3] >> 25) & 1;
397}
398
399BoolInt CPU_IsSupported_SSE2(void)
400{
401 UInt32 a[4];
402 CHECK_SYS_SSE_SUPPORT
403 if (!x86cpuid_Func_1(&a[0]))
404 return 0;
405 return (a[3] >> 26) & 1;
406}
407
408#endif
409
410
411static UInt32 x86cpuid_Func_1_ECX(void)
412{
413 UInt32 a[4];
414 CHECK_SYS_SSE_SUPPORT
415 if (!x86cpuid_Func_1(&a[0]))
240 return 0; 416 return 0;
241 return p.c; 417 return a[2];
242} 418}
243 419
244BoolInt CPU_IsSupported_AES() 420BoolInt CPU_IsSupported_AES(void)
245{ 421{
246 return (X86_CPUID_ECX_Get_Flags() >> 25) & 1; 422 return (x86cpuid_Func_1_ECX() >> 25) & 1;
247} 423}
248 424
249BoolInt CPU_IsSupported_SSSE3() 425BoolInt CPU_IsSupported_SSSE3(void)
250{ 426{
251 return (X86_CPUID_ECX_Get_Flags() >> 9) & 1; 427 return (x86cpuid_Func_1_ECX() >> 9) & 1;
252} 428}
253 429
254BoolInt CPU_IsSupported_SSE41() 430BoolInt CPU_IsSupported_SSE41(void)
255{ 431{
256 return (X86_CPUID_ECX_Get_Flags() >> 19) & 1; 432 return (x86cpuid_Func_1_ECX() >> 19) & 1;
257} 433}
258 434
259BoolInt CPU_IsSupported_SHA() 435BoolInt CPU_IsSupported_SHA(void)
260{ 436{
261 Cx86cpuid p;
262 CHECK_SYS_SSE_SUPPORT 437 CHECK_SYS_SSE_SUPPORT
263 if (!x86cpuid_CheckAndRead(&p))
264 return False;
265 438
266 if (p.maxFunc < 7) 439 if (z7_x86_cpuid_GetMaxFunc() < 7)
267 return False; 440 return False;
268 { 441 {
269 UInt32 d[4] = { 0 }; 442 UInt32 d[4];
270 MyCPUID(7, &d[0], &d[1], &d[2], &d[3]); 443 z7_x86_cpuid(d, 7);
271 return (d[1] >> 29) & 1; 444 return (d[1] >> 29) & 1;
272 } 445 }
273} 446}
274 447
275// #include <stdio.h> 448/*
449MSVC: _xgetbv() intrinsic is available since VS2010SP1.
450 MSVC also defines (_XCR_XFEATURE_ENABLED_MASK) macro in
451 <immintrin.h> that we can use or check.
452 For any 32-bit x86 we can use asm code in MSVC,
453 but MSVC asm code is huge after compilation.
454 So _xgetbv() is better
455
456ICC: _xgetbv() intrinsic is available (in what version of ICC?)
457 ICC defines (__GNUC___) and it supports gnu assembler
458 also ICC supports MASM style code with -use-msasm switch.
459 but ICC doesn't support __attribute__((__target__))
460
461GCC/CLANG 9:
462 _xgetbv() is macro that works via __builtin_ia32_xgetbv()
463 and we need __attribute__((__target__("xsave")).
464 But with __target__("xsave") the function will be not
465 inlined to function that has no __target__("xsave") attribute.
466 If we want _xgetbv() call inlining, then we should use asm version
467 instead of calling _xgetbv().
468 Note:intrinsic is broke before GCC 8.2:
469 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85684
470*/
276 471
277#ifdef _WIN32 472#if defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1100) \
278#include <Windows.h> 473 || defined(_MSC_VER) && (_MSC_VER >= 1600) && (_MSC_FULL_VER >= 160040219) \
474 || defined(__GNUC__) && (__GNUC__ >= 9) \
475 || defined(__clang__) && (__clang_major__ >= 9)
476// we define ATTRIB_XGETBV, if we want to use predefined _xgetbv() from compiler
477#if defined(__INTEL_COMPILER)
478#define ATTRIB_XGETBV
479#elif defined(__GNUC__) || defined(__clang__)
480// we don't define ATTRIB_XGETBV here, because asm version is better for inlining.
481// #define ATTRIB_XGETBV __attribute__((__target__("xsave")))
482#else
483#define ATTRIB_XGETBV
484#endif
485#endif
486
487#if defined(ATTRIB_XGETBV)
488#include <immintrin.h>
279#endif 489#endif
280 490
281BoolInt CPU_IsSupported_AVX2() 491
492// XFEATURE_ENABLED_MASK/XCR0
493#define MY_XCR_XFEATURE_ENABLED_MASK 0
494
495#if defined(ATTRIB_XGETBV)
496ATTRIB_XGETBV
497#endif
498static UInt64 x86_xgetbv_0(UInt32 num)
282{ 499{
283 Cx86cpuid p; 500#if defined(ATTRIB_XGETBV)
284 CHECK_SYS_SSE_SUPPORT 501 {
502 return
503 #if (defined(_MSC_VER))
504 _xgetbv(num);
505 #else
506 __builtin_ia32_xgetbv(
507 #if !defined(__clang__)
508 (int)
509 #endif
510 num);
511 #endif
512 }
513
514#elif defined(__GNUC__) || defined(__clang__) || defined(__SUNPRO_CC)
515
516 UInt32 a, d;
517 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
518 __asm__
519 (
520 "xgetbv"
521 : "=a"(a), "=d"(d) : "c"(num) : "cc"
522 );
523 #else // is old gcc
524 __asm__
525 (
526 ".byte 0x0f, 0x01, 0xd0" "\n\t"
527 : "=a"(a), "=d"(d) : "c"(num) : "cc"
528 );
529 #endif
530 return ((UInt64)d << 32) | a;
531 // return a;
532
533#elif defined(_MSC_VER) && !defined(MY_CPU_AMD64)
534
535 UInt32 a, d;
536 __asm {
537 push eax
538 push edx
539 push ecx
540 mov ecx, num;
541 // xor ecx, ecx // = MY_XCR_XFEATURE_ENABLED_MASK
542 _emit 0x0f
543 _emit 0x01
544 _emit 0xd0
545 mov a, eax
546 mov d, edx
547 pop ecx
548 pop edx
549 pop eax
550 }
551 return ((UInt64)d << 32) | a;
552 // return a;
553
554#else // it's unknown compiler
555 // #error "Need xgetbv function"
556 UNUSED_VAR(num)
557 // for MSVC-X64 we could call external function from external file.
558 /* Actually we had checked OSXSAVE/AVX in cpuid before.
559 So it's expected that OS supports at least AVX and below. */
560 // if (num != MY_XCR_XFEATURE_ENABLED_MASK) return 0; // if not XCR0
561 return
562 // (1 << 0) | // x87
563 (1 << 1) // SSE
564 | (1 << 2); // AVX
565
566#endif
567}
285 568
569#ifdef _WIN32
570/*
571 Windows versions do not know about new ISA extensions that
572 can be introduced. But we still can use new extensions,
573 even if Windows doesn't report about supporting them,
574 But we can use new extensions, only if Windows knows about new ISA extension
575 that changes the number or size of registers: SSE, AVX/XSAVE, AVX512
576 So it's enough to check
577 MY_PF_AVX_INSTRUCTIONS_AVAILABLE
578 instead of
579 MY_PF_AVX2_INSTRUCTIONS_AVAILABLE
580*/
581#define MY_PF_XSAVE_ENABLED 17
582// #define MY_PF_SSSE3_INSTRUCTIONS_AVAILABLE 36
583// #define MY_PF_SSE4_1_INSTRUCTIONS_AVAILABLE 37
584// #define MY_PF_SSE4_2_INSTRUCTIONS_AVAILABLE 38
585// #define MY_PF_AVX_INSTRUCTIONS_AVAILABLE 39
586// #define MY_PF_AVX2_INSTRUCTIONS_AVAILABLE 40
587// #define MY_PF_AVX512F_INSTRUCTIONS_AVAILABLE 41
588#endif
589
590BoolInt CPU_IsSupported_AVX(void)
591{
286 #ifdef _WIN32 592 #ifdef _WIN32
287 #define MY__PF_XSAVE_ENABLED 17 593 if (!IsProcessorFeaturePresent(MY_PF_XSAVE_ENABLED))
288 if (!IsProcessorFeaturePresent(MY__PF_XSAVE_ENABLED))
289 return False; 594 return False;
595 /* PF_AVX_INSTRUCTIONS_AVAILABLE probably is supported starting from
596 some latest Win10 revisions. But we need AVX in older Windows also.
597 So we don't use the following check: */
598 /*
599 if (!IsProcessorFeaturePresent(MY_PF_AVX_INSTRUCTIONS_AVAILABLE))
600 return False;
601 */
290 #endif 602 #endif
291 603
292 if (!x86cpuid_CheckAndRead(&p)) 604 /*
605 OS must use new special XSAVE/XRSTOR instructions to save
606 AVX registers when it required for context switching.
607 At OS statring:
608 OS sets CR4.OSXSAVE flag to signal the processor that OS supports the XSAVE extensions.
609 Also OS sets bitmask in XCR0 register that defines what
610 registers will be processed by XSAVE instruction:
611 XCR0.SSE[bit 0] - x87 registers and state
612 XCR0.SSE[bit 1] - SSE registers and state
613 XCR0.AVX[bit 2] - AVX registers and state
614 CR4.OSXSAVE is reflected to CPUID.1:ECX.OSXSAVE[bit 27].
615 So we can read that bit in user-space.
616 XCR0 is available for reading in user-space by new XGETBV instruction.
617 */
618 {
619 const UInt32 c = x86cpuid_Func_1_ECX();
620 if (0 == (1
621 & (c >> 28) // AVX instructions are supported by hardware
622 & (c >> 27))) // OSXSAVE bit: XSAVE and related instructions are enabled by OS.
623 return False;
624 }
625
626 /* also we can check
627 CPUID.1:ECX.XSAVE [bit 26] : that shows that
628 XSAVE, XRESTOR, XSETBV, XGETBV instructions are supported by hardware.
629 But that check is redundant, because if OSXSAVE bit is set, then XSAVE is also set */
630
631 /* If OS have enabled XSAVE extension instructions (OSXSAVE == 1),
632 in most cases we expect that OS also will support storing/restoring
633 for AVX and SSE states at least.
634 But to be ensure for that we call user-space instruction
635 XGETBV(0) to get XCR0 value that contains bitmask that defines
636 what exact states(registers) OS have enabled for storing/restoring.
637 */
638
639 {
640 const UInt32 bm = (UInt32)x86_xgetbv_0(MY_XCR_XFEATURE_ENABLED_MASK);
641 // printf("\n=== XGetBV=%d\n", bm);
642 return 1
643 & (bm >> 1) // SSE state is supported (set by OS) for storing/restoring
644 & (bm >> 2); // AVX state is supported (set by OS) for storing/restoring
645 }
646 // since Win7SP1: we can use GetEnabledXStateFeatures();
647}
648
649
650BoolInt CPU_IsSupported_AVX2(void)
651{
652 if (!CPU_IsSupported_AVX())
293 return False; 653 return False;
294 if (p.maxFunc < 7) 654 if (z7_x86_cpuid_GetMaxFunc() < 7)
295 return False; 655 return False;
296 { 656 {
297 UInt32 d[4] = { 0 }; 657 UInt32 d[4];
298 MyCPUID(7, &d[0], &d[1], &d[2], &d[3]); 658 z7_x86_cpuid(d, 7);
299 // printf("\ncpuid(7): ebx=%8x ecx=%8x\n", d[1], d[2]); 659 // printf("\ncpuid(7): ebx=%8x ecx=%8x\n", d[1], d[2]);
300 return 1 660 return 1
301 & (d[1] >> 5); // avx2 661 & (d[1] >> 5); // avx2
302 } 662 }
303} 663}
304 664
305BoolInt CPU_IsSupported_VAES_AVX2() 665BoolInt CPU_IsSupported_VAES_AVX2(void)
306{ 666{
307 Cx86cpuid p; 667 if (!CPU_IsSupported_AVX())
308 CHECK_SYS_SSE_SUPPORT
309
310 #ifdef _WIN32
311 #define MY__PF_XSAVE_ENABLED 17
312 if (!IsProcessorFeaturePresent(MY__PF_XSAVE_ENABLED))
313 return False; 668 return False;
314 #endif 669 if (z7_x86_cpuid_GetMaxFunc() < 7)
315
316 if (!x86cpuid_CheckAndRead(&p))
317 return False;
318 if (p.maxFunc < 7)
319 return False; 670 return False;
320 { 671 {
321 UInt32 d[4] = { 0 }; 672 UInt32 d[4];
322 MyCPUID(7, &d[0], &d[1], &d[2], &d[3]); 673 z7_x86_cpuid(d, 7);
323 // printf("\ncpuid(7): ebx=%8x ecx=%8x\n", d[1], d[2]); 674 // printf("\ncpuid(7): ebx=%8x ecx=%8x\n", d[1], d[2]);
324 return 1 675 return 1
325 & (d[1] >> 5) // avx2 676 & (d[1] >> 5) // avx2
@@ -328,20 +679,15 @@ BoolInt CPU_IsSupported_VAES_AVX2()
328 } 679 }
329} 680}
330 681
331BoolInt CPU_IsSupported_PageGB() 682BoolInt CPU_IsSupported_PageGB(void)
332{ 683{
333 Cx86cpuid cpuid; 684 CHECK_CPUID_IS_SUPPORTED
334 if (!x86cpuid_CheckAndRead(&cpuid))
335 return False;
336 { 685 {
337 UInt32 d[4] = { 0 }; 686 UInt32 d[4];
338 MyCPUID(0x80000000, &d[0], &d[1], &d[2], &d[3]); 687 z7_x86_cpuid(d, 0x80000000);
339 if (d[0] < 0x80000001) 688 if (d[0] < 0x80000001)
340 return False; 689 return False;
341 } 690 z7_x86_cpuid(d, 0x80000001);
342 {
343 UInt32 d[4] = { 0 };
344 MyCPUID(0x80000001, &d[0], &d[1], &d[2], &d[3]);
345 return (d[3] >> 26) & 1; 691 return (d[3] >> 26) & 1;
346 } 692 }
347} 693}
@@ -351,11 +697,11 @@ BoolInt CPU_IsSupported_PageGB()
351 697
352#ifdef _WIN32 698#ifdef _WIN32
353 699
354#include <Windows.h> 700#include "7zWindows.h"
355 701
356BoolInt CPU_IsSupported_CRC32() { return IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) ? 1 : 0; } 702BoolInt CPU_IsSupported_CRC32(void) { return IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) ? 1 : 0; }
357BoolInt CPU_IsSupported_CRYPTO() { return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) ? 1 : 0; } 703BoolInt CPU_IsSupported_CRYPTO(void) { return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) ? 1 : 0; }
358BoolInt CPU_IsSupported_NEON() { return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) ? 1 : 0; } 704BoolInt CPU_IsSupported_NEON(void) { return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) ? 1 : 0; }
359 705
360#else 706#else
361 707
@@ -378,28 +724,27 @@ static void Print_sysctlbyname(const char *name)
378 } 724 }
379} 725}
380*/ 726*/
727/*
728 Print_sysctlbyname("hw.pagesize");
729 Print_sysctlbyname("machdep.cpu.brand_string");
730*/
381 731
382static BoolInt My_sysctlbyname_Get_BoolInt(const char *name) 732static BoolInt z7_sysctlbyname_Get_BoolInt(const char *name)
383{ 733{
384 UInt32 val = 0; 734 UInt32 val = 0;
385 if (My_sysctlbyname_Get_UInt32(name, &val) == 0 && val == 1) 735 if (z7_sysctlbyname_Get_UInt32(name, &val) == 0 && val == 1)
386 return 1; 736 return 1;
387 return 0; 737 return 0;
388} 738}
389 739
390 /*
391 Print_sysctlbyname("hw.pagesize");
392 Print_sysctlbyname("machdep.cpu.brand_string");
393 */
394
395BoolInt CPU_IsSupported_CRC32(void) 740BoolInt CPU_IsSupported_CRC32(void)
396{ 741{
397 return My_sysctlbyname_Get_BoolInt("hw.optional.armv8_crc32"); 742 return z7_sysctlbyname_Get_BoolInt("hw.optional.armv8_crc32");
398} 743}
399 744
400BoolInt CPU_IsSupported_NEON(void) 745BoolInt CPU_IsSupported_NEON(void)
401{ 746{
402 return My_sysctlbyname_Get_BoolInt("hw.optional.neon"); 747 return z7_sysctlbyname_Get_BoolInt("hw.optional.neon");
403} 748}
404 749
405#ifdef MY_CPU_ARM64 750#ifdef MY_CPU_ARM64
@@ -461,15 +806,15 @@ MY_HWCAP_CHECK_FUNC (AES)
461 806
462#include <sys/sysctl.h> 807#include <sys/sysctl.h>
463 808
464int My_sysctlbyname_Get(const char *name, void *buf, size_t *bufSize) 809int z7_sysctlbyname_Get(const char *name, void *buf, size_t *bufSize)
465{ 810{
466 return sysctlbyname(name, buf, bufSize, NULL, 0); 811 return sysctlbyname(name, buf, bufSize, NULL, 0);
467} 812}
468 813
469int My_sysctlbyname_Get_UInt32(const char *name, UInt32 *val) 814int z7_sysctlbyname_Get_UInt32(const char *name, UInt32 *val)
470{ 815{
471 size_t bufSize = sizeof(*val); 816 size_t bufSize = sizeof(*val);
472 int res = My_sysctlbyname_Get(name, val, &bufSize); 817 const int res = z7_sysctlbyname_Get(name, val, &bufSize);
473 if (res == 0 && bufSize != sizeof(*val)) 818 if (res == 0 && bufSize != sizeof(*val))
474 return EFAULT; 819 return EFAULT;
475 return res; 820 return res;
diff --git a/C/CpuArch.h b/C/CpuArch.h
index 4856fbb..8e5d8a5 100644
--- a/C/CpuArch.h
+++ b/C/CpuArch.h
@@ -1,8 +1,8 @@
1/* CpuArch.h -- CPU specific code 1/* CpuArch.h -- CPU specific code
22022-07-15 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __CPU_ARCH_H 4#ifndef ZIP7_INC_CPU_ARCH_H
5#define __CPU_ARCH_H 5#define ZIP7_INC_CPU_ARCH_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -51,7 +51,13 @@ MY_CPU_64BIT means that processor can work with 64-bit registers.
51 || defined(__AARCH64EB__) \ 51 || defined(__AARCH64EB__) \
52 || defined(__aarch64__) 52 || defined(__aarch64__)
53 #define MY_CPU_ARM64 53 #define MY_CPU_ARM64
54 #define MY_CPU_NAME "arm64" 54 #ifdef __ILP32__
55 #define MY_CPU_NAME "arm64-32"
56 #define MY_CPU_SIZEOF_POINTER 4
57 #else
58 #define MY_CPU_NAME "arm64"
59 #define MY_CPU_SIZEOF_POINTER 8
60 #endif
55 #define MY_CPU_64BIT 61 #define MY_CPU_64BIT
56#endif 62#endif
57 63
@@ -68,8 +74,10 @@ MY_CPU_64BIT means that processor can work with 64-bit registers.
68 #define MY_CPU_ARM 74 #define MY_CPU_ARM
69 75
70 #if defined(__thumb__) || defined(__THUMBEL__) || defined(_M_ARMT) 76 #if defined(__thumb__) || defined(__THUMBEL__) || defined(_M_ARMT)
77 #define MY_CPU_ARMT
71 #define MY_CPU_NAME "armt" 78 #define MY_CPU_NAME "armt"
72 #else 79 #else
80 #define MY_CPU_ARM32
73 #define MY_CPU_NAME "arm" 81 #define MY_CPU_NAME "arm"
74 #endif 82 #endif
75 /* #define MY_CPU_32BIT */ 83 /* #define MY_CPU_32BIT */
@@ -103,6 +111,8 @@ MY_CPU_64BIT means that processor can work with 64-bit registers.
103 || defined(__PPC__) \ 111 || defined(__PPC__) \
104 || defined(_POWER) 112 || defined(_POWER)
105 113
114#define MY_CPU_PPC_OR_PPC64
115
106#if defined(__ppc64__) \ 116#if defined(__ppc64__) \
107 || defined(__powerpc64__) \ 117 || defined(__powerpc64__) \
108 || defined(_LP64) \ 118 || defined(_LP64) \
@@ -197,6 +207,9 @@ MY_CPU_64BIT means that processor can work with 64-bit registers.
197 #error Stop_Compiling_Bad_Endian 207 #error Stop_Compiling_Bad_Endian
198#endif 208#endif
199 209
210#if !defined(MY_CPU_LE) && !defined(MY_CPU_BE)
211 #error Stop_Compiling_CPU_ENDIAN_must_be_detected_at_compile_time
212#endif
200 213
201#if defined(MY_CPU_32BIT) && defined(MY_CPU_64BIT) 214#if defined(MY_CPU_32BIT) && defined(MY_CPU_64BIT)
202 #error Stop_Compiling_Bad_32_64_BIT 215 #error Stop_Compiling_Bad_32_64_BIT
@@ -253,6 +266,67 @@ MY_CPU_64BIT means that processor can work with 64-bit registers.
253 266
254 267
255 268
269#ifdef __has_builtin
270 #define Z7_has_builtin(x) __has_builtin(x)
271#else
272 #define Z7_has_builtin(x) 0
273#endif
274
275
276#define Z7_BSWAP32_CONST(v) \
277 ( (((UInt32)(v) << 24) ) \
278 | (((UInt32)(v) << 8) & (UInt32)0xff0000) \
279 | (((UInt32)(v) >> 8) & (UInt32)0xff00 ) \
280 | (((UInt32)(v) >> 24) ))
281
282
283#if defined(_MSC_VER) && (_MSC_VER >= 1300)
284
285#include <stdlib.h>
286
287/* Note: these macros will use bswap instruction (486), that is unsupported in 386 cpu */
288
289#pragma intrinsic(_byteswap_ushort)
290#pragma intrinsic(_byteswap_ulong)
291#pragma intrinsic(_byteswap_uint64)
292
293#define Z7_BSWAP16(v) _byteswap_ushort(v)
294#define Z7_BSWAP32(v) _byteswap_ulong (v)
295#define Z7_BSWAP64(v) _byteswap_uint64(v)
296#define Z7_CPU_FAST_BSWAP_SUPPORTED
297
298#elif (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
299 || (defined(__clang__) && Z7_has_builtin(__builtin_bswap16))
300
301#define Z7_BSWAP16(v) __builtin_bswap16(v)
302#define Z7_BSWAP32(v) __builtin_bswap32(v)
303#define Z7_BSWAP64(v) __builtin_bswap64(v)
304#define Z7_CPU_FAST_BSWAP_SUPPORTED
305
306#else
307
308#define Z7_BSWAP16(v) ((UInt16) \
309 ( ((UInt32)(v) << 8) \
310 | ((UInt32)(v) >> 8) \
311 ))
312
313#define Z7_BSWAP32(v) Z7_BSWAP32_CONST(v)
314
315#define Z7_BSWAP64(v) \
316 ( ( ( (UInt64)(v) ) << 8 * 7 ) \
317 | ( ( (UInt64)(v) & ((UInt32)0xff << 8 * 1) ) << 8 * 5 ) \
318 | ( ( (UInt64)(v) & ((UInt32)0xff << 8 * 2) ) << 8 * 3 ) \
319 | ( ( (UInt64)(v) & ((UInt32)0xff << 8 * 3) ) << 8 * 1 ) \
320 | ( ( (UInt64)(v) >> 8 * 1 ) & ((UInt32)0xff << 8 * 3) ) \
321 | ( ( (UInt64)(v) >> 8 * 3 ) & ((UInt32)0xff << 8 * 2) ) \
322 | ( ( (UInt64)(v) >> 8 * 5 ) & ((UInt32)0xff << 8 * 1) ) \
323 | ( ( (UInt64)(v) >> 8 * 7 ) ) \
324 )
325
326#endif
327
328
329
256#ifdef MY_CPU_LE 330#ifdef MY_CPU_LE
257 #if defined(MY_CPU_X86_OR_AMD64) \ 331 #if defined(MY_CPU_X86_OR_AMD64) \
258 || defined(MY_CPU_ARM64) 332 || defined(MY_CPU_ARM64)
@@ -272,13 +346,11 @@ MY_CPU_64BIT means that processor can work with 64-bit registers.
272#define GetUi32(p) (*(const UInt32 *)(const void *)(p)) 346#define GetUi32(p) (*(const UInt32 *)(const void *)(p))
273#ifdef MY_CPU_LE_UNALIGN_64 347#ifdef MY_CPU_LE_UNALIGN_64
274#define GetUi64(p) (*(const UInt64 *)(const void *)(p)) 348#define GetUi64(p) (*(const UInt64 *)(const void *)(p))
349#define SetUi64(p, v) { *(UInt64 *)(void *)(p) = (v); }
275#endif 350#endif
276 351
277#define SetUi16(p, v) { *(UInt16 *)(void *)(p) = (v); } 352#define SetUi16(p, v) { *(UInt16 *)(void *)(p) = (v); }
278#define SetUi32(p, v) { *(UInt32 *)(void *)(p) = (v); } 353#define SetUi32(p, v) { *(UInt32 *)(void *)(p) = (v); }
279#ifdef MY_CPU_LE_UNALIGN_64
280#define SetUi64(p, v) { *(UInt64 *)(void *)(p) = (v); }
281#endif
282 354
283#else 355#else
284 356
@@ -305,51 +377,26 @@ MY_CPU_64BIT means that processor can work with 64-bit registers.
305#endif 377#endif
306 378
307 379
308#ifndef MY_CPU_LE_UNALIGN_64 380#ifndef GetUi64
309
310#define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32)) 381#define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32))
382#endif
311 383
384#ifndef SetUi64
312#define SetUi64(p, v) { Byte *_ppp2_ = (Byte *)(p); UInt64 _vvv2_ = (v); \ 385#define SetUi64(p, v) { Byte *_ppp2_ = (Byte *)(p); UInt64 _vvv2_ = (v); \
313 SetUi32(_ppp2_ , (UInt32)_vvv2_); \ 386 SetUi32(_ppp2_ , (UInt32)_vvv2_) \
314 SetUi32(_ppp2_ + 4, (UInt32)(_vvv2_ >> 32)); } 387 SetUi32(_ppp2_ + 4, (UInt32)(_vvv2_ >> 32)) }
315
316#endif 388#endif
317 389
318 390
391#if defined(MY_CPU_LE_UNALIGN) && defined(Z7_CPU_FAST_BSWAP_SUPPORTED)
319 392
393#define GetBe32(p) Z7_BSWAP32 (*(const UInt32 *)(const void *)(p))
394#define SetBe32(p, v) { (*(UInt32 *)(void *)(p)) = Z7_BSWAP32(v); }
320 395
321#ifdef __has_builtin 396#if defined(MY_CPU_LE_UNALIGN_64)
322 #define MY__has_builtin(x) __has_builtin(x) 397#define GetBe64(p) Z7_BSWAP64 (*(const UInt64 *)(const void *)(p))
323#else
324 #define MY__has_builtin(x) 0
325#endif 398#endif
326 399
327#if defined(MY_CPU_LE_UNALIGN) && /* defined(_WIN64) && */ defined(_MSC_VER) && (_MSC_VER >= 1300)
328
329/* Note: we use bswap instruction, that is unsupported in 386 cpu */
330
331#include <stdlib.h>
332
333#pragma intrinsic(_byteswap_ushort)
334#pragma intrinsic(_byteswap_ulong)
335#pragma intrinsic(_byteswap_uint64)
336
337/* #define GetBe16(p) _byteswap_ushort(*(const UInt16 *)(const Byte *)(p)) */
338#define GetBe32(p) _byteswap_ulong (*(const UInt32 *)(const void *)(p))
339#define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const void *)(p))
340
341#define SetBe32(p, v) (*(UInt32 *)(void *)(p)) = _byteswap_ulong(v)
342
343#elif defined(MY_CPU_LE_UNALIGN) && ( \
344 (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
345 || (defined(__clang__) && MY__has_builtin(__builtin_bswap16)) )
346
347/* #define GetBe16(p) __builtin_bswap16(*(const UInt16 *)(const void *)(p)) */
348#define GetBe32(p) __builtin_bswap32(*(const UInt32 *)(const void *)(p))
349#define GetBe64(p) __builtin_bswap64(*(const UInt64 *)(const void *)(p))
350
351#define SetBe32(p, v) (*(UInt32 *)(void *)(p)) = __builtin_bswap32(v)
352
353#else 400#else
354 401
355#define GetBe32(p) ( \ 402#define GetBe32(p) ( \
@@ -358,8 +405,6 @@ MY_CPU_64BIT means that processor can work with 64-bit registers.
358 ((UInt32)((const Byte *)(p))[2] << 8) | \ 405 ((UInt32)((const Byte *)(p))[2] << 8) | \
359 ((const Byte *)(p))[3] ) 406 ((const Byte *)(p))[3] )
360 407
361#define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4))
362
363#define SetBe32(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \ 408#define SetBe32(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \
364 _ppp_[0] = (Byte)(_vvv_ >> 24); \ 409 _ppp_[0] = (Byte)(_vvv_ >> 24); \
365 _ppp_[1] = (Byte)(_vvv_ >> 16); \ 410 _ppp_[1] = (Byte)(_vvv_ >> 16); \
@@ -368,50 +413,83 @@ MY_CPU_64BIT means that processor can work with 64-bit registers.
368 413
369#endif 414#endif
370 415
416#ifndef GetBe64
417#define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4))
418#endif
371 419
372#ifndef GetBe16 420#ifndef GetBe16
373
374#define GetBe16(p) ( (UInt16) ( \ 421#define GetBe16(p) ( (UInt16) ( \
375 ((UInt16)((const Byte *)(p))[0] << 8) | \ 422 ((UInt16)((const Byte *)(p))[0] << 8) | \
376 ((const Byte *)(p))[1] )) 423 ((const Byte *)(p))[1] ))
424#endif
425
377 426
427#if defined(MY_CPU_BE)
428#define Z7_CONV_BE_TO_NATIVE_CONST32(v) (v)
429#define Z7_CONV_LE_TO_NATIVE_CONST32(v) Z7_BSWAP32_CONST(v)
430#define Z7_CONV_NATIVE_TO_BE_32(v) (v)
431#elif defined(MY_CPU_LE)
432#define Z7_CONV_BE_TO_NATIVE_CONST32(v) Z7_BSWAP32_CONST(v)
433#define Z7_CONV_LE_TO_NATIVE_CONST32(v) (v)
434#define Z7_CONV_NATIVE_TO_BE_32(v) Z7_BSWAP32(v)
435#else
436#error Stop_Compiling_Unknown_Endian_CONV
378#endif 437#endif
379 438
380 439
440#if defined(MY_CPU_BE)
381 441
382#ifdef MY_CPU_X86_OR_AMD64 442#define GetBe32a(p) (*(const UInt32 *)(const void *)(p))
443#define GetBe16a(p) (*(const UInt16 *)(const void *)(p))
444#define SetBe32a(p, v) { *(UInt32 *)(void *)(p) = (v); }
445#define SetBe16a(p, v) { *(UInt16 *)(void *)(p) = (v); }
446
447#define GetUi32a(p) GetUi32(p)
448#define GetUi16a(p) GetUi16(p)
449#define SetUi32a(p, v) SetUi32(p, v)
450#define SetUi16a(p, v) SetUi16(p, v)
451
452#elif defined(MY_CPU_LE)
383 453
384typedef struct 454#define GetUi32a(p) (*(const UInt32 *)(const void *)(p))
385{ 455#define GetUi16a(p) (*(const UInt16 *)(const void *)(p))
386 UInt32 maxFunc; 456#define SetUi32a(p, v) { *(UInt32 *)(void *)(p) = (v); }
387 UInt32 vendor[3]; 457#define SetUi16a(p, v) { *(UInt16 *)(void *)(p) = (v); }
388 UInt32 ver;
389 UInt32 b;
390 UInt32 c;
391 UInt32 d;
392} Cx86cpuid;
393 458
394enum 459#define GetBe32a(p) GetBe32(p)
395{ 460#define GetBe16a(p) GetBe16(p)
396 CPU_FIRM_INTEL, 461#define SetBe32a(p, v) SetBe32(p, v)
397 CPU_FIRM_AMD, 462#define SetBe16a(p, v) SetBe16(p, v)
398 CPU_FIRM_VIA 463
399}; 464#else
465#error Stop_Compiling_Unknown_Endian_CPU_a
466#endif
400 467
401void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d);
402 468
403BoolInt x86cpuid_CheckAndRead(Cx86cpuid *p); 469#if defined(MY_CPU_X86_OR_AMD64) \
404int x86cpuid_GetFirm(const Cx86cpuid *p); 470 || defined(MY_CPU_ARM_OR_ARM64) \
471 || defined(MY_CPU_PPC_OR_PPC64)
472 #define Z7_CPU_FAST_ROTATE_SUPPORTED
473#endif
405 474
406#define x86cpuid_GetFamily(ver) (((ver >> 16) & 0xFF0) | ((ver >> 8) & 0xF))
407#define x86cpuid_GetModel(ver) (((ver >> 12) & 0xF0) | ((ver >> 4) & 0xF))
408#define x86cpuid_GetStepping(ver) (ver & 0xF)
409 475
410BoolInt CPU_Is_InOrder(void); 476#ifdef MY_CPU_X86_OR_AMD64
477
478void Z7_FASTCALL z7_x86_cpuid(UInt32 a[4], UInt32 function);
479UInt32 Z7_FASTCALL z7_x86_cpuid_GetMaxFunc(void);
480#if defined(MY_CPU_AMD64)
481#define Z7_IF_X86_CPUID_SUPPORTED
482#else
483#define Z7_IF_X86_CPUID_SUPPORTED if (z7_x86_cpuid_GetMaxFunc())
484#endif
411 485
412BoolInt CPU_IsSupported_AES(void); 486BoolInt CPU_IsSupported_AES(void);
487BoolInt CPU_IsSupported_AVX(void);
413BoolInt CPU_IsSupported_AVX2(void); 488BoolInt CPU_IsSupported_AVX2(void);
414BoolInt CPU_IsSupported_VAES_AVX2(void); 489BoolInt CPU_IsSupported_VAES_AVX2(void);
490BoolInt CPU_IsSupported_CMOV(void);
491BoolInt CPU_IsSupported_SSE(void);
492BoolInt CPU_IsSupported_SSE2(void);
415BoolInt CPU_IsSupported_SSSE3(void); 493BoolInt CPU_IsSupported_SSSE3(void);
416BoolInt CPU_IsSupported_SSE41(void); 494BoolInt CPU_IsSupported_SSE41(void);
417BoolInt CPU_IsSupported_SHA(void); 495BoolInt CPU_IsSupported_SHA(void);
@@ -436,8 +514,8 @@ BoolInt CPU_IsSupported_AES(void);
436#endif 514#endif
437 515
438#if defined(__APPLE__) 516#if defined(__APPLE__)
439int My_sysctlbyname_Get(const char *name, void *buf, size_t *bufSize); 517int z7_sysctlbyname_Get(const char *name, void *buf, size_t *bufSize);
440int My_sysctlbyname_Get_UInt32(const char *name, UInt32 *val); 518int z7_sysctlbyname_Get_UInt32(const char *name, UInt32 *val);
441#endif 519#endif
442 520
443EXTERN_C_END 521EXTERN_C_END
diff --git a/C/Delta.h b/C/Delta.h
index 2fa54ad..7060954 100644
--- a/C/Delta.h
+++ b/C/Delta.h
@@ -1,8 +1,8 @@
1/* Delta.h -- Delta converter 1/* Delta.h -- Delta converter
22013-01-18 : Igor Pavlov : Public domain */ 22023-03-03 : Igor Pavlov : Public domain */
3 3
4#ifndef __DELTA_H 4#ifndef ZIP7_INC_DELTA_H
5#define __DELTA_H 5#define ZIP7_INC_DELTA_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
diff --git a/C/DllSecur.c b/C/DllSecur.c
index dce0c96..02a0f97 100644
--- a/C/DllSecur.c
+++ b/C/DllSecur.c
@@ -1,18 +1,28 @@
1/* DllSecur.c -- DLL loading security 1/* DllSecur.c -- DLL loading security
22022-07-15 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#ifdef _WIN32 6#ifdef _WIN32
7 7
8#include <Windows.h> 8#include "7zWindows.h"
9 9
10#include "DllSecur.h" 10#include "DllSecur.h"
11 11
12#ifndef UNDER_CE 12#ifndef UNDER_CE
13 13
14#if defined(__GNUC__) && (__GNUC__ >= 8) 14#if (defined(__GNUC__) && (__GNUC__ >= 8)) || defined(__clang__)
15 #pragma GCC diagnostic ignored "-Wcast-function-type" 15 // #pragma GCC diagnostic ignored "-Wcast-function-type"
16#endif
17
18#if defined(__clang__) || defined(__GNUC__)
19typedef void (*Z7_voidFunction)(void);
20#define MY_CAST_FUNC (Z7_voidFunction)
21#elif defined(_MSC_VER) && _MSC_VER > 1920
22#define MY_CAST_FUNC (void *)
23// #pragma warning(disable : 4191) // 'type cast': unsafe conversion from 'FARPROC' to 'void (__cdecl *)()'
24#else
25#define MY_CAST_FUNC
16#endif 26#endif
17 27
18typedef BOOL (WINAPI *Func_SetDefaultDllDirectories)(DWORD DirectoryFlags); 28typedef BOOL (WINAPI *Func_SetDefaultDllDirectories)(DWORD DirectoryFlags);
@@ -20,95 +30,82 @@ typedef BOOL (WINAPI *Func_SetDefaultDllDirectories)(DWORD DirectoryFlags);
20#define MY_LOAD_LIBRARY_SEARCH_USER_DIRS 0x400 30#define MY_LOAD_LIBRARY_SEARCH_USER_DIRS 0x400
21#define MY_LOAD_LIBRARY_SEARCH_SYSTEM32 0x800 31#define MY_LOAD_LIBRARY_SEARCH_SYSTEM32 0x800
22 32
33#define DELIM "\0"
34
23static const char * const g_Dlls = 35static const char * const g_Dlls =
36 "userenv"
37 DELIM "setupapi"
38 DELIM "apphelp"
39 DELIM "propsys"
40 DELIM "dwmapi"
41 DELIM "cryptbase"
42 DELIM "oleacc"
43 DELIM "clbcatq"
44 DELIM "version"
24 #ifndef _CONSOLE 45 #ifndef _CONSOLE
25 "UXTHEME\0" 46 DELIM "uxtheme"
26 #endif 47 #endif
27 "USERENV\0" 48 DELIM;
28 "SETUPAPI\0"
29 "APPHELP\0"
30 "PROPSYS\0"
31 "DWMAPI\0"
32 "CRYPTBASE\0"
33 "OLEACC\0"
34 "CLBCATQ\0"
35 "VERSION\0"
36 ;
37 49
38#endif 50#endif
39 51
40// #define MY_CAST_FUNC (void(*)()) 52#ifdef __clang__
41#define MY_CAST_FUNC 53 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
54#endif
55#if defined (_MSC_VER) && _MSC_VER >= 1900
56// sysinfoapi.h: kit10: GetVersion was declared deprecated
57#pragma warning(disable : 4996)
58#endif
59
60#define IF_NON_VISTA_SET_DLL_DIRS_AND_RETURN \
61 if ((UInt16)GetVersion() != 6) { \
62 const \
63 Func_SetDefaultDllDirectories setDllDirs = \
64 (Func_SetDefaultDllDirectories) MY_CAST_FUNC GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), \
65 "SetDefaultDllDirectories"); \
66 if (setDllDirs) if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS)) return; }
42 67
43void My_SetDefaultDllDirectories() 68void My_SetDefaultDllDirectories(void)
44{ 69{
45 #ifndef UNDER_CE 70 #ifndef UNDER_CE
46 71 IF_NON_VISTA_SET_DLL_DIRS_AND_RETURN
47 OSVERSIONINFO vi;
48 vi.dwOSVersionInfoSize = sizeof(vi);
49 if (!GetVersionEx(&vi) || vi.dwMajorVersion != 6 || vi.dwMinorVersion != 0)
50 {
51 Func_SetDefaultDllDirectories setDllDirs = (Func_SetDefaultDllDirectories)
52 MY_CAST_FUNC GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "SetDefaultDllDirectories");
53 if (setDllDirs)
54 if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS))
55 return;
56 }
57
58 #endif 72 #endif
59} 73}
60 74
61 75
62void LoadSecurityDlls() 76void LoadSecurityDlls(void)
63{ 77{
64 #ifndef UNDER_CE 78 #ifndef UNDER_CE
65 79 // at Vista (ver 6.0) : CoCreateInstance(CLSID_ShellLink, ...) doesn't work after SetDefaultDllDirectories() : Check it ???
66 wchar_t buf[MAX_PATH + 100]; 80 IF_NON_VISTA_SET_DLL_DIRS_AND_RETURN
67
68 {
69 // at Vista (ver 6.0) : CoCreateInstance(CLSID_ShellLink, ...) doesn't work after SetDefaultDllDirectories() : Check it ???
70 OSVERSIONINFO vi;
71 vi.dwOSVersionInfoSize = sizeof(vi);
72 if (!GetVersionEx(&vi) || vi.dwMajorVersion != 6 || vi.dwMinorVersion != 0)
73 {
74 Func_SetDefaultDllDirectories setDllDirs = (Func_SetDefaultDllDirectories)
75 MY_CAST_FUNC GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "SetDefaultDllDirectories");
76 if (setDllDirs)
77 if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS))
78 return;
79 }
80 }
81
82 {
83 unsigned len = GetSystemDirectoryW(buf, MAX_PATH + 2);
84 if (len == 0 || len > MAX_PATH)
85 return;
86 }
87 { 81 {
82 wchar_t buf[MAX_PATH + 100];
88 const char *dll; 83 const char *dll;
89 unsigned pos = (unsigned)lstrlenW(buf); 84 unsigned pos = GetSystemDirectoryW(buf, MAX_PATH + 2);
90 85 if (pos == 0 || pos > MAX_PATH)
86 return;
91 if (buf[pos - 1] != '\\') 87 if (buf[pos - 1] != '\\')
92 buf[pos++] = '\\'; 88 buf[pos++] = '\\';
93 89 for (dll = g_Dlls; *dll != 0;)
94 for (dll = g_Dlls; dll[0] != 0;)
95 { 90 {
96 unsigned k = 0; 91 wchar_t *dest = &buf[pos];
97 for (;;) 92 for (;;)
98 { 93 {
99 char c = *dll++; 94 const char c = *dll++;
100 buf[pos + k] = (Byte)c;
101 k++;
102 if (c == 0) 95 if (c == 0)
103 break; 96 break;
97 *dest++ = (Byte)c;
104 } 98 }
105 99 dest[0] = '.';
106 lstrcatW(buf, L".dll"); 100 dest[1] = 'd';
101 dest[2] = 'l';
102 dest[3] = 'l';
103 dest[4] = 0;
104 // lstrcatW(buf, L".dll");
107 LoadLibraryExW(buf, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); 105 LoadLibraryExW(buf, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
108 } 106 }
109 } 107 }
110
111 #endif 108 #endif
112} 109}
113 110
114#endif 111#endif // _WIN32
diff --git a/C/DllSecur.h b/C/DllSecur.h
index 64ff26c..9fa4153 100644
--- a/C/DllSecur.h
+++ b/C/DllSecur.h
@@ -1,8 +1,8 @@
1/* DllSecur.h -- DLL loading for security 1/* DllSecur.h -- DLL loading for security
22018-02-19 : Igor Pavlov : Public domain */ 22023-03-03 : Igor Pavlov : Public domain */
3 3
4#ifndef __DLL_SECUR_H 4#ifndef ZIP7_INC_DLL_SECUR_H
5#define __DLL_SECUR_H 5#define ZIP7_INC_DLL_SECUR_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
diff --git a/C/HuffEnc.c b/C/HuffEnc.c
index f3c2996..3dc1e39 100644
--- a/C/HuffEnc.c
+++ b/C/HuffEnc.c
@@ -1,5 +1,5 @@
1/* HuffEnc.c -- functions for Huffman encoding 1/* HuffEnc.c -- functions for Huffman encoding
22021-02-09 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -106,7 +106,7 @@ void Huffman_Generate(const UInt32 *freqs, UInt32 *p, Byte *lens, UInt32 numSymb
106 106
107 p[--e] &= MASK; 107 p[--e] &= MASK;
108 lenCounters[1] = 2; 108 lenCounters[1] = 2;
109 while (e > 0) 109 while (e != 0)
110 { 110 {
111 UInt32 len = (p[p[--e] >> NUM_BITS] >> NUM_BITS) + 1; 111 UInt32 len = (p[p[--e] >> NUM_BITS] >> NUM_BITS) + 1;
112 p[e] = (p[e] & MASK) | (len << NUM_BITS); 112 p[e] = (p[e] & MASK) | (len << NUM_BITS);
@@ -146,3 +146,9 @@ void Huffman_Generate(const UInt32 *freqs, UInt32 *p, Byte *lens, UInt32 numSymb
146 } 146 }
147 } 147 }
148} 148}
149
150#undef kMaxLen
151#undef NUM_BITS
152#undef MASK
153#undef NUM_COUNTERS
154#undef HUFFMAN_SPEED_OPT
diff --git a/C/HuffEnc.h b/C/HuffEnc.h
index 92b6878..cbc5d11 100644
--- a/C/HuffEnc.h
+++ b/C/HuffEnc.h
@@ -1,8 +1,8 @@
1/* HuffEnc.h -- Huffman encoding 1/* HuffEnc.h -- Huffman encoding
22013-01-18 : Igor Pavlov : Public domain */ 22023-03-05 : Igor Pavlov : Public domain */
3 3
4#ifndef __HUFF_ENC_H 4#ifndef ZIP7_INC_HUFF_ENC_H
5#define __HUFF_ENC_H 5#define ZIP7_INC_HUFF_ENC_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
diff --git a/C/LzFind.c b/C/LzFind.c
index 1b73c28..0fbd5aa 100644
--- a/C/LzFind.c
+++ b/C/LzFind.c
@@ -1,5 +1,5 @@
1/* LzFind.c -- Match finder for LZ algorithms 1/* LzFind.c -- Match finder for LZ algorithms
22021-11-29 : Igor Pavlov : Public domain */ 22023-03-14 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -17,7 +17,7 @@
17#define kEmptyHashValue 0 17#define kEmptyHashValue 0
18 18
19#define kMaxValForNormalize ((UInt32)0) 19#define kMaxValForNormalize ((UInt32)0)
20// #define kMaxValForNormalize ((UInt32)(1 << 20) + 0xFFF) // for debug 20// #define kMaxValForNormalize ((UInt32)(1 << 20) + 0xfff) // for debug
21 21
22// #define kNormalizeAlign (1 << 7) // alignment for speculated accesses 22// #define kNormalizeAlign (1 << 7) // alignment for speculated accesses
23 23
@@ -67,10 +67,10 @@
67 67
68static void LzInWindow_Free(CMatchFinder *p, ISzAllocPtr alloc) 68static void LzInWindow_Free(CMatchFinder *p, ISzAllocPtr alloc)
69{ 69{
70 if (!p->directInput) 70 // if (!p->directInput)
71 { 71 {
72 ISzAlloc_Free(alloc, p->bufferBase); 72 ISzAlloc_Free(alloc, p->bufBase);
73 p->bufferBase = NULL; 73 p->bufBase = NULL;
74 } 74 }
75} 75}
76 76
@@ -79,7 +79,7 @@ static int LzInWindow_Create2(CMatchFinder *p, UInt32 blockSize, ISzAllocPtr all
79{ 79{
80 if (blockSize == 0) 80 if (blockSize == 0)
81 return 0; 81 return 0;
82 if (!p->bufferBase || p->blockSize != blockSize) 82 if (!p->bufBase || p->blockSize != blockSize)
83 { 83 {
84 // size_t blockSizeT; 84 // size_t blockSizeT;
85 LzInWindow_Free(p, alloc); 85 LzInWindow_Free(p, alloc);
@@ -101,11 +101,11 @@ static int LzInWindow_Create2(CMatchFinder *p, UInt32 blockSize, ISzAllocPtr all
101 #endif 101 #endif
102 */ 102 */
103 103
104 p->bufferBase = (Byte *)ISzAlloc_Alloc(alloc, blockSize); 104 p->bufBase = (Byte *)ISzAlloc_Alloc(alloc, blockSize);
105 // printf("\nbufferBase = %p\n", p->bufferBase); 105 // printf("\nbufferBase = %p\n", p->bufBase);
106 // return 0; // for debug 106 // return 0; // for debug
107 } 107 }
108 return (p->bufferBase != NULL); 108 return (p->bufBase != NULL);
109} 109}
110 110
111static const Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; } 111static const Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
@@ -113,7 +113,7 @@ static const Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return
113static UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return GET_AVAIL_BYTES(p); } 113static UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return GET_AVAIL_BYTES(p); }
114 114
115 115
116MY_NO_INLINE 116Z7_NO_INLINE
117static void MatchFinder_ReadBlock(CMatchFinder *p) 117static void MatchFinder_ReadBlock(CMatchFinder *p)
118{ 118{
119 if (p->streamEndWasReached || p->result != SZ_OK) 119 if (p->streamEndWasReached || p->result != SZ_OK)
@@ -127,8 +127,8 @@ static void MatchFinder_ReadBlock(CMatchFinder *p)
127 UInt32 curSize = 0xFFFFFFFF - GET_AVAIL_BYTES(p); 127 UInt32 curSize = 0xFFFFFFFF - GET_AVAIL_BYTES(p);
128 if (curSize > p->directInputRem) 128 if (curSize > p->directInputRem)
129 curSize = (UInt32)p->directInputRem; 129 curSize = (UInt32)p->directInputRem;
130 p->directInputRem -= curSize;
131 p->streamPos += curSize; 130 p->streamPos += curSize;
131 p->directInputRem -= curSize;
132 if (p->directInputRem == 0) 132 if (p->directInputRem == 0)
133 p->streamEndWasReached = 1; 133 p->streamEndWasReached = 1;
134 return; 134 return;
@@ -136,8 +136,8 @@ static void MatchFinder_ReadBlock(CMatchFinder *p)
136 136
137 for (;;) 137 for (;;)
138 { 138 {
139 Byte *dest = p->buffer + GET_AVAIL_BYTES(p); 139 const Byte *dest = p->buffer + GET_AVAIL_BYTES(p);
140 size_t size = (size_t)(p->bufferBase + p->blockSize - dest); 140 size_t size = (size_t)(p->bufBase + p->blockSize - dest);
141 if (size == 0) 141 if (size == 0)
142 { 142 {
143 /* we call ReadBlock() after NeedMove() and MoveBlock(). 143 /* we call ReadBlock() after NeedMove() and MoveBlock().
@@ -153,7 +153,14 @@ static void MatchFinder_ReadBlock(CMatchFinder *p)
153 // #define kRead 3 153 // #define kRead 3
154 // if (size > kRead) size = kRead; // for debug 154 // if (size > kRead) size = kRead; // for debug
155 155
156 p->result = ISeqInStream_Read(p->stream, dest, &size); 156 /*
157 // we need cast (Byte *)dest.
158 #ifdef __clang__
159 #pragma GCC diagnostic ignored "-Wcast-qual"
160 #endif
161 */
162 p->result = ISeqInStream_Read(p->stream,
163 p->bufBase + (dest - p->bufBase), &size);
157 if (p->result != SZ_OK) 164 if (p->result != SZ_OK)
158 return; 165 return;
159 if (size == 0) 166 if (size == 0)
@@ -173,14 +180,14 @@ static void MatchFinder_ReadBlock(CMatchFinder *p)
173 180
174 181
175 182
176MY_NO_INLINE 183Z7_NO_INLINE
177void MatchFinder_MoveBlock(CMatchFinder *p) 184void MatchFinder_MoveBlock(CMatchFinder *p)
178{ 185{
179 const size_t offset = (size_t)(p->buffer - p->bufferBase) - p->keepSizeBefore; 186 const size_t offset = (size_t)(p->buffer - p->bufBase) - p->keepSizeBefore;
180 const size_t keepBefore = (offset & (kBlockMoveAlign - 1)) + p->keepSizeBefore; 187 const size_t keepBefore = (offset & (kBlockMoveAlign - 1)) + p->keepSizeBefore;
181 p->buffer = p->bufferBase + keepBefore; 188 p->buffer = p->bufBase + keepBefore;
182 memmove(p->bufferBase, 189 memmove(p->bufBase,
183 p->bufferBase + (offset & ~((size_t)kBlockMoveAlign - 1)), 190 p->bufBase + (offset & ~((size_t)kBlockMoveAlign - 1)),
184 keepBefore + (size_t)GET_AVAIL_BYTES(p)); 191 keepBefore + (size_t)GET_AVAIL_BYTES(p));
185} 192}
186 193
@@ -198,7 +205,7 @@ int MatchFinder_NeedMove(CMatchFinder *p)
198 return 0; 205 return 0;
199 if (p->streamEndWasReached || p->result != SZ_OK) 206 if (p->streamEndWasReached || p->result != SZ_OK)
200 return 0; 207 return 0;
201 return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter); 208 return ((size_t)(p->bufBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
202} 209}
203 210
204void MatchFinder_ReadIfRequired(CMatchFinder *p) 211void MatchFinder_ReadIfRequired(CMatchFinder *p)
@@ -214,6 +221,8 @@ static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
214 p->cutValue = 32; 221 p->cutValue = 32;
215 p->btMode = 1; 222 p->btMode = 1;
216 p->numHashBytes = 4; 223 p->numHashBytes = 4;
224 p->numHashBytes_Min = 2;
225 p->numHashOutBits = 0;
217 p->bigHash = 0; 226 p->bigHash = 0;
218} 227}
219 228
@@ -222,8 +231,10 @@ static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
222void MatchFinder_Construct(CMatchFinder *p) 231void MatchFinder_Construct(CMatchFinder *p)
223{ 232{
224 unsigned i; 233 unsigned i;
225 p->bufferBase = NULL; 234 p->buffer = NULL;
235 p->bufBase = NULL;
226 p->directInput = 0; 236 p->directInput = 0;
237 p->stream = NULL;
227 p->hash = NULL; 238 p->hash = NULL;
228 p->expectedDataSize = (UInt64)(Int64)-1; 239 p->expectedDataSize = (UInt64)(Int64)-1;
229 MatchFinder_SetDefaultSettings(p); 240 MatchFinder_SetDefaultSettings(p);
@@ -238,6 +249,8 @@ void MatchFinder_Construct(CMatchFinder *p)
238 } 249 }
239} 250}
240 251
252#undef kCrcPoly
253
241static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAllocPtr alloc) 254static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAllocPtr alloc)
242{ 255{
243 ISzAlloc_Free(alloc, p->hash); 256 ISzAlloc_Free(alloc, p->hash);
@@ -252,7 +265,7 @@ void MatchFinder_Free(CMatchFinder *p, ISzAllocPtr alloc)
252 265
253static CLzRef* AllocRefs(size_t num, ISzAllocPtr alloc) 266static CLzRef* AllocRefs(size_t num, ISzAllocPtr alloc)
254{ 267{
255 size_t sizeInBytes = (size_t)num * sizeof(CLzRef); 268 const size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
256 if (sizeInBytes / sizeof(CLzRef) != num) 269 if (sizeInBytes / sizeof(CLzRef) != num)
257 return NULL; 270 return NULL;
258 return (CLzRef *)ISzAlloc_Alloc(alloc, sizeInBytes); 271 return (CLzRef *)ISzAlloc_Alloc(alloc, sizeInBytes);
@@ -298,6 +311,62 @@ static UInt32 GetBlockSize(CMatchFinder *p, UInt32 historySize)
298} 311}
299 312
300 313
314// input is historySize
315static UInt32 MatchFinder_GetHashMask2(CMatchFinder *p, UInt32 hs)
316{
317 if (p->numHashBytes == 2)
318 return (1 << 16) - 1;
319 if (hs != 0)
320 hs--;
321 hs |= (hs >> 1);
322 hs |= (hs >> 2);
323 hs |= (hs >> 4);
324 hs |= (hs >> 8);
325 // we propagated 16 bits in (hs). Low 16 bits must be set later
326 if (hs >= (1 << 24))
327 {
328 if (p->numHashBytes == 3)
329 hs = (1 << 24) - 1;
330 /* if (bigHash) mode, GetHeads4b() in LzFindMt.c needs (hs >= ((1 << 24) - 1))) */
331 }
332 // (hash_size >= (1 << 16)) : Required for (numHashBytes > 2)
333 hs |= (1 << 16) - 1; /* don't change it! */
334 // bt5: we adjust the size with recommended minimum size
335 if (p->numHashBytes >= 5)
336 hs |= (256 << kLzHash_CrcShift_2) - 1;
337 return hs;
338}
339
340// input is historySize
341static UInt32 MatchFinder_GetHashMask(CMatchFinder *p, UInt32 hs)
342{
343 if (p->numHashBytes == 2)
344 return (1 << 16) - 1;
345 if (hs != 0)
346 hs--;
347 hs |= (hs >> 1);
348 hs |= (hs >> 2);
349 hs |= (hs >> 4);
350 hs |= (hs >> 8);
351 // we propagated 16 bits in (hs). Low 16 bits must be set later
352 hs >>= 1;
353 if (hs >= (1 << 24))
354 {
355 if (p->numHashBytes == 3)
356 hs = (1 << 24) - 1;
357 else
358 hs >>= 1;
359 /* if (bigHash) mode, GetHeads4b() in LzFindMt.c needs (hs >= ((1 << 24) - 1))) */
360 }
361 // (hash_size >= (1 << 16)) : Required for (numHashBytes > 2)
362 hs |= (1 << 16) - 1; /* don't change it! */
363 // bt5: we adjust the size with recommended minimum size
364 if (p->numHashBytes >= 5)
365 hs |= (256 << kLzHash_CrcShift_2) - 1;
366 return hs;
367}
368
369
301int MatchFinder_Create(CMatchFinder *p, UInt32 historySize, 370int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
302 UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, 371 UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
303 ISzAllocPtr alloc) 372 ISzAllocPtr alloc)
@@ -318,78 +387,91 @@ int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
318 p->blockSize = 0; 387 p->blockSize = 0;
319 if (p->directInput || LzInWindow_Create2(p, GetBlockSize(p, historySize), alloc)) 388 if (p->directInput || LzInWindow_Create2(p, GetBlockSize(p, historySize), alloc))
320 { 389 {
321 const UInt32 newCyclicBufferSize = historySize + 1; // do not change it 390 size_t hashSizeSum;
322 UInt32 hs;
323 p->matchMaxLen = matchMaxLen;
324 { 391 {
325 // UInt32 hs4; 392 UInt32 hs;
326 p->fixedHashSize = 0; 393 UInt32 hsCur;
327 hs = (1 << 16) - 1; 394
328 if (p->numHashBytes != 2) 395 if (p->numHashOutBits != 0)
329 { 396 {
330 hs = historySize; 397 unsigned numBits = p->numHashOutBits;
331 if (hs > p->expectedDataSize) 398 const unsigned nbMax =
332 hs = (UInt32)p->expectedDataSize; 399 (p->numHashBytes == 2 ? 16 :
333 if (hs != 0) 400 (p->numHashBytes == 3 ? 24 : 32));
334 hs--; 401 if (numBits > nbMax)
335 hs |= (hs >> 1); 402 numBits = nbMax;
336 hs |= (hs >> 2); 403 if (numBits >= 32)
337 hs |= (hs >> 4); 404 hs = (UInt32)0 - 1;
338 hs |= (hs >> 8); 405 else
339 // we propagated 16 bits in (hs). Low 16 bits must be set later 406 hs = ((UInt32)1 << numBits) - 1;
340 hs >>= 1;
341 if (hs >= (1 << 24))
342 {
343 if (p->numHashBytes == 3)
344 hs = (1 << 24) - 1;
345 else
346 hs >>= 1;
347 /* if (bigHash) mode, GetHeads4b() in LzFindMt.c needs (hs >= ((1 << 24) - 1))) */
348 }
349
350 // hs = ((UInt32)1 << 25) - 1; // for test
351
352 // (hash_size >= (1 << 16)) : Required for (numHashBytes > 2) 407 // (hash_size >= (1 << 16)) : Required for (numHashBytes > 2)
353 hs |= (1 << 16) - 1; /* don't change it! */ 408 hs |= (1 << 16) - 1; /* don't change it! */
354
355 // bt5: we adjust the size with recommended minimum size
356 if (p->numHashBytes >= 5) 409 if (p->numHashBytes >= 5)
357 hs |= (256 << kLzHash_CrcShift_2) - 1; 410 hs |= (256 << kLzHash_CrcShift_2) - 1;
411 {
412 const UInt32 hs2 = MatchFinder_GetHashMask2(p, historySize);
413 if (hs > hs2)
414 hs = hs2;
415 }
416 hsCur = hs;
417 if (p->expectedDataSize < historySize)
418 {
419 const UInt32 hs2 = MatchFinder_GetHashMask2(p, (UInt32)p->expectedDataSize);
420 if (hsCur > hs2)
421 hsCur = hs2;
422 }
358 } 423 }
359 p->hashMask = hs; 424 else
360 hs++; 425 {
361 426 hs = MatchFinder_GetHashMask(p, historySize);
362 /* 427 hsCur = hs;
363 hs4 = (1 << 20); 428 if (p->expectedDataSize < historySize)
364 if (hs4 > hs) 429 {
365 hs4 = hs; 430 hsCur = MatchFinder_GetHashMask(p, (UInt32)p->expectedDataSize);
366 // hs4 = (1 << 16); // for test 431 if (hsCur > hs) // is it possible?
367 p->hash4Mask = hs4 - 1; 432 hsCur = hs;
368 */ 433 }
434 }
435
436 p->hashMask = hsCur;
369 437
370 if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size; 438 hashSizeSum = hs;
371 if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size; 439 hashSizeSum++;
372 // if (p->numHashBytes > 4) p->fixedHashSize += hs4; // kHash4Size; 440 if (hashSizeSum < hs)
373 hs += p->fixedHashSize; 441 return 0;
442 {
443 UInt32 fixedHashSize = 0;
444 if (p->numHashBytes > 2 && p->numHashBytes_Min <= 2) fixedHashSize += kHash2Size;
445 if (p->numHashBytes > 3 && p->numHashBytes_Min <= 3) fixedHashSize += kHash3Size;
446 // if (p->numHashBytes > 4) p->fixedHashSize += hs4; // kHash4Size;
447 hashSizeSum += fixedHashSize;
448 p->fixedHashSize = fixedHashSize;
449 }
374 } 450 }
375 451
452 p->matchMaxLen = matchMaxLen;
453
376 { 454 {
377 size_t newSize; 455 size_t newSize;
378 size_t numSons; 456 size_t numSons;
457 const UInt32 newCyclicBufferSize = historySize + 1; // do not change it
379 p->historySize = historySize; 458 p->historySize = historySize;
380 p->hashSizeSum = hs;
381 p->cyclicBufferSize = newCyclicBufferSize; // it must be = (historySize + 1) 459 p->cyclicBufferSize = newCyclicBufferSize; // it must be = (historySize + 1)
382 460
383 numSons = newCyclicBufferSize; 461 numSons = newCyclicBufferSize;
384 if (p->btMode) 462 if (p->btMode)
385 numSons <<= 1; 463 numSons <<= 1;
386 newSize = hs + numSons; 464 newSize = hashSizeSum + numSons;
465
466 if (numSons < newCyclicBufferSize || newSize < numSons)
467 return 0;
387 468
388 // aligned size is not required here, but it can be better for some loops 469 // aligned size is not required here, but it can be better for some loops
389 #define NUM_REFS_ALIGN_MASK 0xF 470 #define NUM_REFS_ALIGN_MASK 0xF
390 newSize = (newSize + NUM_REFS_ALIGN_MASK) & ~(size_t)NUM_REFS_ALIGN_MASK; 471 newSize = (newSize + NUM_REFS_ALIGN_MASK) & ~(size_t)NUM_REFS_ALIGN_MASK;
391 472
392 if (p->hash && p->numRefs == newSize) 473 // 22.02: we don't reallocate buffer, if old size is enough
474 if (p->hash && p->numRefs >= newSize)
393 return 1; 475 return 1;
394 476
395 MatchFinder_FreeThisClassMemory(p, alloc); 477 MatchFinder_FreeThisClassMemory(p, alloc);
@@ -398,7 +480,7 @@ int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
398 480
399 if (p->hash) 481 if (p->hash)
400 { 482 {
401 p->son = p->hash + p->hashSizeSum; 483 p->son = p->hash + hashSizeSum;
402 return 1; 484 return 1;
403 } 485 }
404 } 486 }
@@ -470,7 +552,8 @@ void MatchFinder_Init_HighHash(CMatchFinder *p)
470 552
471void MatchFinder_Init_4(CMatchFinder *p) 553void MatchFinder_Init_4(CMatchFinder *p)
472{ 554{
473 p->buffer = p->bufferBase; 555 if (!p->directInput)
556 p->buffer = p->bufBase;
474 { 557 {
475 /* kEmptyHashValue = 0 (Zero) is used in hash tables as NO-VALUE marker. 558 /* kEmptyHashValue = 0 (Zero) is used in hash tables as NO-VALUE marker.
476 the code in CMatchFinderMt expects (pos = 1) */ 559 the code in CMatchFinderMt expects (pos = 1) */
@@ -507,20 +590,20 @@ void MatchFinder_Init(CMatchFinder *p)
507 590
508 591
509#ifdef MY_CPU_X86_OR_AMD64 592#ifdef MY_CPU_X86_OR_AMD64
510 #if defined(__clang__) && (__clang_major__ >= 8) \ 593 #if defined(__clang__) && (__clang_major__ >= 4) \
511 || defined(__GNUC__) && (__GNUC__ >= 8) \ 594 || defined(Z7_GCC_VERSION) && (Z7_GCC_VERSION >= 40701)
512 || defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1900) 595 // || defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1900)
513 #define USE_SATUR_SUB_128 596
514 #define USE_AVX2 597 #define USE_LZFIND_SATUR_SUB_128
515 #define ATTRIB_SSE41 __attribute__((__target__("sse4.1"))) 598 #define USE_LZFIND_SATUR_SUB_256
516 #define ATTRIB_AVX2 __attribute__((__target__("avx2"))) 599 #define LZFIND_ATTRIB_SSE41 __attribute__((__target__("sse4.1")))
600 #define LZFIND_ATTRIB_AVX2 __attribute__((__target__("avx2")))
517 #elif defined(_MSC_VER) 601 #elif defined(_MSC_VER)
518 #if (_MSC_VER >= 1600) 602 #if (_MSC_VER >= 1600)
519 #define USE_SATUR_SUB_128 603 #define USE_LZFIND_SATUR_SUB_128
520 #if (_MSC_VER >= 1900) 604 #endif
521 #define USE_AVX2 605 #if (_MSC_VER >= 1900)
522 #include <immintrin.h> // avx 606 #define USE_LZFIND_SATUR_SUB_256
523 #endif
524 #endif 607 #endif
525 #endif 608 #endif
526 609
@@ -529,16 +612,16 @@ void MatchFinder_Init(CMatchFinder *p)
529 612
530 #if defined(__clang__) && (__clang_major__ >= 8) \ 613 #if defined(__clang__) && (__clang_major__ >= 8) \
531 || defined(__GNUC__) && (__GNUC__ >= 8) 614 || defined(__GNUC__) && (__GNUC__ >= 8)
532 #define USE_SATUR_SUB_128 615 #define USE_LZFIND_SATUR_SUB_128
533 #ifdef MY_CPU_ARM64 616 #ifdef MY_CPU_ARM64
534 // #define ATTRIB_SSE41 __attribute__((__target__(""))) 617 // #define LZFIND_ATTRIB_SSE41 __attribute__((__target__("")))
535 #else 618 #else
536 // #define ATTRIB_SSE41 __attribute__((__target__("fpu=crypto-neon-fp-armv8"))) 619 // #define LZFIND_ATTRIB_SSE41 __attribute__((__target__("fpu=crypto-neon-fp-armv8")))
537 #endif 620 #endif
538 621
539 #elif defined(_MSC_VER) 622 #elif defined(_MSC_VER)
540 #if (_MSC_VER >= 1910) 623 #if (_MSC_VER >= 1910)
541 #define USE_SATUR_SUB_128 624 #define USE_LZFIND_SATUR_SUB_128
542 #endif 625 #endif
543 #endif 626 #endif
544 627
@@ -550,121 +633,130 @@ void MatchFinder_Init(CMatchFinder *p)
550 633
551#endif 634#endif
552 635
553/*
554#ifndef ATTRIB_SSE41
555 #define ATTRIB_SSE41
556#endif
557#ifndef ATTRIB_AVX2
558 #define ATTRIB_AVX2
559#endif
560*/
561 636
562#ifdef USE_SATUR_SUB_128 637#ifdef USE_LZFIND_SATUR_SUB_128
563 638
564// #define _SHOW_HW_STATUS 639// #define Z7_SHOW_HW_STATUS
565 640
566#ifdef _SHOW_HW_STATUS 641#ifdef Z7_SHOW_HW_STATUS
567#include <stdio.h> 642#include <stdio.h>
568#define _PRF(x) x 643#define PRF(x) x
569_PRF(;) 644PRF(;)
570#else 645#else
571#define _PRF(x) 646#define PRF(x)
572#endif 647#endif
573 648
649
574#ifdef MY_CPU_ARM_OR_ARM64 650#ifdef MY_CPU_ARM_OR_ARM64
575 651
576#ifdef MY_CPU_ARM64 652#ifdef MY_CPU_ARM64
577// #define FORCE_SATUR_SUB_128 653// #define FORCE_LZFIND_SATUR_SUB_128
578#endif 654#endif
655typedef uint32x4_t LzFind_v128;
656#define SASUB_128_V(v, s) \
657 vsubq_u32(vmaxq_u32(v, s), s)
579 658
580typedef uint32x4_t v128; 659#else // MY_CPU_ARM_OR_ARM64
581#define SASUB_128(i) \
582 *(v128 *)(void *)(items + (i) * 4) = \
583 vsubq_u32(vmaxq_u32(*(const v128 *)(const void *)(items + (i) * 4), sub2), sub2);
584
585#else
586 660
587#include <smmintrin.h> // sse4.1 661#include <smmintrin.h> // sse4.1
588 662
589typedef __m128i v128; 663typedef __m128i LzFind_v128;
590#define SASUB_128(i) \ 664// SSE 4.1
591 *(v128 *)(void *)(items + (i) * 4) = \ 665#define SASUB_128_V(v, s) \
592 _mm_sub_epi32(_mm_max_epu32(*(const v128 *)(const void *)(items + (i) * 4), sub2), sub2); // SSE 4.1 666 _mm_sub_epi32(_mm_max_epu32(v, s), s)
593 667
594#endif 668#endif // MY_CPU_ARM_OR_ARM64
595 669
596 670
671#define SASUB_128(i) \
672 *( LzFind_v128 *)( void *)(items + (i) * 4) = SASUB_128_V( \
673 *(const LzFind_v128 *)(const void *)(items + (i) * 4), sub2);
674
597 675
598MY_NO_INLINE 676Z7_NO_INLINE
599static 677static
600#ifdef ATTRIB_SSE41 678#ifdef LZFIND_ATTRIB_SSE41
601ATTRIB_SSE41 679LZFIND_ATTRIB_SSE41
602#endif 680#endif
603void 681void
604MY_FAST_CALL 682Z7_FASTCALL
605LzFind_SaturSub_128(UInt32 subValue, CLzRef *items, const CLzRef *lim) 683LzFind_SaturSub_128(UInt32 subValue, CLzRef *items, const CLzRef *lim)
606{ 684{
607 v128 sub2 = 685 const LzFind_v128 sub2 =
608 #ifdef MY_CPU_ARM_OR_ARM64 686 #ifdef MY_CPU_ARM_OR_ARM64
609 vdupq_n_u32(subValue); 687 vdupq_n_u32(subValue);
610 #else 688 #else
611 _mm_set_epi32((Int32)subValue, (Int32)subValue, (Int32)subValue, (Int32)subValue); 689 _mm_set_epi32((Int32)subValue, (Int32)subValue, (Int32)subValue, (Int32)subValue);
612 #endif 690 #endif
691 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
613 do 692 do
614 { 693 {
615 SASUB_128(0) 694 SASUB_128(0) SASUB_128(1) items += 2 * 4;
616 SASUB_128(1) 695 SASUB_128(0) SASUB_128(1) items += 2 * 4;
617 SASUB_128(2)
618 SASUB_128(3)
619 items += 4 * 4;
620 } 696 }
621 while (items != lim); 697 while (items != lim);
622} 698}
623 699
624 700
625 701
626#ifdef USE_AVX2 702#ifdef USE_LZFIND_SATUR_SUB_256
627 703
628#include <immintrin.h> // avx 704#include <immintrin.h> // avx
705/*
706clang :immintrin.h uses
707#if !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) || \
708 defined(__AVX2__)
709#include <avx2intrin.h>
710#endif
711so we need <avxintrin.h> for clang-cl */
629 712
630#define SASUB_256(i) *(__m256i *)(void *)(items + (i) * 8) = _mm256_sub_epi32(_mm256_max_epu32(*(const __m256i *)(const void *)(items + (i) * 8), sub2), sub2); // AVX2 713#if defined(__clang__)
714#include <avxintrin.h>
715#include <avx2intrin.h>
716#endif
631 717
632MY_NO_INLINE 718// AVX2:
719#define SASUB_256(i) \
720 *( __m256i *)( void *)(items + (i) * 8) = \
721 _mm256_sub_epi32(_mm256_max_epu32( \
722 *(const __m256i *)(const void *)(items + (i) * 8), sub2), sub2);
723
724Z7_NO_INLINE
633static 725static
634#ifdef ATTRIB_AVX2 726#ifdef LZFIND_ATTRIB_AVX2
635ATTRIB_AVX2 727LZFIND_ATTRIB_AVX2
636#endif 728#endif
637void 729void
638MY_FAST_CALL 730Z7_FASTCALL
639LzFind_SaturSub_256(UInt32 subValue, CLzRef *items, const CLzRef *lim) 731LzFind_SaturSub_256(UInt32 subValue, CLzRef *items, const CLzRef *lim)
640{ 732{
641 __m256i sub2 = _mm256_set_epi32( 733 const __m256i sub2 = _mm256_set_epi32(
642 (Int32)subValue, (Int32)subValue, (Int32)subValue, (Int32)subValue, 734 (Int32)subValue, (Int32)subValue, (Int32)subValue, (Int32)subValue,
643 (Int32)subValue, (Int32)subValue, (Int32)subValue, (Int32)subValue); 735 (Int32)subValue, (Int32)subValue, (Int32)subValue, (Int32)subValue);
736 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
644 do 737 do
645 { 738 {
646 SASUB_256(0) 739 SASUB_256(0) SASUB_256(1) items += 2 * 8;
647 SASUB_256(1) 740 SASUB_256(0) SASUB_256(1) items += 2 * 8;
648 items += 2 * 8;
649 } 741 }
650 while (items != lim); 742 while (items != lim);
651} 743}
652#endif // USE_AVX2 744#endif // USE_LZFIND_SATUR_SUB_256
653 745
654#ifndef FORCE_SATUR_SUB_128 746#ifndef FORCE_LZFIND_SATUR_SUB_128
655typedef void (MY_FAST_CALL *LZFIND_SATUR_SUB_CODE_FUNC)( 747typedef void (Z7_FASTCALL *LZFIND_SATUR_SUB_CODE_FUNC)(
656 UInt32 subValue, CLzRef *items, const CLzRef *lim); 748 UInt32 subValue, CLzRef *items, const CLzRef *lim);
657static LZFIND_SATUR_SUB_CODE_FUNC g_LzFind_SaturSub; 749static LZFIND_SATUR_SUB_CODE_FUNC g_LzFind_SaturSub;
658#endif // FORCE_SATUR_SUB_128 750#endif // FORCE_LZFIND_SATUR_SUB_128
659 751
660#endif // USE_SATUR_SUB_128 752#endif // USE_LZFIND_SATUR_SUB_128
661 753
662 754
663// kEmptyHashValue must be zero 755// kEmptyHashValue must be zero
664// #define SASUB_32(i) v = items[i]; m = v - subValue; if (v < subValue) m = kEmptyHashValue; items[i] = m; 756// #define SASUB_32(i) { UInt32 v = items[i]; UInt32 m = v - subValue; if (v < subValue) m = kEmptyHashValue; items[i] = m; }
665#define SASUB_32(i) v = items[i]; if (v < subValue) v = subValue; items[i] = v - subValue; 757#define SASUB_32(i) { UInt32 v = items[i]; if (v < subValue) v = subValue; items[i] = v - subValue; }
666 758
667#ifdef FORCE_SATUR_SUB_128 759#ifdef FORCE_LZFIND_SATUR_SUB_128
668 760
669#define DEFAULT_SaturSub LzFind_SaturSub_128 761#define DEFAULT_SaturSub LzFind_SaturSub_128
670 762
@@ -672,24 +764,19 @@ static LZFIND_SATUR_SUB_CODE_FUNC g_LzFind_SaturSub;
672 764
673#define DEFAULT_SaturSub LzFind_SaturSub_32 765#define DEFAULT_SaturSub LzFind_SaturSub_32
674 766
675MY_NO_INLINE 767Z7_NO_INLINE
676static 768static
677void 769void
678MY_FAST_CALL 770Z7_FASTCALL
679LzFind_SaturSub_32(UInt32 subValue, CLzRef *items, const CLzRef *lim) 771LzFind_SaturSub_32(UInt32 subValue, CLzRef *items, const CLzRef *lim)
680{ 772{
773 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
681 do 774 do
682 { 775 {
683 UInt32 v; 776 SASUB_32(0) SASUB_32(1) items += 2;
684 SASUB_32(0) 777 SASUB_32(0) SASUB_32(1) items += 2;
685 SASUB_32(1) 778 SASUB_32(0) SASUB_32(1) items += 2;
686 SASUB_32(2) 779 SASUB_32(0) SASUB_32(1) items += 2;
687 SASUB_32(3)
688 SASUB_32(4)
689 SASUB_32(5)
690 SASUB_32(6)
691 SASUB_32(7)
692 items += 8;
693 } 780 }
694 while (items != lim); 781 while (items != lim);
695} 782}
@@ -697,27 +784,23 @@ LzFind_SaturSub_32(UInt32 subValue, CLzRef *items, const CLzRef *lim)
697#endif 784#endif
698 785
699 786
700MY_NO_INLINE 787Z7_NO_INLINE
701void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems) 788void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems)
702{ 789{
703 #define K_NORM_ALIGN_BLOCK_SIZE (1 << 6) 790 #define LZFIND_NORM_ALIGN_BLOCK_SIZE (1 << 7)
704 791 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
705 CLzRef *lim; 792 for (; numItems != 0 && ((unsigned)(ptrdiff_t)items & (LZFIND_NORM_ALIGN_BLOCK_SIZE - 1)) != 0; numItems--)
706
707 for (; numItems != 0 && ((unsigned)(ptrdiff_t)items & (K_NORM_ALIGN_BLOCK_SIZE - 1)) != 0; numItems--)
708 { 793 {
709 UInt32 v; 794 SASUB_32(0)
710 SASUB_32(0);
711 items++; 795 items++;
712 } 796 }
713
714 { 797 {
715 #define K_NORM_ALIGN_MASK (K_NORM_ALIGN_BLOCK_SIZE / 4 - 1) 798 const size_t k_Align_Mask = (LZFIND_NORM_ALIGN_BLOCK_SIZE / 4 - 1);
716 lim = items + (numItems & ~(size_t)K_NORM_ALIGN_MASK); 799 CLzRef *lim = items + (numItems & ~(size_t)k_Align_Mask);
717 numItems &= K_NORM_ALIGN_MASK; 800 numItems &= k_Align_Mask;
718 if (items != lim) 801 if (items != lim)
719 { 802 {
720 #if defined(USE_SATUR_SUB_128) && !defined(FORCE_SATUR_SUB_128) 803 #if defined(USE_LZFIND_SATUR_SUB_128) && !defined(FORCE_LZFIND_SATUR_SUB_128)
721 if (g_LzFind_SaturSub) 804 if (g_LzFind_SaturSub)
722 g_LzFind_SaturSub(subValue, items, lim); 805 g_LzFind_SaturSub(subValue, items, lim);
723 else 806 else
@@ -726,12 +809,10 @@ void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems)
726 } 809 }
727 items = lim; 810 items = lim;
728 } 811 }
729 812 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
730
731 for (; numItems != 0; numItems--) 813 for (; numItems != 0; numItems--)
732 { 814 {
733 UInt32 v; 815 SASUB_32(0)
734 SASUB_32(0);
735 items++; 816 items++;
736 } 817 }
737} 818}
@@ -740,7 +821,7 @@ void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems)
740 821
741// call MatchFinder_CheckLimits() only after (p->pos++) update 822// call MatchFinder_CheckLimits() only after (p->pos++) update
742 823
743MY_NO_INLINE 824Z7_NO_INLINE
744static void MatchFinder_CheckLimits(CMatchFinder *p) 825static void MatchFinder_CheckLimits(CMatchFinder *p)
745{ 826{
746 if (// !p->streamEndWasReached && p->result == SZ_OK && 827 if (// !p->streamEndWasReached && p->result == SZ_OK &&
@@ -768,11 +849,14 @@ static void MatchFinder_CheckLimits(CMatchFinder *p)
768 const UInt32 subValue = (p->pos - p->historySize - 1) /* & ~(UInt32)(kNormalizeAlign - 1) */; 849 const UInt32 subValue = (p->pos - p->historySize - 1) /* & ~(UInt32)(kNormalizeAlign - 1) */;
769 // const UInt32 subValue = (1 << 15); // for debug 850 // const UInt32 subValue = (1 << 15); // for debug
770 // printf("\nMatchFinder_Normalize() subValue == 0x%x\n", subValue); 851 // printf("\nMatchFinder_Normalize() subValue == 0x%x\n", subValue);
771 size_t numSonRefs = p->cyclicBufferSize; 852 MatchFinder_REDUCE_OFFSETS(p, subValue)
772 if (p->btMode) 853 MatchFinder_Normalize3(subValue, p->hash, (size_t)p->hashMask + 1 + p->fixedHashSize);
773 numSonRefs <<= 1; 854 {
774 Inline_MatchFinder_ReduceOffsets(p, subValue); 855 size_t numSonRefs = p->cyclicBufferSize;
775 MatchFinder_Normalize3(subValue, p->hash, (size_t)p->hashSizeSum + numSonRefs); 856 if (p->btMode)
857 numSonRefs <<= 1;
858 MatchFinder_Normalize3(subValue, p->son, numSonRefs);
859 }
776 } 860 }
777 861
778 if (p->cyclicBufferPos == p->cyclicBufferSize) 862 if (p->cyclicBufferPos == p->cyclicBufferSize)
@@ -785,7 +869,7 @@ static void MatchFinder_CheckLimits(CMatchFinder *p)
785/* 869/*
786 (lenLimit > maxLen) 870 (lenLimit > maxLen)
787*/ 871*/
788MY_FORCE_INLINE 872Z7_FORCE_INLINE
789static UInt32 * Hc_GetMatchesSpec(size_t lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son, 873static UInt32 * Hc_GetMatchesSpec(size_t lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
790 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue, 874 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
791 UInt32 *d, unsigned maxLen) 875 UInt32 *d, unsigned maxLen)
@@ -867,7 +951,7 @@ static UInt32 * Hc_GetMatchesSpec(size_t lenLimit, UInt32 curMatch, UInt32 pos,
867} 951}
868 952
869 953
870MY_FORCE_INLINE 954Z7_FORCE_INLINE
871UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son, 955UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
872 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue, 956 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
873 UInt32 *d, UInt32 maxLen) 957 UInt32 *d, UInt32 maxLen)
@@ -1004,7 +1088,7 @@ static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const
1004 1088
1005#define MOVE_POS_RET MOVE_POS return distances; 1089#define MOVE_POS_RET MOVE_POS return distances;
1006 1090
1007MY_NO_INLINE 1091Z7_NO_INLINE
1008static void MatchFinder_MovePos(CMatchFinder *p) 1092static void MatchFinder_MovePos(CMatchFinder *p)
1009{ 1093{
1010 /* we go here at the end of stream data, when (avail < num_hash_bytes) 1094 /* we go here at the end of stream data, when (avail < num_hash_bytes)
@@ -1015,11 +1099,11 @@ static void MatchFinder_MovePos(CMatchFinder *p)
1015 if (p->btMode) 1099 if (p->btMode)
1016 p->sons[(p->cyclicBufferPos << p->btMode) + 1] = 0; // kEmptyHashValue 1100 p->sons[(p->cyclicBufferPos << p->btMode) + 1] = 0; // kEmptyHashValue
1017 */ 1101 */
1018 MOVE_POS; 1102 MOVE_POS
1019} 1103}
1020 1104
1021#define GET_MATCHES_HEADER2(minLen, ret_op) \ 1105#define GET_MATCHES_HEADER2(minLen, ret_op) \
1022 unsigned lenLimit; UInt32 hv; Byte *cur; UInt32 curMatch; \ 1106 unsigned lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \
1023 lenLimit = (unsigned)p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \ 1107 lenLimit = (unsigned)p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
1024 cur = p->buffer; 1108 cur = p->buffer;
1025 1109
@@ -1028,11 +1112,11 @@ static void MatchFinder_MovePos(CMatchFinder *p)
1028 1112
1029#define MF_PARAMS(p) lenLimit, curMatch, p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue 1113#define MF_PARAMS(p) lenLimit, curMatch, p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
1030 1114
1031#define SKIP_FOOTER SkipMatchesSpec(MF_PARAMS(p)); MOVE_POS; } while (--num); 1115#define SKIP_FOOTER SkipMatchesSpec(MF_PARAMS(p)); MOVE_POS } while (--num);
1032 1116
1033#define GET_MATCHES_FOOTER_BASE(_maxLen_, func) \ 1117#define GET_MATCHES_FOOTER_BASE(_maxLen_, func) \
1034 distances = func(MF_PARAMS(p), \ 1118 distances = func(MF_PARAMS(p), \
1035 distances, (UInt32)_maxLen_); MOVE_POS_RET; 1119 distances, (UInt32)_maxLen_); MOVE_POS_RET
1036 1120
1037#define GET_MATCHES_FOOTER_BT(_maxLen_) \ 1121#define GET_MATCHES_FOOTER_BT(_maxLen_) \
1038 GET_MATCHES_FOOTER_BASE(_maxLen_, GetMatchesSpec1) 1122 GET_MATCHES_FOOTER_BASE(_maxLen_, GetMatchesSpec1)
@@ -1052,7 +1136,7 @@ static void MatchFinder_MovePos(CMatchFinder *p)
1052static UInt32* Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) 1136static UInt32* Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1053{ 1137{
1054 GET_MATCHES_HEADER(2) 1138 GET_MATCHES_HEADER(2)
1055 HASH2_CALC; 1139 HASH2_CALC
1056 curMatch = p->hash[hv]; 1140 curMatch = p->hash[hv];
1057 p->hash[hv] = p->pos; 1141 p->hash[hv] = p->pos;
1058 GET_MATCHES_FOOTER_BT(1) 1142 GET_MATCHES_FOOTER_BT(1)
@@ -1061,7 +1145,7 @@ static UInt32* Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1061UInt32* Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) 1145UInt32* Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1062{ 1146{
1063 GET_MATCHES_HEADER(3) 1147 GET_MATCHES_HEADER(3)
1064 HASH_ZIP_CALC; 1148 HASH_ZIP_CALC
1065 curMatch = p->hash[hv]; 1149 curMatch = p->hash[hv];
1066 p->hash[hv] = p->pos; 1150 p->hash[hv] = p->pos;
1067 GET_MATCHES_FOOTER_BT(2) 1151 GET_MATCHES_FOOTER_BT(2)
@@ -1082,7 +1166,7 @@ static UInt32* Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1082 UInt32 *hash; 1166 UInt32 *hash;
1083 GET_MATCHES_HEADER(3) 1167 GET_MATCHES_HEADER(3)
1084 1168
1085 HASH3_CALC; 1169 HASH3_CALC
1086 1170
1087 hash = p->hash; 1171 hash = p->hash;
1088 pos = p->pos; 1172 pos = p->pos;
@@ -1107,7 +1191,7 @@ static UInt32* Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1107 if (maxLen == lenLimit) 1191 if (maxLen == lenLimit)
1108 { 1192 {
1109 SkipMatchesSpec(MF_PARAMS(p)); 1193 SkipMatchesSpec(MF_PARAMS(p));
1110 MOVE_POS_RET; 1194 MOVE_POS_RET
1111 } 1195 }
1112 } 1196 }
1113 1197
@@ -1123,7 +1207,7 @@ static UInt32* Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1123 UInt32 *hash; 1207 UInt32 *hash;
1124 GET_MATCHES_HEADER(4) 1208 GET_MATCHES_HEADER(4)
1125 1209
1126 HASH4_CALC; 1210 HASH4_CALC
1127 1211
1128 hash = p->hash; 1212 hash = p->hash;
1129 pos = p->pos; 1213 pos = p->pos;
@@ -1190,7 +1274,7 @@ static UInt32* Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1190 UInt32 *hash; 1274 UInt32 *hash;
1191 GET_MATCHES_HEADER(5) 1275 GET_MATCHES_HEADER(5)
1192 1276
1193 HASH5_CALC; 1277 HASH5_CALC
1194 1278
1195 hash = p->hash; 1279 hash = p->hash;
1196 pos = p->pos; 1280 pos = p->pos;
@@ -1246,7 +1330,7 @@ static UInt32* Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1246 if (maxLen == lenLimit) 1330 if (maxLen == lenLimit)
1247 { 1331 {
1248 SkipMatchesSpec(MF_PARAMS(p)); 1332 SkipMatchesSpec(MF_PARAMS(p));
1249 MOVE_POS_RET; 1333 MOVE_POS_RET
1250 } 1334 }
1251 break; 1335 break;
1252 } 1336 }
@@ -1263,7 +1347,7 @@ static UInt32* Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1263 UInt32 *hash; 1347 UInt32 *hash;
1264 GET_MATCHES_HEADER(4) 1348 GET_MATCHES_HEADER(4)
1265 1349
1266 HASH4_CALC; 1350 HASH4_CALC
1267 1351
1268 hash = p->hash; 1352 hash = p->hash;
1269 pos = p->pos; 1353 pos = p->pos;
@@ -1314,12 +1398,12 @@ static UInt32* Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1314 if (maxLen == lenLimit) 1398 if (maxLen == lenLimit)
1315 { 1399 {
1316 p->son[p->cyclicBufferPos] = curMatch; 1400 p->son[p->cyclicBufferPos] = curMatch;
1317 MOVE_POS_RET; 1401 MOVE_POS_RET
1318 } 1402 }
1319 break; 1403 break;
1320 } 1404 }
1321 1405
1322 GET_MATCHES_FOOTER_HC(maxLen); 1406 GET_MATCHES_FOOTER_HC(maxLen)
1323} 1407}
1324 1408
1325 1409
@@ -1330,7 +1414,7 @@ static UInt32 * Hc5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1330 UInt32 *hash; 1414 UInt32 *hash;
1331 GET_MATCHES_HEADER(5) 1415 GET_MATCHES_HEADER(5)
1332 1416
1333 HASH5_CALC; 1417 HASH5_CALC
1334 1418
1335 hash = p->hash; 1419 hash = p->hash;
1336 pos = p->pos; 1420 pos = p->pos;
@@ -1386,19 +1470,19 @@ static UInt32 * Hc5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1386 if (maxLen == lenLimit) 1470 if (maxLen == lenLimit)
1387 { 1471 {
1388 p->son[p->cyclicBufferPos] = curMatch; 1472 p->son[p->cyclicBufferPos] = curMatch;
1389 MOVE_POS_RET; 1473 MOVE_POS_RET
1390 } 1474 }
1391 break; 1475 break;
1392 } 1476 }
1393 1477
1394 GET_MATCHES_FOOTER_HC(maxLen); 1478 GET_MATCHES_FOOTER_HC(maxLen)
1395} 1479}
1396 1480
1397 1481
1398UInt32* Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) 1482UInt32* Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1399{ 1483{
1400 GET_MATCHES_HEADER(3) 1484 GET_MATCHES_HEADER(3)
1401 HASH_ZIP_CALC; 1485 HASH_ZIP_CALC
1402 curMatch = p->hash[hv]; 1486 curMatch = p->hash[hv];
1403 p->hash[hv] = p->pos; 1487 p->hash[hv] = p->pos;
1404 GET_MATCHES_FOOTER_HC(2) 1488 GET_MATCHES_FOOTER_HC(2)
@@ -1409,7 +1493,7 @@ static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1409{ 1493{
1410 SKIP_HEADER(2) 1494 SKIP_HEADER(2)
1411 { 1495 {
1412 HASH2_CALC; 1496 HASH2_CALC
1413 curMatch = p->hash[hv]; 1497 curMatch = p->hash[hv];
1414 p->hash[hv] = p->pos; 1498 p->hash[hv] = p->pos;
1415 } 1499 }
@@ -1420,7 +1504,7 @@ void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1420{ 1504{
1421 SKIP_HEADER(3) 1505 SKIP_HEADER(3)
1422 { 1506 {
1423 HASH_ZIP_CALC; 1507 HASH_ZIP_CALC
1424 curMatch = p->hash[hv]; 1508 curMatch = p->hash[hv];
1425 p->hash[hv] = p->pos; 1509 p->hash[hv] = p->pos;
1426 } 1510 }
@@ -1433,7 +1517,7 @@ static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1433 { 1517 {
1434 UInt32 h2; 1518 UInt32 h2;
1435 UInt32 *hash; 1519 UInt32 *hash;
1436 HASH3_CALC; 1520 HASH3_CALC
1437 hash = p->hash; 1521 hash = p->hash;
1438 curMatch = (hash + kFix3HashSize)[hv]; 1522 curMatch = (hash + kFix3HashSize)[hv];
1439 hash[h2] = 1523 hash[h2] =
@@ -1448,7 +1532,7 @@ static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1448 { 1532 {
1449 UInt32 h2, h3; 1533 UInt32 h2, h3;
1450 UInt32 *hash; 1534 UInt32 *hash;
1451 HASH4_CALC; 1535 HASH4_CALC
1452 hash = p->hash; 1536 hash = p->hash;
1453 curMatch = (hash + kFix4HashSize)[hv]; 1537 curMatch = (hash + kFix4HashSize)[hv];
1454 hash [h2] = 1538 hash [h2] =
@@ -1464,7 +1548,7 @@ static void Bt5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1464 { 1548 {
1465 UInt32 h2, h3; 1549 UInt32 h2, h3;
1466 UInt32 *hash; 1550 UInt32 *hash;
1467 HASH5_CALC; 1551 HASH5_CALC
1468 hash = p->hash; 1552 hash = p->hash;
1469 curMatch = (hash + kFix5HashSize)[hv]; 1553 curMatch = (hash + kFix5HashSize)[hv];
1470 hash [h2] = 1554 hash [h2] =
@@ -1478,7 +1562,7 @@ static void Bt5_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1478 1562
1479#define HC_SKIP_HEADER(minLen) \ 1563#define HC_SKIP_HEADER(minLen) \
1480 do { if (p->lenLimit < minLen) { MatchFinder_MovePos(p); num--; continue; } { \ 1564 do { if (p->lenLimit < minLen) { MatchFinder_MovePos(p); num--; continue; } { \
1481 Byte *cur; \ 1565 const Byte *cur; \
1482 UInt32 *hash; \ 1566 UInt32 *hash; \
1483 UInt32 *son; \ 1567 UInt32 *son; \
1484 UInt32 pos = p->pos; \ 1568 UInt32 pos = p->pos; \
@@ -1510,7 +1594,7 @@ static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1510 HC_SKIP_HEADER(4) 1594 HC_SKIP_HEADER(4)
1511 1595
1512 UInt32 h2, h3; 1596 UInt32 h2, h3;
1513 HASH4_CALC; 1597 HASH4_CALC
1514 curMatch = (hash + kFix4HashSize)[hv]; 1598 curMatch = (hash + kFix4HashSize)[hv];
1515 hash [h2] = 1599 hash [h2] =
1516 (hash + kFix3HashSize)[h3] = 1600 (hash + kFix3HashSize)[h3] =
@@ -1540,7 +1624,7 @@ void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1540{ 1624{
1541 HC_SKIP_HEADER(3) 1625 HC_SKIP_HEADER(3)
1542 1626
1543 HASH_ZIP_CALC; 1627 HASH_ZIP_CALC
1544 curMatch = hash[hv]; 1628 curMatch = hash[hv];
1545 hash[hv] = pos; 1629 hash[hv] = pos;
1546 1630
@@ -1590,17 +1674,17 @@ void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder2 *vTable)
1590 1674
1591 1675
1592 1676
1593void LzFindPrepare() 1677void LzFindPrepare(void)
1594{ 1678{
1595 #ifndef FORCE_SATUR_SUB_128 1679 #ifndef FORCE_LZFIND_SATUR_SUB_128
1596 #ifdef USE_SATUR_SUB_128 1680 #ifdef USE_LZFIND_SATUR_SUB_128
1597 LZFIND_SATUR_SUB_CODE_FUNC f = NULL; 1681 LZFIND_SATUR_SUB_CODE_FUNC f = NULL;
1598 #ifdef MY_CPU_ARM_OR_ARM64 1682 #ifdef MY_CPU_ARM_OR_ARM64
1599 { 1683 {
1600 if (CPU_IsSupported_NEON()) 1684 if (CPU_IsSupported_NEON())
1601 { 1685 {
1602 // #pragma message ("=== LzFind NEON") 1686 // #pragma message ("=== LzFind NEON")
1603 _PRF(printf("\n=== LzFind NEON\n")); 1687 PRF(printf("\n=== LzFind NEON\n"));
1604 f = LzFind_SaturSub_128; 1688 f = LzFind_SaturSub_128;
1605 } 1689 }
1606 // f = 0; // for debug 1690 // f = 0; // for debug
@@ -1609,20 +1693,25 @@ void LzFindPrepare()
1609 if (CPU_IsSupported_SSE41()) 1693 if (CPU_IsSupported_SSE41())
1610 { 1694 {
1611 // #pragma message ("=== LzFind SSE41") 1695 // #pragma message ("=== LzFind SSE41")
1612 _PRF(printf("\n=== LzFind SSE41\n")); 1696 PRF(printf("\n=== LzFind SSE41\n"));
1613 f = LzFind_SaturSub_128; 1697 f = LzFind_SaturSub_128;
1614 1698
1615 #ifdef USE_AVX2 1699 #ifdef USE_LZFIND_SATUR_SUB_256
1616 if (CPU_IsSupported_AVX2()) 1700 if (CPU_IsSupported_AVX2())
1617 { 1701 {
1618 // #pragma message ("=== LzFind AVX2") 1702 // #pragma message ("=== LzFind AVX2")
1619 _PRF(printf("\n=== LzFind AVX2\n")); 1703 PRF(printf("\n=== LzFind AVX2\n"));
1620 f = LzFind_SaturSub_256; 1704 f = LzFind_SaturSub_256;
1621 } 1705 }
1622 #endif 1706 #endif
1623 } 1707 }
1624 #endif // MY_CPU_ARM_OR_ARM64 1708 #endif // MY_CPU_ARM_OR_ARM64
1625 g_LzFind_SaturSub = f; 1709 g_LzFind_SaturSub = f;
1626 #endif // USE_SATUR_SUB_128 1710 #endif // USE_LZFIND_SATUR_SUB_128
1627 #endif // FORCE_SATUR_SUB_128 1711 #endif // FORCE_LZFIND_SATUR_SUB_128
1628} 1712}
1713
1714
1715#undef MOVE_POS
1716#undef MOVE_POS_RET
1717#undef PRF
diff --git a/C/LzFind.h b/C/LzFind.h
index eea873f..a3f72c9 100644
--- a/C/LzFind.h
+++ b/C/LzFind.h
@@ -1,8 +1,8 @@
1/* LzFind.h -- Match finder for LZ algorithms 1/* LzFind.h -- Match finder for LZ algorithms
22021-07-13 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZ_FIND_H 4#ifndef ZIP7_INC_LZ_FIND_H
5#define __LZ_FIND_H 5#define ZIP7_INC_LZ_FIND_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -10,9 +10,9 @@ EXTERN_C_BEGIN
10 10
11typedef UInt32 CLzRef; 11typedef UInt32 CLzRef;
12 12
13typedef struct _CMatchFinder 13typedef struct
14{ 14{
15 Byte *buffer; 15 const Byte *buffer;
16 UInt32 pos; 16 UInt32 pos;
17 UInt32 posLimit; 17 UInt32 posLimit;
18 UInt32 streamPos; /* wrap over Zero is allowed (streamPos < pos). Use (UInt32)(streamPos - pos) */ 18 UInt32 streamPos; /* wrap over Zero is allowed (streamPos < pos). Use (UInt32)(streamPos - pos) */
@@ -32,8 +32,8 @@ typedef struct _CMatchFinder
32 UInt32 hashMask; 32 UInt32 hashMask;
33 UInt32 cutValue; 33 UInt32 cutValue;
34 34
35 Byte *bufferBase; 35 Byte *bufBase;
36 ISeqInStream *stream; 36 ISeqInStreamPtr stream;
37 37
38 UInt32 blockSize; 38 UInt32 blockSize;
39 UInt32 keepSizeBefore; 39 UInt32 keepSizeBefore;
@@ -43,7 +43,9 @@ typedef struct _CMatchFinder
43 size_t directInputRem; 43 size_t directInputRem;
44 UInt32 historySize; 44 UInt32 historySize;
45 UInt32 fixedHashSize; 45 UInt32 fixedHashSize;
46 UInt32 hashSizeSum; 46 Byte numHashBytes_Min;
47 Byte numHashOutBits;
48 Byte _pad2_[2];
47 SRes result; 49 SRes result;
48 UInt32 crc[256]; 50 UInt32 crc[256];
49 size_t numRefs; 51 size_t numRefs;
@@ -69,24 +71,45 @@ void MatchFinder_ReadIfRequired(CMatchFinder *p);
69 71
70void MatchFinder_Construct(CMatchFinder *p); 72void MatchFinder_Construct(CMatchFinder *p);
71 73
72/* Conditions: 74/* (directInput = 0) is default value.
73 historySize <= 3 GB 75 It's required to provide correct (directInput) value
74 keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB 76 before calling MatchFinder_Create().
77 You can set (directInput) by any of the following calls:
78 - MatchFinder_SET_DIRECT_INPUT_BUF()
79 - MatchFinder_SET_STREAM()
80 - MatchFinder_SET_STREAM_MODE()
75*/ 81*/
82
83#define MatchFinder_SET_DIRECT_INPUT_BUF(p, _src_, _srcLen_) { \
84 (p)->stream = NULL; \
85 (p)->directInput = 1; \
86 (p)->buffer = (_src_); \
87 (p)->directInputRem = (_srcLen_); }
88
89/*
90#define MatchFinder_SET_STREAM_MODE(p) { \
91 (p)->directInput = 0; }
92*/
93
94#define MatchFinder_SET_STREAM(p, _stream_) { \
95 (p)->stream = _stream_; \
96 (p)->directInput = 0; }
97
98
76int MatchFinder_Create(CMatchFinder *p, UInt32 historySize, 99int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
77 UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, 100 UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
78 ISzAllocPtr alloc); 101 ISzAllocPtr alloc);
79void MatchFinder_Free(CMatchFinder *p, ISzAllocPtr alloc); 102void MatchFinder_Free(CMatchFinder *p, ISzAllocPtr alloc);
80void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems); 103void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems);
81// void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue);
82 104
83/* 105/*
84#define Inline_MatchFinder_InitPos(p, val) \ 106#define MatchFinder_INIT_POS(p, val) \
85 (p)->pos = (val); \ 107 (p)->pos = (val); \
86 (p)->streamPos = (val); 108 (p)->streamPos = (val);
87*/ 109*/
88 110
89#define Inline_MatchFinder_ReduceOffsets(p, subValue) \ 111// void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue);
112#define MatchFinder_REDUCE_OFFSETS(p, subValue) \
90 (p)->pos -= (subValue); \ 113 (p)->pos -= (subValue); \
91 (p)->streamPos -= (subValue); 114 (p)->streamPos -= (subValue);
92 115
@@ -107,7 +130,7 @@ typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object);
107typedef UInt32 * (*Mf_GetMatches_Func)(void *object, UInt32 *distances); 130typedef UInt32 * (*Mf_GetMatches_Func)(void *object, UInt32 *distances);
108typedef void (*Mf_Skip_Func)(void *object, UInt32); 131typedef void (*Mf_Skip_Func)(void *object, UInt32);
109 132
110typedef struct _IMatchFinder 133typedef struct
111{ 134{
112 Mf_Init_Func Init; 135 Mf_Init_Func Init;
113 Mf_GetNumAvailableBytes_Func GetNumAvailableBytes; 136 Mf_GetNumAvailableBytes_Func GetNumAvailableBytes;
diff --git a/C/LzFindMt.c b/C/LzFindMt.c
index 4e67fc3..5253e6e 100644
--- a/C/LzFindMt.c
+++ b/C/LzFindMt.c
@@ -1,5 +1,5 @@
1/* LzFindMt.c -- multithreaded Match finder for LZ algorithms 1/* LzFindMt.c -- multithreaded Match finder for LZ algorithms
22021-12-21 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -69,7 +69,7 @@ extern UInt64 g_NumIters_Bytes;
69 UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 69 UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
70 h3 = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); } 70 h3 = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); }
71 71
72#define __MT_HASH4_CALC { \ 72#define MT_HASH4_CALC { \
73 UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 73 UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
74 h2 = temp & (kHash2Size - 1); \ 74 h2 = temp & (kHash2Size - 1); \
75 temp ^= ((UInt32)cur[2] << 8); \ 75 temp ^= ((UInt32)cur[2] << 8); \
@@ -79,14 +79,14 @@ extern UInt64 g_NumIters_Bytes;
79*/ 79*/
80 80
81 81
82MY_NO_INLINE 82Z7_NO_INLINE
83static void MtSync_Construct(CMtSync *p) 83static void MtSync_Construct(CMtSync *p)
84{ 84{
85 p->affinity = 0; 85 p->affinity = 0;
86 p->wasCreated = False; 86 p->wasCreated = False;
87 p->csWasInitialized = False; 87 p->csWasInitialized = False;
88 p->csWasEntered = False; 88 p->csWasEntered = False;
89 Thread_Construct(&p->thread); 89 Thread_CONSTRUCT(&p->thread)
90 Event_Construct(&p->canStart); 90 Event_Construct(&p->canStart);
91 Event_Construct(&p->wasStopped); 91 Event_Construct(&p->wasStopped);
92 Semaphore_Construct(&p->freeSemaphore); 92 Semaphore_Construct(&p->freeSemaphore);
@@ -116,7 +116,7 @@ static void MtSync_Construct(CMtSync *p)
116 (p)->csWasEntered = False; } 116 (p)->csWasEntered = False; }
117 117
118 118
119MY_NO_INLINE 119Z7_NO_INLINE
120static UInt32 MtSync_GetNextBlock(CMtSync *p) 120static UInt32 MtSync_GetNextBlock(CMtSync *p)
121{ 121{
122 UInt32 numBlocks = 0; 122 UInt32 numBlocks = 0;
@@ -140,14 +140,14 @@ static UInt32 MtSync_GetNextBlock(CMtSync *p)
140 140
141 // buffer is UNLOCKED here 141 // buffer is UNLOCKED here
142 Semaphore_Wait(&p->filledSemaphore); 142 Semaphore_Wait(&p->filledSemaphore);
143 LOCK_BUFFER(p); 143 LOCK_BUFFER(p)
144 return numBlocks; 144 return numBlocks;
145} 145}
146 146
147 147
148/* if Writing (Processing) thread was started, we must call MtSync_StopWriting() */ 148/* if Writing (Processing) thread was started, we must call MtSync_StopWriting() */
149 149
150MY_NO_INLINE 150Z7_NO_INLINE
151static void MtSync_StopWriting(CMtSync *p) 151static void MtSync_StopWriting(CMtSync *p)
152{ 152{
153 if (!Thread_WasCreated(&p->thread) || p->needStart) 153 if (!Thread_WasCreated(&p->thread) || p->needStart)
@@ -185,7 +185,7 @@ static void MtSync_StopWriting(CMtSync *p)
185} 185}
186 186
187 187
188MY_NO_INLINE 188Z7_NO_INLINE
189static void MtSync_Destruct(CMtSync *p) 189static void MtSync_Destruct(CMtSync *p)
190{ 190{
191 PRF(printf("\nMtSync_Destruct %p\n", p)); 191 PRF(printf("\nMtSync_Destruct %p\n", p));
@@ -220,11 +220,11 @@ static void MtSync_Destruct(CMtSync *p)
220 220
221// #define RINOK_THREAD(x) { if ((x) != 0) return SZ_ERROR_THREAD; } 221// #define RINOK_THREAD(x) { if ((x) != 0) return SZ_ERROR_THREAD; }
222// we want to get real system error codes here instead of SZ_ERROR_THREAD 222// we want to get real system error codes here instead of SZ_ERROR_THREAD
223#define RINOK_THREAD(x) RINOK(x) 223#define RINOK_THREAD(x) RINOK_WRes(x)
224 224
225 225
226// call it before each new file (when new starting is required): 226// call it before each new file (when new starting is required):
227MY_NO_INLINE 227Z7_NO_INLINE
228static SRes MtSync_Init(CMtSync *p, UInt32 numBlocks) 228static SRes MtSync_Init(CMtSync *p, UInt32 numBlocks)
229{ 229{
230 WRes wres; 230 WRes wres;
@@ -245,12 +245,12 @@ static WRes MtSync_Create_WRes(CMtSync *p, THREAD_FUNC_TYPE startAddress, void *
245 if (p->wasCreated) 245 if (p->wasCreated)
246 return SZ_OK; 246 return SZ_OK;
247 247
248 RINOK_THREAD(CriticalSection_Init(&p->cs)); 248 RINOK_THREAD(CriticalSection_Init(&p->cs))
249 p->csWasInitialized = True; 249 p->csWasInitialized = True;
250 p->csWasEntered = False; 250 p->csWasEntered = False;
251 251
252 RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->canStart)); 252 RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->canStart))
253 RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->wasStopped)); 253 RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->wasStopped))
254 254
255 p->needStart = True; 255 p->needStart = True;
256 p->exit = True; /* p->exit is unused before (canStart) Event. 256 p->exit = True; /* p->exit is unused before (canStart) Event.
@@ -264,13 +264,13 @@ static WRes MtSync_Create_WRes(CMtSync *p, THREAD_FUNC_TYPE startAddress, void *
264 else 264 else
265 wres = Thread_Create(&p->thread, startAddress, obj); 265 wres = Thread_Create(&p->thread, startAddress, obj);
266 266
267 RINOK_THREAD(wres); 267 RINOK_THREAD(wres)
268 p->wasCreated = True; 268 p->wasCreated = True;
269 return SZ_OK; 269 return SZ_OK;
270} 270}
271 271
272 272
273MY_NO_INLINE 273Z7_NO_INLINE
274static SRes MtSync_Create(CMtSync *p, THREAD_FUNC_TYPE startAddress, void *obj) 274static SRes MtSync_Create(CMtSync *p, THREAD_FUNC_TYPE startAddress, void *obj)
275{ 275{
276 const WRes wres = MtSync_Create_WRes(p, startAddress, obj); 276 const WRes wres = MtSync_Create_WRes(p, startAddress, obj);
@@ -519,7 +519,7 @@ static void HashThreadFunc(CMatchFinderMt *mt)
519 if (mf->pos > (UInt32)kMtMaxValForNormalize - num) 519 if (mf->pos > (UInt32)kMtMaxValForNormalize - num)
520 { 520 {
521 const UInt32 subValue = (mf->pos - mf->historySize - 1); // & ~(UInt32)(kNormalizeAlign - 1); 521 const UInt32 subValue = (mf->pos - mf->historySize - 1); // & ~(UInt32)(kNormalizeAlign - 1);
522 Inline_MatchFinder_ReduceOffsets(mf, subValue); 522 MatchFinder_REDUCE_OFFSETS(mf, subValue)
523 MatchFinder_Normalize3(subValue, mf->hash + mf->fixedHashSize, (size_t)mf->hashMask + 1); 523 MatchFinder_Normalize3(subValue, mf->hash + mf->fixedHashSize, (size_t)mf->hashMask + 1);
524 } 524 }
525 525
@@ -560,7 +560,7 @@ static void HashThreadFunc(CMatchFinderMt *mt)
560*/ 560*/
561 561
562 562
563UInt32 * MY_FAST_CALL GetMatchesSpecN_2(const Byte *lenLimit, size_t pos, const Byte *cur, CLzRef *son, 563UInt32 * Z7_FASTCALL GetMatchesSpecN_2(const Byte *lenLimit, size_t pos, const Byte *cur, CLzRef *son,
564 UInt32 _cutValue, UInt32 *d, size_t _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size, 564 UInt32 _cutValue, UInt32 *d, size_t _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size,
565 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, 565 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize,
566 UInt32 *posRes); 566 UInt32 *posRes);
@@ -749,7 +749,7 @@ static void BtFillBlock(CMatchFinderMt *p, UInt32 globalBlockIndex)
749} 749}
750 750
751 751
752MY_NO_INLINE 752Z7_NO_INLINE
753static void BtThreadFunc(CMatchFinderMt *mt) 753static void BtThreadFunc(CMatchFinderMt *mt)
754{ 754{
755 CMtSync *p = &mt->btSync; 755 CMtSync *p = &mt->btSync;
@@ -864,15 +864,15 @@ SRes MatchFinderMt_Create(CMatchFinderMt *p, UInt32 historySize, UInt32 keepAddB
864 if (!MatchFinder_Create(mf, historySize, keepAddBufferBefore, matchMaxLen, keepAddBufferAfter, alloc)) 864 if (!MatchFinder_Create(mf, historySize, keepAddBufferBefore, matchMaxLen, keepAddBufferAfter, alloc))
865 return SZ_ERROR_MEM; 865 return SZ_ERROR_MEM;
866 866
867 RINOK(MtSync_Create(&p->hashSync, HashThreadFunc2, p)); 867 RINOK(MtSync_Create(&p->hashSync, HashThreadFunc2, p))
868 RINOK(MtSync_Create(&p->btSync, BtThreadFunc2, p)); 868 RINOK(MtSync_Create(&p->btSync, BtThreadFunc2, p))
869 return SZ_OK; 869 return SZ_OK;
870} 870}
871 871
872 872
873SRes MatchFinderMt_InitMt(CMatchFinderMt *p) 873SRes MatchFinderMt_InitMt(CMatchFinderMt *p)
874{ 874{
875 RINOK(MtSync_Init(&p->hashSync, kMtHashNumBlocks)); 875 RINOK(MtSync_Init(&p->hashSync, kMtHashNumBlocks))
876 return MtSync_Init(&p->btSync, kMtBtNumBlocks); 876 return MtSync_Init(&p->btSync, kMtBtNumBlocks);
877} 877}
878 878
@@ -941,7 +941,7 @@ void MatchFinderMt_ReleaseStream(CMatchFinderMt *p)
941} 941}
942 942
943 943
944MY_NO_INLINE 944Z7_NO_INLINE
945static UInt32 MatchFinderMt_GetNextBlock_Bt(CMatchFinderMt *p) 945static UInt32 MatchFinderMt_GetNextBlock_Bt(CMatchFinderMt *p)
946{ 946{
947 if (p->failure_LZ_BT) 947 if (p->failure_LZ_BT)
@@ -1163,7 +1163,7 @@ UInt32* MatchFinderMt_GetMatches_Bt4(CMatchFinderMt *p, UInt32 *d)
1163*/ 1163*/
1164 1164
1165 1165
1166static UInt32 *MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *d) 1166static UInt32 * MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *d)
1167{ 1167{
1168 UInt32 h2, h3, /* h4, */ c2, c3 /* , c4 */; 1168 UInt32 h2, h3, /* h4, */ c2, c3 /* , c4 */;
1169 UInt32 *hash = p->hash; 1169 UInt32 *hash = p->hash;
@@ -1179,9 +1179,8 @@ static UInt32 *MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *d)
1179 (hash + kFix3HashSize)[h3] = m; 1179 (hash + kFix3HashSize)[h3] = m;
1180 // (hash + kFix4HashSize)[h4] = m; 1180 // (hash + kFix4HashSize)[h4] = m;
1181 1181
1182 #define _USE_H2 1182 // #define BT5_USE_H2
1183 1183 // #ifdef BT5_USE_H2
1184 #ifdef _USE_H2
1185 if (c2 >= matchMinPos && cur[(ptrdiff_t)c2 - (ptrdiff_t)m] == cur[0]) 1184 if (c2 >= matchMinPos && cur[(ptrdiff_t)c2 - (ptrdiff_t)m] == cur[0])
1186 { 1185 {
1187 d[1] = m - c2 - 1; 1186 d[1] = m - c2 - 1;
@@ -1197,8 +1196,8 @@ static UInt32 *MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *d)
1197 } 1196 }
1198 d[0] = 3; 1197 d[0] = 3;
1199 d += 2; 1198 d += 2;
1200 1199
1201 #ifdef _USE_H4 1200 #ifdef BT5_USE_H4
1202 if (c4 >= matchMinPos) 1201 if (c4 >= matchMinPos)
1203 if ( 1202 if (
1204 cur[(ptrdiff_t)c4 - (ptrdiff_t)m] == cur[0] && 1203 cur[(ptrdiff_t)c4 - (ptrdiff_t)m] == cur[0] &&
@@ -1214,7 +1213,7 @@ static UInt32 *MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *d)
1214 d[0] = 2; 1213 d[0] = 2;
1215 d += 2; 1214 d += 2;
1216 } 1215 }
1217 #endif 1216 // #endif
1218 1217
1219 if (c3 >= matchMinPos && cur[(ptrdiff_t)c3 - (ptrdiff_t)m] == cur[0]) 1218 if (c3 >= matchMinPos && cur[(ptrdiff_t)c3 - (ptrdiff_t)m] == cur[0])
1220 { 1219 {
@@ -1228,7 +1227,7 @@ static UInt32 *MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *d)
1228 d += 2; 1227 d += 2;
1229 } 1228 }
1230 1229
1231 #ifdef _USE_H4 1230 #ifdef BT5_USE_H4
1232 if (c4 >= matchMinPos) 1231 if (c4 >= matchMinPos)
1233 if ( 1232 if (
1234 cur[(ptrdiff_t)c4 - (ptrdiff_t)m] == cur[0] && 1233 cur[(ptrdiff_t)c4 - (ptrdiff_t)m] == cur[0] &&
@@ -1244,7 +1243,7 @@ static UInt32 *MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *d)
1244} 1243}
1245 1244
1246 1245
1247static UInt32* MatchFinderMt2_GetMatches(CMatchFinderMt *p, UInt32 *d) 1246static UInt32 * MatchFinderMt2_GetMatches(CMatchFinderMt *p, UInt32 *d)
1248{ 1247{
1249 const UInt32 *bt = p->btBufPos; 1248 const UInt32 *bt = p->btBufPos;
1250 const UInt32 len = *bt++; 1249 const UInt32 len = *bt++;
@@ -1268,7 +1267,7 @@ static UInt32* MatchFinderMt2_GetMatches(CMatchFinderMt *p, UInt32 *d)
1268 1267
1269 1268
1270 1269
1271static UInt32* MatchFinderMt_GetMatches(CMatchFinderMt *p, UInt32 *d) 1270static UInt32 * MatchFinderMt_GetMatches(CMatchFinderMt *p, UInt32 *d)
1272{ 1271{
1273 const UInt32 *bt = p->btBufPos; 1272 const UInt32 *bt = p->btBufPos;
1274 UInt32 len = *bt++; 1273 UInt32 len = *bt++;
@@ -1398,3 +1397,10 @@ void MatchFinderMt_CreateVTable(CMatchFinderMt *p, IMatchFinder2 *vTable)
1398 break; 1397 break;
1399 } 1398 }
1400} 1399}
1400
1401#undef RINOK_THREAD
1402#undef PRF
1403#undef MF
1404#undef GetUi24hi_from32
1405#undef LOCK_BUFFER
1406#undef UNLOCK_BUFFER
diff --git a/C/LzFindMt.h b/C/LzFindMt.h
index 660b724..db5923e 100644
--- a/C/LzFindMt.h
+++ b/C/LzFindMt.h
@@ -1,15 +1,15 @@
1/* LzFindMt.h -- multithreaded Match finder for LZ algorithms 1/* LzFindMt.h -- multithreaded Match finder for LZ algorithms
22021-07-12 : Igor Pavlov : Public domain */ 22023-03-05 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZ_FIND_MT_H 4#ifndef ZIP7_INC_LZ_FIND_MT_H
5#define __LZ_FIND_MT_H 5#define ZIP7_INC_LZ_FIND_MT_H
6 6
7#include "LzFind.h" 7#include "LzFind.h"
8#include "Threads.h" 8#include "Threads.h"
9 9
10EXTERN_C_BEGIN 10EXTERN_C_BEGIN
11 11
12typedef struct _CMtSync 12typedef struct
13{ 13{
14 UInt32 numProcessedBlocks; 14 UInt32 numProcessedBlocks;
15 CThread thread; 15 CThread thread;
@@ -39,7 +39,7 @@ typedef UInt32 * (*Mf_Mix_Matches)(void *p, UInt32 matchMinPos, UInt32 *distance
39typedef void (*Mf_GetHeads)(const Byte *buffer, UInt32 pos, 39typedef void (*Mf_GetHeads)(const Byte *buffer, UInt32 pos,
40 UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc); 40 UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc);
41 41
42typedef struct _CMatchFinderMt 42typedef struct
43{ 43{
44 /* LZ */ 44 /* LZ */
45 const Byte *pointerToCurPos; 45 const Byte *pointerToCurPos;
diff --git a/C/LzFindOpt.c b/C/LzFindOpt.c
index 8ff006e..85bdc13 100644
--- a/C/LzFindOpt.c
+++ b/C/LzFindOpt.c
@@ -1,5 +1,5 @@
1/* LzFindOpt.c -- multithreaded Match finder for LZ algorithms 1/* LzFindOpt.c -- multithreaded Match finder for LZ algorithms
22021-07-13 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -41,8 +41,8 @@ UInt64 g_NumIters_Bytes;
41// #define CYC_TO_POS_OFFSET 1 // for debug 41// #define CYC_TO_POS_OFFSET 1 // for debug
42 42
43/* 43/*
44MY_NO_INLINE 44Z7_NO_INLINE
45UInt32 * MY_FAST_CALL GetMatchesSpecN_1(const Byte *lenLimit, size_t pos, const Byte *cur, CLzRef *son, 45UInt32 * Z7_FASTCALL GetMatchesSpecN_1(const Byte *lenLimit, size_t pos, const Byte *cur, CLzRef *son,
46 UInt32 _cutValue, UInt32 *d, size_t _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size, UInt32 *posRes) 46 UInt32 _cutValue, UInt32 *d, size_t _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size, UInt32 *posRes)
47{ 47{
48 do 48 do
@@ -214,13 +214,13 @@ else
214 to eliminate "movsx" BUG in old MSVC x64 compiler. 214 to eliminate "movsx" BUG in old MSVC x64 compiler.
215*/ 215*/
216 216
217UInt32 * MY_FAST_CALL GetMatchesSpecN_2(const Byte *lenLimit, size_t pos, const Byte *cur, CLzRef *son, 217UInt32 * Z7_FASTCALL GetMatchesSpecN_2(const Byte *lenLimit, size_t pos, const Byte *cur, CLzRef *son,
218 UInt32 _cutValue, UInt32 *d, size_t _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size, 218 UInt32 _cutValue, UInt32 *d, size_t _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size,
219 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, 219 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize,
220 UInt32 *posRes); 220 UInt32 *posRes);
221 221
222MY_NO_INLINE 222Z7_NO_INLINE
223UInt32 * MY_FAST_CALL GetMatchesSpecN_2(const Byte *lenLimit, size_t pos, const Byte *cur, CLzRef *son, 223UInt32 * Z7_FASTCALL GetMatchesSpecN_2(const Byte *lenLimit, size_t pos, const Byte *cur, CLzRef *son,
224 UInt32 _cutValue, UInt32 *d, size_t _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size, 224 UInt32 _cutValue, UInt32 *d, size_t _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size,
225 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, 225 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize,
226 UInt32 *posRes) 226 UInt32 *posRes)
@@ -404,7 +404,7 @@ else
404/* 404/*
405typedef UInt32 uint32plus; // size_t 405typedef UInt32 uint32plus; // size_t
406 406
407UInt32 * MY_FAST_CALL GetMatchesSpecN_3(uint32plus lenLimit, size_t pos, const Byte *cur, CLzRef *son, 407UInt32 * Z7_FASTCALL GetMatchesSpecN_3(uint32plus lenLimit, size_t pos, const Byte *cur, CLzRef *son,
408 UInt32 _cutValue, UInt32 *d, uint32plus _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size, 408 UInt32 _cutValue, UInt32 *d, uint32plus _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size,
409 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, 409 size_t _cyclicBufferPos, UInt32 _cyclicBufferSize,
410 UInt32 *posRes) 410 UInt32 *posRes)
diff --git a/C/LzHash.h b/C/LzHash.h
index 77b898c..2b6290b 100644
--- a/C/LzHash.h
+++ b/C/LzHash.h
@@ -1,8 +1,8 @@
1/* LzHash.h -- HASH functions for LZ algorithms 1/* LzHash.h -- HASH constants for LZ algorithms
22019-10-30 : Igor Pavlov : Public domain */ 22023-03-05 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZ_HASH_H 4#ifndef ZIP7_INC_LZ_HASH_H
5#define __LZ_HASH_H 5#define ZIP7_INC_LZ_HASH_H
6 6
7/* 7/*
8 (kHash2Size >= (1 << 8)) : Required 8 (kHash2Size >= (1 << 8)) : Required
diff --git a/C/Lzma2Dec.c b/C/Lzma2Dec.c
index ac970a8..388cbc7 100644
--- a/C/Lzma2Dec.c
+++ b/C/Lzma2Dec.c
@@ -1,5 +1,5 @@
1/* Lzma2Dec.c -- LZMA2 Decoder 1/* Lzma2Dec.c -- LZMA2 Decoder
22021-02-09 : Igor Pavlov : Public domain */ 22023-03-03 : Igor Pavlov : Public domain */
3 3
4/* #define SHOW_DEBUG_INFO */ 4/* #define SHOW_DEBUG_INFO */
5 5
@@ -71,14 +71,14 @@ static SRes Lzma2Dec_GetOldProps(Byte prop, Byte *props)
71SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc) 71SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc)
72{ 72{
73 Byte props[LZMA_PROPS_SIZE]; 73 Byte props[LZMA_PROPS_SIZE];
74 RINOK(Lzma2Dec_GetOldProps(prop, props)); 74 RINOK(Lzma2Dec_GetOldProps(prop, props))
75 return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc); 75 return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
76} 76}
77 77
78SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc) 78SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc)
79{ 79{
80 Byte props[LZMA_PROPS_SIZE]; 80 Byte props[LZMA_PROPS_SIZE];
81 RINOK(Lzma2Dec_GetOldProps(prop, props)); 81 RINOK(Lzma2Dec_GetOldProps(prop, props))
82 return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc); 82 return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
83} 83}
84 84
@@ -474,8 +474,8 @@ SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
474 SizeT outSize = *destLen, inSize = *srcLen; 474 SizeT outSize = *destLen, inSize = *srcLen;
475 *destLen = *srcLen = 0; 475 *destLen = *srcLen = 0;
476 *status = LZMA_STATUS_NOT_SPECIFIED; 476 *status = LZMA_STATUS_NOT_SPECIFIED;
477 Lzma2Dec_Construct(&p); 477 Lzma2Dec_CONSTRUCT(&p)
478 RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc)); 478 RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc))
479 p.decoder.dic = dest; 479 p.decoder.dic = dest;
480 p.decoder.dicBufSize = outSize; 480 p.decoder.dicBufSize = outSize;
481 Lzma2Dec_Init(&p); 481 Lzma2Dec_Init(&p);
@@ -487,3 +487,5 @@ SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
487 Lzma2Dec_FreeProbs(&p, alloc); 487 Lzma2Dec_FreeProbs(&p, alloc);
488 return res; 488 return res;
489} 489}
490
491#undef PRF
diff --git a/C/Lzma2Dec.h b/C/Lzma2Dec.h
index b8ddeac..1f5233a 100644
--- a/C/Lzma2Dec.h
+++ b/C/Lzma2Dec.h
@@ -1,8 +1,8 @@
1/* Lzma2Dec.h -- LZMA2 Decoder 1/* Lzma2Dec.h -- LZMA2 Decoder
22018-02-19 : Igor Pavlov : Public domain */ 22023-03-03 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZMA2_DEC_H 4#ifndef ZIP7_INC_LZMA2_DEC_H
5#define __LZMA2_DEC_H 5#define ZIP7_INC_LZMA2_DEC_H
6 6
7#include "LzmaDec.h" 7#include "LzmaDec.h"
8 8
@@ -22,9 +22,10 @@ typedef struct
22 CLzmaDec decoder; 22 CLzmaDec decoder;
23} CLzma2Dec; 23} CLzma2Dec;
24 24
25#define Lzma2Dec_Construct(p) LzmaDec_Construct(&(p)->decoder) 25#define Lzma2Dec_CONSTRUCT(p) LzmaDec_CONSTRUCT(&(p)->decoder)
26#define Lzma2Dec_FreeProbs(p, alloc) LzmaDec_FreeProbs(&(p)->decoder, alloc) 26#define Lzma2Dec_Construct(p) Lzma2Dec_CONSTRUCT(p)
27#define Lzma2Dec_Free(p, alloc) LzmaDec_Free(&(p)->decoder, alloc) 27#define Lzma2Dec_FreeProbs(p, alloc) LzmaDec_FreeProbs(&(p)->decoder, alloc)
28#define Lzma2Dec_Free(p, alloc) LzmaDec_Free(&(p)->decoder, alloc)
28 29
29SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc); 30SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc);
30SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc); 31SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc);
@@ -90,7 +91,7 @@ Lzma2Dec_GetUnpackExtra() returns the value that shows
90 at current input positon. 91 at current input positon.
91*/ 92*/
92 93
93#define Lzma2Dec_GetUnpackExtra(p) ((p)->isExtraMode ? (p)->unpackSize : 0); 94#define Lzma2Dec_GetUnpackExtra(p) ((p)->isExtraMode ? (p)->unpackSize : 0)
94 95
95 96
96/* ---------- One Call Interface ---------- */ 97/* ---------- One Call Interface ---------- */
diff --git a/C/Lzma2DecMt.c b/C/Lzma2DecMt.c
index 9f1dc52..4bc4dde 100644
--- a/C/Lzma2DecMt.c
+++ b/C/Lzma2DecMt.c
@@ -1,44 +1,44 @@
1/* Lzma2DecMt.c -- LZMA2 Decoder Multi-thread 1/* Lzma2DecMt.c -- LZMA2 Decoder Multi-thread
22021-04-01 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6// #define SHOW_DEBUG_INFO 6// #define SHOW_DEBUG_INFO
7 7// #define Z7_ST
8// #define _7ZIP_ST
9 8
10#ifdef SHOW_DEBUG_INFO 9#ifdef SHOW_DEBUG_INFO
11#include <stdio.h> 10#include <stdio.h>
12#endif 11#endif
13 12
14#ifndef _7ZIP_ST
15#ifdef SHOW_DEBUG_INFO
16#define PRF(x) x
17#else
18#define PRF(x)
19#endif
20#define PRF_STR(s) PRF(printf("\n" s "\n"))
21#define PRF_STR_INT_2(s, d1, d2) PRF(printf("\n" s " %d %d\n", (unsigned)d1, (unsigned)d2))
22#endif
23
24#include "Alloc.h" 13#include "Alloc.h"
25 14
26#include "Lzma2Dec.h" 15#include "Lzma2Dec.h"
27#include "Lzma2DecMt.h" 16#include "Lzma2DecMt.h"
28 17
29#ifndef _7ZIP_ST 18#ifndef Z7_ST
30#include "MtDec.h" 19#include "MtDec.h"
31 20
32#define LZMA2DECMT_OUT_BLOCK_MAX_DEFAULT (1 << 28) 21#define LZMA2DECMT_OUT_BLOCK_MAX_DEFAULT (1 << 28)
33#endif 22#endif
34 23
35 24
25#ifndef Z7_ST
26#ifdef SHOW_DEBUG_INFO
27#define PRF(x) x
28#else
29#define PRF(x)
30#endif
31#define PRF_STR(s) PRF(printf("\n" s "\n");)
32#define PRF_STR_INT_2(s, d1, d2) PRF(printf("\n" s " %d %d\n", (unsigned)d1, (unsigned)d2);)
33#endif
34
35
36void Lzma2DecMtProps_Init(CLzma2DecMtProps *p) 36void Lzma2DecMtProps_Init(CLzma2DecMtProps *p)
37{ 37{
38 p->inBufSize_ST = 1 << 20; 38 p->inBufSize_ST = 1 << 20;
39 p->outStep_ST = 1 << 20; 39 p->outStep_ST = 1 << 20;
40 40
41 #ifndef _7ZIP_ST 41 #ifndef Z7_ST
42 p->numThreads = 1; 42 p->numThreads = 1;
43 p->inBufSize_MT = 1 << 18; 43 p->inBufSize_MT = 1 << 18;
44 p->outBlockMax = LZMA2DECMT_OUT_BLOCK_MAX_DEFAULT; 44 p->outBlockMax = LZMA2DECMT_OUT_BLOCK_MAX_DEFAULT;
@@ -48,7 +48,7 @@ void Lzma2DecMtProps_Init(CLzma2DecMtProps *p)
48 48
49 49
50 50
51#ifndef _7ZIP_ST 51#ifndef Z7_ST
52 52
53/* ---------- CLzma2DecMtThread ---------- */ 53/* ---------- CLzma2DecMtThread ---------- */
54 54
@@ -81,7 +81,7 @@ typedef struct
81 81
82/* ---------- CLzma2DecMt ---------- */ 82/* ---------- CLzma2DecMt ---------- */
83 83
84typedef struct 84struct CLzma2DecMt
85{ 85{
86 // ISzAllocPtr alloc; 86 // ISzAllocPtr alloc;
87 ISzAllocPtr allocMid; 87 ISzAllocPtr allocMid;
@@ -90,9 +90,9 @@ typedef struct
90 CLzma2DecMtProps props; 90 CLzma2DecMtProps props;
91 Byte prop; 91 Byte prop;
92 92
93 ISeqInStream *inStream; 93 ISeqInStreamPtr inStream;
94 ISeqOutStream *outStream; 94 ISeqOutStreamPtr outStream;
95 ICompressProgress *progress; 95 ICompressProgressPtr progress;
96 96
97 BoolInt finishMode; 97 BoolInt finishMode;
98 BoolInt outSize_Defined; 98 BoolInt outSize_Defined;
@@ -111,14 +111,13 @@ typedef struct
111 size_t inPos; 111 size_t inPos;
112 size_t inLim; 112 size_t inLim;
113 113
114 #ifndef _7ZIP_ST 114 #ifndef Z7_ST
115 UInt64 outProcessed_Parse; 115 UInt64 outProcessed_Parse;
116 BoolInt mtc_WasConstructed; 116 BoolInt mtc_WasConstructed;
117 CMtDec mtc; 117 CMtDec mtc;
118 CLzma2DecMtThread coders[MTDEC__THREADS_MAX]; 118 CLzma2DecMtThread coders[MTDEC_THREADS_MAX];
119 #endif 119 #endif
120 120};
121} CLzma2DecMt;
122 121
123 122
124 123
@@ -142,11 +141,11 @@ CLzma2DecMtHandle Lzma2DecMt_Create(ISzAllocPtr alloc, ISzAllocPtr allocMid)
142 141
143 // Lzma2DecMtProps_Init(&p->props); 142 // Lzma2DecMtProps_Init(&p->props);
144 143
145 #ifndef _7ZIP_ST 144 #ifndef Z7_ST
146 p->mtc_WasConstructed = False; 145 p->mtc_WasConstructed = False;
147 { 146 {
148 unsigned i; 147 unsigned i;
149 for (i = 0; i < MTDEC__THREADS_MAX; i++) 148 for (i = 0; i < MTDEC_THREADS_MAX; i++)
150 { 149 {
151 CLzma2DecMtThread *t = &p->coders[i]; 150 CLzma2DecMtThread *t = &p->coders[i];
152 t->dec_created = False; 151 t->dec_created = False;
@@ -156,16 +155,16 @@ CLzma2DecMtHandle Lzma2DecMt_Create(ISzAllocPtr alloc, ISzAllocPtr allocMid)
156 } 155 }
157 #endif 156 #endif
158 157
159 return p; 158 return (CLzma2DecMtHandle)(void *)p;
160} 159}
161 160
162 161
163#ifndef _7ZIP_ST 162#ifndef Z7_ST
164 163
165static void Lzma2DecMt_FreeOutBufs(CLzma2DecMt *p) 164static void Lzma2DecMt_FreeOutBufs(CLzma2DecMt *p)
166{ 165{
167 unsigned i; 166 unsigned i;
168 for (i = 0; i < MTDEC__THREADS_MAX; i++) 167 for (i = 0; i < MTDEC_THREADS_MAX; i++)
169 { 168 {
170 CLzma2DecMtThread *t = &p->coders[i]; 169 CLzma2DecMtThread *t = &p->coders[i];
171 if (t->outBuf) 170 if (t->outBuf)
@@ -196,13 +195,15 @@ static void Lzma2DecMt_FreeSt(CLzma2DecMt *p)
196} 195}
197 196
198 197
199void Lzma2DecMt_Destroy(CLzma2DecMtHandle pp) 198// #define GET_CLzma2DecMt_p CLzma2DecMt *p = (CLzma2DecMt *)(void *)pp;
199
200void Lzma2DecMt_Destroy(CLzma2DecMtHandle p)
200{ 201{
201 CLzma2DecMt *p = (CLzma2DecMt *)pp; 202 // GET_CLzma2DecMt_p
202 203
203 Lzma2DecMt_FreeSt(p); 204 Lzma2DecMt_FreeSt(p);
204 205
205 #ifndef _7ZIP_ST 206 #ifndef Z7_ST
206 207
207 if (p->mtc_WasConstructed) 208 if (p->mtc_WasConstructed)
208 { 209 {
@@ -211,7 +212,7 @@ void Lzma2DecMt_Destroy(CLzma2DecMtHandle pp)
211 } 212 }
212 { 213 {
213 unsigned i; 214 unsigned i;
214 for (i = 0; i < MTDEC__THREADS_MAX; i++) 215 for (i = 0; i < MTDEC_THREADS_MAX; i++)
215 { 216 {
216 CLzma2DecMtThread *t = &p->coders[i]; 217 CLzma2DecMtThread *t = &p->coders[i];
217 if (t->dec_created) 218 if (t->dec_created)
@@ -226,19 +227,19 @@ void Lzma2DecMt_Destroy(CLzma2DecMtHandle pp)
226 227
227 #endif 228 #endif
228 229
229 ISzAlloc_Free(p->alignOffsetAlloc.baseAlloc, pp); 230 ISzAlloc_Free(p->alignOffsetAlloc.baseAlloc, p);
230} 231}
231 232
232 233
233 234
234#ifndef _7ZIP_ST 235#ifndef Z7_ST
235 236
236static void Lzma2DecMt_MtCallback_Parse(void *obj, unsigned coderIndex, CMtDecCallbackInfo *cc) 237static void Lzma2DecMt_MtCallback_Parse(void *obj, unsigned coderIndex, CMtDecCallbackInfo *cc)
237{ 238{
238 CLzma2DecMt *me = (CLzma2DecMt *)obj; 239 CLzma2DecMt *me = (CLzma2DecMt *)obj;
239 CLzma2DecMtThread *t = &me->coders[coderIndex]; 240 CLzma2DecMtThread *t = &me->coders[coderIndex];
240 241
241 PRF_STR_INT_2("Parse", coderIndex, cc->srcSize); 242 PRF_STR_INT_2("Parse", coderIndex, cc->srcSize)
242 243
243 cc->state = MTDEC_PARSE_CONTINUE; 244 cc->state = MTDEC_PARSE_CONTINUE;
244 245
@@ -246,7 +247,7 @@ static void Lzma2DecMt_MtCallback_Parse(void *obj, unsigned coderIndex, CMtDecCa
246 { 247 {
247 if (!t->dec_created) 248 if (!t->dec_created)
248 { 249 {
249 Lzma2Dec_Construct(&t->dec); 250 Lzma2Dec_CONSTRUCT(&t->dec)
250 t->dec_created = True; 251 t->dec_created = True;
251 AlignOffsetAlloc_CreateVTable(&t->alloc); 252 AlignOffsetAlloc_CreateVTable(&t->alloc);
252 { 253 {
@@ -297,7 +298,7 @@ static void Lzma2DecMt_MtCallback_Parse(void *obj, unsigned coderIndex, CMtDecCa
297 // that must be finished at position <= outBlockMax. 298 // that must be finished at position <= outBlockMax.
298 299
299 { 300 {
300 const SizeT srcOrig = cc->srcSize; 301 const size_t srcOrig = cc->srcSize;
301 SizeT srcSize_Point = 0; 302 SizeT srcSize_Point = 0;
302 SizeT dicPos_Point = 0; 303 SizeT dicPos_Point = 0;
303 304
@@ -306,10 +307,10 @@ static void Lzma2DecMt_MtCallback_Parse(void *obj, unsigned coderIndex, CMtDecCa
306 307
307 for (;;) 308 for (;;)
308 { 309 {
309 SizeT srcCur = srcOrig - cc->srcSize; 310 SizeT srcCur = (SizeT)(srcOrig - cc->srcSize);
310 311
311 status = Lzma2Dec_Parse(&t->dec, 312 status = Lzma2Dec_Parse(&t->dec,
312 limit - t->dec.decoder.dicPos, 313 (SizeT)limit - t->dec.decoder.dicPos,
313 cc->src + cc->srcSize, &srcCur, 314 cc->src + cc->srcSize, &srcCur,
314 checkFinishBlock); 315 checkFinishBlock);
315 316
@@ -333,7 +334,7 @@ static void Lzma2DecMt_MtCallback_Parse(void *obj, unsigned coderIndex, CMtDecCa
333 if (t->dec.decoder.dicPos >= (1 << 14)) 334 if (t->dec.decoder.dicPos >= (1 << 14))
334 break; 335 break;
335 dicPos_Point = t->dec.decoder.dicPos; 336 dicPos_Point = t->dec.decoder.dicPos;
336 srcSize_Point = cc->srcSize; 337 srcSize_Point = (SizeT)cc->srcSize;
337 continue; 338 continue;
338 } 339 }
339 340
@@ -391,7 +392,7 @@ static void Lzma2DecMt_MtCallback_Parse(void *obj, unsigned coderIndex, CMtDecCa
391 if (unpackRem != 0) 392 if (unpackRem != 0)
392 { 393 {
393 /* we also reserve space for max possible number of output bytes of current LZMA chunk */ 394 /* we also reserve space for max possible number of output bytes of current LZMA chunk */
394 SizeT rem = limit - dicPos; 395 size_t rem = limit - dicPos;
395 if (rem > unpackRem) 396 if (rem > unpackRem)
396 rem = unpackRem; 397 rem = unpackRem;
397 dicPos += rem; 398 dicPos += rem;
@@ -444,7 +445,7 @@ static SRes Lzma2DecMt_MtCallback_PreCode(void *pp, unsigned coderIndex)
444 } 445 }
445 446
446 t->dec.decoder.dic = dest; 447 t->dec.decoder.dic = dest;
447 t->dec.decoder.dicBufSize = t->outPreSize; 448 t->dec.decoder.dicBufSize = (SizeT)t->outPreSize;
448 449
449 t->needInit = True; 450 t->needInit = True;
450 451
@@ -462,7 +463,7 @@ static SRes Lzma2DecMt_MtCallback_Code(void *pp, unsigned coderIndex,
462 463
463 UNUSED_VAR(srcFinished) 464 UNUSED_VAR(srcFinished)
464 465
465 PRF_STR_INT_2("Code", coderIndex, srcSize); 466 PRF_STR_INT_2("Code", coderIndex, srcSize)
466 467
467 *inCodePos = t->inCodeSize; 468 *inCodePos = t->inCodeSize;
468 *outCodePos = 0; 469 *outCodePos = 0;
@@ -476,13 +477,13 @@ static SRes Lzma2DecMt_MtCallback_Code(void *pp, unsigned coderIndex,
476 477
477 { 478 {
478 ELzmaStatus status; 479 ELzmaStatus status;
479 size_t srcProcessed = srcSize; 480 SizeT srcProcessed = (SizeT)srcSize;
480 BoolInt blockWasFinished = 481 BoolInt blockWasFinished =
481 ((int)t->parseStatus == LZMA_STATUS_FINISHED_WITH_MARK 482 ((int)t->parseStatus == LZMA_STATUS_FINISHED_WITH_MARK
482 || t->parseStatus == LZMA2_PARSE_STATUS_NEW_BLOCK); 483 || t->parseStatus == LZMA2_PARSE_STATUS_NEW_BLOCK);
483 484
484 SRes res = Lzma2Dec_DecodeToDic(&t->dec, 485 SRes res = Lzma2Dec_DecodeToDic(&t->dec,
485 t->outPreSize, 486 (SizeT)t->outPreSize,
486 src, &srcProcessed, 487 src, &srcProcessed,
487 blockWasFinished ? LZMA_FINISH_END : LZMA_FINISH_ANY, 488 blockWasFinished ? LZMA_FINISH_END : LZMA_FINISH_ANY,
488 &status); 489 &status);
@@ -540,7 +541,7 @@ static SRes Lzma2DecMt_MtCallback_Write(void *pp, unsigned coderIndex,
540 UNUSED_VAR(srcSize) 541 UNUSED_VAR(srcSize)
541 UNUSED_VAR(isCross) 542 UNUSED_VAR(isCross)
542 543
543 PRF_STR_INT_2("Write", coderIndex, srcSize); 544 PRF_STR_INT_2("Write", coderIndex, srcSize)
544 545
545 *needContinue = False; 546 *needContinue = False;
546 *canRecode = True; 547 *canRecode = True;
@@ -588,7 +589,7 @@ static SRes Lzma2DecMt_MtCallback_Write(void *pp, unsigned coderIndex,
588 *needContinue = needContinue2; 589 *needContinue = needContinue2;
589 return SZ_OK; 590 return SZ_OK;
590 } 591 }
591 RINOK(MtProgress_ProgressAdd(&me->mtc.mtProgress, 0, 0)); 592 RINOK(MtProgress_ProgressAdd(&me->mtc.mtProgress, 0, 0))
592 } 593 }
593 } 594 }
594 595
@@ -611,11 +612,11 @@ static SRes Lzma2Dec_Prepare_ST(CLzma2DecMt *p)
611{ 612{
612 if (!p->dec_created) 613 if (!p->dec_created)
613 { 614 {
614 Lzma2Dec_Construct(&p->dec); 615 Lzma2Dec_CONSTRUCT(&p->dec)
615 p->dec_created = True; 616 p->dec_created = True;
616 } 617 }
617 618
618 RINOK(Lzma2Dec_Allocate(&p->dec, p->prop, &p->alignOffsetAlloc.vt)); 619 RINOK(Lzma2Dec_Allocate(&p->dec, p->prop, &p->alignOffsetAlloc.vt))
619 620
620 if (!p->inBuf || p->inBufSize != p->props.inBufSize_ST) 621 if (!p->inBuf || p->inBufSize != p->props.inBufSize_ST)
621 { 622 {
@@ -634,7 +635,7 @@ static SRes Lzma2Dec_Prepare_ST(CLzma2DecMt *p)
634 635
635 636
636static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p 637static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p
637 #ifndef _7ZIP_ST 638 #ifndef Z7_ST
638 , BoolInt tMode 639 , BoolInt tMode
639 #endif 640 #endif
640 ) 641 )
@@ -646,7 +647,7 @@ static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p
646 647
647 CLzma2Dec *dec; 648 CLzma2Dec *dec;
648 649
649 #ifndef _7ZIP_ST 650 #ifndef Z7_ST
650 if (tMode) 651 if (tMode)
651 { 652 {
652 Lzma2DecMt_FreeOutBufs(p); 653 Lzma2DecMt_FreeOutBufs(p);
@@ -654,7 +655,7 @@ static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p
654 } 655 }
655 #endif 656 #endif
656 657
657 RINOK(Lzma2Dec_Prepare_ST(p)); 658 RINOK(Lzma2Dec_Prepare_ST(p))
658 659
659 dec = &p->dec; 660 dec = &p->dec;
660 661
@@ -681,7 +682,7 @@ static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p
681 682
682 if (inPos == inLim) 683 if (inPos == inLim)
683 { 684 {
684 #ifndef _7ZIP_ST 685 #ifndef Z7_ST
685 if (tMode) 686 if (tMode)
686 { 687 {
687 inData = MtDec_Read(&p->mtc, &inLim); 688 inData = MtDec_Read(&p->mtc, &inLim);
@@ -710,7 +711,7 @@ static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p
710 { 711 {
711 SizeT next = dec->decoder.dicBufSize; 712 SizeT next = dec->decoder.dicBufSize;
712 if (next - wrPos > p->props.outStep_ST) 713 if (next - wrPos > p->props.outStep_ST)
713 next = wrPos + p->props.outStep_ST; 714 next = wrPos + (SizeT)p->props.outStep_ST;
714 size = next - dicPos; 715 size = next - dicPos;
715 } 716 }
716 717
@@ -726,7 +727,7 @@ static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p
726 } 727 }
727 } 728 }
728 729
729 inProcessed = inLim - inPos; 730 inProcessed = (SizeT)(inLim - inPos);
730 731
731 res = Lzma2Dec_DecodeToDic(dec, dicPos + size, inData + inPos, &inProcessed, finishMode, &status); 732 res = Lzma2Dec_DecodeToDic(dec, dicPos + size, inData + inPos, &inProcessed, finishMode, &status);
732 733
@@ -755,7 +756,7 @@ static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p
755 dec->decoder.dicPos = 0; 756 dec->decoder.dicPos = 0;
756 wrPos = dec->decoder.dicPos; 757 wrPos = dec->decoder.dicPos;
757 758
758 RINOK(res2); 759 RINOK(res2)
759 760
760 if (needStop) 761 if (needStop)
761 { 762 {
@@ -788,7 +789,7 @@ static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p
788 UInt64 outDelta = p->outProcessed - outPrev; 789 UInt64 outDelta = p->outProcessed - outPrev;
789 if (inDelta >= (1 << 22) || outDelta >= (1 << 22)) 790 if (inDelta >= (1 << 22) || outDelta >= (1 << 22))
790 { 791 {
791 RINOK(ICompressProgress_Progress(p->progress, p->inProcessed, p->outProcessed)); 792 RINOK(ICompressProgress_Progress(p->progress, p->inProcessed, p->outProcessed))
792 inPrev = p->inProcessed; 793 inPrev = p->inProcessed;
793 outPrev = p->outProcessed; 794 outPrev = p->outProcessed;
794 } 795 }
@@ -798,20 +799,20 @@ static SRes Lzma2Dec_Decode_ST(CLzma2DecMt *p
798 799
799 800
800 801
801SRes Lzma2DecMt_Decode(CLzma2DecMtHandle pp, 802SRes Lzma2DecMt_Decode(CLzma2DecMtHandle p,
802 Byte prop, 803 Byte prop,
803 const CLzma2DecMtProps *props, 804 const CLzma2DecMtProps *props,
804 ISeqOutStream *outStream, const UInt64 *outDataSize, int finishMode, 805 ISeqOutStreamPtr outStream, const UInt64 *outDataSize, int finishMode,
805 // Byte *outBuf, size_t *outBufSize, 806 // Byte *outBuf, size_t *outBufSize,
806 ISeqInStream *inStream, 807 ISeqInStreamPtr inStream,
807 // const Byte *inData, size_t inDataSize, 808 // const Byte *inData, size_t inDataSize,
808 UInt64 *inProcessed, 809 UInt64 *inProcessed,
809 // UInt64 *outProcessed, 810 // UInt64 *outProcessed,
810 int *isMT, 811 int *isMT,
811 ICompressProgress *progress) 812 ICompressProgressPtr progress)
812{ 813{
813 CLzma2DecMt *p = (CLzma2DecMt *)pp; 814 // GET_CLzma2DecMt_p
814 #ifndef _7ZIP_ST 815 #ifndef Z7_ST
815 BoolInt tMode; 816 BoolInt tMode;
816 #endif 817 #endif
817 818
@@ -845,7 +846,7 @@ SRes Lzma2DecMt_Decode(CLzma2DecMtHandle pp,
845 *isMT = False; 846 *isMT = False;
846 847
847 848
848 #ifndef _7ZIP_ST 849 #ifndef Z7_ST
849 850
850 tMode = False; 851 tMode = False;
851 852
@@ -939,7 +940,7 @@ SRes Lzma2DecMt_Decode(CLzma2DecMtHandle pp,
939 p->readWasFinished = p->mtc.readWasFinished; 940 p->readWasFinished = p->mtc.readWasFinished;
940 p->inProcessed = p->mtc.inProcessed; 941 p->inProcessed = p->mtc.inProcessed;
941 942
942 PRF_STR("----- decoding ST -----"); 943 PRF_STR("----- decoding ST -----")
943 } 944 }
944 } 945 }
945 946
@@ -950,7 +951,7 @@ SRes Lzma2DecMt_Decode(CLzma2DecMtHandle pp,
950 951
951 { 952 {
952 SRes res = Lzma2Dec_Decode_ST(p 953 SRes res = Lzma2Dec_Decode_ST(p
953 #ifndef _7ZIP_ST 954 #ifndef Z7_ST
954 , tMode 955 , tMode
955 #endif 956 #endif
956 ); 957 );
@@ -967,7 +968,7 @@ SRes Lzma2DecMt_Decode(CLzma2DecMtHandle pp,
967 res = p->readRes; 968 res = p->readRes;
968 969
969 /* 970 /*
970 #ifndef _7ZIP_ST 971 #ifndef Z7_ST
971 if (res == SZ_OK && tMode && p->mtc.parseRes != SZ_OK) 972 if (res == SZ_OK && tMode && p->mtc.parseRes != SZ_OK)
972 res = p->mtc.parseRes; 973 res = p->mtc.parseRes;
973 #endif 974 #endif
@@ -980,13 +981,13 @@ SRes Lzma2DecMt_Decode(CLzma2DecMtHandle pp,
980 981
981/* ---------- Read from CLzma2DecMtHandle Interface ---------- */ 982/* ---------- Read from CLzma2DecMtHandle Interface ---------- */
982 983
983SRes Lzma2DecMt_Init(CLzma2DecMtHandle pp, 984SRes Lzma2DecMt_Init(CLzma2DecMtHandle p,
984 Byte prop, 985 Byte prop,
985 const CLzma2DecMtProps *props, 986 const CLzma2DecMtProps *props,
986 const UInt64 *outDataSize, int finishMode, 987 const UInt64 *outDataSize, int finishMode,
987 ISeqInStream *inStream) 988 ISeqInStreamPtr inStream)
988{ 989{
989 CLzma2DecMt *p = (CLzma2DecMt *)pp; 990 // GET_CLzma2DecMt_p
990 991
991 if (prop > 40) 992 if (prop > 40)
992 return SZ_ERROR_UNSUPPORTED; 993 return SZ_ERROR_UNSUPPORTED;
@@ -1015,11 +1016,11 @@ SRes Lzma2DecMt_Init(CLzma2DecMtHandle pp,
1015} 1016}
1016 1017
1017 1018
1018SRes Lzma2DecMt_Read(CLzma2DecMtHandle pp, 1019SRes Lzma2DecMt_Read(CLzma2DecMtHandle p,
1019 Byte *data, size_t *outSize, 1020 Byte *data, size_t *outSize,
1020 UInt64 *inStreamProcessed) 1021 UInt64 *inStreamProcessed)
1021{ 1022{
1022 CLzma2DecMt *p = (CLzma2DecMt *)pp; 1023 // GET_CLzma2DecMt_p
1023 ELzmaFinishMode finishMode; 1024 ELzmaFinishMode finishMode;
1024 SRes readRes; 1025 SRes readRes;
1025 size_t size = *outSize; 1026 size_t size = *outSize;
@@ -1055,8 +1056,8 @@ SRes Lzma2DecMt_Read(CLzma2DecMtHandle pp,
1055 readRes = ISeqInStream_Read(p->inStream, p->inBuf, &p->inLim); 1056 readRes = ISeqInStream_Read(p->inStream, p->inBuf, &p->inLim);
1056 } 1057 }
1057 1058
1058 inCur = p->inLim - p->inPos; 1059 inCur = (SizeT)(p->inLim - p->inPos);
1059 outCur = size; 1060 outCur = (SizeT)size;
1060 1061
1061 res = Lzma2Dec_DecodeToBuf(&p->dec, data, &outCur, 1062 res = Lzma2Dec_DecodeToBuf(&p->dec, data, &outCur,
1062 p->inBuf + p->inPos, &inCur, finishMode, &status); 1063 p->inBuf + p->inPos, &inCur, finishMode, &status);
@@ -1088,3 +1089,7 @@ SRes Lzma2DecMt_Read(CLzma2DecMtHandle pp,
1088 return readRes; 1089 return readRes;
1089 } 1090 }
1090} 1091}
1092
1093#undef PRF
1094#undef PRF_STR
1095#undef PRF_STR_INT_2
diff --git a/C/Lzma2DecMt.h b/C/Lzma2DecMt.h
index 7791c31..93a5cd5 100644
--- a/C/Lzma2DecMt.h
+++ b/C/Lzma2DecMt.h
@@ -1,8 +1,8 @@
1/* Lzma2DecMt.h -- LZMA2 Decoder Multi-thread 1/* Lzma2DecMt.h -- LZMA2 Decoder Multi-thread
22018-02-17 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZMA2_DEC_MT_H 4#ifndef ZIP7_INC_LZMA2_DEC_MT_H
5#define __LZMA2_DEC_MT_H 5#define ZIP7_INC_LZMA2_DEC_MT_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -13,7 +13,7 @@ typedef struct
13 size_t inBufSize_ST; 13 size_t inBufSize_ST;
14 size_t outStep_ST; 14 size_t outStep_ST;
15 15
16 #ifndef _7ZIP_ST 16 #ifndef Z7_ST
17 unsigned numThreads; 17 unsigned numThreads;
18 size_t inBufSize_MT; 18 size_t inBufSize_MT;
19 size_t outBlockMax; 19 size_t outBlockMax;
@@ -38,7 +38,9 @@ SRes:
38 SZ_ERROR_THREAD - error in multithreading functions (only for Mt version) 38 SZ_ERROR_THREAD - error in multithreading functions (only for Mt version)
39*/ 39*/
40 40
41typedef void * CLzma2DecMtHandle; 41typedef struct CLzma2DecMt CLzma2DecMt;
42typedef CLzma2DecMt * CLzma2DecMtHandle;
43// Z7_DECLARE_HANDLE(CLzma2DecMtHandle)
42 44
43CLzma2DecMtHandle Lzma2DecMt_Create(ISzAllocPtr alloc, ISzAllocPtr allocMid); 45CLzma2DecMtHandle Lzma2DecMt_Create(ISzAllocPtr alloc, ISzAllocPtr allocMid);
44void Lzma2DecMt_Destroy(CLzma2DecMtHandle p); 46void Lzma2DecMt_Destroy(CLzma2DecMtHandle p);
@@ -46,11 +48,11 @@ void Lzma2DecMt_Destroy(CLzma2DecMtHandle p);
46SRes Lzma2DecMt_Decode(CLzma2DecMtHandle p, 48SRes Lzma2DecMt_Decode(CLzma2DecMtHandle p,
47 Byte prop, 49 Byte prop,
48 const CLzma2DecMtProps *props, 50 const CLzma2DecMtProps *props,
49 ISeqOutStream *outStream, 51 ISeqOutStreamPtr outStream,
50 const UInt64 *outDataSize, // NULL means undefined 52 const UInt64 *outDataSize, // NULL means undefined
51 int finishMode, // 0 - partial unpacking is allowed, 1 - if lzma2 stream must be finished 53 int finishMode, // 0 - partial unpacking is allowed, 1 - if lzma2 stream must be finished
52 // Byte *outBuf, size_t *outBufSize, 54 // Byte *outBuf, size_t *outBufSize,
53 ISeqInStream *inStream, 55 ISeqInStreamPtr inStream,
54 // const Byte *inData, size_t inDataSize, 56 // const Byte *inData, size_t inDataSize,
55 57
56 // out variables: 58 // out variables:
@@ -58,7 +60,7 @@ SRes Lzma2DecMt_Decode(CLzma2DecMtHandle p,
58 int *isMT, /* out: (*isMT == 0), if single thread decoding was used */ 60 int *isMT, /* out: (*isMT == 0), if single thread decoding was used */
59 61
60 // UInt64 *outProcessed, 62 // UInt64 *outProcessed,
61 ICompressProgress *progress); 63 ICompressProgressPtr progress);
62 64
63 65
64/* ---------- Read from CLzma2DecMtHandle Interface ---------- */ 66/* ---------- Read from CLzma2DecMtHandle Interface ---------- */
@@ -67,7 +69,7 @@ SRes Lzma2DecMt_Init(CLzma2DecMtHandle pp,
67 Byte prop, 69 Byte prop,
68 const CLzma2DecMtProps *props, 70 const CLzma2DecMtProps *props,
69 const UInt64 *outDataSize, int finishMode, 71 const UInt64 *outDataSize, int finishMode,
70 ISeqInStream *inStream); 72 ISeqInStreamPtr inStream);
71 73
72SRes Lzma2DecMt_Read(CLzma2DecMtHandle pp, 74SRes Lzma2DecMt_Read(CLzma2DecMtHandle pp,
73 Byte *data, size_t *outSize, 75 Byte *data, size_t *outSize,
diff --git a/C/Lzma2Enc.c b/C/Lzma2Enc.c
index e61a5df..703e146 100644
--- a/C/Lzma2Enc.c
+++ b/C/Lzma2Enc.c
@@ -1,18 +1,18 @@
1/* Lzma2Enc.c -- LZMA2 Encoder 1/* Lzma2Enc.c -- LZMA2 Encoder
22021-02-09 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#include <string.h> 6#include <string.h>
7 7
8/* #define _7ZIP_ST */ 8/* #define Z7_ST */
9 9
10#include "Lzma2Enc.h" 10#include "Lzma2Enc.h"
11 11
12#ifndef _7ZIP_ST 12#ifndef Z7_ST
13#include "MtCoder.h" 13#include "MtCoder.h"
14#else 14#else
15#define MTCODER__THREADS_MAX 1 15#define MTCODER_THREADS_MAX 1
16#endif 16#endif
17 17
18#define LZMA2_CONTROL_LZMA (1 << 7) 18#define LZMA2_CONTROL_LZMA (1 << 7)
@@ -40,7 +40,7 @@
40typedef struct 40typedef struct
41{ 41{
42 ISeqInStream vt; 42 ISeqInStream vt;
43 ISeqInStream *realStream; 43 ISeqInStreamPtr realStream;
44 UInt64 limit; 44 UInt64 limit;
45 UInt64 processed; 45 UInt64 processed;
46 int finished; 46 int finished;
@@ -53,15 +53,15 @@ static void LimitedSeqInStream_Init(CLimitedSeqInStream *p)
53 p->finished = 0; 53 p->finished = 0;
54} 54}
55 55
56static SRes LimitedSeqInStream_Read(const ISeqInStream *pp, void *data, size_t *size) 56static SRes LimitedSeqInStream_Read(ISeqInStreamPtr pp, void *data, size_t *size)
57{ 57{
58 CLimitedSeqInStream *p = CONTAINER_FROM_VTBL(pp, CLimitedSeqInStream, vt); 58 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CLimitedSeqInStream)
59 size_t size2 = *size; 59 size_t size2 = *size;
60 SRes res = SZ_OK; 60 SRes res = SZ_OK;
61 61
62 if (p->limit != (UInt64)(Int64)-1) 62 if (p->limit != (UInt64)(Int64)-1)
63 { 63 {
64 UInt64 rem = p->limit - p->processed; 64 const UInt64 rem = p->limit - p->processed;
65 if (size2 > rem) 65 if (size2 > rem)
66 size2 = (size_t)rem; 66 size2 = (size_t)rem;
67 } 67 }
@@ -95,8 +95,8 @@ static SRes Lzma2EncInt_InitStream(CLzma2EncInt *p, const CLzma2EncProps *props)
95 { 95 {
96 SizeT propsSize = LZMA_PROPS_SIZE; 96 SizeT propsSize = LZMA_PROPS_SIZE;
97 Byte propsEncoded[LZMA_PROPS_SIZE]; 97 Byte propsEncoded[LZMA_PROPS_SIZE];
98 RINOK(LzmaEnc_SetProps(p->enc, &props->lzmaProps)); 98 RINOK(LzmaEnc_SetProps(p->enc, &props->lzmaProps))
99 RINOK(LzmaEnc_WriteProperties(p->enc, propsEncoded, &propsSize)); 99 RINOK(LzmaEnc_WriteProperties(p->enc, propsEncoded, &propsSize))
100 p->propsByte = propsEncoded[0]; 100 p->propsByte = propsEncoded[0];
101 p->propsAreSet = True; 101 p->propsAreSet = True;
102 } 102 }
@@ -111,23 +111,23 @@ static void Lzma2EncInt_InitBlock(CLzma2EncInt *p)
111} 111}
112 112
113 113
114SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, ISeqInStream *inStream, UInt32 keepWindowSize, 114SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle p, ISeqInStreamPtr inStream, UInt32 keepWindowSize,
115 ISzAllocPtr alloc, ISzAllocPtr allocBig); 115 ISzAllocPtr alloc, ISzAllocPtr allocBig);
116SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen, 116SRes LzmaEnc_MemPrepare(CLzmaEncHandle p, const Byte *src, SizeT srcLen,
117 UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig); 117 UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig);
118SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, BoolInt reInit, 118SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle p, BoolInt reInit,
119 Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize); 119 Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize);
120const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp); 120const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle p);
121void LzmaEnc_Finish(CLzmaEncHandle pp); 121void LzmaEnc_Finish(CLzmaEncHandle p);
122void LzmaEnc_SaveState(CLzmaEncHandle pp); 122void LzmaEnc_SaveState(CLzmaEncHandle p);
123void LzmaEnc_RestoreState(CLzmaEncHandle pp); 123void LzmaEnc_RestoreState(CLzmaEncHandle p);
124 124
125/* 125/*
126UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp); 126UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle p);
127*/ 127*/
128 128
129static SRes Lzma2EncInt_EncodeSubblock(CLzma2EncInt *p, Byte *outBuf, 129static SRes Lzma2EncInt_EncodeSubblock(CLzma2EncInt *p, Byte *outBuf,
130 size_t *packSizeRes, ISeqOutStream *outStream) 130 size_t *packSizeRes, ISeqOutStreamPtr outStream)
131{ 131{
132 size_t packSizeLimit = *packSizeRes; 132 size_t packSizeLimit = *packSizeRes;
133 size_t packSize = packSizeLimit; 133 size_t packSize = packSizeLimit;
@@ -167,7 +167,7 @@ static SRes Lzma2EncInt_EncodeSubblock(CLzma2EncInt *p, Byte *outBuf,
167 167
168 while (unpackSize > 0) 168 while (unpackSize > 0)
169 { 169 {
170 UInt32 u = (unpackSize < LZMA2_COPY_CHUNK_SIZE) ? unpackSize : LZMA2_COPY_CHUNK_SIZE; 170 const UInt32 u = (unpackSize < LZMA2_COPY_CHUNK_SIZE) ? unpackSize : LZMA2_COPY_CHUNK_SIZE;
171 if (packSizeLimit - destPos < u + 3) 171 if (packSizeLimit - destPos < u + 3)
172 return SZ_ERROR_OUTPUT_EOF; 172 return SZ_ERROR_OUTPUT_EOF;
173 outBuf[destPos++] = (Byte)(p->srcPos == 0 ? LZMA2_CONTROL_COPY_RESET_DIC : LZMA2_CONTROL_COPY_NO_RESET); 173 outBuf[destPos++] = (Byte)(p->srcPos == 0 ? LZMA2_CONTROL_COPY_RESET_DIC : LZMA2_CONTROL_COPY_NO_RESET);
@@ -196,9 +196,9 @@ static SRes Lzma2EncInt_EncodeSubblock(CLzma2EncInt *p, Byte *outBuf,
196 196
197 { 197 {
198 size_t destPos = 0; 198 size_t destPos = 0;
199 UInt32 u = unpackSize - 1; 199 const UInt32 u = unpackSize - 1;
200 UInt32 pm = (UInt32)(packSize - 1); 200 const UInt32 pm = (UInt32)(packSize - 1);
201 unsigned mode = (p->srcPos == 0) ? 3 : (p->needInitState ? (p->needInitProp ? 2 : 1) : 0); 201 const unsigned mode = (p->srcPos == 0) ? 3 : (p->needInitState ? (p->needInitProp ? 2 : 1) : 0);
202 202
203 PRF(printf(" ")); 203 PRF(printf(" "));
204 204
@@ -231,7 +231,7 @@ static SRes Lzma2EncInt_EncodeSubblock(CLzma2EncInt *p, Byte *outBuf,
231void Lzma2EncProps_Init(CLzma2EncProps *p) 231void Lzma2EncProps_Init(CLzma2EncProps *p)
232{ 232{
233 LzmaEncProps_Init(&p->lzmaProps); 233 LzmaEncProps_Init(&p->lzmaProps);
234 p->blockSize = LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO; 234 p->blockSize = LZMA2_ENC_PROPS_BLOCK_SIZE_AUTO;
235 p->numBlockThreads_Reduced = -1; 235 p->numBlockThreads_Reduced = -1;
236 p->numBlockThreads_Max = -1; 236 p->numBlockThreads_Max = -1;
237 p->numTotalThreads = -1; 237 p->numTotalThreads = -1;
@@ -251,8 +251,8 @@ void Lzma2EncProps_Normalize(CLzma2EncProps *p)
251 t2 = p->numBlockThreads_Max; 251 t2 = p->numBlockThreads_Max;
252 t3 = p->numTotalThreads; 252 t3 = p->numTotalThreads;
253 253
254 if (t2 > MTCODER__THREADS_MAX) 254 if (t2 > MTCODER_THREADS_MAX)
255 t2 = MTCODER__THREADS_MAX; 255 t2 = MTCODER_THREADS_MAX;
256 256
257 if (t3 <= 0) 257 if (t3 <= 0)
258 { 258 {
@@ -268,8 +268,8 @@ void Lzma2EncProps_Normalize(CLzma2EncProps *p)
268 t1 = 1; 268 t1 = 1;
269 t2 = t3; 269 t2 = t3;
270 } 270 }
271 if (t2 > MTCODER__THREADS_MAX) 271 if (t2 > MTCODER_THREADS_MAX)
272 t2 = MTCODER__THREADS_MAX; 272 t2 = MTCODER_THREADS_MAX;
273 } 273 }
274 else if (t1 <= 0) 274 else if (t1 <= 0)
275 { 275 {
@@ -286,8 +286,8 @@ void Lzma2EncProps_Normalize(CLzma2EncProps *p)
286 286
287 fileSize = p->lzmaProps.reduceSize; 287 fileSize = p->lzmaProps.reduceSize;
288 288
289 if ( p->blockSize != LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID 289 if ( p->blockSize != LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID
290 && p->blockSize != LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO 290 && p->blockSize != LZMA2_ENC_PROPS_BLOCK_SIZE_AUTO
291 && (p->blockSize < fileSize || fileSize == (UInt64)(Int64)-1)) 291 && (p->blockSize < fileSize || fileSize == (UInt64)(Int64)-1))
292 p->lzmaProps.reduceSize = p->blockSize; 292 p->lzmaProps.reduceSize = p->blockSize;
293 293
@@ -297,19 +297,19 @@ void Lzma2EncProps_Normalize(CLzma2EncProps *p)
297 297
298 t1 = p->lzmaProps.numThreads; 298 t1 = p->lzmaProps.numThreads;
299 299
300 if (p->blockSize == LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID) 300 if (p->blockSize == LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID)
301 { 301 {
302 t2r = t2 = 1; 302 t2r = t2 = 1;
303 t3 = t1; 303 t3 = t1;
304 } 304 }
305 else if (p->blockSize == LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO && t2 <= 1) 305 else if (p->blockSize == LZMA2_ENC_PROPS_BLOCK_SIZE_AUTO && t2 <= 1)
306 { 306 {
307 /* if there is no block multi-threading, we use SOLID block */ 307 /* if there is no block multi-threading, we use SOLID block */
308 p->blockSize = LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID; 308 p->blockSize = LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID;
309 } 309 }
310 else 310 else
311 { 311 {
312 if (p->blockSize == LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO) 312 if (p->blockSize == LZMA2_ENC_PROPS_BLOCK_SIZE_AUTO)
313 { 313 {
314 const UInt32 kMinSize = (UInt32)1 << 20; 314 const UInt32 kMinSize = (UInt32)1 << 20;
315 const UInt32 kMaxSize = (UInt32)1 << 28; 315 const UInt32 kMaxSize = (UInt32)1 << 28;
@@ -344,7 +344,7 @@ void Lzma2EncProps_Normalize(CLzma2EncProps *p)
344} 344}
345 345
346 346
347static SRes Progress(ICompressProgress *p, UInt64 inSize, UInt64 outSize) 347static SRes Progress(ICompressProgressPtr p, UInt64 inSize, UInt64 outSize)
348{ 348{
349 return (p && ICompressProgress_Progress(p, inSize, outSize) != SZ_OK) ? SZ_ERROR_PROGRESS : SZ_OK; 349 return (p && ICompressProgress_Progress(p, inSize, outSize) != SZ_OK) ? SZ_ERROR_PROGRESS : SZ_OK;
350} 350}
@@ -352,7 +352,7 @@ static SRes Progress(ICompressProgress *p, UInt64 inSize, UInt64 outSize)
352 352
353/* ---------- Lzma2 ---------- */ 353/* ---------- Lzma2 ---------- */
354 354
355typedef struct 355struct CLzma2Enc
356{ 356{
357 Byte propEncoded; 357 Byte propEncoded;
358 CLzma2EncProps props; 358 CLzma2EncProps props;
@@ -363,23 +363,22 @@ typedef struct
363 ISzAllocPtr alloc; 363 ISzAllocPtr alloc;
364 ISzAllocPtr allocBig; 364 ISzAllocPtr allocBig;
365 365
366 CLzma2EncInt coders[MTCODER__THREADS_MAX]; 366 CLzma2EncInt coders[MTCODER_THREADS_MAX];
367 367
368 #ifndef _7ZIP_ST 368 #ifndef Z7_ST
369 369
370 ISeqOutStream *outStream; 370 ISeqOutStreamPtr outStream;
371 Byte *outBuf; 371 Byte *outBuf;
372 size_t outBuf_Rem; /* remainder in outBuf */ 372 size_t outBuf_Rem; /* remainder in outBuf */
373 373
374 size_t outBufSize; /* size of allocated outBufs[i] */ 374 size_t outBufSize; /* size of allocated outBufs[i] */
375 size_t outBufsDataSizes[MTCODER__BLOCKS_MAX]; 375 size_t outBufsDataSizes[MTCODER_BLOCKS_MAX];
376 BoolInt mtCoder_WasConstructed; 376 BoolInt mtCoder_WasConstructed;
377 CMtCoder mtCoder; 377 CMtCoder mtCoder;
378 Byte *outBufs[MTCODER__BLOCKS_MAX]; 378 Byte *outBufs[MTCODER_BLOCKS_MAX];
379 379
380 #endif 380 #endif
381 381};
382} CLzma2Enc;
383 382
384 383
385 384
@@ -396,30 +395,30 @@ CLzma2EncHandle Lzma2Enc_Create(ISzAllocPtr alloc, ISzAllocPtr allocBig)
396 p->allocBig = allocBig; 395 p->allocBig = allocBig;
397 { 396 {
398 unsigned i; 397 unsigned i;
399 for (i = 0; i < MTCODER__THREADS_MAX; i++) 398 for (i = 0; i < MTCODER_THREADS_MAX; i++)
400 p->coders[i].enc = NULL; 399 p->coders[i].enc = NULL;
401 } 400 }
402 401
403 #ifndef _7ZIP_ST 402 #ifndef Z7_ST
404 p->mtCoder_WasConstructed = False; 403 p->mtCoder_WasConstructed = False;
405 { 404 {
406 unsigned i; 405 unsigned i;
407 for (i = 0; i < MTCODER__BLOCKS_MAX; i++) 406 for (i = 0; i < MTCODER_BLOCKS_MAX; i++)
408 p->outBufs[i] = NULL; 407 p->outBufs[i] = NULL;
409 p->outBufSize = 0; 408 p->outBufSize = 0;
410 } 409 }
411 #endif 410 #endif
412 411
413 return p; 412 return (CLzma2EncHandle)p;
414} 413}
415 414
416 415
417#ifndef _7ZIP_ST 416#ifndef Z7_ST
418 417
419static void Lzma2Enc_FreeOutBufs(CLzma2Enc *p) 418static void Lzma2Enc_FreeOutBufs(CLzma2Enc *p)
420{ 419{
421 unsigned i; 420 unsigned i;
422 for (i = 0; i < MTCODER__BLOCKS_MAX; i++) 421 for (i = 0; i < MTCODER_BLOCKS_MAX; i++)
423 if (p->outBufs[i]) 422 if (p->outBufs[i])
424 { 423 {
425 ISzAlloc_Free(p->alloc, p->outBufs[i]); 424 ISzAlloc_Free(p->alloc, p->outBufs[i]);
@@ -430,12 +429,13 @@ static void Lzma2Enc_FreeOutBufs(CLzma2Enc *p)
430 429
431#endif 430#endif
432 431
432// #define GET_CLzma2Enc_p CLzma2Enc *p = (CLzma2Enc *)(void *)p;
433 433
434void Lzma2Enc_Destroy(CLzma2EncHandle pp) 434void Lzma2Enc_Destroy(CLzma2EncHandle p)
435{ 435{
436 CLzma2Enc *p = (CLzma2Enc *)pp; 436 // GET_CLzma2Enc_p
437 unsigned i; 437 unsigned i;
438 for (i = 0; i < MTCODER__THREADS_MAX; i++) 438 for (i = 0; i < MTCODER_THREADS_MAX; i++)
439 { 439 {
440 CLzma2EncInt *t = &p->coders[i]; 440 CLzma2EncInt *t = &p->coders[i];
441 if (t->enc) 441 if (t->enc)
@@ -446,7 +446,7 @@ void Lzma2Enc_Destroy(CLzma2EncHandle pp)
446 } 446 }
447 447
448 448
449 #ifndef _7ZIP_ST 449 #ifndef Z7_ST
450 if (p->mtCoder_WasConstructed) 450 if (p->mtCoder_WasConstructed)
451 { 451 {
452 MtCoder_Destruct(&p->mtCoder); 452 MtCoder_Destruct(&p->mtCoder);
@@ -458,13 +458,13 @@ void Lzma2Enc_Destroy(CLzma2EncHandle pp)
458 ISzAlloc_Free(p->alloc, p->tempBufLzma); 458 ISzAlloc_Free(p->alloc, p->tempBufLzma);
459 p->tempBufLzma = NULL; 459 p->tempBufLzma = NULL;
460 460
461 ISzAlloc_Free(p->alloc, pp); 461 ISzAlloc_Free(p->alloc, p);
462} 462}
463 463
464 464
465SRes Lzma2Enc_SetProps(CLzma2EncHandle pp, const CLzma2EncProps *props) 465SRes Lzma2Enc_SetProps(CLzma2EncHandle p, const CLzma2EncProps *props)
466{ 466{
467 CLzma2Enc *p = (CLzma2Enc *)pp; 467 // GET_CLzma2Enc_p
468 CLzmaEncProps lzmaProps = props->lzmaProps; 468 CLzmaEncProps lzmaProps = props->lzmaProps;
469 LzmaEncProps_Normalize(&lzmaProps); 469 LzmaEncProps_Normalize(&lzmaProps);
470 if (lzmaProps.lc + lzmaProps.lp > LZMA2_LCLP_MAX) 470 if (lzmaProps.lc + lzmaProps.lp > LZMA2_LCLP_MAX)
@@ -475,16 +475,16 @@ SRes Lzma2Enc_SetProps(CLzma2EncHandle pp, const CLzma2EncProps *props)
475} 475}
476 476
477 477
478void Lzma2Enc_SetDataSize(CLzmaEncHandle pp, UInt64 expectedDataSiize) 478void Lzma2Enc_SetDataSize(CLzma2EncHandle p, UInt64 expectedDataSiize)
479{ 479{
480 CLzma2Enc *p = (CLzma2Enc *)pp; 480 // GET_CLzma2Enc_p
481 p->expectedDataSize = expectedDataSiize; 481 p->expectedDataSize = expectedDataSiize;
482} 482}
483 483
484 484
485Byte Lzma2Enc_WriteProperties(CLzma2EncHandle pp) 485Byte Lzma2Enc_WriteProperties(CLzma2EncHandle p)
486{ 486{
487 CLzma2Enc *p = (CLzma2Enc *)pp; 487 // GET_CLzma2Enc_p
488 unsigned i; 488 unsigned i;
489 UInt32 dicSize = LzmaEncProps_GetDictSize(&p->props.lzmaProps); 489 UInt32 dicSize = LzmaEncProps_GetDictSize(&p->props.lzmaProps);
490 for (i = 0; i < 40; i++) 490 for (i = 0; i < 40; i++)
@@ -497,12 +497,12 @@ Byte Lzma2Enc_WriteProperties(CLzma2EncHandle pp)
497static SRes Lzma2Enc_EncodeMt1( 497static SRes Lzma2Enc_EncodeMt1(
498 CLzma2Enc *me, 498 CLzma2Enc *me,
499 CLzma2EncInt *p, 499 CLzma2EncInt *p,
500 ISeqOutStream *outStream, 500 ISeqOutStreamPtr outStream,
501 Byte *outBuf, size_t *outBufSize, 501 Byte *outBuf, size_t *outBufSize,
502 ISeqInStream *inStream, 502 ISeqInStreamPtr inStream,
503 const Byte *inData, size_t inDataSize, 503 const Byte *inData, size_t inDataSize,
504 int finished, 504 int finished,
505 ICompressProgress *progress) 505 ICompressProgressPtr progress)
506{ 506{
507 UInt64 unpackTotal = 0; 507 UInt64 unpackTotal = 0;
508 UInt64 packTotal = 0; 508 UInt64 packTotal = 0;
@@ -540,12 +540,12 @@ static SRes Lzma2Enc_EncodeMt1(
540 } 540 }
541 } 541 }
542 542
543 RINOK(Lzma2EncInt_InitStream(p, &me->props)); 543 RINOK(Lzma2EncInt_InitStream(p, &me->props))
544 544
545 for (;;) 545 for (;;)
546 { 546 {
547 SRes res = SZ_OK; 547 SRes res = SZ_OK;
548 size_t inSizeCur = 0; 548 SizeT inSizeCur = 0;
549 549
550 Lzma2EncInt_InitBlock(p); 550 Lzma2EncInt_InitBlock(p);
551 551
@@ -559,7 +559,7 @@ static SRes Lzma2Enc_EncodeMt1(
559 if (me->expectedDataSize != (UInt64)(Int64)-1 559 if (me->expectedDataSize != (UInt64)(Int64)-1
560 && me->expectedDataSize >= unpackTotal) 560 && me->expectedDataSize >= unpackTotal)
561 expected = me->expectedDataSize - unpackTotal; 561 expected = me->expectedDataSize - unpackTotal;
562 if (me->props.blockSize != LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID 562 if (me->props.blockSize != LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID
563 && expected > me->props.blockSize) 563 && expected > me->props.blockSize)
564 expected = (size_t)me->props.blockSize; 564 expected = (size_t)me->props.blockSize;
565 565
@@ -569,14 +569,14 @@ static SRes Lzma2Enc_EncodeMt1(
569 &limitedInStream.vt, 569 &limitedInStream.vt,
570 LZMA2_KEEP_WINDOW_SIZE, 570 LZMA2_KEEP_WINDOW_SIZE,
571 me->alloc, 571 me->alloc,
572 me->allocBig)); 572 me->allocBig))
573 } 573 }
574 else 574 else
575 { 575 {
576 inSizeCur = inDataSize - (size_t)unpackTotal; 576 inSizeCur = (SizeT)(inDataSize - (size_t)unpackTotal);
577 if (me->props.blockSize != LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID 577 if (me->props.blockSize != LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID
578 && inSizeCur > me->props.blockSize) 578 && inSizeCur > me->props.blockSize)
579 inSizeCur = (size_t)me->props.blockSize; 579 inSizeCur = (SizeT)(size_t)me->props.blockSize;
580 580
581 // LzmaEnc_SetDataSize(p->enc, inSizeCur); 581 // LzmaEnc_SetDataSize(p->enc, inSizeCur);
582 582
@@ -584,7 +584,7 @@ static SRes Lzma2Enc_EncodeMt1(
584 inData + (size_t)unpackTotal, inSizeCur, 584 inData + (size_t)unpackTotal, inSizeCur,
585 LZMA2_KEEP_WINDOW_SIZE, 585 LZMA2_KEEP_WINDOW_SIZE,
586 me->alloc, 586 me->alloc,
587 me->allocBig)); 587 me->allocBig))
588 } 588 }
589 589
590 for (;;) 590 for (;;)
@@ -621,7 +621,7 @@ static SRes Lzma2Enc_EncodeMt1(
621 621
622 unpackTotal += p->srcPos; 622 unpackTotal += p->srcPos;
623 623
624 RINOK(res); 624 RINOK(res)
625 625
626 if (p->srcPos != (inStream ? limitedInStream.processed : inSizeCur)) 626 if (p->srcPos != (inStream ? limitedInStream.processed : inSizeCur))
627 return SZ_ERROR_FAIL; 627 return SZ_ERROR_FAIL;
@@ -652,12 +652,12 @@ static SRes Lzma2Enc_EncodeMt1(
652 652
653 653
654 654
655#ifndef _7ZIP_ST 655#ifndef Z7_ST
656 656
657static SRes Lzma2Enc_MtCallback_Code(void *pp, unsigned coderIndex, unsigned outBufIndex, 657static SRes Lzma2Enc_MtCallback_Code(void *p, unsigned coderIndex, unsigned outBufIndex,
658 const Byte *src, size_t srcSize, int finished) 658 const Byte *src, size_t srcSize, int finished)
659{ 659{
660 CLzma2Enc *me = (CLzma2Enc *)pp; 660 CLzma2Enc *me = (CLzma2Enc *)p;
661 size_t destSize = me->outBufSize; 661 size_t destSize = me->outBufSize;
662 SRes res; 662 SRes res;
663 CMtProgressThunk progressThunk; 663 CMtProgressThunk progressThunk;
@@ -692,9 +692,9 @@ static SRes Lzma2Enc_MtCallback_Code(void *pp, unsigned coderIndex, unsigned out
692} 692}
693 693
694 694
695static SRes Lzma2Enc_MtCallback_Write(void *pp, unsigned outBufIndex) 695static SRes Lzma2Enc_MtCallback_Write(void *p, unsigned outBufIndex)
696{ 696{
697 CLzma2Enc *me = (CLzma2Enc *)pp; 697 CLzma2Enc *me = (CLzma2Enc *)p;
698 size_t size = me->outBufsDataSizes[outBufIndex]; 698 size_t size = me->outBufsDataSizes[outBufIndex];
699 const Byte *data = me->outBufs[outBufIndex]; 699 const Byte *data = me->outBufs[outBufIndex];
700 700
@@ -713,14 +713,14 @@ static SRes Lzma2Enc_MtCallback_Write(void *pp, unsigned outBufIndex)
713 713
714 714
715 715
716SRes Lzma2Enc_Encode2(CLzma2EncHandle pp, 716SRes Lzma2Enc_Encode2(CLzma2EncHandle p,
717 ISeqOutStream *outStream, 717 ISeqOutStreamPtr outStream,
718 Byte *outBuf, size_t *outBufSize, 718 Byte *outBuf, size_t *outBufSize,
719 ISeqInStream *inStream, 719 ISeqInStreamPtr inStream,
720 const Byte *inData, size_t inDataSize, 720 const Byte *inData, size_t inDataSize,
721 ICompressProgress *progress) 721 ICompressProgressPtr progress)
722{ 722{
723 CLzma2Enc *p = (CLzma2Enc *)pp; 723 // GET_CLzma2Enc_p
724 724
725 if (inStream && inData) 725 if (inStream && inData)
726 return SZ_ERROR_PARAM; 726 return SZ_ERROR_PARAM;
@@ -730,11 +730,11 @@ SRes Lzma2Enc_Encode2(CLzma2EncHandle pp,
730 730
731 { 731 {
732 unsigned i; 732 unsigned i;
733 for (i = 0; i < MTCODER__THREADS_MAX; i++) 733 for (i = 0; i < MTCODER_THREADS_MAX; i++)
734 p->coders[i].propsAreSet = False; 734 p->coders[i].propsAreSet = False;
735 } 735 }
736 736
737 #ifndef _7ZIP_ST 737 #ifndef Z7_ST
738 738
739 if (p->props.numBlockThreads_Reduced > 1) 739 if (p->props.numBlockThreads_Reduced > 1)
740 { 740 {
@@ -772,7 +772,7 @@ SRes Lzma2Enc_Encode2(CLzma2EncHandle pp,
772 return SZ_ERROR_PARAM; /* SZ_ERROR_MEM */ 772 return SZ_ERROR_PARAM; /* SZ_ERROR_MEM */
773 773
774 { 774 {
775 size_t destBlockSize = p->mtCoder.blockSize + (p->mtCoder.blockSize >> 10) + 16; 775 const size_t destBlockSize = p->mtCoder.blockSize + (p->mtCoder.blockSize >> 10) + 16;
776 if (destBlockSize < p->mtCoder.blockSize) 776 if (destBlockSize < p->mtCoder.blockSize)
777 return SZ_ERROR_PARAM; 777 return SZ_ERROR_PARAM;
778 if (p->outBufSize != destBlockSize) 778 if (p->outBufSize != destBlockSize)
@@ -784,7 +784,7 @@ SRes Lzma2Enc_Encode2(CLzma2EncHandle pp,
784 p->mtCoder.expectedDataSize = p->expectedDataSize; 784 p->mtCoder.expectedDataSize = p->expectedDataSize;
785 785
786 { 786 {
787 SRes res = MtCoder_Code(&p->mtCoder); 787 const SRes res = MtCoder_Code(&p->mtCoder);
788 if (!outStream) 788 if (!outStream)
789 *outBufSize = (size_t)(p->outBuf - outBuf); 789 *outBufSize = (size_t)(p->outBuf - outBuf);
790 return res; 790 return res;
@@ -801,3 +801,5 @@ SRes Lzma2Enc_Encode2(CLzma2EncHandle pp,
801 True, /* finished */ 801 True, /* finished */
802 progress); 802 progress);
803} 803}
804
805#undef PRF
diff --git a/C/Lzma2Enc.h b/C/Lzma2Enc.h
index 6a6110f..cb25275 100644
--- a/C/Lzma2Enc.h
+++ b/C/Lzma2Enc.h
@@ -1,15 +1,15 @@
1/* Lzma2Enc.h -- LZMA2 Encoder 1/* Lzma2Enc.h -- LZMA2 Encoder
22017-07-27 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZMA2_ENC_H 4#ifndef ZIP7_INC_LZMA2_ENC_H
5#define __LZMA2_ENC_H 5#define ZIP7_INC_LZMA2_ENC_H
6 6
7#include "LzmaEnc.h" 7#include "LzmaEnc.h"
8 8
9EXTERN_C_BEGIN 9EXTERN_C_BEGIN
10 10
11#define LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO 0 11#define LZMA2_ENC_PROPS_BLOCK_SIZE_AUTO 0
12#define LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID ((UInt64)(Int64)-1) 12#define LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID ((UInt64)(Int64)-1)
13 13
14typedef struct 14typedef struct
15{ 15{
@@ -36,7 +36,9 @@ SRes:
36 SZ_ERROR_THREAD - error in multithreading functions (only for Mt version) 36 SZ_ERROR_THREAD - error in multithreading functions (only for Mt version)
37*/ 37*/
38 38
39typedef void * CLzma2EncHandle; 39typedef struct CLzma2Enc CLzma2Enc;
40typedef CLzma2Enc * CLzma2EncHandle;
41// Z7_DECLARE_HANDLE(CLzma2EncHandle)
40 42
41CLzma2EncHandle Lzma2Enc_Create(ISzAllocPtr alloc, ISzAllocPtr allocBig); 43CLzma2EncHandle Lzma2Enc_Create(ISzAllocPtr alloc, ISzAllocPtr allocBig);
42void Lzma2Enc_Destroy(CLzma2EncHandle p); 44void Lzma2Enc_Destroy(CLzma2EncHandle p);
@@ -44,11 +46,11 @@ SRes Lzma2Enc_SetProps(CLzma2EncHandle p, const CLzma2EncProps *props);
44void Lzma2Enc_SetDataSize(CLzma2EncHandle p, UInt64 expectedDataSiize); 46void Lzma2Enc_SetDataSize(CLzma2EncHandle p, UInt64 expectedDataSiize);
45Byte Lzma2Enc_WriteProperties(CLzma2EncHandle p); 47Byte Lzma2Enc_WriteProperties(CLzma2EncHandle p);
46SRes Lzma2Enc_Encode2(CLzma2EncHandle p, 48SRes Lzma2Enc_Encode2(CLzma2EncHandle p,
47 ISeqOutStream *outStream, 49 ISeqOutStreamPtr outStream,
48 Byte *outBuf, size_t *outBufSize, 50 Byte *outBuf, size_t *outBufSize,
49 ISeqInStream *inStream, 51 ISeqInStreamPtr inStream,
50 const Byte *inData, size_t inDataSize, 52 const Byte *inData, size_t inDataSize,
51 ICompressProgress *progress); 53 ICompressProgressPtr progress);
52 54
53EXTERN_C_END 55EXTERN_C_END
54 56
diff --git a/C/Lzma86.h b/C/Lzma86.h
index bebed5c..e7707e2 100644
--- a/C/Lzma86.h
+++ b/C/Lzma86.h
@@ -1,8 +1,8 @@
1/* Lzma86.h -- LZMA + x86 (BCJ) Filter 1/* Lzma86.h -- LZMA + x86 (BCJ) Filter
22013-01-18 : Igor Pavlov : Public domain */ 22023-03-03 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZMA86_H 4#ifndef ZIP7_INC_LZMA86_H
5#define __LZMA86_H 5#define ZIP7_INC_LZMA86_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
diff --git a/C/Lzma86Dec.c b/C/Lzma86Dec.c
index 2103174..f094d4c 100644
--- a/C/Lzma86Dec.c
+++ b/C/Lzma86Dec.c
@@ -1,5 +1,5 @@
1/* Lzma86Dec.c -- LZMA + x86 (BCJ) Filter Decoder 1/* Lzma86Dec.c -- LZMA + x86 (BCJ) Filter Decoder
22016-05-16 : Igor Pavlov : Public domain */ 22023-03-03 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -46,9 +46,8 @@ SRes Lzma86_Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen)
46 return res; 46 return res;
47 if (useFilter == 1) 47 if (useFilter == 1)
48 { 48 {
49 UInt32 x86State; 49 UInt32 x86State = Z7_BRANCH_CONV_ST_X86_STATE_INIT_VAL;
50 x86_Convert_Init(x86State); 50 z7_BranchConvSt_X86_Dec(dest, *destLen, 0, &x86State);
51 x86_Convert(dest, *destLen, 0, &x86State, 0);
52 } 51 }
53 return SZ_OK; 52 return SZ_OK;
54} 53}
diff --git a/C/Lzma86Enc.c b/C/Lzma86Enc.c
index 14fcd65..0cdde1c 100644
--- a/C/Lzma86Enc.c
+++ b/C/Lzma86Enc.c
@@ -1,5 +1,5 @@
1/* Lzma86Enc.c -- LZMA + x86 (BCJ) Filter Encoder 1/* Lzma86Enc.c -- LZMA + x86 (BCJ) Filter Encoder
22018-07-04 : Igor Pavlov : Public domain */ 22023-03-03 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -46,9 +46,8 @@ int Lzma86_Encode(Byte *dest, size_t *destLen, const Byte *src, size_t srcLen,
46 memcpy(filteredStream, src, srcLen); 46 memcpy(filteredStream, src, srcLen);
47 } 47 }
48 { 48 {
49 UInt32 x86State; 49 UInt32 x86State = Z7_BRANCH_CONV_ST_X86_STATE_INIT_VAL;
50 x86_Convert_Init(x86State); 50 z7_BranchConvSt_X86_Enc(filteredStream, srcLen, 0, &x86State);
51 x86_Convert(filteredStream, srcLen, 0, &x86State, 1);
52 } 51 }
53 } 52 }
54 53
diff --git a/C/LzmaDec.c b/C/LzmaDec.c
index d6742e5..69bb8bb 100644
--- a/C/LzmaDec.c
+++ b/C/LzmaDec.c
@@ -1,5 +1,5 @@
1/* LzmaDec.c -- LZMA Decoder 1/* LzmaDec.c -- LZMA Decoder
22021-04-01 : Igor Pavlov : Public domain */ 22023-04-07 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -8,15 +8,15 @@
8/* #include "CpuArch.h" */ 8/* #include "CpuArch.h" */
9#include "LzmaDec.h" 9#include "LzmaDec.h"
10 10
11#define kNumTopBits 24 11// #define kNumTopBits 24
12#define kTopValue ((UInt32)1 << kNumTopBits) 12#define kTopValue ((UInt32)1 << 24)
13 13
14#define kNumBitModelTotalBits 11 14#define kNumBitModelTotalBits 11
15#define kBitModelTotal (1 << kNumBitModelTotalBits) 15#define kBitModelTotal (1 << kNumBitModelTotalBits)
16 16
17#define RC_INIT_SIZE 5 17#define RC_INIT_SIZE 5
18 18
19#ifndef _LZMA_DEC_OPT 19#ifndef Z7_LZMA_DEC_OPT
20 20
21#define kNumMoveBits 5 21#define kNumMoveBits 5
22#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); } 22#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
@@ -25,14 +25,14 @@
25#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); 25#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
26#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); 26#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
27#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \ 27#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
28 { UPDATE_0(p); i = (i + i); A0; } else \ 28 { UPDATE_0(p) i = (i + i); A0; } else \
29 { UPDATE_1(p); i = (i + i) + 1; A1; } 29 { UPDATE_1(p) i = (i + i) + 1; A1; }
30 30
31#define TREE_GET_BIT(probs, i) { GET_BIT2(probs + i, i, ;, ;); } 31#define TREE_GET_BIT(probs, i) { GET_BIT2(probs + i, i, ;, ;); }
32 32
33#define REV_BIT(p, i, A0, A1) IF_BIT_0(p + i) \ 33#define REV_BIT(p, i, A0, A1) IF_BIT_0(p + i) \
34 { UPDATE_0(p + i); A0; } else \ 34 { UPDATE_0(p + i) A0; } else \
35 { UPDATE_1(p + i); A1; } 35 { UPDATE_1(p + i) A1; }
36#define REV_BIT_VAR( p, i, m) REV_BIT(p, i, i += m; m += m, m += m; i += m; ) 36#define REV_BIT_VAR( p, i, m) REV_BIT(p, i, i += m; m += m, m += m; i += m; )
37#define REV_BIT_CONST(p, i, m) REV_BIT(p, i, i += m; , i += m * 2; ) 37#define REV_BIT_CONST(p, i, m) REV_BIT(p, i, i += m; , i += m * 2; )
38#define REV_BIT_LAST( p, i, m) REV_BIT(p, i, i -= m , ; ) 38#define REV_BIT_LAST( p, i, m) REV_BIT(p, i, i -= m , ; )
@@ -40,19 +40,19 @@
40#define TREE_DECODE(probs, limit, i) \ 40#define TREE_DECODE(probs, limit, i) \
41 { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; } 41 { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
42 42
43/* #define _LZMA_SIZE_OPT */ 43/* #define Z7_LZMA_SIZE_OPT */
44 44
45#ifdef _LZMA_SIZE_OPT 45#ifdef Z7_LZMA_SIZE_OPT
46#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i) 46#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
47#else 47#else
48#define TREE_6_DECODE(probs, i) \ 48#define TREE_6_DECODE(probs, i) \
49 { i = 1; \ 49 { i = 1; \
50 TREE_GET_BIT(probs, i); \ 50 TREE_GET_BIT(probs, i) \
51 TREE_GET_BIT(probs, i); \ 51 TREE_GET_BIT(probs, i) \
52 TREE_GET_BIT(probs, i); \ 52 TREE_GET_BIT(probs, i) \
53 TREE_GET_BIT(probs, i); \ 53 TREE_GET_BIT(probs, i) \
54 TREE_GET_BIT(probs, i); \ 54 TREE_GET_BIT(probs, i) \
55 TREE_GET_BIT(probs, i); \ 55 TREE_GET_BIT(probs, i) \
56 i -= 0x40; } 56 i -= 0x40; }
57#endif 57#endif
58 58
@@ -64,25 +64,25 @@
64 probLit = prob + (offs + bit + symbol); \ 64 probLit = prob + (offs + bit + symbol); \
65 GET_BIT2(probLit, symbol, offs ^= bit; , ;) 65 GET_BIT2(probLit, symbol, offs ^= bit; , ;)
66 66
67#endif // _LZMA_DEC_OPT 67#endif // Z7_LZMA_DEC_OPT
68 68
69 69
70#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_INPUT_EOF; range <<= 8; code = (code << 8) | (*buf++); } 70#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_INPUT_EOF; range <<= 8; code = (code << 8) | (*buf++); }
71 71
72#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound) 72#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound)
73#define UPDATE_0_CHECK range = bound; 73#define UPDATE_0_CHECK range = bound;
74#define UPDATE_1_CHECK range -= bound; code -= bound; 74#define UPDATE_1_CHECK range -= bound; code -= bound;
75#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \ 75#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
76 { UPDATE_0_CHECK; i = (i + i); A0; } else \ 76 { UPDATE_0_CHECK i = (i + i); A0; } else \
77 { UPDATE_1_CHECK; i = (i + i) + 1; A1; } 77 { UPDATE_1_CHECK i = (i + i) + 1; A1; }
78#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;) 78#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
79#define TREE_DECODE_CHECK(probs, limit, i) \ 79#define TREE_DECODE_CHECK(probs, limit, i) \
80 { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; } 80 { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
81 81
82 82
83#define REV_BIT_CHECK(p, i, m) IF_BIT_0_CHECK(p + i) \ 83#define REV_BIT_CHECK(p, i, m) IF_BIT_0_CHECK(p + i) \
84 { UPDATE_0_CHECK; i += m; m += m; } else \ 84 { UPDATE_0_CHECK i += m; m += m; } else \
85 { UPDATE_1_CHECK; m += m; i += m; } 85 { UPDATE_1_CHECK m += m; i += m; }
86 86
87 87
88#define kNumPosBitsMax 4 88#define kNumPosBitsMax 4
@@ -224,14 +224,14 @@ Out:
224*/ 224*/
225 225
226 226
227#ifdef _LZMA_DEC_OPT 227#ifdef Z7_LZMA_DEC_OPT
228 228
229int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit); 229int Z7_FASTCALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit);
230 230
231#else 231#else
232 232
233static 233static
234int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit) 234int Z7_FASTCALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
235{ 235{
236 CLzmaProb *probs = GET_PROBS; 236 CLzmaProb *probs = GET_PROBS;
237 unsigned state = (unsigned)p->state; 237 unsigned state = (unsigned)p->state;
@@ -263,7 +263,7 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
263 IF_BIT_0(prob) 263 IF_BIT_0(prob)
264 { 264 {
265 unsigned symbol; 265 unsigned symbol;
266 UPDATE_0(prob); 266 UPDATE_0(prob)
267 prob = probs + Literal; 267 prob = probs + Literal;
268 if (processedPos != 0 || checkDicSize != 0) 268 if (processedPos != 0 || checkDicSize != 0)
269 prob += (UInt32)3 * ((((processedPos << 8) + dic[(dicPos == 0 ? dicBufSize : dicPos) - 1]) & lpMask) << lc); 269 prob += (UInt32)3 * ((((processedPos << 8) + dic[(dicPos == 0 ? dicBufSize : dicPos) - 1]) & lpMask) << lc);
@@ -273,7 +273,7 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
273 { 273 {
274 state -= (state < 4) ? state : 3; 274 state -= (state < 4) ? state : 3;
275 symbol = 1; 275 symbol = 1;
276 #ifdef _LZMA_SIZE_OPT 276 #ifdef Z7_LZMA_SIZE_OPT
277 do { NORMAL_LITER_DEC } while (symbol < 0x100); 277 do { NORMAL_LITER_DEC } while (symbol < 0x100);
278 #else 278 #else
279 NORMAL_LITER_DEC 279 NORMAL_LITER_DEC
@@ -292,7 +292,7 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
292 unsigned offs = 0x100; 292 unsigned offs = 0x100;
293 state -= (state < 10) ? 3 : 6; 293 state -= (state < 10) ? 3 : 6;
294 symbol = 1; 294 symbol = 1;
295 #ifdef _LZMA_SIZE_OPT 295 #ifdef Z7_LZMA_SIZE_OPT
296 do 296 do
297 { 297 {
298 unsigned bit; 298 unsigned bit;
@@ -321,25 +321,25 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
321 } 321 }
322 322
323 { 323 {
324 UPDATE_1(prob); 324 UPDATE_1(prob)
325 prob = probs + IsRep + state; 325 prob = probs + IsRep + state;
326 IF_BIT_0(prob) 326 IF_BIT_0(prob)
327 { 327 {
328 UPDATE_0(prob); 328 UPDATE_0(prob)
329 state += kNumStates; 329 state += kNumStates;
330 prob = probs + LenCoder; 330 prob = probs + LenCoder;
331 } 331 }
332 else 332 else
333 { 333 {
334 UPDATE_1(prob); 334 UPDATE_1(prob)
335 prob = probs + IsRepG0 + state; 335 prob = probs + IsRepG0 + state;
336 IF_BIT_0(prob) 336 IF_BIT_0(prob)
337 { 337 {
338 UPDATE_0(prob); 338 UPDATE_0(prob)
339 prob = probs + IsRep0Long + COMBINED_PS_STATE; 339 prob = probs + IsRep0Long + COMBINED_PS_STATE;
340 IF_BIT_0(prob) 340 IF_BIT_0(prob)
341 { 341 {
342 UPDATE_0(prob); 342 UPDATE_0(prob)
343 343
344 // that case was checked before with kBadRepCode 344 // that case was checked before with kBadRepCode
345 // if (checkDicSize == 0 && processedPos == 0) { len = kMatchSpecLen_Error_Data + 1; break; } 345 // if (checkDicSize == 0 && processedPos == 0) { len = kMatchSpecLen_Error_Data + 1; break; }
@@ -353,30 +353,30 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
353 state = state < kNumLitStates ? 9 : 11; 353 state = state < kNumLitStates ? 9 : 11;
354 continue; 354 continue;
355 } 355 }
356 UPDATE_1(prob); 356 UPDATE_1(prob)
357 } 357 }
358 else 358 else
359 { 359 {
360 UInt32 distance; 360 UInt32 distance;
361 UPDATE_1(prob); 361 UPDATE_1(prob)
362 prob = probs + IsRepG1 + state; 362 prob = probs + IsRepG1 + state;
363 IF_BIT_0(prob) 363 IF_BIT_0(prob)
364 { 364 {
365 UPDATE_0(prob); 365 UPDATE_0(prob)
366 distance = rep1; 366 distance = rep1;
367 } 367 }
368 else 368 else
369 { 369 {
370 UPDATE_1(prob); 370 UPDATE_1(prob)
371 prob = probs + IsRepG2 + state; 371 prob = probs + IsRepG2 + state;
372 IF_BIT_0(prob) 372 IF_BIT_0(prob)
373 { 373 {
374 UPDATE_0(prob); 374 UPDATE_0(prob)
375 distance = rep2; 375 distance = rep2;
376 } 376 }
377 else 377 else
378 { 378 {
379 UPDATE_1(prob); 379 UPDATE_1(prob)
380 distance = rep3; 380 distance = rep3;
381 rep3 = rep2; 381 rep3 = rep2;
382 } 382 }
@@ -389,37 +389,37 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
389 prob = probs + RepLenCoder; 389 prob = probs + RepLenCoder;
390 } 390 }
391 391
392 #ifdef _LZMA_SIZE_OPT 392 #ifdef Z7_LZMA_SIZE_OPT
393 { 393 {
394 unsigned lim, offset; 394 unsigned lim, offset;
395 CLzmaProb *probLen = prob + LenChoice; 395 CLzmaProb *probLen = prob + LenChoice;
396 IF_BIT_0(probLen) 396 IF_BIT_0(probLen)
397 { 397 {
398 UPDATE_0(probLen); 398 UPDATE_0(probLen)
399 probLen = prob + LenLow + GET_LEN_STATE; 399 probLen = prob + LenLow + GET_LEN_STATE;
400 offset = 0; 400 offset = 0;
401 lim = (1 << kLenNumLowBits); 401 lim = (1 << kLenNumLowBits);
402 } 402 }
403 else 403 else
404 { 404 {
405 UPDATE_1(probLen); 405 UPDATE_1(probLen)
406 probLen = prob + LenChoice2; 406 probLen = prob + LenChoice2;
407 IF_BIT_0(probLen) 407 IF_BIT_0(probLen)
408 { 408 {
409 UPDATE_0(probLen); 409 UPDATE_0(probLen)
410 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits); 410 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
411 offset = kLenNumLowSymbols; 411 offset = kLenNumLowSymbols;
412 lim = (1 << kLenNumLowBits); 412 lim = (1 << kLenNumLowBits);
413 } 413 }
414 else 414 else
415 { 415 {
416 UPDATE_1(probLen); 416 UPDATE_1(probLen)
417 probLen = prob + LenHigh; 417 probLen = prob + LenHigh;
418 offset = kLenNumLowSymbols * 2; 418 offset = kLenNumLowSymbols * 2;
419 lim = (1 << kLenNumHighBits); 419 lim = (1 << kLenNumHighBits);
420 } 420 }
421 } 421 }
422 TREE_DECODE(probLen, lim, len); 422 TREE_DECODE(probLen, lim, len)
423 len += offset; 423 len += offset;
424 } 424 }
425 #else 425 #else
@@ -427,32 +427,32 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
427 CLzmaProb *probLen = prob + LenChoice; 427 CLzmaProb *probLen = prob + LenChoice;
428 IF_BIT_0(probLen) 428 IF_BIT_0(probLen)
429 { 429 {
430 UPDATE_0(probLen); 430 UPDATE_0(probLen)
431 probLen = prob + LenLow + GET_LEN_STATE; 431 probLen = prob + LenLow + GET_LEN_STATE;
432 len = 1; 432 len = 1;
433 TREE_GET_BIT(probLen, len); 433 TREE_GET_BIT(probLen, len)
434 TREE_GET_BIT(probLen, len); 434 TREE_GET_BIT(probLen, len)
435 TREE_GET_BIT(probLen, len); 435 TREE_GET_BIT(probLen, len)
436 len -= 8; 436 len -= 8;
437 } 437 }
438 else 438 else
439 { 439 {
440 UPDATE_1(probLen); 440 UPDATE_1(probLen)
441 probLen = prob + LenChoice2; 441 probLen = prob + LenChoice2;
442 IF_BIT_0(probLen) 442 IF_BIT_0(probLen)
443 { 443 {
444 UPDATE_0(probLen); 444 UPDATE_0(probLen)
445 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits); 445 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
446 len = 1; 446 len = 1;
447 TREE_GET_BIT(probLen, len); 447 TREE_GET_BIT(probLen, len)
448 TREE_GET_BIT(probLen, len); 448 TREE_GET_BIT(probLen, len)
449 TREE_GET_BIT(probLen, len); 449 TREE_GET_BIT(probLen, len)
450 } 450 }
451 else 451 else
452 { 452 {
453 UPDATE_1(probLen); 453 UPDATE_1(probLen)
454 probLen = prob + LenHigh; 454 probLen = prob + LenHigh;
455 TREE_DECODE(probLen, (1 << kLenNumHighBits), len); 455 TREE_DECODE(probLen, (1 << kLenNumHighBits), len)
456 len += kLenNumLowSymbols * 2; 456 len += kLenNumLowSymbols * 2;
457 } 457 }
458 } 458 }
@@ -464,7 +464,7 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
464 UInt32 distance; 464 UInt32 distance;
465 prob = probs + PosSlot + 465 prob = probs + PosSlot +
466 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits); 466 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
467 TREE_6_DECODE(prob, distance); 467 TREE_6_DECODE(prob, distance)
468 if (distance >= kStartPosModelIndex) 468 if (distance >= kStartPosModelIndex)
469 { 469 {
470 unsigned posSlot = (unsigned)distance; 470 unsigned posSlot = (unsigned)distance;
@@ -479,7 +479,7 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
479 distance++; 479 distance++;
480 do 480 do
481 { 481 {
482 REV_BIT_VAR(prob, distance, m); 482 REV_BIT_VAR(prob, distance, m)
483 } 483 }
484 while (--numDirectBits); 484 while (--numDirectBits);
485 distance -= m; 485 distance -= m;
@@ -514,10 +514,10 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
514 distance <<= kNumAlignBits; 514 distance <<= kNumAlignBits;
515 { 515 {
516 unsigned i = 1; 516 unsigned i = 1;
517 REV_BIT_CONST(prob, i, 1); 517 REV_BIT_CONST(prob, i, 1)
518 REV_BIT_CONST(prob, i, 2); 518 REV_BIT_CONST(prob, i, 2)
519 REV_BIT_CONST(prob, i, 4); 519 REV_BIT_CONST(prob, i, 4)
520 REV_BIT_LAST (prob, i, 8); 520 REV_BIT_LAST (prob, i, 8)
521 distance |= i; 521 distance |= i;
522 } 522 }
523 if (distance == (UInt32)0xFFFFFFFF) 523 if (distance == (UInt32)0xFFFFFFFF)
@@ -592,7 +592,7 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
592 } 592 }
593 while (dicPos < limit && buf < bufLimit); 593 while (dicPos < limit && buf < bufLimit);
594 594
595 NORMALIZE; 595 NORMALIZE
596 596
597 p->buf = buf; 597 p->buf = buf;
598 p->range = range; 598 p->range = range;
@@ -613,7 +613,7 @@ int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit
613 613
614 614
615 615
616static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit) 616static void Z7_FASTCALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
617{ 617{
618 unsigned len = (unsigned)p->remainLen; 618 unsigned len = (unsigned)p->remainLen;
619 if (len == 0 /* || len >= kMatchSpecLenStart */) 619 if (len == 0 /* || len >= kMatchSpecLenStart */)
@@ -683,7 +683,7 @@ and we support the following state of (p->checkDicSize):
683 (p->checkDicSize == p->prop.dicSize) 683 (p->checkDicSize == p->prop.dicSize)
684*/ 684*/
685 685
686static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit) 686static int Z7_FASTCALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
687{ 687{
688 if (p->checkDicSize == 0) 688 if (p->checkDicSize == 0)
689 { 689 {
@@ -767,54 +767,54 @@ static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, const Byt
767 else 767 else
768 { 768 {
769 unsigned len; 769 unsigned len;
770 UPDATE_1_CHECK; 770 UPDATE_1_CHECK
771 771
772 prob = probs + IsRep + state; 772 prob = probs + IsRep + state;
773 IF_BIT_0_CHECK(prob) 773 IF_BIT_0_CHECK(prob)
774 { 774 {
775 UPDATE_0_CHECK; 775 UPDATE_0_CHECK
776 state = 0; 776 state = 0;
777 prob = probs + LenCoder; 777 prob = probs + LenCoder;
778 res = DUMMY_MATCH; 778 res = DUMMY_MATCH;
779 } 779 }
780 else 780 else
781 { 781 {
782 UPDATE_1_CHECK; 782 UPDATE_1_CHECK
783 res = DUMMY_REP; 783 res = DUMMY_REP;
784 prob = probs + IsRepG0 + state; 784 prob = probs + IsRepG0 + state;
785 IF_BIT_0_CHECK(prob) 785 IF_BIT_0_CHECK(prob)
786 { 786 {
787 UPDATE_0_CHECK; 787 UPDATE_0_CHECK
788 prob = probs + IsRep0Long + COMBINED_PS_STATE; 788 prob = probs + IsRep0Long + COMBINED_PS_STATE;
789 IF_BIT_0_CHECK(prob) 789 IF_BIT_0_CHECK(prob)
790 { 790 {
791 UPDATE_0_CHECK; 791 UPDATE_0_CHECK
792 break; 792 break;
793 } 793 }
794 else 794 else
795 { 795 {
796 UPDATE_1_CHECK; 796 UPDATE_1_CHECK
797 } 797 }
798 } 798 }
799 else 799 else
800 { 800 {
801 UPDATE_1_CHECK; 801 UPDATE_1_CHECK
802 prob = probs + IsRepG1 + state; 802 prob = probs + IsRepG1 + state;
803 IF_BIT_0_CHECK(prob) 803 IF_BIT_0_CHECK(prob)
804 { 804 {
805 UPDATE_0_CHECK; 805 UPDATE_0_CHECK
806 } 806 }
807 else 807 else
808 { 808 {
809 UPDATE_1_CHECK; 809 UPDATE_1_CHECK
810 prob = probs + IsRepG2 + state; 810 prob = probs + IsRepG2 + state;
811 IF_BIT_0_CHECK(prob) 811 IF_BIT_0_CHECK(prob)
812 { 812 {
813 UPDATE_0_CHECK; 813 UPDATE_0_CHECK
814 } 814 }
815 else 815 else
816 { 816 {
817 UPDATE_1_CHECK; 817 UPDATE_1_CHECK
818 } 818 }
819 } 819 }
820 } 820 }
@@ -826,31 +826,31 @@ static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, const Byt
826 const CLzmaProb *probLen = prob + LenChoice; 826 const CLzmaProb *probLen = prob + LenChoice;
827 IF_BIT_0_CHECK(probLen) 827 IF_BIT_0_CHECK(probLen)
828 { 828 {
829 UPDATE_0_CHECK; 829 UPDATE_0_CHECK
830 probLen = prob + LenLow + GET_LEN_STATE; 830 probLen = prob + LenLow + GET_LEN_STATE;
831 offset = 0; 831 offset = 0;
832 limit = 1 << kLenNumLowBits; 832 limit = 1 << kLenNumLowBits;
833 } 833 }
834 else 834 else
835 { 835 {
836 UPDATE_1_CHECK; 836 UPDATE_1_CHECK
837 probLen = prob + LenChoice2; 837 probLen = prob + LenChoice2;
838 IF_BIT_0_CHECK(probLen) 838 IF_BIT_0_CHECK(probLen)
839 { 839 {
840 UPDATE_0_CHECK; 840 UPDATE_0_CHECK
841 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits); 841 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
842 offset = kLenNumLowSymbols; 842 offset = kLenNumLowSymbols;
843 limit = 1 << kLenNumLowBits; 843 limit = 1 << kLenNumLowBits;
844 } 844 }
845 else 845 else
846 { 846 {
847 UPDATE_1_CHECK; 847 UPDATE_1_CHECK
848 probLen = prob + LenHigh; 848 probLen = prob + LenHigh;
849 offset = kLenNumLowSymbols * 2; 849 offset = kLenNumLowSymbols * 2;
850 limit = 1 << kLenNumHighBits; 850 limit = 1 << kLenNumHighBits;
851 } 851 }
852 } 852 }
853 TREE_DECODE_CHECK(probLen, limit, len); 853 TREE_DECODE_CHECK(probLen, limit, len)
854 len += offset; 854 len += offset;
855 } 855 }
856 856
@@ -860,7 +860,7 @@ static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, const Byt
860 prob = probs + PosSlot + 860 prob = probs + PosSlot +
861 ((len < kNumLenToPosStates - 1 ? len : kNumLenToPosStates - 1) << 861 ((len < kNumLenToPosStates - 1 ? len : kNumLenToPosStates - 1) <<
862 kNumPosSlotBits); 862 kNumPosSlotBits);
863 TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot); 863 TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot)
864 if (posSlot >= kStartPosModelIndex) 864 if (posSlot >= kStartPosModelIndex)
865 { 865 {
866 unsigned numDirectBits = ((posSlot >> 1) - 1); 866 unsigned numDirectBits = ((posSlot >> 1) - 1);
@@ -888,7 +888,7 @@ static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, const Byt
888 unsigned m = 1; 888 unsigned m = 1;
889 do 889 do
890 { 890 {
891 REV_BIT_CHECK(prob, i, m); 891 REV_BIT_CHECK(prob, i, m)
892 } 892 }
893 while (--numDirectBits); 893 while (--numDirectBits);
894 } 894 }
@@ -897,7 +897,7 @@ static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, const Byt
897 } 897 }
898 break; 898 break;
899 } 899 }
900 NORMALIZE_CHECK; 900 NORMALIZE_CHECK
901 901
902 *bufOut = buf; 902 *bufOut = buf;
903 return res; 903 return res;
@@ -943,7 +943,7 @@ When the decoder lookahead, and the lookahead symbol is not end_marker, we have
943*/ 943*/
944 944
945 945
946#define RETURN__NOT_FINISHED__FOR_FINISH \ 946#define RETURN_NOT_FINISHED_FOR_FINISH \
947 *status = LZMA_STATUS_NOT_FINISHED; \ 947 *status = LZMA_STATUS_NOT_FINISHED; \
948 return SZ_ERROR_DATA; // for strict mode 948 return SZ_ERROR_DATA; // for strict mode
949 // return SZ_OK; // for relaxed mode 949 // return SZ_OK; // for relaxed mode
@@ -1029,7 +1029,7 @@ SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *sr
1029 } 1029 }
1030 if (p->remainLen != 0) 1030 if (p->remainLen != 0)
1031 { 1031 {
1032 RETURN__NOT_FINISHED__FOR_FINISH; 1032 RETURN_NOT_FINISHED_FOR_FINISH
1033 } 1033 }
1034 checkEndMarkNow = 1; 1034 checkEndMarkNow = 1;
1035 } 1035 }
@@ -1072,7 +1072,7 @@ SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *sr
1072 for (i = 0; i < (unsigned)dummyProcessed; i++) 1072 for (i = 0; i < (unsigned)dummyProcessed; i++)
1073 p->tempBuf[i] = src[i]; 1073 p->tempBuf[i] = src[i];
1074 // p->remainLen = kMatchSpecLen_Error_Data; 1074 // p->remainLen = kMatchSpecLen_Error_Data;
1075 RETURN__NOT_FINISHED__FOR_FINISH; 1075 RETURN_NOT_FINISHED_FOR_FINISH
1076 } 1076 }
1077 1077
1078 bufLimit = src; 1078 bufLimit = src;
@@ -1150,7 +1150,7 @@ SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *sr
1150 (*srcLen) += (unsigned)dummyProcessed - p->tempBufSize; 1150 (*srcLen) += (unsigned)dummyProcessed - p->tempBufSize;
1151 p->tempBufSize = (unsigned)dummyProcessed; 1151 p->tempBufSize = (unsigned)dummyProcessed;
1152 // p->remainLen = kMatchSpecLen_Error_Data; 1152 // p->remainLen = kMatchSpecLen_Error_Data;
1153 RETURN__NOT_FINISHED__FOR_FINISH; 1153 RETURN_NOT_FINISHED_FOR_FINISH
1154 } 1154 }
1155 } 1155 }
1156 1156
@@ -1299,8 +1299,8 @@ static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAl
1299SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc) 1299SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc)
1300{ 1300{
1301 CLzmaProps propNew; 1301 CLzmaProps propNew;
1302 RINOK(LzmaProps_Decode(&propNew, props, propsSize)); 1302 RINOK(LzmaProps_Decode(&propNew, props, propsSize))
1303 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); 1303 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc))
1304 p->prop = propNew; 1304 p->prop = propNew;
1305 return SZ_OK; 1305 return SZ_OK;
1306} 1306}
@@ -1309,14 +1309,14 @@ SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAll
1309{ 1309{
1310 CLzmaProps propNew; 1310 CLzmaProps propNew;
1311 SizeT dicBufSize; 1311 SizeT dicBufSize;
1312 RINOK(LzmaProps_Decode(&propNew, props, propsSize)); 1312 RINOK(LzmaProps_Decode(&propNew, props, propsSize))
1313 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); 1313 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc))
1314 1314
1315 { 1315 {
1316 UInt32 dictSize = propNew.dicSize; 1316 UInt32 dictSize = propNew.dicSize;
1317 SizeT mask = ((UInt32)1 << 12) - 1; 1317 SizeT mask = ((UInt32)1 << 12) - 1;
1318 if (dictSize >= ((UInt32)1 << 30)) mask = ((UInt32)1 << 22) - 1; 1318 if (dictSize >= ((UInt32)1 << 30)) mask = ((UInt32)1 << 22) - 1;
1319 else if (dictSize >= ((UInt32)1 << 22)) mask = ((UInt32)1 << 20) - 1;; 1319 else if (dictSize >= ((UInt32)1 << 22)) mask = ((UInt32)1 << 20) - 1;
1320 dicBufSize = ((SizeT)dictSize + mask) & ~mask; 1320 dicBufSize = ((SizeT)dictSize + mask) & ~mask;
1321 if (dicBufSize < dictSize) 1321 if (dicBufSize < dictSize)
1322 dicBufSize = dictSize; 1322 dicBufSize = dictSize;
@@ -1348,8 +1348,8 @@ SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
1348 *status = LZMA_STATUS_NOT_SPECIFIED; 1348 *status = LZMA_STATUS_NOT_SPECIFIED;
1349 if (inSize < RC_INIT_SIZE) 1349 if (inSize < RC_INIT_SIZE)
1350 return SZ_ERROR_INPUT_EOF; 1350 return SZ_ERROR_INPUT_EOF;
1351 LzmaDec_Construct(&p); 1351 LzmaDec_CONSTRUCT(&p)
1352 RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc)); 1352 RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc))
1353 p.dic = dest; 1353 p.dic = dest;
1354 p.dicBufSize = outSize; 1354 p.dicBufSize = outSize;
1355 LzmaDec_Init(&p); 1355 LzmaDec_Init(&p);
diff --git a/C/LzmaDec.h b/C/LzmaDec.h
index 6f12962..b0ce28f 100644
--- a/C/LzmaDec.h
+++ b/C/LzmaDec.h
@@ -1,19 +1,19 @@
1/* LzmaDec.h -- LZMA Decoder 1/* LzmaDec.h -- LZMA Decoder
22020-03-19 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZMA_DEC_H 4#ifndef ZIP7_INC_LZMA_DEC_H
5#define __LZMA_DEC_H 5#define ZIP7_INC_LZMA_DEC_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
9EXTERN_C_BEGIN 9EXTERN_C_BEGIN
10 10
11/* #define _LZMA_PROB32 */ 11/* #define Z7_LZMA_PROB32 */
12/* _LZMA_PROB32 can increase the speed on some CPUs, 12/* Z7_LZMA_PROB32 can increase the speed on some CPUs,
13 but memory usage for CLzmaDec::probs will be doubled in that case */ 13 but memory usage for CLzmaDec::probs will be doubled in that case */
14 14
15typedef 15typedef
16#ifdef _LZMA_PROB32 16#ifdef Z7_LZMA_PROB32
17 UInt32 17 UInt32
18#else 18#else
19 UInt16 19 UInt16
@@ -25,7 +25,7 @@ typedef
25 25
26#define LZMA_PROPS_SIZE 5 26#define LZMA_PROPS_SIZE 5
27 27
28typedef struct _CLzmaProps 28typedef struct
29{ 29{
30 Byte lc; 30 Byte lc;
31 Byte lp; 31 Byte lp;
@@ -73,7 +73,8 @@ typedef struct
73 Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; 73 Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
74} CLzmaDec; 74} CLzmaDec;
75 75
76#define LzmaDec_Construct(p) { (p)->dic = NULL; (p)->probs = NULL; } 76#define LzmaDec_CONSTRUCT(p) { (p)->dic = NULL; (p)->probs = NULL; }
77#define LzmaDec_Construct(p) LzmaDec_CONSTRUCT(p)
77 78
78void LzmaDec_Init(CLzmaDec *p); 79void LzmaDec_Init(CLzmaDec *p);
79 80
diff --git a/C/LzmaEnc.c b/C/LzmaEnc.c
index c8b31a1..6d13cac 100644
--- a/C/LzmaEnc.c
+++ b/C/LzmaEnc.c
@@ -1,5 +1,5 @@
1/* LzmaEnc.c -- LZMA Encoder 1/* LzmaEnc.c -- LZMA Encoder
22022-07-15: Igor Pavlov : Public domain */ 22023-04-13: Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -16,22 +16,22 @@
16#include "LzmaEnc.h" 16#include "LzmaEnc.h"
17 17
18#include "LzFind.h" 18#include "LzFind.h"
19#ifndef _7ZIP_ST 19#ifndef Z7_ST
20#include "LzFindMt.h" 20#include "LzFindMt.h"
21#endif 21#endif
22 22
23/* the following LzmaEnc_* declarations is internal LZMA interface for LZMA2 encoder */ 23/* the following LzmaEnc_* declarations is internal LZMA interface for LZMA2 encoder */
24 24
25SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, ISeqInStream *inStream, UInt32 keepWindowSize, 25SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle p, ISeqInStreamPtr inStream, UInt32 keepWindowSize,
26 ISzAllocPtr alloc, ISzAllocPtr allocBig); 26 ISzAllocPtr alloc, ISzAllocPtr allocBig);
27SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen, 27SRes LzmaEnc_MemPrepare(CLzmaEncHandle p, const Byte *src, SizeT srcLen,
28 UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig); 28 UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig);
29SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, BoolInt reInit, 29SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle p, BoolInt reInit,
30 Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize); 30 Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize);
31const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp); 31const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle p);
32void LzmaEnc_Finish(CLzmaEncHandle pp); 32void LzmaEnc_Finish(CLzmaEncHandle p);
33void LzmaEnc_SaveState(CLzmaEncHandle pp); 33void LzmaEnc_SaveState(CLzmaEncHandle p);
34void LzmaEnc_RestoreState(CLzmaEncHandle pp); 34void LzmaEnc_RestoreState(CLzmaEncHandle p);
35 35
36#ifdef SHOW_STAT 36#ifdef SHOW_STAT
37static unsigned g_STAT_OFFSET = 0; 37static unsigned g_STAT_OFFSET = 0;
@@ -40,8 +40,8 @@ static unsigned g_STAT_OFFSET = 0;
40/* for good normalization speed we still reserve 256 MB before 4 GB range */ 40/* for good normalization speed we still reserve 256 MB before 4 GB range */
41#define kLzmaMaxHistorySize ((UInt32)15 << 28) 41#define kLzmaMaxHistorySize ((UInt32)15 << 28)
42 42
43#define kNumTopBits 24 43// #define kNumTopBits 24
44#define kTopValue ((UInt32)1 << kNumTopBits) 44#define kTopValue ((UInt32)1 << 24)
45 45
46#define kNumBitModelTotalBits 11 46#define kNumBitModelTotalBits 11
47#define kBitModelTotal (1 << kNumBitModelTotalBits) 47#define kBitModelTotal (1 << kNumBitModelTotalBits)
@@ -60,6 +60,7 @@ void LzmaEncProps_Init(CLzmaEncProps *p)
60 p->dictSize = p->mc = 0; 60 p->dictSize = p->mc = 0;
61 p->reduceSize = (UInt64)(Int64)-1; 61 p->reduceSize = (UInt64)(Int64)-1;
62 p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1; 62 p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
63 p->numHashOutBits = 0;
63 p->writeEndMark = 0; 64 p->writeEndMark = 0;
64 p->affinity = 0; 65 p->affinity = 0;
65} 66}
@@ -99,7 +100,7 @@ void LzmaEncProps_Normalize(CLzmaEncProps *p)
99 100
100 if (p->numThreads < 0) 101 if (p->numThreads < 0)
101 p->numThreads = 102 p->numThreads =
102 #ifndef _7ZIP_ST 103 #ifndef Z7_ST
103 ((p->btMode && p->algo) ? 2 : 1); 104 ((p->btMode && p->algo) ? 2 : 1);
104 #else 105 #else
105 1; 106 1;
@@ -293,7 +294,7 @@ typedef struct
293#define kNumFullDistances (1 << (kEndPosModelIndex >> 1)) 294#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
294 295
295typedef 296typedef
296#ifdef _LZMA_PROB32 297#ifdef Z7_LZMA_PROB32
297 UInt32 298 UInt32
298#else 299#else
299 UInt16 300 UInt16
@@ -350,7 +351,7 @@ typedef struct
350 Byte *buf; 351 Byte *buf;
351 Byte *bufLim; 352 Byte *bufLim;
352 Byte *bufBase; 353 Byte *bufBase;
353 ISeqOutStream *outStream; 354 ISeqOutStreamPtr outStream;
354 UInt64 processed; 355 UInt64 processed;
355 SRes res; 356 SRes res;
356} CRangeEnc; 357} CRangeEnc;
@@ -383,7 +384,7 @@ typedef struct
383typedef UInt32 CProbPrice; 384typedef UInt32 CProbPrice;
384 385
385 386
386typedef struct 387struct CLzmaEnc
387{ 388{
388 void *matchFinderObj; 389 void *matchFinderObj;
389 IMatchFinder2 matchFinder; 390 IMatchFinder2 matchFinder;
@@ -426,7 +427,7 @@ typedef struct
426 UInt32 dictSize; 427 UInt32 dictSize;
427 SRes result; 428 SRes result;
428 429
429 #ifndef _7ZIP_ST 430 #ifndef Z7_ST
430 BoolInt mtMode; 431 BoolInt mtMode;
431 // begin of CMatchFinderMt is used in LZ thread 432 // begin of CMatchFinderMt is used in LZ thread
432 CMatchFinderMt matchFinderMt; 433 CMatchFinderMt matchFinderMt;
@@ -439,7 +440,7 @@ typedef struct
439 440
440 // we suppose that we have 8-bytes alignment after CMatchFinder 441 // we suppose that we have 8-bytes alignment after CMatchFinder
441 442
442 #ifndef _7ZIP_ST 443 #ifndef Z7_ST
443 Byte pad[128]; 444 Byte pad[128];
444 #endif 445 #endif
445 446
@@ -479,77 +480,59 @@ typedef struct
479 CSaveState saveState; 480 CSaveState saveState;
480 481
481 // BoolInt mf_Failure; 482 // BoolInt mf_Failure;
482 #ifndef _7ZIP_ST 483 #ifndef Z7_ST
483 Byte pad2[128]; 484 Byte pad2[128];
484 #endif 485 #endif
485} CLzmaEnc; 486};
486 487
487 488
488#define MFB (p->matchFinderBase) 489#define MFB (p->matchFinderBase)
489/* 490/*
490#ifndef _7ZIP_ST 491#ifndef Z7_ST
491#define MFB (p->matchFinderMt.MatchFinder) 492#define MFB (p->matchFinderMt.MatchFinder)
492#endif 493#endif
493*/ 494*/
494 495
495#define COPY_ARR(dest, src, arr) memcpy(dest->arr, src->arr, sizeof(src->arr)); 496// #define GET_CLzmaEnc_p CLzmaEnc *p = (CLzmaEnc*)(void *)p;
496 497// #define GET_const_CLzmaEnc_p const CLzmaEnc *p = (const CLzmaEnc*)(const void *)p;
497void LzmaEnc_SaveState(CLzmaEncHandle pp) 498
498{ 499#define COPY_ARR(dest, src, arr) memcpy((dest)->arr, (src)->arr, sizeof((src)->arr));
499 CLzmaEnc *p = (CLzmaEnc *)pp; 500
500 CSaveState *dest = &p->saveState; 501#define COPY_LZMA_ENC_STATE(d, s, p) \
501 502 (d)->state = (s)->state; \
502 dest->state = p->state; 503 COPY_ARR(d, s, reps) \
503 504 COPY_ARR(d, s, posAlignEncoder) \
504 dest->lenProbs = p->lenProbs; 505 COPY_ARR(d, s, isRep) \
505 dest->repLenProbs = p->repLenProbs; 506 COPY_ARR(d, s, isRepG0) \
506 507 COPY_ARR(d, s, isRepG1) \
507 COPY_ARR(dest, p, reps); 508 COPY_ARR(d, s, isRepG2) \
508 509 COPY_ARR(d, s, isMatch) \
509 COPY_ARR(dest, p, posAlignEncoder); 510 COPY_ARR(d, s, isRep0Long) \
510 COPY_ARR(dest, p, isRep); 511 COPY_ARR(d, s, posSlotEncoder) \
511 COPY_ARR(dest, p, isRepG0); 512 COPY_ARR(d, s, posEncoders) \
512 COPY_ARR(dest, p, isRepG1); 513 (d)->lenProbs = (s)->lenProbs; \
513 COPY_ARR(dest, p, isRepG2); 514 (d)->repLenProbs = (s)->repLenProbs; \
514 COPY_ARR(dest, p, isMatch); 515 memcpy((d)->litProbs, (s)->litProbs, ((UInt32)0x300 << (p)->lclp) * sizeof(CLzmaProb));
515 COPY_ARR(dest, p, isRep0Long); 516
516 COPY_ARR(dest, p, posSlotEncoder); 517void LzmaEnc_SaveState(CLzmaEncHandle p)
517 COPY_ARR(dest, p, posEncoders); 518{
518 519 // GET_CLzmaEnc_p
519 memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << p->lclp) * sizeof(CLzmaProb)); 520 CSaveState *v = &p->saveState;
521 COPY_LZMA_ENC_STATE(v, p, p)
520} 522}
521 523
522 524void LzmaEnc_RestoreState(CLzmaEncHandle p)
523void LzmaEnc_RestoreState(CLzmaEncHandle pp)
524{ 525{
525 CLzmaEnc *dest = (CLzmaEnc *)pp; 526 // GET_CLzmaEnc_p
526 const CSaveState *p = &dest->saveState; 527 const CSaveState *v = &p->saveState;
527 528 COPY_LZMA_ENC_STATE(p, v, p)
528 dest->state = p->state;
529
530 dest->lenProbs = p->lenProbs;
531 dest->repLenProbs = p->repLenProbs;
532
533 COPY_ARR(dest, p, reps);
534
535 COPY_ARR(dest, p, posAlignEncoder);
536 COPY_ARR(dest, p, isRep);
537 COPY_ARR(dest, p, isRepG0);
538 COPY_ARR(dest, p, isRepG1);
539 COPY_ARR(dest, p, isRepG2);
540 COPY_ARR(dest, p, isMatch);
541 COPY_ARR(dest, p, isRep0Long);
542 COPY_ARR(dest, p, posSlotEncoder);
543 COPY_ARR(dest, p, posEncoders);
544
545 memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << dest->lclp) * sizeof(CLzmaProb));
546} 529}
547 530
548 531
549 532Z7_NO_INLINE
550SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2) 533SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props2)
551{ 534{
552 CLzmaEnc *p = (CLzmaEnc *)pp; 535 // GET_CLzmaEnc_p
553 CLzmaEncProps props = *props2; 536 CLzmaEncProps props = *props2;
554 LzmaEncProps_Normalize(&props); 537 LzmaEncProps_Normalize(&props);
555 538
@@ -585,6 +568,7 @@ SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
585 p->fastMode = (props.algo == 0); 568 p->fastMode = (props.algo == 0);
586 // p->_maxMode = True; 569 // p->_maxMode = True;
587 MFB.btMode = (Byte)(props.btMode ? 1 : 0); 570 MFB.btMode = (Byte)(props.btMode ? 1 : 0);
571 // MFB.btMode = (Byte)(props.btMode);
588 { 572 {
589 unsigned numHashBytes = 4; 573 unsigned numHashBytes = 4;
590 if (props.btMode) 574 if (props.btMode)
@@ -595,13 +579,15 @@ SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
595 if (props.numHashBytes >= 5) numHashBytes = 5; 579 if (props.numHashBytes >= 5) numHashBytes = 5;
596 580
597 MFB.numHashBytes = numHashBytes; 581 MFB.numHashBytes = numHashBytes;
582 // MFB.numHashBytes_Min = 2;
583 MFB.numHashOutBits = (Byte)props.numHashOutBits;
598 } 584 }
599 585
600 MFB.cutValue = props.mc; 586 MFB.cutValue = props.mc;
601 587
602 p->writeEndMark = (BoolInt)props.writeEndMark; 588 p->writeEndMark = (BoolInt)props.writeEndMark;
603 589
604 #ifndef _7ZIP_ST 590 #ifndef Z7_ST
605 /* 591 /*
606 if (newMultiThread != _multiThread) 592 if (newMultiThread != _multiThread)
607 { 593 {
@@ -618,9 +604,9 @@ SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
618} 604}
619 605
620 606
621void LzmaEnc_SetDataSize(CLzmaEncHandle pp, UInt64 expectedDataSiize) 607void LzmaEnc_SetDataSize(CLzmaEncHandle p, UInt64 expectedDataSiize)
622{ 608{
623 CLzmaEnc *p = (CLzmaEnc *)pp; 609 // GET_CLzmaEnc_p
624 MFB.expectedDataSize = expectedDataSiize; 610 MFB.expectedDataSize = expectedDataSiize;
625} 611}
626 612
@@ -684,7 +670,7 @@ static void RangeEnc_Init(CRangeEnc *p)
684 p->res = SZ_OK; 670 p->res = SZ_OK;
685} 671}
686 672
687MY_NO_INLINE static void RangeEnc_FlushStream(CRangeEnc *p) 673Z7_NO_INLINE static void RangeEnc_FlushStream(CRangeEnc *p)
688{ 674{
689 const size_t num = (size_t)(p->buf - p->bufBase); 675 const size_t num = (size_t)(p->buf - p->bufBase);
690 if (p->res == SZ_OK) 676 if (p->res == SZ_OK)
@@ -696,7 +682,7 @@ MY_NO_INLINE static void RangeEnc_FlushStream(CRangeEnc *p)
696 p->buf = p->bufBase; 682 p->buf = p->bufBase;
697} 683}
698 684
699MY_NO_INLINE static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p) 685Z7_NO_INLINE static void Z7_FASTCALL RangeEnc_ShiftLow(CRangeEnc *p)
700{ 686{
701 UInt32 low = (UInt32)p->low; 687 UInt32 low = (UInt32)p->low;
702 unsigned high = (unsigned)(p->low >> 32); 688 unsigned high = (unsigned)(p->low >> 32);
@@ -741,9 +727,9 @@ static void RangeEnc_FlushData(CRangeEnc *p)
741 ttt = *(prob); \ 727 ttt = *(prob); \
742 newBound = (range >> kNumBitModelTotalBits) * ttt; 728 newBound = (range >> kNumBitModelTotalBits) * ttt;
743 729
744// #define _LZMA_ENC_USE_BRANCH 730// #define Z7_LZMA_ENC_USE_BRANCH
745 731
746#ifdef _LZMA_ENC_USE_BRANCH 732#ifdef Z7_LZMA_ENC_USE_BRANCH
747 733
748#define RC_BIT(p, prob, bit) { \ 734#define RC_BIT(p, prob, bit) { \
749 RC_BIT_PRE(p, prob) \ 735 RC_BIT_PRE(p, prob) \
@@ -811,7 +797,7 @@ static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 sym)
811 CLzmaProb *prob = probs + (sym >> 8); 797 CLzmaProb *prob = probs + (sym >> 8);
812 UInt32 bit = (sym >> 7) & 1; 798 UInt32 bit = (sym >> 7) & 1;
813 sym <<= 1; 799 sym <<= 1;
814 RC_BIT(p, prob, bit); 800 RC_BIT(p, prob, bit)
815 } 801 }
816 while (sym < 0x10000); 802 while (sym < 0x10000);
817 p->range = range; 803 p->range = range;
@@ -833,7 +819,7 @@ static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 sym, UIn
833 bit = (sym >> 7) & 1; 819 bit = (sym >> 7) & 1;
834 sym <<= 1; 820 sym <<= 1;
835 offs &= ~(matchByte ^ sym); 821 offs &= ~(matchByte ^ sym);
836 RC_BIT(p, prob, bit); 822 RC_BIT(p, prob, bit)
837 } 823 }
838 while (sym < 0x10000); 824 while (sym < 0x10000);
839 p->range = range; 825 p->range = range;
@@ -867,10 +853,10 @@ static void LzmaEnc_InitPriceTables(CProbPrice *ProbPrices)
867 853
868 854
869#define GET_PRICE(prob, bit) \ 855#define GET_PRICE(prob, bit) \
870 p->ProbPrices[((prob) ^ (unsigned)(((-(int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; 856 p->ProbPrices[((prob) ^ (unsigned)(((-(int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]
871 857
872#define GET_PRICEa(prob, bit) \ 858#define GET_PRICEa(prob, bit) \
873 ProbPrices[((prob) ^ (unsigned)((-((int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; 859 ProbPrices[((prob) ^ (unsigned)((-((int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]
874 860
875#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits] 861#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
876#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] 862#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
@@ -921,7 +907,7 @@ static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, unsigned numBi
921 unsigned bit = sym & 1; 907 unsigned bit = sym & 1;
922 // RangeEnc_EncodeBit(rc, probs + m, bit); 908 // RangeEnc_EncodeBit(rc, probs + m, bit);
923 sym >>= 1; 909 sym >>= 1;
924 RC_BIT(rc, probs + m, bit); 910 RC_BIT(rc, probs + m, bit)
925 m = (m << 1) | bit; 911 m = (m << 1) | bit;
926 } 912 }
927 while (--numBits); 913 while (--numBits);
@@ -944,15 +930,15 @@ static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, unsigned sym, unsigned posS
944 UInt32 range, ttt, newBound; 930 UInt32 range, ttt, newBound;
945 CLzmaProb *probs = p->low; 931 CLzmaProb *probs = p->low;
946 range = rc->range; 932 range = rc->range;
947 RC_BIT_PRE(rc, probs); 933 RC_BIT_PRE(rc, probs)
948 if (sym >= kLenNumLowSymbols) 934 if (sym >= kLenNumLowSymbols)
949 { 935 {
950 RC_BIT_1(rc, probs); 936 RC_BIT_1(rc, probs)
951 probs += kLenNumLowSymbols; 937 probs += kLenNumLowSymbols;
952 RC_BIT_PRE(rc, probs); 938 RC_BIT_PRE(rc, probs)
953 if (sym >= kLenNumLowSymbols * 2) 939 if (sym >= kLenNumLowSymbols * 2)
954 { 940 {
955 RC_BIT_1(rc, probs); 941 RC_BIT_1(rc, probs)
956 rc->range = range; 942 rc->range = range;
957 // RcTree_Encode(rc, p->high, kLenNumHighBits, sym - kLenNumLowSymbols * 2); 943 // RcTree_Encode(rc, p->high, kLenNumHighBits, sym - kLenNumLowSymbols * 2);
958 LitEnc_Encode(rc, p->high, sym - kLenNumLowSymbols * 2); 944 LitEnc_Encode(rc, p->high, sym - kLenNumLowSymbols * 2);
@@ -965,11 +951,11 @@ static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, unsigned sym, unsigned posS
965 { 951 {
966 unsigned m; 952 unsigned m;
967 unsigned bit; 953 unsigned bit;
968 RC_BIT_0(rc, probs); 954 RC_BIT_0(rc, probs)
969 probs += (posState << (1 + kLenNumLowBits)); 955 probs += (posState << (1 + kLenNumLowBits));
970 bit = (sym >> 2) ; RC_BIT(rc, probs + 1, bit); m = (1 << 1) + bit; 956 bit = (sym >> 2) ; RC_BIT(rc, probs + 1, bit) m = (1 << 1) + bit;
971 bit = (sym >> 1) & 1; RC_BIT(rc, probs + m, bit); m = (m << 1) + bit; 957 bit = (sym >> 1) & 1; RC_BIT(rc, probs + m, bit) m = (m << 1) + bit;
972 bit = sym & 1; RC_BIT(rc, probs + m, bit); 958 bit = sym & 1; RC_BIT(rc, probs + m, bit)
973 rc->range = range; 959 rc->range = range;
974 } 960 }
975} 961}
@@ -990,7 +976,7 @@ static void SetPrices_3(const CLzmaProb *probs, UInt32 startPrice, UInt32 *price
990} 976}
991 977
992 978
993MY_NO_INLINE static void MY_FAST_CALL LenPriceEnc_UpdateTables( 979Z7_NO_INLINE static void Z7_FASTCALL LenPriceEnc_UpdateTables(
994 CLenPriceEnc *p, 980 CLenPriceEnc *p,
995 unsigned numPosStates, 981 unsigned numPosStates,
996 const CLenEnc *enc, 982 const CLenEnc *enc,
@@ -1152,7 +1138,7 @@ static unsigned ReadMatchDistances(CLzmaEnc *p, unsigned *numPairsRes)
1152 + GET_PRICE_1(p->isRep[state]) \ 1138 + GET_PRICE_1(p->isRep[state]) \
1153 + GET_PRICE_0(p->isRepG0[state]) 1139 + GET_PRICE_0(p->isRepG0[state])
1154 1140
1155MY_FORCE_INLINE 1141Z7_FORCE_INLINE
1156static UInt32 GetPrice_PureRep(const CLzmaEnc *p, unsigned repIndex, size_t state, size_t posState) 1142static UInt32 GetPrice_PureRep(const CLzmaEnc *p, unsigned repIndex, size_t state, size_t posState)
1157{ 1143{
1158 UInt32 price; 1144 UInt32 price;
@@ -1331,7 +1317,7 @@ static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)
1331 LitEnc_GetPrice(probs, curByte, p->ProbPrices)); 1317 LitEnc_GetPrice(probs, curByte, p->ProbPrices));
1332 } 1318 }
1333 1319
1334 MakeAs_Lit(&p->opt[1]); 1320 MakeAs_Lit(&p->opt[1])
1335 1321
1336 matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]); 1322 matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
1337 repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]); 1323 repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
@@ -1343,7 +1329,7 @@ static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)
1343 if (shortRepPrice < p->opt[1].price) 1329 if (shortRepPrice < p->opt[1].price)
1344 { 1330 {
1345 p->opt[1].price = shortRepPrice; 1331 p->opt[1].price = shortRepPrice;
1346 MakeAs_ShortRep(&p->opt[1]); 1332 MakeAs_ShortRep(&p->opt[1])
1347 } 1333 }
1348 if (last < 2) 1334 if (last < 2)
1349 { 1335 {
@@ -1410,7 +1396,7 @@ static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)
1410 else 1396 else
1411 { 1397 {
1412 unsigned slot; 1398 unsigned slot;
1413 GetPosSlot2(dist, slot); 1399 GetPosSlot2(dist, slot)
1414 price += p->alignPrices[dist & kAlignMask]; 1400 price += p->alignPrices[dist & kAlignMask];
1415 price += p->posSlotPrices[lenToPosState][slot]; 1401 price += p->posSlotPrices[lenToPosState][slot];
1416 } 1402 }
@@ -1486,7 +1472,7 @@ static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)
1486 unsigned delta = best - cur; 1472 unsigned delta = best - cur;
1487 if (delta != 0) 1473 if (delta != 0)
1488 { 1474 {
1489 MOVE_POS(p, delta); 1475 MOVE_POS(p, delta)
1490 } 1476 }
1491 } 1477 }
1492 cur = best; 1478 cur = best;
@@ -1633,7 +1619,7 @@ static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)
1633 { 1619 {
1634 nextOpt->price = litPrice; 1620 nextOpt->price = litPrice;
1635 nextOpt->len = 1; 1621 nextOpt->len = 1;
1636 MakeAs_Lit(nextOpt); 1622 MakeAs_Lit(nextOpt)
1637 nextIsLit = True; 1623 nextIsLit = True;
1638 } 1624 }
1639 } 1625 }
@@ -1667,7 +1653,7 @@ static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)
1667 { 1653 {
1668 nextOpt->price = shortRepPrice; 1654 nextOpt->price = shortRepPrice;
1669 nextOpt->len = 1; 1655 nextOpt->len = 1;
1670 MakeAs_ShortRep(nextOpt); 1656 MakeAs_ShortRep(nextOpt)
1671 nextIsLit = False; 1657 nextIsLit = False;
1672 } 1658 }
1673 } 1659 }
@@ -1871,7 +1857,7 @@ static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)
1871 dist = MATCHES[(size_t)offs + 1]; 1857 dist = MATCHES[(size_t)offs + 1];
1872 1858
1873 // if (dist >= kNumFullDistances) 1859 // if (dist >= kNumFullDistances)
1874 GetPosSlot2(dist, posSlot); 1860 GetPosSlot2(dist, posSlot)
1875 1861
1876 for (len = /*2*/ startLen; ; len++) 1862 for (len = /*2*/ startLen; ; len++)
1877 { 1863 {
@@ -1962,7 +1948,7 @@ static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)
1962 break; 1948 break;
1963 dist = MATCHES[(size_t)offs + 1]; 1949 dist = MATCHES[(size_t)offs + 1];
1964 // if (dist >= kNumFullDistances) 1950 // if (dist >= kNumFullDistances)
1965 GetPosSlot2(dist, posSlot); 1951 GetPosSlot2(dist, posSlot)
1966 } 1952 }
1967 } 1953 }
1968 } 1954 }
@@ -2138,7 +2124,7 @@ static void WriteEndMarker(CLzmaEnc *p, unsigned posState)
2138 { 2124 {
2139 UInt32 ttt, newBound; 2125 UInt32 ttt, newBound;
2140 RC_BIT_PRE(p, probs + m) 2126 RC_BIT_PRE(p, probs + m)
2141 RC_BIT_1(&p->rc, probs + m); 2127 RC_BIT_1(&p->rc, probs + m)
2142 m = (m << 1) + 1; 2128 m = (m << 1) + 1;
2143 } 2129 }
2144 while (m < (1 << kNumPosSlotBits)); 2130 while (m < (1 << kNumPosSlotBits));
@@ -2163,7 +2149,7 @@ static void WriteEndMarker(CLzmaEnc *p, unsigned posState)
2163 { 2149 {
2164 UInt32 ttt, newBound; 2150 UInt32 ttt, newBound;
2165 RC_BIT_PRE(p, probs + m) 2151 RC_BIT_PRE(p, probs + m)
2166 RC_BIT_1(&p->rc, probs + m); 2152 RC_BIT_1(&p->rc, probs + m)
2167 m = (m << 1) + 1; 2153 m = (m << 1) + 1;
2168 } 2154 }
2169 while (m < kAlignTableSize); 2155 while (m < kAlignTableSize);
@@ -2179,7 +2165,7 @@ static SRes CheckErrors(CLzmaEnc *p)
2179 if (p->rc.res != SZ_OK) 2165 if (p->rc.res != SZ_OK)
2180 p->result = SZ_ERROR_WRITE; 2166 p->result = SZ_ERROR_WRITE;
2181 2167
2182 #ifndef _7ZIP_ST 2168 #ifndef Z7_ST
2183 if ( 2169 if (
2184 // p->mf_Failure || 2170 // p->mf_Failure ||
2185 (p->mtMode && 2171 (p->mtMode &&
@@ -2187,7 +2173,7 @@ static SRes CheckErrors(CLzmaEnc *p)
2187 p->matchFinderMt.failure_LZ_BT)) 2173 p->matchFinderMt.failure_LZ_BT))
2188 ) 2174 )
2189 { 2175 {
2190 p->result = MY_HRES_ERROR__INTERNAL_ERROR; 2176 p->result = MY_HRES_ERROR_INTERNAL_ERROR;
2191 // printf("\nCheckErrors p->matchFinderMt.failureLZ\n"); 2177 // printf("\nCheckErrors p->matchFinderMt.failureLZ\n");
2192 } 2178 }
2193 #endif 2179 #endif
@@ -2201,7 +2187,7 @@ static SRes CheckErrors(CLzmaEnc *p)
2201} 2187}
2202 2188
2203 2189
2204MY_NO_INLINE static SRes Flush(CLzmaEnc *p, UInt32 nowPos) 2190Z7_NO_INLINE static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
2205{ 2191{
2206 /* ReleaseMFStream(); */ 2192 /* ReleaseMFStream(); */
2207 p->finished = True; 2193 p->finished = True;
@@ -2213,7 +2199,7 @@ MY_NO_INLINE static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
2213} 2199}
2214 2200
2215 2201
2216MY_NO_INLINE static void FillAlignPrices(CLzmaEnc *p) 2202Z7_NO_INLINE static void FillAlignPrices(CLzmaEnc *p)
2217{ 2203{
2218 unsigned i; 2204 unsigned i;
2219 const CProbPrice *ProbPrices = p->ProbPrices; 2205 const CProbPrice *ProbPrices = p->ProbPrices;
@@ -2237,7 +2223,7 @@ MY_NO_INLINE static void FillAlignPrices(CLzmaEnc *p)
2237} 2223}
2238 2224
2239 2225
2240MY_NO_INLINE static void FillDistancesPrices(CLzmaEnc *p) 2226Z7_NO_INLINE static void FillDistancesPrices(CLzmaEnc *p)
2241{ 2227{
2242 // int y; for (y = 0; y < 100; y++) { 2228 // int y; for (y = 0; y < 100; y++) {
2243 2229
@@ -2337,7 +2323,7 @@ static void LzmaEnc_Construct(CLzmaEnc *p)
2337 RangeEnc_Construct(&p->rc); 2323 RangeEnc_Construct(&p->rc);
2338 MatchFinder_Construct(&MFB); 2324 MatchFinder_Construct(&MFB);
2339 2325
2340 #ifndef _7ZIP_ST 2326 #ifndef Z7_ST
2341 p->matchFinderMt.MatchFinder = &MFB; 2327 p->matchFinderMt.MatchFinder = &MFB;
2342 MatchFinderMt_Construct(&p->matchFinderMt); 2328 MatchFinderMt_Construct(&p->matchFinderMt);
2343 #endif 2329 #endif
@@ -2345,7 +2331,7 @@ static void LzmaEnc_Construct(CLzmaEnc *p)
2345 { 2331 {
2346 CLzmaEncProps props; 2332 CLzmaEncProps props;
2347 LzmaEncProps_Init(&props); 2333 LzmaEncProps_Init(&props);
2348 LzmaEnc_SetProps(p, &props); 2334 LzmaEnc_SetProps((CLzmaEncHandle)(void *)p, &props);
2349 } 2335 }
2350 2336
2351 #ifndef LZMA_LOG_BSR 2337 #ifndef LZMA_LOG_BSR
@@ -2376,7 +2362,7 @@ static void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAllocPtr alloc)
2376 2362
2377static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAllocPtr alloc, ISzAllocPtr allocBig) 2363static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAllocPtr alloc, ISzAllocPtr allocBig)
2378{ 2364{
2379 #ifndef _7ZIP_ST 2365 #ifndef Z7_ST
2380 MatchFinderMt_Destruct(&p->matchFinderMt, allocBig); 2366 MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
2381 #endif 2367 #endif
2382 2368
@@ -2387,21 +2373,22 @@ static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAllocPtr alloc, ISzAllocPtr allocBi
2387 2373
2388void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig) 2374void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig)
2389{ 2375{
2390 LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig); 2376 // GET_CLzmaEnc_p
2377 LzmaEnc_Destruct(p, alloc, allocBig);
2391 ISzAlloc_Free(alloc, p); 2378 ISzAlloc_Free(alloc, p);
2392} 2379}
2393 2380
2394 2381
2395MY_NO_INLINE 2382Z7_NO_INLINE
2396static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpackSize) 2383static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpackSize)
2397{ 2384{
2398 UInt32 nowPos32, startPos32; 2385 UInt32 nowPos32, startPos32;
2399 if (p->needInit) 2386 if (p->needInit)
2400 { 2387 {
2401 #ifndef _7ZIP_ST 2388 #ifndef Z7_ST
2402 if (p->mtMode) 2389 if (p->mtMode)
2403 { 2390 {
2404 RINOK(MatchFinderMt_InitMt(&p->matchFinderMt)); 2391 RINOK(MatchFinderMt_InitMt(&p->matchFinderMt))
2405 } 2392 }
2406 #endif 2393 #endif
2407 p->matchFinder.Init(p->matchFinderObj); 2394 p->matchFinder.Init(p->matchFinderObj);
@@ -2410,7 +2397,7 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpa
2410 2397
2411 if (p->finished) 2398 if (p->finished)
2412 return p->result; 2399 return p->result;
2413 RINOK(CheckErrors(p)); 2400 RINOK(CheckErrors(p))
2414 2401
2415 nowPos32 = (UInt32)p->nowPos64; 2402 nowPos32 = (UInt32)p->nowPos64;
2416 startPos32 = nowPos32; 2403 startPos32 = nowPos32;
@@ -2473,7 +2460,7 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpa
2473 const Byte *data; 2460 const Byte *data;
2474 unsigned state; 2461 unsigned state;
2475 2462
2476 RC_BIT_0(&p->rc, probs); 2463 RC_BIT_0(&p->rc, probs)
2477 p->rc.range = range; 2464 p->rc.range = range;
2478 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; 2465 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
2479 probs = LIT_PROBS(nowPos32, *(data - 1)); 2466 probs = LIT_PROBS(nowPos32, *(data - 1));
@@ -2487,53 +2474,53 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpa
2487 } 2474 }
2488 else 2475 else
2489 { 2476 {
2490 RC_BIT_1(&p->rc, probs); 2477 RC_BIT_1(&p->rc, probs)
2491 probs = &p->isRep[p->state]; 2478 probs = &p->isRep[p->state];
2492 RC_BIT_PRE(&p->rc, probs) 2479 RC_BIT_PRE(&p->rc, probs)
2493 2480
2494 if (dist < LZMA_NUM_REPS) 2481 if (dist < LZMA_NUM_REPS)
2495 { 2482 {
2496 RC_BIT_1(&p->rc, probs); 2483 RC_BIT_1(&p->rc, probs)
2497 probs = &p->isRepG0[p->state]; 2484 probs = &p->isRepG0[p->state];
2498 RC_BIT_PRE(&p->rc, probs) 2485 RC_BIT_PRE(&p->rc, probs)
2499 if (dist == 0) 2486 if (dist == 0)
2500 { 2487 {
2501 RC_BIT_0(&p->rc, probs); 2488 RC_BIT_0(&p->rc, probs)
2502 probs = &p->isRep0Long[p->state][posState]; 2489 probs = &p->isRep0Long[p->state][posState];
2503 RC_BIT_PRE(&p->rc, probs) 2490 RC_BIT_PRE(&p->rc, probs)
2504 if (len != 1) 2491 if (len != 1)
2505 { 2492 {
2506 RC_BIT_1_BASE(&p->rc, probs); 2493 RC_BIT_1_BASE(&p->rc, probs)
2507 } 2494 }
2508 else 2495 else
2509 { 2496 {
2510 RC_BIT_0_BASE(&p->rc, probs); 2497 RC_BIT_0_BASE(&p->rc, probs)
2511 p->state = kShortRepNextStates[p->state]; 2498 p->state = kShortRepNextStates[p->state];
2512 } 2499 }
2513 } 2500 }
2514 else 2501 else
2515 { 2502 {
2516 RC_BIT_1(&p->rc, probs); 2503 RC_BIT_1(&p->rc, probs)
2517 probs = &p->isRepG1[p->state]; 2504 probs = &p->isRepG1[p->state];
2518 RC_BIT_PRE(&p->rc, probs) 2505 RC_BIT_PRE(&p->rc, probs)
2519 if (dist == 1) 2506 if (dist == 1)
2520 { 2507 {
2521 RC_BIT_0_BASE(&p->rc, probs); 2508 RC_BIT_0_BASE(&p->rc, probs)
2522 dist = p->reps[1]; 2509 dist = p->reps[1];
2523 } 2510 }
2524 else 2511 else
2525 { 2512 {
2526 RC_BIT_1(&p->rc, probs); 2513 RC_BIT_1(&p->rc, probs)
2527 probs = &p->isRepG2[p->state]; 2514 probs = &p->isRepG2[p->state];
2528 RC_BIT_PRE(&p->rc, probs) 2515 RC_BIT_PRE(&p->rc, probs)
2529 if (dist == 2) 2516 if (dist == 2)
2530 { 2517 {
2531 RC_BIT_0_BASE(&p->rc, probs); 2518 RC_BIT_0_BASE(&p->rc, probs)
2532 dist = p->reps[2]; 2519 dist = p->reps[2];
2533 } 2520 }
2534 else 2521 else
2535 { 2522 {
2536 RC_BIT_1_BASE(&p->rc, probs); 2523 RC_BIT_1_BASE(&p->rc, probs)
2537 dist = p->reps[3]; 2524 dist = p->reps[3];
2538 p->reps[3] = p->reps[2]; 2525 p->reps[3] = p->reps[2];
2539 } 2526 }
@@ -2557,7 +2544,7 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpa
2557 else 2544 else
2558 { 2545 {
2559 unsigned posSlot; 2546 unsigned posSlot;
2560 RC_BIT_0(&p->rc, probs); 2547 RC_BIT_0(&p->rc, probs)
2561 p->rc.range = range; 2548 p->rc.range = range;
2562 p->state = kMatchNextStates[p->state]; 2549 p->state = kMatchNextStates[p->state];
2563 2550
@@ -2571,7 +2558,7 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpa
2571 p->reps[0] = dist + 1; 2558 p->reps[0] = dist + 1;
2572 2559
2573 p->matchPriceCount++; 2560 p->matchPriceCount++;
2574 GetPosSlot(dist, posSlot); 2561 GetPosSlot(dist, posSlot)
2575 // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], posSlot); 2562 // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], posSlot);
2576 { 2563 {
2577 UInt32 sym = (UInt32)posSlot + (1 << kNumPosSlotBits); 2564 UInt32 sym = (UInt32)posSlot + (1 << kNumPosSlotBits);
@@ -2582,7 +2569,7 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpa
2582 CLzmaProb *prob = probs + (sym >> kNumPosSlotBits); 2569 CLzmaProb *prob = probs + (sym >> kNumPosSlotBits);
2583 UInt32 bit = (sym >> (kNumPosSlotBits - 1)) & 1; 2570 UInt32 bit = (sym >> (kNumPosSlotBits - 1)) & 1;
2584 sym <<= 1; 2571 sym <<= 1;
2585 RC_BIT(&p->rc, prob, bit); 2572 RC_BIT(&p->rc, prob, bit)
2586 } 2573 }
2587 while (sym < (1 << kNumPosSlotBits * 2)); 2574 while (sym < (1 << kNumPosSlotBits * 2));
2588 p->rc.range = range; 2575 p->rc.range = range;
@@ -2626,10 +2613,10 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpa
2626 { 2613 {
2627 unsigned m = 1; 2614 unsigned m = 1;
2628 unsigned bit; 2615 unsigned bit;
2629 bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit; 2616 bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit) m = (m << 1) + bit;
2630 bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit; 2617 bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit) m = (m << 1) + bit;
2631 bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit; 2618 bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit) m = (m << 1) + bit;
2632 bit = dist & 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); 2619 bit = dist & 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit)
2633 p->rc.range = range; 2620 p->rc.range = range;
2634 // p->alignPriceCount++; 2621 // p->alignPriceCount++;
2635 } 2622 }
@@ -2704,7 +2691,7 @@ static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAllocPtr alloc,
2704 if (!RangeEnc_Alloc(&p->rc, alloc)) 2691 if (!RangeEnc_Alloc(&p->rc, alloc))
2705 return SZ_ERROR_MEM; 2692 return SZ_ERROR_MEM;
2706 2693
2707 #ifndef _7ZIP_ST 2694 #ifndef Z7_ST
2708 p->mtMode = (p->multiThread && !p->fastMode && (MFB.btMode != 0)); 2695 p->mtMode = (p->multiThread && !p->fastMode && (MFB.btMode != 0));
2709 #endif 2696 #endif
2710 2697
@@ -2748,15 +2735,14 @@ static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAllocPtr alloc,
2748 (numFastBytes + LZMA_MATCH_LEN_MAX + 1) 2735 (numFastBytes + LZMA_MATCH_LEN_MAX + 1)
2749 */ 2736 */
2750 2737
2751 #ifndef _7ZIP_ST 2738 #ifndef Z7_ST
2752 if (p->mtMode) 2739 if (p->mtMode)
2753 { 2740 {
2754 RINOK(MatchFinderMt_Create(&p->matchFinderMt, dictSize, beforeSize, 2741 RINOK(MatchFinderMt_Create(&p->matchFinderMt, dictSize, beforeSize,
2755 p->numFastBytes, LZMA_MATCH_LEN_MAX + 1 /* 18.04 */ 2742 p->numFastBytes, LZMA_MATCH_LEN_MAX + 1 /* 18.04 */
2756 , allocBig)); 2743 , allocBig))
2757 p->matchFinderObj = &p->matchFinderMt; 2744 p->matchFinderObj = &p->matchFinderMt;
2758 MFB.bigHash = (Byte)( 2745 MFB.bigHash = (Byte)(MFB.hashMask >= 0xFFFFFF ? 1 : 0);
2759 (p->dictSize > kBigHashDicLimit && MFB.hashMask >= 0xFFFFFF) ? 1 : 0);
2760 MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder); 2746 MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
2761 } 2747 }
2762 else 2748 else
@@ -2872,59 +2858,53 @@ static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAllocPtr
2872 2858
2873 p->finished = False; 2859 p->finished = False;
2874 p->result = SZ_OK; 2860 p->result = SZ_OK;
2875 RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig)); 2861 p->nowPos64 = 0;
2862 p->needInit = 1;
2863 RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig))
2876 LzmaEnc_Init(p); 2864 LzmaEnc_Init(p);
2877 LzmaEnc_InitPrices(p); 2865 LzmaEnc_InitPrices(p);
2878 p->nowPos64 = 0;
2879 return SZ_OK; 2866 return SZ_OK;
2880} 2867}
2881 2868
2882static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, 2869static SRes LzmaEnc_Prepare(CLzmaEncHandle p,
2870 ISeqOutStreamPtr outStream,
2871 ISeqInStreamPtr inStream,
2883 ISzAllocPtr alloc, ISzAllocPtr allocBig) 2872 ISzAllocPtr alloc, ISzAllocPtr allocBig)
2884{ 2873{
2885 CLzmaEnc *p = (CLzmaEnc *)pp; 2874 // GET_CLzmaEnc_p
2886 MFB.stream = inStream; 2875 MatchFinder_SET_STREAM(&MFB, inStream)
2887 p->needInit = 1;
2888 p->rc.outStream = outStream; 2876 p->rc.outStream = outStream;
2889 return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig); 2877 return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
2890} 2878}
2891 2879
2892SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, 2880SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle p,
2893 ISeqInStream *inStream, UInt32 keepWindowSize, 2881 ISeqInStreamPtr inStream, UInt32 keepWindowSize,
2894 ISzAllocPtr alloc, ISzAllocPtr allocBig) 2882 ISzAllocPtr alloc, ISzAllocPtr allocBig)
2895{ 2883{
2896 CLzmaEnc *p = (CLzmaEnc *)pp; 2884 // GET_CLzmaEnc_p
2897 MFB.stream = inStream; 2885 MatchFinder_SET_STREAM(&MFB, inStream)
2898 p->needInit = 1;
2899 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); 2886 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
2900} 2887}
2901 2888
2902static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen) 2889SRes LzmaEnc_MemPrepare(CLzmaEncHandle p,
2903{ 2890 const Byte *src, SizeT srcLen,
2904 MFB.directInput = 1; 2891 UInt32 keepWindowSize,
2905 MFB.bufferBase = (Byte *)src; 2892 ISzAllocPtr alloc, ISzAllocPtr allocBig)
2906 MFB.directInputRem = srcLen;
2907}
2908
2909SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
2910 UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig)
2911{ 2893{
2912 CLzmaEnc *p = (CLzmaEnc *)pp; 2894 // GET_CLzmaEnc_p
2913 LzmaEnc_SetInputBuf(p, src, srcLen); 2895 MatchFinder_SET_DIRECT_INPUT_BUF(&MFB, src, srcLen)
2914 p->needInit = 1; 2896 LzmaEnc_SetDataSize(p, srcLen);
2915
2916 LzmaEnc_SetDataSize(pp, srcLen);
2917 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); 2897 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
2918} 2898}
2919 2899
2920void LzmaEnc_Finish(CLzmaEncHandle pp) 2900void LzmaEnc_Finish(CLzmaEncHandle p)
2921{ 2901{
2922 #ifndef _7ZIP_ST 2902 #ifndef Z7_ST
2923 CLzmaEnc *p = (CLzmaEnc *)pp; 2903 // GET_CLzmaEnc_p
2924 if (p->mtMode) 2904 if (p->mtMode)
2925 MatchFinderMt_ReleaseStream(&p->matchFinderMt); 2905 MatchFinderMt_ReleaseStream(&p->matchFinderMt);
2926 #else 2906 #else
2927 UNUSED_VAR(pp); 2907 UNUSED_VAR(p)
2928 #endif 2908 #endif
2929} 2909}
2930 2910
@@ -2933,13 +2913,13 @@ typedef struct
2933{ 2913{
2934 ISeqOutStream vt; 2914 ISeqOutStream vt;
2935 Byte *data; 2915 Byte *data;
2936 SizeT rem; 2916 size_t rem;
2937 BoolInt overflow; 2917 BoolInt overflow;
2938} CLzmaEnc_SeqOutStreamBuf; 2918} CLzmaEnc_SeqOutStreamBuf;
2939 2919
2940static size_t SeqOutStreamBuf_Write(const ISeqOutStream *pp, const void *data, size_t size) 2920static size_t SeqOutStreamBuf_Write(ISeqOutStreamPtr pp, const void *data, size_t size)
2941{ 2921{
2942 CLzmaEnc_SeqOutStreamBuf *p = CONTAINER_FROM_VTBL(pp, CLzmaEnc_SeqOutStreamBuf, vt); 2922 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CLzmaEnc_SeqOutStreamBuf)
2943 if (p->rem < size) 2923 if (p->rem < size)
2944 { 2924 {
2945 size = p->rem; 2925 size = p->rem;
@@ -2956,25 +2936,25 @@ static size_t SeqOutStreamBuf_Write(const ISeqOutStream *pp, const void *data, s
2956 2936
2957 2937
2958/* 2938/*
2959UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp) 2939UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle p)
2960{ 2940{
2961 const CLzmaEnc *p = (CLzmaEnc *)pp; 2941 GET_const_CLzmaEnc_p
2962 return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); 2942 return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
2963} 2943}
2964*/ 2944*/
2965 2945
2966const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp) 2946const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle p)
2967{ 2947{
2968 const CLzmaEnc *p = (CLzmaEnc *)pp; 2948 // GET_const_CLzmaEnc_p
2969 return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; 2949 return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
2970} 2950}
2971 2951
2972 2952
2973// (desiredPackSize == 0) is not allowed 2953// (desiredPackSize == 0) is not allowed
2974SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, BoolInt reInit, 2954SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle p, BoolInt reInit,
2975 Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize) 2955 Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
2976{ 2956{
2977 CLzmaEnc *p = (CLzmaEnc *)pp; 2957 // GET_CLzmaEnc_p
2978 UInt64 nowPos64; 2958 UInt64 nowPos64;
2979 SRes res; 2959 SRes res;
2980 CLzmaEnc_SeqOutStreamBuf outStream; 2960 CLzmaEnc_SeqOutStreamBuf outStream;
@@ -3006,12 +2986,12 @@ SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, BoolInt reInit,
3006} 2986}
3007 2987
3008 2988
3009MY_NO_INLINE 2989Z7_NO_INLINE
3010static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress) 2990static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgressPtr progress)
3011{ 2991{
3012 SRes res = SZ_OK; 2992 SRes res = SZ_OK;
3013 2993
3014 #ifndef _7ZIP_ST 2994 #ifndef Z7_ST
3015 Byte allocaDummy[0x300]; 2995 Byte allocaDummy[0x300];
3016 allocaDummy[0] = 0; 2996 allocaDummy[0] = 0;
3017 allocaDummy[1] = allocaDummy[0]; 2997 allocaDummy[1] = allocaDummy[0];
@@ -3033,7 +3013,7 @@ static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
3033 } 3013 }
3034 } 3014 }
3035 3015
3036 LzmaEnc_Finish(p); 3016 LzmaEnc_Finish((CLzmaEncHandle)(void *)p);
3037 3017
3038 /* 3018 /*
3039 if (res == SZ_OK && !Inline_MatchFinder_IsFinishedOK(&MFB)) 3019 if (res == SZ_OK && !Inline_MatchFinder_IsFinishedOK(&MFB))
@@ -3045,21 +3025,22 @@ static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
3045} 3025}
3046 3026
3047 3027
3048SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress, 3028SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream, ICompressProgressPtr progress,
3049 ISzAllocPtr alloc, ISzAllocPtr allocBig) 3029 ISzAllocPtr alloc, ISzAllocPtr allocBig)
3050{ 3030{
3051 RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig)); 3031 // GET_CLzmaEnc_p
3052 return LzmaEnc_Encode2((CLzmaEnc *)pp, progress); 3032 RINOK(LzmaEnc_Prepare(p, outStream, inStream, alloc, allocBig))
3033 return LzmaEnc_Encode2(p, progress);
3053} 3034}
3054 3035
3055 3036
3056SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size) 3037SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *props, SizeT *size)
3057{ 3038{
3058 if (*size < LZMA_PROPS_SIZE) 3039 if (*size < LZMA_PROPS_SIZE)
3059 return SZ_ERROR_PARAM; 3040 return SZ_ERROR_PARAM;
3060 *size = LZMA_PROPS_SIZE; 3041 *size = LZMA_PROPS_SIZE;
3061 { 3042 {
3062 const CLzmaEnc *p = (const CLzmaEnc *)pp; 3043 // GET_CLzmaEnc_p
3063 const UInt32 dictSize = p->dictSize; 3044 const UInt32 dictSize = p->dictSize;
3064 UInt32 v; 3045 UInt32 v;
3065 props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc); 3046 props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
@@ -3083,23 +3064,24 @@ SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
3083 while (v < dictSize); 3064 while (v < dictSize);
3084 } 3065 }
3085 3066
3086 SetUi32(props + 1, v); 3067 SetUi32(props + 1, v)
3087 return SZ_OK; 3068 return SZ_OK;
3088 } 3069 }
3089} 3070}
3090 3071
3091 3072
3092unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle pp) 3073unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle p)
3093{ 3074{
3094 return (unsigned)((CLzmaEnc *)pp)->writeEndMark; 3075 // GET_CLzmaEnc_p
3076 return (unsigned)p->writeEndMark;
3095} 3077}
3096 3078
3097 3079
3098SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 3080SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
3099 int writeEndMark, ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig) 3081 int writeEndMark, ICompressProgressPtr progress, ISzAllocPtr alloc, ISzAllocPtr allocBig)
3100{ 3082{
3101 SRes res; 3083 SRes res;
3102 CLzmaEnc *p = (CLzmaEnc *)pp; 3084 // GET_CLzmaEnc_p
3103 3085
3104 CLzmaEnc_SeqOutStreamBuf outStream; 3086 CLzmaEnc_SeqOutStreamBuf outStream;
3105 3087
@@ -3111,7 +3093,7 @@ SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte
3111 p->writeEndMark = writeEndMark; 3093 p->writeEndMark = writeEndMark;
3112 p->rc.outStream = &outStream.vt; 3094 p->rc.outStream = &outStream.vt;
3113 3095
3114 res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig); 3096 res = LzmaEnc_MemPrepare(p, src, srcLen, 0, alloc, allocBig);
3115 3097
3116 if (res == SZ_OK) 3098 if (res == SZ_OK)
3117 { 3099 {
@@ -3120,7 +3102,7 @@ SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte
3120 res = SZ_ERROR_FAIL; 3102 res = SZ_ERROR_FAIL;
3121 } 3103 }
3122 3104
3123 *destLen -= outStream.rem; 3105 *destLen -= (SizeT)outStream.rem;
3124 if (outStream.overflow) 3106 if (outStream.overflow)
3125 return SZ_ERROR_OUTPUT_EOF; 3107 return SZ_ERROR_OUTPUT_EOF;
3126 return res; 3108 return res;
@@ -3129,9 +3111,9 @@ SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte
3129 3111
3130SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 3112SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
3131 const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, 3113 const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
3132 ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig) 3114 ICompressProgressPtr progress, ISzAllocPtr alloc, ISzAllocPtr allocBig)
3133{ 3115{
3134 CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc); 3116 CLzmaEncHandle p = LzmaEnc_Create(alloc);
3135 SRes res; 3117 SRes res;
3136 if (!p) 3118 if (!p)
3137 return SZ_ERROR_MEM; 3119 return SZ_ERROR_MEM;
@@ -3151,10 +3133,10 @@ SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
3151 3133
3152 3134
3153/* 3135/*
3154#ifndef _7ZIP_ST 3136#ifndef Z7_ST
3155void LzmaEnc_GetLzThreads(CLzmaEncHandle pp, HANDLE lz_threads[2]) 3137void LzmaEnc_GetLzThreads(CLzmaEncHandle p, HANDLE lz_threads[2])
3156{ 3138{
3157 const CLzmaEnc *p = (CLzmaEnc *)pp; 3139 GET_const_CLzmaEnc_p
3158 lz_threads[0] = p->matchFinderMt.hashSync.thread; 3140 lz_threads[0] = p->matchFinderMt.hashSync.thread;
3159 lz_threads[1] = p->matchFinderMt.btSync.thread; 3141 lz_threads[1] = p->matchFinderMt.btSync.thread;
3160} 3142}
diff --git a/C/LzmaEnc.h b/C/LzmaEnc.h
index bc2ed50..9f8039a 100644
--- a/C/LzmaEnc.h
+++ b/C/LzmaEnc.h
@@ -1,8 +1,8 @@
1/* LzmaEnc.h -- LZMA Encoder 1/* LzmaEnc.h -- LZMA Encoder
22019-10-30 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZMA_ENC_H 4#ifndef ZIP7_INC_LZMA_ENC_H
5#define __LZMA_ENC_H 5#define ZIP7_INC_LZMA_ENC_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -10,7 +10,7 @@ EXTERN_C_BEGIN
10 10
11#define LZMA_PROPS_SIZE 5 11#define LZMA_PROPS_SIZE 5
12 12
13typedef struct _CLzmaEncProps 13typedef struct
14{ 14{
15 int level; /* 0 <= level <= 9 */ 15 int level; /* 0 <= level <= 9 */
16 UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version 16 UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
@@ -23,10 +23,13 @@ typedef struct _CLzmaEncProps
23 int fb; /* 5 <= fb <= 273, default = 32 */ 23 int fb; /* 5 <= fb <= 273, default = 32 */
24 int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */ 24 int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
25 int numHashBytes; /* 2, 3 or 4, default = 4 */ 25 int numHashBytes; /* 2, 3 or 4, default = 4 */
26 unsigned numHashOutBits; /* default = ? */
26 UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */ 27 UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */
27 unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */ 28 unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
28 int numThreads; /* 1 or 2, default = 2 */ 29 int numThreads; /* 1 or 2, default = 2 */
29 30
31 // int _pad;
32
30 UInt64 reduceSize; /* estimated size of data that will be compressed. default = (UInt64)(Int64)-1. 33 UInt64 reduceSize; /* estimated size of data that will be compressed. default = (UInt64)(Int64)-1.
31 Encoder uses this value to reduce dictionary size */ 34 Encoder uses this value to reduce dictionary size */
32 35
@@ -51,7 +54,9 @@ SRes:
51 SZ_ERROR_THREAD - error in multithreading functions (only for Mt version) 54 SZ_ERROR_THREAD - error in multithreading functions (only for Mt version)
52*/ 55*/
53 56
54typedef void * CLzmaEncHandle; 57typedef struct CLzmaEnc CLzmaEnc;
58typedef CLzmaEnc * CLzmaEncHandle;
59// Z7_DECLARE_HANDLE(CLzmaEncHandle)
55 60
56CLzmaEncHandle LzmaEnc_Create(ISzAllocPtr alloc); 61CLzmaEncHandle LzmaEnc_Create(ISzAllocPtr alloc);
57void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig); 62void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig);
@@ -61,17 +66,17 @@ void LzmaEnc_SetDataSize(CLzmaEncHandle p, UInt64 expectedDataSiize);
61SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size); 66SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size);
62unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle p); 67unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle p);
63 68
64SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream, 69SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream,
65 ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); 70 ICompressProgressPtr progress, ISzAllocPtr alloc, ISzAllocPtr allocBig);
66SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 71SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
67 int writeEndMark, ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); 72 int writeEndMark, ICompressProgressPtr progress, ISzAllocPtr alloc, ISzAllocPtr allocBig);
68 73
69 74
70/* ---------- One Call Interface ---------- */ 75/* ---------- One Call Interface ---------- */
71 76
72SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 77SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
73 const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, 78 const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
74 ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); 79 ICompressProgressPtr progress, ISzAllocPtr alloc, ISzAllocPtr allocBig);
75 80
76EXTERN_C_END 81EXTERN_C_END
77 82
diff --git a/C/LzmaLib.c b/C/LzmaLib.c
index 706e9e5..785e884 100644
--- a/C/LzmaLib.c
+++ b/C/LzmaLib.c
@@ -1,12 +1,14 @@
1/* LzmaLib.c -- LZMA library wrapper 1/* LzmaLib.c -- LZMA library wrapper
22015-06-13 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3
4#include "Precomp.h"
3 5
4#include "Alloc.h" 6#include "Alloc.h"
5#include "LzmaDec.h" 7#include "LzmaDec.h"
6#include "LzmaEnc.h" 8#include "LzmaEnc.h"
7#include "LzmaLib.h" 9#include "LzmaLib.h"
8 10
9MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, 11Z7_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen,
10 unsigned char *outProps, size_t *outPropsSize, 12 unsigned char *outProps, size_t *outPropsSize,
11 int level, /* 0 <= level <= 9, default = 5 */ 13 int level, /* 0 <= level <= 9, default = 5 */
12 unsigned dictSize, /* use (1 << N) or (3 << N). 4 KB < dictSize <= 128 MB */ 14 unsigned dictSize, /* use (1 << N) or (3 << N). 4 KB < dictSize <= 128 MB */
@@ -32,7 +34,7 @@ MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char
32} 34}
33 35
34 36
35MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen, 37Z7_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen,
36 const unsigned char *props, size_t propsSize) 38 const unsigned char *props, size_t propsSize)
37{ 39{
38 ELzmaStatus status; 40 ELzmaStatus status;
diff --git a/C/LzmaLib.h b/C/LzmaLib.h
index c343a85..d7c0724 100644
--- a/C/LzmaLib.h
+++ b/C/LzmaLib.h
@@ -1,14 +1,14 @@
1/* LzmaLib.h -- LZMA library interface 1/* LzmaLib.h -- LZMA library interface
22021-04-03 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __LZMA_LIB_H 4#ifndef ZIP7_INC_LZMA_LIB_H
5#define __LZMA_LIB_H 5#define ZIP7_INC_LZMA_LIB_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
9EXTERN_C_BEGIN 9EXTERN_C_BEGIN
10 10
11#define MY_STDAPI int MY_STD_CALL 11#define Z7_STDAPI int Z7_STDCALL
12 12
13#define LZMA_PROPS_SIZE 5 13#define LZMA_PROPS_SIZE 5
14 14
@@ -100,7 +100,7 @@ Returns:
100 SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) 100 SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
101*/ 101*/
102 102
103MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, 103Z7_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen,
104 unsigned char *outProps, size_t *outPropsSize, /* *outPropsSize must be = 5 */ 104 unsigned char *outProps, size_t *outPropsSize, /* *outPropsSize must be = 5 */
105 int level, /* 0 <= level <= 9, default = 5 */ 105 int level, /* 0 <= level <= 9, default = 5 */
106 unsigned dictSize, /* default = (1 << 24) */ 106 unsigned dictSize, /* default = (1 << 24) */
@@ -130,7 +130,7 @@ Returns:
130 SZ_ERROR_INPUT_EOF - it needs more bytes in input buffer (src) 130 SZ_ERROR_INPUT_EOF - it needs more bytes in input buffer (src)
131*/ 131*/
132 132
133MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, SizeT *srcLen, 133Z7_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, SizeT *srcLen,
134 const unsigned char *props, size_t propsSize); 134 const unsigned char *props, size_t propsSize);
135 135
136EXTERN_C_END 136EXTERN_C_END
diff --git a/C/MtCoder.c b/C/MtCoder.c
index 99dc909..6f58abb 100644
--- a/C/MtCoder.c
+++ b/C/MtCoder.c
@@ -1,28 +1,28 @@
1/* MtCoder.c -- Multi-thread Coder 1/* MtCoder.c -- Multi-thread Coder
22021-12-21 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#include "MtCoder.h" 6#include "MtCoder.h"
7 7
8#ifndef _7ZIP_ST 8#ifndef Z7_ST
9 9
10static SRes MtProgressThunk_Progress(const ICompressProgress *pp, UInt64 inSize, UInt64 outSize) 10static SRes MtProgressThunk_Progress(ICompressProgressPtr pp, UInt64 inSize, UInt64 outSize)
11{ 11{
12 CMtProgressThunk *thunk = CONTAINER_FROM_VTBL(pp, CMtProgressThunk, vt); 12 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CMtProgressThunk)
13 UInt64 inSize2 = 0; 13 UInt64 inSize2 = 0;
14 UInt64 outSize2 = 0; 14 UInt64 outSize2 = 0;
15 if (inSize != (UInt64)(Int64)-1) 15 if (inSize != (UInt64)(Int64)-1)
16 { 16 {
17 inSize2 = inSize - thunk->inSize; 17 inSize2 = inSize - p->inSize;
18 thunk->inSize = inSize; 18 p->inSize = inSize;
19 } 19 }
20 if (outSize != (UInt64)(Int64)-1) 20 if (outSize != (UInt64)(Int64)-1)
21 { 21 {
22 outSize2 = outSize - thunk->outSize; 22 outSize2 = outSize - p->outSize;
23 thunk->outSize = outSize; 23 p->outSize = outSize;
24 } 24 }
25 return MtProgress_ProgressAdd(thunk->mtProgress, inSize2, outSize2); 25 return MtProgress_ProgressAdd(p->mtProgress, inSize2, outSize2);
26} 26}
27 27
28 28
@@ -36,20 +36,12 @@ void MtProgressThunk_CreateVTable(CMtProgressThunk *p)
36#define RINOK_THREAD(x) { if ((x) != 0) return SZ_ERROR_THREAD; } 36#define RINOK_THREAD(x) { if ((x) != 0) return SZ_ERROR_THREAD; }
37 37
38 38
39static WRes ArEvent_OptCreate_And_Reset(CEvent *p)
40{
41 if (Event_IsCreated(p))
42 return Event_Reset(p);
43 return AutoResetEvent_CreateNotSignaled(p);
44}
45
46
47static THREAD_FUNC_DECL ThreadFunc(void *pp); 39static THREAD_FUNC_DECL ThreadFunc(void *pp);
48 40
49 41
50static SRes MtCoderThread_CreateAndStart(CMtCoderThread *t) 42static SRes MtCoderThread_CreateAndStart(CMtCoderThread *t)
51{ 43{
52 WRes wres = ArEvent_OptCreate_And_Reset(&t->startEvent); 44 WRes wres = AutoResetEvent_OptCreate_And_Reset(&t->startEvent);
53 if (wres == 0) 45 if (wres == 0)
54 { 46 {
55 t->stop = False; 47 t->stop = False;
@@ -84,24 +76,6 @@ static void MtCoderThread_Destruct(CMtCoderThread *t)
84 76
85 77
86 78
87static SRes FullRead(ISeqInStream *stream, Byte *data, size_t *processedSize)
88{
89 size_t size = *processedSize;
90 *processedSize = 0;
91 while (size != 0)
92 {
93 size_t cur = size;
94 SRes res = ISeqInStream_Read(stream, data, &cur);
95 *processedSize += cur;
96 data += cur;
97 size -= cur;
98 RINOK(res);
99 if (cur == 0)
100 return SZ_OK;
101 }
102 return SZ_OK;
103}
104
105 79
106/* 80/*
107 ThreadFunc2() returns: 81 ThreadFunc2() returns:
@@ -152,7 +126,7 @@ static SRes ThreadFunc2(CMtCoderThread *t)
152 } 126 }
153 if (res == SZ_OK) 127 if (res == SZ_OK)
154 { 128 {
155 res = FullRead(mtc->inStream, t->inBuf, &size); 129 res = SeqInStream_ReadMax(mtc->inStream, t->inBuf, &size);
156 readProcessed = mtc->readProcessed + size; 130 readProcessed = mtc->readProcessed + size;
157 mtc->readProcessed = readProcessed; 131 mtc->readProcessed = readProcessed;
158 } 132 }
@@ -253,7 +227,7 @@ static SRes ThreadFunc2(CMtCoderThread *t)
253 block->finished = finished; 227 block->finished = finished;
254 } 228 }
255 229
256 #ifdef MTCODER__USE_WRITE_THREAD 230 #ifdef MTCODER_USE_WRITE_THREAD
257 RINOK_THREAD(Event_Set(&mtc->writeEvents[bi])) 231 RINOK_THREAD(Event_Set(&mtc->writeEvents[bi]))
258 #else 232 #else
259 { 233 {
@@ -352,7 +326,7 @@ static THREAD_FUNC_DECL ThreadFunc(void *pp)
352 MtProgress_SetError(&mtc->mtProgress, res); 326 MtProgress_SetError(&mtc->mtProgress, res);
353 } 327 }
354 328
355 #ifndef MTCODER__USE_WRITE_THREAD 329 #ifndef MTCODER_USE_WRITE_THREAD
356 { 330 {
357 unsigned numFinished = (unsigned)InterlockedIncrement(&mtc->numFinishedThreads); 331 unsigned numFinished = (unsigned)InterlockedIncrement(&mtc->numFinishedThreads);
358 if (numFinished == mtc->numStartedThreads) 332 if (numFinished == mtc->numStartedThreads)
@@ -389,7 +363,7 @@ void MtCoder_Construct(CMtCoder *p)
389 Event_Construct(&p->readEvent); 363 Event_Construct(&p->readEvent);
390 Semaphore_Construct(&p->blocksSemaphore); 364 Semaphore_Construct(&p->blocksSemaphore);
391 365
392 for (i = 0; i < MTCODER__THREADS_MAX; i++) 366 for (i = 0; i < MTCODER_THREADS_MAX; i++)
393 { 367 {
394 CMtCoderThread *t = &p->threads[i]; 368 CMtCoderThread *t = &p->threads[i];
395 t->mtCoder = p; 369 t->mtCoder = p;
@@ -397,11 +371,11 @@ void MtCoder_Construct(CMtCoder *p)
397 t->inBuf = NULL; 371 t->inBuf = NULL;
398 t->stop = False; 372 t->stop = False;
399 Event_Construct(&t->startEvent); 373 Event_Construct(&t->startEvent);
400 Thread_Construct(&t->thread); 374 Thread_CONSTRUCT(&t->thread)
401 } 375 }
402 376
403 #ifdef MTCODER__USE_WRITE_THREAD 377 #ifdef MTCODER_USE_WRITE_THREAD
404 for (i = 0; i < MTCODER__BLOCKS_MAX; i++) 378 for (i = 0; i < MTCODER_BLOCKS_MAX; i++)
405 Event_Construct(&p->writeEvents[i]); 379 Event_Construct(&p->writeEvents[i]);
406 #else 380 #else
407 Event_Construct(&p->finishedEvent); 381 Event_Construct(&p->finishedEvent);
@@ -424,14 +398,14 @@ static void MtCoder_Free(CMtCoder *p)
424 Event_Set(&p->readEvent); 398 Event_Set(&p->readEvent);
425 */ 399 */
426 400
427 for (i = 0; i < MTCODER__THREADS_MAX; i++) 401 for (i = 0; i < MTCODER_THREADS_MAX; i++)
428 MtCoderThread_Destruct(&p->threads[i]); 402 MtCoderThread_Destruct(&p->threads[i]);
429 403
430 Event_Close(&p->readEvent); 404 Event_Close(&p->readEvent);
431 Semaphore_Close(&p->blocksSemaphore); 405 Semaphore_Close(&p->blocksSemaphore);
432 406
433 #ifdef MTCODER__USE_WRITE_THREAD 407 #ifdef MTCODER_USE_WRITE_THREAD
434 for (i = 0; i < MTCODER__BLOCKS_MAX; i++) 408 for (i = 0; i < MTCODER_BLOCKS_MAX; i++)
435 Event_Close(&p->writeEvents[i]); 409 Event_Close(&p->writeEvents[i]);
436 #else 410 #else
437 Event_Close(&p->finishedEvent); 411 Event_Close(&p->finishedEvent);
@@ -455,20 +429,20 @@ SRes MtCoder_Code(CMtCoder *p)
455 unsigned i; 429 unsigned i;
456 SRes res = SZ_OK; 430 SRes res = SZ_OK;
457 431
458 if (numThreads > MTCODER__THREADS_MAX) 432 if (numThreads > MTCODER_THREADS_MAX)
459 numThreads = MTCODER__THREADS_MAX; 433 numThreads = MTCODER_THREADS_MAX;
460 numBlocksMax = MTCODER__GET_NUM_BLOCKS_FROM_THREADS(numThreads); 434 numBlocksMax = MTCODER_GET_NUM_BLOCKS_FROM_THREADS(numThreads);
461 435
462 if (p->blockSize < ((UInt32)1 << 26)) numBlocksMax++; 436 if (p->blockSize < ((UInt32)1 << 26)) numBlocksMax++;
463 if (p->blockSize < ((UInt32)1 << 24)) numBlocksMax++; 437 if (p->blockSize < ((UInt32)1 << 24)) numBlocksMax++;
464 if (p->blockSize < ((UInt32)1 << 22)) numBlocksMax++; 438 if (p->blockSize < ((UInt32)1 << 22)) numBlocksMax++;
465 439
466 if (numBlocksMax > MTCODER__BLOCKS_MAX) 440 if (numBlocksMax > MTCODER_BLOCKS_MAX)
467 numBlocksMax = MTCODER__BLOCKS_MAX; 441 numBlocksMax = MTCODER_BLOCKS_MAX;
468 442
469 if (p->blockSize != p->allocatedBufsSize) 443 if (p->blockSize != p->allocatedBufsSize)
470 { 444 {
471 for (i = 0; i < MTCODER__THREADS_MAX; i++) 445 for (i = 0; i < MTCODER_THREADS_MAX; i++)
472 { 446 {
473 CMtCoderThread *t = &p->threads[i]; 447 CMtCoderThread *t = &p->threads[i];
474 if (t->inBuf) 448 if (t->inBuf)
@@ -484,23 +458,23 @@ SRes MtCoder_Code(CMtCoder *p)
484 458
485 MtProgress_Init(&p->mtProgress, p->progress); 459 MtProgress_Init(&p->mtProgress, p->progress);
486 460
487 #ifdef MTCODER__USE_WRITE_THREAD 461 #ifdef MTCODER_USE_WRITE_THREAD
488 for (i = 0; i < numBlocksMax; i++) 462 for (i = 0; i < numBlocksMax; i++)
489 { 463 {
490 RINOK_THREAD(ArEvent_OptCreate_And_Reset(&p->writeEvents[i])); 464 RINOK_THREAD(AutoResetEvent_OptCreate_And_Reset(&p->writeEvents[i]))
491 } 465 }
492 #else 466 #else
493 RINOK_THREAD(ArEvent_OptCreate_And_Reset(&p->finishedEvent)); 467 RINOK_THREAD(AutoResetEvent_OptCreate_And_Reset(&p->finishedEvent))
494 #endif 468 #endif
495 469
496 { 470 {
497 RINOK_THREAD(ArEvent_OptCreate_And_Reset(&p->readEvent)); 471 RINOK_THREAD(AutoResetEvent_OptCreate_And_Reset(&p->readEvent))
498 RINOK_THREAD(Semaphore_OptCreateInit(&p->blocksSemaphore, numBlocksMax, numBlocksMax)); 472 RINOK_THREAD(Semaphore_OptCreateInit(&p->blocksSemaphore, numBlocksMax, numBlocksMax))
499 } 473 }
500 474
501 for (i = 0; i < MTCODER__BLOCKS_MAX - 1; i++) 475 for (i = 0; i < MTCODER_BLOCKS_MAX - 1; i++)
502 p->freeBlockList[i] = i + 1; 476 p->freeBlockList[i] = i + 1;
503 p->freeBlockList[MTCODER__BLOCKS_MAX - 1] = (unsigned)(int)-1; 477 p->freeBlockList[MTCODER_BLOCKS_MAX - 1] = (unsigned)(int)-1;
504 p->freeBlockHead = 0; 478 p->freeBlockHead = 0;
505 479
506 p->readProcessed = 0; 480 p->readProcessed = 0;
@@ -508,10 +482,10 @@ SRes MtCoder_Code(CMtCoder *p)
508 p->numBlocksMax = numBlocksMax; 482 p->numBlocksMax = numBlocksMax;
509 p->stopReading = False; 483 p->stopReading = False;
510 484
511 #ifndef MTCODER__USE_WRITE_THREAD 485 #ifndef MTCODER_USE_WRITE_THREAD
512 p->writeIndex = 0; 486 p->writeIndex = 0;
513 p->writeRes = SZ_OK; 487 p->writeRes = SZ_OK;
514 for (i = 0; i < MTCODER__BLOCKS_MAX; i++) 488 for (i = 0; i < MTCODER_BLOCKS_MAX; i++)
515 p->ReadyBlocks[i] = False; 489 p->ReadyBlocks[i] = False;
516 p->numFinishedThreads = 0; 490 p->numFinishedThreads = 0;
517 #endif 491 #endif
@@ -522,12 +496,12 @@ SRes MtCoder_Code(CMtCoder *p)
522 // for (i = 0; i < numThreads; i++) 496 // for (i = 0; i < numThreads; i++)
523 { 497 {
524 CMtCoderThread *nextThread = &p->threads[p->numStartedThreads++]; 498 CMtCoderThread *nextThread = &p->threads[p->numStartedThreads++];
525 RINOK(MtCoderThread_CreateAndStart(nextThread)); 499 RINOK(MtCoderThread_CreateAndStart(nextThread))
526 } 500 }
527 501
528 RINOK_THREAD(Event_Set(&p->readEvent)) 502 RINOK_THREAD(Event_Set(&p->readEvent))
529 503
530 #ifdef MTCODER__USE_WRITE_THREAD 504 #ifdef MTCODER_USE_WRITE_THREAD
531 { 505 {
532 unsigned bi = 0; 506 unsigned bi = 0;
533 507
@@ -582,7 +556,7 @@ SRes MtCoder_Code(CMtCoder *p)
582 if (res == SZ_OK) 556 if (res == SZ_OK)
583 res = p->mtProgress.res; 557 res = p->mtProgress.res;
584 558
585 #ifndef MTCODER__USE_WRITE_THREAD 559 #ifndef MTCODER_USE_WRITE_THREAD
586 if (res == SZ_OK) 560 if (res == SZ_OK)
587 res = p->writeRes; 561 res = p->writeRes;
588 #endif 562 #endif
@@ -593,3 +567,5 @@ SRes MtCoder_Code(CMtCoder *p)
593} 567}
594 568
595#endif 569#endif
570
571#undef RINOK_THREAD
diff --git a/C/MtCoder.h b/C/MtCoder.h
index 5a5f4d1..1231d3c 100644
--- a/C/MtCoder.h
+++ b/C/MtCoder.h
@@ -1,30 +1,30 @@
1/* MtCoder.h -- Multi-thread Coder 1/* MtCoder.h -- Multi-thread Coder
22018-07-04 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#ifndef __MT_CODER_H 4#ifndef ZIP7_INC_MT_CODER_H
5#define __MT_CODER_H 5#define ZIP7_INC_MT_CODER_H
6 6
7#include "MtDec.h" 7#include "MtDec.h"
8 8
9EXTERN_C_BEGIN 9EXTERN_C_BEGIN
10 10
11/* 11/*
12 if ( defined MTCODER__USE_WRITE_THREAD) : main thread writes all data blocks to output stream 12 if ( defined MTCODER_USE_WRITE_THREAD) : main thread writes all data blocks to output stream
13 if (not defined MTCODER__USE_WRITE_THREAD) : any coder thread can write data blocks to output stream 13 if (not defined MTCODER_USE_WRITE_THREAD) : any coder thread can write data blocks to output stream
14*/ 14*/
15/* #define MTCODER__USE_WRITE_THREAD */ 15/* #define MTCODER_USE_WRITE_THREAD */
16 16
17#ifndef _7ZIP_ST 17#ifndef Z7_ST
18 #define MTCODER__GET_NUM_BLOCKS_FROM_THREADS(numThreads) ((numThreads) + (numThreads) / 8 + 1) 18 #define MTCODER_GET_NUM_BLOCKS_FROM_THREADS(numThreads) ((numThreads) + (numThreads) / 8 + 1)
19 #define MTCODER__THREADS_MAX 64 19 #define MTCODER_THREADS_MAX 64
20 #define MTCODER__BLOCKS_MAX (MTCODER__GET_NUM_BLOCKS_FROM_THREADS(MTCODER__THREADS_MAX) + 3) 20 #define MTCODER_BLOCKS_MAX (MTCODER_GET_NUM_BLOCKS_FROM_THREADS(MTCODER_THREADS_MAX) + 3)
21#else 21#else
22 #define MTCODER__THREADS_MAX 1 22 #define MTCODER_THREADS_MAX 1
23 #define MTCODER__BLOCKS_MAX 1 23 #define MTCODER_BLOCKS_MAX 1
24#endif 24#endif
25 25
26 26
27#ifndef _7ZIP_ST 27#ifndef Z7_ST
28 28
29 29
30typedef struct 30typedef struct
@@ -37,15 +37,15 @@ typedef struct
37 37
38void MtProgressThunk_CreateVTable(CMtProgressThunk *p); 38void MtProgressThunk_CreateVTable(CMtProgressThunk *p);
39 39
40#define MtProgressThunk_Init(p) { (p)->inSize = 0; (p)->outSize = 0; } 40#define MtProgressThunk_INIT(p) { (p)->inSize = 0; (p)->outSize = 0; }
41 41
42 42
43struct _CMtCoder; 43struct CMtCoder_;
44 44
45 45
46typedef struct 46typedef struct
47{ 47{
48 struct _CMtCoder *mtCoder; 48 struct CMtCoder_ *mtCoder;
49 unsigned index; 49 unsigned index;
50 int stop; 50 int stop;
51 Byte *inBuf; 51 Byte *inBuf;
@@ -71,7 +71,7 @@ typedef struct
71} CMtCoderBlock; 71} CMtCoderBlock;
72 72
73 73
74typedef struct _CMtCoder 74typedef struct CMtCoder_
75{ 75{
76 /* input variables */ 76 /* input variables */
77 77
@@ -79,11 +79,11 @@ typedef struct _CMtCoder
79 unsigned numThreadsMax; 79 unsigned numThreadsMax;
80 UInt64 expectedDataSize; 80 UInt64 expectedDataSize;
81 81
82 ISeqInStream *inStream; 82 ISeqInStreamPtr inStream;
83 const Byte *inData; 83 const Byte *inData;
84 size_t inDataSize; 84 size_t inDataSize;
85 85
86 ICompressProgress *progress; 86 ICompressProgressPtr progress;
87 ISzAllocPtr allocBig; 87 ISzAllocPtr allocBig;
88 88
89 IMtCoderCallback2 *mtCallback; 89 IMtCoderCallback2 *mtCallback;
@@ -100,13 +100,13 @@ typedef struct _CMtCoder
100 BoolInt stopReading; 100 BoolInt stopReading;
101 SRes readRes; 101 SRes readRes;
102 102
103 #ifdef MTCODER__USE_WRITE_THREAD 103 #ifdef MTCODER_USE_WRITE_THREAD
104 CAutoResetEvent writeEvents[MTCODER__BLOCKS_MAX]; 104 CAutoResetEvent writeEvents[MTCODER_BLOCKS_MAX];
105 #else 105 #else
106 CAutoResetEvent finishedEvent; 106 CAutoResetEvent finishedEvent;
107 SRes writeRes; 107 SRes writeRes;
108 unsigned writeIndex; 108 unsigned writeIndex;
109 Byte ReadyBlocks[MTCODER__BLOCKS_MAX]; 109 Byte ReadyBlocks[MTCODER_BLOCKS_MAX];
110 LONG numFinishedThreads; 110 LONG numFinishedThreads;
111 #endif 111 #endif
112 112
@@ -120,11 +120,11 @@ typedef struct _CMtCoder
120 CCriticalSection cs; 120 CCriticalSection cs;
121 121
122 unsigned freeBlockHead; 122 unsigned freeBlockHead;
123 unsigned freeBlockList[MTCODER__BLOCKS_MAX]; 123 unsigned freeBlockList[MTCODER_BLOCKS_MAX];
124 124
125 CMtProgress mtProgress; 125 CMtProgress mtProgress;
126 CMtCoderBlock blocks[MTCODER__BLOCKS_MAX]; 126 CMtCoderBlock blocks[MTCODER_BLOCKS_MAX];
127 CMtCoderThread threads[MTCODER__THREADS_MAX]; 127 CMtCoderThread threads[MTCODER_THREADS_MAX];
128} CMtCoder; 128} CMtCoder;
129 129
130 130
diff --git a/C/MtDec.c b/C/MtDec.c
index 45a6713..7820699 100644
--- a/C/MtDec.c
+++ b/C/MtDec.c
@@ -1,5 +1,5 @@
1/* MtDec.c -- Multi-thread Decoder 1/* MtDec.c -- Multi-thread Decoder
22021-12-21 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -14,7 +14,7 @@
14 14
15#include "MtDec.h" 15#include "MtDec.h"
16 16
17#ifndef _7ZIP_ST 17#ifndef Z7_ST
18 18
19#ifdef SHOW_DEBUG_INFO 19#ifdef SHOW_DEBUG_INFO
20#define PRF(x) x 20#define PRF(x) x
@@ -24,7 +24,7 @@
24 24
25#define PRF_STR_INT(s, d) PRF(printf("\n" s " %d\n", (unsigned)d)) 25#define PRF_STR_INT(s, d) PRF(printf("\n" s " %d\n", (unsigned)d))
26 26
27void MtProgress_Init(CMtProgress *p, ICompressProgress *progress) 27void MtProgress_Init(CMtProgress *p, ICompressProgressPtr progress)
28{ 28{
29 p->progress = progress; 29 p->progress = progress;
30 p->res = SZ_OK; 30 p->res = SZ_OK;
@@ -81,36 +81,28 @@ void MtProgress_SetError(CMtProgress *p, SRes res)
81#define RINOK_THREAD(x) RINOK_WRes(x) 81#define RINOK_THREAD(x) RINOK_WRes(x)
82 82
83 83
84static WRes ArEvent_OptCreate_And_Reset(CEvent *p) 84struct CMtDecBufLink_
85{ 85{
86 if (Event_IsCreated(p)) 86 struct CMtDecBufLink_ *next;
87 return Event_Reset(p);
88 return AutoResetEvent_CreateNotSignaled(p);
89}
90
91
92struct __CMtDecBufLink
93{
94 struct __CMtDecBufLink *next;
95 void *pad[3]; 87 void *pad[3];
96}; 88};
97 89
98typedef struct __CMtDecBufLink CMtDecBufLink; 90typedef struct CMtDecBufLink_ CMtDecBufLink;
99 91
100#define MTDEC__LINK_DATA_OFFSET sizeof(CMtDecBufLink) 92#define MTDEC__LINK_DATA_OFFSET sizeof(CMtDecBufLink)
101#define MTDEC__DATA_PTR_FROM_LINK(link) ((Byte *)(link) + MTDEC__LINK_DATA_OFFSET) 93#define MTDEC__DATA_PTR_FROM_LINK(link) ((Byte *)(link) + MTDEC__LINK_DATA_OFFSET)
102 94
103 95
104 96
105static THREAD_FUNC_DECL ThreadFunc(void *pp); 97static THREAD_FUNC_DECL MtDec_ThreadFunc(void *pp);
106 98
107 99
108static WRes MtDecThread_CreateEvents(CMtDecThread *t) 100static WRes MtDecThread_CreateEvents(CMtDecThread *t)
109{ 101{
110 WRes wres = ArEvent_OptCreate_And_Reset(&t->canWrite); 102 WRes wres = AutoResetEvent_OptCreate_And_Reset(&t->canWrite);
111 if (wres == 0) 103 if (wres == 0)
112 { 104 {
113 wres = ArEvent_OptCreate_And_Reset(&t->canRead); 105 wres = AutoResetEvent_OptCreate_And_Reset(&t->canRead);
114 if (wres == 0) 106 if (wres == 0)
115 return SZ_OK; 107 return SZ_OK;
116 } 108 }
@@ -126,7 +118,7 @@ static SRes MtDecThread_CreateAndStart(CMtDecThread *t)
126 { 118 {
127 if (Thread_WasCreated(&t->thread)) 119 if (Thread_WasCreated(&t->thread))
128 return SZ_OK; 120 return SZ_OK;
129 wres = Thread_Create(&t->thread, ThreadFunc, t); 121 wres = Thread_Create(&t->thread, MtDec_ThreadFunc, t);
130 if (wres == 0) 122 if (wres == 0)
131 return SZ_OK; 123 return SZ_OK;
132 } 124 }
@@ -167,7 +159,7 @@ static void MtDecThread_CloseThread(CMtDecThread *t)
167static void MtDec_CloseThreads(CMtDec *p) 159static void MtDec_CloseThreads(CMtDec *p)
168{ 160{
169 unsigned i; 161 unsigned i;
170 for (i = 0; i < MTDEC__THREADS_MAX; i++) 162 for (i = 0; i < MTDEC_THREADS_MAX; i++)
171 MtDecThread_CloseThread(&p->threads[i]); 163 MtDecThread_CloseThread(&p->threads[i]);
172} 164}
173 165
@@ -179,25 +171,6 @@ static void MtDecThread_Destruct(CMtDecThread *t)
179 171
180 172
181 173
182static SRes FullRead(ISeqInStream *stream, Byte *data, size_t *processedSize)
183{
184 size_t size = *processedSize;
185 *processedSize = 0;
186 while (size != 0)
187 {
188 size_t cur = size;
189 SRes res = ISeqInStream_Read(stream, data, &cur);
190 *processedSize += cur;
191 data += cur;
192 size -= cur;
193 RINOK(res);
194 if (cur == 0)
195 return SZ_OK;
196 }
197 return SZ_OK;
198}
199
200
201static SRes MtDec_GetError_Spec(CMtDec *p, UInt64 interruptIndex, BoolInt *wasInterrupted) 174static SRes MtDec_GetError_Spec(CMtDec *p, UInt64 interruptIndex, BoolInt *wasInterrupted)
202{ 175{
203 SRes res; 176 SRes res;
@@ -253,7 +226,7 @@ Byte *MtDec_GetCrossBuff(CMtDec *p)
253 226
254 227
255/* 228/*
256 ThreadFunc2() returns: 229 MtDec_ThreadFunc2() returns:
257 0 - in all normal cases (even for stream error or memory allocation error) 230 0 - in all normal cases (even for stream error or memory allocation error)
258 (!= 0) - WRes error return by system threading function 231 (!= 0) - WRes error return by system threading function
259*/ 232*/
@@ -261,11 +234,11 @@ Byte *MtDec_GetCrossBuff(CMtDec *p)
261// #define MTDEC_ProgessStep (1 << 22) 234// #define MTDEC_ProgessStep (1 << 22)
262#define MTDEC_ProgessStep (1 << 0) 235#define MTDEC_ProgessStep (1 << 0)
263 236
264static WRes ThreadFunc2(CMtDecThread *t) 237static WRes MtDec_ThreadFunc2(CMtDecThread *t)
265{ 238{
266 CMtDec *p = t->mtDec; 239 CMtDec *p = t->mtDec;
267 240
268 PRF_STR_INT("ThreadFunc2", t->index); 241 PRF_STR_INT("MtDec_ThreadFunc2", t->index)
269 242
270 // SetThreadAffinityMask(GetCurrentThread(), 1 << t->index); 243 // SetThreadAffinityMask(GetCurrentThread(), 1 << t->index);
271 244
@@ -295,13 +268,13 @@ static WRes ThreadFunc2(CMtDecThread *t)
295 // CMtDecCallbackInfo parse; 268 // CMtDecCallbackInfo parse;
296 CMtDecThread *nextThread; 269 CMtDecThread *nextThread;
297 270
298 PRF_STR_INT("=============== Event_Wait(&t->canRead)", t->index); 271 PRF_STR_INT("=============== Event_Wait(&t->canRead)", t->index)
299 272
300 RINOK_THREAD(Event_Wait(&t->canRead)); 273 RINOK_THREAD(Event_Wait(&t->canRead))
301 if (p->exitThread) 274 if (p->exitThread)
302 return 0; 275 return 0;
303 276
304 PRF_STR_INT("after Event_Wait(&t->canRead)", t->index); 277 PRF_STR_INT("after Event_Wait(&t->canRead)", t->index)
305 278
306 // if (t->index == 3) return 19; // for test 279 // if (t->index == 3) return 19; // for test
307 280
@@ -373,7 +346,7 @@ static WRes ThreadFunc2(CMtDecThread *t)
373 { 346 {
374 size = p->inBufSize; 347 size = p->inBufSize;
375 348
376 res = FullRead(p->inStream, data, &size); 349 res = SeqInStream_ReadMax(p->inStream, data, &size);
377 350
378 // size = 10; // test 351 // size = 10; // test
379 352
@@ -615,7 +588,7 @@ static WRes ThreadFunc2(CMtDecThread *t)
615 // if ( !finish ) we must call Event_Set(&nextThread->canWrite) in any case 588 // if ( !finish ) we must call Event_Set(&nextThread->canWrite) in any case
616 // if ( finish ) we switch to single-thread mode and there are 2 ways at the end of current iteration (current block): 589 // if ( finish ) we switch to single-thread mode and there are 2 ways at the end of current iteration (current block):
617 // - if (needContinue) after Write(&needContinue), we restore decoding with new iteration 590 // - if (needContinue) after Write(&needContinue), we restore decoding with new iteration
618 // - otherwise we stop decoding and exit from ThreadFunc2() 591 // - otherwise we stop decoding and exit from MtDec_ThreadFunc2()
619 592
620 // Don't change (finish) variable in the further code 593 // Don't change (finish) variable in the further code
621 594
@@ -688,7 +661,7 @@ static WRes ThreadFunc2(CMtDecThread *t)
688 661
689 // ---------- WRITE ---------- 662 // ---------- WRITE ----------
690 663
691 RINOK_THREAD(Event_Wait(&t->canWrite)); 664 RINOK_THREAD(Event_Wait(&t->canWrite))
692 665
693 { 666 {
694 BoolInt isErrorMode = False; 667 BoolInt isErrorMode = False;
@@ -801,14 +774,14 @@ static WRes ThreadFunc2(CMtDecThread *t)
801 774
802 if (!finish) 775 if (!finish)
803 { 776 {
804 RINOK_THREAD(Event_Set(&nextThread->canWrite)); 777 RINOK_THREAD(Event_Set(&nextThread->canWrite))
805 } 778 }
806 else 779 else
807 { 780 {
808 if (needContinue) 781 if (needContinue)
809 { 782 {
810 // we restore decoding with new iteration 783 // we restore decoding with new iteration
811 RINOK_THREAD(Event_Set(&p->threads[0].canWrite)); 784 RINOK_THREAD(Event_Set(&p->threads[0].canWrite))
812 } 785 }
813 else 786 else
814 { 787 {
@@ -817,7 +790,7 @@ static WRes ThreadFunc2(CMtDecThread *t)
817 return SZ_OK; 790 return SZ_OK;
818 p->exitThread = True; 791 p->exitThread = True;
819 } 792 }
820 RINOK_THREAD(Event_Set(&p->threads[0].canRead)); 793 RINOK_THREAD(Event_Set(&p->threads[0].canRead))
821 } 794 }
822 } 795 }
823 } 796 }
@@ -836,7 +809,7 @@ static WRes ThreadFunc2(CMtDecThread *t)
836#endif 809#endif
837 810
838 811
839static THREAD_FUNC_DECL ThreadFunc1(void *pp) 812static THREAD_FUNC_DECL MtDec_ThreadFunc1(void *pp)
840{ 813{
841 WRes res; 814 WRes res;
842 815
@@ -845,7 +818,7 @@ static THREAD_FUNC_DECL ThreadFunc1(void *pp)
845 818
846 // fprintf(stdout, "\n%d = %p\n", t->index, &t); 819 // fprintf(stdout, "\n%d = %p\n", t->index, &t);
847 820
848 res = ThreadFunc2(t); 821 res = MtDec_ThreadFunc2(t);
849 p = t->mtDec; 822 p = t->mtDec;
850 if (res == 0) 823 if (res == 0)
851 return (THREAD_FUNC_RET_TYPE)(UINT_PTR)p->exitThreadWRes; 824 return (THREAD_FUNC_RET_TYPE)(UINT_PTR)p->exitThreadWRes;
@@ -862,14 +835,14 @@ static THREAD_FUNC_DECL ThreadFunc1(void *pp)
862 return (THREAD_FUNC_RET_TYPE)(UINT_PTR)res; 835 return (THREAD_FUNC_RET_TYPE)(UINT_PTR)res;
863} 836}
864 837
865static MY_NO_INLINE THREAD_FUNC_DECL ThreadFunc(void *pp) 838static Z7_NO_INLINE THREAD_FUNC_DECL MtDec_ThreadFunc(void *pp)
866{ 839{
867 #ifdef USE_ALLOCA 840 #ifdef USE_ALLOCA
868 CMtDecThread *t = (CMtDecThread *)pp; 841 CMtDecThread *t = (CMtDecThread *)pp;
869 // fprintf(stderr, "\n%d = %p - before", t->index, &t); 842 // fprintf(stderr, "\n%d = %p - before", t->index, &t);
870 t->allocaPtr = alloca(t->index * 128); 843 t->allocaPtr = alloca(t->index * 128);
871 #endif 844 #endif
872 return ThreadFunc1(pp); 845 return MtDec_ThreadFunc1(pp);
873} 846}
874 847
875 848
@@ -883,7 +856,7 @@ int MtDec_PrepareRead(CMtDec *p)
883 856
884 { 857 {
885 unsigned i; 858 unsigned i;
886 for (i = 0; i < MTDEC__THREADS_MAX; i++) 859 for (i = 0; i < MTDEC_THREADS_MAX; i++)
887 if (i > p->numStartedThreads 860 if (i > p->numStartedThreads
888 || p->numFilledThreads <= 861 || p->numFilledThreads <=
889 (i >= p->filledThreadStart ? 862 (i >= p->filledThreadStart ?
@@ -987,7 +960,7 @@ void MtDec_Construct(CMtDec *p)
987 960
988 p->allocatedBufsSize = 0; 961 p->allocatedBufsSize = 0;
989 962
990 for (i = 0; i < MTDEC__THREADS_MAX; i++) 963 for (i = 0; i < MTDEC_THREADS_MAX; i++)
991 { 964 {
992 CMtDecThread *t = &p->threads[i]; 965 CMtDecThread *t = &p->threads[i];
993 t->mtDec = p; 966 t->mtDec = p;
@@ -995,7 +968,7 @@ void MtDec_Construct(CMtDec *p)
995 t->inBuf = NULL; 968 t->inBuf = NULL;
996 Event_Construct(&t->canRead); 969 Event_Construct(&t->canRead);
997 Event_Construct(&t->canWrite); 970 Event_Construct(&t->canWrite);
998 Thread_Construct(&t->thread); 971 Thread_CONSTRUCT(&t->thread)
999 } 972 }
1000 973
1001 // Event_Construct(&p->finishedEvent); 974 // Event_Construct(&p->finishedEvent);
@@ -1010,7 +983,7 @@ static void MtDec_Free(CMtDec *p)
1010 983
1011 p->exitThread = True; 984 p->exitThread = True;
1012 985
1013 for (i = 0; i < MTDEC__THREADS_MAX; i++) 986 for (i = 0; i < MTDEC_THREADS_MAX; i++)
1014 MtDecThread_Destruct(&p->threads[i]); 987 MtDecThread_Destruct(&p->threads[i]);
1015 988
1016 // Event_Close(&p->finishedEvent); 989 // Event_Close(&p->finishedEvent);
@@ -1061,15 +1034,15 @@ SRes MtDec_Code(CMtDec *p)
1061 1034
1062 { 1035 {
1063 unsigned numThreads = p->numThreadsMax; 1036 unsigned numThreads = p->numThreadsMax;
1064 if (numThreads > MTDEC__THREADS_MAX) 1037 if (numThreads > MTDEC_THREADS_MAX)
1065 numThreads = MTDEC__THREADS_MAX; 1038 numThreads = MTDEC_THREADS_MAX;
1066 p->numStartedThreads_Limit = numThreads; 1039 p->numStartedThreads_Limit = numThreads;
1067 p->numStartedThreads = 0; 1040 p->numStartedThreads = 0;
1068 } 1041 }
1069 1042
1070 if (p->inBufSize != p->allocatedBufsSize) 1043 if (p->inBufSize != p->allocatedBufsSize)
1071 { 1044 {
1072 for (i = 0; i < MTDEC__THREADS_MAX; i++) 1045 for (i = 0; i < MTDEC_THREADS_MAX; i++)
1073 { 1046 {
1074 CMtDecThread *t = &p->threads[i]; 1047 CMtDecThread *t = &p->threads[i];
1075 if (t->inBuf) 1048 if (t->inBuf)
@@ -1086,7 +1059,7 @@ SRes MtDec_Code(CMtDec *p)
1086 1059
1087 MtProgress_Init(&p->mtProgress, p->progress); 1060 MtProgress_Init(&p->mtProgress, p->progress);
1088 1061
1089 // RINOK_THREAD(ArEvent_OptCreate_And_Reset(&p->finishedEvent)); 1062 // RINOK_THREAD(AutoResetEvent_OptCreate_And_Reset(&p->finishedEvent))
1090 p->exitThread = False; 1063 p->exitThread = False;
1091 p->exitThreadWRes = 0; 1064 p->exitThreadWRes = 0;
1092 1065
@@ -1098,7 +1071,7 @@ SRes MtDec_Code(CMtDec *p)
1098 wres = MtDecThread_CreateEvents(nextThread); 1071 wres = MtDecThread_CreateEvents(nextThread);
1099 if (wres == 0) { wres = Event_Set(&nextThread->canWrite); 1072 if (wres == 0) { wres = Event_Set(&nextThread->canWrite);
1100 if (wres == 0) { wres = Event_Set(&nextThread->canRead); 1073 if (wres == 0) { wres = Event_Set(&nextThread->canRead);
1101 if (wres == 0) { THREAD_FUNC_RET_TYPE res = ThreadFunc(nextThread); 1074 if (wres == 0) { THREAD_FUNC_RET_TYPE res = MtDec_ThreadFunc(nextThread);
1102 wres = (WRes)(UINT_PTR)res; 1075 wres = (WRes)(UINT_PTR)res;
1103 if (wres != 0) 1076 if (wres != 0)
1104 { 1077 {
@@ -1137,3 +1110,5 @@ SRes MtDec_Code(CMtDec *p)
1137} 1110}
1138 1111
1139#endif 1112#endif
1113
1114#undef PRF
diff --git a/C/MtDec.h b/C/MtDec.h
index c2da46a..c28e8d9 100644
--- a/C/MtDec.h
+++ b/C/MtDec.h
@@ -1,46 +1,46 @@
1/* MtDec.h -- Multi-thread Decoder 1/* MtDec.h -- Multi-thread Decoder
22020-03-05 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __MT_DEC_H 4#ifndef ZIP7_INC_MT_DEC_H
5#define __MT_DEC_H 5#define ZIP7_INC_MT_DEC_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
9#ifndef _7ZIP_ST 9#ifndef Z7_ST
10#include "Threads.h" 10#include "Threads.h"
11#endif 11#endif
12 12
13EXTERN_C_BEGIN 13EXTERN_C_BEGIN
14 14
15#ifndef _7ZIP_ST 15#ifndef Z7_ST
16 16
17#ifndef _7ZIP_ST 17#ifndef Z7_ST
18 #define MTDEC__THREADS_MAX 32 18 #define MTDEC_THREADS_MAX 32
19#else 19#else
20 #define MTDEC__THREADS_MAX 1 20 #define MTDEC_THREADS_MAX 1
21#endif 21#endif
22 22
23 23
24typedef struct 24typedef struct
25{ 25{
26 ICompressProgress *progress; 26 ICompressProgressPtr progress;
27 SRes res; 27 SRes res;
28 UInt64 totalInSize; 28 UInt64 totalInSize;
29 UInt64 totalOutSize; 29 UInt64 totalOutSize;
30 CCriticalSection cs; 30 CCriticalSection cs;
31} CMtProgress; 31} CMtProgress;
32 32
33void MtProgress_Init(CMtProgress *p, ICompressProgress *progress); 33void MtProgress_Init(CMtProgress *p, ICompressProgressPtr progress);
34SRes MtProgress_Progress_ST(CMtProgress *p); 34SRes MtProgress_Progress_ST(CMtProgress *p);
35SRes MtProgress_ProgressAdd(CMtProgress *p, UInt64 inSize, UInt64 outSize); 35SRes MtProgress_ProgressAdd(CMtProgress *p, UInt64 inSize, UInt64 outSize);
36SRes MtProgress_GetError(CMtProgress *p); 36SRes MtProgress_GetError(CMtProgress *p);
37void MtProgress_SetError(CMtProgress *p, SRes res); 37void MtProgress_SetError(CMtProgress *p, SRes res);
38 38
39struct _CMtDec; 39struct CMtDec;
40 40
41typedef struct 41typedef struct
42{ 42{
43 struct _CMtDec *mtDec; 43 struct CMtDec_ *mtDec;
44 unsigned index; 44 unsigned index;
45 void *inBuf; 45 void *inBuf;
46 46
@@ -117,7 +117,7 @@ typedef struct
117 117
118 118
119 119
120typedef struct _CMtDec 120typedef struct CMtDec_
121{ 121{
122 /* input variables */ 122 /* input variables */
123 123
@@ -126,11 +126,11 @@ typedef struct _CMtDec
126 // size_t inBlockMax; 126 // size_t inBlockMax;
127 unsigned numThreadsMax_2; 127 unsigned numThreadsMax_2;
128 128
129 ISeqInStream *inStream; 129 ISeqInStreamPtr inStream;
130 // const Byte *inData; 130 // const Byte *inData;
131 // size_t inDataSize; 131 // size_t inDataSize;
132 132
133 ICompressProgress *progress; 133 ICompressProgressPtr progress;
134 ISzAllocPtr alloc; 134 ISzAllocPtr alloc;
135 135
136 IMtDecCallback2 *mtCallback; 136 IMtDecCallback2 *mtCallback;
@@ -171,11 +171,11 @@ typedef struct _CMtDec
171 unsigned filledThreadStart; 171 unsigned filledThreadStart;
172 unsigned numFilledThreads; 172 unsigned numFilledThreads;
173 173
174 #ifndef _7ZIP_ST 174 #ifndef Z7_ST
175 BoolInt needInterrupt; 175 BoolInt needInterrupt;
176 UInt64 interruptIndex; 176 UInt64 interruptIndex;
177 CMtProgress mtProgress; 177 CMtProgress mtProgress;
178 CMtDecThread threads[MTDEC__THREADS_MAX]; 178 CMtDecThread threads[MTDEC_THREADS_MAX];
179 #endif 179 #endif
180} CMtDec; 180} CMtDec;
181 181
diff --git a/C/Ppmd.h b/C/Ppmd.h
index b198792..66b2626 100644
--- a/C/Ppmd.h
+++ b/C/Ppmd.h
@@ -1,9 +1,9 @@
1/* Ppmd.h -- PPMD codec common code 1/* Ppmd.h -- PPMD codec common code
22021-04-13 : Igor Pavlov : Public domain 22023-03-05 : Igor Pavlov : Public domain
3This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */ 3This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
4 4
5#ifndef __PPMD_H 5#ifndef ZIP7_INC_PPMD_H
6#define __PPMD_H 6#define ZIP7_INC_PPMD_H
7 7
8#include "CpuArch.h" 8#include "CpuArch.h"
9 9
@@ -48,8 +48,10 @@ typedef struct
48 Byte Count; /* Count to next change of Shift */ 48 Byte Count; /* Count to next change of Shift */
49} CPpmd_See; 49} CPpmd_See;
50 50
51#define Ppmd_See_Update(p) if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \ 51#define Ppmd_See_UPDATE(p) \
52 { (p)->Summ = (UInt16)((p)->Summ << 1); (p)->Count = (Byte)(3 << (p)->Shift++); } 52 { if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \
53 { (p)->Summ = (UInt16)((p)->Summ << 1); \
54 (p)->Count = (Byte)(3 << (p)->Shift++); }}
53 55
54 56
55typedef struct 57typedef struct
diff --git a/C/Ppmd7.c b/C/Ppmd7.c
index cf401cb..6e1307e 100644
--- a/C/Ppmd7.c
+++ b/C/Ppmd7.c
@@ -1,5 +1,5 @@
1/* Ppmd7.c -- PPMdH codec 1/* Ppmd7.c -- PPMdH codec
22021-04-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */ 3This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
4 4
5#include "Precomp.h" 5#include "Precomp.h"
@@ -14,7 +14,7 @@ This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
14MY_ALIGN(16) 14MY_ALIGN(16)
15static const Byte PPMD7_kExpEscape[16] = { 25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 }; 15static const Byte PPMD7_kExpEscape[16] = { 25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 };
16MY_ALIGN(16) 16MY_ALIGN(16)
17static const UInt16 kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051}; 17static const UInt16 PPMD7_kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051};
18 18
19#define MAX_FREQ 124 19#define MAX_FREQ 124
20#define UNIT_SIZE 12 20#define UNIT_SIZE 12
@@ -33,7 +33,7 @@ static const UInt16 kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x
33#define ONE_STATE(ctx) Ppmd7Context_OneState(ctx) 33#define ONE_STATE(ctx) Ppmd7Context_OneState(ctx)
34#define SUFFIX(ctx) CTX((ctx)->Suffix) 34#define SUFFIX(ctx) CTX((ctx)->Suffix)
35 35
36typedef CPpmd7_Context * CTX_PTR; 36typedef CPpmd7_Context * PPMD7_CTX_PTR;
37 37
38struct CPpmd7_Node_; 38struct CPpmd7_Node_;
39 39
@@ -107,14 +107,14 @@ BoolInt Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAllocPtr alloc)
107// ---------- Internal Memory Allocator ---------- 107// ---------- Internal Memory Allocator ----------
108 108
109/* We can use CPpmd7_Node in list of free units (as in Ppmd8) 109/* We can use CPpmd7_Node in list of free units (as in Ppmd8)
110 But we still need one additional list walk pass in GlueFreeBlocks(). 110 But we still need one additional list walk pass in Ppmd7_GlueFreeBlocks().
111 So we use simple CPpmd_Void_Ref instead of CPpmd7_Node in InsertNode() / RemoveNode() 111 So we use simple CPpmd_Void_Ref instead of CPpmd7_Node in Ppmd7_InsertNode() / Ppmd7_RemoveNode()
112*/ 112*/
113 113
114#define EMPTY_NODE 0 114#define EMPTY_NODE 0
115 115
116 116
117static void InsertNode(CPpmd7 *p, void *node, unsigned indx) 117static void Ppmd7_InsertNode(CPpmd7 *p, void *node, unsigned indx)
118{ 118{
119 *((CPpmd_Void_Ref *)node) = p->FreeList[indx]; 119 *((CPpmd_Void_Ref *)node) = p->FreeList[indx];
120 // ((CPpmd7_Node *)node)->Next = (CPpmd7_Node_Ref)p->FreeList[indx]; 120 // ((CPpmd7_Node *)node)->Next = (CPpmd7_Node_Ref)p->FreeList[indx];
@@ -124,7 +124,7 @@ static void InsertNode(CPpmd7 *p, void *node, unsigned indx)
124} 124}
125 125
126 126
127static void *RemoveNode(CPpmd7 *p, unsigned indx) 127static void *Ppmd7_RemoveNode(CPpmd7 *p, unsigned indx)
128{ 128{
129 CPpmd_Void_Ref *node = (CPpmd_Void_Ref *)Ppmd7_GetPtr(p, p->FreeList[indx]); 129 CPpmd_Void_Ref *node = (CPpmd_Void_Ref *)Ppmd7_GetPtr(p, p->FreeList[indx]);
130 p->FreeList[indx] = *node; 130 p->FreeList[indx] = *node;
@@ -134,32 +134,32 @@ static void *RemoveNode(CPpmd7 *p, unsigned indx)
134} 134}
135 135
136 136
137static void SplitBlock(CPpmd7 *p, void *ptr, unsigned oldIndx, unsigned newIndx) 137static void Ppmd7_SplitBlock(CPpmd7 *p, void *ptr, unsigned oldIndx, unsigned newIndx)
138{ 138{
139 unsigned i, nu = I2U(oldIndx) - I2U(newIndx); 139 unsigned i, nu = I2U(oldIndx) - I2U(newIndx);
140 ptr = (Byte *)ptr + U2B(I2U(newIndx)); 140 ptr = (Byte *)ptr + U2B(I2U(newIndx));
141 if (I2U(i = U2I(nu)) != nu) 141 if (I2U(i = U2I(nu)) != nu)
142 { 142 {
143 unsigned k = I2U(--i); 143 unsigned k = I2U(--i);
144 InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1); 144 Ppmd7_InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1);
145 } 145 }
146 InsertNode(p, ptr, i); 146 Ppmd7_InsertNode(p, ptr, i);
147} 147}
148 148
149 149
150/* we use CPpmd7_Node_Union union to solve XLC -O2 strict pointer aliasing problem */ 150/* we use CPpmd7_Node_Union union to solve XLC -O2 strict pointer aliasing problem */
151 151
152typedef union _CPpmd7_Node_Union 152typedef union
153{ 153{
154 CPpmd7_Node Node; 154 CPpmd7_Node Node;
155 CPpmd7_Node_Ref NextRef; 155 CPpmd7_Node_Ref NextRef;
156} CPpmd7_Node_Union; 156} CPpmd7_Node_Union;
157 157
158/* Original PPmdH (Ppmd7) code uses doubly linked list in GlueFreeBlocks() 158/* Original PPmdH (Ppmd7) code uses doubly linked list in Ppmd7_GlueFreeBlocks()
159 we use single linked list similar to Ppmd8 code */ 159 we use single linked list similar to Ppmd8 code */
160 160
161 161
162static void GlueFreeBlocks(CPpmd7 *p) 162static void Ppmd7_GlueFreeBlocks(CPpmd7 *p)
163{ 163{
164 /* 164 /*
165 we use first UInt16 field of 12-bytes UNITs as record type stamp 165 we use first UInt16 field of 12-bytes UNITs as record type stamp
@@ -239,27 +239,27 @@ static void GlueFreeBlocks(CPpmd7 *p)
239 if (nu == 0) 239 if (nu == 0)
240 continue; 240 continue;
241 for (; nu > 128; nu -= 128, node += 128) 241 for (; nu > 128; nu -= 128, node += 128)
242 InsertNode(p, node, PPMD_NUM_INDEXES - 1); 242 Ppmd7_InsertNode(p, node, PPMD_NUM_INDEXES - 1);
243 if (I2U(i = U2I(nu)) != nu) 243 if (I2U(i = U2I(nu)) != nu)
244 { 244 {
245 unsigned k = I2U(--i); 245 unsigned k = I2U(--i);
246 InsertNode(p, node + k, (unsigned)nu - k - 1); 246 Ppmd7_InsertNode(p, node + k, (unsigned)nu - k - 1);
247 } 247 }
248 InsertNode(p, node, i); 248 Ppmd7_InsertNode(p, node, i);
249 } 249 }
250} 250}
251 251
252 252
253MY_NO_INLINE 253Z7_NO_INLINE
254static void *AllocUnitsRare(CPpmd7 *p, unsigned indx) 254static void *Ppmd7_AllocUnitsRare(CPpmd7 *p, unsigned indx)
255{ 255{
256 unsigned i; 256 unsigned i;
257 257
258 if (p->GlueCount == 0) 258 if (p->GlueCount == 0)
259 { 259 {
260 GlueFreeBlocks(p); 260 Ppmd7_GlueFreeBlocks(p);
261 if (p->FreeList[indx] != 0) 261 if (p->FreeList[indx] != 0)
262 return RemoveNode(p, indx); 262 return Ppmd7_RemoveNode(p, indx);
263 } 263 }
264 264
265 i = indx; 265 i = indx;
@@ -277,17 +277,17 @@ static void *AllocUnitsRare(CPpmd7 *p, unsigned indx)
277 while (p->FreeList[i] == 0); 277 while (p->FreeList[i] == 0);
278 278
279 { 279 {
280 void *block = RemoveNode(p, i); 280 void *block = Ppmd7_RemoveNode(p, i);
281 SplitBlock(p, block, i, indx); 281 Ppmd7_SplitBlock(p, block, i, indx);
282 return block; 282 return block;
283 } 283 }
284} 284}
285 285
286 286
287static void *AllocUnits(CPpmd7 *p, unsigned indx) 287static void *Ppmd7_AllocUnits(CPpmd7 *p, unsigned indx)
288{ 288{
289 if (p->FreeList[indx] != 0) 289 if (p->FreeList[indx] != 0)
290 return RemoveNode(p, indx); 290 return Ppmd7_RemoveNode(p, indx);
291 { 291 {
292 UInt32 numBytes = U2B(I2U(indx)); 292 UInt32 numBytes = U2B(I2U(indx));
293 Byte *lo = p->LoUnit; 293 Byte *lo = p->LoUnit;
@@ -297,11 +297,11 @@ static void *AllocUnits(CPpmd7 *p, unsigned indx)
297 return lo; 297 return lo;
298 } 298 }
299 } 299 }
300 return AllocUnitsRare(p, indx); 300 return Ppmd7_AllocUnitsRare(p, indx);
301} 301}
302 302
303 303
304#define MyMem12Cpy(dest, src, num) \ 304#define MEM_12_CPY(dest, src, num) \
305 { UInt32 *d = (UInt32 *)dest; const UInt32 *z = (const UInt32 *)src; UInt32 n = num; \ 305 { UInt32 *d = (UInt32 *)dest; const UInt32 *z = (const UInt32 *)src; UInt32 n = num; \
306 do { d[0] = z[0]; d[1] = z[1]; d[2] = z[2]; z += 3; d += 3; } while (--n); } 306 do { d[0] = z[0]; d[1] = z[1]; d[2] = z[2]; z += 3; d += 3; } while (--n); }
307 307
@@ -315,12 +315,12 @@ static void *ShrinkUnits(CPpmd7 *p, void *oldPtr, unsigned oldNU, unsigned newNU
315 return oldPtr; 315 return oldPtr;
316 if (p->FreeList[i1] != 0) 316 if (p->FreeList[i1] != 0)
317 { 317 {
318 void *ptr = RemoveNode(p, i1); 318 void *ptr = Ppmd7_RemoveNode(p, i1);
319 MyMem12Cpy(ptr, oldPtr, newNU); 319 MEM_12_CPY(ptr, oldPtr, newNU)
320 InsertNode(p, oldPtr, i0); 320 Ppmd7_InsertNode(p, oldPtr, i0);
321 return ptr; 321 return ptr;
322 } 322 }
323 SplitBlock(p, oldPtr, i0, i1); 323 Ppmd7_SplitBlock(p, oldPtr, i0, i1);
324 return oldPtr; 324 return oldPtr;
325} 325}
326*/ 326*/
@@ -329,14 +329,14 @@ static void *ShrinkUnits(CPpmd7 *p, void *oldPtr, unsigned oldNU, unsigned newNU
329#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) 329#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p)
330static void SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v) 330static void SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v)
331{ 331{
332 Ppmd_SET_SUCCESSOR(p, v); 332 Ppmd_SET_SUCCESSOR(p, v)
333} 333}
334 334
335 335
336 336
337MY_NO_INLINE 337Z7_NO_INLINE
338static 338static
339void RestartModel(CPpmd7 *p) 339void Ppmd7_RestartModel(CPpmd7 *p)
340{ 340{
341 unsigned i, k; 341 unsigned i, k;
342 342
@@ -352,8 +352,8 @@ void RestartModel(CPpmd7 *p)
352 p->PrevSuccess = 0; 352 p->PrevSuccess = 0;
353 353
354 { 354 {
355 CPpmd7_Context *mc = (CTX_PTR)(void *)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */ 355 CPpmd7_Context *mc = (PPMD7_CTX_PTR)(void *)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */
356 CPpmd_State *s = (CPpmd_State *)p->LoUnit; /* AllocUnits(p, PPMD_NUM_INDEXES - 1); */ 356 CPpmd_State *s = (CPpmd_State *)p->LoUnit; /* Ppmd7_AllocUnits(p, PPMD_NUM_INDEXES - 1); */
357 357
358 p->LoUnit += U2B(256 / 2); 358 p->LoUnit += U2B(256 / 2);
359 p->MaxContext = p->MinContext = mc; 359 p->MaxContext = p->MinContext = mc;
@@ -391,7 +391,7 @@ void RestartModel(CPpmd7 *p)
391 { 391 {
392 unsigned m; 392 unsigned m;
393 UInt16 *dest = p->BinSumm[i] + k; 393 UInt16 *dest = p->BinSumm[i] + k;
394 UInt16 val = (UInt16)(PPMD_BIN_SCALE - kInitBinEsc[k] / (i + 2)); 394 const UInt16 val = (UInt16)(PPMD_BIN_SCALE - PPMD7_kInitBinEsc[k] / (i + 2));
395 for (m = 0; m < 64; m += 8) 395 for (m = 0; m < 64; m += 8)
396 dest[m] = val; 396 dest[m] = val;
397 } 397 }
@@ -423,13 +423,13 @@ void Ppmd7_Init(CPpmd7 *p, unsigned maxOrder)
423{ 423{
424 p->MaxOrder = maxOrder; 424 p->MaxOrder = maxOrder;
425 425
426 RestartModel(p); 426 Ppmd7_RestartModel(p);
427} 427}
428 428
429 429
430 430
431/* 431/*
432 CreateSuccessors() 432 Ppmd7_CreateSuccessors()
433 It's called when (FoundState->Successor) is RAW-Successor, 433 It's called when (FoundState->Successor) is RAW-Successor,
434 that is the link to position in Raw text. 434 that is the link to position in Raw text.
435 So we create Context records and write the links to 435 So we create Context records and write the links to
@@ -445,10 +445,10 @@ void Ppmd7_Init(CPpmd7 *p, unsigned maxOrder)
445 also it can return pointer to real context of same order, 445 also it can return pointer to real context of same order,
446*/ 446*/
447 447
448MY_NO_INLINE 448Z7_NO_INLINE
449static CTX_PTR CreateSuccessors(CPpmd7 *p) 449static PPMD7_CTX_PTR Ppmd7_CreateSuccessors(CPpmd7 *p)
450{ 450{
451 CTX_PTR c = p->MinContext; 451 PPMD7_CTX_PTR c = p->MinContext;
452 CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState); 452 CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState);
453 Byte newSym, newFreq; 453 Byte newSym, newFreq;
454 unsigned numPs = 0; 454 unsigned numPs = 0;
@@ -522,15 +522,15 @@ static CTX_PTR CreateSuccessors(CPpmd7 *p)
522 522
523 do 523 do
524 { 524 {
525 CTX_PTR c1; 525 PPMD7_CTX_PTR c1;
526 /* = AllocContext(p); */ 526 /* = AllocContext(p); */
527 if (p->HiUnit != p->LoUnit) 527 if (p->HiUnit != p->LoUnit)
528 c1 = (CTX_PTR)(void *)(p->HiUnit -= UNIT_SIZE); 528 c1 = (PPMD7_CTX_PTR)(void *)(p->HiUnit -= UNIT_SIZE);
529 else if (p->FreeList[0] != 0) 529 else if (p->FreeList[0] != 0)
530 c1 = (CTX_PTR)RemoveNode(p, 0); 530 c1 = (PPMD7_CTX_PTR)Ppmd7_RemoveNode(p, 0);
531 else 531 else
532 { 532 {
533 c1 = (CTX_PTR)AllocUnitsRare(p, 0); 533 c1 = (PPMD7_CTX_PTR)Ppmd7_AllocUnitsRare(p, 0);
534 if (!c1) 534 if (!c1)
535 return NULL; 535 return NULL;
536 } 536 }
@@ -550,16 +550,16 @@ static CTX_PTR CreateSuccessors(CPpmd7 *p)
550 550
551 551
552 552
553#define SwapStates(s) \ 553#define SWAP_STATES(s) \
554 { CPpmd_State tmp = s[0]; s[0] = s[-1]; s[-1] = tmp; } 554 { CPpmd_State tmp = s[0]; s[0] = s[-1]; s[-1] = tmp; }
555 555
556 556
557void Ppmd7_UpdateModel(CPpmd7 *p); 557void Ppmd7_UpdateModel(CPpmd7 *p);
558MY_NO_INLINE 558Z7_NO_INLINE
559void Ppmd7_UpdateModel(CPpmd7 *p) 559void Ppmd7_UpdateModel(CPpmd7 *p)
560{ 560{
561 CPpmd_Void_Ref maxSuccessor, minSuccessor; 561 CPpmd_Void_Ref maxSuccessor, minSuccessor;
562 CTX_PTR c, mc; 562 PPMD7_CTX_PTR c, mc;
563 unsigned s0, ns; 563 unsigned s0, ns;
564 564
565 565
@@ -592,7 +592,7 @@ void Ppmd7_UpdateModel(CPpmd7 *p)
592 592
593 if (s[0].Freq >= s[-1].Freq) 593 if (s[0].Freq >= s[-1].Freq)
594 { 594 {
595 SwapStates(s); 595 SWAP_STATES(s)
596 s--; 596 s--;
597 } 597 }
598 } 598 }
@@ -610,10 +610,10 @@ void Ppmd7_UpdateModel(CPpmd7 *p)
610 { 610 {
611 /* MAX ORDER context */ 611 /* MAX ORDER context */
612 /* (FoundState->Successor) is RAW-Successor. */ 612 /* (FoundState->Successor) is RAW-Successor. */
613 p->MaxContext = p->MinContext = CreateSuccessors(p); 613 p->MaxContext = p->MinContext = Ppmd7_CreateSuccessors(p);
614 if (!p->MinContext) 614 if (!p->MinContext)
615 { 615 {
616 RestartModel(p); 616 Ppmd7_RestartModel(p);
617 return; 617 return;
618 } 618 }
619 SetSuccessor(p->FoundState, REF(p->MinContext)); 619 SetSuccessor(p->FoundState, REF(p->MinContext));
@@ -629,7 +629,7 @@ void Ppmd7_UpdateModel(CPpmd7 *p)
629 p->Text = text; 629 p->Text = text;
630 if (text >= p->UnitsStart) 630 if (text >= p->UnitsStart)
631 { 631 {
632 RestartModel(p); 632 Ppmd7_RestartModel(p);
633 return; 633 return;
634 } 634 }
635 maxSuccessor = REF(text); 635 maxSuccessor = REF(text);
@@ -645,10 +645,10 @@ void Ppmd7_UpdateModel(CPpmd7 *p)
645 if (minSuccessor <= maxSuccessor) 645 if (minSuccessor <= maxSuccessor)
646 { 646 {
647 // minSuccessor is RAW-Successor. So we will create real contexts records: 647 // minSuccessor is RAW-Successor. So we will create real contexts records:
648 CTX_PTR cs = CreateSuccessors(p); 648 PPMD7_CTX_PTR cs = Ppmd7_CreateSuccessors(p);
649 if (!cs) 649 if (!cs)
650 { 650 {
651 RestartModel(p); 651 Ppmd7_RestartModel(p);
652 return; 652 return;
653 } 653 }
654 minSuccessor = REF(cs); 654 minSuccessor = REF(cs);
@@ -715,16 +715,16 @@ void Ppmd7_UpdateModel(CPpmd7 *p)
715 unsigned i = U2I(oldNU); 715 unsigned i = U2I(oldNU);
716 if (i != U2I((size_t)oldNU + 1)) 716 if (i != U2I((size_t)oldNU + 1))
717 { 717 {
718 void *ptr = AllocUnits(p, i + 1); 718 void *ptr = Ppmd7_AllocUnits(p, i + 1);
719 void *oldPtr; 719 void *oldPtr;
720 if (!ptr) 720 if (!ptr)
721 { 721 {
722 RestartModel(p); 722 Ppmd7_RestartModel(p);
723 return; 723 return;
724 } 724 }
725 oldPtr = STATS(c); 725 oldPtr = STATS(c);
726 MyMem12Cpy(ptr, oldPtr, oldNU); 726 MEM_12_CPY(ptr, oldPtr, oldNU)
727 InsertNode(p, oldPtr, i); 727 Ppmd7_InsertNode(p, oldPtr, i);
728 c->Union4.Stats = STATS_REF(ptr); 728 c->Union4.Stats = STATS_REF(ptr);
729 } 729 }
730 } 730 }
@@ -739,10 +739,10 @@ void Ppmd7_UpdateModel(CPpmd7 *p)
739 else 739 else
740 { 740 {
741 // instead of One-symbol context we create 2-symbol context 741 // instead of One-symbol context we create 2-symbol context
742 CPpmd_State *s = (CPpmd_State*)AllocUnits(p, 0); 742 CPpmd_State *s = (CPpmd_State*)Ppmd7_AllocUnits(p, 0);
743 if (!s) 743 if (!s)
744 { 744 {
745 RestartModel(p); 745 Ppmd7_RestartModel(p);
746 return; 746 return;
747 } 747 }
748 { 748 {
@@ -795,8 +795,8 @@ void Ppmd7_UpdateModel(CPpmd7 *p)
795 795
796 796
797 797
798MY_NO_INLINE 798Z7_NO_INLINE
799static void Rescale(CPpmd7 *p) 799static void Ppmd7_Rescale(CPpmd7 *p)
800{ 800{
801 unsigned i, adder, sumFreq, escFreq; 801 unsigned i, adder, sumFreq, escFreq;
802 CPpmd_State *stats = STATS(p->MinContext); 802 CPpmd_State *stats = STATS(p->MinContext);
@@ -885,7 +885,7 @@ static void Rescale(CPpmd7 *p)
885 *s = *stats; 885 *s = *stats;
886 s->Freq = (Byte)freq; // (freq <= 260 / 4) 886 s->Freq = (Byte)freq; // (freq <= 260 / 4)
887 p->FoundState = s; 887 p->FoundState = s;
888 InsertNode(p, stats, U2I(n0)); 888 Ppmd7_InsertNode(p, stats, U2I(n0));
889 return; 889 return;
890 } 890 }
891 891
@@ -899,13 +899,13 @@ static void Rescale(CPpmd7 *p)
899 { 899 {
900 if (p->FreeList[i1] != 0) 900 if (p->FreeList[i1] != 0)
901 { 901 {
902 void *ptr = RemoveNode(p, i1); 902 void *ptr = Ppmd7_RemoveNode(p, i1);
903 p->MinContext->Union4.Stats = STATS_REF(ptr); 903 p->MinContext->Union4.Stats = STATS_REF(ptr);
904 MyMem12Cpy(ptr, (const void *)stats, n1); 904 MEM_12_CPY(ptr, (const void *)stats, n1)
905 InsertNode(p, stats, i0); 905 Ppmd7_InsertNode(p, stats, i0);
906 } 906 }
907 else 907 else
908 SplitBlock(p, stats, i0, i1); 908 Ppmd7_SplitBlock(p, stats, i0, i1);
909 } 909 }
910 } 910 }
911 } 911 }
@@ -948,9 +948,9 @@ CPpmd_See *Ppmd7_MakeEscFreq(CPpmd7 *p, unsigned numMasked, UInt32 *escFreq)
948} 948}
949 949
950 950
951static void NextContext(CPpmd7 *p) 951static void Ppmd7_NextContext(CPpmd7 *p)
952{ 952{
953 CTX_PTR c = CTX(SUCCESSOR(p->FoundState)); 953 PPMD7_CTX_PTR c = CTX(SUCCESSOR(p->FoundState));
954 if (p->OrderFall == 0 && (const Byte *)c > p->Text) 954 if (p->OrderFall == 0 && (const Byte *)c > p->Text)
955 p->MaxContext = p->MinContext = c; 955 p->MaxContext = p->MinContext = c;
956 else 956 else
@@ -967,12 +967,12 @@ void Ppmd7_Update1(CPpmd7 *p)
967 s->Freq = (Byte)freq; 967 s->Freq = (Byte)freq;
968 if (freq > s[-1].Freq) 968 if (freq > s[-1].Freq)
969 { 969 {
970 SwapStates(s); 970 SWAP_STATES(s)
971 p->FoundState = --s; 971 p->FoundState = --s;
972 if (freq > MAX_FREQ) 972 if (freq > MAX_FREQ)
973 Rescale(p); 973 Ppmd7_Rescale(p);
974 } 974 }
975 NextContext(p); 975 Ppmd7_NextContext(p);
976} 976}
977 977
978 978
@@ -988,8 +988,8 @@ void Ppmd7_Update1_0(CPpmd7 *p)
988 freq += 4; 988 freq += 4;
989 s->Freq = (Byte)freq; 989 s->Freq = (Byte)freq;
990 if (freq > MAX_FREQ) 990 if (freq > MAX_FREQ)
991 Rescale(p); 991 Ppmd7_Rescale(p);
992 NextContext(p); 992 Ppmd7_NextContext(p);
993} 993}
994 994
995 995
@@ -1000,7 +1000,7 @@ void Ppmd7_UpdateBin(CPpmd7 *p)
1000 p->FoundState->Freq = (Byte)(freq + (freq < 128)); 1000 p->FoundState->Freq = (Byte)(freq + (freq < 128));
1001 p->PrevSuccess = 1; 1001 p->PrevSuccess = 1;
1002 p->RunLength++; 1002 p->RunLength++;
1003 NextContext(p); 1003 Ppmd7_NextContext(p);
1004} 1004}
1005*/ 1005*/
1006 1006
@@ -1013,7 +1013,7 @@ void Ppmd7_Update2(CPpmd7 *p)
1013 p->MinContext->Union2.SummFreq = (UInt16)(p->MinContext->Union2.SummFreq + 4); 1013 p->MinContext->Union2.SummFreq = (UInt16)(p->MinContext->Union2.SummFreq + 4);
1014 s->Freq = (Byte)freq; 1014 s->Freq = (Byte)freq;
1015 if (freq > MAX_FREQ) 1015 if (freq > MAX_FREQ)
1016 Rescale(p); 1016 Ppmd7_Rescale(p);
1017 Ppmd7_UpdateModel(p); 1017 Ppmd7_UpdateModel(p);
1018} 1018}
1019 1019
@@ -1042,8 +1042,8 @@ Last UNIT of array at offset (Size - 12) is root order-0 CPpmd7_Context record.
1042The code can free UNITs memory blocks that were allocated to store CPpmd_State vectors. 1042The code can free UNITs memory blocks that were allocated to store CPpmd_State vectors.
1043The code doesn't free UNITs allocated for CPpmd7_Context records. 1043The code doesn't free UNITs allocated for CPpmd7_Context records.
1044 1044
1045The code calls RestartModel(), when there is no free memory for allocation. 1045The code calls Ppmd7_RestartModel(), when there is no free memory for allocation.
1046And RestartModel() changes the state to orignal start state, with full free block. 1046And Ppmd7_RestartModel() changes the state to orignal start state, with full free block.
1047 1047
1048 1048
1049The code allocates UNITs with the following order: 1049The code allocates UNITs with the following order:
@@ -1051,14 +1051,14 @@ The code allocates UNITs with the following order:
1051Allocation of 1 UNIT for Context record 1051Allocation of 1 UNIT for Context record
1052 - from free space (HiUnit) down to (LoUnit) 1052 - from free space (HiUnit) down to (LoUnit)
1053 - from FreeList[0] 1053 - from FreeList[0]
1054 - AllocUnitsRare() 1054 - Ppmd7_AllocUnitsRare()
1055 1055
1056AllocUnits() for CPpmd_State vectors: 1056Ppmd7_AllocUnits() for CPpmd_State vectors:
1057 - from FreeList[i] 1057 - from FreeList[i]
1058 - from free space (LoUnit) up to (HiUnit) 1058 - from free space (LoUnit) up to (HiUnit)
1059 - AllocUnitsRare() 1059 - Ppmd7_AllocUnitsRare()
1060 1060
1061AllocUnitsRare() 1061Ppmd7_AllocUnitsRare()
1062 - if (GlueCount == 0) 1062 - if (GlueCount == 0)
1063 { Glue lists, GlueCount = 255, allocate from FreeList[i]] } 1063 { Glue lists, GlueCount = 255, allocate from FreeList[i]] }
1064 - loop for all higher sized FreeList[...] lists 1064 - loop for all higher sized FreeList[...] lists
@@ -1093,8 +1093,8 @@ The PPMd code tries to fulfill the condition:
1093We have (Sum(Stats[].Freq) <= 256 * 124), because of (MAX_FREQ = 124) 1093We have (Sum(Stats[].Freq) <= 256 * 124), because of (MAX_FREQ = 124)
1094So (4 = 128 - 124) is average reserve for Escape_Freq for each symbol. 1094So (4 = 128 - 124) is average reserve for Escape_Freq for each symbol.
1095If (CPpmd_State::Freq) is not aligned for 4, the reserve can be 5, 6 or 7. 1095If (CPpmd_State::Freq) is not aligned for 4, the reserve can be 5, 6 or 7.
1096SummFreq and Escape_Freq can be changed in Rescale() and *Update*() functions. 1096SummFreq and Escape_Freq can be changed in Ppmd7_Rescale() and *Update*() functions.
1097Rescale() can remove symbols only from max-order contexts. So Escape_Freq can increase after multiple calls of Rescale() for 1097Ppmd7_Rescale() can remove symbols only from max-order contexts. So Escape_Freq can increase after multiple calls of Ppmd7_Rescale() for
1098max-order context. 1098max-order context.
1099 1099
1100When the PPMd code still break (Total <= RC::Range) condition in range coder, 1100When the PPMd code still break (Total <= RC::Range) condition in range coder,
@@ -1102,3 +1102,21 @@ we have two ways to resolve that problem:
1102 1) we can report error, if we want to keep compatibility with original PPMd code that has no fix for such cases. 1102 1) we can report error, if we want to keep compatibility with original PPMd code that has no fix for such cases.
1103 2) we can reduce (Total) value to (RC::Range) by reducing (Escape_Freq) part of (Total) value. 1103 2) we can reduce (Total) value to (RC::Range) by reducing (Escape_Freq) part of (Total) value.
1104*/ 1104*/
1105
1106#undef MAX_FREQ
1107#undef UNIT_SIZE
1108#undef U2B
1109#undef U2I
1110#undef I2U
1111#undef I2U_UInt16
1112#undef REF
1113#undef STATS_REF
1114#undef CTX
1115#undef STATS
1116#undef ONE_STATE
1117#undef SUFFIX
1118#undef NODE
1119#undef EMPTY_NODE
1120#undef MEM_12_CPY
1121#undef SUCCESSOR
1122#undef SWAP_STATES
diff --git a/C/Ppmd7.h b/C/Ppmd7.h
index d31809a..d9eb326 100644
--- a/C/Ppmd7.h
+++ b/C/Ppmd7.h
@@ -1,11 +1,11 @@
1/* Ppmd7.h -- Ppmd7 (PPMdH) compression codec 1/* Ppmd7.h -- Ppmd7 (PPMdH) compression codec
22021-04-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on: 3This code is based on:
4 PPMd var.H (2001): Dmitry Shkarin : Public domain */ 4 PPMd var.H (2001): Dmitry Shkarin : Public domain */
5 5
6 6
7#ifndef __PPMD7_H 7#ifndef ZIP7_INC_PPMD7_H
8#define __PPMD7_H 8#define ZIP7_INC_PPMD7_H
9 9
10#include "Ppmd.h" 10#include "Ppmd.h"
11 11
@@ -55,7 +55,7 @@ typedef struct
55 UInt32 Range; 55 UInt32 Range;
56 UInt32 Code; 56 UInt32 Code;
57 UInt32 Low; 57 UInt32 Low;
58 IByteIn *Stream; 58 IByteInPtr Stream;
59} CPpmd7_RangeDec; 59} CPpmd7_RangeDec;
60 60
61 61
@@ -66,7 +66,7 @@ typedef struct
66 // Byte _dummy_[3]; 66 // Byte _dummy_[3];
67 UInt64 Low; 67 UInt64 Low;
68 UInt64 CacheSize; 68 UInt64 CacheSize;
69 IByteOut *Stream; 69 IByteOutPtr Stream;
70} CPpmd7z_RangeEnc; 70} CPpmd7z_RangeEnc;
71 71
72 72
diff --git a/C/Ppmd7Dec.c b/C/Ppmd7Dec.c
index 55d74ff..8323828 100644
--- a/C/Ppmd7Dec.c
+++ b/C/Ppmd7Dec.c
@@ -1,5 +1,5 @@
1/* Ppmd7Dec.c -- Ppmd7z (PPMdH with 7z Range Coder) Decoder 1/* Ppmd7Dec.c -- Ppmd7z (PPMdH with 7z Range Coder) Decoder
22021-04-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on: 3This code is based on:
4 PPMd var.H (2001): Dmitry Shkarin : Public domain */ 4 PPMd var.H (2001): Dmitry Shkarin : Public domain */
5 5
@@ -8,7 +8,7 @@ This code is based on:
8 8
9#include "Ppmd7.h" 9#include "Ppmd7.h"
10 10
11#define kTopValue (1 << 24) 11#define kTopValue ((UInt32)1 << 24)
12 12
13 13
14#define READ_BYTE(p) IByteIn_Read((p)->Stream) 14#define READ_BYTE(p) IByteIn_Read((p)->Stream)
@@ -37,9 +37,9 @@ BoolInt Ppmd7z_RangeDec_Init(CPpmd7_RangeDec *p)
37 37
38#define R (&p->rc.dec) 38#define R (&p->rc.dec)
39 39
40MY_FORCE_INLINE 40Z7_FORCE_INLINE
41// MY_NO_INLINE 41// Z7_NO_INLINE
42static void RangeDec_Decode(CPpmd7 *p, UInt32 start, UInt32 size) 42static void Ppmd7z_RD_Decode(CPpmd7 *p, UInt32 start, UInt32 size)
43{ 43{
44 44
45 45
@@ -48,18 +48,18 @@ static void RangeDec_Decode(CPpmd7 *p, UInt32 start, UInt32 size)
48 RC_NORM_LOCAL(R) 48 RC_NORM_LOCAL(R)
49} 49}
50 50
51#define RC_Decode(start, size) RangeDec_Decode(p, start, size); 51#define RC_Decode(start, size) Ppmd7z_RD_Decode(p, start, size);
52#define RC_DecodeFinal(start, size) RC_Decode(start, size) RC_NORM_REMOTE(R) 52#define RC_DecodeFinal(start, size) RC_Decode(start, size) RC_NORM_REMOTE(R)
53#define RC_GetThreshold(total) (R->Code / (R->Range /= (total))) 53#define RC_GetThreshold(total) (R->Code / (R->Range /= (total)))
54 54
55 55
56#define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref)) 56#define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref))
57typedef CPpmd7_Context * CTX_PTR; 57// typedef CPpmd7_Context * CTX_PTR;
58#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) 58#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p)
59void Ppmd7_UpdateModel(CPpmd7 *p); 59void Ppmd7_UpdateModel(CPpmd7 *p);
60 60
61#define MASK(sym) ((unsigned char *)charMask)[sym] 61#define MASK(sym) ((unsigned char *)charMask)[sym]
62// MY_FORCE_INLINE 62// Z7_FORCE_INLINE
63// static 63// static
64int Ppmd7z_DecodeSymbol(CPpmd7 *p) 64int Ppmd7z_DecodeSymbol(CPpmd7 *p)
65{ 65{
@@ -70,7 +70,7 @@ int Ppmd7z_DecodeSymbol(CPpmd7 *p)
70 CPpmd_State *s = Ppmd7_GetStats(p, p->MinContext); 70 CPpmd_State *s = Ppmd7_GetStats(p, p->MinContext);
71 unsigned i; 71 unsigned i;
72 UInt32 count, hiCnt; 72 UInt32 count, hiCnt;
73 UInt32 summFreq = p->MinContext->Union2.SummFreq; 73 const UInt32 summFreq = p->MinContext->Union2.SummFreq;
74 74
75 75
76 76
@@ -81,7 +81,7 @@ int Ppmd7z_DecodeSymbol(CPpmd7 *p)
81 if ((Int32)(count -= s->Freq) < 0) 81 if ((Int32)(count -= s->Freq) < 0)
82 { 82 {
83 Byte sym; 83 Byte sym;
84 RC_DecodeFinal(0, s->Freq); 84 RC_DecodeFinal(0, s->Freq)
85 p->FoundState = s; 85 p->FoundState = s;
86 sym = s->Symbol; 86 sym = s->Symbol;
87 Ppmd7_Update1_0(p); 87 Ppmd7_Update1_0(p);
@@ -96,7 +96,7 @@ int Ppmd7z_DecodeSymbol(CPpmd7 *p)
96 if ((Int32)(count -= (++s)->Freq) < 0) 96 if ((Int32)(count -= (++s)->Freq) < 0)
97 { 97 {
98 Byte sym; 98 Byte sym;
99 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq); 99 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq)
100 p->FoundState = s; 100 p->FoundState = s;
101 sym = s->Symbol; 101 sym = s->Symbol;
102 Ppmd7_Update1(p); 102 Ppmd7_Update1(p);
@@ -109,10 +109,10 @@ int Ppmd7z_DecodeSymbol(CPpmd7 *p)
109 return PPMD7_SYM_ERROR; 109 return PPMD7_SYM_ERROR;
110 110
111 hiCnt -= count; 111 hiCnt -= count;
112 RC_Decode(hiCnt, summFreq - hiCnt); 112 RC_Decode(hiCnt, summFreq - hiCnt)
113 113
114 p->HiBitsFlag = PPMD7_HiBitsFlag_3(p->FoundState->Symbol); 114 p->HiBitsFlag = PPMD7_HiBitsFlag_3(p->FoundState->Symbol);
115 PPMD_SetAllBitsIn256Bytes(charMask); 115 PPMD_SetAllBitsIn256Bytes(charMask)
116 // i = p->MinContext->NumStats - 1; 116 // i = p->MinContext->NumStats - 1;
117 // do { MASK((--s)->Symbol) = 0; } while (--i); 117 // do { MASK((--s)->Symbol) = 0; } while (--i);
118 { 118 {
@@ -152,7 +152,7 @@ int Ppmd7z_DecodeSymbol(CPpmd7 *p)
152 // Ppmd7_UpdateBin(p); 152 // Ppmd7_UpdateBin(p);
153 { 153 {
154 unsigned freq = s->Freq; 154 unsigned freq = s->Freq;
155 CTX_PTR c = CTX(SUCCESSOR(s)); 155 CPpmd7_Context *c = CTX(SUCCESSOR(s));
156 sym = s->Symbol; 156 sym = s->Symbol;
157 p->FoundState = s; 157 p->FoundState = s;
158 p->PrevSuccess = 1; 158 p->PrevSuccess = 1;
@@ -176,7 +176,7 @@ int Ppmd7z_DecodeSymbol(CPpmd7 *p)
176 R->Range -= size0; 176 R->Range -= size0;
177 RC_NORM_LOCAL(R) 177 RC_NORM_LOCAL(R)
178 178
179 PPMD_SetAllBitsIn256Bytes(charMask); 179 PPMD_SetAllBitsIn256Bytes(charMask)
180 MASK(Ppmd7Context_OneState(p->MinContext)->Symbol) = 0; 180 MASK(Ppmd7Context_OneState(p->MinContext)->Symbol) = 0;
181 p->PrevSuccess = 0; 181 p->PrevSuccess = 0;
182 } 182 }
@@ -245,13 +245,13 @@ int Ppmd7z_DecodeSymbol(CPpmd7 *p)
245 { 245 {
246 count -= s->Freq & (unsigned)(MASK((s)->Symbol)); s++; if ((Int32)count < 0) break; 246 count -= s->Freq & (unsigned)(MASK((s)->Symbol)); s++; if ((Int32)count < 0) break;
247 // count -= s->Freq & (unsigned)(MASK((s)->Symbol)); s++; if ((Int32)count < 0) break; 247 // count -= s->Freq & (unsigned)(MASK((s)->Symbol)); s++; if ((Int32)count < 0) break;
248 }; 248 }
249 } 249 }
250 s--; 250 s--;
251 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq); 251 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq)
252 252
253 // new (see->Summ) value can overflow over 16-bits in some rare cases 253 // new (see->Summ) value can overflow over 16-bits in some rare cases
254 Ppmd_See_Update(see); 254 Ppmd_See_UPDATE(see)
255 p->FoundState = s; 255 p->FoundState = s;
256 sym = s->Symbol; 256 sym = s->Symbol;
257 Ppmd7_Update2(p); 257 Ppmd7_Update2(p);
@@ -261,7 +261,7 @@ int Ppmd7z_DecodeSymbol(CPpmd7 *p)
261 if (count >= freqSum) 261 if (count >= freqSum)
262 return PPMD7_SYM_ERROR; 262 return PPMD7_SYM_ERROR;
263 263
264 RC_Decode(hiCnt, freqSum - hiCnt); 264 RC_Decode(hiCnt, freqSum - hiCnt)
265 265
266 // We increase (see->Summ) for sum of Freqs of all non_Masked symbols. 266 // We increase (see->Summ) for sum of Freqs of all non_Masked symbols.
267 // new (see->Summ) value can overflow over 16-bits in some rare cases 267 // new (see->Summ) value can overflow over 16-bits in some rare cases
@@ -295,3 +295,18 @@ Byte *Ppmd7z_DecodeSymbols(CPpmd7 *p, Byte *buf, const Byte *lim)
295 return buf; 295 return buf;
296} 296}
297*/ 297*/
298
299#undef kTopValue
300#undef READ_BYTE
301#undef RC_NORM_BASE
302#undef RC_NORM_1
303#undef RC_NORM
304#undef RC_NORM_LOCAL
305#undef RC_NORM_REMOTE
306#undef R
307#undef RC_Decode
308#undef RC_DecodeFinal
309#undef RC_GetThreshold
310#undef CTX
311#undef SUCCESSOR
312#undef MASK
diff --git a/C/Ppmd7Enc.c b/C/Ppmd7Enc.c
index 62139c5..41106ba 100644
--- a/C/Ppmd7Enc.c
+++ b/C/Ppmd7Enc.c
@@ -1,5 +1,5 @@
1/* Ppmd7Enc.c -- Ppmd7z (PPMdH with 7z Range Coder) Encoder 1/* Ppmd7Enc.c -- Ppmd7z (PPMdH with 7z Range Coder) Encoder
22021-04-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on: 3This code is based on:
4 PPMd var.H (2001): Dmitry Shkarin : Public domain */ 4 PPMd var.H (2001): Dmitry Shkarin : Public domain */
5 5
@@ -8,7 +8,7 @@ This code is based on:
8 8
9#include "Ppmd7.h" 9#include "Ppmd7.h"
10 10
11#define kTopValue (1 << 24) 11#define kTopValue ((UInt32)1 << 24)
12 12
13#define R (&p->rc.enc) 13#define R (&p->rc.enc)
14 14
@@ -20,8 +20,8 @@ void Ppmd7z_Init_RangeEnc(CPpmd7 *p)
20 R->CacheSize = 1; 20 R->CacheSize = 1;
21} 21}
22 22
23MY_NO_INLINE 23Z7_NO_INLINE
24static void RangeEnc_ShiftLow(CPpmd7 *p) 24static void Ppmd7z_RangeEnc_ShiftLow(CPpmd7 *p)
25{ 25{
26 if ((UInt32)R->Low < (UInt32)0xFF000000 || (unsigned)(R->Low >> 32) != 0) 26 if ((UInt32)R->Low < (UInt32)0xFF000000 || (unsigned)(R->Low >> 32) != 0)
27 { 27 {
@@ -38,53 +38,53 @@ static void RangeEnc_ShiftLow(CPpmd7 *p)
38 R->Low = (UInt32)((UInt32)R->Low << 8); 38 R->Low = (UInt32)((UInt32)R->Low << 8);
39} 39}
40 40
41#define RC_NORM_BASE(p) if (R->Range < kTopValue) { R->Range <<= 8; RangeEnc_ShiftLow(p); 41#define RC_NORM_BASE(p) if (R->Range < kTopValue) { R->Range <<= 8; Ppmd7z_RangeEnc_ShiftLow(p);
42#define RC_NORM_1(p) RC_NORM_BASE(p) } 42#define RC_NORM_1(p) RC_NORM_BASE(p) }
43#define RC_NORM(p) RC_NORM_BASE(p) RC_NORM_BASE(p) }} 43#define RC_NORM(p) RC_NORM_BASE(p) RC_NORM_BASE(p) }}
44 44
45// we must use only one type of Normalization from two: LOCAL or REMOTE 45// we must use only one type of Normalization from two: LOCAL or REMOTE
46#define RC_NORM_LOCAL(p) // RC_NORM(p) 46#define RC_NORM_LOCAL(p) // RC_NORM(p)
47#define RC_NORM_REMOTE(p) RC_NORM(p) 47#define RC_NORM_REMOTE(p) RC_NORM(p)
48 48
49/* 49/*
50#define RangeEnc_Encode(p, start, _size_) \ 50#define Ppmd7z_RangeEnc_Encode(p, start, _size_) \
51 { UInt32 size = _size_; \ 51 { UInt32 size = _size_; \
52 R->Low += start * R->Range; \ 52 R->Low += start * R->Range; \
53 R->Range *= size; \ 53 R->Range *= size; \
54 RC_NORM_LOCAL(p); } 54 RC_NORM_LOCAL(p); }
55*/ 55*/
56 56
57MY_FORCE_INLINE 57Z7_FORCE_INLINE
58// MY_NO_INLINE 58// Z7_NO_INLINE
59static void RangeEnc_Encode(CPpmd7 *p, UInt32 start, UInt32 size) 59static void Ppmd7z_RangeEnc_Encode(CPpmd7 *p, UInt32 start, UInt32 size)
60{ 60{
61 R->Low += start * R->Range; 61 R->Low += start * R->Range;
62 R->Range *= size; 62 R->Range *= size;
63 RC_NORM_LOCAL(p); 63 RC_NORM_LOCAL(p)
64} 64}
65 65
66void Ppmd7z_Flush_RangeEnc(CPpmd7 *p) 66void Ppmd7z_Flush_RangeEnc(CPpmd7 *p)
67{ 67{
68 unsigned i; 68 unsigned i;
69 for (i = 0; i < 5; i++) 69 for (i = 0; i < 5; i++)
70 RangeEnc_ShiftLow(p); 70 Ppmd7z_RangeEnc_ShiftLow(p);
71} 71}
72 72
73 73
74 74
75#define RC_Encode(start, size) RangeEnc_Encode(p, start, size); 75#define RC_Encode(start, size) Ppmd7z_RangeEnc_Encode(p, start, size);
76#define RC_EncodeFinal(start, size) RC_Encode(start, size); RC_NORM_REMOTE(p); 76#define RC_EncodeFinal(start, size) RC_Encode(start, size) RC_NORM_REMOTE(p)
77 77
78#define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref)) 78#define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref))
79#define SUFFIX(ctx) CTX((ctx)->Suffix) 79#define SUFFIX(ctx) CTX((ctx)->Suffix)
80typedef CPpmd7_Context * CTX_PTR; 80// typedef CPpmd7_Context * CTX_PTR;
81#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) 81#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p)
82 82
83void Ppmd7_UpdateModel(CPpmd7 *p); 83void Ppmd7_UpdateModel(CPpmd7 *p);
84 84
85#define MASK(sym) ((unsigned char *)charMask)[sym] 85#define MASK(sym) ((unsigned char *)charMask)[sym]
86 86
87MY_FORCE_INLINE 87Z7_FORCE_INLINE
88static 88static
89void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol) 89void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol)
90{ 90{
@@ -104,7 +104,7 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol)
104 if (s->Symbol == symbol) 104 if (s->Symbol == symbol)
105 { 105 {
106 // R->Range /= p->MinContext->Union2.SummFreq; 106 // R->Range /= p->MinContext->Union2.SummFreq;
107 RC_EncodeFinal(0, s->Freq); 107 RC_EncodeFinal(0, s->Freq)
108 p->FoundState = s; 108 p->FoundState = s;
109 Ppmd7_Update1_0(p); 109 Ppmd7_Update1_0(p);
110 return; 110 return;
@@ -117,7 +117,7 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol)
117 if ((++s)->Symbol == symbol) 117 if ((++s)->Symbol == symbol)
118 { 118 {
119 // R->Range /= p->MinContext->Union2.SummFreq; 119 // R->Range /= p->MinContext->Union2.SummFreq;
120 RC_EncodeFinal(sum, s->Freq); 120 RC_EncodeFinal(sum, s->Freq)
121 p->FoundState = s; 121 p->FoundState = s;
122 Ppmd7_Update1(p); 122 Ppmd7_Update1(p);
123 return; 123 return;
@@ -127,10 +127,10 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol)
127 while (--i); 127 while (--i);
128 128
129 // R->Range /= p->MinContext->Union2.SummFreq; 129 // R->Range /= p->MinContext->Union2.SummFreq;
130 RC_Encode(sum, p->MinContext->Union2.SummFreq - sum); 130 RC_Encode(sum, p->MinContext->Union2.SummFreq - sum)
131 131
132 p->HiBitsFlag = PPMD7_HiBitsFlag_3(p->FoundState->Symbol); 132 p->HiBitsFlag = PPMD7_HiBitsFlag_3(p->FoundState->Symbol);
133 PPMD_SetAllBitsIn256Bytes(charMask); 133 PPMD_SetAllBitsIn256Bytes(charMask)
134 // MASK(s->Symbol) = 0; 134 // MASK(s->Symbol) = 0;
135 // i = p->MinContext->NumStats - 1; 135 // i = p->MinContext->NumStats - 1;
136 // do { MASK((--s)->Symbol) = 0; } while (--i); 136 // do { MASK((--s)->Symbol) = 0; } while (--i);
@@ -153,20 +153,20 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol)
153 UInt16 *prob = Ppmd7_GetBinSumm(p); 153 UInt16 *prob = Ppmd7_GetBinSumm(p);
154 CPpmd_State *s = Ppmd7Context_OneState(p->MinContext); 154 CPpmd_State *s = Ppmd7Context_OneState(p->MinContext);
155 UInt32 pr = *prob; 155 UInt32 pr = *prob;
156 UInt32 bound = (R->Range >> 14) * pr; 156 const UInt32 bound = (R->Range >> 14) * pr;
157 pr = PPMD_UPDATE_PROB_1(pr); 157 pr = PPMD_UPDATE_PROB_1(pr);
158 if (s->Symbol == symbol) 158 if (s->Symbol == symbol)
159 { 159 {
160 *prob = (UInt16)(pr + (1 << PPMD_INT_BITS)); 160 *prob = (UInt16)(pr + (1 << PPMD_INT_BITS));
161 // RangeEnc_EncodeBit_0(p, bound); 161 // RangeEnc_EncodeBit_0(p, bound);
162 R->Range = bound; 162 R->Range = bound;
163 RC_NORM_1(p); 163 RC_NORM_1(p)
164 164
165 // p->FoundState = s; 165 // p->FoundState = s;
166 // Ppmd7_UpdateBin(p); 166 // Ppmd7_UpdateBin(p);
167 { 167 {
168 unsigned freq = s->Freq; 168 const unsigned freq = s->Freq;
169 CTX_PTR c = CTX(SUCCESSOR(s)); 169 CPpmd7_Context *c = CTX(SUCCESSOR(s));
170 p->FoundState = s; 170 p->FoundState = s;
171 p->PrevSuccess = 1; 171 p->PrevSuccess = 1;
172 p->RunLength++; 172 p->RunLength++;
@@ -187,7 +187,7 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol)
187 R->Range -= bound; 187 R->Range -= bound;
188 RC_NORM_LOCAL(p) 188 RC_NORM_LOCAL(p)
189 189
190 PPMD_SetAllBitsIn256Bytes(charMask); 190 PPMD_SetAllBitsIn256Bytes(charMask)
191 MASK(s->Symbol) = 0; 191 MASK(s->Symbol) = 0;
192 p->PrevSuccess = 0; 192 p->PrevSuccess = 0;
193 } 193 }
@@ -248,14 +248,14 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol)
248 248
249 do 249 do
250 { 250 {
251 unsigned cur = s->Symbol; 251 const unsigned cur = s->Symbol;
252 if ((int)cur == symbol) 252 if ((int)cur == symbol)
253 { 253 {
254 UInt32 low = sum; 254 const UInt32 low = sum;
255 UInt32 freq = s->Freq; 255 const UInt32 freq = s->Freq;
256 unsigned num2; 256 unsigned num2;
257 257
258 Ppmd_See_Update(see); 258 Ppmd_See_UPDATE(see)
259 p->FoundState = s; 259 p->FoundState = s;
260 sum += escFreq; 260 sum += escFreq;
261 261
@@ -279,7 +279,7 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol)
279 279
280 280
281 R->Range /= sum; 281 R->Range /= sum;
282 RC_EncodeFinal(low, freq); 282 RC_EncodeFinal(low, freq)
283 Ppmd7_Update2(p); 283 Ppmd7_Update2(p);
284 return; 284 return;
285 } 285 }
@@ -289,21 +289,21 @@ void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol)
289 while (--i); 289 while (--i);
290 290
291 { 291 {
292 UInt32 total = sum + escFreq; 292 const UInt32 total = sum + escFreq;
293 see->Summ = (UInt16)(see->Summ + total); 293 see->Summ = (UInt16)(see->Summ + total);
294 294
295 R->Range /= total; 295 R->Range /= total;
296 RC_Encode(sum, escFreq); 296 RC_Encode(sum, escFreq)
297 } 297 }
298 298
299 { 299 {
300 CPpmd_State *s2 = Ppmd7_GetStats(p, p->MinContext); 300 const CPpmd_State *s2 = Ppmd7_GetStats(p, p->MinContext);
301 s--; 301 s--;
302 MASK(s->Symbol) = 0; 302 MASK(s->Symbol) = 0;
303 do 303 do
304 { 304 {
305 unsigned sym0 = s2[0].Symbol; 305 const unsigned sym0 = s2[0].Symbol;
306 unsigned sym1 = s2[1].Symbol; 306 const unsigned sym1 = s2[1].Symbol;
307 s2 += 2; 307 s2 += 2;
308 MASK(sym0) = 0; 308 MASK(sym0) = 0;
309 MASK(sym1) = 0; 309 MASK(sym1) = 0;
@@ -321,3 +321,18 @@ void Ppmd7z_EncodeSymbols(CPpmd7 *p, const Byte *buf, const Byte *lim)
321 Ppmd7z_EncodeSymbol(p, *buf); 321 Ppmd7z_EncodeSymbol(p, *buf);
322 } 322 }
323} 323}
324
325#undef kTopValue
326#undef WRITE_BYTE
327#undef RC_NORM_BASE
328#undef RC_NORM_1
329#undef RC_NORM
330#undef RC_NORM_LOCAL
331#undef RC_NORM_REMOTE
332#undef R
333#undef RC_Encode
334#undef RC_EncodeFinal
335#undef SUFFIX
336#undef CTX
337#undef SUCCESSOR
338#undef MASK
diff --git a/C/Ppmd7aDec.c b/C/Ppmd7aDec.c
index c424578..55e164e 100644
--- a/C/Ppmd7aDec.c
+++ b/C/Ppmd7aDec.c
@@ -1,5 +1,5 @@
1/* Ppmd7aDec.c -- PPMd7a (PPMdH) Decoder 1/* Ppmd7aDec.c -- PPMd7a (PPMdH) Decoder
22021-04-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on: 3This code is based on:
4 PPMd var.H (2001): Dmitry Shkarin : Public domain 4 PPMd var.H (2001): Dmitry Shkarin : Public domain
5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */ 5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */
@@ -8,8 +8,8 @@ This code is based on:
8 8
9#include "Ppmd7.h" 9#include "Ppmd7.h"
10 10
11#define kTop (1 << 24) 11#define kTop ((UInt32)1 << 24)
12#define kBot (1 << 15) 12#define kBot ((UInt32)1 << 15)
13 13
14#define READ_BYTE(p) IByteIn_Read((p)->Stream) 14#define READ_BYTE(p) IByteIn_Read((p)->Stream)
15 15
@@ -37,9 +37,9 @@ BoolInt Ppmd7a_RangeDec_Init(CPpmd7_RangeDec *p)
37 37
38#define R (&p->rc.dec) 38#define R (&p->rc.dec)
39 39
40MY_FORCE_INLINE 40Z7_FORCE_INLINE
41// MY_NO_INLINE 41// Z7_NO_INLINE
42static void RangeDec_Decode(CPpmd7 *p, UInt32 start, UInt32 size) 42static void Ppmd7a_RD_Decode(CPpmd7 *p, UInt32 start, UInt32 size)
43{ 43{
44 start *= R->Range; 44 start *= R->Range;
45 R->Low += start; 45 R->Low += start;
@@ -48,9 +48,9 @@ static void RangeDec_Decode(CPpmd7 *p, UInt32 start, UInt32 size)
48 RC_NORM_LOCAL(R) 48 RC_NORM_LOCAL(R)
49} 49}
50 50
51#define RC_Decode(start, size) RangeDec_Decode(p, start, size); 51#define RC_Decode(start, size) Ppmd7a_RD_Decode(p, start, size);
52#define RC_DecodeFinal(start, size) RC_Decode(start, size) RC_NORM_REMOTE(R) 52#define RC_DecodeFinal(start, size) RC_Decode(start, size) RC_NORM_REMOTE(R)
53#define RC_GetThreshold(total) (R->Code / (R->Range /= (total))) 53#define RC_GetThreshold(total) (R->Code / (R->Range /= (total)))
54 54
55 55
56#define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref)) 56#define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref))
@@ -70,7 +70,7 @@ int Ppmd7a_DecodeSymbol(CPpmd7 *p)
70 CPpmd_State *s = Ppmd7_GetStats(p, p->MinContext); 70 CPpmd_State *s = Ppmd7_GetStats(p, p->MinContext);
71 unsigned i; 71 unsigned i;
72 UInt32 count, hiCnt; 72 UInt32 count, hiCnt;
73 UInt32 summFreq = p->MinContext->Union2.SummFreq; 73 const UInt32 summFreq = p->MinContext->Union2.SummFreq;
74 74
75 if (summFreq > R->Range) 75 if (summFreq > R->Range)
76 return PPMD7_SYM_ERROR; 76 return PPMD7_SYM_ERROR;
@@ -81,7 +81,7 @@ int Ppmd7a_DecodeSymbol(CPpmd7 *p)
81 if ((Int32)(count -= s->Freq) < 0) 81 if ((Int32)(count -= s->Freq) < 0)
82 { 82 {
83 Byte sym; 83 Byte sym;
84 RC_DecodeFinal(0, s->Freq); 84 RC_DecodeFinal(0, s->Freq)
85 p->FoundState = s; 85 p->FoundState = s;
86 sym = s->Symbol; 86 sym = s->Symbol;
87 Ppmd7_Update1_0(p); 87 Ppmd7_Update1_0(p);
@@ -96,7 +96,7 @@ int Ppmd7a_DecodeSymbol(CPpmd7 *p)
96 if ((Int32)(count -= (++s)->Freq) < 0) 96 if ((Int32)(count -= (++s)->Freq) < 0)
97 { 97 {
98 Byte sym; 98 Byte sym;
99 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq); 99 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq)
100 p->FoundState = s; 100 p->FoundState = s;
101 sym = s->Symbol; 101 sym = s->Symbol;
102 Ppmd7_Update1(p); 102 Ppmd7_Update1(p);
@@ -109,10 +109,10 @@ int Ppmd7a_DecodeSymbol(CPpmd7 *p)
109 return PPMD7_SYM_ERROR; 109 return PPMD7_SYM_ERROR;
110 110
111 hiCnt -= count; 111 hiCnt -= count;
112 RC_Decode(hiCnt, summFreq - hiCnt); 112 RC_Decode(hiCnt, summFreq - hiCnt)
113 113
114 p->HiBitsFlag = PPMD7_HiBitsFlag_3(p->FoundState->Symbol); 114 p->HiBitsFlag = PPMD7_HiBitsFlag_3(p->FoundState->Symbol);
115 PPMD_SetAllBitsIn256Bytes(charMask); 115 PPMD_SetAllBitsIn256Bytes(charMask)
116 // i = p->MinContext->NumStats - 1; 116 // i = p->MinContext->NumStats - 1;
117 // do { MASK((--s)->Symbol) = 0; } while (--i); 117 // do { MASK((--s)->Symbol) = 0; } while (--i);
118 { 118 {
@@ -176,7 +176,7 @@ int Ppmd7a_DecodeSymbol(CPpmd7 *p)
176 R->Range = (R->Range & ~((UInt32)PPMD_BIN_SCALE - 1)) - size0; 176 R->Range = (R->Range & ~((UInt32)PPMD_BIN_SCALE - 1)) - size0;
177 RC_NORM_LOCAL(R) 177 RC_NORM_LOCAL(R)
178 178
179 PPMD_SetAllBitsIn256Bytes(charMask); 179 PPMD_SetAllBitsIn256Bytes(charMask)
180 MASK(Ppmd7Context_OneState(p->MinContext)->Symbol) = 0; 180 MASK(Ppmd7Context_OneState(p->MinContext)->Symbol) = 0;
181 p->PrevSuccess = 0; 181 p->PrevSuccess = 0;
182 } 182 }
@@ -245,13 +245,13 @@ int Ppmd7a_DecodeSymbol(CPpmd7 *p)
245 { 245 {
246 count -= s->Freq & (unsigned)(MASK((s)->Symbol)); s++; if ((Int32)count < 0) break; 246 count -= s->Freq & (unsigned)(MASK((s)->Symbol)); s++; if ((Int32)count < 0) break;
247 // count -= s->Freq & (unsigned)(MASK((s)->Symbol)); s++; if ((Int32)count < 0) break; 247 // count -= s->Freq & (unsigned)(MASK((s)->Symbol)); s++; if ((Int32)count < 0) break;
248 }; 248 }
249 } 249 }
250 s--; 250 s--;
251 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq); 251 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq)
252 252
253 // new (see->Summ) value can overflow over 16-bits in some rare cases 253 // new (see->Summ) value can overflow over 16-bits in some rare cases
254 Ppmd_See_Update(see); 254 Ppmd_See_UPDATE(see)
255 p->FoundState = s; 255 p->FoundState = s;
256 sym = s->Symbol; 256 sym = s->Symbol;
257 Ppmd7_Update2(p); 257 Ppmd7_Update2(p);
@@ -261,7 +261,7 @@ int Ppmd7a_DecodeSymbol(CPpmd7 *p)
261 if (count >= freqSum) 261 if (count >= freqSum)
262 return PPMD7_SYM_ERROR; 262 return PPMD7_SYM_ERROR;
263 263
264 RC_Decode(hiCnt, freqSum - hiCnt); 264 RC_Decode(hiCnt, freqSum - hiCnt)
265 265
266 // We increase (see->Summ) for sum of Freqs of all non_Masked symbols. 266 // We increase (see->Summ) for sum of Freqs of all non_Masked symbols.
267 // new (see->Summ) value can overflow over 16-bits in some rare cases 267 // new (see->Summ) value can overflow over 16-bits in some rare cases
@@ -277,3 +277,19 @@ int Ppmd7a_DecodeSymbol(CPpmd7 *p)
277 while (s != s2); 277 while (s != s2);
278 } 278 }
279} 279}
280
281#undef kTop
282#undef kBot
283#undef READ_BYTE
284#undef RC_NORM_BASE
285#undef RC_NORM_1
286#undef RC_NORM
287#undef RC_NORM_LOCAL
288#undef RC_NORM_REMOTE
289#undef R
290#undef RC_Decode
291#undef RC_DecodeFinal
292#undef RC_GetThreshold
293#undef CTX
294#undef SUCCESSOR
295#undef MASK
diff --git a/C/Ppmd8.c b/C/Ppmd8.c
index fda8b88..28abf27 100644
--- a/C/Ppmd8.c
+++ b/C/Ppmd8.c
@@ -1,5 +1,5 @@
1/* Ppmd8.c -- PPMdI codec 1/* Ppmd8.c -- PPMdI codec
22021-04-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on PPMd var.I (2002): Dmitry Shkarin : Public domain */ 3This code is based on PPMd var.I (2002): Dmitry Shkarin : Public domain */
4 4
5#include "Precomp.h" 5#include "Precomp.h"
@@ -14,7 +14,7 @@ This code is based on PPMd var.I (2002): Dmitry Shkarin : Public domain */
14MY_ALIGN(16) 14MY_ALIGN(16)
15static const Byte PPMD8_kExpEscape[16] = { 25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 }; 15static const Byte PPMD8_kExpEscape[16] = { 25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 };
16MY_ALIGN(16) 16MY_ALIGN(16)
17static const UInt16 kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051}; 17static const UInt16 PPMD8_kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051};
18 18
19#define MAX_FREQ 124 19#define MAX_FREQ 124
20#define UNIT_SIZE 12 20#define UNIT_SIZE 12
@@ -33,7 +33,7 @@ static const UInt16 kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x
33#define ONE_STATE(ctx) Ppmd8Context_OneState(ctx) 33#define ONE_STATE(ctx) Ppmd8Context_OneState(ctx)
34#define SUFFIX(ctx) CTX((ctx)->Suffix) 34#define SUFFIX(ctx) CTX((ctx)->Suffix)
35 35
36typedef CPpmd8_Context * CTX_PTR; 36typedef CPpmd8_Context * PPMD8_CTX_PTR;
37 37
38struct CPpmd8_Node_; 38struct CPpmd8_Node_;
39 39
@@ -114,7 +114,7 @@ BoolInt Ppmd8_Alloc(CPpmd8 *p, UInt32 size, ISzAllocPtr alloc)
114#define EMPTY_NODE 0xFFFFFFFF 114#define EMPTY_NODE 0xFFFFFFFF
115 115
116 116
117static void InsertNode(CPpmd8 *p, void *node, unsigned indx) 117static void Ppmd8_InsertNode(CPpmd8 *p, void *node, unsigned indx)
118{ 118{
119 ((CPpmd8_Node *)node)->Stamp = EMPTY_NODE; 119 ((CPpmd8_Node *)node)->Stamp = EMPTY_NODE;
120 ((CPpmd8_Node *)node)->Next = (CPpmd8_Node_Ref)p->FreeList[indx]; 120 ((CPpmd8_Node *)node)->Next = (CPpmd8_Node_Ref)p->FreeList[indx];
@@ -124,7 +124,7 @@ static void InsertNode(CPpmd8 *p, void *node, unsigned indx)
124} 124}
125 125
126 126
127static void *RemoveNode(CPpmd8 *p, unsigned indx) 127static void *Ppmd8_RemoveNode(CPpmd8 *p, unsigned indx)
128{ 128{
129 CPpmd8_Node *node = NODE((CPpmd8_Node_Ref)p->FreeList[indx]); 129 CPpmd8_Node *node = NODE((CPpmd8_Node_Ref)p->FreeList[indx]);
130 p->FreeList[indx] = node->Next; 130 p->FreeList[indx] = node->Next;
@@ -134,16 +134,16 @@ static void *RemoveNode(CPpmd8 *p, unsigned indx)
134} 134}
135 135
136 136
137static void SplitBlock(CPpmd8 *p, void *ptr, unsigned oldIndx, unsigned newIndx) 137static void Ppmd8_SplitBlock(CPpmd8 *p, void *ptr, unsigned oldIndx, unsigned newIndx)
138{ 138{
139 unsigned i, nu = I2U(oldIndx) - I2U(newIndx); 139 unsigned i, nu = I2U(oldIndx) - I2U(newIndx);
140 ptr = (Byte *)ptr + U2B(I2U(newIndx)); 140 ptr = (Byte *)ptr + U2B(I2U(newIndx));
141 if (I2U(i = U2I(nu)) != nu) 141 if (I2U(i = U2I(nu)) != nu)
142 { 142 {
143 unsigned k = I2U(--i); 143 unsigned k = I2U(--i);
144 InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1); 144 Ppmd8_InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1);
145 } 145 }
146 InsertNode(p, ptr, i); 146 Ppmd8_InsertNode(p, ptr, i);
147} 147}
148 148
149 149
@@ -159,7 +159,7 @@ static void SplitBlock(CPpmd8 *p, void *ptr, unsigned oldIndx, unsigned newIndx)
159 159
160 160
161 161
162static void GlueFreeBlocks(CPpmd8 *p) 162static void Ppmd8_GlueFreeBlocks(CPpmd8 *p)
163{ 163{
164 /* 164 /*
165 we use first UInt32 field of 12-bytes UNITs as record type stamp 165 we use first UInt32 field of 12-bytes UNITs as record type stamp
@@ -239,27 +239,27 @@ static void GlueFreeBlocks(CPpmd8 *p)
239 if (nu == 0) 239 if (nu == 0)
240 continue; 240 continue;
241 for (; nu > 128; nu -= 128, node += 128) 241 for (; nu > 128; nu -= 128, node += 128)
242 InsertNode(p, node, PPMD_NUM_INDEXES - 1); 242 Ppmd8_InsertNode(p, node, PPMD_NUM_INDEXES - 1);
243 if (I2U(i = U2I(nu)) != nu) 243 if (I2U(i = U2I(nu)) != nu)
244 { 244 {
245 unsigned k = I2U(--i); 245 unsigned k = I2U(--i);
246 InsertNode(p, node + k, (unsigned)nu - k - 1); 246 Ppmd8_InsertNode(p, node + k, (unsigned)nu - k - 1);
247 } 247 }
248 InsertNode(p, node, i); 248 Ppmd8_InsertNode(p, node, i);
249 } 249 }
250} 250}
251 251
252 252
253MY_NO_INLINE 253Z7_NO_INLINE
254static void *AllocUnitsRare(CPpmd8 *p, unsigned indx) 254static void *Ppmd8_AllocUnitsRare(CPpmd8 *p, unsigned indx)
255{ 255{
256 unsigned i; 256 unsigned i;
257 257
258 if (p->GlueCount == 0) 258 if (p->GlueCount == 0)
259 { 259 {
260 GlueFreeBlocks(p); 260 Ppmd8_GlueFreeBlocks(p);
261 if (p->FreeList[indx] != 0) 261 if (p->FreeList[indx] != 0)
262 return RemoveNode(p, indx); 262 return Ppmd8_RemoveNode(p, indx);
263 } 263 }
264 264
265 i = indx; 265 i = indx;
@@ -277,17 +277,17 @@ static void *AllocUnitsRare(CPpmd8 *p, unsigned indx)
277 while (p->FreeList[i] == 0); 277 while (p->FreeList[i] == 0);
278 278
279 { 279 {
280 void *block = RemoveNode(p, i); 280 void *block = Ppmd8_RemoveNode(p, i);
281 SplitBlock(p, block, i, indx); 281 Ppmd8_SplitBlock(p, block, i, indx);
282 return block; 282 return block;
283 } 283 }
284} 284}
285 285
286 286
287static void *AllocUnits(CPpmd8 *p, unsigned indx) 287static void *Ppmd8_AllocUnits(CPpmd8 *p, unsigned indx)
288{ 288{
289 if (p->FreeList[indx] != 0) 289 if (p->FreeList[indx] != 0)
290 return RemoveNode(p, indx); 290 return Ppmd8_RemoveNode(p, indx);
291 { 291 {
292 UInt32 numBytes = U2B(I2U(indx)); 292 UInt32 numBytes = U2B(I2U(indx));
293 Byte *lo = p->LoUnit; 293 Byte *lo = p->LoUnit;
@@ -297,11 +297,11 @@ static void *AllocUnits(CPpmd8 *p, unsigned indx)
297 return lo; 297 return lo;
298 } 298 }
299 } 299 }
300 return AllocUnitsRare(p, indx); 300 return Ppmd8_AllocUnitsRare(p, indx);
301} 301}
302 302
303 303
304#define MyMem12Cpy(dest, src, num) \ 304#define MEM_12_CPY(dest, src, num) \
305 { UInt32 *d = (UInt32 *)dest; const UInt32 *z = (const UInt32 *)src; UInt32 n = num; \ 305 { UInt32 *d = (UInt32 *)dest; const UInt32 *z = (const UInt32 *)src; UInt32 n = num; \
306 do { d[0] = z[0]; d[1] = z[1]; d[2] = z[2]; z += 3; d += 3; } while (--n); } 306 do { d[0] = z[0]; d[1] = z[1]; d[2] = z[2]; z += 3; d += 3; } while (--n); }
307 307
@@ -315,26 +315,26 @@ static void *ShrinkUnits(CPpmd8 *p, void *oldPtr, unsigned oldNU, unsigned newNU
315 return oldPtr; 315 return oldPtr;
316 if (p->FreeList[i1] != 0) 316 if (p->FreeList[i1] != 0)
317 { 317 {
318 void *ptr = RemoveNode(p, i1); 318 void *ptr = Ppmd8_RemoveNode(p, i1);
319 MyMem12Cpy(ptr, oldPtr, newNU); 319 MEM_12_CPY(ptr, oldPtr, newNU)
320 InsertNode(p, oldPtr, i0); 320 Ppmd8_InsertNode(p, oldPtr, i0);
321 return ptr; 321 return ptr;
322 } 322 }
323 SplitBlock(p, oldPtr, i0, i1); 323 Ppmd8_SplitBlock(p, oldPtr, i0, i1);
324 return oldPtr; 324 return oldPtr;
325} 325}
326 326
327 327
328static void FreeUnits(CPpmd8 *p, void *ptr, unsigned nu) 328static void FreeUnits(CPpmd8 *p, void *ptr, unsigned nu)
329{ 329{
330 InsertNode(p, ptr, U2I(nu)); 330 Ppmd8_InsertNode(p, ptr, U2I(nu));
331} 331}
332 332
333 333
334static void SpecialFreeUnit(CPpmd8 *p, void *ptr) 334static void SpecialFreeUnit(CPpmd8 *p, void *ptr)
335{ 335{
336 if ((Byte *)ptr != p->UnitsStart) 336 if ((Byte *)ptr != p->UnitsStart)
337 InsertNode(p, ptr, 0); 337 Ppmd8_InsertNode(p, ptr, 0);
338 else 338 else
339 { 339 {
340 #ifdef PPMD8_FREEZE_SUPPORT 340 #ifdef PPMD8_FREEZE_SUPPORT
@@ -352,10 +352,10 @@ static void *MoveUnitsUp(CPpmd8 *p, void *oldPtr, unsigned nu)
352 void *ptr; 352 void *ptr;
353 if ((Byte *)oldPtr > p->UnitsStart + (1 << 14) || REF(oldPtr) > p->FreeList[indx]) 353 if ((Byte *)oldPtr > p->UnitsStart + (1 << 14) || REF(oldPtr) > p->FreeList[indx])
354 return oldPtr; 354 return oldPtr;
355 ptr = RemoveNode(p, indx); 355 ptr = Ppmd8_RemoveNode(p, indx);
356 MyMem12Cpy(ptr, oldPtr, nu); 356 MEM_12_CPY(ptr, oldPtr, nu)
357 if ((Byte *)oldPtr != p->UnitsStart) 357 if ((Byte *)oldPtr != p->UnitsStart)
358 InsertNode(p, oldPtr, indx); 358 Ppmd8_InsertNode(p, oldPtr, indx);
359 else 359 else
360 p->UnitsStart += U2B(I2U(indx)); 360 p->UnitsStart += U2B(I2U(indx));
361 return ptr; 361 return ptr;
@@ -411,22 +411,22 @@ static void ExpandTextArea(CPpmd8 *p)
411 411
412 412
413#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) 413#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p)
414static void SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v) 414static void Ppmd8State_SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v)
415{ 415{
416 Ppmd_SET_SUCCESSOR(p, v); 416 Ppmd_SET_SUCCESSOR(p, v)
417} 417}
418 418
419#define RESET_TEXT(offs) { p->Text = p->Base + p->AlignOffset + (offs); } 419#define RESET_TEXT(offs) { p->Text = p->Base + p->AlignOffset + (offs); }
420 420
421MY_NO_INLINE 421Z7_NO_INLINE
422static 422static
423void RestartModel(CPpmd8 *p) 423void Ppmd8_RestartModel(CPpmd8 *p)
424{ 424{
425 unsigned i, k, m; 425 unsigned i, k, m;
426 426
427 memset(p->FreeList, 0, sizeof(p->FreeList)); 427 memset(p->FreeList, 0, sizeof(p->FreeList));
428 memset(p->Stamps, 0, sizeof(p->Stamps)); 428 memset(p->Stamps, 0, sizeof(p->Stamps));
429 RESET_TEXT(0); 429 RESET_TEXT(0)
430 p->HiUnit = p->Text + p->Size; 430 p->HiUnit = p->Text + p->Size;
431 p->LoUnit = p->UnitsStart = p->HiUnit - p->Size / 8 / UNIT_SIZE * 7 * UNIT_SIZE; 431 p->LoUnit = p->UnitsStart = p->HiUnit - p->Size / 8 / UNIT_SIZE * 7 * UNIT_SIZE;
432 p->GlueCount = 0; 432 p->GlueCount = 0;
@@ -436,8 +436,8 @@ void RestartModel(CPpmd8 *p)
436 p->PrevSuccess = 0; 436 p->PrevSuccess = 0;
437 437
438 { 438 {
439 CPpmd8_Context *mc = (CTX_PTR)(void *)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */ 439 CPpmd8_Context *mc = (PPMD8_CTX_PTR)(void *)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */
440 CPpmd_State *s = (CPpmd_State *)p->LoUnit; /* AllocUnits(p, PPMD_NUM_INDEXES - 1); */ 440 CPpmd_State *s = (CPpmd_State *)p->LoUnit; /* Ppmd8_AllocUnits(p, PPMD_NUM_INDEXES - 1); */
441 441
442 p->LoUnit += U2B(256 / 2); 442 p->LoUnit += U2B(256 / 2);
443 p->MaxContext = p->MinContext = mc; 443 p->MaxContext = p->MinContext = mc;
@@ -452,7 +452,7 @@ void RestartModel(CPpmd8 *p)
452 { 452 {
453 s->Symbol = (Byte)i; 453 s->Symbol = (Byte)i;
454 s->Freq = 1; 454 s->Freq = 1;
455 SetSuccessor(s, 0); 455 Ppmd8State_SetSuccessor(s, 0);
456 } 456 }
457 } 457 }
458 458
@@ -475,7 +475,7 @@ void RestartModel(CPpmd8 *p)
475 { 475 {
476 unsigned r; 476 unsigned r;
477 UInt16 *dest = p->BinSumm[m] + k; 477 UInt16 *dest = p->BinSumm[m] + k;
478 UInt16 val = (UInt16)(PPMD_BIN_SCALE - kInitBinEsc[k] / (i + 1)); 478 const UInt16 val = (UInt16)(PPMD_BIN_SCALE - PPMD8_kInitBinEsc[k] / (i + 1));
479 for (r = 0; r < 64; r += 8) 479 for (r = 0; r < 64; r += 8)
480 dest[r] = val; 480 dest[r] = val;
481 } 481 }
@@ -507,7 +507,7 @@ void Ppmd8_Init(CPpmd8 *p, unsigned maxOrder, unsigned restoreMethod)
507{ 507{
508 p->MaxOrder = maxOrder; 508 p->MaxOrder = maxOrder;
509 p->RestoreMethod = restoreMethod; 509 p->RestoreMethod = restoreMethod;
510 RestartModel(p); 510 Ppmd8_RestartModel(p);
511} 511}
512 512
513 513
@@ -531,7 +531,7 @@ Refresh() is called when we remove some symbols (successors) in context.
531It increases Escape_Freq for sum of all removed symbols. 531It increases Escape_Freq for sum of all removed symbols.
532*/ 532*/
533 533
534static void Refresh(CPpmd8 *p, CTX_PTR ctx, unsigned oldNU, unsigned scale) 534static void Refresh(CPpmd8 *p, PPMD8_CTX_PTR ctx, unsigned oldNU, unsigned scale)
535{ 535{
536 unsigned i = ctx->NumStats, escFreq, sumFreq, flags; 536 unsigned i = ctx->NumStats, escFreq, sumFreq, flags;
537 CPpmd_State *s = (CPpmd_State *)ShrinkUnits(p, STATS(ctx), oldNU, (i + 2) >> 1); 537 CPpmd_State *s = (CPpmd_State *)ShrinkUnits(p, STATS(ctx), oldNU, (i + 2) >> 1);
@@ -581,7 +581,7 @@ static void Refresh(CPpmd8 *p, CTX_PTR ctx, unsigned oldNU, unsigned scale)
581} 581}
582 582
583 583
584static void SwapStates(CPpmd_State *t1, CPpmd_State *t2) 584static void SWAP_STATES(CPpmd_State *t1, CPpmd_State *t2)
585{ 585{
586 CPpmd_State tmp = *t1; 586 CPpmd_State tmp = *t1;
587 *t1 = *t2; 587 *t1 = *t2;
@@ -597,7 +597,7 @@ CutOff() reduces contexts:
597 if the (Union4.Stats) is close to (UnitsStart), it moves it up. 597 if the (Union4.Stats) is close to (UnitsStart), it moves it up.
598*/ 598*/
599 599
600static CPpmd_Void_Ref CutOff(CPpmd8 *p, CTX_PTR ctx, unsigned order) 600static CPpmd_Void_Ref CutOff(CPpmd8 *p, PPMD8_CTX_PTR ctx, unsigned order)
601{ 601{
602 int ns = ctx->NumStats; 602 int ns = ctx->NumStats;
603 unsigned nu; 603 unsigned nu;
@@ -613,7 +613,7 @@ static CPpmd_Void_Ref CutOff(CPpmd8 *p, CTX_PTR ctx, unsigned order)
613 successor = CutOff(p, CTX(successor), order + 1); 613 successor = CutOff(p, CTX(successor), order + 1);
614 else 614 else
615 successor = 0; 615 successor = 0;
616 SetSuccessor(s, successor); 616 Ppmd8State_SetSuccessor(s, successor);
617 if (successor || order <= 9) /* O_BOUND */ 617 if (successor || order <= 9) /* O_BOUND */
618 return REF(ctx); 618 return REF(ctx);
619 } 619 }
@@ -630,11 +630,11 @@ static CPpmd_Void_Ref CutOff(CPpmd8 *p, CTX_PTR ctx, unsigned order)
630 if ((UInt32)((Byte *)stats - p->UnitsStart) <= (1 << 14) 630 if ((UInt32)((Byte *)stats - p->UnitsStart) <= (1 << 14)
631 && (CPpmd_Void_Ref)ctx->Union4.Stats <= p->FreeList[indx]) 631 && (CPpmd_Void_Ref)ctx->Union4.Stats <= p->FreeList[indx])
632 { 632 {
633 void *ptr = RemoveNode(p, indx); 633 void *ptr = Ppmd8_RemoveNode(p, indx);
634 ctx->Union4.Stats = STATS_REF(ptr); 634 ctx->Union4.Stats = STATS_REF(ptr);
635 MyMem12Cpy(ptr, (const void *)stats, nu); 635 MEM_12_CPY(ptr, (const void *)stats, nu)
636 if ((Byte *)stats != p->UnitsStart) 636 if ((Byte *)stats != p->UnitsStart)
637 InsertNode(p, stats, indx); 637 Ppmd8_InsertNode(p, stats, indx);
638 else 638 else
639 p->UnitsStart += U2B(I2U(indx)); 639 p->UnitsStart += U2B(I2U(indx));
640 stats = ptr; 640 stats = ptr;
@@ -656,16 +656,16 @@ static CPpmd_Void_Ref CutOff(CPpmd8 *p, CTX_PTR ctx, unsigned order)
656 } 656 }
657 else 657 else
658 { 658 {
659 SwapStates(s, s2); 659 SWAP_STATES(s, s2);
660 SetSuccessor(s2, 0); 660 Ppmd8State_SetSuccessor(s2, 0);
661 } 661 }
662 } 662 }
663 else 663 else
664 { 664 {
665 if (order < p->MaxOrder) 665 if (order < p->MaxOrder)
666 SetSuccessor(s, CutOff(p, CTX(successor), order + 1)); 666 Ppmd8State_SetSuccessor(s, CutOff(p, CTX(successor), order + 1));
667 else 667 else
668 SetSuccessor(s, 0); 668 Ppmd8State_SetSuccessor(s, 0);
669 } 669 }
670 } 670 }
671 while (--s >= stats); 671 while (--s >= stats);
@@ -711,7 +711,7 @@ RemoveBinContexts()
711 removes Bin Context without Successor, if suffix of that context is also binary. 711 removes Bin Context without Successor, if suffix of that context is also binary.
712*/ 712*/
713 713
714static CPpmd_Void_Ref RemoveBinContexts(CPpmd8 *p, CTX_PTR ctx, unsigned order) 714static CPpmd_Void_Ref RemoveBinContexts(CPpmd8 *p, PPMD8_CTX_PTR ctx, unsigned order)
715{ 715{
716 if (!ctx->NumStats) 716 if (!ctx->NumStats)
717 { 717 {
@@ -721,7 +721,7 @@ static CPpmd_Void_Ref RemoveBinContexts(CPpmd8 *p, CTX_PTR ctx, unsigned order)
721 successor = RemoveBinContexts(p, CTX(successor), order + 1); 721 successor = RemoveBinContexts(p, CTX(successor), order + 1);
722 else 722 else
723 successor = 0; 723 successor = 0;
724 SetSuccessor(s, successor); 724 Ppmd8State_SetSuccessor(s, successor);
725 /* Suffix context can be removed already, since different (high-order) 725 /* Suffix context can be removed already, since different (high-order)
726 Successors may refer to same context. So we check Flags == 0xFF (Stamp == EMPTY_NODE) */ 726 Successors may refer to same context. So we check Flags == 0xFF (Stamp == EMPTY_NODE) */
727 if (!successor && (!SUFFIX(ctx)->NumStats || SUFFIX(ctx)->Flags == 0xFF)) 727 if (!successor && (!SUFFIX(ctx)->NumStats || SUFFIX(ctx)->Flags == 0xFF))
@@ -737,9 +737,9 @@ static CPpmd_Void_Ref RemoveBinContexts(CPpmd8 *p, CTX_PTR ctx, unsigned order)
737 { 737 {
738 CPpmd_Void_Ref successor = SUCCESSOR(s); 738 CPpmd_Void_Ref successor = SUCCESSOR(s);
739 if ((Byte *)Ppmd8_GetPtr(p, successor) >= p->UnitsStart && order < p->MaxOrder) 739 if ((Byte *)Ppmd8_GetPtr(p, successor) >= p->UnitsStart && order < p->MaxOrder)
740 SetSuccessor(s, RemoveBinContexts(p, CTX(successor), order + 1)); 740 Ppmd8State_SetSuccessor(s, RemoveBinContexts(p, CTX(successor), order + 1));
741 else 741 else
742 SetSuccessor(s, 0); 742 Ppmd8State_SetSuccessor(s, 0);
743 } 743 }
744 while (--s >= STATS(ctx)); 744 while (--s >= STATS(ctx));
745 } 745 }
@@ -767,15 +767,15 @@ static UInt32 GetUsedMemory(const CPpmd8 *p)
767#endif 767#endif
768 768
769 769
770static void RestoreModel(CPpmd8 *p, CTX_PTR ctxError 770static void RestoreModel(CPpmd8 *p, PPMD8_CTX_PTR ctxError
771 #ifdef PPMD8_FREEZE_SUPPORT 771 #ifdef PPMD8_FREEZE_SUPPORT
772 , CTX_PTR fSuccessor 772 , PPMD8_CTX_PTR fSuccessor
773 #endif 773 #endif
774 ) 774 )
775{ 775{
776 CTX_PTR c; 776 PPMD8_CTX_PTR c;
777 CPpmd_State *s; 777 CPpmd_State *s;
778 RESET_TEXT(0); 778 RESET_TEXT(0)
779 779
780 // we go here in cases of error of allocation for context (c1) 780 // we go here in cases of error of allocation for context (c1)
781 // Order(MinContext) < Order(ctxError) <= Order(MaxContext) 781 // Order(MinContext) < Order(ctxError) <= Order(MaxContext)
@@ -831,7 +831,7 @@ static void RestoreModel(CPpmd8 *p, CTX_PTR ctxError
831 else 831 else
832 #endif 832 #endif
833 if (p->RestoreMethod == PPMD8_RESTORE_METHOD_RESTART || GetUsedMemory(p) < (p->Size >> 1)) 833 if (p->RestoreMethod == PPMD8_RESTORE_METHOD_RESTART || GetUsedMemory(p) < (p->Size >> 1))
834 RestartModel(p); 834 Ppmd8_RestartModel(p);
835 else 835 else
836 { 836 {
837 while (p->MaxContext->Suffix) 837 while (p->MaxContext->Suffix)
@@ -850,8 +850,8 @@ static void RestoreModel(CPpmd8 *p, CTX_PTR ctxError
850 850
851 851
852 852
853MY_NO_INLINE 853Z7_NO_INLINE
854static CTX_PTR CreateSuccessors(CPpmd8 *p, BoolInt skip, CPpmd_State *s1, CTX_PTR c) 854static PPMD8_CTX_PTR Ppmd8_CreateSuccessors(CPpmd8 *p, BoolInt skip, CPpmd_State *s1, PPMD8_CTX_PTR c)
855{ 855{
856 856
857 CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState); 857 CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState);
@@ -927,15 +927,15 @@ static CTX_PTR CreateSuccessors(CPpmd8 *p, BoolInt skip, CPpmd_State *s1, CTX_PT
927 927
928 do 928 do
929 { 929 {
930 CTX_PTR c1; 930 PPMD8_CTX_PTR c1;
931 /* = AllocContext(p); */ 931 /* = AllocContext(p); */
932 if (p->HiUnit != p->LoUnit) 932 if (p->HiUnit != p->LoUnit)
933 c1 = (CTX_PTR)(void *)(p->HiUnit -= UNIT_SIZE); 933 c1 = (PPMD8_CTX_PTR)(void *)(p->HiUnit -= UNIT_SIZE);
934 else if (p->FreeList[0] != 0) 934 else if (p->FreeList[0] != 0)
935 c1 = (CTX_PTR)RemoveNode(p, 0); 935 c1 = (PPMD8_CTX_PTR)Ppmd8_RemoveNode(p, 0);
936 else 936 else
937 { 937 {
938 c1 = (CTX_PTR)AllocUnitsRare(p, 0); 938 c1 = (PPMD8_CTX_PTR)Ppmd8_AllocUnitsRare(p, 0);
939 if (!c1) 939 if (!c1)
940 return NULL; 940 return NULL;
941 } 941 }
@@ -943,9 +943,9 @@ static CTX_PTR CreateSuccessors(CPpmd8 *p, BoolInt skip, CPpmd_State *s1, CTX_PT
943 c1->NumStats = 0; 943 c1->NumStats = 0;
944 c1->Union2.State2.Symbol = newSym; 944 c1->Union2.State2.Symbol = newSym;
945 c1->Union2.State2.Freq = newFreq; 945 c1->Union2.State2.Freq = newFreq;
946 SetSuccessor(ONE_STATE(c1), upBranch); 946 Ppmd8State_SetSuccessor(ONE_STATE(c1), upBranch);
947 c1->Suffix = REF(c); 947 c1->Suffix = REF(c);
948 SetSuccessor(ps[--numPs], REF(c1)); 948 Ppmd8State_SetSuccessor(ps[--numPs], REF(c1));
949 c = c1; 949 c = c1;
950 } 950 }
951 while (numPs != 0); 951 while (numPs != 0);
@@ -954,10 +954,10 @@ static CTX_PTR CreateSuccessors(CPpmd8 *p, BoolInt skip, CPpmd_State *s1, CTX_PT
954} 954}
955 955
956 956
957static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c) 957static PPMD8_CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, PPMD8_CTX_PTR c)
958{ 958{
959 CPpmd_State *s = NULL; 959 CPpmd_State *s = NULL;
960 CTX_PTR c1 = c; 960 PPMD8_CTX_PTR c1 = c;
961 CPpmd_Void_Ref upBranch = REF(p->Text); 961 CPpmd_Void_Ref upBranch = REF(p->Text);
962 962
963 #ifdef PPMD8_FREEZE_SUPPORT 963 #ifdef PPMD8_FREEZE_SUPPORT
@@ -967,7 +967,7 @@ static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c)
967 ps[numPs++] = p->FoundState; 967 ps[numPs++] = p->FoundState;
968 #endif 968 #endif
969 969
970 SetSuccessor(p->FoundState, upBranch); 970 Ppmd8State_SetSuccessor(p->FoundState, upBranch);
971 p->OrderFall++; 971 p->OrderFall++;
972 972
973 for (;;) 973 for (;;)
@@ -985,8 +985,8 @@ static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c)
985 #ifdef PPMD8_FREEZE_SUPPORT 985 #ifdef PPMD8_FREEZE_SUPPORT
986 if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE) 986 if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
987 { 987 {
988 do { SetSuccessor(ps[--numPs], REF(c)); } while (numPs); 988 do { Ppmd8State_SetSuccessor(ps[--numPs], REF(c)); } while (numPs);
989 RESET_TEXT(1); 989 RESET_TEXT(1)
990 p->OrderFall = 1; 990 p->OrderFall = 1;
991 } 991 }
992 #endif 992 #endif
@@ -1014,7 +1014,7 @@ static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c)
1014 #ifdef PPMD8_FREEZE_SUPPORT 1014 #ifdef PPMD8_FREEZE_SUPPORT
1015 ps[numPs++] = s; 1015 ps[numPs++] = s;
1016 #endif 1016 #endif
1017 SetSuccessor(s, upBranch); 1017 Ppmd8State_SetSuccessor(s, upBranch);
1018 p->OrderFall++; 1018 p->OrderFall++;
1019 } 1019 }
1020 1020
@@ -1022,8 +1022,8 @@ static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c)
1022 if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE) 1022 if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
1023 { 1023 {
1024 c = CTX(SUCCESSOR(s)); 1024 c = CTX(SUCCESSOR(s));
1025 do { SetSuccessor(ps[--numPs], REF(c)); } while (numPs); 1025 do { Ppmd8State_SetSuccessor(ps[--numPs], REF(c)); } while (numPs);
1026 RESET_TEXT(1); 1026 RESET_TEXT(1)
1027 p->OrderFall = 1; 1027 p->OrderFall = 1;
1028 return c; 1028 return c;
1029 } 1029 }
@@ -1031,15 +1031,15 @@ static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c)
1031 #endif 1031 #endif
1032 if (SUCCESSOR(s) <= upBranch) 1032 if (SUCCESSOR(s) <= upBranch)
1033 { 1033 {
1034 CTX_PTR successor; 1034 PPMD8_CTX_PTR successor;
1035 CPpmd_State *s2 = p->FoundState; 1035 CPpmd_State *s2 = p->FoundState;
1036 p->FoundState = s; 1036 p->FoundState = s;
1037 1037
1038 successor = CreateSuccessors(p, False, NULL, c); 1038 successor = Ppmd8_CreateSuccessors(p, False, NULL, c);
1039 if (!successor) 1039 if (!successor)
1040 SetSuccessor(s, 0); 1040 Ppmd8State_SetSuccessor(s, 0);
1041 else 1041 else
1042 SetSuccessor(s, REF(successor)); 1042 Ppmd8State_SetSuccessor(s, REF(successor));
1043 p->FoundState = s2; 1043 p->FoundState = s2;
1044 } 1044 }
1045 1045
@@ -1047,7 +1047,7 @@ static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c)
1047 CPpmd_Void_Ref successor = SUCCESSOR(s); 1047 CPpmd_Void_Ref successor = SUCCESSOR(s);
1048 if (p->OrderFall == 1 && c1 == p->MaxContext) 1048 if (p->OrderFall == 1 && c1 == p->MaxContext)
1049 { 1049 {
1050 SetSuccessor(p->FoundState, successor); 1050 Ppmd8State_SetSuccessor(p->FoundState, successor);
1051 p->Text--; 1051 p->Text--;
1052 } 1052 }
1053 if (successor == 0) 1053 if (successor == 0)
@@ -1059,11 +1059,11 @@ static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c)
1059 1059
1060 1060
1061void Ppmd8_UpdateModel(CPpmd8 *p); 1061void Ppmd8_UpdateModel(CPpmd8 *p);
1062MY_NO_INLINE 1062Z7_NO_INLINE
1063void Ppmd8_UpdateModel(CPpmd8 *p) 1063void Ppmd8_UpdateModel(CPpmd8 *p)
1064{ 1064{
1065 CPpmd_Void_Ref maxSuccessor, minSuccessor = SUCCESSOR(p->FoundState); 1065 CPpmd_Void_Ref maxSuccessor, minSuccessor = SUCCESSOR(p->FoundState);
1066 CTX_PTR c; 1066 PPMD8_CTX_PTR c;
1067 unsigned s0, ns, fFreq = p->FoundState->Freq; 1067 unsigned s0, ns, fFreq = p->FoundState->Freq;
1068 Byte flag, fSymbol = p->FoundState->Symbol; 1068 Byte flag, fSymbol = p->FoundState->Symbol;
1069 { 1069 {
@@ -1096,7 +1096,7 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1096 1096
1097 if (s[0].Freq >= s[-1].Freq) 1097 if (s[0].Freq >= s[-1].Freq)
1098 { 1098 {
1099 SwapStates(&s[0], &s[-1]); 1099 SWAP_STATES(&s[0], &s[-1]);
1100 s--; 1100 s--;
1101 } 1101 }
1102 } 1102 }
@@ -1112,14 +1112,14 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1112 c = p->MaxContext; 1112 c = p->MaxContext;
1113 if (p->OrderFall == 0 && minSuccessor) 1113 if (p->OrderFall == 0 && minSuccessor)
1114 { 1114 {
1115 CTX_PTR cs = CreateSuccessors(p, True, s, p->MinContext); 1115 PPMD8_CTX_PTR cs = Ppmd8_CreateSuccessors(p, True, s, p->MinContext);
1116 if (!cs) 1116 if (!cs)
1117 { 1117 {
1118 SetSuccessor(p->FoundState, 0); 1118 Ppmd8State_SetSuccessor(p->FoundState, 0);
1119 RESTORE_MODEL(c, CTX(minSuccessor)); 1119 RESTORE_MODEL(c, CTX(minSuccessor));
1120 return; 1120 return;
1121 } 1121 }
1122 SetSuccessor(p->FoundState, REF(cs)); 1122 Ppmd8State_SetSuccessor(p->FoundState, REF(cs));
1123 p->MinContext = p->MaxContext = cs; 1123 p->MinContext = p->MaxContext = cs;
1124 return; 1124 return;
1125 } 1125 }
@@ -1141,7 +1141,7 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1141 1141
1142 if (!minSuccessor) 1142 if (!minSuccessor)
1143 { 1143 {
1144 CTX_PTR cs = ReduceOrder(p, s, p->MinContext); 1144 PPMD8_CTX_PTR cs = ReduceOrder(p, s, p->MinContext);
1145 if (!cs) 1145 if (!cs)
1146 { 1146 {
1147 RESTORE_MODEL(c, NULL); 1147 RESTORE_MODEL(c, NULL);
@@ -1151,7 +1151,7 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1151 } 1151 }
1152 else if ((Byte *)Ppmd8_GetPtr(p, minSuccessor) < p->UnitsStart) 1152 else if ((Byte *)Ppmd8_GetPtr(p, minSuccessor) < p->UnitsStart)
1153 { 1153 {
1154 CTX_PTR cs = CreateSuccessors(p, False, s, p->MinContext); 1154 PPMD8_CTX_PTR cs = Ppmd8_CreateSuccessors(p, False, s, p->MinContext);
1155 if (!cs) 1155 if (!cs)
1156 { 1156 {
1157 RESTORE_MODEL(c, NULL); 1157 RESTORE_MODEL(c, NULL);
@@ -1169,7 +1169,7 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1169 else if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE) 1169 else if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
1170 { 1170 {
1171 maxSuccessor = minSuccessor; 1171 maxSuccessor = minSuccessor;
1172 RESET_TEXT(0); 1172 RESET_TEXT(0)
1173 p->OrderFall = 0; 1173 p->OrderFall = 0;
1174 } 1174 }
1175 #endif 1175 #endif
@@ -1219,7 +1219,7 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1219 unsigned i = U2I(oldNU); 1219 unsigned i = U2I(oldNU);
1220 if (i != U2I((size_t)oldNU + 1)) 1220 if (i != U2I((size_t)oldNU + 1))
1221 { 1221 {
1222 void *ptr = AllocUnits(p, i + 1); 1222 void *ptr = Ppmd8_AllocUnits(p, i + 1);
1223 void *oldPtr; 1223 void *oldPtr;
1224 if (!ptr) 1224 if (!ptr)
1225 { 1225 {
@@ -1227,8 +1227,8 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1227 return; 1227 return;
1228 } 1228 }
1229 oldPtr = STATS(c); 1229 oldPtr = STATS(c);
1230 MyMem12Cpy(ptr, oldPtr, oldNU); 1230 MEM_12_CPY(ptr, oldPtr, oldNU)
1231 InsertNode(p, oldPtr, i); 1231 Ppmd8_InsertNode(p, oldPtr, i);
1232 c->Union4.Stats = STATS_REF(ptr); 1232 c->Union4.Stats = STATS_REF(ptr);
1233 } 1233 }
1234 } 1234 }
@@ -1243,7 +1243,7 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1243 else 1243 else
1244 { 1244 {
1245 1245
1246 CPpmd_State *s = (CPpmd_State*)AllocUnits(p, 0); 1246 CPpmd_State *s = (CPpmd_State*)Ppmd8_AllocUnits(p, 0);
1247 if (!s) 1247 if (!s)
1248 { 1248 {
1249 RESTORE_MODEL(c, CTX(minSuccessor)); 1249 RESTORE_MODEL(c, CTX(minSuccessor));
@@ -1255,7 +1255,7 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1255 s->Symbol = c->Union2.State2.Symbol; 1255 s->Symbol = c->Union2.State2.Symbol;
1256 s->Successor_0 = c->Union4.State4.Successor_0; 1256 s->Successor_0 = c->Union4.State4.Successor_0;
1257 s->Successor_1 = c->Union4.State4.Successor_1; 1257 s->Successor_1 = c->Union4.State4.Successor_1;
1258 // SetSuccessor(s, c->Union4.Stats); // call it only for debug purposes to check the order of 1258 // Ppmd8State_SetSuccessor(s, c->Union4.Stats); // call it only for debug purposes to check the order of
1259 // (Successor_0 and Successor_1) in LE/BE. 1259 // (Successor_0 and Successor_1) in LE/BE.
1260 c->Union4.Stats = REF(s); 1260 c->Union4.Stats = REF(s);
1261 if (freq < MAX_FREQ / 4 - 1) 1261 if (freq < MAX_FREQ / 4 - 1)
@@ -1275,7 +1275,7 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1275 UInt32 sf = (UInt32)s0 + sum; 1275 UInt32 sf = (UInt32)s0 + sum;
1276 s->Symbol = fSymbol; 1276 s->Symbol = fSymbol;
1277 c->NumStats = (Byte)(ns1 + 1); 1277 c->NumStats = (Byte)(ns1 + 1);
1278 SetSuccessor(s, maxSuccessor); 1278 Ppmd8State_SetSuccessor(s, maxSuccessor);
1279 c->Flags |= flag; 1279 c->Flags |= flag;
1280 if (cf < 6 * sf) 1280 if (cf < 6 * sf)
1281 { 1281 {
@@ -1299,8 +1299,8 @@ void Ppmd8_UpdateModel(CPpmd8 *p)
1299 1299
1300 1300
1301 1301
1302MY_NO_INLINE 1302Z7_NO_INLINE
1303static void Rescale(CPpmd8 *p) 1303static void Ppmd8_Rescale(CPpmd8 *p)
1304{ 1304{
1305 unsigned i, adder, sumFreq, escFreq; 1305 unsigned i, adder, sumFreq, escFreq;
1306 CPpmd_State *stats = STATS(p->MinContext); 1306 CPpmd_State *stats = STATS(p->MinContext);
@@ -1389,7 +1389,7 @@ static void Rescale(CPpmd8 *p)
1389 *s = *stats; 1389 *s = *stats;
1390 s->Freq = (Byte)freq; 1390 s->Freq = (Byte)freq;
1391 p->FoundState = s; 1391 p->FoundState = s;
1392 InsertNode(p, stats, U2I(n0)); 1392 Ppmd8_InsertNode(p, stats, U2I(n0));
1393 return; 1393 return;
1394 } 1394 }
1395 1395
@@ -1452,9 +1452,9 @@ CPpmd_See *Ppmd8_MakeEscFreq(CPpmd8 *p, unsigned numMasked1, UInt32 *escFreq)
1452} 1452}
1453 1453
1454 1454
1455static void NextContext(CPpmd8 *p) 1455static void Ppmd8_NextContext(CPpmd8 *p)
1456{ 1456{
1457 CTX_PTR c = CTX(SUCCESSOR(p->FoundState)); 1457 PPMD8_CTX_PTR c = CTX(SUCCESSOR(p->FoundState));
1458 if (p->OrderFall == 0 && (const Byte *)c >= p->UnitsStart) 1458 if (p->OrderFall == 0 && (const Byte *)c >= p->UnitsStart)
1459 p->MaxContext = p->MinContext = c; 1459 p->MaxContext = p->MinContext = c;
1460 else 1460 else
@@ -1471,12 +1471,12 @@ void Ppmd8_Update1(CPpmd8 *p)
1471 s->Freq = (Byte)freq; 1471 s->Freq = (Byte)freq;
1472 if (freq > s[-1].Freq) 1472 if (freq > s[-1].Freq)
1473 { 1473 {
1474 SwapStates(s, &s[-1]); 1474 SWAP_STATES(s, &s[-1]);
1475 p->FoundState = --s; 1475 p->FoundState = --s;
1476 if (freq > MAX_FREQ) 1476 if (freq > MAX_FREQ)
1477 Rescale(p); 1477 Ppmd8_Rescale(p);
1478 } 1478 }
1479 NextContext(p); 1479 Ppmd8_NextContext(p);
1480} 1480}
1481 1481
1482 1482
@@ -1492,8 +1492,8 @@ void Ppmd8_Update1_0(CPpmd8 *p)
1492 freq += 4; 1492 freq += 4;
1493 s->Freq = (Byte)freq; 1493 s->Freq = (Byte)freq;
1494 if (freq > MAX_FREQ) 1494 if (freq > MAX_FREQ)
1495 Rescale(p); 1495 Ppmd8_Rescale(p);
1496 NextContext(p); 1496 Ppmd8_NextContext(p);
1497} 1497}
1498 1498
1499 1499
@@ -1504,7 +1504,7 @@ void Ppmd8_UpdateBin(CPpmd8 *p)
1504 p->FoundState->Freq = (Byte)(freq + (freq < 196)); // Ppmd8 (196) 1504 p->FoundState->Freq = (Byte)(freq + (freq < 196)); // Ppmd8 (196)
1505 p->PrevSuccess = 1; 1505 p->PrevSuccess = 1;
1506 p->RunLength++; 1506 p->RunLength++;
1507 NextContext(p); 1507 Ppmd8_NextContext(p);
1508} 1508}
1509*/ 1509*/
1510 1510
@@ -1517,7 +1517,7 @@ void Ppmd8_Update2(CPpmd8 *p)
1517 p->MinContext->Union2.SummFreq = (UInt16)(p->MinContext->Union2.SummFreq + 4); 1517 p->MinContext->Union2.SummFreq = (UInt16)(p->MinContext->Union2.SummFreq + 4);
1518 s->Freq = (Byte)freq; 1518 s->Freq = (Byte)freq;
1519 if (freq > MAX_FREQ) 1519 if (freq > MAX_FREQ)
1520 Rescale(p); 1520 Ppmd8_Rescale(p);
1521 Ppmd8_UpdateModel(p); 1521 Ppmd8_UpdateModel(p);
1522} 1522}
1523 1523
@@ -1526,7 +1526,7 @@ void Ppmd8_Update2(CPpmd8 *p)
1526 GlueCount, and Glue method 1526 GlueCount, and Glue method
1527 BinSum 1527 BinSum
1528 See / EscFreq 1528 See / EscFreq
1529 CreateSuccessors updates more suffix contexts 1529 Ppmd8_CreateSuccessors updates more suffix contexts
1530 Ppmd8_UpdateModel consts. 1530 Ppmd8_UpdateModel consts.
1531 PrevSuccess Update 1531 PrevSuccess Update
1532 1532
@@ -1535,3 +1535,31 @@ Flags:
1535 (1 << 3) - there is symbol in Stats with (sym >= 0x40) in 1535 (1 << 3) - there is symbol in Stats with (sym >= 0x40) in
1536 (1 << 4) - main symbol of context is (sym >= 0x40) 1536 (1 << 4) - main symbol of context is (sym >= 0x40)
1537*/ 1537*/
1538
1539#undef RESET_TEXT
1540#undef FLAG_RESCALED
1541#undef FLAG_PREV_HIGH
1542#undef HiBits_Prepare
1543#undef HiBits_Convert_3
1544#undef HiBits_Convert_4
1545#undef PPMD8_HiBitsFlag_3
1546#undef PPMD8_HiBitsFlag_4
1547#undef RESTORE_MODEL
1548
1549#undef MAX_FREQ
1550#undef UNIT_SIZE
1551#undef U2B
1552#undef U2I
1553#undef I2U
1554
1555#undef REF
1556#undef STATS_REF
1557#undef CTX
1558#undef STATS
1559#undef ONE_STATE
1560#undef SUFFIX
1561#undef NODE
1562#undef EMPTY_NODE
1563#undef MEM_12_CPY
1564#undef SUCCESSOR
1565#undef SWAP_STATES
diff --git a/C/Ppmd8.h b/C/Ppmd8.h
index fe93fe7..d5bb57e 100644
--- a/C/Ppmd8.h
+++ b/C/Ppmd8.h
@@ -1,11 +1,11 @@
1/* Ppmd8.h -- Ppmd8 (PPMdI) compression codec 1/* Ppmd8.h -- Ppmd8 (PPMdI) compression codec
22021-04-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on: 3This code is based on:
4 PPMd var.I (2002): Dmitry Shkarin : Public domain 4 PPMd var.I (2002): Dmitry Shkarin : Public domain
5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */ 5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */
6 6
7#ifndef __PPMD8_H 7#ifndef ZIP7_INC_PPMD8_H
8#define __PPMD8_H 8#define ZIP7_INC_PPMD8_H
9 9
10#include "Ppmd.h" 10#include "Ppmd.h"
11 11
@@ -87,8 +87,8 @@ typedef struct
87 UInt32 Low; 87 UInt32 Low;
88 union 88 union
89 { 89 {
90 IByteIn *In; 90 IByteInPtr In;
91 IByteOut *Out; 91 IByteOutPtr Out;
92 } Stream; 92 } Stream;
93 93
94 Byte Indx2Units[PPMD_NUM_INDEXES + 2]; // +2 for alignment 94 Byte Indx2Units[PPMD_NUM_INDEXES + 2]; // +2 for alignment
diff --git a/C/Ppmd8Dec.c b/C/Ppmd8Dec.c
index d205de2..72d3626 100644
--- a/C/Ppmd8Dec.c
+++ b/C/Ppmd8Dec.c
@@ -1,5 +1,5 @@
1/* Ppmd8Dec.c -- Ppmd8 (PPMdI) Decoder 1/* Ppmd8Dec.c -- Ppmd8 (PPMdI) Decoder
22021-04-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on: 3This code is based on:
4 PPMd var.I (2002): Dmitry Shkarin : Public domain 4 PPMd var.I (2002): Dmitry Shkarin : Public domain
5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */ 5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */
@@ -8,8 +8,8 @@ This code is based on:
8 8
9#include "Ppmd8.h" 9#include "Ppmd8.h"
10 10
11#define kTop (1 << 24) 11#define kTop ((UInt32)1 << 24)
12#define kBot (1 << 15) 12#define kBot ((UInt32)1 << 15)
13 13
14#define READ_BYTE(p) IByteIn_Read((p)->Stream.In) 14#define READ_BYTE(p) IByteIn_Read((p)->Stream.In)
15 15
@@ -37,9 +37,9 @@ BoolInt Ppmd8_Init_RangeDec(CPpmd8 *p)
37 37
38#define R p 38#define R p
39 39
40MY_FORCE_INLINE 40Z7_FORCE_INLINE
41// MY_NO_INLINE 41// Z7_NO_INLINE
42static void RangeDec_Decode(CPpmd8 *p, UInt32 start, UInt32 size) 42static void Ppmd8_RD_Decode(CPpmd8 *p, UInt32 start, UInt32 size)
43{ 43{
44 start *= R->Range; 44 start *= R->Range;
45 R->Low += start; 45 R->Low += start;
@@ -48,13 +48,13 @@ static void RangeDec_Decode(CPpmd8 *p, UInt32 start, UInt32 size)
48 RC_NORM_LOCAL(R) 48 RC_NORM_LOCAL(R)
49} 49}
50 50
51#define RC_Decode(start, size) RangeDec_Decode(p, start, size); 51#define RC_Decode(start, size) Ppmd8_RD_Decode(p, start, size);
52#define RC_DecodeFinal(start, size) RC_Decode(start, size) RC_NORM_REMOTE(R) 52#define RC_DecodeFinal(start, size) RC_Decode(start, size) RC_NORM_REMOTE(R)
53#define RC_GetThreshold(total) (R->Code / (R->Range /= (total))) 53#define RC_GetThreshold(total) (R->Code / (R->Range /= (total)))
54 54
55 55
56#define CTX(ref) ((CPpmd8_Context *)Ppmd8_GetContext(p, ref)) 56#define CTX(ref) ((CPpmd8_Context *)Ppmd8_GetContext(p, ref))
57typedef CPpmd8_Context * CTX_PTR; 57// typedef CPpmd8_Context * CTX_PTR;
58#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) 58#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p)
59void Ppmd8_UpdateModel(CPpmd8 *p); 59void Ppmd8_UpdateModel(CPpmd8 *p);
60 60
@@ -81,7 +81,7 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
81 if ((Int32)(count -= s->Freq) < 0) 81 if ((Int32)(count -= s->Freq) < 0)
82 { 82 {
83 Byte sym; 83 Byte sym;
84 RC_DecodeFinal(0, s->Freq); 84 RC_DecodeFinal(0, s->Freq)
85 p->FoundState = s; 85 p->FoundState = s;
86 sym = s->Symbol; 86 sym = s->Symbol;
87 Ppmd8_Update1_0(p); 87 Ppmd8_Update1_0(p);
@@ -96,7 +96,7 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
96 if ((Int32)(count -= (++s)->Freq) < 0) 96 if ((Int32)(count -= (++s)->Freq) < 0)
97 { 97 {
98 Byte sym; 98 Byte sym;
99 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq); 99 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq)
100 p->FoundState = s; 100 p->FoundState = s;
101 sym = s->Symbol; 101 sym = s->Symbol;
102 Ppmd8_Update1(p); 102 Ppmd8_Update1(p);
@@ -109,10 +109,10 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
109 return PPMD8_SYM_ERROR; 109 return PPMD8_SYM_ERROR;
110 110
111 hiCnt -= count; 111 hiCnt -= count;
112 RC_Decode(hiCnt, summFreq - hiCnt); 112 RC_Decode(hiCnt, summFreq - hiCnt)
113 113
114 114
115 PPMD_SetAllBitsIn256Bytes(charMask); 115 PPMD_SetAllBitsIn256Bytes(charMask)
116 // i = p->MinContext->NumStats - 1; 116 // i = p->MinContext->NumStats - 1;
117 // do { MASK((--s)->Symbol) = 0; } while (--i); 117 // do { MASK((--s)->Symbol) = 0; } while (--i);
118 { 118 {
@@ -152,7 +152,7 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
152 // Ppmd8_UpdateBin(p); 152 // Ppmd8_UpdateBin(p);
153 { 153 {
154 unsigned freq = s->Freq; 154 unsigned freq = s->Freq;
155 CTX_PTR c = CTX(SUCCESSOR(s)); 155 CPpmd8_Context *c = CTX(SUCCESSOR(s));
156 sym = s->Symbol; 156 sym = s->Symbol;
157 p->FoundState = s; 157 p->FoundState = s;
158 p->PrevSuccess = 1; 158 p->PrevSuccess = 1;
@@ -176,7 +176,7 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
176 R->Range = (R->Range & ~((UInt32)PPMD_BIN_SCALE - 1)) - size0; 176 R->Range = (R->Range & ~((UInt32)PPMD_BIN_SCALE - 1)) - size0;
177 RC_NORM_LOCAL(R) 177 RC_NORM_LOCAL(R)
178 178
179 PPMD_SetAllBitsIn256Bytes(charMask); 179 PPMD_SetAllBitsIn256Bytes(charMask)
180 MASK(Ppmd8Context_OneState(p->MinContext)->Symbol) = 0; 180 MASK(Ppmd8Context_OneState(p->MinContext)->Symbol) = 0;
181 p->PrevSuccess = 0; 181 p->PrevSuccess = 0;
182 } 182 }
@@ -227,7 +227,7 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
227 see = Ppmd8_MakeEscFreq(p, numMasked, &freqSum); 227 see = Ppmd8_MakeEscFreq(p, numMasked, &freqSum);
228 freqSum += hiCnt; 228 freqSum += hiCnt;
229 freqSum2 = freqSum; 229 freqSum2 = freqSum;
230 PPMD8_CORRECT_SUM_RANGE(R, freqSum2); 230 PPMD8_CORRECT_SUM_RANGE(R, freqSum2)
231 231
232 232
233 count = RC_GetThreshold(freqSum2); 233 count = RC_GetThreshold(freqSum2);
@@ -235,7 +235,7 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
235 if (count < hiCnt) 235 if (count < hiCnt)
236 { 236 {
237 Byte sym; 237 Byte sym;
238 // Ppmd_See_Update(see); // new (see->Summ) value can overflow over 16-bits in some rare cases 238 // Ppmd_See_UPDATE(see) // new (see->Summ) value can overflow over 16-bits in some rare cases
239 s = Ppmd8_GetStats(p, p->MinContext); 239 s = Ppmd8_GetStats(p, p->MinContext);
240 hiCnt = count; 240 hiCnt = count;
241 241
@@ -248,10 +248,10 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
248 } 248 }
249 } 249 }
250 s--; 250 s--;
251 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq); 251 RC_DecodeFinal((hiCnt - count) - s->Freq, s->Freq)
252 252
253 // new (see->Summ) value can overflow over 16-bits in some rare cases 253 // new (see->Summ) value can overflow over 16-bits in some rare cases
254 Ppmd_See_Update(see); 254 Ppmd_See_UPDATE(see)
255 p->FoundState = s; 255 p->FoundState = s;
256 sym = s->Symbol; 256 sym = s->Symbol;
257 Ppmd8_Update2(p); 257 Ppmd8_Update2(p);
@@ -261,7 +261,7 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
261 if (count >= freqSum2) 261 if (count >= freqSum2)
262 return PPMD8_SYM_ERROR; 262 return PPMD8_SYM_ERROR;
263 263
264 RC_Decode(hiCnt, freqSum2 - hiCnt); 264 RC_Decode(hiCnt, freqSum2 - hiCnt)
265 265
266 // We increase (see->Summ) for sum of Freqs of all non_Masked symbols. 266 // We increase (see->Summ) for sum of Freqs of all non_Masked symbols.
267 // new (see->Summ) value can overflow over 16-bits in some rare cases 267 // new (see->Summ) value can overflow over 16-bits in some rare cases
@@ -277,3 +277,19 @@ int Ppmd8_DecodeSymbol(CPpmd8 *p)
277 while (s != s2); 277 while (s != s2);
278 } 278 }
279} 279}
280
281#undef kTop
282#undef kBot
283#undef READ_BYTE
284#undef RC_NORM_BASE
285#undef RC_NORM_1
286#undef RC_NORM
287#undef RC_NORM_LOCAL
288#undef RC_NORM_REMOTE
289#undef R
290#undef RC_Decode
291#undef RC_DecodeFinal
292#undef RC_GetThreshold
293#undef CTX
294#undef SUCCESSOR
295#undef MASK
diff --git a/C/Ppmd8Enc.c b/C/Ppmd8Enc.c
index 32ff805..9e29ef7 100644
--- a/C/Ppmd8Enc.c
+++ b/C/Ppmd8Enc.c
@@ -1,5 +1,5 @@
1/* Ppmd8Enc.c -- Ppmd8 (PPMdI) Encoder 1/* Ppmd8Enc.c -- Ppmd8 (PPMdI) Encoder
22021-04-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on: 3This code is based on:
4 PPMd var.I (2002): Dmitry Shkarin : Public domain 4 PPMd var.I (2002): Dmitry Shkarin : Public domain
5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */ 5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */
@@ -8,8 +8,8 @@ This code is based on:
8 8
9#include "Ppmd8.h" 9#include "Ppmd8.h"
10 10
11#define kTop (1 << 24) 11#define kTop ((UInt32)1 << 24)
12#define kBot (1 << 15) 12#define kBot ((UInt32)1 << 15)
13 13
14#define WRITE_BYTE(p) IByteOut_Write(p->Stream.Out, (Byte)(p->Low >> 24)) 14#define WRITE_BYTE(p) IByteOut_Write(p->Stream.Out, (Byte)(p->Low >> 24))
15 15
@@ -54,13 +54,13 @@ void Ppmd8_Flush_RangeEnc(CPpmd8 *p)
54 54
55 55
56 56
57MY_FORCE_INLINE 57Z7_FORCE_INLINE
58// MY_NO_INLINE 58// Z7_NO_INLINE
59static void RangeEnc_Encode(CPpmd8 *p, UInt32 start, UInt32 size, UInt32 total) 59static void Ppmd8_RangeEnc_Encode(CPpmd8 *p, UInt32 start, UInt32 size, UInt32 total)
60{ 60{
61 R->Low += start * (R->Range /= total); 61 R->Low += start * (R->Range /= total);
62 R->Range *= size; 62 R->Range *= size;
63 RC_NORM_LOCAL(R); 63 RC_NORM_LOCAL(R)
64} 64}
65 65
66 66
@@ -72,19 +72,19 @@ static void RangeEnc_Encode(CPpmd8 *p, UInt32 start, UInt32 size, UInt32 total)
72 72
73 73
74 74
75#define RC_Encode(start, size, total) RangeEnc_Encode(p, start, size, total); 75#define RC_Encode(start, size, total) Ppmd8_RangeEnc_Encode(p, start, size, total);
76#define RC_EncodeFinal(start, size, total) RC_Encode(start, size, total); RC_NORM_REMOTE(p); 76#define RC_EncodeFinal(start, size, total) RC_Encode(start, size, total) RC_NORM_REMOTE(p)
77 77
78#define CTX(ref) ((CPpmd8_Context *)Ppmd8_GetContext(p, ref)) 78#define CTX(ref) ((CPpmd8_Context *)Ppmd8_GetContext(p, ref))
79 79
80typedef CPpmd8_Context * CTX_PTR; 80// typedef CPpmd8_Context * CTX_PTR;
81#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p) 81#define SUCCESSOR(p) Ppmd_GET_SUCCESSOR(p)
82 82
83void Ppmd8_UpdateModel(CPpmd8 *p); 83void Ppmd8_UpdateModel(CPpmd8 *p);
84 84
85#define MASK(sym) ((unsigned char *)charMask)[sym] 85#define MASK(sym) ((unsigned char *)charMask)[sym]
86 86
87// MY_FORCE_INLINE 87// Z7_FORCE_INLINE
88// static 88// static
89void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol) 89void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
90{ 90{
@@ -104,7 +104,7 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
104 if (s->Symbol == symbol) 104 if (s->Symbol == symbol)
105 { 105 {
106 106
107 RC_EncodeFinal(0, s->Freq, summFreq); 107 RC_EncodeFinal(0, s->Freq, summFreq)
108 p->FoundState = s; 108 p->FoundState = s;
109 Ppmd8_Update1_0(p); 109 Ppmd8_Update1_0(p);
110 return; 110 return;
@@ -117,7 +117,7 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
117 if ((++s)->Symbol == symbol) 117 if ((++s)->Symbol == symbol)
118 { 118 {
119 119
120 RC_EncodeFinal(sum, s->Freq, summFreq); 120 RC_EncodeFinal(sum, s->Freq, summFreq)
121 p->FoundState = s; 121 p->FoundState = s;
122 Ppmd8_Update1(p); 122 Ppmd8_Update1(p);
123 return; 123 return;
@@ -127,10 +127,10 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
127 while (--i); 127 while (--i);
128 128
129 129
130 RC_Encode(sum, summFreq - sum, summFreq); 130 RC_Encode(sum, summFreq - sum, summFreq)
131 131
132 132
133 PPMD_SetAllBitsIn256Bytes(charMask); 133 PPMD_SetAllBitsIn256Bytes(charMask)
134 // MASK(s->Symbol) = 0; 134 // MASK(s->Symbol) = 0;
135 // i = p->MinContext->NumStats; 135 // i = p->MinContext->NumStats;
136 // do { MASK((--s)->Symbol) = 0; } while (--i); 136 // do { MASK((--s)->Symbol) = 0; } while (--i);
@@ -153,20 +153,20 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
153 UInt16 *prob = Ppmd8_GetBinSumm(p); 153 UInt16 *prob = Ppmd8_GetBinSumm(p);
154 CPpmd_State *s = Ppmd8Context_OneState(p->MinContext); 154 CPpmd_State *s = Ppmd8Context_OneState(p->MinContext);
155 UInt32 pr = *prob; 155 UInt32 pr = *prob;
156 UInt32 bound = (R->Range >> 14) * pr; 156 const UInt32 bound = (R->Range >> 14) * pr;
157 pr = PPMD_UPDATE_PROB_1(pr); 157 pr = PPMD_UPDATE_PROB_1(pr);
158 if (s->Symbol == symbol) 158 if (s->Symbol == symbol)
159 { 159 {
160 *prob = (UInt16)(pr + (1 << PPMD_INT_BITS)); 160 *prob = (UInt16)(pr + (1 << PPMD_INT_BITS));
161 // RangeEnc_EncodeBit_0(p, bound); 161 // RangeEnc_EncodeBit_0(p, bound);
162 R->Range = bound; 162 R->Range = bound;
163 RC_NORM(R); 163 RC_NORM(R)
164 164
165 // p->FoundState = s; 165 // p->FoundState = s;
166 // Ppmd8_UpdateBin(p); 166 // Ppmd8_UpdateBin(p);
167 { 167 {
168 unsigned freq = s->Freq; 168 const unsigned freq = s->Freq;
169 CTX_PTR c = CTX(SUCCESSOR(s)); 169 CPpmd8_Context *c = CTX(SUCCESSOR(s));
170 p->FoundState = s; 170 p->FoundState = s;
171 p->PrevSuccess = 1; 171 p->PrevSuccess = 1;
172 p->RunLength++; 172 p->RunLength++;
@@ -187,7 +187,7 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
187 R->Range = (R->Range & ~((UInt32)PPMD_BIN_SCALE - 1)) - bound; 187 R->Range = (R->Range & ~((UInt32)PPMD_BIN_SCALE - 1)) - bound;
188 RC_NORM_LOCAL(R) 188 RC_NORM_LOCAL(R)
189 189
190 PPMD_SetAllBitsIn256Bytes(charMask); 190 PPMD_SetAllBitsIn256Bytes(charMask)
191 MASK(s->Symbol) = 0; 191 MASK(s->Symbol) = 0;
192 p->PrevSuccess = 0; 192 p->PrevSuccess = 0;
193 } 193 }
@@ -248,14 +248,14 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
248 248
249 do 249 do
250 { 250 {
251 unsigned cur = s->Symbol; 251 const unsigned cur = s->Symbol;
252 if ((int)cur == symbol) 252 if ((int)cur == symbol)
253 { 253 {
254 UInt32 low = sum; 254 const UInt32 low = sum;
255 UInt32 freq = s->Freq; 255 const UInt32 freq = s->Freq;
256 unsigned num2; 256 unsigned num2;
257 257
258 Ppmd_See_Update(see); 258 Ppmd_See_UPDATE(see)
259 p->FoundState = s; 259 p->FoundState = s;
260 sum += escFreq; 260 sum += escFreq;
261 261
@@ -277,9 +277,9 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
277 } 277 }
278 } 278 }
279 279
280 PPMD8_CORRECT_SUM_RANGE(p, sum); 280 PPMD8_CORRECT_SUM_RANGE(p, sum)
281 281
282 RC_EncodeFinal(low, freq, sum); 282 RC_EncodeFinal(low, freq, sum)
283 Ppmd8_Update2(p); 283 Ppmd8_Update2(p);
284 return; 284 return;
285 } 285 }
@@ -291,19 +291,19 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
291 { 291 {
292 UInt32 total = sum + escFreq; 292 UInt32 total = sum + escFreq;
293 see->Summ = (UInt16)(see->Summ + total); 293 see->Summ = (UInt16)(see->Summ + total);
294 PPMD8_CORRECT_SUM_RANGE(p, total); 294 PPMD8_CORRECT_SUM_RANGE(p, total)
295 295
296 RC_Encode(sum, total - sum, total); 296 RC_Encode(sum, total - sum, total)
297 } 297 }
298 298
299 { 299 {
300 CPpmd_State *s2 = Ppmd8_GetStats(p, p->MinContext); 300 const CPpmd_State *s2 = Ppmd8_GetStats(p, p->MinContext);
301 s--; 301 s--;
302 MASK(s->Symbol) = 0; 302 MASK(s->Symbol) = 0;
303 do 303 do
304 { 304 {
305 unsigned sym0 = s2[0].Symbol; 305 const unsigned sym0 = s2[0].Symbol;
306 unsigned sym1 = s2[1].Symbol; 306 const unsigned sym1 = s2[1].Symbol;
307 s2 += 2; 307 s2 += 2;
308 MASK(sym0) = 0; 308 MASK(sym0) = 0;
309 MASK(sym1) = 0; 309 MASK(sym1) = 0;
@@ -312,3 +312,27 @@ void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol)
312 } 312 }
313 } 313 }
314} 314}
315
316
317
318
319
320
321
322
323
324#undef kTop
325#undef kBot
326#undef WRITE_BYTE
327#undef RC_NORM_BASE
328#undef RC_NORM_1
329#undef RC_NORM
330#undef RC_NORM_LOCAL
331#undef RC_NORM_REMOTE
332#undef R
333#undef RC_Encode
334#undef RC_EncodeFinal
335
336#undef CTX
337#undef SUCCESSOR
338#undef MASK
diff --git a/C/Precomp.h b/C/Precomp.h
index e8ff8b4..69afb2f 100644
--- a/C/Precomp.h
+++ b/C/Precomp.h
@@ -1,8 +1,8 @@
1/* Precomp.h -- StdAfx 1/* Precomp.h -- StdAfx
22013-11-12 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_PRECOMP_H 4#ifndef ZIP7_INC_PRECOMP_H
5#define __7Z_PRECOMP_H 5#define ZIP7_INC_PRECOMP_H
6 6
7#include "Compiler.h" 7#include "Compiler.h"
8/* #include "7zTypes.h" */ 8/* #include "7zTypes.h" */
diff --git a/C/RotateDefs.h b/C/RotateDefs.h
index 8f01d1a..c16b4f8 100644
--- a/C/RotateDefs.h
+++ b/C/RotateDefs.h
@@ -1,14 +1,14 @@
1/* RotateDefs.h -- Rotate functions 1/* RotateDefs.h -- Rotate functions
22015-03-25 : Igor Pavlov : Public domain */ 22023-06-18 : Igor Pavlov : Public domain */
3 3
4#ifndef __ROTATE_DEFS_H 4#ifndef ZIP7_INC_ROTATE_DEFS_H
5#define __ROTATE_DEFS_H 5#define ZIP7_INC_ROTATE_DEFS_H
6 6
7#ifdef _MSC_VER 7#ifdef _MSC_VER
8 8
9#include <stdlib.h> 9#include <stdlib.h>
10 10
11/* don't use _rotl with MINGW. It can insert slow call to function. */ 11/* don't use _rotl with old MINGW. It can insert slow call to function. */
12 12
13/* #if (_MSC_VER >= 1200) */ 13/* #if (_MSC_VER >= 1200) */
14#pragma intrinsic(_rotl) 14#pragma intrinsic(_rotl)
@@ -18,12 +18,32 @@
18#define rotlFixed(x, n) _rotl((x), (n)) 18#define rotlFixed(x, n) _rotl((x), (n))
19#define rotrFixed(x, n) _rotr((x), (n)) 19#define rotrFixed(x, n) _rotr((x), (n))
20 20
21#if (_MSC_VER >= 1300)
22#define Z7_ROTL64(x, n) _rotl64((x), (n))
23#define Z7_ROTR64(x, n) _rotr64((x), (n))
24#else
25#define Z7_ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n))))
26#define Z7_ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
27#endif
28
21#else 29#else
22 30
23/* new compilers can translate these macros to fast commands. */ 31/* new compilers can translate these macros to fast commands. */
24 32
33#if defined(__clang__) && (__clang_major__ >= 4) \
34 || defined(__GNUC__) && (__GNUC__ >= 5)
35/* GCC 4.9.0 and clang 3.5 can recognize more correct version: */
36#define rotlFixed(x, n) (((x) << (n)) | ((x) >> (-(n) & 31)))
37#define rotrFixed(x, n) (((x) >> (n)) | ((x) << (-(n) & 31)))
38#define Z7_ROTL64(x, n) (((x) << (n)) | ((x) >> (-(n) & 63)))
39#define Z7_ROTR64(x, n) (((x) >> (n)) | ((x) << (-(n) & 63)))
40#else
41/* for old GCC / clang: */
25#define rotlFixed(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) 42#define rotlFixed(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
26#define rotrFixed(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) 43#define rotrFixed(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
44#define Z7_ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n))))
45#define Z7_ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
46#endif
27 47
28#endif 48#endif
29 49
diff --git a/C/Sha1.c b/C/Sha1.c
index 9665b5b..fd6c018 100644
--- a/C/Sha1.c
+++ b/C/Sha1.c
@@ -1,5 +1,5 @@
1/* Sha1.c -- SHA-1 Hash 1/* Sha1.c -- SHA-1 Hash
22021-07-13 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on public domain code of Steve Reid from Wei Dai's Crypto++ library. */ 3This code is based on public domain code of Steve Reid from Wei Dai's Crypto++ library. */
4 4
5#include "Precomp.h" 5#include "Precomp.h"
@@ -17,48 +17,48 @@ This code is based on public domain code of Steve Reid from Wei Dai's Crypto++ l
17#ifdef MY_CPU_X86_OR_AMD64 17#ifdef MY_CPU_X86_OR_AMD64
18 #ifdef _MSC_VER 18 #ifdef _MSC_VER
19 #if _MSC_VER >= 1200 19 #if _MSC_VER >= 1200
20 #define _SHA_SUPPORTED 20 #define Z7_COMPILER_SHA1_SUPPORTED
21 #endif 21 #endif
22 #elif defined(__clang__) 22 #elif defined(__clang__)
23 #if (__clang_major__ >= 8) // fix that check 23 #if (__clang_major__ >= 8) // fix that check
24 #define _SHA_SUPPORTED 24 #define Z7_COMPILER_SHA1_SUPPORTED
25 #endif 25 #endif
26 #elif defined(__GNUC__) 26 #elif defined(__GNUC__)
27 #if (__GNUC__ >= 8) // fix that check 27 #if (__GNUC__ >= 8) // fix that check
28 #define _SHA_SUPPORTED 28 #define Z7_COMPILER_SHA1_SUPPORTED
29 #endif 29 #endif
30 #elif defined(__INTEL_COMPILER) 30 #elif defined(__INTEL_COMPILER)
31 #if (__INTEL_COMPILER >= 1800) // fix that check 31 #if (__INTEL_COMPILER >= 1800) // fix that check
32 #define _SHA_SUPPORTED 32 #define Z7_COMPILER_SHA1_SUPPORTED
33 #endif 33 #endif
34 #endif 34 #endif
35#elif defined(MY_CPU_ARM_OR_ARM64) 35#elif defined(MY_CPU_ARM_OR_ARM64)
36 #ifdef _MSC_VER 36 #ifdef _MSC_VER
37 #if _MSC_VER >= 1910 && _MSC_VER >= 1929 && _MSC_FULL_VER >= 192930037 37 #if _MSC_VER >= 1910 && _MSC_VER >= 1929 && _MSC_FULL_VER >= 192930037
38 #define _SHA_SUPPORTED 38 #define Z7_COMPILER_SHA1_SUPPORTED
39 #endif 39 #endif
40 #elif defined(__clang__) 40 #elif defined(__clang__)
41 #if (__clang_major__ >= 8) // fix that check 41 #if (__clang_major__ >= 8) // fix that check
42 #define _SHA_SUPPORTED 42 #define Z7_COMPILER_SHA1_SUPPORTED
43 #endif 43 #endif
44 #elif defined(__GNUC__) 44 #elif defined(__GNUC__)
45 #if (__GNUC__ >= 6) // fix that check 45 #if (__GNUC__ >= 6) // fix that check
46 #define _SHA_SUPPORTED 46 #define Z7_COMPILER_SHA1_SUPPORTED
47 #endif 47 #endif
48 #endif 48 #endif
49#endif 49#endif
50 50
51void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks); 51void Z7_FASTCALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks);
52 52
53#ifdef _SHA_SUPPORTED 53#ifdef Z7_COMPILER_SHA1_SUPPORTED
54 void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks); 54 void Z7_FASTCALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks);
55 55
56 static SHA1_FUNC_UPDATE_BLOCKS g_FUNC_UPDATE_BLOCKS = Sha1_UpdateBlocks; 56 static SHA1_FUNC_UPDATE_BLOCKS g_SHA1_FUNC_UPDATE_BLOCKS = Sha1_UpdateBlocks;
57 static SHA1_FUNC_UPDATE_BLOCKS g_FUNC_UPDATE_BLOCKS_HW; 57 static SHA1_FUNC_UPDATE_BLOCKS g_SHA1_FUNC_UPDATE_BLOCKS_HW;
58 58
59 #define UPDATE_BLOCKS(p) p->func_UpdateBlocks 59 #define SHA1_UPDATE_BLOCKS(p) p->func_UpdateBlocks
60#else 60#else
61 #define UPDATE_BLOCKS(p) Sha1_UpdateBlocks 61 #define SHA1_UPDATE_BLOCKS(p) Sha1_UpdateBlocks
62#endif 62#endif
63 63
64 64
@@ -66,16 +66,16 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
66{ 66{
67 SHA1_FUNC_UPDATE_BLOCKS func = Sha1_UpdateBlocks; 67 SHA1_FUNC_UPDATE_BLOCKS func = Sha1_UpdateBlocks;
68 68
69 #ifdef _SHA_SUPPORTED 69 #ifdef Z7_COMPILER_SHA1_SUPPORTED
70 if (algo != SHA1_ALGO_SW) 70 if (algo != SHA1_ALGO_SW)
71 { 71 {
72 if (algo == SHA1_ALGO_DEFAULT) 72 if (algo == SHA1_ALGO_DEFAULT)
73 func = g_FUNC_UPDATE_BLOCKS; 73 func = g_SHA1_FUNC_UPDATE_BLOCKS;
74 else 74 else
75 { 75 {
76 if (algo != SHA1_ALGO_HW) 76 if (algo != SHA1_ALGO_HW)
77 return False; 77 return False;
78 func = g_FUNC_UPDATE_BLOCKS_HW; 78 func = g_SHA1_FUNC_UPDATE_BLOCKS_HW;
79 if (!func) 79 if (!func)
80 return False; 80 return False;
81 } 81 }
@@ -91,21 +91,22 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
91 91
92 92
93/* define it for speed optimization */ 93/* define it for speed optimization */
94// #define _SHA1_UNROLL 94// #define Z7_SHA1_UNROLL
95 95
96// allowed unroll steps: (1, 2, 4, 5, 20) 96// allowed unroll steps: (1, 2, 4, 5, 20)
97 97
98#ifdef _SHA1_UNROLL 98#undef Z7_SHA1_BIG_W
99#ifdef Z7_SHA1_UNROLL
99 #define STEP_PRE 20 100 #define STEP_PRE 20
100 #define STEP_MAIN 20 101 #define STEP_MAIN 20
101#else 102#else
102 #define _SHA1_BIG_W 103 #define Z7_SHA1_BIG_W
103 #define STEP_PRE 5 104 #define STEP_PRE 5
104 #define STEP_MAIN 5 105 #define STEP_MAIN 5
105#endif 106#endif
106 107
107 108
108#ifdef _SHA1_BIG_W 109#ifdef Z7_SHA1_BIG_W
109 #define kNumW 80 110 #define kNumW 80
110 #define w(i) W[i] 111 #define w(i) W[i]
111#else 112#else
@@ -150,11 +151,11 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
150*/ 151*/
151 152
152#define M5(i, fx, wx0, wx1) \ 153#define M5(i, fx, wx0, wx1) \
153 T5 ( a,b,c,d,e, fx, wx0((i) ) ); \ 154 T5 ( a,b,c,d,e, fx, wx0((i) ) ) \
154 T5 ( e,a,b,c,d, fx, wx1((i)+1) ); \ 155 T5 ( e,a,b,c,d, fx, wx1((i)+1) ) \
155 T5 ( d,e,a,b,c, fx, wx1((i)+2) ); \ 156 T5 ( d,e,a,b,c, fx, wx1((i)+2) ) \
156 T5 ( c,d,e,a,b, fx, wx1((i)+3) ); \ 157 T5 ( c,d,e,a,b, fx, wx1((i)+3) ) \
157 T5 ( b,c,d,e,a, fx, wx1((i)+4) ); \ 158 T5 ( b,c,d,e,a, fx, wx1((i)+4) ) \
158 159
159#define R5(i, fx, wx) \ 160#define R5(i, fx, wx) \
160 M5 ( i, fx, wx, wx) \ 161 M5 ( i, fx, wx, wx) \
@@ -163,17 +164,17 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
163#if STEP_PRE > 5 164#if STEP_PRE > 5
164 165
165 #define R20_START \ 166 #define R20_START \
166 R5 ( 0, f0, w0); \ 167 R5 ( 0, f0, w0) \
167 R5 ( 5, f0, w0); \ 168 R5 ( 5, f0, w0) \
168 R5 ( 10, f0, w0); \ 169 R5 ( 10, f0, w0) \
169 M5 ( 15, f0, w0, w1); \ 170 M5 ( 15, f0, w0, w1) \
170 171
171 #elif STEP_PRE == 5 172 #elif STEP_PRE == 5
172 173
173 #define R20_START \ 174 #define R20_START \
174 { size_t i; for (i = 0; i < 15; i += STEP_PRE) \ 175 { size_t i; for (i = 0; i < 15; i += STEP_PRE) \
175 { R5(i, f0, w0); } } \ 176 { R5(i, f0, w0) } } \
176 M5 ( 15, f0, w0, w1); \ 177 M5 ( 15, f0, w0, w1) \
177 178
178#else 179#else
179 180
@@ -187,8 +188,8 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
187 188
188 #define R20_START \ 189 #define R20_START \
189 { size_t i; for (i = 0; i < 16; i += STEP_PRE) \ 190 { size_t i; for (i = 0; i < 16; i += STEP_PRE) \
190 { R_PRE(i, f0, w0); } } \ 191 { R_PRE(i, f0, w0) } } \
191 R4 ( 16, f0, w1); \ 192 R4 ( 16, f0, w1) \
192 193
193#endif 194#endif
194 195
@@ -197,10 +198,10 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
197#if STEP_MAIN > 5 198#if STEP_MAIN > 5
198 199
199 #define R20(ii, fx) \ 200 #define R20(ii, fx) \
200 R5 ( (ii) , fx, w1); \ 201 R5 ( (ii) , fx, w1) \
201 R5 ( (ii) + 5 , fx, w1); \ 202 R5 ( (ii) + 5 , fx, w1) \
202 R5 ( (ii) + 10, fx, w1); \ 203 R5 ( (ii) + 10, fx, w1) \
203 R5 ( (ii) + 15, fx, w1); \ 204 R5 ( (ii) + 15, fx, w1) \
204 205
205#else 206#else
206 207
@@ -216,7 +217,7 @@ BoolInt Sha1_SetFunction(CSha1 *p, unsigned algo)
216 217
217 #define R20(ii, fx) \ 218 #define R20(ii, fx) \
218 { size_t i; for (i = (ii); i < (ii) + 20; i += STEP_MAIN) \ 219 { size_t i; for (i = (ii); i < (ii) + 20; i += STEP_MAIN) \
219 { R_MAIN(i, fx, w1); } } \ 220 { R_MAIN(i, fx, w1) } } \
220 221
221#endif 222#endif
222 223
@@ -235,8 +236,8 @@ void Sha1_InitState(CSha1 *p)
235void Sha1_Init(CSha1 *p) 236void Sha1_Init(CSha1 *p)
236{ 237{
237 p->func_UpdateBlocks = 238 p->func_UpdateBlocks =
238 #ifdef _SHA_SUPPORTED 239 #ifdef Z7_COMPILER_SHA1_SUPPORTED
239 g_FUNC_UPDATE_BLOCKS; 240 g_SHA1_FUNC_UPDATE_BLOCKS;
240 #else 241 #else
241 NULL; 242 NULL;
242 #endif 243 #endif
@@ -244,8 +245,8 @@ void Sha1_Init(CSha1 *p)
244} 245}
245 246
246 247
247MY_NO_INLINE 248Z7_NO_INLINE
248void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks) 249void Z7_FASTCALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks)
249{ 250{
250 UInt32 a, b, c, d, e; 251 UInt32 a, b, c, d, e;
251 UInt32 W[kNumW]; 252 UInt32 W[kNumW];
@@ -266,9 +267,9 @@ void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t nu
266 #endif 267 #endif
267 268
268 R20_START 269 R20_START
269 R20(20, f1); 270 R20(20, f1)
270 R20(40, f2); 271 R20(40, f2)
271 R20(60, f3); 272 R20(60, f3)
272 273
273 a += state[0]; 274 a += state[0];
274 b += state[1]; 275 b += state[1];
@@ -288,7 +289,7 @@ void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t nu
288} 289}
289 290
290 291
291#define Sha1_UpdateBlock(p) UPDATE_BLOCKS(p)(p->state, p->buffer, 1) 292#define Sha1_UpdateBlock(p) SHA1_UPDATE_BLOCKS(p)(p->state, p->buffer, 1)
292 293
293void Sha1_Update(CSha1 *p, const Byte *data, size_t size) 294void Sha1_Update(CSha1 *p, const Byte *data, size_t size)
294{ 295{
@@ -318,7 +319,7 @@ void Sha1_Update(CSha1 *p, const Byte *data, size_t size)
318 } 319 }
319 { 320 {
320 size_t numBlocks = size >> 6; 321 size_t numBlocks = size >> 6;
321 UPDATE_BLOCKS(p)(p->state, data, numBlocks); 322 SHA1_UPDATE_BLOCKS(p)(p->state, data, numBlocks);
322 size &= 0x3F; 323 size &= 0x3F;
323 if (size == 0) 324 if (size == 0)
324 return; 325 return;
@@ -361,18 +362,18 @@ void Sha1_Final(CSha1 *p, Byte *digest)
361 memset(&p->buffer[pos], 0, (64 - 8) - pos); 362 memset(&p->buffer[pos], 0, (64 - 8) - pos);
362 363
363 { 364 {
364 UInt64 numBits = (p->count << 3); 365 const UInt64 numBits = (p->count << 3);
365 SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32)); 366 SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32))
366 SetBe32(p->buffer + 64 - 4, (UInt32)(numBits)); 367 SetBe32(p->buffer + 64 - 4, (UInt32)(numBits))
367 } 368 }
368 369
369 Sha1_UpdateBlock(p); 370 Sha1_UpdateBlock(p);
370 371
371 SetBe32(digest, p->state[0]); 372 SetBe32(digest, p->state[0])
372 SetBe32(digest + 4, p->state[1]); 373 SetBe32(digest + 4, p->state[1])
373 SetBe32(digest + 8, p->state[2]); 374 SetBe32(digest + 8, p->state[2])
374 SetBe32(digest + 12, p->state[3]); 375 SetBe32(digest + 12, p->state[3])
375 SetBe32(digest + 16, p->state[4]); 376 SetBe32(digest + 16, p->state[4])
376 377
377 378
378 379
@@ -384,10 +385,10 @@ void Sha1_Final(CSha1 *p, Byte *digest)
384void Sha1_PrepareBlock(const CSha1 *p, Byte *block, unsigned size) 385void Sha1_PrepareBlock(const CSha1 *p, Byte *block, unsigned size)
385{ 386{
386 const UInt64 numBits = (p->count + size) << 3; 387 const UInt64 numBits = (p->count + size) << 3;
387 SetBe32(&((UInt32 *)(void *)block)[SHA1_NUM_BLOCK_WORDS - 2], (UInt32)(numBits >> 32)); 388 SetBe32(&((UInt32 *)(void *)block)[SHA1_NUM_BLOCK_WORDS - 2], (UInt32)(numBits >> 32))
388 SetBe32(&((UInt32 *)(void *)block)[SHA1_NUM_BLOCK_WORDS - 1], (UInt32)(numBits)); 389 SetBe32(&((UInt32 *)(void *)block)[SHA1_NUM_BLOCK_WORDS - 1], (UInt32)(numBits))
389 // SetBe32((UInt32 *)(block + size), 0x80000000); 390 // SetBe32((UInt32 *)(block + size), 0x80000000);
390 SetUi32((UInt32 *)(void *)(block + size), 0x80); 391 SetUi32((UInt32 *)(void *)(block + size), 0x80)
391 size += 4; 392 size += 4;
392 while (size != (SHA1_NUM_BLOCK_WORDS - 2) * 4) 393 while (size != (SHA1_NUM_BLOCK_WORDS - 2) * 4)
393 { 394 {
@@ -407,19 +408,19 @@ void Sha1_GetBlockDigest(const CSha1 *p, const Byte *data, Byte *destDigest)
407 st[3] = p->state[3]; 408 st[3] = p->state[3];
408 st[4] = p->state[4]; 409 st[4] = p->state[4];
409 410
410 UPDATE_BLOCKS(p)(st, data, 1); 411 SHA1_UPDATE_BLOCKS(p)(st, data, 1);
411 412
412 SetBe32(destDigest + 0 , st[0]); 413 SetBe32(destDigest + 0 , st[0])
413 SetBe32(destDigest + 1 * 4, st[1]); 414 SetBe32(destDigest + 1 * 4, st[1])
414 SetBe32(destDigest + 2 * 4, st[2]); 415 SetBe32(destDigest + 2 * 4, st[2])
415 SetBe32(destDigest + 3 * 4, st[3]); 416 SetBe32(destDigest + 3 * 4, st[3])
416 SetBe32(destDigest + 4 * 4, st[4]); 417 SetBe32(destDigest + 4 * 4, st[4])
417} 418}
418 419
419 420
420void Sha1Prepare() 421void Sha1Prepare(void)
421{ 422{
422 #ifdef _SHA_SUPPORTED 423 #ifdef Z7_COMPILER_SHA1_SUPPORTED
423 SHA1_FUNC_UPDATE_BLOCKS f, f_hw; 424 SHA1_FUNC_UPDATE_BLOCKS f, f_hw;
424 f = Sha1_UpdateBlocks; 425 f = Sha1_UpdateBlocks;
425 f_hw = NULL; 426 f_hw = NULL;
@@ -467,7 +468,31 @@ void Sha1Prepare()
467 f = f_hw = Sha1_UpdateBlocks_HW; 468 f = f_hw = Sha1_UpdateBlocks_HW;
468 } 469 }
469 } 470 }
470 g_FUNC_UPDATE_BLOCKS = f; 471 g_SHA1_FUNC_UPDATE_BLOCKS = f;
471 g_FUNC_UPDATE_BLOCKS_HW = f_hw; 472 g_SHA1_FUNC_UPDATE_BLOCKS_HW = f_hw;
472 #endif 473 #endif
473} 474}
475
476#undef kNumW
477#undef w
478#undef w0
479#undef w1
480#undef f0
481#undef f1
482#undef f2
483#undef f3
484#undef T1
485#undef T5
486#undef M5
487#undef R1
488#undef R2
489#undef R4
490#undef R5
491#undef R20_START
492#undef R_PRE
493#undef R_MAIN
494#undef STEP_PRE
495#undef STEP_MAIN
496#undef Z7_SHA1_BIG_W
497#undef Z7_SHA1_UNROLL
498#undef Z7_COMPILER_SHA1_SUPPORTED
diff --git a/C/Sha1.h b/C/Sha1.h
index 345a816..fecd9d3 100644
--- a/C/Sha1.h
+++ b/C/Sha1.h
@@ -1,8 +1,8 @@
1/* Sha1.h -- SHA-1 Hash 1/* Sha1.h -- SHA-1 Hash
22021-02-08 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_SHA1_H 4#ifndef ZIP7_INC_SHA1_H
5#define __7Z_SHA1_H 5#define ZIP7_INC_SHA1_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -14,7 +14,7 @@ EXTERN_C_BEGIN
14#define SHA1_BLOCK_SIZE (SHA1_NUM_BLOCK_WORDS * 4) 14#define SHA1_BLOCK_SIZE (SHA1_NUM_BLOCK_WORDS * 4)
15#define SHA1_DIGEST_SIZE (SHA1_NUM_DIGEST_WORDS * 4) 15#define SHA1_DIGEST_SIZE (SHA1_NUM_DIGEST_WORDS * 4)
16 16
17typedef void (MY_FAST_CALL *SHA1_FUNC_UPDATE_BLOCKS)(UInt32 state[5], const Byte *data, size_t numBlocks); 17typedef void (Z7_FASTCALL *SHA1_FUNC_UPDATE_BLOCKS)(UInt32 state[5], const Byte *data, size_t numBlocks);
18 18
19/* 19/*
20 if (the system supports different SHA1 code implementations) 20 if (the system supports different SHA1 code implementations)
@@ -34,9 +34,9 @@ typedef struct
34{ 34{
35 SHA1_FUNC_UPDATE_BLOCKS func_UpdateBlocks; 35 SHA1_FUNC_UPDATE_BLOCKS func_UpdateBlocks;
36 UInt64 count; 36 UInt64 count;
37 UInt64 __pad_2[2]; 37 UInt64 _pad_2[2];
38 UInt32 state[SHA1_NUM_DIGEST_WORDS]; 38 UInt32 state[SHA1_NUM_DIGEST_WORDS];
39 UInt32 __pad_3[3]; 39 UInt32 _pad_3[3];
40 Byte buffer[SHA1_BLOCK_SIZE]; 40 Byte buffer[SHA1_BLOCK_SIZE];
41} CSha1; 41} CSha1;
42 42
@@ -62,7 +62,7 @@ void Sha1_Final(CSha1 *p, Byte *digest);
62void Sha1_PrepareBlock(const CSha1 *p, Byte *block, unsigned size); 62void Sha1_PrepareBlock(const CSha1 *p, Byte *block, unsigned size);
63void Sha1_GetBlockDigest(const CSha1 *p, const Byte *data, Byte *destDigest); 63void Sha1_GetBlockDigest(const CSha1 *p, const Byte *data, Byte *destDigest);
64 64
65// void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks); 65// void Z7_FASTCALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks);
66 66
67/* 67/*
68call Sha1Prepare() once at program start. 68call Sha1Prepare() once at program start.
diff --git a/C/Sha1Opt.c b/C/Sha1Opt.c
index 63132da..27796aa 100644
--- a/C/Sha1Opt.c
+++ b/C/Sha1Opt.c
@@ -1,7 +1,9 @@
1/* Sha1Opt.c -- SHA-1 optimized code for SHA-1 hardware instructions 1/* Sha1Opt.c -- SHA-1 optimized code for SHA-1 hardware instructions
22021-04-01 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5#include "Compiler.h"
6#include "CpuArch.h"
5 7
6#if defined(_MSC_VER) 8#if defined(_MSC_VER)
7#if (_MSC_VER < 1900) && (_MSC_VER >= 1200) 9#if (_MSC_VER < 1900) && (_MSC_VER >= 1200)
@@ -9,41 +11,26 @@
9#endif 11#endif
10#endif 12#endif
11 13
12#include "CpuArch.h"
13
14#ifdef MY_CPU_X86_OR_AMD64 14#ifdef MY_CPU_X86_OR_AMD64
15 #if defined(__clang__) 15 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1600) // fix that check
16 #if (__clang_major__ >= 8) // fix that check
17 #define USE_HW_SHA 16 #define USE_HW_SHA
18 #ifndef __SHA__ 17 #elif defined(Z7_LLVM_CLANG_VERSION) && (Z7_LLVM_CLANG_VERSION >= 30800) \
19 #define ATTRIB_SHA __attribute__((__target__("sha,ssse3"))) 18 || defined(Z7_APPLE_CLANG_VERSION) && (Z7_APPLE_CLANG_VERSION >= 50100) \
20 #if defined(_MSC_VER) 19 || defined(Z7_GCC_VERSION) && (Z7_GCC_VERSION >= 40900)
21 // SSSE3: for clang-cl:
22 #include <tmmintrin.h>
23 #define __SHA__
24 #endif
25 #endif
26 #pragma clang diagnostic ignored "-Wvector-conversion"
27 #endif
28 #elif defined(__GNUC__)
29 #if (__GNUC__ >= 8) // fix that check
30 #define USE_HW_SHA 20 #define USE_HW_SHA
31 #ifndef __SHA__ 21 #if !defined(_INTEL_COMPILER)
22 // icc defines __GNUC__, but icc doesn't support __attribute__(__target__)
23 #if !defined(__SHA__) || !defined(__SSSE3__)
32 #define ATTRIB_SHA __attribute__((__target__("sha,ssse3"))) 24 #define ATTRIB_SHA __attribute__((__target__("sha,ssse3")))
33 // #pragma GCC target("sha,ssse3")
34 #endif 25 #endif
35 #endif 26 #endif
36 #elif defined(__INTEL_COMPILER)
37 #if (__INTEL_COMPILER >= 1800) // fix that check
38 #define USE_HW_SHA
39 #endif
40 #elif defined(_MSC_VER) 27 #elif defined(_MSC_VER)
41 #ifdef USE_MY_MM 28 #ifdef USE_MY_MM
42 #define USE_VER_MIN 1300 29 #define USE_VER_MIN 1300
43 #else 30 #else
44 #define USE_VER_MIN 1910 31 #define USE_VER_MIN 1900
45 #endif 32 #endif
46 #if _MSC_VER >= USE_VER_MIN 33 #if (_MSC_VER >= USE_VER_MIN)
47 #define USE_HW_SHA 34 #define USE_HW_SHA
48 #endif 35 #endif
49 #endif 36 #endif
@@ -52,16 +39,19 @@
52#ifdef USE_HW_SHA 39#ifdef USE_HW_SHA
53 40
54// #pragma message("Sha1 HW") 41// #pragma message("Sha1 HW")
55// #include <wmmintrin.h>
56 42
57#if !defined(_MSC_VER) || (_MSC_VER >= 1900) 43// sse/sse2/ssse3:
44#include <tmmintrin.h>
45// sha*:
58#include <immintrin.h> 46#include <immintrin.h>
59#else
60#include <emmintrin.h>
61 47
62#if defined(_MSC_VER) && (_MSC_VER >= 1600) 48#if defined (__clang__) && defined(_MSC_VER)
63// #include <intrin.h> 49 // #if !defined(__SSSE3__)
64#endif 50 // #endif
51 #if !defined(__SHA__)
52 #include <shaintrin.h>
53 #endif
54#else
65 55
66#ifdef USE_MY_MM 56#ifdef USE_MY_MM
67#include "My_mm.h" 57#include "My_mm.h"
@@ -87,37 +77,37 @@ SHA:
87 _mm_sha1* 77 _mm_sha1*
88*/ 78*/
89 79
90#define ADD_EPI32(dest, src) dest = _mm_add_epi32(dest, src); 80
91#define XOR_SI128(dest, src) dest = _mm_xor_si128(dest, src); 81#define XOR_SI128(dest, src) dest = _mm_xor_si128(dest, src);
92#define SHUFFLE_EPI8(dest, mask) dest = _mm_shuffle_epi8(dest, mask); 82#define SHUFFLE_EPI8(dest, mask) dest = _mm_shuffle_epi8(dest, mask);
93#define SHUFFLE_EPI32(dest, mask) dest = _mm_shuffle_epi32(dest, mask); 83#define SHUFFLE_EPI32(dest, mask) dest = _mm_shuffle_epi32(dest, mask);
94 84#ifdef __clang__
95#define SHA1_RND4(abcd, e0, f) abcd = _mm_sha1rnds4_epu32(abcd, e0, f); 85#define SHA1_RNDS4_RET_TYPE_CAST (__m128i)
96#define SHA1_NEXTE(e, m) e = _mm_sha1nexte_epu32(e, m); 86#else
97 87#define SHA1_RNDS4_RET_TYPE_CAST
98 88#endif
99 89#define SHA1_RND4(abcd, e0, f) abcd = SHA1_RNDS4_RET_TYPE_CAST _mm_sha1rnds4_epu32(abcd, e0, f);
100 90#define SHA1_NEXTE(e, m) e = _mm_sha1nexte_epu32(e, m);
101 91#define ADD_EPI32(dest, src) dest = _mm_add_epi32(dest, src);
102#define SHA1_MSG1(dest, src) dest = _mm_sha1msg1_epu32(dest, src); 92#define SHA1_MSG1(dest, src) dest = _mm_sha1msg1_epu32(dest, src);
103#define SHA1_MSG2(dest, src) dest = _mm_sha1msg2_epu32(dest, src); 93#define SHA1_MSG2(dest, src) dest = _mm_sha1msg2_epu32(dest, src);
104 94
105 95
106#define LOAD_SHUFFLE(m, k) \ 96#define LOAD_SHUFFLE(m, k) \
107 m = _mm_loadu_si128((const __m128i *)(const void *)(data + (k) * 16)); \ 97 m = _mm_loadu_si128((const __m128i *)(const void *)(data + (k) * 16)); \
108 SHUFFLE_EPI8(m, mask); \ 98 SHUFFLE_EPI8(m, mask) \
109 99
110#define SM1(m0, m1, m2, m3) \ 100#define SM1(m0, m1, m2, m3) \
111 SHA1_MSG1(m0, m1); \ 101 SHA1_MSG1(m0, m1) \
112 102
113#define SM2(m0, m1, m2, m3) \ 103#define SM2(m0, m1, m2, m3) \
114 XOR_SI128(m3, m1); \ 104 XOR_SI128(m3, m1) \
115 SHA1_MSG2(m3, m2); \ 105 SHA1_MSG2(m3, m2) \
116 106
117#define SM3(m0, m1, m2, m3) \ 107#define SM3(m0, m1, m2, m3) \
118 XOR_SI128(m3, m1); \ 108 XOR_SI128(m3, m1) \
119 SM1(m0, m1, m2, m3) \ 109 SM1(m0, m1, m2, m3) \
120 SHA1_MSG2(m3, m2); \ 110 SHA1_MSG2(m3, m2) \
121 111
122#define NNN(m0, m1, m2, m3) 112#define NNN(m0, m1, m2, m3)
123 113
@@ -139,9 +129,9 @@ SHA:
139 129
140#define R4(k, e0, e1, m0, m1, m2, m3, OP) \ 130#define R4(k, e0, e1, m0, m1, m2, m3, OP) \
141 e1 = abcd; \ 131 e1 = abcd; \
142 SHA1_RND4(abcd, e0, (k) / 5); \ 132 SHA1_RND4(abcd, e0, (k) / 5) \
143 SHA1_NEXTE(e1, m1); \ 133 SHA1_NEXTE(e1, m1) \
144 OP(m0, m1, m2, m3); \ 134 OP(m0, m1, m2, m3) \
145 135
146#define R16(k, mx, OP0, OP1, OP2, OP3) \ 136#define R16(k, mx, OP0, OP1, OP2, OP3) \
147 R4 ( (k)*4+0, e0,e1, m0,m1,m2,m3, OP0 ) \ 137 R4 ( (k)*4+0, e0,e1, m0,m1,m2,m3, OP0 ) \
@@ -150,18 +140,18 @@ SHA:
150 R4 ( (k)*4+3, e1,e0, m3,mx,m1,m2, OP3 ) \ 140 R4 ( (k)*4+3, e1,e0, m3,mx,m1,m2, OP3 ) \
151 141
152#define PREPARE_STATE \ 142#define PREPARE_STATE \
153 SHUFFLE_EPI32 (abcd, 0x1B); \ 143 SHUFFLE_EPI32 (abcd, 0x1B) \
154 SHUFFLE_EPI32 (e0, 0x1B); \ 144 SHUFFLE_EPI32 (e0, 0x1B) \
155 145
156 146
157 147
158 148
159 149
160void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks); 150void Z7_FASTCALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks);
161#ifdef ATTRIB_SHA 151#ifdef ATTRIB_SHA
162ATTRIB_SHA 152ATTRIB_SHA
163#endif 153#endif
164void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks) 154void Z7_FASTCALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks)
165{ 155{
166 const __m128i mask = _mm_set_epi32(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f); 156 const __m128i mask = _mm_set_epi32(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f);
167 157
@@ -190,15 +180,15 @@ void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t
190 LOAD_SHUFFLE (m2, 2) 180 LOAD_SHUFFLE (m2, 2)
191 LOAD_SHUFFLE (m3, 3) 181 LOAD_SHUFFLE (m3, 3)
192 182
193 ADD_EPI32(e0, m0); 183 ADD_EPI32(e0, m0)
194 184
195 R16 ( 0, m0, SM1, SM3, SM3, SM3 ); 185 R16 ( 0, m0, SM1, SM3, SM3, SM3 )
196 R16 ( 1, m0, SM3, SM3, SM3, SM3 ); 186 R16 ( 1, m0, SM3, SM3, SM3, SM3 )
197 R16 ( 2, m0, SM3, SM3, SM3, SM3 ); 187 R16 ( 2, m0, SM3, SM3, SM3, SM3 )
198 R16 ( 3, m0, SM3, SM3, SM3, SM3 ); 188 R16 ( 3, m0, SM3, SM3, SM3, SM3 )
199 R16 ( 4, e2, SM2, NNN, NNN, NNN ); 189 R16 ( 4, e2, SM2, NNN, NNN, NNN )
200 190
201 ADD_EPI32(abcd, abcd_save); 191 ADD_EPI32(abcd, abcd_save)
202 192
203 data += 64; 193 data += 64;
204 } 194 }
@@ -274,11 +264,11 @@ typedef uint32x4_t v128;
274#define H(e) e = vsha1h_u32(vgetq_lane_u32(abcd, 0)) 264#define H(e) e = vsha1h_u32(vgetq_lane_u32(abcd, 0))
275#define T(m, c) t = vaddq_u32(m, c) 265#define T(m, c) t = vaddq_u32(m, c)
276 266
277void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); 267void Z7_FASTCALL Sha1_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks);
278#ifdef ATTRIB_SHA 268#ifdef ATTRIB_SHA
279ATTRIB_SHA 269ATTRIB_SHA
280#endif 270#endif
281void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks) 271void Z7_FASTCALL Sha1_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks)
282{ 272{
283 v128 abcd; 273 v128 abcd;
284 v128 c0, c1, c2, c3; 274 v128 c0, c1, c2, c3;
@@ -353,12 +343,12 @@ void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t
353// #include <stdlib.h> 343// #include <stdlib.h>
354 344
355// #include "Sha1.h" 345// #include "Sha1.h"
356void MY_FAST_CALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks); 346void Z7_FASTCALL Sha1_UpdateBlocks(UInt32 state[5], const Byte *data, size_t numBlocks);
357 347
358#pragma message("Sha1 HW-SW stub was used") 348#pragma message("Sha1 HW-SW stub was used")
359 349
360void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks); 350void Z7_FASTCALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks);
361void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks) 351void Z7_FASTCALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t numBlocks)
362{ 352{
363 Sha1_UpdateBlocks(state, data, numBlocks); 353 Sha1_UpdateBlocks(state, data, numBlocks);
364 /* 354 /*
@@ -371,3 +361,26 @@ void MY_FAST_CALL Sha1_UpdateBlocks_HW(UInt32 state[5], const Byte *data, size_t
371} 361}
372 362
373#endif 363#endif
364
365#undef SU0
366#undef SU1
367#undef C
368#undef P
369#undef M
370#undef H
371#undef T
372#undef MY_rev32_for_LE
373#undef NNN
374#undef LOAD_128
375#undef STORE_128
376#undef LOAD_SHUFFLE
377#undef SM1
378#undef SM2
379#undef SM3
380#undef NNN
381#undef R4
382#undef R16
383#undef PREPARE_STATE
384#undef USE_HW_SHA
385#undef ATTRIB_SHA
386#undef USE_VER_MIN
diff --git a/C/Sha256.c b/C/Sha256.c
index 8b3983e..018cf6f 100644
--- a/C/Sha256.c
+++ b/C/Sha256.c
@@ -1,5 +1,5 @@
1/* Sha256.c -- SHA-256 Hash 1/* Sha256.c -- SHA-256 Hash
22021-04-01 : Igor Pavlov : Public domain 22023-04-02 : Igor Pavlov : Public domain
3This code is based on public domain code from Wei Dai's Crypto++ library. */ 3This code is based on public domain code from Wei Dai's Crypto++ library. */
4 4
5#include "Precomp.h" 5#include "Precomp.h"
@@ -17,48 +17,48 @@ This code is based on public domain code from Wei Dai's Crypto++ library. */
17#ifdef MY_CPU_X86_OR_AMD64 17#ifdef MY_CPU_X86_OR_AMD64
18 #ifdef _MSC_VER 18 #ifdef _MSC_VER
19 #if _MSC_VER >= 1200 19 #if _MSC_VER >= 1200
20 #define _SHA_SUPPORTED 20 #define Z7_COMPILER_SHA256_SUPPORTED
21 #endif 21 #endif
22 #elif defined(__clang__) 22 #elif defined(__clang__)
23 #if (__clang_major__ >= 8) // fix that check 23 #if (__clang_major__ >= 8) // fix that check
24 #define _SHA_SUPPORTED 24 #define Z7_COMPILER_SHA256_SUPPORTED
25 #endif 25 #endif
26 #elif defined(__GNUC__) 26 #elif defined(__GNUC__)
27 #if (__GNUC__ >= 8) // fix that check 27 #if (__GNUC__ >= 8) // fix that check
28 #define _SHA_SUPPORTED 28 #define Z7_COMPILER_SHA256_SUPPORTED
29 #endif 29 #endif
30 #elif defined(__INTEL_COMPILER) 30 #elif defined(__INTEL_COMPILER)
31 #if (__INTEL_COMPILER >= 1800) // fix that check 31 #if (__INTEL_COMPILER >= 1800) // fix that check
32 #define _SHA_SUPPORTED 32 #define Z7_COMPILER_SHA256_SUPPORTED
33 #endif 33 #endif
34 #endif 34 #endif
35#elif defined(MY_CPU_ARM_OR_ARM64) 35#elif defined(MY_CPU_ARM_OR_ARM64)
36 #ifdef _MSC_VER 36 #ifdef _MSC_VER
37 #if _MSC_VER >= 1910 37 #if _MSC_VER >= 1910
38 #define _SHA_SUPPORTED 38 #define Z7_COMPILER_SHA256_SUPPORTED
39 #endif 39 #endif
40 #elif defined(__clang__) 40 #elif defined(__clang__)
41 #if (__clang_major__ >= 8) // fix that check 41 #if (__clang_major__ >= 8) // fix that check
42 #define _SHA_SUPPORTED 42 #define Z7_COMPILER_SHA256_SUPPORTED
43 #endif 43 #endif
44 #elif defined(__GNUC__) 44 #elif defined(__GNUC__)
45 #if (__GNUC__ >= 6) // fix that check 45 #if (__GNUC__ >= 6) // fix that check
46 #define _SHA_SUPPORTED 46 #define Z7_COMPILER_SHA256_SUPPORTED
47 #endif 47 #endif
48 #endif 48 #endif
49#endif 49#endif
50 50
51void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks); 51void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks);
52 52
53#ifdef _SHA_SUPPORTED 53#ifdef Z7_COMPILER_SHA256_SUPPORTED
54 void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); 54 void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks);
55 55
56 static SHA256_FUNC_UPDATE_BLOCKS g_FUNC_UPDATE_BLOCKS = Sha256_UpdateBlocks; 56 static SHA256_FUNC_UPDATE_BLOCKS g_SHA256_FUNC_UPDATE_BLOCKS = Sha256_UpdateBlocks;
57 static SHA256_FUNC_UPDATE_BLOCKS g_FUNC_UPDATE_BLOCKS_HW; 57 static SHA256_FUNC_UPDATE_BLOCKS g_SHA256_FUNC_UPDATE_BLOCKS_HW;
58 58
59 #define UPDATE_BLOCKS(p) p->func_UpdateBlocks 59 #define SHA256_UPDATE_BLOCKS(p) p->func_UpdateBlocks
60#else 60#else
61 #define UPDATE_BLOCKS(p) Sha256_UpdateBlocks 61 #define SHA256_UPDATE_BLOCKS(p) Sha256_UpdateBlocks
62#endif 62#endif
63 63
64 64
@@ -66,16 +66,16 @@ BoolInt Sha256_SetFunction(CSha256 *p, unsigned algo)
66{ 66{
67 SHA256_FUNC_UPDATE_BLOCKS func = Sha256_UpdateBlocks; 67 SHA256_FUNC_UPDATE_BLOCKS func = Sha256_UpdateBlocks;
68 68
69 #ifdef _SHA_SUPPORTED 69 #ifdef Z7_COMPILER_SHA256_SUPPORTED
70 if (algo != SHA256_ALGO_SW) 70 if (algo != SHA256_ALGO_SW)
71 { 71 {
72 if (algo == SHA256_ALGO_DEFAULT) 72 if (algo == SHA256_ALGO_DEFAULT)
73 func = g_FUNC_UPDATE_BLOCKS; 73 func = g_SHA256_FUNC_UPDATE_BLOCKS;
74 else 74 else
75 { 75 {
76 if (algo != SHA256_ALGO_HW) 76 if (algo != SHA256_ALGO_HW)
77 return False; 77 return False;
78 func = g_FUNC_UPDATE_BLOCKS_HW; 78 func = g_SHA256_FUNC_UPDATE_BLOCKS_HW;
79 if (!func) 79 if (!func)
80 return False; 80 return False;
81 } 81 }
@@ -92,17 +92,18 @@ BoolInt Sha256_SetFunction(CSha256 *p, unsigned algo)
92 92
93/* define it for speed optimization */ 93/* define it for speed optimization */
94 94
95#ifdef _SFX 95#ifdef Z7_SFX
96 #define STEP_PRE 1 96 #define STEP_PRE 1
97 #define STEP_MAIN 1 97 #define STEP_MAIN 1
98#else 98#else
99 #define STEP_PRE 2 99 #define STEP_PRE 2
100 #define STEP_MAIN 4 100 #define STEP_MAIN 4
101 // #define _SHA256_UNROLL 101 // #define Z7_SHA256_UNROLL
102#endif 102#endif
103 103
104#undef Z7_SHA256_BIG_W
104#if STEP_MAIN != 16 105#if STEP_MAIN != 16
105 #define _SHA256_BIG_W 106 #define Z7_SHA256_BIG_W
106#endif 107#endif
107 108
108 109
@@ -124,8 +125,8 @@ void Sha256_InitState(CSha256 *p)
124void Sha256_Init(CSha256 *p) 125void Sha256_Init(CSha256 *p)
125{ 126{
126 p->func_UpdateBlocks = 127 p->func_UpdateBlocks =
127 #ifdef _SHA_SUPPORTED 128 #ifdef Z7_COMPILER_SHA256_SUPPORTED
128 g_FUNC_UPDATE_BLOCKS; 129 g_SHA256_FUNC_UPDATE_BLOCKS;
129 #else 130 #else
130 NULL; 131 NULL;
131 #endif 132 #endif
@@ -145,7 +146,7 @@ void Sha256_Init(CSha256 *p)
145 146
146#define blk2_main(j, i) s1(w(j, (i)-2)) + w(j, (i)-7) + s0(w(j, (i)-15)) 147#define blk2_main(j, i) s1(w(j, (i)-2)) + w(j, (i)-7) + s0(w(j, (i)-15))
147 148
148#ifdef _SHA256_BIG_W 149#ifdef Z7_SHA256_BIG_W
149 // we use +i instead of +(i) to change the order to solve CLANG compiler warning for signed/unsigned. 150 // we use +i instead of +(i) to change the order to solve CLANG compiler warning for signed/unsigned.
150 #define w(j, i) W[(size_t)(j) + i] 151 #define w(j, i) W[(size_t)(j) + i]
151 #define blk2(j, i) (w(j, i) = w(j, (i)-16) + blk2_main(j, i)) 152 #define blk2(j, i) (w(j, i) = w(j, (i)-16) + blk2_main(j, i))
@@ -176,7 +177,7 @@ void Sha256_Init(CSha256 *p)
176#define R1_PRE(i) T1( W_PRE, i) 177#define R1_PRE(i) T1( W_PRE, i)
177#define R1_MAIN(i) T1( W_MAIN, i) 178#define R1_MAIN(i) T1( W_MAIN, i)
178 179
179#if (!defined(_SHA256_UNROLL) || STEP_MAIN < 8) && (STEP_MAIN >= 4) 180#if (!defined(Z7_SHA256_UNROLL) || STEP_MAIN < 8) && (STEP_MAIN >= 4)
180#define R2_MAIN(i) \ 181#define R2_MAIN(i) \
181 R1_MAIN(i) \ 182 R1_MAIN(i) \
182 R1_MAIN(i + 1) \ 183 R1_MAIN(i + 1) \
@@ -185,7 +186,7 @@ void Sha256_Init(CSha256 *p)
185 186
186 187
187 188
188#if defined(_SHA256_UNROLL) && STEP_MAIN >= 8 189#if defined(Z7_SHA256_UNROLL) && STEP_MAIN >= 8
189 190
190#define T4( a,b,c,d,e,f,g,h, wx, i) \ 191#define T4( a,b,c,d,e,f,g,h, wx, i) \
191 h += S1(e) + Ch(e,f,g) + K[(i)+(size_t)(j)] + wx(i); \ 192 h += S1(e) + Ch(e,f,g) + K[(i)+(size_t)(j)] + wx(i); \
@@ -223,7 +224,7 @@ void Sha256_Init(CSha256 *p)
223 224
224#endif 225#endif
225 226
226void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); 227void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks);
227 228
228// static 229// static
229extern MY_ALIGN(64) 230extern MY_ALIGN(64)
@@ -252,11 +253,11 @@ const UInt32 SHA256_K_ARRAY[64] = {
252#define K SHA256_K_ARRAY 253#define K SHA256_K_ARRAY
253 254
254 255
255MY_NO_INLINE 256Z7_NO_INLINE
256void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks) 257void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks)
257{ 258{
258 UInt32 W 259 UInt32 W
259 #ifdef _SHA256_BIG_W 260 #ifdef Z7_SHA256_BIG_W
260 [64]; 261 [64];
261 #else 262 #else
262 [16]; 263 [16];
@@ -266,7 +267,7 @@ void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t
266 267
267 UInt32 a,b,c,d,e,f,g,h; 268 UInt32 a,b,c,d,e,f,g,h;
268 269
269 #if !defined(_SHA256_UNROLL) || (STEP_MAIN <= 4) || (STEP_PRE <= 4) 270 #if !defined(Z7_SHA256_UNROLL) || (STEP_MAIN <= 4) || (STEP_PRE <= 4)
270 UInt32 tmp; 271 UInt32 tmp;
271 #endif 272 #endif
272 273
@@ -297,12 +298,12 @@ void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t
297 298
298 #else 299 #else
299 300
300 R1_PRE(0); 301 R1_PRE(0)
301 #if STEP_PRE >= 2 302 #if STEP_PRE >= 2
302 R1_PRE(1); 303 R1_PRE(1)
303 #if STEP_PRE >= 4 304 #if STEP_PRE >= 4
304 R1_PRE(2); 305 R1_PRE(2)
305 R1_PRE(3); 306 R1_PRE(3)
306 #endif 307 #endif
307 #endif 308 #endif
308 309
@@ -311,32 +312,32 @@ void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t
311 312
312 for (j = 16; j < 64; j += STEP_MAIN) 313 for (j = 16; j < 64; j += STEP_MAIN)
313 { 314 {
314 #if defined(_SHA256_UNROLL) && STEP_MAIN >= 8 315 #if defined(Z7_SHA256_UNROLL) && STEP_MAIN >= 8
315 316
316 #if STEP_MAIN < 8 317 #if STEP_MAIN < 8
317 R4_MAIN(0); 318 R4_MAIN(0)
318 #else 319 #else
319 R8_MAIN(0); 320 R8_MAIN(0)
320 #if STEP_MAIN == 16 321 #if STEP_MAIN == 16
321 R8_MAIN(8); 322 R8_MAIN(8)
322 #endif 323 #endif
323 #endif 324 #endif
324 325
325 #else 326 #else
326 327
327 R1_MAIN(0); 328 R1_MAIN(0)
328 #if STEP_MAIN >= 2 329 #if STEP_MAIN >= 2
329 R1_MAIN(1); 330 R1_MAIN(1)
330 #if STEP_MAIN >= 4 331 #if STEP_MAIN >= 4
331 R2_MAIN(2); 332 R2_MAIN(2)
332 #if STEP_MAIN >= 8 333 #if STEP_MAIN >= 8
333 R2_MAIN(4); 334 R2_MAIN(4)
334 R2_MAIN(6); 335 R2_MAIN(6)
335 #if STEP_MAIN >= 16 336 #if STEP_MAIN >= 16
336 R2_MAIN(8); 337 R2_MAIN(8)
337 R2_MAIN(10); 338 R2_MAIN(10)
338 R2_MAIN(12); 339 R2_MAIN(12)
339 R2_MAIN(14); 340 R2_MAIN(14)
340 #endif 341 #endif
341 #endif 342 #endif
342 #endif 343 #endif
@@ -367,7 +368,7 @@ void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t
367#undef s1 368#undef s1
368#undef K 369#undef K
369 370
370#define Sha256_UpdateBlock(p) UPDATE_BLOCKS(p)(p->state, p->buffer, 1) 371#define Sha256_UpdateBlock(p) SHA256_UPDATE_BLOCKS(p)(p->state, p->buffer, 1)
371 372
372void Sha256_Update(CSha256 *p, const Byte *data, size_t size) 373void Sha256_Update(CSha256 *p, const Byte *data, size_t size)
373{ 374{
@@ -397,7 +398,7 @@ void Sha256_Update(CSha256 *p, const Byte *data, size_t size)
397 } 398 }
398 { 399 {
399 size_t numBlocks = size >> 6; 400 size_t numBlocks = size >> 6;
400 UPDATE_BLOCKS(p)(p->state, data, numBlocks); 401 SHA256_UPDATE_BLOCKS(p)(p->state, data, numBlocks);
401 size &= 0x3F; 402 size &= 0x3F;
402 if (size == 0) 403 if (size == 0)
403 return; 404 return;
@@ -441,8 +442,8 @@ void Sha256_Final(CSha256 *p, Byte *digest)
441 442
442 { 443 {
443 UInt64 numBits = (p->count << 3); 444 UInt64 numBits = (p->count << 3);
444 SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32)); 445 SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32))
445 SetBe32(p->buffer + 64 - 4, (UInt32)(numBits)); 446 SetBe32(p->buffer + 64 - 4, (UInt32)(numBits))
446 } 447 }
447 448
448 Sha256_UpdateBlock(p); 449 Sha256_UpdateBlock(p);
@@ -451,8 +452,8 @@ void Sha256_Final(CSha256 *p, Byte *digest)
451 { 452 {
452 UInt32 v0 = p->state[i]; 453 UInt32 v0 = p->state[i];
453 UInt32 v1 = p->state[(size_t)i + 1]; 454 UInt32 v1 = p->state[(size_t)i + 1];
454 SetBe32(digest , v0); 455 SetBe32(digest , v0)
455 SetBe32(digest + 4, v1); 456 SetBe32(digest + 4, v1)
456 digest += 8; 457 digest += 8;
457 } 458 }
458 459
@@ -460,9 +461,9 @@ void Sha256_Final(CSha256 *p, Byte *digest)
460} 461}
461 462
462 463
463void Sha256Prepare() 464void Sha256Prepare(void)
464{ 465{
465 #ifdef _SHA_SUPPORTED 466 #ifdef Z7_COMPILER_SHA256_SUPPORTED
466 SHA256_FUNC_UPDATE_BLOCKS f, f_hw; 467 SHA256_FUNC_UPDATE_BLOCKS f, f_hw;
467 f = Sha256_UpdateBlocks; 468 f = Sha256_UpdateBlocks;
468 f_hw = NULL; 469 f_hw = NULL;
@@ -480,7 +481,36 @@ void Sha256Prepare()
480 // printf("\n========== HW SHA256 ======== \n"); 481 // printf("\n========== HW SHA256 ======== \n");
481 f = f_hw = Sha256_UpdateBlocks_HW; 482 f = f_hw = Sha256_UpdateBlocks_HW;
482 } 483 }
483 g_FUNC_UPDATE_BLOCKS = f; 484 g_SHA256_FUNC_UPDATE_BLOCKS = f;
484 g_FUNC_UPDATE_BLOCKS_HW = f_hw; 485 g_SHA256_FUNC_UPDATE_BLOCKS_HW = f_hw;
485 #endif 486 #endif
486} 487}
488
489#undef S0
490#undef S1
491#undef s0
492#undef s1
493#undef Ch
494#undef Maj
495#undef W_MAIN
496#undef W_PRE
497#undef w
498#undef blk2_main
499#undef blk2
500#undef T1
501#undef T4
502#undef T8
503#undef R1_PRE
504#undef R1_MAIN
505#undef R2_MAIN
506#undef R4
507#undef R4_PRE
508#undef R4_MAIN
509#undef R8
510#undef R8_PRE
511#undef R8_MAIN
512#undef STEP_PRE
513#undef STEP_MAIN
514#undef Z7_SHA256_BIG_W
515#undef Z7_SHA256_UNROLL
516#undef Z7_COMPILER_SHA256_SUPPORTED
diff --git a/C/Sha256.h b/C/Sha256.h
index aa38501..9e04223 100644
--- a/C/Sha256.h
+++ b/C/Sha256.h
@@ -1,8 +1,8 @@
1/* Sha256.h -- SHA-256 Hash 1/* Sha256.h -- SHA-256 Hash
22021-01-01 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_SHA256_H 4#ifndef ZIP7_INC_SHA256_H
5#define __7Z_SHA256_H 5#define ZIP7_INC_SHA256_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
@@ -14,7 +14,7 @@ EXTERN_C_BEGIN
14#define SHA256_BLOCK_SIZE (SHA256_NUM_BLOCK_WORDS * 4) 14#define SHA256_BLOCK_SIZE (SHA256_NUM_BLOCK_WORDS * 4)
15#define SHA256_DIGEST_SIZE (SHA256_NUM_DIGEST_WORDS * 4) 15#define SHA256_DIGEST_SIZE (SHA256_NUM_DIGEST_WORDS * 4)
16 16
17typedef void (MY_FAST_CALL *SHA256_FUNC_UPDATE_BLOCKS)(UInt32 state[8], const Byte *data, size_t numBlocks); 17typedef void (Z7_FASTCALL *SHA256_FUNC_UPDATE_BLOCKS)(UInt32 state[8], const Byte *data, size_t numBlocks);
18 18
19/* 19/*
20 if (the system supports different SHA256 code implementations) 20 if (the system supports different SHA256 code implementations)
@@ -34,7 +34,7 @@ typedef struct
34{ 34{
35 SHA256_FUNC_UPDATE_BLOCKS func_UpdateBlocks; 35 SHA256_FUNC_UPDATE_BLOCKS func_UpdateBlocks;
36 UInt64 count; 36 UInt64 count;
37 UInt64 __pad_2[2]; 37 UInt64 _pad_2[2];
38 UInt32 state[SHA256_NUM_DIGEST_WORDS]; 38 UInt32 state[SHA256_NUM_DIGEST_WORDS];
39 39
40 Byte buffer[SHA256_BLOCK_SIZE]; 40 Byte buffer[SHA256_BLOCK_SIZE];
@@ -62,7 +62,7 @@ void Sha256_Final(CSha256 *p, Byte *digest);
62 62
63 63
64 64
65// void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks); 65// void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks);
66 66
67/* 67/*
68call Sha256Prepare() once at program start. 68call Sha256Prepare() once at program start.
diff --git a/C/Sha256Opt.c b/C/Sha256Opt.c
index decc138..e4465e3 100644
--- a/C/Sha256Opt.c
+++ b/C/Sha256Opt.c
@@ -1,7 +1,9 @@
1/* Sha256Opt.c -- SHA-256 optimized code for SHA-256 hardware instructions 1/* Sha256Opt.c -- SHA-256 optimized code for SHA-256 hardware instructions
22021-04-01 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5#include "Compiler.h"
6#include "CpuArch.h"
5 7
6#if defined(_MSC_VER) 8#if defined(_MSC_VER)
7#if (_MSC_VER < 1900) && (_MSC_VER >= 1200) 9#if (_MSC_VER < 1900) && (_MSC_VER >= 1200)
@@ -9,41 +11,26 @@
9#endif 11#endif
10#endif 12#endif
11 13
12#include "CpuArch.h"
13
14#ifdef MY_CPU_X86_OR_AMD64 14#ifdef MY_CPU_X86_OR_AMD64
15 #if defined(__clang__) 15 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1600) // fix that check
16 #if (__clang_major__ >= 8) // fix that check
17 #define USE_HW_SHA 16 #define USE_HW_SHA
18 #ifndef __SHA__ 17 #elif defined(Z7_LLVM_CLANG_VERSION) && (Z7_LLVM_CLANG_VERSION >= 30800) \
19 #define ATTRIB_SHA __attribute__((__target__("sha,ssse3"))) 18 || defined(Z7_APPLE_CLANG_VERSION) && (Z7_APPLE_CLANG_VERSION >= 50100) \
20 #if defined(_MSC_VER) 19 || defined(Z7_GCC_VERSION) && (Z7_GCC_VERSION >= 40900)
21 // SSSE3: for clang-cl:
22 #include <tmmintrin.h>
23 #define __SHA__
24 #endif
25 #endif
26
27 #endif
28 #elif defined(__GNUC__)
29 #if (__GNUC__ >= 8) // fix that check
30 #define USE_HW_SHA 20 #define USE_HW_SHA
31 #ifndef __SHA__ 21 #if !defined(_INTEL_COMPILER)
22 // icc defines __GNUC__, but icc doesn't support __attribute__(__target__)
23 #if !defined(__SHA__) || !defined(__SSSE3__)
32 #define ATTRIB_SHA __attribute__((__target__("sha,ssse3"))) 24 #define ATTRIB_SHA __attribute__((__target__("sha,ssse3")))
33 // #pragma GCC target("sha,ssse3")
34 #endif 25 #endif
35 #endif 26 #endif
36 #elif defined(__INTEL_COMPILER)
37 #if (__INTEL_COMPILER >= 1800) // fix that check
38 #define USE_HW_SHA
39 #endif
40 #elif defined(_MSC_VER) 27 #elif defined(_MSC_VER)
41 #ifdef USE_MY_MM 28 #ifdef USE_MY_MM
42 #define USE_VER_MIN 1300 29 #define USE_VER_MIN 1300
43 #else 30 #else
44 #define USE_VER_MIN 1910 31 #define USE_VER_MIN 1900
45 #endif 32 #endif
46 #if _MSC_VER >= USE_VER_MIN 33 #if (_MSC_VER >= USE_VER_MIN)
47 #define USE_HW_SHA 34 #define USE_HW_SHA
48 #endif 35 #endif
49 #endif 36 #endif
@@ -52,16 +39,19 @@
52#ifdef USE_HW_SHA 39#ifdef USE_HW_SHA
53 40
54// #pragma message("Sha256 HW") 41// #pragma message("Sha256 HW")
55// #include <wmmintrin.h>
56 42
57#if !defined(_MSC_VER) || (_MSC_VER >= 1900) 43// sse/sse2/ssse3:
44#include <tmmintrin.h>
45// sha*:
58#include <immintrin.h> 46#include <immintrin.h>
59#else
60#include <emmintrin.h>
61 47
62#if defined(_MSC_VER) && (_MSC_VER >= 1600) 48#if defined (__clang__) && defined(_MSC_VER)
63// #include <intrin.h> 49 // #if !defined(__SSSE3__)
64#endif 50 // #endif
51 #if !defined(__SHA__)
52 #include <shaintrin.h>
53 #endif
54#else
65 55
66#ifdef USE_MY_MM 56#ifdef USE_MY_MM
67#include "My_mm.h" 57#include "My_mm.h"
@@ -98,9 +88,9 @@ const UInt32 SHA256_K_ARRAY[64];
98#define K SHA256_K_ARRAY 88#define K SHA256_K_ARRAY
99 89
100 90
101#define ADD_EPI32(dest, src) dest = _mm_add_epi32(dest, src); 91#define ADD_EPI32(dest, src) dest = _mm_add_epi32(dest, src);
102#define SHA256_MSG1(dest, src) dest = _mm_sha256msg1_epu32(dest, src); 92#define SHA256_MSG1(dest, src) dest = _mm_sha256msg1_epu32(dest, src);
103#define SHA25G_MSG2(dest, src) dest = _mm_sha256msg2_epu32(dest, src); 93#define SHA25G_MSG2(dest, src) dest = _mm_sha256msg2_epu32(dest, src);
104 94
105 95
106#define LOAD_SHUFFLE(m, k) \ 96#define LOAD_SHUFFLE(m, k) \
@@ -112,7 +102,7 @@ const UInt32 SHA256_K_ARRAY[64];
112 102
113#define SM2(g0, g1, g2, g3) \ 103#define SM2(g0, g1, g2, g3) \
114 tmp = _mm_alignr_epi8(g1, g0, 4); \ 104 tmp = _mm_alignr_epi8(g1, g0, 4); \
115 ADD_EPI32(g2, tmp); \ 105 ADD_EPI32(g2, tmp) \
116 SHA25G_MSG2(g2, g1); \ 106 SHA25G_MSG2(g2, g1); \
117 107
118// #define LS0(k, g0, g1, g2, g3) LOAD_SHUFFLE(g0, k) 108// #define LS0(k, g0, g1, g2, g3) LOAD_SHUFFLE(g0, k)
@@ -138,16 +128,16 @@ const UInt32 SHA256_K_ARRAY[64];
138// We use scheme with 3 rounds ahead for SHA256_MSG1 / 2 rounds ahead for SHA256_MSG2 128// We use scheme with 3 rounds ahead for SHA256_MSG1 / 2 rounds ahead for SHA256_MSG2
139 129
140#define R4(k, g0, g1, g2, g3, OP0, OP1) \ 130#define R4(k, g0, g1, g2, g3, OP0, OP1) \
141 RND2_0(g0, k); \ 131 RND2_0(g0, k) \
142 OP0(g0, g1, g2, g3); \ 132 OP0(g0, g1, g2, g3) \
143 RND2_1; \ 133 RND2_1 \
144 OP1(g0, g1, g2, g3); \ 134 OP1(g0, g1, g2, g3) \
145 135
146#define R16(k, OP0, OP1, OP2, OP3, OP4, OP5, OP6, OP7) \ 136#define R16(k, OP0, OP1, OP2, OP3, OP4, OP5, OP6, OP7) \
147 R4 ( (k)*4+0, m0, m1, m2, m3, OP0, OP1 ) \ 137 R4 ( (k)*4+0, m0,m1,m2,m3, OP0, OP1 ) \
148 R4 ( (k)*4+1, m1, m2, m3, m0, OP2, OP3 ) \ 138 R4 ( (k)*4+1, m1,m2,m3,m0, OP2, OP3 ) \
149 R4 ( (k)*4+2, m2, m3, m0, m1, OP4, OP5 ) \ 139 R4 ( (k)*4+2, m2,m3,m0,m1, OP4, OP5 ) \
150 R4 ( (k)*4+3, m3, m0, m1, m2, OP6, OP7 ) \ 140 R4 ( (k)*4+3, m3,m0,m1,m2, OP6, OP7 ) \
151 141
152#define PREPARE_STATE \ 142#define PREPARE_STATE \
153 tmp = _mm_shuffle_epi32(state0, 0x1B); /* abcd */ \ 143 tmp = _mm_shuffle_epi32(state0, 0x1B); /* abcd */ \
@@ -157,11 +147,11 @@ const UInt32 SHA256_K_ARRAY[64];
157 state1 = _mm_unpackhi_epi64(state1, tmp); /* abef */ \ 147 state1 = _mm_unpackhi_epi64(state1, tmp); /* abef */ \
158 148
159 149
160void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); 150void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks);
161#ifdef ATTRIB_SHA 151#ifdef ATTRIB_SHA
162ATTRIB_SHA 152ATTRIB_SHA
163#endif 153#endif
164void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks) 154void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks)
165{ 155{
166 const __m128i mask = _mm_set_epi32(0x0c0d0e0f, 0x08090a0b, 0x04050607, 0x00010203); 156 const __m128i mask = _mm_set_epi32(0x0c0d0e0f, 0x08090a0b, 0x04050607, 0x00010203);
167 __m128i tmp; 157 __m128i tmp;
@@ -192,13 +182,13 @@ void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size
192 182
193 183
194 184
195 R16 ( 0, NNN, NNN, SM1, NNN, SM1, SM2, SM1, SM2 ); 185 R16 ( 0, NNN, NNN, SM1, NNN, SM1, SM2, SM1, SM2 )
196 R16 ( 1, SM1, SM2, SM1, SM2, SM1, SM2, SM1, SM2 ); 186 R16 ( 1, SM1, SM2, SM1, SM2, SM1, SM2, SM1, SM2 )
197 R16 ( 2, SM1, SM2, SM1, SM2, SM1, SM2, SM1, SM2 ); 187 R16 ( 2, SM1, SM2, SM1, SM2, SM1, SM2, SM1, SM2 )
198 R16 ( 3, SM1, SM2, NNN, SM2, NNN, NNN, NNN, NNN ); 188 R16 ( 3, SM1, SM2, NNN, SM2, NNN, NNN, NNN, NNN )
199 189
200 ADD_EPI32(state0, state0_save); 190 ADD_EPI32(state0, state0_save)
201 ADD_EPI32(state1, state1_save); 191 ADD_EPI32(state1, state1_save)
202 192
203 data += 64; 193 data += 64;
204 } 194 }
@@ -298,11 +288,11 @@ const UInt32 SHA256_K_ARRAY[64];
298 R4 ( (k)*4+3, m3, m0, m1, m2, OP6, OP7 ) \ 288 R4 ( (k)*4+3, m3, m0, m1, m2, OP6, OP7 ) \
299 289
300 290
301void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); 291void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks);
302#ifdef ATTRIB_SHA 292#ifdef ATTRIB_SHA
303ATTRIB_SHA 293ATTRIB_SHA
304#endif 294#endif
305void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks) 295void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks)
306{ 296{
307 v128 state0, state1; 297 v128 state0, state1;
308 298
@@ -353,12 +343,12 @@ void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size
353// #include <stdlib.h> 343// #include <stdlib.h>
354 344
355// #include "Sha256.h" 345// #include "Sha256.h"
356void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks); 346void Z7_FASTCALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks);
357 347
358#pragma message("Sha256 HW-SW stub was used") 348#pragma message("Sha256 HW-SW stub was used")
359 349
360void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks); 350void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks);
361void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks) 351void Z7_FASTCALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size_t numBlocks)
362{ 352{
363 Sha256_UpdateBlocks(state, data, numBlocks); 353 Sha256_UpdateBlocks(state, data, numBlocks);
364 /* 354 /*
@@ -371,3 +361,26 @@ void MY_FAST_CALL Sha256_UpdateBlocks_HW(UInt32 state[8], const Byte *data, size
371} 361}
372 362
373#endif 363#endif
364
365
366
367#undef K
368#undef RND2
369#undef RND2_0
370#undef RND2_1
371
372#undef MY_rev32_for_LE
373#undef NNN
374#undef LOAD_128
375#undef STORE_128
376#undef LOAD_SHUFFLE
377#undef SM1
378#undef SM2
379
380#undef NNN
381#undef R4
382#undef R16
383#undef PREPARE_STATE
384#undef USE_HW_SHA
385#undef ATTRIB_SHA
386#undef USE_VER_MIN
diff --git a/C/Sort.h b/C/Sort.h
index 2e2963a..1817b65 100644
--- a/C/Sort.h
+++ b/C/Sort.h
@@ -1,8 +1,8 @@
1/* Sort.h -- Sort functions 1/* Sort.h -- Sort functions
22014-04-05 : Igor Pavlov : Public domain */ 22023-03-05 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_SORT_H 4#ifndef ZIP7_INC_SORT_H
5#define __7Z_SORT_H 5#define ZIP7_INC_SORT_H
6 6
7#include "7zTypes.h" 7#include "7zTypes.h"
8 8
diff --git a/C/SwapBytes.c b/C/SwapBytes.c
new file mode 100644
index 0000000..7901bba
--- /dev/null
+++ b/C/SwapBytes.c
@@ -0,0 +1,800 @@
1/* SwapBytes.c -- Byte Swap conversion filter
22023-04-07 : Igor Pavlov : Public domain */
3
4#include "Precomp.h"
5
6#include "Compiler.h"
7#include "CpuArch.h"
8#include "RotateDefs.h"
9#include "SwapBytes.h"
10
11typedef UInt16 CSwapUInt16;
12typedef UInt32 CSwapUInt32;
13
14// #define k_SwapBytes_Mode_BASE 0
15
16#ifdef MY_CPU_X86_OR_AMD64
17
18#define k_SwapBytes_Mode_SSE2 1
19#define k_SwapBytes_Mode_SSSE3 2
20#define k_SwapBytes_Mode_AVX2 3
21
22 // #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1900)
23 #if defined(__clang__) && (__clang_major__ >= 4) \
24 || defined(Z7_GCC_VERSION) && (Z7_GCC_VERSION >= 40701)
25 #define k_SwapBytes_Mode_MAX k_SwapBytes_Mode_AVX2
26 #define SWAP_ATTRIB_SSE2 __attribute__((__target__("sse2")))
27 #define SWAP_ATTRIB_SSSE3 __attribute__((__target__("ssse3")))
28 #define SWAP_ATTRIB_AVX2 __attribute__((__target__("avx2")))
29 #elif defined(_MSC_VER)
30 #if (_MSC_VER == 1900)
31 #pragma warning(disable : 4752) // found Intel(R) Advanced Vector Extensions; consider using /arch:AVX
32 #endif
33 #if (_MSC_VER >= 1900)
34 #define k_SwapBytes_Mode_MAX k_SwapBytes_Mode_AVX2
35 #elif (_MSC_VER >= 1500) // (VS2008)
36 #define k_SwapBytes_Mode_MAX k_SwapBytes_Mode_SSSE3
37 #elif (_MSC_VER >= 1310) // (VS2003)
38 #define k_SwapBytes_Mode_MAX k_SwapBytes_Mode_SSE2
39 #endif
40 #endif // _MSC_VER
41
42/*
43// for debug
44#ifdef k_SwapBytes_Mode_MAX
45#undef k_SwapBytes_Mode_MAX
46#endif
47*/
48
49#ifndef k_SwapBytes_Mode_MAX
50#define k_SwapBytes_Mode_MAX 0
51#endif
52
53#if (k_SwapBytes_Mode_MAX != 0) && defined(MY_CPU_AMD64)
54 #define k_SwapBytes_Mode_MIN k_SwapBytes_Mode_SSE2
55#else
56 #define k_SwapBytes_Mode_MIN 0
57#endif
58
59#if (k_SwapBytes_Mode_MAX >= k_SwapBytes_Mode_AVX2)
60 #define USE_SWAP_AVX2
61#endif
62#if (k_SwapBytes_Mode_MAX >= k_SwapBytes_Mode_SSSE3)
63 #define USE_SWAP_SSSE3
64#endif
65#if (k_SwapBytes_Mode_MAX >= k_SwapBytes_Mode_SSE2)
66 #define USE_SWAP_128
67#endif
68
69#if k_SwapBytes_Mode_MAX <= k_SwapBytes_Mode_MIN || !defined(USE_SWAP_128)
70#define FORCE_SWAP_MODE
71#endif
72
73
74#ifdef USE_SWAP_128
75/*
76 <mmintrin.h> MMX
77<xmmintrin.h> SSE
78<emmintrin.h> SSE2
79<pmmintrin.h> SSE3
80<tmmintrin.h> SSSE3
81<smmintrin.h> SSE4.1
82<nmmintrin.h> SSE4.2
83<ammintrin.h> SSE4A
84<wmmintrin.h> AES
85<immintrin.h> AVX, AVX2, FMA
86*/
87
88#include <emmintrin.h> // sse2
89// typedef __m128i v128;
90
91#define SWAP2_128(i) { \
92 const __m128i v = *(const __m128i *)(const void *)(items + (i) * 8); \
93 *( __m128i *)( void *)(items + (i) * 8) = \
94 _mm_or_si128( \
95 _mm_slli_epi16(v, 8), \
96 _mm_srli_epi16(v, 8)); }
97// _mm_or_si128() has more ports to execute than _mm_add_epi16().
98
99static
100#ifdef SWAP_ATTRIB_SSE2
101SWAP_ATTRIB_SSE2
102#endif
103void
104Z7_FASTCALL
105SwapBytes2_128(CSwapUInt16 *items, const CSwapUInt16 *lim)
106{
107 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
108 do
109 {
110 SWAP2_128(0) SWAP2_128(1) items += 2 * 8;
111 SWAP2_128(0) SWAP2_128(1) items += 2 * 8;
112 }
113 while (items != lim);
114}
115
116/*
117// sse2
118#define SWAP4_128_pack(i) { \
119 __m128i v = *(const __m128i *)(const void *)(items + (i) * 4); \
120 __m128i v0 = _mm_unpacklo_epi8(v, mask); \
121 __m128i v1 = _mm_unpackhi_epi8(v, mask); \
122 v0 = _mm_shufflelo_epi16(v0, 0x1b); \
123 v1 = _mm_shufflelo_epi16(v1, 0x1b); \
124 v0 = _mm_shufflehi_epi16(v0, 0x1b); \
125 v1 = _mm_shufflehi_epi16(v1, 0x1b); \
126 *(__m128i *)(void *)(items + (i) * 4) = _mm_packus_epi16(v0, v1); }
127
128static
129#ifdef SWAP_ATTRIB_SSE2
130SWAP_ATTRIB_SSE2
131#endif
132void
133Z7_FASTCALL
134SwapBytes4_128_pack(CSwapUInt32 *items, const CSwapUInt32 *lim)
135{
136 const __m128i mask = _mm_setzero_si128();
137 // const __m128i mask = _mm_set_epi16(0, 0, 0, 0, 0, 0, 0, 0);
138 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
139 do
140 {
141 SWAP4_128_pack(0); items += 1 * 4;
142 // SWAP4_128_pack(0); SWAP4_128_pack(1); items += 2 * 4;
143 }
144 while (items != lim);
145}
146
147// sse2
148#define SWAP4_128_shift(i) { \
149 __m128i v = *(const __m128i *)(const void *)(items + (i) * 4); \
150 __m128i v2; \
151 v2 = _mm_or_si128( \
152 _mm_slli_si128(_mm_and_si128(v, mask), 1), \
153 _mm_and_si128(_mm_srli_si128(v, 1), mask)); \
154 v = _mm_or_si128( \
155 _mm_slli_epi32(v, 24), \
156 _mm_srli_epi32(v, 24)); \
157 *(__m128i *)(void *)(items + (i) * 4) = _mm_or_si128(v2, v); }
158
159static
160#ifdef SWAP_ATTRIB_SSE2
161SWAP_ATTRIB_SSE2
162#endif
163void
164Z7_FASTCALL
165SwapBytes4_128_shift(CSwapUInt32 *items, const CSwapUInt32 *lim)
166{
167 #define M1 0xff00
168 const __m128i mask = _mm_set_epi32(M1, M1, M1, M1);
169 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
170 do
171 {
172 // SWAP4_128_shift(0) SWAP4_128_shift(1) items += 2 * 4;
173 // SWAP4_128_shift(0) SWAP4_128_shift(1) items += 2 * 4;
174 SWAP4_128_shift(0); items += 1 * 4;
175 }
176 while (items != lim);
177}
178*/
179
180
181#if defined(USE_SWAP_SSSE3) || defined(USE_SWAP_AVX2)
182
183#define SWAP_SHUF_REV_SEQ_2_VALS(v) (v)+1, (v)
184#define SWAP_SHUF_REV_SEQ_4_VALS(v) (v)+3, (v)+2, (v)+1, (v)
185
186#define SWAP2_SHUF_MASK_16_BYTES \
187 SWAP_SHUF_REV_SEQ_2_VALS (0 * 2), \
188 SWAP_SHUF_REV_SEQ_2_VALS (1 * 2), \
189 SWAP_SHUF_REV_SEQ_2_VALS (2 * 2), \
190 SWAP_SHUF_REV_SEQ_2_VALS (3 * 2), \
191 SWAP_SHUF_REV_SEQ_2_VALS (4 * 2), \
192 SWAP_SHUF_REV_SEQ_2_VALS (5 * 2), \
193 SWAP_SHUF_REV_SEQ_2_VALS (6 * 2), \
194 SWAP_SHUF_REV_SEQ_2_VALS (7 * 2)
195
196#define SWAP4_SHUF_MASK_16_BYTES \
197 SWAP_SHUF_REV_SEQ_4_VALS (0 * 4), \
198 SWAP_SHUF_REV_SEQ_4_VALS (1 * 4), \
199 SWAP_SHUF_REV_SEQ_4_VALS (2 * 4), \
200 SWAP_SHUF_REV_SEQ_4_VALS (3 * 4)
201
202#if defined(USE_SWAP_AVX2)
203/* if we use 256_BIT_INIT_MASK, each static array mask will be larger for 16 bytes */
204// #define SWAP_USE_256_BIT_INIT_MASK
205#endif
206
207#if defined(SWAP_USE_256_BIT_INIT_MASK) && defined(USE_SWAP_AVX2)
208#define SWAP_MASK_INIT_SIZE 32
209#else
210#define SWAP_MASK_INIT_SIZE 16
211#endif
212
213MY_ALIGN(SWAP_MASK_INIT_SIZE)
214static const Byte k_ShufMask_Swap2[] =
215{
216 SWAP2_SHUF_MASK_16_BYTES
217 #if SWAP_MASK_INIT_SIZE > 16
218 , SWAP2_SHUF_MASK_16_BYTES
219 #endif
220};
221
222MY_ALIGN(SWAP_MASK_INIT_SIZE)
223static const Byte k_ShufMask_Swap4[] =
224{
225 SWAP4_SHUF_MASK_16_BYTES
226 #if SWAP_MASK_INIT_SIZE > 16
227 , SWAP4_SHUF_MASK_16_BYTES
228 #endif
229};
230
231
232#ifdef USE_SWAP_SSSE3
233
234#include <tmmintrin.h> // ssse3
235
236#define SHUF_128(i) *(items + (i)) = \
237 _mm_shuffle_epi8(*(items + (i)), mask); // SSSE3
238
239// Z7_NO_INLINE
240static
241#ifdef SWAP_ATTRIB_SSSE3
242SWAP_ATTRIB_SSSE3
243#endif
244Z7_ATTRIB_NO_VECTORIZE
245void
246Z7_FASTCALL
247ShufBytes_128(void *items8, const void *lim8, const void *mask128_ptr)
248{
249 __m128i *items = (__m128i *)items8;
250 const __m128i *lim = (const __m128i *)lim8;
251 // const __m128i mask = _mm_set_epi8(SHUF_SWAP2_MASK_16_VALS);
252 // const __m128i mask = _mm_set_epi8(SHUF_SWAP4_MASK_16_VALS);
253 // const __m128i mask = _mm_load_si128((const __m128i *)(const void *)&(k_ShufMask_Swap4[0]));
254 // const __m128i mask = _mm_load_si128((const __m128i *)(const void *)&(k_ShufMask_Swap4[0]));
255 // const __m128i mask = *(const __m128i *)(const void *)&(k_ShufMask_Swap4[0]);
256 const __m128i mask = *(const __m128i *)mask128_ptr;
257 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
258 do
259 {
260 SHUF_128(0) SHUF_128(1) items += 2;
261 SHUF_128(0) SHUF_128(1) items += 2;
262 }
263 while (items != lim);
264}
265
266#endif // USE_SWAP_SSSE3
267
268
269
270#ifdef USE_SWAP_AVX2
271
272#include <immintrin.h> // avx, avx2
273#if defined(__clang__)
274#include <avxintrin.h>
275#include <avx2intrin.h>
276#endif
277
278#define SHUF_256(i) *(items + (i)) = \
279 _mm256_shuffle_epi8(*(items + (i)), mask); // AVX2
280
281// Z7_NO_INLINE
282static
283#ifdef SWAP_ATTRIB_AVX2
284SWAP_ATTRIB_AVX2
285#endif
286Z7_ATTRIB_NO_VECTORIZE
287void
288Z7_FASTCALL
289ShufBytes_256(void *items8, const void *lim8, const void *mask128_ptr)
290{
291 __m256i *items = (__m256i *)items8;
292 const __m256i *lim = (const __m256i *)lim8;
293 /*
294 UNUSED_VAR(mask128_ptr)
295 __m256i mask =
296 for Swap4: _mm256_setr_epi8(SWAP4_SHUF_MASK_16_BYTES, SWAP4_SHUF_MASK_16_BYTES);
297 for Swap2: _mm256_setr_epi8(SWAP2_SHUF_MASK_16_BYTES, SWAP2_SHUF_MASK_16_BYTES);
298 */
299 const __m256i mask =
300 #if SWAP_MASK_INIT_SIZE > 16
301 *(const __m256i *)(const void *)mask128_ptr;
302 #else
303 /* msvc: broadcastsi128() version reserves the stack for no reason
304 msvc 19.29-: _mm256_insertf128_si256() / _mm256_set_m128i)) versions use non-avx movdqu xmm0,XMMWORD PTR [r8]
305 msvc 19.30+ (VS2022): replaces _mm256_set_m128i(m,m) to vbroadcastf128(m) as we want
306 */
307 // _mm256_broadcastsi128_si256(*mask128_ptr);
308 /*
309 #define MY_mm256_set_m128i(hi, lo) _mm256_insertf128_si256(_mm256_castsi128_si256(lo), (hi), 1)
310 MY_mm256_set_m128i
311 */
312 _mm256_set_m128i(
313 *(const __m128i *)mask128_ptr,
314 *(const __m128i *)mask128_ptr);
315 #endif
316
317 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
318 do
319 {
320 SHUF_256(0) SHUF_256(1) items += 2;
321 SHUF_256(0) SHUF_256(1) items += 2;
322 }
323 while (items != lim);
324}
325
326#endif // USE_SWAP_AVX2
327#endif // USE_SWAP_SSSE3 || USE_SWAP_AVX2
328#endif // USE_SWAP_128
329
330
331
332// compile message "NEON intrinsics not available with the soft-float ABI"
333#elif defined(MY_CPU_ARM_OR_ARM64) || \
334 (defined(__ARM_ARCH) && (__ARM_ARCH >= 7))
335// #elif defined(MY_CPU_ARM64)
336
337 #if defined(__clang__) && (__clang_major__ >= 8) \
338 || defined(__GNUC__) && (__GNUC__ >= 8)
339 #if (defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) \
340 || defined(MY_CPU_ARM64)
341 #define USE_SWAP_128
342 #endif
343 #ifdef MY_CPU_ARM64
344 // #define SWAP_ATTRIB_NEON __attribute__((__target__("")))
345 #else
346 // #define SWAP_ATTRIB_NEON __attribute__((__target__("fpu=crypto-neon-fp-armv8")))
347 #endif
348 #elif defined(_MSC_VER)
349 #if (_MSC_VER >= 1910)
350 #define USE_SWAP_128
351 #endif
352 #endif
353
354 #if defined(_MSC_VER) && defined(MY_CPU_ARM64)
355 #include <arm64_neon.h>
356 #else
357 #include <arm_neon.h>
358 #endif
359
360#ifndef USE_SWAP_128
361 #define FORCE_SWAP_MODE
362#else
363
364#ifdef MY_CPU_ARM64
365 // for debug : comment it
366 #define FORCE_SWAP_MODE
367#else
368 #define k_SwapBytes_Mode_NEON 1
369#endif
370// typedef uint8x16_t v128;
371#define SWAP2_128(i) *(uint8x16_t *) (void *)(items + (i) * 8) = \
372 vrev16q_u8(*(const uint8x16_t *)(const void *)(items + (i) * 8));
373#define SWAP4_128(i) *(uint8x16_t *) (void *)(items + (i) * 4) = \
374 vrev32q_u8(*(const uint8x16_t *)(const void *)(items + (i) * 4));
375
376// Z7_NO_INLINE
377static
378#ifdef SWAP_ATTRIB_NEON
379SWAP_ATTRIB_NEON
380#endif
381Z7_ATTRIB_NO_VECTORIZE
382void
383Z7_FASTCALL
384SwapBytes2_128(CSwapUInt16 *items, const CSwapUInt16 *lim)
385{
386 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
387 do
388 {
389 SWAP2_128(0) SWAP2_128(1) items += 2 * 8;
390 SWAP2_128(0) SWAP2_128(1) items += 2 * 8;
391 }
392 while (items != lim);
393}
394
395// Z7_NO_INLINE
396static
397#ifdef SWAP_ATTRIB_NEON
398SWAP_ATTRIB_NEON
399#endif
400Z7_ATTRIB_NO_VECTORIZE
401void
402Z7_FASTCALL
403SwapBytes4_128(CSwapUInt32 *items, const CSwapUInt32 *lim)
404{
405 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
406 do
407 {
408 SWAP4_128(0) SWAP4_128(1) items += 2 * 4;
409 SWAP4_128(0) SWAP4_128(1) items += 2 * 4;
410 }
411 while (items != lim);
412}
413
414#endif // USE_SWAP_128
415
416#else // MY_CPU_ARM_OR_ARM64
417#define FORCE_SWAP_MODE
418#endif // MY_CPU_ARM_OR_ARM64
419
420
421
422
423
424
425#if defined(Z7_MSC_VER_ORIGINAL) && defined(MY_CPU_X86)
426 /* _byteswap_ushort() in MSVC x86 32-bit works via slow { mov dh, al; mov dl, ah }
427 So we use own versions of byteswap function */
428 #if (_MSC_VER < 1400 ) // old MSVC-X86 without _rotr16() support
429 #define SWAP2_16(i) { UInt32 v = items[i]; v += (v << 16); v >>= 8; items[i] = (CSwapUInt16)v; }
430 #else // is new MSVC-X86 with fast _rotr16()
431 #include <intrin.h>
432 #define SWAP2_16(i) { items[i] = _rotr16(items[i], 8); }
433 #endif
434#else // is not MSVC-X86
435 #define SWAP2_16(i) { CSwapUInt16 v = items[i]; items[i] = Z7_BSWAP16(v); }
436#endif // MSVC-X86
437
438#if defined(Z7_CPU_FAST_BSWAP_SUPPORTED)
439 #define SWAP4_32(i) { CSwapUInt32 v = items[i]; items[i] = Z7_BSWAP32(v); }
440#else
441 #define SWAP4_32(i) \
442 { UInt32 v = items[i]; \
443 v = ((v & 0xff00ff) << 8) + ((v >> 8) & 0xff00ff); \
444 v = rotlFixed(v, 16); \
445 items[i] = v; }
446#endif
447
448
449
450
451#if defined(FORCE_SWAP_MODE) && defined(USE_SWAP_128)
452 #define DEFAULT_Swap2 SwapBytes2_128
453 #if !defined(MY_CPU_X86_OR_AMD64)
454 #define DEFAULT_Swap4 SwapBytes4_128
455 #endif
456#endif
457
458#if !defined(DEFAULT_Swap2) || !defined(DEFAULT_Swap4)
459
460#define SWAP_BASE_FUNCS_PREFIXES \
461Z7_FORCE_INLINE \
462static \
463Z7_ATTRIB_NO_VECTOR \
464void Z7_FASTCALL
465
466
467#ifdef MY_CPU_64BIT
468
469#if defined(MY_CPU_ARM64) \
470 && defined(__ARM_ARCH) && (__ARM_ARCH >= 8) \
471 && ( (defined(__GNUC__) && (__GNUC__ >= 4)) \
472 || (defined(__clang__) && (__clang_major__ >= 4)))
473
474 #define SWAP2_64_VAR(v) asm ("rev16 %x0,%x0" : "+r" (v));
475 #define SWAP4_64_VAR(v) asm ("rev32 %x0,%x0" : "+r" (v));
476
477#else // is not ARM64-GNU
478
479#if !defined(MY_CPU_X86_OR_AMD64) || (k_SwapBytes_Mode_MIN == 0) || !defined(USE_SWAP_128)
480 #define SWAP2_64_VAR(v) \
481 v = ( 0x00ff00ff00ff00ff & (v >> 8)) \
482 + ((0x00ff00ff00ff00ff & v) << 8);
483 /* plus gives faster code in MSVC */
484#endif
485
486#ifdef Z7_CPU_FAST_BSWAP_SUPPORTED
487 #define SWAP4_64_VAR(v) \
488 v = Z7_BSWAP64(v); \
489 v = Z7_ROTL64(v, 32);
490#else
491 #define SWAP4_64_VAR(v) \
492 v = ( 0x000000ff000000ff & (v >> 24)) \
493 + ((0x000000ff000000ff & v) << 24 ) \
494 + ( 0x0000ff000000ff00 & (v >> 8)) \
495 + ((0x0000ff000000ff00 & v) << 8 ) \
496 ;
497#endif
498
499#endif // ARM64-GNU
500
501
502#ifdef SWAP2_64_VAR
503
504#define SWAP2_64(i) { \
505 UInt64 v = *(const UInt64 *)(const void *)(items + (i) * 4); \
506 SWAP2_64_VAR(v) \
507 *(UInt64 *)(void *)(items + (i) * 4) = v; }
508
509SWAP_BASE_FUNCS_PREFIXES
510SwapBytes2_64(CSwapUInt16 *items, const CSwapUInt16 *lim)
511{
512 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
513 do
514 {
515 SWAP2_64(0) SWAP2_64(1) items += 2 * 4;
516 SWAP2_64(0) SWAP2_64(1) items += 2 * 4;
517 }
518 while (items != lim);
519}
520
521 #define DEFAULT_Swap2 SwapBytes2_64
522 #if !defined(FORCE_SWAP_MODE)
523 #define SWAP2_DEFAULT_MODE 0
524 #endif
525#else // !defined(SWAP2_64_VAR)
526 #define DEFAULT_Swap2 SwapBytes2_128
527 #if !defined(FORCE_SWAP_MODE)
528 #define SWAP2_DEFAULT_MODE 1
529 #endif
530#endif // SWAP2_64_VAR
531
532
533#define SWAP4_64(i) { \
534 UInt64 v = *(const UInt64 *)(const void *)(items + (i) * 2); \
535 SWAP4_64_VAR(v) \
536 *(UInt64 *)(void *)(items + (i) * 2) = v; }
537
538SWAP_BASE_FUNCS_PREFIXES
539SwapBytes4_64(CSwapUInt32 *items, const CSwapUInt32 *lim)
540{
541 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
542 do
543 {
544 SWAP4_64(0) SWAP4_64(1) items += 2 * 2;
545 SWAP4_64(0) SWAP4_64(1) items += 2 * 2;
546 }
547 while (items != lim);
548}
549
550#define DEFAULT_Swap4 SwapBytes4_64
551
552#else // is not 64BIT
553
554
555#if defined(MY_CPU_ARM_OR_ARM64) \
556 && defined(__ARM_ARCH) && (__ARM_ARCH >= 6) \
557 && ( (defined(__GNUC__) && (__GNUC__ >= 4)) \
558 || (defined(__clang__) && (__clang_major__ >= 4)))
559
560#ifdef MY_CPU_64BIT
561 #define SWAP2_32_VAR(v) asm ("rev16 %w0,%w0" : "+r" (v));
562#else
563 #define SWAP2_32_VAR(v) asm ("rev16 %0,%0" : "+r" (v)); // for clang/gcc
564 // asm ("rev16 %r0,%r0" : "+r" (a)); // for gcc
565#endif
566
567#elif defined(_MSC_VER) && (_MSC_VER < 1300) && defined(MY_CPU_X86) \
568 || !defined(Z7_CPU_FAST_BSWAP_SUPPORTED) \
569 || !defined(Z7_CPU_FAST_ROTATE_SUPPORTED)
570 // old msvc doesn't support _byteswap_ulong()
571 #define SWAP2_32_VAR(v) \
572 v = ((v & 0xff00ff) << 8) + ((v >> 8) & 0xff00ff);
573
574#else // is not ARM and is not old-MSVC-X86 and fast BSWAP/ROTATE are supported
575 #define SWAP2_32_VAR(v) \
576 v = Z7_BSWAP32(v); \
577 v = rotlFixed(v, 16);
578
579#endif // GNU-ARM*
580
581#define SWAP2_32(i) { \
582 UInt32 v = *(const UInt32 *)(const void *)(items + (i) * 2); \
583 SWAP2_32_VAR(v); \
584 *(UInt32 *)(void *)(items + (i) * 2) = v; }
585
586
587SWAP_BASE_FUNCS_PREFIXES
588SwapBytes2_32(CSwapUInt16 *items, const CSwapUInt16 *lim)
589{
590 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
591 do
592 {
593 SWAP2_32(0) SWAP2_32(1) items += 2 * 2;
594 SWAP2_32(0) SWAP2_32(1) items += 2 * 2;
595 }
596 while (items != lim);
597}
598
599
600SWAP_BASE_FUNCS_PREFIXES
601SwapBytes4_32(CSwapUInt32 *items, const CSwapUInt32 *lim)
602{
603 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
604 do
605 {
606 SWAP4_32(0) SWAP4_32(1) items += 2;
607 SWAP4_32(0) SWAP4_32(1) items += 2;
608 }
609 while (items != lim);
610}
611
612#define DEFAULT_Swap2 SwapBytes2_32
613#define DEFAULT_Swap4 SwapBytes4_32
614#if !defined(FORCE_SWAP_MODE)
615 #define SWAP2_DEFAULT_MODE 0
616#endif
617
618#endif // MY_CPU_64BIT
619#endif // if !defined(DEFAULT_Swap2) || !defined(DEFAULT_Swap4)
620
621
622
623#if !defined(FORCE_SWAP_MODE)
624static unsigned g_SwapBytes_Mode;
625#endif
626
627/* size of largest unrolled loop iteration: 128 bytes = 4 * 32 bytes (AVX). */
628#define SWAP_ITERATION_BLOCK_SIZE_MAX (1 << 7)
629
630// 32 bytes for (AVX) or 2 * 16-bytes for NEON.
631#define SWAP_VECTOR_ALIGN_SIZE (1 << 5)
632
633Z7_NO_INLINE
634void z7_SwapBytes2(CSwapUInt16 *items, size_t numItems)
635{
636 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
637 for (; numItems != 0 && ((unsigned)(ptrdiff_t)items & (SWAP_VECTOR_ALIGN_SIZE - 1)) != 0; numItems--)
638 {
639 SWAP2_16(0)
640 items++;
641 }
642 {
643 const size_t k_Align_Mask = SWAP_ITERATION_BLOCK_SIZE_MAX / sizeof(CSwapUInt16) - 1;
644 size_t numItems2 = numItems;
645 CSwapUInt16 *lim;
646 numItems &= k_Align_Mask;
647 numItems2 &= ~(size_t)k_Align_Mask;
648 lim = items + numItems2;
649 if (numItems2 != 0)
650 {
651 #if !defined(FORCE_SWAP_MODE)
652 #ifdef MY_CPU_X86_OR_AMD64
653 #ifdef USE_SWAP_AVX2
654 if (g_SwapBytes_Mode > k_SwapBytes_Mode_SSSE3)
655 ShufBytes_256((__m256i *)(void *)items,
656 (const __m256i *)(const void *)lim,
657 (const __m128i *)(const void *)&(k_ShufMask_Swap2[0]));
658 else
659 #endif
660 #ifdef USE_SWAP_SSSE3
661 if (g_SwapBytes_Mode >= k_SwapBytes_Mode_SSSE3)
662 ShufBytes_128((__m128i *)(void *)items,
663 (const __m128i *)(const void *)lim,
664 (const __m128i *)(const void *)&(k_ShufMask_Swap2[0]));
665 else
666 #endif
667 #endif // MY_CPU_X86_OR_AMD64
668 #if SWAP2_DEFAULT_MODE == 0
669 if (g_SwapBytes_Mode != 0)
670 SwapBytes2_128(items, lim);
671 else
672 #endif
673 #endif // FORCE_SWAP_MODE
674 DEFAULT_Swap2(items, lim);
675 }
676 items = lim;
677 }
678 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
679 for (; numItems != 0; numItems--)
680 {
681 SWAP2_16(0)
682 items++;
683 }
684}
685
686
687Z7_NO_INLINE
688void z7_SwapBytes4(CSwapUInt32 *items, size_t numItems)
689{
690 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
691 for (; numItems != 0 && ((unsigned)(ptrdiff_t)items & (SWAP_VECTOR_ALIGN_SIZE - 1)) != 0; numItems--)
692 {
693 SWAP4_32(0)
694 items++;
695 }
696 {
697 const size_t k_Align_Mask = SWAP_ITERATION_BLOCK_SIZE_MAX / sizeof(CSwapUInt32) - 1;
698 size_t numItems2 = numItems;
699 CSwapUInt32 *lim;
700 numItems &= k_Align_Mask;
701 numItems2 &= ~(size_t)k_Align_Mask;
702 lim = items + numItems2;
703 if (numItems2 != 0)
704 {
705 #if !defined(FORCE_SWAP_MODE)
706 #ifdef MY_CPU_X86_OR_AMD64
707 #ifdef USE_SWAP_AVX2
708 if (g_SwapBytes_Mode > k_SwapBytes_Mode_SSSE3)
709 ShufBytes_256((__m256i *)(void *)items,
710 (const __m256i *)(const void *)lim,
711 (const __m128i *)(const void *)&(k_ShufMask_Swap4[0]));
712 else
713 #endif
714 #ifdef USE_SWAP_SSSE3
715 if (g_SwapBytes_Mode >= k_SwapBytes_Mode_SSSE3)
716 ShufBytes_128((__m128i *)(void *)items,
717 (const __m128i *)(const void *)lim,
718 (const __m128i *)(const void *)&(k_ShufMask_Swap4[0]));
719 else
720 #endif
721 #else // MY_CPU_X86_OR_AMD64
722
723 if (g_SwapBytes_Mode != 0)
724 SwapBytes4_128(items, lim);
725 else
726 #endif // MY_CPU_X86_OR_AMD64
727 #endif // FORCE_SWAP_MODE
728 DEFAULT_Swap4(items, lim);
729 }
730 items = lim;
731 }
732 Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
733 for (; numItems != 0; numItems--)
734 {
735 SWAP4_32(0)
736 items++;
737 }
738}
739
740
741// #define SHOW_HW_STATUS
742
743#ifdef SHOW_HW_STATUS
744#include <stdio.h>
745#define PRF(x) x
746#else
747#define PRF(x)
748#endif
749
750void z7_SwapBytesPrepare(void)
751{
752#ifndef FORCE_SWAP_MODE
753 unsigned mode = 0; // k_SwapBytes_Mode_BASE;
754
755#ifdef MY_CPU_ARM_OR_ARM64
756 {
757 if (CPU_IsSupported_NEON())
758 {
759 // #pragma message ("=== SwapBytes NEON")
760 PRF(printf("\n=== SwapBytes NEON\n");)
761 mode = k_SwapBytes_Mode_NEON;
762 }
763 }
764#else // MY_CPU_ARM_OR_ARM64
765 {
766 #ifdef USE_SWAP_AVX2
767 if (CPU_IsSupported_AVX2())
768 {
769 // #pragma message ("=== SwapBytes AVX2")
770 PRF(printf("\n=== SwapBytes AVX2\n");)
771 mode = k_SwapBytes_Mode_AVX2;
772 }
773 else
774 #endif
775 #ifdef USE_SWAP_SSSE3
776 if (CPU_IsSupported_SSSE3())
777 {
778 // #pragma message ("=== SwapBytes SSSE3")
779 PRF(printf("\n=== SwapBytes SSSE3\n");)
780 mode = k_SwapBytes_Mode_SSSE3;
781 }
782 else
783 #endif
784 #if !defined(MY_CPU_AMD64)
785 if (CPU_IsSupported_SSE2())
786 #endif
787 {
788 // #pragma message ("=== SwapBytes SSE2")
789 PRF(printf("\n=== SwapBytes SSE2\n");)
790 mode = k_SwapBytes_Mode_SSE2;
791 }
792 }
793#endif // MY_CPU_ARM_OR_ARM64
794 g_SwapBytes_Mode = mode;
795 // g_SwapBytes_Mode = 0; // for debug
796#endif // FORCE_SWAP_MODE
797 PRF(printf("\n=== SwapBytesPrepare\n");)
798}
799
800#undef PRF
diff --git a/C/SwapBytes.h b/C/SwapBytes.h
new file mode 100644
index 0000000..d442467
--- /dev/null
+++ b/C/SwapBytes.h
@@ -0,0 +1,17 @@
1/* SwapBytes.h -- Byte Swap conversion filter
22023-04-02 : Igor Pavlov : Public domain */
3
4#ifndef ZIP7_INC_SWAP_BYTES_H
5#define ZIP7_INC_SWAP_BYTES_H
6
7#include "7zTypes.h"
8
9EXTERN_C_BEGIN
10
11void z7_SwapBytes2(UInt16 *data, size_t numItems);
12void z7_SwapBytes4(UInt32 *data, size_t numItems);
13void z7_SwapBytesPrepare(void);
14
15EXTERN_C_END
16
17#endif
diff --git a/C/Threads.c b/C/Threads.c
index 58eb90f..cf52bd3 100644
--- a/C/Threads.c
+++ b/C/Threads.c
@@ -1,5 +1,5 @@
1/* Threads.c -- multithreading library 1/* Threads.c -- multithreading library
22021-12-21 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -11,9 +11,9 @@
11 11
12#include "Threads.h" 12#include "Threads.h"
13 13
14static WRes GetError() 14static WRes GetError(void)
15{ 15{
16 DWORD res = GetLastError(); 16 const DWORD res = GetLastError();
17 return res ? (WRes)res : 1; 17 return res ? (WRes)res : 1;
18} 18}
19 19
@@ -173,6 +173,9 @@ WRes CriticalSection_Init(CCriticalSection *p)
173 Windows XP, 2003 : can raise a STATUS_NO_MEMORY exception 173 Windows XP, 2003 : can raise a STATUS_NO_MEMORY exception
174 Windows Vista+ : no exceptions */ 174 Windows Vista+ : no exceptions */
175 #ifdef _MSC_VER 175 #ifdef _MSC_VER
176 #ifdef __clang__
177 #pragma GCC diagnostic ignored "-Wlanguage-extension-token"
178 #endif
176 __try 179 __try
177 #endif 180 #endif
178 { 181 {
@@ -193,18 +196,26 @@ WRes CriticalSection_Init(CCriticalSection *p)
193// ---------- POSIX ---------- 196// ---------- POSIX ----------
194 197
195#ifndef __APPLE__ 198#ifndef __APPLE__
196#ifndef _7ZIP_AFFINITY_DISABLE 199#ifndef Z7_AFFINITY_DISABLE
197// _GNU_SOURCE can be required for pthread_setaffinity_np() / CPU_ZERO / CPU_SET 200// _GNU_SOURCE can be required for pthread_setaffinity_np() / CPU_ZERO / CPU_SET
201// clang < 3.6 : unknown warning group '-Wreserved-id-macro'
202// clang 3.6 - 12.01 : gives warning "macro name is a reserved identifier"
203// clang >= 13 : do not give warning
204#if !defined(_GNU_SOURCE)
205 #if defined(__clang__) && (__clang_major__ >= 4) && (__clang_major__ <= 12)
206 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
207 #endif
198#define _GNU_SOURCE 208#define _GNU_SOURCE
199#endif 209#endif // !defined(_GNU_SOURCE)
200#endif 210#endif // Z7_AFFINITY_DISABLE
211#endif // __APPLE__
201 212
202#include "Threads.h" 213#include "Threads.h"
203 214
204#include <errno.h> 215#include <errno.h>
205#include <stdlib.h> 216#include <stdlib.h>
206#include <string.h> 217#include <string.h>
207#ifdef _7ZIP_AFFINITY_SUPPORTED 218#ifdef Z7_AFFINITY_SUPPORTED
208// #include <sched.h> 219// #include <sched.h>
209#endif 220#endif
210 221
@@ -212,15 +223,12 @@ WRes CriticalSection_Init(CCriticalSection *p)
212// #include <stdio.h> 223// #include <stdio.h>
213// #define PRF(p) p 224// #define PRF(p) p
214#define PRF(p) 225#define PRF(p)
215 226#define Print(s) PRF(printf("\n%s\n", s);)
216#define Print(s) PRF(printf("\n%s\n", s))
217
218// #include <stdio.h>
219 227
220WRes Thread_Create_With_CpuSet(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, const CCpuSet *cpuSet) 228WRes Thread_Create_With_CpuSet(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, const CCpuSet *cpuSet)
221{ 229{
222 // new thread in Posix probably inherits affinity from parrent thread 230 // new thread in Posix probably inherits affinity from parrent thread
223 Print("Thread_Create_With_CpuSet"); 231 Print("Thread_Create_With_CpuSet")
224 232
225 pthread_attr_t attr; 233 pthread_attr_t attr;
226 int ret; 234 int ret;
@@ -228,7 +236,7 @@ WRes Thread_Create_With_CpuSet(CThread *p, THREAD_FUNC_TYPE func, LPVOID param,
228 236
229 p->_created = 0; 237 p->_created = 0;
230 238
231 RINOK(pthread_attr_init(&attr)); 239 RINOK(pthread_attr_init(&attr))
232 240
233 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 241 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
234 242
@@ -236,7 +244,7 @@ WRes Thread_Create_With_CpuSet(CThread *p, THREAD_FUNC_TYPE func, LPVOID param,
236 { 244 {
237 if (cpuSet) 245 if (cpuSet)
238 { 246 {
239 #ifdef _7ZIP_AFFINITY_SUPPORTED 247 #ifdef Z7_AFFINITY_SUPPORTED
240 248
241 /* 249 /*
242 printf("\n affinity :"); 250 printf("\n affinity :");
@@ -292,7 +300,7 @@ WRes Thread_Create(CThread *p, THREAD_FUNC_TYPE func, LPVOID param)
292 300
293WRes Thread_Create_With_Affinity(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, CAffinityMask affinity) 301WRes Thread_Create_With_Affinity(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, CAffinityMask affinity)
294{ 302{
295 Print("Thread_Create_WithAffinity"); 303 Print("Thread_Create_WithAffinity")
296 CCpuSet cs; 304 CCpuSet cs;
297 unsigned i; 305 unsigned i;
298 CpuSet_Zero(&cs); 306 CpuSet_Zero(&cs);
@@ -312,7 +320,7 @@ WRes Thread_Create_With_Affinity(CThread *p, THREAD_FUNC_TYPE func, LPVOID param
312 320
313WRes Thread_Close(CThread *p) 321WRes Thread_Close(CThread *p)
314{ 322{
315 // Print("Thread_Close"); 323 // Print("Thread_Close")
316 int ret; 324 int ret;
317 if (!p->_created) 325 if (!p->_created)
318 return 0; 326 return 0;
@@ -326,7 +334,7 @@ WRes Thread_Close(CThread *p)
326 334
327WRes Thread_Wait_Close(CThread *p) 335WRes Thread_Wait_Close(CThread *p)
328{ 336{
329 // Print("Thread_Wait_Close"); 337 // Print("Thread_Wait_Close")
330 void *thread_return; 338 void *thread_return;
331 int ret; 339 int ret;
332 if (!p->_created) 340 if (!p->_created)
@@ -343,8 +351,8 @@ WRes Thread_Wait_Close(CThread *p)
343 351
344static WRes Event_Create(CEvent *p, int manualReset, int signaled) 352static WRes Event_Create(CEvent *p, int manualReset, int signaled)
345{ 353{
346 RINOK(pthread_mutex_init(&p->_mutex, NULL)); 354 RINOK(pthread_mutex_init(&p->_mutex, NULL))
347 RINOK(pthread_cond_init(&p->_cond, NULL)); 355 RINOK(pthread_cond_init(&p->_cond, NULL))
348 p->_manual_reset = manualReset; 356 p->_manual_reset = manualReset;
349 p->_state = (signaled ? True : False); 357 p->_state = (signaled ? True : False);
350 p->_created = 1; 358 p->_created = 1;
@@ -363,7 +371,7 @@ WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p)
363 371
364WRes Event_Set(CEvent *p) 372WRes Event_Set(CEvent *p)
365{ 373{
366 RINOK(pthread_mutex_lock(&p->_mutex)); 374 RINOK(pthread_mutex_lock(&p->_mutex))
367 p->_state = True; 375 p->_state = True;
368 int res1 = pthread_cond_broadcast(&p->_cond); 376 int res1 = pthread_cond_broadcast(&p->_cond);
369 int res2 = pthread_mutex_unlock(&p->_mutex); 377 int res2 = pthread_mutex_unlock(&p->_mutex);
@@ -372,14 +380,14 @@ WRes Event_Set(CEvent *p)
372 380
373WRes Event_Reset(CEvent *p) 381WRes Event_Reset(CEvent *p)
374{ 382{
375 RINOK(pthread_mutex_lock(&p->_mutex)); 383 RINOK(pthread_mutex_lock(&p->_mutex))
376 p->_state = False; 384 p->_state = False;
377 return pthread_mutex_unlock(&p->_mutex); 385 return pthread_mutex_unlock(&p->_mutex);
378} 386}
379 387
380WRes Event_Wait(CEvent *p) 388WRes Event_Wait(CEvent *p)
381{ 389{
382 RINOK(pthread_mutex_lock(&p->_mutex)); 390 RINOK(pthread_mutex_lock(&p->_mutex))
383 while (p->_state == False) 391 while (p->_state == False)
384 { 392 {
385 // ETIMEDOUT 393 // ETIMEDOUT
@@ -411,8 +419,8 @@ WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount)
411{ 419{
412 if (initCount > maxCount || maxCount < 1) 420 if (initCount > maxCount || maxCount < 1)
413 return EINVAL; 421 return EINVAL;
414 RINOK(pthread_mutex_init(&p->_mutex, NULL)); 422 RINOK(pthread_mutex_init(&p->_mutex, NULL))
415 RINOK(pthread_cond_init(&p->_cond, NULL)); 423 RINOK(pthread_cond_init(&p->_cond, NULL))
416 p->_count = initCount; 424 p->_count = initCount;
417 p->_maxCount = maxCount; 425 p->_maxCount = maxCount;
418 p->_created = 1; 426 p->_created = 1;
@@ -448,7 +456,7 @@ WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 releaseCount)
448 if (releaseCount < 1) 456 if (releaseCount < 1)
449 return EINVAL; 457 return EINVAL;
450 458
451 RINOK(pthread_mutex_lock(&p->_mutex)); 459 RINOK(pthread_mutex_lock(&p->_mutex))
452 460
453 newCount = p->_count + releaseCount; 461 newCount = p->_count + releaseCount;
454 if (newCount > p->_maxCount) 462 if (newCount > p->_maxCount)
@@ -458,13 +466,13 @@ WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 releaseCount)
458 p->_count = newCount; 466 p->_count = newCount;
459 ret = pthread_cond_broadcast(&p->_cond); 467 ret = pthread_cond_broadcast(&p->_cond);
460 } 468 }
461 RINOK(pthread_mutex_unlock(&p->_mutex)); 469 RINOK(pthread_mutex_unlock(&p->_mutex))
462 return ret; 470 return ret;
463} 471}
464 472
465WRes Semaphore_Wait(CSemaphore *p) 473WRes Semaphore_Wait(CSemaphore *p)
466{ 474{
467 RINOK(pthread_mutex_lock(&p->_mutex)); 475 RINOK(pthread_mutex_lock(&p->_mutex))
468 while (p->_count < 1) 476 while (p->_count < 1)
469 { 477 {
470 pthread_cond_wait(&p->_cond, &p->_mutex); 478 pthread_cond_wait(&p->_cond, &p->_mutex);
@@ -489,7 +497,7 @@ WRes Semaphore_Close(CSemaphore *p)
489 497
490WRes CriticalSection_Init(CCriticalSection *p) 498WRes CriticalSection_Init(CCriticalSection *p)
491{ 499{
492 // Print("CriticalSection_Init"); 500 // Print("CriticalSection_Init")
493 if (!p) 501 if (!p)
494 return EINTR; 502 return EINTR;
495 return pthread_mutex_init(&p->_mutex, NULL); 503 return pthread_mutex_init(&p->_mutex, NULL);
@@ -497,7 +505,7 @@ WRes CriticalSection_Init(CCriticalSection *p)
497 505
498void CriticalSection_Enter(CCriticalSection *p) 506void CriticalSection_Enter(CCriticalSection *p)
499{ 507{
500 // Print("CriticalSection_Enter"); 508 // Print("CriticalSection_Enter")
501 if (p) 509 if (p)
502 { 510 {
503 // int ret = 511 // int ret =
@@ -507,7 +515,7 @@ void CriticalSection_Enter(CCriticalSection *p)
507 515
508void CriticalSection_Leave(CCriticalSection *p) 516void CriticalSection_Leave(CCriticalSection *p)
509{ 517{
510 // Print("CriticalSection_Leave"); 518 // Print("CriticalSection_Leave")
511 if (p) 519 if (p)
512 { 520 {
513 // int ret = 521 // int ret =
@@ -517,7 +525,7 @@ void CriticalSection_Leave(CCriticalSection *p)
517 525
518void CriticalSection_Delete(CCriticalSection *p) 526void CriticalSection_Delete(CCriticalSection *p)
519{ 527{
520 // Print("CriticalSection_Delete"); 528 // Print("CriticalSection_Delete")
521 if (p) 529 if (p)
522 { 530 {
523 // int ret = 531 // int ret =
@@ -527,14 +535,28 @@ void CriticalSection_Delete(CCriticalSection *p)
527 535
528LONG InterlockedIncrement(LONG volatile *addend) 536LONG InterlockedIncrement(LONG volatile *addend)
529{ 537{
530 // Print("InterlockedIncrement"); 538 // Print("InterlockedIncrement")
531 #ifdef USE_HACK_UNSAFE_ATOMIC 539 #ifdef USE_HACK_UNSAFE_ATOMIC
532 LONG val = *addend + 1; 540 LONG val = *addend + 1;
533 *addend = val; 541 *addend = val;
534 return val; 542 return val;
535 #else 543 #else
544
545 #if defined(__clang__) && (__clang_major__ >= 8)
546 #pragma GCC diagnostic ignored "-Watomic-implicit-seq-cst"
547 #endif
536 return __sync_add_and_fetch(addend, 1); 548 return __sync_add_and_fetch(addend, 1);
537 #endif 549 #endif
538} 550}
539 551
540#endif // _WIN32 552#endif // _WIN32
553
554WRes AutoResetEvent_OptCreate_And_Reset(CAutoResetEvent *p)
555{
556 if (Event_IsCreated(p))
557 return Event_Reset(p);
558 return AutoResetEvent_CreateNotSignaled(p);
559}
560
561#undef PRF
562#undef Print
diff --git a/C/Threads.h b/C/Threads.h
index 89ecb92..4028464 100644
--- a/C/Threads.h
+++ b/C/Threads.h
@@ -1,18 +1,19 @@
1/* Threads.h -- multithreading library 1/* Threads.h -- multithreading library
22021-12-21 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_THREADS_H 4#ifndef ZIP7_INC_THREADS_H
5#define __7Z_THREADS_H 5#define ZIP7_INC_THREADS_H
6 6
7#ifdef _WIN32 7#ifdef _WIN32
8#include <Windows.h> 8#include "7zWindows.h"
9
9#else 10#else
10 11
11#if defined(__linux__) 12#if defined(__linux__)
12#if !defined(__APPLE__) && !defined(_AIX) && !defined(__ANDROID__) 13#if !defined(__APPLE__) && !defined(_AIX) && !defined(__ANDROID__)
13#ifndef _7ZIP_AFFINITY_DISABLE 14#ifndef Z7_AFFINITY_DISABLE
14#define _7ZIP_AFFINITY_SUPPORTED 15#define Z7_AFFINITY_SUPPORTED
15// #pragma message(" ==== _7ZIP_AFFINITY_SUPPORTED") 16// #pragma message(" ==== Z7_AFFINITY_SUPPORTED")
16// #define _GNU_SOURCE 17// #define _GNU_SOURCE
17#endif 18#endif
18#endif 19#endif
@@ -33,7 +34,7 @@ WRes Handle_WaitObject(HANDLE h);
33 34
34typedef HANDLE CThread; 35typedef HANDLE CThread;
35 36
36#define Thread_Construct(p) { *(p) = NULL; } 37#define Thread_CONSTRUCT(p) { *(p) = NULL; }
37#define Thread_WasCreated(p) (*(p) != NULL) 38#define Thread_WasCreated(p) (*(p) != NULL)
38#define Thread_Close(p) HandlePtr_Close(p) 39#define Thread_Close(p) HandlePtr_Close(p)
39// #define Thread_Wait(p) Handle_WaitObject(*(p)) 40// #define Thread_Wait(p) Handle_WaitObject(*(p))
@@ -52,42 +53,46 @@ typedef
52 #endif 53 #endif
53 THREAD_FUNC_RET_TYPE; 54 THREAD_FUNC_RET_TYPE;
54 55
56#define THREAD_FUNC_RET_ZERO 0
57
55typedef DWORD_PTR CAffinityMask; 58typedef DWORD_PTR CAffinityMask;
56typedef DWORD_PTR CCpuSet; 59typedef DWORD_PTR CCpuSet;
57 60
58#define CpuSet_Zero(p) { *(p) = 0; } 61#define CpuSet_Zero(p) *(p) = (0)
59#define CpuSet_Set(p, cpu) { *(p) |= ((DWORD_PTR)1 << (cpu)); } 62#define CpuSet_Set(p, cpu) *(p) |= ((DWORD_PTR)1 << (cpu))
60 63
61#else // _WIN32 64#else // _WIN32
62 65
63typedef struct _CThread 66typedef struct
64{ 67{
65 pthread_t _tid; 68 pthread_t _tid;
66 int _created; 69 int _created;
67} CThread; 70} CThread;
68 71
69#define Thread_Construct(p) { (p)->_tid = 0; (p)->_created = 0; } 72#define Thread_CONSTRUCT(p) { (p)->_tid = 0; (p)->_created = 0; }
70#define Thread_WasCreated(p) ((p)->_created != 0) 73#define Thread_WasCreated(p) ((p)->_created != 0)
71WRes Thread_Close(CThread *p); 74WRes Thread_Close(CThread *p);
72// #define Thread_Wait Thread_Wait_Close 75// #define Thread_Wait Thread_Wait_Close
73 76
74typedef void * THREAD_FUNC_RET_TYPE; 77typedef void * THREAD_FUNC_RET_TYPE;
78#define THREAD_FUNC_RET_ZERO NULL
79
75 80
76typedef UInt64 CAffinityMask; 81typedef UInt64 CAffinityMask;
77 82
78#ifdef _7ZIP_AFFINITY_SUPPORTED 83#ifdef Z7_AFFINITY_SUPPORTED
79 84
80typedef cpu_set_t CCpuSet; 85typedef cpu_set_t CCpuSet;
81#define CpuSet_Zero(p) CPU_ZERO(p) 86#define CpuSet_Zero(p) CPU_ZERO(p)
82#define CpuSet_Set(p, cpu) CPU_SET(cpu, p) 87#define CpuSet_Set(p, cpu) CPU_SET(cpu, p)
83#define CpuSet_IsSet(p, cpu) CPU_ISSET(cpu, p) 88#define CpuSet_IsSet(p, cpu) CPU_ISSET(cpu, p)
84 89
85#else 90#else
86 91
87typedef UInt64 CCpuSet; 92typedef UInt64 CCpuSet;
88#define CpuSet_Zero(p) { *(p) = 0; } 93#define CpuSet_Zero(p) *(p) = (0)
89#define CpuSet_Set(p, cpu) { *(p) |= ((UInt64)1 << (cpu)); } 94#define CpuSet_Set(p, cpu) *(p) |= ((UInt64)1 << (cpu))
90#define CpuSet_IsSet(p, cpu) ((*(p) & ((UInt64)1 << (cpu))) != 0) 95#define CpuSet_IsSet(p, cpu) ((*(p) & ((UInt64)1 << (cpu))) != 0)
91 96
92#endif 97#endif
93 98
@@ -95,7 +100,7 @@ typedef UInt64 CCpuSet;
95#endif // _WIN32 100#endif // _WIN32
96 101
97 102
98#define THREAD_FUNC_CALL_TYPE MY_STD_CALL 103#define THREAD_FUNC_CALL_TYPE Z7_STDCALL
99 104
100#if defined(_WIN32) && defined(__GNUC__) 105#if defined(_WIN32) && defined(__GNUC__)
101/* GCC compiler for x86 32-bit uses the rule: 106/* GCC compiler for x86 32-bit uses the rule:
@@ -187,6 +192,7 @@ WRes ManualResetEvent_Create(CManualResetEvent *p, int signaled);
187WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p); 192WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p);
188WRes AutoResetEvent_Create(CAutoResetEvent *p, int signaled); 193WRes AutoResetEvent_Create(CAutoResetEvent *p, int signaled);
189WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p); 194WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p);
195
190WRes Event_Set(CEvent *p); 196WRes Event_Set(CEvent *p);
191WRes Event_Reset(CEvent *p); 197WRes Event_Reset(CEvent *p);
192WRes Event_Wait(CEvent *p); 198WRes Event_Wait(CEvent *p);
@@ -227,6 +233,8 @@ LONG InterlockedIncrement(LONG volatile *addend);
227 233
228#endif // _WIN32 234#endif // _WIN32
229 235
236WRes AutoResetEvent_OptCreate_And_Reset(CAutoResetEvent *p);
237
230EXTERN_C_END 238EXTERN_C_END
231 239
232#endif 240#endif
diff --git a/C/Util/7z/7z.dsp b/C/Util/7z/7z.dsp
index be0f0a7..11e1b03 100644
--- a/C/Util/7z/7z.dsp
+++ b/C/Util/7z/7z.dsp
@@ -42,7 +42,7 @@ RSC=rc.exe
42# PROP Ignore_Export_Lib 0 42# PROP Ignore_Export_Lib 0
43# PROP Target_Dir "" 43# PROP Target_Dir ""
44# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c 44# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
45# ADD CPP /nologo /MD /W4 /WX /GX /O2 /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /FAcs /Yu"Precomp.h" /FD /c 45# ADD CPP /nologo /MD /W4 /WX /GX /O2 /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /D "Z7_PPMD_SUPPORT" /FAcs /Yu"Precomp.h" /FD /c
46# ADD BASE RSC /l 0x419 /d "NDEBUG" 46# ADD BASE RSC /l 0x419 /d "NDEBUG"
47# ADD RSC /l 0x419 /d "NDEBUG" 47# ADD RSC /l 0x419 /d "NDEBUG"
48BSC32=bscmake.exe 48BSC32=bscmake.exe
@@ -67,7 +67,7 @@ LINK32=link.exe
67# PROP Ignore_Export_Lib 0 67# PROP Ignore_Export_Lib 0
68# PROP Target_Dir "" 68# PROP Target_Dir ""
69# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c 69# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
70# ADD CPP /nologo /W4 /WX /Gm /GX /ZI /Od /D "_DEBUG" /D "_SZ_ALLOC_DEBUG2" /D "_SZ_NO_INT_64_A" /D "WIN32" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Yu"Precomp.h" /FD /GZ /c 70# ADD CPP /nologo /W4 /WX /Gm /GX /ZI /Od /D "_DEBUG" /D "_SZ_ALLOC_DEBUG2" /D "_SZ_NO_INT_64_A" /D "WIN32" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /D "Z7_PPMD_SUPPORT" /Yu"Precomp.h" /FD /GZ /c
71# ADD BASE RSC /l 0x419 /d "_DEBUG" 71# ADD BASE RSC /l 0x419 /d "_DEBUG"
72# ADD RSC /l 0x419 /d "_DEBUG" 72# ADD RSC /l 0x419 /d "_DEBUG"
73BSC32=bscmake.exe 73BSC32=bscmake.exe
@@ -145,6 +145,10 @@ SOURCE=..\..\7zTypes.h
145# End Source File 145# End Source File
146# Begin Source File 146# Begin Source File
147 147
148SOURCE=..\..\7zWindows.h
149# End Source File
150# Begin Source File
151
148SOURCE=..\..\Bcj2.c 152SOURCE=..\..\Bcj2.c
149# End Source File 153# End Source File
150# Begin Source File 154# Begin Source File
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
22021-04-29 : Igor Pavlov : Public domain */ 22023-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
38static const ISzAlloc g_Alloc = { SzAlloc, SzFree }; 37static const ISzAlloc g_Alloc = { SzAlloc, SzFree };
38// static const ISzAlloc g_Alloc_temp = { SzAllocTemp, SzFreeTemp };
39 39
40 40
41static void Print(const char *s) 41static 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
70static size_t Utf16_To_Utf8_Calc(const UInt16 *src, const UInt16 *srcLim) 70static 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
165static SRes Utf16_To_Char(CBuf *buf, const UInt16 *s 165static 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
329static LONG TIME_GetBias() 329static 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
365static Int64 Time_FileTimeToUnixTime64(const FILETIME *ft) 365static 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, &times[0]); 411 FILETIME_To_timespec(NULL, &times[0]);
412 FILETIME_To_timespec(mTime, &times[1]); 412 FILETIME_To_timespec(mTime, &times[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
477static void PrintLF() 477static 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
544int MY_CDECL main(int numargs, char *args[]) 544int 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);
diff --git a/C/Util/7z/Precomp.h b/C/Util/7z/Precomp.h
index 588a66f..bc8fa21 100644
--- a/C/Util/7z/Precomp.h
+++ b/C/Util/7z/Precomp.h
@@ -1,8 +1,12 @@
1/* Precomp.h -- StdAfx 1/* Precomp.h -- StdAfx
22013-06-16 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_PRECOMP_H 4#ifndef ZIP7_INC_PRECOMP_H
5#define __7Z_PRECOMP_H 5#define ZIP7_INC_PRECOMP_H
6
7#if defined(_MSC_VER) && _MSC_VER >= 1800
8#pragma warning(disable : 4464) // relative include path contains '..'
9#endif
6 10
7#include "../../Compiler.h" 11#include "../../Compiler.h"
8#include "../../7zTypes.h" 12#include "../../7zTypes.h"
diff --git a/C/Util/7z/makefile b/C/Util/7z/makefile
index 9a49fd5..dfc560e 100644
--- a/C/Util/7z/makefile
+++ b/C/Util/7z/makefile
@@ -1,4 +1,4 @@
1CFLAGS = $(CFLAGS) -D_7ZIP_PPMD_SUPPPORT 1CFLAGS = $(CFLAGS) -DZ7_PPMD_SUPPORT -DZ7_EXTRACT_ONLY
2 2
3PROG = 7zDec.exe 3PROG = 7zDec.exe
4 4
diff --git a/C/Util/7z/makefile.gcc b/C/Util/7z/makefile.gcc
index 4263d67..f48d362 100644
--- a/C/Util/7z/makefile.gcc
+++ b/C/Util/7z/makefile.gcc
@@ -1,6 +1,6 @@
1PROG = 7zdec 1PROG = 7zdec
2 2
3LOCAL_FLAGS = -D_7ZIP_PPMD_SUPPPORT 3LOCAL_FLAGS = -DZ7_PPMD_SUPPORT -DZ7_EXTRACT_ONLY
4 4
5include ../../../CPP/7zip/LzmaDec_gcc.mak 5include ../../../CPP/7zip/LzmaDec_gcc.mak
6 6
@@ -19,8 +19,6 @@ OBJS = \
19 $O/Ppmd7Dec.o \ 19 $O/Ppmd7Dec.o \
20 $O/7zCrc.o \ 20 $O/7zCrc.o \
21 $O/7zCrcOpt.o \ 21 $O/7zCrcOpt.o \
22 $O/Sha256.o \
23 $O/Sha256Opt.o \
24 $O/7zAlloc.o \ 22 $O/7zAlloc.o \
25 $O/7zArcIn.o \ 23 $O/7zArcIn.o \
26 $O/7zBuf.o \ 24 $O/7zBuf.o \
diff --git a/C/Util/7zipInstall/7zipInstall.c b/C/Util/7zipInstall/7zipInstall.c
index 2649734..7f5fd19 100644
--- a/C/Util/7zipInstall/7zipInstall.c
+++ b/C/Util/7zipInstall/7zipInstall.c
@@ -1,16 +1,31 @@
1/* 7zipInstall.c - 7-Zip Installer 1/* 7zipInstall.c - 7-Zip Installer
22022-07-15 : Igor Pavlov : Public domain */ 22023-04-04 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#define SZ_ERROR_ABORT 100 6#define SZ_ERROR_ABORT 100
7 7
8#ifdef _MSC_VER 8#include "../../7zWindows.h"
9
10#if defined(_MSC_VER) && _MSC_VER < 1600
9#pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union 11#pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union
10#endif 12#endif
11 13
12#include <windows.h> 14#ifdef Z7_OLD_WIN_SDK
15struct IShellView;
16#define SHFOLDERAPI EXTERN_C DECLSPEC_IMPORT HRESULT STDAPICALLTYPE
17SHFOLDERAPI SHGetFolderPathW(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath);
18#define BIF_NEWDIALOGSTYLE 0x0040 // Use the new dialog layout with the ability to resize
19typedef enum {
20 SHGFP_TYPE_CURRENT = 0, // current value for user, verify it exists
21 SHGFP_TYPE_DEFAULT = 1, // default value, may not exist
22} SHGFP_TYPE;
23#endif
24#if defined(__MINGW32__) || defined(__MINGW64__)
25#include <shlobj.h>
26#else
13#include <ShlObj.h> 27#include <ShlObj.h>
28#endif
14 29
15#include "../../7z.h" 30#include "../../7z.h"
16#include "../../7zAlloc.h" 31#include "../../7zAlloc.h"
@@ -22,40 +37,46 @@
22 37
23#include "resource.h" 38#include "resource.h"
24 39
25#if defined(__GNUC__) && (__GNUC__ >= 8) 40#if (defined(__GNUC__) && (__GNUC__ >= 8)) || defined(__clang__)
26 #pragma GCC diagnostic ignored "-Wcast-function-type" 41 // #pragma GCC diagnostic ignored "-Wcast-function-type"
42#endif
43
44#if defined(__clang__) || defined(__GNUC__)
45typedef void (*Z7_voidFunction)(void);
46#define MY_CAST_FUNC (Z7_voidFunction)
47#elif defined(_MSC_VER) && _MSC_VER > 1920
48#define MY_CAST_FUNC (void *)
49// #pragma warning(disable : 4191) // 'type cast': unsafe conversion from 'FARPROC' to 'void (__cdecl *)()'
50#else
51#define MY_CAST_FUNC
27#endif 52#endif
28 53
29#define LLL_(quote) L##quote 54#define LLL_(quote) L##quote
30#define LLL(quote) LLL_(quote) 55#define LLL(quote) LLL_(quote)
31 56
32#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
33
34#define wcscat lstrcatW 57#define wcscat lstrcatW
35#define wcslen lstrlenW 58#define wcslen (size_t)lstrlenW
36#define wcscpy lstrcpyW 59#define wcscpy lstrcpyW
37// wcsncpy() and lstrcpynW() work differently. We don't use them. 60// wcsncpy() and lstrcpynW() work differently. We don't use them.
38 61
39
40#define kInputBufSize ((size_t)1 << 18) 62#define kInputBufSize ((size_t)1 << 18)
41 63
42 64#define Z7_7ZIP_CUR_VER ((MY_VER_MAJOR << 16) | MY_VER_MINOR)
43#define _7ZIP_CUR_VER ((MY_VER_MAJOR << 16) | MY_VER_MINOR) 65#define Z7_7ZIP_DLL_VER_COMPAT ((16 << 16) | 3)
44#define _7ZIP_DLL_VER_COMPAT ((16 << 16) | 3)
45 66
46static LPCSTR const k_7zip = "7-Zip"; 67static LPCSTR const k_7zip = "7-Zip";
47 68
48static LPCWSTR const k_Reg_Software_7zip = L"Software\\7-Zip"; 69static LPCWSTR const k_Reg_Software_7zip = L"Software\\7-Zip";
49 70
50// #define _64BIT_INSTALLER 1 71// #define Z7_64BIT_INSTALLER 1
51 72
52#ifdef _WIN64 73#ifdef _WIN64
53 #define _64BIT_INSTALLER 1 74 #define Z7_64BIT_INSTALLER 1
54#endif 75#endif
55 76
56#define k_7zip_with_Ver_base L"7-Zip " LLL(MY_VERSION) 77#define k_7zip_with_Ver_base L"7-Zip " LLL(MY_VERSION)
57 78
58#ifdef _64BIT_INSTALLER 79#ifdef Z7_64BIT_INSTALLER
59 80
60 // #define USE_7ZIP_32_DLL 81 // #define USE_7ZIP_32_DLL
61 82
@@ -84,14 +105,14 @@ static LPCWSTR const k_7zip_Setup = k_7zip_with_Ver L" Setup";
84static LPCWSTR const k_Reg_Path = L"Path"; 105static LPCWSTR const k_Reg_Path = L"Path";
85 106
86static LPCWSTR const k_Reg_Path32 = L"Path" 107static LPCWSTR const k_Reg_Path32 = L"Path"
87 #ifdef _64BIT_INSTALLER 108 #ifdef Z7_64BIT_INSTALLER
88 L"64" 109 L"64"
89 #else 110 #else
90 L"32" 111 L"32"
91 #endif 112 #endif
92 ; 113 ;
93 114
94#if defined(_64BIT_INSTALLER) && !defined(_WIN64) 115#if defined(Z7_64BIT_INSTALLER) && !defined(_WIN64)
95 #define k_Reg_WOW_Flag KEY_WOW64_64KEY 116 #define k_Reg_WOW_Flag KEY_WOW64_64KEY
96#else 117#else
97 #define k_Reg_WOW_Flag 0 118 #define k_Reg_WOW_Flag 0
@@ -126,8 +147,6 @@ static WCHAR cmdError[MAX_PATH + 4];
126static WCHAR path[MAX_PATH * 2 + 40]; 147static WCHAR path[MAX_PATH * 2 + 40];
127 148
128 149
129// #define MAKE_CHAR_UPPER(c) ((((c) >= 'a' && (c) <= 'z') ? (c) -= 0x20 : (c)))
130
131 150
132static void CpyAscii(wchar_t *dest, const char *s) 151static void CpyAscii(wchar_t *dest, const char *s)
133{ 152{
@@ -200,9 +219,12 @@ static DWORD GetFileVersion(LPCWSTR s)
200 return 0; 219 return 0;
201 } 220 }
202 221
203 my_GetFileVersionInfoSizeW = (Func_GetFileVersionInfoSizeW)GetProcAddress(g_version_dll_hModule, "GetFileVersionInfoSizeW"); 222 my_GetFileVersionInfoSizeW = (Func_GetFileVersionInfoSizeW) MY_CAST_FUNC GetProcAddress(g_version_dll_hModule,
204 my_GetFileVersionInfoW = (Func_GetFileVersionInfoW)GetProcAddress(g_version_dll_hModule, "GetFileVersionInfoW"); 223 "GetFileVersionInfoSizeW");
205 my_VerQueryValueW = (Func_VerQueryValueW)GetProcAddress(g_version_dll_hModule, "VerQueryValueW"); 224 my_GetFileVersionInfoW = (Func_GetFileVersionInfoW) MY_CAST_FUNC GetProcAddress(g_version_dll_hModule,
225 "GetFileVersionInfoW");
226 my_VerQueryValueW = (Func_VerQueryValueW) MY_CAST_FUNC GetProcAddress(g_version_dll_hModule,
227 "VerQueryValueW");
206 228
207 if (!my_GetFileVersionInfoSizeW 229 if (!my_GetFileVersionInfoSizeW
208 || !my_GetFileVersionInfoW 230 || !my_GetFileVersionInfoW
@@ -253,7 +275,7 @@ static int ReverseFind_PathSepar(const wchar_t *s)
253 } 275 }
254} 276}
255 277
256static WRes CreateComplexDir() 278static WRes CreateComplexDir(void)
257{ 279{
258 WCHAR s[MAX_PATH + 10]; 280 WCHAR s[MAX_PATH + 10];
259 281
@@ -287,7 +309,7 @@ static WRes CreateComplexDir()
287 { 309 {
288 size_t len = wcslen(s); 310 size_t len = wcslen(s);
289 { 311 {
290 int pos = ReverseFind_PathSepar(s); 312 const int pos = ReverseFind_PathSepar(s);
291 if (pos < 0) 313 if (pos < 0)
292 return wres; 314 return wres;
293 if ((unsigned)pos < prefixSize) 315 if ((unsigned)pos < prefixSize)
@@ -297,7 +319,7 @@ static WRes CreateComplexDir()
297 if (len == 1) 319 if (len == 1)
298 return 0; 320 return 0;
299 s[pos] = 0; 321 s[pos] = 0;
300 len = pos; 322 len = (unsigned)pos;
301 } 323 }
302 } 324 }
303 325
@@ -309,7 +331,7 @@ static WRes CreateComplexDir()
309 break; 331 break;
310 if (wres == ERROR_ALREADY_EXISTS) 332 if (wres == ERROR_ALREADY_EXISTS)
311 { 333 {
312 DWORD attrib = GetFileAttributesW(s); 334 const DWORD attrib = GetFileAttributesW(s);
313 if (attrib != INVALID_FILE_ATTRIBUTES) 335 if (attrib != INVALID_FILE_ATTRIBUTES)
314 if ((attrib & FILE_ATTRIBUTE_DIRECTORY) == 0) 336 if ((attrib & FILE_ATTRIBUTE_DIRECTORY) == 0)
315 return ERROR_ALREADY_EXISTS; 337 return ERROR_ALREADY_EXISTS;
@@ -323,7 +345,7 @@ static WRes CreateComplexDir()
323 345
324 for (;;) 346 for (;;)
325 { 347 {
326 size_t pos = wcslen(s); 348 const size_t pos = wcslen(s);
327 if (pos >= len) 349 if (pos >= len)
328 return 0; 350 return 0;
329 s[pos] = CHAR_PATH_SEPARATOR; 351 s[pos] = CHAR_PATH_SEPARATOR;
@@ -339,7 +361,7 @@ static int MyRegistry_QueryString(HKEY hKey, LPCWSTR name, LPWSTR dest)
339{ 361{
340 DWORD cnt = MAX_PATH * sizeof(name[0]); 362 DWORD cnt = MAX_PATH * sizeof(name[0]);
341 DWORD type = 0; 363 DWORD type = 0;
342 LONG res = RegQueryValueExW(hKey, name, NULL, &type, (LPBYTE)dest, (DWORD *)&cnt); 364 const LONG res = RegQueryValueExW(hKey, name, NULL, &type, (LPBYTE)dest, &cnt);
343 if (type != REG_SZ) 365 if (type != REG_SZ)
344 return False; 366 return False;
345 return res == ERROR_SUCCESS; 367 return res == ERROR_SUCCESS;
@@ -348,11 +370,11 @@ static int MyRegistry_QueryString(HKEY hKey, LPCWSTR name, LPWSTR dest)
348static int MyRegistry_QueryString2(HKEY hKey, LPCWSTR keyName, LPCWSTR valName, LPWSTR dest) 370static int MyRegistry_QueryString2(HKEY hKey, LPCWSTR keyName, LPCWSTR valName, LPWSTR dest)
349{ 371{
350 HKEY key = 0; 372 HKEY key = 0;
351 LONG res = RegOpenKeyExW(hKey, keyName, 0, KEY_READ | k_Reg_WOW_Flag, &key); 373 const LONG res = RegOpenKeyExW(hKey, keyName, 0, KEY_READ | k_Reg_WOW_Flag, &key);
352 if (res != ERROR_SUCCESS) 374 if (res != ERROR_SUCCESS)
353 return False; 375 return False;
354 { 376 {
355 BoolInt res2 = MyRegistry_QueryString(key, valName, dest); 377 const BoolInt res2 = MyRegistry_QueryString(key, valName, dest);
356 RegCloseKey(key); 378 RegCloseKey(key);
357 return res2; 379 return res2;
358 } 380 }
@@ -550,7 +572,7 @@ static void NormalizePrefix(WCHAR *s)
550 572
551 for (;; i++) 573 for (;; i++)
552 { 574 {
553 wchar_t c = s[i]; 575 const wchar_t c = s[i];
554 if (c == 0) 576 if (c == 0)
555 break; 577 break;
556 if (c == '/') 578 if (c == '/')
@@ -587,7 +609,7 @@ static LPCWSTR FindSubString(LPCWSTR s1, const char *s2)
587 return NULL; 609 return NULL;
588 for (i = 0;; i++) 610 for (i = 0;; i++)
589 { 611 {
590 Byte b = s2[i]; 612 const char b = s2[i];
591 if (b == 0) 613 if (b == 0)
592 return s1; 614 return s1;
593 if (MyWCharLower_Ascii(s1[i]) != (Byte)MyCharLower_Ascii(b)) 615 if (MyWCharLower_Ascii(s1[i]) != (Byte)MyCharLower_Ascii(b))
@@ -610,7 +632,7 @@ static void Set7zipPostfix(WCHAR *s)
610 632
611static int Install(void); 633static int Install(void);
612 634
613static void OnClose() 635static void OnClose(void)
614{ 636{
615 if (g_Install_was_Pressed && !g_Finished) 637 if (g_Install_was_Pressed && !g_Finished)
616 { 638 {
@@ -624,7 +646,13 @@ static void OnClose()
624 g_HWND = NULL; 646 g_HWND = NULL;
625} 647}
626 648
627static INT_PTR CALLBACK MyDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 649static
650#ifdef Z7_OLD_WIN_SDK
651 BOOL
652#else
653 INT_PTR
654#endif
655CALLBACK MyDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
628{ 656{
629 // UNUSED_VAR(hwnd) 657 // UNUSED_VAR(hwnd)
630 UNUSED_VAR(lParam) 658 UNUSED_VAR(lParam)
@@ -730,7 +758,7 @@ static LONG SetRegKey_Path2(HKEY parentKey)
730 return res; 758 return res;
731} 759}
732 760
733static void SetRegKey_Path() 761static void SetRegKey_Path(void)
734{ 762{
735 SetRegKey_Path2(HKEY_CURRENT_USER); 763 SetRegKey_Path2(HKEY_CURRENT_USER);
736 SetRegKey_Path2(HKEY_LOCAL_MACHINE); 764 SetRegKey_Path2(HKEY_LOCAL_MACHINE);
@@ -828,7 +856,7 @@ static void SetShellProgramsGroup(HWND hwndOwner)
828static LPCWSTR const k_Shell_Approved = L"Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"; 856static LPCWSTR const k_Shell_Approved = L"Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved";
829static LPCWSTR const k_7zip_ShellExtension = L"7-Zip Shell Extension"; 857static LPCWSTR const k_7zip_ShellExtension = L"7-Zip Shell Extension";
830 858
831static void WriteCLSID() 859static void WriteCLSID(void)
832{ 860{
833 HKEY destKey; 861 HKEY destKey;
834 LONG res; 862 LONG res;
@@ -879,12 +907,12 @@ static LPCSTR const k_ShellEx_Items[] =
879 , "Drive\\shellex\\DragDropHandlers" 907 , "Drive\\shellex\\DragDropHandlers"
880}; 908};
881 909
882static void WriteShellEx() 910static void WriteShellEx(void)
883{ 911{
884 unsigned i; 912 unsigned i;
885 WCHAR destPath[MAX_PATH + 40]; 913 WCHAR destPath[MAX_PATH + 40];
886 914
887 for (i = 0; i < ARRAY_SIZE(k_ShellEx_Items); i++) 915 for (i = 0; i < Z7_ARRAY_SIZE(k_ShellEx_Items); i++)
888 { 916 {
889 CpyAscii(destPath, k_ShellEx_Items[i]); 917 CpyAscii(destPath, k_ShellEx_Items[i]);
890 CatAscii(destPath, "\\7-Zip"); 918 CatAscii(destPath, "\\7-Zip");
@@ -968,7 +996,7 @@ static const wchar_t *GetCmdParam(const wchar_t *s)
968 quoteMode = !quoteMode; 996 quoteMode = !quoteMode;
969 continue; 997 continue;
970 } 998 }
971 if (pos >= ARRAY_SIZE(cmd) - 1) 999 if (pos >= Z7_ARRAY_SIZE(cmd) - 1)
972 exit(1); 1000 exit(1);
973 cmd[pos++] = c; 1001 cmd[pos++] = c;
974 } 1002 }
@@ -1026,7 +1054,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1026 for (;;) 1054 for (;;)
1027 { 1055 {
1028 { 1056 {
1029 wchar_t c = *s; 1057 const wchar_t c = *s;
1030 if (c == 0) 1058 if (c == 0)
1031 break; 1059 break;
1032 if (c == ' ') 1060 if (c == ' ')
@@ -1070,11 +1098,12 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1070 } 1098 }
1071 } 1099 }
1072 1100
1073 #if defined(_64BIT_INSTALLER) && !defined(_WIN64) 1101 #if defined(Z7_64BIT_INSTALLER) && !defined(_WIN64)
1074 { 1102 {
1075 BOOL isWow64 = FALSE; 1103 BOOL isWow64 = FALSE;
1076 Func_IsWow64Process func_IsWow64Process = (Func_IsWow64Process) 1104 const Func_IsWow64Process func_IsWow64Process = (Func_IsWow64Process)
1077 GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "IsWow64Process"); 1105 MY_CAST_FUNC GetProcAddress(GetModuleHandleW(L"kernel32.dll"),
1106 "IsWow64Process");
1078 1107
1079 if (func_IsWow64Process) 1108 if (func_IsWow64Process)
1080 func_IsWow64Process(GetCurrentProcess(), &isWow64); 1109 func_IsWow64Process(GetCurrentProcess(), &isWow64);
@@ -1093,7 +1122,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1093 { 1122 {
1094 HKEY key = 0; 1123 HKEY key = 0;
1095 BoolInt ok = False; 1124 BoolInt ok = False;
1096 LONG res = RegOpenKeyExW(HKEY_CURRENT_USER, k_Reg_Software_7zip, 0, KEY_READ | k_Reg_WOW_Flag, &key); 1125 const LONG res = RegOpenKeyExW(HKEY_CURRENT_USER, k_Reg_Software_7zip, 0, KEY_READ | k_Reg_WOW_Flag, &key);
1097 if (res == ERROR_SUCCESS) 1126 if (res == ERROR_SUCCESS)
1098 { 1127 {
1099 ok = MyRegistry_QueryString(key, k_Reg_Path32, path); 1128 ok = MyRegistry_QueryString(key, k_Reg_Path32, path);
@@ -1109,7 +1138,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1109 CpyAscii(path, "\\Program Files\\"); 1138 CpyAscii(path, "\\Program Files\\");
1110 #else 1139 #else
1111 1140
1112 #ifdef _64BIT_INSTALLER 1141 #ifdef Z7_64BIT_INSTALLER
1113 { 1142 {
1114 DWORD ttt = GetEnvironmentVariableW(L"ProgramW6432", path, MAX_PATH); 1143 DWORD ttt = GetEnvironmentVariableW(L"ProgramW6432", path, MAX_PATH);
1115 if (ttt == 0 || ttt > MAX_PATH) 1144 if (ttt == 0 || ttt > MAX_PATH)
@@ -1150,7 +1179,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1150 return 1; 1179 return 1;
1151 1180
1152 { 1181 {
1153 HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); 1182 const HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
1154 // SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hIcon); 1183 // SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hIcon);
1155 SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon); 1184 SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon);
1156 } 1185 }
@@ -1244,7 +1273,7 @@ static int Install(void)
1244 allocTempImp.Free = SzFreeTemp; 1273 allocTempImp.Free = SzFreeTemp;
1245 1274
1246 { 1275 {
1247 DWORD len = GetModuleFileNameW(NULL, sfxPath, MAX_PATH); 1276 const DWORD len = GetModuleFileNameW(NULL, sfxPath, MAX_PATH);
1248 if (len == 0 || len > MAX_PATH) 1277 if (len == 0 || len > MAX_PATH)
1249 return 1; 1278 return 1;
1250 } 1279 }
@@ -1286,7 +1315,7 @@ if (res == SZ_OK)
1286 1315
1287 for (;;) 1316 for (;;)
1288 { 1317 {
1289 wchar_t c = path[i++]; 1318 const wchar_t c = path[i++];
1290 if (c == 0) 1319 if (c == 0)
1291 break; 1320 break;
1292 if (c != ' ') 1321 if (c != ' ')
@@ -1318,7 +1347,7 @@ if (res == SZ_OK)
1318 { 1347 {
1319 lookStream.bufSize = kInputBufSize; 1348 lookStream.bufSize = kInputBufSize;
1320 lookStream.realStream = &archiveStream.vt; 1349 lookStream.realStream = &archiveStream.vt;
1321 LookToRead2_Init(&lookStream); 1350 LookToRead2_INIT(&lookStream)
1322 } 1351 }
1323 } 1352 }
1324 1353
@@ -1372,7 +1401,7 @@ if (res == SZ_OK)
1372 } 1401 }
1373 1402
1374 { 1403 {
1375 size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL); 1404 const size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL);
1376 if (len >= MAX_PATH) 1405 if (len >= MAX_PATH)
1377 { 1406 {
1378 res = SZ_ERROR_FAIL; 1407 res = SZ_ERROR_FAIL;
@@ -1468,8 +1497,8 @@ if (res == SZ_OK)
1468 #endif 1497 #endif
1469 ) 1498 )
1470 { 1499 {
1471 DWORD ver = GetFileVersion(path); 1500 const DWORD ver = GetFileVersion(path);
1472 fileLevel = ((ver < _7ZIP_DLL_VER_COMPAT || ver > _7ZIP_CUR_VER) ? 2 : 1); 1501 fileLevel = ((ver < Z7_7ZIP_DLL_VER_COMPAT || ver > Z7_7ZIP_CUR_VER) ? 2 : 1);
1473 tempIndex++; 1502 tempIndex++;
1474 continue; 1503 continue;
1475 } 1504 }
@@ -1537,7 +1566,7 @@ if (res == SZ_OK)
1537 #endif 1566 #endif
1538 1567
1539 { 1568 {
1540 SRes winRes2 = File_Close(&outFile); 1569 const WRes winRes2 = File_Close(&outFile);
1541 if (res != SZ_OK) 1570 if (res != SZ_OK)
1542 break; 1571 break;
1543 if (winRes2 != 0) 1572 if (winRes2 != 0)
diff --git a/C/Util/7zipInstall/7zipInstall.dsp b/C/Util/7zipInstall/7zipInstall.dsp
index d3b5c4c..4c6ea4d 100644
--- a/C/Util/7zipInstall/7zipInstall.dsp
+++ b/C/Util/7zipInstall/7zipInstall.dsp
@@ -152,6 +152,10 @@ SOURCE=..\..\7zVersion.h
152# End Source File 152# End Source File
153# Begin Source File 153# Begin Source File
154 154
155SOURCE=..\..\7zWindows.h
156# End Source File
157# Begin Source File
158
155SOURCE=..\..\Bcj2.c 159SOURCE=..\..\Bcj2.c
156# End Source File 160# End Source File
157# Begin Source File 161# Begin Source File
@@ -220,6 +224,10 @@ SOURCE=..\..\LzmaDec.h
220# PROP Default_Filter "" 224# PROP Default_Filter ""
221# Begin Source File 225# Begin Source File
222 226
227SOURCE=..\..\Compiler.h
228# End Source File
229# Begin Source File
230
223SOURCE=.\Precomp.c 231SOURCE=.\Precomp.c
224# ADD CPP /Yc"Precomp.h" 232# ADD CPP /Yc"Precomp.h"
225# End Source File 233# End Source File
diff --git a/C/Util/7zipInstall/Precomp.h b/C/Util/7zipInstall/Precomp.h
index 4c90d47..bc8fa21 100644
--- a/C/Util/7zipInstall/Precomp.h
+++ b/C/Util/7zipInstall/Precomp.h
@@ -1,11 +1,14 @@
1/* Precomp.h -- StdAfx 1/* Precomp.h -- StdAfx
22015-05-24 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_PRECOMP_H 4#ifndef ZIP7_INC_PRECOMP_H
5#define __7Z_PRECOMP_H 5#define ZIP7_INC_PRECOMP_H
6 6
7#include "../../Compiler.h" 7#if defined(_MSC_VER) && _MSC_VER >= 1800
8#pragma warning(disable : 4464) // relative include path contains '..'
9#endif
8 10
11#include "../../Compiler.h"
9#include "../../7zTypes.h" 12#include "../../7zTypes.h"
10 13
11#endif 14#endif
diff --git a/C/Util/7zipInstall/makefile b/C/Util/7zipInstall/makefile
index ab8893a..18e2783 100644
--- a/C/Util/7zipInstall/makefile
+++ b/C/Util/7zipInstall/makefile
@@ -1,15 +1,16 @@
1PROG = 7zipInstall.exe 1PROG = 7zipInstall.exe
2MY_FIXED = 1 2MY_FIXED = 1
3 3
4!IFDEF _64BIT_INSTALLER 4!IFDEF Z7_64BIT_INSTALLER
5CFLAGS = $(CFLAGS) -D_64BIT_INSTALLER 5CFLAGS = $(CFLAGS) -DZ7_64BIT_INSTALLER
6!ENDIF 6!ENDIF
7 7
8CFLAGS = $(CFLAGS) -D_LZMA_SIZE_OPT
9
10CFLAGS = $(CFLAGS) \ 8CFLAGS = $(CFLAGS) \
11 -D_7Z_NO_METHOD_LZMA2 \ 9 -DZ7_LZMA_SIZE_OPT \
12 -D_7Z_NO_METHODS_FILTERS 10 -DZ7_NO_METHOD_LZMA2 \
11 -DZ7_NO_METHODS_FILTERS \
12 -DZ7_USE_NATIVE_BRANCH_FILTER \
13 -DZ7_EXTRACT_ONLY \
13 14
14MAIN_OBJS = \ 15MAIN_OBJS = \
15 $O\7zipInstall.obj \ 16 $O\7zipInstall.obj \
@@ -25,6 +26,7 @@ C_OBJS = \
25 $O\7zDec.obj \ 26 $O\7zDec.obj \
26 $O\7zStream.obj \ 27 $O\7zStream.obj \
27 $O\Bcj2.obj \ 28 $O\Bcj2.obj \
29 $O\Bra.obj \
28 $O\CpuArch.obj \ 30 $O\CpuArch.obj \
29 $O\DllSecur.obj \ 31 $O\DllSecur.obj \
30 $O\LzmaDec.obj \ 32 $O\LzmaDec.obj \
diff --git a/C/Util/7zipUninstall/7zipUninstall.c b/C/Util/7zipUninstall/7zipUninstall.c
index c8e8a89..8bc18b3 100644
--- a/C/Util/7zipUninstall/7zipUninstall.c
+++ b/C/Util/7zipUninstall/7zipUninstall.c
@@ -3,40 +3,64 @@
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
6#ifdef _MSC_VER 6// #define SZ_ERROR_ABORT 100
7
8#include "../../7zWindows.h"
9
10#if defined(_MSC_VER) && _MSC_VER < 1600
7#pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union 11#pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union
8#pragma warning(disable : 4011) // vs2010: identifier truncated to _CRT_SECURE_CPP_OVERLOAD_SECURE
9#endif 12#endif
10 13
11// #define SZ_ERROR_ABORT 100 14#ifdef Z7_OLD_WIN_SDK
12 15struct IShellView;
13#include <windows.h> 16#define SHFOLDERAPI EXTERN_C DECLSPEC_IMPORT HRESULT STDAPICALLTYPE
17SHFOLDERAPI SHGetFolderPathW(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath);
18#define BIF_NEWDIALOGSTYLE 0x0040 // Use the new dialog layout with the ability to resize
19typedef enum {
20 SHGFP_TYPE_CURRENT = 0, // current value for user, verify it exists
21 SHGFP_TYPE_DEFAULT = 1, // default value, may not exist
22} SHGFP_TYPE;
23#endif
24#if defined(__MINGW32__) || defined(__MINGW64__)
25#include <shlobj.h>
26#else
14#include <ShlObj.h> 27#include <ShlObj.h>
28#endif
15 29
16#include "../../7zVersion.h" 30#include "../../7zVersion.h"
17 31
18#include "resource.h" 32#include "resource.h"
19 33
20#if defined(__GNUC__) && (__GNUC__ >= 8) 34#if (defined(__GNUC__) && (__GNUC__ >= 8)) || defined(__clang__)
21 #pragma GCC diagnostic ignored "-Wcast-function-type" 35 // #pragma GCC diagnostic ignored "-Wcast-function-type"
22#endif 36#endif
23 37
38#if defined(_MSC_VER) && _MSC_VER > 1920
39#define MY_CAST_FUNC (void *)
40// #pragma warning(disable : 4191) // 'type cast': unsafe conversion from 'FARPROC' to 'void (__cdecl *)()'
41#else
42#define MY_CAST_FUNC
43#endif
44
45
24#define LLL_(quote) L##quote 46#define LLL_(quote) L##quote
25#define LLL(quote) LLL_(quote) 47#define LLL(quote) LLL_(quote)
26 48
27#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 49#define wcscat lstrcatW
50#define wcslen (size_t)lstrlenW
51#define wcscpy lstrcpyW
28 52
29// static LPCWSTR const k_7zip = L"7-Zip"; 53// static LPCWSTR const k_7zip = L"7-Zip";
30 54
31// #define _64BIT_INSTALLER 1 55// #define Z7_64BIT_INSTALLER 1
32 56
33#ifdef _WIN64 57#ifdef _WIN64
34 #define _64BIT_INSTALLER 1 58 #define Z7_64BIT_INSTALLER 1
35#endif 59#endif
36 60
37#define k_7zip_with_Ver_base L"7-Zip " LLL(MY_VERSION) 61#define k_7zip_with_Ver_base L"7-Zip " LLL(MY_VERSION)
38 62
39#ifdef _64BIT_INSTALLER 63#ifdef Z7_64BIT_INSTALLER
40 64
41 // #define USE_7ZIP_32_DLL 65 // #define USE_7ZIP_32_DLL
42 66
@@ -64,14 +88,14 @@ static LPCWSTR const k_Reg_Software_7zip = L"Software\\7-Zip";
64static LPCWSTR const k_Reg_Path = L"Path"; 88static LPCWSTR const k_Reg_Path = L"Path";
65 89
66static LPCWSTR const k_Reg_Path32 = L"Path" 90static LPCWSTR const k_Reg_Path32 = L"Path"
67 #ifdef _64BIT_INSTALLER 91 #ifdef Z7_64BIT_INSTALLER
68 L"64" 92 L"64"
69 #else 93 #else
70 L"32" 94 L"32"
71 #endif 95 #endif
72 ; 96 ;
73 97
74#if defined(_64BIT_INSTALLER) && !defined(_WIN64) 98#if defined(Z7_64BIT_INSTALLER) && !defined(_WIN64)
75 #define k_Reg_WOW_Flag KEY_WOW64_64KEY 99 #define k_Reg_WOW_Flag KEY_WOW64_64KEY
76#else 100#else
77 #define k_Reg_WOW_Flag 0 101 #define k_Reg_WOW_Flag 0
@@ -116,14 +140,14 @@ static WCHAR copyPath[MAX_PATH * 2 + 40];
116 140
117static LPCWSTR const kUninstallExe = L"Uninstall.exe"; 141static LPCWSTR const kUninstallExe = L"Uninstall.exe";
118 142
119#define MAKE_CHAR_UPPER(c) ((((c) >= 'a' && (c) <= 'z') ? (c) -= 0x20 : (c))) 143#define MAKE_CHAR_UPPER(c) ((((c) >= 'a' && (c) <= 'z') ? (c) - 0x20 : (c)))
120 144
121 145
122static void CpyAscii(wchar_t *dest, const char *s) 146static void CpyAscii(wchar_t *dest, const char *s)
123{ 147{
124 for (;;) 148 for (;;)
125 { 149 {
126 Byte b = (Byte)*s++; 150 const Byte b = (Byte)*s++;
127 *dest++ = b; 151 *dest++ = b;
128 if (b == 0) 152 if (b == 0)
129 return; 153 return;
@@ -173,7 +197,7 @@ static BoolInt IsString1PrefixedByString2_NoCase(const wchar_t *s1, const wchar_
173 for (;;) 197 for (;;)
174 { 198 {
175 wchar_t c1; 199 wchar_t c1;
176 wchar_t c2 = *s2++; 200 const wchar_t c2 = *s2++;
177 if (c2 == 0) 201 if (c2 == 0)
178 return True; 202 return True;
179 c1 = *s1++; 203 c1 = *s1++;
@@ -184,7 +208,7 @@ static BoolInt IsString1PrefixedByString2_NoCase(const wchar_t *s1, const wchar_
184 208
185static void NormalizePrefix(WCHAR *s) 209static void NormalizePrefix(WCHAR *s)
186{ 210{
187 size_t len = wcslen(s); 211 const size_t len = wcslen(s);
188 if (len != 0) 212 if (len != 0)
189 if (s[len - 1] != WCHAR_PATH_SEPARATOR) 213 if (s[len - 1] != WCHAR_PATH_SEPARATOR)
190 { 214 {
@@ -197,7 +221,7 @@ static int MyRegistry_QueryString(HKEY hKey, LPCWSTR name, LPWSTR dest)
197{ 221{
198 DWORD cnt = MAX_PATH * sizeof(name[0]); 222 DWORD cnt = MAX_PATH * sizeof(name[0]);
199 DWORD type = 0; 223 DWORD type = 0;
200 LONG res = RegQueryValueExW(hKey, name, NULL, &type, (LPBYTE)dest, (DWORD *)&cnt); 224 const LONG res = RegQueryValueExW(hKey, name, NULL, &type, (LPBYTE)dest, &cnt);
201 if (type != REG_SZ) 225 if (type != REG_SZ)
202 return False; 226 return False;
203 return res == ERROR_SUCCESS; 227 return res == ERROR_SUCCESS;
@@ -206,11 +230,11 @@ static int MyRegistry_QueryString(HKEY hKey, LPCWSTR name, LPWSTR dest)
206static int MyRegistry_QueryString2(HKEY hKey, LPCWSTR keyName, LPCWSTR valName, LPWSTR dest) 230static int MyRegistry_QueryString2(HKEY hKey, LPCWSTR keyName, LPCWSTR valName, LPWSTR dest)
207{ 231{
208 HKEY key = 0; 232 HKEY key = 0;
209 LONG res = RegOpenKeyExW(hKey, keyName, 0, KEY_READ | k_Reg_WOW_Flag, &key); 233 const LONG res = RegOpenKeyExW(hKey, keyName, 0, KEY_READ | k_Reg_WOW_Flag, &key);
210 if (res != ERROR_SUCCESS) 234 if (res != ERROR_SUCCESS)
211 return False; 235 return False;
212 { 236 {
213 BoolInt res2 = MyRegistry_QueryString(key, valName, dest); 237 const BoolInt res2 = MyRegistry_QueryString(key, valName, dest);
214 RegCloseKey(key); 238 RegCloseKey(key);
215 return res2; 239 return res2;
216 } 240 }
@@ -237,11 +261,11 @@ static LONG MyRegistry_DeleteKey(HKEY parentKey, LPCWSTR name)
237static int MyRegistry_QueryString2_32(HKEY hKey, LPCWSTR keyName, LPCWSTR valName, LPWSTR dest) 261static int MyRegistry_QueryString2_32(HKEY hKey, LPCWSTR keyName, LPCWSTR valName, LPWSTR dest)
238{ 262{
239 HKEY key = 0; 263 HKEY key = 0;
240 LONG res = RegOpenKeyExW(hKey, keyName, 0, KEY_READ | k_Reg_WOW_Flag_32, &key); 264 const LONG res = RegOpenKeyExW(hKey, keyName, 0, KEY_READ | k_Reg_WOW_Flag_32, &key);
241 if (res != ERROR_SUCCESS) 265 if (res != ERROR_SUCCESS)
242 return False; 266 return False;
243 { 267 {
244 BoolInt res2 = MyRegistry_QueryString(key, valName, dest); 268 const BoolInt res2 = MyRegistry_QueryString(key, valName, dest);
245 RegCloseKey(key); 269 RegCloseKey(key);
246 return res2; 270 return res2;
247 } 271 }
@@ -282,7 +306,7 @@ static void MyReg_DeleteVal_Path_if_Equal(HKEY hKey, LPCWSTR name)
282static void SetRegKey_Path2(HKEY parentKey) 306static void SetRegKey_Path2(HKEY parentKey)
283{ 307{
284 HKEY key = 0; 308 HKEY key = 0;
285 LONG res = MyRegistry_OpenKey_ReadWrite(parentKey, k_Reg_Software_7zip, &key); 309 const LONG res = MyRegistry_OpenKey_ReadWrite(parentKey, k_Reg_Software_7zip, &key);
286 if (res == ERROR_SUCCESS) 310 if (res == ERROR_SUCCESS)
287 { 311 {
288 MyReg_DeleteVal_Path_if_Equal(key, k_Reg_Path32); 312 MyReg_DeleteVal_Path_if_Equal(key, k_Reg_Path32);
@@ -293,7 +317,7 @@ static void SetRegKey_Path2(HKEY parentKey)
293 } 317 }
294} 318}
295 319
296static void SetRegKey_Path() 320static void SetRegKey_Path(void)
297{ 321{
298 SetRegKey_Path2(HKEY_CURRENT_USER); 322 SetRegKey_Path2(HKEY_CURRENT_USER);
299 SetRegKey_Path2(HKEY_LOCAL_MACHINE); 323 SetRegKey_Path2(HKEY_LOCAL_MACHINE);
@@ -426,7 +450,7 @@ static BoolInt AreEqual_Path_PrefixName(const wchar_t *s, const wchar_t *prefix,
426 return AreStringsEqual_NoCase(s + wcslen(prefix), name); 450 return AreStringsEqual_NoCase(s + wcslen(prefix), name);
427} 451}
428 452
429static void WriteCLSID() 453static void WriteCLSID(void)
430{ 454{
431 WCHAR s[MAX_PATH + 30]; 455 WCHAR s[MAX_PATH + 30];
432 456
@@ -435,14 +459,14 @@ static void WriteCLSID()
435 if (AreEqual_Path_PrefixName(s, path, L"7-zip.dll")) 459 if (AreEqual_Path_PrefixName(s, path, L"7-zip.dll"))
436 { 460 {
437 { 461 {
438 LONG res = MyRegistry_DeleteKey(HKEY_CLASSES_ROOT, k_Reg_CLSID_7zip_Inproc); 462 const LONG res = MyRegistry_DeleteKey(HKEY_CLASSES_ROOT, k_Reg_CLSID_7zip_Inproc);
439 if (res == ERROR_SUCCESS) 463 if (res == ERROR_SUCCESS)
440 MyRegistry_DeleteKey(HKEY_CLASSES_ROOT, k_Reg_CLSID_7zip); 464 MyRegistry_DeleteKey(HKEY_CLASSES_ROOT, k_Reg_CLSID_7zip);
441 } 465 }
442 466
443 { 467 {
444 unsigned i; 468 unsigned i;
445 for (i = 0; i < ARRAY_SIZE(k_ShellEx_Items); i++) 469 for (i = 0; i < Z7_ARRAY_SIZE(k_ShellEx_Items); i++)
446 { 470 {
447 WCHAR destPath[MAX_PATH]; 471 WCHAR destPath[MAX_PATH];
448 CpyAscii(destPath, k_ShellEx_Items[i]); 472 CpyAscii(destPath, k_ShellEx_Items[i]);
@@ -454,7 +478,7 @@ static void WriteCLSID()
454 478
455 { 479 {
456 HKEY destKey = 0; 480 HKEY destKey = 0;
457 LONG res = MyRegistry_OpenKey_ReadWrite(HKEY_LOCAL_MACHINE, k_Shell_Approved, &destKey); 481 const LONG res = MyRegistry_OpenKey_ReadWrite(HKEY_LOCAL_MACHINE, k_Shell_Approved, &destKey);
458 if (res == ERROR_SUCCESS) 482 if (res == ERROR_SUCCESS)
459 { 483 {
460 RegDeleteValueW(destKey, k_7zip_CLSID); 484 RegDeleteValueW(destKey, k_7zip_CLSID);
@@ -472,14 +496,14 @@ static void WriteCLSID()
472 if (AreEqual_Path_PrefixName(s, path, L"7-zip32.dll")) 496 if (AreEqual_Path_PrefixName(s, path, L"7-zip32.dll"))
473 { 497 {
474 { 498 {
475 LONG res = MyRegistry_DeleteKey_32(HKEY_CLASSES_ROOT, k_Reg_CLSID_7zip_Inproc); 499 const LONG res = MyRegistry_DeleteKey_32(HKEY_CLASSES_ROOT, k_Reg_CLSID_7zip_Inproc);
476 if (res == ERROR_SUCCESS) 500 if (res == ERROR_SUCCESS)
477 MyRegistry_DeleteKey_32(HKEY_CLASSES_ROOT, k_Reg_CLSID_7zip); 501 MyRegistry_DeleteKey_32(HKEY_CLASSES_ROOT, k_Reg_CLSID_7zip);
478 } 502 }
479 503
480 { 504 {
481 unsigned i; 505 unsigned i;
482 for (i = 0; i < ARRAY_SIZE(k_ShellEx_Items); i++) 506 for (i = 0; i < Z7_ARRAY_SIZE(k_ShellEx_Items); i++)
483 { 507 {
484 WCHAR destPath[MAX_PATH]; 508 WCHAR destPath[MAX_PATH];
485 CpyAscii(destPath, k_ShellEx_Items[i]); 509 CpyAscii(destPath, k_ShellEx_Items[i]);
@@ -491,7 +515,7 @@ static void WriteCLSID()
491 515
492 { 516 {
493 HKEY destKey = 0; 517 HKEY destKey = 0;
494 LONG res = MyRegistry_OpenKey_ReadWrite_32(HKEY_LOCAL_MACHINE, k_Shell_Approved, &destKey); 518 const LONG res = MyRegistry_OpenKey_ReadWrite_32(HKEY_LOCAL_MACHINE, k_Shell_Approved, &destKey);
495 if (res == ERROR_SUCCESS) 519 if (res == ERROR_SUCCESS)
496 { 520 {
497 RegDeleteValueW(destKey, k_7zip_CLSID); 521 RegDeleteValueW(destKey, k_7zip_CLSID);
@@ -526,7 +550,7 @@ static const wchar_t *GetCmdParam(const wchar_t *s)
526 BoolInt quoteMode = False; 550 BoolInt quoteMode = False;
527 for (;; s++) 551 for (;; s++)
528 { 552 {
529 wchar_t c = *s; 553 const wchar_t c = *s;
530 if (c == 0 || (c == L' ' && !quoteMode)) 554 if (c == 0 || (c == L' ' && !quoteMode))
531 break; 555 break;
532 if (c == L'\"') 556 if (c == L'\"')
@@ -534,7 +558,7 @@ static const wchar_t *GetCmdParam(const wchar_t *s)
534 quoteMode = !quoteMode; 558 quoteMode = !quoteMode;
535 continue; 559 continue;
536 } 560 }
537 if (pos >= ARRAY_SIZE(cmd) - 1) 561 if (pos >= Z7_ARRAY_SIZE(cmd) - 1)
538 exit(1); 562 exit(1);
539 cmd[pos++] = c; 563 cmd[pos++] = c;
540 } 564 }
@@ -558,7 +582,7 @@ static void RemoveQuotes(wchar_t *s)
558} 582}
559*/ 583*/
560 584
561static BoolInt DoesFileOrDirExist() 585static BoolInt DoesFileOrDirExist(void)
562{ 586{
563 return (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES); 587 return (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES);
564} 588}
@@ -573,7 +597,7 @@ static BOOL RemoveFileAfterReboot2(const WCHAR *s)
573 #endif 597 #endif
574} 598}
575 599
576static BOOL RemoveFileAfterReboot() 600static BOOL RemoveFileAfterReboot(void)
577{ 601{
578 return RemoveFileAfterReboot2(path); 602 return RemoveFileAfterReboot2(path);
579} 603}
@@ -584,7 +608,7 @@ static BoolInt IsThereSpace(const wchar_t *s)
584{ 608{
585 for (;;) 609 for (;;)
586 { 610 {
587 wchar_t c = *s++; 611 const wchar_t c = *s++;
588 if (c == 0) 612 if (c == 0)
589 return False; 613 return False;
590 if (c == ' ') 614 if (c == ' ')
@@ -594,7 +618,7 @@ static BoolInt IsThereSpace(const wchar_t *s)
594 618
595static void AddPathParam(wchar_t *dest, const wchar_t *src) 619static void AddPathParam(wchar_t *dest, const wchar_t *src)
596{ 620{
597 BoolInt needQuote = IsThereSpace(src); 621 const BoolInt needQuote = IsThereSpace(src);
598 if (needQuote) 622 if (needQuote)
599 CatAscii(dest, "\""); 623 CatAscii(dest, "\"");
600 wcscat(dest, src); 624 wcscat(dest, src);
@@ -618,9 +642,9 @@ static BoolInt GetErrorMessage(DWORD errorCode, WCHAR *message)
618 return True; 642 return True;
619} 643}
620 644
621static BOOL RemoveDir() 645static BOOL RemoveDir(void)
622{ 646{
623 DWORD attrib = GetFileAttributesW(path); 647 const DWORD attrib = GetFileAttributesW(path);
624 if (attrib == INVALID_FILE_ATTRIBUTES) 648 if (attrib == INVALID_FILE_ATTRIBUTES)
625 return TRUE; 649 return TRUE;
626 if (RemoveDirectoryW(path)) 650 if (RemoveDirectoryW(path))
@@ -670,7 +694,7 @@ static const char * const k_Names =
670 694
671 695
672 696
673static int Install() 697static int Install(void)
674{ 698{
675 SRes res = SZ_OK; 699 SRes res = SZ_OK;
676 WRes winRes = 0; 700 WRes winRes = 0;
@@ -724,7 +748,7 @@ static int Install()
724 748
725 for (;;) 749 for (;;)
726 { 750 {
727 char c = *curName; 751 const char c = *curName;
728 if (c == 0) 752 if (c == 0)
729 break; 753 break;
730 curName++; 754 curName++;
@@ -743,7 +767,7 @@ static int Install()
743 SetWindowTextW(g_InfoLine_HWND, temp); 767 SetWindowTextW(g_InfoLine_HWND, temp);
744 768
745 { 769 {
746 DWORD attrib = GetFileAttributesW(path); 770 const DWORD attrib = GetFileAttributesW(path);
747 if (attrib == INVALID_FILE_ATTRIBUTES) 771 if (attrib == INVALID_FILE_ATTRIBUTES)
748 continue; 772 continue;
749 if (attrib & FILE_ATTRIBUTE_READONLY) 773 if (attrib & FILE_ATTRIBUTE_READONLY)
@@ -803,7 +827,7 @@ static int Install()
803} 827}
804 828
805 829
806static void OnClose() 830static void OnClose(void)
807{ 831{
808 if (g_Install_was_Pressed && !g_Finished) 832 if (g_Install_was_Pressed && !g_Finished)
809 { 833 {
@@ -817,7 +841,13 @@ static void OnClose()
817 g_HWND = NULL; 841 g_HWND = NULL;
818} 842}
819 843
820static INT_PTR CALLBACK MyDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 844static
845#ifdef Z7_OLD_WIN_SDK
846 BOOL
847#else
848 INT_PTR
849#endif
850CALLBACK MyDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
821{ 851{
822 UNUSED_VAR(lParam) 852 UNUSED_VAR(lParam)
823 853
@@ -905,7 +935,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
905 #endif 935 #endif
906 936
907 #ifndef UNDER_CE 937 #ifndef UNDER_CE
908 func_RegDeleteKeyExW = (Func_RegDeleteKeyExW) 938 func_RegDeleteKeyExW = (Func_RegDeleteKeyExW) MY_CAST_FUNC
909 GetProcAddress(GetModuleHandleW(L"advapi32.dll"), "RegDeleteKeyExW"); 939 GetProcAddress(GetModuleHandleW(L"advapi32.dll"), "RegDeleteKeyExW");
910 #endif 940 #endif
911 941
@@ -976,7 +1006,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
976 1006
977 { 1007 {
978 wchar_t *name; 1008 wchar_t *name;
979 DWORD len = GetModuleFileNameW(NULL, modulePath, MAX_PATH); 1009 const DWORD len = GetModuleFileNameW(NULL, modulePath, MAX_PATH);
980 if (len == 0 || len > MAX_PATH) 1010 if (len == 0 || len > MAX_PATH)
981 return 1; 1011 return 1;
982 1012
@@ -987,7 +1017,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
987 wchar_t *s = modulePrefix; 1017 wchar_t *s = modulePrefix;
988 for (;;) 1018 for (;;)
989 { 1019 {
990 wchar_t c = *s++; 1020 const wchar_t c = *s++;
991 if (c == 0) 1021 if (c == 0)
992 break; 1022 break;
993 if (c == WCHAR_PATH_SEPARATOR) 1023 if (c == WCHAR_PATH_SEPARATOR)
@@ -1037,7 +1067,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1037 unsigned k; 1067 unsigned k;
1038 for (k = 0; k < 8; k++) 1068 for (k = 0; k < 8; k++)
1039 { 1069 {
1040 unsigned t = value & 0xF; 1070 const unsigned t = value & 0xF;
1041 value >>= 4; 1071 value >>= 4;
1042 s[7 - k] = (wchar_t)((t < 10) ? ('0' + t) : ('A' + (t - 10))); 1072 s[7 - k] = (wchar_t)((t < 10) ? ('0' + t) : ('A' + (t - 10)));
1043 } 1073 }
@@ -1134,7 +1164,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1134 return 1; 1164 return 1;
1135 1165
1136 { 1166 {
1137 HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); 1167 const HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
1138 // SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hIcon); 1168 // SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hIcon);
1139 SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon); 1169 SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon);
1140 } 1170 }
diff --git a/C/Util/7zipUninstall/7zipUninstall.dsp b/C/Util/7zipUninstall/7zipUninstall.dsp
index cc7b6b6..bb7473f 100644
--- a/C/Util/7zipUninstall/7zipUninstall.dsp
+++ b/C/Util/7zipUninstall/7zipUninstall.dsp
@@ -104,6 +104,14 @@ SOURCE=..\..\7zVersion.h
104# PROP Default_Filter "" 104# PROP Default_Filter ""
105# Begin Source File 105# Begin Source File
106 106
107SOURCE=..\..\7zWindows.h
108# End Source File
109# Begin Source File
110
111SOURCE=..\..\Compiler.h
112# End Source File
113# Begin Source File
114
107SOURCE=.\Precomp.c 115SOURCE=.\Precomp.c
108# ADD CPP /Yc"Precomp.h" 116# ADD CPP /Yc"Precomp.h"
109# End Source File 117# End Source File
diff --git a/C/Util/7zipUninstall/Precomp.h b/C/Util/7zipUninstall/Precomp.h
index 4c90d47..bc8fa21 100644
--- a/C/Util/7zipUninstall/Precomp.h
+++ b/C/Util/7zipUninstall/Precomp.h
@@ -1,11 +1,14 @@
1/* Precomp.h -- StdAfx 1/* Precomp.h -- StdAfx
22015-05-24 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_PRECOMP_H 4#ifndef ZIP7_INC_PRECOMP_H
5#define __7Z_PRECOMP_H 5#define ZIP7_INC_PRECOMP_H
6 6
7#include "../../Compiler.h" 7#if defined(_MSC_VER) && _MSC_VER >= 1800
8#pragma warning(disable : 4464) // relative include path contains '..'
9#endif
8 10
11#include "../../Compiler.h"
9#include "../../7zTypes.h" 12#include "../../7zTypes.h"
10 13
11#endif 14#endif
diff --git a/C/Util/7zipUninstall/makefile b/C/Util/7zipUninstall/makefile
index 60c2fe2..9d8aafc 100644
--- a/C/Util/7zipUninstall/makefile
+++ b/C/Util/7zipUninstall/makefile
@@ -1,8 +1,8 @@
1PROG = 7zipUninstall.exe 1PROG = 7zipUninstall.exe
2MY_FIXED = 1 2MY_FIXED = 1
3 3
4!IFDEF _64BIT_INSTALLER 4!IFDEF Z7_64BIT_INSTALLER
5CFLAGS = $(CFLAGS) -D_64BIT_INSTALLER 5CFLAGS = $(CFLAGS) -DZ7_64BIT_INSTALLER
6!ENDIF 6!ENDIF
7 7
8MAIN_OBJS = \ 8MAIN_OBJS = \
diff --git a/C/Util/Lzma/LzmaUtil.c b/C/Util/Lzma/LzmaUtil.c
index 62a5907..b9b974b 100644
--- a/C/Util/Lzma/LzmaUtil.c
+++ b/C/Util/Lzma/LzmaUtil.c
@@ -1,7 +1,7 @@
1/* LzmaUtil.c -- Test application for LZMA compression 1/* LzmaUtil.c -- Test application for LZMA compression
22021-11-01 : Igor Pavlov : Public domain */ 22023-03-07 : Igor Pavlov : Public domain */
3 3
4#include "../../Precomp.h" 4#include "Precomp.h"
5 5
6#include <stdio.h> 6#include <stdio.h>
7#include <stdlib.h> 7#include <stdlib.h>
@@ -21,48 +21,80 @@ static const char * const kCantWriteMessage = "Cannot write output file";
21static const char * const kCantAllocateMessage = "Cannot allocate memory"; 21static const char * const kCantAllocateMessage = "Cannot allocate memory";
22static const char * const kDataErrorMessage = "Data error"; 22static const char * const kDataErrorMessage = "Data error";
23 23
24static void PrintHelp(char *buffer) 24static void Print(const char *s)
25{ 25{
26 strcat(buffer, 26 fputs(s, stdout);
27 "\nLZMA-C " MY_VERSION_CPU " : " MY_COPYRIGHT_DATE "\n\n"
28 "Usage: lzma <e|d> inputFile outputFile\n"
29 " e: encode file\n"
30 " d: decode file\n");
31} 27}
32 28
33static int PrintError(char *buffer, const char *message) 29static void PrintHelp(void)
34{ 30{
35 strcat(buffer, "\nError: "); 31 Print(
36 strcat(buffer, message); 32 "\n" "LZMA-C " MY_VERSION_CPU " : " MY_COPYRIGHT_DATE
37 strcat(buffer, "\n"); 33 "\n"
34 "\n" "Usage: lzma <e|d> inputFile outputFile"
35 "\n" " e: encode file"
36 "\n" " d: decode file"
37 "\n");
38}
39
40static int PrintError(const char *message)
41{
42 Print("\nError: ");
43 Print(message);
44 Print("\n");
38 return 1; 45 return 1;
39} 46}
40 47
41static int PrintError_WRes(char *buffer, const char *message, WRes wres) 48#define CONVERT_INT_TO_STR(charType, tempSize) \
49 unsigned char temp[tempSize]; unsigned i = 0; \
50 while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \
51 *s++ = (charType)('0' + (unsigned)val); \
52 while (i != 0) { i--; *s++ = (charType)temp[i]; } \
53 *s = 0; \
54 return s;
55
56static char * Convert_unsigned_To_str(unsigned val, char *s)
42{ 57{
43 strcat(buffer, "\nError: "); 58 CONVERT_INT_TO_STR(char, 32)
44 strcat(buffer, message); 59}
45 sprintf(buffer + strlen(buffer), "\nSystem error code: %d", (unsigned)wres); 60
61static void Print_unsigned(unsigned code)
62{
63 char str[32];
64 Convert_unsigned_To_str(code, str);
65 Print(str);
66}
67
68static int PrintError_WRes(const char *message, WRes wres)
69{
70 PrintError(message);
71 Print("\nSystem error code: ");
72 Print_unsigned((unsigned)wres);
46 #ifndef _WIN32 73 #ifndef _WIN32
47 { 74 {
48 const char *s = strerror(wres); 75 const char *s = strerror(wres);
49 if (s) 76 if (s)
50 sprintf(buffer + strlen(buffer), " : %s", s); 77 {
78 Print(" : ");
79 Print(s);
80 }
51 } 81 }
52 #endif 82 #endif
53 strcat(buffer, "\n"); 83 Print("\n");
54 return 1; 84 return 1;
55} 85}
56 86
57static int PrintErrorNumber(char *buffer, SRes val) 87static int PrintErrorNumber(SRes val)
58{ 88{
59 sprintf(buffer + strlen(buffer), "\n7-Zip error code: %d\n", (unsigned)val); 89 Print("\n7-Zip error code: ");
90 Print_unsigned((unsigned)val);
91 Print("\n");
60 return 1; 92 return 1;
61} 93}
62 94
63static int PrintUserError(char *buffer) 95static int PrintUserError(void)
64{ 96{
65 return PrintError(buffer, "Incorrect command"); 97 return PrintError("Incorrect command");
66} 98}
67 99
68 100
@@ -70,10 +102,10 @@ static int PrintUserError(char *buffer)
70#define OUT_BUF_SIZE (1 << 16) 102#define OUT_BUF_SIZE (1 << 16)
71 103
72 104
73static SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream, 105static SRes Decode2(CLzmaDec *state, ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream,
74 UInt64 unpackSize) 106 UInt64 unpackSize)
75{ 107{
76 int thereIsSize = (unpackSize != (UInt64)(Int64)-1); 108 const int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
77 Byte inBuf[IN_BUF_SIZE]; 109 Byte inBuf[IN_BUF_SIZE];
78 Byte outBuf[OUT_BUF_SIZE]; 110 Byte outBuf[OUT_BUF_SIZE];
79 size_t inPos = 0, inSize = 0, outPos = 0; 111 size_t inPos = 0, inSize = 0, outPos = 0;
@@ -83,7 +115,7 @@ static SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inS
83 if (inPos == inSize) 115 if (inPos == inSize)
84 { 116 {
85 inSize = IN_BUF_SIZE; 117 inSize = IN_BUF_SIZE;
86 RINOK(inStream->Read(inStream, inBuf, &inSize)); 118 RINOK(inStream->Read(inStream, inBuf, &inSize))
87 inPos = 0; 119 inPos = 0;
88 } 120 }
89 { 121 {
@@ -124,7 +156,7 @@ static SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inS
124} 156}
125 157
126 158
127static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream) 159static SRes Decode(ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream)
128{ 160{
129 UInt64 unpackSize; 161 UInt64 unpackSize;
130 int i; 162 int i;
@@ -137,27 +169,29 @@ static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream)
137 169
138 /* Read and parse header */ 170 /* Read and parse header */
139 171
140 RINOK(SeqInStream_Read(inStream, header, sizeof(header))); 172 {
141 173 size_t size = sizeof(header);
174 RINOK(SeqInStream_ReadMax(inStream, header, &size))
175 if (size != sizeof(header))
176 return SZ_ERROR_INPUT_EOF;
177 }
142 unpackSize = 0; 178 unpackSize = 0;
143 for (i = 0; i < 8; i++) 179 for (i = 0; i < 8; i++)
144 unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8); 180 unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8);
145 181
146 LzmaDec_Construct(&state); 182 LzmaDec_CONSTRUCT(&state)
147 RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc)); 183 RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc))
148 res = Decode2(&state, outStream, inStream, unpackSize); 184 res = Decode2(&state, outStream, inStream, unpackSize);
149 LzmaDec_Free(&state, &g_Alloc); 185 LzmaDec_Free(&state, &g_Alloc);
150 return res; 186 return res;
151} 187}
152 188
153static SRes Encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize, char *rs) 189static SRes Encode(ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream, UInt64 fileSize)
154{ 190{
155 CLzmaEncHandle enc; 191 CLzmaEncHandle enc;
156 SRes res; 192 SRes res;
157 CLzmaEncProps props; 193 CLzmaEncProps props;
158 194
159 UNUSED_VAR(rs);
160
161 enc = LzmaEnc_Create(&g_Alloc); 195 enc = LzmaEnc_Create(&g_Alloc);
162 if (enc == 0) 196 if (enc == 0)
163 return SZ_ERROR_MEM; 197 return SZ_ERROR_MEM;
@@ -187,7 +221,7 @@ static SRes Encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 file
187} 221}
188 222
189 223
190static int main2(int numArgs, const char *args[], char *rs) 224int Z7_CDECL main(int numArgs, const char *args[])
191{ 225{
192 CFileSeqInStream inStream; 226 CFileSeqInStream inStream;
193 CFileOutStream outStream; 227 CFileOutStream outStream;
@@ -208,29 +242,31 @@ static int main2(int numArgs, const char *args[], char *rs)
208 242
209 if (numArgs == 1) 243 if (numArgs == 1)
210 { 244 {
211 PrintHelp(rs); 245 PrintHelp();
212 return 0; 246 return 0;
213 } 247 }
214 248
215 if (numArgs < 3 || numArgs > 4 || strlen(args[1]) != 1) 249 if (numArgs < 3 || numArgs > 4 || strlen(args[1]) != 1)
216 return PrintUserError(rs); 250 return PrintUserError();
217 251
218 c = args[1][0]; 252 c = args[1][0];
219 encodeMode = (c == 'e' || c == 'E'); 253 encodeMode = (c == 'e' || c == 'E');
220 if (!encodeMode && c != 'd' && c != 'D') 254 if (!encodeMode && c != 'd' && c != 'D')
221 return PrintUserError(rs); 255 return PrintUserError();
222 256
257 /*
223 { 258 {
224 size_t t4 = sizeof(UInt32); 259 size_t t4 = sizeof(UInt32);
225 size_t t8 = sizeof(UInt64); 260 size_t t8 = sizeof(UInt64);
226 if (t4 != 4 || t8 != 8) 261 if (t4 != 4 || t8 != 8)
227 return PrintError(rs, "Incorrect UInt32 or UInt64"); 262 return PrintError("Incorrect UInt32 or UInt64");
228 } 263 }
264 */
229 265
230 { 266 {
231 WRes wres = InFile_Open(&inStream.file, args[2]); 267 const WRes wres = InFile_Open(&inStream.file, args[2]);
232 if (wres != 0) 268 if (wres != 0)
233 return PrintError_WRes(rs, "Cannot open input file", wres); 269 return PrintError_WRes("Cannot open input file", wres);
234 } 270 }
235 271
236 if (numArgs > 3) 272 if (numArgs > 3)
@@ -239,18 +275,18 @@ static int main2(int numArgs, const char *args[], char *rs)
239 useOutFile = True; 275 useOutFile = True;
240 wres = OutFile_Open(&outStream.file, args[3]); 276 wres = OutFile_Open(&outStream.file, args[3]);
241 if (wres != 0) 277 if (wres != 0)
242 return PrintError_WRes(rs, "Cannot open output file", wres); 278 return PrintError_WRes("Cannot open output file", wres);
243 } 279 }
244 else if (encodeMode) 280 else if (encodeMode)
245 PrintUserError(rs); 281 PrintUserError();
246 282
247 if (encodeMode) 283 if (encodeMode)
248 { 284 {
249 UInt64 fileSize; 285 UInt64 fileSize;
250 WRes wres = File_GetLength(&inStream.file, &fileSize); 286 const WRes wres = File_GetLength(&inStream.file, &fileSize);
251 if (wres != 0) 287 if (wres != 0)
252 return PrintError_WRes(rs, "Cannot get file length", wres); 288 return PrintError_WRes("Cannot get file length", wres);
253 res = Encode(&outStream.vt, &inStream.vt, fileSize, rs); 289 res = Encode(&outStream.vt, &inStream.vt, fileSize);
254 } 290 }
255 else 291 else
256 { 292 {
@@ -264,23 +300,14 @@ static int main2(int numArgs, const char *args[], char *rs)
264 if (res != SZ_OK) 300 if (res != SZ_OK)
265 { 301 {
266 if (res == SZ_ERROR_MEM) 302 if (res == SZ_ERROR_MEM)
267 return PrintError(rs, kCantAllocateMessage); 303 return PrintError(kCantAllocateMessage);
268 else if (res == SZ_ERROR_DATA) 304 else if (res == SZ_ERROR_DATA)
269 return PrintError(rs, kDataErrorMessage); 305 return PrintError(kDataErrorMessage);
270 else if (res == SZ_ERROR_WRITE) 306 else if (res == SZ_ERROR_WRITE)
271 return PrintError_WRes(rs, kCantWriteMessage, outStream.wres); 307 return PrintError_WRes(kCantWriteMessage, outStream.wres);
272 else if (res == SZ_ERROR_READ) 308 else if (res == SZ_ERROR_READ)
273 return PrintError_WRes(rs, kCantReadMessage, inStream.wres); 309 return PrintError_WRes(kCantReadMessage, inStream.wres);
274 return PrintErrorNumber(rs, res); 310 return PrintErrorNumber(res);
275 } 311 }
276 return 0; 312 return 0;
277} 313}
278
279
280int MY_CDECL main(int numArgs, const char *args[])
281{
282 char rs[1000] = { 0 };
283 int res = main2(numArgs, args, rs);
284 fputs(rs, stdout);
285 return res;
286}
diff --git a/C/Util/Lzma/LzmaUtil.dsp b/C/Util/Lzma/LzmaUtil.dsp
index 4e38e4a..e2e7d42 100644
--- a/C/Util/Lzma/LzmaUtil.dsp
+++ b/C/Util/Lzma/LzmaUtil.dsp
@@ -106,6 +106,10 @@ SOURCE=..\..\7zVersion.h
106# End Source File 106# End Source File
107# Begin Source File 107# Begin Source File
108 108
109SOURCE=..\..\7zWindows.h
110# End Source File
111# Begin Source File
112
109SOURCE=..\..\Alloc.c 113SOURCE=..\..\Alloc.c
110# End Source File 114# End Source File
111# Begin Source File 115# Begin Source File
@@ -114,6 +118,10 @@ SOURCE=..\..\Alloc.h
114# End Source File 118# End Source File
115# Begin Source File 119# Begin Source File
116 120
121SOURCE=..\..\Compiler.h
122# End Source File
123# Begin Source File
124
117SOURCE=..\..\CpuArch.h 125SOURCE=..\..\CpuArch.h
118# End Source File 126# End Source File
119# Begin Source File 127# Begin Source File
@@ -162,6 +170,14 @@ SOURCE=.\LzmaUtil.c
162# End Source File 170# End Source File
163# Begin Source File 171# Begin Source File
164 172
173SOURCE=..\..\Precomp.h
174# End Source File
175# Begin Source File
176
177SOURCE=.\Precomp.h
178# End Source File
179# Begin Source File
180
165SOURCE=..\..\Threads.c 181SOURCE=..\..\Threads.c
166# End Source File 182# End Source File
167# Begin Source File 183# Begin Source File
diff --git a/C/Util/Lzma/Precomp.h b/C/Util/Lzma/Precomp.h
new file mode 100644
index 0000000..bc8fa21
--- /dev/null
+++ b/C/Util/Lzma/Precomp.h
@@ -0,0 +1,14 @@
1/* Precomp.h -- StdAfx
22023-03-04 : Igor Pavlov : Public domain */
3
4#ifndef ZIP7_INC_PRECOMP_H
5#define ZIP7_INC_PRECOMP_H
6
7#if defined(_MSC_VER) && _MSC_VER >= 1800
8#pragma warning(disable : 4464) // relative include path contains '..'
9#endif
10
11#include "../../Compiler.h"
12#include "../../7zTypes.h"
13
14#endif
diff --git a/C/Util/LzmaLib/LzmaLib.dsp b/C/Util/LzmaLib/LzmaLib.dsp
index 6ce91dc..bacd967 100644
--- a/C/Util/LzmaLib/LzmaLib.dsp
+++ b/C/Util/LzmaLib/LzmaLib.dsp
@@ -101,6 +101,10 @@ SOURCE=.\LzmaLib.def
101 101
102SOURCE=.\LzmaLibExports.c 102SOURCE=.\LzmaLibExports.c
103# End Source File 103# End Source File
104# Begin Source File
105
106SOURCE=.\Precomp.h
107# End Source File
104# End Group 108# End Group
105# Begin Source File 109# Begin Source File
106 110
@@ -108,6 +112,10 @@ SOURCE=..\..\7zTypes.h
108# End Source File 112# End Source File
109# Begin Source File 113# Begin Source File
110 114
115SOURCE=..\..\7zWindows.h
116# End Source File
117# Begin Source File
118
111SOURCE=..\..\Alloc.c 119SOURCE=..\..\Alloc.c
112# End Source File 120# End Source File
113# Begin Source File 121# Begin Source File
@@ -116,6 +124,14 @@ SOURCE=..\..\Alloc.h
116# End Source File 124# End Source File
117# Begin Source File 125# Begin Source File
118 126
127SOURCE=..\..\Compiler.h
128# End Source File
129# Begin Source File
130
131SOURCE=..\..\CpuArch.h
132# End Source File
133# Begin Source File
134
119SOURCE=..\..\IStream.h 135SOURCE=..\..\IStream.h
120# End Source File 136# End Source File
121# Begin Source File 137# Begin Source File
@@ -168,6 +184,10 @@ SOURCE=..\..\LzmaLib.h
168# End Source File 184# End Source File
169# Begin Source File 185# Begin Source File
170 186
187SOURCE=..\..\Precomp.h
188# End Source File
189# Begin Source File
190
171SOURCE=.\resource.rc 191SOURCE=.\resource.rc
172# End Source File 192# End Source File
173# Begin Source File 193# Begin Source File
diff --git a/C/Util/LzmaLib/LzmaLibExports.c b/C/Util/LzmaLib/LzmaLibExports.c
index 4a28a9a..a46c9a8 100644
--- a/C/Util/LzmaLib/LzmaLibExports.c
+++ b/C/Util/LzmaLib/LzmaLibExports.c
@@ -1,14 +1,15 @@
1/* LzmaLibExports.c -- LZMA library DLL Entry point 1/* LzmaLibExports.c -- LZMA library DLL Entry point
22015-11-08 : Igor Pavlov : Public domain */ 22023-03-05 : Igor Pavlov : Public domain */
3 3
4#include "../../Precomp.h" 4#include "Precomp.h"
5 5
6#include <windows.h> 6#include "../../7zWindows.h"
7 7
8BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved);
8BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) 9BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
9{ 10{
10 UNUSED_VAR(hInstance); 11 UNUSED_VAR(hInstance)
11 UNUSED_VAR(dwReason); 12 UNUSED_VAR(dwReason)
12 UNUSED_VAR(lpReserved); 13 UNUSED_VAR(lpReserved)
13 return TRUE; 14 return TRUE;
14} 15}
diff --git a/C/Util/LzmaLib/Precomp.c b/C/Util/LzmaLib/Precomp.c
new file mode 100644
index 0000000..01605e3
--- /dev/null
+++ b/C/Util/LzmaLib/Precomp.c
@@ -0,0 +1,4 @@
1/* Precomp.c -- StdAfx
22013-01-21 : Igor Pavlov : Public domain */
3
4#include "Precomp.h"
diff --git a/C/Util/LzmaLib/Precomp.h b/C/Util/LzmaLib/Precomp.h
new file mode 100644
index 0000000..bc8fa21
--- /dev/null
+++ b/C/Util/LzmaLib/Precomp.h
@@ -0,0 +1,14 @@
1/* Precomp.h -- StdAfx
22023-03-04 : Igor Pavlov : Public domain */
3
4#ifndef ZIP7_INC_PRECOMP_H
5#define ZIP7_INC_PRECOMP_H
6
7#if defined(_MSC_VER) && _MSC_VER >= 1800
8#pragma warning(disable : 4464) // relative include path contains '..'
9#endif
10
11#include "../../Compiler.h"
12#include "../../7zTypes.h"
13
14#endif
diff --git a/C/Util/LzmaLib/makefile b/C/Util/LzmaLib/makefile
index b36f1de..b8e054e 100644
--- a/C/Util/LzmaLib/makefile
+++ b/C/Util/LzmaLib/makefile
@@ -21,6 +21,7 @@ C_OBJS = \
21 $O\Threads.obj \ 21 $O\Threads.obj \
22 22
23OBJS = \ 23OBJS = \
24 $O\Precomp.obj \
24 $(LIB_OBJS) \ 25 $(LIB_OBJS) \
25 $(C_OBJS) \ 26 $(C_OBJS) \
26 $O\resource.res 27 $O\resource.res
@@ -30,7 +31,24 @@ OBJS = \
30$(SLIBPATH): $O $(OBJS) 31$(SLIBPATH): $O $(OBJS)
31 lib -out:$(SLIBPATH) $(OBJS) $(LIBS) 32 lib -out:$(SLIBPATH) $(OBJS) $(LIBS)
32 33
34
35MAK_SINGLE_FILE = 1
36
37$O\Precomp.obj: Precomp.c
38 $(CCOMPL_PCH)
39
40!IFDEF MAK_SINGLE_FILE
41
33$(LIB_OBJS): $(*B).c 42$(LIB_OBJS): $(*B).c
34 $(COMPL_O2) 43 $(CCOMPL_USE)
35$(C_OBJS): ../../$(*B).c 44$(C_OBJS): ../../$(*B).c
36 $(COMPL_O2) 45 $(CCOMPL_USE)
46
47!ELSE
48
49{.}.c{$O}.obj::
50 $(CCOMPLB_USE)
51{../../../C}.c{$O}.obj::
52 $(CCOMPLB_USE)
53
54!ENDIF
diff --git a/C/Util/SfxSetup/Precomp.h b/C/Util/SfxSetup/Precomp.h
index 588a66f..bc8fa21 100644
--- a/C/Util/SfxSetup/Precomp.h
+++ b/C/Util/SfxSetup/Precomp.h
@@ -1,8 +1,12 @@
1/* Precomp.h -- StdAfx 1/* Precomp.h -- StdAfx
22013-06-16 : Igor Pavlov : Public domain */ 22023-03-04 : Igor Pavlov : Public domain */
3 3
4#ifndef __7Z_PRECOMP_H 4#ifndef ZIP7_INC_PRECOMP_H
5#define __7Z_PRECOMP_H 5#define ZIP7_INC_PRECOMP_H
6
7#if defined(_MSC_VER) && _MSC_VER >= 1800
8#pragma warning(disable : 4464) // relative include path contains '..'
9#endif
6 10
7#include "../../Compiler.h" 11#include "../../Compiler.h"
8#include "../../7zTypes.h" 12#include "../../7zTypes.h"
diff --git a/C/Util/SfxSetup/SfxSetup.c b/C/Util/SfxSetup/SfxSetup.c
index ef19aea..7304a0b 100644
--- a/C/Util/SfxSetup/SfxSetup.c
+++ b/C/Util/SfxSetup/SfxSetup.c
@@ -26,6 +26,12 @@
26 26
27#define kInputBufSize ((size_t)1 << 18) 27#define kInputBufSize ((size_t)1 << 18)
28 28
29
30#define wcscat lstrcatW
31#define wcslen (size_t)lstrlenW
32#define wcscpy lstrcpyW
33// wcsncpy() and lstrcpynW() work differently. We don't use them.
34
29static const char * const kExts[] = 35static const char * const kExts[] =
30{ 36{
31 "bat" 37 "bat"
@@ -64,7 +70,7 @@ static unsigned FindExt(const wchar_t *s, unsigned *extLen)
64 return len; 70 return len;
65} 71}
66 72
67#define MAKE_CHAR_UPPER(c) ((((c) >= 'a' && (c) <= 'z') ? (c) -= 0x20 : (c))) 73#define MAKE_CHAR_UPPER(c) ((((c) >= 'a' && (c) <= 'z') ? (c) - 0x20 : (c)))
68 74
69static unsigned FindItem(const char * const *items, unsigned num, const wchar_t *s, unsigned len) 75static unsigned FindItem(const char * const *items, unsigned num, const wchar_t *s, unsigned len)
70{ 76{
@@ -72,13 +78,13 @@ static unsigned FindItem(const char * const *items, unsigned num, const wchar_t
72 for (i = 0; i < num; i++) 78 for (i = 0; i < num; i++)
73 { 79 {
74 const char *item = items[i]; 80 const char *item = items[i];
75 unsigned itemLen = (unsigned)strlen(item); 81 const unsigned itemLen = (unsigned)strlen(item);
76 unsigned j; 82 unsigned j;
77 if (len != itemLen) 83 if (len != itemLen)
78 continue; 84 continue;
79 for (j = 0; j < len; j++) 85 for (j = 0; j < len; j++)
80 { 86 {
81 unsigned c = (Byte)item[j]; 87 const unsigned c = (Byte)item[j];
82 if (c != s[j] && MAKE_CHAR_UPPER(c) != s[j]) 88 if (c != s[j] && MAKE_CHAR_UPPER(c) != s[j])
83 break; 89 break;
84 } 90 }
@@ -96,10 +102,20 @@ static BOOL WINAPI HandlerRoutine(DWORD ctrlType)
96} 102}
97#endif 103#endif
98 104
105
106#ifdef _CONSOLE
107static void PrintStr(const char *s)
108{
109 fputs(s, stdout);
110}
111#endif
112
99static void PrintErrorMessage(const char *message) 113static void PrintErrorMessage(const char *message)
100{ 114{
101 #ifdef _CONSOLE 115 #ifdef _CONSOLE
102 printf("\n7-Zip Error: %s\n", message); 116 PrintStr("\n7-Zip Error: ");
117 PrintStr(message);
118 PrintStr("\n");
103 #else 119 #else
104 #ifdef UNDER_CE 120 #ifdef UNDER_CE
105 WCHAR messageW[256 + 4]; 121 WCHAR messageW[256 + 4];
@@ -179,7 +195,7 @@ static WRes RemoveDirWithSubItems(WCHAR *path)
179 WIN32_FIND_DATAW fd; 195 WIN32_FIND_DATAW fd;
180 HANDLE handle; 196 HANDLE handle;
181 WRes res = 0; 197 WRes res = 0;
182 size_t len = wcslen(path); 198 const size_t len = wcslen(path);
183 wcscpy(path + len, L"*"); 199 wcscpy(path + len, L"*");
184 handle = FindFirstFileW(path, &fd); 200 handle = FindFirstFileW(path, &fd);
185 path[len] = L'\0'; 201 path[len] = L'\0';
@@ -228,7 +244,7 @@ static WRes RemoveDirWithSubItems(WCHAR *path)
228} 244}
229 245
230#ifdef _CONSOLE 246#ifdef _CONSOLE
231int MY_CDECL main() 247int Z7_CDECL main(void)
232#else 248#else
233int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 249int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
234 #ifdef UNDER_CE 250 #ifdef UNDER_CE
@@ -290,7 +306,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
290 BoolInt quoteMode = False; 306 BoolInt quoteMode = False;
291 for (;; cmdLineParams++) 307 for (;; cmdLineParams++)
292 { 308 {
293 wchar_t c = *cmdLineParams; 309 const wchar_t c = *cmdLineParams;
294 if (c == L'\"') 310 if (c == L'\"')
295 quoteMode = !quoteMode; 311 quoteMode = !quoteMode;
296 else if (c == 0 || (c == L' ' && !quoteMode)) 312 else if (c == 0 || (c == L' ' && !quoteMode))
@@ -324,7 +340,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
324 unsigned k; 340 unsigned k;
325 for (k = 0; k < 8; k++) 341 for (k = 0; k < 8; k++)
326 { 342 {
327 unsigned t = value & 0xF; 343 const unsigned t = value & 0xF;
328 value >>= 4; 344 value >>= 4;
329 s[7 - k] = (wchar_t)((t < 10) ? ('0' + t) : ('A' + (t - 10))); 345 s[7 - k] = (wchar_t)((t < 10) ? ('0' + t) : ('A' + (t - 10)));
330 } 346 }
@@ -386,7 +402,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
386 { 402 {
387 lookStream.bufSize = kInputBufSize; 403 lookStream.bufSize = kInputBufSize;
388 lookStream.realStream = &archiveStream.vt; 404 lookStream.realStream = &archiveStream.vt;
389 LookToRead2_Init(&lookStream); 405 LookToRead2_INIT(&lookStream)
390 } 406 }
391 } 407 }
392 408
@@ -455,11 +471,11 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
455 unsigned extLen; 471 unsigned extLen;
456 const WCHAR *name = temp + nameStartPos; 472 const WCHAR *name = temp + nameStartPos;
457 unsigned len = (unsigned)wcslen(name); 473 unsigned len = (unsigned)wcslen(name);
458 unsigned nameLen = FindExt(temp + nameStartPos, &extLen); 474 const unsigned nameLen = FindExt(temp + nameStartPos, &extLen);
459 unsigned extPrice = FindItem(kExts, sizeof(kExts) / sizeof(kExts[0]), name + len - extLen, extLen); 475 const unsigned extPrice = FindItem(kExts, sizeof(kExts) / sizeof(kExts[0]), name + len - extLen, extLen);
460 unsigned namePrice = FindItem(kNames, sizeof(kNames) / sizeof(kNames[0]), name, nameLen); 476 const unsigned namePrice = FindItem(kNames, sizeof(kNames) / sizeof(kNames[0]), name, nameLen);
461 477
462 unsigned price = namePrice + extPrice * 64 + (nameStartPos == 0 ? 0 : (1 << 12)); 478 const unsigned price = namePrice + extPrice * 64 + (nameStartPos == 0 ? 0 : (1 << 12));
463 if (minPrice > price) 479 if (minPrice > price)
464 { 480 {
465 minPrice = price; 481 minPrice = price;
@@ -500,7 +516,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
500 #endif 516 #endif
501 517
502 { 518 {
503 SRes res2 = File_Close(&outFile); 519 const SRes res2 = File_Close(&outFile);
504 if (res != SZ_OK) 520 if (res != SZ_OK)
505 break; 521 break;
506 if (res2 != SZ_OK) 522 if (res2 != SZ_OK)
@@ -550,7 +566,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
550 WCHAR oldCurDir[MAX_PATH + 2]; 566 WCHAR oldCurDir[MAX_PATH + 2];
551 oldCurDir[0] = 0; 567 oldCurDir[0] = 0;
552 { 568 {
553 DWORD needLen = GetCurrentDirectory(MAX_PATH + 1, oldCurDir); 569 const DWORD needLen = GetCurrentDirectory(MAX_PATH + 1, oldCurDir);
554 if (needLen == 0 || needLen > MAX_PATH) 570 if (needLen == 0 || needLen > MAX_PATH)
555 oldCurDir[0] = 0; 571 oldCurDir[0] = 0;
556 SetCurrentDirectory(workCurDir); 572 SetCurrentDirectory(workCurDir);
diff --git a/C/Util/SfxSetup/makefile b/C/Util/SfxSetup/makefile
index 544da67..bc0cf8b 100644
--- a/C/Util/SfxSetup/makefile
+++ b/C/Util/SfxSetup/makefile
@@ -1,6 +1,9 @@
1PROG = 7zS2.sfx 1PROG = 7zS2.sfx
2MY_FIXED = 1 2MY_FIXED = 1
3 3
4CFLAGS = $(CFLAGS) \
5 -DZ7_EXTRACT_ONLY \
6
4C_OBJS = \ 7C_OBJS = \
5 $O\7zAlloc.obj \ 8 $O\7zAlloc.obj \
6 $O\7zArcIn.obj \ 9 $O\7zArcIn.obj \
diff --git a/C/Util/SfxSetup/makefile_con b/C/Util/SfxSetup/makefile_con
index d0f8352..9f4b916 100644
--- a/C/Util/SfxSetup/makefile_con
+++ b/C/Util/SfxSetup/makefile_con
@@ -1,6 +1,8 @@
1PROG = 7zS2con.sfx 1PROG = 7zS2con.sfx
2MY_FIXED = 1 2MY_FIXED = 1
3CFLAGS = $(CFLAGS) -D_CONSOLE 3
4CFLAGS = $(CFLAGS) -D_CONSOLE \
5 -DZ7_EXTRACT_ONLY \
4 6
5C_OBJS = \ 7C_OBJS = \
6 $O\7zAlloc.obj \ 8 $O\7zAlloc.obj \
diff --git a/C/Xz.c b/C/Xz.c
index 7c53b60..4ad0710 100644
--- a/C/Xz.c
+++ b/C/Xz.c
@@ -1,5 +1,5 @@
1/* Xz.c - Xz 1/* Xz.c - Xz
22021-02-09 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -70,7 +70,7 @@ int XzCheck_Final(CXzCheck *p, Byte *digest)
70 switch (p->mode) 70 switch (p->mode)
71 { 71 {
72 case XZ_CHECK_CRC32: 72 case XZ_CHECK_CRC32:
73 SetUi32(digest, CRC_GET_DIGEST(p->crc)); 73 SetUi32(digest, CRC_GET_DIGEST(p->crc))
74 break; 74 break;
75 case XZ_CHECK_CRC64: 75 case XZ_CHECK_CRC64:
76 { 76 {
diff --git a/C/Xz.h b/C/Xz.h
index 849b944..d5001f6 100644
--- a/C/Xz.h
+++ b/C/Xz.h
@@ -1,21 +1,23 @@
1/* Xz.h - Xz interface 1/* Xz.h - Xz interface
22021-04-01 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#ifndef __XZ_H 4#ifndef ZIP7_INC_XZ_H
5#define __XZ_H 5#define ZIP7_INC_XZ_H
6 6
7#include "Sha256.h" 7#include "Sha256.h"
8#include "Delta.h"
8 9
9EXTERN_C_BEGIN 10EXTERN_C_BEGIN
10 11
11#define XZ_ID_Subblock 1 12#define XZ_ID_Subblock 1
12#define XZ_ID_Delta 3 13#define XZ_ID_Delta 3
13#define XZ_ID_X86 4 14#define XZ_ID_X86 4
14#define XZ_ID_PPC 5 15#define XZ_ID_PPC 5
15#define XZ_ID_IA64 6 16#define XZ_ID_IA64 6
16#define XZ_ID_ARM 7 17#define XZ_ID_ARM 7
17#define XZ_ID_ARMT 8 18#define XZ_ID_ARMT 8
18#define XZ_ID_SPARC 9 19#define XZ_ID_SPARC 9
20#define XZ_ID_ARM64 0xa
19#define XZ_ID_LZMA2 0x21 21#define XZ_ID_LZMA2 0x21
20 22
21unsigned Xz_ReadVarInt(const Byte *p, size_t maxSize, UInt64 *value); 23unsigned Xz_ReadVarInt(const Byte *p, size_t maxSize, UInt64 *value);
@@ -53,7 +55,7 @@ typedef struct
53#define XzBlock_HasUnsupportedFlags(p) (((p)->flags & ~(XZ_BF_NUM_FILTERS_MASK | XZ_BF_PACK_SIZE | XZ_BF_UNPACK_SIZE)) != 0) 55#define XzBlock_HasUnsupportedFlags(p) (((p)->flags & ~(XZ_BF_NUM_FILTERS_MASK | XZ_BF_PACK_SIZE | XZ_BF_UNPACK_SIZE)) != 0)
54 56
55SRes XzBlock_Parse(CXzBlock *p, const Byte *header); 57SRes XzBlock_Parse(CXzBlock *p, const Byte *header);
56SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, BoolInt *isIndex, UInt32 *headerSizeRes); 58SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStreamPtr inStream, BoolInt *isIndex, UInt32 *headerSizeRes);
57 59
58/* ---------- xz stream ---------- */ 60/* ---------- xz stream ---------- */
59 61
@@ -101,7 +103,7 @@ typedef UInt16 CXzStreamFlags;
101unsigned XzFlags_GetCheckSize(CXzStreamFlags f); 103unsigned XzFlags_GetCheckSize(CXzStreamFlags f);
102 104
103SRes Xz_ParseHeader(CXzStreamFlags *p, const Byte *buf); 105SRes Xz_ParseHeader(CXzStreamFlags *p, const Byte *buf);
104SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream); 106SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStreamPtr inStream);
105 107
106typedef struct 108typedef struct
107{ 109{
@@ -112,6 +114,7 @@ typedef struct
112typedef struct 114typedef struct
113{ 115{
114 CXzStreamFlags flags; 116 CXzStreamFlags flags;
117 // Byte _pad[6];
115 size_t numBlocks; 118 size_t numBlocks;
116 CXzBlockSizes *blocks; 119 CXzBlockSizes *blocks;
117 UInt64 startOffset; 120 UInt64 startOffset;
@@ -134,7 +137,7 @@ typedef struct
134 137
135void Xzs_Construct(CXzs *p); 138void Xzs_Construct(CXzs *p);
136void Xzs_Free(CXzs *p, ISzAllocPtr alloc); 139void Xzs_Free(CXzs *p, ISzAllocPtr alloc);
137SRes Xzs_ReadBackward(CXzs *p, ILookInStream *inStream, Int64 *startOffset, ICompressProgress *progress, ISzAllocPtr alloc); 140SRes Xzs_ReadBackward(CXzs *p, ILookInStreamPtr inStream, Int64 *startOffset, ICompressProgressPtr progress, ISzAllocPtr alloc);
138 141
139UInt64 Xzs_GetNumBlocks(const CXzs *p); 142UInt64 Xzs_GetNumBlocks(const CXzs *p);
140UInt64 Xzs_GetUnpackSize(const CXzs *p); 143UInt64 Xzs_GetUnpackSize(const CXzs *p);
@@ -160,9 +163,9 @@ typedef enum
160} ECoderFinishMode; 163} ECoderFinishMode;
161 164
162 165
163typedef struct _IStateCoder 166typedef struct
164{ 167{
165 void *p; 168 void *p; // state object;
166 void (*Free)(void *p, ISzAllocPtr alloc); 169 void (*Free)(void *p, ISzAllocPtr alloc);
167 SRes (*SetProps)(void *p, const Byte *props, size_t propSize, ISzAllocPtr alloc); 170 SRes (*SetProps)(void *p, const Byte *props, size_t propSize, ISzAllocPtr alloc);
168 void (*Init)(void *p); 171 void (*Init)(void *p);
@@ -174,6 +177,20 @@ typedef struct _IStateCoder
174} IStateCoder; 177} IStateCoder;
175 178
176 179
180typedef struct
181{
182 UInt32 methodId;
183 UInt32 delta;
184 UInt32 ip;
185 UInt32 X86_State;
186 Byte delta_State[DELTA_STATE_SIZE];
187} CXzBcFilterStateBase;
188
189typedef SizeT (*Xz_Func_BcFilterStateBase_Filter)(CXzBcFilterStateBase *p, Byte *data, SizeT size);
190
191SRes Xz_StateCoder_Bc_SetFromMethod_Func(IStateCoder *p, UInt64 id,
192 Xz_Func_BcFilterStateBase_Filter func, ISzAllocPtr alloc);
193
177 194
178#define MIXCODER_NUM_FILTERS_MAX 4 195#define MIXCODER_NUM_FILTERS_MAX 4
179 196
@@ -422,7 +439,7 @@ typedef struct
422 size_t outStep_ST; // size of output buffer for Single-Thread decoding 439 size_t outStep_ST; // size of output buffer for Single-Thread decoding
423 BoolInt ignoreErrors; // if set to 1, the decoder can ignore some errors and it skips broken parts of data. 440 BoolInt ignoreErrors; // if set to 1, the decoder can ignore some errors and it skips broken parts of data.
424 441
425 #ifndef _7ZIP_ST 442 #ifndef Z7_ST
426 unsigned numThreads; // the number of threads for Multi-Thread decoding. if (umThreads == 1) it will use Single-thread decoding 443 unsigned numThreads; // the number of threads for Multi-Thread decoding. if (umThreads == 1) it will use Single-thread decoding
427 size_t inBufSize_MT; // size of small input data buffers for Multi-Thread decoding. Big number of such small buffers can be created 444 size_t inBufSize_MT; // size of small input data buffers for Multi-Thread decoding. Big number of such small buffers can be created
428 size_t memUseMax; // the limit of total memory usage for Multi-Thread decoding. 445 size_t memUseMax; // the limit of total memory usage for Multi-Thread decoding.
@@ -432,8 +449,9 @@ typedef struct
432 449
433void XzDecMtProps_Init(CXzDecMtProps *p); 450void XzDecMtProps_Init(CXzDecMtProps *p);
434 451
435 452typedef struct CXzDecMt CXzDecMt;
436typedef void * CXzDecMtHandle; 453typedef CXzDecMt * CXzDecMtHandle;
454// Z7_DECLARE_HANDLE(CXzDecMtHandle)
437 455
438/* 456/*
439 alloc : XzDecMt uses CAlignOffsetAlloc internally for addresses allocated by (alloc). 457 alloc : XzDecMt uses CAlignOffsetAlloc internally for addresses allocated by (alloc).
@@ -503,14 +521,14 @@ SRes XzDecMt_Decode(CXzDecMtHandle p,
503 const CXzDecMtProps *props, 521 const CXzDecMtProps *props,
504 const UInt64 *outDataSize, // NULL means undefined 522 const UInt64 *outDataSize, // NULL means undefined
505 int finishMode, // 0 - partial unpacking is allowed, 1 - xz stream(s) must be finished 523 int finishMode, // 0 - partial unpacking is allowed, 1 - xz stream(s) must be finished
506 ISeqOutStream *outStream, 524 ISeqOutStreamPtr outStream,
507 // Byte *outBuf, size_t *outBufSize, 525 // Byte *outBuf, size_t *outBufSize,
508 ISeqInStream *inStream, 526 ISeqInStreamPtr inStream,
509 // const Byte *inData, size_t inDataSize, 527 // const Byte *inData, size_t inDataSize,
510 CXzStatInfo *stat, // out: decoding results and statistics 528 CXzStatInfo *stat, // out: decoding results and statistics
511 int *isMT, // out: 0 means that ST (Single-Thread) version was used 529 int *isMT, // out: 0 means that ST (Single-Thread) version was used
512 // 1 means that MT (Multi-Thread) version was used 530 // 1 means that MT (Multi-Thread) version was used
513 ICompressProgress *progress); 531 ICompressProgressPtr progress);
514 532
515EXTERN_C_END 533EXTERN_C_END
516 534
diff --git a/C/XzCrc64.c b/C/XzCrc64.c
index b6d02cb..c2fad6c 100644
--- a/C/XzCrc64.c
+++ b/C/XzCrc64.c
@@ -1,5 +1,5 @@
1/* XzCrc64.c -- CRC64 calculation 1/* XzCrc64.c -- CRC64 calculation
22017-05-23 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -12,39 +12,30 @@
12 #define CRC64_NUM_TABLES 4 12 #define CRC64_NUM_TABLES 4
13#else 13#else
14 #define CRC64_NUM_TABLES 5 14 #define CRC64_NUM_TABLES 5
15 #define CRC_UINT64_SWAP(v) \
16 ((v >> 56) \
17 | ((v >> 40) & ((UInt64)0xFF << 8)) \
18 | ((v >> 24) & ((UInt64)0xFF << 16)) \
19 | ((v >> 8) & ((UInt64)0xFF << 24)) \
20 | ((v << 8) & ((UInt64)0xFF << 32)) \
21 | ((v << 24) & ((UInt64)0xFF << 40)) \
22 | ((v << 40) & ((UInt64)0xFF << 48)) \
23 | ((v << 56)))
24 15
25 UInt64 MY_FAST_CALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table); 16 UInt64 Z7_FASTCALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table);
26#endif 17#endif
27 18
28#ifndef MY_CPU_BE 19#ifndef MY_CPU_BE
29 UInt64 MY_FAST_CALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *table); 20 UInt64 Z7_FASTCALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *table);
30#endif 21#endif
31 22
32typedef UInt64 (MY_FAST_CALL *CRC64_FUNC)(UInt64 v, const void *data, size_t size, const UInt64 *table); 23typedef UInt64 (Z7_FASTCALL *CRC64_FUNC)(UInt64 v, const void *data, size_t size, const UInt64 *table);
33 24
34static CRC64_FUNC g_Crc64Update; 25static CRC64_FUNC g_Crc64Update;
35UInt64 g_Crc64Table[256 * CRC64_NUM_TABLES]; 26UInt64 g_Crc64Table[256 * CRC64_NUM_TABLES];
36 27
37UInt64 MY_FAST_CALL Crc64Update(UInt64 v, const void *data, size_t size) 28UInt64 Z7_FASTCALL Crc64Update(UInt64 v, const void *data, size_t size)
38{ 29{
39 return g_Crc64Update(v, data, size, g_Crc64Table); 30 return g_Crc64Update(v, data, size, g_Crc64Table);
40} 31}
41 32
42UInt64 MY_FAST_CALL Crc64Calc(const void *data, size_t size) 33UInt64 Z7_FASTCALL Crc64Calc(const void *data, size_t size)
43{ 34{
44 return g_Crc64Update(CRC64_INIT_VAL, data, size, g_Crc64Table) ^ CRC64_INIT_VAL; 35 return g_Crc64Update(CRC64_INIT_VAL, data, size, g_Crc64Table) ^ CRC64_INIT_VAL;
45} 36}
46 37
47void MY_FAST_CALL Crc64GenerateTable() 38void Z7_FASTCALL Crc64GenerateTable(void)
48{ 39{
49 UInt32 i; 40 UInt32 i;
50 for (i = 0; i < 256; i++) 41 for (i = 0; i < 256; i++)
@@ -57,7 +48,7 @@ void MY_FAST_CALL Crc64GenerateTable()
57 } 48 }
58 for (i = 256; i < 256 * CRC64_NUM_TABLES; i++) 49 for (i = 256; i < 256 * CRC64_NUM_TABLES; i++)
59 { 50 {
60 UInt64 r = g_Crc64Table[(size_t)i - 256]; 51 const UInt64 r = g_Crc64Table[(size_t)i - 256];
61 g_Crc64Table[i] = g_Crc64Table[r & 0xFF] ^ (r >> 8); 52 g_Crc64Table[i] = g_Crc64Table[r & 0xFF] ^ (r >> 8);
62 } 53 }
63 54
@@ -76,11 +67,14 @@ void MY_FAST_CALL Crc64GenerateTable()
76 { 67 {
77 for (i = 256 * CRC64_NUM_TABLES - 1; i >= 256; i--) 68 for (i = 256 * CRC64_NUM_TABLES - 1; i >= 256; i--)
78 { 69 {
79 UInt64 x = g_Crc64Table[(size_t)i - 256]; 70 const UInt64 x = g_Crc64Table[(size_t)i - 256];
80 g_Crc64Table[i] = CRC_UINT64_SWAP(x); 71 g_Crc64Table[i] = Z7_BSWAP64(x);
81 } 72 }
82 g_Crc64Update = XzCrc64UpdateT1_BeT4; 73 g_Crc64Update = XzCrc64UpdateT1_BeT4;
83 } 74 }
84 } 75 }
85 #endif 76 #endif
86} 77}
78
79#undef kCrc64Poly
80#undef CRC64_NUM_TABLES
diff --git a/C/XzCrc64.h b/C/XzCrc64.h
index 08dbc33..ca46869 100644
--- a/C/XzCrc64.h
+++ b/C/XzCrc64.h
@@ -1,8 +1,8 @@
1/* XzCrc64.h -- CRC64 calculation 1/* XzCrc64.h -- CRC64 calculation
22013-01-18 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#ifndef __XZ_CRC64_H 4#ifndef ZIP7_INC_XZ_CRC64_H
5#define __XZ_CRC64_H 5#define ZIP7_INC_XZ_CRC64_H
6 6
7#include <stddef.h> 7#include <stddef.h>
8 8
@@ -12,14 +12,14 @@ EXTERN_C_BEGIN
12 12
13extern UInt64 g_Crc64Table[]; 13extern UInt64 g_Crc64Table[];
14 14
15void MY_FAST_CALL Crc64GenerateTable(void); 15void Z7_FASTCALL Crc64GenerateTable(void);
16 16
17#define CRC64_INIT_VAL UINT64_CONST(0xFFFFFFFFFFFFFFFF) 17#define CRC64_INIT_VAL UINT64_CONST(0xFFFFFFFFFFFFFFFF)
18#define CRC64_GET_DIGEST(crc) ((crc) ^ CRC64_INIT_VAL) 18#define CRC64_GET_DIGEST(crc) ((crc) ^ CRC64_INIT_VAL)
19#define CRC64_UPDATE_BYTE(crc, b) (g_Crc64Table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) 19#define CRC64_UPDATE_BYTE(crc, b) (g_Crc64Table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
20 20
21UInt64 MY_FAST_CALL Crc64Update(UInt64 crc, const void *data, size_t size); 21UInt64 Z7_FASTCALL Crc64Update(UInt64 crc, const void *data, size_t size);
22UInt64 MY_FAST_CALL Crc64Calc(const void *data, size_t size); 22UInt64 Z7_FASTCALL Crc64Calc(const void *data, size_t size);
23 23
24EXTERN_C_END 24EXTERN_C_END
25 25
diff --git a/C/XzCrc64Opt.c b/C/XzCrc64Opt.c
index 93a9fff..d03374c 100644
--- a/C/XzCrc64Opt.c
+++ b/C/XzCrc64Opt.c
@@ -1,5 +1,5 @@
1/* XzCrc64Opt.c -- CRC64 calculation 1/* XzCrc64Opt.c -- CRC64 calculation
22021-02-09 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -9,15 +9,15 @@
9 9
10#define CRC64_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) 10#define CRC64_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
11 11
12UInt64 MY_FAST_CALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *table); 12UInt64 Z7_FASTCALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *table);
13UInt64 MY_FAST_CALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *table) 13UInt64 Z7_FASTCALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, const UInt64 *table)
14{ 14{
15 const Byte *p = (const Byte *)data; 15 const Byte *p = (const Byte *)data;
16 for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++) 16 for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
17 v = CRC64_UPDATE_BYTE_2(v, *p); 17 v = CRC64_UPDATE_BYTE_2(v, *p);
18 for (; size >= 4; size -= 4, p += 4) 18 for (; size >= 4; size -= 4, p += 4)
19 { 19 {
20 UInt32 d = (UInt32)v ^ *(const UInt32 *)(const void *)p; 20 const UInt32 d = (UInt32)v ^ *(const UInt32 *)(const void *)p;
21 v = (v >> 32) 21 v = (v >> 32)
22 ^ (table + 0x300)[((d ) & 0xFF)] 22 ^ (table + 0x300)[((d ) & 0xFF)]
23 ^ (table + 0x200)[((d >> 8) & 0xFF)] 23 ^ (table + 0x200)[((d >> 8) & 0xFF)]
@@ -34,29 +34,19 @@ UInt64 MY_FAST_CALL XzCrc64UpdateT4(UInt64 v, const void *data, size_t size, con
34 34
35#ifndef MY_CPU_LE 35#ifndef MY_CPU_LE
36 36
37#define CRC_UINT64_SWAP(v) \
38 ((v >> 56) \
39 | ((v >> 40) & ((UInt64)0xFF << 8)) \
40 | ((v >> 24) & ((UInt64)0xFF << 16)) \
41 | ((v >> 8) & ((UInt64)0xFF << 24)) \
42 | ((v << 8) & ((UInt64)0xFF << 32)) \
43 | ((v << 24) & ((UInt64)0xFF << 40)) \
44 | ((v << 40) & ((UInt64)0xFF << 48)) \
45 | ((v << 56)))
46
47#define CRC64_UPDATE_BYTE_2_BE(crc, b) (table[(Byte)((crc) >> 56) ^ (b)] ^ ((crc) << 8)) 37#define CRC64_UPDATE_BYTE_2_BE(crc, b) (table[(Byte)((crc) >> 56) ^ (b)] ^ ((crc) << 8))
48 38
49UInt64 MY_FAST_CALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table); 39UInt64 Z7_FASTCALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table);
50UInt64 MY_FAST_CALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table) 40UInt64 Z7_FASTCALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size, const UInt64 *table)
51{ 41{
52 const Byte *p = (const Byte *)data; 42 const Byte *p = (const Byte *)data;
53 table += 0x100; 43 table += 0x100;
54 v = CRC_UINT64_SWAP(v); 44 v = Z7_BSWAP64(v);
55 for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++) 45 for (; size > 0 && ((unsigned)(ptrdiff_t)p & 3) != 0; size--, p++)
56 v = CRC64_UPDATE_BYTE_2_BE(v, *p); 46 v = CRC64_UPDATE_BYTE_2_BE(v, *p);
57 for (; size >= 4; size -= 4, p += 4) 47 for (; size >= 4; size -= 4, p += 4)
58 { 48 {
59 UInt32 d = (UInt32)(v >> 32) ^ *(const UInt32 *)(const void *)p; 49 const UInt32 d = (UInt32)(v >> 32) ^ *(const UInt32 *)(const void *)p;
60 v = (v << 32) 50 v = (v << 32)
61 ^ (table + 0x000)[((d ) & 0xFF)] 51 ^ (table + 0x000)[((d ) & 0xFF)]
62 ^ (table + 0x100)[((d >> 8) & 0xFF)] 52 ^ (table + 0x100)[((d >> 8) & 0xFF)]
@@ -65,7 +55,7 @@ UInt64 MY_FAST_CALL XzCrc64UpdateT1_BeT4(UInt64 v, const void *data, size_t size
65 } 55 }
66 for (; size > 0; size--, p++) 56 for (; size > 0; size--, p++)
67 v = CRC64_UPDATE_BYTE_2_BE(v, *p); 57 v = CRC64_UPDATE_BYTE_2_BE(v, *p);
68 return CRC_UINT64_SWAP(v); 58 return Z7_BSWAP64(v);
69} 59}
70 60
71#endif 61#endif
diff --git a/C/XzDec.c b/C/XzDec.c
index 3f96a37..a5f7039 100644
--- a/C/XzDec.c
+++ b/C/XzDec.c
@@ -1,5 +1,5 @@
1/* XzDec.c -- Xz Decode 1/* XzDec.c -- Xz Decode
22021-09-04 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -67,7 +67,8 @@ unsigned Xz_ReadVarInt(const Byte *p, size_t maxSize, UInt64 *value)
67 return 0; 67 return 0;
68} 68}
69 69
70/* ---------- BraState ---------- */ 70
71/* ---------- XzBcFilterState ---------- */
71 72
72#define BRA_BUF_SIZE (1 << 14) 73#define BRA_BUF_SIZE (1 << 14)
73 74
@@ -76,27 +77,29 @@ typedef struct
76 size_t bufPos; 77 size_t bufPos;
77 size_t bufConv; 78 size_t bufConv;
78 size_t bufTotal; 79 size_t bufTotal;
80 Byte *buf; // must be aligned for 4 bytes
81 Xz_Func_BcFilterStateBase_Filter filter_func;
82 // int encodeMode;
83 CXzBcFilterStateBase base;
84 // Byte buf[BRA_BUF_SIZE];
85} CXzBcFilterState;
79 86
80 int encodeMode;
81
82 UInt32 methodId;
83 UInt32 delta;
84 UInt32 ip;
85 UInt32 x86State;
86 Byte deltaState[DELTA_STATE_SIZE];
87 87
88 Byte buf[BRA_BUF_SIZE]; 88static void XzBcFilterState_Free(void *pp, ISzAllocPtr alloc)
89} CBraState;
90
91static void BraState_Free(void *pp, ISzAllocPtr alloc)
92{ 89{
93 ISzAlloc_Free(alloc, pp); 90 if (pp)
91 {
92 CXzBcFilterState *p = ((CXzBcFilterState *)pp);
93 ISzAlloc_Free(alloc, p->buf);
94 ISzAlloc_Free(alloc, pp);
95 }
94} 96}
95 97
96static SRes BraState_SetProps(void *pp, const Byte *props, size_t propSize, ISzAllocPtr alloc) 98
99static SRes XzBcFilterState_SetProps(void *pp, const Byte *props, size_t propSize, ISzAllocPtr alloc)
97{ 100{
98 CBraState *p = ((CBraState *)pp); 101 CXzBcFilterStateBase *p = &((CXzBcFilterState *)pp)->base;
99 UNUSED_VAR(alloc); 102 UNUSED_VAR(alloc)
100 p->ip = 0; 103 p->ip = 0;
101 if (p->methodId == XZ_ID_Delta) 104 if (p->methodId == XZ_ID_Delta)
102 { 105 {
@@ -114,6 +117,7 @@ static SRes BraState_SetProps(void *pp, const Byte *props, size_t propSize, ISzA
114 case XZ_ID_PPC: 117 case XZ_ID_PPC:
115 case XZ_ID_ARM: 118 case XZ_ID_ARM:
116 case XZ_ID_SPARC: 119 case XZ_ID_SPARC:
120 case XZ_ID_ARM64:
117 if ((v & 3) != 0) 121 if ((v & 3) != 0)
118 return SZ_ERROR_UNSUPPORTED; 122 return SZ_ERROR_UNSUPPORTED;
119 break; 123 break;
@@ -134,73 +138,90 @@ static SRes BraState_SetProps(void *pp, const Byte *props, size_t propSize, ISzA
134 return SZ_OK; 138 return SZ_OK;
135} 139}
136 140
137static void BraState_Init(void *pp) 141
142static void XzBcFilterState_Init(void *pp)
138{ 143{
139 CBraState *p = ((CBraState *)pp); 144 CXzBcFilterState *p = ((CXzBcFilterState *)pp);
140 p->bufPos = p->bufConv = p->bufTotal = 0; 145 p->bufPos = p->bufConv = p->bufTotal = 0;
141 x86_Convert_Init(p->x86State); 146 p->base.X86_State = Z7_BRANCH_CONV_ST_X86_STATE_INIT_VAL;
142 if (p->methodId == XZ_ID_Delta) 147 if (p->base.methodId == XZ_ID_Delta)
143 Delta_Init(p->deltaState); 148 Delta_Init(p->base.delta_State);
144} 149}
145 150
146 151
147#define CASE_BRA_CONV(isa) case XZ_ID_ ## isa: size = isa ## _Convert(data, size, p->ip, p->encodeMode); break; 152static const z7_Func_BranchConv g_Funcs_BranchConv_RISC_Dec[] =
148 153{
149static SizeT BraState_Filter(void *pp, Byte *data, SizeT size) 154 Z7_BRANCH_CONV_DEC(PPC),
155 Z7_BRANCH_CONV_DEC(IA64),
156 Z7_BRANCH_CONV_DEC(ARM),
157 Z7_BRANCH_CONV_DEC(ARMT),
158 Z7_BRANCH_CONV_DEC(SPARC),
159 Z7_BRANCH_CONV_DEC(ARM64)
160};
161
162static SizeT XzBcFilterStateBase_Filter_Dec(CXzBcFilterStateBase *p, Byte *data, SizeT size)
150{ 163{
151 CBraState *p = ((CBraState *)pp);
152 switch (p->methodId) 164 switch (p->methodId)
153 { 165 {
154 case XZ_ID_Delta: 166 case XZ_ID_Delta:
155 if (p->encodeMode) 167 Delta_Decode(p->delta_State, p->delta, data, size);
156 Delta_Encode(p->deltaState, p->delta, data, size);
157 else
158 Delta_Decode(p->deltaState, p->delta, data, size);
159 break; 168 break;
160 case XZ_ID_X86: 169 case XZ_ID_X86:
161 size = x86_Convert(data, size, p->ip, &p->x86State, p->encodeMode); 170 size = (SizeT)(z7_BranchConvSt_X86_Dec(data, size, p->ip, &p->X86_State) - data);
171 break;
172 default:
173 if (p->methodId >= XZ_ID_PPC)
174 {
175 const UInt32 i = p->methodId - XZ_ID_PPC;
176 if (i < Z7_ARRAY_SIZE(g_Funcs_BranchConv_RISC_Dec))
177 size = (SizeT)(g_Funcs_BranchConv_RISC_Dec[i](data, size, p->ip) - data);
178 }
162 break; 179 break;
163 CASE_BRA_CONV(PPC)
164 CASE_BRA_CONV(IA64)
165 CASE_BRA_CONV(ARM)
166 CASE_BRA_CONV(ARMT)
167 CASE_BRA_CONV(SPARC)
168 } 180 }
169 p->ip += (UInt32)size; 181 p->ip += (UInt32)size;
170 return size; 182 return size;
171} 183}
172 184
173 185
174static SRes BraState_Code2(void *pp, 186static SizeT XzBcFilterState_Filter(void *pp, Byte *data, SizeT size)
187{
188 CXzBcFilterState *p = ((CXzBcFilterState *)pp);
189 return p->filter_func(&p->base, data, size);
190}
191
192
193static SRes XzBcFilterState_Code2(void *pp,
175 Byte *dest, SizeT *destLen, 194 Byte *dest, SizeT *destLen,
176 const Byte *src, SizeT *srcLen, int srcWasFinished, 195 const Byte *src, SizeT *srcLen, int srcWasFinished,
177 ECoderFinishMode finishMode, 196 ECoderFinishMode finishMode,
178 // int *wasFinished 197 // int *wasFinished
179 ECoderStatus *status) 198 ECoderStatus *status)
180{ 199{
181 CBraState *p = ((CBraState *)pp); 200 CXzBcFilterState *p = ((CXzBcFilterState *)pp);
182 SizeT destRem = *destLen; 201 SizeT destRem = *destLen;
183 SizeT srcRem = *srcLen; 202 SizeT srcRem = *srcLen;
184 UNUSED_VAR(finishMode); 203 UNUSED_VAR(finishMode)
185 204
186 *destLen = 0; 205 *destLen = 0;
187 *srcLen = 0; 206 *srcLen = 0;
188 // *wasFinished = False; 207 // *wasFinished = False;
189 *status = CODER_STATUS_NOT_FINISHED; 208 *status = CODER_STATUS_NOT_FINISHED;
190 209
191 while (destRem > 0) 210 while (destRem != 0)
192 { 211 {
193 if (p->bufPos != p->bufConv)
194 { 212 {
195 size_t size = p->bufConv - p->bufPos; 213 size_t size = p->bufConv - p->bufPos;
196 if (size > destRem) 214 if (size)
197 size = destRem; 215 {
198 memcpy(dest, p->buf + p->bufPos, size); 216 if (size > destRem)
199 p->bufPos += size; 217 size = destRem;
200 *destLen += size; 218 memcpy(dest, p->buf + p->bufPos, size);
201 dest += size; 219 p->bufPos += size;
202 destRem -= size; 220 *destLen += size;
203 continue; 221 dest += size;
222 destRem -= size;
223 continue;
224 }
204 } 225 }
205 226
206 p->bufTotal -= p->bufPos; 227 p->bufTotal -= p->bufPos;
@@ -220,7 +241,7 @@ static SRes BraState_Code2(void *pp,
220 if (p->bufTotal == 0) 241 if (p->bufTotal == 0)
221 break; 242 break;
222 243
223 p->bufConv = BraState_Filter(pp, p->buf, p->bufTotal); 244 p->bufConv = p->filter_func(&p->base, p->buf, p->bufTotal);
224 245
225 if (p->bufConv == 0) 246 if (p->bufConv == 0)
226 { 247 {
@@ -240,27 +261,37 @@ static SRes BraState_Code2(void *pp,
240} 261}
241 262
242 263
243SRes BraState_SetFromMethod(IStateCoder *p, UInt64 id, int encodeMode, ISzAllocPtr alloc); 264#define XZ_IS_SUPPORTED_FILTER_ID(id) \
244SRes BraState_SetFromMethod(IStateCoder *p, UInt64 id, int encodeMode, ISzAllocPtr alloc) 265 ((id) >= XZ_ID_Delta && (id) <= XZ_ID_ARM64)
266
267SRes Xz_StateCoder_Bc_SetFromMethod_Func(IStateCoder *p, UInt64 id,
268 Xz_Func_BcFilterStateBase_Filter func, ISzAllocPtr alloc)
245{ 269{
246 CBraState *decoder; 270 CXzBcFilterState *decoder;
247 if (id < XZ_ID_Delta || id > XZ_ID_SPARC) 271 if (!XZ_IS_SUPPORTED_FILTER_ID(id))
248 return SZ_ERROR_UNSUPPORTED; 272 return SZ_ERROR_UNSUPPORTED;
249 decoder = (CBraState *)p->p; 273 decoder = (CXzBcFilterState *)p->p;
250 if (!decoder) 274 if (!decoder)
251 { 275 {
252 decoder = (CBraState *)ISzAlloc_Alloc(alloc, sizeof(CBraState)); 276 decoder = (CXzBcFilterState *)ISzAlloc_Alloc(alloc, sizeof(CXzBcFilterState));
253 if (!decoder) 277 if (!decoder)
254 return SZ_ERROR_MEM; 278 return SZ_ERROR_MEM;
279 decoder->buf = ISzAlloc_Alloc(alloc, BRA_BUF_SIZE);
280 if (!decoder->buf)
281 {
282 ISzAlloc_Free(alloc, decoder);
283 return SZ_ERROR_MEM;
284 }
255 p->p = decoder; 285 p->p = decoder;
256 p->Free = BraState_Free; 286 p->Free = XzBcFilterState_Free;
257 p->SetProps = BraState_SetProps; 287 p->SetProps = XzBcFilterState_SetProps;
258 p->Init = BraState_Init; 288 p->Init = XzBcFilterState_Init;
259 p->Code2 = BraState_Code2; 289 p->Code2 = XzBcFilterState_Code2;
260 p->Filter = BraState_Filter; 290 p->Filter = XzBcFilterState_Filter;
291 decoder->filter_func = func;
261 } 292 }
262 decoder->methodId = (UInt32)id; 293 decoder->base.methodId = (UInt32)id;
263 decoder->encodeMode = encodeMode; 294 // decoder->encodeMode = encodeMode;
264 return SZ_OK; 295 return SZ_OK;
265} 296}
266 297
@@ -279,9 +310,9 @@ static void SbState_Free(void *pp, ISzAllocPtr alloc)
279 310
280static SRes SbState_SetProps(void *pp, const Byte *props, size_t propSize, ISzAllocPtr alloc) 311static SRes SbState_SetProps(void *pp, const Byte *props, size_t propSize, ISzAllocPtr alloc)
281{ 312{
282 UNUSED_VAR(pp); 313 UNUSED_VAR(pp)
283 UNUSED_VAR(props); 314 UNUSED_VAR(props)
284 UNUSED_VAR(alloc); 315 UNUSED_VAR(alloc)
285 return (propSize == 0) ? SZ_OK : SZ_ERROR_UNSUPPORTED; 316 return (propSize == 0) ? SZ_OK : SZ_ERROR_UNSUPPORTED;
286} 317}
287 318
@@ -297,7 +328,7 @@ static SRes SbState_Code2(void *pp, Byte *dest, SizeT *destLen, const Byte *src,
297{ 328{
298 CSbDec *p = (CSbDec *)pp; 329 CSbDec *p = (CSbDec *)pp;
299 SRes res; 330 SRes res;
300 UNUSED_VAR(srcWasFinished); 331 UNUSED_VAR(srcWasFinished)
301 p->dest = dest; 332 p->dest = dest;
302 p->destLen = *destLen; 333 p->destLen = *destLen;
303 p->src = src; 334 p->src = src;
@@ -389,7 +420,7 @@ static SRes Lzma2State_Code2(void *pp, Byte *dest, SizeT *destLen, const Byte *s
389 ELzmaStatus status2; 420 ELzmaStatus status2;
390 /* ELzmaFinishMode fm = (finishMode == LZMA_FINISH_ANY) ? LZMA_FINISH_ANY : LZMA_FINISH_END; */ 421 /* ELzmaFinishMode fm = (finishMode == LZMA_FINISH_ANY) ? LZMA_FINISH_ANY : LZMA_FINISH_END; */
391 SRes res; 422 SRes res;
392 UNUSED_VAR(srcWasFinished); 423 UNUSED_VAR(srcWasFinished)
393 if (spec->outBufMode) 424 if (spec->outBufMode)
394 { 425 {
395 SizeT dicPos = spec->decoder.decoder.dicPos; 426 SizeT dicPos = spec->decoder.decoder.dicPos;
@@ -420,7 +451,7 @@ static SRes Lzma2State_SetFromMethod(IStateCoder *p, Byte *outBuf, size_t outBuf
420 p->Init = Lzma2State_Init; 451 p->Init = Lzma2State_Init;
421 p->Code2 = Lzma2State_Code2; 452 p->Code2 = Lzma2State_Code2;
422 p->Filter = NULL; 453 p->Filter = NULL;
423 Lzma2Dec_Construct(&spec->decoder); 454 Lzma2Dec_CONSTRUCT(&spec->decoder)
424 } 455 }
425 spec->outBufMode = False; 456 spec->outBufMode = False;
426 if (outBuf) 457 if (outBuf)
@@ -519,7 +550,8 @@ static SRes MixCoder_SetFromMethod(CMixCoder *p, unsigned coderIndex, UInt64 met
519 } 550 }
520 if (coderIndex == 0) 551 if (coderIndex == 0)
521 return SZ_ERROR_UNSUPPORTED; 552 return SZ_ERROR_UNSUPPORTED;
522 return BraState_SetFromMethod(sc, methodId, 0, p->alloc); 553 return Xz_StateCoder_Bc_SetFromMethod_Func(sc, methodId,
554 XzBcFilterStateBase_Filter_Dec, p->alloc);
523} 555}
524 556
525 557
@@ -568,7 +600,7 @@ static SRes MixCoder_Code(CMixCoder *p,
568 SizeT destLen2, srcLen2; 600 SizeT destLen2, srcLen2;
569 int wasFinished; 601 int wasFinished;
570 602
571 PRF_STR("------- MixCoder Single ----------"); 603 PRF_STR("------- MixCoder Single ----------")
572 604
573 srcLen2 = srcLenOrig; 605 srcLen2 = srcLenOrig;
574 destLen2 = destLenOrig; 606 destLen2 = destLenOrig;
@@ -615,14 +647,14 @@ static SRes MixCoder_Code(CMixCoder *p,
615 processed = coder->Filter(coder->p, p->outBuf, processed); 647 processed = coder->Filter(coder->p, p->outBuf, processed);
616 if (wasFinished || (destFinish && p->outWritten == destLenOrig)) 648 if (wasFinished || (destFinish && p->outWritten == destLenOrig))
617 processed = p->outWritten; 649 processed = p->outWritten;
618 PRF_STR_INT("filter", i); 650 PRF_STR_INT("filter", i)
619 } 651 }
620 *destLen = processed; 652 *destLen = processed;
621 } 653 }
622 return res; 654 return res;
623 } 655 }
624 656
625 PRF_STR("standard mix"); 657 PRF_STR("standard mix")
626 658
627 if (p->numCoders != 1) 659 if (p->numCoders != 1)
628 { 660 {
@@ -779,7 +811,7 @@ static BoolInt Xz_CheckFooter(CXzStreamFlags flags, UInt64 indexSize, const Byte
779 811
780static BoolInt XzBlock_AreSupportedFilters(const CXzBlock *p) 812static BoolInt XzBlock_AreSupportedFilters(const CXzBlock *p)
781{ 813{
782 unsigned numFilters = XzBlock_GetNumFilters(p) - 1; 814 const unsigned numFilters = XzBlock_GetNumFilters(p) - 1;
783 unsigned i; 815 unsigned i;
784 { 816 {
785 const CXzFilter *f = &p->filters[numFilters]; 817 const CXzFilter *f = &p->filters[numFilters];
@@ -795,8 +827,7 @@ static BoolInt XzBlock_AreSupportedFilters(const CXzBlock *p)
795 if (f->propsSize != 1) 827 if (f->propsSize != 1)
796 return False; 828 return False;
797 } 829 }
798 else if (f->id < XZ_ID_Delta 830 else if (!XZ_IS_SUPPORTED_FILTER_ID(f->id)
799 || f->id > XZ_ID_SPARC
800 || (f->propsSize != 0 && f->propsSize != 4)) 831 || (f->propsSize != 0 && f->propsSize != 4))
801 return False; 832 return False;
802 } 833 }
@@ -821,22 +852,24 @@ SRes XzBlock_Parse(CXzBlock *p, const Byte *header)
821 p->packSize = (UInt64)(Int64)-1; 852 p->packSize = (UInt64)(Int64)-1;
822 if (XzBlock_HasPackSize(p)) 853 if (XzBlock_HasPackSize(p))
823 { 854 {
824 READ_VARINT_AND_CHECK(header, pos, headerSize, &p->packSize); 855 READ_VARINT_AND_CHECK(header, pos, headerSize, &p->packSize)
825 if (p->packSize == 0 || p->packSize + headerSize >= (UInt64)1 << 63) 856 if (p->packSize == 0 || p->packSize + headerSize >= (UInt64)1 << 63)
826 return SZ_ERROR_ARCHIVE; 857 return SZ_ERROR_ARCHIVE;
827 } 858 }
828 859
829 p->unpackSize = (UInt64)(Int64)-1; 860 p->unpackSize = (UInt64)(Int64)-1;
830 if (XzBlock_HasUnpackSize(p)) 861 if (XzBlock_HasUnpackSize(p))
831 READ_VARINT_AND_CHECK(header, pos, headerSize, &p->unpackSize); 862 {
863 READ_VARINT_AND_CHECK(header, pos, headerSize, &p->unpackSize)
864 }
832 865
833 numFilters = XzBlock_GetNumFilters(p); 866 numFilters = XzBlock_GetNumFilters(p);
834 for (i = 0; i < numFilters; i++) 867 for (i = 0; i < numFilters; i++)
835 { 868 {
836 CXzFilter *filter = p->filters + i; 869 CXzFilter *filter = p->filters + i;
837 UInt64 size; 870 UInt64 size;
838 READ_VARINT_AND_CHECK(header, pos, headerSize, &filter->id); 871 READ_VARINT_AND_CHECK(header, pos, headerSize, &filter->id)
839 READ_VARINT_AND_CHECK(header, pos, headerSize, &size); 872 READ_VARINT_AND_CHECK(header, pos, headerSize, &size)
840 if (size > headerSize - pos || size > XZ_FILTER_PROPS_SIZE_MAX) 873 if (size > headerSize - pos || size > XZ_FILTER_PROPS_SIZE_MAX)
841 return SZ_ERROR_ARCHIVE; 874 return SZ_ERROR_ARCHIVE;
842 filter->propsSize = (UInt32)size; 875 filter->propsSize = (UInt32)size;
@@ -894,20 +927,20 @@ static SRes XzDecMix_Init(CMixCoder *p, const CXzBlock *block, Byte *outBuf, siz
894 MixCoder_Free(p); 927 MixCoder_Free(p);
895 for (i = 0; i < numFilters; i++) 928 for (i = 0; i < numFilters; i++)
896 { 929 {
897 RINOK(MixCoder_SetFromMethod(p, i, block->filters[numFilters - 1 - i].id, outBuf, outBufSize)); 930 RINOK(MixCoder_SetFromMethod(p, i, block->filters[numFilters - 1 - i].id, outBuf, outBufSize))
898 } 931 }
899 p->numCoders = numFilters; 932 p->numCoders = numFilters;
900 } 933 }
901 else 934 else
902 { 935 {
903 RINOK(MixCoder_ResetFromMethod(p, 0, block->filters[numFilters - 1].id, outBuf, outBufSize)); 936 RINOK(MixCoder_ResetFromMethod(p, 0, block->filters[numFilters - 1].id, outBuf, outBufSize))
904 } 937 }
905 938
906 for (i = 0; i < numFilters; i++) 939 for (i = 0; i < numFilters; i++)
907 { 940 {
908 const CXzFilter *f = &block->filters[numFilters - 1 - i]; 941 const CXzFilter *f = &block->filters[numFilters - 1 - i];
909 IStateCoder *sc = &p->coders[i]; 942 IStateCoder *sc = &p->coders[i];
910 RINOK(sc->SetProps(sc->p, f->props, f->propsSize, p->alloc)); 943 RINOK(sc->SetProps(sc->p, f->props, f->propsSize, p->alloc))
911 } 944 }
912 945
913 MixCoder_Init(p); 946 MixCoder_Init(p);
@@ -1054,14 +1087,14 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
1054 (*destLen) += destLen2; 1087 (*destLen) += destLen2;
1055 p->unpackSize += destLen2; 1088 p->unpackSize += destLen2;
1056 1089
1057 RINOK(res); 1090 RINOK(res)
1058 1091
1059 if (*status != CODER_STATUS_FINISHED_WITH_MARK) 1092 if (*status != CODER_STATUS_FINISHED_WITH_MARK)
1060 { 1093 {
1061 if (p->block.packSize == p->packSize 1094 if (p->block.packSize == p->packSize
1062 && *status == CODER_STATUS_NEEDS_MORE_INPUT) 1095 && *status == CODER_STATUS_NEEDS_MORE_INPUT)
1063 { 1096 {
1064 PRF_STR("CODER_STATUS_NEEDS_MORE_INPUT"); 1097 PRF_STR("CODER_STATUS_NEEDS_MORE_INPUT")
1065 *status = CODER_STATUS_NOT_SPECIFIED; 1098 *status = CODER_STATUS_NOT_SPECIFIED;
1066 return SZ_ERROR_DATA; 1099 return SZ_ERROR_DATA;
1067 } 1100 }
@@ -1078,7 +1111,7 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
1078 if ((p->block.packSize != (UInt64)(Int64)-1 && p->block.packSize != p->packSize) 1111 if ((p->block.packSize != (UInt64)(Int64)-1 && p->block.packSize != p->packSize)
1079 || (p->block.unpackSize != (UInt64)(Int64)-1 && p->block.unpackSize != p->unpackSize)) 1112 || (p->block.unpackSize != (UInt64)(Int64)-1 && p->block.unpackSize != p->unpackSize))
1080 { 1113 {
1081 PRF_STR("ERROR: block.size mismatch"); 1114 PRF_STR("ERROR: block.size mismatch")
1082 return SZ_ERROR_DATA; 1115 return SZ_ERROR_DATA;
1083 } 1116 }
1084 } 1117 }
@@ -1109,7 +1142,7 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
1109 } 1142 }
1110 else 1143 else
1111 { 1144 {
1112 RINOK(Xz_ParseHeader(&p->streamFlags, p->buf)); 1145 RINOK(Xz_ParseHeader(&p->streamFlags, p->buf))
1113 p->numStartedStreams++; 1146 p->numStartedStreams++;
1114 p->indexSize = 0; 1147 p->indexSize = 0;
1115 p->numBlocks = 0; 1148 p->numBlocks = 0;
@@ -1155,7 +1188,7 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
1155 } 1188 }
1156 else 1189 else
1157 { 1190 {
1158 RINOK(XzBlock_Parse(&p->block, p->buf)); 1191 RINOK(XzBlock_Parse(&p->block, p->buf))
1159 if (!XzBlock_AreSupportedFilters(&p->block)) 1192 if (!XzBlock_AreSupportedFilters(&p->block))
1160 return SZ_ERROR_UNSUPPORTED; 1193 return SZ_ERROR_UNSUPPORTED;
1161 p->numTotalBlocks++; 1194 p->numTotalBlocks++;
@@ -1168,7 +1201,7 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
1168 p->headerParsedOk = True; 1201 p->headerParsedOk = True;
1169 return SZ_OK; 1202 return SZ_OK;
1170 } 1203 }
1171 RINOK(XzDecMix_Init(&p->decoder, &p->block, p->outBuf, p->outBufSize)); 1204 RINOK(XzDecMix_Init(&p->decoder, &p->block, p->outBuf, p->outBufSize))
1172 } 1205 }
1173 break; 1206 break;
1174 } 1207 }
@@ -1389,7 +1422,7 @@ UInt64 XzUnpacker_GetExtraSize(const CXzUnpacker *p)
1389 1422
1390 1423
1391 1424
1392#ifndef _7ZIP_ST 1425#ifndef Z7_ST
1393#include "MtDec.h" 1426#include "MtDec.h"
1394#endif 1427#endif
1395 1428
@@ -1400,7 +1433,7 @@ void XzDecMtProps_Init(CXzDecMtProps *p)
1400 p->outStep_ST = 1 << 20; 1433 p->outStep_ST = 1 << 20;
1401 p->ignoreErrors = False; 1434 p->ignoreErrors = False;
1402 1435
1403 #ifndef _7ZIP_ST 1436 #ifndef Z7_ST
1404 p->numThreads = 1; 1437 p->numThreads = 1;
1405 p->inBufSize_MT = 1 << 18; 1438 p->inBufSize_MT = 1 << 18;
1406 p->memUseMax = sizeof(size_t) << 28; 1439 p->memUseMax = sizeof(size_t) << 28;
@@ -1409,7 +1442,7 @@ void XzDecMtProps_Init(CXzDecMtProps *p)
1409 1442
1410 1443
1411 1444
1412#ifndef _7ZIP_ST 1445#ifndef Z7_ST
1413 1446
1414/* ---------- CXzDecMtThread ---------- */ 1447/* ---------- CXzDecMtThread ---------- */
1415 1448
@@ -1448,7 +1481,7 @@ typedef struct
1448 1481
1449/* ---------- CXzDecMt ---------- */ 1482/* ---------- CXzDecMt ---------- */
1450 1483
1451typedef struct 1484struct CXzDecMt
1452{ 1485{
1453 CAlignOffsetAlloc alignOffsetAlloc; 1486 CAlignOffsetAlloc alignOffsetAlloc;
1454 ISzAllocPtr allocMid; 1487 ISzAllocPtr allocMid;
@@ -1456,9 +1489,9 @@ typedef struct
1456 CXzDecMtProps props; 1489 CXzDecMtProps props;
1457 size_t unpackBlockMaxSize; 1490 size_t unpackBlockMaxSize;
1458 1491
1459 ISeqInStream *inStream; 1492 ISeqInStreamPtr inStream;
1460 ISeqOutStream *outStream; 1493 ISeqOutStreamPtr outStream;
1461 ICompressProgress *progress; 1494 ICompressProgressPtr progress;
1462 1495
1463 BoolInt finishMode; 1496 BoolInt finishMode;
1464 BoolInt outSize_Defined; 1497 BoolInt outSize_Defined;
@@ -1481,7 +1514,7 @@ typedef struct
1481 ECoderStatus status; 1514 ECoderStatus status;
1482 SRes codeRes; 1515 SRes codeRes;
1483 1516
1484 #ifndef _7ZIP_ST 1517 #ifndef Z7_ST
1485 BoolInt mainDecoderWasCalled; 1518 BoolInt mainDecoderWasCalled;
1486 // int statErrorDefined; 1519 // int statErrorDefined;
1487 int finishedDecoderIndex; 1520 int finishedDecoderIndex;
@@ -1504,10 +1537,9 @@ typedef struct
1504 1537
1505 BoolInt mtc_WasConstructed; 1538 BoolInt mtc_WasConstructed;
1506 CMtDec mtc; 1539 CMtDec mtc;
1507 CXzDecMtThread coders[MTDEC__THREADS_MAX]; 1540 CXzDecMtThread coders[MTDEC_THREADS_MAX];
1508 #endif 1541 #endif
1509 1542};
1510} CXzDecMt;
1511 1543
1512 1544
1513 1545
@@ -1535,11 +1567,11 @@ CXzDecMtHandle XzDecMt_Create(ISzAllocPtr alloc, ISzAllocPtr allocMid)
1535 1567
1536 XzDecMtProps_Init(&p->props); 1568 XzDecMtProps_Init(&p->props);
1537 1569
1538 #ifndef _7ZIP_ST 1570 #ifndef Z7_ST
1539 p->mtc_WasConstructed = False; 1571 p->mtc_WasConstructed = False;
1540 { 1572 {
1541 unsigned i; 1573 unsigned i;
1542 for (i = 0; i < MTDEC__THREADS_MAX; i++) 1574 for (i = 0; i < MTDEC_THREADS_MAX; i++)
1543 { 1575 {
1544 CXzDecMtThread *coder = &p->coders[i]; 1576 CXzDecMtThread *coder = &p->coders[i];
1545 coder->dec_created = False; 1577 coder->dec_created = False;
@@ -1549,16 +1581,16 @@ CXzDecMtHandle XzDecMt_Create(ISzAllocPtr alloc, ISzAllocPtr allocMid)
1549 } 1581 }
1550 #endif 1582 #endif
1551 1583
1552 return p; 1584 return (CXzDecMtHandle)p;
1553} 1585}
1554 1586
1555 1587
1556#ifndef _7ZIP_ST 1588#ifndef Z7_ST
1557 1589
1558static void XzDecMt_FreeOutBufs(CXzDecMt *p) 1590static void XzDecMt_FreeOutBufs(CXzDecMt *p)
1559{ 1591{
1560 unsigned i; 1592 unsigned i;
1561 for (i = 0; i < MTDEC__THREADS_MAX; i++) 1593 for (i = 0; i < MTDEC_THREADS_MAX; i++)
1562 { 1594 {
1563 CXzDecMtThread *coder = &p->coders[i]; 1595 CXzDecMtThread *coder = &p->coders[i];
1564 if (coder->outBuf) 1596 if (coder->outBuf)
@@ -1595,13 +1627,15 @@ static void XzDecMt_FreeSt(CXzDecMt *p)
1595} 1627}
1596 1628
1597 1629
1598void XzDecMt_Destroy(CXzDecMtHandle pp) 1630// #define GET_CXzDecMt_p CXzDecMt *p = pp;
1631
1632void XzDecMt_Destroy(CXzDecMtHandle p)
1599{ 1633{
1600 CXzDecMt *p = (CXzDecMt *)pp; 1634 // GET_CXzDecMt_p
1601 1635
1602 XzDecMt_FreeSt(p); 1636 XzDecMt_FreeSt(p);
1603 1637
1604 #ifndef _7ZIP_ST 1638 #ifndef Z7_ST
1605 1639
1606 if (p->mtc_WasConstructed) 1640 if (p->mtc_WasConstructed)
1607 { 1641 {
@@ -1610,7 +1644,7 @@ void XzDecMt_Destroy(CXzDecMtHandle pp)
1610 } 1644 }
1611 { 1645 {
1612 unsigned i; 1646 unsigned i;
1613 for (i = 0; i < MTDEC__THREADS_MAX; i++) 1647 for (i = 0; i < MTDEC_THREADS_MAX; i++)
1614 { 1648 {
1615 CXzDecMtThread *t = &p->coders[i]; 1649 CXzDecMtThread *t = &p->coders[i];
1616 if (t->dec_created) 1650 if (t->dec_created)
@@ -1625,12 +1659,12 @@ void XzDecMt_Destroy(CXzDecMtHandle pp)
1625 1659
1626 #endif 1660 #endif
1627 1661
1628 ISzAlloc_Free(p->alignOffsetAlloc.baseAlloc, pp); 1662 ISzAlloc_Free(p->alignOffsetAlloc.baseAlloc, p);
1629} 1663}
1630 1664
1631 1665
1632 1666
1633#ifndef _7ZIP_ST 1667#ifndef Z7_ST
1634 1668
1635static void XzDecMt_Callback_Parse(void *obj, unsigned coderIndex, CMtDecCallbackInfo *cc) 1669static void XzDecMt_Callback_Parse(void *obj, unsigned coderIndex, CMtDecCallbackInfo *cc)
1636{ 1670{
@@ -1696,7 +1730,7 @@ static void XzDecMt_Callback_Parse(void *obj, unsigned coderIndex, CMtDecCallbac
1696 coder->dec.parseMode = True; 1730 coder->dec.parseMode = True;
1697 coder->dec.headerParsedOk = False; 1731 coder->dec.headerParsedOk = False;
1698 1732
1699 PRF_STR_INT("Parse", srcSize2); 1733 PRF_STR_INT("Parse", srcSize2)
1700 1734
1701 res = XzUnpacker_Code(&coder->dec, 1735 res = XzUnpacker_Code(&coder->dec,
1702 NULL, &destSize, 1736 NULL, &destSize,
@@ -2071,7 +2105,7 @@ static SRes XzDecMt_Callback_Write(void *pp, unsigned coderIndex,
2071 } 2105 }
2072 data += cur; 2106 data += cur;
2073 size -= cur; 2107 size -= cur;
2074 // PRF_STR_INT("Written size =", size); 2108 // PRF_STR_INT("Written size =", size)
2075 if (size == 0) 2109 if (size == 0)
2076 break; 2110 break;
2077 res = MtProgress_ProgressAdd(&me->mtc.mtProgress, 0, 0); 2111 res = MtProgress_ProgressAdd(&me->mtc.mtProgress, 0, 0);
@@ -2087,7 +2121,7 @@ static SRes XzDecMt_Callback_Write(void *pp, unsigned coderIndex,
2087 return res; 2121 return res;
2088 } 2122 }
2089 2123
2090 RINOK(res); 2124 RINOK(res)
2091 2125
2092 if (coder->inPreSize != coder->inCodeSize 2126 if (coder->inPreSize != coder->inCodeSize
2093 || coder->blockPackTotal != coder->inCodeSize) 2127 || coder->blockPackTotal != coder->inCodeSize)
@@ -2106,13 +2140,13 @@ static SRes XzDecMt_Callback_Write(void *pp, unsigned coderIndex,
2106 // (coder->state == MTDEC_PARSE_END) means that there are no other working threads 2140 // (coder->state == MTDEC_PARSE_END) means that there are no other working threads
2107 // so we can use mtc variables without lock 2141 // so we can use mtc variables without lock
2108 2142
2109 PRF_STR_INT("Write MTDEC_PARSE_END", me->mtc.inProcessed); 2143 PRF_STR_INT("Write MTDEC_PARSE_END", me->mtc.inProcessed)
2110 2144
2111 me->mtc.mtProgress.totalInSize = me->mtc.inProcessed; 2145 me->mtc.mtProgress.totalInSize = me->mtc.inProcessed;
2112 { 2146 {
2113 CXzUnpacker *dec = &me->dec; 2147 CXzUnpacker *dec = &me->dec;
2114 2148
2115 PRF_STR_INT("PostSingle", srcSize); 2149 PRF_STR_INT("PostSingle", srcSize)
2116 2150
2117 { 2151 {
2118 size_t srcProcessed = srcSize; 2152 size_t srcProcessed = srcSize;
@@ -2186,7 +2220,7 @@ static SRes XzDecMt_Callback_Write(void *pp, unsigned coderIndex,
2186 me->mtc.crossEnd = srcSize; 2220 me->mtc.crossEnd = srcSize;
2187 } 2221 }
2188 2222
2189 PRF_STR_INT("XZ_STATE_STREAM_HEADER crossEnd = ", (unsigned)me->mtc.crossEnd); 2223 PRF_STR_INT("XZ_STATE_STREAM_HEADER crossEnd = ", (unsigned)me->mtc.crossEnd)
2190 2224
2191 return SZ_OK; 2225 return SZ_OK;
2192 } 2226 }
@@ -2277,7 +2311,7 @@ static SRes XzDecMt_Callback_Write(void *pp, unsigned coderIndex,
2277 UInt64 inDelta = me->mtc.inProcessed - inProgressPrev; 2311 UInt64 inDelta = me->mtc.inProcessed - inProgressPrev;
2278 if (inDelta >= (1 << 22)) 2312 if (inDelta >= (1 << 22))
2279 { 2313 {
2280 RINOK(MtProgress_Progress_ST(&me->mtc.mtProgress)); 2314 RINOK(MtProgress_Progress_ST(&me->mtc.mtProgress))
2281 inProgressPrev = me->mtc.inProcessed; 2315 inProgressPrev = me->mtc.inProcessed;
2282 } 2316 }
2283 } 2317 }
@@ -2331,7 +2365,7 @@ void XzStatInfo_Clear(CXzStatInfo *p)
2331*/ 2365*/
2332 2366
2333static SRes XzDecMt_Decode_ST(CXzDecMt *p 2367static SRes XzDecMt_Decode_ST(CXzDecMt *p
2334 #ifndef _7ZIP_ST 2368 #ifndef Z7_ST
2335 , BoolInt tMode 2369 , BoolInt tMode
2336 #endif 2370 #endif
2337 , CXzStatInfo *stat) 2371 , CXzStatInfo *stat)
@@ -2343,7 +2377,7 @@ static SRes XzDecMt_Decode_ST(CXzDecMt *p
2343 2377
2344 CXzUnpacker *dec; 2378 CXzUnpacker *dec;
2345 2379
2346 #ifndef _7ZIP_ST 2380 #ifndef Z7_ST
2347 if (tMode) 2381 if (tMode)
2348 { 2382 {
2349 XzDecMt_FreeOutBufs(p); 2383 XzDecMt_FreeOutBufs(p);
@@ -2400,7 +2434,7 @@ static SRes XzDecMt_Decode_ST(CXzDecMt *p
2400 2434
2401 if (inPos == inLim) 2435 if (inPos == inLim)
2402 { 2436 {
2403 #ifndef _7ZIP_ST 2437 #ifndef Z7_ST
2404 if (tMode) 2438 if (tMode)
2405 { 2439 {
2406 inData = MtDec_Read(&p->mtc, &inLim); 2440 inData = MtDec_Read(&p->mtc, &inLim);
@@ -2577,19 +2611,19 @@ static void XzStatInfo_SetStat(const CXzUnpacker *dec,
2577 2611
2578 2612
2579 2613
2580SRes XzDecMt_Decode(CXzDecMtHandle pp, 2614SRes XzDecMt_Decode(CXzDecMtHandle p,
2581 const CXzDecMtProps *props, 2615 const CXzDecMtProps *props,
2582 const UInt64 *outDataSize, int finishMode, 2616 const UInt64 *outDataSize, int finishMode,
2583 ISeqOutStream *outStream, 2617 ISeqOutStreamPtr outStream,
2584 // Byte *outBuf, size_t *outBufSize, 2618 // Byte *outBuf, size_t *outBufSize,
2585 ISeqInStream *inStream, 2619 ISeqInStreamPtr inStream,
2586 // const Byte *inData, size_t inDataSize, 2620 // const Byte *inData, size_t inDataSize,
2587 CXzStatInfo *stat, 2621 CXzStatInfo *stat,
2588 int *isMT, 2622 int *isMT,
2589 ICompressProgress *progress) 2623 ICompressProgressPtr progress)
2590{ 2624{
2591 CXzDecMt *p = (CXzDecMt *)pp; 2625 // GET_CXzDecMt_p
2592 #ifndef _7ZIP_ST 2626 #ifndef Z7_ST
2593 BoolInt tMode; 2627 BoolInt tMode;
2594 #endif 2628 #endif
2595 2629
@@ -2640,7 +2674,7 @@ SRes XzDecMt_Decode(CXzDecMtHandle pp,
2640 */ 2674 */
2641 2675
2642 2676
2643 #ifndef _7ZIP_ST 2677 #ifndef Z7_ST
2644 2678
2645 p->isBlockHeaderState_Parse = False; 2679 p->isBlockHeaderState_Parse = False;
2646 p->isBlockHeaderState_Write = False; 2680 p->isBlockHeaderState_Write = False;
@@ -2782,7 +2816,7 @@ SRes XzDecMt_Decode(CXzDecMtHandle pp,
2782 return res; 2816 return res;
2783 } 2817 }
2784 2818
2785 PRF_STR("----- decoding ST -----"); 2819 PRF_STR("----- decoding ST -----")
2786 } 2820 }
2787 2821
2788 #endif 2822 #endif
@@ -2792,13 +2826,13 @@ SRes XzDecMt_Decode(CXzDecMtHandle pp,
2792 2826
2793 { 2827 {
2794 SRes res = XzDecMt_Decode_ST(p 2828 SRes res = XzDecMt_Decode_ST(p
2795 #ifndef _7ZIP_ST 2829 #ifndef Z7_ST
2796 , tMode 2830 , tMode
2797 #endif 2831 #endif
2798 , stat 2832 , stat
2799 ); 2833 );
2800 2834
2801 #ifndef _7ZIP_ST 2835 #ifndef Z7_ST
2802 // we must set error code from MT decoding at first 2836 // we must set error code from MT decoding at first
2803 if (p->mainErrorCode != SZ_OK) 2837 if (p->mainErrorCode != SZ_OK)
2804 stat->DecodeRes = p->mainErrorCode; 2838 stat->DecodeRes = p->mainErrorCode;
@@ -2835,3 +2869,7 @@ SRes XzDecMt_Decode(CXzDecMtHandle pp,
2835 return res; 2869 return res;
2836 } 2870 }
2837} 2871}
2872
2873#undef PRF
2874#undef PRF_STR
2875#undef PRF_STR_INT_2
diff --git a/C/XzEnc.c b/C/XzEnc.c
index be174cc..22408e2 100644
--- a/C/XzEnc.c
+++ b/C/XzEnc.c
@@ -1,5 +1,5 @@
1/* XzEnc.c -- Xz Encode 1/* XzEnc.c -- Xz Encode
22021-04-01 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -18,13 +18,13 @@
18 18
19#include "XzEnc.h" 19#include "XzEnc.h"
20 20
21// #define _7ZIP_ST 21// #define Z7_ST
22 22
23#ifndef _7ZIP_ST 23#ifndef Z7_ST
24#include "MtCoder.h" 24#include "MtCoder.h"
25#else 25#else
26#define MTCODER__THREADS_MAX 1 26#define MTCODER_THREADS_MAX 1
27#define MTCODER__BLOCKS_MAX 1 27#define MTCODER_BLOCKS_MAX 1
28#endif 28#endif
29 29
30#define XZ_GET_PAD_SIZE(dataSize) ((4 - ((unsigned)(dataSize) & 3)) & 3) 30#define XZ_GET_PAD_SIZE(dataSize) ((4 - ((unsigned)(dataSize) & 3)) & 3)
@@ -35,25 +35,25 @@
35#define XZ_GET_ESTIMATED_BLOCK_TOTAL_PACK_SIZE(unpackSize) (XZ_BLOCK_HEADER_SIZE_MAX + XZ_GET_MAX_BLOCK_PACK_SIZE(unpackSize)) 35#define XZ_GET_ESTIMATED_BLOCK_TOTAL_PACK_SIZE(unpackSize) (XZ_BLOCK_HEADER_SIZE_MAX + XZ_GET_MAX_BLOCK_PACK_SIZE(unpackSize))
36 36
37 37
38#define XzBlock_ClearFlags(p) (p)->flags = 0; 38// #define XzBlock_ClearFlags(p) (p)->flags = 0;
39#define XzBlock_SetNumFilters(p, n) (p)->flags = (Byte)((p)->flags | ((n) - 1)); 39#define XzBlock_ClearFlags_SetNumFilters(p, n) (p)->flags = (Byte)((n) - 1);
40#define XzBlock_SetHasPackSize(p) (p)->flags |= XZ_BF_PACK_SIZE; 40#define XzBlock_SetHasPackSize(p) (p)->flags |= XZ_BF_PACK_SIZE;
41#define XzBlock_SetHasUnpackSize(p) (p)->flags |= XZ_BF_UNPACK_SIZE; 41#define XzBlock_SetHasUnpackSize(p) (p)->flags |= XZ_BF_UNPACK_SIZE;
42 42
43 43
44static SRes WriteBytes(ISeqOutStream *s, const void *buf, size_t size) 44static SRes WriteBytes(ISeqOutStreamPtr s, const void *buf, size_t size)
45{ 45{
46 return (ISeqOutStream_Write(s, buf, size) == size) ? SZ_OK : SZ_ERROR_WRITE; 46 return (ISeqOutStream_Write(s, buf, size) == size) ? SZ_OK : SZ_ERROR_WRITE;
47} 47}
48 48
49static SRes WriteBytesUpdateCrc(ISeqOutStream *s, const void *buf, size_t size, UInt32 *crc) 49static SRes WriteBytes_UpdateCrc(ISeqOutStreamPtr s, const void *buf, size_t size, UInt32 *crc)
50{ 50{
51 *crc = CrcUpdate(*crc, buf, size); 51 *crc = CrcUpdate(*crc, buf, size);
52 return WriteBytes(s, buf, size); 52 return WriteBytes(s, buf, size);
53} 53}
54 54
55 55
56static SRes Xz_WriteHeader(CXzStreamFlags f, ISeqOutStream *s) 56static SRes Xz_WriteHeader(CXzStreamFlags f, ISeqOutStreamPtr s)
57{ 57{
58 UInt32 crc; 58 UInt32 crc;
59 Byte header[XZ_STREAM_HEADER_SIZE]; 59 Byte header[XZ_STREAM_HEADER_SIZE];
@@ -61,12 +61,12 @@ static SRes Xz_WriteHeader(CXzStreamFlags f, ISeqOutStream *s)
61 header[XZ_SIG_SIZE] = (Byte)(f >> 8); 61 header[XZ_SIG_SIZE] = (Byte)(f >> 8);
62 header[XZ_SIG_SIZE + 1] = (Byte)(f & 0xFF); 62 header[XZ_SIG_SIZE + 1] = (Byte)(f & 0xFF);
63 crc = CrcCalc(header + XZ_SIG_SIZE, XZ_STREAM_FLAGS_SIZE); 63 crc = CrcCalc(header + XZ_SIG_SIZE, XZ_STREAM_FLAGS_SIZE);
64 SetUi32(header + XZ_SIG_SIZE + XZ_STREAM_FLAGS_SIZE, crc); 64 SetUi32(header + XZ_SIG_SIZE + XZ_STREAM_FLAGS_SIZE, crc)
65 return WriteBytes(s, header, XZ_STREAM_HEADER_SIZE); 65 return WriteBytes(s, header, XZ_STREAM_HEADER_SIZE);
66} 66}
67 67
68 68
69static SRes XzBlock_WriteHeader(const CXzBlock *p, ISeqOutStream *s) 69static SRes XzBlock_WriteHeader(const CXzBlock *p, ISeqOutStreamPtr s)
70{ 70{
71 Byte header[XZ_BLOCK_HEADER_SIZE_MAX]; 71 Byte header[XZ_BLOCK_HEADER_SIZE_MAX];
72 72
@@ -91,7 +91,7 @@ static SRes XzBlock_WriteHeader(const CXzBlock *p, ISeqOutStream *s)
91 header[pos++] = 0; 91 header[pos++] = 0;
92 92
93 header[0] = (Byte)(pos >> 2); 93 header[0] = (Byte)(pos >> 2);
94 SetUi32(header + pos, CrcCalc(header, pos)); 94 SetUi32(header + pos, CrcCalc(header, pos))
95 return WriteBytes(s, header, pos + 4); 95 return WriteBytes(s, header, pos + 4);
96} 96}
97 97
@@ -182,7 +182,7 @@ static SRes XzEncIndex_AddIndexRecord(CXzEncIndex *p, UInt64 unpackSize, UInt64
182 size_t newSize = p->allocated * 2 + 16 * 2; 182 size_t newSize = p->allocated * 2 + 16 * 2;
183 if (newSize < p->size + pos) 183 if (newSize < p->size + pos)
184 return SZ_ERROR_MEM; 184 return SZ_ERROR_MEM;
185 RINOK(XzEncIndex_ReAlloc(p, newSize, alloc)); 185 RINOK(XzEncIndex_ReAlloc(p, newSize, alloc))
186 } 186 }
187 memcpy(p->blocks + p->size, buf, pos); 187 memcpy(p->blocks + p->size, buf, pos);
188 p->size += pos; 188 p->size += pos;
@@ -191,7 +191,7 @@ static SRes XzEncIndex_AddIndexRecord(CXzEncIndex *p, UInt64 unpackSize, UInt64
191} 191}
192 192
193 193
194static SRes XzEncIndex_WriteFooter(const CXzEncIndex *p, CXzStreamFlags flags, ISeqOutStream *s) 194static SRes XzEncIndex_WriteFooter(const CXzEncIndex *p, CXzStreamFlags flags, ISeqOutStreamPtr s)
195{ 195{
196 Byte buf[32]; 196 Byte buf[32];
197 UInt64 globalPos; 197 UInt64 globalPos;
@@ -200,8 +200,8 @@ static SRes XzEncIndex_WriteFooter(const CXzEncIndex *p, CXzStreamFlags flags, I
200 200
201 globalPos = pos; 201 globalPos = pos;
202 buf[0] = 0; 202 buf[0] = 0;
203 RINOK(WriteBytesUpdateCrc(s, buf, pos, &crc)); 203 RINOK(WriteBytes_UpdateCrc(s, buf, pos, &crc))
204 RINOK(WriteBytesUpdateCrc(s, p->blocks, p->size, &crc)); 204 RINOK(WriteBytes_UpdateCrc(s, p->blocks, p->size, &crc))
205 globalPos += p->size; 205 globalPos += p->size;
206 206
207 pos = XZ_GET_PAD_SIZE(globalPos); 207 pos = XZ_GET_PAD_SIZE(globalPos);
@@ -211,12 +211,12 @@ static SRes XzEncIndex_WriteFooter(const CXzEncIndex *p, CXzStreamFlags flags, I
211 globalPos += pos; 211 globalPos += pos;
212 212
213 crc = CrcUpdate(crc, buf + 4 - pos, pos); 213 crc = CrcUpdate(crc, buf + 4 - pos, pos);
214 SetUi32(buf + 4, CRC_GET_DIGEST(crc)); 214 SetUi32(buf + 4, CRC_GET_DIGEST(crc))
215 215
216 SetUi32(buf + 8 + 4, (UInt32)(globalPos >> 2)); 216 SetUi32(buf + 8 + 4, (UInt32)(globalPos >> 2))
217 buf[8 + 8] = (Byte)(flags >> 8); 217 buf[8 + 8] = (Byte)(flags >> 8);
218 buf[8 + 9] = (Byte)(flags & 0xFF); 218 buf[8 + 9] = (Byte)(flags & 0xFF);
219 SetUi32(buf + 8, CrcCalc(buf + 8 + 4, 6)); 219 SetUi32(buf + 8, CrcCalc(buf + 8 + 4, 6))
220 buf[8 + 10] = XZ_FOOTER_SIG_0; 220 buf[8 + 10] = XZ_FOOTER_SIG_0;
221 buf[8 + 11] = XZ_FOOTER_SIG_1; 221 buf[8 + 11] = XZ_FOOTER_SIG_1;
222 222
@@ -230,7 +230,7 @@ static SRes XzEncIndex_WriteFooter(const CXzEncIndex *p, CXzStreamFlags flags, I
230typedef struct 230typedef struct
231{ 231{
232 ISeqInStream vt; 232 ISeqInStream vt;
233 ISeqInStream *realStream; 233 ISeqInStreamPtr realStream;
234 const Byte *data; 234 const Byte *data;
235 UInt64 limit; 235 UInt64 limit;
236 UInt64 processed; 236 UInt64 processed;
@@ -251,9 +251,9 @@ static void SeqCheckInStream_GetDigest(CSeqCheckInStream *p, Byte *digest)
251 XzCheck_Final(&p->check, digest); 251 XzCheck_Final(&p->check, digest);
252} 252}
253 253
254static SRes SeqCheckInStream_Read(const ISeqInStream *pp, void *data, size_t *size) 254static SRes SeqCheckInStream_Read(ISeqInStreamPtr pp, void *data, size_t *size)
255{ 255{
256 CSeqCheckInStream *p = CONTAINER_FROM_VTBL(pp, CSeqCheckInStream, vt); 256 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeqCheckInStream)
257 size_t size2 = *size; 257 size_t size2 = *size;
258 SRes res = SZ_OK; 258 SRes res = SZ_OK;
259 259
@@ -285,15 +285,15 @@ static SRes SeqCheckInStream_Read(const ISeqInStream *pp, void *data, size_t *si
285typedef struct 285typedef struct
286{ 286{
287 ISeqOutStream vt; 287 ISeqOutStream vt;
288 ISeqOutStream *realStream; 288 ISeqOutStreamPtr realStream;
289 Byte *outBuf; 289 Byte *outBuf;
290 size_t outBufLimit; 290 size_t outBufLimit;
291 UInt64 processed; 291 UInt64 processed;
292} CSeqSizeOutStream; 292} CSeqSizeOutStream;
293 293
294static size_t SeqSizeOutStream_Write(const ISeqOutStream *pp, const void *data, size_t size) 294static size_t SeqSizeOutStream_Write(ISeqOutStreamPtr pp, const void *data, size_t size)
295{ 295{
296 CSeqSizeOutStream *p = CONTAINER_FROM_VTBL(pp, CSeqSizeOutStream, vt); 296 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeqSizeOutStream)
297 if (p->realStream) 297 if (p->realStream)
298 size = ISeqOutStream_Write(p->realStream, data, size); 298 size = ISeqOutStream_Write(p->realStream, data, size);
299 else 299 else
@@ -313,8 +313,8 @@ static size_t SeqSizeOutStream_Write(const ISeqOutStream *pp, const void *data,
313 313
314typedef struct 314typedef struct
315{ 315{
316 ISeqInStream p; 316 ISeqInStream vt;
317 ISeqInStream *realStream; 317 ISeqInStreamPtr realStream;
318 IStateCoder StateCoder; 318 IStateCoder StateCoder;
319 Byte *buf; 319 Byte *buf;
320 size_t curPos; 320 size_t curPos;
@@ -323,7 +323,39 @@ typedef struct
323} CSeqInFilter; 323} CSeqInFilter;
324 324
325 325
326SRes BraState_SetFromMethod(IStateCoder *p, UInt64 id, int encodeMode, ISzAllocPtr alloc); 326static const z7_Func_BranchConv g_Funcs_BranchConv_RISC_Enc[] =
327{
328 Z7_BRANCH_CONV_ENC(PPC),
329 Z7_BRANCH_CONV_ENC(IA64),
330 Z7_BRANCH_CONV_ENC(ARM),
331 Z7_BRANCH_CONV_ENC(ARMT),
332 Z7_BRANCH_CONV_ENC(SPARC),
333 Z7_BRANCH_CONV_ENC(ARM64)
334};
335
336static SizeT XzBcFilterStateBase_Filter_Enc(CXzBcFilterStateBase *p, Byte *data, SizeT size)
337{
338 switch (p->methodId)
339 {
340 case XZ_ID_Delta:
341 Delta_Encode(p->delta_State, p->delta, data, size);
342 break;
343 case XZ_ID_X86:
344 size = (SizeT)(z7_BranchConvSt_X86_Enc(data, size, p->ip, &p->X86_State) - data);
345 break;
346 default:
347 if (p->methodId >= XZ_ID_PPC)
348 {
349 const UInt32 i = p->methodId - XZ_ID_PPC;
350 if (i < Z7_ARRAY_SIZE(g_Funcs_BranchConv_RISC_Enc))
351 size = (SizeT)(g_Funcs_BranchConv_RISC_Enc[i](data, size, p->ip) - data);
352 }
353 break;
354 }
355 p->ip += (UInt32)size;
356 return size;
357}
358
327 359
328static SRes SeqInFilter_Init(CSeqInFilter *p, const CXzFilter *props, ISzAllocPtr alloc) 360static SRes SeqInFilter_Init(CSeqInFilter *p, const CXzFilter *props, ISzAllocPtr alloc)
329{ 361{
@@ -335,17 +367,17 @@ static SRes SeqInFilter_Init(CSeqInFilter *p, const CXzFilter *props, ISzAllocPt
335 } 367 }
336 p->curPos = p->endPos = 0; 368 p->curPos = p->endPos = 0;
337 p->srcWasFinished = 0; 369 p->srcWasFinished = 0;
338 RINOK(BraState_SetFromMethod(&p->StateCoder, props->id, 1, alloc)); 370 RINOK(Xz_StateCoder_Bc_SetFromMethod_Func(&p->StateCoder, props->id, XzBcFilterStateBase_Filter_Enc, alloc))
339 RINOK(p->StateCoder.SetProps(p->StateCoder.p, props->props, props->propsSize, alloc)); 371 RINOK(p->StateCoder.SetProps(p->StateCoder.p, props->props, props->propsSize, alloc))
340 p->StateCoder.Init(p->StateCoder.p); 372 p->StateCoder.Init(p->StateCoder.p);
341 return SZ_OK; 373 return SZ_OK;
342} 374}
343 375
344 376
345static SRes SeqInFilter_Read(const ISeqInStream *pp, void *data, size_t *size) 377static SRes SeqInFilter_Read(ISeqInStreamPtr pp, void *data, size_t *size)
346{ 378{
347 CSeqInFilter *p = CONTAINER_FROM_VTBL(pp, CSeqInFilter, p); 379 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSeqInFilter)
348 size_t sizeOriginal = *size; 380 const size_t sizeOriginal = *size;
349 if (sizeOriginal == 0) 381 if (sizeOriginal == 0)
350 return SZ_OK; 382 return SZ_OK;
351 *size = 0; 383 *size = 0;
@@ -356,7 +388,7 @@ static SRes SeqInFilter_Read(const ISeqInStream *pp, void *data, size_t *size)
356 { 388 {
357 p->curPos = 0; 389 p->curPos = 0;
358 p->endPos = FILTER_BUF_SIZE; 390 p->endPos = FILTER_BUF_SIZE;
359 RINOK(ISeqInStream_Read(p->realStream, p->buf, &p->endPos)); 391 RINOK(ISeqInStream_Read(p->realStream, p->buf, &p->endPos))
360 if (p->endPos == 0) 392 if (p->endPos == 0)
361 p->srcWasFinished = 1; 393 p->srcWasFinished = 1;
362 } 394 }
@@ -381,7 +413,7 @@ static void SeqInFilter_Construct(CSeqInFilter *p)
381{ 413{
382 p->buf = NULL; 414 p->buf = NULL;
383 p->StateCoder.p = NULL; 415 p->StateCoder.p = NULL;
384 p->p.Read = SeqInFilter_Read; 416 p->vt.Read = SeqInFilter_Read;
385} 417}
386 418
387static void SeqInFilter_Free(CSeqInFilter *p, ISzAllocPtr alloc) 419static void SeqInFilter_Free(CSeqInFilter *p, ISzAllocPtr alloc)
@@ -406,13 +438,13 @@ static void SeqInFilter_Free(CSeqInFilter *p, ISzAllocPtr alloc)
406typedef struct 438typedef struct
407{ 439{
408 ISeqInStream vt; 440 ISeqInStream vt;
409 ISeqInStream *inStream; 441 ISeqInStreamPtr inStream;
410 CSbEnc enc; 442 CSbEnc enc;
411} CSbEncInStream; 443} CSbEncInStream;
412 444
413static SRes SbEncInStream_Read(const ISeqInStream *pp, void *data, size_t *size) 445static SRes SbEncInStream_Read(ISeqInStreamPtr pp, void *data, size_t *size)
414{ 446{
415 CSbEncInStream *p = CONTAINER_FROM_VTBL(pp, CSbEncInStream, vt); 447 CSbEncInStream *p = Z7_CONTAINER_FROM_VTBL(pp, CSbEncInStream, vt);
416 size_t sizeOriginal = *size; 448 size_t sizeOriginal = *size;
417 if (sizeOriginal == 0) 449 if (sizeOriginal == 0)
418 return SZ_OK; 450 return SZ_OK;
@@ -422,7 +454,7 @@ static SRes SbEncInStream_Read(const ISeqInStream *pp, void *data, size_t *size)
422 if (p->enc.needRead && !p->enc.readWasFinished) 454 if (p->enc.needRead && !p->enc.readWasFinished)
423 { 455 {
424 size_t processed = p->enc.needReadSizeMax; 456 size_t processed = p->enc.needReadSizeMax;
425 RINOK(p->inStream->Read(p->inStream, p->enc.buf + p->enc.readPos, &processed)); 457 RINOK(p->inStream->Read(p->inStream, p->enc.buf + p->enc.readPos, &processed))
426 p->enc.readPos += processed; 458 p->enc.readPos += processed;
427 if (processed == 0) 459 if (processed == 0)
428 { 460 {
@@ -433,7 +465,7 @@ static SRes SbEncInStream_Read(const ISeqInStream *pp, void *data, size_t *size)
433 } 465 }
434 466
435 *size = sizeOriginal; 467 *size = sizeOriginal;
436 RINOK(SbEnc_Read(&p->enc, data, size)); 468 RINOK(SbEnc_Read(&p->enc, data, size))
437 if (*size != 0 || !p->enc.needRead) 469 if (*size != 0 || !p->enc.needRead)
438 return SZ_OK; 470 return SZ_OK;
439 } 471 }
@@ -473,7 +505,7 @@ void XzFilterProps_Init(CXzFilterProps *p)
473void XzProps_Init(CXzProps *p) 505void XzProps_Init(CXzProps *p)
474{ 506{
475 p->checkId = XZ_CHECK_CRC32; 507 p->checkId = XZ_CHECK_CRC32;
476 p->blockSize = XZ_PROPS__BLOCK_SIZE__AUTO; 508 p->blockSize = XZ_PROPS_BLOCK_SIZE_AUTO;
477 p->numBlockThreads_Reduced = -1; 509 p->numBlockThreads_Reduced = -1;
478 p->numBlockThreads_Max = -1; 510 p->numBlockThreads_Max = -1;
479 p->numTotalThreads = -1; 511 p->numTotalThreads = -1;
@@ -502,8 +534,8 @@ static void XzEncProps_Normalize_Fixed(CXzProps *p)
502 t2 = p->numBlockThreads_Max; 534 t2 = p->numBlockThreads_Max;
503 t3 = p->numTotalThreads; 535 t3 = p->numTotalThreads;
504 536
505 if (t2 > MTCODER__THREADS_MAX) 537 if (t2 > MTCODER_THREADS_MAX)
506 t2 = MTCODER__THREADS_MAX; 538 t2 = MTCODER_THREADS_MAX;
507 539
508 if (t3 <= 0) 540 if (t3 <= 0)
509 { 541 {
@@ -519,8 +551,8 @@ static void XzEncProps_Normalize_Fixed(CXzProps *p)
519 t1 = 1; 551 t1 = 1;
520 t2 = t3; 552 t2 = t3;
521 } 553 }
522 if (t2 > MTCODER__THREADS_MAX) 554 if (t2 > MTCODER_THREADS_MAX)
523 t2 = MTCODER__THREADS_MAX; 555 t2 = MTCODER_THREADS_MAX;
524 } 556 }
525 else if (t1 <= 0) 557 else if (t1 <= 0)
526 { 558 {
@@ -571,7 +603,7 @@ static void XzProps_Normalize(CXzProps *p)
571 /* we normalize xzProps properties, but we normalize only some of CXzProps::lzma2Props properties. 603 /* we normalize xzProps properties, but we normalize only some of CXzProps::lzma2Props properties.
572 Lzma2Enc_SetProps() will normalize lzma2Props later. */ 604 Lzma2Enc_SetProps() will normalize lzma2Props later. */
573 605
574 if (p->blockSize == XZ_PROPS__BLOCK_SIZE__SOLID) 606 if (p->blockSize == XZ_PROPS_BLOCK_SIZE_SOLID)
575 { 607 {
576 p->lzma2Props.lzmaProps.reduceSize = p->reduceSize; 608 p->lzma2Props.lzmaProps.reduceSize = p->reduceSize;
577 p->numBlockThreads_Reduced = 1; 609 p->numBlockThreads_Reduced = 1;
@@ -583,15 +615,15 @@ static void XzProps_Normalize(CXzProps *p)
583 else 615 else
584 { 616 {
585 CLzma2EncProps *lzma2 = &p->lzma2Props; 617 CLzma2EncProps *lzma2 = &p->lzma2Props;
586 if (p->blockSize == LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO) 618 if (p->blockSize == LZMA2_ENC_PROPS_BLOCK_SIZE_AUTO)
587 { 619 {
588 // xz-auto 620 // xz-auto
589 p->lzma2Props.lzmaProps.reduceSize = p->reduceSize; 621 p->lzma2Props.lzmaProps.reduceSize = p->reduceSize;
590 622
591 if (lzma2->blockSize == LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID) 623 if (lzma2->blockSize == LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID)
592 { 624 {
593 // if (xz-auto && lzma2-solid) - we use solid for both 625 // if (xz-auto && lzma2-solid) - we use solid for both
594 p->blockSize = XZ_PROPS__BLOCK_SIZE__SOLID; 626 p->blockSize = XZ_PROPS_BLOCK_SIZE_SOLID;
595 p->numBlockThreads_Reduced = 1; 627 p->numBlockThreads_Reduced = 1;
596 p->numBlockThreads_Max = 1; 628 p->numBlockThreads_Max = 1;
597 if (p->lzma2Props.numTotalThreads <= 0) 629 if (p->lzma2Props.numTotalThreads <= 0)
@@ -610,9 +642,9 @@ static void XzProps_Normalize(CXzProps *p)
610 p->blockSize = tp.blockSize; // fixed or solid 642 p->blockSize = tp.blockSize; // fixed or solid
611 p->numBlockThreads_Reduced = tp.numBlockThreads_Reduced; 643 p->numBlockThreads_Reduced = tp.numBlockThreads_Reduced;
612 p->numBlockThreads_Max = tp.numBlockThreads_Max; 644 p->numBlockThreads_Max = tp.numBlockThreads_Max;
613 if (lzma2->blockSize == LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO) 645 if (lzma2->blockSize == LZMA2_ENC_PROPS_BLOCK_SIZE_AUTO)
614 lzma2->blockSize = tp.blockSize; // fixed or solid, LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID 646 lzma2->blockSize = tp.blockSize; // fixed or solid, LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID
615 if (lzma2->lzmaProps.reduceSize > tp.blockSize && tp.blockSize != LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID) 647 if (lzma2->lzmaProps.reduceSize > tp.blockSize && tp.blockSize != LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID)
616 lzma2->lzmaProps.reduceSize = tp.blockSize; 648 lzma2->lzmaProps.reduceSize = tp.blockSize;
617 lzma2->numBlockThreads_Reduced = 1; 649 lzma2->numBlockThreads_Reduced = 1;
618 lzma2->numBlockThreads_Max = 1; 650 lzma2->numBlockThreads_Max = 1;
@@ -631,9 +663,9 @@ static void XzProps_Normalize(CXzProps *p)
631 r = p->blockSize; 663 r = p->blockSize;
632 lzma2->lzmaProps.reduceSize = r; 664 lzma2->lzmaProps.reduceSize = r;
633 } 665 }
634 if (lzma2->blockSize == LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO) 666 if (lzma2->blockSize == LZMA2_ENC_PROPS_BLOCK_SIZE_AUTO)
635 lzma2->blockSize = LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID; 667 lzma2->blockSize = LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID;
636 else if (lzma2->blockSize > p->blockSize && lzma2->blockSize != LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID) 668 else if (lzma2->blockSize > p->blockSize && lzma2->blockSize != LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID)
637 lzma2->blockSize = p->blockSize; 669 lzma2->blockSize = p->blockSize;
638 670
639 XzEncProps_Normalize_Fixed(p); 671 XzEncProps_Normalize_Fixed(p);
@@ -704,17 +736,17 @@ typedef struct
704static SRes Xz_CompressBlock( 736static SRes Xz_CompressBlock(
705 CLzma2WithFilters *lzmaf, 737 CLzma2WithFilters *lzmaf,
706 738
707 ISeqOutStream *outStream, 739 ISeqOutStreamPtr outStream,
708 Byte *outBufHeader, 740 Byte *outBufHeader,
709 Byte *outBufData, size_t outBufDataLimit, 741 Byte *outBufData, size_t outBufDataLimit,
710 742
711 ISeqInStream *inStream, 743 ISeqInStreamPtr inStream,
712 // UInt64 expectedSize, 744 // UInt64 expectedSize,
713 const Byte *inBuf, // used if (!inStream) 745 const Byte *inBuf, // used if (!inStream)
714 size_t inBufSize, // used if (!inStream), it's block size, props->blockSize is ignored 746 size_t inBufSize, // used if (!inStream), it's block size, props->blockSize is ignored
715 747
716 const CXzProps *props, 748 const CXzProps *props,
717 ICompressProgress *progress, 749 ICompressProgressPtr progress,
718 int *inStreamFinished, /* only for inStream version */ 750 int *inStreamFinished, /* only for inStream version */
719 CXzEncBlockInfo *blockSizes, 751 CXzEncBlockInfo *blockSizes,
720 ISzAllocPtr alloc, 752 ISzAllocPtr alloc,
@@ -731,12 +763,12 @@ static SRes Xz_CompressBlock(
731 763
732 *inStreamFinished = False; 764 *inStreamFinished = False;
733 765
734 RINOK(Lzma2WithFilters_Create(lzmaf, alloc, allocBig)); 766 RINOK(Lzma2WithFilters_Create(lzmaf, alloc, allocBig))
735 767
736 RINOK(Lzma2Enc_SetProps(lzmaf->lzma2, &props->lzma2Props)); 768 RINOK(Lzma2Enc_SetProps(lzmaf->lzma2, &props->lzma2Props))
737 769
738 XzBlock_ClearFlags(&block); 770 // XzBlock_ClearFlags(&block)
739 XzBlock_SetNumFilters(&block, 1 + (fp ? 1 : 0)); 771 XzBlock_ClearFlags_SetNumFilters(&block, 1 + (fp ? 1 : 0))
740 772
741 if (fp) 773 if (fp)
742 { 774 {
@@ -752,7 +784,7 @@ static SRes Xz_CompressBlock(
752 else if (fp->ipDefined) 784 else if (fp->ipDefined)
753 { 785 {
754 Byte *ptr = filter->props; 786 Byte *ptr = filter->props;
755 SetUi32(ptr, fp->ip); 787 SetUi32(ptr, fp->ip)
756 filter->propsSize = 4; 788 filter->propsSize = 4;
757 } 789 }
758 } 790 }
@@ -777,13 +809,13 @@ static SRes Xz_CompressBlock(
777 if (props->blockSize != (UInt64)(Int64)-1) 809 if (props->blockSize != (UInt64)(Int64)-1)
778 if (expectedSize > props->blockSize) 810 if (expectedSize > props->blockSize)
779 block.unpackSize = props->blockSize; 811 block.unpackSize = props->blockSize;
780 XzBlock_SetHasUnpackSize(&block); 812 XzBlock_SetHasUnpackSize(&block)
781 } 813 }
782 */ 814 */
783 815
784 if (outStream) 816 if (outStream)
785 { 817 {
786 RINOK(XzBlock_WriteHeader(&block, &seqSizeOutStream.vt)); 818 RINOK(XzBlock_WriteHeader(&block, &seqSizeOutStream.vt))
787 } 819 }
788 820
789 checkInStream.vt.Read = SeqCheckInStream_Read; 821 checkInStream.vt.Read = SeqCheckInStream_Read;
@@ -801,13 +833,13 @@ static SRes Xz_CompressBlock(
801 if (fp->id == XZ_ID_Subblock) 833 if (fp->id == XZ_ID_Subblock)
802 { 834 {
803 lzmaf->sb.inStream = &checkInStream.vt; 835 lzmaf->sb.inStream = &checkInStream.vt;
804 RINOK(SbEncInStream_Init(&lzmaf->sb)); 836 RINOK(SbEncInStream_Init(&lzmaf->sb))
805 } 837 }
806 else 838 else
807 #endif 839 #endif
808 { 840 {
809 lzmaf->filter.realStream = &checkInStream.vt; 841 lzmaf->filter.realStream = &checkInStream.vt;
810 RINOK(SeqInFilter_Init(&lzmaf->filter, filter, alloc)); 842 RINOK(SeqInFilter_Init(&lzmaf->filter, filter, alloc))
811 } 843 }
812 } 844 }
813 845
@@ -841,7 +873,7 @@ static SRes Xz_CompressBlock(
841 #ifdef USE_SUBBLOCK 873 #ifdef USE_SUBBLOCK
842 (fp->id == XZ_ID_Subblock) ? &lzmaf->sb.vt: 874 (fp->id == XZ_ID_Subblock) ? &lzmaf->sb.vt:
843 #endif 875 #endif
844 &lzmaf->filter.p) : 876 &lzmaf->filter.vt) :
845 &checkInStream.vt) : NULL, 877 &checkInStream.vt) : NULL,
846 878
847 useStream ? NULL : inBuf, 879 useStream ? NULL : inBuf,
@@ -852,7 +884,7 @@ static SRes Xz_CompressBlock(
852 if (outBuf) 884 if (outBuf)
853 seqSizeOutStream.processed += outSize; 885 seqSizeOutStream.processed += outSize;
854 886
855 RINOK(res); 887 RINOK(res)
856 blockSizes->unpackSize = checkInStream.processed; 888 blockSizes->unpackSize = checkInStream.processed;
857 } 889 }
858 { 890 {
@@ -866,7 +898,7 @@ static SRes Xz_CompressBlock(
866 buf[3] = 0; 898 buf[3] = 0;
867 899
868 SeqCheckInStream_GetDigest(&checkInStream, buf + 4); 900 SeqCheckInStream_GetDigest(&checkInStream, buf + 4);
869 RINOK(WriteBytes(&seqSizeOutStream.vt, buf + (4 - padSize), padSize + XzFlags_GetCheckSize((CXzStreamFlags)props->checkId))); 901 RINOK(WriteBytes(&seqSizeOutStream.vt, buf + (4 - padSize), padSize + XzFlags_GetCheckSize((CXzStreamFlags)props->checkId)))
870 902
871 blockSizes->totalSize = seqSizeOutStream.processed - padSize; 903 blockSizes->totalSize = seqSizeOutStream.processed - padSize;
872 904
@@ -877,12 +909,12 @@ static SRes Xz_CompressBlock(
877 seqSizeOutStream.processed = 0; 909 seqSizeOutStream.processed = 0;
878 910
879 block.unpackSize = blockSizes->unpackSize; 911 block.unpackSize = blockSizes->unpackSize;
880 XzBlock_SetHasUnpackSize(&block); 912 XzBlock_SetHasUnpackSize(&block)
881 913
882 block.packSize = packSize; 914 block.packSize = packSize;
883 XzBlock_SetHasPackSize(&block); 915 XzBlock_SetHasPackSize(&block)
884 916
885 RINOK(XzBlock_WriteHeader(&block, &seqSizeOutStream.vt)); 917 RINOK(XzBlock_WriteHeader(&block, &seqSizeOutStream.vt))
886 918
887 blockSizes->headerSize = (size_t)seqSizeOutStream.processed; 919 blockSizes->headerSize = (size_t)seqSizeOutStream.processed;
888 blockSizes->totalSize += seqSizeOutStream.processed; 920 blockSizes->totalSize += seqSizeOutStream.processed;
@@ -906,15 +938,15 @@ static SRes Xz_CompressBlock(
906typedef struct 938typedef struct
907{ 939{
908 ICompressProgress vt; 940 ICompressProgress vt;
909 ICompressProgress *progress; 941 ICompressProgressPtr progress;
910 UInt64 inOffset; 942 UInt64 inOffset;
911 UInt64 outOffset; 943 UInt64 outOffset;
912} CCompressProgress_XzEncOffset; 944} CCompressProgress_XzEncOffset;
913 945
914 946
915static SRes CompressProgress_XzEncOffset_Progress(const ICompressProgress *pp, UInt64 inSize, UInt64 outSize) 947static SRes CompressProgress_XzEncOffset_Progress(ICompressProgressPtr pp, UInt64 inSize, UInt64 outSize)
916{ 948{
917 const CCompressProgress_XzEncOffset *p = CONTAINER_FROM_VTBL(pp, CCompressProgress_XzEncOffset, vt); 949 const CCompressProgress_XzEncOffset *p = Z7_CONTAINER_FROM_VTBL_CONST(pp, CCompressProgress_XzEncOffset, vt);
918 inSize += p->inOffset; 950 inSize += p->inOffset;
919 outSize += p->outOffset; 951 outSize += p->outOffset;
920 return ICompressProgress_Progress(p->progress, inSize, outSize); 952 return ICompressProgress_Progress(p->progress, inSize, outSize);
@@ -923,7 +955,7 @@ static SRes CompressProgress_XzEncOffset_Progress(const ICompressProgress *pp, U
923 955
924 956
925 957
926typedef struct 958struct CXzEnc
927{ 959{
928 ISzAllocPtr alloc; 960 ISzAllocPtr alloc;
929 ISzAllocPtr allocBig; 961 ISzAllocPtr allocBig;
@@ -933,20 +965,19 @@ typedef struct
933 965
934 CXzEncIndex xzIndex; 966 CXzEncIndex xzIndex;
935 967
936 CLzma2WithFilters lzmaf_Items[MTCODER__THREADS_MAX]; 968 CLzma2WithFilters lzmaf_Items[MTCODER_THREADS_MAX];
937 969
938 size_t outBufSize; /* size of allocated outBufs[i] */ 970 size_t outBufSize; /* size of allocated outBufs[i] */
939 Byte *outBufs[MTCODER__BLOCKS_MAX]; 971 Byte *outBufs[MTCODER_BLOCKS_MAX];
940 972
941 #ifndef _7ZIP_ST 973 #ifndef Z7_ST
942 unsigned checkType; 974 unsigned checkType;
943 ISeqOutStream *outStream; 975 ISeqOutStreamPtr outStream;
944 BoolInt mtCoder_WasConstructed; 976 BoolInt mtCoder_WasConstructed;
945 CMtCoder mtCoder; 977 CMtCoder mtCoder;
946 CXzEncBlockInfo EncBlocks[MTCODER__BLOCKS_MAX]; 978 CXzEncBlockInfo EncBlocks[MTCODER_BLOCKS_MAX];
947 #endif 979 #endif
948 980};
949} CXzEnc;
950 981
951 982
952static void XzEnc_Construct(CXzEnc *p) 983static void XzEnc_Construct(CXzEnc *p)
@@ -955,13 +986,13 @@ static void XzEnc_Construct(CXzEnc *p)
955 986
956 XzEncIndex_Construct(&p->xzIndex); 987 XzEncIndex_Construct(&p->xzIndex);
957 988
958 for (i = 0; i < MTCODER__THREADS_MAX; i++) 989 for (i = 0; i < MTCODER_THREADS_MAX; i++)
959 Lzma2WithFilters_Construct(&p->lzmaf_Items[i]); 990 Lzma2WithFilters_Construct(&p->lzmaf_Items[i]);
960 991
961 #ifndef _7ZIP_ST 992 #ifndef Z7_ST
962 p->mtCoder_WasConstructed = False; 993 p->mtCoder_WasConstructed = False;
963 { 994 {
964 for (i = 0; i < MTCODER__BLOCKS_MAX; i++) 995 for (i = 0; i < MTCODER_BLOCKS_MAX; i++)
965 p->outBufs[i] = NULL; 996 p->outBufs[i] = NULL;
966 p->outBufSize = 0; 997 p->outBufSize = 0;
967 } 998 }
@@ -972,7 +1003,7 @@ static void XzEnc_Construct(CXzEnc *p)
972static void XzEnc_FreeOutBufs(CXzEnc *p) 1003static void XzEnc_FreeOutBufs(CXzEnc *p)
973{ 1004{
974 unsigned i; 1005 unsigned i;
975 for (i = 0; i < MTCODER__BLOCKS_MAX; i++) 1006 for (i = 0; i < MTCODER_BLOCKS_MAX; i++)
976 if (p->outBufs[i]) 1007 if (p->outBufs[i])
977 { 1008 {
978 ISzAlloc_Free(p->alloc, p->outBufs[i]); 1009 ISzAlloc_Free(p->alloc, p->outBufs[i]);
@@ -988,10 +1019,10 @@ static void XzEnc_Free(CXzEnc *p, ISzAllocPtr alloc)
988 1019
989 XzEncIndex_Free(&p->xzIndex, alloc); 1020 XzEncIndex_Free(&p->xzIndex, alloc);
990 1021
991 for (i = 0; i < MTCODER__THREADS_MAX; i++) 1022 for (i = 0; i < MTCODER_THREADS_MAX; i++)
992 Lzma2WithFilters_Free(&p->lzmaf_Items[i], alloc); 1023 Lzma2WithFilters_Free(&p->lzmaf_Items[i], alloc);
993 1024
994 #ifndef _7ZIP_ST 1025 #ifndef Z7_ST
995 if (p->mtCoder_WasConstructed) 1026 if (p->mtCoder_WasConstructed)
996 { 1027 {
997 MtCoder_Destruct(&p->mtCoder); 1028 MtCoder_Destruct(&p->mtCoder);
@@ -1013,37 +1044,38 @@ CXzEncHandle XzEnc_Create(ISzAllocPtr alloc, ISzAllocPtr allocBig)
1013 p->expectedDataSize = (UInt64)(Int64)-1; 1044 p->expectedDataSize = (UInt64)(Int64)-1;
1014 p->alloc = alloc; 1045 p->alloc = alloc;
1015 p->allocBig = allocBig; 1046 p->allocBig = allocBig;
1016 return p; 1047 return (CXzEncHandle)p;
1017} 1048}
1018 1049
1050// #define GET_CXzEnc_p CXzEnc *p = (CXzEnc *)(void *)pp;
1019 1051
1020void XzEnc_Destroy(CXzEncHandle pp) 1052void XzEnc_Destroy(CXzEncHandle p)
1021{ 1053{
1022 CXzEnc *p = (CXzEnc *)pp; 1054 // GET_CXzEnc_p
1023 XzEnc_Free(p, p->alloc); 1055 XzEnc_Free(p, p->alloc);
1024 ISzAlloc_Free(p->alloc, p); 1056 ISzAlloc_Free(p->alloc, p);
1025} 1057}
1026 1058
1027 1059
1028SRes XzEnc_SetProps(CXzEncHandle pp, const CXzProps *props) 1060SRes XzEnc_SetProps(CXzEncHandle p, const CXzProps *props)
1029{ 1061{
1030 CXzEnc *p = (CXzEnc *)pp; 1062 // GET_CXzEnc_p
1031 p->xzProps = *props; 1063 p->xzProps = *props;
1032 XzProps_Normalize(&p->xzProps); 1064 XzProps_Normalize(&p->xzProps);
1033 return SZ_OK; 1065 return SZ_OK;
1034} 1066}
1035 1067
1036 1068
1037void XzEnc_SetDataSize(CXzEncHandle pp, UInt64 expectedDataSiize) 1069void XzEnc_SetDataSize(CXzEncHandle p, UInt64 expectedDataSiize)
1038{ 1070{
1039 CXzEnc *p = (CXzEnc *)pp; 1071 // GET_CXzEnc_p
1040 p->expectedDataSize = expectedDataSiize; 1072 p->expectedDataSize = expectedDataSiize;
1041} 1073}
1042 1074
1043 1075
1044 1076
1045 1077
1046#ifndef _7ZIP_ST 1078#ifndef Z7_ST
1047 1079
1048static SRes XzEnc_MtCallback_Code(void *pp, unsigned coderIndex, unsigned outBufIndex, 1080static SRes XzEnc_MtCallback_Code(void *pp, unsigned coderIndex, unsigned outBufIndex,
1049 const Byte *src, size_t srcSize, int finished) 1081 const Byte *src, size_t srcSize, int finished)
@@ -1073,7 +1105,7 @@ static SRes XzEnc_MtCallback_Code(void *pp, unsigned coderIndex, unsigned outBuf
1073 1105
1074 MtProgressThunk_CreateVTable(&progressThunk); 1106 MtProgressThunk_CreateVTable(&progressThunk);
1075 progressThunk.mtProgress = &me->mtCoder.mtProgress; 1107 progressThunk.mtProgress = &me->mtCoder.mtProgress;
1076 MtProgressThunk_Init(&progressThunk); 1108 MtProgressThunk_INIT(&progressThunk)
1077 1109
1078 { 1110 {
1079 CXzEncBlockInfo blockSizes; 1111 CXzEncBlockInfo blockSizes;
@@ -1112,11 +1144,11 @@ static SRes XzEnc_MtCallback_Write(void *pp, unsigned outBufIndex)
1112 const CXzEncBlockInfo *bInfo = &me->EncBlocks[outBufIndex]; 1144 const CXzEncBlockInfo *bInfo = &me->EncBlocks[outBufIndex];
1113 const Byte *data = me->outBufs[outBufIndex]; 1145 const Byte *data = me->outBufs[outBufIndex];
1114 1146
1115 RINOK(WriteBytes(me->outStream, data, bInfo->headerSize)); 1147 RINOK(WriteBytes(me->outStream, data, bInfo->headerSize))
1116 1148
1117 { 1149 {
1118 UInt64 totalPackFull = bInfo->totalSize + XZ_GET_PAD_SIZE(bInfo->totalSize); 1150 UInt64 totalPackFull = bInfo->totalSize + XZ_GET_PAD_SIZE(bInfo->totalSize);
1119 RINOK(WriteBytes(me->outStream, data + XZ_BLOCK_HEADER_SIZE_MAX, (size_t)totalPackFull - bInfo->headerSize)); 1151 RINOK(WriteBytes(me->outStream, data + XZ_BLOCK_HEADER_SIZE_MAX, (size_t)totalPackFull - bInfo->headerSize))
1120 } 1152 }
1121 1153
1122 return XzEncIndex_AddIndexRecord(&me->xzIndex, bInfo->unpackSize, bInfo->totalSize, me->alloc); 1154 return XzEncIndex_AddIndexRecord(&me->xzIndex, bInfo->unpackSize, bInfo->totalSize, me->alloc);
@@ -1126,9 +1158,9 @@ static SRes XzEnc_MtCallback_Write(void *pp, unsigned outBufIndex)
1126 1158
1127 1159
1128 1160
1129SRes XzEnc_Encode(CXzEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress) 1161SRes XzEnc_Encode(CXzEncHandle p, ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream, ICompressProgressPtr progress)
1130{ 1162{
1131 CXzEnc *p = (CXzEnc *)pp; 1163 // GET_CXzEnc_p
1132 1164
1133 const CXzProps *props = &p->xzProps; 1165 const CXzProps *props = &p->xzProps;
1134 1166
@@ -1137,7 +1169,7 @@ SRes XzEnc_Encode(CXzEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStr
1137 UInt64 numBlocks = 1; 1169 UInt64 numBlocks = 1;
1138 UInt64 blockSize = props->blockSize; 1170 UInt64 blockSize = props->blockSize;
1139 1171
1140 if (blockSize != XZ_PROPS__BLOCK_SIZE__SOLID 1172 if (blockSize != XZ_PROPS_BLOCK_SIZE_SOLID
1141 && props->reduceSize != (UInt64)(Int64)-1) 1173 && props->reduceSize != (UInt64)(Int64)-1)
1142 { 1174 {
1143 numBlocks = props->reduceSize / blockSize; 1175 numBlocks = props->reduceSize / blockSize;
@@ -1147,13 +1179,13 @@ SRes XzEnc_Encode(CXzEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStr
1147 else 1179 else
1148 blockSize = (UInt64)1 << 62; 1180 blockSize = (UInt64)1 << 62;
1149 1181
1150 RINOK(XzEncIndex_PreAlloc(&p->xzIndex, numBlocks, blockSize, XZ_GET_ESTIMATED_BLOCK_TOTAL_PACK_SIZE(blockSize), p->alloc)); 1182 RINOK(XzEncIndex_PreAlloc(&p->xzIndex, numBlocks, blockSize, XZ_GET_ESTIMATED_BLOCK_TOTAL_PACK_SIZE(blockSize), p->alloc))
1151 } 1183 }
1152 1184
1153 RINOK(Xz_WriteHeader((CXzStreamFlags)props->checkId, outStream)); 1185 RINOK(Xz_WriteHeader((CXzStreamFlags)props->checkId, outStream))
1154 1186
1155 1187
1156 #ifndef _7ZIP_ST 1188 #ifndef Z7_ST
1157 if (props->numBlockThreads_Reduced > 1) 1189 if (props->numBlockThreads_Reduced > 1)
1158 { 1190 {
1159 IMtCoderCallback2 vt; 1191 IMtCoderCallback2 vt;
@@ -1180,8 +1212,8 @@ SRes XzEnc_Encode(CXzEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStr
1180 p->mtCoder.mtCallback = &vt; 1212 p->mtCoder.mtCallback = &vt;
1181 p->mtCoder.mtCallbackObject = p; 1213 p->mtCoder.mtCallbackObject = p;
1182 1214
1183 if ( props->blockSize == XZ_PROPS__BLOCK_SIZE__SOLID 1215 if ( props->blockSize == XZ_PROPS_BLOCK_SIZE_SOLID
1184 || props->blockSize == XZ_PROPS__BLOCK_SIZE__AUTO) 1216 || props->blockSize == XZ_PROPS_BLOCK_SIZE_AUTO)
1185 return SZ_ERROR_FAIL; 1217 return SZ_ERROR_FAIL;
1186 1218
1187 p->mtCoder.blockSize = (size_t)props->blockSize; 1219 p->mtCoder.blockSize = (size_t)props->blockSize;
@@ -1200,7 +1232,7 @@ SRes XzEnc_Encode(CXzEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStr
1200 p->mtCoder.numThreadsMax = (unsigned)props->numBlockThreads_Max; 1232 p->mtCoder.numThreadsMax = (unsigned)props->numBlockThreads_Max;
1201 p->mtCoder.expectedDataSize = p->expectedDataSize; 1233 p->mtCoder.expectedDataSize = p->expectedDataSize;
1202 1234
1203 RINOK(MtCoder_Code(&p->mtCoder)); 1235 RINOK(MtCoder_Code(&p->mtCoder))
1204 } 1236 }
1205 else 1237 else
1206 #endif 1238 #endif
@@ -1217,7 +1249,7 @@ SRes XzEnc_Encode(CXzEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStr
1217 1249
1218 writeStartSizes = 0; 1250 writeStartSizes = 0;
1219 1251
1220 if (props->blockSize != XZ_PROPS__BLOCK_SIZE__SOLID) 1252 if (props->blockSize != XZ_PROPS_BLOCK_SIZE_SOLID)
1221 { 1253 {
1222 writeStartSizes = (props->forceWriteSizesInHeader > 0); 1254 writeStartSizes = (props->forceWriteSizesInHeader > 0);
1223 1255
@@ -1274,18 +1306,18 @@ SRes XzEnc_Encode(CXzEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStr
1274 &inStreamFinished, 1306 &inStreamFinished,
1275 &blockSizes, 1307 &blockSizes,
1276 p->alloc, 1308 p->alloc,
1277 p->allocBig)); 1309 p->allocBig))
1278 1310
1279 { 1311 {
1280 UInt64 totalPackFull = blockSizes.totalSize + XZ_GET_PAD_SIZE(blockSizes.totalSize); 1312 UInt64 totalPackFull = blockSizes.totalSize + XZ_GET_PAD_SIZE(blockSizes.totalSize);
1281 1313
1282 if (writeStartSizes) 1314 if (writeStartSizes)
1283 { 1315 {
1284 RINOK(WriteBytes(outStream, p->outBufs[0], blockSizes.headerSize)); 1316 RINOK(WriteBytes(outStream, p->outBufs[0], blockSizes.headerSize))
1285 RINOK(WriteBytes(outStream, bufData, (size_t)totalPackFull - blockSizes.headerSize)); 1317 RINOK(WriteBytes(outStream, bufData, (size_t)totalPackFull - blockSizes.headerSize))
1286 } 1318 }
1287 1319
1288 RINOK(XzEncIndex_AddIndexRecord(&p->xzIndex, blockSizes.unpackSize, blockSizes.totalSize, p->alloc)); 1320 RINOK(XzEncIndex_AddIndexRecord(&p->xzIndex, blockSizes.unpackSize, blockSizes.totalSize, p->alloc))
1289 1321
1290 progress2.inOffset += blockSizes.unpackSize; 1322 progress2.inOffset += blockSizes.unpackSize;
1291 progress2.outOffset += totalPackFull; 1323 progress2.outOffset += totalPackFull;
@@ -1302,8 +1334,8 @@ SRes XzEnc_Encode(CXzEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStr
1302 1334
1303#include "Alloc.h" 1335#include "Alloc.h"
1304 1336
1305SRes Xz_Encode(ISeqOutStream *outStream, ISeqInStream *inStream, 1337SRes Xz_Encode(ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream,
1306 const CXzProps *props, ICompressProgress *progress) 1338 const CXzProps *props, ICompressProgressPtr progress)
1307{ 1339{
1308 SRes res; 1340 SRes res;
1309 CXzEncHandle xz = XzEnc_Create(&g_Alloc, &g_BigAlloc); 1341 CXzEncHandle xz = XzEnc_Create(&g_Alloc, &g_BigAlloc);
@@ -1317,7 +1349,7 @@ SRes Xz_Encode(ISeqOutStream *outStream, ISeqInStream *inStream,
1317} 1349}
1318 1350
1319 1351
1320SRes Xz_EncodeEmpty(ISeqOutStream *outStream) 1352SRes Xz_EncodeEmpty(ISeqOutStreamPtr outStream)
1321{ 1353{
1322 SRes res; 1354 SRes res;
1323 CXzEncIndex xzIndex; 1355 CXzEncIndex xzIndex;
diff --git a/C/XzEnc.h b/C/XzEnc.h
index 0c29e7e..77b78c0 100644
--- a/C/XzEnc.h
+++ b/C/XzEnc.h
@@ -1,8 +1,8 @@
1/* XzEnc.h -- Xz Encode 1/* XzEnc.h -- Xz Encode
22017-06-27 : Igor Pavlov : Public domain */ 22023-04-13 : Igor Pavlov : Public domain */
3 3
4#ifndef __XZ_ENC_H 4#ifndef ZIP7_INC_XZ_ENC_H
5#define __XZ_ENC_H 5#define ZIP7_INC_XZ_ENC_H
6 6
7#include "Lzma2Enc.h" 7#include "Lzma2Enc.h"
8 8
@@ -11,8 +11,8 @@
11EXTERN_C_BEGIN 11EXTERN_C_BEGIN
12 12
13 13
14#define XZ_PROPS__BLOCK_SIZE__AUTO LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO 14#define XZ_PROPS_BLOCK_SIZE_AUTO LZMA2_ENC_PROPS_BLOCK_SIZE_AUTO
15#define XZ_PROPS__BLOCK_SIZE__SOLID LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID 15#define XZ_PROPS_BLOCK_SIZE_SOLID LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID
16 16
17 17
18typedef struct 18typedef struct
@@ -41,19 +41,20 @@ typedef struct
41 41
42void XzProps_Init(CXzProps *p); 42void XzProps_Init(CXzProps *p);
43 43
44 44typedef struct CXzEnc CXzEnc;
45typedef void * CXzEncHandle; 45typedef CXzEnc * CXzEncHandle;
46// Z7_DECLARE_HANDLE(CXzEncHandle)
46 47
47CXzEncHandle XzEnc_Create(ISzAllocPtr alloc, ISzAllocPtr allocBig); 48CXzEncHandle XzEnc_Create(ISzAllocPtr alloc, ISzAllocPtr allocBig);
48void XzEnc_Destroy(CXzEncHandle p); 49void XzEnc_Destroy(CXzEncHandle p);
49SRes XzEnc_SetProps(CXzEncHandle p, const CXzProps *props); 50SRes XzEnc_SetProps(CXzEncHandle p, const CXzProps *props);
50void XzEnc_SetDataSize(CXzEncHandle p, UInt64 expectedDataSiize); 51void XzEnc_SetDataSize(CXzEncHandle p, UInt64 expectedDataSiize);
51SRes XzEnc_Encode(CXzEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress); 52SRes XzEnc_Encode(CXzEncHandle p, ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream, ICompressProgressPtr progress);
52 53
53SRes Xz_Encode(ISeqOutStream *outStream, ISeqInStream *inStream, 54SRes Xz_Encode(ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream,
54 const CXzProps *props, ICompressProgress *progress); 55 const CXzProps *props, ICompressProgressPtr progress);
55 56
56SRes Xz_EncodeEmpty(ISeqOutStream *outStream); 57SRes Xz_EncodeEmpty(ISeqOutStreamPtr outStream);
57 58
58EXTERN_C_END 59EXTERN_C_END
59 60
diff --git a/C/XzIn.c b/C/XzIn.c
index 84f868e..d0fc763 100644
--- a/C/XzIn.c
+++ b/C/XzIn.c
@@ -1,5 +1,5 @@
1/* XzIn.c - Xz input 1/* XzIn.c - Xz input
22021-09-04 : Igor Pavlov : Public domain */ 22023-04-02 : Igor Pavlov : Public domain */
3 3
4#include "Precomp.h" 4#include "Precomp.h"
5 5
@@ -15,11 +15,13 @@
15#define XZ_FOOTER_SIG_CHECK(p) ((p)[0] == XZ_FOOTER_SIG_0 && (p)[1] == XZ_FOOTER_SIG_1) 15#define XZ_FOOTER_SIG_CHECK(p) ((p)[0] == XZ_FOOTER_SIG_0 && (p)[1] == XZ_FOOTER_SIG_1)
16 16
17 17
18SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream) 18SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStreamPtr inStream)
19{ 19{
20 Byte sig[XZ_STREAM_HEADER_SIZE]; 20 Byte sig[XZ_STREAM_HEADER_SIZE];
21 RINOK(SeqInStream_Read2(inStream, sig, XZ_STREAM_HEADER_SIZE, SZ_ERROR_NO_ARCHIVE)); 21 size_t processedSize = XZ_STREAM_HEADER_SIZE;
22 if (memcmp(sig, XZ_SIG, XZ_SIG_SIZE) != 0) 22 RINOK(SeqInStream_ReadMax(inStream, sig, &processedSize))
23 if (processedSize != XZ_STREAM_HEADER_SIZE
24 || memcmp(sig, XZ_SIG, XZ_SIG_SIZE) != 0)
23 return SZ_ERROR_NO_ARCHIVE; 25 return SZ_ERROR_NO_ARCHIVE;
24 return Xz_ParseHeader(p, sig); 26 return Xz_ParseHeader(p, sig);
25} 27}
@@ -29,12 +31,12 @@ SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream)
29 if (s == 0) return SZ_ERROR_ARCHIVE; \ 31 if (s == 0) return SZ_ERROR_ARCHIVE; \
30 pos += s; } 32 pos += s; }
31 33
32SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, BoolInt *isIndex, UInt32 *headerSizeRes) 34SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStreamPtr inStream, BoolInt *isIndex, UInt32 *headerSizeRes)
33{ 35{
34 Byte header[XZ_BLOCK_HEADER_SIZE_MAX]; 36 Byte header[XZ_BLOCK_HEADER_SIZE_MAX];
35 unsigned headerSize; 37 unsigned headerSize;
36 *headerSizeRes = 0; 38 *headerSizeRes = 0;
37 RINOK(SeqInStream_ReadByte(inStream, &header[0])); 39 RINOK(SeqInStream_ReadByte(inStream, &header[0]))
38 headerSize = (unsigned)header[0]; 40 headerSize = (unsigned)header[0];
39 if (headerSize == 0) 41 if (headerSize == 0)
40 { 42 {
@@ -46,7 +48,12 @@ SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, BoolInt *isIndex, U
46 *isIndex = False; 48 *isIndex = False;
47 headerSize = (headerSize << 2) + 4; 49 headerSize = (headerSize << 2) + 4;
48 *headerSizeRes = headerSize; 50 *headerSizeRes = headerSize;
49 RINOK(SeqInStream_Read(inStream, header + 1, headerSize - 1)); 51 {
52 size_t processedSize = headerSize - 1;
53 RINOK(SeqInStream_ReadMax(inStream, header + 1, &processedSize))
54 if (processedSize != headerSize - 1)
55 return SZ_ERROR_INPUT_EOF;
56 }
50 return XzBlock_Parse(p, header); 57 return XzBlock_Parse(p, header);
51} 58}
52 59
@@ -58,7 +65,9 @@ UInt64 Xz_GetUnpackSize(const CXzStream *p)
58 UInt64 size = 0; 65 UInt64 size = 0;
59 size_t i; 66 size_t i;
60 for (i = 0; i < p->numBlocks; i++) 67 for (i = 0; i < p->numBlocks; i++)
61 ADD_SIZE_CHECK(size, p->blocks[i].unpackSize); 68 {
69 ADD_SIZE_CHECK(size, p->blocks[i].unpackSize)
70 }
62 return size; 71 return size;
63} 72}
64 73
@@ -67,12 +76,14 @@ UInt64 Xz_GetPackSize(const CXzStream *p)
67 UInt64 size = 0; 76 UInt64 size = 0;
68 size_t i; 77 size_t i;
69 for (i = 0; i < p->numBlocks; i++) 78 for (i = 0; i < p->numBlocks; i++)
70 ADD_SIZE_CHECK(size, (p->blocks[i].totalSize + 3) & ~(UInt64)3); 79 {
80 ADD_SIZE_CHECK(size, (p->blocks[i].totalSize + 3) & ~(UInt64)3)
81 }
71 return size; 82 return size;
72} 83}
73 84
74/* 85/*
75SRes XzBlock_ReadFooter(CXzBlock *p, CXzStreamFlags f, ISeqInStream *inStream) 86SRes XzBlock_ReadFooter(CXzBlock *p, CXzStreamFlags f, ISeqInStreamPtr inStream)
76{ 87{
77 return SeqInStream_Read(inStream, p->check, XzFlags_GetCheckSize(f)); 88 return SeqInStream_Read(inStream, p->check, XzFlags_GetCheckSize(f));
78} 89}
@@ -93,7 +104,7 @@ static SRes Xz_ReadIndex2(CXzStream *p, const Byte *buf, size_t size, ISzAllocPt
93 104
94 { 105 {
95 UInt64 numBlocks64; 106 UInt64 numBlocks64;
96 READ_VARINT_AND_CHECK(buf, pos, size, &numBlocks64); 107 READ_VARINT_AND_CHECK(buf, pos, size, &numBlocks64)
97 numBlocks = (size_t)numBlocks64; 108 numBlocks = (size_t)numBlocks64;
98 if (numBlocks != numBlocks64 || numBlocks * 2 > size) 109 if (numBlocks != numBlocks64 || numBlocks * 2 > size)
99 return SZ_ERROR_ARCHIVE; 110 return SZ_ERROR_ARCHIVE;
@@ -110,8 +121,8 @@ static SRes Xz_ReadIndex2(CXzStream *p, const Byte *buf, size_t size, ISzAllocPt
110 for (i = 0; i < numBlocks; i++) 121 for (i = 0; i < numBlocks; i++)
111 { 122 {
112 CXzBlockSizes *block = &p->blocks[i]; 123 CXzBlockSizes *block = &p->blocks[i];
113 READ_VARINT_AND_CHECK(buf, pos, size, &block->totalSize); 124 READ_VARINT_AND_CHECK(buf, pos, size, &block->totalSize)
114 READ_VARINT_AND_CHECK(buf, pos, size, &block->unpackSize); 125 READ_VARINT_AND_CHECK(buf, pos, size, &block->unpackSize)
115 if (block->totalSize == 0) 126 if (block->totalSize == 0)
116 return SZ_ERROR_ARCHIVE; 127 return SZ_ERROR_ARCHIVE;
117 } 128 }
@@ -122,7 +133,7 @@ static SRes Xz_ReadIndex2(CXzStream *p, const Byte *buf, size_t size, ISzAllocPt
122 return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE; 133 return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
123} 134}
124 135
125static SRes Xz_ReadIndex(CXzStream *p, ILookInStream *stream, UInt64 indexSize, ISzAllocPtr alloc) 136static SRes Xz_ReadIndex(CXzStream *p, ILookInStreamPtr stream, UInt64 indexSize, ISzAllocPtr alloc)
126{ 137{
127 SRes res; 138 SRes res;
128 size_t size; 139 size_t size;
@@ -142,14 +153,14 @@ static SRes Xz_ReadIndex(CXzStream *p, ILookInStream *stream, UInt64 indexSize,
142 return res; 153 return res;
143} 154}
144 155
145static SRes LookInStream_SeekRead_ForArc(ILookInStream *stream, UInt64 offset, void *buf, size_t size) 156static SRes LookInStream_SeekRead_ForArc(ILookInStreamPtr stream, UInt64 offset, void *buf, size_t size)
146{ 157{
147 RINOK(LookInStream_SeekTo(stream, offset)); 158 RINOK(LookInStream_SeekTo(stream, offset))
148 return LookInStream_Read(stream, buf, size); 159 return LookInStream_Read(stream, buf, size);
149 /* return LookInStream_Read2(stream, buf, size, SZ_ERROR_NO_ARCHIVE); */ 160 /* return LookInStream_Read2(stream, buf, size, SZ_ERROR_NO_ARCHIVE); */
150} 161}
151 162
152static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOffset, ISzAllocPtr alloc) 163static SRes Xz_ReadBackward(CXzStream *p, ILookInStreamPtr stream, Int64 *startOffset, ISzAllocPtr alloc)
153{ 164{
154 UInt64 indexSize; 165 UInt64 indexSize;
155 Byte buf[XZ_STREAM_FOOTER_SIZE]; 166 Byte buf[XZ_STREAM_FOOTER_SIZE];
@@ -159,7 +170,7 @@ static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOff
159 return SZ_ERROR_NO_ARCHIVE; 170 return SZ_ERROR_NO_ARCHIVE;
160 171
161 pos -= XZ_STREAM_FOOTER_SIZE; 172 pos -= XZ_STREAM_FOOTER_SIZE;
162 RINOK(LookInStream_SeekRead_ForArc(stream, pos, buf, XZ_STREAM_FOOTER_SIZE)); 173 RINOK(LookInStream_SeekRead_ForArc(stream, pos, buf, XZ_STREAM_FOOTER_SIZE))
163 174
164 if (!XZ_FOOTER_SIG_CHECK(buf + 10)) 175 if (!XZ_FOOTER_SIG_CHECK(buf + 10))
165 { 176 {
@@ -174,7 +185,7 @@ static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOff
174 185
175 i = (pos > TEMP_BUF_SIZE) ? TEMP_BUF_SIZE : (size_t)pos; 186 i = (pos > TEMP_BUF_SIZE) ? TEMP_BUF_SIZE : (size_t)pos;
176 pos -= i; 187 pos -= i;
177 RINOK(LookInStream_SeekRead_ForArc(stream, pos, temp, i)); 188 RINOK(LookInStream_SeekRead_ForArc(stream, pos, temp, i))
178 total += (UInt32)i; 189 total += (UInt32)i;
179 for (; i != 0; i--) 190 for (; i != 0; i--)
180 if (temp[i - 1] != 0) 191 if (temp[i - 1] != 0)
@@ -193,7 +204,7 @@ static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOff
193 if (pos < XZ_STREAM_FOOTER_SIZE) 204 if (pos < XZ_STREAM_FOOTER_SIZE)
194 return SZ_ERROR_NO_ARCHIVE; 205 return SZ_ERROR_NO_ARCHIVE;
195 pos -= XZ_STREAM_FOOTER_SIZE; 206 pos -= XZ_STREAM_FOOTER_SIZE;
196 RINOK(LookInStream_SeekRead_ForArc(stream, pos, buf, XZ_STREAM_FOOTER_SIZE)); 207 RINOK(LookInStream_SeekRead_ForArc(stream, pos, buf, XZ_STREAM_FOOTER_SIZE))
197 if (!XZ_FOOTER_SIG_CHECK(buf + 10)) 208 if (!XZ_FOOTER_SIG_CHECK(buf + 10))
198 return SZ_ERROR_NO_ARCHIVE; 209 return SZ_ERROR_NO_ARCHIVE;
199 } 210 }
@@ -217,8 +228,8 @@ static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOff
217 return SZ_ERROR_ARCHIVE; 228 return SZ_ERROR_ARCHIVE;
218 229
219 pos -= indexSize; 230 pos -= indexSize;
220 RINOK(LookInStream_SeekTo(stream, pos)); 231 RINOK(LookInStream_SeekTo(stream, pos))
221 RINOK(Xz_ReadIndex(p, stream, indexSize, alloc)); 232 RINOK(Xz_ReadIndex(p, stream, indexSize, alloc))
222 233
223 { 234 {
224 UInt64 totalSize = Xz_GetPackSize(p); 235 UInt64 totalSize = Xz_GetPackSize(p);
@@ -227,7 +238,7 @@ static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOff
227 || pos < totalSize + XZ_STREAM_HEADER_SIZE) 238 || pos < totalSize + XZ_STREAM_HEADER_SIZE)
228 return SZ_ERROR_ARCHIVE; 239 return SZ_ERROR_ARCHIVE;
229 pos -= (totalSize + XZ_STREAM_HEADER_SIZE); 240 pos -= (totalSize + XZ_STREAM_HEADER_SIZE);
230 RINOK(LookInStream_SeekTo(stream, pos)); 241 RINOK(LookInStream_SeekTo(stream, pos))
231 *startOffset = (Int64)pos; 242 *startOffset = (Int64)pos;
232 } 243 }
233 { 244 {
@@ -236,7 +247,7 @@ static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOff
236 SecToRead_CreateVTable(&secToRead); 247 SecToRead_CreateVTable(&secToRead);
237 secToRead.realStream = stream; 248 secToRead.realStream = stream;
238 249
239 RINOK(Xz_ReadHeader(&headerFlags, &secToRead.vt)); 250 RINOK(Xz_ReadHeader(&headerFlags, &secToRead.vt))
240 return (p->flags == headerFlags) ? SZ_OK : SZ_ERROR_ARCHIVE; 251 return (p->flags == headerFlags) ? SZ_OK : SZ_ERROR_ARCHIVE;
241 } 252 }
242} 253}
@@ -274,7 +285,9 @@ UInt64 Xzs_GetUnpackSize(const CXzs *p)
274 UInt64 size = 0; 285 UInt64 size = 0;
275 size_t i; 286 size_t i;
276 for (i = 0; i < p->num; i++) 287 for (i = 0; i < p->num; i++)
277 ADD_SIZE_CHECK(size, Xz_GetUnpackSize(&p->streams[i])); 288 {
289 ADD_SIZE_CHECK(size, Xz_GetUnpackSize(&p->streams[i]))
290 }
278 return size; 291 return size;
279} 292}
280 293
@@ -284,15 +297,17 @@ UInt64 Xzs_GetPackSize(const CXzs *p)
284 UInt64 size = 0; 297 UInt64 size = 0;
285 size_t i; 298 size_t i;
286 for (i = 0; i < p->num; i++) 299 for (i = 0; i < p->num; i++)
287 ADD_SIZE_CHECK(size, Xz_GetTotalSize(&p->streams[i])); 300 {
301 ADD_SIZE_CHECK(size, Xz_GetTotalSize(&p->streams[i]))
302 }
288 return size; 303 return size;
289} 304}
290*/ 305*/
291 306
292SRes Xzs_ReadBackward(CXzs *p, ILookInStream *stream, Int64 *startOffset, ICompressProgress *progress, ISzAllocPtr alloc) 307SRes Xzs_ReadBackward(CXzs *p, ILookInStreamPtr stream, Int64 *startOffset, ICompressProgressPtr progress, ISzAllocPtr alloc)
293{ 308{
294 Int64 endOffset = 0; 309 Int64 endOffset = 0;
295 RINOK(ILookInStream_Seek(stream, &endOffset, SZ_SEEK_END)); 310 RINOK(ILookInStream_Seek(stream, &endOffset, SZ_SEEK_END))
296 *startOffset = endOffset; 311 *startOffset = endOffset;
297 for (;;) 312 for (;;)
298 { 313 {
@@ -301,7 +316,7 @@ SRes Xzs_ReadBackward(CXzs *p, ILookInStream *stream, Int64 *startOffset, ICompr
301 Xz_Construct(&st); 316 Xz_Construct(&st);
302 res = Xz_ReadBackward(&st, stream, startOffset, alloc); 317 res = Xz_ReadBackward(&st, stream, startOffset, alloc);
303 st.startOffset = (UInt64)*startOffset; 318 st.startOffset = (UInt64)*startOffset;
304 RINOK(res); 319 RINOK(res)
305 if (p->num == p->numAllocated) 320 if (p->num == p->numAllocated)
306 { 321 {
307 const size_t newNum = p->num + p->num / 4 + 1; 322 const size_t newNum = p->num + p->num / 4 + 1;
@@ -317,7 +332,7 @@ SRes Xzs_ReadBackward(CXzs *p, ILookInStream *stream, Int64 *startOffset, ICompr
317 p->streams[p->num++] = st; 332 p->streams[p->num++] = st;
318 if (*startOffset == 0) 333 if (*startOffset == 0)
319 break; 334 break;
320 RINOK(LookInStream_SeekTo(stream, (UInt64)*startOffset)); 335 RINOK(LookInStream_SeekTo(stream, (UInt64)*startOffset))
321 if (progress && ICompressProgress_Progress(progress, (UInt64)(endOffset - *startOffset), (UInt64)(Int64)-1) != SZ_OK) 336 if (progress && ICompressProgress_Progress(progress, (UInt64)(endOffset - *startOffset), (UInt64)(Int64)-1) != SZ_OK)
322 return SZ_ERROR_PROGRESS; 337 return SZ_ERROR_PROGRESS;
323 } 338 }
diff --git a/C/warn_clang.mak b/C/warn_clang.mak
index ed4f908..0d6446d 100644
--- a/C/warn_clang.mak
+++ b/C/warn_clang.mak
@@ -1,37 +1 @@
1CFLAGS_WARN_CLANG_3_8_UNIQ = \ CFLAGS_WARN = -Weverything -Wfatal-errors
2 -Wno-reserved-id-macro \
3 -Wno-old-style-cast \
4 -Wno-c++11-long-long \
5 -Wno-unused-macros \
6
7CFLAGS_WARN_CLANG_3_8 = \
8 $(CFLAGS_WARN_CLANG_3_8_UNIQ) \
9 -Weverything \
10 -Wno-extra-semi \
11 -Wno-sign-conversion \
12 -Wno-language-extension-token \
13 -Wno-global-constructors \
14 -Wno-non-virtual-dtor \
15 -Wno-switch-enum \
16 -Wno-covered-switch-default \
17 -Wno-cast-qual \
18 -Wno-padded \
19 -Wno-exit-time-destructors \
20 -Wno-weak-vtables \
21
22CFLAGS_WARN_CLANG_12= $(CFLAGS_WARN_CLANG_3_8) \
23 -Wno-extra-semi-stmt \
24 -Wno-zero-as-null-pointer-constant \
25 -Wno-deprecated-dynamic-exception-spec \
26 -Wno-c++98-compat-pedantic \
27 -Wno-atomic-implicit-seq-cst \
28 -Wconversion \
29 -Wno-sign-conversion \
30
31CFLAGS_WARN_1 = \
32 -Wno-deprecated-copy-dtor \
33
34
35
36
37CFLAGS_WARN = $(CFLAGS_WARN_CLANG_12) $(CFLAGS_WARN_1)
diff --git a/C/warn_clang_mac.mak b/C/warn_clang_mac.mak
index 41044a2..44afc53 100644
--- a/C/warn_clang_mac.mak
+++ b/C/warn_clang_mac.mak
@@ -1,37 +1 @@
1CFLAGS_WARN_CLANG_3_8_UNIQ = \ CFLAGS_WARN = -Weverything -Wfatal-errors -Wno-poison-system-directories
2 -Wno-reserved-id-macro \
3 -Wno-old-style-cast \
4 -Wno-c++11-long-long \
5 -Wno-unused-macros \
6
7CFLAGS_WARN_CLANG_3_8 = \
8 $(CFLAGS_WARN_CLANG_3_8_UNIQ) \
9 -Weverything \
10 -Wno-extra-semi \
11 -Wno-sign-conversion \
12 -Wno-language-extension-token \
13 -Wno-global-constructors \
14 -Wno-non-virtual-dtor \
15 -Wno-switch-enum \
16 -Wno-covered-switch-default \
17 -Wno-cast-qual \
18 -Wno-padded \
19 -Wno-exit-time-destructors \
20 -Wno-weak-vtables \
21
22CFLAGS_WARN_CLANG_12= $(CFLAGS_WARN_CLANG_3_8) \
23 -Wno-extra-semi-stmt \
24 -Wno-zero-as-null-pointer-constant \
25 -Wno-deprecated-dynamic-exception-spec \
26 -Wno-c++98-compat-pedantic \
27 -Wno-atomic-implicit-seq-cst \
28 -Wconversion \
29 -Wno-sign-conversion \
30
31CFLAGS_WARN_MAC = \
32 -Wno-poison-system-directories \
33 -Wno-c++11-long-long \
34 -Wno-atomic-implicit-seq-cst \
35
36
37CFLAGS_WARN = $(CFLAGS_WARN_CLANG_12) $(CFLAGS_WARN_MAC)