1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/***************************************************************************
* _______ _______ _______ *
* | ___ |____ | ___ | *
* | | |_| / /| | |_| *
* | |_____ / / | |_____ *
* |_____ | / / |_____ | *
* _ | | / / _ | | *
* | |___| |/ /____| |___| | *
* |_______|_______|_______| *
* *
* Wiimms SZS Tools *
* https://szs.wiimm.de/ *
* *
***************************************************************************
* *
* This file is part of the SZS project. *
* Visit https://szs.wiimm.de/ for project details and sources. *
* *
* Copyright (c) 2011-2024 by Dirk Clemens <wiimm@wiimm.de> *
* *
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
* *
***************************************************************************/
#ifndef SZS_LIB_BZIP2_H
#define SZS_LIB_BZIP2_H 1
#ifndef NO_BZIP2
#define _GNU_SOURCE 1
#include "lib-std.h"
//
///////////////////////////////////////////////////////////////////////////////
/////////////// definitions ///////////////
///////////////////////////////////////////////////////////////////////////////
#ifndef _BZLIB_H
typedef void BZFILE;
#endif
#define BZIP2_DEFAULT_COMPR 9
//-----------------------------------------------------------------------------
typedef struct BZIP2_t
{
File_t * file; // IO file
BZFILE * handle; // bzip2 handle
int compr_level; // active compression level
} BZIP2_t;
//
///////////////////////////////////////////////////////////////////////////////
/////////////// helpers ///////////////
///////////////////////////////////////////////////////////////////////////////
int IsBZ
(
// returns
// -1: not BZIP2 data
// 1..9: seems to be BZIP2 data; compression level is returned
cvp data, // NULL or data to investigate
uint size // size of 'data'
);
//-----------------------------------------------------------------------------
int IsBZIP2
(
// returns
// -1: not BZIP2 data
// 1..9: seems to be BZIP2 data; compression level is returned
cvp data, // NULL or data to investigate
int size // size of 'data'
);
//-----------------------------------------------------------------------------
ccp GetMessageBZIP2
(
int err, // error code
ccp unkown_error // result for unkown error codes
);
//-----------------------------------------------------------------------------
int CalcCompressionLevelBZIP2
(
int compr_level // valid are 1..9 / 0: use default value
);
//-----------------------------------------------------------------------------
u32 CalcMemoryUsageBZIP2
(
int compr_level, // valid are 1..9 / 0: use default value
bool is_writing // false: reading mode, true: writing mode
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// BZIP2 writing ///////////////
///////////////////////////////////////////////////////////////////////////////
#if 0 // not needed yet
///////////////////////////////////////////////////////////////////////////////
enumError EncodeBZIP2_Open
(
BZIP2_t * bz, // data structure, will be initialized
File_t * file, // destination file
int compr_level // valid are 1..9 / 0: use default value
);
//-----------------------------------------------------------------------------
enumError EncodeBZIP2_Write
(
BZIP2_t * bz, // created by EncodeBZIP2_Open()
const void * data, // data to write
size_t data_size // size of data to write
);
//-----------------------------------------------------------------------------
enumError EncodeBZIP2_Close
(
BZIP2_t * bz, // NULL or created by EncodeBZIP2_Open()
u32 * bytes_written // not NULL: store written bytes
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// BZIP2 reading ///////////////
///////////////////////////////////////////////////////////////////////////////
enumError DecodeBZIP2_Open
(
BZIP2_t * bz, // data structure, will be initialized
File_t * file // source file
);
//-----------------------------------------------------------------------------
enumError DecodeBZIP2_Read
(
BZIP2_t * bz, // created by DecodeBZIP2_Open()
void * buf, // destination buffer
size_t buf_size, // size of destination buffer
u32 * buf_written // not NULL: store bytes written to buf
);
//-----------------------------------------------------------------------------
enumError DecodeBZIP2_Close
(
BZIP2_t * bz // NULL or created by DecodeBZIP2_Open()
);
///////////////////////////////////////////////////////////////////////////////
#endif // 0 // not needed yet
///////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
/////////////// BZIP2 memory conversions ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[BZ2Manager_t]]
typedef struct BZ2Manager_t
{
//--- source
cvp src_data; // BZ2 is automatically detected and
// decoded by DecodeBZIP2(). Never NULL
uint src_size; // size of 'src_data'
//--- decoded
u8 *data; // NULL or data, alloced if not part of 'src_data'
uint size; // size of 'data'
}
BZ2Manager_t;
///////////////////////////////////////////////////////////////////////////////
enumError EncodeBZIP2buf
(
void *dest, // valid destination buffer
uint dest_size, // size of 'dest'
uint *dest_written, // store num bytes written to 'dest', never NULL
const void *src, // source buffer
uint src_size, // size of source buffer
int compr_level, // valid are 1..9 / 0: use default value
bool add_dec_size // true: add decompressed size
);
//-----------------------------------------------------------------------------
enumError EncodeBZIP2
(
u8 **dest_ptr, // result: store destination buffer addr
uint *dest_written, // store num bytes written to 'dest', never NULL
bool use_iobuf, // true: allow the usage of 'iobuf'
uint header_size, // insert 'header_size' bytes before dest data
bool add_dec_size, // true: add decompressed size before BZIP2 header
const void *src, // source buffer
uint src_size, // size of source buffer
int compr_level // valid are 1..9 / 0: use default value
);
//-----------------------------------------------------------------------------
enumError DecodeBZIP2bin
(
u8 **dest_ptr, // result: store destination buffer addr
uint *dest_written, // store num bytes written to 'dest_ptr', never NULL
uint header_size, // insert 'header_size' bytes before dest data
const void *src, // source buffer
uint src_size // size of source buffer
);
//-----------------------------------------------------------------------------
enumError DecodeBZIP2part
(
// decompress until dest buffer full or end of source reached
// return ERR_WARNING for an incomplete decompression
void *dest_buf, // result: store read data here
uint dest_size, // size of 'dest_buf'
uint *dest_written, // store num bytes written to 'dest_ptr', never NULL
const void *src, // source buffer
uint src_size // size of source buffer
);
//-----------------------------------------------------------------------------
enumError DecodeBZIP2buf
(
void *dest, // valid destination buffer
uint dest_size, // size of 'dest'
uint *dest_written, // store num bytes written to 'dest_ptr', never NULL
const void *src, // source buffer
uint src_size // size of source buffer
);
//-----------------------------------------------------------------------------
enumError DecodeBZIP2
(
u8 **dest_ptr, // result: store destination buffer addr
uint *dest_written, // store num bytes written to 'dest', never NULL
uint header_size, // insert 'header_size' bytes before dest data
const void *src, // source buffer, first 4 bytes = dest size
uint src_size // size of source buffer
);
//-----------------------------------------------------------------------------
enumError DecodeBZIP2Manager
(
BZ2Manager_t *mgr // manager data
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// BZ2S = BZ2-Source ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[BZ2SMode_t]]
typedef enum BZ2Status_t
{
BZ2S_F_BZIP2 = 1, // flag: source was bzip2 encoded
BZ2S_M_TYPE = ~1, // types without flags
BZ2S_T_NONE = 0, // no data found
BZ2S_T_INTERN = 2, // internal data used
BZ2S_T_FILE = 4, // external file used
}
BZ2Status_t;
//-----------------------------------------------------------------------------
// [[BZ2Source_t]]
typedef struct BZ2Source_t
{
//--- search files
ccp search; // Semicolon separated list of filenames
// or sub-paths to search.
// NULL terminate source lists.
//--- internal fall back data
cvp src_data; // NULL or internal fall back data.
// BZ2 is automatically detected and
// decoded by DecodeBZIP2().
uint src_size; // size of 'src_data'
file_format_t fform; // not FF_UNKNOWN: Allow only this type
uint part_offset; // >0: use only a part of (decoced) 'src_data'
uint part_size; // >0: limit result to this size if
// 'src_data' is used
//--- result of search
BZ2Status_t status; // return value of SearchBZ2S()
ccp fname; // NULL or filename, alloced if not 'InternalBZ2S'
u8 *data; // NULL or data, alloced if not part of 'src_data'
uint size; // size of 'data'
}
BZ2Source_t;
///////////////////////////////////////////////////////////////////////////////
typedef enum SearchLog_t
{
SEA_LOG_OFF,
SEA_LOG_SUCCESS,
SEA_LOG_FOUND,
SEA_LOG_ALL,
}
SearchLog_t;
extern const char InternalBZ2S[]; // = "<intern>"
//-----------------------------------------------------------------------------
uint ClearBZ2S
(
// Returns the number of cleard elements
// Call this function to free results of SearchBZ2S() and SearchListBZ2S()
// Both functions call ClearBZ2S() at the beginning.
BZ2Source_t *src, // valid BZ2Source_t list
int n // >=0: clear 'n' elements max,
// but always stop at list end!
);
//-----------------------------------------------------------------------------
static inline bool IsDataAllocedBZ2S ( BZ2Source_t *src )
{
DASSERT(src);
return !src->src_data
|| src->data < (u8*)src->src_data
|| src->data > (u8*)src->src_data + src->src_size;
}
//-----------------------------------------------------------------------------
u8 * NonConstDataBZ2S
(
// Converts src->data to malloced data (modifications possible)
// return src->data after conversion.
BZ2Source_t *src // valid BZ2Source_t
);
//-----------------------------------------------------------------------------
BZ2Status_t SearchBZ2S
(
BZ2Source_t *src, // valid single BZ2Source_t
ccp *dir_list, // list with search directories
// NULL and empty strings are ignored.
int n_dir_list, // number of elements in 'dir_list'
// <0: NULL termianted list
SearchLog_t log_level // one of SEA_LOG_*
);
//-----------------------------------------------------------------------------
uint SearchListBZ2S
(
// returns the number of found (file or internal) sources
BZ2Source_t *src, // valid BZ2Source_t list
int n, // >=0: search 'n' elements max
// but always stop at list end!
ccp *dir_list, // NULL or list with search directories
// NULL and empty strings are ignored.
int n_dir_list, // number of elements in 'dir_list'
// <0: NULL terminated list
SearchLog_t log_level // one of SEA_LOG_*
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// END ///////////////
///////////////////////////////////////////////////////////////////////////////
#endif // !NO_BZIP2
#endif // SZS_LIB_BZIP2_H 1
|