1 /*****************************************************************************
2 
3     LZO library binding (lzoconf.h functions)
4 
5     Please consult the original header documentation for details.
6 
7     You need to have the library installed and link with -llzo2.
8 
9     Copyright:
10         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
11         All rights reserved.
12 
13     License:
14         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
15         Alternatively, this file may be distributed under the terms of the Tango
16         3-Clause BSD License (see LICENSE_BSD.txt for details).
17 
18         Bear in mind this module provides bindings to an external library that
19         has its own license, which might be more restrictive. Please check the
20         external library license to see which conditions apply for linking.
21 
22  ******************************************************************************/
23 
24 module ocean.io.compress.lzo.c.lzoconf;
25 
26 
27 extern (C)
28 {
29     /**************************************************************************
30 
31         Status codes
32 
33      **************************************************************************/
34 
35     enum LzoStatus : int
36     {
37         OK                =  0, // LZO_E_OK
38         Error             = -1, // LZO_E_ERROR
39         OutOfMemory       = -2, // LZO_E_OUT_OF_MEMORY      [not used right now]
40         NotCompressible   = -3, // LZO_E_NOT_COMPRESSIBLE   [not used right now]
41         InputOverrun      = -4, // LZO_E_INPUT_OVERRUN
42         OutputOverrun     = -5, // LZO_E_OUTPUT_OVERRUN
43         LookBehindOverrun = -6, // LZO_E_LOOKBEHIND_OVERRUN
44         EofNotFound       = -7, // LZO_E_EOF_NOT_FOUND
45         InputNotConsumed  = -8, // LZO_E_INPUT_NOT_CONSUMED
46         NotYetImplemented = -9 // LZO_E_NOT_YET_IMPLEMENTED [not used right now]
47     }
48 
49     /**************************************************************************
50 
51         Working memory size
52 
53      **************************************************************************/
54 
55     static immutable size_t Lzo1x1WorkmemSize = 16 * 1024 * (ubyte*).sizeof;
56 
57     /**************************************************************************
58 
59         Function type definitions
60 
61      **************************************************************************/
62 
63     alias int function ( ubyte* src, size_t src_len,
64                          ubyte* dst, size_t* dst_len, void* wrkmem ) lzo_compress_t;
65 
66     alias int function ( ubyte* src, size_t src_len,
67                          ubyte* dst, size_t* dst_len, void* wrkmem ) lzo_decompress_t;
68 
69     alias int function ( ubyte* src, size_t src_len,
70                          ubyte* dst, size_t* dst_len, void* wrkmem ) lzo_optimize_t;
71 
72     alias int function ( ubyte* src, size_t src_len,
73                          ubyte* dst, size_t* dst_len,
74                          void* wrkmem, char* dict, size_t dict_len ) lzo_compress_dict_t;
75 
76     alias void* function ( lzo_callback_t* self, size_t items, size_t size ) lzo_alloc_func_t;
77 
78     alias void function ( lzo_callback_t* self, void* ptr ) lzo_free_func_t;
79 
80     alias void function ( lzo_callback_t*, size_t, size_t, int ) lzo_progress_func_t;
81 
82     /**************************************************************************
83 
84         lzo_callback_t structure
85 
86      **************************************************************************/
87 
88     struct lzo_callback_t
89     {
90         lzo_alloc_func_t    nalloc;
91         lzo_free_func_t     nfree;
92         lzo_progress_func_t nprogress;
93 
94         void* user1;
95         size_t  user2;
96         size_t user3;
97     };
98 
99     /**************************************************************************
100 
101         Calculates an Adler-32 value from data in _buf.
102 
103         Params:
104             _adler = initial Adler-32 value
105             _buf   = data buffer
106             _len   = data length
107 
108         Returns:
109             resulting Adler-32 value
110 
111      **************************************************************************/
112 
113     uint lzo_adler32 ( uint _adler, ubyte* _buf, uint _len );
114 
115     /**************************************************************************
116 
117         Returns the library version number.
118 
119         Returns:
120             library version number
121 
122      **************************************************************************/
123 
124     uint lzo_version ( );
125 
126     /**************************************************************************
127 
128         Initializes the library and informs it about the size of a variety of
129         data types.
130 
131         Note that both "int" and "long" C datatypes correspond to "int" in D;
132         D's "long" corresponds to C99's "long long".
133 
134         Params:
135             ver               = supposed library version number
136             sizeof_short      = short.sizeof
137             sizeof_int        = int.sizeof
138             sizeof_long       = int.sizeof
139             sizeof_uint32     = uint.sizeof
140             sizeof_uint       = uint.sizeof,
141             sizeof_dict_t     = (ubyte*).sizeof
142             sizeof_charp      = (char*).sizeof
143             sizeof_voidp      = (void*).sizeof
144             sizeof_callback_t = lzo_callback_t.sizeof
145 
146         Returns:
147             LzoStatus.OK if the library feels that it is in a healty condition
148             or something else if it is not well disposed today.
149 
150      **************************************************************************/
151 
152     private LzoStatus __lzo_init_v2( uint ver,
153                                      int  sizeof_short,
154                                      int  sizeof_int,
155                                      int  sizeof_long,
156                                      int  sizeof_uint32,
157                                      int  sizeof_uint,
158                                      int  sizeof_dict_t,    // ubyte*
159                                      int  sizeof_charp,
160                                      int  sizeof_voidp,
161                                      int  sizeof_callback_t );
162 
163     /**************************************************************************
164 
165         Calculates a 32-bit CRC value from data in _buf.
166 
167         Params:
168             _c   = initial 32-bit CRC value
169             _buf = data buffer
170             _len   = data length
171 
172         Returns:
173             resulting 32-bit CRC value
174 
175     **************************************************************************/
176 
177     uint lzo_crc32   ( uint _c, in ubyte* _buf, uint _len );
178 
179     /**************************************************************************
180 
181         Returns the table of 32-bit CRC values of all byte values. The table has
182         a length of 256.
183 
184         Returns:
185             table of 32-bit CRC values of all byte values
186 
187     **************************************************************************/
188 
189     uint* lzo_get_crc32_table ( );
190 }
191 
192 /******************************************************************************
193 
194     Calculates the maximum compressed length of data which has a length of
195     uncompressed_length.
196 
197     Note: Surprisingly, this is more than uncompressed_length but that's the
198           worst case for completely uncompressable data.
199 
200     Parameters:
201         uncompressed_length = length of data to compressed
202 
203     Returns:
204         maximum compressed length of data
205 
206  ******************************************************************************/
207 
208 size_t lzo1x_max_compressed_length ( size_t uncompressed_length )
209 {
210     return uncompressed_length + (uncompressed_length >> 4) + 0x40 + 3;
211 }
212 
213 /******************************************************************************
214 
215     Returns the initial Adler-32 value to use with lzo_adler32().
216 
217     Returns:
218         initial Adler-32 value
219 
220  ******************************************************************************/
221 
222 uint lzo_adler32_init ( )
223 {
224     return lzo_adler32(0, null, 0);
225 }
226 
227 /**************************************************************************
228 
229     Initializes the library and informs it about the size of a variety of
230     data types.
231 
232     Returns:
233         LzoStatus.OK if the library feels that it is in a healty condition or
234         something else if it is not well disposed today.
235 
236  **************************************************************************/
237 
238 int lzo_init ( )
239 {
240     return __lzo_init_v2(lzo_version(),
241                          short.sizeof, int.sizeof, size_t.sizeof, uint.sizeof,size_t.sizeof,
242                          (ubyte*).sizeof, (char*).sizeof, (void*).sizeof, lzo_callback_t.sizeof);
243 }
244 
245 /******************************************************************************
246 
247     Returns the initial 32-bit CRC value to use with lzo_crc32().
248 
249     Returns:
250         initial 32-bit CRC value
251 
252 ******************************************************************************/
253 
254 uint lzo_crc32_init ( )
255 {
256     return lzo_crc32(0, null, 0);
257 }
258