blob: b698427726bc98de16c04a724a917020a69b480e [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
Paul Bakker0464dd92014-07-09 10:16:18 +020032#if defined(POLARSSL_ERROR_C) || defined(POLARSSL_ERROR_STRERROR_DUMMY)
Paul Bakkerb2a11402013-06-24 13:02:12 +020033#include "polarssl/error.h"
Paul Bakker0464dd92014-07-09 10:16:18 +020034#endif
35
36#if defined(POLARSSL_ERROR_C)
Paul Bakkerb2a11402013-06-24 13:02:12 +020037
Paul Bakker9d781402011-05-09 16:17:09 +000038HEADER_INCLUDED
39
40#include <string.h>
41
Paul Bakker17d99fc2013-11-21 17:34:13 +010042#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
43 !defined(EFI32)
Paul Bakkerd0a345e2011-11-10 13:03:42 +000044#define snprintf _snprintf
45#endif
46
Paul Bakkere2ab84f2013-06-29 18:24:32 +020047void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000048{
49 size_t len;
50 int use_ret;
51
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020052 if( buflen == 0 )
53 return;
54
Paul Bakker9d781402011-05-09 16:17:09 +000055 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020056 /* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
57 buflen -= 1;
58
Paul Bakker9d781402011-05-09 16:17:09 +000059 if( ret < 0 )
60 ret = -ret;
61
62 if( ret & 0xFF80 )
63 {
64 use_ret = ret & 0xFF80;
65
66 // High level error codes
67 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020068 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +000069HIGH_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020070 // END generated code
71
Paul Bakker9d781402011-05-09 16:17:09 +000072 if( strlen( buf ) == 0 )
73 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
74 }
75
76 use_ret = ret & ~0xFF80;
77
78 if( use_ret == 0 )
79 return;
80
81 // If high level code is present, make a concatenation between both
82 // error strings.
83 //
84 len = strlen( buf );
85
86 if( len > 0 )
87 {
88 if( buflen - len < 5 )
89 return;
90
91 snprintf( buf + len, buflen - len, " : " );
92
93 buf += len + 3;
94 buflen -= len + 3;
95 }
96
97 // Low level error codes
98 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020099 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +0000100LOW_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200101 // END generated code
102
Paul Bakker9d781402011-05-09 16:17:09 +0000103 if( strlen( buf ) != 0 )
104 return;
105
106 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
107}
108
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200109#if defined(POLARSSL_ERROR_STRERROR_BC)
110void error_strerror( int ret, char *buf, size_t buflen )
111{
Paul Bakkerb887f112013-10-11 15:09:40 +0200112 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200113}
114#endif /* POLARSSL_ERROR_STRERROR_BC */
115
Paul Bakkera0234372013-03-20 14:42:21 +0100116#else /* POLARSSL_ERROR_C */
117
118#if defined(POLARSSL_ERROR_STRERROR_DUMMY)
119
120#include <string.h>
121
122/*
123 * Provide an non-function in case POLARSSL_ERROR_C is not defined
124 */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200125void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100126{
127 ((void) ret);
128
129 if( buflen > 0 )
130 buf[0] = '\0';
131}
132
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200133#if defined(POLARSSL_ERROR_STRERROR_BC)
134void error_strerror( int ret, char *buf, size_t buflen )
135{
Paul Bakkerb887f112013-10-11 15:09:40 +0200136 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200137}
138#endif /* POLARSSL_ERROR_STRERROR_BC */
Paul Bakkera0234372013-03-20 14:42:21 +0100139#endif /* POLARSSL_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200140
Paul Bakker9a736322012-11-14 12:39:52 +0000141#endif /* POLARSSL_ERROR_C */