forked from dearlulu/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_archiver.cpp
374 lines (317 loc) · 8.75 KB
/
lua_archiver.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
** repository: https://github.com/trumanzhao/luna
** trumanzhao, 2016/10/19, [email protected]
*/
#include <assert.h>
#include <string.h>
#include <algorithm>
#include "lua/lua.hpp"
#include "lz4/lz4.h"
#include "lua_archiver.h"
#include "var_int.h"
enum class ar_type
{
nill,
number,
integer,
string,
string_idx,
bool_true,
bool_false,
table_head,
table_tail,
count
};
static const int small_int_max = UCHAR_MAX - (int)ar_type::count;
static const int max_share_string = UCHAR_MAX;
static const int max_table_depth = 16;
static int normal_index(lua_State* L, int idx) { return idx >= 0 ? idx : lua_gettop(L) + idx + 1; }
lua_archiver::lua_archiver(size_t size)
{
m_buffer_size = size;
m_lz_threshold = size;
m_ar_buffer = new unsigned char[m_buffer_size];
m_lz_buffer = new unsigned char[m_buffer_size];
}
lua_archiver::~lua_archiver()
{
delete[] m_ar_buffer;
delete[] m_lz_buffer;
}
void lua_archiver::set_buffer_size(size_t size)
{
if (size > 0)
{
delete[] m_ar_buffer;
m_ar_buffer = new unsigned char[size];
delete[] m_lz_buffer;
m_lz_buffer = new unsigned char[size];
m_buffer_size = size;
}
}
void* lua_archiver::save(size_t* data_len, lua_State* L, int first, int last)
{
*m_ar_buffer = 'x';
m_begin = m_ar_buffer;
m_end = m_ar_buffer + m_buffer_size;
m_pos = m_begin + 1;
m_table_depth = 0;
m_shared_string.clear();
m_shared_strlen.clear();
for (int i = first; i <= last; i++)
{
if (!save_value(L, i))
return nullptr;
}
*data_len = (size_t)(m_pos - m_begin);
if (*data_len >= m_lz_threshold && m_buffer_size < 1 + LZ4_COMPRESSBOUND(*data_len))
{
*m_lz_buffer = 'z';
int len = LZ4_compress_default((const char*)m_begin + 1, (char*)m_lz_buffer + 1, (int)*data_len, (int)m_buffer_size - 1);
if (len <= 0)
return nullptr;
*data_len = 1 + len;
return m_lz_buffer;
}
return m_begin;
}
bool lua_archiver::load(int* param_count, lua_State* L, void* data, size_t data_len)
{
if (data_len == 0)
return false;
m_pos = (unsigned char*)data;
m_end = (unsigned char*)data + data_len;
if (*m_pos == 'z')
{
m_pos++;
int len = LZ4_decompress_safe((const char*)m_pos, (char*)m_lz_buffer, (int)data_len - 1, (int)m_buffer_size);
if (len <= 0)
return false;
m_pos = m_lz_buffer;
m_end = m_lz_buffer + len;
}
else
{
if (*m_pos != 'x')
return false;
m_pos++;
}
m_shared_string.clear();
m_shared_strlen.clear();
int count = 0;
int top = lua_gettop(L);
while (m_pos < m_end)
{
if (!load_value(L))
{
lua_settop(L, top);
return false;
}
count++;
}
*param_count = count;
return true;
}
bool lua_archiver::save_value(lua_State* L, int idx)
{
int type = lua_type(L, idx);
switch (type)
{
case LUA_TNIL:
return save_nill();
case LUA_TNUMBER:
return lua_isinteger(L, idx) ? save_integer(lua_tointeger(L, idx)) : save_number(lua_tonumber(L, idx));
case LUA_TBOOLEAN:
return save_bool(!!lua_toboolean(L, idx));
case LUA_TSTRING:
return save_string(L, idx);
case LUA_TTABLE:
return save_table(L, idx);
default:
break;
}
return false;
}
bool lua_archiver::save_number(double v)
{
if (m_end - m_pos < sizeof(unsigned char) + sizeof(double))
return false;
*m_pos++ = (unsigned char)ar_type::number;
memcpy(m_pos, &v, sizeof(double));
m_pos += sizeof(double);
return true;
}
bool lua_archiver::save_integer(int64_t v)
{
if (v >= 0 && v <= small_int_max)
{
if (m_end - m_pos < sizeof(unsigned char))
return false;
*m_pos++ = (unsigned char)(v + (int)ar_type::count);
return true;
}
if (v > small_int_max)
{
v -= small_int_max;
}
if (m_end - m_pos < sizeof(unsigned char))
return false;
*m_pos++ = (unsigned char)ar_type::integer;
size_t len = encode_s64(m_pos, (size_t)(m_end - m_pos), v);
m_pos += len;
return len > 0;
}
bool lua_archiver::save_bool(bool v)
{
if (m_end - m_pos < sizeof(unsigned char))
return false;
*m_pos++ = (unsigned char)(v ? ar_type::bool_true : ar_type::bool_false);
return true;
}
bool lua_archiver::save_nill()
{
if (m_end - m_pos < sizeof(unsigned char))
return false;
*m_pos++ = (unsigned char)ar_type::nill;
return true;
}
bool lua_archiver::save_table(lua_State* L, int idx)
{
if (m_end - m_pos < (ptrdiff_t)sizeof(unsigned char))
return false;
if (++m_table_depth > max_table_depth)
return false;
*m_pos++ = (unsigned char)ar_type::table_head;
idx = normal_index(L, idx);
lua_pushnil(L);
while (lua_next(L, idx))
{
if (!save_value(L, -2) || !save_value(L, -1))
return false;
lua_pop(L, 1);
}
--m_table_depth;
if (m_end - m_pos < (ptrdiff_t)sizeof(unsigned char))
return false;
*m_pos++ = (unsigned char)ar_type::table_tail;
return true;
}
bool lua_archiver::save_string(lua_State* L, int idx)
{
size_t len = 0, encode_len = 0;
const char* str = lua_tolstring(L, idx, &len);
int shared = find_shared_str(str);
if (shared >= 0)
{
if (m_end - m_pos < sizeof(unsigned char))
return false;
*m_pos++ = (unsigned char)ar_type::string_idx;
encode_len = encode_u64(m_pos, (size_t)(m_end - m_pos), shared);
m_pos += encode_len;
return encode_len > 0;
}
if (m_end - m_pos < sizeof(unsigned char))
return false;
*m_pos++ = (unsigned char)ar_type::string;
encode_len = encode_u64(m_pos, (size_t)(m_end - m_pos), len);
if (encode_len == 0)
return false;
m_pos += encode_len;
if (m_end - m_pos < (int)len)
return false;
memcpy(m_pos, str, len);
m_pos += len;
if (m_shared_string.size() < max_share_string)
{
m_shared_string.push_back(str);
}
return true;
}
int lua_archiver::find_shared_str(const char* str)
{
auto it = std::find(m_shared_string.begin(), m_shared_string.end(), str);
if (it != m_shared_string.end())
return (int)(it - m_shared_string.begin());
return -1;
}
bool lua_archiver::load_value(lua_State* L)
{
if (m_end - m_pos < (ptrdiff_t)sizeof(unsigned char))
return false;
int code = *m_pos++;
if (code >= (int)ar_type::count)
{
lua_pushinteger(L, code - (int)ar_type::count);
return true;
}
double number = 0;
int64_t integer = 0;
size_t decode_len = 0;
uint64_t str_len = 0, str_idx = 0;
switch ((ar_type)code)
{
case ar_type::nill:
lua_pushnil(L);
break;
case ar_type::number:
if (m_end - m_pos < (ptrdiff_t)sizeof(double))
return false;
memcpy(&number, m_pos, sizeof(double));
m_pos += sizeof(double);
lua_pushnumber(L, number);
break;
case ar_type::integer:
decode_len = decode_s64(&integer, m_pos, (size_t)(m_end - m_pos));
if (decode_len == 0)
return false;
m_pos += decode_len;
if (integer >= 0)
{
integer += small_int_max;
}
lua_pushinteger(L, integer);
break;
case ar_type::bool_true:
lua_pushboolean(L, true);
break;
case ar_type::bool_false:
lua_pushboolean(L, false);
break;
case ar_type::string:
decode_len = decode_u64(&str_len, m_pos, (size_t)(m_end - m_pos));
if (decode_len == 0)
return false;
m_pos += decode_len;
if (m_end - m_pos < (ptrdiff_t)str_len)
return false;
m_shared_string.push_back((char*)m_pos);
m_shared_strlen.push_back((size_t)str_len);
lua_pushlstring(L, (char*)m_pos, (size_t)str_len);
m_pos += str_len;
break;
case ar_type::string_idx:
decode_len = decode_u64(&str_idx, m_pos, (size_t)(m_end - m_pos));
if (decode_len == 0 || str_idx >= m_shared_string.size())
return false;
m_pos += decode_len;
lua_pushlstring(L, m_shared_string[(int)str_idx], m_shared_strlen[(int)str_idx]);
break;
case ar_type::table_head:
lua_newtable(L);
while (m_pos < m_end)
{
if (*m_pos == (unsigned char)ar_type::table_tail)
{
m_pos++;
return true;
}
if (!load_value(L) || !load_value(L))
return false;
lua_settable(L, -3);
}
return false;
default:
return false;
}
return true;
}