blob: 458280c989b18528fb5e54e8e52dbad43a546bc0 [file] [log] [blame]
Paul Bakker01cc3942012-05-08 08:36:15 +00001/*
2 * Translate error code to error string
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker01cc3942012-05-08 08:36:15 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker01cc3942012-05-08 08:36:15 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000037
38#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000039#include <stdlib.h>
40#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000041#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000042
43#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020044 "\n usage: strerror <errorcode>\n" \
45 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Rich Evans85b05ec2015-02-12 11:37:29 +000048int main( void )
Paul Bakker01cc3942012-05-08 08:36:15 +000049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Paul Bakker01cc3942012-05-08 08:36:15 +000051 return( 0 );
52}
53#else
54int main( int argc, char *argv[] )
55{
Paul Bakker62534dd2013-06-30 12:45:07 +020056 long int val;
57 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000058
59 if( argc != 2 )
60 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_printf( USAGE );
Paul Bakker01cc3942012-05-08 08:36:15 +000062 return( 0 );
63 }
64
Paul Bakker62534dd2013-06-30 12:45:07 +020065 val = strtol( argv[1], &end, 10 );
66 if( *end != '\0' )
67 {
68 val = strtol( argv[1], &end, 16 );
69 if( *end != '\0' )
70 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_printf( USAGE );
Paul Bakker62534dd2013-06-30 12:45:07 +020072 return( 0 );
73 }
74 }
75 if( val > 0 )
76 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +000077
Paul Bakker62534dd2013-06-30 12:45:07 +020078 if( val != 0 )
Paul Bakker01cc3942012-05-08 08:36:15 +000079 {
80 char error_buf[200];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_strerror( val, error_buf, 200 );
82 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
Paul Bakker01cc3942012-05-08 08:36:15 +000083 }
84
85#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker01cc3942012-05-08 08:36:15 +000087 fflush( stdout ); getchar();
88#endif
89
Paul Bakker62534dd2013-06-30 12:45:07 +020090 return( val );
Paul Bakker01cc3942012-05-08 08:36:15 +000091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#endif /* MBEDTLS_ERROR_C */