Coverage Report

Created: 2026-01-09 07:21

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
    {
498
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_device_hdr_get_descriptor_type(st));
499
0
        if (tmp != NULL) {
500
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_device_hdr_get_descriptor_type(st), tmp);
501
0
        } else {
502
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_device_hdr_get_descriptor_type(st));
503
0
        }
504
0
    }
505
0
    g_string_append_printf(str, "  usb: 0x%x\n",
506
0
                           (guint) fu_usb_device_hdr_get_usb(st));
507
0
    {
508
0
        const gchar *tmp = fu_usb_class_to_string(fu_usb_device_hdr_get_device_class(st));
509
0
        if (tmp != NULL) {
510
0
            g_string_append_printf(str, "  device_class: 0x%x [%s]\n", (guint) fu_usb_device_hdr_get_device_class(st), tmp);
511
0
        } else {
512
0
            g_string_append_printf(str, "  device_class: 0x%x\n", (guint) fu_usb_device_hdr_get_device_class(st));
513
0
        }
514
0
    }
515
0
    g_string_append_printf(str, "  device_sub_class: 0x%x\n",
516
0
                           (guint) fu_usb_device_hdr_get_device_sub_class(st));
517
0
    g_string_append_printf(str, "  device_protocol: 0x%x\n",
518
0
                           (guint) fu_usb_device_hdr_get_device_protocol(st));
519
0
    g_string_append_printf(str, "  max_packet_size0: 0x%x\n",
520
0
                           (guint) fu_usb_device_hdr_get_max_packet_size0(st));
521
0
    g_string_append_printf(str, "  vendor: 0x%x\n",
522
0
                           (guint) fu_usb_device_hdr_get_vendor(st));
523
0
    g_string_append_printf(str, "  product: 0x%x\n",
524
0
                           (guint) fu_usb_device_hdr_get_product(st));
525
0
    g_string_append_printf(str, "  device: 0x%x\n",
526
0
                           (guint) fu_usb_device_hdr_get_device(st));
527
0
    g_string_append_printf(str, "  manufacturer_idx: 0x%x\n",
528
0
                           (guint) fu_usb_device_hdr_get_manufacturer_idx(st));
529
0
    g_string_append_printf(str, "  product_idx: 0x%x\n",
530
0
                           (guint) fu_usb_device_hdr_get_product_idx(st));
531
0
    g_string_append_printf(str, "  serial_number_idx: 0x%x\n",
532
0
                           (guint) fu_usb_device_hdr_get_serial_number_idx(st));
533
0
    g_string_append_printf(str, "  num_configurations: 0x%x\n",
534
0
                           (guint) fu_usb_device_hdr_get_num_configurations(st));
535
0
    if (str->len > 0)
536
0
        g_string_set_size(str, str->len - 1);
537
0
    return g_string_free(g_steal_pointer(&str), FALSE);
538
0
}
539
static gboolean
540
fu_usb_device_hdr_validate_internal(FuUsbDeviceHdr *st, GError **error)
541
0
{
542
0
    g_return_val_if_fail(st != NULL, FALSE);
543
0
    if (fu_usb_device_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_DEVICE) {
544
0
        g_set_error(error,
545
0
                    FWUPD_ERROR,
546
0
                    FWUPD_ERROR_INVALID_DATA,
547
0
                    "constant FuUsbDeviceHdr.descriptor_type was not valid, "
548
0
                    "expected 'FU_USB_DESCRIPTOR_KIND_DEVICE' and got 0x%x '%s'",
549
0
                    fu_usb_device_hdr_get_descriptor_type(st),
550
0
                    fu_usb_descriptor_kind_to_string(fu_usb_device_hdr_get_descriptor_type(st)));
551
0
        return FALSE;
552
0
    }
553
0
    return TRUE;
554
0
}
555
static gboolean
556
fu_usb_device_hdr_parse_internal(FuUsbDeviceHdr *st, GError **error)
557
0
{
558
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
559
0
        g_autofree gchar *str = fu_usb_device_hdr_to_string(st);
560
0
        g_debug("%s", str);
561
0
    }
562
0
    if (!fu_usb_device_hdr_validate_internal(st, error))
563
0
        return FALSE;
564
0
    return TRUE;
565
0
}
566
567
/**
568
 * fu_usb_device_hdr_parse: (skip):
569
 **/
570
static FuUsbDeviceHdr *
571
fu_usb_device_hdr_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
572
0
{
573
0
    g_autoptr(FuUsbDeviceHdr) st = fu_usb_device_hdr_new_internal();
574
0
    g_return_val_if_fail(buf != NULL, NULL);
575
0
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
576
0
    if (!fu_memchk_read(bufsz, offset, 18, error)) {
577
0
        g_prefix_error_literal(error, "invalid struct FuUsbDeviceHdr: ");
578
0
        return NULL;
579
0
    }
580
0
    st->buf = g_byte_array_new();
581
0
    g_byte_array_append(st->buf, buf + offset, 18);
582
0
    if (!fu_usb_device_hdr_parse_internal(st, error))
583
0
        return NULL;
584
0
    return g_steal_pointer(&st);
585
0
}
586
/**
587
 * fu_usb_device_hdr_parse_bytes: (skip):
588
 **/
589
FuUsbDeviceHdr *
590
fu_usb_device_hdr_parse_bytes(GBytes *blob, gsize offset, GError **error)
591
0
{
592
0
    gsize bufsz = 0;
593
0
    const guint8 *buf = g_bytes_get_data(blob, &bufsz);
594
0
    return fu_usb_device_hdr_parse(buf, bufsz, offset, error);
595
0
}
596
/**
597
 * fu_usb_descriptor_hdr_ref: (skip):
598
 **/
599
FuUsbDescriptorHdr *
600
fu_usb_descriptor_hdr_ref(FuUsbDescriptorHdr *st)
601
0
{
602
0
    g_return_val_if_fail(st != NULL, NULL);
603
0
    st->refcount++;
604
0
    return st;
605
0
}
606
/**
607
 * fu_usb_descriptor_hdr_unref: (skip):
608
 **/
609
void
610
fu_usb_descriptor_hdr_unref(FuUsbDescriptorHdr *st)
611
0
{
612
0
    g_return_if_fail(st != NULL);
613
0
    if (st->refcount == 0) {
614
0
        g_critical("FuUsbDescriptorHdr refcount already zero");
615
0
        return;
616
0
    }
617
0
    if (--st->refcount > 0)
618
0
        return;
619
0
    if (st->buf != NULL)
620
0
        g_byte_array_unref(st->buf);
621
0
    g_free(st);
622
0
}
623
/**
624
 * fu_usb_descriptor_hdr_new_internal: (skip):
625
 **/
626
static FuUsbDescriptorHdr *
627
fu_usb_descriptor_hdr_new_internal(void)
628
0
{
629
0
    FuUsbDescriptorHdr *st = g_new0(FuUsbDescriptorHdr, 1);
630
0
    st->refcount = 1;
631
0
    return st;
632
0
}
633
634
/* getters */
635
/**
636
 * fu_usb_descriptor_hdr_get_length: (skip):
637
 **/
638
guint8
639
fu_usb_descriptor_hdr_get_length(const FuUsbDescriptorHdr *st)
640
0
{
641
0
    g_return_val_if_fail(st != NULL, 0x0);
642
0
    return st->buf->data[0];
643
0
}
644
/**
645
 * fu_usb_descriptor_hdr_get_descriptor_type: (skip):
646
 **/
647
static FuUsbDescriptorKind
648
fu_usb_descriptor_hdr_get_descriptor_type(const FuUsbDescriptorHdr *st)
649
0
{
650
0
    g_return_val_if_fail(st != NULL, 0x0);
651
0
    return st->buf->data[1];
652
0
}
653
/**
654
 * fu_usb_descriptor_hdr_get_total_length: (skip):
655
 **/
656
guint16
657
fu_usb_descriptor_hdr_get_total_length(const FuUsbDescriptorHdr *st)
658
0
{
659
0
    g_return_val_if_fail(st != NULL, 0x0);
660
0
    return fu_memread_uint16(st->buf->data + 2, G_LITTLE_ENDIAN);
661
0
}
662
/**
663
 * fu_usb_descriptor_hdr_get_num_interfaces: (skip):
664
 **/
665
guint8
666
fu_usb_descriptor_hdr_get_num_interfaces(const FuUsbDescriptorHdr *st)
667
0
{
668
0
    g_return_val_if_fail(st != NULL, 0x0);
669
0
    return st->buf->data[4];
670
0
}
671
/**
672
 * fu_usb_descriptor_hdr_get_configuration_value: (skip):
673
 **/
674
guint8
675
fu_usb_descriptor_hdr_get_configuration_value(const FuUsbDescriptorHdr *st)
676
0
{
677
0
    g_return_val_if_fail(st != NULL, 0x0);
678
0
    return st->buf->data[5];
679
0
}
680
/**
681
 * fu_usb_descriptor_hdr_get_configuration: (skip):
682
 **/
683
guint8
684
fu_usb_descriptor_hdr_get_configuration(const FuUsbDescriptorHdr *st)
685
0
{
686
0
    g_return_val_if_fail(st != NULL, 0x0);
687
0
    return st->buf->data[6];
688
0
}
689
/**
690
 * fu_usb_descriptor_hdr_get_attributes: (skip):
691
 **/
692
guint8
693
fu_usb_descriptor_hdr_get_attributes(const FuUsbDescriptorHdr *st)
694
0
{
695
0
    g_return_val_if_fail(st != NULL, 0x0);
696
0
    return st->buf->data[7];
697
0
}
698
/**
699
 * fu_usb_descriptor_hdr_get_max_power: (skip):
700
 **/
701
guint8
702
fu_usb_descriptor_hdr_get_max_power(const FuUsbDescriptorHdr *st)
703
0
{
704
0
    g_return_val_if_fail(st != NULL, 0x0);
705
0
    return st->buf->data[8];
706
0
}
707
708
/* setters */
709
/**
710
 * fu_usb_descriptor_hdr_to_string: (skip):
711
 **/
712
static gchar *
713
fu_usb_descriptor_hdr_to_string(const FuUsbDescriptorHdr *st)
714
0
{
715
0
    g_autoptr(GString) str = g_string_new("FuUsbDescriptorHdr:\n");
716
0
    g_return_val_if_fail(st != NULL, NULL);
717
0
    g_string_append_printf(str, "  length: 0x%x\n",
718
0
                           (guint) fu_usb_descriptor_hdr_get_length(st));
719
0
    {
720
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_descriptor_hdr_get_descriptor_type(st));
721
0
        if (tmp != NULL) {
722
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_descriptor_hdr_get_descriptor_type(st), tmp);
723
0
        } else {
724
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_descriptor_hdr_get_descriptor_type(st));
725
0
        }
726
0
    }
727
0
    g_string_append_printf(str, "  total_length: 0x%x\n",
728
0
                           (guint) fu_usb_descriptor_hdr_get_total_length(st));
729
0
    g_string_append_printf(str, "  num_interfaces: 0x%x\n",
730
0
                           (guint) fu_usb_descriptor_hdr_get_num_interfaces(st));
731
0
    g_string_append_printf(str, "  configuration_value: 0x%x\n",
732
0
                           (guint) fu_usb_descriptor_hdr_get_configuration_value(st));
733
0
    g_string_append_printf(str, "  configuration: 0x%x\n",
734
0
                           (guint) fu_usb_descriptor_hdr_get_configuration(st));
735
0
    g_string_append_printf(str, "  attributes: 0x%x\n",
736
0
                           (guint) fu_usb_descriptor_hdr_get_attributes(st));
737
0
    g_string_append_printf(str, "  max_power: 0x%x\n",
738
0
                           (guint) fu_usb_descriptor_hdr_get_max_power(st));
739
0
    if (str->len > 0)
740
0
        g_string_set_size(str, str->len - 1);
741
0
    return g_string_free(g_steal_pointer(&str), FALSE);
742
0
}
743
static gboolean
744
fu_usb_descriptor_hdr_validate_internal(FuUsbDescriptorHdr *st, GError **error)
745
0
{
746
0
    g_return_val_if_fail(st != NULL, FALSE);
747
0
    if (fu_usb_descriptor_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_CONFIG) {
748
0
        g_set_error(error,
749
0
                    FWUPD_ERROR,
750
0
                    FWUPD_ERROR_INVALID_DATA,
751
0
                    "constant FuUsbDescriptorHdr.descriptor_type was not valid, "
752
0
                    "expected 'FU_USB_DESCRIPTOR_KIND_CONFIG' and got 0x%x '%s'",
753
0
                    fu_usb_descriptor_hdr_get_descriptor_type(st),
754
0
                    fu_usb_descriptor_kind_to_string(fu_usb_descriptor_hdr_get_descriptor_type(st)));
755
0
        return FALSE;
756
0
    }
757
0
    return TRUE;
758
0
}
759
static gboolean
760
fu_usb_descriptor_hdr_parse_internal(FuUsbDescriptorHdr *st, GError **error)
761
0
{
762
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
763
0
        g_autofree gchar *str = fu_usb_descriptor_hdr_to_string(st);
764
0
        g_debug("%s", str);
765
0
    }
766
0
    if (!fu_usb_descriptor_hdr_validate_internal(st, error))
767
0
        return FALSE;
768
0
    return TRUE;
769
0
}
770
/**
771
 * fu_usb_descriptor_hdr_parse_stream: (skip):
772
 **/
773
FuUsbDescriptorHdr *
774
fu_usb_descriptor_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
775
0
{
776
0
    g_autoptr(FuUsbDescriptorHdr) st = fu_usb_descriptor_hdr_new_internal();
777
0
    st->buf = fu_input_stream_read_byte_array(stream, offset, 9, NULL, error);
778
0
    if (st->buf == NULL) {
779
0
        g_prefix_error(error, "FuUsbDescriptorHdr failed read of 0x%x: ", (guint) 9);
780
0
        return NULL;
781
0
    }
782
0
    if (st->buf->len != 9) {
783
0
        g_set_error(error,
784
0
                    FWUPD_ERROR,
785
0
                    FWUPD_ERROR_INVALID_DATA,
786
0
                    "FuUsbDescriptorHdr requested 0x%x and got 0x%x",
787
0
                    (guint) 9,
788
0
                    (guint) st->buf->len);
789
0
        return NULL;
790
0
    }
791
0
    if (!fu_usb_descriptor_hdr_parse_internal(st, error))
792
0
        return NULL;
793
0
    return g_steal_pointer(&st);
794
0
}
795
/**
796
 * fu_usb_hid_descriptor_hdr_ref: (skip):
797
 **/
798
FuUsbHidDescriptorHdr *
799
fu_usb_hid_descriptor_hdr_ref(FuUsbHidDescriptorHdr *st)
800
0
{
801
0
    g_return_val_if_fail(st != NULL, NULL);
802
0
    st->refcount++;
803
0
    return st;
804
0
}
805
/**
806
 * fu_usb_hid_descriptor_hdr_unref: (skip):
807
 **/
808
void
809
fu_usb_hid_descriptor_hdr_unref(FuUsbHidDescriptorHdr *st)
810
0
{
811
0
    g_return_if_fail(st != NULL);
812
0
    if (st->refcount == 0) {
813
0
        g_critical("FuUsbHidDescriptorHdr refcount already zero");
814
0
        return;
815
0
    }
816
0
    if (--st->refcount > 0)
817
0
        return;
818
0
    if (st->buf != NULL)
819
0
        g_byte_array_unref(st->buf);
820
0
    g_free(st);
821
0
}
822
/**
823
 * fu_usb_hid_descriptor_hdr_new_internal: (skip):
824
 **/
825
static FuUsbHidDescriptorHdr *
826
fu_usb_hid_descriptor_hdr_new_internal(void)
827
0
{
828
0
    FuUsbHidDescriptorHdr *st = g_new0(FuUsbHidDescriptorHdr, 1);
829
0
    st->refcount = 1;
830
0
    return st;
831
0
}
832
833
/* getters */
834
/**
835
 * fu_usb_hid_descriptor_hdr_get_length: (skip):
836
 **/
837
guint8
838
fu_usb_hid_descriptor_hdr_get_length(const FuUsbHidDescriptorHdr *st)
839
0
{
840
0
    g_return_val_if_fail(st != NULL, 0x0);
841
0
    return st->buf->data[0];
842
0
}
843
/**
844
 * fu_usb_hid_descriptor_hdr_get_descriptor_type: (skip):
845
 **/
846
static FuUsbDescriptorKind
847
fu_usb_hid_descriptor_hdr_get_descriptor_type(const FuUsbHidDescriptorHdr *st)
848
0
{
849
0
    g_return_val_if_fail(st != NULL, 0x0);
850
0
    return st->buf->data[1];
851
0
}
852
/**
853
 * fu_usb_hid_descriptor_hdr_get_hid: (skip):
854
 **/
855
guint16
856
fu_usb_hid_descriptor_hdr_get_hid(const FuUsbHidDescriptorHdr *st)
857
0
{
858
0
    g_return_val_if_fail(st != NULL, 0x0);
859
0
    return fu_memread_uint16(st->buf->data + 2, G_LITTLE_ENDIAN);
860
0
}
861
/**
862
 * fu_usb_hid_descriptor_hdr_get_country_code: (skip):
863
 **/
864
guint8
865
fu_usb_hid_descriptor_hdr_get_country_code(const FuUsbHidDescriptorHdr *st)
866
0
{
867
0
    g_return_val_if_fail(st != NULL, 0x0);
868
0
    return st->buf->data[4];
869
0
}
870
/**
871
 * fu_usb_hid_descriptor_hdr_get_num_descriptors: (skip):
872
 **/
873
guint8
874
fu_usb_hid_descriptor_hdr_get_num_descriptors(const FuUsbHidDescriptorHdr *st)
875
0
{
876
0
    g_return_val_if_fail(st != NULL, 0x0);
877
0
    return st->buf->data[5];
878
0
}
879
/**
880
 * fu_usb_hid_descriptor_hdr_get_class_descriptor_type: (skip):
881
 **/
882
guint8
883
fu_usb_hid_descriptor_hdr_get_class_descriptor_type(const FuUsbHidDescriptorHdr *st)
884
0
{
885
0
    g_return_val_if_fail(st != NULL, 0x0);
886
0
    return st->buf->data[6];
887
0
}
888
/**
889
 * fu_usb_hid_descriptor_hdr_get_class_descriptor_length: (skip):
890
 **/
891
guint16
892
fu_usb_hid_descriptor_hdr_get_class_descriptor_length(const FuUsbHidDescriptorHdr *st)
893
0
{
894
0
    g_return_val_if_fail(st != NULL, 0x0);
895
0
    return fu_memread_uint16(st->buf->data + 7, G_LITTLE_ENDIAN);
896
0
}
897
898
/* setters */
899
/**
900
 * fu_usb_hid_descriptor_hdr_to_string: (skip):
901
 **/
902
static gchar *
903
fu_usb_hid_descriptor_hdr_to_string(const FuUsbHidDescriptorHdr *st)
904
0
{
905
0
    g_autoptr(GString) str = g_string_new("FuUsbHidDescriptorHdr:\n");
906
0
    g_return_val_if_fail(st != NULL, NULL);
907
0
    g_string_append_printf(str, "  length: 0x%x\n",
908
0
                           (guint) fu_usb_hid_descriptor_hdr_get_length(st));
909
0
    {
910
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_hid_descriptor_hdr_get_descriptor_type(st));
911
0
        if (tmp != NULL) {
912
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_hid_descriptor_hdr_get_descriptor_type(st), tmp);
913
0
        } else {
914
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_hid_descriptor_hdr_get_descriptor_type(st));
915
0
        }
916
0
    }
917
0
    g_string_append_printf(str, "  hid: 0x%x\n",
918
0
                           (guint) fu_usb_hid_descriptor_hdr_get_hid(st));
919
0
    g_string_append_printf(str, "  country_code: 0x%x\n",
920
0
                           (guint) fu_usb_hid_descriptor_hdr_get_country_code(st));
921
0
    g_string_append_printf(str, "  num_descriptors: 0x%x\n",
922
0
                           (guint) fu_usb_hid_descriptor_hdr_get_num_descriptors(st));
923
0
    g_string_append_printf(str, "  class_descriptor_type: 0x%x\n",
924
0
                           (guint) fu_usb_hid_descriptor_hdr_get_class_descriptor_type(st));
925
0
    g_string_append_printf(str, "  class_descriptor_length: 0x%x\n",
926
0
                           (guint) fu_usb_hid_descriptor_hdr_get_class_descriptor_length(st));
927
0
    if (str->len > 0)
928
0
        g_string_set_size(str, str->len - 1);
929
0
    return g_string_free(g_steal_pointer(&str), FALSE);
930
0
}
931
static gboolean
932
fu_usb_hid_descriptor_hdr_validate_internal(FuUsbHidDescriptorHdr *st, GError **error)
933
0
{
934
0
    g_return_val_if_fail(st != NULL, FALSE);
935
0
    if (fu_usb_hid_descriptor_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_HID) {
936
0
        g_set_error(error,
937
0
                    FWUPD_ERROR,
938
0
                    FWUPD_ERROR_INVALID_DATA,
939
0
                    "constant FuUsbHidDescriptorHdr.descriptor_type was not valid, "
940
0
                    "expected 'FU_USB_DESCRIPTOR_KIND_HID' and got 0x%x '%s'",
941
0
                    fu_usb_hid_descriptor_hdr_get_descriptor_type(st),
942
0
                    fu_usb_descriptor_kind_to_string(fu_usb_hid_descriptor_hdr_get_descriptor_type(st)));
943
0
        return FALSE;
944
0
    }
945
0
    return TRUE;
946
0
}
947
static gboolean
948
fu_usb_hid_descriptor_hdr_parse_internal(FuUsbHidDescriptorHdr *st, GError **error)
949
0
{
950
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
951
0
        g_autofree gchar *str = fu_usb_hid_descriptor_hdr_to_string(st);
952
0
        g_debug("%s", str);
953
0
    }
954
0
    if (!fu_usb_hid_descriptor_hdr_validate_internal(st, error))
955
0
        return FALSE;
956
0
    return TRUE;
957
0
}
958
/**
959
 * fu_usb_hid_descriptor_hdr_parse_stream: (skip):
960
 **/
961
FuUsbHidDescriptorHdr *
962
fu_usb_hid_descriptor_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
963
0
{
964
0
    g_autoptr(FuUsbHidDescriptorHdr) st = fu_usb_hid_descriptor_hdr_new_internal();
965
0
    st->buf = fu_input_stream_read_byte_array(stream, offset, 9, NULL, error);
966
0
    if (st->buf == NULL) {
967
0
        g_prefix_error(error, "FuUsbHidDescriptorHdr failed read of 0x%x: ", (guint) 9);
968
0
        return NULL;
969
0
    }
970
0
    if (st->buf->len != 9) {
971
0
        g_set_error(error,
972
0
                    FWUPD_ERROR,
973
0
                    FWUPD_ERROR_INVALID_DATA,
974
0
                    "FuUsbHidDescriptorHdr requested 0x%x and got 0x%x",
975
0
                    (guint) 9,
976
0
                    (guint) st->buf->len);
977
0
        return NULL;
978
0
    }
979
0
    if (!fu_usb_hid_descriptor_hdr_parse_internal(st, error))
980
0
        return NULL;
981
0
    return g_steal_pointer(&st);
982
0
}
983
/**
984
 * fu_usb_dfu_descriptor_hdr_ref: (skip):
985
 **/
986
FuUsbDfuDescriptorHdr *
987
fu_usb_dfu_descriptor_hdr_ref(FuUsbDfuDescriptorHdr *st)
988
0
{
989
0
    g_return_val_if_fail(st != NULL, NULL);
990
0
    st->refcount++;
991
0
    return st;
992
0
}
993
/**
994
 * fu_usb_dfu_descriptor_hdr_unref: (skip):
995
 **/
996
void
997
fu_usb_dfu_descriptor_hdr_unref(FuUsbDfuDescriptorHdr *st)
998
0
{
999
0
    g_return_if_fail(st != NULL);
1000
0
    if (st->refcount == 0) {
1001
0
        g_critical("FuUsbDfuDescriptorHdr refcount already zero");
1002
0
        return;
1003
0
    }
1004
0
    if (--st->refcount > 0)
1005
0
        return;
1006
0
    if (st->buf != NULL)
1007
0
        g_byte_array_unref(st->buf);
1008
0
    g_free(st);
1009
0
}
1010
/**
1011
 * fu_usb_dfu_descriptor_hdr_new_internal: (skip):
1012
 **/
1013
static FuUsbDfuDescriptorHdr *
1014
fu_usb_dfu_descriptor_hdr_new_internal(void)
1015
0
{
1016
0
    FuUsbDfuDescriptorHdr *st = g_new0(FuUsbDfuDescriptorHdr, 1);
1017
0
    st->refcount = 1;
1018
0
    return st;
1019
0
}
1020
1021
/* getters */
1022
/**
1023
 * fu_usb_dfu_descriptor_hdr_get_length: (skip):
1024
 **/
1025
guint8
1026
fu_usb_dfu_descriptor_hdr_get_length(const FuUsbDfuDescriptorHdr *st)
1027
0
{
1028
0
    g_return_val_if_fail(st != NULL, 0x0);
1029
0
    return st->buf->data[0];
1030
0
}
1031
/**
1032
 * fu_usb_dfu_descriptor_hdr_get_descriptor_type: (skip):
1033
 **/
1034
static FuUsbDescriptorKind
1035
fu_usb_dfu_descriptor_hdr_get_descriptor_type(const FuUsbDfuDescriptorHdr *st)
1036
0
{
1037
0
    g_return_val_if_fail(st != NULL, 0x0);
1038
0
    return st->buf->data[1];
1039
0
}
1040
/**
1041
 * fu_usb_dfu_descriptor_hdr_get_attributes: (skip):
1042
 **/
1043
guint8
1044
fu_usb_dfu_descriptor_hdr_get_attributes(const FuUsbDfuDescriptorHdr *st)
1045
0
{
1046
0
    g_return_val_if_fail(st != NULL, 0x0);
1047
0
    return st->buf->data[2];
1048
0
}
1049
/**
1050
 * fu_usb_dfu_descriptor_hdr_get_detach_timeout: (skip):
1051
 **/
1052
guint16
1053
fu_usb_dfu_descriptor_hdr_get_detach_timeout(const FuUsbDfuDescriptorHdr *st)
1054
0
{
1055
0
    g_return_val_if_fail(st != NULL, 0x0);
1056
0
    return fu_memread_uint16(st->buf->data + 3, G_LITTLE_ENDIAN);
1057
0
}
1058
/**
1059
 * fu_usb_dfu_descriptor_hdr_get_transfer_size: (skip):
1060
 **/
1061
guint16
1062
fu_usb_dfu_descriptor_hdr_get_transfer_size(const FuUsbDfuDescriptorHdr *st)
1063
0
{
1064
0
    g_return_val_if_fail(st != NULL, 0x0);
1065
0
    return fu_memread_uint16(st->buf->data + 5, G_LITTLE_ENDIAN);
1066
0
}
1067
/**
1068
 * fu_usb_dfu_descriptor_hdr_get_dfu_version: (skip):
1069
 **/
1070
guint16
1071
fu_usb_dfu_descriptor_hdr_get_dfu_version(const FuUsbDfuDescriptorHdr *st)
1072
0
{
1073
0
    g_return_val_if_fail(st != NULL, 0x0);
1074
0
    return fu_memread_uint16(st->buf->data + 7, G_LITTLE_ENDIAN);
1075
0
}
1076
1077
/* setters */
1078
/**
1079
 * fu_usb_dfu_descriptor_hdr_to_string: (skip):
1080
 **/
1081
static gchar *
1082
fu_usb_dfu_descriptor_hdr_to_string(const FuUsbDfuDescriptorHdr *st)
1083
0
{
1084
0
    g_autoptr(GString) str = g_string_new("FuUsbDfuDescriptorHdr:\n");
1085
0
    g_return_val_if_fail(st != NULL, NULL);
1086
0
    g_string_append_printf(str, "  length: 0x%x\n",
1087
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_length(st));
1088
0
    {
1089
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_dfu_descriptor_hdr_get_descriptor_type(st));
1090
0
        if (tmp != NULL) {
1091
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_dfu_descriptor_hdr_get_descriptor_type(st), tmp);
1092
0
        } else {
1093
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_dfu_descriptor_hdr_get_descriptor_type(st));
1094
0
        }
1095
0
    }
1096
0
    g_string_append_printf(str, "  attributes: 0x%x\n",
1097
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_attributes(st));
1098
0
    g_string_append_printf(str, "  detach_timeout: 0x%x\n",
1099
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_detach_timeout(st));
1100
0
    g_string_append_printf(str, "  transfer_size: 0x%x\n",
1101
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_transfer_size(st));
1102
0
    g_string_append_printf(str, "  dfu_version: 0x%x\n",
1103
0
                           (guint) fu_usb_dfu_descriptor_hdr_get_dfu_version(st));
1104
0
    if (str->len > 0)
1105
0
        g_string_set_size(str, str->len - 1);
1106
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1107
0
}
1108
static gboolean
1109
fu_usb_dfu_descriptor_hdr_validate_internal(FuUsbDfuDescriptorHdr *st, GError **error)
1110
0
{
1111
0
    g_return_val_if_fail(st != NULL, FALSE);
1112
0
    if (fu_usb_dfu_descriptor_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_HID) {
1113
0
        g_set_error(error,
1114
0
                    FWUPD_ERROR,
1115
0
                    FWUPD_ERROR_INVALID_DATA,
1116
0
                    "constant FuUsbDfuDescriptorHdr.descriptor_type was not valid, "
1117
0
                    "expected 'FU_USB_DESCRIPTOR_KIND_HID' and got 0x%x '%s'",
1118
0
                    fu_usb_dfu_descriptor_hdr_get_descriptor_type(st),
1119
0
                    fu_usb_descriptor_kind_to_string(fu_usb_dfu_descriptor_hdr_get_descriptor_type(st)));
1120
0
        return FALSE;
1121
0
    }
1122
0
    return TRUE;
1123
0
}
1124
static gboolean
1125
fu_usb_dfu_descriptor_hdr_parse_internal(FuUsbDfuDescriptorHdr *st, GError **error)
1126
0
{
1127
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
1128
0
        g_autofree gchar *str = fu_usb_dfu_descriptor_hdr_to_string(st);
1129
0
        g_debug("%s", str);
1130
0
    }
1131
0
    if (!fu_usb_dfu_descriptor_hdr_validate_internal(st, error))
1132
0
        return FALSE;
1133
0
    return TRUE;
1134
0
}
1135
1136
/**
1137
 * fu_usb_dfu_descriptor_hdr_parse: (skip):
1138
 **/
1139
static FuUsbDfuDescriptorHdr *
1140
fu_usb_dfu_descriptor_hdr_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
1141
0
{
1142
0
    g_autoptr(FuUsbDfuDescriptorHdr) st = fu_usb_dfu_descriptor_hdr_new_internal();
1143
0
    g_return_val_if_fail(buf != NULL, NULL);
1144
0
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
1145
0
    if (!fu_memchk_read(bufsz, offset, 9, error)) {
1146
0
        g_prefix_error_literal(error, "invalid struct FuUsbDfuDescriptorHdr: ");
1147
0
        return NULL;
1148
0
    }
1149
0
    st->buf = g_byte_array_new();
1150
0
    g_byte_array_append(st->buf, buf + offset, 9);
1151
0
    if (!fu_usb_dfu_descriptor_hdr_parse_internal(st, error))
1152
0
        return NULL;
1153
0
    return g_steal_pointer(&st);
1154
0
}
1155
/**
1156
 * fu_usb_dfu_descriptor_hdr_parse_bytes: (skip):
1157
 **/
1158
FuUsbDfuDescriptorHdr *
1159
fu_usb_dfu_descriptor_hdr_parse_bytes(GBytes *blob, gsize offset, GError **error)
1160
0
{
1161
0
    gsize bufsz = 0;
1162
0
    const guint8 *buf = g_bytes_get_data(blob, &bufsz);
1163
0
    return fu_usb_dfu_descriptor_hdr_parse(buf, bufsz, offset, error);
1164
0
}
1165
/**
1166
 * fu_usb_interface_hdr_ref: (skip):
1167
 **/
1168
FuUsbInterfaceHdr *
1169
fu_usb_interface_hdr_ref(FuUsbInterfaceHdr *st)
1170
0
{
1171
0
    g_return_val_if_fail(st != NULL, NULL);
1172
0
    st->refcount++;
1173
0
    return st;
1174
0
}
1175
/**
1176
 * fu_usb_interface_hdr_unref: (skip):
1177
 **/
1178
void
1179
fu_usb_interface_hdr_unref(FuUsbInterfaceHdr *st)
1180
0
{
1181
0
    g_return_if_fail(st != NULL);
1182
0
    if (st->refcount == 0) {
1183
0
        g_critical("FuUsbInterfaceHdr refcount already zero");
1184
0
        return;
1185
0
    }
1186
0
    if (--st->refcount > 0)
1187
0
        return;
1188
0
    if (st->buf != NULL)
1189
0
        g_byte_array_unref(st->buf);
1190
0
    g_free(st);
1191
0
}
1192
/**
1193
 * fu_usb_interface_hdr_new_internal: (skip):
1194
 **/
1195
static FuUsbInterfaceHdr *
1196
fu_usb_interface_hdr_new_internal(void)
1197
0
{
1198
0
    FuUsbInterfaceHdr *st = g_new0(FuUsbInterfaceHdr, 1);
1199
0
    st->refcount = 1;
1200
0
    return st;
1201
0
}
1202
1203
/* getters */
1204
/**
1205
 * fu_usb_interface_hdr_get_length: (skip):
1206
 **/
1207
guint8
1208
fu_usb_interface_hdr_get_length(const FuUsbInterfaceHdr *st)
1209
0
{
1210
0
    g_return_val_if_fail(st != NULL, 0x0);
1211
0
    return st->buf->data[0];
1212
0
}
1213
/**
1214
 * fu_usb_interface_hdr_get_descriptor_type: (skip):
1215
 **/
1216
static FuUsbDescriptorKind
1217
fu_usb_interface_hdr_get_descriptor_type(const FuUsbInterfaceHdr *st)
1218
0
{
1219
0
    g_return_val_if_fail(st != NULL, 0x0);
1220
0
    return st->buf->data[1];
1221
0
}
1222
/**
1223
 * fu_usb_interface_hdr_get_interface_number: (skip):
1224
 **/
1225
guint8
1226
fu_usb_interface_hdr_get_interface_number(const FuUsbInterfaceHdr *st)
1227
0
{
1228
0
    g_return_val_if_fail(st != NULL, 0x0);
1229
0
    return st->buf->data[2];
1230
0
}
1231
/**
1232
 * fu_usb_interface_hdr_get_alternate_setting: (skip):
1233
 **/
1234
guint8
1235
fu_usb_interface_hdr_get_alternate_setting(const FuUsbInterfaceHdr *st)
1236
0
{
1237
0
    g_return_val_if_fail(st != NULL, 0x0);
1238
0
    return st->buf->data[3];
1239
0
}
1240
/**
1241
 * fu_usb_interface_hdr_get_num_endpoints: (skip):
1242
 **/
1243
guint8
1244
fu_usb_interface_hdr_get_num_endpoints(const FuUsbInterfaceHdr *st)
1245
0
{
1246
0
    g_return_val_if_fail(st != NULL, 0x0);
1247
0
    return st->buf->data[4];
1248
0
}
1249
/**
1250
 * fu_usb_interface_hdr_get_interface_class: (skip):
1251
 **/
1252
FuUsbClass
1253
fu_usb_interface_hdr_get_interface_class(const FuUsbInterfaceHdr *st)
1254
0
{
1255
0
    g_return_val_if_fail(st != NULL, 0x0);
1256
0
    return st->buf->data[5];
1257
0
}
1258
/**
1259
 * fu_usb_interface_hdr_get_interface_sub_class: (skip):
1260
 **/
1261
guint8
1262
fu_usb_interface_hdr_get_interface_sub_class(const FuUsbInterfaceHdr *st)
1263
0
{
1264
0
    g_return_val_if_fail(st != NULL, 0x0);
1265
0
    return st->buf->data[6];
1266
0
}
1267
/**
1268
 * fu_usb_interface_hdr_get_interface_protocol: (skip):
1269
 **/
1270
guint8
1271
fu_usb_interface_hdr_get_interface_protocol(const FuUsbInterfaceHdr *st)
1272
0
{
1273
0
    g_return_val_if_fail(st != NULL, 0x0);
1274
0
    return st->buf->data[7];
1275
0
}
1276
/**
1277
 * fu_usb_interface_hdr_get_interface: (skip):
1278
 **/
1279
guint8
1280
fu_usb_interface_hdr_get_interface(const FuUsbInterfaceHdr *st)
1281
0
{
1282
0
    g_return_val_if_fail(st != NULL, 0x0);
1283
0
    return st->buf->data[8];
1284
0
}
1285
1286
/* setters */
1287
/**
1288
 * fu_usb_interface_hdr_to_string: (skip):
1289
 **/
1290
static gchar *
1291
fu_usb_interface_hdr_to_string(const FuUsbInterfaceHdr *st)
1292
0
{
1293
0
    g_autoptr(GString) str = g_string_new("FuUsbInterfaceHdr:\n");
1294
0
    g_return_val_if_fail(st != NULL, NULL);
1295
0
    g_string_append_printf(str, "  length: 0x%x\n",
1296
0
                           (guint) fu_usb_interface_hdr_get_length(st));
1297
0
    {
1298
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_interface_hdr_get_descriptor_type(st));
1299
0
        if (tmp != NULL) {
1300
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_interface_hdr_get_descriptor_type(st), tmp);
1301
0
        } else {
1302
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_interface_hdr_get_descriptor_type(st));
1303
0
        }
1304
0
    }
1305
0
    g_string_append_printf(str, "  interface_number: 0x%x\n",
1306
0
                           (guint) fu_usb_interface_hdr_get_interface_number(st));
1307
0
    g_string_append_printf(str, "  alternate_setting: 0x%x\n",
1308
0
                           (guint) fu_usb_interface_hdr_get_alternate_setting(st));
1309
0
    g_string_append_printf(str, "  num_endpoints: 0x%x\n",
1310
0
                           (guint) fu_usb_interface_hdr_get_num_endpoints(st));
1311
0
    {
1312
0
        const gchar *tmp = fu_usb_class_to_string(fu_usb_interface_hdr_get_interface_class(st));
1313
0
        if (tmp != NULL) {
1314
0
            g_string_append_printf(str, "  interface_class: 0x%x [%s]\n", (guint) fu_usb_interface_hdr_get_interface_class(st), tmp);
1315
0
        } else {
1316
0
            g_string_append_printf(str, "  interface_class: 0x%x\n", (guint) fu_usb_interface_hdr_get_interface_class(st));
1317
0
        }
1318
0
    }
1319
0
    g_string_append_printf(str, "  interface_sub_class: 0x%x\n",
1320
0
                           (guint) fu_usb_interface_hdr_get_interface_sub_class(st));
1321
0
    g_string_append_printf(str, "  interface_protocol: 0x%x\n",
1322
0
                           (guint) fu_usb_interface_hdr_get_interface_protocol(st));
1323
0
    g_string_append_printf(str, "  interface: 0x%x\n",
1324
0
                           (guint) fu_usb_interface_hdr_get_interface(st));
1325
0
    if (str->len > 0)
1326
0
        g_string_set_size(str, str->len - 1);
1327
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1328
0
}
1329
static gboolean
1330
fu_usb_interface_hdr_validate_internal(FuUsbInterfaceHdr *st, GError **error)
1331
0
{
1332
0
    g_return_val_if_fail(st != NULL, FALSE);
1333
0
    if (fu_usb_interface_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_INTERFACE) {
1334
0
        g_set_error(error,
1335
0
                    FWUPD_ERROR,
1336
0
                    FWUPD_ERROR_INVALID_DATA,
1337
0
                    "constant FuUsbInterfaceHdr.descriptor_type was not valid, "
1338
0
                    "expected 'FU_USB_DESCRIPTOR_KIND_INTERFACE' and got 0x%x '%s'",
1339
0
                    fu_usb_interface_hdr_get_descriptor_type(st),
1340
0
                    fu_usb_descriptor_kind_to_string(fu_usb_interface_hdr_get_descriptor_type(st)));
1341
0
        return FALSE;
1342
0
    }
1343
0
    return TRUE;
1344
0
}
1345
static gboolean
1346
fu_usb_interface_hdr_parse_internal(FuUsbInterfaceHdr *st, GError **error)
1347
0
{
1348
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
1349
0
        g_autofree gchar *str = fu_usb_interface_hdr_to_string(st);
1350
0
        g_debug("%s", str);
1351
0
    }
1352
0
    if (!fu_usb_interface_hdr_validate_internal(st, error))
1353
0
        return FALSE;
1354
0
    return TRUE;
1355
0
}
1356
/**
1357
 * fu_usb_interface_hdr_parse_stream: (skip):
1358
 **/
1359
FuUsbInterfaceHdr *
1360
fu_usb_interface_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
1361
0
{
1362
0
    g_autoptr(FuUsbInterfaceHdr) st = fu_usb_interface_hdr_new_internal();
1363
0
    st->buf = fu_input_stream_read_byte_array(stream, offset, 9, NULL, error);
1364
0
    if (st->buf == NULL) {
1365
0
        g_prefix_error(error, "FuUsbInterfaceHdr failed read of 0x%x: ", (guint) 9);
1366
0
        return NULL;
1367
0
    }
1368
0
    if (st->buf->len != 9) {
1369
0
        g_set_error(error,
1370
0
                    FWUPD_ERROR,
1371
0
                    FWUPD_ERROR_INVALID_DATA,
1372
0
                    "FuUsbInterfaceHdr requested 0x%x and got 0x%x",
1373
0
                    (guint) 9,
1374
0
                    (guint) st->buf->len);
1375
0
        return NULL;
1376
0
    }
1377
0
    if (!fu_usb_interface_hdr_parse_internal(st, error))
1378
0
        return NULL;
1379
0
    return g_steal_pointer(&st);
1380
0
}
1381
/**
1382
 * fu_usb_endpoint_hdr_ref: (skip):
1383
 **/
1384
FuUsbEndpointHdr *
1385
fu_usb_endpoint_hdr_ref(FuUsbEndpointHdr *st)
1386
0
{
1387
0
    g_return_val_if_fail(st != NULL, NULL);
1388
0
    st->refcount++;
1389
0
    return st;
1390
0
}
1391
/**
1392
 * fu_usb_endpoint_hdr_unref: (skip):
1393
 **/
1394
void
1395
fu_usb_endpoint_hdr_unref(FuUsbEndpointHdr *st)
1396
0
{
1397
0
    g_return_if_fail(st != NULL);
1398
0
    if (st->refcount == 0) {
1399
0
        g_critical("FuUsbEndpointHdr refcount already zero");
1400
0
        return;
1401
0
    }
1402
0
    if (--st->refcount > 0)
1403
0
        return;
1404
0
    if (st->buf != NULL)
1405
0
        g_byte_array_unref(st->buf);
1406
0
    g_free(st);
1407
0
}
1408
/**
1409
 * fu_usb_endpoint_hdr_new_internal: (skip):
1410
 **/
1411
static FuUsbEndpointHdr *
1412
fu_usb_endpoint_hdr_new_internal(void)
1413
0
{
1414
0
    FuUsbEndpointHdr *st = g_new0(FuUsbEndpointHdr, 1);
1415
0
    st->refcount = 1;
1416
0
    return st;
1417
0
}
1418
1419
/* getters */
1420
/**
1421
 * fu_usb_endpoint_hdr_get_length: (skip):
1422
 **/
1423
guint8
1424
fu_usb_endpoint_hdr_get_length(const FuUsbEndpointHdr *st)
1425
0
{
1426
0
    g_return_val_if_fail(st != NULL, 0x0);
1427
0
    return st->buf->data[0];
1428
0
}
1429
/**
1430
 * fu_usb_endpoint_hdr_get_descriptor_type: (skip):
1431
 **/
1432
FuUsbDescriptorKind
1433
fu_usb_endpoint_hdr_get_descriptor_type(const FuUsbEndpointHdr *st)
1434
0
{
1435
0
    g_return_val_if_fail(st != NULL, 0x0);
1436
0
    return st->buf->data[1];
1437
0
}
1438
/**
1439
 * fu_usb_endpoint_hdr_get_endpoint_address: (skip):
1440
 **/
1441
guint8
1442
fu_usb_endpoint_hdr_get_endpoint_address(const FuUsbEndpointHdr *st)
1443
0
{
1444
0
    g_return_val_if_fail(st != NULL, 0x0);
1445
0
    return st->buf->data[2];
1446
0
}
1447
/**
1448
 * fu_usb_endpoint_hdr_get_attributes: (skip):
1449
 **/
1450
guint8
1451
fu_usb_endpoint_hdr_get_attributes(const FuUsbEndpointHdr *st)
1452
0
{
1453
0
    g_return_val_if_fail(st != NULL, 0x0);
1454
0
    return st->buf->data[3];
1455
0
}
1456
/**
1457
 * fu_usb_endpoint_hdr_get_max_packet_size: (skip):
1458
 **/
1459
guint16
1460
fu_usb_endpoint_hdr_get_max_packet_size(const FuUsbEndpointHdr *st)
1461
0
{
1462
0
    g_return_val_if_fail(st != NULL, 0x0);
1463
0
    return fu_memread_uint16(st->buf->data + 4, G_LITTLE_ENDIAN);
1464
0
}
1465
/**
1466
 * fu_usb_endpoint_hdr_get_interval: (skip):
1467
 **/
1468
guint8
1469
fu_usb_endpoint_hdr_get_interval(const FuUsbEndpointHdr *st)
1470
0
{
1471
0
    g_return_val_if_fail(st != NULL, 0x0);
1472
0
    return st->buf->data[6];
1473
0
}
1474
1475
/* setters */
1476
/**
1477
 * fu_usb_endpoint_hdr_to_string: (skip):
1478
 **/
1479
static gchar *
1480
fu_usb_endpoint_hdr_to_string(const FuUsbEndpointHdr *st)
1481
0
{
1482
0
    g_autoptr(GString) str = g_string_new("FuUsbEndpointHdr:\n");
1483
0
    g_return_val_if_fail(st != NULL, NULL);
1484
0
    g_string_append_printf(str, "  length: 0x%x\n",
1485
0
                           (guint) fu_usb_endpoint_hdr_get_length(st));
1486
0
    {
1487
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_endpoint_hdr_get_descriptor_type(st));
1488
0
        if (tmp != NULL) {
1489
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_endpoint_hdr_get_descriptor_type(st), tmp);
1490
0
        } else {
1491
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_endpoint_hdr_get_descriptor_type(st));
1492
0
        }
1493
0
    }
1494
0
    g_string_append_printf(str, "  endpoint_address: 0x%x\n",
1495
0
                           (guint) fu_usb_endpoint_hdr_get_endpoint_address(st));
1496
0
    g_string_append_printf(str, "  attributes: 0x%x\n",
1497
0
                           (guint) fu_usb_endpoint_hdr_get_attributes(st));
1498
0
    g_string_append_printf(str, "  max_packet_size: 0x%x\n",
1499
0
                           (guint) fu_usb_endpoint_hdr_get_max_packet_size(st));
1500
0
    g_string_append_printf(str, "  interval: 0x%x\n",
1501
0
                           (guint) fu_usb_endpoint_hdr_get_interval(st));
1502
0
    if (str->len > 0)
1503
0
        g_string_set_size(str, str->len - 1);
1504
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1505
0
}
1506
static gboolean
1507
fu_usb_endpoint_hdr_validate_internal(FuUsbEndpointHdr *st, GError **error)
1508
0
{
1509
0
    g_return_val_if_fail(st != NULL, FALSE);
1510
0
    return TRUE;
1511
0
}
1512
static gboolean
1513
fu_usb_endpoint_hdr_parse_internal(FuUsbEndpointHdr *st, GError **error)
1514
0
{
1515
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
1516
0
        g_autofree gchar *str = fu_usb_endpoint_hdr_to_string(st);
1517
0
        g_debug("%s", str);
1518
0
    }
1519
0
    if (!fu_usb_endpoint_hdr_validate_internal(st, error))
1520
0
        return FALSE;
1521
0
    return TRUE;
1522
0
}
1523
/**
1524
 * fu_usb_endpoint_hdr_parse_stream: (skip):
1525
 **/
1526
FuUsbEndpointHdr *
1527
fu_usb_endpoint_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
1528
0
{
1529
0
    g_autoptr(FuUsbEndpointHdr) st = fu_usb_endpoint_hdr_new_internal();
1530
0
    st->buf = fu_input_stream_read_byte_array(stream, offset, 7, NULL, error);
1531
0
    if (st->buf == NULL) {
1532
0
        g_prefix_error(error, "FuUsbEndpointHdr failed read of 0x%x: ", (guint) 7);
1533
0
        return NULL;
1534
0
    }
1535
0
    if (st->buf->len != 7) {
1536
0
        g_set_error(error,
1537
0
                    FWUPD_ERROR,
1538
0
                    FWUPD_ERROR_INVALID_DATA,
1539
0
                    "FuUsbEndpointHdr requested 0x%x and got 0x%x",
1540
0
                    (guint) 7,
1541
0
                    (guint) st->buf->len);
1542
0
        return NULL;
1543
0
    }
1544
0
    if (!fu_usb_endpoint_hdr_parse_internal(st, error))
1545
0
        return NULL;
1546
0
    return g_steal_pointer(&st);
1547
0
}
1548
/**
1549
 * fu_usb_bos_hdr_ref: (skip):
1550
 **/
1551
FuUsbBosHdr *
1552
fu_usb_bos_hdr_ref(FuUsbBosHdr *st)
1553
0
{
1554
0
    g_return_val_if_fail(st != NULL, NULL);
1555
0
    st->refcount++;
1556
0
    return st;
1557
0
}
1558
/**
1559
 * fu_usb_bos_hdr_unref: (skip):
1560
 **/
1561
void
1562
fu_usb_bos_hdr_unref(FuUsbBosHdr *st)
1563
0
{
1564
0
    g_return_if_fail(st != NULL);
1565
0
    if (st->refcount == 0) {
1566
0
        g_critical("FuUsbBosHdr refcount already zero");
1567
0
        return;
1568
0
    }
1569
0
    if (--st->refcount > 0)
1570
0
        return;
1571
0
    if (st->buf != NULL)
1572
0
        g_byte_array_unref(st->buf);
1573
0
    g_free(st);
1574
0
}
1575
/**
1576
 * fu_usb_bos_hdr_new_internal: (skip):
1577
 **/
1578
static FuUsbBosHdr *
1579
fu_usb_bos_hdr_new_internal(void)
1580
0
{
1581
0
    FuUsbBosHdr *st = g_new0(FuUsbBosHdr, 1);
1582
0
    st->refcount = 1;
1583
0
    return st;
1584
0
}
1585
1586
/* getters */
1587
/**
1588
 * fu_usb_bos_hdr_get_length: (skip):
1589
 **/
1590
guint8
1591
fu_usb_bos_hdr_get_length(const FuUsbBosHdr *st)
1592
0
{
1593
0
    g_return_val_if_fail(st != NULL, 0x0);
1594
0
    return st->buf->data[0];
1595
0
}
1596
/**
1597
 * fu_usb_bos_hdr_get_descriptor_type: (skip):
1598
 **/
1599
FuUsbDescriptorKind
1600
fu_usb_bos_hdr_get_descriptor_type(const FuUsbBosHdr *st)
1601
0
{
1602
0
    g_return_val_if_fail(st != NULL, 0x0);
1603
0
    return st->buf->data[1];
1604
0
}
1605
/**
1606
 * fu_usb_bos_hdr_get_dev_capability_type: (skip):
1607
 **/
1608
guint8
1609
fu_usb_bos_hdr_get_dev_capability_type(const FuUsbBosHdr *st)
1610
0
{
1611
0
    g_return_val_if_fail(st != NULL, 0x0);
1612
0
    return st->buf->data[2];
1613
0
}
1614
1615
/* setters */
1616
/**
1617
 * fu_usb_bos_hdr_set_length: (skip):
1618
 **/
1619
void
1620
fu_usb_bos_hdr_set_length(FuUsbBosHdr *st, guint8 value)
1621
0
{
1622
0
    g_return_if_fail(st != NULL);
1623
0
    st->buf->data[0] = value;
1624
0
}
1625
/**
1626
 * fu_usb_bos_hdr_set_descriptor_type: (skip):
1627
 **/
1628
void
1629
fu_usb_bos_hdr_set_descriptor_type(FuUsbBosHdr *st, FuUsbDescriptorKind value)
1630
0
{
1631
0
    g_return_if_fail(st != NULL);
1632
0
    st->buf->data[1] = value;
1633
0
}
1634
/**
1635
 * fu_usb_bos_hdr_set_dev_capability_type: (skip):
1636
 **/
1637
void
1638
fu_usb_bos_hdr_set_dev_capability_type(FuUsbBosHdr *st, guint8 value)
1639
0
{
1640
0
    g_return_if_fail(st != NULL);
1641
0
    st->buf->data[2] = value;
1642
0
}
1643
/**
1644
 * fu_usb_bos_hdr_new: (skip):
1645
 **/
1646
FuUsbBosHdr *
1647
fu_usb_bos_hdr_new(void)
1648
0
{
1649
0
    FuUsbBosHdr *st = fu_usb_bos_hdr_new_internal();
1650
0
    st->buf = g_byte_array_sized_new(3);
1651
0
    fu_byte_array_set_size(st->buf, 3, 0x0);
1652
0
    fu_usb_bos_hdr_set_length(st, 3);
1653
0
    fu_usb_bos_hdr_set_descriptor_type(st, FU_USB_DESCRIPTOR_KIND_BOS);
1654
0
    return st;
1655
0
}
1656
/**
1657
 * fu_usb_bos_hdr_to_string: (skip):
1658
 **/
1659
static gchar *
1660
fu_usb_bos_hdr_to_string(const FuUsbBosHdr *st)
1661
0
{
1662
0
    g_autoptr(GString) str = g_string_new("FuUsbBosHdr:\n");
1663
0
    g_return_val_if_fail(st != NULL, NULL);
1664
0
    g_string_append_printf(str, "  length: 0x%x\n",
1665
0
                           (guint) fu_usb_bos_hdr_get_length(st));
1666
0
    {
1667
0
        const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_bos_hdr_get_descriptor_type(st));
1668
0
        if (tmp != NULL) {
1669
0
            g_string_append_printf(str, "  descriptor_type: 0x%x [%s]\n", (guint) fu_usb_bos_hdr_get_descriptor_type(st), tmp);
1670
0
        } else {
1671
0
            g_string_append_printf(str, "  descriptor_type: 0x%x\n", (guint) fu_usb_bos_hdr_get_descriptor_type(st));
1672
0
        }
1673
0
    }
1674
0
    g_string_append_printf(str, "  dev_capability_type: 0x%x\n",
1675
0
                           (guint) fu_usb_bos_hdr_get_dev_capability_type(st));
1676
0
    if (str->len > 0)
1677
0
        g_string_set_size(str, str->len - 1);
1678
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1679
0
}
1680
static gboolean
1681
fu_usb_bos_hdr_validate_internal(FuUsbBosHdr *st, GError **error)
1682
0
{
1683
0
    g_return_val_if_fail(st != NULL, FALSE);
1684
0
    return TRUE;
1685
0
}
1686
static gboolean
1687
fu_usb_bos_hdr_parse_internal(FuUsbBosHdr *st, GError **error)
1688
0
{
1689
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
1690
0
        g_autofree gchar *str = fu_usb_bos_hdr_to_string(st);
1691
0
        g_debug("%s", str);
1692
0
    }
1693
0
    if (!fu_usb_bos_hdr_validate_internal(st, error))
1694
0
        return FALSE;
1695
0
    return TRUE;
1696
0
}
1697
/**
1698
 * fu_usb_bos_hdr_parse_stream: (skip):
1699
 **/
1700
FuUsbBosHdr *
1701
fu_usb_bos_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
1702
0
{
1703
0
    g_autoptr(FuUsbBosHdr) st = fu_usb_bos_hdr_new_internal();
1704
0
    st->buf = fu_input_stream_read_byte_array(stream, offset, 3, NULL, error);
1705
0
    if (st->buf == NULL) {
1706
0
        g_prefix_error(error, "FuUsbBosHdr failed read of 0x%x: ", (guint) 3);
1707
0
        return NULL;
1708
0
    }
1709
0
    if (st->buf->len != 3) {
1710
0
        g_set_error(error,
1711
0
                    FWUPD_ERROR,
1712
0
                    FWUPD_ERROR_INVALID_DATA,
1713
0
                    "FuUsbBosHdr requested 0x%x and got 0x%x",
1714
0
                    (guint) 3,
1715
0
                    (guint) st->buf->len);
1716
0
        return NULL;
1717
0
    }
1718
0
    if (!fu_usb_bos_hdr_parse_internal(st, error))
1719
0
        return NULL;
1720
0
    return g_steal_pointer(&st);
1721
0
}