25
25
*/
26
26
27
27
#include <stdio.h>
28
+ #include <string.h>
28
29
29
30
#include "py/nlr.h"
30
31
#include "py/runtime.h"
39
40
#define DEBUG_printf (...) (void)0
40
41
#endif
41
42
42
- STATIC int mod_uzlib_grow_buf (TINF_DATA * d , unsigned alloc_req ) {
43
- if (alloc_req < 256 ) {
44
- alloc_req = 256 ;
45
- }
46
- DEBUG_printf ("uzlib: Resizing buffer to " UINT_FMT " bytes\n" , d -> destSize + alloc_req );
47
- d -> destStart = m_renew (byte , d -> destStart , d -> destSize , d -> destSize + alloc_req );
48
- d -> destSize += alloc_req ;
49
- return 0 ;
50
- }
51
-
52
43
STATIC mp_obj_t mod_uzlib_decompress (size_t n_args , const mp_obj_t * args ) {
53
44
(void )n_args ;
54
45
mp_obj_t data = args [0 ];
55
46
mp_buffer_info_t bufinfo ;
56
47
mp_get_buffer_raise (data , & bufinfo , MP_BUFFER_READ );
57
48
58
49
TINF_DATA * decomp = m_new_obj (TINF_DATA );
50
+ memset (decomp , 0 , sizeof (* decomp ));
59
51
DEBUG_printf ("sizeof(TINF_DATA)=" UINT_FMT "\n" , sizeof (* decomp ));
52
+ uzlib_uncompress_init (decomp , NULL , 0 );
53
+ mp_uint_t dest_buf_size = (bufinfo .len + 15 ) & ~15 ;
54
+ byte * dest_buf = m_new (byte , dest_buf_size );
60
55
61
- decomp -> destSize = ( bufinfo . len + 15 ) & ~ 15 ;
62
- decomp -> destStart = m_new ( byte , decomp -> destSize ) ;
56
+ decomp -> dest = dest_buf ;
57
+ decomp -> destSize = dest_buf_size ;
63
58
DEBUG_printf ("uzlib: Initial out buffer: " UINT_FMT " bytes\n" , decomp -> destSize );
64
- decomp -> destGrow = mod_uzlib_grow_buf ;
65
59
decomp -> source = bufinfo .buf ;
66
60
67
61
int st ;
62
+ bool is_zlib = true;
63
+
68
64
if (n_args > 1 && MP_OBJ_SMALL_INT_VALUE (args [1 ]) < 0 ) {
69
- st = tinf_uncompress_dyn (decomp );
70
- } else {
71
- st = tinf_zlib_uncompress_dyn (decomp , bufinfo .len );
65
+ is_zlib = false;
72
66
}
73
- if (st != 0 ) {
74
- nlr_raise (mp_obj_new_exception_arg1 (& mp_type_ValueError , MP_OBJ_NEW_SMALL_INT (st )));
67
+
68
+ if (is_zlib ) {
69
+ st = uzlib_zlib_parse_header (decomp );
70
+ if (st < 0 ) {
71
+ goto error ;
72
+ }
73
+ }
74
+
75
+ while (1 ) {
76
+ st = uzlib_uncompress_chksum (decomp );
77
+ if (st < 0 ) {
78
+ goto error ;
79
+ }
80
+ if (st == TINF_DONE ) {
81
+ break ;
82
+ }
83
+ size_t offset = decomp -> dest - dest_buf ;
84
+ dest_buf = m_renew (byte , dest_buf , dest_buf_size , dest_buf_size + 256 );
85
+ dest_buf_size += 256 ;
86
+ decomp -> dest = dest_buf + offset ;
87
+ decomp -> destSize = 256 ;
75
88
}
76
89
77
- mp_uint_t final_sz = decomp -> dest - decomp -> destStart ;
78
- DEBUG_printf ("uzlib: Resizing from " UINT_FMT " to final size: " UINT_FMT " bytes\n" , decomp -> destSize , final_sz );
79
- decomp -> destStart = (byte * )m_renew (byte , decomp -> destStart , decomp -> destSize , final_sz );
80
- mp_obj_t res = mp_obj_new_bytearray_by_ref (final_sz , decomp -> destStart );
90
+ mp_uint_t final_sz = decomp -> dest - dest_buf ;
91
+ DEBUG_printf ("uzlib: Resizing from " UINT_FMT " to final size: " UINT_FMT " bytes\n" , dest_buf_size , final_sz );
92
+ dest_buf = (byte * )m_renew (byte , dest_buf , dest_buf_size , final_sz );
93
+ mp_obj_t res = mp_obj_new_bytearray_by_ref (final_sz , dest_buf );
81
94
m_del_obj (TINF_DATA , decomp );
82
95
return res ;
96
+
97
+ error :
98
+ nlr_raise (mp_obj_new_exception_arg1 (& mp_type_ValueError , MP_OBJ_NEW_SMALL_INT (st )));
83
99
}
84
100
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_uzlib_decompress_obj , 1 , 3 , mod_uzlib_decompress );
85
101
@@ -102,5 +118,6 @@ const mp_obj_module_t mp_module_uzlib = {
102
118
#include "uzlib/tinflate.c"
103
119
#include "uzlib/tinfzlib.c"
104
120
#include "uzlib/adler32.c"
121
+ #include "uzlib/crc32.c"
105
122
106
123
#endif // MICROPY_PY_UZLIB
0 commit comments