aboutsummaryrefslogtreecommitdiff
path: root/C/XzDec.c
diff options
context:
space:
mode:
Diffstat (limited to 'C/XzDec.c')
-rw-r--r--C/XzDec.c300
1 files changed, 169 insertions, 131 deletions
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