blob: 34295adc23e6cd5834ce712d36e35d5f14c672bc [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker747a83a2014-02-01 22:50:07 +010024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010028
29#if defined(POLARSSL_PLATFORM_C)
30
31#include "polarssl/platform.h"
32
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010033#if defined(POLARSSL_PLATFORM_MEMORY)
34#if !defined(POLARSSL_PLATFORM_STD_MALLOC)
35static void *platform_malloc_uninit( size_t len )
36{
37 ((void) len);
38 return( NULL );
39}
40
Manuel Pégourié-Gonnard74bc68a2014-04-02 13:20:00 +020041#define POLARSSL_PLATFORM_STD_MALLOC platform_malloc_uninit
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010042#endif /* !POLARSSL_PLATFORM_STD_MALLOC */
43
44#if !defined(POLARSSL_PLATFORM_STD_FREE)
45static void platform_free_uninit( void *ptr )
46{
47 ((void) ptr);
48}
49
Manuel Pégourié-Gonnard74bc68a2014-04-02 13:20:00 +020050#define POLARSSL_PLATFORM_STD_FREE platform_free_uninit
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010051#endif /* !POLARSSL_PLATFORM_STD_FREE */
52
53void * (*polarssl_malloc)( size_t ) = POLARSSL_PLATFORM_STD_MALLOC;
54void (*polarssl_free)( void * ) = POLARSSL_PLATFORM_STD_FREE;
55
56int platform_set_malloc_free( void * (*malloc_func)( size_t ),
57 void (*free_func)( void * ) )
58{
59 polarssl_malloc = malloc_func;
60 polarssl_free = free_func;
61 return( 0 );
62}
63#endif /* POLARSSL_PLATFORM_MEMORY */
64
Rich Evans46b0a8d2015-01-30 10:47:32 +000065#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT)
66#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF)
67/*
68 * Make dummy function to prevent NULL pointer dereferences
69 */
70static int platform_snprintf_uninit( char * s, size_t n,
71 const char * format, ... )
72{
73 ((void) s);
74 ((void) n);
75 ((void) format)
76 return( 0 );
77}
78
79#define POLARSSL_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
80#endif /* !POLARSSL_PLATFORM_STD_SNPRINTF */
81
82int (*polarssl_snprintf)( char * s, size_t n,
83 const char * format,
84 ... ) = POLARSSL_PLATFORM_STD_SNPRINTF;
85
86int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
87 const char * format,
88 ... ) )
89{
90 polarssl_snprintf = snprintf_func;
91 return( 0 );
92}
93#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */
94
Paul Bakker747a83a2014-02-01 22:50:07 +010095#if defined(POLARSSL_PLATFORM_PRINTF_ALT)
96#if !defined(POLARSSL_PLATFORM_STD_PRINTF)
97/*
98 * Make dummy function to prevent NULL pointer dereferences
99 */
100static int platform_printf_uninit( const char *format, ... )
101{
102 ((void) format);
103 return( 0 );
104}
105
106#define POLARSSL_PLATFORM_STD_PRINTF platform_printf_uninit
107#endif /* !POLARSSL_PLATFORM_STD_PRINTF */
108
109int (*polarssl_printf)( const char *, ... ) = POLARSSL_PLATFORM_STD_PRINTF;
110
111int platform_set_printf( int (*printf_func)( const char *, ... ) )
112{
113 polarssl_printf = printf_func;
114 return( 0 );
115}
116#endif /* POLARSSL_PLATFORM_PRINTF_ALT */
117
118#if defined(POLARSSL_PLATFORM_FPRINTF_ALT)
119#if !defined(POLARSSL_PLATFORM_STD_FPRINTF)
120/*
121 * Make dummy function to prevent NULL pointer dereferences
122 */
123static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
124{
125 ((void) stream);
126 ((void) format);
127 return( 0 );
128}
129
Paul Bakker10a9dd32014-04-25 11:18:34 +0200130#define POLARSSL_PLATFORM_STD_FPRINTF platform_fprintf_uninit
Paul Bakker747a83a2014-02-01 22:50:07 +0100131#endif /* !POLARSSL_PLATFORM_STD_FPRINTF */
132
133int (*polarssl_fprintf)( FILE *, const char *, ... ) =
134 POLARSSL_PLATFORM_STD_FPRINTF;
135
136int platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
137{
138 polarssl_fprintf = fprintf_func;
139 return( 0 );
140}
141#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */
142
Rich Evansc39cb492015-01-30 12:01:34 +0000143#if defined(POLARSSL_PLATFORM_EXIT_ALT)
144#if !defined(POLARSSL_STD_EXIT)
145/*
146 * Make dummy function to prevent NULL pointer dereferences
147 */
148static void platform_exit_uninit( int status )
149{
150 ((void) status);
151 return( 0 );
152}
153
154#define POLARSSL_STD_EXIT platform_exit_uninit
155#endif /* !POLARSSL_STD_EXIT */
156
157int (*polarssl_exit)( int status ) = POLARSSL_STD_EXIT;
158
159int platform_set_exit( void (*exit_func)( int status ) )
160{
161 polarssl_exit = exit_func;
162 return( 0 );
163}
164#endif /* POLARSSL_PLATFORM_EXIT_ALT */
165
Paul Bakker747a83a2014-02-01 22:50:07 +0100166#endif /* POLARSSL_PLATFORM_C */