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