Coverage Report

Created: 2025-12-14 06:56

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