Coverage Report

Created: 2026-02-26 06:27

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