From 747a83a0f775361140592bb684ea2f30f97c40ad Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Sat, 1 Feb 2014 22:50:07 +0100 Subject: [PATCH] Platform abstraction layer for memory, printf and fprintf --- include/polarssl/config.h | 35 +++++++++++++++ include/polarssl/platform.h | 88 +++++++++++++++++++++++++++++++++++++ library/CMakeLists.txt | 1 + library/Makefile | 2 +- library/platform.c | 80 +++++++++++++++++++++++++++++++++ 5 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 include/polarssl/platform.h create mode 100644 library/platform.c diff --git a/include/polarssl/config.h b/include/polarssl/config.h index dd172ce16..be1005710 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -113,6 +113,24 @@ * Comment if your system does not support the IPv6 socket interface */ #define POLARSSL_HAVE_IPV6 + +/** + * \def POLARSSL_PLATFORM_XXX_ALT + * + * Uncomment a macro to let PolarSSL support the function in the platform + * abstraction layer. + * + * Example: In case you uncomment POLARSSL_PLATFORM_PRINTF_ALT, PolarSSL will + * provide a function "platform_set_printf()" that allows you to set an + * alternative printf function pointer. + * + * All these define require POLARSSL_PLATFORM_C to be defined! + * + * Uncomment a macro to enable alternate implementation of specific base + * platform function + */ +//#define POLARSSL_PLATFORM_PRINTF_ALT +//#define POLARSSL_PLATFORM_FPRINTF_ALT /* \} name SECTION: System support */ /** @@ -1620,6 +1638,19 @@ */ #define POLARSSL_PKCS12_C +/** + * \def POLARSSL_PLATFORM_C + * + * Enable the platform abstraction layer that allows you to re-assign + * functions like malloc(), free(), printf(), fprintf() + * + * Module: library/platform.c + * Caller: Most other .c files + * + * This module enables abstraction of common (libc) functions. + */ +#define POLARSSL_PLATFORM_C + /** * \def POLARSSL_RIPEMD160_C * @@ -1969,6 +2000,10 @@ #define POLARSSL_MEMORY_STDMALLOC malloc /**< Default allocator to use, can be undefined */ #define POLARSSL_MEMORY_STDFREE free /**< Default free to use, can be undefined */ +// Platform options +#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */ +#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */ + // SSL Cache options // #define SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */ diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h new file mode 100644 index 000000000..61b31190f --- /dev/null +++ b/include/polarssl/platform.h @@ -0,0 +1,88 @@ +/** + * \file platform.h + * + * \brief PolarSSL Platform abstraction layer + * + * Copyright (C) 2006-2014, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * 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. + */ +#ifndef POLARSSL_PLATFORM_H +#define POLARSSL_PLATFORM_H + +#include "config.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#if !defined(POLARSSL_CONFIG_OPTIONS) +#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */ +#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */ +#endif /* POLARSSL_CONFIG_OPTIONS */ + +/* + * The function pointers for malloc and free + */ +#if defined(POLARSSL_MEMORY_C) +#include "memory.h" +#else +#define polarssl_malloc malloc +#define polarssl_free free +#endif + +/* + * The function pointers for printf + */ +#if defined(POLARSSL_PLATFORM_PRINTF_ALT) +extern int (*polarssl_printf)( const char *format, ... ); + +/** + * \brief Set your own printf function pointer + * + * \param printf_func the printf function implementation + * + * \return 0 + */ +int platform_set_printf( int (*printf_func)( const char *, ... ) ); +#else +#define polarssl_printf printf +#endif + +/* + * The function pointers for fprintf + */ +#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) +extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... ); + +int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *, + ... ) ); +#else +#define polarssl_fprintf fprintf +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* platform.h */ diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 77aac2042..d948ca0c4 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -46,6 +46,7 @@ set(src pk_wrap.c pkparse.c pkwrite.c + platform.c ripemd160.c rsa.c sha1.c diff --git a/library/Makefile b/library/Makefile index 311e31637..931602426 100644 --- a/library/Makefile +++ b/library/Makefile @@ -52,7 +52,7 @@ OBJS= aes.o aesni.o arc4.o \ padlock.o pbkdf2.o pem.o \ pkcs5.o pkcs11.o pkcs12.o \ pk.o pk_wrap.o pkparse.o \ - pkwrite.o ripemd160.o \ + pkwrite.o platform.o ripemd160.o \ rsa.o sha1.o sha256.o \ sha512.o ssl_cache.o ssl_cli.o \ ssl_srv.o ssl_ciphersuites.o \ diff --git a/library/platform.c b/library/platform.c new file mode 100644 index 000000000..32f949ff0 --- /dev/null +++ b/library/platform.c @@ -0,0 +1,80 @@ +/* + * Platform abstraction layer + * + * Copyright (C) 2006-2014, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * 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. + */ + +#include "polarssl/config.h" + +#if defined(POLARSSL_PLATFORM_C) + +#include "polarssl/platform.h" + +#if defined(POLARSSL_PLATFORM_PRINTF_ALT) +#if !defined(POLARSSL_PLATFORM_STD_PRINTF) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_printf_uninit( const char *format, ... ) +{ + ((void) format); + return( 0 ); +} + +#define POLARSSL_PLATFORM_STD_PRINTF platform_printf_uninit +#endif /* !POLARSSL_PLATFORM_STD_PRINTF */ + +int (*polarssl_printf)( const char *, ... ) = POLARSSL_PLATFORM_STD_PRINTF; + +int platform_set_printf( int (*printf_func)( const char *, ... ) ) +{ + polarssl_printf = printf_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_PRINTF_ALT */ + +#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) +#if !defined(POLARSSL_PLATFORM_STD_FPRINTF) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) +{ + ((void) stream); + ((void) format); + return( 0 ); +} + +#define POLARSSL_PLATFORM_STD_fPRINTF platform_fprintf_uninit +#endif /* !POLARSSL_PLATFORM_STD_FPRINTF */ + +int (*polarssl_fprintf)( FILE *, const char *, ... ) = + POLARSSL_PLATFORM_STD_FPRINTF; + +int platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) +{ + polarssl_fprintf = fprintf_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */ + +#endif /* POLARSSL_PLATFORM_C */