blob: 34af46e00eb967645319c39fbaa71a49b26269e6 [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é-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.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
Paul Bakker747a83a2014-02-01 22:50:07 +010033#ifdef __cplusplus
34extern "C" {
35#endif
36
Paul Bakker088c5c52014-04-25 11:11:10 +020037/**
38 * \name SECTION: Module settings
39 *
40 * The configuration options you can set for this module are in this section.
41 * Either change them in config.h or define them on the compiler command line.
42 * \{
43 */
44
45#if !defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010047#include <stdlib.h>
Rich Evans46b0a8d2015-01-30 10:47:32 +000048#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF)
49#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
50#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020051#if !defined(POLARSSL_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +010052#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020053#endif
54#if !defined(POLARSSL_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +010055#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020056#endif
57#if !defined(POLARSSL_PLATFORM_STD_MALLOC)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010058#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020059#endif
60#if !defined(POLARSSL_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010061#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020062#endif
Rich Evansc39cb492015-01-30 12:01:34 +000063#if !defined(POLARSSL_PLATFORM_STD_EXIT)
64#define POLARSSL_PLATFORM_STD_EXIT exit /**< Default free to use */
65#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020066#else /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +020067#if defined(POLARSSL_PLATFORM_STD_MEM_HDR)
68#include POLARSSL_PLATFORM_STD_MEM_HDR
69#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020070#endif /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */
71
72/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +010073
74/*
75 * The function pointers for malloc and free
76 */
Paul Bakkera9066cf2014-02-05 15:13:04 +010077#if defined(POLARSSL_PLATFORM_MEMORY)
Manuel Pégourié-Gonnardfb57e642015-03-05 13:38:29 +000078#if defined(POLARSSL_PLATFORM_FREE_MACRO) && \
79 defined(POLARSSL_PLATFORM_MALLOC_MACRO)
80#define polarssl_free POLARSSL_PLATFORM_FREE_MACRO
81#define polarssl_malloc POLARSSL_PLATFORM_MALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +000082#else
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010083extern void * (*polarssl_malloc)( size_t len );
84extern void (*polarssl_free)( void *ptr );
85
86/**
87 * \brief Set your own memory implementation function pointers
88 *
89 * \param malloc_func the malloc function implementation
90 * \param free_func the free function implementation
91 *
92 * \return 0 if successful
93 */
94int platform_set_malloc_free( void * (*malloc_func)( size_t ),
95 void (*free_func)( void * ) );
Rich Evans401bb902015-02-10 12:28:15 +000096#endif /* POLARSSL_PLATFORM_FREE_MACRO && POLARSSL_PLATFORM_MALLOC_MACRO */
Rich Evans00ab4702015-02-06 13:43:58 +000097#else /* !POLARSSL_PLATFORM_MEMORY */
Manuel Pégourié-Gonnardfb57e642015-03-05 13:38:29 +000098#define polarssl_free free
99#define polarssl_malloc malloc
Rich Evans401bb902015-02-10 12:28:15 +0000100#endif /* POLARSSL_PLATFORM_MEMORY && !POLARSSL_PLATFORM_{FREE,MALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100101
102/*
103 * The function pointers for fprintf
104 */
105#if defined(POLARSSL_PLATFORM_FPRINTF_ALT)
106extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... );
107
Rich Evansc39cb492015-01-30 12:01:34 +0000108/**
109 * \brief Set your own fprintf function pointer
110 *
111 * \param fprintf_func the fprintf function implementation
112 *
113 * \return 0
114 */
Paul Bakker747a83a2014-02-01 22:50:07 +0100115int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
116 ... ) );
117#else
Rich Evans16f8cd82015-02-06 16:14:34 +0000118#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO)
119#define polarssl_fprintf POLARSSL_PLATFORM_FPRINTF_MACRO
120#else
Paul Bakker747a83a2014-02-01 22:50:07 +0100121#define polarssl_fprintf fprintf
Rich Evans16f8cd82015-02-06 16:14:34 +0000122#endif /* POLARSSL_PLATFORM_FPRINTF_MACRO */
Rich Evansc39cb492015-01-30 12:01:34 +0000123#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */
124
125/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000126 * The function pointers for printf
127 */
128#if defined(POLARSSL_PLATFORM_PRINTF_ALT)
129extern int (*polarssl_printf)( const char *format, ... );
130
131/**
132 * \brief Set your own printf function pointer
133 *
134 * \param printf_func the printf function implementation
135 *
136 * \return 0
137 */
138int platform_set_printf( int (*printf_func)( const char *, ... ) );
139#else /* !POLARSSL_PLATFORM_PRINTF_ALT */
140#if defined(POLARSSL_PLATFORM_PRINTF_MACRO)
141#define polarssl_printf POLARSSL_PLATFORM_PRINTF_MACRO
142#else
143#define polarssl_printf printf
144#endif /* POLARSSL_PLATFORM_PRINTF_MACRO */
145#endif /* POLARSSL_PLATFORM_PRINTF_ALT */
146
147/*
148 * The function pointers for snprintf
149 */
150#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT)
151extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... );
152
153/**
154 * \brief Set your own snprintf function pointer
155 *
156 * \param snprintf_func the snprintf function implementation
157 *
158 * \return 0
159 */
160int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
161 const char * format, ... ) );
162#else /* POLARSSL_PLATFORM_SNPRINTF_ALT */
163#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO)
164#define polarssl_snprintf POLARSSL_PLATFORM_SNPRINTF_MACRO
165#else
166#define polarssl_snprintf snprintf
167#endif /* POLARSSL_PLATFORM_SNPRINTF_MACRO */
168#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */
169
170/*
Rich Evansc39cb492015-01-30 12:01:34 +0000171 * The function pointers for exit
172 */
173#if defined(POLARSSL_PLATFORM_EXIT_ALT)
174extern void (*polarssl_exit)( int status );
175
176/**
177 * \brief Set your own exit function pointer
178 *
179 * \param exit_func the exit function implementation
180 *
181 * \return 0
182 */
183int platform_set_exit( void (*exit_func)( int status ) );
184#else
Rich Evans16f8cd82015-02-06 16:14:34 +0000185#if defined(POLARSSL_PLATFORM_EXIT_MACRO)
186#define polarssl_exit POLARSSL_PLATFORM_EXIT_MACRO
187#else
Rich Evansc39cb492015-01-30 12:01:34 +0000188#define polarssl_exit exit
Rich Evans16f8cd82015-02-06 16:14:34 +0000189#endif /* POLARSSL_PLATFORM_EXIT_MACRO */
Rich Evansc39cb492015-01-30 12:01:34 +0000190#endif /* POLARSSL_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100191
192#ifdef __cplusplus
193}
194#endif
195
196#endif /* platform.h */