|
29 | 29 | #include "lua.h" |
30 | 30 | #include "watchdog.h" |
31 | 31 |
|
32 | | -/* |
33 | | - * Test steps: |
34 | | - * |
35 | | - * 1. Create a test file with some text |
36 | | - * |
37 | | - * 2. Compress the file |
38 | | - * |
39 | | - * lz4 -9 -B32 test.txt test.lz4 |
40 | | - * |
41 | | - * 3. Convert to header data |
42 | | - * |
43 | | - * xxd -include test.lz4 test.h |
44 | | - * |
45 | | - * 4. Copy the header data to the test buffer below |
46 | | - */ |
47 | | - |
48 | | -const unsigned char test_data[] = { |
49 | | - 0x04, 0x22, 0x4d, 0x18, 0x64, 0x40, 0xa7, 0x24, 0x02, 0x00, 0x00, 0xf0, |
50 | | - 0x30, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, |
51 | | - 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x66, 0x69, |
52 | | - 0x6c, 0x65, 0x20, 0x74, 0x68, 0x61, 0x74, 0x20, 0x63, 0x6f, 0x6e, 0x74, |
53 | | - 0x61, 0x69, 0x6e, 0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, |
54 | | - 0x78, 0x74, 0x2e, 0x0a, 0x0a, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x49, |
55 | | - 0x70, 0x73, 0x75, 0x6d, 0x35, 0x00, 0xc1, 0x73, 0x69, 0x6d, 0x70, 0x6c, |
56 | | - 0x79, 0x20, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x23, 0x00, 0xf1, 0x0d, 0x20, |
57 | | - 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, |
58 | | - 0x69, 0x6e, 0x67, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x79, 0x70, 0x65, |
59 | | - 0x73, 0x65, 0x74, 0x10, 0x00, 0xa8, 0x69, 0x6e, 0x64, 0x75, 0x73, 0x74, |
60 | | - 0x72, 0x79, 0x2e, 0x20, 0x4b, 0x00, 0x81, 0x68, 0x61, 0x73, 0x20, 0x62, |
61 | | - 0x65, 0x65, 0x6e, 0x3c, 0x00, 0x04, 0x23, 0x00, 0xb8, 0x27, 0x73, 0x20, |
62 | | - 0x73, 0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x62, 0x00, 0xa1, 0x65, |
63 | | - 0x76, 0x65, 0x72, 0x20, 0x73, 0x69, 0x6e, 0x63, 0x65, 0x2e, 0x00, 0xf2, |
64 | | - 0x07, 0x31, 0x35, 0x30, 0x30, 0x73, 0x2c, 0x20, 0x77, 0x68, 0x65, 0x6e, |
65 | | - 0x20, 0x61, 0x6e, 0x20, 0x75, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x81, |
66 | | - 0x00, 0xf1, 0x01, 0x65, 0x72, 0x20, 0x74, 0x6f, 0x6f, 0x6b, 0x20, 0x61, |
67 | | - 0x20, 0x67, 0x61, 0x6c, 0x6c, 0x65, 0x79, 0x9e, 0x00, 0x31, 0x79, 0x70, |
68 | | - 0x65, 0x96, 0x00, 0xf2, 0x07, 0x73, 0x63, 0x72, 0x61, 0x6d, 0x62, 0x6c, |
69 | | - 0x65, 0x64, 0x20, 0x69, 0x74, 0x20, 0x74, 0x6f, 0x20, 0x6d, 0x61, 0x6b, |
70 | | - 0x65, 0x20, 0x61, 0x20, 0x00, 0xf1, 0x02, 0x73, 0x70, 0x65, 0x63, 0x69, |
71 | | - 0x6d, 0x65, 0x6e, 0x20, 0x62, 0x6f, 0x6f, 0x6b, 0x2e, 0x20, 0x49, 0x74, |
72 | | - 0xa2, 0x00, 0xf1, 0x1b, 0x73, 0x75, 0x72, 0x76, 0x69, 0x76, 0x65, 0x64, |
73 | | - 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79, 0x20, 0x66, 0x69, |
74 | | - 0x76, 0x65, 0x20, 0x63, 0x65, 0x6e, 0x74, 0x75, 0x72, 0x69, 0x65, 0x73, |
75 | | - 0x2c, 0x20, 0x62, 0x75, 0x74, 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x9a, 0x00, |
76 | | - 0xf8, 0x05, 0x6c, 0x65, 0x61, 0x70, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20, |
77 | | - 0x65, 0x6c, 0x65, 0x63, 0x74, 0x72, 0x6f, 0x6e, 0x69, 0x63, 0x0c, 0x01, |
78 | | - 0x80, 0x2c, 0x20, 0x72, 0x65, 0x6d, 0x61, 0x69, 0x6e, 0x17, 0x01, 0xf1, |
79 | | - 0x06, 0x65, 0x73, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x6c, 0x79, |
80 | | - 0x20, 0x75, 0x6e, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x64, 0x79, 0x00, |
81 | | - 0xd0, 0x77, 0x61, 0x73, 0x20, 0x70, 0x6f, 0x70, 0x75, 0x6c, 0x61, 0x72, |
82 | | - 0x69, 0x73, 0xad, 0x00, 0x02, 0x25, 0x01, 0xa1, 0x31, 0x39, 0x36, 0x30, |
83 | | - 0x73, 0x20, 0x77, 0x69, 0x74, 0x68, 0x0f, 0x00, 0x70, 0x72, 0x65, 0x6c, |
84 | | - 0x65, 0x61, 0x73, 0x65, 0xe1, 0x00, 0xf4, 0x00, 0x4c, 0x65, 0x74, 0x72, |
85 | | - 0x61, 0x73, 0x65, 0x74, 0x20, 0x73, 0x68, 0x65, 0x65, 0x74, 0x73, 0xc8, |
86 | | - 0x01, 0x00, 0x66, 0x00, 0x08, 0x73, 0x01, 0x60, 0x70, 0x61, 0x73, 0x73, |
87 | | - 0x61, 0x67, 0xb5, 0x00, 0x00, 0x0d, 0x01, 0x30, 0x6d, 0x6f, 0x72, 0x45, |
88 | | - 0x00, 0x00, 0xcb, 0x00, 0x22, 0x6c, 0x79, 0x57, 0x00, 0xf0, 0x00, 0x64, |
89 | | - 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x20, 0x70, 0x75, 0x62, 0x6c, 0x69, |
90 | | - 0x73, 0x68, 0x40, 0x00, 0xf0, 0x15, 0x73, 0x6f, 0x66, 0x74, 0x77, 0x61, |
91 | | - 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x41, 0x6c, 0x64, 0x75, |
92 | | - 0x73, 0x20, 0x50, 0x61, 0x67, 0x65, 0x4d, 0x61, 0x6b, 0x65, 0x72, 0x20, |
93 | | - 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x28, 0x00, 0x81, 0x76, 0x65, 0x72, |
94 | | - 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x8f, 0x00, 0xb0, 0x6f, 0x72, 0x65, 0x6d, |
95 | | - 0x20, 0x49, 0x70, 0x73, 0x75, 0x6d, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x31, |
96 | | - 0xb8, 0x0d, 0xa8}; |
97 | | - |
98 | 32 | static int registered_function = 0; |
99 | 33 | static uint8_t decompression_buffer[4096]; |
100 | 34 | static size_t decompression_buffer_size = 0; |
@@ -125,8 +59,6 @@ static void process_function_callback(void *context, |
125 | 59 | decompression_buffer_size = data_size; |
126 | 60 | memcpy(decompression_buffer, data, data_size); |
127 | 61 |
|
128 | | - LOG("decompression_buffer_size: %d", decompression_buffer_size); |
129 | | - |
130 | 62 | lua_sethook(L_global, |
131 | 63 | decompression_lua_handler, |
132 | 64 | LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT, |
@@ -154,22 +86,24 @@ static int lua_compression_register_process_function(lua_State *L) |
154 | 86 |
|
155 | 87 | static int lua_compression_decompress(lua_State *L) |
156 | 88 | { |
157 | | - lua_Integer block_size = luaL_checkinteger(L, 1); |
| 89 | + size_t length; |
| 90 | + const char *data = luaL_checklstring(L, 1, &length); |
| 91 | + |
| 92 | + lua_Integer block_size = luaL_checkinteger(L, 2); |
158 | 93 |
|
159 | 94 | if (block_size <= 0) |
160 | 95 | { |
161 | 96 | luaL_error(L, "bytes must be greater than 0"); |
162 | 97 | } |
163 | 98 |
|
164 | 99 | int status = compression_decompress(block_size, |
165 | | - test_data, // TODO pass the source data to the decompression function |
166 | | - sizeof(test_data), // TODO set the source data size |
| 100 | + data, |
| 101 | + length, |
167 | 102 | process_function_callback, |
168 | 103 | NULL); |
169 | 104 |
|
170 | 105 | if (status) |
171 | 106 | { |
172 | | - LOG("%d", status); |
173 | 107 | luaL_error(L, "decompression failed"); |
174 | 108 | } |
175 | 109 |
|
|
0 commit comments