blob: 3d463885c39bdd8ff459a7c0a8c1b579b1e6f723 [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
Manuel Pégourié-Gonnard7381ff02015-08-04 11:12:49 +02004 * Copyright (C) 2006-2015, 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 Bakkerd0a345e2011-11-10 13:03:42 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakker9d781402011-05-09 16:17:09 +000047{
48 size_t len;
49 int use_ret;
50
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020051 if( buflen == 0 )
52 return;
53
Paul Bakker9d781402011-05-09 16:17:09 +000054 memset( buf, 0x00, buflen );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020055
Paul Bakker9d781402011-05-09 16:17:09 +000056 if( ret < 0 )
57 ret = -ret;
58
59 if( ret & 0xFF80 )
60 {
61 use_ret = ret & 0xFF80;
62
63 // High level error codes
64 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020065 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +000066HIGH_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020067 // END generated code
68
Paul Bakker9d781402011-05-09 16:17:09 +000069 if( strlen( buf ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Paul Bakker9d781402011-05-09 16:17:09 +000071 }
72
73 use_ret = ret & ~0xFF80;
74
75 if( use_ret == 0 )
76 return;
77
78 // If high level code is present, make a concatenation between both
79 // error strings.
80 //
81 len = strlen( buf );
82
83 if( len > 0 )
84 {
85 if( buflen - len < 5 )
86 return;
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_snprintf( buf + len, buflen - len, " : " );
Paul Bakker9d781402011-05-09 16:17:09 +000089
90 buf += len + 3;
91 buflen -= len + 3;
92 }
93
94 // Low level error codes
95 //
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020096 // BEGIN generated code
Paul Bakker9d781402011-05-09 16:17:09 +000097LOW_LEVEL_CODE_CHECKS
Manuel Pégourié-Gonnardfe671f42014-04-11 18:06:44 +020098 // END generated code
99
Paul Bakker9d781402011-05-09 16:17:09 +0000100 if( strlen( buf ) != 0 )
101 return;
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
Paul Bakker9d781402011-05-09 16:17:09 +0000104}
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#else /* MBEDTLS_ERROR_C */
Paul Bakkera0234372013-03-20 14:42:21 +0100107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
Paul Bakkera0234372013-03-20 14:42:21 +0100109
Paul Bakkera0234372013-03-20 14:42:21 +0100110/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 * Provide an non-function in case MBEDTLS_ERROR_C is not defined
Paul Bakkera0234372013-03-20 14:42:21 +0100112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113void mbedtls_strerror( int ret, char *buf, size_t buflen )
Paul Bakkera0234372013-03-20 14:42:21 +0100114{
115 ((void) ret);
116
117 if( buflen > 0 )
118 buf[0] = '\0';
119}
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
Paul Bakkere2ab84f2013-06-29 18:24:32 +0200122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#endif /* MBEDTLS_ERROR_C */