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 | /* 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/guids.h"
#include "gate/strings.h"
#include "gate/results.h"
#if defined(GATE_SYS_WIN) && !defined(GATE_SYS_WIN16)
# define GATE_CORE_GUIDS_WINAPI_IMPL 1
#else
# define GATE_CORE_GUIDS_GATE_IMPL 1
#endif
gate_bool_t gate_guid_equals(gate_guid_t const* g1, gate_guid_t const* g2)
{
return gate_mem_compare(g1, g2, sizeof(gate_guid_t)) == 0;
}
gate_result_t gate_guid_to_string(gate_guid_t const* guid, char text[37])
{
gate_str_print_hex_uint32(&text[0], 8, guid->item1, false);
text[8] = '-';
gate_str_print_hex_uint16(&text[9], 4, guid->item2, false);
text[13] = '-';
gate_str_print_hex_uint16(&text[14], 4, guid->item3, false);
text[18] = '-';
gate_str_print_hex_byte(&text[19], 2, guid->item4[0], false);
gate_str_print_hex_byte(&text[21], 2, guid->item4[1], false);
text[23] = '-';
gate_str_print_hex_byte(&text[24], 2, guid->item4[2], false);
gate_str_print_hex_byte(&text[26], 2, guid->item4[3], false);
gate_str_print_hex_byte(&text[28], 2, guid->item4[4], false);
gate_str_print_hex_byte(&text[30], 2, guid->item4[5], false);
gate_str_print_hex_byte(&text[32], 2, guid->item4[6], false);
gate_str_print_hex_byte(&text[34], 2, guid->item4[7], false);
text[36] = '\0';
return GATE_RESULT_OK;
}
gate_result_t gate_guid_parse_string(char const* srctext, gate_size_t srctextlen, gate_guid_t* guid)
{
gate_result_t ret;
gate_uint64_t val;
gate_size_t len;
char const* ptr;<--- The scope of the variable 'ptr' can be reduced. [+]The scope of the variable 'ptr' 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_size_t ndx;
do
{
if ((srctext == NULL) || (guid == NULL))
{
ret = GATE_RESULT_INVALIDARG;
break;
}
if (srctextlen != 0)
{
if (*srctext == '{')
{
++srctext;
--srctextlen;
}
}
if (srctextlen < 36)
{
ret = GATE_RESULT_INVALIDINPUT;
break;
}
ret = GATE_RESULT_INVALIDDATA;<--- Variable 'ret' is reassigned a value before the old one has been used.
if ((srctext[8] != '-') || (srctext[13] != '-') || (srctext[18] != '-') || (srctext[23] != '-'))
{
break;
}
if (8 != (len = gate_str_parse_hex_int(&srctext[0], 8, &val))) break;
guid->item1 = (gate_uint32_t)val;
if (4 != (len = gate_str_parse_hex_int(&srctext[9], 4, &val))) break;
guid->item2 = (gate_uint16_t)val;
if (4 != (len = gate_str_parse_hex_int(&srctext[14], 4, &val))) break;
guid->item3 = (gate_uint16_t)val;
if (2 != (len = gate_str_parse_hex_int(&srctext[19], 2, &val))) break;
guid->item4[0] = (gate_uint8_t)val;
if (2 != (len = gate_str_parse_hex_int(&srctext[21], 2, &val))) break;
guid->item4[1] = (gate_uint8_t)val;
ptr = &srctext[24];
ret = GATE_RESULT_OK;<--- Variable 'ret' is reassigned a value before the old one has been used.
for (ndx = 0; ndx != 6; ++ndx)
{
if (2 != (len = gate_str_parse_hex_int(ptr, 2, &val)))
{
ret = GATE_RESULT_INVALIDDATA;
break;
}
guid->item4[ndx + 2] = (gate_uint8_t)val;
ptr += 2;
}
} while (0);
return ret;
}
#if defined(GATE_CORE_GUIDS_WINAPI_IMPL)
#include "gate/platform/windows/win32com.h"
gate_result_t gate_guid_generate(gate_guid_t* guid)
{
GUID tmp;
HRESULT hr = gate_win32_com_create_guid(&tmp);
if (SUCCEEDED(hr))
{
gate_mem_copy(guid, &tmp, sizeof(gate_guid_t));
return GATE_RESULT_OK;
}
return GATE_RESULT_FAILED;
}
#endif /* GATE_CORE_GUIDS_WINAPI_IMPL */
#if defined(GATE_CORE_GUIDS_GATE_IMPL)
#include "gate/randomgen.h"
gate_result_t gate_guid_generate(gate_guid_t* guid)
{
gate_randomsession_t session;
gate_result_t ret = gate_randomgenerator_create(&session);
if (GATE_SUCCEEDED(ret))
{
ret = gate_randomgenerator_get_buffer(&session, guid, sizeof(gate_guid_t));
gate_randomgenerator_destroy(&session);
}
return ret;
}
#endif /* GATE_CORE_GUIDS_GATE_IMPL */
|