1 //-----------------------------------------------------------------------------
2 // Borrowed initially from https://github.com/LuaDist/bitlib
3 // Copyright (C) Reuben Thomas 2000-2008
4 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // See LICENSE.txt for the text of the license.
17 //-----------------------------------------------------------------------------
18 // Bitwise operations library
19 //-----------------------------------------------------------------------------
25 #include "pm3_bit_limits.h"
26 #include "pm3_bitlib.h"
29 /* FIXME: Assumes lua_Integer is ptrdiff_t */
30 #define LUA_INTEGER_MAX PTRDIFF_MAX
31 #define LUA_INTEGER_MIN PTRDIFF_MIN
33 /* FIXME: Assumes size_t is an unsigned lua_Integer */
34 typedef size_t lua_UInteger
;
35 #define LUA_UINTEGER_MAX SIZE_MAX
38 /* Bit type size and limits */
41 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? \
42 BITLIB_FLOAT_BITS : (CHAR_BIT * sizeof(lua_Integer)))
44 /* This code may give warnings if BITLIB_FLOAT_* are too big to fit in
45 long, but that doesn't matter since in that case they won't be
48 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MAX : LUA_INTEGER_MAX)
51 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MIN : LUA_INTEGER_MIN)
54 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_UMAX : LUA_UINTEGER_MAX)
57 /* Define TOBIT to get a bit value */
60 #define TOBIT(L, n, res) \
61 ((void)(res), luaL_checkinteger((L), (n)))
66 /* FIXME: Assumes lua_Number fits in a double (use of fmod). */
67 #define TOBIT(L, n, res) \
68 ((lua_Integer)(((res) = fmod(luaL_checknumber(L, (n)), (double)BIT_UMAX + 1.0)), \
69 (res) > BIT_MAX ? ((res) -= (double)BIT_UMAX, (res) -= 1) : \
70 ((res) < BIT_MIN ? ((res) += (double)BIT_UMAX, (res) += 1) : (res))))
74 #define BIT_TRUNCATE(i) \
80 The macros MONADIC and VARIADIC only deal with bitwise operations.
82 LOGICAL_SHIFT truncates its left-hand operand before shifting so
83 that any extra bits at the most-significant end are not shifted
86 ARITHMETIC_SHIFT does not truncate its left-hand operand, so that
87 the sign bits are not removed and right shift work properly.
90 #define MONADIC(name, op) \
91 static int bit_ ## name(lua_State *L) { \
93 lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \
97 #define VARIADIC(name, op) \
98 static int bit_ ## name(lua_State *L) { \
100 int n = lua_gettop(L), i; \
101 lua_Integer w = TOBIT(L, 1, f); \
102 for (i = 2; i <= n; i++) \
103 w op TOBIT(L, i, f); \
104 lua_pushinteger(L, BIT_TRUNCATE(w)); \
108 #define LOGICAL_SHIFT(name, op) \
109 static int bit_ ## name(lua_State *L) { \
111 lua_Number n = luaL_checknumber(L, 2); \
112 lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \
117 #define ARITHMETIC_SHIFT(name, op) \
118 static int bit_ ## name(lua_State *L) { \
120 lua_Number n = luaL_checknumber(L, 2); \
121 lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \
131 ARITHMETIC_SHIFT(lshift
, <<)
132 LOGICAL_SHIFT(rshift
, >>)
133 ARITHMETIC_SHIFT(arshift
, >>)
135 static const struct luaL_Reg bitlib
[] = {
141 {"lshift", bit_lshift
},
142 {"rshift", bit_rshift
},
143 {"arshift", bit_arshift
},
147 LUALIB_API
int luaopen_bit(lua_State
*L
);
148 LUALIB_API
int luaopen_bit(lua_State
*L
) {
149 luaL_newlib(L
, bitlib
);
150 //luaL_register(L, "bit", bitlib);
151 lua_pushnumber(L
, BIT_BITS
);
152 lua_setfield(L
, -2, "bits");
157 LUALIB_API int luaopen_bit (lua_State *L) {
158 luaL_register(L, "bit", bitlib);
159 lua_pushnumber(L, BIT_BITS);
160 lua_setfield(L, -2, "bits");
167 int set_bit_library(lua_State
*L
) {
169 luaL_requiref(L
, "bit", luaopen_bit
, 1);