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