78 files changed
+27046
-17006
No files matched your search
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
** $Id: lbitlib.c,v 1.18 2013/03/19 13:19:12 roberto Exp $
|
||||
** Standard library for bitwise operations
|
||||
** See Copyright Notice in lua.h
|
||||
**
|
||||
** Patched to provide a compatibility layer with Lua5.3+ where
|
||||
** bit32 library was removed.
|
||||
*/
|
||||
|
||||
#include "lua_bitlib.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
|
||||
/* number of bits to consider in a number */
|
||||
#define LUA_BITLIB_NBITS 32
|
||||
|
||||
|
||||
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_BITLIB_NBITS - 1)) << 1))
|
||||
|
||||
/* macro to trim extra bits */
|
||||
#define trim(x) ((x) & ALLONES)
|
||||
|
||||
|
||||
/* builds a number with 'n' ones (1 <= n <= LUA_BITLIB_NBITS) */
|
||||
#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
|
||||
|
||||
|
||||
typedef lua_Unsigned b_uint;
|
||||
|
||||
|
||||
static b_uint andaux(lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = ~(b_uint)0;
|
||||
for (i = 1; i <= n; i++)
|
||||
r &= (b_uint)luaL_checkinteger(L, i);
|
||||
return trim(r);
|
||||
}
|
||||
|
||||
|
||||
static int b_and(lua_State *L) {
|
||||
b_uint r = andaux(L);
|
||||
lua_pushinteger(L, r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_test(lua_State *L) {
|
||||
b_uint r = andaux(L);
|
||||
lua_pushboolean(L, r != 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_or(lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = 0;
|
||||
for (i = 1; i <= n; i++)
|
||||
r |= (b_uint) luaL_checkinteger(L, i);
|
||||
lua_pushinteger(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_xor(lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = 0;
|
||||
for (i = 1; i <= n; i++)
|
||||
r ^= (b_uint) luaL_checkinteger(L, i);
|
||||
lua_pushinteger(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_not(lua_State *L) {
|
||||
b_uint r = ~((b_uint)luaL_checkinteger(L, 1));
|
||||
lua_pushinteger(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_shift(lua_State *L, b_uint r, int i) {
|
||||
if (i < 0) { /* shift right? */
|
||||
i = -i;
|
||||
r = trim(r);
|
||||
if (i >= LUA_BITLIB_NBITS) r = 0;
|
||||
else r >>= i;
|
||||
} else { /* shift left */
|
||||
if (i >= LUA_BITLIB_NBITS) r = 0;
|
||||
else r <<= i;
|
||||
r = trim(r);
|
||||
}
|
||||
lua_pushinteger(L, r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_lshift(lua_State *L) {
|
||||
return b_shift(L, (b_uint)luaL_checkinteger(L, 1), luaL_checkinteger(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_rshift(lua_State *L) {
|
||||
return b_shift(L, (b_uint)luaL_checkinteger(L, 1), -luaL_checkinteger(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_arshift(lua_State *L) {
|
||||
b_uint r = (b_uint)luaL_checkinteger(L, 1);
|
||||
int i = luaL_checkinteger(L, 2);
|
||||
if (i < 0 || !(r & ((b_uint)1 << (LUA_BITLIB_NBITS - 1))))
|
||||
return b_shift(L, r, -i);
|
||||
else { /* arithmetic shift for 'negative' number */
|
||||
if (i >= LUA_BITLIB_NBITS) r = ALLONES;
|
||||
else
|
||||
r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
|
||||
lua_pushinteger(L, r);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int b_rot(lua_State *L, int i) {
|
||||
b_uint r = (b_uint)luaL_checkinteger(L, 1);
|
||||
i &= (LUA_BITLIB_NBITS - 1); /* i = i % NBITS */
|
||||
r = trim(r);
|
||||
if (i != 0) /* avoid undefined shift of LUA_BITLIB_NBITS when i == 0 */
|
||||
r = (r << i) | (r >> (LUA_BITLIB_NBITS - i));
|
||||
|
||||
lua_pushinteger(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_lrot(lua_State *L) {
|
||||
return b_rot(L, luaL_checkinteger(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_rrot(lua_State *L) {
|
||||
return b_rot(L, -luaL_checkinteger(L, 2));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** get field and width arguments for field-manipulation functions,
|
||||
** checking whether they are valid.
|
||||
** ('luaL_error' called without 'return' to avoid later warnings about
|
||||
** 'width' being used uninitialized.)
|
||||
*/
|
||||
static int fieldargs(lua_State *L, int farg, int *width) {
|
||||
int f = luaL_checkinteger(L, farg);
|
||||
int w = luaL_optinteger(L, farg + 1, 1);
|
||||
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
|
||||
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
|
||||
if (f + w > LUA_BITLIB_NBITS)
|
||||
luaL_error(L, "trying to access non-existent bits");
|
||||
*width = w;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
static int b_extract(lua_State *L) {
|
||||
int w;
|
||||
b_uint r = (b_uint)luaL_checkinteger(L, 1);
|
||||
int f = fieldargs(L, 2, &w);
|
||||
r = (r >> f) & mask(w);
|
||||
lua_pushinteger(L, r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_replace(lua_State *L) {
|
||||
int w;
|
||||
b_uint r = (b_uint)luaL_checkinteger(L, 1);
|
||||
b_uint v = (b_uint)luaL_checkinteger(L, 2);
|
||||
int f = fieldargs(L, 3, &w);
|
||||
int m = mask(w);
|
||||
v &= m; /* erase bits outside given width */
|
||||
r = (r & ~(m << f)) | (v << f);
|
||||
lua_pushinteger(L, r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void register_bit32_lib(lua_State *L) {
|
||||
static const luaL_Reg bitlib[] = {
|
||||
{"arshift", b_arshift},
|
||||
{"band", b_and},
|
||||
{"bnot", b_not},
|
||||
{"bor", b_or},
|
||||
{"bxor", b_xor},
|
||||
{"btest", b_test},
|
||||
{"extract", b_extract},
|
||||
{"lrotate", b_lrot},
|
||||
{"lshift", b_lshift},
|
||||
{"replace", b_replace},
|
||||
{"rrotate", b_rrot},
|
||||
{"rshift", b_rshift},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
luaL_newlib(L, bitlib);
|
||||
lua_setfield(L, -2, "bit32");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Bitwise manipulation library which got deprecated in lua5.3
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef LUA_BITLIB_H__
|
||||
#define LUA_BITLIB_H__
|
||||
|
||||
#include <lua.h>
|
||||
|
||||
void register_bit32_lib(lua_State *L);
|
||||
|
||||
#endif
|
||||
+1862
-1788
File diff suppressed because it is too large.
Load diff
+18
-21
@@ -22,6 +22,7 @@
|
||||
#include <errno.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua_bitlib.h"
|
||||
#include "cmdmain.h"
|
||||
#include "proxmark3.h"
|
||||
#include "comms.h"
|
||||
@@ -320,7 +321,7 @@ static int l_GetFromFlashMemSpiffs(lua_State *L) {
|
||||
}
|
||||
|
||||
lua_pushlstring(L, (const char *)data, len);
|
||||
lua_pushunsigned(L, len);
|
||||
lua_pushinteger(L, len);
|
||||
free(data);
|
||||
return 2;
|
||||
}
|
||||
@@ -344,11 +345,11 @@ static int l_WaitForResponseTimeout(lua_State *L) {
|
||||
|
||||
// extract first param. cmd byte to look for
|
||||
if (n >= 1)
|
||||
cmd = luaL_checkunsigned(L, 1);
|
||||
cmd = (uint32_t)luaL_checkinteger(L, 1);
|
||||
|
||||
// extract second param. timeout value
|
||||
if (n >= 2)
|
||||
ms_timeout = luaL_checkunsigned(L, 2);
|
||||
ms_timeout = luaL_checkinteger(L, 2);
|
||||
|
||||
PacketResponseNG resp;
|
||||
if (WaitForResponseTimeout(cmd, &resp, ms_timeout) == false) {
|
||||
@@ -643,7 +644,7 @@ static int l_crc8legic(lua_State *L) {
|
||||
size_t size;
|
||||
const char *p_hexstr = luaL_checklstring(L, 1, &size);
|
||||
uint16_t retval = CRC8Legic((uint8_t *)p_hexstr, size);
|
||||
lua_pushunsigned(L, retval);
|
||||
lua_pushinteger(L, retval);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -659,7 +660,7 @@ static int l_crc16legic(lua_State *L) {
|
||||
|
||||
init_table(CRC_LEGIC_16);
|
||||
uint16_t retval = crc16_legic((uint8_t *)p_hexstr, hexsize, uidcrc);
|
||||
lua_pushunsigned(L, retval);
|
||||
lua_pushinteger(L, retval);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -669,7 +670,7 @@ static int l_crc16(lua_State *L) {
|
||||
const char *p_str = luaL_checklstring(L, 1, &size);
|
||||
|
||||
uint16_t checksum = Crc16ex(CRC_CCITT, (uint8_t *) p_str, size);
|
||||
lua_pushunsigned(L, checksum);
|
||||
lua_pushinteger(L, checksum);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -735,7 +736,7 @@ static int l_reveng_models(lua_State *L) {
|
||||
#define NMODELS 106
|
||||
|
||||
int count = 0;
|
||||
uint8_t in_width = luaL_checkunsigned(L, 1);
|
||||
uint8_t in_width = (uint8_t)luaL_checkinteger(L, 1);
|
||||
if (in_width > 89)
|
||||
return returnToLuaWithError(L, "Width cannot exceed 89, got %d", in_width);
|
||||
|
||||
@@ -916,8 +917,8 @@ static int l_keygen_algoB(lua_State *L) {
|
||||
uint32_t pwd = ul_ev1_pwdgenB(uid);
|
||||
uint16_t pack = ul_ev1_packgenB(uid);
|
||||
|
||||
lua_pushunsigned(L, pwd);
|
||||
lua_pushunsigned(L, pack);
|
||||
lua_pushinteger(L, pwd);
|
||||
lua_pushinteger(L, pack);
|
||||
return 2;
|
||||
}
|
||||
|
||||
@@ -949,8 +950,8 @@ static int l_keygen_algoD(lua_State *L) {
|
||||
uint32_t pwd = ul_ev1_pwdgenD(uid);
|
||||
uint16_t pack = ul_ev1_packgenD(uid);
|
||||
|
||||
lua_pushunsigned(L, pwd);
|
||||
lua_pushunsigned(L, pack);
|
||||
lua_pushinteger(L, pwd);
|
||||
lua_pushinteger(L, pack);
|
||||
return 2;
|
||||
}
|
||||
|
||||
@@ -1037,7 +1038,7 @@ static int l_T55xx_readblock(lua_State *L) {
|
||||
return returnToLuaWithError(L, "Failed to get actual data");
|
||||
}
|
||||
|
||||
lua_pushunsigned(L, blockData);
|
||||
lua_pushinteger(L, blockData);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1436,16 +1437,12 @@ int set_pm3_libraries(lua_State *L) {
|
||||
};
|
||||
|
||||
lua_pushglobaltable(L);
|
||||
// Core library is in this table. Contains '
|
||||
// this is 'pm3' table
|
||||
lua_newtable(L);
|
||||
|
||||
// put the function into the hash table.
|
||||
for (int i = 0; libs[i].name; i++) {
|
||||
lua_pushcfunction(L, libs[i].func);
|
||||
lua_setfield(L, -2, libs[i].name); // set the name, pop stack
|
||||
}
|
||||
// Name of 'core'
|
||||
// bit32 compatibility shim
|
||||
register_bit32_lib(L);
|
||||
|
||||
// Core module
|
||||
luaL_newlib(L, libs);
|
||||
lua_setfield(L, -2, "core");
|
||||
|
||||
// remove the global environment table from the stack
|
||||
|
||||
Reference in new issue
Block a user