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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/hashes.h"
#include "gate/strings.h"
#include "gate/debugging.h"
#include "gate/guids.h"
#include "gate/times.h"
#include "gate/structs.h"
#include "gate/objects.h"
#include "gate/properties.h"
static gate_uint32_t hash_initer = 5381;
static gate_uint32_t hash_finisher = 1566083941;
#define GATE_HASH_GEN_UPDATE(store, value) store = ((store << 5) + store) ^ value
void gate_hash_generator_init(gate_hash_generator_context_t* generator)
{
gate_mem_clear(generator, sizeof(gate_hash_generator_t));
generator->stores[0] = hash_initer;
generator->stores[1] = hash_initer;
generator->len = 0;
}
void gate_hash_generator_update(gate_hash_generator_context_t* generator, void const* data, gate_size_t data_len)
{
gate_uint8_t const* ptr = (gate_uint8_t const*)data;
gate_uint32_t chr;
gate_uint32_t h;
gate_hash_generator_context_t context; /* operate on local stack to speed up operations */
gate_mem_copy(&context, generator, sizeof(context));
while (data_len-- != 0)
{
chr = (gate_uint32_t)*ptr;
h = context.stores[context.len % 2];
GATE_HASH_GEN_UPDATE(h, chr);
context.stores[context.len % 2] = h;
++context.len;
++ptr;
}
gate_mem_copy(generator, &context, sizeof(context));
}
gate_hash_code_t gate_hash_generator_finish(gate_hash_generator_context_t* generator)
{
return generator->stores[0] + generator->stores[1] * hash_finisher;
}
gate_uint32_t gate_hash_generate(void const* data, gate_size_t data_len)
{
gate_uint8_t const* ptr = (gate_uint8_t const*)data;
gate_uint32_t h1 = hash_initer;
gate_uint32_t h2 = hash_initer;
gate_uint32_t chr;
while (data_len-- != 0)
{
chr = (gate_uint32_t)ptr[0];
GATE_HASH_GEN_UPDATE(h1, chr);
if (data_len-- == 0)
{
break;
}
chr = (gate_uint32_t)ptr[1];
GATE_HASH_GEN_UPDATE(h2, chr);
ptr += 2;
}
return h1 + (h2 * 1566083941);
}
gate_hash_code_t gate_hash_generate_1(void const* data)
{
return gate_hash_generate(data, 1);
}
gate_hash_code_t gate_hash_generate_2(void const* data)
{
return gate_hash_generate(data, 2);
}
gate_hash_code_t gate_hash_generate_4(void const* data)
{
return gate_hash_generate(data, 4);
}
gate_hash_code_t gate_hash_generate_8(void const* data)
{
return gate_hash_generate(data, 8);
}
static gate_hash_code_t gate_hash_generate_address(void const* data)
{
return gate_hash_generate(data, sizeof(gate_uintptr_t));
}
static gate_hash_code_t gate_hash_generate_cstring(void const* data)
{
gate_const_str8_t const* ptr = (gate_const_str8_t const*)data;
gate_size_t len = gate_str_length(*ptr);
return gate_hash_generate(*ptr, len);
}
static gate_hash_code_t gate_hash_generate_wstring(void const* data)
{
wchar_t const* const* ptr = (wchar_t const* const*)data;
gate_size_t len = 0;
wchar_t const* s = *ptr;
if (s)
{
while (*s)
{
++len;
++s;
}
}
return gate_hash_generate(*ptr, len * sizeof(wchar_t));
}
static gate_hash_code_t gate_hash_generate_guid(void const* data)
{
return gate_hash_generate(data, sizeof(gate_guid_t));
}
static gate_hash_code_t gate_hash_generate_date(void const* data)
{
return gate_hash_generate(data, sizeof(gate_date_t));
}
static gate_hash_code_t gate_hash_generate_daytime(void const* data)
{
return gate_hash_generate(data, sizeof(gate_daytime_t));
}
static gate_hash_code_t gate_hash_generate_datetime(void const* data)
{
return gate_hash_generate(data, sizeof(gate_datetime_t));
}
static gate_hash_code_t gate_hash_generate_time(void const* data)
{
return gate_hash_generate(data, sizeof(gate_time_t));
}
gate_hash_code_t gate_hash_generate_string(void const* data)
{
gate_string_t const* ptr = (gate_string_t const*)data;
return gate_hash_generate(gate_string_ptr(ptr, 0), gate_string_length(ptr));
}
static gate_hash_code_t gate_hash_generate_struct(void const* data)
{
gate_struct_t const* ptr = (gate_struct_t const*)data;
gate_hash_generator_context_t gen;
char const* name = gate_struct_get_name(ptr);
gate_size_t len = gate_struct_get_member_count(ptr);
gate_size_t ndx;
gate_type_id_t tid;
void const* ptr_member;<--- The scope of the variable 'ptr_member' can be reduced. [+]The scope of the variable 'ptr_member' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_type_hash_generator_t member_hashgen;
gate_hash_code_t member_hash;
gate_hash_generator_init(&gen);
gate_hash_generator_update(&gen, name, gate_str_length(name));
for (ndx = 0; ndx != len; ++ndx)
{
tid = gate_struct_get_member_type(ptr, ndx);
name = gate_struct_get_member_name(ptr, ndx);
ptr_member = gate_struct_get_member(ptr, ndx);
member_hashgen = gate_hash_generator_of(tid);
gate_hash_generator_update(&gen, &tid, sizeof(tid));
gate_hash_generator_update(&gen, name, gate_str_length(name));
if (member_hashgen)
{
member_hash = member_hashgen(ptr_member);
gate_hash_generator_update(&gen, &member_hash, sizeof(member_hash));
}
}
return gate_hash_generator_finish(&gen);
}
static gate_hash_code_t gate_hash_generate_object(void const* data)
{
gate_object_t const* obj = (gate_object_t const*)data;
gate_uintptr_t address = (gate_uintptr_t)obj;
return gate_hash_generate_address(&address);
}
static gate_hash_code_t gate_hash_generate_property(void const* data)
{
gate_property_t const* prop = (gate_property_t const*)data;
gate_hash_generator_context_t gen;
gate_uint32_t prop_type;
gate_bool_t b = false;
gate_int64_t i = 0;
gate_real64_t r = 0.0;
gate_string_t str = GATE_STRING_INIT_EMPTY;
gate_size_t len;
gate_size_t index;
gate_property_t const* ptr_prop;
gate_hash_code_t hv;
gate_array_t names = GATE_INIT_EMPTY;
gate_string_t const* ptr_name;<--- The scope of the variable 'ptr_name' can be reduced. [+]The scope of the variable 'ptr_name' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_hash_generator_init(&gen);
prop_type = gate_property_get_type(prop);
switch (prop_type)
{
case GATE_PROPERTY_TYPE_EMPTY:
break;
case GATE_PROPERTY_TYPE_BOOL:
gate_property_get_bool(prop, &b);
gate_hash_generator_update(&gen, b ? "1" : "0", 1);
break;
case GATE_PROPERTY_TYPE_INT:
gate_property_get_int(prop, &i);
gate_hash_generator_update(&gen, &i, sizeof(i));
break;
case GATE_PROPERTY_TYPE_REAL:
gate_property_get_real(prop, &r);
gate_hash_generator_update(&gen, &r, sizeof(r));
break;
case GATE_PROPERTY_TYPE_STRING:
gate_property_get_string(prop, &str);
gate_hash_generator_update(&gen, str.str, str.length);
gate_string_release(&str);
break;
case GATE_PROPERTY_TYPE_ARRAY:
len = gate_property_array_length(prop);
for (index = 0; index != len; ++index)
{
ptr_prop = gate_property_array_get(prop, index);
if (ptr_prop)
{
hv = gate_hash_generate_property(ptr_prop);
gate_hash_generator_update(&gen, &hv, sizeof(hv));
}
}
break;
case GATE_PROPERTY_TYPE_OBJECT:
if (gate_property_member_names(prop, &names))
{
len = gate_array_length(&names);
for (index = 0; index != len; ++index)
{
ptr_name = (gate_string_t const*)gate_array_get(&names, index);
if (ptr_name)
{
gate_hash_generator_update(&gen, ptr_name->str, ptr_name->length);
ptr_prop = gate_property_member_get(prop, ptr_name);
if (ptr_prop)
{
hv = gate_hash_generate_property(ptr_prop);
gate_hash_generator_update(&gen, &hv, sizeof(hv));
}
}
}
gate_array_release(&names);
}
break;
}
return gate_hash_generator_finish(&gen);
}
gate_type_hash_generator_t gate_hash_generator_of(gate_type_id_t type_id)
{
gate_type_hash_generator_t func = NULL;
switch (type_id)
{
case GATE_TYPE_VOID:
/* basic numeric types */
case GATE_TYPE_BOOL: return &gate_hash_generate_1;
case GATE_TYPE_I8: return &gate_hash_generate_1;
case GATE_TYPE_UI8: return &gate_hash_generate_1;
case GATE_TYPE_I16: return &gate_hash_generate_2;
case GATE_TYPE_UI16: return &gate_hash_generate_2;
case GATE_TYPE_I32: return &gate_hash_generate_4;
case GATE_TYPE_UI32: return &gate_hash_generate_4;
case GATE_TYPE_I64: return &gate_hash_generate_8;
case GATE_TYPE_UI64: return &gate_hash_generate_8;
case GATE_TYPE_R32: return &gate_hash_generate_4;
case GATE_TYPE_R64: return &gate_hash_generate_8;
case GATE_TYPE_ADDRESS: return &gate_hash_generate_address;
/* basic gate structures (POD) */
case GATE_TYPE_DATAPTR: return &gate_hash_generate_address;
case GATE_TYPE_FUNCPTR: return &gate_hash_generate_address;
case GATE_TYPE_CSTR: return &gate_hash_generate_cstring;
case GATE_TYPE_WSTR: return &gate_hash_generate_wstring;
case GATE_TYPE_GUID: return &gate_hash_generate_guid;
case GATE_TYPE_DATE: return &gate_hash_generate_date;
case GATE_TYPE_DAYTIME: return &gate_hash_generate_daytime;
case GATE_TYPE_DATETIME: return &gate_hash_generate_datetime;
case GATE_TYPE_TIME: return &gate_hash_generate_time;
/* basic generic types */
case GATE_TYPE_STRING: return &gate_hash_generate_string;
case GATE_TYPE_ARRAY: return NULL; /* not supported */
/* basic reference types */
case GATE_TYPE_STRUCT: return &gate_hash_generate_struct;
case GATE_TYPE_OBJECT: return &gate_hash_generate_object;
case GATE_TYPE_PROPERTY: return &gate_hash_generate_property;
/* arraylists */
/* arraylist of basic numeric types */
case GATE_TYPE_ARRAYLIST:
case GATE_TYPE_ARRAYLIST_BOOL:
case GATE_TYPE_ARRAYLIST_I8:
case GATE_TYPE_ARRAYLIST_UI8:
case GATE_TYPE_ARRAYLIST_I16:
case GATE_TYPE_ARRAYLIST_UI16:
case GATE_TYPE_ARRAYLIST_I32:
case GATE_TYPE_ARRAYLIST_UI32:
case GATE_TYPE_ARRAYLIST_I64:
case GATE_TYPE_ARRAYLIST_UI64:
case GATE_TYPE_ARRAYLIST_R32:
case GATE_TYPE_ARRAYLIST_R64:
case GATE_TYPE_ARRAYLIST_ADDRESS:
/* arraylist of basic gate structures (POD) */
case GATE_TYPE_ARRAYLIST_DATAPTR:
case GATE_TYPE_ARRAYLIST_FUNCPTR:
case GATE_TYPE_ARRAYLIST_CSTR:
case GATE_TYPE_ARRAYLIST_WSTR:
case GATE_TYPE_ARRAYLIST_GUID:
case GATE_TYPE_ARRAYLIST_DATE:
case GATE_TYPE_ARRAYLIST_DAYTIME:
case GATE_TYPE_ARRAYLIST_DATETIME:
case GATE_TYPE_ARRAYLIST_TIME:
/* arraylist of basic generic types */
case GATE_TYPE_ARRAYLIST_STRING:
case GATE_TYPE_ARRAYLIST_ARRAY:
/* arraylist of basic reference types */
case GATE_TYPE_ARRAYLIST_STRUCT:
case GATE_TYPE_ARRAYLIST_OBJECT:
case GATE_TYPE_ARRAYLIST_PROPERTY:
break;
}
GATE_DEBUG_ASSERT(func != NULL);
return func;
}
|