Skip to content

Commit d043751

Browse files
committed
Fix bug where zdict could not be set for igzipdecompressor
1 parent 0bd2d96 commit d043751

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

src/isal/igzip_libmodule.c

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ IgzipDecompressor_dealloc(IgzipDecompressor *self)
4949
Py_TYPE(self)->tp_free((PyObject *)self);
5050
}
5151

52+
53+
static int
54+
set_inflate_zdict_IgzipDecompressor(IgzipDecompressor *self)
55+
{
56+
Py_buffer zdict_buf;
57+
if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
58+
return -1;
59+
}
60+
if ((size_t)zdict_buf.len > UINT32_MAX) {
61+
PyErr_SetString(PyExc_OverflowError,
62+
"zdict length does not fit in an unsigned 32-bit integer");
63+
PyBuffer_Release(&zdict_buf);
64+
return -1;
65+
}
66+
int err;
67+
err = isal_inflate_set_dict(&self->state,
68+
zdict_buf.buf, (uint32_t)zdict_buf.len);
69+
PyBuffer_Release(&zdict_buf);
70+
if (err != ISAL_DECOMP_OK) {
71+
isal_inflate_error(err);
72+
return -1;
73+
}
74+
return 0;
75+
}
76+
77+
5278
/* Decompress data of length d->bzs_avail_in_real in d->state.next_in. The output
5379
buffer is allocated dynamically and returned. At most max_length bytes are
5480
returned, so some of the input may not be consumed. d->state.next_in and
@@ -105,12 +131,28 @@ decompress_buf(IgzipDecompressor *self, Py_ssize_t max_length)
105131
err = isal_inflate(&(self->state));
106132
Py_END_ALLOW_THREADS;
107133

108-
if (err != ISAL_DECOMP_OK){
109-
isal_inflate_error(err);
110-
goto error;
134+
switch(err) {
135+
case ISAL_DECOMP_OK:
136+
break;
137+
case ISAL_NEED_DICT:
138+
if (self->zdict != NULL) {
139+
if (set_inflate_zdict_IgzipDecompressor(self) < 0) {
140+
goto error;
141+
}
142+
else
143+
break;
144+
}
145+
else {
146+
isal_inflate_error(err);
147+
goto error;
148+
}
149+
default:
150+
isal_inflate_error(err);
151+
goto error;
111152
}
112-
} while (self->state.avail_out == 0 &&
113-
self->state.block_state != ISAL_BLOCK_FINISH);
153+
154+
} while (self->state.avail_out == 0 || err == ISAL_NEED_DICT);
155+
114156
} while(self->avail_in_real != 0 &&
115157
self->state.block_state != ISAL_BLOCK_FINISH);
116158

@@ -414,7 +456,6 @@ igzip_lib_IgzipDecompressor__new__(PyTypeObject *type,
414456
return NULL;
415457
}
416458
IgzipDecompressor *self = PyObject_New(IgzipDecompressor, type);
417-
int err;
418459
self->eof = 0;
419460
self->needs_input = 1;
420461
self->avail_in_real = 0;
@@ -436,26 +477,10 @@ igzip_lib_IgzipDecompressor__new__(PyTypeObject *type,
436477
self->state.hist_bits = hist_bits;
437478
self->state.crc_flag = flag;
438479
if (self->zdict != NULL){
439-
Py_buffer zdict_buf;
440-
if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
441-
Py_CLEAR(self);
442-
return NULL;
443-
}
444-
if ((size_t)zdict_buf.len > UINT32_MAX) {
445-
PyErr_SetString(PyExc_OverflowError,
446-
"zdict length does not fit in an unsigned 32-bits int");
447-
PyBuffer_Release(&zdict_buf);
480+
if (set_inflate_zdict_IgzipDecompressor(self) < 0) {
448481
Py_CLEAR(self);
449482
return NULL;
450483
}
451-
err = isal_inflate_set_dict(&(self->state), zdict_buf.buf,
452-
(uint32_t)zdict_buf.len);
453-
PyBuffer_Release(&zdict_buf);
454-
if (err != ISAL_DECOMP_OK) {
455-
isal_inflate_error(err);
456-
Py_CLEAR(self);
457-
return NULL;
458-
}
459484
}
460485
return (PyObject *)self;
461486
}

src/isal/isal_zlibmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ isal_zlib_Decompress_decompress_impl(decompobject *self, Py_buffer *data,
724724
Py_BEGIN_ALLOW_THREADS
725725
err = isal_inflate(&self->zst);
726726
Py_END_ALLOW_THREADS
727+
727728
switch(err) {
728729
case ISAL_DECOMP_OK:
729730
break;

0 commit comments

Comments
 (0)