blob: 98b5765c3f9152c853f5c5d748d063e5b7931d17 [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
Paul Bakker9a736322012-11-14 12:39:52 +00004 * Copyright (C) 2006-2012, 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
26#include "polarssl/config.h"
27
28#if defined(POLARSSL_ERROR_C)
29
Paul Bakkerb2a11402013-06-24 13:02:12 +020030#include "polarssl/error.h"
31
Paul Bakker9d781402011-05-09 16:17:09 +000032HEADER_INCLUDED
33
34#include <string.h>
35
Paul Bakkerfa6a6202013-10-28 18:48:30 +010036#if defined(EFIX64) || defined(EFI32)
37#include <stdio.h>
38#endif
39
40#if defined(_MSC_VER) && !defined snprintf
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 //
65HIGH_LEVEL_CODE_CHECKS
66 if( strlen( buf ) == 0 )
67 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
68 }
69
70 use_ret = ret & ~0xFF80;
71
72 if( use_ret == 0 )
73 return;
74
75 // If high level code is present, make a concatenation between both
76 // error strings.
77 //
78 len = strlen( buf );
79
80 if( len > 0 )
81 {
82 if( buflen - len < 5 )
83 return;
84
85 snprintf( buf + len, buflen - len, " : " );
86
87 buf += len + 3;
88 buflen -= len + 3;
89 }
90
91 // Low level error codes
92 //
93LOW_LEVEL_CODE_CHECKS
94 if( strlen( buf ) != 0 )
95 return;
96
97 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
98}
99
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200100#if defined(POLARSSL_ERROR_STRERROR_BC)
101void error_strerror( int ret, char *buf, size_t buflen )
102{
Paul Bakkerb887f112013-10-11 15:09:40 +0200103 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200104}
105#endif /* POLARSSL_ERROR_STRERROR_BC */
106
Paul Bakkera0234372013-03-20 14:42:21 +0100107#else /* POLARSSL_ERROR_C */
108
109#if defined(POLARSSL_ERROR_STRERROR_DUMMY)
110
111#include <string.h>
112
113/*
114 * Provide an non-function in case POLARSSL_ERROR_C is not defined
115 */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200116void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100117{
118 ((void) ret);
119
120 if( buflen > 0 )
121 buf[0] = '\0';
122}
123
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200124#if defined(POLARSSL_ERROR_STRERROR_BC)
125void error_strerror( int ret, char *buf, size_t buflen )
126{
Paul Bakkerb887f112013-10-11 15:09:40 +0200127 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200128}
129#endif /* POLARSSL_ERROR_STRERROR_BC */
Paul Bakkera0234372013-03-20 14:42:21 +0100130#endif /* POLARSSL_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200131
Paul Bakker9a736322012-11-14 12:39:52 +0000132#endif /* POLARSSL_ERROR_C */