summaryrefslogtreecommitdiff
path: root/lib/gnutls_mem.c
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2001-08-04 07:39:42 +0000
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2001-08-04 07:39:42 +0000
commit1e32c5bffd69edc51d93ab6bd4871823c4024deb (patch)
tree1898c3af9f39891bf071815e7cfa7d5715fc813c /lib/gnutls_mem.c
parentffe3858495bf4a099e8f8c33c0db085f755e9ca6 (diff)
downloadgnutls-1e32c5bffd69edc51d93ab6bd4871823c4024deb.tar.gz
added internal memory handlers
Diffstat (limited to 'lib/gnutls_mem.c')
-rw-r--r--lib/gnutls_mem.c149
1 files changed, 149 insertions, 0 deletions
diff --git a/lib/gnutls_mem.c b/lib/gnutls_mem.c
new file mode 100644
index 0000000000..2f50d087fe
--- /dev/null
+++ b/lib/gnutls_mem.c
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2001 Nikos Mavroyanopoulos
+ *
+ * This file is part of GNUTLS.
+ *
+ * GNUTLS 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.
+ *
+ * GNUTLS 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 <gnutls_int.h>
+#include <gnutls_errors.h>
+#include <gnutls_num.h>
+
+/* #define MALLOC_DEBUG */
+#define EXTRA_SIZE sizeof(size_t)+1
+
+int _gnutls_is_secure_memory(const svoid* mem) {
+ return *((opaque*)mem-1);
+}
+
+void* gnutls_malloc( size_t size) {
+opaque* ret;
+ if (size==0) return NULL;
+
+ ret = malloc( size+EXTRA_SIZE);
+ if (ret==NULL) return ret;
+
+ *((int*)ret) = size;
+ ret[sizeof(size_t)] = 0; /* not secure */
+
+ ret += EXTRA_SIZE;
+
+#ifdef MALLOC_DEBUG
+ _gnutls_log("Allocated: %x with %d bytes\n", ret, _gnutls_malloc_ptr_size(ret));
+#endif
+
+ return ret;
+
+}
+
+void* gnutls_calloc( size_t nmemb, size_t size) {
+void* ret;
+ ret = gnutls_malloc( size);
+ if (ret==NULL) return ret;
+
+ memset( ret, 0, size);
+
+ return ret;
+}
+
+size_t _gnutls_malloc_ptr_size( void* _ptr) {
+opaque* ptr = _ptr;
+
+ if (_ptr==NULL) return 0;
+
+ return *( (int*)((opaque*)ptr-sizeof(size_t)-1));
+}
+
+void* gnutls_realloc( void* ptr, size_t size) {
+void* ret;
+ ret = gnutls_malloc( size);
+ if (ret==NULL) return ret;
+
+ if (ptr!=NULL) {
+ memcpy( ret, ptr, GMIN( _gnutls_malloc_ptr_size(ptr), size));
+ gnutls_free(ptr);
+ }
+
+ return ret;
+}
+
+void gnutls_free( void* _ptr) {
+opaque* ptr = _ptr;
+
+ if (_ptr==NULL) return;
+
+ ptr -= EXTRA_SIZE;
+
+#ifdef MALLOC_DEBUG
+ _gnutls_log("Freed: %x with %d bytes\n", _ptr, _gnutls_malloc_ptr_size(_ptr));
+#endif
+ free( ptr);
+}
+
+svoid* secure_malloc( size_t size) {
+opaque* ret;
+ ret = gnutls_malloc( size);
+ if (ret==NULL) return ret;
+
+ *((opaque*)ret-1) = 1; /* secure mem */
+
+ return ret;
+
+}
+
+svoid* secure_calloc( size_t nmemb, size_t size) {
+svoid* ret;
+ ret = secure_malloc( size);
+ if (ret==NULL) return ret;
+
+ memset( ret, 0, size);
+
+ return ret;
+}
+
+size_t _secure_ptr_size( svoid* ptr) {
+ return _gnutls_malloc_ptr_size( ptr);
+}
+
+svoid* secure_realloc( svoid* ptr, size_t size) {
+svoid* ret;
+ ret = secure_malloc( size);
+ if (ret==NULL) return ret;
+
+ if (ptr!=NULL) {
+ memcpy( ret, ptr, GMIN( _secure_ptr_size(ptr), size));
+ secure_free(ptr);
+ }
+
+ return ret;
+}
+
+void secure_free( svoid* ptr) {
+ memset( ptr, 0, _secure_ptr_size( ptr));
+ gnutls_free( ptr);
+}
+
+char* gnutls_strdup( const char* s) {
+int size = strlen(s);
+char* ret;
+
+ ret = gnutls_malloc(size+1); /* hold null */
+ if (ret==NULL) return ret;
+
+ strcpy( ret, s);
+
+ return ret;
+}