From 5ff3c4017bdbfe78238ef48c3b521e79819ed359 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 12 Mar 2015 09:50:22 +0100 Subject: [PATCH] add: a experimental lualib, unstable. fix: Introduced a wrong variable name due to "cut & paste" before, now fixed. --- armsrc/lfops.c | 4 +- client/lualibs/precalc.lua | 103 +++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 client/lualibs/precalc.lua diff --git a/armsrc/lfops.c b/armsrc/lfops.c index 887e8404..70c6e33b 100644 --- a/armsrc/lfops.c +++ b/armsrc/lfops.c @@ -1465,7 +1465,7 @@ void CopyIndala224toT55x7(int uid1, int uid2, int uid3, int uid4, int uid5, int int DemodPCF7931(uint8_t **outBlocks) { uint8_t BitStream[256] = {0x00}; - uint8_t Blocks[8][16] = [0x00}; + uint8_t Blocks[8][16]; uint8_t *dest = BigBuf_get_addr(); int GraphTraceLen = BigBuf_max_traceLen(); int i, j, lastval, bitidx, half_switch; @@ -1496,7 +1496,7 @@ int DemodPCF7931(uint8_t **outBlocks) { } else { while(i < GraphTraceLen) { - if( !(dest[i] < dest[i-1]) && v[i] < lmin) + if( !(dest[i] < dest[i-1]) && dest[i] < lmin) break; i++; } diff --git a/client/lualibs/precalc.lua b/client/lualibs/precalc.lua new file mode 100644 index 00000000..284c8b62 --- /dev/null +++ b/client/lualibs/precalc.lua @@ -0,0 +1,103 @@ +--[[ + This is an experimental lib. +--]] +local utils = require('utils') + +-- LOOKUP Tables +local perm = {} +perm [1]= { 0x0, 0x1, 0x3, 0x2, 0x7, 0x6, 0x4, 0x5, 0xF, 0xE, 0xC, 0xD, 0x8, 0x9, 0xB, 0xA } +perm [2]= { 0x1, 0x0, 0x2, 0x3, 0x6, 0x7, 0x5, 0x4, 0xE, 0xF, 0xD, 0xC, 0x9, 0x8, 0xA, 0xB } +perm [3]= { 0x2, 0x3, 0x1, 0x0, 0x5, 0x4, 0x6, 0x7, 0xD, 0xC, 0xE, 0xF,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e, 0xe, 0x1, 0xe, 0x1, 0x1, 0xe } + +local function ApplyPermutationAndShifts( pos, value, nibble) + local el2bytes = shifts[pos] + local el2 = el2bytes[nibble+1] --one indexed + local shiftInit = el2bytes[1] + local permrow = perm[shiftInit+1] + local j = 1 + for i = 1,16 do + if permrow[i] == el2 then + j = i + break + end + end + local rsbytes = perm[value+1] + local rs = rsbytes[j] + return rs +end + +local function GetOne( uid, block ) + + if uid == nil then return nil, 'empty uid string' end + if #uid == 0 then return nil, 'empty uid string' end + if #uid ~= 8 then return nil, 'uid wrong length. Should be 4 hex bytes' end + if type(block) ~= 'number' then return nil, 'block is not number' end + if block > 16 or block < 0 then return nil, 'block is out-of-range' end + + local s = ('%s%02X'):format(uid,block) + local nibble1 = tonumber(s:sub(1,1),16) + 1 + + local permuted = '' + for i = 1, #s do + local el_row = shifts[i] + local el_value = el_row[nibble1] + j = 1 + while j <= i do + if i-j > 0 then + local nibble = tonumber(s:sub(j+1,j+1),16) + el_value = ApplyPermutationAndShifts(i-j, el_value, nibble) + end + j = j+1 + end + permuted =('%s%X'):format(permuted,el_value) + end + + permuted = 'C2'..permuted + local crc64numStr = utils.Crc64(permuted) + local keybytes = utils.ConvertAsciiToBytes(crc64numStr, true) + local key = utils.ConvertBytesToHex(keybytes) + return key:sub(1,12) +end + +local PreCalc = +{ + GetAll = function(id) + if id == nil then return nil, 'empty string' end + if #id == 0 then return nil, 'empty string' end + if #id ~= 8 then return nil, 'wrong length. Should be 4 hex bytes' end + + local list = '4b0b20107ccb' + for i = 1,15 do + local key, err = GetOne(id,i) + if not key then return oops(err) end + list = list..key + end + return list + end, +} +return PreCalc \ No newline at end of file -- 2.39.5