add cyassl runtime alloc routines override, move to ctaocrypt so both can use, submitted by eof

This commit is contained in:
Todd A Ouska 2011-04-15 16:43:00 -07:00
parent 7014d6bbc1
commit 651b793791
7 changed files with 230 additions and 41 deletions

View File

@ -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 <stdlib.h>
#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 */

View File

@ -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,

View File

@ -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 */

View File

@ -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 <stdlib.h>
#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 <cyassl_memory.h>
#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

View File

@ -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 */

View File

@ -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 */

View File

@ -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