blob: 18824cef7b71eaf4be1d3f5528ae3b3cfdac9f72 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker01cc3942012-05-08 08:36:15 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker01cc3942012-05-08 08:36:15 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020059#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_printf printf
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020061#define mbedtls_exit exit
Rich Evansf90016a2015-01-19 14:26:37 +000062#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000066
67#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000068#include <stdlib.h>
69#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000070#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000071
72#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020073 "\n usage: strerror <errorcode>\n" \
74 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Rich Evans85b05ec2015-02-12 11:37:29 +000077int main( void )
Paul Bakker01cc3942012-05-08 08:36:15 +000078{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020080 mbedtls_exit( 0 );
Paul Bakker01cc3942012-05-08 08:36:15 +000081}
82#else
83int main( int argc, char *argv[] )
84{
Paul Bakker62534dd2013-06-30 12:45:07 +020085 long int val;
86 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000087
88 if( argc != 2 )
89 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_printf( USAGE );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020091 mbedtls_exit( 0 );
Paul Bakker01cc3942012-05-08 08:36:15 +000092 }
93
Paul Bakker62534dd2013-06-30 12:45:07 +020094 val = strtol( argv[1], &end, 10 );
95 if( *end != '\0' )
96 {
97 val = strtol( argv[1], &end, 16 );
98 if( *end != '\0' )
99 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_printf( USAGE );
Paul Bakker62534dd2013-06-30 12:45:07 +0200101 return( 0 );
102 }
103 }
104 if( val > 0 )
105 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +0000106
Paul Bakker62534dd2013-06-30 12:45:07 +0200107 if( val != 0 )
Paul Bakker01cc3942012-05-08 08:36:15 +0000108 {
109 char error_buf[200];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_strerror( val, error_buf, 200 );
111 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
Paul Bakker01cc3942012-05-08 08:36:15 +0000112 }
113
114#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker01cc3942012-05-08 08:36:15 +0000116 fflush( stdout ); getchar();
117#endif
118
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200119 mbedtls_exit( val );
Paul Bakker01cc3942012-05-08 08:36:15 +0000120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#endif /* MBEDTLS_ERROR_C */