cppcheck reduce variable scopes

This commit is contained in:
Philippe Teuwen committed 2021-09-05 23:42:14 +02:00
1 parent d37547917e
commit 9f36ce1b9d
15 files changed
+102 -140

No files matched your search

+17 -26
View File
@@ -539,10 +539,8 @@ int CmdEM4x05Dump(const char *Cmd) {
uint32_t data[16];
int success = PM3_SUCCESS;
int status, status14, status15;
uint32_t lock_bits = 0x00; // no blocks locked
bool gotLockBits = false;
bool lockInPW2 = false;
uint32_t word = 0;
const char *info[] = {"Info/User", "UID", "Password", "User", "Config", "User", "User", "User", "User", "User", "User", "User", "User", "User", "Lock", "Lock"};
@@ -555,7 +553,7 @@ int CmdEM4x05Dump(const char *Cmd) {
if (usePwd) {
// Test first if the password is correct
status = em4x05_login_ext(pwd);
int status = em4x05_login_ext(pwd);
if (status == PM3_SUCCESS) {
PrintAndLogEx(INFO, "password is " _GREEN_("correct"));
} else if (status == PM3_EFAILED) {
@@ -572,11 +570,10 @@ int CmdEM4x05Dump(const char *Cmd) {
PrintAndLogEx(INFO, "-----+----------+-------+---+-----");
if (card_type == EM_4205 || card_type == EM_4305 || card_type == EM_UNKNOWN) {
bool lockInPW2 = false;
// To flag any blocks locked we need to read blocks 14 and 15 first
// dont swap endian until we get block lock flags.
status14 = em4x05_read_word_ext(EM4305_PROT1_BLOCK, pwd, usePwd, &word);
int status14 = em4x05_read_word_ext(EM4305_PROT1_BLOCK, pwd, usePwd, &word);
if (status14 == PM3_SUCCESS) {
if ((word & 0x00008000) != 0x00) {
lock_bits = word;
@@ -586,7 +583,7 @@ int CmdEM4x05Dump(const char *Cmd) {
} else {
success = PM3_ESOFT; // If any error ensure fail is set so not to save invalid data
}
status15 = em4x05_read_word_ext(EM4305_PROT2_BLOCK, pwd, usePwd, &word);
int status15 = em4x05_read_word_ext(EM4305_PROT2_BLOCK, pwd, usePwd, &word);
if (status15 == PM3_SUCCESS) {
if ((word & 0x00008000) != 0x00) { // assume block 15 is the current lock block
lock_bits = word;
@@ -612,7 +609,7 @@ int CmdEM4x05Dump(const char *Cmd) {
}
} else {
// success &= em4x05_read_word_ext(addr, pwd, usePwd, &word);
status = em4x05_read_word_ext(addr, pwd, usePwd, &word); // Get status for single read
int status = em4x05_read_word_ext(addr, pwd, usePwd, &word); // Get status for single read
if (status != PM3_SUCCESS)
success = PM3_ESOFT; // If any error ensure fail is set so not to save invalid data
data[addr] = BSWAP_32(word);
@@ -647,7 +644,7 @@ int CmdEM4x05Dump(const char *Cmd) {
// To flag any blocks locked we need to read block 3 first
// dont swap endian until we get block lock flags.
status14 = em4x05_read_word_ext(EM4469_PROT_BLOCK, pwd, usePwd, &word);
int status14 = em4x05_read_word_ext(EM4469_PROT_BLOCK, pwd, usePwd, &word);
if (status14 == PM3_SUCCESS) {
lock_bits = word;
gotLockBits = true;
@@ -656,10 +653,8 @@ int CmdEM4x05Dump(const char *Cmd) {
success = PM3_ESOFT; // If any error ensure fail is set so not to save invalid data
}
uint32_t lockbit;
for (; addr < 16; addr++) {
lockbit = (lock_bits >> (addr * 2)) & 3;
uint32_t lockbit = (lock_bits >> (addr * 2)) & 3;
if (addr == 2) {
if (usePwd) {
data[addr] = BSWAP_32(pwd);
@@ -671,7 +666,7 @@ int CmdEM4x05Dump(const char *Cmd) {
}
} else {
status = em4x05_read_word_ext(addr, pwd, usePwd, &word);
int status = em4x05_read_word_ext(addr, pwd, usePwd, &word);
if (status != PM3_SUCCESS) {
success = PM3_ESOFT; // If any error ensure fail is set so not to save invalid data
}
@@ -1970,9 +1965,6 @@ uint32_t static em4x05_Sniff_GetBlock(char *bits, bool fwd) {
int CmdEM4x05Sniff(const char *Cmd) {
bool pwd = false, fwd = false;
bool haveData, sampleData = true;
CLIParserContext *ctx;
CLIParserInit(&ctx, "lf em 4x05 sniff",
"Sniff EM4x05 commands sent from a programmer",
@@ -1988,8 +1980,8 @@ int CmdEM4x05Sniff(const char *Cmd) {
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
sampleData = (arg_get_lit(ctx, 1) == false);
fwd = arg_get_lit(ctx, 2);
bool sampleData = (arg_get_lit(ctx, 1) == false);
bool fwd = arg_get_lit(ctx, 2);
CLIParserFree(ctx);
char cmdText[100];
@@ -1999,9 +1991,7 @@ int CmdEM4x05Sniff(const char *Cmd) {
int i, bitidx;
int ZeroWidth; // 32-42 "1" is 32
int CycleWidth;
size_t idx = 0, pulseSamples, pktOffset;
uint32_t tmpValue;
bool eop = false;
size_t pulseSamples;
// setup and sample data from Proxmark
// if not directed to existing sample/graphbuffer
@@ -2019,15 +2009,16 @@ int CmdEM4x05Sniff(const char *Cmd) {
PrintAndLogEx(SUCCESS, "offset | Command | Data | blk | raw");
PrintAndLogEx(SUCCESS, "-------+-------------+----------+-----+------------------------------------------------------------");
idx = 0;
size_t idx = 0;
// loop though sample buffer
while (idx < g_GraphTraceLen) {
eop = false;
haveData = false;
pwd = false;
bool eop = false;
bool haveData = false;
bool pwd = false;
uint32_t tmpValue;
idx = em4x05_Sniff_GetNextBitStart(idx, g_GraphTraceLen, g_GraphBuffer, &pulseSamples);
pktOffset = idx;
size_t pktOffset = idx;
if (pulseSamples >= 10) { // Should be 18 so a bit less to allow for processing
// Use first bit to get "0" bit samples as a reference
+39 -39
View File
@@ -46,7 +46,7 @@ static uint8_t isEven_64_63(const uint8_t *data) { // 8
//NEDAP demod - ASK/Biphase (or Diphase), RF/64 with preamble of 1111111110 (always a 128 bit data stream)
int demodNedap(bool verbose) {
(void) verbose; // unused so far
uint8_t data[16], buffer[7], r0, r1, r2, r3, r4, r5, idxC1, idxC2, idxC3, idxC4, idxC5, fixed0, fixed1, unk1, unk2, subtype; // 4 bits
uint8_t data[16], buffer[7], subtype; // 4 bits
size_t size, offset = 0;
uint16_t checksum, customerCode; // 12 bits
uint32_t badgeId; // max 99999
@@ -118,19 +118,19 @@ int demodNedap(bool verbose) {
ret = PM3_ESOFT;
}
idxC1 = invTranslateTable[(data[3] & 0x1e) >> 1];
idxC2 = invTranslateTable[(data[4] & 0x1e) >> 1];
idxC3 = invTranslateTable[(data[5] & 0x1e) >> 1];
idxC4 = invTranslateTable[(data[6] & 0x1e) >> 1];
idxC5 = invTranslateTable[(data[7] & 0x1e) >> 1];
uint8_t idxC1 = invTranslateTable[(data[3] & 0x1e) >> 1];
uint8_t idxC2 = invTranslateTable[(data[4] & 0x1e) >> 1];
uint8_t idxC3 = invTranslateTable[(data[5] & 0x1e) >> 1];
uint8_t idxC4 = invTranslateTable[(data[6] & 0x1e) >> 1];
uint8_t idxC5 = invTranslateTable[(data[7] & 0x1e) >> 1];
// validation
if ((idxC1 != 0xFF) && (idxC2 != 0xFF) && (idxC3 != 0xFF) && (idxC4 != 0xFF) && (idxC5 != 0xFF)) {
r1 = idxC1;
r2 = ((10 + idxC2) - (idxC1 + 1)) % 10;
r3 = ((10 + idxC3) - (idxC2 + 1)) % 10;
r4 = ((10 + idxC4) - (idxC3 + 1)) % 10;
r5 = ((10 + idxC5) - (idxC4 + 1)) % 10;
uint8_t r1 = idxC1;
uint8_t r2 = ((10 + idxC2) - (idxC1 + 1)) % 10;
uint8_t r3 = ((10 + idxC3) - (idxC2 + 1)) % 10;
uint8_t r4 = ((10 + idxC4) - (idxC3 + 1)) % 10;
uint8_t r5 = ((10 + idxC5) - (idxC4 + 1)) % 10;
badgeId = r1 * 10000 + r2 * 1000 + r3 * 100 + r4 * 10 + r5;
@@ -171,18 +171,18 @@ int demodNedap(bool verbose) {
}
//
r4 = (data[8] >> 3) & 0x0F;
r5 = ((data[8] << 1) & 0x0F) | (data[9] >> 7);
r2 = (data[9] >> 2) & 0x0F;
r3 = ((data[9] << 2) & 0x0F) | (data[10] >> 6);
r0 = ((data[10] >> 1) & 0x0F);
r1 = ((data[10] << 3) & 0x0F) | (data[11] >> 5);
uint8_t r4 = (data[8] >> 3) & 0x0F;
uint8_t r5 = ((data[8] << 1) & 0x0F) | (data[9] >> 7);
uint8_t r2 = (data[9] >> 2) & 0x0F;
uint8_t r3 = ((data[9] << 2) & 0x0F) | (data[10] >> 6);
uint8_t r0 = ((data[10] >> 1) & 0x0F);
uint8_t r1 = ((data[10] << 3) & 0x0F) | (data[11] >> 5);
fixed0 = ((data[11] << 4) & 0xF0) | (data[12] >> 4);
fixed1 = ((data[12] << 5) & 0xE0) | (data[13] >> 3);
uint8_t fixed0 = ((data[11] << 4) & 0xF0) | (data[12] >> 4);
uint8_t fixed1 = ((data[12] << 5) & 0xE0) | (data[13] >> 3);
unk1 = ((data[13] << 6) & 0xC0) | (data[14] >> 2);
unk2 = ((data[14] << 7) & 0xC0) | (data[15] >> 1);
uint8_t unk1 = ((data[13] << 6) & 0xC0) | (data[14] >> 2);
uint8_t unk2 = ((data[14] << 7) & 0xC0) | (data[15] >> 1);
// validation 2
if (!r0 && (r1 < 10) && (r2 < 10) && (r3 < 10) && (r4 < 10) && (r5 < 10)) {
@@ -293,21 +293,20 @@ static int CmdLFNedapReader(const char *Cmd) {
}
static void NedapGen(uint8_t subType, uint16_t customerCode, uint32_t id, bool isLong, uint8_t *data) { // 8 or 16
uint8_t buffer[7], r1, r2, r3, r4, r5, idxC1, idxC2, idxC3, idxC4, idxC5, i, tmp, carry, id2, id1, id0;
uint16_t checksum;
uint8_t buffer[7];
r1 = (uint8_t)(id / 10000);
r2 = (uint8_t)((id % 10000) / 1000);
r3 = (uint8_t)((id % 1000) / 100);
r4 = (uint8_t)((id % 100) / 10);
r5 = (uint8_t)(id % 10);
uint8_t r1 = (uint8_t)(id / 10000);
uint8_t r2 = (uint8_t)((id % 10000) / 1000);
uint8_t r3 = (uint8_t)((id % 1000) / 100);
uint8_t r4 = (uint8_t)((id % 100) / 10);
uint8_t r5 = (uint8_t)(id % 10);
// first part
idxC1 = r1;
idxC2 = (idxC1 + 1 + r2) % 10;
idxC3 = (idxC2 + 1 + r3) % 10;
idxC4 = (idxC3 + 1 + r4) % 10;
idxC5 = (idxC4 + 1 + r5) % 10;
uint8_t idxC1 = r1;
uint8_t idxC2 = (idxC1 + 1 + r2) % 10;
uint8_t idxC3 = (idxC2 + 1 + r3) % 10;
uint8_t idxC4 = (idxC3 + 1 + r4) % 10;
uint8_t idxC5 = (idxC4 + 1 + r5) % 10;
buffer[0] = 0xc0 | (subType & 0x0F);
buffer[1] = (customerCode & 0x0FF0) >> 4;
@@ -317,7 +316,7 @@ static void NedapGen(uint8_t subType, uint16_t customerCode, uint32_t id, bool i
// checksum
init_table(CRC_XMODEM);
checksum = crc16_xmodem(buffer, 5);
uint16_t checksum = crc16_xmodem(buffer, 5);
buffer[6] = ((checksum & 0x000F) << 4) | (buffer[4] & 0x0F);
buffer[5] = (checksum & 0x00F0) | ((buffer[4] & 0xF0) >> 4);
@@ -325,8 +324,9 @@ static void NedapGen(uint8_t subType, uint16_t customerCode, uint32_t id, bool i
buffer[3] = ((checksum & 0xF000) >> 8) | ((buffer[3] & 0xF0) >> 4);
// carry calc
for (i = 0, carry = 0; i < sizeof(buffer); i++) {
tmp = buffer[sizeof(buffer) - 1 - i];
uint8_t carry = 0;
for (uint8_t i = 0; i < sizeof(buffer); i++) {
uint8_t tmp = buffer[sizeof(buffer) - 1 - i];
data[7 - i] = ((tmp & 0x7F) << 1) | carry;
carry = (tmp & 0x80) >> 7;
}
@@ -335,9 +335,9 @@ static void NedapGen(uint8_t subType, uint16_t customerCode, uint32_t id, bool i
// second part
if (isLong) {
id0 = r1;
id1 = (r2 << 4) | r3;
id2 = (r4 << 4) | r5;
uint8_t id0 = r1;
uint8_t id1 = (r2 << 4) | r3;
uint8_t id2 = (r4 << 4) | r5;
data[8] = (id2 >> 1);
data[9] = ((id2 & 0x01) << 7) | (id1 >> 2);
+2 -3
View File
@@ -4047,7 +4047,6 @@ static int CmdT55xxSniff(const char *Cmd) {
int minWidth = 1000;
int maxWidth = 0;
uint16_t dataLen = 0;
data[0] = 0;
bool have_data = false;
sprintf(modeText, "Default");
@@ -4125,7 +4124,7 @@ static int CmdT55xxSniff(const char *Cmd) {
// Fixed bit - Default
if (have_data == false && (APPROX_EQ(pulseBuffer[0], maxWidth, tolerance))) {
dataLen = t55sniff_get_packet(pulseBuffer, data, minWidth, maxWidth, tolerance);
uint16_t dataLen = t55sniff_get_packet(pulseBuffer, data, minWidth, maxWidth, tolerance);
// if ((dataLen == 39) )
// printf ("Fixed | Data end of 80 samples | offset : %llu - datalen %-2d - data : %s --- - Bit 0 width : %d\n",idx,dataLen,data,pulseBuffer[0]);
@@ -4209,7 +4208,7 @@ static int CmdT55xxSniff(const char *Cmd) {
if (have_data == false && (APPROX_EQ(pulseBuffer[0], minWidth, tolerance))) {
// leading 0 (should = 0 width)
// 1 of 4 (leads with 00)
dataLen = t55sniff_get_packet(pulseBuffer, data, minWidth, maxWidth, tolerance);
uint16_t dataLen = t55sniff_get_packet(pulseBuffer, data, minWidth, maxWidth, tolerance);
// **** Should check to 0 to be actual 0 as well i.e. 01 .... data ....
if ((data[0] == '0') && (data[1] == '1')) {
if (dataLen == 73) {
+2 -4
View File
@@ -389,10 +389,9 @@ int EMVSearchPSE(Iso7816CommandChannel channel, bool ActivateField, bool LeaveFi
return 1;
}
bool fileFound = false;
struct tlvdb *t = tlvdb_parse_multi(data, datalen);
if (t) {
bool fileFound = false;
// PSE/PPSE with SFI
struct tlvdb *tsfi = tlvdb_find_path(t, (tlv_tag_t[]) {0x6f, 0xa5, 0x88, 0x00});
if (tsfi) {
@@ -473,7 +472,6 @@ int EMVSearch(Iso7816CommandChannel channel, bool ActivateField, bool LeaveField
size_t datalen = 0;
uint16_t sw = 0;
int res = 0;
int retrycnt = 0;
for (int i = 0; i < ARRAYLEN(AIDlist); i ++) {
@@ -483,7 +481,7 @@ int EMVSearch(Iso7816CommandChannel channel, bool ActivateField, bool LeaveField
}
param_gethex_to_eol(AIDlist[i].aid, 0, aidbuf, sizeof(aidbuf), &aidlen);
res = EMVSelect(channel, (i == 0) ? ActivateField : false, true, aidbuf, aidlen, data, sizeof(data), &datalen, &sw, tlv);
int res = EMVSelect(channel, (i == 0) ? ActivateField : false, true, aidbuf, aidlen, data, sizeof(data), &datalen, &sw, tlv);
// retry if error and not returned sw error
if (res && res != 5) {
if (++retrycnt < 3) {
+1 -2
View File
@@ -329,10 +329,9 @@ static void *bf_thread(void *thread_arg) {
memcpy(bytes_to_recover, targ->bytes_to_recover, sizeof(bytes_to_recover));
memcpy(keytable, targ->keytable, sizeof(keytable));
int found;
while (!(brute & endmask)) {
found = __atomic_load_n(&loclass_found, __ATOMIC_SEQ_CST);
int found = __atomic_load_n(&loclass_found, __ATOMIC_SEQ_CST);
if (found != 0xFF) return NULL;