diff --git a/ctaocrypt/include/cyassl_memory.h b/ctaocrypt/include/cyassl_memory.h new file mode 100644 index 000000000..a7576c1db --- /dev/null +++ b/ctaocrypt/include/cyassl_memory.h @@ -0,0 +1,52 @@ +/* cyassl_memory.h + * + * Copyright (C) 2006-2011 Sawtooth Consulting Ltd. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef CYASSL_MEMORY_H +#define CYASSL_MEMORY_H + +#include + +#ifdef __cplusplus + extern "C" { +#endif + + + +typedef void *(*CyaSSL_Malloc_cb)(size_t size); +typedef void (*CyaSSL_Free_cb)(void *ptr); +typedef void *(*CyaSSL_Realloc_cb)(void *ptr, size_t size); + + +int CyaSSL_SetAllocators(CyaSSL_Malloc_cb malloc_function, + CyaSSL_Free_cb free_function, + CyaSSL_Realloc_cb realloc_function); + +void* CyaSSL_Malloc(size_t size); +void CyaSSL_Free(void *ptr); +void* CyaSSL_Realloc(void *ptr, size_t size); + + + +#ifdef __cplusplus +} +#endif + +#endif /* CYASSL_MEMORY_H */ diff --git a/ctaocrypt/include/logging.h b/ctaocrypt/include/logging.h index 69405e58d..807970e5c 100644 --- a/ctaocrypt/include/logging.h +++ b/ctaocrypt/include/logging.h @@ -1,3 +1,24 @@ +/* logging.h + * + * Copyright (C) 2006-2011 Sawtooth Consulting Ltd. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + #ifndef CYASSL_LOGGING_H #define CYASSL_LOGGING_H @@ -10,9 +31,9 @@ enum CYA_Log_Levels { ERROR_LOG = 0, INFO_LOG, - ENTER_LOG, - LEAVE_LOG, - OTHER_LOG + ENTER_LOG, + LEAVE_LOG, + OTHER_LOG }; typedef void (*CyaSSL_Logging_cb)(const int logLevel, diff --git a/ctaocrypt/include/os_settings.h b/ctaocrypt/include/os_settings.h index 94a50cf23..8cdb9e1b5 100644 --- a/ctaocrypt/include/os_settings.h +++ b/ctaocrypt/include/os_settings.h @@ -260,6 +260,11 @@ #endif /* MICRIUM */ + +#if !defined(XMALLOC_USER) && !defined(MICRIUM_MALLOC) + #define USE_CYASSL_MEMORY +#endif + /* Place any other flags or defines here */ diff --git a/ctaocrypt/include/types.h b/ctaocrypt/include/types.h index 207ee644c..75b2389d9 100644 --- a/ctaocrypt/include/types.h +++ b/ctaocrypt/include/types.h @@ -134,11 +134,12 @@ enum { extern void *XREALLOC(void *p, size_t n, void* heap, int type); extern void XFREE(void *p, void* heap, int type); #elif !defined(MICRIUM_MALLOC) - /* defaults to C runtime if user doesn't override and not Micrium */ - #include - #define XMALLOC(s, h, t) malloc((s)) - #define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));} - #define XREALLOC(p, n, h, t) realloc((p), (n)) + /* default C runtime, can install different routines at runtime */ + #define USE_CYASSL_MEMORY + #include + #define XMALLOC(s, h, t) CyaSSL_Malloc((s)) + #define XFREE(p, h, t) {void* xp = (p); if((xp)) CyaSSL_Free((xp));} + #define XREALLOC(p, n, h, t) CyaSSL_Realloc((p), (n)) #endif #ifndef STRING_USER diff --git a/ctaocrypt/src/cyassl_memory.c b/ctaocrypt/src/cyassl_memory.c new file mode 100644 index 000000000..f7522413d --- /dev/null +++ b/ctaocrypt/src/cyassl_memory.c @@ -0,0 +1,92 @@ +/* cyassl_memory.c + * + * Copyright (C) 2006-2011 Sawtooth Consulting Ltd. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + + +#include "os_settings.h" + +#ifdef USE_CYASSL_MEMORY + +#include "cyassl_memory.h" + + +/* Set these to default values initially. */ +static CyaSSL_Malloc_cb malloc_function = 0; +static CyaSSL_Free_cb free_function = 0; +static CyaSSL_Realloc_cb realloc_function = 0; + +int CyaSSL_SetAllocators(CyaSSL_Malloc_cb mf, + CyaSSL_Free_cb ff, + CyaSSL_Realloc_cb rf) +{ + int res = 0; + + if (mf) + malloc_function = mf; + else + res = -1; + + if (ff) + free_function = ff; + else + res = -1; + + if (rf) + realloc_function = rf; + else + res = -1; + + return res; +} + + +void* CyaSSL_Malloc(size_t size) +{ + void* res = 0; + + if (malloc_function) + res = malloc_function(size); + else + res = malloc(size); + + return res; +} + +void CyaSSL_Free(void *ptr) +{ + if (free_function) + free_function(ptr); + else + free(ptr); +} + +void* CyaSSL_Realloc(void *ptr, size_t size) +{ + void* res = 0; + + if (realloc_function) + res = realloc_function(ptr, size); + else + res = realloc(ptr, size); + + return res; +} + +#endif /* USE_CYASSL_MEMORY */ diff --git a/ctaocrypt/src/logging.c b/ctaocrypt/src/logging.c index 9bdc875a3..ec78c1ef1 100644 --- a/ctaocrypt/src/logging.c +++ b/ctaocrypt/src/logging.c @@ -1,8 +1,26 @@ -#include "logging.h" +/* logging.c + * + * Copyright (C) 2006-2011 Sawtooth Consulting Ltd. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ -#ifdef HAVE_CONFIG_H - #include "config.h" -#endif +#include "os_settings.h" +#include "logging.h" /* Set these to default values initially. */ @@ -12,15 +30,14 @@ static int loggingEnabled = 0; int CyaSSL_SetLoggingCb(CyaSSL_Logging_cb f) { - int res = 0; + int res = 0; - if (f) - log_function = f; - else - res = -1; - - return res; + if (f) + log_function = f; + else + res = -1; + return res; } @@ -51,9 +68,9 @@ void CyaSSL_Debugging_OFF(void) static void log(const int logLevel, const char *const logMessage) { - if (log_function) - log_function(logLevel, logMessage); - else { + if (log_function) + log_function(logLevel, logMessage); + else { if (loggingEnabled) { #ifdef THREADX dc_log_printf("%s\n", logMessage); @@ -64,44 +81,44 @@ static void log(const int logLevel, const char *const logMessage) #else fprintf(stderr, "%s\n", logMessage); #endif - } - } + } + } } void CYASSL_MSG(const char* msg) { - log(INFO_LOG , msg); + log(INFO_LOG , msg); } void CYASSL_ENTER(const char* msg) { - if (loggingEnabled) { - char buffer[80]; - sprintf(buffer, "CyaSSL Entering %s", msg); - log(ENTER_LOG , buffer); - } + if (loggingEnabled) { + char buffer[80]; + sprintf(buffer, "CyaSSL Entering %s", msg); + log(ENTER_LOG , buffer); + } } void CYASSL_LEAVE(const char* msg, int ret) { - if (loggingEnabled) { - char buffer[80]; - sprintf(buffer, "CyaSSL Leaving %s, return %d", msg, ret); - log(LEAVE_LOG , buffer); - } + if (loggingEnabled) { + char buffer[80]; + sprintf(buffer, "CyaSSL Leaving %s, return %d", msg, ret); + log(LEAVE_LOG , buffer); + } } void CYASSL_ERROR(int error) { - if (loggingEnabled) { - char buffer[80]; - sprintf(buffer, "CyaSSL error occured, error = %d", error); - log(ERROR_LOG , buffer); - } + if (loggingEnabled) { + char buffer[80]; + sprintf(buffer, "CyaSSL error occured, error = %d", error); + log(ERROR_LOG , buffer); + } } #endif /* DEBUG_CYASSL */ diff --git a/src/Makefile.am b/src/Makefile.am index b025db61f..bcefac3ca 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,8 @@ libcyassl_la_SOURCES = \ ../ctaocrypt/src/random.c ../ctaocrypt/src/rsa.c ../ctaocrypt/src/sha.c \ ../ctaocrypt/src/aes.c ../ctaocrypt/src/sha256.c ../ctaocrypt/src/dh.c \ ../ctaocrypt/src/dsa.c ../ctaocrypt/src/arc4.c ../ctaocrypt/src/rabbit.c \ - ../ctaocrypt/src/pwdbased.c ../ctaocrypt/src/logging.c + ../ctaocrypt/src/pwdbased.c ../ctaocrypt/src/logging.c \ + ../ctaocrypt/src/cyassl_memory.c libcyassl_la_LDFLAGS = -no-undefined -version-info 1:0:0 EXTRA_DIST = ../include/*.h ../include/openssl/*.h ../include/*.rc