1 /*
2  *  Translate error code to error string
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 
8 #include "mbedtls/build_info.h"
9 
10 #include "mbedtls/platform.h"
11 
12 #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
13 #include "mbedtls/error.h"
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #endif
19 
20 #define USAGE \
21     "\n usage: strerror <errorcode>\n" \
22     "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
23 
24 #if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
main(void)25 int main(void)
26 {
27     mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
28     mbedtls_exit(0);
29 }
30 #else
main(int argc,char * argv[])31 int main(int argc, char *argv[])
32 {
33     long int val;
34     char *end = argv[1];
35 
36     if (argc != 2) {
37         mbedtls_printf(USAGE);
38         mbedtls_exit(0);
39     }
40 
41     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;
47         }
48     }
49     if (val > 0) {
50         val = -val;
51     }
52 
53     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);
60 }
61 #endif /* MBEDTLS_ERROR_C */
62