comparison libtomcrypt/src/misc/crypt/crypt_sizes.c @ 1711:e9dba7abd939

Merge libtomcrypt v1.18.2
author Matt Johnston <matt@ucc.asn.au>
date Wed, 10 Jun 2020 23:16:13 +0800
parents 6dba84798cd5
children
comparison
equal deleted inserted replaced
1710:1ff2a1034c52 1711:e9dba7abd939
305 * a -1 return value signifies insufficient space made available 305 * a -1 return value signifies insufficient space made available
306 */ 306 */
307 int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) { 307 int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
308 int i; 308 int i;
309 unsigned int total_len = 0; 309 unsigned int total_len = 0;
310 char number[32], *ptr; 310 char *ptr;
311 int number_len; 311 int number_len;
312 int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]); 312 int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
313 313
314 /* calculate amount of memory required for the list */ 314 /* calculate amount of memory required for the list */
315 for (i=0; i<count; i++) { 315 for (i=0; i<count; i++) {
316 total_len += (unsigned int)strlen(_crypt_sizes[i].name) + 1; 316 number_len = snprintf(NULL, 0, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
317 /* the above +1 is for the commas */ 317 if (number_len < 0)
318 number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
319 if ((number_len < 0) ||
320 ((unsigned int)number_len >= sizeof(number)))
321 return -1; 318 return -1;
322 total_len += (unsigned int)strlen(number) + 1; 319 total_len += number_len;
323 /* this last +1 is for newlines (and ending NULL) */ 320 /* this last +1 is for newlines (and ending NULL) */
324 } 321 }
325 322
326 if (names_list == NULL) { 323 if (names_list == NULL) {
327 *names_list_size = total_len; 324 *names_list_size = total_len;
330 return -1; 327 return -1;
331 } 328 }
332 /* build the names list */ 329 /* build the names list */
333 ptr = names_list; 330 ptr = names_list;
334 for (i=0; i<count; i++) { 331 for (i=0; i<count; i++) {
335 strcpy(ptr, _crypt_sizes[i].name); 332 number_len = snprintf(ptr, total_len, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
336 ptr += strlen(_crypt_sizes[i].name); 333 if (number_len < 0) return -1;
337 strcpy(ptr, ","); 334 if ((unsigned int)number_len > total_len) return -1;
338 ptr += 1; 335 total_len -= number_len;
339
340 number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
341 strcpy(ptr, number);
342 ptr += number_len; 336 ptr += number_len;
343 strcpy(ptr, "\n");
344 ptr += 1;
345 } 337 }
346 /* to remove the trailing new-line */ 338 /* to remove the trailing new-line */
347 ptr -= 1; 339 ptr -= 1;
348 *ptr = 0; 340 *ptr = 0;
349 } 341 }