blob: 23dba94f5dd91a571f29f704fad31fab68d8edda [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/*
2 * Platform abstraction layer
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker747a83a2014-02-01 22:50:07 +01005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker747a83a2014-02-01 22:50:07 +01007 *
Paul Bakker747a83a2014-02-01 22:50:07 +01008 * 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 Bakker747a83a2014-02-01 22:50:07 +010028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Paul Bakker747a83a2014-02-01 22:50:07 +010030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/platform.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_PLATFORM_MEMORY)
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020034#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
35static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010036{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020037 ((void) n);
38 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010039 return( NULL );
40}
41
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020042#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
43#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010046static void platform_free_uninit( void *ptr )
47{
48 ((void) ptr);
49}
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
52#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010053
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020054void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010056
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020057int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010058 void (*free_func)( void * ) )
59{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020060 mbedtls_calloc = calloc_func;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_free = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010062 return( 0 );
63}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#endif /* MBEDTLS_PLATFORM_MEMORY */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010065
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020066#if defined(_WIN32)
67#include <stdarg.h>
68int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
69{
70 int ret;
71 va_list argp;
72
73 va_start( argp, fmt );
74 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
75 va_end( argp );
76
77 return( ret );
78}
79#endif
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
82#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +000083/*
84 * Make dummy function to prevent NULL pointer dereferences
85 */
86static int platform_snprintf_uninit( char * s, size_t n,
87 const char * format, ... )
88{
89 ((void) s);
90 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +010091 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +000092 return( 0 );
93}
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
96#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +000097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +000099 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000103 const char * format,
104 ... ) )
105{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000107 return( 0 );
108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
112#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100113/*
114 * Make dummy function to prevent NULL pointer dereferences
115 */
116static int platform_printf_uninit( const char *format, ... )
117{
118 ((void) format);
119 return( 0 );
120}
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
123#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100130 return( 0 );
131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
135#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100136/*
137 * Make dummy function to prevent NULL pointer dereferences
138 */
139static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
140{
141 ((void) stream);
142 ((void) format);
143 return( 0 );
144}
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
147#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
150 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100155 return( 0 );
156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
160#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000161/*
162 * Make dummy function to prevent NULL pointer dereferences
163 */
164static void platform_exit_uninit( int status )
165{
166 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
170#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000171
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100172void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000175{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000177 return( 0 );
178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#endif /* MBEDTLS_PLATFORM_C */