|
6 | 6 | * The library is free for all purposes without any express |
7 | 7 | * guarantee it works. |
8 | 8 | */ |
| 9 | + |
9 | 10 | #include "tomcrypt.h" |
10 | 11 |
|
| 12 | +#if _POSIX_C_SOURCE >= 200112L |
| 13 | +#include <libgen.h> |
| 14 | +#else |
| 15 | +#define basename(x) x |
| 16 | +#endif |
11 | 17 | /** |
12 | 18 | @file demo_crypt_sizes.c |
13 | 19 |
|
14 | 20 | Demo how to get various sizes to dynamic languages |
15 | 21 | like Python - Larry Bugbee, February 2013 |
16 | 22 | */ |
17 | 23 |
|
| 24 | +static void _print_line(const char* cmd, const char* desc) |
| 25 | +{ |
| 26 | + printf(" %-16s - %s\n", cmd, desc); |
| 27 | +} |
| 28 | + |
| 29 | +int main(int argc, char **argv) |
| 30 | +{ |
| 31 | + if (argc == 1) { |
| 32 | + /* given a specific size name, get and print its size */ |
| 33 | + char name[] = "ltc_hash_descriptor"; |
| 34 | + unsigned int size; |
| 35 | + char *sizes_list; |
| 36 | + unsigned int sizes_list_len; |
| 37 | + if (crypt_get_size(name, &size) != 0) exit(EXIT_FAILURE); |
| 38 | + printf("\n size of '%s' is %u \n\n", name, size); |
| 39 | + |
| 40 | + /* get and print the length of the names (and sizes) list */ |
| 41 | + if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE); |
| 42 | + printf(" need to allocate %u bytes \n\n", sizes_list_len); |
18 | 43 |
|
19 | | -int main(void) { |
20 | | - |
21 | | - /* given a specific size name, get and print its size */ |
22 | | - char name[] = "ltc_hash_descriptor"; |
23 | | - unsigned int size; |
24 | | - char *sizes_list; |
25 | | - unsigned int sizes_list_len; |
26 | | - if(crypt_get_size(name, &size) != 0) |
27 | | - exit(EXIT_FAILURE); |
28 | | - printf("\n size of '%s' is %u \n\n", name, size); |
29 | | - |
30 | | - /* get and print the length of the names (and sizes) list */ |
31 | | - if(crypt_list_all_sizes(NULL, &sizes_list_len) != 0) |
32 | | - exit(EXIT_FAILURE); |
33 | | - printf(" need to allocate %u bytes \n\n", sizes_list_len); |
34 | | - |
35 | | - /* get and print the names (and sizes) list */ |
36 | | - sizes_list = malloc(sizes_list_len); |
37 | | - if(crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) |
38 | | - exit(EXIT_FAILURE); |
39 | | - printf(" supported sizes:\n\n%s\n\n", sizes_list); |
40 | | - return 0; |
| 44 | + /* get and print the names (and sizes) list */ |
| 45 | + sizes_list = malloc(sizes_list_len); |
| 46 | + if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE); |
| 47 | + printf(" supported sizes:\n\n%s\n\n", sizes_list); |
| 48 | + } else if (argc == 2) { |
| 49 | + if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { |
| 50 | + char* base = strdup(basename(argv[0])); |
| 51 | + printf("Usage: %s [-a] [-s name]\n\n", base); |
| 52 | + _print_line("<no argument>", "The old behavior of the demo"); |
| 53 | + _print_line("-a", "Only lists all sizes"); |
| 54 | + _print_line("-s name", "List a single size given as argument"); |
| 55 | + _print_line("-h", "The help you're looking at"); |
| 56 | + free(base); |
| 57 | + } else if (strcmp(argv[1], "-a") == 0) { |
| 58 | + char *sizes_list; |
| 59 | + unsigned int sizes_list_len; |
| 60 | + /* get and print the length of the names (and sizes) list */ |
| 61 | + if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE); |
| 62 | + /* get and print the names (and sizes) list */ |
| 63 | + sizes_list = malloc(sizes_list_len); |
| 64 | + if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE); |
| 65 | + printf("%s\n", sizes_list); |
| 66 | + } |
| 67 | + } else if (argc == 3) { |
| 68 | + if (strcmp(argv[1], "-s") == 0) { |
| 69 | + unsigned int size; |
| 70 | + if (crypt_get_size(argv[2], &size) != 0) exit(EXIT_FAILURE); |
| 71 | + printf("%s,%u\n", argv[2], size); |
| 72 | + } |
| 73 | + } |
| 74 | + return 0; |
41 | 75 | } |
42 | 76 |
|
43 | 77 | /* ref: $Format:%D$ */ |
|
0 commit comments