blob: 1addabe55ae31c88b9205aa940ab74f5251f3f38 [file] [log] [blame]
Stefan Reinauer2e200cd2012-10-30 14:02:45 -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
Alexandru Gagniuc7c9bb412014-01-29 16:55:48 -06009typedef uint16_t CLzmaProb;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070010
11/* ---------- LZMA Properties ---------- */
12
13#define LZMA_PROPS_SIZE 5
14
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060015struct CLzmaProps
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070016{
17 unsigned lc, lp, pb;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060018 uint32_t dicSize;
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060019};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070020
21/* LzmaProps_Decode - decodes properties
22Returns:
23 SZ_OK
24 SZ_ERROR_UNSUPPORTED - Unsupported properties
25*/
26
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060027SRes LzmaProps_Decode(struct CLzmaProps *p, const uint8_t *data, unsigned size);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070028
29
30/* ---------- LZMA Decoder state ---------- */
31
32/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
33 Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
34
35#define LZMA_REQUIRED_INPUT_MAX 20
36
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060037struct CLzmaDec
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070038{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060039 struct CLzmaProps prop;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070040 CLzmaProb *probs;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060041 uint8_t *dic;
42 const uint8_t *buf;
43 uint32_t range, code;
44 size_t dicPos;
45 size_t dicBufSize;
46 uint32_t processedPos;
47 uint32_t checkDicSize;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070048 unsigned state;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060049 uint32_t reps[4];
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070050 unsigned remainLen;
51 int needFlush;
52 int needInitState;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060053 uint32_t numProbs;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070054 unsigned tempBufSize;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060055 uint8_t tempBuf[LZMA_REQUIRED_INPUT_MAX];
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060056};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070057
58#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
59
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060060void LzmaDec_Init(struct CLzmaDec *p);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070061
62/* There are two types of LZMA streams:
63 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
64 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
65
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060066enum ELzmaFinishMode
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070067{
68 LZMA_FINISH_ANY, /* finish at any point */
69 LZMA_FINISH_END /* block must be finished at the end */
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060070};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070071
72/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
73
74 You must use LZMA_FINISH_END, when you know that current output buffer
75 covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
76
77 If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
78 and output value of destLen will be less than output buffer size limit.
79 You can check status result also.
80
81 You can use multiple checks to test data integrity after full decompression:
82 1) Check Result and "status" variable.
83 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
84 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
85 You must use correct finish mode in that case. */
86
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060087enum ELzmaStatus
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070088{
89 LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
90 LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
91 LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
92 LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
93 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060094};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070095
96/* ELzmaStatus is used only as output value for function call */
97
98
99/* ---------- Interfaces ---------- */
100
101/* There are 3 levels of interfaces:
102 1) Dictionary Interface
103 2) Buffer Interface
104 3) One Call Interface
105 You can select any of these interfaces, but don't mix functions from different
106 groups for same object. */
107
108
109/* There are two variants to allocate state for Dictionary Interface:
110 1) LzmaDec_Allocate / LzmaDec_Free
111 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
112 You can use variant 2, if you set dictionary buffer manually.
113 For Buffer Interface you must always use variant 1.
114
115LzmaDec_Allocate* can return:
116 SZ_OK
117 SZ_ERROR_MEM - Memory allocation error
118 SZ_ERROR_UNSUPPORTED - Unsupported properties
119*/
Kyösti Mälkkiecd84242013-09-13 07:57:49 +0300120
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600121SRes LzmaDec_AllocateProbs(struct CLzmaDec *p, const uint8_t *props, unsigned propsSize, struct ISzAlloc *alloc);
122void LzmaDec_FreeProbs(struct CLzmaDec *p, struct ISzAlloc *alloc);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700123
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600124SRes LzmaDec_Allocate(struct CLzmaDec *state, const uint8_t *prop, unsigned propsSize, struct ISzAlloc *alloc);
125void LzmaDec_Free(struct CLzmaDec *state, struct ISzAlloc *alloc);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700126
127/* ---------- Dictionary Interface ---------- */
128
129/* You can use it, if you want to eliminate the overhead for data copying from
130 dictionary to some other external buffer.
131 You must work with CLzmaDec variables directly in this interface.
132
133 STEPS:
134 LzmaDec_Constr()
135 LzmaDec_Allocate()
136 for (each new stream)
137 {
138 LzmaDec_Init()
139 while (it needs more decompression)
140 {
141 LzmaDec_DecodeToDic()
142 use data from CLzmaDec::dic and update CLzmaDec::dicPos
143 }
144 }
145 LzmaDec_Free()
146*/
147
148/* LzmaDec_DecodeToDic
Kyösti Mälkkiecd84242013-09-13 07:57:49 +0300149
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700150 The decoding to internal dictionary buffer (CLzmaDec::dic).
151 You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
152
153finishMode:
154 It has meaning only if the decoding reaches output limit (dicLimit).
155 LZMA_FINISH_ANY - Decode just dicLimit bytes.
156 LZMA_FINISH_END - Stream must be finished after dicLimit.
157
158Returns:
159 SZ_OK
160 status:
161 LZMA_STATUS_FINISHED_WITH_MARK
162 LZMA_STATUS_NOT_FINISHED
163 LZMA_STATUS_NEEDS_MORE_INPUT
164 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
165 SZ_ERROR_DATA - Data error
166*/
167
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600168SRes LzmaDec_DecodeToDic(struct CLzmaDec *p, size_t dicLimit,
169 const uint8_t *src, size_t *srcLen, enum ELzmaFinishMode finishMode, enum ELzmaStatus *status);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700170
171
172/* ---------- Buffer Interface ---------- */
173
174/* It's zlib-like interface.
175 See LzmaDec_DecodeToDic description for information about STEPS and return results,
176 but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
177 to work with CLzmaDec variables manually.
178
179finishMode:
180 It has meaning only if the decoding reaches output limit (*destLen).
181 LZMA_FINISH_ANY - Decode just destLen bytes.
182 LZMA_FINISH_END - Stream must be finished after (*destLen).
183*/
184
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600185SRes LzmaDec_DecodeToBuf(struct CLzmaDec *p, uint8_t *dest, size_t *destLen,
186 const uint8_t *src, size_t *srcLen, enum ELzmaFinishMode finishMode, enum ELzmaStatus *status);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700187
188
189/* ---------- One Call Interface ---------- */
190
191/* LzmaDecode
192
193finishMode:
194 It has meaning only if the decoding reaches output limit (*destLen).
195 LZMA_FINISH_ANY - Decode just destLen bytes.
196 LZMA_FINISH_END - Stream must be finished after (*destLen).
197
198Returns:
199 SZ_OK
200 status:
201 LZMA_STATUS_FINISHED_WITH_MARK
202 LZMA_STATUS_NOT_FINISHED
203 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
204 SZ_ERROR_DATA - Data error
205 SZ_ERROR_MEM - Memory allocation error
206 SZ_ERROR_UNSUPPORTED - Unsupported properties
207 SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
208*/
209
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600210SRes LzmaDecode(uint8_t *dest, size_t *destLen, const uint8_t *src, size_t *srcLen,
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600211 const uint8_t *propData, unsigned propSize, enum ELzmaFinishMode finishMode,
212 enum ELzmaStatus *status, struct ISzAlloc *alloc);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700213
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700214#endif