1 /* converted by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
10 // conversion of LZMA SDK 2103 (lzma2103.7z)
15 // ////////////////////////////////////////////////////////////////////////// //
16 static if (!is(typeof(usize
))) public alias usize
= size_t
;
19 // ////////////////////////////////////////////////////////////////////////// //
20 /* LZMA_PROB32 can increase the speed on some CPUs,
21 but memory usage for CLzmaDec::probs will be doubled in that case */
22 //version = LZMA_PROB32;
24 version(LZMA_PROB32
) {
25 alias CLzmaProb
= uint;
27 alias CLzmaProb
= ushort;
31 // ////////////////////////////////////////////////////////////////////////// //
38 SZ_ERROR_UNSUPPORTED
= 4,
40 SZ_ERROR_INPUT_EOF
= 6,
41 SZ_ERROR_OUTPUT_EOF
= 7,
44 SZ_ERROR_PROGRESS
= 10,
53 /* The following interfaces use first parameter as pointer to structure */
56 SRes
delegate (/*const*/ISeqInStream
* p
, void* buf
, usize
* size
) nothrow Read
;
57 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
58 (output(*size) < input(*size)) is allowed */
62 struct ISeqOutStream
{
63 usize
delegate (/*const*/ISeqOutStream
* p
, const(void)* buf
, usize size
) nothrow Write
;
64 /* Returns: result - the number of actually written bytes.
65 (result < size) means error */
69 struct ICompressProgress
{
70 SRes
delegate (/*const*/ICompressProgress
* p
, ulong inSize
, ulong outSize
) nothrow Progress
;
71 /* Returns: result. (result != SZ_OK) means break.
72 Value (ulong)(long)-1 for size means unknown value. */
76 alias ISzAllocPtr
= ISzAlloc
*;
78 void *delegate (ISzAllocPtr p
, usize size
) nothrow Alloc
;
79 void delegate (ISzAllocPtr p
, void *address
) nothrow Free
; /* address can be 0 */
82 public void *ISzAlloc_Alloc (ISzAllocPtr p
, usize size
) nothrow {
84 return p
.Alloc(p
, size
);
87 public void ISzAlloc_Free (ISzAllocPtr p
, void *address
) nothrow {
93 // ////////////////////////////////////////////////////////////////////////// //
94 __gshared ISzAlloc lzmaDefAllocator
= ISzAlloc(
95 delegate void* (ISzAllocPtr p
, usize size
) nothrow {
96 import core
.stdc
.stdlib
: malloc
;
100 delegate void (ISzAllocPtr p
, void* address
) nothrow {
101 import core
.stdc
.stdlib
: free
;
102 if (address
!is null) free(address
);
107 // ////////////////////////////////////////////////////////////////////////// //
108 enum LZMA_PROPS_SIZE
= 5;
111 //**************************************************************************
115 //**************************************************************************
117 struct CLzmaEncProps
{
118 int level
; /* 0 <= level <= 9 */
119 uint dictSize
; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
120 (1 << 12) <= dictSize <= (3 << 29) for 64-bit version
121 default = (1 << 24) */
122 int lc
; /* 0 <= lc <= 8, default = 3 */
123 int lp
; /* 0 <= lp <= 4, default = 0 */
124 int pb
; /* 0 <= pb <= 4, default = 2 */
125 int algo
; /* 0 - fast, 1 - normal, default = 1 */
126 int fb
; /* 5 <= fb <= 273, default = 32 */
127 int btMode
; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
128 int numHashBytes
; /* 2, 3 or 4, default = 4 */
129 uint mc
; /* 1 <= mc <= (1 << 30), default = 32 */
130 uint writeEndMark
;/* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
132 ulong reduceSize
; /* estimated size of data that will be compressed. default = (ulong)(long)-1.
133 Encoder uses this value to reduce dictionary size */
136 alias CLzmaEncHandle
= void*;
139 public void LzmaEncProps_Init (CLzmaEncProps
* p
) @nogc;
140 public void LzmaEncProps_Normalize (CLzmaEncProps
* p
) @nogc;
141 public uint LzmaEncProps_GetDictSize (const(CLzmaEncProps
)* props2
) @nogc;
143 /* LzmaEnc* functions can return the following exit codes:
146 SZ_ERROR_MEM - Memory allocation error
147 SZ_ERROR_PARAM - Incorrect paramater in props
148 SZ_ERROR_WRITE - ISeqOutStream write callback error
149 SZ_ERROR_OUTPUT_EOF - output buffer overflow - version with (ubyte *) output
150 SZ_ERROR_PROGRESS - some break from progress callback
153 public CLzmaEncHandle
LzmaEnc_Create (ISzAllocPtr alloc
);
154 public void LzmaEnc_Destroy (CLzmaEncHandle p
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
);
156 public SRes
LzmaEnc_SetProps (CLzmaEncHandle pp
, const(CLzmaEncProps
)* props2
) @nogc;
157 public void LzmaEnc_SetDataSize (CLzmaEncHandle pp
, ulong expectedDataSiize
) @nogc;
158 public SRes
LzmaEnc_WriteProperties (CLzmaEncHandle pp
, ubyte* props
, uint* size
) @nogc;
159 public uint LzmaEnc_IsWriteEndMark (CLzmaEncHandle pp
) @nogc;
161 public SRes
LzmaEnc_MemEncode (CLzmaEncHandle pp
, ubyte* dest
, usize
* destLen
, const(ubyte)* src
, usize srcLen
,
162 int writeEndMark
, ICompressProgress
*progress
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
);
164 public SRes
LzmaEnc_Encode (CLzmaEncHandle pp
, ISeqOutStream
*outStream
, ISeqInStream
*inStream
,
165 ICompressProgress
*progress
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
);
167 /* ---------- One Call Interface ---------- */
168 public SRes
LzmaEncode (ubyte* dest
, usize
* destLen
, const(ubyte)* src
, usize srcLen
,
169 const(CLzmaEncProps
)* props
, ubyte* propsEncoded
, uint* propsSize
,
170 int writeEndMark
, ICompressProgress
*progress
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
);
175 //**************************************************************************
179 //**************************************************************************
188 /* LzmaProps_Decode - decodes properties
191 SZ_ERROR_UNSUPPORTED - Unsupported properties
194 //SRes LzmaProps_Decode(CLzmaProps *p, const uint8_t *data, unsigned size);
197 /* ---------- LZMA Decoder state ---------- */
199 /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
200 Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
202 enum LZMA_REQUIRED_INPUT_MAX
= 20;
207 CLzmaProb
*probs_1664
;
222 ubyte[LZMA_REQUIRED_INPUT_MAX
] tempBuf
= void;
225 //#define LzmaDec_Construct(p) { (p)->dic = NULL; (p)->probs = NULL; }
227 //public void LzmaDec_Construct (CLzmaDec *p) nothrow @nogc { p.dic = null; p.probs = null; }
229 /* There are two types of LZMA streams:
230 - Stream with end mark. That end mark adds about 6 bytes to compressed size.
231 - Stream without end mark. You must know exact uncompressed size to decompress such stream. */
233 alias ELzmaFinishMode
= int;
235 LZMA_FINISH_ANY
, /* finish at any point */
236 LZMA_FINISH_END
/* block must be finished at the end */
239 /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
241 You must use LZMA_FINISH_END, when you know that current output buffer
242 covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
244 If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
245 and output value of destLen will be less than output buffer size limit.
246 You can check status result also.
248 You can use multiple checks to test data integrity after full decompression:
249 1) Check Result and "status" variable.
250 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
251 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
252 You must use correct finish mode in that case. */
254 alias ELzmaStatus
= int;
256 LZMA_STATUS_NOT_SPECIFIED
, /* use main error code instead */
257 LZMA_STATUS_FINISHED_WITH_MARK
, /* stream was finished with end mark. */
258 LZMA_STATUS_NOT_FINISHED
, /* stream was not finished */
259 LZMA_STATUS_NEEDS_MORE_INPUT
, /* you must provide more input bytes */
260 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
/* there is probability that stream was finished without end mark */
263 /* ELzmaStatus is used only as output value for function call */
266 /* ---------- Interfaces ---------- */
268 /* There are 3 levels of interfaces:
269 1) Dictionary Interface
271 3) One Call Interface
272 You can select any of these interfaces, but don't mix functions from different
273 groups for same object. */
276 /* There are two variants to allocate state for Dictionary Interface:
277 1) LzmaDec_Allocate / LzmaDec_Free
278 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
279 You can use variant 2, if you set dictionary buffer manually.
280 For Buffer Interface you must always use variant 1.
282 LzmaDec_Allocate* can return:
284 SZ_ERROR_MEM - Memory allocation error
285 SZ_ERROR_UNSUPPORTED - Unsupported properties
289 // you can use this to check and decode properties
290 public SRes
LzmaProps_Decode (CLzmaProps
* p
, const(ubyte)* data
, uint size
) @nogc;
292 public void LzmaDec_Init (CLzmaDec
* p
) @nogc;
294 public SRes
LzmaDec_Allocate (CLzmaDec
* p
, const(ubyte)* props
, uint propsSize
, ISzAllocPtr alloc
);
295 public void LzmaDec_Free (CLzmaDec
* p
, ISzAllocPtr alloc
);
297 //k8: the following two functions are used for... something, i don't know.
298 //k8: you don't need to call them, `LzmaDec_Allocate()` and `LzmaDec_Free()` will do it for you.
299 public SRes
LzmaDec_AllocateProbs (CLzmaDec
* p
, const(ubyte)* props
, uint propsSize
, ISzAllocPtr alloc
);
300 public void LzmaDec_FreeProbs (CLzmaDec
*p
, ISzAllocPtr alloc
);
302 /* ---------- Dictionary Interface ---------- */
304 /* You can use it, if you want to eliminate the overhead for data copying from
305 dictionary to some other external buffer.
306 You must work with CLzmaDec variables directly in this interface.
311 for (each new stream)
314 while (it needs more decompression)
316 LzmaDec_DecodeToDic()
317 use data from CLzmaDec::dic and update CLzmaDec::dicPos
323 /* LzmaDec_DecodeToDic
325 The decoding to internal dictionary buffer (CLzmaDec::dic).
326 You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
329 It has meaning only if the decoding reaches output limit (dicLimit).
330 LZMA_FINISH_ANY - Decode just dicLimit bytes.
331 LZMA_FINISH_END - Stream must be finished after dicLimit.
336 LZMA_STATUS_FINISHED_WITH_MARK
337 LZMA_STATUS_NOT_FINISHED
338 LZMA_STATUS_NEEDS_MORE_INPUT
339 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
340 SZ_ERROR_DATA - Data error
341 SZ_ERROR_FAIL - Some unexpected error: internal error of code, memory corruption or hardware failure
344 public SRes
LzmaDec_DecodeToDic (CLzmaDec
* p
, usize dicLimit
, const(ubyte)* src
, usize
*srcLen
,
345 ELzmaFinishMode finishMode
, ELzmaStatus
*status
) @nogc;
348 /* ---------- Buffer Interface ---------- */
350 /* It's zlib-like interface.
351 See LzmaDec_DecodeToDic description for information about STEPS and return results,
352 but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
353 to work with CLzmaDec variables manually.
356 It has meaning only if the decoding reaches output limit (*destLen).
357 LZMA_FINISH_ANY - Decode just destLen bytes.
358 LZMA_FINISH_END - Stream must be finished after (*destLen).
361 public SRes
LzmaDec_DecodeToBuf (CLzmaDec
* p
, ubyte* dest
, usize
* destLen
, const(ubyte)* src
, usize
*srcLen
,
362 ELzmaFinishMode finishMode
, ELzmaStatus
*status
) @nogc;
365 /* ---------- One Call Interface ---------- */
370 It has meaning only if the decoding reaches output limit (*destLen).
371 LZMA_FINISH_ANY - Decode just destLen bytes.
372 LZMA_FINISH_END - Stream must be finished after (*destLen).
377 LZMA_STATUS_FINISHED_WITH_MARK
378 LZMA_STATUS_NOT_FINISHED
379 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
380 SZ_ERROR_DATA - Data error
381 SZ_ERROR_MEM - Memory allocation error
382 SZ_ERROR_UNSUPPORTED - Unsupported properties
383 SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
384 SZ_ERROR_FAIL - Some unexpected error: internal error of code, memory corruption or hardware failure
387 public SRes
LzmaDecode (ubyte* dest
, usize
* destLen
, const(ubyte)* src
, usize
*srcLen
,
388 const(ubyte)* propData
, uint propSize
, ELzmaFinishMode finishMode
,
389 ELzmaStatus
*status
, ISzAllocPtr alloc
);
393 public import iv
.dlzma
.dec;
394 public import iv
.dlzma
.enc
;