Line data Source code
1 : /* 2 : * Copyright (C) 2001-2012 Free Software Foundation, Inc. 3 : * 4 : * Author: Nikos Mavrogiannopoulos 5 : * 6 : * This file is part of GnuTLS. 7 : * 8 : * The GnuTLS is free software; you can redistribute it and/or 9 : * modify it under the terms of the GNU Lesser General Public License 10 : * as published by the Free Software Foundation; either version 2.1 of 11 : * the License, or (at your option) any later version. 12 : * 13 : * This library is distributed in the hope that it will be useful, but 14 : * WITHOUT ANY WARRANTY; without even the implied warranty of 15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 : * Lesser General Public License for more details. 17 : * 18 : * You should have received a copy of the GNU Lesser General Public License 19 : * along with this program. If not, see <https://www.gnu.org/licenses/> 20 : * 21 : */ 22 : 23 : #include "gnutls_int.h" 24 : #include "errors.h" 25 : #include <num.h> 26 : #include <xsize.h> 27 : 28 : gnutls_alloc_function gnutls_secure_malloc = malloc; 29 : gnutls_alloc_function gnutls_malloc = malloc; 30 : gnutls_free_function gnutls_free = free; 31 : gnutls_realloc_function gnutls_realloc = realloc; 32 : 33 : void *(*gnutls_calloc) (size_t, size_t) = calloc; 34 : char *(*gnutls_strdup) (const char *) = _gnutls_strdup; 35 : 36 0 : void *_gnutls_calloc(size_t nmemb, size_t size) 37 : { 38 0 : void *ret; 39 0 : size_t n = xtimes(nmemb, size); 40 0 : ret = (size_in_bounds_p(n) ? gnutls_malloc(n) : NULL); 41 0 : if (ret != NULL) 42 0 : memset(ret, 0, size); 43 0 : return ret; 44 : } 45 : 46 : /* This realloc will free ptr in case realloc 47 : * fails. 48 : */ 49 361514 : void *gnutls_realloc_fast(void *ptr, size_t size) 50 : { 51 361514 : void *ret; 52 : 53 361514 : if (size == 0) 54 : return ptr; 55 : 56 361514 : ret = gnutls_realloc(ptr, size); 57 361514 : if (ret == NULL) { 58 0 : gnutls_free(ptr); 59 : } 60 : 61 : return ret; 62 : } 63 : 64 168277 : char *_gnutls_strdup(const char *str) 65 : { 66 168277 : size_t siz; 67 168277 : char *ret; 68 : 69 168277 : if(unlikely(!str)) 70 : return NULL; 71 : 72 168277 : siz = strlen(str) + 1; 73 : 74 168277 : ret = gnutls_malloc(siz); 75 168277 : if (ret != NULL) 76 168277 : memcpy(ret, str, siz); 77 : return ret; 78 : } 79 : 80 : #if 0 81 : /* don't use them. They are included for documentation. 82 : */ 83 : 84 : /** 85 : * gnutls_malloc: 86 : * @s: size to allocate in bytes 87 : * 88 : * This function will allocate 's' bytes data, and 89 : * return a pointer to memory. This function is supposed 90 : * to be used by callbacks. 91 : * 92 : * The allocation function used is the one set by 93 : * gnutls_global_set_mem_functions(). 94 : **/ 95 : void *gnutls_malloc(size_t s) 96 : { 97 : int x; 98 : } 99 : 100 : /** 101 : * gnutls_free: 102 : * @ptr: pointer to memory 103 : * 104 : * This function will free data pointed by ptr. 105 : * 106 : * The deallocation function used is the one set by 107 : * gnutls_global_set_mem_functions(). 108 : * 109 : **/ 110 : void gnutls_free(void *ptr) 111 : { 112 : int x; 113 : } 114 : 115 : #endif 116 : 117 : /* Returns 1 if the provided buffer is all zero. 118 : * It leaks no information via timing. 119 : */ 120 1626 : unsigned _gnutls_mem_is_zero(const uint8_t *ptr, unsigned size) 121 : { 122 1626 : unsigned i; 123 1626 : uint8_t res = 0; 124 : 125 85098 : for (i=0;i<size;i++) { 126 83472 : res |= ptr[i]; 127 : } 128 : 129 1626 : return ((res==0)?1:0); 130 : }