GCC Code Coverage Report


Directory: src/gate/
File: src/gate/guids.c
Date: 2025-12-12 23:40:09
Exec Total Coverage
Lines: 51 59 86.4%
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 5 gate_result_t gate_guid_to_string(gate_guid_t const* guid, char text[37])
45 {
46 5 gate_str_print_hex_uint32(&text[0], 8, guid->item1, false);
47 5 text[8] = '-';
48 5 gate_str_print_hex_uint16(&text[9], 4, guid->item2, false);
49 5 text[13] = '-';
50 5 gate_str_print_hex_uint16(&text[14], 4, guid->item3, false);
51 5 text[18] = '-';
52 5 gate_str_print_hex_byte(&text[19], 2, guid->item4[0], false);
53 5 gate_str_print_hex_byte(&text[21], 2, guid->item4[1], false);
54 5 text[23] = '-';
55 5 gate_str_print_hex_byte(&text[24], 2, guid->item4[2], false);
56 5 gate_str_print_hex_byte(&text[26], 2, guid->item4[3], false);
57 5 gate_str_print_hex_byte(&text[28], 2, guid->item4[4], false);
58 5 gate_str_print_hex_byte(&text[30], 2, guid->item4[5], false);
59 5 gate_str_print_hex_byte(&text[32], 2, guid->item4[6], false);
60 5 gate_str_print_hex_byte(&text[34], 2, guid->item4[7], false);
61 5 text[36] = '\0';
62 5 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 do
68 {
69 gate_uint64_t val;
70 gate_size_t len;
71 char const* ptr;
72 gate_size_t ndx;
73
74
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))
75 {
76 2 ret = GATE_RESULT_INVALIDARG;
77 2 break;
78 }
79
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (srctextlen != 0)
80 {
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (*srctext == '{')
82 {
83 ++srctext;
84 --srctextlen;
85 }
86 }
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (srctextlen < 36)
88 {
89 ret = GATE_RESULT_INVALIDINPUT;
90 break;
91 }
92
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 not taken.
✓ Branch 7 taken 1 times.
1 if ((srctext[8] != '-') || (srctext[13] != '-') || (srctext[18] != '-') || (srctext[23] != '-'))
94 {
95 ret = GATE_RESULT_INVALIDDATA;
96 break;
97 }
98
99
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;
100 1 guid->item1 = (gate_uint32_t)val;
101
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;
102 1 guid->item2 = (gate_uint16_t)val;
103
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;
104 1 guid->item3 = (gate_uint16_t)val;
105
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;
106 1 guid->item4[0] = (gate_uint8_t)val;
107
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;
108 1 guid->item4[1] = (gate_uint8_t)val;
109
110 1 ptr = &srctext[24];
111 1 ret = GATE_RESULT_OK;
112
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (ndx = 0; ndx != 6; ++ndx)
113 {
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (2 != (len = gate_str_parse_hex_int(ptr, 2, &val)))
115 {
116 ret = GATE_RESULT_INVALIDDATA;
117 break;
118 }
119 6 guid->item4[ndx + 2] = (gate_uint8_t)val;
120 6 ptr += 2;
121 }
122 } while (0);
123 3 return ret;
124 }
125
126
127 #if defined(GATE_CORE_GUIDS_WINAPI_IMPL)
128
129 #include "gate/platform/windows/win32com.h"
130
131 gate_result_t gate_guid_generate(gate_guid_t* guid)
132 {
133 GUID tmp;
134 HRESULT hr = gate_win32_com_create_guid(&tmp);
135 if (SUCCEEDED(hr))
136 {
137 gate_mem_copy(guid, &tmp, sizeof(gate_guid_t));
138 return GATE_RESULT_OK;
139 }
140 return GATE_RESULT_FAILED;
141 }
142
143 #endif /* GATE_CORE_GUIDS_WINAPI_IMPL */
144
145
146 #if defined(GATE_CORE_GUIDS_GATE_IMPL)
147
148 #include "gate/randomgen.h"
149
150 4 gate_result_t gate_guid_generate(gate_guid_t* guid)
151 {
152 gate_randomsession_t session;
153 4 gate_result_t ret = gate_randomgenerator_create(&session);
154
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (GATE_SUCCEEDED(ret))
155 {
156 4 ret = gate_randomgenerator_get_buffer(&session, guid, sizeof(gate_guid_t));
157 4 gate_randomgenerator_destroy(&session);
158 }
159 4 return ret;
160 }
161
162 #endif /* GATE_CORE_GUIDS_GATE_IMPL */
163