Skip to content

Commit 9b5e03b

Browse files
committed
Improve register_{cipher,hash,prng}()
Only go once through `X_descriptor[]` when calling `register_X()` Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 9a1c288 commit 9b5e03b

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/misc/crypt/crypt_register_cipher.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,31 @@
1414
*/
1515
int register_cipher(const struct ltc_cipher_descriptor *cipher)
1616
{
17-
int x;
17+
int x, blank = -1;
1818

1919
LTC_ARGCHK(cipher != NULL);
2020

21+
if (cipher->name == NULL)
22+
return -1;
23+
2124
/* is it already registered? */
2225
LTC_MUTEX_LOCK(&ltc_cipher_mutex);
2326
for (x = 0; x < TAB_SIZE; x++) {
2427
if (cipher_descriptor[x].name != NULL && cipher_descriptor[x].ID == cipher->ID) {
2528
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
2629
return x;
2730
}
31+
if (cipher_descriptor[x].name == NULL && blank == -1) {
32+
blank = x;
33+
}
2834
}
2935

3036
/* find a blank spot */
31-
for (x = 0; x < TAB_SIZE; x++) {
32-
if (cipher_descriptor[x].name == NULL) {
33-
XMEMCPY(&cipher_descriptor[x], cipher, sizeof(struct ltc_cipher_descriptor));
34-
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
35-
return x;
36-
}
37+
if (blank != -1) {
38+
XMEMCPY(&cipher_descriptor[blank], cipher, sizeof(struct ltc_cipher_descriptor));
3739
}
3840

3941
/* no spot */
4042
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
41-
return -1;
43+
return blank;
4244
}

src/misc/crypt/crypt_register_hash.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,31 @@
1414
*/
1515
int register_hash(const struct ltc_hash_descriptor *hash)
1616
{
17-
int x;
17+
int x, blank = -1;
1818

1919
LTC_ARGCHK(hash != NULL);
2020

21+
if (hash->name == NULL)
22+
return -1;
23+
2124
/* is it already registered? */
2225
LTC_MUTEX_LOCK(&ltc_hash_mutex);
2326
for (x = 0; x < TAB_SIZE; x++) {
2427
if (XMEMCMP(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)) == 0) {
2528
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
2629
return x;
2730
}
31+
if (hash_descriptor[x].name == NULL && blank == -1) {
32+
blank = x;
33+
}
2834
}
2935

30-
/* find a blank spot */
31-
for (x = 0; x < TAB_SIZE; x++) {
32-
if (hash_descriptor[x].name == NULL) {
33-
XMEMCPY(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor));
34-
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
35-
return x;
36-
}
36+
/* fill in blank spot */
37+
if (blank != -1) {
38+
XMEMCPY(&hash_descriptor[blank], hash, sizeof(struct ltc_hash_descriptor));
3739
}
3840

3941
/* no spot */
4042
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
41-
return -1;
43+
return blank;
4244
}

src/misc/crypt/crypt_register_prng.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,31 @@
1414
*/
1515
int register_prng(const struct ltc_prng_descriptor *prng)
1616
{
17-
int x;
17+
int x, blank = -1;
1818

1919
LTC_ARGCHK(prng != NULL);
2020

21+
if (prng->name == NULL)
22+
return -1;
23+
2124
/* is it already registered? */
2225
LTC_MUTEX_LOCK(&ltc_prng_mutex);
2326
for (x = 0; x < TAB_SIZE; x++) {
2427
if (XMEMCMP(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)) == 0) {
2528
LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
2629
return x;
2730
}
31+
if (prng_descriptor[x].name == NULL && blank == -1) {
32+
blank = x;
33+
}
2834
}
2935

3036
/* find a blank spot */
31-
for (x = 0; x < TAB_SIZE; x++) {
32-
if (prng_descriptor[x].name == NULL) {
33-
XMEMCPY(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor));
34-
LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
35-
return x;
36-
}
37+
if (blank != -1) {
38+
XMEMCPY(&prng_descriptor[blank], prng, sizeof(struct ltc_prng_descriptor));
3739
}
3840

3941
/* no spot */
4042
LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
41-
return -1;
43+
return blank;
4244
}

0 commit comments

Comments
 (0)