blob: 61f63cf5aa6f074d27cc38c9005d59120098f118 [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 Bakkerd0a345e2011-11-10 13:03:42 +000036#if defined _MSC_VER && !defined snprintf
37#define snprintf _snprintf
38#endif
39
Paul Bakkere2ab84f2013-06-29 18:24:32 +020040void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000041{
42 size_t len;
43 int use_ret;
44
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020045 if( buflen == 0 )
46 return;
47
Paul Bakker9d781402011-05-09 16:17:09 +000048 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020049 /* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
50 buflen -= 1;
51
Paul Bakker9d781402011-05-09 16:17:09 +000052 if( ret < 0 )
53 ret = -ret;
54
55 if( ret & 0xFF80 )
56 {
57 use_ret = ret & 0xFF80;
58
59 // High level error codes
60 //
61HIGH_LEVEL_CODE_CHECKS
62 if( strlen( buf ) == 0 )
63 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
64 }
65
66 use_ret = ret & ~0xFF80;
67
68 if( use_ret == 0 )
69 return;
70
71 // If high level code is present, make a concatenation between both
72 // error strings.
73 //
74 len = strlen( buf );
75
76 if( len > 0 )
77 {
78 if( buflen - len < 5 )
79 return;
80
81 snprintf( buf + len, buflen - len, " : " );
82
83 buf += len + 3;
84 buflen -= len + 3;
85 }
86
87 // Low level error codes
88 //
89LOW_LEVEL_CODE_CHECKS
90 if( strlen( buf ) != 0 )
91 return;
92
93 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
94}
95
Paul Bakkere2ab84f2013-06-29 18:24:32 +020096#if defined(POLARSSL_ERROR_STRERROR_BC)
97void error_strerror( int ret, char *buf, size_t buflen )
98{
Paul Bakkerb887f112013-10-11 15:09:40 +020099 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200100}
101#endif /* POLARSSL_ERROR_STRERROR_BC */
102
Paul Bakkera0234372013-03-20 14:42:21 +0100103#else /* POLARSSL_ERROR_C */
104
105#if defined(POLARSSL_ERROR_STRERROR_DUMMY)
106
107#include <string.h>
108
109/*
110 * Provide an non-function in case POLARSSL_ERROR_C is not defined
111 */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200112void polarssl_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100113{
114 ((void) ret);
115
116 if( buflen > 0 )
117 buf[0] = '\0';
118}
119
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200120#if defined(POLARSSL_ERROR_STRERROR_BC)
121void error_strerror( int ret, char *buf, size_t buflen )
122{
Paul Bakkerb887f112013-10-11 15:09:40 +0200123 polarssl_strerror( ret, buf, buflen );
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200124}
125#endif /* POLARSSL_ERROR_STRERROR_BC */
Paul Bakkera0234372013-03-20 14:42:21 +0100126#endif /* POLARSSL_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200127
Paul Bakker9a736322012-11-14 12:39:52 +0000128#endif /* POLARSSL_ERROR_C */