1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright (c) 2012-18 Miranda NG team (https://miranda-ng.org),
Copyright (c) 2000-12 Miranda IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.
This program 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.
This program 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 "stdafx.h"
#define BLOCK_ALLOCED 0xABBABABA
#define BLOCK_FREED 0xDEADBEEF
static int CheckBlock(void* blk)
{
int result = FALSE;
char* p = (char*)blk - sizeof(DWORD)*2;
DWORD size, *b, *e;
__try
{
size = *(DWORD*)p;
b = (DWORD*)&p[ sizeof(DWORD) ];
e = (DWORD*)&p[ sizeof(DWORD)*2 + size ];
if (*b != BLOCK_ALLOCED || *e != BLOCK_ALLOCED)
{
if (*b == BLOCK_FREED && *e == BLOCK_FREED)
OutputDebugStringA("memory block is already deleted\n");
else
OutputDebugStringA("memory block is corrupted\n");
#if defined(_DEBUG)
DebugBreak();
#endif
}
else result = TRUE;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
OutputDebugStringA("access violation during checking memory block\n");
#if defined(_DEBUG)
DebugBreak();
#endif
}
return result;
}
/******************************************************************************/
MIR_C_CORE_DLL(void*) mir_alloc(size_t size)
{
if (size == 0)
return nullptr;
char *p = (char*)malloc(size + sizeof(DWORD)* 3);
if (p == nullptr) {
OutputDebugStringA("memory overflow\n");
#if defined(_DEBUG)
DebugBreak();
#endif
return nullptr;
}
*(DWORD*)p = (DWORD)size;
*(DWORD*)&p[sizeof(DWORD)] = BLOCK_ALLOCED;
*(DWORD*)&p[size + sizeof(DWORD)*2] = BLOCK_ALLOCED;
return p + sizeof(DWORD)* 2;
}
/******************************************************************************/
MIR_C_CORE_DLL(void*) mir_calloc(size_t size)
{
void* p = mir_alloc(size);
if (p != nullptr)
memset(p, 0, size);
return p;
}
/******************************************************************************/
MIR_C_CORE_DLL(void*) mir_realloc(void* ptr, size_t size)
{
char *p;
if (ptr != nullptr) {
if (!CheckBlock(ptr))
return nullptr;
p = (char*)ptr - sizeof(DWORD)*2;
}
else p = nullptr;
p = (char*)realloc(p, size + sizeof(DWORD)*3);
if (p == nullptr) {
OutputDebugStringA("memory overflow\n");
#if defined(_DEBUG)
DebugBreak();
#endif
return nullptr;
}
*(DWORD*)p = (DWORD)size;
*(DWORD*)&p[sizeof(DWORD)] = BLOCK_ALLOCED;
*(DWORD*)&p[size + sizeof(DWORD)*2] = BLOCK_ALLOCED;
return p + sizeof(DWORD)*2;
}
/******************************************************************************/
MIR_C_CORE_DLL(void) mir_free(void* ptr)
{
char* p;
DWORD size;
if (ptr == nullptr)
return;
if (!CheckBlock(ptr))
return;
p = (char*)ptr - sizeof(DWORD)*2;
size = *(DWORD*)p;
*(DWORD*)&p[sizeof(DWORD)] = BLOCK_FREED;
*(DWORD*)&p[size + sizeof(DWORD)*2] = BLOCK_FREED;
free(p);
}
/******************************************************************************/
MIR_CORE_DLL(char*) mir_strdup(const char *str)
{
if (str == nullptr)
return nullptr;
char *p = (char*)mir_alloc(strlen(str)+1);
if (p)
strcpy(p, str);
return p;
}
MIR_CORE_DLL(wchar_t*) mir_wstrdup(const wchar_t *str)
{
if (str == nullptr)
return nullptr;
wchar_t *p = (wchar_t*)mir_alloc(sizeof(wchar_t)*(wcslen(str)+1));
if (p)
wcscpy(p, str);
return p;
}
/******************************************************************************/
MIR_CORE_DLL(char*) mir_strndup(const char *str, size_t len)
{
if (str == nullptr || len == 0)
return nullptr;
char *p = (char*)mir_alloc(len+1);
if (p) {
memcpy(p, str, len);
p[len] = 0;
}
return p;
}
MIR_CORE_DLL(wchar_t*) mir_wstrndup(const wchar_t *str, size_t len)
{
if (str == nullptr || len == 0)
return nullptr;
wchar_t *p = (wchar_t*)mir_alloc(sizeof(wchar_t)*(len+1));
if (p) {
memcpy(p, str, sizeof(wchar_t)*len);
p[len] = 0;
}
return p;
}
/******************************************************************************/
MIR_CORE_DLL(int) mir_snprintf(char *buffer, size_t count, const char* fmt, ...)
{
va_list va;
va_start(va, fmt);
int len = _vsnprintf(buffer, count-1, fmt, va);
va_end(va);
buffer[count-1] = 0;
return len;
}
/******************************************************************************/
MIR_CORE_DLL(int) mir_snwprintf(wchar_t *buffer, size_t count, const wchar_t* fmt, ...)
{
va_list va;
va_start(va, fmt);
int len = _vsntprintf(buffer, count-1, fmt, va);
va_end(va);
buffer[count-1] = 0;
return len;
}
/******************************************************************************/
MIR_CORE_DLL(int) mir_vsnprintf(char *buffer, size_t count, const char* fmt, va_list va)
{
int len = _vsnprintf(buffer, count-1, fmt, va);
buffer[count-1] = 0;
return len;
}
/******************************************************************************/
MIR_CORE_DLL(int) mir_vsnwprintf(wchar_t *buffer, size_t count, const wchar_t* fmt, va_list va)
{
int len = _vsntprintf(buffer, count-1, fmt, va);
buffer[count-1] = 0;
return len;
}
/******************************************************************************/
MIR_CORE_DLL(wchar_t*) mir_a2u_cp(const char* src, int codepage)
{
if (src == nullptr)
return nullptr;
int cbLen = MultiByteToWideChar(codepage, 0, src, -1, nullptr, 0);
wchar_t* result = (wchar_t*)mir_alloc(sizeof(wchar_t)*(cbLen+1));
if (result == nullptr)
return nullptr;
MultiByteToWideChar(codepage, 0, src, -1, result, cbLen);
result[cbLen] = 0;
return result;
}
/******************************************************************************/
MIR_CORE_DLL(wchar_t*) mir_a2u(const char* src)
{
return mir_a2u_cp(src, Langpack_GetDefaultCodePage());
}
/******************************************************************************/
MIR_CORE_DLL(char*) mir_u2a_cp(const wchar_t* src, int codepage)
{
if (src == nullptr)
return nullptr;
int cbLen = WideCharToMultiByte(codepage, 0, src, -1, nullptr, 0, nullptr, nullptr);
char* result = (char*)mir_alloc(cbLen+1);
if (result == nullptr)
return nullptr;
WideCharToMultiByte(codepage, 0, src, -1, result, cbLen, nullptr, nullptr);
result[cbLen] = 0;
return result;
}
/******************************************************************************/
MIR_CORE_DLL(char*) mir_u2a(const wchar_t* src)
{
return mir_u2a_cp(src, Langpack_GetDefaultCodePage());
}
|