blob: 39d64802ecce6b31fd620b6c6a5755c630a480ba [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker9d781402011-05-09 16:17:09 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker9d781402011-05-09 16:17:09 +00007 *
Paul Bakker9d781402011-05-09 16:17:09 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker9d781402011-05-09 16:17:09 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker9d781402011-05-09 16:17:09 +000028
Paul Bakker0464dd92014-07-09 10:16:18 +020029#if defined(POLARSSL_ERROR_C) || defined(POLARSSL_ERROR_STRERROR_DUMMY)
Paul Bakkerb2a11402013-06-24 13:02:12 +020030#include "polarssl/error.h"
Paul Bakker0464dd92014-07-09 10:16:18 +020031#endif
32
33#if defined(POLARSSL_ERROR_C)
Paul Bakkerb2a11402013-06-24 13:02:12 +020034
Paul Bakker9d781402011-05-09 16:17:09 +000035HEADER_INCLUDED
36
37#include <string.h>
38
Paul Bakker17d99fc2013-11-21 17:34:13 +010039#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
40 !defined(EFI32)
Paul Bakkerd0a345e2011-11-10 13:03:42 +000041#define snprintf _snprintf
42#endif
43
Paul Bakkere2ab84f2013-06-29 18:24:32 +020044void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000045{
46 size_t len;
47 int use_ret;
48
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020049 if( buflen == 0 )
50 return;
51
Paul Bakker9d781402011-05-09 16:17:09 +000052 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020053 /* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
54 buflen -= 1;
55
Paul Bakker9d781402011-05-09 16:17:09 +000056 if( ret < 0 )
57 ret = -ret;
58
59 if( ret & 0xFF80 )
60 {
61 use_ret = ret & 0xFF80;
62
63 // High level error codes
64 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020065 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +000066HIGH_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020067 // END generated code
68
Paul Bakker9d781402011-05-09 16:17:09 +000069 if( strlen( buf ) == 0 )
70 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 snprintf( buf + len, buflen - len, " : " );
89
90 buf += len + 3;
91 buflen -= len + 3;
92 }
93
94 // Low level error codes
95 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020096 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +000097LOW_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020098 // END generated code
99
Paul Bakker9d781402011-05-09 16:17:09 +0000100 if( strlen( buf ) != 0 )
101 return;
102
103 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
104}
105
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200106#if defined(POLARSSL_ERROR_STRERROR_BC)
107void error_strerror( int ret, char *buf, size_t buflen )
108{
Paul Bakkerb887f112013-10-11 15:09:40 +0200109 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200110}
111#endif /* POLARSSL_ERROR_STRERROR_BC */
112
Paul Bakkera0234372013-03-20 14:42:21 +0100113#else /* POLARSSL_ERROR_C */
114
115#if defined(POLARSSL_ERROR_STRERROR_DUMMY)
116
117#include <string.h>
118
119/*
120 * Provide an non-function in case POLARSSL_ERROR_C is not defined
121 */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200122void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100123{
124 ((void) ret);
125
126 if( buflen > 0 )
127 buf[0] = '\0';
128}
129
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200130#if defined(POLARSSL_ERROR_STRERROR_BC)
131void error_strerror( int ret, char *buf, size_t buflen )
132{
Paul Bakkerb887f112013-10-11 15:09:40 +0200133 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200134}
135#endif /* POLARSSL_ERROR_STRERROR_BC */
Paul Bakkera0234372013-03-20 14:42:21 +0100136#endif /* POLARSSL_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200137
Paul Bakker9a736322012-11-14 12:39:52 +0000138#endif /* POLARSSL_ERROR_C */