From 0344c602eadc0802776b65ff90f0a02c856cf53c Mon Sep 17 00:00:00 2001 From: Tom Rini Date: Tue, 8 Oct 2024 13:56:50 -0600 Subject: Squashed 'lib/mbedtls/external/mbedtls/' content from commit 2ca6c285a0dd git-subtree-dir: lib/mbedtls/external/mbedtls git-subtree-split: 2ca6c285a0dd3f33982dd57299012dacab1ff206 --- programs/util/strerror.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 programs/util/strerror.c (limited to 'programs/util/strerror.c') diff --git a/programs/util/strerror.c b/programs/util/strerror.c new file mode 100644 index 00000000000..316f28614bb --- /dev/null +++ b/programs/util/strerror.c @@ -0,0 +1,61 @@ +/* + * Translate error code to error string + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + */ + +#include "mbedtls/build_info.h" + +#include "mbedtls/platform.h" + +#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) +#include "mbedtls/error.h" + +#include +#include +#include +#endif + +#define USAGE \ + "\n usage: strerror \n" \ + "\n where can be a decimal or hexadecimal (starts with 0x or -0x)\n" + +#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY) +int main(void) +{ + mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n"); + mbedtls_exit(0); +} +#else +int main(int argc, char *argv[]) +{ + long int val; + char *end = argv[1]; + + if (argc != 2) { + mbedtls_printf(USAGE); + mbedtls_exit(0); + } + + val = strtol(argv[1], &end, 10); + if (*end != '\0') { + val = strtol(argv[1], &end, 16); + if (*end != '\0') { + mbedtls_printf(USAGE); + return 0; + } + } + if (val > 0) { + val = -val; + } + + if (val != 0) { + char error_buf[200]; + mbedtls_strerror(val, error_buf, 200); + mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf); + } + + mbedtls_exit(val); +} +#endif /* MBEDTLS_ERROR_C */ -- cgit v1.2.3