From 0d2c590974319db6fc54400ce153d86b68a09852 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 28 Feb 2016 22:43:21 +0100 Subject: [PATCH] ADD: num_to_bytebitsLSBF function. ADD: lf guard clone - works... needs some checking. ADD: added a option to "addparity" to set zero on fixed pos. --- client/cmddata.c | 4 +- client/cmdlfguard.c | 139 +++++++++++++++++++++++++------------------- client/util.c | 20 ++++++- client/util.h | 15 ++++- common/lfdemod.c | 17 +++--- 5 files changed, 123 insertions(+), 72 deletions(-) diff --git a/client/cmddata.c b/client/cmddata.c index 8e0668f7..311e5b32 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -619,13 +619,13 @@ int CmdG_Prox_II_Demod(const char *Cmd) continue; } if (keyCnt<8){ //lsb first - xorKey = xorKey | (DemodBuffer[startIdx+idx]< clone Guardall tag"}, + {"clone", CmdGuardClone, 0, " clone Guardall tag"}, // {"sim", CmdGuardSim, 0, " simulate Guardall tag"}, {NULL, NULL, 0, NULL} }; diff --git a/client/util.c b/client/util.c index ae8e4fec..9768dbeb 100644 --- a/client/util.c +++ b/client/util.c @@ -103,6 +103,7 @@ void print_hex(const uint8_t * data, const size_t len) { printf("%02x ", data[i]); printf("\n"); } + void print_hex_break(const uint8_t *data, const size_t len, uint8_t breaks) { int rownum = 0; @@ -178,6 +179,7 @@ char *sprint_hex_ascii(const uint8_t *data, const size_t len) { sprintf(tmp, "%s| %s", sprint_hex(data, max_len) , data); return buf; } + void num_to_bytes(uint64_t n, size_t len, uint8_t* dest) { while (len--) { @@ -197,12 +199,22 @@ uint64_t bytes_to_num(uint8_t* src, size_t len) return num; } -void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest) { +// takes a number (uint64_t) and creates a binarray in dest. +void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest) { while (len--) { dest[len] = n & 1; n >>= 1; } } +//least significant bit first +void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest) +{ + for(int i = 0 ; i < len ; ++i) { + dest[i] = n & 1; + n >>= 1; + } +} + // aa,bb,cc,dd,ee,ff,gg,hh, ii,jj,kk,ll,mm,nn,oo,pp // to @@ -220,6 +232,8 @@ uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockS return tmp; } +// takes a uint8_t src array, for len items and reverses the byte order in blocksizes (8,16,32,64), +// returns: the dest array contains the reordered src array. void SwapEndian64ex(const uint8_t *src, const size_t len, const uint8_t blockSize, uint8_t *dest){ for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){ for (size_t i = 0; i < blockSize; i++){ @@ -228,7 +242,6 @@ void SwapEndian64ex(const uint8_t *src, const size_t len, const uint8_t blockSiz } } - // ------------------------------------------------------------------------- // string parameters lib // ------------------------------------------------------------------------- @@ -493,6 +506,7 @@ void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length) *(target)= GetParity(source + length / 2, ODD, length / 2); } +// xor two arrays together for len items. The dst array contains the new xored values. void xor(unsigned char * dst, unsigned char * src, size_t len) { for( ; len > 0; len--,dst++,src++) *dst ^= *src; @@ -502,6 +516,7 @@ int32_t le24toh (uint8_t data[3]) { return (data[2] << 16) | (data[1] << 8) | data[0]; } +// Pack a bitarray into a uint32_t. uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits) { if (len > 32) return 0; @@ -526,6 +541,7 @@ void rol(uint8_t *data, const size_t len){ data[len-1] = first; } +// Swap bit order on a uint32_t value. Can be limited by nrbits just use say 8bits reversal uint32_t SwapBits(uint32_t value, int nrbits) { uint32_t newvalue = 0; for(int i = 0; i < nrbits; i++) { diff --git a/client/util.h b/client/util.h index 2813bb8c..9c16ba5b 100644 --- a/client/util.h +++ b/client/util.h @@ -36,6 +36,18 @@ #define EVEN 0 #define ODD 1 +#ifndef NIBBLE_HIGH +# define NIBBLE_HIGH(b) ( (b & 0xF0) >> 4 ) +#endif +#ifndef NIBBLE_LOW +# define NIBBLE_LOW(b) ( b & 0x0F ) +#endif +#ifndef CRUMB +# define CRUMB(b,p) (((b & (0x3 << p) ) >> p ) & 0xF) +#endif +#ifndef SWAP_NIBBLE +# define SWAP_NIBBLE(b) ( (NIBBLE_LOW(b)<< 4) | NIBBLE_HIGH(b)) +#endif int ukbhit(void); void AddLogLine(char *fileName, char *extData, char *c); @@ -53,7 +65,8 @@ char *sprint_hex_ascii(const uint8_t *data, const size_t len); void num_to_bytes(uint64_t n, size_t len, uint8_t* dest); uint64_t bytes_to_num(uint8_t* src, size_t len); -void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest); +void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest); +void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest); uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize); void SwapEndian64ex(const uint8_t *src, const size_t len, const uint8_t blockSize, uint8_t *dest); diff --git a/common/lfdemod.c b/common/lfdemod.c index 1c05920d..edebe456 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -95,7 +95,7 @@ size_t removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t p // by marshmellow // takes a array of binary values, length of bits per parity (includes parity bit), -// Parity Type (1 for odd; 0 for even; 2 Always 1's), and binary Length (length to run) +// Parity Type (1 for odd; 0 for even; 2 Always 1's; 3 Always 0's), and binary Length (length to run) size_t addParity(uint8_t *BitSource, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType) { uint32_t parityWd = 0; @@ -105,12 +105,16 @@ size_t addParity(uint8_t *BitSource, uint8_t *dest, uint8_t sourceLen, uint8_t p parityWd = (parityWd << 1) | BitSource[word+bit]; dest[j++] = (BitSource[word+bit]); } + // if parity fails then return 0 - if (pType == 2) { // then marker bit which should be a 1 - dest[j++]=1; - } else { - dest[j++] = parityTest(parityWd, pLen-1, pType) ^ 1; + switch (pType) { + case 3: dest[j++]=0; break; // marker bit which should be a 0 + case 2: dest[j++]=1; break; // marker bit which should be a 1 + default: + dest[j++] = parityTest(parityWd, pLen-1, pType) ^ 1; + break; } + bitCnt += pLen; parityWd = 0; } @@ -122,8 +126,7 @@ size_t addParity(uint8_t *BitSource, uint8_t *dest, uint8_t sourceLen, uint8_t p uint32_t bytebits_to_byte(uint8_t *src, size_t numbits) { uint32_t num = 0; - for(int i = 0 ; i < numbits ; i++) - { + for(int i = 0 ; i < numbits ; i++) { num = (num << 1) | (*src); src++; } -- 2.39.2