1 /* LzmaDec.h -- LZMA Decoder
2 2023-04-02 : Igor Pavlov : Public domain */
4 #ifndef ZIP7_INC_LZMA_DEC_H
5 #define ZIP7_INC_LZMA_DEC_H
11 /* #define Z7_LZMA_PROB32 */
12 /* Z7_LZMA_PROB32 can increase the speed on some CPUs,
13 but memory usage for CLzmaDec::probs will be doubled in that case */
24 /* ---------- LZMA Properties ---------- */
26 #define LZMA_PROPS_SIZE 5
37 /* LzmaProps_Decode - decodes properties
40 SZ_ERROR_UNSUPPORTED - Unsupported properties
43 SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
46 /* ---------- LZMA Decoder state ---------- */
48 /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
49 Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
51 #define LZMA_REQUIRED_INPUT_MAX 20
55 /* Don't change this structure. ASM code can use it. */
58 CLzmaProb *probs_1664;
73 Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
76 #define LzmaDec_CONSTRUCT(p) { (p)->dic = NULL; (p)->probs = NULL; }
77 #define LzmaDec_Construct(p) LzmaDec_CONSTRUCT(p)
79 void LzmaDec_Init(CLzmaDec *p);
81 /* There are two types of LZMA streams:
82 - Stream with end mark. That end mark adds about 6 bytes to compressed size.
83 - Stream without end mark. You must know exact uncompressed size to decompress such stream. */
87 LZMA_FINISH_ANY, /* finish at any point */
88 LZMA_FINISH_END /* block must be finished at the end */
91 /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
93 You must use LZMA_FINISH_END, when you know that current output buffer
94 covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
96 If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
97 and output value of destLen will be less than output buffer size limit.
98 You can check status result also.
100 You can use multiple checks to test data integrity after full decompression:
101 1) Check Result and "status" variable.
102 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
103 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
104 You must use correct finish mode in that case. */
108 LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
109 LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
110 LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
111 LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
112 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
115 /* ELzmaStatus is used only as output value for function call */
118 /* ---------- Interfaces ---------- */
120 /* There are 3 levels of interfaces:
121 1) Dictionary Interface
123 3) One Call Interface
124 You can select any of these interfaces, but don't mix functions from different
125 groups for same object. */
128 /* There are two variants to allocate state for Dictionary Interface:
129 1) LzmaDec_Allocate / LzmaDec_Free
130 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
131 You can use variant 2, if you set dictionary buffer manually.
132 For Buffer Interface you must always use variant 1.
134 LzmaDec_Allocate* can return:
136 SZ_ERROR_MEM - Memory allocation error
137 SZ_ERROR_UNSUPPORTED - Unsupported properties
140 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc);
141 void LzmaDec_FreeProbs(CLzmaDec *p, ISzAllocPtr alloc);
143 SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc);
144 void LzmaDec_Free(CLzmaDec *p, ISzAllocPtr alloc);
146 /* ---------- Dictionary Interface ---------- */
148 /* You can use it, if you want to eliminate the overhead for data copying from
149 dictionary to some other external buffer.
150 You must work with CLzmaDec variables directly in this interface.
155 for (each new stream)
158 while (it needs more decompression)
160 LzmaDec_DecodeToDic()
161 use data from CLzmaDec::dic and update CLzmaDec::dicPos
167 /* LzmaDec_DecodeToDic
169 The decoding to internal dictionary buffer (CLzmaDec::dic).
170 You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
173 It has meaning only if the decoding reaches output limit (dicLimit).
174 LZMA_FINISH_ANY - Decode just dicLimit bytes.
175 LZMA_FINISH_END - Stream must be finished after dicLimit.
180 LZMA_STATUS_FINISHED_WITH_MARK
181 LZMA_STATUS_NOT_FINISHED
182 LZMA_STATUS_NEEDS_MORE_INPUT
183 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
184 SZ_ERROR_DATA - Data error
185 SZ_ERROR_FAIL - Some unexpected error: internal error of code, memory corruption or hardware failure
188 SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
189 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
192 /* ---------- Buffer Interface ---------- */
194 /* It's zlib-like interface.
195 See LzmaDec_DecodeToDic description for information about STEPS and return results,
196 but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
197 to work with CLzmaDec variables manually.
200 It has meaning only if the decoding reaches output limit (*destLen).
201 LZMA_FINISH_ANY - Decode just destLen bytes.
202 LZMA_FINISH_END - Stream must be finished after (*destLen).
205 SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
206 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
209 /* ---------- One Call Interface ---------- */
214 It has meaning only if the decoding reaches output limit (*destLen).
215 LZMA_FINISH_ANY - Decode just destLen bytes.
216 LZMA_FINISH_END - Stream must be finished after (*destLen).
221 LZMA_STATUS_FINISHED_WITH_MARK
222 LZMA_STATUS_NOT_FINISHED
223 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
224 SZ_ERROR_DATA - Data error
225 SZ_ERROR_MEM - Memory allocation error
226 SZ_ERROR_UNSUPPORTED - Unsupported properties
227 SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
228 SZ_ERROR_FAIL - Some unexpected error: internal error of code, memory corruption or hardware failure
231 SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
232 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
233 ELzmaStatus *status, ISzAllocPtr alloc);