blob: 3e76c301bb23c6aff811f3cc8957e06c356c40db [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/**
2 * \file platform.h
3 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief mbed TLS Platform abstraction layer
Paul Bakker747a83a2014-02-01 22:50:07 +01005 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker747a83a2014-02-01 22:50:07 +01007 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker747a83a2014-02-01 22:50:07 +01009 *
Paul Bakker747a83a2014-02-01 22:50:07 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_PLATFORM_H
25#define POLARSSL_PLATFORM_H
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker747a83a2014-02-01 22:50:07 +010028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010032
Manuel Pégourié-Gonnard0a155b82015-01-23 17:28:27 +000033/* Temporary compability hack for to keep the deprecated MEMORY_C working */
34#if defined(POLARSSL_MEMORY_C) && !defined(POLARSSL_PLATFORM_MEMORY)
35#define POLARSSL_PLATFORM_MEMORY
36#endif
37
Paul Bakker747a83a2014-02-01 22:50:07 +010038#ifdef __cplusplus
39extern "C" {
40#endif
41
Paul Bakker088c5c52014-04-25 11:11:10 +020042/**
43 * \name SECTION: Module settings
44 *
45 * The configuration options you can set for this module are in this section.
46 * Either change them in config.h or define them on the compiler command line.
47 * \{
48 */
49
50#if !defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010052#include <stdlib.h>
Rich Evans46b0a8d2015-01-30 10:47:32 +000053#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF)
54#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
55#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020056#if !defined(POLARSSL_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +010057#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020058#endif
59#if !defined(POLARSSL_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +010060#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020061#endif
62#if !defined(POLARSSL_PLATFORM_STD_MALLOC)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010063#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020064#endif
65#if !defined(POLARSSL_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010066#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020067#endif
Rich Evansc39cb492015-01-30 12:01:34 +000068#if !defined(POLARSSL_PLATFORM_STD_EXIT)
69#define POLARSSL_PLATFORM_STD_EXIT exit /**< Default free to use */
70#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020071#else /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +020072#if defined(POLARSSL_PLATFORM_STD_MEM_HDR)
73#include POLARSSL_PLATFORM_STD_MEM_HDR
74#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020075#endif /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */
76
77/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +010078
79/*
80 * The function pointers for malloc and free
81 */
Paul Bakkera9066cf2014-02-05 15:13:04 +010082#if defined(POLARSSL_PLATFORM_MEMORY)
Rich Evans401bb902015-02-10 12:28:15 +000083#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\
84 defined(POLARSSL_PLATFORM_MALLOC_MACRO)
85#define polarssl_free POLARSSL_PLATFORM_FREE_MACRO
86#define polarssl_malloc POLARSSL_PLATFORM_MALLOC_MACRO
87#else
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010088extern void * (*polarssl_malloc)( size_t len );
89extern void (*polarssl_free)( void *ptr );
90
91/**
92 * \brief Set your own memory implementation function pointers
93 *
94 * \param malloc_func the malloc function implementation
95 * \param free_func the free function implementation
96 *
97 * \return 0 if successful
98 */
99int platform_set_malloc_free( void * (*malloc_func)( size_t ),
100 void (*free_func)( void * ) );
Rich Evans401bb902015-02-10 12:28:15 +0000101#endif /* POLARSSL_PLATFORM_FREE_MACRO && POLARSSL_PLATFORM_MALLOC_MACRO */
Rich Evans00ab4702015-02-06 13:43:58 +0000102#else /* !POLARSSL_PLATFORM_MEMORY */
Rich Evans16f8cd82015-02-06 16:14:34 +0000103#define polarssl_free free
Rich Evans16f8cd82015-02-06 16:14:34 +0000104#define polarssl_malloc malloc
Rich Evans401bb902015-02-10 12:28:15 +0000105#endif /* POLARSSL_PLATFORM_MEMORY && !POLARSSL_PLATFORM_{FREE,MALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100106
107/*
108 * The function pointers for fprintf
109 */
110#if defined(POLARSSL_PLATFORM_FPRINTF_ALT)
111extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... );
112
Rich Evansc39cb492015-01-30 12:01:34 +0000113/**
114 * \brief Set your own fprintf function pointer
115 *
116 * \param fprintf_func the fprintf function implementation
117 *
118 * \return 0
119 */
Paul Bakker747a83a2014-02-01 22:50:07 +0100120int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
121 ... ) );
122#else
Rich Evans16f8cd82015-02-06 16:14:34 +0000123#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO)
124#define polarssl_fprintf POLARSSL_PLATFORM_FPRINTF_MACRO
125#else
Paul Bakker747a83a2014-02-01 22:50:07 +0100126#define polarssl_fprintf fprintf
Rich Evans16f8cd82015-02-06 16:14:34 +0000127#endif /* POLARSSL_PLATFORM_FPRINTF_MACRO */
Rich Evansc39cb492015-01-30 12:01:34 +0000128#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */
129
130/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000131 * The function pointers for printf
132 */
133#if defined(POLARSSL_PLATFORM_PRINTF_ALT)
134extern int (*polarssl_printf)( const char *format, ... );
135
136/**
137 * \brief Set your own printf function pointer
138 *
139 * \param printf_func the printf function implementation
140 *
141 * \return 0
142 */
143int platform_set_printf( int (*printf_func)( const char *, ... ) );
144#else /* !POLARSSL_PLATFORM_PRINTF_ALT */
145#if defined(POLARSSL_PLATFORM_PRINTF_MACRO)
146#define polarssl_printf POLARSSL_PLATFORM_PRINTF_MACRO
147#else
148#define polarssl_printf printf
149#endif /* POLARSSL_PLATFORM_PRINTF_MACRO */
150#endif /* POLARSSL_PLATFORM_PRINTF_ALT */
151
152/*
153 * The function pointers for snprintf
154 */
155#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT)
156extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... );
157
158/**
159 * \brief Set your own snprintf function pointer
160 *
161 * \param snprintf_func the snprintf function implementation
162 *
163 * \return 0
164 */
165int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
166 const char * format, ... ) );
167#else /* POLARSSL_PLATFORM_SNPRINTF_ALT */
168#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO)
169#define polarssl_snprintf POLARSSL_PLATFORM_SNPRINTF_MACRO
170#else
171#define polarssl_snprintf snprintf
172#endif /* POLARSSL_PLATFORM_SNPRINTF_MACRO */
173#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */
174
175/*
Rich Evansc39cb492015-01-30 12:01:34 +0000176 * The function pointers for exit
177 */
178#if defined(POLARSSL_PLATFORM_EXIT_ALT)
179extern void (*polarssl_exit)( int status );
180
181/**
182 * \brief Set your own exit function pointer
183 *
184 * \param exit_func the exit function implementation
185 *
186 * \return 0
187 */
188int platform_set_exit( void (*exit_func)( int status ) );
189#else
Rich Evans16f8cd82015-02-06 16:14:34 +0000190#if defined(POLARSSL_PLATFORM_EXIT_MACRO)
191#define polarssl_exit POLARSSL_PLATFORM_EXIT_MACRO
192#else
Rich Evansc39cb492015-01-30 12:01:34 +0000193#define polarssl_exit exit
Rich Evans16f8cd82015-02-06 16:14:34 +0000194#endif /* POLARSSL_PLATFORM_EXIT_MACRO */
Rich Evansc39cb492015-01-30 12:01:34 +0000195#endif /* POLARSSL_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100196
197#ifdef __cplusplus
198}
199#endif
200
201#endif /* platform.h */