|
8 | 8 | */ |
9 | 9 | #include "tomcrypt.h" |
10 | 10 |
|
| 11 | +#if _POSIX_C_SOURCE >= 200112L |
| 12 | +#include <libgen.h> |
| 13 | +#else |
| 14 | +#define basename(x) x |
| 15 | +#endif |
| 16 | + |
11 | 17 | /** |
12 | 18 | @file demo_crypt_constants.c |
13 | 19 |
|
|
17 | 23 | Larry Bugbee, February 2013 |
18 | 24 | */ |
19 | 25 |
|
| 26 | +static void _print_line(const char* cmd, const char* desc) |
| 27 | +{ |
| 28 | + printf(" %-16s - %s\n", cmd, desc); |
| 29 | +} |
20 | 30 |
|
21 | | -int main(void) { |
22 | | - /* given a specific constant name, get and print its value */ |
23 | | - char name[] = "CTR_COUNTER_BIG_ENDIAN"; |
24 | | - int value; |
25 | | - char *names_list; |
26 | | - unsigned int names_list_len; |
| 31 | +int main(int argc, char **argv) |
| 32 | +{ |
| 33 | + if (argc == 1) { |
| 34 | + /* given a specific constant name, get and print its value */ |
| 35 | + char name[] = "CTR_COUNTER_BIG_ENDIAN"; |
| 36 | + int value; |
| 37 | + char *names_list; |
| 38 | + unsigned int names_list_len; |
27 | 39 |
|
28 | | - if (crypt_get_constant(name, &value) != 0) |
29 | | - exit(EXIT_FAILURE); |
30 | | - printf("\n %s is %d \n\n", name, value); |
| 40 | + if (crypt_get_constant(name, &value) != 0) exit(EXIT_FAILURE); |
| 41 | + printf("\n %s is %d \n\n", name, value); |
31 | 42 |
|
32 | | - /* get and print the length of the names (and values) list */ |
| 43 | + /* get and print the length of the names (and values) list */ |
33 | 44 |
|
34 | | - if (crypt_list_all_constants(NULL, &names_list_len) != 0) |
35 | | - exit(EXIT_FAILURE); |
36 | | - printf(" need to allocate %u bytes \n\n", names_list_len); |
| 45 | + if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE); |
| 46 | + printf(" need to allocate %u bytes \n\n", names_list_len); |
37 | 47 |
|
38 | | - /* get and print the names (and values) list */ |
39 | | - if ((names_list = malloc(names_list_len)) == NULL) |
40 | | - exit(EXIT_FAILURE); |
41 | | - if (crypt_list_all_constants(names_list, &names_list_len) != 0) |
42 | | - exit(EXIT_FAILURE); |
43 | | - printf(" supported constants:\n\n%s\n\n", names_list); |
44 | | - free(names_list); |
| 48 | + /* get and print the names (and values) list */ |
| 49 | + if ((names_list = malloc(names_list_len)) == NULL) exit(EXIT_FAILURE); |
| 50 | + if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE); |
| 51 | + printf(" supported constants:\n\n%s\n\n", names_list); |
| 52 | + free(names_list); |
| 53 | + } else if (argc == 2) { |
| 54 | + if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { |
| 55 | + char* base = strdup(basename(argv[0])); |
| 56 | + printf("Usage: %s [-a] [-s name]\n\n", base); |
| 57 | + _print_line("<no argument>", "The old behavior of the demo"); |
| 58 | + _print_line("-a", "Only lists all constants"); |
| 59 | + _print_line("-s name", "List a single constant given as argument"); |
| 60 | + _print_line("-h", "The help you're looking at"); |
| 61 | + free(base); |
| 62 | + } else if (strcmp(argv[1], "-a") == 0) { |
| 63 | + char *names_list; |
| 64 | + unsigned int names_list_len; |
| 65 | + /* get and print the length of the names (and values) list */ |
| 66 | + if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE); |
| 67 | + /* get and print the names (and values) list */ |
| 68 | + names_list = malloc(names_list_len); |
| 69 | + if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE); |
| 70 | + printf("%s\n", names_list); |
| 71 | + } |
| 72 | + } else if (argc == 3) { |
| 73 | + if (strcmp(argv[1], "-s") == 0) { |
| 74 | + int value; |
| 75 | + if (crypt_get_constant(argv[2], &value) != 0) exit(EXIT_FAILURE); |
| 76 | + printf("%s,%u\n", argv[2], value); |
| 77 | + } |
| 78 | + } |
45 | 79 |
|
46 | | - return 0; |
| 80 | + return 0; |
47 | 81 | } |
48 | 82 |
|
49 | 83 |
|
|
0 commit comments