Coverage Report

Created: 2023-03-26 07:38

/src/yara/libyara/mem.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright (c) 2007-2013. The YARA Authors. All Rights Reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
9
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation and/or
12
other materials provided with the distribution.
13
14
3. Neither the name of the copyright holder nor the names of its contributors
15
may be used to endorse or promote products derived from this software without
16
specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <yara/error.h>
31
#include <yara/mem.h>
32
33
#if defined(_WIN32) || defined(__CYGWIN__)
34
35
#include <string.h>
36
#include <windows.h>
37
38
static HANDLE hHeap;
39
40
int yr_heap_alloc(void)
41
{
42
  hHeap = HeapCreate(0, 0x8000, 0);
43
44
  if (hHeap == NULL)
45
    return ERROR_INTERNAL_FATAL_ERROR;
46
47
  return ERROR_SUCCESS;
48
}
49
50
int yr_heap_free(void)
51
{
52
  if (HeapDestroy(hHeap))
53
    return ERROR_SUCCESS;
54
  else
55
    return ERROR_INTERNAL_FATAL_ERROR;
56
}
57
58
void* yr_calloc(size_t count, size_t size)
59
{
60
  return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, count * size);
61
}
62
63
void* yr_malloc(size_t size)
64
{
65
  return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, size);
66
}
67
68
void* yr_realloc(void* ptr, size_t size)
69
{
70
  if (ptr == NULL)
71
    return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, size);
72
73
  return (void*) HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, ptr, size);
74
}
75
76
char* yr_strdup(const char* str)
77
{
78
  size_t len = strlen(str);
79
  char* dup = (char*) yr_malloc(len + 1);
80
81
  if (dup == NULL)
82
    return NULL;
83
84
  memcpy(dup, str, len);
85
  dup[len] = '\0';
86
87
  return (char*) dup;
88
}
89
90
char* yr_strndup(const char* str, size_t n)
91
{
92
  size_t len = strnlen(str, n);
93
  char* dup = (char*) yr_malloc(len + 1);
94
95
  if (dup == NULL)
96
    return NULL;
97
98
  memcpy(dup, str, len);
99
  dup[len] = '\0';
100
101
  return (char*) dup;
102
}
103
104
YR_API void yr_free(void* ptr)
105
{
106
  HeapFree(hHeap, 0, ptr);
107
}
108
109
#else
110
111
#include <stdio.h>
112
#include <stdlib.h>
113
#include <string.h>
114
115
int yr_heap_alloc(void)
116
2
{
117
2
  return ERROR_SUCCESS;
118
2
}
119
120
int yr_heap_free(void)
121
0
{
122
0
  return ERROR_SUCCESS;
123
0
}
124
125
void* yr_calloc(size_t count, size_t size)
126
2.65k
{
127
2.65k
  return calloc(count, size);
128
2.65k
}
129
130
void* yr_malloc(size_t size)
131
28.9M
{
132
28.9M
  return malloc(size);
133
28.9M
}
134
135
void* yr_realloc(void* ptr, size_t size)
136
8.61k
{
137
8.61k
  return realloc(ptr, size);
138
8.61k
}
139
140
char* yr_strdup(const char* str)
141
133k
{
142
133k
  return strdup(str);
143
133k
}
144
145
char* yr_strndup(const char* str, size_t n)
146
0
{
147
0
  return strndup(str, n);
148
0
}
149
150
YR_API void yr_free(void* ptr)
151
29.0M
{
152
29.0M
  free(ptr);
153
29.0M
}
154
155
#endif