diff options
author | Nicholas Tay <nick@windblume.net> | 2022-01-14 00:04:38 +1100 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2022-01-14 00:04:38 +1100 |
commit | 42bd03b610153c4679e29001b859a1d783f9db69 (patch) | |
tree | 316415c1b8960dcb22cb28e56eeb1afce64d7f58 /passgen.c | |
parent | fb4cc1accdf41204978beef6f7c9e2bdf4acbb54 (diff) | |
download | passgen-42bd03b610153c4679e29001b859a1d783f9db69.tar.gz passgen-42bd03b610153c4679e29001b859a1d783f9db69.tar.bz2 passgen-42bd03b610153c4679e29001b859a1d783f9db69.zip |
Split out classes into an array of custom struct
Would allow us to access in --help. Also just seems like a nicer way to
store in case we need to extend it.
Diffstat (limited to 'passgen.c')
-rw-r--r-- | passgen.c | 35 |
1 files changed, 24 insertions, 11 deletions
@@ -37,11 +37,21 @@ #endif -#define CLASS(c, chars) \ - case c: \ - class = chars; \ - class_size = sizeof(chars)-1; \ - break; +#define CLASS(ch, chars) \ + { \ + .c = ch, \ + .letters = chars, \ + .size = sizeof(chars) - 1, \ + }, +struct grammar_class { + char c; + char *letters; + int size; +}; +struct grammar_class classes[] = { + CLASSES +}; +int const classes_n = sizeof(classes) / sizeof(classes[0]); #ifdef USE_WINCRYPT @@ -175,18 +185,21 @@ Compile-time options:\n\ c += 'a' - 'A'; } - char *class; - int class_size = 0; - switch (c) { - CLASSES - default: + struct grammar_class *class = NULL; + for (int j = 0; j < classes_n; ++j) { + if (c == classes[j].c) { + class = &classes[j]; + break; + } + } + if (class == NULL) { fprintf(stderr, "ERROR: Invalid grammar character '%c'.\n", c); err = true; goto cleanup; } do { - password[i] = class[get_rng() % class_size] - (caps ? 'a' - 'A' : 0); + password[i] = class->letters[get_rng() % class->size] - (caps ? 'a' - 'A' : 0); } while (i != 0 && password[i] == password[i - 1]); } |