wiegand decode - added binary param to decode

This commit is contained in:
iceman1001 2021-03-29 17:38:20 +02:00
commit 8278c733fe
3 changed files with 48 additions and 7 deletions

View file

@ -114,28 +114,47 @@ int CmdWiegandDecode(const char *Cmd) {
void *argtable[] = { void *argtable[] = {
arg_param_begin, arg_param_begin,
arg_lit0("p", "parity", "ignore invalid parity"), arg_lit0("p", "parity", "ignore invalid parity"),
arg_strx1("r", "raw", "<hex>", "raw hex to be decoded"), arg_strx0("r", "raw", "<hex>", "raw hex to be decoded"),
arg_str0("b", "bin", "<bin>", "binary string to be decoded"),
arg_param_end arg_param_end
}; };
CLIExecWithReturn(ctx, Cmd, argtable, false); CLIExecWithReturn(ctx, Cmd, argtable, false);
bool ignore_parity = arg_get_lit(ctx, 1); bool ignore_parity = arg_get_lit(ctx, 1);
int len = 0; int hlen = 0;
char hex[40] = {0}; char hex[40] = {0};
CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)hex, sizeof(hex), &len); CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)hex, sizeof(hex), &hlen);
int blen = 0;
uint8_t binarr[100] = {0x00};
int res = CLIParamBinToBuf(arg_get_str(ctx, 3), binarr, sizeof(binarr), &blen);
CLIParserFree(ctx); CLIParserFree(ctx);
if (len == 0) { if (res) {
PrintAndLogEx(ERR, "empty input"); PrintAndLogEx(FAILED, "Error parsing binary string");
return PM3_EINVARG; return PM3_EINVARG;
} }
uint32_t top = 0, mid = 0, bot = 0; uint32_t top = 0, mid = 0, bot = 0;
hexstring_to_u96(&top, &mid, &bot, hex);
if (hlen) {
res = hexstring_to_u96(&top, &mid, &bot, hex);
if (res != hlen) {
PrintAndLogEx(ERR, "hex string contains none hex chars");
return PM3_EINVARG;
}
} else if (blen) {
uint16_t n = binarray_to_u96(&top, &mid, &bot, binarr, blen);
if (n != blen) {
PrintAndLogEx(ERR, "Binary string contains none <0|1> chars");
return PM3_EINVARG;
}
} else {
PrintAndLogEx(ERR, "empty input");
return PM3_EINVARG;
}
wiegand_message_t packed = initialize_message_object(top, mid, bot); wiegand_message_t packed = initialize_message_object(top, mid, bot);
HIDTryUnpack(&packed, ignore_parity); HIDTryUnpack(&packed, ignore_parity);
return PM3_SUCCESS; return PM3_SUCCESS;
} }

View file

@ -1041,6 +1041,27 @@ int binstring_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const char *str)
return i; return i;
} }
/**
* Converts a binary array to component "hi2", "hi" and "lo" 32-bit integers,
* one bit at a time.
*
* Returns the number of bits entered.
*/
int binarray_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, uint8_t *arr, int arrlen) {
int i = 0;
for(; i < arrlen; i++) {
uint8_t n = arr[i];
if (n > 1)
break;
*hi2 = (*hi2 << 1) | (*hi >> 31);
*hi = (*hi << 1) | (*lo >> 31);
*lo = (*lo << 1) | (n & 0x1);
}
return i;
}
inline uint32_t bitcount32(uint32_t a) { inline uint32_t bitcount32(uint32_t a) {
#if defined __GNUC__ #if defined __GNUC__
return __builtin_popcountl(a); return __builtin_popcountl(a);

View file

@ -107,6 +107,7 @@ char *str_dup(const char *src);
char *str_ndup(const char *src, size_t len); char *str_ndup(const char *src, size_t len);
int hexstring_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const char *str); int hexstring_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const char *str);
int binstring_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const char *str); int binstring_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const char *str);
int binarray_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, uint8_t *arr, int arrlen);
uint32_t bitcount32(uint32_t a); uint32_t bitcount32(uint32_t a);
uint64_t bitcount64(uint64_t a); uint64_t bitcount64(uint64_t a);