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