1 module botan.compression.lzma_hd;
2 
3 import botan.constants;
4 static if (BOTAN_HAS_LZMA):
5 import std.conv;
6 package:
7 
8 extern(C) nothrow @nogc:
9 
10 enum LZMA_FILTER_LZMA1 = 0x4000000000000001UL;
11 enum LZMA_FILTER_LZMA2 = 0x21UL;
12 
13 enum lzma_match_finder
14 {
15     LZMA_MF_HC3     = 0x03,    
16     LZMA_MF_HC4     = 0x04,    
17     LZMA_MF_BT2     = 0x12,    
18     LZMA_MF_BT3     = 0x13,
19     LZMA_MF_BT4     = 0x14
20 }
21 
22 
23 nothrow lzma_bool lzma_mf_is_supported(lzma_match_finder match_finder);
24 
25 enum lzma_mode
26 {
27     LZMA_MODE_FAST = 1,
28     LZMA_MODE_NORMAL = 2
29 }
30 
31 
32 nothrow lzma_bool lzma_mode_is_supported(lzma_mode mode);
33 
34 struct lzma_options_lzma
35 {
36     uint dict_size;
37     enum LZMA_DICT_SIZE_MIN     =  4096U;
38     enum LZMA_DICT_SIZE_DEFAULT =  (1U << 23);
39     
40     const ubyte *preset_dict;
41     uint preset_dict_size;
42     uint lc;
43     enum LZMA_LCLP_MIN   = 0;
44     enum LZMA_LCLP_MAX   = 4;
45     enum LZMA_LC_DEFAULT = 3;
46     
47     uint lp;
48     enum LZMA_LP_DEFAULT = 0;
49     uint pb;
50     enum LZMA_PB_MIN     = 0;
51     enum LZMA_PB_MAX     = 4;
52     enum LZMA_PB_DEFAULT = 2;
53     
54     /** Compression mode */
55     lzma_mode mode;
56     uint nice_len;
57     lzma_match_finder mf;
58     uint depth;
59     uint reserved_int1;
60     uint reserved_int2;
61     uint reserved_int3;
62     uint reserved_int4;
63     uint reserved_int5;
64     uint reserved_int6;
65     uint reserved_int7;
66     uint reserved_int8;
67     lzma_reserved_enum reserved_enum1;
68     lzma_reserved_enum reserved_enum2;
69     lzma_reserved_enum reserved_enum3;
70     lzma_reserved_enum reserved_enum4;
71     void *reserved_ptr1;
72     void *reserved_ptr2;
73     
74 }
75 
76 nothrow lzma_bool lzma_lzma_preset(
77     lzma_options_lzma *options, uint preset);
78 
79 //TODO: initialize fields to void?
80 struct lzma_block
81 {
82     uint version_;
83     uint header_size;
84     enum LZMA_BLOCK_HEADER_SIZE_MIN = 8;
85     enum LZMA_BLOCK_HEADER_SIZE_MAX = 1024;
86     lzma_check check;    
87     lzma_vli compressed_size;    
88     lzma_vli uncompressed_size;    
89     lzma_filter *filters;    
90     ubyte[LZMA_CHECK_SIZE_MAX] raw_check;    
91     void *reserved_ptr1;
92     void *reserved_ptr2;
93     void *reserved_ptr3;
94     uint reserved_int1;
95     uint reserved_int2;
96     lzma_vli reserved_int3;
97     lzma_vli reserved_int4;
98     lzma_vli reserved_int5;
99     lzma_vli reserved_int6;
100     lzma_vli reserved_int7;
101     lzma_vli reserved_int8;
102     lzma_reserved_enum reserved_enum1;
103     lzma_reserved_enum reserved_enum2;
104     lzma_reserved_enum reserved_enum3;
105     lzma_reserved_enum reserved_enum4;
106     lzma_bool reserved_bool1;
107     lzma_bool reserved_bool2;
108     lzma_bool reserved_bool3;
109     lzma_bool reserved_bool4;
110     lzma_bool reserved_bool5;
111     lzma_bool reserved_bool6;
112     lzma_bool reserved_bool7;
113     lzma_bool reserved_bool8;
114 }
115 template lzma_block_header_size_decode(uint b)
116 {
117     enum lzma_block_header_size_decode = (b+1)*4;
118 }
119 lzma_ret lzma_block_header_size(lzma_block *block);
120 lzma_ret lzma_block_header_encode(const (lzma_block)* block, ubyte* out_);
121 lzma_ret lzma_block_header_decode(lzma_block* block,
122     lzma_allocator* allocator, const(ubyte)* in_);
123 lzma_ret lzma_block_compressed_size(
124     lzma_block* block, lzma_vli unpadded_size);
125 lzma_vli lzma_block_unpadded_size(const lzma_block* block);
126 pure lzma_vli lzma_block_total_size(const(lzma_block*) block);
127 lzma_ret lzma_block_encoder(
128     lzma_stream* strm, lzma_block* block);
129 lzma_ret lzma_block_decoder(
130     lzma_stream *strm, lzma_block *block);
131 size_t lzma_block_buffer_bound(size_t uncompressed_size);
132 lzma_ret lzma_block_buffer_encode(
133     lzma_block *block, lzma_allocator *allocator,
134     const(ubyte)* in_, size_t in_size,
135     ubyte* out_, size_t *out_pos, size_t out_size);
136 lzma_ret lzma_block_buffer_decode(
137     lzma_block *block, lzma_allocator *allocator,
138     const(ubyte)* in_, size_t *in_pos, size_t in_size,
139     ubyte* out_, size_t *out_pos, size_t out_size);
140 enum LZMA_FILTER_X86 = 0x04UL;
141 
142 enum LZMA_FILTER_POWERPC = 0x05UL;
143 enum LZMA_FILTER_IA64 = 0x06UL;
144 enum LZMA_FILTER_ARM = 0x07UL;
145 enum LZMA_FILTER_ARMTHUMB = 0x08UL;
146 enum LZMA_FILTER_SPARC = 0x09UL;
147 struct lzma_options_bcj
148 {
149     
150     uint start_offset;
151 }
152 
153 
154 alias lzma_bool = bool;
155 enum lzma_reserved_enum
156 {
157     LZMA_RESERVED_ENUM      = 0
158 }
159 alias lzma_ret = int;
160 enum : lzma_ret
161 {
162     LZMA_OK                 = 0,
163     LZMA_STREAM_END         = 1,
164     LZMA_NO_CHECK           = 2,
165     LZMA_UNSUPPORTED_CHECK  = 3,
166     LZMA_GET_CHECK          = 4,
167     LZMA_MEM_ERROR          = 5,
168     LZMA_MEMLIMIT_ERROR     = 6,
169     LZMA_FORMAT_ERROR       = 7,
170     LZMA_OPTIONS_ERROR      = 8,
171     LZMA_DATA_ERROR         = 9,
172     LZMA_BUF_ERROR          = 10,
173     LZMA_PROG_ERROR         = 11,
174 }
175 alias lzma_action = int;
176 enum : lzma_action
177 {
178     LZMA_RUN = 0,
179     LZMA_SYNC_FLUSH = 1,
180     LZMA_FULL_FLUSH = 2,
181     LZMA_FINISH = 3
182 }
183 struct lzma_allocator
184 {
185     void* function(void *opaque, size_t nmemb, size_t size) alloc;
186     void function(void *opaque, void *ptr) free;
187     void *opaque;
188 }
189 struct lzma_internal {}
190 struct lzma_stream
191 {
192     const(ubyte)* next_in;
193     
194     size_t avail_in;
195     
196     ulong total_in;
197     ubyte* next_out;
198     
199     size_t avail_out;
200     
201     ulong total_out;
202     
203     lzma_allocator *allocator;
204     
205     lzma_internal *internal;
206     
207     void *reserved_ptr1;
208     void *reserved_ptr2;
209     void *reserved_ptr3;
210     void *reserved_ptr4;
211     ulong reserved_int1;
212     ulong reserved_int2;
213     size_t reserved_int3;
214     size_t reserved_int4;
215     lzma_reserved_enum reserved_enum1;
216     lzma_reserved_enum reserved_enum2;
217 }
218 //
219 @property lzma_stream LZMA_STREAM_INIT(){ return lzma_stream.init; }
220 lzma_ret lzma_code(lzma_stream *strm, lzma_action action);
221 void lzma_end(lzma_stream *strm);
222 pure ulong lzma_memusage(const lzma_stream *strm);
223 pure ulong lzma_memlimit_get(const lzma_stream *strm);
224 lzma_ret lzma_memlimit_set(lzma_stream *strm, ulong memlimit);
225 
226 alias lzma_check = int;
227 enum : lzma_check
228 {
229     LZMA_CHECK_NONE     = 0,
230     LZMA_CHECK_CRC32    = 1,
231     LZMA_CHECK_CRC64    = 4,
232     LZMA_CHECK_SHA256   = 10
233 }
234 enum LZMA_CHECK_ID_MAX = 15;
235 lzma_bool lzma_check_is_supported(lzma_check check);
236 uint lzma_check_size(lzma_check check);
237 enum LZMA_CHECK_SIZE_MAX = 64;
238 pure uint lzma_crc32(
239     const(ubyte)* buf, size_t size, uint crc);
240 pure ulong lzma_crc64(
241     const(ubyte)* buf, size_t size, ulong crc);
242 lzma_check lzma_get_check(const lzma_stream *strm);
243 
244 enum uint LZMA_PRESET_DEFAULT = 6U;
245 enum uint LZMA_PRESET_LEVEL_MASK = 0x1FU;
246 enum uint LZMA_PRESET_EXTREME = (1U << 31);
247 pure ulong lzma_easy_encoder_memusage(uint preset);
248 pure ulong lzma_easy_decoder_memusage(uint preset);
249 lzma_ret lzma_easy_encoder(
250     lzma_stream *strm, uint preset, lzma_check check);
251 lzma_ret lzma_easy_buffer_encode(
252     uint preset, lzma_check check,
253     lzma_allocator *allocator, const(ubyte)* in_, size_t in_size,
254     ubyte* out_, size_t *out_pos, size_t out_size);
255 lzma_ret lzma_stream_encoder(lzma_stream *strm,
256     const lzma_filter *filters, lzma_check check);
257 lzma_ret lzma_alone_encoder(
258     lzma_stream *strm, const lzma_options_lzma *options);
259 size_t lzma_stream_buffer_bound(size_t uncompressed_size);
260 lzma_ret lzma_stream_buffer_encode(
261     lzma_filter *filters, lzma_check check,
262     lzma_allocator *allocator, const(ubyte)* in_, size_t in_size,
263     ubyte* out_, size_t *out_pos, size_t out_size);
264 enum uint LZMA_TELL_NO_CHECK = 0x01U;
265 enum uint  LZMA_TELL_UNSUPPORTED_CHECK = 0x02U;
266 enum uint LZMA_TELL_ANY_CHECK = 0x04U;
267 enum uint LZMA_CONCATENATED = 0x08U;
268 lzma_ret lzma_stream_decoder(
269     lzma_stream *strm, ulong memlimit, uint flags);
270 lzma_ret lzma_auto_decoder(
271     lzma_stream *strm, ulong memlimit, uint flags);
272 lzma_ret lzma_alone_decoder(
273     lzma_stream *strm, ulong memlimit);
274 lzma_ret lzma_stream_buffer_decode(
275     ulong *memlimit, uint flags, lzma_allocator *allocator,
276     const (ubyte)* in_, size_t *in_pos, size_t in_size,
277     ubyte* out_, size_t *out_pos, size_t out_size);
278 
279 enum LZMA_FILTER_DELTA = 0x03UL;
280 enum lzma_delta_type
281 {
282     LZMA_DELTA_TYPE_BYTE
283 }
284 struct lzma_options_delta
285 {
286     
287     lzma_delta_type type;
288     
289     uint dist;
290     enum LZMA_DELTA_DIST_MIN = 1;
291     enum LZMA_DELTA_DIST_MAX = 256;
292     
293     uint reserved_int1;
294     uint reserved_int2;
295     uint reserved_int3;
296     uint reserved_int4;
297     void *reserved_ptr1;
298     void *reserved_ptr2;
299 }
300 
301 enum LZMA_FILTERS_MAX = 4;
302 struct lzma_filter
303 {
304     
305     lzma_vli id;
306     
307     void *options;
308 }
309 lzma_bool lzma_filter_encoder_is_supported(lzma_vli id);
310 lzma_bool lzma_filter_decoder_is_supported(lzma_vli id);
311 lzma_ret lzma_filters_copy(const lzma_filter*src,
312     lzma_filter *dest, lzma_allocator *allocator);
313 pure ulong lzma_raw_encoder_memusage(const lzma_filter *filters);
314 pure ulong lzma_raw_decoder_memusage(const lzma_filter *filters);
315 lzma_ret lzma_raw_encoder(
316     lzma_stream *strm, const lzma_filter *filters);
317 lzma_ret lzma_raw_decoder(
318     lzma_stream *strm, const lzma_filter *filters);
319 lzma_ret lzma_filters_update(
320     lzma_stream *strm, const lzma_filter *filters);
321 lzma_ret lzma_raw_buffer_encode(
322     const lzma_filter *filters, lzma_allocator *allocator,
323     const(ubyte) *in_, size_t in_size, ubyte *out_,
324     size_t *out_pos, size_t out_size);
325 lzma_ret lzma_raw_buffer_decode(const lzma_filter *filters,
326     lzma_allocator *allocator,
327     const(ubyte) *in_, size_t *in_pos, size_t in_size,
328     ubyte *out_, size_t *out_pos, size_t out_size);
329 lzma_ret lzma_properties_size(
330     uint *size, const lzma_filter *filter);
331 lzma_ret lzma_properties_encode(
332     const lzma_filter *filter, ubyte *props);
333 lzma_ret lzma_properties_decode(
334     lzma_filter *filter, lzma_allocator *allocator,
335     const ubyte *props, size_t props_size);
336 lzma_ret lzma_filter_flags_size(
337     uint *size, const lzma_filter *filter);
338 lzma_ret lzma_filter_flags_encode(const lzma_filter *filter,
339     ubyte *out_, size_t *out_pos, size_t out_size);
340 lzma_ret lzma_filter_flags_decode(
341     lzma_filter *filter, lzma_allocator *allocator,
342     const ubyte *in_, size_t *in_pos, size_t in_size);
343 ulong lzma_physmem();
344 
345 struct lzma_index {}
346 struct lzma_index_iter
347 {
348     struct StreamStruct
349     {
350         
351         const lzma_stream_flags *flags;
352         const void *reserved_ptr1;
353         const void *reserved_ptr2;
354         const void *reserved_ptr3;
355         
356         lzma_vli number;
357         
358         lzma_vli block_count;
359         
360         lzma_vli compressed_offset;
361         
362         lzma_vli uncompressed_offset;
363         
364         lzma_vli compressed_size;
365         
366         lzma_vli uncompressed_size;
367         
368         lzma_vli padding;
369         lzma_vli reserved_vli1;
370         lzma_vli reserved_vli2;
371         lzma_vli reserved_vli3;
372         lzma_vli reserved_vli4;
373     }
374     StreamStruct m_stream;
375     struct BlockStruct
376     {
377         
378         lzma_vli number_in_file;
379         
380         lzma_vli compressed_file_offset;
381         
382         lzma_vli uncompressed_file_offset;
383         
384         lzma_vli number_in_stream;
385         
386         lzma_vli compressed_stream_offset;
387         
388         lzma_vli uncompressed_stream_offset;
389         
390         lzma_vli uncompressed_size;
391         
392         lzma_vli unpadded_size;
393         
394         lzma_vli total_size;
395         lzma_vli reserved_vli1;
396         lzma_vli reserved_vli2;
397         lzma_vli reserved_vli3;
398         lzma_vli reserved_vli4;
399         const void *reserved_ptr1;
400         const void *reserved_ptr2;
401         const void *reserved_ptr3;
402         const void *reserved_ptr4;
403     }
404     BlockStruct block;
405     
406     union InternalData
407     {
408         const void *p;
409         size_t s;
410         lzma_vli v;
411     }
412     InternalData[6] internal;
413 }
414 enum lzma_index_iter_mode
415 {
416     LZMA_INDEX_ITER_ANY             = 0,
417     LZMA_INDEX_ITER_STREAM          = 1,
418     LZMA_INDEX_ITER_BLOCK           = 2,
419     LZMA_INDEX_ITER_NONEMPTY_BLOCK  = 3
420 }
421 ulong lzma_index_memusage(lzma_vli streams, lzma_vli blocks);
422 ulong lzma_index_memused(const lzma_index *i);
423 lzma_index* lzma_index_init(lzma_allocator *allocator);
424 void lzma_index_end(lzma_index *i, lzma_allocator *allocator);
425 lzma_ret lzma_index_append(
426     lzma_index *i, lzma_allocator *allocator,
427     lzma_vli unpadded_size, lzma_vli uncompressed_size);
428 lzma_ret lzma_index_stream_flags(
429     lzma_index *i, const lzma_stream_flags *stream_flags);
430 pure uint lzma_index_checks(const lzma_index *i);
431 lzma_ret lzma_index_stream_padding(
432     lzma_index *i, lzma_vli stream_padding);
433 pure lzma_vli lzma_index_stream_count(const lzma_index *i);
434 pure lzma_vli lzma_index_block_count(const lzma_index *i);
435 pure lzma_vli lzma_index_size(const lzma_index *i);
436 pure lzma_vli lzma_index_stream_size(const lzma_index *i);
437 pure lzma_vli lzma_index_total_size(const lzma_index *i);
438 pure lzma_vli lzma_index_file_size(const lzma_index *i);
439 pure lzma_vli lzma_index_uncompressed_size(const lzma_index *i);
440 void lzma_index_iter_init(
441     lzma_index_iter *iter, const lzma_index *i);
442 void lzma_index_iter_rewind(lzma_index_iter *iter);
443 lzma_bool lzma_index_iter_next(
444     lzma_index_iter *iter, lzma_index_iter_mode mode);
445 lzma_bool lzma_index_iter_locate(
446     lzma_index_iter *iter, lzma_vli target);
447 lzma_ret lzma_index_cat(
448     lzma_index *dest, lzma_index *src, lzma_allocator *allocator);
449 lzma_index * lzma_index_dup(
450     const lzma_index *i, lzma_allocator *allocator);
451 lzma_ret lzma_index_encoder(
452     lzma_stream *strm, const lzma_index *i);
453 lzma_ret lzma_index_decoder(
454     lzma_stream *strm, lzma_index **i, ulong memlimit);
455 lzma_ret lzma_index_buffer_encode(const lzma_index *i,
456     ubyte *out_, size_t *out_pos, size_t out_size);
457 lzma_ret lzma_index_buffer_decode(lzma_index **i,
458     ulong *memlimit, lzma_allocator *allocator,
459     const ubyte *in_, size_t *in_pos, size_t in_size);
460 struct lzma_index_hash {}
461 lzma_index_hash * lzma_index_hash_init(
462     lzma_index_hash *index_hash, lzma_allocator *allocator);
463 void lzma_index_hash_end(
464     lzma_index_hash *index_hash, lzma_allocator *allocator);
465 lzma_ret lzma_index_hash_append(lzma_index_hash *index_hash,
466     lzma_vli unpadded_size, lzma_vli uncompressed_size);
467 lzma_ret lzma_index_hash_decode(lzma_index_hash *index_hash,
468     const ubyte *in_, size_t *in_pos, size_t in_size);
469 pure lzma_vli lzma_index_hash_size(
470     const lzma_index_hash *index_hash);
471 enum LZMA_STREAM_HEADER_SIZE = 12;
472 struct lzma_stream_flags
473 {
474     
475     uint version_;
476     
477     lzma_vli backward_size;
478     enum LZMA_BACKWARD_SIZE_MIN = 4;
479     enum LZMA_BACKWARD_SIZE_MAX = (1UL << 34);
480     
481     lzma_check check;
482     
483     lzma_reserved_enum reserved_enum1;
484     lzma_reserved_enum reserved_enum2;
485     lzma_reserved_enum reserved_enum3;
486     lzma_reserved_enum reserved_enum4;
487     lzma_bool reserved_bool1;
488     lzma_bool reserved_bool2;
489     lzma_bool reserved_bool3;
490     lzma_bool reserved_bool4;
491     lzma_bool reserved_bool5;
492     lzma_bool reserved_bool6;
493     lzma_bool reserved_bool7;
494     lzma_bool reserved_bool8;
495     uint reserved_int1;
496     uint reserved_int2;
497 }
498 lzma_ret lzma_stream_header_encode(
499     const lzma_stream_flags *options, ubyte *out_);
500 lzma_ret lzma_stream_footer_encode(
501     const lzma_stream_flags *options, ubyte *out_);
502 lzma_ret lzma_stream_header_decode(
503     lzma_stream_flags *options, const ubyte *in_);
504 lzma_ret lzma_stream_footer_decode(
505     lzma_stream_flags *options, const ubyte *in_);
506 pure lzma_ret lzma_stream_flags_compare(
507     const lzma_stream_flags *a, const lzma_stream_flags *b);
508 
509 enum LZMA_VERSION_MAJOR = 5;
510 enum LZMA_VERSION_MINOR = 0;
511 enum LZMA_VERSION_PATCH = 3;
512 enum LZMA_VERSION_STABILITY = LZMA_VERSION_STABILITY_STABLE;
513 enum LZMA_VERSION_COMMIT = "";
514 enum LZMA_VERSION_STABILITY_ALPHA = 0;
515 enum LZMA_VERSION_STABILITY_BETA = 1;
516 enum LZMA_VERSION_STABILITY_STABLE = 2;
517 enum LZMA_VERSION = (LZMA_VERSION_MAJOR * 10000000U 
518     + LZMA_VERSION_MINOR * 10000U
519     + LZMA_VERSION_PATCH * 10U
520     + LZMA_VERSION_STABILITY);
521 static if (LZMA_VERSION_STABILITY == LZMA_VERSION_STABILITY_ALPHA)
522     enum LZMA_VERSION_STABILITY_STRING = "alpha";
523 else static if (LZMA_VERSION_STABILITY == LZMA_VERSION_STABILITY_BETA)
524     enum LZMA_VERSION_STABILITY_STRING = "beta";
525 else static if (LZMA_VERSION_STABILITY == LZMA_VERSION_STABILITY_STABLE)
526     enum LZMA_VERSION_STABILITY_STRING = "";
527 else
528     static assert(false, "Incorrect LZMA_VERSION_STABILITY");
529 enum LZMA_VERSION_STRING = 
530     to!string(LZMA_VERSION_MAJOR) ~ "." ~ to!string(LZMA_VERSION_MINOR) ~
531         "." ~ to!string(LZMA_VERSION_PATCH) ~ LZMA_VERSION_STABILITY_STRING ~
532         LZMA_VERSION_COMMIT;
533 uint lzma_version_number();
534 immutable(char)* lzma_version_string();
535 enum LZMA_VLI_MAX = (ulong.max / 2);
536 enum LZMA_VLI_UNKNOWN = ulong.max;
537 enum LZMA_VLI_BYTES_MAX = 9;
538 //#define LZMA_VLI_C(n) UINT64_C(n)
539 alias lzma_vli = ulong;
540 bool lzma_vli_is_valid(lzma_vli vli)
541 {
542     return vli <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN;
543 }
544 lzma_ret lzma_vli_encode(lzma_vli vli, size_t *vli_pos,
545     ubyte* out_, size_t *out_pos, size_t out_size);
546 lzma_ret lzma_vli_decode(lzma_vli *vli, size_t *vli_pos,
547     const(ubyte)* in_, size_t *in_pos, size_t in_size);
548 pure uint lzma_vli_size(lzma_vli vli);
549