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