blob: 4ea9d329b730593d718b5cc8464cb9b06937dbda [file] [log] [blame]
Paul Bakker01cc3942012-05-08 08:36:15 +00001/*
2 * Translate error code to error string
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker01cc3942012-05-08 08:36:15 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_printf printf
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020059#define mbedtls_exit exit
Rich Evansf90016a2015-01-19 14:26:37 +000060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000064
65#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000066#include <stdlib.h>
67#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000068#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000069
70#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020071 "\n usage: strerror <errorcode>\n" \
72 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Rich Evans85b05ec2015-02-12 11:37:29 +000075int main( void )
Paul Bakker01cc3942012-05-08 08:36:15 +000076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020078 mbedtls_exit( 0 );
Paul Bakker01cc3942012-05-08 08:36:15 +000079}
80#else
81int main( int argc, char *argv[] )
82{
Paul Bakker62534dd2013-06-30 12:45:07 +020083 long int val;
84 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000085
86 if( argc != 2 )
87 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_printf( USAGE );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020089 mbedtls_exit( 0 );
Paul Bakker01cc3942012-05-08 08:36:15 +000090 }
91
Paul Bakker62534dd2013-06-30 12:45:07 +020092 val = strtol( argv[1], &end, 10 );
93 if( *end != '\0' )
94 {
95 val = strtol( argv[1], &end, 16 );
96 if( *end != '\0' )
97 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_printf( USAGE );
Paul Bakker62534dd2013-06-30 12:45:07 +020099 return( 0 );
100 }
101 }
102 if( val > 0 )
103 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +0000104
Paul Bakker62534dd2013-06-30 12:45:07 +0200105 if( val != 0 )
Paul Bakker01cc3942012-05-08 08:36:15 +0000106 {
107 char error_buf[200];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_strerror( val, error_buf, 200 );
109 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
Paul Bakker01cc3942012-05-08 08:36:15 +0000110 }
111
112#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker01cc3942012-05-08 08:36:15 +0000114 fflush( stdout ); getchar();
115#endif
116
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200117 mbedtls_exit( val );
Paul Bakker01cc3942012-05-08 08:36:15 +0000118}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#endif /* MBEDTLS_ERROR_C */