blob: d66f6eb8af2b42dd7bbc3c592221c8d41d139813 [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker9d781402011-05-09 16:17:09 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9d781402011-05-09 16:17:09 +00007 *
Paul Bakker9d781402011-05-09 16:17:09 +00008 * 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker9d781402011-05-09 16:17:09 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/error.h"
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000031#include <string.h>
Paul Bakker0464dd92014-07-09 10:16:18 +020032#endif
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Rich Evans6aa04bc2015-01-30 11:18:42 +000036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_snprintf snprintf
Rich Evans6aa04bc2015-01-30 11:18:42 +000038#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ERROR_C)
Paul Bakkerb2a11402013-06-24 13:02:12 +020041
Paul Bakkerdaae3b72015-02-08 15:49:54 +010042#include <stdio.h>
Paul Bakker9d781402011-05-09 16:17:09 +000043
Manuel Pégourié-Gonnardf5dc8ec2015-02-13 14:32:17 +000044HEADER_INCLUDED
Paul Bakker17d99fc2013-11-21 17:34:13 +010045#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
46 !defined(EFI32)
Paul Bakkerd0a345e2011-11-10 13:03:42 +000047#define snprintf _snprintf
48#endif
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000051{
52 size_t len;
53 int use_ret;
54
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020055 if( buflen == 0 )
56 return;
57
Paul Bakker9d781402011-05-09 16:17:09 +000058 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020059 /* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
60 buflen -= 1;
61
Paul Bakker9d781402011-05-09 16:17:09 +000062 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é-Gonnardfe671f42014-04-11 18:06:44 +020071 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +000072HIGH_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020073 // END generated code
74
Paul Bakker9d781402011-05-09 16:17:09 +000075 if( strlen( buf ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Paul Bakker9d781402011-05-09 16:17:09 +000077 }
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_snprintf( buf + len, buflen - len, " : " );
Paul Bakker9d781402011-05-09 16:17:09 +000095
96 buf += len + 3;
97 buflen -= len + 3;
98 }
99
100 // Low level error codes
101 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200102 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +0000103LOW_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +0200104 // END generated code
105
Paul Bakker9d781402011-05-09 16:17:09 +0000106 if( strlen( buf ) != 0 )
107 return;
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Paul Bakker9d781402011-05-09 16:17:09 +0000110}
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#else /* MBEDTLS_ERROR_C */
Paul Bakkera0234372013-03-20 14:42:21 +0100113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Paul Bakkera0234372013-03-20 14:42:21 +0100115
Paul Bakkera0234372013-03-20 14:42:21 +0100116/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 * Provide an non-function in case MBEDTLS_ERROR_C is not defined
Paul Bakkera0234372013-03-20 14:42:21 +0100118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100120{
121 ((void) ret);
122
123 if( buflen > 0 )
124 buf[0] = '\0';
125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#endif /* MBEDTLS_ERROR_C */