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