2 64-bit Cyclic Redundancy Check
12 a_u64 (*eval
)(a_u64
const table
[0x100], void const *pdata
, a_size nbyte
, a_u64 value
);
16 constructor for 64-bit Cyclic Redundancy Check
17 @tparam integer poly polynomial that is CRC's divisor
18 @tparam boolean reversed whether or not to reverse
19 @treturn a.crc64 64-bit Cyclic Redundancy Check userdata
22 int liba_crc64_new(lua_State
*L
)
27 int top
= lua_gettop(L
);
30 luaL_checktype(L
, 2, LUA_TBOOLEAN
);
31 reversed
= lua_toboolean(L
, 2);
33 poly
= lua_u64_get(L
, 1);
34 ctx
= lua_newclass(L
, struct crc64
);
35 lua_registry_get(L
, liba_crc64_new
);
36 lua_setmetatable(L
, -2);
39 a_crc64l_init(ctx
->table
, poly
);
44 a_crc64m_init(ctx
->table
, poly
);
51 generate for 64-bit Cyclic Redundancy Check
52 @tparam a.crc64 ctx 64-bit Cyclic Redundancy Check userdata
53 @tparam integer poly polynomial that is CRC's divisor
54 @tparam boolean reversed whether or not to reverse
55 @treturn a.crc64 64-bit Cyclic Redundancy Check userdata
58 int liba_crc64_gen(lua_State
*L
)
63 int top
= lua_gettop(L
);
66 luaL_checktype(L
, 3, LUA_TBOOLEAN
);
67 reversed
= lua_toboolean(L
, 3);
69 poly
= lua_u64_get(L
, 2);
70 ctx
= (struct crc64
*)lua_touserdata(L
, 1);
73 a_crc64l_init(ctx
->table
, poly
);
78 a_crc64m_init(ctx
->table
, poly
);
86 calculate for 64-bit Cyclic Redundancy Check
87 @tparam a.crc64 ctx 64-bit Cyclic Redundancy Check userdata
88 @tparam string block block to be processed
89 @tparam integer value initial value
90 @treturn integer output value
93 int liba_crc64_eval(lua_State
*L
)
95 struct crc64
*const ctx
= (struct crc64
*)lua_touserdata(L
, 1);
100 char const *const p
= luaL_checklstring(L
, 2, &n
);
101 if (lua_gettop(L
) > 2) { value
= lua_u64_get(L
, 3); }
102 lua_u64_new(L
, ctx
->eval(ctx
->table
, p
, n
, value
));
109 pack a block and its 64-bit Cyclic Redundancy Check value
110 @tparam a.crc64 ctx 64-bit Cyclic Redundancy Check userdata
111 @tparam string block block to be processed
112 @tparam integer value initial value
113 @treturn string packed block
116 int liba_crc64_pack(lua_State
*L
)
118 struct crc64
*const ctx
= (struct crc64
*)lua_touserdata(L
, 1);
128 char const *p
= luaL_checklstring(L
, 2, &n
);
129 if (lua_gettop(L
) > 2) { value
= lua_u64_get(L
, 3); }
130 u
.s
= lua_pushfstring(L
, "%s ", p
) + n
;
131 value
= ctx
->eval(ctx
->table
, p
, n
, value
);
132 ctx
->eval
== a_crc64m
133 ? a_u64_setb(u
.p
, value
)
134 : a_u64_setl(u
.p
, value
);
140 static int liba_crc64_set(lua_State
*L
)
142 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
144 case 0xE8859EEB: // __name
145 case 0xE70C48C6: // __call
146 case 0xA65758B2: // __index
147 case 0xAEB551C6: // __newindex
150 lua_getmetatable(L
, 1);
157 static int liba_crc64_get(lua_State
*L
)
159 struct crc64
const *const ctx
= (struct crc64
const *)lua_touserdata(L
, 1);
160 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
162 case 0x014FE58A: // table
163 lua_array_u64_new(L
, ctx
->table
, 0x100);
165 case 0xA65758B2: // __index
166 lua_registry_get(L
, liba_crc64_new
);
167 lua_pushstring(L
, "table");
168 lua_array_u64_new(L
, ctx
->table
, 0x100);
172 lua_getmetatable(L
, 1);
179 static int liba_crc64_(lua_State
*L
)
181 lua_pushcfunction(L
, liba_crc64_new
);
183 lua_call(L
, lua_gettop(L
) - 1, 1);
187 int luaopen_liba_crc64(lua_State
*L
)
189 static lua_fun
const funcs
[] = {
190 {"new", liba_crc64_new
},
191 {"gen", liba_crc64_gen
},
192 {"eval", liba_crc64_eval
},
193 {"pack", liba_crc64_pack
},
195 lua_createtable(L
, 0, A_LEN(funcs
));
196 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
197 lua_createtable(L
, 0, 1);
198 lua_fun_set(L
, -1, "__call", liba_crc64_
);
199 lua_setmetatable(L
, -2);
201 static lua_fun
const metas
[] = {
202 {"__newindex", liba_crc64_set
},
203 {"__index", liba_crc64_get
},
204 {"__call", liba_crc64_eval
},
206 lua_createtable(L
, 0, A_LEN(metas
) + A_LEN(funcs
) + 1);
207 lua_fun_reg(L
, -1, metas
, A_LEN(metas
));
208 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
209 lua_str_set(L
, -1, "__name", "a.crc64");
210 lua_registry_set(L
, liba_crc64_new
);