]> git.cworth.org Git - vogl/blob - src/voglcore/lzma_LzmaDec.h
Initial vogl checkin
[vogl] / src / voglcore / lzma_LzmaDec.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 /* LzmaDec.h -- LZMA Decoder
28 2008-10-04 : Igor Pavlov : Public domain */
29
30 #ifndef __LZMADEC_H
31 #define __LZMADEC_H
32
33 #include "lzma_Types.h"
34
35 namespace vogl
36 {
37
38 /* #define _LZMA_PROB32 */
39 /* _LZMA_PROB32 can increase the speed on some CPUs,
40    but memory usage for CLzmaDec::probs will be doubled in that case */
41
42 #ifdef _LZMA_PROB32
43 #define CLzmaProb UInt32
44 #else
45 #define CLzmaProb UInt16
46 #endif
47
48 /* ---------- LZMA Properties ---------- */
49
50 #define LZMA_PROPS_SIZE 5
51
52     typedef struct _CLzmaProps
53     {
54         unsigned lc, lp, pb;
55         UInt32 dicSize;
56     } CLzmaProps;
57
58     /* LzmaProps_Decode - decodes properties
59 Returns:
60   SZ_OK
61   SZ_ERROR_UNSUPPORTED - Unsupported properties
62 */
63
64     SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
65
66 /* ---------- LZMA Decoder state ---------- */
67
68 /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
69    Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
70
71 #define LZMA_REQUIRED_INPUT_MAX 20
72
73     typedef struct
74     {
75         CLzmaProps prop;
76         CLzmaProb *probs;
77         Byte *dic;
78         const Byte *buf;
79         UInt32 range, code;
80         SizeT dicPos;
81         SizeT dicBufSize;
82         UInt32 processedPos;
83         UInt32 checkDicSize;
84         unsigned state;
85         UInt32 reps[4];
86         unsigned remainLen;
87         int needFlush;
88         int needInitState;
89         UInt32 numProbs;
90         unsigned tempBufSize;
91         Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
92     } CLzmaDec;
93
94 #define LzmaDec_Construct(p) \
95     {                        \
96         (p)->dic = 0;        \
97         (p)->probs = 0;      \
98     }
99
100     void LzmaDec_Init(CLzmaDec *p);
101
102     /* There are two types of LZMA streams:
103      0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
104      1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
105
106     typedef enum
107     {
108         LZMA_FINISH_ANY, /* finish at any point */
109         LZMA_FINISH_END  /* block must be finished at the end */
110     } ELzmaFinishMode;
111
112     /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
113
114    You must use LZMA_FINISH_END, when you know that current output buffer
115    covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
116
117    If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
118    and output value of destLen will be less than output buffer size limit.
119    You can check status result also.
120
121    You can use multiple checks to test data integrity after full decompression:
122      1) Check Result and "status" variable.
123      2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
124      3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
125         You must use correct finish mode in that case. */
126
127     typedef enum
128     {
129         LZMA_STATUS_NOT_SPECIFIED,              /* use main error code instead */
130         LZMA_STATUS_FINISHED_WITH_MARK,         /* stream was finished with end mark. */
131         LZMA_STATUS_NOT_FINISHED,               /* stream was not finished */
132         LZMA_STATUS_NEEDS_MORE_INPUT,           /* you must provide more input bytes */
133         LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
134     } ELzmaStatus;
135
136     /* ELzmaStatus is used only as output value for function call */
137
138     /* ---------- Interfaces ---------- */
139
140     /* There are 3 levels of interfaces:
141      1) Dictionary Interface
142      2) Buffer Interface
143      3) One Call Interface
144    You can select any of these interfaces, but don't mix functions from different
145    groups for same object. */
146
147     /* There are two variants to allocate state for Dictionary Interface:
148      1) LzmaDec_Allocate / LzmaDec_Free
149      2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
150    You can use variant 2, if you set dictionary buffer manually.
151    For Buffer Interface you must always use variant 1.
152
153 LzmaDec_Allocate* can return:
154   SZ_OK
155   SZ_ERROR_MEM         - Memory allocation error
156   SZ_ERROR_UNSUPPORTED - Unsupported properties
157 */
158
159     SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
160     void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
161
162     SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);
163     void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);
164
165     /* ---------- Dictionary Interface ---------- */
166
167     /* You can use it, if you want to eliminate the overhead for data copying from
168    dictionary to some other external buffer.
169    You must work with CLzmaDec variables directly in this interface.
170
171    STEPS:
172      LzmaDec_Constr()
173      LzmaDec_Allocate()
174      for (each new stream)
175      {
176        LzmaDec_Init()
177        while (it needs more decompression)
178        {
179          LzmaDec_DecodeToDic()
180          use data from CLzmaDec::dic and update CLzmaDec::dicPos
181        }
182      }
183      LzmaDec_Free()
184 */
185
186     /* LzmaDec_DecodeToDic
187
188    The decoding to internal dictionary buffer (CLzmaDec::dic).
189    You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
190
191 finishMode:
192   It has meaning only if the decoding reaches output limit (dicLimit).
193   LZMA_FINISH_ANY - Decode just dicLimit bytes.
194   LZMA_FINISH_END - Stream must be finished after dicLimit.
195
196 Returns:
197   SZ_OK
198     status:
199       LZMA_STATUS_FINISHED_WITH_MARK
200       LZMA_STATUS_NOT_FINISHED
201       LZMA_STATUS_NEEDS_MORE_INPUT
202       LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
203   SZ_ERROR_DATA - Data error
204 */
205
206     SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
207                              const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
208
209     /* ---------- Buffer Interface ---------- */
210
211     /* It's zlib-like interface.
212    See LzmaDec_DecodeToDic description for information about STEPS and return results,
213    but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
214    to work with CLzmaDec variables manually.
215
216 finishMode:
217   It has meaning only if the decoding reaches output limit (*destLen).
218   LZMA_FINISH_ANY - Decode just destLen bytes.
219   LZMA_FINISH_END - Stream must be finished after (*destLen).
220 */
221
222     SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
223                              const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
224
225     /* ---------- One Call Interface ---------- */
226
227     /* LzmaDecode
228
229 finishMode:
230   It has meaning only if the decoding reaches output limit (*destLen).
231   LZMA_FINISH_ANY - Decode just destLen bytes.
232   LZMA_FINISH_END - Stream must be finished after (*destLen).
233
234 Returns:
235   SZ_OK
236     status:
237       LZMA_STATUS_FINISHED_WITH_MARK
238       LZMA_STATUS_NOT_FINISHED
239       LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
240   SZ_ERROR_DATA - Data error
241   SZ_ERROR_MEM  - Memory allocation error
242   SZ_ERROR_UNSUPPORTED - Unsupported properties
243   SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
244 */
245
246     SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
247                     const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
248                     ELzmaStatus *status, ISzAlloc *alloc);
249 }
250
251 #endif