mbed TLS v2.0.0
platform.h
Go to the documentation of this file.
1 
24 #ifndef MBEDTLS_PLATFORM_H
25 #define MBEDTLS_PLATFORM_H
26 
27 #if !defined(MBEDTLS_CONFIG_FILE)
28 #include "config.h"
29 #else
30 #include MBEDTLS_CONFIG_FILE
31 #endif
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
45 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
46 #include <stdio.h>
47 #include <stdlib.h>
48 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
49 #if defined(_WIN32)
50 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
51 #else
52 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
53 #endif
54 #endif
55 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
56 #define MBEDTLS_PLATFORM_STD_PRINTF printf
57 #endif
58 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
59 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
60 #endif
61 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
62 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
63 #endif
64 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
65 #define MBEDTLS_PLATFORM_STD_FREE free
66 #endif
67 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
68 #define MBEDTLS_PLATFORM_STD_EXIT exit
69 #endif
70 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
71 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
72 #include MBEDTLS_PLATFORM_STD_MEM_HDR
73 #endif
74 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
75 
76 /* \} name SECTION: Module settings */
77 
78 /*
79  * The function pointers for calloc and free
80  */
81 #if defined(MBEDTLS_PLATFORM_MEMORY)
82 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
83  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
84 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
85 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
86 #else
87 /* For size_t */
88 #include <stddef.h>
89 extern void * (*mbedtls_calloc)( size_t n, size_t size );
90 extern void (*mbedtls_free)( void *ptr );
91 
100 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
101  void (*free_func)( void * ) );
102 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
103 #else /* !MBEDTLS_PLATFORM_MEMORY */
104 #define mbedtls_free free
105 #define mbedtls_calloc calloc
106 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
107 
108 /*
109  * The function pointers for fprintf
110  */
111 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
112 /* We need FILE * */
113 #include <stdio.h>
114 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
115 
123 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
124  ... ) );
125 #else
126 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
127 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
128 #else
129 #define mbedtls_fprintf fprintf
130 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
131 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
132 
133 /*
134  * The function pointers for printf
135  */
136 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
137 extern int (*mbedtls_printf)( const char *format, ... );
138 
146 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
147 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
148 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
149 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
150 #else
151 #define mbedtls_printf printf
152 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
153 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
154 
155 /*
156  * The function pointers for snprintf
157  *
158  * The snprintf implementation should conform to C99:
159  * - it *must* always correctly zero-terminate the buffer
160  * (except when n == 0, then it must leave the buffer untouched)
161  * - however it is acceptable to return -1 instead of the required length when
162  * the destination buffer is too short.
163  */
164 #if defined(_WIN32)
165 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
166 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
167 #endif
168 
169 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
170 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
171 
179 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
180  const char * format, ... ) );
181 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
182 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
183 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
184 #else
185 #define mbedtls_snprintf snprintf
186 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
187 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
188 
189 /*
190  * The function pointers for exit
191  */
192 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
193 extern void (*mbedtls_exit)( int status );
194 
202 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
203 #else
204 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
205 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
206 #else
207 #define mbedtls_exit exit
208 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
209 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif /* platform.h */
#define mbedtls_free
Definition: platform.h:104
Compatibility names (set of defines)
#define mbedtls_fprintf
Definition: platform.h:129
#define mbedtls_exit
Definition: platform.h:207
#define mbedtls_snprintf
Definition: platform.h:185
#define mbedtls_printf
Definition: platform.h:151