| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "common.h" |
| 23 |
|
| 24 |
#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 25 |
|
| 26 |
#include "mps_common.h" |
| 27 |
|
| 28 |
#if defined(MBEDTLS_MPS_ENABLE_TRACE) |
| 29 |
|
| 30 |
#include "mps_trace.h" |
| 31 |
#include <stdarg.h> |
| 32 |
|
| 33 |
static int trace_depth = 0; |
| 34 |
|
| 35 |
#define color_default "\x1B[0m" |
| 36 |
#define color_red "\x1B[1;31m" |
| 37 |
#define color_green "\x1B[1;32m" |
| 38 |
#define color_yellow "\x1B[1;33m" |
| 39 |
#define color_blue "\x1B[1;34m" |
| 40 |
#define color_magenta "\x1B[1;35m" |
| 41 |
#define color_cyan "\x1B[1;36m" |
| 42 |
#define color_white "\x1B[1;37m" |
| 43 |
|
| 44 |
static char const * colors[] = |
| 45 |
{ |
| 46 |
color_default, |
| 47 |
color_green, |
| 48 |
color_yellow, |
| 49 |
color_magenta, |
| 50 |
color_cyan, |
| 51 |
color_blue, |
| 52 |
color_white |
| 53 |
}; |
| 54 |
|
| 55 |
#define MPS_TRACE_BUF_SIZE 100 |
| 56 |
|
| 57 |
void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... ) |
| 58 |
{ |
| 59 |
int ret; |
| 60 |
char str[MPS_TRACE_BUF_SIZE]; |
| 61 |
va_list argp; |
| 62 |
va_start( argp, format ); |
| 63 |
ret = mbedtls_vsnprintf( str, MPS_TRACE_BUF_SIZE, format, argp ); |
| 64 |
va_end( argp ); |
| 65 |
|
| 66 |
if( ret >= 0 && ret < MPS_TRACE_BUF_SIZE ) |
| 67 |
{ |
| 68 |
str[ret] = '\0'; |
| 69 |
mbedtls_printf( "[%d|L%d]: %s\n", id, line, str ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
int mbedtls_mps_trace_get_depth() |
| 74 |
{ |
| 75 |
return trace_depth; |
| 76 |
} |
| 77 |
void mbedtls_mps_trace_dec_depth() |
| 78 |
{ |
| 79 |
trace_depth--; |
| 80 |
} |
| 81 |
void mbedtls_mps_trace_inc_depth() |
| 82 |
{ |
| 83 |
trace_depth++; |
| 84 |
} |
| 85 |
|
| 86 |
void mbedtls_mps_trace_color( int id ) |
| 87 |
{ |
| 88 |
if( id > (int) ( sizeof( colors ) / sizeof( *colors ) ) ) |
| 89 |
return; |
| 90 |
printf( "%s", colors[ id ] ); |
| 91 |
} |
| 92 |
|
| 93 |
void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty ) |
| 94 |
{ |
| 95 |
if( level > 0 ) |
| 96 |
{ |
| 97 |
while( --level ) |
| 98 |
printf( "| " ); |
| 99 |
|
| 100 |
printf( "| " ); |
| 101 |
} |
| 102 |
|
| 103 |
switch( ty ) |
| 104 |
{ |
| 105 |
case MBEDTLS_MPS_TRACE_TYPE_COMMENT: |
| 106 |
mbedtls_printf( "@ " ); |
| 107 |
break; |
| 108 |
|
| 109 |
case MBEDTLS_MPS_TRACE_TYPE_CALL: |
| 110 |
mbedtls_printf( "+--> " ); |
| 111 |
break; |
| 112 |
|
| 113 |
case MBEDTLS_MPS_TRACE_TYPE_ERROR: |
| 114 |
mbedtls_printf( "E " ); |
| 115 |
break; |
| 116 |
|
| 117 |
case MBEDTLS_MPS_TRACE_TYPE_RETURN: |
| 118 |
mbedtls_printf( "< " ); |
| 119 |
break; |
| 120 |
|
| 121 |
default: |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#endif |
| 127 |
#endif |