166
166
c_avail_output = output_buf_size
167
167
output_buf = PyString_FromStringAndSize(NULL, c_avail_output)
168
168
c_output_buffer = <uint8_t *>PyString_AS_STRING(output_buf)
170
173
retcode = xd3_encode_memory(c_input, c_input_size,
171
174
c_source, c_source_size,
172
175
c_output_buffer, &c_output_size,
180
183
return output_buf[:c_output_size]
183
# def decode_memory(val):
184
# int xd3_encode_memory (uint8_t *input,
185
# usize_t input_size,
187
# usize_t source_size,
188
# uint8_t *output_buffer,
189
# usize_t *output_size,
190
# usize_t avail_output,
193
# int xd3_decode_memory (uint8_t *input,
194
# usize_t input_size,
196
# usize_t source_size,
197
# uint8_t *output_buf,
198
# usize_t *output_size,
199
# usize_t avail_output,
186
def decode_memory(input, output_size, source=None, flags=None):
187
"""Decode the encoded bytes into their expanded form.
189
:param input: The encoded bytes (from something like encode_memory).
190
:param output_size: The size of the buffer to allocate.
191
:param source: If the bytes were encoded using source, you must supply that
193
:param flags: Decompression flags, if not supplied, reasonable values will
196
cdef uint8_t *c_input
197
cdef usize_t c_input_size
198
cdef uint8_t *c_source
199
cdef usize_t c_source_size
200
cdef uint8_t *c_output_buffer
201
cdef usize_t c_output_size
202
cdef usize_t c_avail_output
206
if not PyString_CheckExact(input):
207
raise TypeError('input must be a string buffer')
208
c_input = <uint8_t *>PyString_AS_STRING(input)
209
c_input_size = PyString_GET_SIZE(input)
214
if not PyString_CheckExact(source):
215
raise TypeError('source input must be a string buffer')
216
c_source = <uint8_t *>PyString_AS_STRING(source)
217
c_source_size = PyString_GET_SIZE(source)
219
c_avail_output = output_size
220
output_buf = PyString_FromStringAndSize(NULL, c_avail_output)
221
c_output_buffer = <uint8_t *>PyString_AS_STRING(output_buf)
226
retcode = xd3_decode_memory(c_input, c_input_size,
227
c_source, c_source_size,
228
c_output_buffer, &c_output_size,
229
c_avail_output, c_flags)
232
raise Exception('foobar: %s' % (retcode,))
233
# Now we want to resize the output buffer
234
# _PyString_Resize() might be the function to use, but it is a bit risky,
235
# and hard to use from pyrex, so instead, just slice it
236
return output_buf[:c_output_size]