Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
platform.c
00001 /* 00002 * Platform abstraction layer 00003 * 00004 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 00005 * SPDX-License-Identifier: Apache-2.0 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00008 * not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 * 00019 * This file is part of mbed TLS (https://tls.mbed.org) 00020 */ 00021 00022 #if !defined(MBEDTLS_CONFIG_FILE) 00023 #include "mbedtls/config.h" 00024 #else 00025 #include MBEDTLS_CONFIG_FILE 00026 #endif 00027 00028 #if defined(MBEDTLS_PLATFORM_C) 00029 00030 #include "mbedtls/platform.h" 00031 00032 #if defined(MBEDTLS_PLATFORM_MEMORY) 00033 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC) 00034 static void *platform_calloc_uninit( size_t n, size_t size ) 00035 { 00036 ((void) n); 00037 ((void) size); 00038 return( NULL ); 00039 } 00040 00041 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit 00042 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */ 00043 00044 #if !defined(MBEDTLS_PLATFORM_STD_FREE) 00045 static void platform_free_uninit( void *ptr ) 00046 { 00047 ((void) ptr); 00048 } 00049 00050 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit 00051 #endif /* !MBEDTLS_PLATFORM_STD_FREE */ 00052 00053 void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; 00054 void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE; 00055 00056 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), 00057 void (*free_func)( void * ) ) 00058 { 00059 mbedtls_calloc = calloc_func; 00060 mbedtls_free = free_func; 00061 return( 0 ); 00062 } 00063 #endif /* MBEDTLS_PLATFORM_MEMORY */ 00064 00065 #if defined(_WIN32) 00066 #include <stdarg.h> 00067 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) 00068 { 00069 int ret; 00070 va_list argp; 00071 00072 /* Avoid calling the invalid parameter handler by checking ourselves */ 00073 if( s == NULL || n == 0 || fmt == NULL ) 00074 return( -1 ); 00075 00076 va_start( argp, fmt ); 00077 #if defined(_TRUNCATE) 00078 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); 00079 #else 00080 ret = _vsnprintf( s, n, fmt, argp ); 00081 if( ret < 0 || (size_t) ret == n ) 00082 { 00083 s[n-1] = '\0'; 00084 ret = -1; 00085 } 00086 #endif 00087 va_end( argp ); 00088 00089 return( ret ); 00090 } 00091 #endif 00092 00093 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) 00094 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) 00095 /* 00096 * Make dummy function to prevent NULL pointer dereferences 00097 */ 00098 static int platform_snprintf_uninit( char * s, size_t n, 00099 const char * format, ... ) 00100 { 00101 ((void) s); 00102 ((void) n); 00103 ((void) format); 00104 return( 0 ); 00105 } 00106 00107 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit 00108 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */ 00109 00110 int (*mbedtls_snprintf)( char * s, size_t n, 00111 const char * format, 00112 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF; 00113 00114 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, 00115 const char * format, 00116 ... ) ) 00117 { 00118 mbedtls_snprintf = snprintf_func; 00119 return( 0 ); 00120 } 00121 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ 00122 00123 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) 00124 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF) 00125 /* 00126 * Make dummy function to prevent NULL pointer dereferences 00127 */ 00128 static int platform_printf_uninit( const char *format, ... ) 00129 { 00130 ((void) format); 00131 return( 0 ); 00132 } 00133 00134 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit 00135 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */ 00136 00137 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF; 00138 00139 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ) 00140 { 00141 mbedtls_printf = printf_func; 00142 return( 0 ); 00143 } 00144 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ 00145 00146 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) 00147 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) 00148 /* 00149 * Make dummy function to prevent NULL pointer dereferences 00150 */ 00151 static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) 00152 { 00153 ((void) stream); 00154 ((void) format); 00155 return( 0 ); 00156 } 00157 00158 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit 00159 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */ 00160 00161 int (*mbedtls_fprintf)( FILE *, const char *, ... ) = 00162 MBEDTLS_PLATFORM_STD_FPRINTF; 00163 00164 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) 00165 { 00166 mbedtls_fprintf = fprintf_func; 00167 return( 0 ); 00168 } 00169 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ 00170 00171 #if defined(MBEDTLS_PLATFORM_EXIT_ALT) 00172 #if !defined(MBEDTLS_PLATFORM_STD_EXIT) 00173 /* 00174 * Make dummy function to prevent NULL pointer dereferences 00175 */ 00176 static void platform_exit_uninit( int status ) 00177 { 00178 ((void) status); 00179 } 00180 00181 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit 00182 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */ 00183 00184 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT; 00185 00186 int mbedtls_platform_set_exit( void (*exit_func)( int status ) ) 00187 { 00188 mbedtls_exit = exit_func; 00189 return( 0 ); 00190 } 00191 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */ 00192 00193 #if defined(MBEDTLS_HAVE_TIME) 00194 00195 #if defined(MBEDTLS_PLATFORM_TIME_ALT) 00196 #if !defined(MBEDTLS_PLATFORM_STD_TIME) 00197 /* 00198 * Make dummy function to prevent NULL pointer dereferences 00199 */ 00200 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer ) 00201 { 00202 ((void) timer); 00203 return( 0 ); 00204 } 00205 00206 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit 00207 #endif /* !MBEDTLS_PLATFORM_STD_TIME */ 00208 00209 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME; 00210 00211 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) ) 00212 { 00213 mbedtls_time = time_func; 00214 return( 0 ); 00215 } 00216 #endif /* MBEDTLS_PLATFORM_TIME_ALT */ 00217 00218 #endif /* MBEDTLS_HAVE_TIME */ 00219 00220 #if defined(MBEDTLS_ENTROPY_NV_SEED) 00221 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) 00222 /* Default implementations for the platform independent seed functions use 00223 * standard libc file functions to read from and write to a pre-defined filename 00224 */ 00225 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ) 00226 { 00227 FILE *file; 00228 size_t n; 00229 00230 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL ) 00231 return -1; 00232 00233 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len ) 00234 { 00235 fclose( file ); 00236 return -1; 00237 } 00238 00239 fclose( file ); 00240 return( n ); 00241 } 00242 00243 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) 00244 { 00245 FILE *file; 00246 size_t n; 00247 00248 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL ) 00249 return -1; 00250 00251 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len ) 00252 { 00253 fclose( file ); 00254 return -1; 00255 } 00256 00257 fclose( file ); 00258 return( n ); 00259 } 00260 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ 00261 00262 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) 00263 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) 00264 /* 00265 * Make dummy function to prevent NULL pointer dereferences 00266 */ 00267 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len ) 00268 { 00269 ((void) buf); 00270 ((void) buf_len); 00271 return( -1 ); 00272 } 00273 00274 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit 00275 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */ 00276 00277 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) 00278 /* 00279 * Make dummy function to prevent NULL pointer dereferences 00280 */ 00281 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len ) 00282 { 00283 ((void) buf); 00284 ((void) buf_len); 00285 return( -1 ); 00286 } 00287 00288 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit 00289 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */ 00290 00291 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) = 00292 MBEDTLS_PLATFORM_STD_NV_SEED_READ; 00293 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) = 00294 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE; 00295 00296 int mbedtls_platform_set_nv_seed( 00297 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), 00298 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) ) 00299 { 00300 mbedtls_nv_seed_read = nv_seed_read_func; 00301 mbedtls_nv_seed_write = nv_seed_write_func; 00302 return( 0 ); 00303 } 00304 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */ 00305 #endif /* MBEDTLS_ENTROPY_NV_SEED */ 00306 00307 #endif /* MBEDTLS_PLATFORM_C */
Generated on Tue Jul 12 2022 11:02:49 by
