less hardcoded sizes and more ARRAYLEN

This commit is contained in:
Philippe Teuwen
2019-07-31 23:44:53 +02:00
parent 74288ad128
commit f276dca3f1
16 changed files with 43 additions and 53 deletions

View File

@@ -257,7 +257,6 @@ const APDUCode APDUCodeTable[] = {
{"9FXX", APDUCODE_TYPE_NONE, "Command successfully executed; 'xx' bytes of data are available and can be requested using GET RESPONSE."},
{"9XXX", APDUCODE_TYPE_NONE, "Application related status, (ISO 7816-3)"}
};
const size_t APDUCodeTableLen = sizeof(APDUCodeTable) / sizeof(APDUCode);
static int CodeCmp(const char *code1, const char *code2) {
int xsymb = 0;
@@ -279,12 +278,12 @@ static int CodeCmp(const char *code1, const char *code2) {
const APDUCode *GetAPDUCode(uint8_t sw1, uint8_t sw2) {
char buf[6] = {0};
int mineq = APDUCodeTableLen;
int mineq = ARRAYLEN(APDUCodeTable);
int mineqindx = 0;
sprintf(buf, "%02X%02X", sw1, sw2);
for (int i = 0; i < APDUCodeTableLen; i++) {
for (int i = 0; i < ARRAYLEN(APDUCodeTable); i++) {
int res = CodeCmp(APDUCodeTable[i].ID, buf);
// equal
@@ -300,7 +299,7 @@ const APDUCode *GetAPDUCode(uint8_t sw1, uint8_t sw2) {
}
// if we have not equal, but with some 'X'
if (mineqindx < APDUCodeTableLen) {
if (mineqindx < ARRAYLEN(APDUCodeTable)) {
return &APDUCodeTable[mineqindx];
}

View File

@@ -17,6 +17,7 @@
#include <config.h>
#endif
#include "commonutil.h"
#include "tlv.h"
#include "emv_tags.h"
@@ -437,7 +438,7 @@ static int emv_tlv_compare(const void *a, const void *b) {
}
static const struct emv_tag *emv_get_tag(const struct tlv *tlv) {
struct emv_tag *tag = bsearch(tlv, emv_tags, sizeof(emv_tags) / sizeof(emv_tags[0]),
struct emv_tag *tag = bsearch(tlv, emv_tags, ARRAYLEN(emv_tags),
sizeof(emv_tags[0]), emv_tlv_compare);
return tag ? tag : &emv_tags[0];

View File

@@ -18,7 +18,6 @@ static const char *PSElist [] = {
"325041592E5359532E4444463031", // 2PAY.SYS.DDF01 - Visa Proximity Payment System Environment - PPSE
"315041592E5359532E4444463031" // 1PAY.SYS.DDF01 - Visa Payment System Environment - PSE
};
//static const size_t PSElistLen = sizeof(PSElist)/sizeof(char*);
const char *TransactionTypeStr[] = {
"MSD",
@@ -119,7 +118,6 @@ static const TAIDList AIDlist [] = {
{ CV_OTHER, "D5780000021010" }, // Bankaxept Norway Bankaxept Norwegian domestic debit card
{ CV_OTHER, "F0000000030001" }, // BRADESCO - Brazilian Bank Banco Bradesco
};
static const size_t AIDlistLen = sizeof(AIDlist) / sizeof(TAIDList);
static bool APDULogging = false;
void SetAPDULogging(bool logging) {
@@ -133,7 +131,7 @@ enum CardPSVendor GetCardPSVendor(uint8_t *AID, size_t AIDlen) {
hex_to_buffer((uint8_t *)buf, AID, AIDlen, sizeof(buf) - 1, 0, 0, true);
for (int i = 0; i < AIDlistLen; i ++) {
for (int i = 0; i < ARRAYLEN(AIDlist); i ++) {
if (strncmp(AIDlist[i].aid, buf, strlen(AIDlist[i].aid)) == 0) {
return AIDlist[i].vendor;
}
@@ -530,7 +528,7 @@ int EMVSearch(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON,
int res = 0;
int retrycnt = 0;
for (int i = 0; i < AIDlistLen; i ++) {
for (int i = 0; i < ARRAYLEN(AIDlist); i ++) {
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);
// retry if error and not returned sw error

View File

@@ -57,10 +57,9 @@ static const ApplicationDataElm ApplicationData[] = {
{0x00, "end..."}
};
int ApplicationDataLen = sizeof(ApplicationData) / sizeof(ApplicationDataElm);
const char *GetApplicationDataName(tlv_tag_t tag) {
for (int i = 0; i < ApplicationDataLen; i++)
for (int i = 0; i < ARRAYLEN(ApplicationData); i++)
if (ApplicationData[i].Tag == tag)
return ApplicationData[i].Name;

View File

@@ -20,6 +20,7 @@
#include "../crypto.h"
#include "../dump.h"
#include "util_posix.h"
#include "commonutil.h"
#include <stdlib.h>
#include <string.h>
@@ -312,7 +313,7 @@ int exec_crypto_test(bool verbose) {
}
fprintf(stdout, "Crypto raw test: passed\n\n");
for (i = 0; i < sizeof(keylengths) / sizeof(keylengths[0]); i++) {
for (i = 0; i < ARRAYLEN(keylengths); i++) {
unsigned int kl = keylengths[i];
ret = test_genkey(kl, message, kl / 8, verbose);
if (ret) {