1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 /* LzmaDec.h -- LZMA Decoder
28 2008-10-04 : Igor Pavlov : Public domain */
33 #include "lzma_Types.h"
38 /* #define _LZMA_PROB32 */
39 /* _LZMA_PROB32 can increase the speed on some CPUs,
40 but memory usage for CLzmaDec::probs will be doubled in that case */
43 #define CLzmaProb UInt32
45 #define CLzmaProb UInt16
48 /* ---------- LZMA Properties ---------- */
50 #define LZMA_PROPS_SIZE 5
52 typedef struct _CLzmaProps
58 /* LzmaProps_Decode - decodes properties
61 SZ_ERROR_UNSUPPORTED - Unsupported properties
64 SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
66 /* ---------- LZMA Decoder state ---------- */
68 /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
69 Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
71 #define LZMA_REQUIRED_INPUT_MAX 20
91 Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
94 #define LzmaDec_Construct(p) \
100 void LzmaDec_Init(CLzmaDec *p);
102 /* There are two types of LZMA streams:
103 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
104 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
108 LZMA_FINISH_ANY, /* finish at any point */
109 LZMA_FINISH_END /* block must be finished at the end */
112 /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
114 You must use LZMA_FINISH_END, when you know that current output buffer
115 covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
117 If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
118 and output value of destLen will be less than output buffer size limit.
119 You can check status result also.
121 You can use multiple checks to test data integrity after full decompression:
122 1) Check Result and "status" variable.
123 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
124 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
125 You must use correct finish mode in that case. */
129 LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
130 LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
131 LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
132 LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
133 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
136 /* ELzmaStatus is used only as output value for function call */
138 /* ---------- Interfaces ---------- */
140 /* There are 3 levels of interfaces:
141 1) Dictionary Interface
143 3) One Call Interface
144 You can select any of these interfaces, but don't mix functions from different
145 groups for same object. */
147 /* There are two variants to allocate state for Dictionary Interface:
148 1) LzmaDec_Allocate / LzmaDec_Free
149 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
150 You can use variant 2, if you set dictionary buffer manually.
151 For Buffer Interface you must always use variant 1.
153 LzmaDec_Allocate* can return:
155 SZ_ERROR_MEM - Memory allocation error
156 SZ_ERROR_UNSUPPORTED - Unsupported properties
159 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
160 void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
162 SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);
163 void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);
165 /* ---------- Dictionary Interface ---------- */
167 /* You can use it, if you want to eliminate the overhead for data copying from
168 dictionary to some other external buffer.
169 You must work with CLzmaDec variables directly in this interface.
174 for (each new stream)
177 while (it needs more decompression)
179 LzmaDec_DecodeToDic()
180 use data from CLzmaDec::dic and update CLzmaDec::dicPos
186 /* LzmaDec_DecodeToDic
188 The decoding to internal dictionary buffer (CLzmaDec::dic).
189 You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
192 It has meaning only if the decoding reaches output limit (dicLimit).
193 LZMA_FINISH_ANY - Decode just dicLimit bytes.
194 LZMA_FINISH_END - Stream must be finished after dicLimit.
199 LZMA_STATUS_FINISHED_WITH_MARK
200 LZMA_STATUS_NOT_FINISHED
201 LZMA_STATUS_NEEDS_MORE_INPUT
202 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
203 SZ_ERROR_DATA - Data error
206 SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
207 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
209 /* ---------- Buffer Interface ---------- */
211 /* It's zlib-like interface.
212 See LzmaDec_DecodeToDic description for information about STEPS and return results,
213 but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
214 to work with CLzmaDec variables manually.
217 It has meaning only if the decoding reaches output limit (*destLen).
218 LZMA_FINISH_ANY - Decode just destLen bytes.
219 LZMA_FINISH_END - Stream must be finished after (*destLen).
222 SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
223 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
225 /* ---------- One Call Interface ---------- */
230 It has meaning only if the decoding reaches output limit (*destLen).
231 LZMA_FINISH_ANY - Decode just destLen bytes.
232 LZMA_FINISH_END - Stream must be finished after (*destLen).
237 LZMA_STATUS_FINISHED_WITH_MARK
238 LZMA_STATUS_NOT_FINISHED
239 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
240 SZ_ERROR_DATA - Data error
241 SZ_ERROR_MEM - Memory allocation error
242 SZ_ERROR_UNSUPPORTED - Unsupported properties
243 SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
246 SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
247 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
248 ELzmaStatus *status, ISzAlloc *alloc);