renamed 'hf mfdes readdata, writedata' to 'read/write'
[RRG-proxmark3.git] / client / src / pm3_bitlib.c
blob064c0f3c9c13056f8ffe785b91ed70735ba413a0
1 /* Bitwise operations library */
2 /* (c) Reuben Thomas 2000-2008 */
3 /* See README for license */
5 #include <lua.h>
6 #include <lauxlib.h>
7 #include <limits.h>
9 #include "pm3_bit_limits.h"
10 #include "pm3_bitlib.h"
13 /* FIXME: Assumes lua_Integer is ptrdiff_t */
14 #define LUA_INTEGER_MAX PTRDIFF_MAX
15 #define LUA_INTEGER_MIN PTRDIFF_MIN
17 /* FIXME: Assumes size_t is an unsigned lua_Integer */
18 typedef size_t lua_UInteger;
19 #define LUA_UINTEGER_MAX SIZE_MAX
22 /* Bit type size and limits */
24 #define BIT_BITS \
25 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? \
26 BITLIB_FLOAT_BITS : (CHAR_BIT * sizeof(lua_Integer)))
28 /* This code may give warnings if BITLIB_FLOAT_* are too big to fit in
29 long, but that doesn't matter since in that case they won't be
30 used. */
31 #define BIT_MAX \
32 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MAX : LUA_INTEGER_MAX)
34 #define BIT_MIN \
35 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MIN : LUA_INTEGER_MIN)
37 #define BIT_UMAX \
38 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_UMAX : LUA_UINTEGER_MAX)
41 /* Define TOBIT to get a bit value */
42 #ifdef BUILTIN_CAST
43 #define
44 #define TOBIT(L, n, res) \
45 ((void)(res), luaL_checkinteger((L), (n)))
46 #else
47 #include <stdint.h>
48 #include <math.h>
50 /* FIXME: Assumes lua_Number fits in a double (use of fmod). */
51 #define TOBIT(L, n, res) \
52 ((lua_Integer)(((res) = fmod(luaL_checknumber(L, (n)), (double)BIT_UMAX + 1.0)), \
53 (res) > BIT_MAX ? ((res) -= (double)BIT_UMAX, (res) -= 1) : \
54 ((res) < BIT_MIN ? ((res) += (double)BIT_UMAX, (res) += 1) : (res))))
55 #endif
58 #define BIT_TRUNCATE(i) \
59 ((i) & BIT_UMAX)
62 /* Operations
64 The macros MONADIC and VARIADIC only deal with bitwise operations.
66 LOGICAL_SHIFT truncates its left-hand operand before shifting so
67 that any extra bits at the most-significant end are not shifted
68 into the result.
70 ARITHMETIC_SHIFT does not truncate its left-hand operand, so that
71 the sign bits are not removed and right shift work properly.
74 #define MONADIC(name, op) \
75 static int bit_ ## name(lua_State *L) { \
76 lua_Number f; \
77 lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \
78 return 1; \
81 #define VARIADIC(name, op) \
82 static int bit_ ## name(lua_State *L) { \
83 lua_Number f; \
84 int n = lua_gettop(L), i; \
85 lua_Integer w = TOBIT(L, 1, f); \
86 for (i = 2; i <= n; i++) \
87 w op TOBIT(L, i, f); \
88 lua_pushinteger(L, BIT_TRUNCATE(w)); \
89 return 1; \
92 #define LOGICAL_SHIFT(name, op) \
93 static int bit_ ## name(lua_State *L) { \
94 lua_Number f; \
95 lua_Number n = luaL_checknumber(L, 2); \
96 lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \
97 (unsigned)n)); \
98 return 1; \
101 #define ARITHMETIC_SHIFT(name, op) \
102 static int bit_ ## name(lua_State *L) { \
103 lua_Number f; \
104 lua_Number n = luaL_checknumber(L, 2); \
105 lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \
106 (unsigned)n)); \
107 return 1; \
110 MONADIC(cast, +)
111 MONADIC(bnot, ~)
112 VARIADIC(band, &=)
113 VARIADIC(bor, |=)
114 VARIADIC(bxor, ^=)
115 ARITHMETIC_SHIFT(lshift, <<)
116 LOGICAL_SHIFT(rshift, >>)
117 ARITHMETIC_SHIFT(arshift, >>)
119 static const struct luaL_Reg bitlib[] = {
120 {"cast", bit_cast},
121 {"bnot", bit_bnot},
122 {"band", bit_band},
123 {"bor", bit_bor},
124 {"bxor", bit_bxor},
125 {"lshift", bit_lshift},
126 {"rshift", bit_rshift},
127 {"arshift", bit_arshift},
128 {NULL, NULL}
131 LUALIB_API int luaopen_bit(lua_State *L);
132 LUALIB_API int luaopen_bit(lua_State *L) {
133 luaL_newlib(L, bitlib);
134 //luaL_register(L, "bit", bitlib);
135 lua_pushnumber(L, BIT_BITS);
136 lua_setfield(L, -2, "bits");
137 return 1;
141 LUALIB_API int luaopen_bit (lua_State *L) {
142 luaL_register(L, "bit", bitlib);
143 lua_pushnumber(L, BIT_BITS);
144 lua_setfield(L, -2, "bits");
145 return 1;
149 ** Open bit library
151 int set_bit_library(lua_State *L) {
153 luaL_requiref(L, "bit", luaopen_bit, 1);
154 lua_pop(L, 1);
155 return 1;