/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/security/xyssl-0.9/library/debug.c

Go to the documentation of this file.
00001 /*
00002  *  Debugging routines
00003  *
00004  *  Copyright (C) 2006-2007  Christophe Devine
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #include "xyssl/config.h"
00022 
00023 #if defined(XYSSL_DEBUG_C)
00024 
00025 #include "xyssl/debug.h"
00026 
00027 #include <stdarg.h>
00028 #include <stdlib.h>
00029 
00030 #if defined _MSC_VER && !defined  snprintf
00031 #define  snprintf  _snprintf
00032 #endif
00033 
00034 #if defined _MSC_VER && !defined vsnprintf
00035 #define vsnprintf _vsnprintf
00036 #endif
00037 
00038 char *debug_fmt( const char *format, ... )
00039 {
00040     va_list argp;
00041     static char str[512];
00042     int maxlen = sizeof( str ) - 1;
00043 
00044     va_start( argp, format );
00045     vsnprintf( str, maxlen, format, argp );
00046     va_end( argp );
00047 
00048     str[maxlen] = '\0';
00049     return( str );
00050 }
00051 
00052 void debug_print_msg( ssl_context *ssl, int level,
00053                       char *file, int line, char *text )
00054 {
00055     char str[512];
00056     int maxlen = sizeof( str ) - 1;
00057 
00058     if( ssl->f_dbg == NULL )
00059         return;
00060 
00061     snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
00062     str[maxlen] = '\0';
00063     ssl->f_dbg( ssl->p_dbg, level, str );
00064 }
00065 
00066 void debug_print_ret( ssl_context *ssl, int level,
00067                       char *file, int line, char *text, int ret )
00068 {
00069     char str[512];
00070     int maxlen = sizeof( str ) - 1;
00071 
00072     if( ssl->f_dbg == NULL )
00073         return;
00074 
00075     snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
00076               file, line, text, ret, ret );
00077 
00078     str[maxlen] = '\0';
00079     ssl->f_dbg( ssl->p_dbg, level, str );
00080 }
00081 
00082 void debug_print_buf( ssl_context *ssl, int level,
00083                       char *file, int line, char *text,
00084                       unsigned char *buf, int len )
00085 {
00086     char str[512];
00087     int i, maxlen = sizeof( str ) - 1;
00088 
00089     if( ssl->f_dbg == NULL || len < 0 )
00090         return;
00091 
00092     snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
00093               file, line, text, len );
00094 
00095     str[maxlen] = '\0';
00096     ssl->f_dbg( ssl->p_dbg, level, str );
00097 
00098     for( i = 0; i < len; i++ )
00099     {
00100         if( i >= 4096 )
00101             break;
00102 
00103         if( i % 16 == 0 )
00104         {
00105             if( i > 0 )
00106                 ssl->f_dbg( ssl->p_dbg, level, "\n" );
00107 
00108             snprintf( str, maxlen, "%s(%04d): %04x: ", file, line, i );
00109 
00110             str[maxlen] = '\0';
00111             ssl->f_dbg( ssl->p_dbg, level, str );
00112         }
00113 
00114         snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
00115 
00116         str[maxlen] = '\0';
00117         ssl->f_dbg( ssl->p_dbg, level, str );
00118     }
00119 
00120     if( len > 0 )
00121         ssl->f_dbg( ssl->p_dbg, level, "\n" );
00122 }
00123 
00124 void debug_print_mpi( ssl_context *ssl, int level,
00125                       char *file, int line, char *text, mpi *X )
00126 {
00127     char str[512];
00128     int i, j, k, n, maxlen = sizeof( str ) - 1;
00129 
00130     if( ssl->f_dbg == NULL || X == NULL )
00131         return;
00132 
00133     for( n = X->n - 1; n >= 0; n-- )
00134         if( X->p[n] != 0 )
00135             break;
00136 
00137     snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
00138               file, line, text, ((n + 1) * sizeof( t_int )) << 3 );
00139 
00140     str[maxlen] = '\0';
00141     ssl->f_dbg( ssl->p_dbg, level, str );
00142 
00143     for( i = n, j = 0; i >= 0; i--, j++ )
00144     {
00145         if( j % ( 16 / sizeof( t_int ) ) == 0 )
00146         {
00147             if( j > 0 )
00148                 ssl->f_dbg( ssl->p_dbg, level, "\n" );
00149 
00150             snprintf( str, maxlen, "%s(%04d): ", file, line );
00151 
00152             str[maxlen] = '\0';
00153             ssl->f_dbg( ssl->p_dbg, level, str );
00154         }
00155 
00156         for( k = sizeof( t_int ) - 1; k >= 0; k-- )
00157         {
00158             snprintf( str, maxlen, " %02x", (unsigned int)
00159                       ( X->p[i] >> (k << 3) ) & 0xFF );
00160 
00161             str[maxlen] = '\0';
00162             ssl->f_dbg( ssl->p_dbg, level, str );
00163         }
00164     }
00165 
00166     ssl->f_dbg( ssl->p_dbg, level, "\n" );
00167 }
00168 
00169 void debug_print_crt( ssl_context *ssl, int level,
00170                       char *file, int line, char *text, x509_cert *crt )
00171 {
00172     char str[512], prefix[64], *p;
00173     int i = 0, maxlen = sizeof( prefix ) - 1;
00174 
00175     if( ssl->f_dbg == NULL || crt == NULL )
00176         return;
00177 
00178     snprintf( prefix, maxlen, "%s(%04d): ", file, line );
00179     prefix[maxlen] = '\0';
00180     maxlen = sizeof( str ) - 1;
00181 
00182     while( crt != NULL && crt->next != NULL )
00183     {
00184         p = x509parse_cert_info( prefix, crt );
00185 
00186         snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
00187                   file, line, text, ++i, p );
00188 
00189         str[maxlen] = '\0';
00190         ssl->f_dbg( ssl->p_dbg, level, str );
00191 
00192         debug_print_mpi( ssl, level, file, line,
00193                          "crt->rsa.N", &crt->rsa.N );
00194 
00195         debug_print_mpi( ssl, level, file, line,
00196                          "crt->rsa.E", &crt->rsa.E );
00197 
00198         crt = crt->next;
00199     }
00200 }
00201 
00202 #endif

Generated on Fri Jul 11 17:59:46 2008 for Mobile-C by  doxygen 1.5.4