blob: 4bfd8a1c2c297dab887fbfc3013dca54b59b3fec [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker01cc3942012-05-08 08:36:15 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/error.h"
Rich Evans18b78c72015-02-11 14:06:19 +000026
27#include <stdio.h>
Paul Bakker01cc3942012-05-08 08:36:15 +000028#include <stdlib.h>
29#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000030#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000031
32#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020033 "\n usage: strerror <errorcode>\n" \
34 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Gilles Peskine449bd832023-01-11 14:50:10 +010037int main(void)
Paul Bakker01cc3942012-05-08 08:36:15 +000038{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010040 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000041}
42#else
Gilles Peskine449bd832023-01-11 14:50:10 +010043int main(int argc, char *argv[])
Paul Bakker01cc3942012-05-08 08:36:15 +000044{
Paul Bakker62534dd2013-06-30 12:45:07 +020045 long int val;
46 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (argc != 2) {
49 mbedtls_printf(USAGE);
50 mbedtls_exit(0);
Paul Bakker01cc3942012-05-08 08:36:15 +000051 }
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053 val = strtol(argv[1], &end, 10);
54 if (*end != '\0') {
55 val = strtol(argv[1], &end, 16);
56 if (*end != '\0') {
57 mbedtls_printf(USAGE);
58 return 0;
Paul Bakker62534dd2013-06-30 12:45:07 +020059 }
60 }
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (val > 0) {
Paul Bakker62534dd2013-06-30 12:45:07 +020062 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +000063 }
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065 if (val != 0) {
66 char error_buf[200];
67 mbedtls_strerror(val, error_buf, 200);
68 mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf);
69 }
70
71 mbedtls_exit(val);
Paul Bakker01cc3942012-05-08 08:36:15 +000072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#endif /* MBEDTLS_ERROR_C */