diff options
author | Nicholas Tay <nick@windblume.net> | 2022-01-13 23:19:28 +1100 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2022-01-13 23:19:28 +1100 |
commit | 1414a5667339637ca815dee52a527abb9bcfc528 (patch) | |
tree | ad99cc064060e5438e4e598da5911b7d8adec069 /passgen.c | |
parent | afd063a3ce9936f5eb1f5fd00fff6423f782994e (diff) | |
download | passgen-1414a5667339637ca815dee52a527abb9bcfc528.tar.gz passgen-1414a5667339637ca815dee52a527abb9bcfc528.tar.bz2 passgen-1414a5667339637ca815dee52a527abb9bcfc528.zip |
Use static buffers for regular password generation
This is probably a micro-optimisation, but since that is partly the
point of this random C project, let's do it :)
Diffstat (limited to '')
-rw-r--r-- | passgen.c | 38 |
1 files changed, 22 insertions, 16 deletions
@@ -86,18 +86,24 @@ unsigned int get_rng(void) return r; } +char grammar_buf[STATIC_BUFFER_SIZE + 1] = DEFAULT_GRAMMAR; +char password_buf[STATIC_BUFFER_SIZE + 1]; + char *build_grammar(int triplets, int specials, int numbers, int *grammar_size) { if (triplets < 1) { fprintf(stderr, "ERROR: Cannot have less than one triplet."); return NULL; } - *grammar_size = triplets * 3 + specials + numbers; - char *build = malloc(*grammar_size + 1); - if (build == NULL) { - perror("malloc"); - return NULL; + + char *build = grammar_buf; + if (*grammar_size > STATIC_BUFFER_SIZE) { + build = malloc(*grammar_size + 1); + if (build == NULL) { + perror("malloc"); + return NULL; + } } build[*grammar_size] = 0; @@ -113,10 +119,9 @@ char *build_grammar(int triplets, int specials, int numbers, int *grammar_size) int main(int argc, char *argv[]) { bool err = false; - bool custom_grammar = false; - char *grammar = DEFAULT_GRAMMAR; + char *grammar = grammar_buf; int grammar_size = sizeof(DEFAULT_GRAMMAR) - 1; - char *password = NULL; + char *password = password_buf; if (argc == 2) { /* Take first argument as the grammar */ @@ -132,14 +137,15 @@ int main(int argc, char *argv[]) err = true; goto cleanup; } - custom_grammar = true; } - password = malloc(grammar_size + 1); - if (password == NULL) { - perror("malloc"); - err = true; - goto cleanup; + if (grammar_size > STATIC_BUFFER_SIZE) { + password = malloc(grammar_size + 1); + if (password == NULL) { + perror("malloc"); + err = true; + goto cleanup; + } } password[grammar_size] = 0; @@ -179,9 +185,9 @@ cleanup: #ifdef USE_WINCRYPT CryptReleaseContext(win_rng, 0); #endif - if (custom_grammar) + if (grammar != grammar_buf) free(grammar); - if (password) + if (password != password_buf) free(password); if (err) |