blob: 2215f07aa1b549776a3e9990f355c34410c9f04a [file] [log] [blame]
Paul Bakker01cc3942012-05-08 08:36:15 +00001/*
2 * Translate error code to error string
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakker01cc3942012-05-08 08:36:15 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker01cc3942012-05-08 08:36:15 +00007 *
Paul Bakker01cc3942012-05-08 08:36:15 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000038
39#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000040#include <stdlib.h>
41#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000042#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000043
44#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020045 "\n usage: strerror <errorcode>\n" \
46 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Rich Evans85b05ec2015-02-12 11:37:29 +000049int main( void )
Paul Bakker01cc3942012-05-08 08:36:15 +000050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Paul Bakker01cc3942012-05-08 08:36:15 +000052 return( 0 );
53}
54#else
55int main( int argc, char *argv[] )
56{
Paul Bakker62534dd2013-06-30 12:45:07 +020057 long int val;
58 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000059
60 if( argc != 2 )
61 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_printf( USAGE );
Paul Bakker01cc3942012-05-08 08:36:15 +000063 return( 0 );
64 }
65
Paul Bakker62534dd2013-06-30 12:45:07 +020066 val = strtol( argv[1], &end, 10 );
67 if( *end != '\0' )
68 {
69 val = strtol( argv[1], &end, 16 );
70 if( *end != '\0' )
71 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_printf( USAGE );
Paul Bakker62534dd2013-06-30 12:45:07 +020073 return( 0 );
74 }
75 }
76 if( val > 0 )
77 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +000078
Paul Bakker62534dd2013-06-30 12:45:07 +020079 if( val != 0 )
Paul Bakker01cc3942012-05-08 08:36:15 +000080 {
81 char error_buf[200];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_strerror( val, error_buf, 200 );
83 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
Paul Bakker01cc3942012-05-08 08:36:15 +000084 }
85
86#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker01cc3942012-05-08 08:36:15 +000088 fflush( stdout ); getchar();
89#endif
90
Paul Bakker62534dd2013-06-30 12:45:07 +020091 return( val );
Paul Bakker01cc3942012-05-08 08:36:15 +000092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#endif /* MBEDTLS_ERROR_C */