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