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 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
17 #include "mbedtls/error.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #endif
23
24 #define USAGE \
25 "\n usage: strerror <errorcode>\n" \
26 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
27
28 #if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
main(void)29 int main(void)
30 {
31 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
32 mbedtls_exit(0);
33 }
34 #else
main(int argc,char * argv[])35 int main(int argc, char *argv[])
36 {
37 long int val;
38 char *end = argv[1];
39
40 if (argc != 2) {
41 mbedtls_printf(USAGE);
42 mbedtls_exit(0);
43 }
44
45 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;
51 }
52 }
53 if (val > 0) {
54 val = -val;
55 }
56
57 if (val != 0) {
58 char error_buf[200];
59 mbedtls_strerror(val, error_buf, 200);
60 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf);
61 }
62
63 #if defined(_WIN32)
64 mbedtls_printf(" + Press Enter to exit this program.\n");
65 fflush(stdout); getchar();
66 #endif
67
68 mbedtls_exit(val);
69 }
70 #endif /* MBEDTLS_ERROR_C */
71