From 67e3ac3f542a3bdd20764364fdfed677461d0a4b Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 29 Apr 2019 14:19:41 +0200 Subject: [PATCH] add: ndef parsing to LUA --- client/scripting.c | 40 ++++++++++++++++++++++++++++++++++++ client/scripting.h | 3 ++- client/scripts/ndef_dump.lua | 12 +++++++---- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/client/scripting.c b/client/scripting.c index fe1fe1def..59a03025f 100644 --- a/client/scripting.c +++ b/client/scripting.c @@ -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} }; diff --git a/client/scripting.h b/client/scripting.h index 75a08605b..4db566dcc 100644 --- a/client/scripting.h +++ b/client/scripting.h @@ -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/" diff --git a/client/scripts/ndef_dump.lua b/client/scripts/ndef_dump.lua index 0db3108f0..da6b7d806 100644 --- a/client/scripts/ndef_dump.lua +++ b/client/scripts/ndef_dump.lua @@ -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