blob: 5d1bbc9e60f588100c0b07ac0f4a78287784b538 [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>
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020032#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020034#define mbedtls_exit exit
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000039
40#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000041#include <stdlib.h>
42#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000043#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000044
45#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020046 "\n usage: strerror <errorcode>\n" \
47 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Rich Evans85b05ec2015-02-12 11:37:29 +000050int main( void )
Paul Bakker01cc3942012-05-08 08:36:15 +000051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020053 mbedtls_exit( 0 );
Paul Bakker01cc3942012-05-08 08:36:15 +000054}
55#else
56int main( int argc, char *argv[] )
57{
Paul Bakker62534dd2013-06-30 12:45:07 +020058 long int val;
59 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000060
61 if( argc != 2 )
62 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_printf( USAGE );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020064 mbedtls_exit( 0 );
Paul Bakker01cc3942012-05-08 08:36:15 +000065 }
66
Paul Bakker62534dd2013-06-30 12:45:07 +020067 val = strtol( argv[1], &end, 10 );
68 if( *end != '\0' )
69 {
70 val = strtol( argv[1], &end, 16 );
71 if( *end != '\0' )
72 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_printf( USAGE );
Paul Bakker62534dd2013-06-30 12:45:07 +020074 return( 0 );
75 }
76 }
77 if( val > 0 )
78 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +000079
Paul Bakker62534dd2013-06-30 12:45:07 +020080 if( val != 0 )
Paul Bakker01cc3942012-05-08 08:36:15 +000081 {
82 char error_buf[200];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_strerror( val, error_buf, 200 );
84 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
Paul Bakker01cc3942012-05-08 08:36:15 +000085 }
86
87#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker01cc3942012-05-08 08:36:15 +000089 fflush( stdout ); getchar();
90#endif
91
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020092 mbedtls_exit( val );
Paul Bakker01cc3942012-05-08 08:36:15 +000093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#endif /* MBEDTLS_ERROR_C */