2 * GRUB -- GRand Unified Bootloader
3 * Copyright (c) 1999-2008 Igor Pavlov
4 * Copyright (C) 2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 * This code was taken from LZMA SDK 4.58 beta, and was slightly modified
22 * to adapt it to GRUB's requirement.
24 * See <http://www.7-zip.org>, for more information about LZMA.
32 /* #define _LZMA_PROB32 */
33 /* _LZMA_PROB32 can increase the speed on some CPUs,
34 but memory usage for CLzmaDec::probs will be doubled in that case */
37 #define CLzmaProb UInt32
39 #define CLzmaProb UInt16
43 /* ---------- LZMA Properties ---------- */
45 #define LZMA_PROPS_SIZE 5
47 typedef struct _CLzmaProps
53 /* LzmaProps_Decode - decodes properties
56 SZ_ERROR_UNSUPPORTED - Unsupported properties
59 SRes
LzmaProps_Decode(CLzmaProps
*p
, const Byte
*data
, unsigned size
);
62 /* ---------- LZMA Decoder state ---------- */
64 /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
65 Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
67 #define LZMA_REQUIRED_INPUT_MAX 20
87 Byte tempBuf
[LZMA_REQUIRED_INPUT_MAX
];
90 #define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
92 void LzmaDec_Init(CLzmaDec
*p
);
94 /* There are two types of LZMA streams:
95 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
96 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
100 LZMA_FINISH_ANY
, /* finish at any point */
101 LZMA_FINISH_END
/* block must be finished at the end */
104 /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
106 You must use LZMA_FINISH_END, when you know that current output buffer
107 covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
109 If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
110 and output value of destLen will be less than output buffer size limit.
111 You can check status result also.
113 You can use multiple checks to test data integrity after full decompression:
114 1) Check Result and "status" variable.
115 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
116 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
117 You must use correct finish mode in that case. */
121 LZMA_STATUS_NOT_SPECIFIED
, /* use main error code instead */
122 LZMA_STATUS_FINISHED_WITH_MARK
, /* stream was finished with end mark. */
123 LZMA_STATUS_NOT_FINISHED
, /* stream was not finished */
124 LZMA_STATUS_NEEDS_MORE_INPUT
, /* you must provide more input bytes */
125 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
/* there is probability that stream was finished without end mark */
128 /* ELzmaStatus is used only as output value for function call */
131 /* ---------- Interfaces ---------- */
133 /* There are 3 levels of interfaces:
134 1) Dictionary Interface
136 3) One Call Interface
137 You can select any of these interfaces, but don't mix functions from different
138 groups for same object. */
141 /* There are two variants to allocate state for Dictionary Interface:
142 1) LzmaDec_Allocate / LzmaDec_Free
143 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
144 You can use variant 2, if you set dictionary buffer manually.
145 For Buffer Interface you must always use variant 1.
147 LzmaDec_Allocate* can return:
149 SZ_ERROR_MEM - Memory allocation error
150 SZ_ERROR_UNSUPPORTED - Unsupported properties
153 SRes
LzmaDec_AllocateProbs(CLzmaDec
*p
, const Byte
*props
, unsigned propsSize
, ISzAlloc
*alloc
);
154 void LzmaDec_FreeProbs(CLzmaDec
*p
, ISzAlloc
*alloc
);
156 SRes
LzmaDec_Allocate(CLzmaDec
*state
, const Byte
*prop
, unsigned propsSize
, ISzAlloc
*alloc
);
157 void LzmaDec_Free(CLzmaDec
*state
, ISzAlloc
*alloc
);
159 /* ---------- Dictionary Interface ---------- */
161 /* You can use it, if you want to eliminate the overhead for data copying from
162 dictionary to some other external buffer.
163 You must work with CLzmaDec variables directly in this interface.
168 for (each new stream)
171 while (it needs more decompression)
173 LzmaDec_DecodeToDic()
174 use data from CLzmaDec::dic and update CLzmaDec::dicPos
180 /* LzmaDec_DecodeToDic
182 The decoding to internal dictionary buffer (CLzmaDec::dic).
183 You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
186 It has meaning only if the decoding reaches output limit (dicLimit).
187 LZMA_FINISH_ANY - Decode just dicLimit bytes.
188 LZMA_FINISH_END - Stream must be finished after dicLimit.
193 LZMA_STATUS_FINISHED_WITH_MARK
194 LZMA_STATUS_NOT_FINISHED
195 LZMA_STATUS_NEEDS_MORE_INPUT
196 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
197 SZ_ERROR_DATA - Data error
200 SRes
LzmaDec_DecodeToDic(CLzmaDec
*p
, SizeT dicLimit
,
201 const Byte
*src
, SizeT
*srcLen
, ELzmaFinishMode finishMode
, ELzmaStatus
*status
);
204 /* ---------- Buffer Interface ---------- */
206 /* It's zlib-like interface.
207 See LzmaDec_DecodeToDic description for information about STEPS and return results,
208 but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
209 to work with CLzmaDec variables manually.
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).
217 SRes
LzmaDec_DecodeToBuf(CLzmaDec
*p
, Byte
*dest
, SizeT
*destLen
,
218 const Byte
*src
, SizeT
*srcLen
, ELzmaFinishMode finishMode
, ELzmaStatus
*status
);
221 /* ---------- One Call Interface ---------- */
226 It has meaning only if the decoding reaches output limit (*destLen).
227 LZMA_FINISH_ANY - Decode just destLen bytes.
228 LZMA_FINISH_END - Stream must be finished after (*destLen).
233 LZMA_STATUS_FINISHED_WITH_MARK
234 LZMA_STATUS_NOT_FINISHED
235 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
236 SZ_ERROR_DATA - Data error
237 SZ_ERROR_MEM - Memory allocation error
238 SZ_ERROR_UNSUPPORTED - Unsupported properties
239 SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
242 SRes
LzmaDecode(Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT
*srcLen
,
243 const Byte
*propData
, unsigned propSize
, ELzmaFinishMode finishMode
,
244 ELzmaStatus
*status
, ISzAlloc
*alloc
);