Skip to content

Commit daaab84

Browse files
authored
Merge pull request #263 from libtom/pr/crypt_sizes
missing items in crypt sizes
2 parents b5ab8ec + 6bbb450 commit daaab84

10 files changed

Lines changed: 706 additions & 154 deletions

File tree

demos/constants.c

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
*/
99
#include "tomcrypt.h"
1010

11+
#if _POSIX_C_SOURCE >= 200112L
12+
#include <libgen.h>
13+
#else
14+
#define basename(x) x
15+
#endif
16+
1117
/**
1218
@file demo_crypt_constants.c
1319
@@ -17,33 +23,61 @@
1723
Larry Bugbee, February 2013
1824
*/
1925

26+
static void _print_line(const char* cmd, const char* desc)
27+
{
28+
printf(" %-16s - %s\n", cmd, desc);
29+
}
2030

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;
2739

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);
3142

32-
/* get and print the length of the names (and values) list */
43+
/* get and print the length of the names (and values) list */
3344

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);
3747

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+
}
4579

46-
return 0;
80+
return 0;
4781
}
4882

4983

0 commit comments

Comments
 (0)