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