blob: 84967ded12bcb2e6d664ddbacdc65a2404cde662 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker01cc3942012-05-08 08:36:15 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000018
19#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000020#include <stdlib.h>
21#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000022#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000023
24#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020025 "\n usage: strerror <errorcode>\n" \
26 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010029int main(void)
Paul Bakker01cc3942012-05-08 08:36:15 +000030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000033}
34#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035int main(int argc, char *argv[])
Paul Bakker01cc3942012-05-08 08:36:15 +000036{
Paul Bakker62534dd2013-06-30 12:45:07 +020037 long int val;
38 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040 if (argc != 2) {
41 mbedtls_printf(USAGE);
42 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000043 }
44
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 val = strtol(argv[1], &end, 10);
46 if (*end != '\0') {
47 val = strtol(argv[1], &end, 16);
48 if (*end != '\0') {
49 mbedtls_printf(USAGE);
50 return 0;
Paul Bakker62534dd2013-06-30 12:45:07 +020051 }
52 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 if (val > 0) {
Paul Bakker62534dd2013-06-30 12:45:07 +020054 val = -val;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 }
Paul Bakker01cc3942012-05-08 08:36:15 +000056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 if (val != 0) {
Paul Bakker01cc3942012-05-08 08:36:15 +000058 char error_buf[200];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 mbedtls_strerror(val, error_buf, 200);
60 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf);
Paul Bakker01cc3942012-05-08 08:36:15 +000061 }
62
63#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 mbedtls_printf(" + Press Enter to exit this program.\n");
65 fflush(stdout); getchar();
Paul Bakker01cc3942012-05-08 08:36:15 +000066#endif
67
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 mbedtls_exit(val);
Paul Bakker01cc3942012-05-08 08:36:15 +000069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#endif /* MBEDTLS_ERROR_C */