blob: 77f183109443f68e93cd2c1db46dfa2a93365cd2 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker01cc3942012-05-08 08:36:15 +000018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000030
31#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000032#include <stdlib.h>
33#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000034#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000035
36#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020037 "\n usage: strerror <errorcode>\n" \
38 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041int main(void)
Paul Bakker01cc3942012-05-08 08:36:15 +000042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000045}
46#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047int main(int argc, char *argv[])
Paul Bakker01cc3942012-05-08 08:36:15 +000048{
Paul Bakker62534dd2013-06-30 12:45:07 +020049 long int val;
50 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052 if (argc != 2) {
53 mbedtls_printf(USAGE);
54 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000055 }
56
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 val = strtol(argv[1], &end, 10);
58 if (*end != '\0') {
59 val = strtol(argv[1], &end, 16);
60 if (*end != '\0') {
61 mbedtls_printf(USAGE);
62 return 0;
Paul Bakker62534dd2013-06-30 12:45:07 +020063 }
64 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 if (val > 0) {
Paul Bakker62534dd2013-06-30 12:45:07 +020066 val = -val;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 }
Paul Bakker01cc3942012-05-08 08:36:15 +000068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 if (val != 0) {
Paul Bakker01cc3942012-05-08 08:36:15 +000070 char error_buf[200];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 mbedtls_strerror(val, error_buf, 200);
72 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf);
Paul Bakker01cc3942012-05-08 08:36:15 +000073 }
74
75#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 mbedtls_printf(" + Press Enter to exit this program.\n");
77 fflush(stdout); getchar();
Paul Bakker01cc3942012-05-08 08:36:15 +000078#endif
79
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 mbedtls_exit(val);
Paul Bakker01cc3942012-05-08 08:36:15 +000081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#endif /* MBEDTLS_ERROR_C */