Skip to content

Commit fe32e7e

Browse files
committed
Add LTC_ARRAY_SIZE() macro
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 8d44e48 commit fe32e7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+135
-129
lines changed

demos/hashsum.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
'\255')
3131
#define HEXOF(x) (x - s_base(x))
3232

33+
#ifndef LTC_ARRAY_SIZE
34+
#define LTC_ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
35+
#endif
36+
3337
static char* hashsum;
3438

3539
static void cleanup(void)
@@ -190,7 +194,7 @@ int main(int argc, char **argv)
190194
die(EXIT_FAILURE);
191195
}
192196

193-
for (x = 0; x < sizeof(idxs)/sizeof(idxs[0]); ++x) {
197+
for (x = 0; x < LTC_ARRAY_SIZE(idxs); ++x) {
194198
idxs[x] = -2;
195199
}
196200
argn = 1;
@@ -243,7 +247,7 @@ int main(int argc, char **argv)
243247
die(EXIT_FAILURE);
244248
}
245249
idx++;
246-
if ((size_t)idx >= sizeof(idxs)/sizeof(idxs[0])) {
250+
if ((size_t)idx >= LTC_ARRAY_SIZE(idxs)) {
247251
fprintf(stderr, "%s: Too many '-a' options chosen\n", hashsum);
248252
die(EXIT_FAILURE);
249253
}

demos/pem-info.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static const struct {
3434
static const char *s_map_cipher(const char *name)
3535
{
3636
unsigned long n;
37-
for (n = 0; n < sizeof(cipher_name_map)/sizeof(cipher_name_map[0]); ++n) {
37+
for (n = 0; n < LTC_ARRAY_SIZE(cipher_name_map); ++n) {
3838
if (strcmp(name, cipher_name_map[n].is) == 0)
3939
return cipher_name_map[n].should;
4040
}
@@ -61,7 +61,7 @@ static const char *s_map_mode(enum cipher_mode mode)
6161
{
6262
size_t n;
6363
mode &= cm_modes | cm_1bit | cm_8bit;
64-
for (n = 0; n < sizeof(cipher_mode_map)/sizeof(cipher_mode_map[0]); ++n) {
64+
for (n = 0; n < LTC_ARRAY_SIZE(cipher_mode_map); ++n) {
6565
if (cipher_mode_map[n].mode == mode)
6666
return cipher_mode_map[n].name;
6767
}

demos/timing.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static const struct {
658658

659659
if (ltc_mp.name == NULL) return;
660660

661-
for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
661+
for (x = 0; x < LTC_ARRAY_SIZE(groups); x++) {
662662
t2 = 0;
663663
for (y = 0; y < 4; y++) {
664664
t_start();
@@ -1408,7 +1408,7 @@ if (argc > 1) {
14081408
if (strstr(argv[1], "-h")) {
14091409
die(EXIT_SUCCESS);
14101410
} else if (strstr(argv[1], "-l")) {
1411-
for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
1411+
for (i = 0; i < LTC_ARRAY_SIZE(test_functions); ++i) {
14121412
printf("%s\n", test_functions[i].name);
14131413
}
14141414
exit(0);
@@ -1446,7 +1446,7 @@ if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT
14461446
/* single test name from commandline */
14471447
if (argc > 1) single_test = argv[1];
14481448

1449-
for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
1449+
for (i = 0; i < LTC_ARRAY_SIZE(test_functions); ++i) {
14501450
if (single_test && strstr(test_functions[i].name, single_test) == NULL) {
14511451
continue;
14521452
}

demos/tv_gen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ static void ccm_gen(void)
535535
nonce[z] = z;
536536
}
537537

538-
for (t = 0; t < sizeof(taglen)/sizeof(taglen[0]); ++t) {
538+
for (t = 0; t < LTC_ARRAY_SIZE(taglen); ++t) {
539539
for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
540540
for (z = 0; z < y1; z++) {
541541
plaintext[z] = (unsigned char)(z & 255);

src/ciphers/aes/aes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ int ECB_TEST(void)
669669
unsigned char tmp[2][16];
670670
int i, y;
671671

672-
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
672+
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
673673
zeromem(&key, sizeof(key));
674674
if ((err = rijndael_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
675675
return err;

src/ciphers/aes/aes_desc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ int AES_TEST(void)
189189
int y;
190190
#endif
191191

192-
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
192+
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
193193
zeromem(&key, sizeof(key));
194194
if ((err = AES_SETUP(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
195195
return err;

src/ciphers/aes/aesni.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ int aesni_test(void)
313313
unsigned char tmp[2][16];
314314
int i, y;
315315

316-
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
316+
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
317317
zeromem(&key, sizeof(key));
318318
if ((err = aesni_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
319319
return err;

src/ciphers/anubis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,7 @@ int anubis_test(void)
14981498
unsigned char buf[2][16];
14991499
symmetric_key skey;
15001500

1501-
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
1501+
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
15021502
anubis_setup(tests[x].key, tests[x].keylen, 0, &skey);
15031503
anubis_ecb_encrypt(tests[x].pt, buf[0], &skey);
15041504
anubis_ecb_decrypt(buf[0], buf[1], &skey);

src/ciphers/camellia.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ int camellia_test(void)
680680
int err;
681681
unsigned int x;
682682

683-
for (x = 0; x < sizeof(tests)/sizeof(tests[0]); x++) {
683+
for (x = 0; x < LTC_ARRAY_SIZE(tests); x++) {
684684
zeromem(&skey, sizeof(skey));
685685
if ((err = camellia_setup(tests[x].key, tests[x].keylen, 0, &skey)) != CRYPT_OK) {
686686
return err;

src/ciphers/des.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,7 @@ int des_test(void)
20182018
symmetric_key skey;
20192019
int i, err;
20202020

2021-
for (i = 0; i < (int)(sizeof(cases)/sizeof(cases[0])); i++)
2021+
for (i = 0; i < (int)LTC_ARRAY_SIZE(cases); i++)
20222022
{
20232023
if ((err = des_setup(cases[i].key, 8, 0, &skey)) != CRYPT_OK) {
20242024
return err;
@@ -2125,7 +2125,7 @@ int des3_test(void)
21252125
return err;
21262126
}
21272127

2128-
for (i = 0; i < (int)(sizeof(cases)/sizeof(cases[0])); i++)
2128+
for (i = 0; i < (int)LTC_ARRAY_SIZE(cases); i++)
21292129
{
21302130
if ((err = des3_setup(cases[i].key, 16, 0, &skey)) != CRYPT_OK) {
21312131
return err;

0 commit comments

Comments
 (0)