From d5e33fb20918d3dfb9e3707ec7a5a31994f158c1 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 26 Mar 2024 15:13:23 +0100 Subject: [PATCH] added some support functions --- client/src/util.c | 25 +++++++++++++++++++++++++ client/src/util.h | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/client/src/util.c b/client/src/util.c index d409d2292..577a790e9 100644 --- a/client/src/util.c +++ b/client/src/util.c @@ -1145,6 +1145,12 @@ void binstr_2_bytes(uint8_t *target, size_t *targetlen, const char *src) { } } +void hex_xor(uint8_t *d, uint8_t *x, int n) { + while(n--) { + d[n] ^= x[n]; + } +} + // return parity bit required to match type uint8_t GetParity(const uint8_t *bits, uint8_t type, int length) { int x; @@ -1474,3 +1480,22 @@ void sb_append_char(smartbuf *sb, unsigned char c) { sb->ptr[sb->idx] = c; sb->idx++; } + +uint8_t get_highest_frequency(const uint8_t *d, uint8_t n) { + + uint8_t frequency[256] = {0}; + uint8_t highest = 0; + uint8_t v = 0; + + // Count the frequency of each byte + for(uint8_t i = 0; i < n; i++) { + frequency[d[i]]++; + + if (frequency[d[i]] > highest) { + highest = frequency[d[i]]; + v = d[i]; + } + } + PrintAndLogEx(DEBUG, "highest occurance... %u xor byte... 0x%02X", highest, v); + return v; +} \ No newline at end of file diff --git a/client/src/util.h b/client/src/util.h index 226443dab..eedd409f8 100644 --- a/client/src/util.h +++ b/client/src/util.h @@ -133,6 +133,8 @@ int binstr_2_binarray(uint8_t *target, char *source, int length); void bytes_2_binstr(char *target, const uint8_t *source, size_t sourcelen); void binstr_2_bytes(uint8_t *target, size_t *targetlen, const char *src); +void hex_xor(uint8_t *d, uint8_t *x, int n); + uint8_t GetParity(const uint8_t *bits, uint8_t type, int length); void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length); void wiegand_add_parity_swapped(uint8_t *target, uint8_t *source, uint8_t length); @@ -179,4 +181,6 @@ struct smartbuf { size_t idx; } typedef smartbuf; void sb_append_char(smartbuf *sb, unsigned char c); + +uint8_t get_highest_frequency(const uint8_t *d, uint8_t n); #endif