Coverage Report

Created: 2026-02-26 06:27

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