2014-02-01 21:50:07 +00:00
|
|
|
/*
|
|
|
|
* Platform abstraction layer
|
|
|
|
*
|
2015-07-27 09:11:48 +00:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2014-02-01 21:50:07 +00:00
|
|
|
*
|
2015-03-06 13:17:10 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2014-02-01 21:50:07 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_MEMORY)
|
2015-05-26 14:15:20 +00:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
|
|
|
|
static void *platform_calloc_uninit( size_t n, size_t size )
|
2014-02-04 16:30:24 +00:00
|
|
|
{
|
2015-05-26 14:15:20 +00:00
|
|
|
((void) n);
|
|
|
|
((void) size);
|
2014-02-04 16:30:24 +00:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2015-05-26 14:15:20 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
|
2014-02-04 16:30:24 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_FREE)
|
2014-02-04 16:30:24 +00:00
|
|
|
static void platform_free_uninit( void *ptr )
|
|
|
|
{
|
|
|
|
((void) ptr);
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_FREE */
|
2014-02-04 16:30:24 +00:00
|
|
|
|
2015-05-26 14:15:20 +00:00
|
|
|
void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
|
2015-04-08 10:49:31 +00:00
|
|
|
void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
|
2014-02-04 16:30:24 +00:00
|
|
|
|
2015-05-26 14:15:20 +00:00
|
|
|
int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
|
2014-02-04 16:30:24 +00:00
|
|
|
void (*free_func)( void * ) )
|
|
|
|
{
|
2015-05-26 14:15:20 +00:00
|
|
|
mbedtls_calloc = calloc_func;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free = free_func;
|
2014-02-04 16:30:24 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_MEMORY */
|
2014-02-04 16:30:24 +00:00
|
|
|
|
2015-06-22 08:23:34 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <stdarg.h>
|
|
|
|
int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list argp;
|
|
|
|
|
2015-06-26 15:45:00 +00:00
|
|
|
/* Avoid calling the invalid parameter handler by checking ourselves */
|
|
|
|
if( s == NULL || n == 0 || fmt == NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
2015-06-22 08:23:34 +00:00
|
|
|
va_start( argp, fmt );
|
2015-07-08 14:46:13 +00:00
|
|
|
#if defined(_TRUNCATE)
|
2015-06-22 08:23:34 +00:00
|
|
|
ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
|
2015-07-08 14:46:13 +00:00
|
|
|
#else
|
|
|
|
ret = _vsnprintf( s, n, fmt, argp );
|
|
|
|
if( ret < 0 || (size_t) ret == n )
|
|
|
|
{
|
|
|
|
s[n-1] = '\0';
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-22 08:23:34 +00:00
|
|
|
va_end( argp );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
|
2015-01-30 10:47:32 +00:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
|
|
|
static int platform_snprintf_uninit( char * s, size_t n,
|
|
|
|
const char * format, ... )
|
|
|
|
{
|
|
|
|
((void) s);
|
|
|
|
((void) n);
|
2015-06-03 09:20:33 +00:00
|
|
|
((void) format);
|
2015-01-30 10:47:32 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
|
2015-01-30 10:47:32 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int (*mbedtls_snprintf)( char * s, size_t n,
|
2015-01-30 10:47:32 +00:00
|
|
|
const char * format,
|
2015-04-08 10:49:31 +00:00
|
|
|
... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
|
2015-01-30 10:47:32 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
|
2015-01-30 10:47:32 +00:00
|
|
|
const char * format,
|
|
|
|
... ) )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_snprintf = snprintf_func;
|
2015-01-30 10:47:32 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
|
2015-01-30 10:47:32 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
|
2014-02-01 21:50:07 +00:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
|
|
|
static int platform_printf_uninit( const char *format, ... )
|
|
|
|
{
|
|
|
|
((void) format);
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
|
2014-02-01 21:50:07 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf = printf_func;
|
2014-02-01 21:50:07 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
|
2014-02-01 21:50:07 +00:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
|
|
|
static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
|
|
|
|
{
|
|
|
|
((void) stream);
|
|
|
|
((void) format);
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
|
|
|
|
MBEDTLS_PLATFORM_STD_FPRINTF;
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
|
2014-02-01 21:50:07 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf = fprintf_func;
|
2014-02-01 21:50:07 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
|
2015-01-30 12:01:34 +00:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
|
|
|
static void platform_exit_uninit( int status )
|
|
|
|
{
|
|
|
|
((void) status);
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
|
2015-01-30 12:01:34 +00:00
|
|
|
|
2015-06-03 09:33:55 +00:00
|
|
|
void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
|
2015-01-30 12:01:34 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
|
2015-01-30 12:01:34 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_exit = exit_func;
|
2015-01-30 12:01:34 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
|
2015-01-30 12:01:34 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|