1/* 2 * Translate error code to error string 3 * 4 * Copyright The Mbed TLS Contributors 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. 18 */ 19 20#include "mbedtls/build_info.h" 21 22#include "mbedtls/platform.h" 23 24#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY) 25#include "mbedtls/error.h" 26 27#include <stdio.h> 28#include <stdlib.h> 29#include <string.h> 30#endif 31 32#define USAGE \ 33 "\n usage: strerror <errorcode>\n" \ 34 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n" 35 36#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY) 37int main(void) 38{ 39 mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n"); 40 mbedtls_exit(0); 41} 42#else 43int main(int argc, char *argv[]) 44{ 45 long int val; 46 char *end = argv[1]; 47 48 if (argc != 2) { 49 mbedtls_printf(USAGE); 50 mbedtls_exit(0); 51 } 52 53 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; 59 } 60 } 61 if (val > 0) { 62 val = -val; 63 } 64 65 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); 72} 73#endif /* MBEDTLS_ERROR_C */ 74