1
- // Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2
- // 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022
3
- // Python Software Foundation; All Rights Reserved
4
-
5
- // This file is part of python-isal which is distributed under the
6
- // PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2.
7
-
8
- // This file was modified from Cpython Modules/bz2module.c file from the 3.9
9
- // branch.
10
-
11
- // Changes compared to CPython:
12
- // - The BZ2Decompressor has been used as a basis for IgzipDecompressor.
13
- // Functionality is almost the same. IgzipDecompressor does have a more
14
- // elaborate __init__ to set settings. It also implements decompress_buf more
15
- // akin to how decompression is implemented in isal_shared.h
16
- // - Constants were added that are particular to igzip_lib.
17
- // - Argument parsers were written using th CPython API rather than argument
18
- // clinic.
1
+ /*
2
+ Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3
+ 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022
4
+ Python Software Foundation; All Rights Reserved
5
+
6
+ This file is part of python-isal which is distributed under the
7
+ PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2.
8
+
9
+ This file was modified from Cpython Modules/bz2module.c file from the 3.9
10
+ branch.
11
+
12
+ Changes compared to CPython:
13
+ - The BZ2Decompressor has been used as a basis for IgzipDecompressor.
14
+ Functionality is almost the same. IgzipDecompressor does have a more
15
+ elaborate __init__ to set settings. It also implements decompress_buf more
16
+ akin to how decompression is implemented in isal_shared.h
17
+ - Constants were added that are particular to igzip_lib.
18
+ - Argument parsers were written using th CPython API rather than argument
19
+ clinic.
20
+ */
19
21
20
22
#include "isal_shared.h"
21
23
22
24
typedef struct {
23
25
PyObject_HEAD
24
- struct inflate_state state ;
25
- char eof ; /* T_BOOL expects a char */
26
26
PyObject * unused_data ;
27
27
PyObject * zdict ;
28
- char needs_input ;
29
28
uint8_t * input_buffer ;
30
29
Py_ssize_t input_buffer_size ;
31
-
32
30
/* inflate_state>avail_in is only 32 bit, so we store the true length
33
31
separately. Conversion and looping is encapsulated in
34
32
decompress_buf() */
35
33
Py_ssize_t avail_in_real ;
34
+ char eof ; /* T_BOOL expects a char */
35
+ char needs_input ;
36
+ /* Struct inflate state contains a massive buffer at the end. Put it at
37
+ the end of the IgzipDecompressor so members can be accessed easily. */
38
+ struct inflate_state state ;
36
39
} IgzipDecompressor ;
37
40
38
41
static void
39
42
IgzipDecompressor_dealloc (IgzipDecompressor * self )
40
43
{
41
- if (self -> input_buffer != NULL )
42
- PyMem_Free (self -> input_buffer );
44
+ PyMem_Free (self -> input_buffer );
43
45
Py_CLEAR (self -> unused_data );
44
46
Py_CLEAR (self -> zdict );
45
47
Py_TYPE (self )-> tp_free ((PyObject * )self );
@@ -62,16 +64,16 @@ decompress_buf(IgzipDecompressor *self, Py_ssize_t max_length)
62
64
63
65
int err ;
64
66
65
- // In Python 3.10 sometimes sys.maxsize is passed by default. In those cases
66
- // we do want to use DEF_BUF_SIZE as start buffer.
67
+ /* In Python 3.10 sometimes sys.maxsize is passed by default. In those cases
68
+ we do want to use DEF_BUF_SIZE as start buffer. */
67
69
if ((max_length < 0 ) || max_length == PY_SSIZE_T_MAX ) {
68
70
hard_limit = PY_SSIZE_T_MAX ;
69
71
obuflen = DEF_BUF_SIZE ;
70
72
} else {
71
- // Assume that decompressor is used in file decompression with a fixed
72
- // block size of max_length. In that case we will reach max_length almost
73
- // always (except at the end of the file). So it makes sense to allocate
74
- // max_length.
73
+ /* Assume that decompressor is used in file decompression with a fixed
74
+ block size of max_length. In that case we will reach max_length almost
75
+ always (except at the end of the file). So it makes sense to allocate
76
+ max_length. */
75
77
hard_limit = max_length ;
76
78
obuflen = max_length ;
77
79
if (obuflen > DEF_MAX_INITIAL_BUF_SIZE ){
@@ -102,8 +104,10 @@ decompress_buf(IgzipDecompressor *self, Py_ssize_t max_length)
102
104
isal_inflate_error (err );
103
105
goto error ;
104
106
}
105
- } while (self -> state .avail_out == 0 && self -> state .block_state != ISAL_BLOCK_FINISH );
106
- } while (self -> avail_in_real != 0 && self -> state .block_state != ISAL_BLOCK_FINISH );
107
+ } while (self -> state .avail_out == 0 &&
108
+ self -> state .block_state != ISAL_BLOCK_FINISH );
109
+ } while (self -> avail_in_real != 0 &&
110
+ self -> state .block_state != ISAL_BLOCK_FINISH );
107
111
108
112
if (self -> state .block_state == ISAL_BLOCK_FINISH )
109
113
self -> eof = 1 ;
@@ -189,7 +193,8 @@ decompress(IgzipDecompressor *self, uint8_t *data, size_t len, Py_ssize_t max_le
189
193
goto error ;
190
194
char * new_data_ptr = PyBytes_AS_STRING (new_data );
191
195
bitbuffer_copy (& (self -> state ), new_data_ptr , bytes_in_bitbuffer );
192
- memcpy (new_data_ptr + bytes_in_bitbuffer , self -> state .next_in , self -> avail_in_real );
196
+ memcpy (new_data_ptr + bytes_in_bitbuffer , self -> state .next_in ,
197
+ self -> avail_in_real );
193
198
Py_XSETREF (self -> unused_data , new_data );
194
199
}
195
200
}
@@ -257,13 +262,14 @@ PyDoc_STRVAR(igzip_lib_compress__doc__,
257
262
" the header and trailer are controlled by the flag parameter." );
258
263
259
264
#define IGZIP_LIB_COMPRESS_METHODDEF \
260
- {"compress", (PyCFunction)(void(*)(void))igzip_lib_compress, METH_VARARGS|METH_KEYWORDS, igzip_lib_compress__doc__}
265
+ {"compress", (PyCFunction)(void(*)(void))igzip_lib_compress, \
266
+ METH_VARARGS|METH_KEYWORDS, igzip_lib_compress__doc__}
261
267
262
268
static PyObject *
263
269
igzip_lib_compress (PyObject * module , PyObject * args , PyObject * kwargs )
264
270
{
265
- char * keywords [] = {"" , "level" , "flag" , "mem_level" , "hist_bits" , NULL };
266
- char * format = "y*|iiii:compress" ;
271
+ static char * keywords [] = {"" , "level" , "flag" , "mem_level" , "hist_bits" , NULL };
272
+ static char * format = "y*|iiii:compress" ;
267
273
Py_buffer data = {NULL , NULL };
268
274
int level = ISAL_DEFAULT_COMPRESSION ;
269
275
int flag = COMP_DEFLATE ;
@@ -298,13 +304,14 @@ PyDoc_STRVAR(igzip_lib_decompress__doc__,
298
304
" The initial output buffer size." );
299
305
300
306
#define IGZIP_LIB_DECOMPRESS_METHODDEF \
301
- {"decompress", (PyCFunction)(void(*)(void))igzip_lib_decompress, METH_VARARGS|METH_KEYWORDS, igzip_lib_decompress__doc__}
307
+ {"decompress", (PyCFunction)(void(*)(void))igzip_lib_decompress, \
308
+ METH_VARARGS|METH_KEYWORDS, igzip_lib_decompress__doc__}
302
309
303
310
static PyObject *
304
311
igzip_lib_decompress (PyObject * module , PyObject * args , PyObject * kwargs )
305
312
{
306
- char * keywords [] = {"" , "flag" , "hist_bits" , "bufsize" , NULL };
307
- char * format = "y*|iin:decompress" ;
313
+ static char * keywords [] = {"" , "flag" , "hist_bits" , "bufsize" , NULL };
314
+ static char * format = "y*|iin:decompress" ;
308
315
Py_buffer data = {NULL , NULL };
309
316
int flag = DECOMP_DEFLATE ;
310
317
int hist_bits = ISAL_DEF_MAX_HIST_BITS ;
@@ -315,7 +322,7 @@ igzip_lib_decompress(PyObject *module, PyObject *args, PyObject *kwargs)
315
322
& data , & flag , & hist_bits , & bufsize )) {
316
323
return NULL ;
317
324
}
318
- PyObject * return_value = igzip_lib_decompress_impl (& data , flag , hist_bits , bufsize );
325
+ PyObject * return_value = igzip_lib_decompress_impl (& data , flag , hist_bits , bufsize );
319
326
PyBuffer_Release (& data );
320
327
return return_value ;
321
328
}
@@ -340,13 +347,16 @@ PyDoc_STRVAR(igzip_lib_IgzipDecompressor_decompress__doc__,
340
347
"the unused_data attribute." );
341
348
342
349
#define IGZIP_LIB_IGZIPDECOMPRESSOR_DECOMPRESS_METHODDEF \
343
- {"decompress", (PyCFunction)(void(*)(void))igzip_lib_IgzipDecompressor_decompress, METH_VARARGS|METH_KEYWORDS, igzip_lib_IgzipDecompressor_decompress__doc__}
350
+ {"decompress", (PyCFunction)(void(*)(void))igzip_lib_IgzipDecompressor_decompress, \
351
+ METH_VARARGS|METH_KEYWORDS, igzip_lib_IgzipDecompressor_decompress__doc__}
344
352
345
353
static PyObject *
346
- igzip_lib_IgzipDecompressor_decompress (IgzipDecompressor * self , PyObject * args , PyObject * kwargs )
354
+ igzip_lib_IgzipDecompressor_decompress (IgzipDecompressor * self ,
355
+ PyObject * args ,
356
+ PyObject * kwargs )
347
357
{
348
- char * keywords [] = {"" , "max_length" , NULL };
349
- char * format = "y*|n:decompress" ;
358
+ static char * keywords [] = {"" , "max_length" , NULL };
359
+ static char * format = "y*|n:decompress" ;
350
360
Py_buffer data = {NULL , NULL };
351
361
Py_ssize_t max_length = -1 ;
352
362
@@ -383,8 +393,8 @@ igzip_lib_IgzipDecompressor__new__(PyTypeObject *type,
383
393
PyObject * args ,
384
394
PyObject * kwargs )
385
395
{
386
- char * keywords [] = {"flag" , "hist_bits" , "zdict" , NULL };
387
- char * format = "|iiO:IgzipDecompressor" ;
396
+ static char * keywords [] = {"flag" , "hist_bits" , "zdict" , NULL };
397
+ static char * format = "|iiO:IgzipDecompressor" ;
388
398
int flag = ISAL_DEFLATE ;
389
399
int hist_bits = ISAL_DEF_MAX_HIST_BITS ;
390
400
PyObject * zdict = NULL ;
@@ -458,8 +468,9 @@ static PyMemberDef IgzipDecompressor_members[] = {
458
468
READONLY , IgzipDecompressor_unused_data__doc__ },
459
469
{"needs_input" , T_BOOL , offsetof(IgzipDecompressor , needs_input ), READONLY ,
460
470
IgzipDecompressor_needs_input_doc },
461
- {"crc" , T_UINT , offsetof(IgzipDecompressor , state ) + offsetof(struct inflate_state , crc ), READONLY ,
462
- IgzipDecompressor_crc_doc },
471
+ {"crc" , T_UINT ,
472
+ offsetof(IgzipDecompressor , state ) + offsetof(struct inflate_state , crc ),
473
+ READONLY , IgzipDecompressor_crc_doc },
463
474
{NULL }
464
475
};
465
476
@@ -580,10 +591,12 @@ PyInit_igzip_lib(void)
580
591
return NULL ;
581
592
}
582
593
583
- if (PyType_Ready (& IgzipDecompressor_Type ) != 0 )
594
+ if (PyType_Ready (& IgzipDecompressor_Type ) != 0 ) {
584
595
return NULL ;
596
+ }
585
597
Py_INCREF (& IgzipDecompressor_Type );
586
- if (PyModule_AddObject (m , "IgzipDecompressor" , (PyObject * )& IgzipDecompressor_Type ) < 0 ) {
598
+ if (PyModule_AddObject (m , "IgzipDecompressor" ,
599
+ (PyObject * )& IgzipDecompressor_Type ) < 0 ) {
587
600
return NULL ;
588
601
}
589
602
0 commit comments