Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Error message information |
| 3 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 7 | * |
| 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 Bakker | ac6168b | 2013-06-06 14:52:23 +0200 | [diff] [blame] | 27 | #include "polarssl/error.h" |
| 28 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 29 | HEADER_INCLUDED |
| 30 | |
| 31 | #include <string.h> |
| 32 | |
Paul Bakker | d0a345e | 2011-11-10 13:03:42 +0000 | [diff] [blame] | 33 | #if defined _MSC_VER && !defined snprintf |
| 34 | #define snprintf _snprintf |
| 35 | #endif |
| 36 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 37 | void 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 | // |
| 53 | HIGH_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 | // |
| 81 | LOW_LEVEL_CODE_CHECKS |
| 82 | if( strlen( buf ) != 0 ) |
| 83 | return; |
| 84 | |
| 85 | snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); |
| 86 | } |
| 87 | |
Paul Bakker | 93bab7f | 2013-04-12 13:17:02 +0200 | [diff] [blame] | 88 | #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 | */ |
| 97 | void 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 Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 106 | #endif /* POLARSSL_ERROR_C */ |