Coverage Report

Created: 2026-12-01 06:28

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.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
    if (val == FU_USWID_PAYLOAD_COMPRESSION_VENDOR)
75
0
        return "vendor";
76
0
    return NULL;
77
0
}
78
/**
79
 * fu_uswid_payload_compression_from_string:
80
 * @val: (nullable): a string, e.g. `zlib`
81
 *
82
 * Converts a string to an enumerated value.
83
 *
84
 * Returns: enumerated value
85
 **/
86
FuUswidPayloadCompression
87
fu_uswid_payload_compression_from_string(const gchar *val)
88
0
{
89
0
    if (g_strcmp0(val, "none") == 0)
90
0
        return FU_USWID_PAYLOAD_COMPRESSION_NONE;
91
0
    if (g_strcmp0(val, "zlib") == 0)
92
0
        return FU_USWID_PAYLOAD_COMPRESSION_ZLIB;
93
0
    if (g_strcmp0(val, "lzma") == 0)
94
0
        return FU_USWID_PAYLOAD_COMPRESSION_LZMA;
95
0
    if (g_strcmp0(val, "vendor") == 0)
96
0
        return FU_USWID_PAYLOAD_COMPRESSION_VENDOR;
97
0
    return FU_USWID_PAYLOAD_COMPRESSION_NONE;
98
0
}
99
100
/**
101
 * fu_uswid_payload_format_to_string:
102
 * @val: value, e.g. %FU_USWID_PAYLOAD_FORMAT_CYCLONEDX
103
 *
104
 * Converts an enumerated value to a string.
105
 *
106
 * Returns: identifier string
107
 **/
108
const gchar *
109
fu_uswid_payload_format_to_string(FuUswidPayloadFormat val)
110
0
{
111
0
    if (val == FU_USWID_PAYLOAD_FORMAT_COSWID)
112
0
        return "coswid";
113
0
    if (val == FU_USWID_PAYLOAD_FORMAT_CYCLONEDX)
114
0
        return "cyclonedx";
115
0
    if (val == FU_USWID_PAYLOAD_FORMAT_SPDX)
116
0
        return "spdx";
117
0
    return NULL;
118
0
}
119
/**
120
 * fu_uswid_payload_format_from_string:
121
 * @val: (nullable): a string, e.g. `cyclonedx`
122
 *
123
 * Converts a string to an enumerated value.
124
 *
125
 * Returns: enumerated value
126
 **/
127
FuUswidPayloadFormat
128
fu_uswid_payload_format_from_string(const gchar *val)
129
0
{
130
0
    if (g_strcmp0(val, "coswid") == 0)
131
0
        return FU_USWID_PAYLOAD_FORMAT_COSWID;
132
0
    if (g_strcmp0(val, "cyclonedx") == 0)
133
0
        return FU_USWID_PAYLOAD_FORMAT_CYCLONEDX;
134
0
    if (g_strcmp0(val, "spdx") == 0)
135
0
        return FU_USWID_PAYLOAD_FORMAT_SPDX;
136
0
    return FU_USWID_PAYLOAD_FORMAT_COSWID;
137
0
}
138
/**
139
 * fu_struct_uswid_ref: (skip):
140
 **/
141
FuStructUswid *
142
fu_struct_uswid_ref(FuStructUswid *st)
143
0
{
144
0
    g_return_val_if_fail(st != NULL, NULL);
145
0
    st->refcount++;
146
0
    return st;
147
0
}
148
/**
149
 * fu_struct_uswid_unref: (skip):
150
 **/
151
void
152
fu_struct_uswid_unref(FuStructUswid *st)
153
150k
{
154
150k
    g_return_if_fail(st != NULL);
155
150k
    if (st->refcount == 0) {
156
0
        g_critical("FuStructUswid refcount already zero");
157
0
        return;
158
0
    }
159
150k
    if (--st->refcount > 0)
160
0
        return;
161
150k
    if (st->buf != NULL)
162
149k
        g_byte_array_unref(st->buf);
163
150k
    g_free(st);
164
150k
}
165
/**
166
 * fu_struct_uswid_new_internal: (skip):
167
 **/
168
static FuStructUswid *
169
fu_struct_uswid_new_internal(void)
170
150k
{
171
150k
    FuStructUswid *st = g_new0(FuStructUswid, 1);
172
150k
    st->refcount = 1;
173
150k
    return st;
174
150k
}
175
176
/* getters */
177
/**
178
 * fu_struct_uswid_get_magic: (skip):
179
 **/
180
static const fwupd_guid_t *
181
fu_struct_uswid_get_magic(const FuStructUswid *st)
182
148k
{
183
148k
    g_return_val_if_fail(st != NULL, NULL);
184
148k
    return (const fwupd_guid_t *) (st->buf->data + 0);
185
148k
}
186
/**
187
 * fu_struct_uswid_get_hdrver: (skip):
188
 **/
189
guint8
190
fu_struct_uswid_get_hdrver(const FuStructUswid *st)
191
74.4k
{
192
74.4k
    g_return_val_if_fail(st != NULL, 0x0);
193
74.4k
    return st->buf->data[16];
194
74.4k
}
195
/**
196
 * fu_struct_uswid_get_hdrsz: (skip):
197
 **/
198
guint16
199
fu_struct_uswid_get_hdrsz(const FuStructUswid *st)
200
74.0k
{
201
74.0k
    g_return_val_if_fail(st != NULL, 0x0);
202
74.0k
    return fu_memread_uint16(st->buf->data + 17, G_LITTLE_ENDIAN);
203
74.0k
}
204
/**
205
 * fu_struct_uswid_get_payloadsz: (skip):
206
 **/
207
guint32
208
fu_struct_uswid_get_payloadsz(const FuStructUswid *st)
209
74.0k
{
210
74.0k
    g_return_val_if_fail(st != NULL, 0x0);
211
74.0k
    return fu_memread_uint32(st->buf->data + 19, G_LITTLE_ENDIAN);
212
74.0k
}
213
/**
214
 * fu_struct_uswid_get_flags: (skip):
215
 **/
216
FuUswidHeaderFlags
217
fu_struct_uswid_get_flags(const FuStructUswid *st)
218
68.4k
{
219
68.4k
    g_return_val_if_fail(st != NULL, 0x0);
220
68.4k
    return st->buf->data[23];
221
68.4k
}
222
/**
223
 * fu_struct_uswid_get_compression: (skip):
224
 **/
225
FuUswidPayloadCompression
226
fu_struct_uswid_get_compression(const FuStructUswid *st)
227
4.51k
{
228
4.51k
    g_return_val_if_fail(st != NULL, 0x0);
229
4.51k
    return st->buf->data[24];
230
4.51k
}
231
/**
232
 * fu_struct_uswid_get_format: (skip):
233
 **/
234
FuUswidPayloadFormat
235
fu_struct_uswid_get_format(const FuStructUswid *st)
236
23.0k
{
237
23.0k
    g_return_val_if_fail(st != NULL, 0x0);
238
23.0k
    return st->buf->data[25];
239
23.0k
}
240
241
/* setters */
242
/**
243
 * fu_struct_uswid_set_magic: (skip):
244
 **/
245
static void
246
fu_struct_uswid_set_magic(FuStructUswid *st, const fwupd_guid_t *value)
247
868
{
248
868
    g_return_if_fail(st != NULL);
249
868
    g_return_if_fail(value != NULL);
250
868
    memcpy(st->buf->data + 0, value, sizeof(*value)); /* nocheck:blocked */
251
868
}
252
/**
253
 * fu_struct_uswid_set_hdrver: (skip):
254
 **/
255
void
256
fu_struct_uswid_set_hdrver(FuStructUswid *st, guint8 value)
257
1.72k
{
258
1.72k
    g_return_if_fail(st != NULL);
259
1.72k
    st->buf->data[16] = value;
260
1.72k
}
261
/**
262
 * fu_struct_uswid_set_hdrsz: (skip):
263
 **/
264
void
265
fu_struct_uswid_set_hdrsz(FuStructUswid *st, guint16 value)
266
1.72k
{
267
1.72k
    g_return_if_fail(st != NULL);
268
1.72k
    fu_memwrite_uint16(st->buf->data + 17, value, G_LITTLE_ENDIAN);
269
1.72k
}
270
/**
271
 * fu_struct_uswid_set_payloadsz: (skip):
272
 **/
273
void
274
fu_struct_uswid_set_payloadsz(FuStructUswid *st, guint32 value)
275
852
{
276
852
    g_return_if_fail(st != NULL);
277
852
    fu_memwrite_uint32(st->buf->data + 19, value, G_LITTLE_ENDIAN);
278
852
}
279
/**
280
 * fu_struct_uswid_set_flags: (skip):
281
 **/
282
void
283
fu_struct_uswid_set_flags(FuStructUswid *st, FuUswidHeaderFlags value)
284
852
{
285
852
    g_return_if_fail(st != NULL);
286
852
    st->buf->data[23] = value;
287
852
}
288
/**
289
 * fu_struct_uswid_set_compression: (skip):
290
 **/
291
void
292
fu_struct_uswid_set_compression(FuStructUswid *st, FuUswidPayloadCompression value)
293
1.72k
{
294
1.72k
    g_return_if_fail(st != NULL);
295
1.72k
    st->buf->data[24] = value;
296
1.72k
}
297
/**
298
 * fu_struct_uswid_set_format: (skip):
299
 **/
300
void
301
fu_struct_uswid_set_format(FuStructUswid *st, FuUswidPayloadFormat value)
302
1.72k
{
303
1.72k
    g_return_if_fail(st != NULL);
304
1.72k
    st->buf->data[25] = value;
305
1.72k
}
306
/**
307
 * fu_struct_uswid_new: (skip):
308
 **/
309
FuStructUswid *
310
fu_struct_uswid_new(void)
311
868
{
312
868
    FuStructUswid *st = fu_struct_uswid_new_internal();
313
868
    st->buf = g_byte_array_sized_new(26);
314
868
    fu_byte_array_set_size(st->buf, 26, 0x0);
315
868
    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");
316
868
    fu_struct_uswid_set_hdrver(st, 0x04);
317
868
    fu_struct_uswid_set_hdrsz(st, 26);
318
868
    fu_struct_uswid_set_compression(st, FU_USWID_PAYLOAD_COMPRESSION_NONE);
319
868
    fu_struct_uswid_set_format(st, FU_USWID_PAYLOAD_FORMAT_COSWID);
320
868
    return st;
321
868
}
322
/**
323
 * fu_struct_uswid_to_string: (skip):
324
 **/
325
static gchar *
326
fu_struct_uswid_to_string(const FuStructUswid *st)
327
0
{
328
0
    g_autoptr(GString) str = g_string_new("FuStructUswid:\n");
329
0
    g_return_val_if_fail(st != NULL, NULL);
330
0
    {
331
0
        g_autofree gchar *tmp = fwupd_guid_to_string(fu_struct_uswid_get_magic(st), FWUPD_GUID_FLAG_MIXED_ENDIAN);
332
0
        g_string_append_printf(str, "  magic: %s\n", tmp);
333
0
    }
334
0
    g_string_append_printf(str, "  hdrver: 0x%x\n",
335
0
                           (guint) fu_struct_uswid_get_hdrver(st));
336
0
    g_string_append_printf(str, "  hdrsz: 0x%x\n",
337
0
                           (guint) fu_struct_uswid_get_hdrsz(st));
338
0
    g_string_append_printf(str, "  payloadsz: 0x%x\n",
339
0
                           (guint) fu_struct_uswid_get_payloadsz(st));
340
0
    {
341
0
        const gchar *tmp = fu_uswid_header_flags_to_string(fu_struct_uswid_get_flags(st));
342
0
        if (tmp != NULL) {
343
0
            g_string_append_printf(str, "  flags: 0x%x [%s]\n", (guint) fu_struct_uswid_get_flags(st), tmp);
344
0
        } else {
345
0
            g_string_append_printf(str, "  flags: 0x%x\n", (guint) fu_struct_uswid_get_flags(st));
346
0
        }
347
0
    }
348
0
    {
349
0
        const gchar *tmp = fu_uswid_payload_compression_to_string(fu_struct_uswid_get_compression(st));
350
0
        if (tmp != NULL) {
351
0
            g_string_append_printf(str, "  compression: 0x%x [%s]\n", (guint) fu_struct_uswid_get_compression(st), tmp);
352
0
        } else {
353
0
            g_string_append_printf(str, "  compression: 0x%x\n", (guint) fu_struct_uswid_get_compression(st));
354
0
        }
355
0
    }
356
0
    {
357
0
        const gchar *tmp = fu_uswid_payload_format_to_string(fu_struct_uswid_get_format(st));
358
0
        if (tmp != NULL) {
359
0
            g_string_append_printf(str, "  format: 0x%x [%s]\n", (guint) fu_struct_uswid_get_format(st), tmp);
360
0
        } else {
361
0
            g_string_append_printf(str, "  format: 0x%x\n", (guint) fu_struct_uswid_get_format(st));
362
0
        }
363
0
    }
364
0
    if (str->len > 0)
365
0
        g_string_set_size(str, str->len - 1);
366
0
    return g_string_free(g_steal_pointer(&str), FALSE);
367
0
}
368
static gboolean
369
fu_struct_uswid_validate_internal(FuStructUswid *st, GError **error)
370
148k
{
371
148k
    g_return_val_if_fail(st != NULL, FALSE);
372
148k
    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) {
373
0
        g_set_error_literal(error,
374
0
                            FWUPD_ERROR,
375
0
                            FWUPD_ERROR_INVALID_DATA,
376
0
                            "constant FuStructUswid.magic was not valid");
377
0
        return FALSE;
378
0
    }
379
148k
    return TRUE;
380
148k
}
381
/**
382
 * fu_struct_uswid_validate_stream: (skip):
383
 **/
384
gboolean
385
fu_struct_uswid_validate_stream(FuInputStream *stream, gsize offset, GError **error)
386
74.7k
{
387
74.7k
    g_autoptr(FuStructUswid) st = fu_struct_uswid_new_internal();
388
74.7k
    g_return_val_if_fail(FWUPD_IS_INPUT_STREAM(stream), FALSE);
389
74.7k
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
390
74.7k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 26, NULL, error);
391
74.7k
    if (st->buf == NULL) {
392
0
        g_prefix_error(error, "FuStructUswid failed read of 0x%x: ", (guint) 26);
393
0
        return FALSE;
394
0
    }
395
74.7k
    if (st->buf->len != 26) {
396
368
        g_set_error(error,
397
368
                    FWUPD_ERROR,
398
368
                    FWUPD_ERROR_INVALID_DATA,
399
368
                    "FuStructUswid requested 0x%x and got 0x%x",
400
368
                    (guint) 26,
401
368
                    (guint) st->buf->len);
402
368
        return FALSE;
403
368
    }
404
74.4k
    return fu_struct_uswid_validate_internal(st, error);
405
74.7k
}
406
static gboolean
407
fu_struct_uswid_parse_internal(FuStructUswid *st, GError **error)
408
74.4k
{
409
74.4k
    if (g_log_get_debug_enabled()) {
410
0
        g_autofree gchar *str = fu_struct_uswid_to_string(st);
411
0
        g_debug("%s", str);
412
0
    }
413
74.4k
    if (!fu_struct_uswid_validate_internal(st, error))
414
0
        return FALSE;
415
74.4k
    return TRUE;
416
74.4k
}
417
/**
418
 * fu_struct_uswid_parse_stream: (skip):
419
 **/
420
FuStructUswid *
421
fu_struct_uswid_parse_stream(FuInputStream *stream, gsize offset, GError **error)
422
74.4k
{
423
74.4k
    g_autoptr(FuStructUswid) st = fu_struct_uswid_new_internal();
424
74.4k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 26, NULL, error);
425
74.4k
    if (st->buf == NULL) {
426
0
        g_prefix_error(error, "FuStructUswid failed read of 0x%x: ", (guint) 26);
427
0
        return NULL;
428
0
    }
429
74.4k
    if (st->buf->len != 26) {
430
0
        g_set_error(error,
431
0
                    FWUPD_ERROR,
432
0
                    FWUPD_ERROR_INVALID_DATA,
433
0
                    "FuStructUswid requested 0x%x and got 0x%x",
434
0
                    (guint) 26,
435
0
                    (guint) st->buf->len);
436
0
        return NULL;
437
0
    }
438
74.4k
    if (!fu_struct_uswid_parse_internal(st, error))
439
0
        return NULL;
440
74.4k
    return g_steal_pointer(&st);
441
74.4k
}