added some support functions

This commit is contained in:
iceman1001 2024-03-26 15:13:23 +01:00
commit d5e33fb209
2 changed files with 29 additions and 0 deletions

View file

@ -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;
}

View file

@ -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