Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | HEADER_INCLUDED |
| 31 | |
| 32 | #include <string.h> |
| 33 | |
Paul Bakker | d0a345e | 2011-11-10 13:03:42 +0000 | [diff] [blame^] | 34 | #if defined _MSC_VER && !defined snprintf |
| 35 | #define snprintf _snprintf |
| 36 | #endif |
| 37 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 38 | void 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 | // |
| 54 | HIGH_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 | // |
| 82 | LOW_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 */ |