1 /*
2  *  Translate error code to error string
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #define mbedtls_printf     printf
33 #endif
34 
35 #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
36 #include "mbedtls/error.h"
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #endif
42 
43 #define USAGE \
44     "\n usage: strerror <errorcode>\n" \
45     "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
46 
47 #if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
main(void)48 int main( void )
49 {
50     mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
51     return( 0 );
52 }
53 #else
main(int argc,char * argv[])54 int main( int argc, char *argv[] )
55 {
56     long int val;
57     char *end = argv[1];
58 
59     if( argc != 2 )
60     {
61         mbedtls_printf( USAGE );
62         return( 0 );
63     }
64 
65     val = strtol( argv[1], &end, 10 );
66     if( *end != '\0' )
67     {
68         val = strtol( argv[1], &end, 16 );
69         if( *end != '\0' )
70         {
71             mbedtls_printf( USAGE );
72             return( 0 );
73         }
74     }
75     if( val > 0 )
76         val = -val;
77 
78     if( val != 0 )
79     {
80         char error_buf[200];
81         mbedtls_strerror( val, error_buf, 200 );
82         mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
83     }
84 
85 #if defined(_WIN32)
86     mbedtls_printf( "  + Press Enter to exit this program.\n" );
87     fflush( stdout ); getchar();
88 #endif
89 
90     return( val );
91 }
92 #endif /* MBEDTLS_ERROR_C */
93