blob: 6e923459dfe2ee60b4d48768cee528bd70a8332c [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* LzmaDec.h -- LZMA Decoder
22009-02-07 : Igor Pavlov : Public domain */
3
4#ifndef __LZMA_DEC_H
5#define __LZMA_DEC_H
6
7#include "Types.h"
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13/* #define _LZMA_PROB32 */
14/* _LZMA_PROB32 can increase the speed on some CPUs,
15 but memory usage for CLzmaDec::probs will be doubled in that case */
16
17typedef const ISzAlloc * ISzAllocPtr;
18
19typedef
20#ifdef _LZMA_PROB32
21 UInt32
22#else
23 UInt16
24#endif
25 CLzmaProb;
26
27
28/* ---------- LZMA Properties ---------- */
29
30#define LZMA_PROPS_SIZE 5
31
32typedef struct _CLzmaProps
33{
34 Byte lc;
35 Byte lp;
36 Byte pb;
37 Byte _pad_;
38 UInt32 dicSize;
39} CLzmaProps;
40
41/* LzmaProps_Decode - decodes properties
42Returns:
43 SZ_OK
44 SZ_ERROR_UNSUPPORTED - Unsupported properties
45*/
46
47SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
48
49
50/* ---------- LZMA Decoder state ---------- */
51
52/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
53 Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
54
55#define LZMA_REQUIRED_INPUT_MAX 20
56
57typedef struct
58{
59 /* Don't change this structure. ASM code can use it. */
60 CLzmaProps prop;
61 CLzmaProb *probs;
62 CLzmaProb *probs_1664;
63 Byte *dic;
64 SizeT dicBufSize;
65 SizeT dicPos;
66 const Byte *buf;
67 UInt32 range;
68 UInt32 code;
69 UInt32 processedPos;
70 UInt32 checkDicSize;
71 UInt32 reps[4];
72 UInt32 state;
73 UInt32 remainLen;
74
75 UInt32 numProbs;
76 unsigned tempBufSize;
77 Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
78} CLzmaDec;
79
80#define LzmaDec_Construct(p) { (p)->dic = NULL; (p)->probs = NULL; }
81
82void LzmaDec_Init(CLzmaDec *p);
83
84/* There are two types of LZMA streams:
85 - Stream with end mark. That end mark adds about 6 bytes to compressed size.
86 - Stream without end mark. You must know exact uncompressed size to decompress such stream. */
87
88typedef enum
89{
90 LZMA_FINISH_ANY, /* finish at any point */
91 LZMA_FINISH_END /* block must be finished at the end */
92} ELzmaFinishMode;
93
94/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
95
96 You must use LZMA_FINISH_END, when you know that current output buffer
97 covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
98
99 If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
100 and output value of destLen will be less than output buffer size limit.
101 You can check status result also.
102
103 You can use multiple checks to test data integrity after full decompression:
104 1) Check Result and "status" variable.
105 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
106 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
107 You must use correct finish mode in that case. */
108
109typedef enum
110{
111 LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
112 LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
113 LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
114 LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
115 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
116} ELzmaStatus;
117
118/* ELzmaStatus is used only as output value for function call */
119
120
121/* ---------- Interfaces ---------- */
122
123/* There are 3 levels of interfaces:
124 1) Dictionary Interface
125 2) Buffer Interface
126 3) One Call Interface
127 You can select any of these interfaces, but don't mix functions from different
128 groups for same object. */
129
130
131/* There are two variants to allocate state for Dictionary Interface:
132 1) LzmaDec_Allocate / LzmaDec_Free
133 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
134 You can use variant 2, if you set dictionary buffer manually.
135 For Buffer Interface you must always use variant 1.
136
137LzmaDec_Allocate* can return:
138 SZ_OK
139 SZ_ERROR_MEM - Memory allocation error
140 SZ_ERROR_UNSUPPORTED - Unsupported properties
141*/
142
143SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc);
144void LzmaDec_FreeProbs(CLzmaDec *p, ISzAllocPtr alloc);
145
146SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc);
147void LzmaDec_Free(CLzmaDec *p, ISzAllocPtr alloc);
148
149/* ---------- Dictionary Interface ---------- */
150
151/* You can use it, if you want to eliminate the overhead for data copying from
152 dictionary to some other external buffer.
153 You must work with CLzmaDec variables directly in this interface.
154
155 STEPS:
156 LzmaDec_Construct()
157 LzmaDec_Allocate()
158 for (each new stream)
159 {
160 LzmaDec_Init()
161 while (it needs more decompression)
162 {
163 LzmaDec_DecodeToDic()
164 use data from CLzmaDec::dic and update CLzmaDec::dicPos
165 }
166 }
167 LzmaDec_Free()
168*/
169
170/* LzmaDec_DecodeToDic
171
172 The decoding to internal dictionary buffer (CLzmaDec::dic).
173 You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
174
175finishMode:
176 It has meaning only if the decoding reaches output limit (dicLimit).
177 LZMA_FINISH_ANY - Decode just dicLimit bytes.
178 LZMA_FINISH_END - Stream must be finished after dicLimit.
179
180Returns:
181 SZ_OK
182 status:
183 LZMA_STATUS_FINISHED_WITH_MARK
184 LZMA_STATUS_NOT_FINISHED
185 LZMA_STATUS_NEEDS_MORE_INPUT
186 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
187 SZ_ERROR_DATA - Data error
188*/
189
190SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
191 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
192
193
194/* ---------- Buffer Interface ---------- */
195
196/* It's zlib-like interface.
197 See LzmaDec_DecodeToDic description for information about STEPS and return results,
198 but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
199 to work with CLzmaDec variables manually.
200
201finishMode:
202 It has meaning only if the decoding reaches output limit (*destLen).
203 LZMA_FINISH_ANY - Decode just destLen bytes.
204 LZMA_FINISH_END - Stream must be finished after (*destLen).
205*/
206
207SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
208 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
209
210
211/* ---------- One Call Interface ---------- */
212
213/* LzmaDecode
214
215finishMode:
216 It has meaning only if the decoding reaches output limit (*destLen).
217 LZMA_FINISH_ANY - Decode just destLen bytes.
218 LZMA_FINISH_END - Stream must be finished after (*destLen).
219
220Returns:
221 SZ_OK
222 status:
223 LZMA_STATUS_FINISHED_WITH_MARK
224 LZMA_STATUS_NOT_FINISHED
225 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
226 SZ_ERROR_DATA - Data error
227 SZ_ERROR_MEM - Memory allocation error
228 SZ_ERROR_UNSUPPORTED - Unsupported properties
229 SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
230*/
231
232SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
233 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
234 ELzmaStatus *status, ISzAllocPtr alloc);
235
236#ifdef __cplusplus
237}
238#endif
239
240#endif