1/*
2 *  Error message information
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_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
29#include "mbedtls/error.h"
30#include <string.h>
31#endif
32
33#if defined(MBEDTLS_PLATFORM_C)
34#include "mbedtls/platform.h"
35#else
36#define mbedtls_snprintf snprintf
37#define mbedtls_time_t   time_t
38#endif
39
40#if defined(MBEDTLS_ERROR_C)
41
42#include <stdio.h>
43
44HEADER_INCLUDED
45
46void mbedtls_strerror( int ret, char *buf, size_t buflen )
47{
48    size_t len;
49    int use_ret;
50
51    if( buflen == 0 )
52        return;
53
54    memset( buf, 0x00, buflen );
55
56    if( ret < 0 )
57        ret = -ret;
58
59    if( ret & 0xFF80 )
60    {
61        use_ret = ret & 0xFF80;
62
63        // High level error codes
64        //
65        // BEGIN generated code
66HIGH_LEVEL_CODE_CHECKS
67        // END generated code
68
69        if( strlen( buf ) == 0 )
70            mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
71    }
72
73    use_ret = ret & ~0xFF80;
74
75    if( use_ret == 0 )
76        return;
77
78    // If high level code is present, make a concatenation between both
79    // error strings.
80    //
81    len = strlen( buf );
82
83    if( len > 0 )
84    {
85        if( buflen - len < 5 )
86            return;
87
88        mbedtls_snprintf( buf + len, buflen - len, " : " );
89
90        buf += len + 3;
91        buflen -= len + 3;
92    }
93
94    // Low level error codes
95    //
96    // BEGIN generated code
97LOW_LEVEL_CODE_CHECKS
98    // END generated code
99
100    if( strlen( buf ) != 0 )
101        return;
102
103    mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
104}
105
106#else /* MBEDTLS_ERROR_C */
107
108#if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
109
110/*
111 * Provide an non-function in case MBEDTLS_ERROR_C is not defined
112 */
113void mbedtls_strerror( int ret, char *buf, size_t buflen )
114{
115    ((void) ret);
116
117    if( buflen > 0 )
118        buf[0] = '\0';
119}
120
121#endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
122
123#endif /* MBEDTLS_ERROR_C */
124