Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Translate error code to error string |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 22 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #endif |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 26 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/error.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 30 | |
| 31 | #include <stdio.h> |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #endif |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 35 | |
| 36 | #define USAGE \ |
Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 37 | "\n usage: strerror <errorcode>\n" \ |
| 38 | "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n" |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 41 | int main(void) |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 42 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n"); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 44 | mbedtls_exit(0); |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 45 | } |
| 46 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | int main(int argc, char *argv[]) |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 48 | { |
Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 49 | long int val; |
| 50 | char *end = argv[1]; |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 51 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | if (argc != 2) { |
| 53 | mbedtls_printf(USAGE); |
| 54 | mbedtls_exit(0); |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 57 | 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 Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 63 | } |
| 64 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 65 | if (val > 0) { |
Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 66 | val = -val; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 67 | } |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 68 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 69 | if (val != 0) { |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 70 | char error_buf[200]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | mbedtls_strerror(val, error_buf, 200); |
| 72 | mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf); |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 77 | fflush(stdout); getchar(); |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | mbedtls_exit(val); |
Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 81 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | #endif /* MBEDTLS_ERROR_C */ |