diff --git a/include/polarssl/check_config.h b/include/polarssl/check_config.h index 51b124d6d..be7aefeee 100644 --- a/include/polarssl/check_config.h +++ b/include/polarssl/check_config.h @@ -198,6 +198,11 @@ #error "POLARSSL_PKCS11_C defined, but not all prerequisites" #endif +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) && ( defined(_WIN32)\ + && !defined(EFIX64) && !defined(EFI32) ) +#error "POLARSSL_PLATFORM_SNPRINTF_ALT defined but not available on Windows" +#endif + #if defined(POLARSSL_RSA_C) && ( !defined(POLARSSL_BIGNUM_C) || \ !defined(POLARSSL_OID_C) ) #error "POLARSSL_RSA_C defined, but not all prerequisites" diff --git a/include/polarssl/config.h b/include/polarssl/config.h index 02e8985af..75cf677c5 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -159,9 +159,13 @@ * * All these define require POLARSSL_PLATFORM_C to be defined! * + * WARNING: POLARSSL_PLATFORM_SNPRINTF_ALT is not available on Windows + * for compatibility reasons. + * * Uncomment a macro to enable alternate implementation of specific base * platform function */ +//#define POLARSSL_PLATFORM_SNPRINTF_ALT //#define POLARSSL_PLATFORM_PRINTF_ALT //#define POLARSSL_PLATFORM_FPRINTF_ALT /* \} name SECTION: System support */ @@ -1890,7 +1894,7 @@ * \def POLARSSL_PLATFORM_C * * Enable the platform abstraction layer that allows you to re-assign - * functions like malloc(), free(), printf(), fprintf() + * functions like malloc(), free(), snprintf(), printf(), fprintf() * * Module: library/platform.c * Caller: Most other .c files @@ -2238,6 +2242,7 @@ //#define POLARSSL_PLATFORM_STD_MEM_HDR /**< Header to include if POLARSSL_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */ //#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use, can be undefined */ //#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use, can be undefined */ //#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 */ diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h index 127b7fe3e..4844d2d01 100644 --- a/include/polarssl/platform.h +++ b/include/polarssl/platform.h @@ -50,6 +50,9 @@ extern "C" { #if !defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS) #include #include +#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF) +#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */ +#endif #if !defined(POLARSSL_PLATFORM_STD_PRINTF) #define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */ #endif @@ -92,6 +95,25 @@ int platform_set_malloc_free( void * (*malloc_func)( size_t ), #define polarssl_free free #endif /* POLARSSL_PLATFORM_MEMORY */ +/* + * The function pointers for snprintf + */ +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... ); + +/** + * \brief Set your own snprintf function pointer + * + * \param snprintf_func the snprintf function implementation + * + * \return 0 + */ +int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, + const char * format, ... ) ); +#else /* POLARSSL_PLATFORM_SNPRINTF_ALT */ +#define polarssl_snprintf snprintf +#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ + /* * The function pointers for printf */ diff --git a/library/platform.c b/library/platform.c index 3eb4b1a8e..8a26f7b84 100644 --- a/library/platform.c +++ b/library/platform.c @@ -62,6 +62,36 @@ int platform_set_malloc_free( void * (*malloc_func)( size_t ), } #endif /* POLARSSL_PLATFORM_MEMORY */ +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF) +/* + * 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); + ((void) format) + return( 0 ); +} + +#define POLARSSL_PLATFORM_STD_SNPRINTF platform_snprintf_uninit +#endif /* !POLARSSL_PLATFORM_STD_SNPRINTF */ + +int (*polarssl_snprintf)( char * s, size_t n, + const char * format, + ... ) = POLARSSL_PLATFORM_STD_SNPRINTF; + +int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, + const char * format, + ... ) ) +{ + polarssl_snprintf = snprintf_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ + #if defined(POLARSSL_PLATFORM_PRINTF_ALT) #if !defined(POLARSSL_PLATFORM_STD_PRINTF) /*