blob: 30ee257a5d157e01083232d5e006d30163e91234 [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +02004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker9d781402011-05-09 16:17:09 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker9d781402011-05-09 16:17:09 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker9d781402011-05-09 16:17:09 +000031
32#if defined(POLARSSL_ERROR_C)
33
Paul Bakkerb2a11402013-06-24 13:02:12 +020034#include "polarssl/error.h"
35
Paul Bakker9d781402011-05-09 16:17:09 +000036HEADER_INCLUDED
37
38#include <string.h>
39
Paul Bakker17d99fc2013-11-21 17:34:13 +010040#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
41 !defined(EFI32)
Paul Bakkerd0a345e2011-11-10 13:03:42 +000042#define snprintf _snprintf
43#endif
44
Paul Bakkere2ab84f2013-06-29 18:24:32 +020045void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000046{
47 size_t len;
48 int use_ret;
49
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020050 if( buflen == 0 )
51 return;
52
Paul Bakker9d781402011-05-09 16:17:09 +000053 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020054 /* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
55 buflen -= 1;
56
Paul Bakker9d781402011-05-09 16:17:09 +000057 if( ret < 0 )
58 ret = -ret;
59
60 if( ret & 0xFF80 )
61 {
62 use_ret = ret & 0xFF80;
63
64 // High level error codes
65 //
66HIGH_LEVEL_CODE_CHECKS
67 if( strlen( buf ) == 0 )
68 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
69 }
70
71 use_ret = ret & ~0xFF80;
72
73 if( use_ret == 0 )
74 return;
75
76 // If high level code is present, make a concatenation between both
77 // error strings.
78 //
79 len = strlen( buf );
80
81 if( len > 0 )
82 {
83 if( buflen - len < 5 )
84 return;
85
86 snprintf( buf + len, buflen - len, " : " );
87
88 buf += len + 3;
89 buflen -= len + 3;
90 }
91
92 // Low level error codes
93 //
94LOW_LEVEL_CODE_CHECKS
95 if( strlen( buf ) != 0 )
96 return;
97
98 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
99}
100
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200101#if defined(POLARSSL_ERROR_STRERROR_BC)
102void error_strerror( int ret, char *buf, size_t buflen )
103{
Paul Bakkerb887f112013-10-11 15:09:40 +0200104 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200105}
106#endif /* POLARSSL_ERROR_STRERROR_BC */
107
Paul Bakkera0234372013-03-20 14:42:21 +0100108#else /* POLARSSL_ERROR_C */
109
110#if defined(POLARSSL_ERROR_STRERROR_DUMMY)
111
112#include <string.h>
113
114/*
115 * Provide an non-function in case POLARSSL_ERROR_C is not defined
116 */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200117void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100118{
119 ((void) ret);
120
121 if( buflen > 0 )
122 buf[0] = '\0';
123}
124
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200125#if defined(POLARSSL_ERROR_STRERROR_BC)
126void error_strerror( int ret, char *buf, size_t buflen )
127{
Paul Bakkerb887f112013-10-11 15:09:40 +0200128 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200129}
130#endif /* POLARSSL_ERROR_STRERROR_BC */
Paul Bakkera0234372013-03-20 14:42:21 +0100131#endif /* POLARSSL_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200132
Paul Bakker9a736322012-11-14 12:39:52 +0000133#endif /* POLARSSL_ERROR_C */