fix one too small
[RRG-proxmark3.git] / client / src / pm3_bitlib.c
blobe6d0022f7952c8e2c34a0316263fc8b40bd6a81a
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.
5 //
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 //-----------------------------------------------------------------------------
21 #include <lua.h>
22 #include <lauxlib.h>
23 #include <limits.h>
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 */
40 #define BIT_BITS \
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
46 used. */
47 #define BIT_MAX \
48 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MAX : LUA_INTEGER_MAX)
50 #define BIT_MIN \
51 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MIN : LUA_INTEGER_MIN)
53 #define BIT_UMAX \
54 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_UMAX : LUA_UINTEGER_MAX)
57 /* Define TOBIT to get a bit value */
58 #ifdef BUILTIN_CAST
59 #define
60 #define TOBIT(L, n, res) \
61 ((void)(res), luaL_checkinteger((L), (n)))
62 #else
63 #include <stdint.h>
64 #include <math.h>
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))))
71 #endif
74 #define BIT_TRUNCATE(i) \
75 ((i) & BIT_UMAX)
78 /* Operations
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
84 into the result.
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) { \
92 lua_Number f; \
93 lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \
94 return 1; \
97 #define VARIADIC(name, op) \
98 static int bit_ ## name(lua_State *L) { \
99 lua_Number f; \
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)); \
105 return 1; \
108 #define LOGICAL_SHIFT(name, op) \
109 static int bit_ ## name(lua_State *L) { \
110 lua_Number f; \
111 lua_Number n = luaL_checknumber(L, 2); \
112 lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \
113 (unsigned)n)); \
114 return 1; \
117 #define ARITHMETIC_SHIFT(name, op) \
118 static int bit_ ## name(lua_State *L) { \
119 lua_Number f; \
120 lua_Number n = luaL_checknumber(L, 2); \
121 lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \
122 (unsigned)n)); \
123 return 1; \
126 MONADIC(cast, +)
127 MONADIC(bnot, ~)
128 VARIADIC(band, &=)
129 VARIADIC(bor, |=)
130 VARIADIC(bxor, ^=)
131 ARITHMETIC_SHIFT(lshift, <<)
132 LOGICAL_SHIFT(rshift, >>)
133 ARITHMETIC_SHIFT(arshift, >>)
135 static const struct luaL_Reg bitlib[] = {
136 {"cast", bit_cast},
137 {"bnot", bit_bnot},
138 {"band", bit_band},
139 {"bor", bit_bor},
140 {"bxor", bit_bxor},
141 {"lshift", bit_lshift},
142 {"rshift", bit_rshift},
143 {"arshift", bit_arshift},
144 {NULL, NULL}
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");
153 return 1;
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");
161 return 1;
165 ** Open bit library
167 int set_bit_library(lua_State *L) {
169 luaL_requiref(L, "bit", luaopen_bit, 1);
170 lua_pop(L, 1);
171 return 1;