comparison common-algo.c @ 1303:eed9376a4ad6

improve algorithm list parsing
author Matt Johnston <matt@ucc.asn.au>
date Mon, 11 Jul 2016 22:40:38 +0800
parents 139935236c72
children 2c9dac2d6707
comparison
equal deleted inserted replaced
1302:6a14b1f6dc04 1303:eed9376a4ad6
529 } 529 }
530 530
531 return NULL; 531 return NULL;
532 } 532 }
533 533
534 static void
535 try_add_algo(const char *algo_name, algo_type *algos,
536 const char *algo_desc, algo_type * new_algos, int *num_ret)
537 {
538 algo_type *match_algo = check_algo(algo_name, algos);
539 if (!match_algo)
540 {
541 dropbear_log(LOG_WARNING, "This Dropbear program does not support '%s' %s algorithm", algo_name, algo_desc);
542 return;
543 }
544
545 new_algos[*num_ret] = *match_algo;
546 (*num_ret)++;
547 }
548
549 /* Checks a user provided comma-separated algorithm list for available 534 /* Checks a user provided comma-separated algorithm list for available
550 * options. Any that are not acceptable are removed in-place. Returns the 535 * options. Any that are not acceptable are removed in-place. Returns the
551 * number of valid algorithms. */ 536 * number of valid algorithms. */
552 int 537 int
553 check_user_algos(const char* user_algo_list, algo_type * algos, 538 check_user_algos(const char* user_algo_list, algo_type * algos,
554 const char *algo_desc) 539 const char *algo_desc)
555 { 540 {
556 algo_type new_algos[MAX_PROPOSED_ALGO]; 541 algo_type new_algos[MAX_PROPOSED_ALGO+1];
557 /* this has two passes. first we sweep through the given list of
558 * algorithms and mark them as usable=2 in the algo_type[] array... */
559 int num_ret = 0;
560 char *work_list = m_strdup(user_algo_list); 542 char *work_list = m_strdup(user_algo_list);
561 char *last_name = work_list; 543 char *start = work_list;
562 char *c; 544 char *c;
563 for (c = work_list; *c; c++) 545 int n;
546 /* So we can iterate and look for null terminator */
547 memset(new_algos, 0x0, sizeof(new_algos));
548 for (c = work_list, n = 0; ; c++)
564 { 549 {
565 if (*c == ',') 550 char oc = *c;
566 { 551 if (n >= MAX_PROPOSED_ALGO) {
552 dropbear_exit("Too many algorithms '%s'", user_algo_list);
553 }
554 if (*c == ',' || *c == '\0') {
555 algo_type *match_algo = NULL;
567 *c = '\0'; 556 *c = '\0';
568 try_add_algo(last_name, algos, algo_desc, new_algos, &num_ret); 557 match_algo = check_algo(start, algos);
558 if (match_algo) {
559 if (check_algo(start, new_algos)) {
560 TRACE(("Skip repeated algorithm '%s'", start))
561 } else {
562 new_algos[n] = *match_algo;
563 n++;
564 }
565 } else {
566 dropbear_log(LOG_WARNING, "This Dropbear program does not support '%s' %s algorithm", start, algo_desc);
567 }
569 c++; 568 c++;
570 last_name = c; 569 start = c;
571 } 570 }
572 } 571 if (oc == '\0') {
573 try_add_algo(last_name, algos, algo_desc, new_algos, &num_ret); 572 break;
573 }
574 }
574 m_free(work_list); 575 m_free(work_list);
575 576 /* n+1 to include a null terminator */
576 new_algos[num_ret].name = NULL; 577 memcpy(algos, new_algos, sizeof(*new_algos) * (n+1));
577 578 return n;
578 /* Copy one more as a blank delimiter */
579 memcpy(algos, new_algos, sizeof(*new_algos) * (num_ret+1));
580 return num_ret;
581 } 579 }
582 #endif /* ENABLE_USER_ALGO_LIST */ 580 #endif /* ENABLE_USER_ALGO_LIST */