Coverage Report

Created: 2026-06-15 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/fu-usb-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-usb-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
24
25
/**
26
 * fu_usb_class_to_string:
27
 * @val: value, e.g. %FU_USB_CLASS_AUDIO
28
 *
29
 * Converts an enumerated value to a string.
30
 *
31
 * Returns: identifier string
32
 **/
33
const gchar *
34
fu_usb_class_to_string(FuUsbClass val)
35
0
{
36
0
    if (val == FU_USB_CLASS_INTERFACE_DESC)
37
0
        return "interface-desc";
38
0
    if (val == FU_USB_CLASS_AUDIO)
39
0
        return "audio";
40
0
    if (val == FU_USB_CLASS_COMMUNICATIONS)
41
0
        return "communications";
42
0
    if (val == FU_USB_CLASS_HID)
43
0
        return "hid";
44
0
    if (val == FU_USB_CLASS_PHYSICAL)
45
0
        return "physical";
46
0
    if (val == FU_USB_CLASS_IMAGE)
47
0
        return "image";
48
0
    if (val == FU_USB_CLASS_PRINTER)
49
0
        return "printer";
50
0
    if (val == FU_USB_CLASS_MASS_STORAGE)
51
0
        return "mass-storage";
52
0
    if (val == FU_USB_CLASS_HUB)
53
0
        return "hub";
54
0
    if (val == FU_USB_CLASS_CDC_DATA)
55
0
        return "cdc-data";
56
0
    if (val == FU_USB_CLASS_SMART_CARD)
57
0
        return "smart-card";
58
0
    if (val == FU_USB_CLASS_CONTENT_SECURITY)
59
0
        return "content-security";
60
0
    if (val == FU_USB_CLASS_VIDEO)
61
0
        return "video";
62
0
    if (val == FU_USB_CLASS_PERSONAL_HEALTHCARE)
63
0
        return "personal-healthcare";
64
0
    if (val == FU_USB_CLASS_AUDIO_VIDEO)
65
0
        return "audio-video";
66
0
    if (val == FU_USB_CLASS_BILLBOARD)
67
0
        return "billboard";
68
0
    if (val == FU_USB_CLASS_DIAGNOSTIC)
69
0
        return "diagnostic";
70
0
    if (val == FU_USB_CLASS_WIRELESS_CONTROLLER)
71
0
        return "wireless-controller";
72
0
    if (val == FU_USB_CLASS_MISCELLANEOUS)
73
0
        return "miscellaneous";
74
0
    if (val == FU_USB_CLASS_APPLICATION_SPECIFIC)
75
0
        return "application-specific";
76
0
    if (val == FU_USB_CLASS_VENDOR_SPECIFIC)
77
0
        return "vendor-specific";
78
0
    return NULL;
79
0
}
80
81
82
/**
83
 * fu_usb_descriptor_kind_to_string:
84
 * @val: value, e.g. %FU_USB_DESCRIPTOR_KIND_DEVICE
85
 *
86
 * Converts an enumerated value to a string.
87
 *
88
 * Returns: identifier string
89
 **/
90
const gchar *
91
fu_usb_descriptor_kind_to_string(FuUsbDescriptorKind val)
92
182k
{
93
182k
    if (val == FU_USB_DESCRIPTOR_KIND_INVALID)
94
1.10k
        return "invalid";
95
181k
    if (val == FU_USB_DESCRIPTOR_KIND_DEVICE)
96
144k
        return "device";
97
36.8k
    if (val == FU_USB_DESCRIPTOR_KIND_CONFIG)
98
1
        return "config";
99
36.8k
    if (val == FU_USB_DESCRIPTOR_KIND_STRING)
100
11.2k
        return "string";
101
25.5k
    if (val == FU_USB_DESCRIPTOR_KIND_INTERFACE)
102
1
        return "interface";
103
25.5k
    if (val == FU_USB_DESCRIPTOR_KIND_ENDPOINT)
104
1
        return "endpoint";
105
25.5k
    if (val == FU_USB_DESCRIPTOR_KIND_INTERFACE_ASSOCIATION)
106
243
        return "interface-association";
107
25.3k
    if (val == FU_USB_DESCRIPTOR_KIND_BOS)
108
244
        return "bos";
109
25.0k
    if (val == FU_USB_DESCRIPTOR_KIND_DEVICE_CAPABILITY)
110
919
        return "device-capability";
111
24.1k
    if (val == FU_USB_DESCRIPTOR_KIND_HID)
112
2
        return "hid";
113
24.1k
    if (val == FU_USB_DESCRIPTOR_KIND_REPORT)
114
692
        return "report";
115
23.4k
    if (val == FU_USB_DESCRIPTOR_KIND_PHYSICAL)
116
324
        return "physical";
117
23.1k
    if (val == FU_USB_DESCRIPTOR_KIND_HUB)
118
232
        return "hub";
119
22.9k
    if (val == FU_USB_DESCRIPTOR_KIND_SUPERSPEED_HUB)
120
578
        return "superspeed-hub";
121
22.3k
    if (val == FU_USB_DESCRIPTOR_KIND_SS_ENDPOINT_COMPANION)
122
276
        return "ss-endpoint-companion";
123
22.0k
    return NULL;
124
22.3k
}
125
/**
126
 * fu_usb_descriptor_kind_from_string:
127
 * @val: (nullable): a string, e.g. `device`
128
 *
129
 * Converts a string to an enumerated value.
130
 *
131
 * Returns: enumerated value
132
 **/
133
FuUsbDescriptorKind
134
fu_usb_descriptor_kind_from_string(const gchar *val)
135
0
{
136
0
    if (g_strcmp0(val, "invalid") == 0)
137
0
        return FU_USB_DESCRIPTOR_KIND_INVALID;
138
0
    if (g_strcmp0(val, "device") == 0)
139
0
        return FU_USB_DESCRIPTOR_KIND_DEVICE;
140
0
    if (g_strcmp0(val, "config") == 0)
141
0
        return FU_USB_DESCRIPTOR_KIND_CONFIG;
142
0
    if (g_strcmp0(val, "string") == 0)
143
0
        return FU_USB_DESCRIPTOR_KIND_STRING;
144
0
    if (g_strcmp0(val, "interface") == 0)
145
0
        return FU_USB_DESCRIPTOR_KIND_INTERFACE;
146
0
    if (g_strcmp0(val, "endpoint") == 0)
147
0
        return FU_USB_DESCRIPTOR_KIND_ENDPOINT;
148
0
    if (g_strcmp0(val, "interface-association") == 0)
149
0
        return FU_USB_DESCRIPTOR_KIND_INTERFACE_ASSOCIATION;
150
0
    if (g_strcmp0(val, "bos") == 0)
151
0
        return FU_USB_DESCRIPTOR_KIND_BOS;
152
0
    if (g_strcmp0(val, "device-capability") == 0)
153
0
        return FU_USB_DESCRIPTOR_KIND_DEVICE_CAPABILITY;
154
0
    if (g_strcmp0(val, "hid") == 0)
155
0
        return FU_USB_DESCRIPTOR_KIND_HID;
156
0
    if (g_strcmp0(val, "report") == 0)
157
0
        return FU_USB_DESCRIPTOR_KIND_REPORT;
158
0
    if (g_strcmp0(val, "physical") == 0)
159
0
        return FU_USB_DESCRIPTOR_KIND_PHYSICAL;
160
0
    if (g_strcmp0(val, "hub") == 0)
161
0
        return FU_USB_DESCRIPTOR_KIND_HUB;
162
0
    if (g_strcmp0(val, "superspeed-hub") == 0)
163
0
        return FU_USB_DESCRIPTOR_KIND_SUPERSPEED_HUB;
164
0
    if (g_strcmp0(val, "ss-endpoint-companion") == 0)
165
0
        return FU_USB_DESCRIPTOR_KIND_SS_ENDPOINT_COMPANION;
166
0
    return FU_USB_DESCRIPTOR_KIND_INVALID;
167
0
}
168
/**
169
 * fu_usb_base_hdr_ref: (skip):
170
 **/
171
FuUsbBaseHdr *
172
fu_usb_base_hdr_ref(FuUsbBaseHdr *st)
173
0
{
174
0
    g_return_val_if_fail(st != NULL, NULL);
175
0
    st->refcount++;
176
0
    return st;
177
0
}
178
/**
179
 * fu_usb_base_hdr_unref: (skip):
180
 **/
181
void
182
fu_usb_base_hdr_unref(FuUsbBaseHdr *st)
183
672k
{
184
672k
    g_return_if_fail(st != NULL);
185
672k
    if (st->refcount == 0) {
186
0
        g_critical("FuUsbBaseHdr refcount already zero");
187
0
        return;
188
0
    }
189
672k
    if (--st->refcount > 0)
190
0
        return;
191
672k
    if (st->buf != NULL)
192
672k
        g_byte_array_unref(st->buf);
193
672k
    g_free(st);
194
672k
}
195
/**
196
 * fu_usb_base_hdr_new_internal: (skip):
197
 **/
198
static FuUsbBaseHdr *
199
fu_usb_base_hdr_new_internal(void)
200
672k
{
201
672k
    FuUsbBaseHdr *st = g_new0(FuUsbBaseHdr, 1);
202
672k
    st->refcount = 1;
203
672k
    return st;
204
672k
}
205
206
/* getters */
207
/**
208
 * fu_usb_base_hdr_get_length: (skip):
209
 **/
210
guint8
211
fu_usb_base_hdr_get_length(const FuUsbBaseHdr *st)
212
1.12M
{
213
1.12M
    g_return_val_if_fail(st != NULL, 0x0);
214
1.12M
    return st->buf->data[0];
215
1.12M
}
216
/**
217
 * fu_usb_base_hdr_get_descriptor_type: (skip):
218
 **/
219
FuUsbDescriptorKind
220
fu_usb_base_hdr_get_descriptor_type(const FuUsbBaseHdr *st)
221
671k
{
222
671k
    g_return_val_if_fail(st != NULL, 0x0);
223
671k
    return st->buf->data[1];
224
671k
}
225
226
/* setters */
227
/**
228
 * fu_usb_base_hdr_to_string: (skip):
229
 **/
230
static gchar *
231
fu_usb_base_hdr_to_string(const FuUsbBaseHdr *st)
232
0
{
233
0
    g_autoptr(GString) str = g_string_new("FuUsbBaseHdr:\n");
234
0
    g_return_val_if_fail(st != NULL, NULL);
235
0
    g_string_append_printf(str, "  length: 0x%x\n",
236
0
                           (guint) fu_usb_base_hdr_get_length(st));
237
0
    {
238
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_base_hdr_get_descriptor_type(st));
239
0
        if (tmp != NULL) {
240
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_base_hdr_get_descriptor_type(st), tmp);
241
0
        } else {
242
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_base_hdr_get_descriptor_type(st));
243
0
        }
244
0
    }
245
0
    if (str->len > 0)
246
0
        g_string_set_size(str, str->len - 1);
247
0
    return g_string_free(g_steal_pointer(&str), FALSE);
248
0
}
249
static gboolean
250
fu_usb_base_hdr_parse_internal(FuUsbBaseHdr *st, GError **error)
251
672k
{
252
672k
    if (g_log_get_debug_enabled()) {
253
0
        g_autofree gchar *str = fu_usb_base_hdr_to_string(st);
254
0
        g_debug("%s", str);
255
0
    }
256
672k
    return TRUE;
257
672k
}
258
259
/**
260
 * fu_usb_base_hdr_parse: (skip):
261
 **/
262
FuUsbBaseHdr *
263
fu_usb_base_hdr_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
264
435k
{
265
435k
    g_autoptr(FuUsbBaseHdr) st = fu_usb_base_hdr_new_internal();
266
435k
    g_return_val_if_fail(buf != NULL, NULL);
267
435k
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
268
435k
    if (!fu_memchk_read(bufsz, offset, 2, error)) {
269
82
        g_prefix_error_literal(error, "invalid struct FuUsbBaseHdr: ");
270
82
        return NULL;
271
82
    }
272
435k
    st->buf = g_byte_array_new();
273
435k
    g_byte_array_append(st->buf, buf + offset, 2);
274
435k
    if (!fu_usb_base_hdr_parse_internal(st, error))
275
0
        return NULL;
276
435k
    return g_steal_pointer(&st);
277
435k
}
278
/**
279
 * fu_usb_base_hdr_parse_bytes: (skip):
280
 **/
281
FuUsbBaseHdr *
282
fu_usb_base_hdr_parse_bytes(GBytes *blob, gsize offset, GError **error)
283
435k
{
284
435k
    gsize bufsz = 0;
285
435k
    const guint8 *buf = fu_bytes_get_data_safe(blob, &bufsz, error);
286
435k
    if (buf == NULL)
287
0
        return NULL;
288
435k
    return fu_usb_base_hdr_parse(buf, bufsz, offset, error);
289
435k
}
290
/**
291
 * fu_usb_base_hdr_parse_stream: (skip):
292
 **/
293
FuUsbBaseHdr *
294
fu_usb_base_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
295
236k
{
296
236k
    g_autoptr(FuUsbBaseHdr) st = fu_usb_base_hdr_new_internal();
297
236k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 2, NULL, error);
298
236k
    if (st->buf == NULL) {
299
0
        g_prefix_error(error, "FuUsbBaseHdr failed read of 0x%x: ", (guint) 2);
300
0
        return NULL;
301
0
    }
302
236k
    if (st->buf->len != 2) {
303
176
        g_set_error(error,
304
176
                    FWUPD_ERROR,
305
176
                    FWUPD_ERROR_INVALID_DATA,
306
176
                    "FuUsbBaseHdr requested 0x%x and got 0x%x",
307
176
                    (guint) 2,
308
176
                    (guint) st->buf->len);
309
176
        return NULL;
310
176
    }
311
236k
    if (!fu_usb_base_hdr_parse_internal(st, error))
312
0
        return NULL;
313
236k
    return g_steal_pointer(&st);
314
236k
}
315
/**
316
 * fu_usb_device_hdr_ref: (skip):
317
 **/
318
FuUsbDeviceHdr *
319
fu_usb_device_hdr_ref(FuUsbDeviceHdr *st)
320
0
{
321
0
    g_return_val_if_fail(st != NULL, NULL);
322
0
    st->refcount++;
323
0
    return st;
324
0
}
325
/**
326
 * fu_usb_device_hdr_unref: (skip):
327
 **/
328
void
329
fu_usb_device_hdr_unref(FuUsbDeviceHdr *st)
330
773
{
331
773
    g_return_if_fail(st != NULL);
332
773
    if (st->refcount == 0) {
333
0
        g_critical("FuUsbDeviceHdr refcount already zero");
334
0
        return;
335
0
    }
336
773
    if (--st->refcount > 0)
337
0
        return;
338
773
    if (st->buf != NULL)
339
749
        g_byte_array_unref(st->buf);
340
773
    g_free(st);
341
773
}
342
/**
343
 * fu_usb_device_hdr_new_internal: (skip):
344
 **/
345
static FuUsbDeviceHdr *
346
fu_usb_device_hdr_new_internal(void)
347
773
{
348
773
    FuUsbDeviceHdr *st = g_new0(FuUsbDeviceHdr, 1);
349
773
    st->refcount = 1;
350
773
    return st;
351
773
}
352
353
/* getters */
354
/**
355
 * fu_usb_device_hdr_get_length: (skip):
356
 **/
357
guint8
358
fu_usb_device_hdr_get_length(const FuUsbDeviceHdr *st)
359
1.40k
{
360
1.40k
    g_return_val_if_fail(st != NULL, 0x0);
361
1.40k
    return st->buf->data[0];
362
1.40k
}
363
/**
364
 * fu_usb_device_hdr_get_descriptor_type: (skip):
365
 **/
366
static FuUsbDescriptorKind
367
fu_usb_device_hdr_get_descriptor_type(const FuUsbDeviceHdr *st)
368
839
{
369
839
    g_return_val_if_fail(st != NULL, 0x0);
370
839
    return st->buf->data[1];
371
839
}
372
/**
373
 * fu_usb_device_hdr_get_usb: (skip):
374
 **/
375
guint16
376
fu_usb_device_hdr_get_usb(const FuUsbDeviceHdr *st)
377
704
{
378
704
    g_return_val_if_fail(st != NULL, 0x0);
379
704
    return fu_memread_uint16(st->buf->data + 2, G_LITTLE_ENDIAN);
380
704
}
381
/**
382
 * fu_usb_device_hdr_get_device_class: (skip):
383
 **/
384
FuUsbClass
385
fu_usb_device_hdr_get_device_class(const FuUsbDeviceHdr *st)
386
704
{
387
704
    g_return_val_if_fail(st != NULL, 0x0);
388
704
    return st->buf->data[4];
389
704
}
390
/**
391
 * fu_usb_device_hdr_get_device_sub_class: (skip):
392
 **/
393
guint8
394
fu_usb_device_hdr_get_device_sub_class(const FuUsbDeviceHdr *st)
395
704
{
396
704
    g_return_val_if_fail(st != NULL, 0x0);
397
704
    return st->buf->data[5];
398
704
}
399
/**
400
 * fu_usb_device_hdr_get_device_protocol: (skip):
401
 **/
402
guint8
403
fu_usb_device_hdr_get_device_protocol(const FuUsbDeviceHdr *st)
404
704
{
405
704
    g_return_val_if_fail(st != NULL, 0x0);
406
704
    return st->buf->data[6];
407
704
}
408
/**
409
 * fu_usb_device_hdr_get_max_packet_size0: (skip):
410
 **/
411
guint8
412
fu_usb_device_hdr_get_max_packet_size0(const FuUsbDeviceHdr *st)
413
704
{
414
704
    g_return_val_if_fail(st != NULL, 0x0);
415
704
    return st->buf->data[7];
416
704
}
417
/**
418
 * fu_usb_device_hdr_get_vendor: (skip):
419
 **/
420
guint16
421
fu_usb_device_hdr_get_vendor(const FuUsbDeviceHdr *st)
422
704
{
423
704
    g_return_val_if_fail(st != NULL, 0x0);
424
704
    return fu_memread_uint16(st->buf->data + 8, G_LITTLE_ENDIAN);
425
704
}
426
/**
427
 * fu_usb_device_hdr_get_product: (skip):
428
 **/
429
guint16
430
fu_usb_device_hdr_get_product(const FuUsbDeviceHdr *st)
431
704
{
432
704
    g_return_val_if_fail(st != NULL, 0x0);
433
704
    return fu_memread_uint16(st->buf->data + 10, G_LITTLE_ENDIAN);
434
704
}
435
/**
436
 * fu_usb_device_hdr_get_device: (skip):
437
 **/
438
guint16
439
fu_usb_device_hdr_get_device(const FuUsbDeviceHdr *st)
440
704
{
441
704
    g_return_val_if_fail(st != NULL, 0x0);
442
704
    return fu_memread_uint16(st->buf->data + 12, G_LITTLE_ENDIAN);
443
704
}
444
/**
445
 * fu_usb_device_hdr_get_manufacturer_idx: (skip):
446
 **/
447
guint8
448
fu_usb_device_hdr_get_manufacturer_idx(const FuUsbDeviceHdr *st)
449
704
{
450
704
    g_return_val_if_fail(st != NULL, 0x0);
451
704
    return st->buf->data[14];
452
704
}
453
/**
454
 * fu_usb_device_hdr_get_product_idx: (skip):
455
 **/
456
guint8
457
fu_usb_device_hdr_get_product_idx(const FuUsbDeviceHdr *st)
458
704
{
459
704
    g_return_val_if_fail(st != NULL, 0x0);
460
704
    return st->buf->data[15];
461
704
}
462
/**
463
 * fu_usb_device_hdr_get_serial_number_idx: (skip):
464
 **/
465
guint8
466
fu_usb_device_hdr_get_serial_number_idx(const FuUsbDeviceHdr *st)
467
704
{
468
704
    g_return_val_if_fail(st != NULL, 0x0);
469
704
    return st->buf->data[16];
470
704
}
471
/**
472
 * fu_usb_device_hdr_get_num_configurations: (skip):
473
 **/
474
guint8
475
fu_usb_device_hdr_get_num_configurations(const FuUsbDeviceHdr *st)
476
704
{
477
704
    g_return_val_if_fail(st != NULL, 0x0);
478
704
    return st->buf->data[17];
479
704
}
480
481
/* setters */
482
/**
483
 * fu_usb_device_hdr_to_string: (skip):
484
 **/
485
static gchar *
486
fu_usb_device_hdr_to_string(const FuUsbDeviceHdr *st)
487
0
{
488
0
    g_autoptr(GString) str = g_string_new("FuUsbDeviceHdr:\n");
489
0
    g_return_val_if_fail(st != NULL, NULL);
490
0
    g_string_append_printf(str, "  length: 0x%x\n",
491
0
                           (guint) fu_usb_device_hdr_get_length(st));
492
0
    {
493
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_device_hdr_get_descriptor_type(st));
494
0
        if (tmp != NULL) {
495
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_device_hdr_get_descriptor_type(st), tmp);
496
0
        } else {
497
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_device_hdr_get_descriptor_type(st));
498
0
        }
499
0
    }
500
0
    g_string_append_printf(str, "  usb: 0x%x\n",
501
0
                           (guint) fu_usb_device_hdr_get_usb(st));
502
0
    {
503
0
        const gchar *tmp = fu_usb_class_to_string(fu_usb_device_hdr_get_device_class(st));
504
0
        if (tmp != NULL) {
505
0
            g_string_append_printf(str, "  device_class: 0x%x [%s]\n", (guint) fu_usb_device_hdr_get_device_class(st), tmp);
506
0
        } else {
507
0
            g_string_append_printf(str, "  device_class: 0x%x\n", (guint) fu_usb_device_hdr_get_device_class(st));
508
0
        }
509
0
    }
510
0
    g_string_append_printf(str, "  device_sub_class: 0x%x\n",
511
0
                           (guint) fu_usb_device_hdr_get_device_sub_class(st));
512
0
    g_string_append_printf(str, "  device_protocol: 0x%x\n",
513
0
                           (guint) fu_usb_device_hdr_get_device_protocol(st));
514
0
    g_string_append_printf(str, "  max_packet_size0: 0x%x\n",
515
0
                           (guint) fu_usb_device_hdr_get_max_packet_size0(st));
516
0
    g_string_append_printf(str, "  vendor: 0x%x\n",
517
0
                           (guint) fu_usb_device_hdr_get_vendor(st));
518
0
    g_string_append_printf(str, "  product: 0x%x\n",
519
0
                           (guint) fu_usb_device_hdr_get_product(st));
520
0
    g_string_append_printf(str, "  device: 0x%x\n",
521
0
                           (guint) fu_usb_device_hdr_get_device(st));
522
0
    g_string_append_printf(str, "  manufacturer_idx: 0x%x\n",
523
0
                           (guint) fu_usb_device_hdr_get_manufacturer_idx(st));
524
0
    g_string_append_printf(str, "  product_idx: 0x%x\n",
525
0
                           (guint) fu_usb_device_hdr_get_product_idx(st));
526
0
    g_string_append_printf(str, "  serial_number_idx: 0x%x\n",
527
0
                           (guint) fu_usb_device_hdr_get_serial_number_idx(st));
528
0
    g_string_append_printf(str, "  num_configurations: 0x%x\n",
529
0
                           (guint) fu_usb_device_hdr_get_num_configurations(st));
530
0
    if (str->len > 0)
531
0
        g_string_set_size(str, str->len - 1);
532
0
    return g_string_free(g_steal_pointer(&str), FALSE);
533
0
}
534
static gboolean
535
fu_usb_device_hdr_validate_internal(FuUsbDeviceHdr *st, GError **error)
536
749
{
537
749
    g_return_val_if_fail(st != NULL, FALSE);
538
749
    if (fu_usb_device_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_DEVICE) {
539
45
        g_set_error(error,
540
45
                    FWUPD_ERROR,
541
45
                    FWUPD_ERROR_INVALID_DATA,
542
45
                    "constant FuUsbDeviceHdr.descriptor_type was not valid, "
543
45
                    "expected 'FU_USB_DESCRIPTOR_KIND_DEVICE' and got 0x%x '%s'",
544
45
                    fu_usb_device_hdr_get_descriptor_type(st),
545
45
                    fu_usb_descriptor_kind_to_string(fu_usb_device_hdr_get_descriptor_type(st)));
546
45
        return FALSE;
547
45
    }
548
704
    return TRUE;
549
749
}
550
static gboolean
551
fu_usb_device_hdr_parse_internal(FuUsbDeviceHdr *st, GError **error)
552
749
{
553
749
    if (g_log_get_debug_enabled()) {
554
0
        g_autofree gchar *str = fu_usb_device_hdr_to_string(st);
555
0
        g_debug("%s", str);
556
0
    }
557
749
    if (!fu_usb_device_hdr_validate_internal(st, error))
558
45
        return FALSE;
559
704
    return TRUE;
560
749
}
561
562
/**
563
 * fu_usb_device_hdr_parse: (skip):
564
 **/
565
static FuUsbDeviceHdr *
566
fu_usb_device_hdr_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
567
773
{
568
773
    g_autoptr(FuUsbDeviceHdr) st = fu_usb_device_hdr_new_internal();
569
773
    g_return_val_if_fail(buf != NULL, NULL);
570
773
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
571
773
    if (!fu_memchk_read(bufsz, offset, 18, error)) {
572
24
        g_prefix_error_literal(error, "invalid struct FuUsbDeviceHdr: ");
573
24
        return NULL;
574
24
    }
575
749
    st->buf = g_byte_array_new();
576
749
    g_byte_array_append(st->buf, buf + offset, 18);
577
749
    if (!fu_usb_device_hdr_parse_internal(st, error))
578
45
        return NULL;
579
704
    return g_steal_pointer(&st);
580
749
}
581
/**
582
 * fu_usb_device_hdr_parse_bytes: (skip):
583
 **/
584
FuUsbDeviceHdr *
585
fu_usb_device_hdr_parse_bytes(GBytes *blob, gsize offset, GError **error)
586
773
{
587
773
    gsize bufsz = 0;
588
773
    const guint8 *buf = fu_bytes_get_data_safe(blob, &bufsz, error);
589
773
    if (buf == NULL)
590
0
        return NULL;
591
773
    return fu_usb_device_hdr_parse(buf, bufsz, offset, error);
592
773
}
593
/**
594
 * fu_usb_descriptor_hdr_ref: (skip):
595
 **/
596
FuUsbDescriptorHdr *
597
fu_usb_descriptor_hdr_ref(FuUsbDescriptorHdr *st)
598
0
{
599
0
    g_return_val_if_fail(st != NULL, NULL);
600
0
    st->refcount++;
601
0
    return st;
602
0
}
603
/**
604
 * fu_usb_descriptor_hdr_unref: (skip):
605
 **/
606
void
607
fu_usb_descriptor_hdr_unref(FuUsbDescriptorHdr *st)
608
127k
{
609
127k
    g_return_if_fail(st != NULL);
610
127k
    if (st->refcount == 0) {
611
0
        g_critical("FuUsbDescriptorHdr refcount already zero");
612
0
        return;
613
0
    }
614
127k
    if (--st->refcount > 0)
615
0
        return;
616
127k
    if (st->buf != NULL)
617
127k
        g_byte_array_unref(st->buf);
618
127k
    g_free(st);
619
127k
}
620
/**
621
 * fu_usb_descriptor_hdr_new_internal: (skip):
622
 **/
623
static FuUsbDescriptorHdr *
624
fu_usb_descriptor_hdr_new_internal(void)
625
127k
{
626
127k
    FuUsbDescriptorHdr *st = g_new0(FuUsbDescriptorHdr, 1);
627
127k
    st->refcount = 1;
628
127k
    return st;
629
127k
}
630
631
/* getters */
632
/**
633
 * fu_usb_descriptor_hdr_get_length: (skip):
634
 **/
635
guint8
636
fu_usb_descriptor_hdr_get_length(const FuUsbDescriptorHdr *st)
637
0
{
638
0
    g_return_val_if_fail(st != NULL, 0x0);
639
0
    return st->buf->data[0];
640
0
}
641
/**
642
 * fu_usb_descriptor_hdr_get_descriptor_type: (skip):
643
 **/
644
static FuUsbDescriptorKind
645
fu_usb_descriptor_hdr_get_descriptor_type(const FuUsbDescriptorHdr *st)
646
127k
{
647
127k
    g_return_val_if_fail(st != NULL, 0x0);
648
127k
    return st->buf->data[1];
649
127k
}
650
/**
651
 * fu_usb_descriptor_hdr_get_total_length: (skip):
652
 **/
653
guint16
654
fu_usb_descriptor_hdr_get_total_length(const FuUsbDescriptorHdr *st)
655
0
{
656
0
    g_return_val_if_fail(st != NULL, 0x0);
657
0
    return fu_memread_uint16(st->buf->data + 2, G_LITTLE_ENDIAN);
658
0
}
659
/**
660
 * fu_usb_descriptor_hdr_get_num_interfaces: (skip):
661
 **/
662
guint8
663
fu_usb_descriptor_hdr_get_num_interfaces(const FuUsbDescriptorHdr *st)
664
0
{
665
0
    g_return_val_if_fail(st != NULL, 0x0);
666
0
    return st->buf->data[4];
667
0
}
668
/**
669
 * fu_usb_descriptor_hdr_get_configuration_value: (skip):
670
 **/
671
guint8
672
fu_usb_descriptor_hdr_get_configuration_value(const FuUsbDescriptorHdr *st)
673
127k
{
674
127k
    g_return_val_if_fail(st != NULL, 0x0);
675
127k
    return st->buf->data[5];
676
127k
}
677
/**
678
 * fu_usb_descriptor_hdr_get_configuration: (skip):
679
 **/
680
guint8
681
fu_usb_descriptor_hdr_get_configuration(const FuUsbDescriptorHdr *st)
682
127k
{
683
127k
    g_return_val_if_fail(st != NULL, 0x0);
684
127k
    return st->buf->data[6];
685
127k
}
686
/**
687
 * fu_usb_descriptor_hdr_get_attributes: (skip):
688
 **/
689
guint8
690
fu_usb_descriptor_hdr_get_attributes(const FuUsbDescriptorHdr *st)
691
0
{
692
0
    g_return_val_if_fail(st != NULL, 0x0);
693
0
    return st->buf->data[7];
694
0
}
695
/**
696
 * fu_usb_descriptor_hdr_get_max_power: (skip):
697
 **/
698
guint8
699
fu_usb_descriptor_hdr_get_max_power(const FuUsbDescriptorHdr *st)
700
0
{
701
0
    g_return_val_if_fail(st != NULL, 0x0);
702
0
    return st->buf->data[8];
703
0
}
704
705
/* setters */
706
/**
707
 * fu_usb_descriptor_hdr_to_string: (skip):
708
 **/
709
static gchar *
710
fu_usb_descriptor_hdr_to_string(const FuUsbDescriptorHdr *st)
711
0
{
712
0
    g_autoptr(GString) str = g_string_new("FuUsbDescriptorHdr:\n");
713
0
    g_return_val_if_fail(st != NULL, NULL);
714
0
    g_string_append_printf(str, "  length: 0x%x\n",
715
0
                           (guint) fu_usb_descriptor_hdr_get_length(st));
716
0
    {
717
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_descriptor_hdr_get_descriptor_type(st));
718
0
        if (tmp != NULL) {
719
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_descriptor_hdr_get_descriptor_type(st), tmp);
720
0
        } else {
721
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_descriptor_hdr_get_descriptor_type(st));
722
0
        }
723
0
    }
724
0
    g_string_append_printf(str, "  total_length: 0x%x\n",
725
0
                           (guint) fu_usb_descriptor_hdr_get_total_length(st));
726
0
    g_string_append_printf(str, "  num_interfaces: 0x%x\n",
727
0
                           (guint) fu_usb_descriptor_hdr_get_num_interfaces(st));
728
0
    g_string_append_printf(str, "  configuration_value: 0x%x\n",
729
0
                           (guint) fu_usb_descriptor_hdr_get_configuration_value(st));
730
0
    g_string_append_printf(str, "  configuration: 0x%x\n",
731
0
                           (guint) fu_usb_descriptor_hdr_get_configuration(st));
732
0
    g_string_append_printf(str, "  attributes: 0x%x\n",
733
0
                           (guint) fu_usb_descriptor_hdr_get_attributes(st));
734
0
    g_string_append_printf(str, "  max_power: 0x%x\n",
735
0
                           (guint) fu_usb_descriptor_hdr_get_max_power(st));
736
0
    if (str->len > 0)
737
0
        g_string_set_size(str, str->len - 1);
738
0
    return g_string_free(g_steal_pointer(&str), FALSE);
739
0
}
740
static gboolean
741
fu_usb_descriptor_hdr_validate_internal(FuUsbDescriptorHdr *st, GError **error)
742
127k
{
743
127k
    g_return_val_if_fail(st != NULL, FALSE);
744
127k
    if (fu_usb_descriptor_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_CONFIG) {
745
0
        g_set_error(error,
746
0
                    FWUPD_ERROR,
747
0
                    FWUPD_ERROR_INVALID_DATA,
748
0
                    "constant FuUsbDescriptorHdr.descriptor_type was not valid, "
749
0
                    "expected 'FU_USB_DESCRIPTOR_KIND_CONFIG' and got 0x%x '%s'",
750
0
                    fu_usb_descriptor_hdr_get_descriptor_type(st),
751
0
                    fu_usb_descriptor_kind_to_string(fu_usb_descriptor_hdr_get_descriptor_type(st)));
752
0
        return FALSE;
753
0
    }
754
127k
    return TRUE;
755
127k
}
756
static gboolean
757
fu_usb_descriptor_hdr_parse_internal(FuUsbDescriptorHdr *st, GError **error)
758
127k
{
759
127k
    if (g_log_get_debug_enabled()) {
760
0
        g_autofree gchar *str = fu_usb_descriptor_hdr_to_string(st);
761
0
        g_debug("%s", str);
762
0
    }
763
127k
    if (!fu_usb_descriptor_hdr_validate_internal(st, error))
764
0
        return FALSE;
765
127k
    return TRUE;
766
127k
}
767
/**
768
 * fu_usb_descriptor_hdr_parse_stream: (skip):
769
 **/
770
FuUsbDescriptorHdr *
771
fu_usb_descriptor_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
772
127k
{
773
127k
    g_autoptr(FuUsbDescriptorHdr) st = fu_usb_descriptor_hdr_new_internal();
774
127k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 9, NULL, error);
775
127k
    if (st->buf == NULL) {
776
0
        g_prefix_error(error, "FuUsbDescriptorHdr failed read of 0x%x: ", (guint) 9);
777
0
        return NULL;
778
0
    }
779
127k
    if (st->buf->len != 9) {
780
31
        g_set_error(error,
781
31
                    FWUPD_ERROR,
782
31
                    FWUPD_ERROR_INVALID_DATA,
783
31
                    "FuUsbDescriptorHdr requested 0x%x and got 0x%x",
784
31
                    (guint) 9,
785
31
                    (guint) st->buf->len);
786
31
        return NULL;
787
31
    }
788
127k
    if (!fu_usb_descriptor_hdr_parse_internal(st, error))
789
0
        return NULL;
790
127k
    return g_steal_pointer(&st);
791
127k
}
792
/**
793
 * fu_usb_hid_descriptor_hdr_ref: (skip):
794
 **/
795
FuUsbHidDescriptorHdr *
796
fu_usb_hid_descriptor_hdr_ref(FuUsbHidDescriptorHdr *st)
797
0
{
798
0
    g_return_val_if_fail(st != NULL, NULL);
799
0
    st->refcount++;
800
0
    return st;
801
0
}
802
/**
803
 * fu_usb_hid_descriptor_hdr_unref: (skip):
804
 **/
805
void
806
fu_usb_hid_descriptor_hdr_unref(FuUsbHidDescriptorHdr *st)
807
13.0k
{
808
13.0k
    g_return_if_fail(st != NULL);
809
13.0k
    if (st->refcount == 0) {
810
0
        g_critical("FuUsbHidDescriptorHdr refcount already zero");
811
0
        return;
812
0
    }
813
13.0k
    if (--st->refcount > 0)
814
0
        return;
815
13.0k
    if (st->buf != NULL)
816
13.0k
        g_byte_array_unref(st->buf);
817
13.0k
    g_free(st);
818
13.0k
}
819
/**
820
 * fu_usb_hid_descriptor_hdr_new_internal: (skip):
821
 **/
822
static FuUsbHidDescriptorHdr *
823
fu_usb_hid_descriptor_hdr_new_internal(void)
824
13.0k
{
825
13.0k
    FuUsbHidDescriptorHdr *st = g_new0(FuUsbHidDescriptorHdr, 1);
826
13.0k
    st->refcount = 1;
827
13.0k
    return st;
828
13.0k
}
829
830
/* getters */
831
/**
832
 * fu_usb_hid_descriptor_hdr_get_length: (skip):
833
 **/
834
guint8
835
fu_usb_hid_descriptor_hdr_get_length(const FuUsbHidDescriptorHdr *st)
836
0
{
837
0
    g_return_val_if_fail(st != NULL, 0x0);
838
0
    return st->buf->data[0];
839
0
}
840
/**
841
 * fu_usb_hid_descriptor_hdr_get_descriptor_type: (skip):
842
 **/
843
static FuUsbDescriptorKind
844
fu_usb_hid_descriptor_hdr_get_descriptor_type(const FuUsbHidDescriptorHdr *st)
845
13.0k
{
846
13.0k
    g_return_val_if_fail(st != NULL, 0x0);
847
13.0k
    return st->buf->data[1];
848
13.0k
}
849
/**
850
 * fu_usb_hid_descriptor_hdr_get_hid: (skip):
851
 **/
852
guint16
853
fu_usb_hid_descriptor_hdr_get_hid(const FuUsbHidDescriptorHdr *st)
854
0
{
855
0
    g_return_val_if_fail(st != NULL, 0x0);
856
0
    return fu_memread_uint16(st->buf->data + 2, G_LITTLE_ENDIAN);
857
0
}
858
/**
859
 * fu_usb_hid_descriptor_hdr_get_country_code: (skip):
860
 **/
861
guint8
862
fu_usb_hid_descriptor_hdr_get_country_code(const FuUsbHidDescriptorHdr *st)
863
0
{
864
0
    g_return_val_if_fail(st != NULL, 0x0);
865
0
    return st->buf->data[4];
866
0
}
867
/**
868
 * fu_usb_hid_descriptor_hdr_get_num_descriptors: (skip):
869
 **/
870
guint8
871
fu_usb_hid_descriptor_hdr_get_num_descriptors(const FuUsbHidDescriptorHdr *st)
872
0
{
873
0
    g_return_val_if_fail(st != NULL, 0x0);
874
0
    return st->buf->data[5];
875
0
}
876
/**
877
 * fu_usb_hid_descriptor_hdr_get_class_descriptor_type: (skip):
878
 **/
879
guint8
880
fu_usb_hid_descriptor_hdr_get_class_descriptor_type(const FuUsbHidDescriptorHdr *st)
881
0
{
882
0
    g_return_val_if_fail(st != NULL, 0x0);
883
0
    return st->buf->data[6];
884
0
}
885
/**
886
 * fu_usb_hid_descriptor_hdr_get_class_descriptor_length: (skip):
887
 **/
888
guint16
889
fu_usb_hid_descriptor_hdr_get_class_descriptor_length(const FuUsbHidDescriptorHdr *st)
890
13.0k
{
891
13.0k
    g_return_val_if_fail(st != NULL, 0x0);
892
13.0k
    return fu_memread_uint16(st->buf->data + 7, G_LITTLE_ENDIAN);
893
13.0k
}
894
895
/* setters */
896
/**
897
 * fu_usb_hid_descriptor_hdr_to_string: (skip):
898
 **/
899
static gchar *
900
fu_usb_hid_descriptor_hdr_to_string(const FuUsbHidDescriptorHdr *st)
901
0
{
902
0
    g_autoptr(GString) str = g_string_new("FuUsbHidDescriptorHdr:\n");
903
0
    g_return_val_if_fail(st != NULL, NULL);
904
0
    g_string_append_printf(str, "  length: 0x%x\n",
905
0
                           (guint) fu_usb_hid_descriptor_hdr_get_length(st));
906
0
    {
907
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_hid_descriptor_hdr_get_descriptor_type(st));
908
0
        if (tmp != NULL) {
909
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_hid_descriptor_hdr_get_descriptor_type(st), tmp);
910
0
        } else {
911
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_hid_descriptor_hdr_get_descriptor_type(st));
912
0
        }
913
0
    }
914
0
    g_string_append_printf(str, "  hid: 0x%x\n",
915
0
                           (guint) fu_usb_hid_descriptor_hdr_get_hid(st));
916
0
    g_string_append_printf(str, "  country_code: 0x%x\n",
917
0
                           (guint) fu_usb_hid_descriptor_hdr_get_country_code(st));
918
0
    g_string_append_printf(str, "  num_descriptors: 0x%x\n",
919
0
                           (guint) fu_usb_hid_descriptor_hdr_get_num_descriptors(st));
920
0
    g_string_append_printf(str, "  class_descriptor_type: 0x%x\n",
921
0
                           (guint) fu_usb_hid_descriptor_hdr_get_class_descriptor_type(st));
922
0
    g_string_append_printf(str, "  class_descriptor_length: 0x%x\n",
923
0
                           (guint) fu_usb_hid_descriptor_hdr_get_class_descriptor_length(st));
924
0
    if (str->len > 0)
925
0
        g_string_set_size(str, str->len - 1);
926
0
    return g_string_free(g_steal_pointer(&str), FALSE);
927
0
}
928
static gboolean
929
fu_usb_hid_descriptor_hdr_validate_internal(FuUsbHidDescriptorHdr *st, GError **error)
930
13.0k
{
931
13.0k
    g_return_val_if_fail(st != NULL, FALSE);
932
13.0k
    if (fu_usb_hid_descriptor_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_HID) {
933
0
        g_set_error(error,
934
0
                    FWUPD_ERROR,
935
0
                    FWUPD_ERROR_INVALID_DATA,
936
0
                    "constant FuUsbHidDescriptorHdr.descriptor_type was not valid, "
937
0
                    "expected 'FU_USB_DESCRIPTOR_KIND_HID' and got 0x%x '%s'",
938
0
                    fu_usb_hid_descriptor_hdr_get_descriptor_type(st),
939
0
                    fu_usb_descriptor_kind_to_string(fu_usb_hid_descriptor_hdr_get_descriptor_type(st)));
940
0
        return FALSE;
941
0
    }
942
13.0k
    return TRUE;
943
13.0k
}
944
static gboolean
945
fu_usb_hid_descriptor_hdr_parse_internal(FuUsbHidDescriptorHdr *st, GError **error)
946
13.0k
{
947
13.0k
    if (g_log_get_debug_enabled()) {
948
0
        g_autofree gchar *str = fu_usb_hid_descriptor_hdr_to_string(st);
949
0
        g_debug("%s", str);
950
0
    }
951
13.0k
    if (!fu_usb_hid_descriptor_hdr_validate_internal(st, error))
952
0
        return FALSE;
953
13.0k
    return TRUE;
954
13.0k
}
955
/**
956
 * fu_usb_hid_descriptor_hdr_parse_stream: (skip):
957
 **/
958
FuUsbHidDescriptorHdr *
959
fu_usb_hid_descriptor_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
960
13.0k
{
961
13.0k
    g_autoptr(FuUsbHidDescriptorHdr) st = fu_usb_hid_descriptor_hdr_new_internal();
962
13.0k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 9, NULL, error);
963
13.0k
    if (st->buf == NULL) {
964
0
        g_prefix_error(error, "FuUsbHidDescriptorHdr failed read of 0x%x: ", (guint) 9);
965
0
        return NULL;
966
0
    }
967
13.0k
    if (st->buf->len != 9) {
968
10
        g_set_error(error,
969
10
                    FWUPD_ERROR,
970
10
                    FWUPD_ERROR_INVALID_DATA,
971
10
                    "FuUsbHidDescriptorHdr requested 0x%x and got 0x%x",
972
10
                    (guint) 9,
973
10
                    (guint) st->buf->len);
974
10
        return NULL;
975
10
    }
976
13.0k
    if (!fu_usb_hid_descriptor_hdr_parse_internal(st, error))
977
0
        return NULL;
978
13.0k
    return g_steal_pointer(&st);
979
13.0k
}
980
/**
981
 * fu_usb_dfu_descriptor_hdr_ref: (skip):
982
 **/
983
FuUsbDfuDescriptorHdr *
984
fu_usb_dfu_descriptor_hdr_ref(FuUsbDfuDescriptorHdr *st)
985
0
{
986
0
    g_return_val_if_fail(st != NULL, NULL);
987
0
    st->refcount++;
988
0
    return st;
989
0
}
990
/**
991
 * fu_usb_dfu_descriptor_hdr_unref: (skip):
992
 **/
993
void
994
fu_usb_dfu_descriptor_hdr_unref(FuUsbDfuDescriptorHdr *st)
995
0
{
996
0
    g_return_if_fail(st != NULL);
997
0
    if (st->refcount == 0) {
998
0
        g_critical("FuUsbDfuDescriptorHdr refcount already zero");
999
0
        return;
1000
0
    }
1001
0
    if (--st->refcount > 0)
1002
0
        return;
1003
0
    if (st->buf != NULL)
1004
0
        g_byte_array_unref(st->buf);
1005
0
    g_free(st);
1006
0
}
1007
/**
1008
 * fu_usb_dfu_descriptor_hdr_new_internal: (skip):
1009
 **/
1010
static FuUsbDfuDescriptorHdr *
1011
fu_usb_dfu_descriptor_hdr_new_internal(void)
1012
0
{
1013
0
    FuUsbDfuDescriptorHdr *st = g_new0(FuUsbDfuDescriptorHdr, 1);
1014
0
    st->refcount = 1;
1015
0
    return st;
1016
0
}
1017
1018
/* getters */
1019
/**
1020
 * fu_usb_dfu_descriptor_hdr_get_length: (skip):
1021
 **/
1022
guint8
1023
fu_usb_dfu_descriptor_hdr_get_length(const FuUsbDfuDescriptorHdr *st)
1024
0
{
1025
0
    g_return_val_if_fail(st != NULL, 0x0);
1026
0
    return st->buf->data[0];
1027
0
}
1028
/**
1029
 * fu_usb_dfu_descriptor_hdr_get_descriptor_type: (skip):
1030
 **/
1031
static FuUsbDescriptorKind
1032
fu_usb_dfu_descriptor_hdr_get_descriptor_type(const FuUsbDfuDescriptorHdr *st)
1033
0
{
1034
0
    g_return_val_if_fail(st != NULL, 0x0);
1035
0
    return st->buf->data[1];
1036
0
}
1037
/**
1038
 * fu_usb_dfu_descriptor_hdr_get_attributes: (skip):
1039
 **/
1040
guint8
1041
fu_usb_dfu_descriptor_hdr_get_attributes(const FuUsbDfuDescriptorHdr *st)
1042
0
{
1043
0
    g_return_val_if_fail(st != NULL, 0x0);
1044
0
    return st->buf->data[2];
1045
0
}
1046
/**
1047
 * fu_usb_dfu_descriptor_hdr_get_detach_timeout: (skip):
1048
 **/
1049
guint16
1050
fu_usb_dfu_descriptor_hdr_get_detach_timeout(const FuUsbDfuDescriptorHdr *st)
1051
0
{
1052
0
    g_return_val_if_fail(st != NULL, 0x0);
1053
0
    return fu_memread_uint16(st->buf->data + 3, G_LITTLE_ENDIAN);
1054
0
}
1055
/**
1056
 * fu_usb_dfu_descriptor_hdr_get_transfer_size: (skip):
1057
 **/
1058
guint16
1059
fu_usb_dfu_descriptor_hdr_get_transfer_size(const FuUsbDfuDescriptorHdr *st)
1060
0
{
1061
0
    g_return_val_if_fail(st != NULL, 0x0);
1062
0
    return fu_memread_uint16(st->buf->data + 5, G_LITTLE_ENDIAN);
1063
0
}
1064
/**
1065
 * fu_usb_dfu_descriptor_hdr_get_dfu_version: (skip):
1066
 **/
1067
guint16
1068
fu_usb_dfu_descriptor_hdr_get_dfu_version(const FuUsbDfuDescriptorHdr *st)
1069
0
{
1070
0
    g_return_val_if_fail(st != NULL, 0x0);
1071
0
    return fu_memread_uint16(st->buf->data + 7, G_LITTLE_ENDIAN);
1072
0
}
1073
1074
/* setters */
1075
/**
1076
 * fu_usb_dfu_descriptor_hdr_to_string: (skip):
1077
 **/
1078
static gchar *
1079
fu_usb_dfu_descriptor_hdr_to_string(const FuUsbDfuDescriptorHdr *st)
1080
0
{
1081
0
    g_autoptr(GString) str = g_string_new("FuUsbDfuDescriptorHdr:\n");
1082
0
    g_return_val_if_fail(st != NULL, NULL);
1083
0
    g_string_append_printf(str, "  length: 0x%x\n",
1084
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_length(st));
1085
0
    {
1086
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_dfu_descriptor_hdr_get_descriptor_type(st));
1087
0
        if (tmp != NULL) {
1088
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_dfu_descriptor_hdr_get_descriptor_type(st), tmp);
1089
0
        } else {
1090
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_dfu_descriptor_hdr_get_descriptor_type(st));
1091
0
        }
1092
0
    }
1093
0
    g_string_append_printf(str, "  attributes: 0x%x\n",
1094
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_attributes(st));
1095
0
    g_string_append_printf(str, "  detach_timeout: 0x%x\n",
1096
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_detach_timeout(st));
1097
0
    g_string_append_printf(str, "  transfer_size: 0x%x\n",
1098
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_transfer_size(st));
1099
0
    g_string_append_printf(str, "  dfu_version: 0x%x\n",
1100
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_dfu_version(st));
1101
0
    if (str->len > 0)
1102
0
        g_string_set_size(str, str->len - 1);
1103
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1104
0
}
1105
static gboolean
1106
fu_usb_dfu_descriptor_hdr_validate_internal(FuUsbDfuDescriptorHdr *st, GError **error)
1107
0
{
1108
0
    g_return_val_if_fail(st != NULL, FALSE);
1109
0
    if (fu_usb_dfu_descriptor_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_HID) {
1110
0
        g_set_error(error,
1111
0
                    FWUPD_ERROR,
1112
0
                    FWUPD_ERROR_INVALID_DATA,
1113
0
                    "constant FuUsbDfuDescriptorHdr.descriptor_type was not valid, "
1114
0
                    "expected 'FU_USB_DESCRIPTOR_KIND_HID' and got 0x%x '%s'",
1115
0
                    fu_usb_dfu_descriptor_hdr_get_descriptor_type(st),
1116
0
                    fu_usb_descriptor_kind_to_string(fu_usb_dfu_descriptor_hdr_get_descriptor_type(st)));
1117
0
        return FALSE;
1118
0
    }
1119
0
    return TRUE;
1120
0
}
1121
static gboolean
1122
fu_usb_dfu_descriptor_hdr_parse_internal(FuUsbDfuDescriptorHdr *st, GError **error)
1123
0
{
1124
0
    if (g_log_get_debug_enabled()) {
1125
0
        g_autofree gchar *str = fu_usb_dfu_descriptor_hdr_to_string(st);
1126
0
        g_debug("%s", str);
1127
0
    }
1128
0
    if (!fu_usb_dfu_descriptor_hdr_validate_internal(st, error))
1129
0
        return FALSE;
1130
0
    return TRUE;
1131
0
}
1132
1133
/**
1134
 * fu_usb_dfu_descriptor_hdr_parse: (skip):
1135
 **/
1136
static FuUsbDfuDescriptorHdr *
1137
fu_usb_dfu_descriptor_hdr_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
1138
0
{
1139
0
    g_autoptr(FuUsbDfuDescriptorHdr) st = fu_usb_dfu_descriptor_hdr_new_internal();
1140
0
    g_return_val_if_fail(buf != NULL, NULL);
1141
0
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
1142
0
    if (!fu_memchk_read(bufsz, offset, 9, error)) {
1143
0
        g_prefix_error_literal(error, "invalid struct FuUsbDfuDescriptorHdr: ");
1144
0
        return NULL;
1145
0
    }
1146
0
    st->buf = g_byte_array_new();
1147
0
    g_byte_array_append(st->buf, buf + offset, 9);
1148
0
    if (!fu_usb_dfu_descriptor_hdr_parse_internal(st, error))
1149
0
        return NULL;
1150
0
    return g_steal_pointer(&st);
1151
0
}
1152
/**
1153
 * fu_usb_dfu_descriptor_hdr_parse_bytes: (skip):
1154
 **/
1155
FuUsbDfuDescriptorHdr *
1156
fu_usb_dfu_descriptor_hdr_parse_bytes(GBytes *blob, gsize offset, GError **error)
1157
0
{
1158
0
    gsize bufsz = 0;
1159
0
    const guint8 *buf = fu_bytes_get_data_safe(blob, &bufsz, error);
1160
0
    if (buf == NULL)
1161
0
        return NULL;
1162
0
    return fu_usb_dfu_descriptor_hdr_parse(buf, bufsz, offset, error);
1163
0
}
1164
/**
1165
 * fu_usb_interface_hdr_ref: (skip):
1166
 **/
1167
FuUsbInterfaceHdr *
1168
fu_usb_interface_hdr_ref(FuUsbInterfaceHdr *st)
1169
0
{
1170
0
    g_return_val_if_fail(st != NULL, NULL);
1171
0
    st->refcount++;
1172
0
    return st;
1173
0
}
1174
/**
1175
 * fu_usb_interface_hdr_unref: (skip):
1176
 **/
1177
void
1178
fu_usb_interface_hdr_unref(FuUsbInterfaceHdr *st)
1179
86.4k
{
1180
86.4k
    g_return_if_fail(st != NULL);
1181
86.4k
    if (st->refcount == 0) {
1182
0
        g_critical("FuUsbInterfaceHdr refcount already zero");
1183
0
        return;
1184
0
    }
1185
86.4k
    if (--st->refcount > 0)
1186
0
        return;
1187
86.4k
    if (st->buf != NULL)
1188
86.4k
        g_byte_array_unref(st->buf);
1189
86.4k
    g_free(st);
1190
86.4k
}
1191
/**
1192
 * fu_usb_interface_hdr_new_internal: (skip):
1193
 **/
1194
static FuUsbInterfaceHdr *
1195
fu_usb_interface_hdr_new_internal(void)
1196
86.4k
{
1197
86.4k
    FuUsbInterfaceHdr *st = g_new0(FuUsbInterfaceHdr, 1);
1198
86.4k
    st->refcount = 1;
1199
86.4k
    return st;
1200
86.4k
}
1201
1202
/* getters */
1203
/**
1204
 * fu_usb_interface_hdr_get_length: (skip):
1205
 **/
1206
guint8
1207
fu_usb_interface_hdr_get_length(const FuUsbInterfaceHdr *st)
1208
86.3k
{
1209
86.3k
    g_return_val_if_fail(st != NULL, 0x0);
1210
86.3k
    return st->buf->data[0];
1211
86.3k
}
1212
/**
1213
 * fu_usb_interface_hdr_get_descriptor_type: (skip):
1214
 **/
1215
static FuUsbDescriptorKind
1216
fu_usb_interface_hdr_get_descriptor_type(const FuUsbInterfaceHdr *st)
1217
86.3k
{
1218
86.3k
    g_return_val_if_fail(st != NULL, 0x0);
1219
86.3k
    return st->buf->data[1];
1220
86.3k
}
1221
/**
1222
 * fu_usb_interface_hdr_get_interface_number: (skip):
1223
 **/
1224
guint8
1225
fu_usb_interface_hdr_get_interface_number(const FuUsbInterfaceHdr *st)
1226
86.3k
{
1227
86.3k
    g_return_val_if_fail(st != NULL, 0x0);
1228
86.3k
    return st->buf->data[2];
1229
86.3k
}
1230
/**
1231
 * fu_usb_interface_hdr_get_alternate_setting: (skip):
1232
 **/
1233
guint8
1234
fu_usb_interface_hdr_get_alternate_setting(const FuUsbInterfaceHdr *st)
1235
86.3k
{
1236
86.3k
    g_return_val_if_fail(st != NULL, 0x0);
1237
86.3k
    return st->buf->data[3];
1238
86.3k
}
1239
/**
1240
 * fu_usb_interface_hdr_get_num_endpoints: (skip):
1241
 **/
1242
guint8
1243
fu_usb_interface_hdr_get_num_endpoints(const FuUsbInterfaceHdr *st)
1244
86.3k
{
1245
86.3k
    g_return_val_if_fail(st != NULL, 0x0);
1246
86.3k
    return st->buf->data[4];
1247
86.3k
}
1248
/**
1249
 * fu_usb_interface_hdr_get_interface_class: (skip):
1250
 **/
1251
FuUsbClass
1252
fu_usb_interface_hdr_get_interface_class(const FuUsbInterfaceHdr *st)
1253
86.3k
{
1254
86.3k
    g_return_val_if_fail(st != NULL, 0x0);
1255
86.3k
    return st->buf->data[5];
1256
86.3k
}
1257
/**
1258
 * fu_usb_interface_hdr_get_interface_sub_class: (skip):
1259
 **/
1260
guint8
1261
fu_usb_interface_hdr_get_interface_sub_class(const FuUsbInterfaceHdr *st)
1262
86.3k
{
1263
86.3k
    g_return_val_if_fail(st != NULL, 0x0);
1264
86.3k
    return st->buf->data[6];
1265
86.3k
}
1266
/**
1267
 * fu_usb_interface_hdr_get_interface_protocol: (skip):
1268
 **/
1269
guint8
1270
fu_usb_interface_hdr_get_interface_protocol(const FuUsbInterfaceHdr *st)
1271
86.3k
{
1272
86.3k
    g_return_val_if_fail(st != NULL, 0x0);
1273
86.3k
    return st->buf->data[7];
1274
86.3k
}
1275
/**
1276
 * fu_usb_interface_hdr_get_interface: (skip):
1277
 **/
1278
guint8
1279
fu_usb_interface_hdr_get_interface(const FuUsbInterfaceHdr *st)
1280
86.3k
{
1281
86.3k
    g_return_val_if_fail(st != NULL, 0x0);
1282
86.3k
    return st->buf->data[8];
1283
86.3k
}
1284
1285
/* setters */
1286
/**
1287
 * fu_usb_interface_hdr_to_string: (skip):
1288
 **/
1289
static gchar *
1290
fu_usb_interface_hdr_to_string(const FuUsbInterfaceHdr *st)
1291
0
{
1292
0
    g_autoptr(GString) str = g_string_new("FuUsbInterfaceHdr:\n");
1293
0
    g_return_val_if_fail(st != NULL, NULL);
1294
0
    g_string_append_printf(str, "  length: 0x%x\n",
1295
0
                           (guint) fu_usb_interface_hdr_get_length(st));
1296
0
    {
1297
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_interface_hdr_get_descriptor_type(st));
1298
0
        if (tmp != NULL) {
1299
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_interface_hdr_get_descriptor_type(st), tmp);
1300
0
        } else {
1301
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_interface_hdr_get_descriptor_type(st));
1302
0
        }
1303
0
    }
1304
0
    g_string_append_printf(str, "  interface_number: 0x%x\n",
1305
0
                           (guint) fu_usb_interface_hdr_get_interface_number(st));
1306
0
    g_string_append_printf(str, "  alternate_setting: 0x%x\n",
1307
0
                           (guint) fu_usb_interface_hdr_get_alternate_setting(st));
1308
0
    g_string_append_printf(str, "  num_endpoints: 0x%x\n",
1309
0
                           (guint) fu_usb_interface_hdr_get_num_endpoints(st));
1310
0
    {
1311
0
        const gchar *tmp = fu_usb_class_to_string(fu_usb_interface_hdr_get_interface_class(st));
1312
0
        if (tmp != NULL) {
1313
0
            g_string_append_printf(str, "  interface_class: 0x%x [%s]\n", (guint) fu_usb_interface_hdr_get_interface_class(st), tmp);
1314
0
        } else {
1315
0
            g_string_append_printf(str, "  interface_class: 0x%x\n", (guint) fu_usb_interface_hdr_get_interface_class(st));
1316
0
        }
1317
0
    }
1318
0
    g_string_append_printf(str, "  interface_sub_class: 0x%x\n",
1319
0
                           (guint) fu_usb_interface_hdr_get_interface_sub_class(st));
1320
0
    g_string_append_printf(str, "  interface_protocol: 0x%x\n",
1321
0
                           (guint) fu_usb_interface_hdr_get_interface_protocol(st));
1322
0
    g_string_append_printf(str, "  interface: 0x%x\n",
1323
0
                           (guint) fu_usb_interface_hdr_get_interface(st));
1324
0
    if (str->len > 0)
1325
0
        g_string_set_size(str, str->len - 1);
1326
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1327
0
}
1328
static gboolean
1329
fu_usb_interface_hdr_validate_internal(FuUsbInterfaceHdr *st, GError **error)
1330
86.3k
{
1331
86.3k
    g_return_val_if_fail(st != NULL, FALSE);
1332
86.3k
    if (fu_usb_interface_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_INTERFACE) {
1333
0
        g_set_error(error,
1334
0
                    FWUPD_ERROR,
1335
0
                    FWUPD_ERROR_INVALID_DATA,
1336
0
                    "constant FuUsbInterfaceHdr.descriptor_type was not valid, "
1337
0
                    "expected 'FU_USB_DESCRIPTOR_KIND_INTERFACE' and got 0x%x '%s'",
1338
0
                    fu_usb_interface_hdr_get_descriptor_type(st),
1339
0
                    fu_usb_descriptor_kind_to_string(fu_usb_interface_hdr_get_descriptor_type(st)));
1340
0
        return FALSE;
1341
0
    }
1342
86.3k
    return TRUE;
1343
86.3k
}
1344
static gboolean
1345
fu_usb_interface_hdr_parse_internal(FuUsbInterfaceHdr *st, GError **error)
1346
86.3k
{
1347
86.3k
    if (g_log_get_debug_enabled()) {
1348
0
        g_autofree gchar *str = fu_usb_interface_hdr_to_string(st);
1349
0
        g_debug("%s", str);
1350
0
    }
1351
86.3k
    if (!fu_usb_interface_hdr_validate_internal(st, error))
1352
0
        return FALSE;
1353
86.3k
    return TRUE;
1354
86.3k
}
1355
/**
1356
 * fu_usb_interface_hdr_parse_stream: (skip):
1357
 **/
1358
FuUsbInterfaceHdr *
1359
fu_usb_interface_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
1360
86.4k
{
1361
86.4k
    g_autoptr(FuUsbInterfaceHdr) st = fu_usb_interface_hdr_new_internal();
1362
86.4k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 9, NULL, error);
1363
86.4k
    if (st->buf == NULL) {
1364
0
        g_prefix_error(error, "FuUsbInterfaceHdr failed read of 0x%x: ", (guint) 9);
1365
0
        return NULL;
1366
0
    }
1367
86.4k
    if (st->buf->len != 9) {
1368
38
        g_set_error(error,
1369
38
                    FWUPD_ERROR,
1370
38
                    FWUPD_ERROR_INVALID_DATA,
1371
38
                    "FuUsbInterfaceHdr requested 0x%x and got 0x%x",
1372
38
                    (guint) 9,
1373
38
                    (guint) st->buf->len);
1374
38
        return NULL;
1375
38
    }
1376
86.3k
    if (!fu_usb_interface_hdr_parse_internal(st, error))
1377
0
        return NULL;
1378
86.3k
    return g_steal_pointer(&st);
1379
86.3k
}
1380
/**
1381
 * fu_usb_endpoint_hdr_ref: (skip):
1382
 **/
1383
FuUsbEndpointHdr *
1384
fu_usb_endpoint_hdr_ref(FuUsbEndpointHdr *st)
1385
0
{
1386
0
    g_return_val_if_fail(st != NULL, NULL);
1387
0
    st->refcount++;
1388
0
    return st;
1389
0
}
1390
/**
1391
 * fu_usb_endpoint_hdr_unref: (skip):
1392
 **/
1393
void
1394
fu_usb_endpoint_hdr_unref(FuUsbEndpointHdr *st)
1395
26.6k
{
1396
26.6k
    g_return_if_fail(st != NULL);
1397
26.6k
    if (st->refcount == 0) {
1398
0
        g_critical("FuUsbEndpointHdr refcount already zero");
1399
0
        return;
1400
0
    }
1401
26.6k
    if (--st->refcount > 0)
1402
0
        return;
1403
26.6k
    if (st->buf != NULL)
1404
26.6k
        g_byte_array_unref(st->buf);
1405
26.6k
    g_free(st);
1406
26.6k
}
1407
/**
1408
 * fu_usb_endpoint_hdr_new_internal: (skip):
1409
 **/
1410
static FuUsbEndpointHdr *
1411
fu_usb_endpoint_hdr_new_internal(void)
1412
26.6k
{
1413
26.6k
    FuUsbEndpointHdr *st = g_new0(FuUsbEndpointHdr, 1);
1414
26.6k
    st->refcount = 1;
1415
26.6k
    return st;
1416
26.6k
}
1417
1418
/* getters */
1419
/**
1420
 * fu_usb_endpoint_hdr_get_length: (skip):
1421
 **/
1422
guint8
1423
fu_usb_endpoint_hdr_get_length(const FuUsbEndpointHdr *st)
1424
26.5k
{
1425
26.5k
    g_return_val_if_fail(st != NULL, 0x0);
1426
26.5k
    return st->buf->data[0];
1427
26.5k
}
1428
/**
1429
 * fu_usb_endpoint_hdr_get_descriptor_type: (skip):
1430
 **/
1431
FuUsbDescriptorKind
1432
fu_usb_endpoint_hdr_get_descriptor_type(const FuUsbEndpointHdr *st)
1433
26.5k
{
1434
26.5k
    g_return_val_if_fail(st != NULL, 0x0);
1435
26.5k
    return st->buf->data[1];
1436
26.5k
}
1437
/**
1438
 * fu_usb_endpoint_hdr_get_endpoint_address: (skip):
1439
 **/
1440
guint8
1441
fu_usb_endpoint_hdr_get_endpoint_address(const FuUsbEndpointHdr *st)
1442
26.5k
{
1443
26.5k
    g_return_val_if_fail(st != NULL, 0x0);
1444
26.5k
    return st->buf->data[2];
1445
26.5k
}
1446
/**
1447
 * fu_usb_endpoint_hdr_get_attributes: (skip):
1448
 **/
1449
guint8
1450
fu_usb_endpoint_hdr_get_attributes(const FuUsbEndpointHdr *st)
1451
26.5k
{
1452
26.5k
    g_return_val_if_fail(st != NULL, 0x0);
1453
26.5k
    return st->buf->data[3];
1454
26.5k
}
1455
/**
1456
 * fu_usb_endpoint_hdr_get_max_packet_size: (skip):
1457
 **/
1458
guint16
1459
fu_usb_endpoint_hdr_get_max_packet_size(const FuUsbEndpointHdr *st)
1460
26.5k
{
1461
26.5k
    g_return_val_if_fail(st != NULL, 0x0);
1462
26.5k
    return fu_memread_uint16(st->buf->data + 4, G_LITTLE_ENDIAN);
1463
26.5k
}
1464
/**
1465
 * fu_usb_endpoint_hdr_get_interval: (skip):
1466
 **/
1467
guint8
1468
fu_usb_endpoint_hdr_get_interval(const FuUsbEndpointHdr *st)
1469
26.5k
{
1470
26.5k
    g_return_val_if_fail(st != NULL, 0x0);
1471
26.5k
    return st->buf->data[6];
1472
26.5k
}
1473
1474
/* setters */
1475
/**
1476
 * fu_usb_endpoint_hdr_to_string: (skip):
1477
 **/
1478
static gchar *
1479
fu_usb_endpoint_hdr_to_string(const FuUsbEndpointHdr *st)
1480
0
{
1481
0
    g_autoptr(GString) str = g_string_new("FuUsbEndpointHdr:\n");
1482
0
    g_return_val_if_fail(st != NULL, NULL);
1483
0
    g_string_append_printf(str, "  length: 0x%x\n",
1484
0
                           (guint) fu_usb_endpoint_hdr_get_length(st));
1485
0
    {
1486
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_endpoint_hdr_get_descriptor_type(st));
1487
0
        if (tmp != NULL) {
1488
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_endpoint_hdr_get_descriptor_type(st), tmp);
1489
0
        } else {
1490
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_endpoint_hdr_get_descriptor_type(st));
1491
0
        }
1492
0
    }
1493
0
    g_string_append_printf(str, "  endpoint_address: 0x%x\n",
1494
0
                           (guint) fu_usb_endpoint_hdr_get_endpoint_address(st));
1495
0
    g_string_append_printf(str, "  attributes: 0x%x\n",
1496
0
                           (guint) fu_usb_endpoint_hdr_get_attributes(st));
1497
0
    g_string_append_printf(str, "  max_packet_size: 0x%x\n",
1498
0
                           (guint) fu_usb_endpoint_hdr_get_max_packet_size(st));
1499
0
    g_string_append_printf(str, "  interval: 0x%x\n",
1500
0
                           (guint) fu_usb_endpoint_hdr_get_interval(st));
1501
0
    if (str->len > 0)
1502
0
        g_string_set_size(str, str->len - 1);
1503
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1504
0
}
1505
static gboolean
1506
fu_usb_endpoint_hdr_parse_internal(FuUsbEndpointHdr *st, GError **error)
1507
26.5k
{
1508
26.5k
    if (g_log_get_debug_enabled()) {
1509
0
        g_autofree gchar *str = fu_usb_endpoint_hdr_to_string(st);
1510
0
        g_debug("%s", str);
1511
0
    }
1512
26.5k
    return TRUE;
1513
26.5k
}
1514
/**
1515
 * fu_usb_endpoint_hdr_parse_stream: (skip):
1516
 **/
1517
FuUsbEndpointHdr *
1518
fu_usb_endpoint_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
1519
26.6k
{
1520
26.6k
    g_autoptr(FuUsbEndpointHdr) st = fu_usb_endpoint_hdr_new_internal();
1521
26.6k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 7, NULL, error);
1522
26.6k
    if (st->buf == NULL) {
1523
0
        g_prefix_error(error, "FuUsbEndpointHdr failed read of 0x%x: ", (guint) 7);
1524
0
        return NULL;
1525
0
    }
1526
26.6k
    if (st->buf->len != 7) {
1527
41
        g_set_error(error,
1528
41
                    FWUPD_ERROR,
1529
41
                    FWUPD_ERROR_INVALID_DATA,
1530
41
                    "FuUsbEndpointHdr requested 0x%x and got 0x%x",
1531
41
                    (guint) 7,
1532
41
                    (guint) st->buf->len);
1533
41
        return NULL;
1534
41
    }
1535
26.5k
    if (!fu_usb_endpoint_hdr_parse_internal(st, error))
1536
0
        return NULL;
1537
26.5k
    return g_steal_pointer(&st);
1538
26.5k
}
1539
/**
1540
 * fu_usb_bos_hdr_ref: (skip):
1541
 **/
1542
FuUsbBosHdr *
1543
fu_usb_bos_hdr_ref(FuUsbBosHdr *st)
1544
0
{
1545
0
    g_return_val_if_fail(st != NULL, NULL);
1546
0
    st->refcount++;
1547
0
    return st;
1548
0
}
1549
/**
1550
 * fu_usb_bos_hdr_unref: (skip):
1551
 **/
1552
void
1553
fu_usb_bos_hdr_unref(FuUsbBosHdr *st)
1554
0
{
1555
0
    g_return_if_fail(st != NULL);
1556
0
    if (st->refcount == 0) {
1557
0
        g_critical("FuUsbBosHdr refcount already zero");
1558
0
        return;
1559
0
    }
1560
0
    if (--st->refcount > 0)
1561
0
        return;
1562
0
    if (st->buf != NULL)
1563
0
        g_byte_array_unref(st->buf);
1564
0
    g_free(st);
1565
0
}
1566
/**
1567
 * fu_usb_bos_hdr_new_internal: (skip):
1568
 **/
1569
static FuUsbBosHdr *
1570
fu_usb_bos_hdr_new_internal(void)
1571
0
{
1572
0
    FuUsbBosHdr *st = g_new0(FuUsbBosHdr, 1);
1573
0
    st->refcount = 1;
1574
0
    return st;
1575
0
}
1576
1577
/* getters */
1578
/**
1579
 * fu_usb_bos_hdr_get_length: (skip):
1580
 **/
1581
guint8
1582
fu_usb_bos_hdr_get_length(const FuUsbBosHdr *st)
1583
0
{
1584
0
    g_return_val_if_fail(st != NULL, 0x0);
1585
0
    return st->buf->data[0];
1586
0
}
1587
/**
1588
 * fu_usb_bos_hdr_get_descriptor_type: (skip):
1589
 **/
1590
FuUsbDescriptorKind
1591
fu_usb_bos_hdr_get_descriptor_type(const FuUsbBosHdr *st)
1592
0
{
1593
0
    g_return_val_if_fail(st != NULL, 0x0);
1594
0
    return st->buf->data[1];
1595
0
}
1596
/**
1597
 * fu_usb_bos_hdr_get_dev_capability_type: (skip):
1598
 **/
1599
guint8
1600
fu_usb_bos_hdr_get_dev_capability_type(const FuUsbBosHdr *st)
1601
0
{
1602
0
    g_return_val_if_fail(st != NULL, 0x0);
1603
0
    return st->buf->data[2];
1604
0
}
1605
1606
/* setters */
1607
/**
1608
 * fu_usb_bos_hdr_set_length: (skip):
1609
 **/
1610
void
1611
fu_usb_bos_hdr_set_length(FuUsbBosHdr *st, guint8 value)
1612
0
{
1613
0
    g_return_if_fail(st != NULL);
1614
0
    st->buf->data[0] = value;
1615
0
}
1616
/**
1617
 * fu_usb_bos_hdr_set_descriptor_type: (skip):
1618
 **/
1619
void
1620
fu_usb_bos_hdr_set_descriptor_type(FuUsbBosHdr *st, FuUsbDescriptorKind value)
1621
0
{
1622
0
    g_return_if_fail(st != NULL);
1623
0
    st->buf->data[1] = value;
1624
0
}
1625
/**
1626
 * fu_usb_bos_hdr_set_dev_capability_type: (skip):
1627
 **/
1628
void
1629
fu_usb_bos_hdr_set_dev_capability_type(FuUsbBosHdr *st, guint8 value)
1630
0
{
1631
0
    g_return_if_fail(st != NULL);
1632
0
    st->buf->data[2] = value;
1633
0
}
1634
/**
1635
 * fu_usb_bos_hdr_new: (skip):
1636
 **/
1637
FuUsbBosHdr *
1638
fu_usb_bos_hdr_new(void)
1639
0
{
1640
0
    FuUsbBosHdr *st = fu_usb_bos_hdr_new_internal();
1641
0
    st->buf = g_byte_array_sized_new(3);
1642
0
    fu_byte_array_set_size(st->buf, 3, 0x0);
1643
0
    fu_usb_bos_hdr_set_length(st, 3);
1644
0
    fu_usb_bos_hdr_set_descriptor_type(st, FU_USB_DESCRIPTOR_KIND_BOS);
1645
0
    return st;
1646
0
}
1647
/**
1648
 * fu_usb_bos_hdr_to_string: (skip):
1649
 **/
1650
static gchar *
1651
fu_usb_bos_hdr_to_string(const FuUsbBosHdr *st)
1652
0
{
1653
0
    g_autoptr(GString) str = g_string_new("FuUsbBosHdr:\n");
1654
0
    g_return_val_if_fail(st != NULL, NULL);
1655
0
    g_string_append_printf(str, "  length: 0x%x\n",
1656
0
                           (guint) fu_usb_bos_hdr_get_length(st));
1657
0
    {
1658
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_bos_hdr_get_descriptor_type(st));
1659
0
        if (tmp != NULL) {
1660
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_bos_hdr_get_descriptor_type(st), tmp);
1661
0
        } else {
1662
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_bos_hdr_get_descriptor_type(st));
1663
0
        }
1664
0
    }
1665
0
    g_string_append_printf(str, "  dev_capability_type: 0x%x\n",
1666
0
                           (guint) fu_usb_bos_hdr_get_dev_capability_type(st));
1667
0
    if (str->len > 0)
1668
0
        g_string_set_size(str, str->len - 1);
1669
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1670
0
}
1671
static gboolean
1672
fu_usb_bos_hdr_parse_internal(FuUsbBosHdr *st, GError **error)
1673
0
{
1674
0
    if (g_log_get_debug_enabled()) {
1675
0
        g_autofree gchar *str = fu_usb_bos_hdr_to_string(st);
1676
0
        g_debug("%s", str);
1677
0
    }
1678
0
    return TRUE;
1679
0
}
1680
1681
/**
1682
 * fu_usb_bos_hdr_parse: (skip):
1683
 **/
1684
FuUsbBosHdr *
1685
fu_usb_bos_hdr_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
1686
0
{
1687
0
    g_autoptr(FuUsbBosHdr) st = fu_usb_bos_hdr_new_internal();
1688
0
    g_return_val_if_fail(buf != NULL, NULL);
1689
0
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
1690
0
    if (!fu_memchk_read(bufsz, offset, 3, error)) {
1691
0
        g_prefix_error_literal(error, "invalid struct FuUsbBosHdr: ");
1692
0
        return NULL;
1693
0
    }
1694
0
    st->buf = g_byte_array_new();
1695
0
    g_byte_array_append(st->buf, buf + offset, 3);
1696
0
    if (!fu_usb_bos_hdr_parse_internal(st, error))
1697
0
        return NULL;
1698
0
    return g_steal_pointer(&st);
1699
0
}
1700
/**
1701
 * fu_usb_bos_hdr_parse_stream: (skip):
1702
 **/
1703
FuUsbBosHdr *
1704
fu_usb_bos_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
1705
0
{
1706
0
    g_autoptr(FuUsbBosHdr) st = fu_usb_bos_hdr_new_internal();
1707
0
    st->buf = fu_input_stream_read_byte_array(stream, offset, 3, NULL, error);
1708
0
    if (st->buf == NULL) {
1709
0
        g_prefix_error(error, "FuUsbBosHdr failed read of 0x%x: ", (guint) 3);
1710
0
        return NULL;
1711
0
    }
1712
0
    if (st->buf->len != 3) {
1713
0
        g_set_error(error,
1714
0
                    FWUPD_ERROR,
1715
0
                    FWUPD_ERROR_INVALID_DATA,
1716
0
                    "FuUsbBosHdr requested 0x%x and got 0x%x",
1717
0
                    (guint) 3,
1718
0
                    (guint) st->buf->len);
1719
0
        return NULL;
1720
0
    }
1721
0
    if (!fu_usb_bos_hdr_parse_internal(st, error))
1722
0
        return NULL;
1723
0
    return g_steal_pointer(&st);
1724
0
}