platform.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * \file platform.h
  3. *
  4. * \brief mbed TLS Platform abstraction layer
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #ifndef MBEDTLS_PLATFORM_H
  24. #define MBEDTLS_PLATFORM_H
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /**
  34. * \name SECTION: Module settings
  35. *
  36. * The configuration options you can set for this module are in this section.
  37. * Either change them in config.h or define them on the compiler command line.
  38. * \{
  39. */
  40. extern int ets_snprintf(char *buf, unsigned int size, const char *format, ...);
  41. extern void *pvPortCalloc(unsigned int count, unsigned int size);
  42. extern void vPortFree( void *pv );
  43. #define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
  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 /**< Default snprintf to use */
  50. #else
  51. #define MBEDTLS_PLATFORM_STD_SNPRINTF ets_snprintf /**< Default snprintf to use */
  52. #endif
  53. #endif
  54. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  55. #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
  56. #endif
  57. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  58. #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
  59. #endif
  60. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  61. #define MBEDTLS_PLATFORM_STD_CALLOC pvPortCalloc /**< Default allocator to use */
  62. #endif
  63. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  64. #define MBEDTLS_PLATFORM_STD_FREE vPortFree /**< Default free to use */
  65. #endif
  66. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  67. #define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default free to use */
  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. /* \} name SECTION: Module settings */
  75. /*
  76. * The function pointers for calloc and free
  77. */
  78. #if defined(MBEDTLS_PLATFORM_MEMORY)
  79. #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
  80. defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
  81. #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
  82. #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
  83. #else
  84. /* For size_t */
  85. #include <stddef.h>
  86. extern void * (*mbedtls_calloc)( size_t n, size_t size );
  87. extern void (*mbedtls_free)( void *ptr );
  88. /**
  89. * \brief Set your own memory implementation function pointers
  90. *
  91. * \param calloc_func the calloc function implementation
  92. * \param free_func the free function implementation
  93. *
  94. * \return 0 if successful
  95. */
  96. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  97. void (*free_func)( void * ) );
  98. #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
  99. #else /* !MBEDTLS_PLATFORM_MEMORY */
  100. #define mbedtls_free vPortFree
  101. #define mbedtls_calloc pvPortCalloc
  102. #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
  103. /*
  104. * The function pointers for fprintf
  105. */
  106. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  107. /* We need FILE * */
  108. #include <stdio.h>
  109. extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
  110. /**
  111. * \brief Set your own fprintf function pointer
  112. *
  113. * \param fprintf_func the fprintf function implementation
  114. *
  115. * \return 0
  116. */
  117. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
  118. ... ) );
  119. #else
  120. #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
  121. #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
  122. #else
  123. #define mbedtls_fprintf fprintf
  124. #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
  125. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  126. /*
  127. * The function pointers for printf
  128. */
  129. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  130. extern int (*mbedtls_printf)( const char *format, ... );
  131. /**
  132. * \brief Set your own printf function pointer
  133. *
  134. * \param printf_func the printf function implementation
  135. *
  136. * \return 0
  137. */
  138. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
  139. #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
  140. #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
  141. #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
  142. #else
  143. #define mbedtls_printf os_printf
  144. #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
  145. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  146. /*
  147. * The function pointers for snprintf
  148. *
  149. * The snprintf implementation should conform to C99:
  150. * - it *must* always correctly zero-terminate the buffer
  151. * (except when n == 0, then it must leave the buffer untouched)
  152. * - however it is acceptable to return -1 instead of the required length when
  153. * the destination buffer is too short.
  154. */
  155. #if defined(_WIN32)
  156. /* For Windows (inc. MSYS2), we provide our own fixed implementation */
  157. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
  158. #endif
  159. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  160. extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
  161. /**
  162. * \brief Set your own snprintf function pointer
  163. *
  164. * \param snprintf_func the snprintf function implementation
  165. *
  166. * \return 0
  167. */
  168. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  169. const char * format, ... ) );
  170. #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  171. #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
  172. #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
  173. #else
  174. #define mbedtls_snprintf ets_snprintf
  175. #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
  176. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  177. /*
  178. * The function pointers for exit
  179. */
  180. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  181. extern void (*mbedtls_exit)( int status );
  182. /**
  183. * \brief Set your own exit function pointer
  184. *
  185. * \param exit_func the exit function implementation
  186. *
  187. * \return 0
  188. */
  189. int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
  190. #else
  191. #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
  192. #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
  193. #else
  194. #define mbedtls_exit exit
  195. #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
  196. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  197. #ifdef __cplusplus
  198. }
  199. #endif
  200. #endif /* platform.h */