mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 18:48:13 -07:00
move common util fcts to /common to avoid common files (e.g. crc) to depend on non-common files
This commit is contained in:
parent
8b99df9074
commit
4eaa2fc5aa
19 changed files with 155 additions and 209 deletions
89
common/commonutil.c
Normal file
89
common/commonutil.c
Normal file
|
@ -0,0 +1,89 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Jonathan Westhues, Sept 2005
|
||||
//
|
||||
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||
// the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Utility functions used in many places, not specific to any piece of code.
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "commonutil.h"
|
||||
|
||||
/*
|
||||
ref http://www.csm.ornl.gov/~dunigan/crc.html
|
||||
Returns the value v with the bottom b [0,32] bits reflected.
|
||||
Example: reflect(0x3e23L,3) == 0x3e26
|
||||
*/
|
||||
uint32_t reflect(uint32_t v, int b) {
|
||||
uint32_t t = v;
|
||||
for (int i = 0; i < b; ++i) {
|
||||
if (t & 1)
|
||||
v |= BITMASK((b - 1) - i);
|
||||
else
|
||||
v &= ~BITMASK((b - 1) - i);
|
||||
t >>= 1;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
uint8_t reflect8(uint8_t b) {
|
||||
return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
|
||||
}
|
||||
uint16_t reflect16(uint16_t b) {
|
||||
uint16_t v = 0;
|
||||
v |= (b & 0x8000) >> 15;
|
||||
v |= (b & 0x4000) >> 13;
|
||||
v |= (b & 0x2000) >> 11;
|
||||
v |= (b & 0x1000) >> 9;
|
||||
v |= (b & 0x0800) >> 7;
|
||||
v |= (b & 0x0400) >> 5;
|
||||
v |= (b & 0x0200) >> 3;
|
||||
v |= (b & 0x0100) >> 1;
|
||||
|
||||
v |= (b & 0x0080) << 1;
|
||||
v |= (b & 0x0040) << 3;
|
||||
v |= (b & 0x0020) << 5;
|
||||
v |= (b & 0x0010) << 7;
|
||||
v |= (b & 0x0008) << 9;
|
||||
v |= (b & 0x0004) << 11;
|
||||
v |= (b & 0x0002) << 13;
|
||||
v |= (b & 0x0001) << 15;
|
||||
return v;
|
||||
}
|
||||
|
||||
void num_to_bytes(uint64_t n, size_t len, uint8_t *dest) {
|
||||
while (len--) {
|
||||
dest[len] = (uint8_t) n;
|
||||
n >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t bytes_to_num(uint8_t *src, size_t len) {
|
||||
uint64_t num = 0;
|
||||
while (len--) {
|
||||
num = (num << 8) | (*src);
|
||||
src++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
// RotateLeft - Ultralight, Desfire
|
||||
void rol(uint8_t *data, const size_t len) {
|
||||
uint8_t first = data[0];
|
||||
for (size_t i = 0; i < len - 1; i++) {
|
||||
data[i] = data[i + 1];
|
||||
}
|
||||
data[len - 1] = first;
|
||||
}
|
||||
|
||||
void lsl(uint8_t *data, size_t len) {
|
||||
for (size_t n = 0; n < len - 1; n++) {
|
||||
data[n] = (data[n] << 1) | (data[n + 1] >> 7);
|
||||
}
|
||||
data[len - 1] <<= 1;
|
||||
}
|
||||
|
||||
int32_t le24toh(uint8_t data[3]) {
|
||||
return (data[2] << 16) | (data[1] << 8) | data[0];
|
||||
}
|
||||
|
55
common/commonutil.h
Normal file
55
common/commonutil.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Jonathan Westhues, Aug 2005
|
||||
//
|
||||
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||
// the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Utility functions used in many places, not specific to any piece of code.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef __COMMONUTIL_H
|
||||
#define __COMMONUTIL_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <inttypes.h>
|
||||
// endian change for 16bit
|
||||
#ifdef __GNUC__
|
||||
#ifndef BSWAP_16
|
||||
#define BSWAP_16(x) __builtin_bswap16(x)
|
||||
#endif
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
#ifndef BSWAP_16
|
||||
#define BSWAP_16(x) _byteswap_ushort(x)
|
||||
#endif
|
||||
#else
|
||||
#ifndef BSWAP_16
|
||||
# define BSWAP_16(x) ((( ((x) & 0xFF00 ) >> 8))| ( (((x) & 0x00FF) << 8)))
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef BITMASK
|
||||
# define BITMASK(X) (1 << (X))
|
||||
#endif
|
||||
#ifndef ARRAYLEN
|
||||
# define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0]))
|
||||
#endif
|
||||
|
||||
#ifndef NTIME
|
||||
# define NTIME(n) for (int _index = 0; _index < n; _index++)
|
||||
#endif
|
||||
|
||||
uint32_t reflect(uint32_t v, int b); // used in crc.c ...
|
||||
uint8_t reflect8(uint8_t b); // dedicated 8bit reversal
|
||||
uint16_t reflect16(uint16_t b); // dedicated 16bit reversal
|
||||
|
||||
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 rol(uint8_t *data, const size_t len);
|
||||
void lsl(uint8_t *data, size_t len);
|
||||
int32_t le24toh(uint8_t data[3]);
|
||||
|
||||
#endif
|
|
@ -10,7 +10,7 @@
|
|||
#define __CRC_H
|
||||
|
||||
#include "common.h" //stdint, stddef, stdbool
|
||||
#include "util.h" // reflect, bswap_16
|
||||
#include "commonutil.h" // reflect, bswap_16
|
||||
|
||||
typedef struct crc_ctx {
|
||||
uint32_t state;
|
||||
|
|
|
@ -10,7 +10,9 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "util.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "commonutil.h"
|
||||
|
||||
#define CRC16_POLY_CCITT 0x1021
|
||||
#define CRC16_POLY_LEGIC 0xc6c6 //0x6363
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
#include <stddef.h>
|
||||
#include "proxmark3.h"
|
||||
#include "apps.h"
|
||||
#include "util.h"
|
||||
#include "BigBuf.h"
|
||||
#include "mifare.h"
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
#include <stdlib.h> // for
|
||||
#include <stdbool.h> // for bool
|
||||
#include "parity.h" // for parity test
|
||||
#include "util.h" // for ARRAYLEN
|
||||
|
||||
//might not be high enough for noisy environments
|
||||
#define NOISE_AMPLITUDE_THRESHOLD 15
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
#ifndef __TEA_H
|
||||
#define __TEA_H
|
||||
|
||||
#include "util.h"
|
||||
#include "commonutil.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
void tea_encrypt(uint8_t *v, uint8_t *key);
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
#define __WIEGAND_H
|
||||
|
||||
#include "common.h"
|
||||
#include "util.h"
|
||||
|
||||
uint8_t getParity(uint8_t *bits, uint8_t len, uint8_t type);
|
||||
uint8_t checkParity(uint32_t bits, uint8_t len, uint8_t type);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue