blob: 9f8368126784f3802325feca11da886e789849c0 [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
4 * Copyright (C) 2006-2010, Brainspark B.V.
5 *
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
30HEADER_INCLUDED
31
32#include <string.h>
33
Paul Bakkerd0a345e2011-11-10 13:03:42 +000034#if defined _MSC_VER && !defined snprintf
35#define snprintf _snprintf
36#endif
37
Paul Bakker9d781402011-05-09 16:17:09 +000038void error_strerror( int ret, char *buf, size_t buflen )
39{
40 size_t len;
41 int use_ret;
42
43 memset( buf, 0x00, buflen );
44
45 if( ret < 0 )
46 ret = -ret;
47
48 if( ret & 0xFF80 )
49 {
50 use_ret = ret & 0xFF80;
51
52 // High level error codes
53 //
54HIGH_LEVEL_CODE_CHECKS
55 if( strlen( buf ) == 0 )
56 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
57 }
58
59 use_ret = ret & ~0xFF80;
60
61 if( use_ret == 0 )
62 return;
63
64 // If high level code is present, make a concatenation between both
65 // error strings.
66 //
67 len = strlen( buf );
68
69 if( len > 0 )
70 {
71 if( buflen - len < 5 )
72 return;
73
74 snprintf( buf + len, buflen - len, " : " );
75
76 buf += len + 3;
77 buflen -= len + 3;
78 }
79
80 // Low level error codes
81 //
82LOW_LEVEL_CODE_CHECKS
83 if( strlen( buf ) != 0 )
84 return;
85
86 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
87}
88
89#endif /* POLARSSL_VERBOSE_ERROR */