28 #if defined(POLARSSL_DEBUG_C)
35 #if defined _MSC_VER && !defined snprintf
36 #define snprintf _snprintf
39 #if defined _MSC_VER && !defined vsnprintf
40 #define vsnprintf _vsnprintf
43 char *
debug_fmt(
const char *format, ... )
47 int maxlen =
sizeof( str ) - 1;
49 va_start( argp, format );
50 vsnprintf( str, maxlen, format, argp );
58 const char *file,
int line,
const char *text )
61 int maxlen =
sizeof( str ) - 1;
63 if( ssl->
f_dbg == NULL )
66 snprintf( str, maxlen,
"%s(%04d): %s\n", file, line, text );
72 const char *file,
int line,
73 const char *text,
int ret )
76 int maxlen =
sizeof( str ) - 1;
78 if( ssl->
f_dbg == NULL )
81 snprintf( str, maxlen,
"%s(%04d): %s() returned %d (0x%x)\n",
82 file, line, text, ret, ret );
89 const char *file,
int line,
const char *text,
90 unsigned char *buf,
size_t len )
93 size_t i, maxlen =
sizeof( str ) - 1;
95 if( ssl->
f_dbg == NULL )
98 snprintf( str, maxlen,
"%s(%04d): dumping '%s' (%d bytes)\n",
99 file, line, text, (
unsigned int) len );
104 for( i = 0; i < len; i++ )
114 snprintf( str, maxlen,
"%s(%04d): %04x: ", file, line,
121 snprintf( str, maxlen,
" %02x", (
unsigned int) buf[i] );
131 #if defined(POLARSSL_ECP_C)
133 const char *file,
int line,
137 int maxlen =
sizeof( str ) - 1;
139 snprintf( str, maxlen,
"%s(X)", text );
143 snprintf( str, maxlen,
"%s(Y)", text );
147 snprintf( str, maxlen,
"%s(Z)", text );
153 #if defined(POLARSSL_BIGNUM_C)
155 const char *file,
int line,
156 const char *text,
const mpi *X )
159 int j, k, maxlen =
sizeof( str ) - 1, zeros = 1;
162 if( ssl->
f_dbg == NULL || X == NULL )
165 for( n = X->
n - 1; n > 0; n-- )
169 for( j = (
sizeof(
t_uint) << 3 ) - 1; j >= 0; j-- )
170 if( ( ( X->
p[n] >> j ) & 1 ) != 0 )
173 snprintf( str, maxlen,
"%s(%04d): value of '%s' (%d bits) is:\n",
175 (
int) ( ( n * (
sizeof(
t_uint) << 3 ) ) + j + 1 ) );
180 for( i = n + 1, j = 0; i > 0; i-- )
182 if( zeros && X->
p[i - 1] == 0 )
185 for( k =
sizeof(
t_uint ) - 1; k >= 0; k-- )
187 if( zeros && ( ( X->
p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
197 snprintf( str, maxlen,
"%s(%04d): ", file, line );
203 snprintf( str, maxlen,
" %02x", (
unsigned int)
204 ( X->
p[i - 1] >> (k << 3) ) & 0xFF );
216 snprintf( str, maxlen,
"%s(%04d): ", file, line );
227 #if defined(POLARSSL_X509_CRT_PARSE_C)
228 static void debug_print_pk(
const ssl_context *ssl,
int level,
229 const char *file,
int line,
236 memset( items, 0,
sizeof( items ) );
244 for( i = 0; i <
sizeof( items ); i++ )
249 snprintf( name,
sizeof( name ),
"%s%s", text, items[i].name );
250 name[
sizeof( name ) - 1] =
'\0';
255 #if defined(POLARSSL_ECP_C)
265 const char *file,
int line,
266 const char *text,
const x509_crt *crt )
268 char str[1024], prefix[64];
269 int i = 0, maxlen =
sizeof( prefix ) - 1;
271 if( ssl->
f_dbg == NULL || crt == NULL )
274 snprintf( prefix, maxlen,
"%s(%04d): ", file, line );
275 prefix[maxlen] =
'\0';
276 maxlen =
sizeof( str ) - 1;
283 snprintf( str, maxlen,
"%s(%04d): %s #%d:\n%s",
284 file, line, text, ++i, buf );
289 debug_print_pk( ssl, level, file, line,
"crt->", &crt->
pk );