mbedtls ported to mbed-classic
Fork of mbedtls by
source/platform.c@1:24750b9ad5ef, 2016-01-22 (annotated)
- Committer:
- Christopher Haster
- Date:
- Fri Jan 22 16:44:49 2016 -0600
- Revision:
- 1:24750b9ad5ef
Initial move of mbedtls to mercurial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
1:24750b9ad5ef | 1 | /* |
Christopher Haster |
1:24750b9ad5ef | 2 | * Platform abstraction layer |
Christopher Haster |
1:24750b9ad5ef | 3 | * |
Christopher Haster |
1:24750b9ad5ef | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Christopher Haster |
1:24750b9ad5ef | 5 | * SPDX-License-Identifier: Apache-2.0 |
Christopher Haster |
1:24750b9ad5ef | 6 | * |
Christopher Haster |
1:24750b9ad5ef | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Christopher Haster |
1:24750b9ad5ef | 8 | * not use this file except in compliance with the License. |
Christopher Haster |
1:24750b9ad5ef | 9 | * You may obtain a copy of the License at |
Christopher Haster |
1:24750b9ad5ef | 10 | * |
Christopher Haster |
1:24750b9ad5ef | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
1:24750b9ad5ef | 12 | * |
Christopher Haster |
1:24750b9ad5ef | 13 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
1:24750b9ad5ef | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Christopher Haster |
1:24750b9ad5ef | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
1:24750b9ad5ef | 16 | * See the License for the specific language governing permissions and |
Christopher Haster |
1:24750b9ad5ef | 17 | * limitations under the License. |
Christopher Haster |
1:24750b9ad5ef | 18 | * |
Christopher Haster |
1:24750b9ad5ef | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Christopher Haster |
1:24750b9ad5ef | 20 | */ |
Christopher Haster |
1:24750b9ad5ef | 21 | |
Christopher Haster |
1:24750b9ad5ef | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Christopher Haster |
1:24750b9ad5ef | 23 | #include "mbedtls/config.h" |
Christopher Haster |
1:24750b9ad5ef | 24 | #else |
Christopher Haster |
1:24750b9ad5ef | 25 | #include MBEDTLS_CONFIG_FILE |
Christopher Haster |
1:24750b9ad5ef | 26 | #endif |
Christopher Haster |
1:24750b9ad5ef | 27 | |
Christopher Haster |
1:24750b9ad5ef | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Christopher Haster |
1:24750b9ad5ef | 29 | |
Christopher Haster |
1:24750b9ad5ef | 30 | #include "mbedtls/platform.h" |
Christopher Haster |
1:24750b9ad5ef | 31 | |
Christopher Haster |
1:24750b9ad5ef | 32 | #if defined(MBEDTLS_PLATFORM_MEMORY) |
Christopher Haster |
1:24750b9ad5ef | 33 | #if !defined(MBEDTLS_PLATFORM_STD_CALLOC) |
Christopher Haster |
1:24750b9ad5ef | 34 | static void *platform_calloc_uninit( size_t n, size_t size ) |
Christopher Haster |
1:24750b9ad5ef | 35 | { |
Christopher Haster |
1:24750b9ad5ef | 36 | ((void) n); |
Christopher Haster |
1:24750b9ad5ef | 37 | ((void) size); |
Christopher Haster |
1:24750b9ad5ef | 38 | return( NULL ); |
Christopher Haster |
1:24750b9ad5ef | 39 | } |
Christopher Haster |
1:24750b9ad5ef | 40 | |
Christopher Haster |
1:24750b9ad5ef | 41 | #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit |
Christopher Haster |
1:24750b9ad5ef | 42 | #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */ |
Christopher Haster |
1:24750b9ad5ef | 43 | |
Christopher Haster |
1:24750b9ad5ef | 44 | #if !defined(MBEDTLS_PLATFORM_STD_FREE) |
Christopher Haster |
1:24750b9ad5ef | 45 | static void platform_free_uninit( void *ptr ) |
Christopher Haster |
1:24750b9ad5ef | 46 | { |
Christopher Haster |
1:24750b9ad5ef | 47 | ((void) ptr); |
Christopher Haster |
1:24750b9ad5ef | 48 | } |
Christopher Haster |
1:24750b9ad5ef | 49 | |
Christopher Haster |
1:24750b9ad5ef | 50 | #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit |
Christopher Haster |
1:24750b9ad5ef | 51 | #endif /* !MBEDTLS_PLATFORM_STD_FREE */ |
Christopher Haster |
1:24750b9ad5ef | 52 | |
Christopher Haster |
1:24750b9ad5ef | 53 | void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; |
Christopher Haster |
1:24750b9ad5ef | 54 | void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE; |
Christopher Haster |
1:24750b9ad5ef | 55 | |
Christopher Haster |
1:24750b9ad5ef | 56 | int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), |
Christopher Haster |
1:24750b9ad5ef | 57 | void (*free_func)( void * ) ) |
Christopher Haster |
1:24750b9ad5ef | 58 | { |
Christopher Haster |
1:24750b9ad5ef | 59 | mbedtls_calloc = calloc_func; |
Christopher Haster |
1:24750b9ad5ef | 60 | mbedtls_free = free_func; |
Christopher Haster |
1:24750b9ad5ef | 61 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 62 | } |
Christopher Haster |
1:24750b9ad5ef | 63 | #endif /* MBEDTLS_PLATFORM_MEMORY */ |
Christopher Haster |
1:24750b9ad5ef | 64 | |
Christopher Haster |
1:24750b9ad5ef | 65 | #if defined(_WIN32) |
Christopher Haster |
1:24750b9ad5ef | 66 | #include <stdarg.h> |
Christopher Haster |
1:24750b9ad5ef | 67 | int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) |
Christopher Haster |
1:24750b9ad5ef | 68 | { |
Christopher Haster |
1:24750b9ad5ef | 69 | int ret; |
Christopher Haster |
1:24750b9ad5ef | 70 | va_list argp; |
Christopher Haster |
1:24750b9ad5ef | 71 | |
Christopher Haster |
1:24750b9ad5ef | 72 | /* Avoid calling the invalid parameter handler by checking ourselves */ |
Christopher Haster |
1:24750b9ad5ef | 73 | if( s == NULL || n == 0 || fmt == NULL ) |
Christopher Haster |
1:24750b9ad5ef | 74 | return( -1 ); |
Christopher Haster |
1:24750b9ad5ef | 75 | |
Christopher Haster |
1:24750b9ad5ef | 76 | va_start( argp, fmt ); |
Christopher Haster |
1:24750b9ad5ef | 77 | #if defined(_TRUNCATE) |
Christopher Haster |
1:24750b9ad5ef | 78 | ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); |
Christopher Haster |
1:24750b9ad5ef | 79 | #else |
Christopher Haster |
1:24750b9ad5ef | 80 | ret = _vsnprintf( s, n, fmt, argp ); |
Christopher Haster |
1:24750b9ad5ef | 81 | if( ret < 0 || (size_t) ret == n ) |
Christopher Haster |
1:24750b9ad5ef | 82 | { |
Christopher Haster |
1:24750b9ad5ef | 83 | s[n-1] = '\0'; |
Christopher Haster |
1:24750b9ad5ef | 84 | ret = -1; |
Christopher Haster |
1:24750b9ad5ef | 85 | } |
Christopher Haster |
1:24750b9ad5ef | 86 | #endif |
Christopher Haster |
1:24750b9ad5ef | 87 | va_end( argp ); |
Christopher Haster |
1:24750b9ad5ef | 88 | |
Christopher Haster |
1:24750b9ad5ef | 89 | return( ret ); |
Christopher Haster |
1:24750b9ad5ef | 90 | } |
Christopher Haster |
1:24750b9ad5ef | 91 | #endif |
Christopher Haster |
1:24750b9ad5ef | 92 | |
Christopher Haster |
1:24750b9ad5ef | 93 | #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) |
Christopher Haster |
1:24750b9ad5ef | 94 | #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) |
Christopher Haster |
1:24750b9ad5ef | 95 | /* |
Christopher Haster |
1:24750b9ad5ef | 96 | * Make dummy function to prevent NULL pointer dereferences |
Christopher Haster |
1:24750b9ad5ef | 97 | */ |
Christopher Haster |
1:24750b9ad5ef | 98 | static int platform_snprintf_uninit( char * s, size_t n, |
Christopher Haster |
1:24750b9ad5ef | 99 | const char * format, ... ) |
Christopher Haster |
1:24750b9ad5ef | 100 | { |
Christopher Haster |
1:24750b9ad5ef | 101 | ((void) s); |
Christopher Haster |
1:24750b9ad5ef | 102 | ((void) n); |
Christopher Haster |
1:24750b9ad5ef | 103 | ((void) format); |
Christopher Haster |
1:24750b9ad5ef | 104 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 105 | } |
Christopher Haster |
1:24750b9ad5ef | 106 | |
Christopher Haster |
1:24750b9ad5ef | 107 | #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit |
Christopher Haster |
1:24750b9ad5ef | 108 | #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */ |
Christopher Haster |
1:24750b9ad5ef | 109 | |
Christopher Haster |
1:24750b9ad5ef | 110 | int (*mbedtls_snprintf)( char * s, size_t n, |
Christopher Haster |
1:24750b9ad5ef | 111 | const char * format, |
Christopher Haster |
1:24750b9ad5ef | 112 | ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF; |
Christopher Haster |
1:24750b9ad5ef | 113 | |
Christopher Haster |
1:24750b9ad5ef | 114 | int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, |
Christopher Haster |
1:24750b9ad5ef | 115 | const char * format, |
Christopher Haster |
1:24750b9ad5ef | 116 | ... ) ) |
Christopher Haster |
1:24750b9ad5ef | 117 | { |
Christopher Haster |
1:24750b9ad5ef | 118 | mbedtls_snprintf = snprintf_func; |
Christopher Haster |
1:24750b9ad5ef | 119 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 120 | } |
Christopher Haster |
1:24750b9ad5ef | 121 | #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ |
Christopher Haster |
1:24750b9ad5ef | 122 | |
Christopher Haster |
1:24750b9ad5ef | 123 | #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) |
Christopher Haster |
1:24750b9ad5ef | 124 | #if !defined(MBEDTLS_PLATFORM_STD_PRINTF) |
Christopher Haster |
1:24750b9ad5ef | 125 | /* |
Christopher Haster |
1:24750b9ad5ef | 126 | * Make dummy function to prevent NULL pointer dereferences |
Christopher Haster |
1:24750b9ad5ef | 127 | */ |
Christopher Haster |
1:24750b9ad5ef | 128 | static int platform_printf_uninit( const char *format, ... ) |
Christopher Haster |
1:24750b9ad5ef | 129 | { |
Christopher Haster |
1:24750b9ad5ef | 130 | ((void) format); |
Christopher Haster |
1:24750b9ad5ef | 131 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 132 | } |
Christopher Haster |
1:24750b9ad5ef | 133 | |
Christopher Haster |
1:24750b9ad5ef | 134 | #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit |
Christopher Haster |
1:24750b9ad5ef | 135 | #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */ |
Christopher Haster |
1:24750b9ad5ef | 136 | |
Christopher Haster |
1:24750b9ad5ef | 137 | int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF; |
Christopher Haster |
1:24750b9ad5ef | 138 | |
Christopher Haster |
1:24750b9ad5ef | 139 | int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ) |
Christopher Haster |
1:24750b9ad5ef | 140 | { |
Christopher Haster |
1:24750b9ad5ef | 141 | mbedtls_printf = printf_func; |
Christopher Haster |
1:24750b9ad5ef | 142 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 143 | } |
Christopher Haster |
1:24750b9ad5ef | 144 | #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ |
Christopher Haster |
1:24750b9ad5ef | 145 | |
Christopher Haster |
1:24750b9ad5ef | 146 | #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) |
Christopher Haster |
1:24750b9ad5ef | 147 | #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) |
Christopher Haster |
1:24750b9ad5ef | 148 | /* |
Christopher Haster |
1:24750b9ad5ef | 149 | * Make dummy function to prevent NULL pointer dereferences |
Christopher Haster |
1:24750b9ad5ef | 150 | */ |
Christopher Haster |
1:24750b9ad5ef | 151 | static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) |
Christopher Haster |
1:24750b9ad5ef | 152 | { |
Christopher Haster |
1:24750b9ad5ef | 153 | ((void) stream); |
Christopher Haster |
1:24750b9ad5ef | 154 | ((void) format); |
Christopher Haster |
1:24750b9ad5ef | 155 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 156 | } |
Christopher Haster |
1:24750b9ad5ef | 157 | |
Christopher Haster |
1:24750b9ad5ef | 158 | #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit |
Christopher Haster |
1:24750b9ad5ef | 159 | #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */ |
Christopher Haster |
1:24750b9ad5ef | 160 | |
Christopher Haster |
1:24750b9ad5ef | 161 | int (*mbedtls_fprintf)( FILE *, const char *, ... ) = |
Christopher Haster |
1:24750b9ad5ef | 162 | MBEDTLS_PLATFORM_STD_FPRINTF; |
Christopher Haster |
1:24750b9ad5ef | 163 | |
Christopher Haster |
1:24750b9ad5ef | 164 | int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) |
Christopher Haster |
1:24750b9ad5ef | 165 | { |
Christopher Haster |
1:24750b9ad5ef | 166 | mbedtls_fprintf = fprintf_func; |
Christopher Haster |
1:24750b9ad5ef | 167 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 168 | } |
Christopher Haster |
1:24750b9ad5ef | 169 | #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ |
Christopher Haster |
1:24750b9ad5ef | 170 | |
Christopher Haster |
1:24750b9ad5ef | 171 | #if defined(MBEDTLS_PLATFORM_EXIT_ALT) |
Christopher Haster |
1:24750b9ad5ef | 172 | #if !defined(MBEDTLS_PLATFORM_STD_EXIT) |
Christopher Haster |
1:24750b9ad5ef | 173 | /* |
Christopher Haster |
1:24750b9ad5ef | 174 | * Make dummy function to prevent NULL pointer dereferences |
Christopher Haster |
1:24750b9ad5ef | 175 | */ |
Christopher Haster |
1:24750b9ad5ef | 176 | static void platform_exit_uninit( int status ) |
Christopher Haster |
1:24750b9ad5ef | 177 | { |
Christopher Haster |
1:24750b9ad5ef | 178 | ((void) status); |
Christopher Haster |
1:24750b9ad5ef | 179 | } |
Christopher Haster |
1:24750b9ad5ef | 180 | |
Christopher Haster |
1:24750b9ad5ef | 181 | #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit |
Christopher Haster |
1:24750b9ad5ef | 182 | #endif /* !MBEDTLS_PLATFORM_STD_EXIT */ |
Christopher Haster |
1:24750b9ad5ef | 183 | |
Christopher Haster |
1:24750b9ad5ef | 184 | void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT; |
Christopher Haster |
1:24750b9ad5ef | 185 | |
Christopher Haster |
1:24750b9ad5ef | 186 | int mbedtls_platform_set_exit( void (*exit_func)( int status ) ) |
Christopher Haster |
1:24750b9ad5ef | 187 | { |
Christopher Haster |
1:24750b9ad5ef | 188 | mbedtls_exit = exit_func; |
Christopher Haster |
1:24750b9ad5ef | 189 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 190 | } |
Christopher Haster |
1:24750b9ad5ef | 191 | #endif /* MBEDTLS_PLATFORM_EXIT_ALT */ |
Christopher Haster |
1:24750b9ad5ef | 192 | |
Christopher Haster |
1:24750b9ad5ef | 193 | #endif /* MBEDTLS_PLATFORM_C */ |