-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrap.c
291 lines (227 loc) · 7.35 KB
/
wrap.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
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
#include <stddef.h>
#include <stdint.h>
#include "lua.h"
#include "lauxlib.h"
#define VERSION "1.00"
#if LUA_VERSION_NUM < 502
/* -----copied from bit32.c: ----- */
/* ----- adapted from lua-5.2.0 luaconf.h: ----- */
/*
@@ LUA_UNSIGNED is the integral type used by lua_pushunsigned/lua_tounsigned.
** It must have at least 32 bits.
*/
#define LUA_UNSIGNED unsigned LUAI_INT32
#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */
/* On a Microsoft compiler on a Pentium, use assembler to avoid clashes
with a DirectX idiosyncrasy */
#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */
#define MS_ASMTRICK
#else /* }{ */
/* the next definition uses a trick that should work on any machine
using IEEE754 with a 32-bit integer type */
#define LUA_IEEE754TRICK
/*
@@ LUA_IEEEENDIAN is the endianness of doubles in your machine
** (0 for little endian, 1 for big endian); if not defined, Lua will
** check it dynamically.
*/
/* check for known architectures */
#if defined(__i386__) || defined(__i386) || defined(__X86__) || \
defined (__x86_64)
#define LUA_IEEEENDIAN 0
#elif defined(__POWERPC__) || defined(__ppc__)
#define LUA_IEEEENDIAN 1
#endif
#endif /* } */
#endif /* } */
/* ----- from lua-5.2.0 lua.h: ----- */
/* unsigned integer type */
typedef LUA_UNSIGNED lua_Unsigned;
/* ----- adapted from lua-5.2.0 llimits.h: ----- */
/* lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
*/
#if defined(MS_ASMTRICK) /* { */
/* trick with Microsoft assembler for X86 */
#define lua_number2unsigned(i,n) \
{__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;}
#elif defined(LUA_IEEE754TRICK) /* }{ */
/* the next trick should work on any machine using IEEE754 with
a 32-bit integer type */
union luai_Cast2 { double l_d; LUAI_INT32 l_p[2]; };
#if !defined(LUA_IEEEENDIAN) /* { */
#define LUAI_EXTRAIEEE \
static const union luai_Cast2 ieeeendian = {-(33.0 + 6755399441055744.0)};
#define LUA_IEEEENDIAN (ieeeendian.l_p[1] == 33)
#else
#define LUAI_EXTRAIEEE /* empty */
#endif /* } */
#define lua_number2int32(i,n,t) \
{ LUAI_EXTRAIEEE \
volatile union luai_Cast2 u; u.l_d = (n) + 6755399441055744.0; \
(i) = (t)u.l_p[LUA_IEEEENDIAN]; }
#define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
#endif /* } */
#endif
#if LUA_VERSION_NUM != 502
#if !defined(lua_number2unsigned) /* { */
/* the following definition assures proper modulo behavior */
#if defined(LUA_NUMBER_DOUBLE)
#include <math.h>
#define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1)
#define lua_number2unsigned(i,n) \
((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED))
#else
#define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
#endif
#endif /* } */
/* on several machines, coercion from unsigned to double is slow,
so it may be worth to avoid */
#define lua_unsigned2number(u) \
(((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
/* ----- adapted from lua-5.2.0 lapi.c: ----- */
static void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
lua_Number n;
n = lua_unsigned2number(u);
lua_pushnumber(L, n);
}
/* ----- adapted from lua-5.2.0-work3 lbitlib.c getuintarg(): ----- */
static lua_Unsigned luaL_checkunsigned (lua_State *L, int arg) {
lua_Unsigned r;
lua_Number x = lua_tonumber(L, arg);
if (x == 0) luaL_checktype(L, arg, LUA_TNUMBER);
lua_number2unsigned(r, x);
return r;
}
/* -----End of copied from bit32.c ----- */
#endif
#include "crc32.h"
static const char *crc32_reg_name = "ehj.crc32";
static void create_meta(lua_State *L, const char *name, const luaL_Reg *lib) {
luaL_newmetatable(L, name);
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* push metatable */
lua_rawset(L, -3); /* metatable.__index = metatable */
/* register metatable functions */
#if LUA_VERSION_NUM < 502
luaL_register(L, NULL, lib);
#else
luaL_setfuncs(L, lib, 0);
#endif
/* remove metatable from stack */
lua_pop(L, 1);
}
static int Lcrc_reset(lua_State *L) {
uint32_t *_crc32 = (uint32_t*) luaL_checkudata(L, 1, crc32_reg_name);
*_crc32 = 0;
lua_settop(L, 1); // return userdata itself
return 1;
}
static int Lcrc_update(lua_State *L) {
uint32_t *_crc32 = (uint32_t*) luaL_checkudata(L, 1, crc32_reg_name);
const uint8_t *data;
size_t data_len;
luaL_checktype(L, 2, LUA_TSTRING);
data = lua_tolstring (L, 2, &data_len);
*_crc32 = crc32(*_crc32, data, data_len );
lua_settop(L, 1); // return userdata itself
return 1;
}
static int Lcrc_tonumber(lua_State *L) {
uint32_t *_crc32 = (uint32_t*) luaL_checkudata(L, 1, crc32_reg_name);
lua_pushunsigned(L, *_crc32);
return 1;
}
static int Lcrc_tostring(lua_State *L) {
uint32_t *_crc32 = (uint32_t*) luaL_checkudata(L, 1, crc32_reg_name);
uint32_t crc_out = *_crc32;
uint8_t crcout_str[4];
int i;
for (i = 3; i>=0; i--) {
crcout_str[i] = crc_out & 0xff;
crc_out>>=8;
}
lua_pushlstring (L, crcout_str, 4);
return 1;
}
static int Lcrc_tohex(lua_State *L) {
uint32_t *_crc32 = (uint32_t*) luaL_checkudata(L, 1, crc32_reg_name);
uint8_t crcout_str[9];
sprintf(crcout_str, "%8.8x", *_crc32);
lua_pushlstring (L, crcout_str, 8);
return 1;
}
static const luaL_Reg crc32_meta_reg[] = {
{"reset", Lcrc_reset },
{"update", Lcrc_update },
{"tonumber", Lcrc_tonumber },
{"tostring", Lcrc_tostring },
{"tohex", Lcrc_tohex },
{NULL, NULL}
};
static int Lcrc32(lua_State *L) {
uint32_t crc_in, crc_out;
const uint8_t *data;
size_t data_len;
luaL_checktype(L, 2, LUA_TSTRING);
data = lua_tolstring (L, 2, &data_len);
if (lua_type(L,1) == LUA_TSTRING) {
// input CRC as 4 byte string
size_t crc_len;
int i;
const uint8_t *crcin_str = lua_tolstring(L,1, &crc_len);
if (crc_len>4) crc_len = 4;
crc_in = 0;
for (i = 0; i < crc_len; i++) {
crc_in<<=8;
crc_in |= crcin_str[i];
}
crc_out = crc32(crc_in, data, data_len );
// return CRC as 4 byte string
uint8_t crcout_str[4];
for (i = 3; i>=0; i--) {
crcout_str[i] = crc_out & 0xff;
crc_out>>=8;
}
lua_pushlstring (L, crcout_str, 4);
} else {
crc_in = luaL_checkunsigned(L, 1);
crc_out = crc32(crc_in, data, data_len );
lua_pushunsigned(L, crc_out);
}
return 1;
}
static int Lnewcrc32(lua_State *L) {
uint32_t *crc32 = (uint32_t *)lua_newuserdata(L, sizeof(uint32_t));
if (luaL_newmetatable(L, crc32_reg_name)) {
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* push metatable */
lua_rawset(L, -3); /* metatable.__index = metatable */
/* register metatable functions */
#if LUA_VERSION_NUM < 502
luaL_register(L, NULL, crc32_meta_reg);
#else
luaL_setfuncs(L, crc32_meta_reg, 0);
#endif
}
lua_setmetatable(L, -2);
*crc32 = 0;
return 1;
}
static const luaL_Reg Rcrc32[] = {
{"crc32", Lcrc32 },
{"newcrc32", Lnewcrc32 },
{NULL, NULL}
};
LUALIB_API int luaopen_crc32(lua_State *L) {
create_meta(L,crc32_reg_name, crc32_meta_reg);
#if LUA_VERSION_NUM < 502
lua_newtable (L);
luaL_register(L, NULL, Rcrc32);
#else
luaL_newlib(L, Rcrc32);
#endif
lua_pushliteral(L, VERSION);
lua_setfield(L, -2, "version");
return 1;
}