| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Error message information | 
|  | 3 | * | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2012, Brainspark B.V. | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 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 |  | 
| Paul Bakker | b2a1140 | 2013-06-24 13:02:12 +0200 | [diff] [blame] | 30 | #include "polarssl/error.h" | 
|  | 31 |  | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 32 | HEADER_INCLUDED | 
|  | 33 |  | 
|  | 34 | #include <string.h> | 
|  | 35 |  | 
| Paul Bakker | 17d99fc | 2013-11-21 17:34:13 +0100 | [diff] [blame] | 36 | #if defined(_MSC_VER) && !defined  snprintf && !defined(EFIX64) && \ | 
|  | 37 | !defined(EFI32) | 
| Paul Bakker | d0a345e | 2011-11-10 13:03:42 +0000 | [diff] [blame] | 38 | #define  snprintf  _snprintf | 
|  | 39 | #endif | 
|  | 40 |  | 
| Paul Bakker | e2ab84f | 2013-06-29 18:24:32 +0200 | [diff] [blame] | 41 | void polarssl_strerror( int ret, char *buf, size_t buflen ) | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 42 | { | 
|  | 43 | size_t len; | 
|  | 44 | int use_ret; | 
|  | 45 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 46 | if( buflen == 0 ) | 
|  | 47 | return; | 
|  | 48 |  | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 49 | memset( buf, 0x00, buflen ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 50 | /* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */ | 
|  | 51 | buflen -= 1; | 
|  | 52 |  | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 53 | if( ret < 0 ) | 
|  | 54 | ret = -ret; | 
|  | 55 |  | 
|  | 56 | if( ret & 0xFF80 ) | 
|  | 57 | { | 
|  | 58 | use_ret = ret & 0xFF80; | 
|  | 59 |  | 
|  | 60 | // High level error codes | 
|  | 61 | // | 
|  | 62 | HIGH_LEVEL_CODE_CHECKS | 
|  | 63 | if( strlen( buf ) == 0 ) | 
|  | 64 | snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | use_ret = ret & ~0xFF80; | 
|  | 68 |  | 
|  | 69 | if( use_ret == 0 ) | 
|  | 70 | return; | 
|  | 71 |  | 
|  | 72 | // If high level code is present, make a concatenation between both | 
|  | 73 | // error strings. | 
|  | 74 | // | 
|  | 75 | len = strlen( buf ); | 
|  | 76 |  | 
|  | 77 | if( len > 0 ) | 
|  | 78 | { | 
|  | 79 | if( buflen - len < 5 ) | 
|  | 80 | return; | 
|  | 81 |  | 
|  | 82 | snprintf( buf + len, buflen - len, " : " ); | 
|  | 83 |  | 
|  | 84 | buf += len + 3; | 
|  | 85 | buflen -= len + 3; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | // Low level error codes | 
|  | 89 | // | 
|  | 90 | LOW_LEVEL_CODE_CHECKS | 
|  | 91 | if( strlen( buf ) != 0 ) | 
|  | 92 | return; | 
|  | 93 |  | 
|  | 94 | snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); | 
|  | 95 | } | 
|  | 96 |  | 
| Paul Bakker | e2ab84f | 2013-06-29 18:24:32 +0200 | [diff] [blame] | 97 | #if defined(POLARSSL_ERROR_STRERROR_BC) | 
|  | 98 | void error_strerror( int ret, char *buf, size_t buflen ) | 
|  | 99 | { | 
| Paul Bakker | b887f11 | 2013-10-11 15:09:40 +0200 | [diff] [blame] | 100 | polarssl_strerror( ret, buf, buflen ); | 
| Paul Bakker | e2ab84f | 2013-06-29 18:24:32 +0200 | [diff] [blame] | 101 | } | 
|  | 102 | #endif /* POLARSSL_ERROR_STRERROR_BC */ | 
|  | 103 |  | 
| Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 104 | #else /* POLARSSL_ERROR_C */ | 
|  | 105 |  | 
|  | 106 | #if defined(POLARSSL_ERROR_STRERROR_DUMMY) | 
|  | 107 |  | 
|  | 108 | #include <string.h> | 
|  | 109 |  | 
|  | 110 | /* | 
|  | 111 | * Provide an non-function in case POLARSSL_ERROR_C is not defined | 
|  | 112 | */ | 
| Paul Bakker | e2ab84f | 2013-06-29 18:24:32 +0200 | [diff] [blame] | 113 | void polarssl_strerror( int ret, char *buf, size_t buflen ) | 
| Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 114 | { | 
|  | 115 | ((void) ret); | 
|  | 116 |  | 
|  | 117 | if( buflen > 0 ) | 
|  | 118 | buf[0] = '\0'; | 
|  | 119 | } | 
|  | 120 |  | 
| Paul Bakker | e2ab84f | 2013-06-29 18:24:32 +0200 | [diff] [blame] | 121 | #if defined(POLARSSL_ERROR_STRERROR_BC) | 
|  | 122 | void error_strerror( int ret, char *buf, size_t buflen ) | 
|  | 123 | { | 
| Paul Bakker | b887f11 | 2013-10-11 15:09:40 +0200 | [diff] [blame] | 124 | polarssl_strerror( ret, buf, buflen ); | 
| Paul Bakker | e2ab84f | 2013-06-29 18:24:32 +0200 | [diff] [blame] | 125 | } | 
|  | 126 | #endif /* POLARSSL_ERROR_STRERROR_BC */ | 
| Paul Bakker | a023437 | 2013-03-20 14:42:21 +0100 | [diff] [blame] | 127 | #endif /* POLARSSL_ERROR_STRERROR_DUMMY */ | 
| Paul Bakker | e2ab84f | 2013-06-29 18:24:32 +0200 | [diff] [blame] | 128 |  | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 129 | #endif /* POLARSSL_ERROR_C */ |