GCC Code Coverage Report


Directory: src/gate/
File: src/gate/guids.c
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 52 58 89.7%
Functions: 4 4 100.0%
Branches: 19 34 55.9%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/guids.h"
30 #include "gate/strings.h"
31 #include "gate/results.h"
32
33 #if defined(GATE_SYS_WIN) && !defined(GATE_SYS_WIN16)
34 # define GATE_CORE_GUIDS_WINAPI_IMPL 1
35 #else
36 # define GATE_CORE_GUIDS_GATE_IMPL 1
37 #endif
38
39
40 10 gate_bool_t gate_guid_equals(gate_guid_t const* g1, gate_guid_t const* g2)
41 {
42 10 return gate_mem_compare(g1, g2, sizeof(gate_guid_t)) == 0;
43 }
44 3 gate_result_t gate_guid_to_string(gate_guid_t const* guid, char text[37])
45 {
46 3 gate_str_print_hex_uint32(&text[0], 8, guid->item1, false);
47 3 text[8] = '-';
48 3 gate_str_print_hex_uint16(&text[9], 4, guid->item2, false);
49 3 text[13] = '-';
50 3 gate_str_print_hex_uint16(&text[14], 4, guid->item3, false);
51 3 text[18] = '-';
52 3 gate_str_print_hex_byte(&text[19], 2, guid->item4[0], false);
53 3 gate_str_print_hex_byte(&text[21], 2, guid->item4[1], false);
54 3 text[23] = '-';
55 3 gate_str_print_hex_byte(&text[24], 2, guid->item4[2], false);
56 3 gate_str_print_hex_byte(&text[26], 2, guid->item4[3], false);
57 3 gate_str_print_hex_byte(&text[28], 2, guid->item4[4], false);
58 3 gate_str_print_hex_byte(&text[30], 2, guid->item4[5], false);
59 3 gate_str_print_hex_byte(&text[32], 2, guid->item4[6], false);
60 3 gate_str_print_hex_byte(&text[34], 2, guid->item4[7], false);
61 3 text[36] = '\0';
62 3 return GATE_RESULT_OK;
63 }
64 3 gate_result_t gate_guid_parse_string(char const* srctext, gate_size_t srctextlen, gate_guid_t* guid)
65 {
66 gate_result_t ret;
67 gate_uint64_t val;
68 gate_size_t len;
69 char const* ptr;
70 gate_size_t ndx;
71 do
72 {
73
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
3 if ((srctext == NULL) || (guid == NULL))
74 {
75 2 ret = GATE_RESULT_INVALIDARG;
76 2 break;
77 }
78
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (srctextlen != 0)
79 {
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (*srctext == '{')
81 {
82 ++srctext;
83 --srctextlen;
84 }
85 }
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (srctextlen < 36)
87 {
88 ret = GATE_RESULT_INVALIDINPUT;
89 break;
90 }
91
92 1 ret = GATE_RESULT_INVALIDDATA;
93
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 if ((srctext[8] != '-') || (srctext[13] != '-') || (srctext[18] != '-') || (srctext[23] != '-'))
94 {
95 break;
96 }
97
98
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (8 != (len = gate_str_parse_hex_int(&srctext[0], 8, &val))) break;
99 1 guid->item1 = (gate_uint32_t)val;
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (4 != (len = gate_str_parse_hex_int(&srctext[9], 4, &val))) break;
101 1 guid->item2 = (gate_uint16_t)val;
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (4 != (len = gate_str_parse_hex_int(&srctext[14], 4, &val))) break;
103 1 guid->item3 = (gate_uint16_t)val;
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (2 != (len = gate_str_parse_hex_int(&srctext[19], 2, &val))) break;
105 1 guid->item4[0] = (gate_uint8_t)val;
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (2 != (len = gate_str_parse_hex_int(&srctext[21], 2, &val))) break;
107 1 guid->item4[1] = (gate_uint8_t)val;
108
109 1 ptr = &srctext[24];
110 1 ret = GATE_RESULT_OK;
111
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (ndx = 0; ndx != 6; ++ndx)
112 {
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (2 != (len = gate_str_parse_hex_int(ptr, 2, &val)))
114 {
115 ret = GATE_RESULT_INVALIDDATA;
116 break;
117 }
118 6 guid->item4[ndx + 2] = (gate_uint8_t)val;
119 6 ptr += 2;
120 }
121 } while (0);
122 3 return ret;
123 }
124
125
126 #if defined(GATE_CORE_GUIDS_WINAPI_IMPL)
127
128 #include "gate/platform/windows/win32com.h"
129
130 gate_result_t gate_guid_generate(gate_guid_t* guid)
131 {
132 GUID tmp;
133 HRESULT hr = gate_win32_com_create_guid(&tmp);
134 if (SUCCEEDED(hr))
135 {
136 gate_mem_copy(guid, &tmp, sizeof(gate_guid_t));
137 return GATE_RESULT_OK;
138 }
139 return GATE_RESULT_FAILED;
140 }
141
142 #endif /* GATE_CORE_GUIDS_WINAPI_IMPL */
143
144
145 #if defined(GATE_CORE_GUIDS_GATE_IMPL)
146
147 #include "gate/randomgen.h"
148
149 3 gate_result_t gate_guid_generate(gate_guid_t* guid)
150 {
151 gate_randomsession_t session;
152 3 gate_result_t ret = gate_randomgenerator_create(&session);
153
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (GATE_SUCCEEDED(ret))
154 {
155 3 ret = gate_randomgenerator_get_buffer(&session, guid, sizeof(gate_guid_t));
156 3 gate_randomgenerator_destroy(&session);
157 }
158 3 return ret;
159 }
160
161 #endif /* GATE_CORE_GUIDS_GATE_IMPL */
162