blob: e20bed6e8f93042b2658bff046bef606000a87a5 [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 Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker01cc3942012-05-08 08:36:15 +00006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker01cc3942012-05-08 08:36:15 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000016
17#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000018#include <stdlib.h>
19#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000020#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000021
22#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020023 "\n usage: strerror <errorcode>\n" \
24 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Gilles Peskine449bd832023-01-11 14:50:10 +010027int main(void)
Paul Bakker01cc3942012-05-08 08:36:15 +000028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010030 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000031}
32#else
Gilles Peskine449bd832023-01-11 14:50:10 +010033int main(int argc, char *argv[])
Paul Bakker01cc3942012-05-08 08:36:15 +000034{
Paul Bakker62534dd2013-06-30 12:45:07 +020035 long int val;
36 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000037
Gilles Peskine449bd832023-01-11 14:50:10 +010038 if (argc != 2) {
39 mbedtls_printf(USAGE);
40 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000041 }
42
Gilles Peskine449bd832023-01-11 14:50:10 +010043 val = strtol(argv[1], &end, 10);
44 if (*end != '\0') {
45 val = strtol(argv[1], &end, 16);
46 if (*end != '\0') {
47 mbedtls_printf(USAGE);
48 return 0;
Paul Bakker62534dd2013-06-30 12:45:07 +020049 }
50 }
Gilles Peskine449bd832023-01-11 14:50:10 +010051 if (val > 0) {
Paul Bakker62534dd2013-06-30 12:45:07 +020052 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +000053 }
54
Gilles Peskine449bd832023-01-11 14:50:10 +010055 if (val != 0) {
56 char error_buf[200];
57 mbedtls_strerror(val, error_buf, 200);
58 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf);
59 }
60
61 mbedtls_exit(val);
Paul Bakker01cc3942012-05-08 08:36:15 +000062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#endif /* MBEDTLS_ERROR_C */