~jameinel/+junk/xdelta3-pyrex

« back to all changes in this revision

Viewing changes to xd3.pyx

  • Committer: John Arbash Meinel
  • Date: 2009-02-20 18:20:05 UTC
  • Revision ID: john@arbash-meinel.com-20090220182005-3e9skz4gx1zepyq7
Hook up the .decode() functionality.

The current wrappers are now capable of encoding and decoding.
This work does give us a future capability to do incremental
encoding/decoding if we so desire.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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)
169
 
    c_flags = 0
 
169
    if flags is None:
 
170
        c_flags = 0
 
171
    else:
 
172
        c_flags = flags
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]
181
184
 
182
185
 
183
 
# def decode_memory(val):
184
 
#     int xd3_encode_memory (uint8_t *input,
185
 
#                            usize_t input_size,
186
 
#                            uint8_t *source,
187
 
#                            usize_t source_size,
188
 
#                            uint8_t *output_buffer,
189
 
#                            usize_t *output_size,
190
 
#                            usize_t avail_output,
191
 
#                            int     flags)
192
 
193
 
#     int xd3_decode_memory (uint8_t *input,
194
 
#                            usize_t input_size,
195
 
#                            uint8_t *source,
196
 
#                            usize_t source_size,
197
 
#                            uint8_t *output_buf,
198
 
#                            usize_t *output_size,
199
 
#                            usize_t avail_output,
200
 
#                            int     flags)
 
186
def decode_memory(input, output_size, source=None, flags=None):
 
187
    """Decode the encoded bytes into their expanded form.
 
188
 
 
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
 
192
        string here.
 
193
    :param flags: Decompression flags, if not supplied, reasonable values will
 
194
        be assumed.
 
195
    """
 
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
 
203
    cdef int     c_flags
 
204
    cdef int retcode
 
205
 
 
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)
 
210
    if source is None:
 
211
        c_source = NULL
 
212
        c_source_size = 0
 
213
    else:
 
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)
 
218
 
 
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)
 
222
    if flags is None:
 
223
        c_flags = 0
 
224
    else:
 
225
        c_flags = flags
 
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)
 
230
 
 
231
    if retcode != 0:
 
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]