| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Translate error code to error string | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2012, ARM Limited, All Rights Reserved | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 5 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 7 | * | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 8 | *  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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #endif | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 28 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform.h" | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 31 | #else | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #define mbedtls_printf     printf | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 34 | #endif | 
|  | 35 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/error.h" | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 38 |  | 
|  | 39 | #include <stdio.h> | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 40 | #include <stdlib.h> | 
|  | 41 | #include <string.h> | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 42 | #endif | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 43 |  | 
|  | 44 | #define USAGE \ | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 45 | "\n usage: strerror <errorcode>\n" \ | 
|  | 46 | "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n" | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 47 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 49 | int main( void ) | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 50 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n"); | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 52 | return( 0 ); | 
|  | 53 | } | 
|  | 54 | #else | 
|  | 55 | int main( int argc, char *argv[] ) | 
|  | 56 | { | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 57 | long int val; | 
|  | 58 | char *end = argv[1]; | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 59 |  | 
|  | 60 | if( argc != 2 ) | 
|  | 61 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | mbedtls_printf( USAGE ); | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 63 | return( 0 ); | 
|  | 64 | } | 
|  | 65 |  | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 66 | 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | mbedtls_printf( USAGE ); | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 73 | return( 0 ); | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 | if( val > 0 ) | 
|  | 77 | val = -val; | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 78 |  | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 79 | if( val != 0 ) | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 80 | { | 
|  | 81 | char error_buf[200]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | mbedtls_strerror( val, error_buf, 200 ); | 
|  | 83 | mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf ); | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | #if defined(_WIN32) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | mbedtls_printf( "  + Press Enter to exit this program.\n" ); | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 88 | fflush( stdout ); getchar(); | 
|  | 89 | #endif | 
|  | 90 |  | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 91 | return( val ); | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 92 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 93 | #endif /* MBEDTLS_ERROR_C */ |