Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion demos/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ int main(int argc, char **argv)
/* get and print the length of the names (and values) list */
if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE);
/* get and print the names (and values) list */
names_list = malloc(names_list_len);
if ((names_list = malloc(names_list_len)) == NULL) exit(EXIT_FAILURE);
if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE);
printf("%s\n", names_list);
free(names_list);
}
} else if (argc == 3) {
if (strcmp(argv[1], "-s") == 0) {
Expand Down
6 changes: 4 additions & 2 deletions demos/sizes.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ int main(int argc, char **argv)
printf(" need to allocate %u bytes \n\n", sizes_list_len);

/* get and print the names (and sizes) list */
sizes_list = malloc(sizes_list_len);
if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
printf(" supported sizes:\n\n%s\n\n", sizes_list);
free(sizes_list);
} else if (argc == 2) {
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
char* base = strdup(basename(argv[0]));
Expand All @@ -60,9 +61,10 @@ int main(int argc, char **argv)
/* get and print the length of the names (and sizes) list */
if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
/* get and print the names (and sizes) list */
sizes_list = malloc(sizes_list_len);
if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
printf("%s\n", sizes_list);
free(sizes_list);
}
} else if (argc == 3) {
if (strcmp(argv[1], "-s") == 0) {
Expand Down
25 changes: 8 additions & 17 deletions src/misc/crypt/crypt_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,16 @@ int crypt_get_constant(const char* namein, int *valueout) {
int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
int i;
unsigned int total_len = 0;
char number[32], *ptr;
char *ptr;
int number_len;
int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);

/* calculate amount of memory required for the list */
for (i=0; i<count; i++) {
total_len += (unsigned int)strlen(_crypt_constants[i].name) + 1;
/* the above +1 is for the commas */
number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
if ((number_len < 0) ||
((unsigned int)number_len >= sizeof(number)))
number_len = snprintf(NULL, 0, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
if (number_len < 0)
return -1;
total_len += number_len + 1;
/* this last +1 is for newlines (and ending NULL) */
total_len += number_len;
}

if (names_list == NULL) {
Expand All @@ -283,16 +279,11 @@ int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
/* build the names list */
ptr = names_list;
for (i=0; i<count; i++) {
strcpy(ptr, _crypt_constants[i].name);
ptr += strlen(_crypt_constants[i].name);
strcpy(ptr, ",");
ptr += 1;

number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
strcpy(ptr, number);
number_len = snprintf(ptr, total_len, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
if (number_len < 0) return -1;
if ((unsigned int)number_len > total_len) return -1;
total_len -= number_len;
ptr += number_len;
strcpy(ptr, "\n");
ptr += 1;
}
/* to remove the trailing new-line */
ptr -= 1;
Expand Down
24 changes: 8 additions & 16 deletions src/misc/crypt/crypt_sizes.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,16 @@ int crypt_get_size(const char* namein, unsigned int *sizeout) {
int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
int i;
unsigned int total_len = 0;
char number[32], *ptr;
char *ptr;
int number_len;
int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);

/* calculate amount of memory required for the list */
for (i=0; i<count; i++) {
total_len += (unsigned int)strlen(_crypt_sizes[i].name) + 1;
/* the above +1 is for the commas */
number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
if ((number_len < 0) ||
((unsigned int)number_len >= sizeof(number)))
number_len = snprintf(NULL, 0, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
if (number_len < 0)
return -1;
total_len += (unsigned int)strlen(number) + 1;
total_len += number_len;
/* this last +1 is for newlines (and ending NULL) */
}

Expand All @@ -344,16 +341,11 @@ int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
/* build the names list */
ptr = names_list;
for (i=0; i<count; i++) {
strcpy(ptr, _crypt_sizes[i].name);
ptr += strlen(_crypt_sizes[i].name);
strcpy(ptr, ",");
ptr += 1;

number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
strcpy(ptr, number);
number_len = snprintf(ptr, total_len, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
if (number_len < 0) return -1;
if ((unsigned int)number_len > total_len) return -1;
total_len -= number_len;
ptr += number_len;
strcpy(ptr, "\n");
ptr += 1;
}
/* to remove the trailing new-line */
ptr -= 1;
Expand Down