blob: 91ed60c6f93a13241ea65cc97b60a8133564c7ed [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakker9d781402011-05-09 16:17:09 +00005 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker9d781402011-05-09 16:17:09 +00007 *
8 * 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
23#include "polarssl/config.h"
24
25#if defined(POLARSSL_ERROR_C)
26
Paul Bakkerac6168b2013-06-06 14:52:23 +020027#include "polarssl/error.h"
28
Paul Bakker9d781402011-05-09 16:17:09 +000029HEADER_INCLUDED
30
31#include <string.h>
32
Paul Bakkerd0a345e2011-11-10 13:03:42 +000033#if defined _MSC_VER && !defined snprintf
34#define snprintf _snprintf
35#endif
36
Paul Bakker9d781402011-05-09 16:17:09 +000037void error_strerror( int ret, char *buf, size_t buflen )
38{
39 size_t len;
40 int use_ret;
41
42 memset( buf, 0x00, buflen );
43
44 if( ret < 0 )
45 ret = -ret;
46
47 if( ret & 0xFF80 )
48 {
49 use_ret = ret & 0xFF80;
50
51 // High level error codes
52 //
53HIGH_LEVEL_CODE_CHECKS
54 if( strlen( buf ) == 0 )
55 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
56 }
57
58 use_ret = ret & ~0xFF80;
59
60 if( use_ret == 0 )
61 return;
62
63 // If high level code is present, make a concatenation between both
64 // error strings.
65 //
66 len = strlen( buf );
67
68 if( len > 0 )
69 {
70 if( buflen - len < 5 )
71 return;
72
73 snprintf( buf + len, buflen - len, " : " );
74
75 buf += len + 3;
76 buflen -= len + 3;
77 }
78
79 // Low level error codes
80 //
81LOW_LEVEL_CODE_CHECKS
82 if( strlen( buf ) != 0 )
83 return;
84
85 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
86}
87
Paul Bakker93bab7f2013-04-12 13:17:02 +020088#else /* POLARSSL_ERROR_C */
89
90#if defined(POLARSSL_ERROR_STRERROR_DUMMY)
91
92#include <string.h>
93
94/*
95 * Provide an non-function in case POLARSSL_ERROR_C is not defined
96 */
97void error_strerror( int ret, char *buf, size_t buflen )
98{
99 ((void) ret);
100
101 if( buflen > 0 )
102 buf[0] = '\0';
103}
104
105#endif /* POLARSSL_ERROR_STRERROR_DUMMY */
Paul Bakker9a736322012-11-14 12:39:52 +0000106#endif /* POLARSSL_ERROR_C */