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