-
Notifications
You must be signed in to change notification settings - Fork 0
/
modufastlz.c
132 lines (107 loc) · 4.36 KB
/
modufastlz.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Damiano Mazzella
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#if defined(MICROPY_PY_UFASTLZ)
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "py/objstr.h"
#include "py/objint.h"
#include "py/runtime.h"
#include "fastlz.h"
STATIC mp_obj_t ufastlz_compress(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args)
{
enum
{
ARG_level,
};
static const mp_arg_t allowed_args[] = {
{MP_QSTR_level, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1}},
};
mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
mp_buffer_info_t bufinfo_data;
mp_get_buffer_raise(args[0], &bufinfo_data, MP_BUFFER_READ);
mp_int_t level = vals[ARG_level].u_int;
if (level != 1 && level != 2)
{
mp_raise_ValueError(MP_ERROR_TEXT("level must be either 1 or 2"));
}
if (bufinfo_data.len < 16)
{
mp_raise_ValueError(MP_ERROR_TEXT("input must be >= 16 bytes len"));
}
vstr_t vstr_out;
vstr_init(&vstr_out, MAX((mp_int_t)(1.05 * bufinfo_data.len), 66));
if (!(vstr_out.len = fastlz_compress_level(level, bufinfo_data.buf, bufinfo_data.len, vstr_out.buf)))
{
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("compress: %d"), vstr_out.len);
}
mp_obj_t out = mp_obj_new_bytes((const byte *)vstr_out.buf, vstr_out.len);
vstr_clear(&vstr_out);
return out;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(fastlz_compress_obj, 1, ufastlz_compress);
STATIC mp_obj_t ufastlz_decompress(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args)
{
enum
{
ARG_maxout
};
static const mp_arg_t allowed_args[] = {
{MP_QSTR_maxout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1024}},
};
mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
mp_buffer_info_t bufinfo_data;
mp_get_buffer_raise(args[0], &bufinfo_data, MP_BUFFER_READ);
mp_int_t maxout = vals[ARG_maxout].u_int;
vstr_t vstr_out;
vstr_init(&vstr_out, maxout);
if (!(vstr_out.len = fastlz_decompress(bufinfo_data.buf, bufinfo_data.len, vstr_out.buf, maxout)))
{
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("decompress: %d"), vstr_out.len);
}
mp_obj_t out = mp_obj_new_bytes((const byte *)vstr_out.buf, vstr_out.len);
vstr_clear(&vstr_out);
return out;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(fastlz_decompress_obj, 1, ufastlz_decompress);
STATIC const mp_rom_map_elem_t mp_module_ufastlz_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__fastlz)},
{MP_ROM_QSTR(MP_QSTR_compress), MP_ROM_PTR(&fastlz_compress_obj)},
{MP_ROM_QSTR(MP_QSTR_decompress), MP_ROM_PTR(&fastlz_decompress_obj)},
};
STATIC MP_DEFINE_CONST_DICT(mp_module_ufastlz_globals, mp_module_ufastlz_globals_table);
const mp_obj_module_t mp_module_ufastlz = {
.base = {&mp_type_module},
.globals = (mp_obj_dict_t *)&mp_module_ufastlz_globals,
};
// Register the module to make it available in Python
MP_REGISTER_MODULE(MP_QSTR__fastlz, mp_module_ufastlz);
#endif // MICROPY_PY_UFASTLZ