blob: c15904f35852b4de83958b09406f0a80d376e281 [file] [log] [blame]
Paul Bakker01cc3942012-05-08 08:36:15 +00001/*
2 * Translate error code to error string
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker01cc3942012-05-08 08:36:15 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020023# include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# include <stdio.h>
26# include <stdlib.h>
27# define mbedtls_printf printf
28# define mbedtls_exit exit
Rich Evansf90016a2015-01-19 14:26:37 +000029#endif
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020032# include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000033
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020034# include <stdio.h>
35# include <stdlib.h>
36# include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000037#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000038
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020040 "\n usage: strerror <errorcode>\n" \
41 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044int main(void)
Paul Bakker01cc3942012-05-08 08:36:15 +000045{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046 mbedtls_printf(
47 "MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
48 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000049}
50#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051int main(int argc, char *argv[])
Paul Bakker01cc3942012-05-08 08:36:15 +000052{
Paul Bakker62534dd2013-06-30 12:45:07 +020053 long int val;
54 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000055
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056 if (argc != 2) {
57 mbedtls_printf(USAGE);
58 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000059 }
60
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020061 val = strtol(argv[1], &end, 10);
62 if (*end != '\0') {
63 val = strtol(argv[1], &end, 16);
64 if (*end != '\0') {
65 mbedtls_printf(USAGE);
66 return 0;
Paul Bakker62534dd2013-06-30 12:45:07 +020067 }
68 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069 if (val > 0)
Paul Bakker62534dd2013-06-30 12:45:07 +020070 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +000071
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072 if (val != 0) {
Paul Bakker01cc3942012-05-08 08:36:15 +000073 char error_buf[200];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020074 mbedtls_strerror(val, error_buf, 200);
75 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int)-val,
76 error_buf);
Paul Bakker01cc3942012-05-08 08:36:15 +000077 }
78
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079# if defined(_WIN32)
80 mbedtls_printf(" + Press Enter to exit this program.\n");
81 fflush(stdout);
82 getchar();
83# endif
Paul Bakker01cc3942012-05-08 08:36:15 +000084
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020085 mbedtls_exit(val);
Paul Bakker01cc3942012-05-08 08:36:15 +000086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#endif /* MBEDTLS_ERROR_C */