4 #ifdef _SZ_ONE_DIRECTORY
5 #include "LzmaDecode.h"
7 #include "../../Compress/LZMA_C/LzmaDecode.h"
10 CMethodID k_Copy
= { { 0x0 }, 1 };
11 CMethodID k_LZMA
= { { 0x3, 0x1, 0x1 }, 3 };
15 typedef struct _CLzmaInCallbackImp
17 ILzmaInCallback InCallback
;
18 ISzInStream
*InStream
;
22 int LzmaReadImp(void *object
, const unsigned char **buffer
, SizeT
*size
)
24 CLzmaInCallbackImp
*cb
= (CLzmaInCallbackImp
*)object
;
28 res
= cb
->InStream
->Read((void *)cb
->InStream
, (void **)buffer
, cb
->Size
, &processedSize
);
29 *size
= (SizeT
)processedSize
;
30 if (processedSize
> cb
->Size
)
32 cb
->Size
-= processedSize
;
40 SZ_RESULT
SzDecode(const CFileSize
*packSizes
, const CFolder
*folder
,
42 ISzInStream
*inStream
,
46 Byte
*outBuffer
, size_t outSize
,
47 size_t *outSizeProcessed
, ISzAlloc
*allocMain
)
52 if (folder
->NumPackStreams
!= 1)
54 if (folder
->NumCoders
!= 1)
56 coder
= folder
->Coders
;
57 *outSizeProcessed
= 0;
59 for (si
= 0; si
< folder
->NumPackStreams
; si
++)
60 inSize
+= (size_t)packSizes
[si
];
62 if (AreMethodsEqual(&coder
->MethodID
, &k_Copy
))
65 if (inSize
!= outSize
)
66 return SZE_DATA_ERROR
;
68 for (i
= 0; i
< inSize
;)
73 RINOK(inStream
->Read((void *)inStream
, (void **)&inBuffer
, inSize
- i
, &bufferSize
));
75 return SZE_DATA_ERROR
;
76 if (bufferSize
> inSize
- i
)
78 *outSizeProcessed
+= bufferSize
;
79 for (j
= 0; j
< bufferSize
&& i
< inSize
; j
++, i
++)
80 outBuffer
[i
] = inBuffer
[j
];
83 for (i
= 0; i
< inSize
; i
++)
84 outBuffer
[i
] = inBuffer
[i
];
85 *outSizeProcessed
= inSize
;
90 if (AreMethodsEqual(&coder
->MethodID
, &k_LZMA
))
93 CLzmaInCallbackImp lzmaCallback
;
98 CLzmaDecoderState state
; /* it's about 24-80 bytes structure, if int is 32-bit */
100 SizeT outSizeProcessedLoc
;
103 lzmaCallback
.Size
= inSize
;
104 lzmaCallback
.InStream
= inStream
;
105 lzmaCallback
.InCallback
.Read
= LzmaReadImp
;
108 if (LzmaDecodeProperties(&state
.Properties
, coder
->Properties
.Items
,
109 coder
->Properties
.Capacity
) != LZMA_RESULT_OK
)
112 state
.Probs
= (CProb
*)allocMain
->Alloc(LzmaGetNumProbs(&state
.Properties
) * sizeof(CProb
));
113 if (state
.Probs
== 0)
114 return SZE_OUTOFMEMORY
;
116 #ifdef _LZMA_OUT_READ
117 if (state
.Properties
.DictionarySize
== 0)
118 state
.Dictionary
= 0;
121 state
.Dictionary
= (unsigned char *)allocMain
->Alloc(state
.Properties
.DictionarySize
);
122 if (state
.Dictionary
== 0)
124 allocMain
->Free(state
.Probs
);
125 return SZE_OUTOFMEMORY
;
128 LzmaDecoderInit(&state
);
131 result
= LzmaDecode(&state
,
133 &lzmaCallback
.InCallback
,
135 inBuffer
, (SizeT
)inSize
, &inProcessed
,
137 outBuffer
, (SizeT
)outSize
, &outSizeProcessedLoc
);
138 *outSizeProcessed
= (size_t)outSizeProcessedLoc
;
139 allocMain
->Free(state
.Probs
);
140 #ifdef _LZMA_OUT_READ
141 allocMain
->Free(state
.Dictionary
);
143 if (result
== LZMA_RESULT_DATA_ERROR
)
144 return SZE_DATA_ERROR
;
145 if (result
!= LZMA_RESULT_OK
)