add: ndef parsing to LUA

This commit is contained in:
iceman1001 2019-04-29 14:19:41 +02:00
parent 03d6454d95
commit 67e3ac3f54
3 changed files with 50 additions and 5 deletions

View file

@ -951,6 +951,45 @@ static int l_T55xx_detect(lua_State *L) {
return 2;
}
//
static int l_ndefparse(lua_State *L) {
size_t len = 0, size;
//Check number of arguments
int n = lua_gettop(L);
if (n != 3) {
return returnToLuaWithError(L, "You need to supply three parameters");
}
size_t datalen = luaL_checknumber(L, 1);
bool verbose = luaL_checknumber(L, 2);
uint8_t *data = calloc(datalen, sizeof(uint8_t));
if (data == 0) {
return returnToLuaWithError(L, "Allocating memory failed");
}
// data
const char *p_data = luaL_checklstring(L, 3, &size);
if (size) {
if (size > (datalen << 1) )
size = (datalen << 1);
uint32_t tmp;
for (int i = 0; i < size; i += 2) {
sscanf(&p_data[i], "%02x", &tmp);
data[i >> 1] = tmp & 0xFF;
len++;
}
}
int res = NDEFDecodeAndPrint(data, datalen, verbose);
lua_pushinteger(L, res);
return 1;
}
/**
* @brief Sets the lua path to include "./lualibs/?.lua", in order for a script to be
* able to do "require('foobar')" if foobar.lua is within lualibs folder.
@ -1009,6 +1048,7 @@ int set_pm3_libraries(lua_State *L) {
{"keygen_algo_d", l_keygen_algoD},
{"t55xx_readblock", l_T55xx_readblock},
{"t55xx_detect", l_T55xx_detect},
{"ndefparse" , l_ndefparse},
{NULL, NULL}
};

View file

@ -29,7 +29,8 @@
#include "cmdhfmfhard.h"
#include "cmdhfmfu.h"
#include "protocols.h"
#include "cmdlft55xx.h" // read t55xx etc
#include "cmdlft55xx.h" // read t55xx etc
#include "mifare/ndef.h" // ndef parsing
#define LUA_LIBRARIES_DIRECTORY "lualibs/"
#define LUA_SCRIPTS_DIRECTORY "scripts/"

View file

@ -92,7 +92,7 @@ end
local function getBlock(blockno)
local block, err
local c = Command:newMIX{cmd = cmds.CMD_MIFAREU_READBL, arg1 = blockno, data = 0}
block, err = getblockdata(c:sendMIX(false))
block, err = getblockdata(c:scr(false))
if not block then return oops(err) end
if #block < 32 then
@ -153,8 +153,9 @@ local function main( args)
end
-- Block 3 contains number of blocks
local b3chars = utils.ConvertHexToBytes(blocks[4]);
local numBlocks = b3chars[3] * 2 + 6
print("Number of blocks:", numBlocks)
local t5tarea = b3chars[3] * 8
local t5tarea_blocks = t5tarea / 4;
print("Number of blocks:", t5tarea_blocks)
-- NDEF compliant?
if b3chars[1] ~= 0xE1 then
@ -173,7 +174,7 @@ local function main( args)
removing bytes from 5 to 18 from each answer.
--]]
print('Dumping data...please wait')
for i = 4, numBlocks - 1, 1 do
for i = 4, t5tarea_blocks - 1, 1 do
blocks, err = getBlock(i)
if err then
disconnect();
@ -190,6 +191,9 @@ local function main( args)
print('Manufacturer', info.manufacturer)
print('Type ', info.name)
core.ndefparse( t5tarea, true, blockData);
for k,v in ipairs(blockData) do
print(string.format('Block %02x: %02x %02x %02x %02x', k-1, string.byte(v, 1,4)))
end