blob: 316f28614bbe14b342a5451ac0a707fbc1691a28 [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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker01cc3942012-05-08 08:36:15 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000013#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000014
15#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000016#include <stdlib.h>
17#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000018#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000019
20#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020021 "\n usage: strerror <errorcode>\n" \
22 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Gilles Peskine449bd832023-01-11 14:50:10 +010025int main(void)
Paul Bakker01cc3942012-05-08 08:36:15 +000026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010028 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000029}
30#else
Gilles Peskine449bd832023-01-11 14:50:10 +010031int main(int argc, char *argv[])
Paul Bakker01cc3942012-05-08 08:36:15 +000032{
Paul Bakker62534dd2013-06-30 12:45:07 +020033 long int val;
34 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000035
Gilles Peskine449bd832023-01-11 14:50:10 +010036 if (argc != 2) {
37 mbedtls_printf(USAGE);
38 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000039 }
40
Gilles Peskine449bd832023-01-11 14:50:10 +010041 val = strtol(argv[1], &end, 10);
42 if (*end != '\0') {
43 val = strtol(argv[1], &end, 16);
44 if (*end != '\0') {
45 mbedtls_printf(USAGE);
46 return 0;
Paul Bakker62534dd2013-06-30 12:45:07 +020047 }
48 }
Gilles Peskine449bd832023-01-11 14:50:10 +010049 if (val > 0) {
Paul Bakker62534dd2013-06-30 12:45:07 +020050 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +000051 }
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053 if (val != 0) {
54 char error_buf[200];
55 mbedtls_strerror(val, error_buf, 200);
56 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf);
57 }
58
59 mbedtls_exit(val);
Paul Bakker01cc3942012-05-08 08:36:15 +000060}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#endif /* MBEDTLS_ERROR_C */