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