Coverage Report

Created: 2025-11-11 06:44

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