Blame lib/mem.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2001-2012 Free Software Foundation, Inc.
Packit 549fdc
 *
Packit 549fdc
 * Author: Nikos Mavrogiannopoulos
Packit 549fdc
 *
Packit 549fdc
 * This file is part of GnuTLS.
Packit 549fdc
 *
Packit 549fdc
 * The GnuTLS is free software; you can redistribute it and/or
Packit 549fdc
 * modify it under the terms of the GNU Lesser General Public License
Packit 549fdc
 * as published by the Free Software Foundation; either version 2.1 of
Packit 549fdc
 * the License, or (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * This library is distributed in the hope that it will be useful, but
Packit 549fdc
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
 * Lesser General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
Packit 549fdc
 *
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
#include "gnutls_int.h"
Packit 549fdc
#include "errors.h"
Packit 549fdc
#include <num.h>
Packit 549fdc
#include <xsize.h>
Packit 549fdc
Packit 549fdc
gnutls_alloc_function gnutls_secure_malloc = malloc;
Packit 549fdc
gnutls_alloc_function gnutls_malloc = malloc;
Packit 549fdc
gnutls_free_function gnutls_free = free;
Packit 549fdc
gnutls_realloc_function gnutls_realloc = realloc;
Packit 549fdc
Packit 549fdc
void *(*gnutls_calloc) (size_t, size_t) = calloc;
Packit 549fdc
char *(*gnutls_strdup) (const char *) = _gnutls_strdup;
Packit 549fdc
Packit 549fdc
void *_gnutls_calloc(size_t nmemb, size_t size)
Packit 549fdc
{
Packit 549fdc
	void *ret;
Packit 549fdc
	size_t n = xtimes(nmemb, size);
Packit 549fdc
	ret = (size_in_bounds_p(n) ? gnutls_malloc(n) : NULL);
Packit 549fdc
	if (ret != NULL)
Packit 549fdc
		memset(ret, 0, size);
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/* This realloc will free ptr in case realloc
Packit 549fdc
 * fails.
Packit 549fdc
 */
Packit 549fdc
void *gnutls_realloc_fast(void *ptr, size_t size)
Packit 549fdc
{
Packit 549fdc
	void *ret;
Packit 549fdc
Packit 549fdc
	if (size == 0)
Packit 549fdc
		return ptr;
Packit 549fdc
Packit 549fdc
	ret = gnutls_realloc(ptr, size);
Packit 549fdc
	if (ret == NULL) {
Packit 549fdc
		gnutls_free(ptr);
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
char *_gnutls_strdup(const char *str)
Packit 549fdc
{
Packit 549fdc
	size_t siz;
Packit 549fdc
	char *ret;
Packit 549fdc
Packit 549fdc
	if(unlikely(!str))
Packit 549fdc
		return NULL;
Packit 549fdc
Packit 549fdc
	siz = strlen(str) + 1;
Packit 549fdc
Packit 549fdc
	ret = gnutls_malloc(siz);
Packit 549fdc
	if (ret != NULL)
Packit 549fdc
		memcpy(ret, str, siz);
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
#if 0
Packit 549fdc
/* don't use them. They are included for documentation.
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_malloc:
Packit 549fdc
 * @s: size to allocate in bytes
Packit 549fdc
 *
Packit 549fdc
 * This function will allocate 's' bytes data, and
Packit 549fdc
 * return a pointer to memory. This function is supposed
Packit 549fdc
 * to be used by callbacks.
Packit 549fdc
 *
Packit 549fdc
 * The allocation function used is the one set by
Packit 549fdc
 * gnutls_global_set_mem_functions().
Packit 549fdc
 **/
Packit 549fdc
void *gnutls_malloc(size_t s)
Packit 549fdc
{
Packit 549fdc
	int x;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_free:
Packit 549fdc
 * @ptr: pointer to memory
Packit 549fdc
 *
Packit 549fdc
 * This function will free data pointed by ptr.
Packit 549fdc
 *
Packit 549fdc
 * The deallocation function used is the one set by
Packit 549fdc
 * gnutls_global_set_mem_functions().
Packit 549fdc
 *
Packit 549fdc
 **/
Packit 549fdc
void gnutls_free(void *ptr)
Packit 549fdc
{
Packit 549fdc
	int x;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
#endif
Packit 549fdc
Packit 549fdc
/* Returns 1 if the provided buffer is all zero.
Packit 549fdc
 * It leaks no information via timing.
Packit 549fdc
 */
Packit 549fdc
unsigned _gnutls_mem_is_zero(const uint8_t *ptr, unsigned size)
Packit 549fdc
{
Packit 549fdc
	unsigned i;
Packit 549fdc
	uint8_t res = 0;
Packit 549fdc
Packit 549fdc
	for (i=0;i
Packit 549fdc
		res |= ptr[i];
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	return ((res==0)?1:0);
Packit 549fdc
}