首页 > 解决方案 > 如何为此方法格式化字符串?

问题描述

我在尝试使此方法起作用时遇到问题,我正在尝试将 mbedtls 与 psk 一起使用。事实证明,对于像我这样的 C 新手来说,他们的例子很难理解。

/*
 * Parse a string of pairs name1,key1[,name2,key2[,...]]
 * into a usable psk_entry list.
 *
 * Modifies the input string! This is not production quality!
 */

psk_entry * psk_parse( char * psk_string )
{
  psk_entry *cur = NULL, *new = NULL;
  char *p = psk_string;
  char *end = p;
  char *key_hex;

  while( *end != '\n' )
  {
    end++;
  }
  *end = ',';

  while( p <= end )
  {
    if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
      goto error;

    memset( new, 0, sizeof( psk_entry ) );

    GET_ITEM( new->name );
    GET_ITEM( key_hex );

    if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
    {
      goto error;
    }

    new->next = cur;
    cur = new;
  }

  return( cur );

  error:
  psk_free( new );
  psk_free( cur );
  return( 0 );
}

使此方法起作用的预期字符串格式是什么?目前,我有

char * list = "JD,4f07d80fde6469fbdbf1f154a47f27c916dba68b644ff1ffa26295e598855810";

*end = ','它被传递给该方法,但是在尝试添加时我不断收到段错误。

标签: cmbedtls

解决方案


推荐阅读