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