Coverage Report

Created: 2026-09-14 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/fu-uswid-struct.c
Line
Count
Source
1
/*
2
 * This file is auto-generated, do not modify!
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
#include "config.h"
8
9
#include <glib.h>
10
11
#include "fu-uswid-struct.h"
12
#include "fu-byte-array.h"
13
#include "fu-bytes.h"
14
#include "fu-mem-private.h"
15
#include "fu-string.h"
16
17
#ifdef G_LOG_DOMAIN
18
  #undef G_LOG_DOMAIN
19
#endif
20
0
#define G_LOG_DOMAIN "FuStruct"
21
22
/**
23
 * fu_uswid_header_flags_to_string:
24
 * @val: value, e.g. %FU_USWID_HEADER_FLAG_COMPRESSED
25
 *
26
 * Converts an enumerated value to a string.
27
 *
28
 * Returns: identifier string
29
 **/
30
const gchar *
31
fu_uswid_header_flags_to_string(FuUswidHeaderFlags val)
32
0
{
33
0
    if (val == FU_USWID_HEADER_FLAG_NONE)
34
0
        return "none";
35
0
    if (val == FU_USWID_HEADER_FLAG_COMPRESSED)
36
0
        return "compressed";
37
0
    return NULL;
38
0
}
39
/**
40
 * fu_uswid_header_flags_from_string:
41
 * @val: (nullable): a string, e.g. `compressed`
42
 *
43
 * Converts a string to an enumerated value.
44
 *
45
 * Returns: enumerated value
46
 **/
47
FuUswidHeaderFlags
48
fu_uswid_header_flags_from_string(const gchar *val)
49
0
{
50
0
    if (g_strcmp0(val, "none") == 0)
51
0
        return FU_USWID_HEADER_FLAG_NONE;
52
0
    if (g_strcmp0(val, "compressed") == 0)
53
0
        return FU_USWID_HEADER_FLAG_COMPRESSED;
54
0
    return FU_USWID_HEADER_FLAG_NONE;
55
0
}
56
57
/**
58
 * fu_uswid_payload_compression_to_string:
59
 * @val: value, e.g. %FU_USWID_PAYLOAD_COMPRESSION_ZLIB
60
 *
61
 * Converts an enumerated value to a string.
62
 *
63
 * Returns: identifier string
64
 **/
65
const gchar *
66
fu_uswid_payload_compression_to_string(FuUswidPayloadCompression val)
67
0
{
68
0
    if (val == FU_USWID_PAYLOAD_COMPRESSION_NONE)
69
0
        return "none";
70
0
    if (val == FU_USWID_PAYLOAD_COMPRESSION_ZLIB)
71
0
        return "zlib";
72
0
    if (val == FU_USWID_PAYLOAD_COMPRESSION_LZMA)
73
0
        return "lzma";
74
0
    return NULL;
75
0
}
76
/**
77
 * fu_uswid_payload_compression_from_string:
78
 * @val: (nullable): a string, e.g. `zlib`
79
 *
80
 * Converts a string to an enumerated value.
81
 *
82
 * Returns: enumerated value
83
 **/
84
FuUswidPayloadCompression
85
fu_uswid_payload_compression_from_string(const gchar *val)
86
0
{
87
0
    if (g_strcmp0(val, "none") == 0)
88
0
        return FU_USWID_PAYLOAD_COMPRESSION_NONE;
89
0
    if (g_strcmp0(val, "zlib") == 0)
90
0
        return FU_USWID_PAYLOAD_COMPRESSION_ZLIB;
91
0
    if (g_strcmp0(val, "lzma") == 0)
92
0
        return FU_USWID_PAYLOAD_COMPRESSION_LZMA;
93
0
    return FU_USWID_PAYLOAD_COMPRESSION_NONE;
94
0
}
95
96
/**
97
 * fu_uswid_payload_format_to_string:
98
 * @val: value, e.g. %FU_USWID_PAYLOAD_FORMAT_CYCLONEDX
99
 *
100
 * Converts an enumerated value to a string.
101
 *
102
 * Returns: identifier string
103
 **/
104
const gchar *
105
fu_uswid_payload_format_to_string(FuUswidPayloadFormat val)
106
0
{
107
0
    if (val == FU_USWID_PAYLOAD_FORMAT_COSWID)
108
0
        return "coswid";
109
0
    if (val == FU_USWID_PAYLOAD_FORMAT_CYCLONEDX)
110
0
        return "cyclonedx";
111
0
    if (val == FU_USWID_PAYLOAD_FORMAT_SPDX)
112
0
        return "spdx";
113
0
    return NULL;
114
0
}
115
/**
116
 * fu_uswid_payload_format_from_string:
117
 * @val: (nullable): a string, e.g. `cyclonedx`
118
 *
119
 * Converts a string to an enumerated value.
120
 *
121
 * Returns: enumerated value
122
 **/
123
FuUswidPayloadFormat
124
fu_uswid_payload_format_from_string(const gchar *val)
125
0
{
126
0
    if (g_strcmp0(val, "coswid") == 0)
127
0
        return FU_USWID_PAYLOAD_FORMAT_COSWID;
128
0
    if (g_strcmp0(val, "cyclonedx") == 0)
129
0
        return FU_USWID_PAYLOAD_FORMAT_CYCLONEDX;
130
0
    if (g_strcmp0(val, "spdx") == 0)
131
0
        return FU_USWID_PAYLOAD_FORMAT_SPDX;
132
0
    return FU_USWID_PAYLOAD_FORMAT_COSWID;
133
0
}
134
/**
135
 * fu_struct_uswid_ref: (skip):
136
 **/
137
FuStructUswid *
138
fu_struct_uswid_ref(FuStructUswid *st)
139
0
{
140
0
    g_return_val_if_fail(st != NULL, NULL);
141
0
    st->refcount++;
142
0
    return st;
143
0
}
144
/**
145
 * fu_struct_uswid_unref: (skip):
146
 **/
147
void
148
fu_struct_uswid_unref(FuStructUswid *st)
149
135k
{
150
135k
    g_return_if_fail(st != NULL);
151
135k
    if (st->refcount == 0) {
152
0
        g_critical("FuStructUswid refcount already zero");
153
0
        return;
154
0
    }
155
135k
    if (--st->refcount > 0)
156
0
        return;
157
135k
    if (st->buf != NULL)
158
132k
        g_byte_array_unref(st->buf);
159
135k
    g_free(st);
160
135k
}
161
/**
162
 * fu_struct_uswid_new_internal: (skip):
163
 **/
164
static FuStructUswid *
165
fu_struct_uswid_new_internal(void)
166
135k
{
167
135k
    FuStructUswid *st = g_new0(FuStructUswid, 1);
168
135k
    st->refcount = 1;
169
135k
    return st;
170
135k
}
171
172
/* getters */
173
/**
174
 * fu_struct_uswid_get_magic: (skip):
175
 **/
176
static const fwupd_guid_t *
177
fu_struct_uswid_get_magic(const FuStructUswid *st)
178
131k
{
179
131k
    g_return_val_if_fail(st != NULL, NULL);
180
131k
    return (const fwupd_guid_t *) (st->buf->data + 0);
181
131k
}
182
/**
183
 * fu_struct_uswid_get_hdrver: (skip):
184
 **/
185
guint8
186
fu_struct_uswid_get_hdrver(const FuStructUswid *st)
187
65.9k
{
188
65.9k
    g_return_val_if_fail(st != NULL, 0x0);
189
65.9k
    return st->buf->data[16];
190
65.9k
}
191
/**
192
 * fu_struct_uswid_get_hdrsz: (skip):
193
 **/
194
guint16
195
fu_struct_uswid_get_hdrsz(const FuStructUswid *st)
196
65.5k
{
197
65.5k
    g_return_val_if_fail(st != NULL, 0x0);
198
65.5k
    return fu_memread_uint16(st->buf->data + 17, G_LITTLE_ENDIAN);
199
65.5k
}
200
/**
201
 * fu_struct_uswid_get_payloadsz: (skip):
202
 **/
203
guint32
204
fu_struct_uswid_get_payloadsz(const FuStructUswid *st)
205
65.5k
{
206
65.5k
    g_return_val_if_fail(st != NULL, 0x0);
207
65.5k
    return fu_memread_uint32(st->buf->data + 19, G_LITTLE_ENDIAN);
208
65.5k
}
209
/**
210
 * fu_struct_uswid_get_flags: (skip):
211
 **/
212
FuUswidHeaderFlags
213
fu_struct_uswid_get_flags(const FuStructUswid *st)
214
60.2k
{
215
60.2k
    g_return_val_if_fail(st != NULL, 0x0);
216
60.2k
    return st->buf->data[23];
217
60.2k
}
218
/**
219
 * fu_struct_uswid_get_compression: (skip):
220
 **/
221
FuUswidPayloadCompression
222
fu_struct_uswid_get_compression(const FuStructUswid *st)
223
5.09k
{
224
5.09k
    g_return_val_if_fail(st != NULL, 0x0);
225
5.09k
    return st->buf->data[24];
226
5.09k
}
227
/**
228
 * fu_struct_uswid_get_format: (skip):
229
 **/
230
FuUswidPayloadFormat
231
fu_struct_uswid_get_format(const FuStructUswid *st)
232
16.8k
{
233
16.8k
    g_return_val_if_fail(st != NULL, 0x0);
234
16.8k
    return st->buf->data[25];
235
16.8k
}
236
237
/* setters */
238
/**
239
 * fu_struct_uswid_set_magic: (skip):
240
 **/
241
static void
242
fu_struct_uswid_set_magic(FuStructUswid *st, const fwupd_guid_t *value)
243
3.23k
{
244
3.23k
    g_return_if_fail(st != NULL);
245
3.23k
    g_return_if_fail(value != NULL);
246
3.23k
    memcpy(st->buf->data + 0, value, sizeof(*value)); /* nocheck:blocked */
247
3.23k
}
248
/**
249
 * fu_struct_uswid_set_hdrver: (skip):
250
 **/
251
void
252
fu_struct_uswid_set_hdrver(FuStructUswid *st, guint8 value)
253
6.42k
{
254
6.42k
    g_return_if_fail(st != NULL);
255
6.42k
    st->buf->data[16] = value;
256
6.42k
}
257
/**
258
 * fu_struct_uswid_set_hdrsz: (skip):
259
 **/
260
void
261
fu_struct_uswid_set_hdrsz(FuStructUswid *st, guint16 value)
262
6.42k
{
263
6.42k
    g_return_if_fail(st != NULL);
264
6.42k
    fu_memwrite_uint16(st->buf->data + 17, value, G_LITTLE_ENDIAN);
265
6.42k
}
266
/**
267
 * fu_struct_uswid_set_payloadsz: (skip):
268
 **/
269
void
270
fu_struct_uswid_set_payloadsz(FuStructUswid *st, guint32 value)
271
3.18k
{
272
3.18k
    g_return_if_fail(st != NULL);
273
3.18k
    fu_memwrite_uint32(st->buf->data + 19, value, G_LITTLE_ENDIAN);
274
3.18k
}
275
/**
276
 * fu_struct_uswid_set_flags: (skip):
277
 **/
278
void
279
fu_struct_uswid_set_flags(FuStructUswid *st, FuUswidHeaderFlags value)
280
3.18k
{
281
3.18k
    g_return_if_fail(st != NULL);
282
3.18k
    st->buf->data[23] = value;
283
3.18k
}
284
/**
285
 * fu_struct_uswid_set_compression: (skip):
286
 **/
287
void
288
fu_struct_uswid_set_compression(FuStructUswid *st, FuUswidPayloadCompression value)
289
6.42k
{
290
6.42k
    g_return_if_fail(st != NULL);
291
6.42k
    st->buf->data[24] = value;
292
6.42k
}
293
/**
294
 * fu_struct_uswid_set_format: (skip):
295
 **/
296
void
297
fu_struct_uswid_set_format(FuStructUswid *st, FuUswidPayloadFormat value)
298
6.42k
{
299
6.42k
    g_return_if_fail(st != NULL);
300
6.42k
    st->buf->data[25] = value;
301
6.42k
}
302
/**
303
 * fu_struct_uswid_new: (skip):
304
 **/
305
FuStructUswid *
306
fu_struct_uswid_new(void)
307
3.23k
{
308
3.23k
    FuStructUswid *st = fu_struct_uswid_new_internal();
309
3.23k
    st->buf = g_byte_array_sized_new(26);
310
3.23k
    fu_byte_array_set_size(st->buf, 26, 0x0);
311
3.23k
    fu_struct_uswid_set_magic(st, (fwupd_guid_t *) "\x53\x42\x4f\x4d\xd6\xba\x2e\xac\xa3\xe6\x7a\x52\xaa\xee\x3b\xaf");
312
3.23k
    fu_struct_uswid_set_hdrver(st, 0x04);
313
3.23k
    fu_struct_uswid_set_hdrsz(st, 26);
314
3.23k
    fu_struct_uswid_set_compression(st, FU_USWID_PAYLOAD_COMPRESSION_NONE);
315
3.23k
    fu_struct_uswid_set_format(st, FU_USWID_PAYLOAD_FORMAT_COSWID);
316
3.23k
    return st;
317
3.23k
}
318
/**
319
 * fu_struct_uswid_to_string: (skip):
320
 **/
321
static gchar *
322
fu_struct_uswid_to_string(const FuStructUswid *st)
323
0
{
324
0
    g_autoptr(GString) str = g_string_new("FuStructUswid:\n");
325
0
    g_return_val_if_fail(st != NULL, NULL);
326
0
    {
327
0
        g_autofree gchar *tmp = fwupd_guid_to_string(fu_struct_uswid_get_magic(st), FWUPD_GUID_FLAG_MIXED_ENDIAN);
328
0
        g_string_append_printf(str, "  magic: %s\n", tmp);
329
0
    }
330
0
    g_string_append_printf(str, "  hdrver: 0x%x\n",
331
0
                           (guint) fu_struct_uswid_get_hdrver(st));
332
0
    g_string_append_printf(str, "  hdrsz: 0x%x\n",
333
0
                           (guint) fu_struct_uswid_get_hdrsz(st));
334
0
    g_string_append_printf(str, "  payloadsz: 0x%x\n",
335
0
                           (guint) fu_struct_uswid_get_payloadsz(st));
336
0
    {
337
0
        const gchar *tmp = fu_uswid_header_flags_to_string(fu_struct_uswid_get_flags(st));
338
0
        if (tmp != NULL) {
339
0
            g_string_append_printf(str, "  flags: 0x%x [%s]\n", (guint) fu_struct_uswid_get_flags(st), tmp);
340
0
        } else {
341
0
            g_string_append_printf(str, "  flags: 0x%x\n", (guint) fu_struct_uswid_get_flags(st));
342
0
        }
343
0
    }
344
0
    {
345
0
        const gchar *tmp = fu_uswid_payload_compression_to_string(fu_struct_uswid_get_compression(st));
346
0
        if (tmp != NULL) {
347
0
            g_string_append_printf(str, "  compression: 0x%x [%s]\n", (guint) fu_struct_uswid_get_compression(st), tmp);
348
0
        } else {
349
0
            g_string_append_printf(str, "  compression: 0x%x\n", (guint) fu_struct_uswid_get_compression(st));
350
0
        }
351
0
    }
352
0
    {
353
0
        const gchar *tmp = fu_uswid_payload_format_to_string(fu_struct_uswid_get_format(st));
354
0
        if (tmp != NULL) {
355
0
            g_string_append_printf(str, "  format: 0x%x [%s]\n", (guint) fu_struct_uswid_get_format(st), tmp);
356
0
        } else {
357
0
            g_string_append_printf(str, "  format: 0x%x\n", (guint) fu_struct_uswid_get_format(st));
358
0
        }
359
0
    }
360
0
    if (str->len > 0)
361
0
        g_string_set_size(str, str->len - 1);
362
0
    return g_string_free(g_steal_pointer(&str), FALSE);
363
0
}
364
static gboolean
365
fu_struct_uswid_validate_internal(FuStructUswid *st, GError **error)
366
131k
{
367
131k
    g_return_val_if_fail(st != NULL, FALSE);
368
131k
    if (memcmp(fu_struct_uswid_get_magic(st), "\x53\x42\x4f\x4d\xd6\xba\x2e\xac\xa3\xe6\x7a\x52\xaa\xee\x3b\xaf", 16) != 0) {
369
0
        g_set_error_literal(error,
370
0
                            FWUPD_ERROR,
371
0
                            FWUPD_ERROR_INVALID_DATA,
372
0
                            "constant FuStructUswid.magic was not valid");
373
0
        return FALSE;
374
0
    }
375
131k
    return TRUE;
376
131k
}
377
/**
378
 * fu_struct_uswid_validate_stream: (skip):
379
 **/
380
gboolean
381
fu_struct_uswid_validate_stream(FuInputStream *stream, gsize offset, GError **error)
382
66.3k
{
383
66.3k
    g_autoptr(FuStructUswid) st = fu_struct_uswid_new_internal();
384
66.3k
    g_return_val_if_fail(FU_IS_INPUT_STREAM(stream), FALSE);
385
66.3k
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
386
66.3k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 26, NULL, error);
387
66.3k
    if (st->buf == NULL) {
388
0
        g_prefix_error(error, "FuStructUswid failed read of 0x%x: ", (guint) 26);
389
0
        return FALSE;
390
0
    }
391
66.3k
    if (st->buf->len != 26) {
392
330
        g_set_error(error,
393
330
                    FWUPD_ERROR,
394
330
                    FWUPD_ERROR_INVALID_DATA,
395
330
                    "FuStructUswid requested 0x%x and got 0x%x",
396
330
                    (guint) 26,
397
330
                    (guint) st->buf->len);
398
330
        return FALSE;
399
330
    }
400
65.9k
    return fu_struct_uswid_validate_internal(st, error);
401
66.3k
}
402
static gboolean
403
fu_struct_uswid_parse_internal(FuStructUswid *st, GError **error)
404
65.9k
{
405
65.9k
    if (g_log_get_debug_enabled()) {
406
0
        g_autofree gchar *str = fu_struct_uswid_to_string(st);
407
0
        g_debug("%s", str);
408
0
    }
409
65.9k
    if (!fu_struct_uswid_validate_internal(st, error))
410
0
        return FALSE;
411
65.9k
    return TRUE;
412
65.9k
}
413
/**
414
 * fu_struct_uswid_parse_stream: (skip):
415
 **/
416
FuStructUswid *
417
fu_struct_uswid_parse_stream(FuInputStream *stream, gsize offset, GError **error)
418
65.9k
{
419
65.9k
    g_autoptr(FuStructUswid) st = fu_struct_uswid_new_internal();
420
65.9k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 26, NULL, error);
421
65.9k
    if (st->buf == NULL) {
422
0
        g_prefix_error(error, "FuStructUswid failed read of 0x%x: ", (guint) 26);
423
0
        return NULL;
424
0
    }
425
65.9k
    if (st->buf->len != 26) {
426
0
        g_set_error(error,
427
0
                    FWUPD_ERROR,
428
0
                    FWUPD_ERROR_INVALID_DATA,
429
0
                    "FuStructUswid requested 0x%x and got 0x%x",
430
0
                    (guint) 26,
431
0
                    (guint) st->buf->len);
432
0
        return NULL;
433
0
    }
434
65.9k
    if (!fu_struct_uswid_parse_internal(st, error))
435
0
        return NULL;
436
65.9k
    return g_steal_pointer(&st);
437
65.9k
}