blob: 7449d85abce0bfce02ad1f21d5567f8e577c96e4 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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"
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000031#include <string.h>
Paul Bakker0464dd92014-07-09 10:16:18 +020032#endif
33
Rich Evans6aa04bc2015-01-30 11:18:42 +000034#if defined(POLARSSL_PLATFORM_C)
35#include "polarssl/platform.h"
36#else
37#define polarssl_snprintf snprintf
38#endif
39
Paul Bakker0464dd92014-07-09 10:16:18 +020040#if defined(POLARSSL_ERROR_C)
Paul Bakkerb2a11402013-06-24 13:02:12 +020041
Paul Bakkerdaae3b72015-02-08 15:49:54 +010042#include <stdio.h>
Paul Bakker9d781402011-05-09 16:17:09 +000043
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000044HEADER_INCLUDED
Paul Bakker17d99fc2013-11-21 17:34:13 +010045#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
46 !defined(EFI32)
Paul Bakkerd0a345e2011-11-10 13:03:42 +000047#define snprintf _snprintf
48#endif
49
Paul Bakkere2ab84f2013-06-29 18:24:32 +020050void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000051{
52 size_t len;
53 int use_ret;
54
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020055 if( buflen == 0 )
56 return;
57
Paul Bakker9d781402011-05-09 16:17:09 +000058 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020059 /* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
60 buflen -= 1;
61
Paul Bakker9d781402011-05-09 16:17:09 +000062 if( ret < 0 )
63 ret = -ret;
64
65 if( ret & 0xFF80 )
66 {
67 use_ret = ret & 0xFF80;
68
69 // High level error codes
70 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020071 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +000072HIGH_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020073 // END generated code
74
Paul Bakker9d781402011-05-09 16:17:09 +000075 if( strlen( buf ) == 0 )
Rich Evans6aa04bc2015-01-30 11:18:42 +000076 polarssl_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Paul Bakker9d781402011-05-09 16:17:09 +000077 }
78
79 use_ret = ret & ~0xFF80;
80
81 if( use_ret == 0 )
82 return;
83
84 // If high level code is present, make a concatenation between both
85 // error strings.
86 //
87 len = strlen( buf );
88
89 if( len > 0 )
90 {
91 if( buflen - len < 5 )
92 return;
93
Rich Evans6aa04bc2015-01-30 11:18:42 +000094 polarssl_snprintf( buf + len, buflen - len, " : " );
Paul Bakker9d781402011-05-09 16:17:09 +000095
96 buf += len + 3;
97 buflen -= len + 3;
98 }
99
100 // Low level error codes
101 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200102 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +0000103LOW_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200104 // END generated code
105
Paul Bakker9d781402011-05-09 16:17:09 +0000106 if( strlen( buf ) != 0 )
107 return;
108
Rich Evans6aa04bc2015-01-30 11:18:42 +0000109 polarssl_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Paul Bakker9d781402011-05-09 16:17:09 +0000110}
111
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200112#if defined(POLARSSL_ERROR_STRERROR_BC)
113void error_strerror( int ret, char *buf, size_t buflen )
114{
Paul Bakkerb887f112013-10-11 15:09:40 +0200115 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200116}
117#endif /* POLARSSL_ERROR_STRERROR_BC */
118
Paul Bakkera0234372013-03-20 14:42:21 +0100119#else /* POLARSSL_ERROR_C */
120
121#if defined(POLARSSL_ERROR_STRERROR_DUMMY)
122
Paul Bakkera0234372013-03-20 14:42:21 +0100123/*
124 * Provide an non-function in case POLARSSL_ERROR_C is not defined
125 */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200126void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100127{
128 ((void) ret);
129
130 if( buflen > 0 )
131 buf[0] = '\0';
132}
133
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200134#if defined(POLARSSL_ERROR_STRERROR_BC)
135void error_strerror( int ret, char *buf, size_t buflen )
136{
Paul Bakkerb887f112013-10-11 15:09:40 +0200137 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200138}
139#endif /* POLARSSL_ERROR_STRERROR_BC */
Paul Bakkera0234372013-03-20 14:42:21 +0100140#endif /* POLARSSL_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200141
Paul Bakker9a736322012-11-14 12:39:52 +0000142#endif /* POLARSSL_ERROR_C */