CMS 3D CMS Logo

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