Coverage Report

Created: 2026-04-12 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/fu-zip-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-zip-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
 * fu_zip_compression_to_string:
24
 * @val: value, e.g. %FU_ZIP_COMPRESSION_SHRUNK
25
 *
26
 * Converts an enumerated value to a string.
27
 *
28
 * Returns: identifier string
29
 **/
30
const gchar *
31
fu_zip_compression_to_string(FuZipCompression val)
32
45
{
33
45
    if (val == FU_ZIP_COMPRESSION_NONE)
34
0
        return "none";
35
45
    if (val == FU_ZIP_COMPRESSION_SHRUNK)
36
1
        return "shrunk";
37
44
    if (val == FU_ZIP_COMPRESSION_IMPLODE)
38
1
        return "implode";
39
43
    if (val == FU_ZIP_COMPRESSION_TOKENIZE)
40
1
        return "tokenize";
41
42
    if (val == FU_ZIP_COMPRESSION_DEFLATE)
42
0
        return "deflate";
43
42
    if (val == FU_ZIP_COMPRESSION_DEFLATE64)
44
1
        return "deflate64";
45
41
    if (val == FU_ZIP_COMPRESSION_BZIP2)
46
1
        return "bzip2";
47
40
    if (val == FU_ZIP_COMPRESSION_LZMA)
48
1
        return "lzma";
49
39
    if (val == FU_ZIP_COMPRESSION_TERSE)
50
1
        return "terse";
51
38
    if (val == FU_ZIP_COMPRESSION_ZSTANDARD)
52
1
        return "zstandard";
53
37
    if (val == FU_ZIP_COMPRESSION_XZ)
54
1
        return "xz";
55
36
    return NULL;
56
37
}
57
/**
58
 * fu_zip_compression_from_string:
59
 * @val: (nullable): a string, e.g. `shrunk`
60
 *
61
 * Converts a string to an enumerated value.
62
 *
63
 * Returns: enumerated value
64
 **/
65
FuZipCompression
66
fu_zip_compression_from_string(const gchar *val)
67
0
{
68
0
    if (g_strcmp0(val, "none") == 0)
69
0
        return FU_ZIP_COMPRESSION_NONE;
70
0
    if (g_strcmp0(val, "shrunk") == 0)
71
0
        return FU_ZIP_COMPRESSION_SHRUNK;
72
0
    if (g_strcmp0(val, "implode") == 0)
73
0
        return FU_ZIP_COMPRESSION_IMPLODE;
74
0
    if (g_strcmp0(val, "tokenize") == 0)
75
0
        return FU_ZIP_COMPRESSION_TOKENIZE;
76
0
    if (g_strcmp0(val, "deflate") == 0)
77
0
        return FU_ZIP_COMPRESSION_DEFLATE;
78
0
    if (g_strcmp0(val, "deflate64") == 0)
79
0
        return FU_ZIP_COMPRESSION_DEFLATE64;
80
0
    if (g_strcmp0(val, "bzip2") == 0)
81
0
        return FU_ZIP_COMPRESSION_BZIP2;
82
0
    if (g_strcmp0(val, "lzma") == 0)
83
0
        return FU_ZIP_COMPRESSION_LZMA;
84
0
    if (g_strcmp0(val, "terse") == 0)
85
0
        return FU_ZIP_COMPRESSION_TERSE;
86
0
    if (g_strcmp0(val, "zstandard") == 0)
87
0
        return FU_ZIP_COMPRESSION_ZSTANDARD;
88
0
    if (g_strcmp0(val, "xz") == 0)
89
0
        return FU_ZIP_COMPRESSION_XZ;
90
0
    return FU_ZIP_COMPRESSION_NONE;
91
0
}
92
93
/**
94
 * fu_zip_flags_to_string:
95
 * @val: value, e.g. %FU_ZIP_FLAG_ENCRYPTED
96
 *
97
 * Converts an enumerated value to a string.
98
 *
99
 * Returns: identifier string
100
 *
101
 **/
102
static gchar *
103
fu_zip_flags_to_string(FuZipFlags val)
104
0
{
105
0
    const gchar *data[6] = {0};
106
0
    guint idx = 0;
107
0
    if (val == FU_ZIP_FLAG_NONE)
108
0
        return g_strdup("none");
109
0
    if (val & FU_ZIP_FLAG_ENCRYPTED)
110
0
        data[idx++] = "encrypted";
111
0
    if (val & FU_ZIP_FLAG_DEFLATE_HI)
112
0
        data[idx++] = "deflate-hi";
113
0
    if (val & FU_ZIP_FLAG_DEFLATE_LO)
114
0
        data[idx++] = "deflate-lo";
115
0
    if (val & FU_ZIP_FLAG_DATA_DESCRIPTOR)
116
0
        data[idx++] = "data-descriptor";
117
0
    if (val & FU_ZIP_FLAG_COMPRESSED_PATCHED)
118
0
        data[idx++] = "compressed-patched";
119
0
    return g_strjoinv(",", (gchar **)data);
120
0
}
121
122
/**
123
 * fu_zip_extra_header_id_to_string:
124
 * @val: value, e.g. %FU_ZIP_EXTRA_HEADER_ID_EXTENDED_TIMESTAMP
125
 *
126
 * Converts an enumerated value to a string.
127
 *
128
 * Returns: identifier string
129
 **/
130
static const gchar *
131
fu_zip_extra_header_id_to_string(FuZipExtraHeaderId val)
132
0
{
133
0
    if (val == FU_ZIP_EXTRA_HEADER_ID_NONE)
134
0
        return "none";
135
0
    if (val == FU_ZIP_EXTRA_HEADER_ID_EXTENDED_TIMESTAMP)
136
0
        return "extended-timestamp";
137
0
    if (val == FU_ZIP_EXTRA_HEADER_ID_INFO_ZIP_UNIX)
138
0
        return "info-zip-unix";
139
0
    if (val == FU_ZIP_EXTRA_HEADER_ID_INFO_ZIP_UNIX_UID_GID)
140
0
        return "info-zip-unix-uid-gid";
141
0
    return NULL;
142
0
}
143
/**
144
 * fu_struct_zip_extra_hdr_ref: (skip):
145
 **/
146
FuStructZipExtraHdr *
147
fu_struct_zip_extra_hdr_ref(FuStructZipExtraHdr *st)
148
0
{
149
0
    g_return_val_if_fail(st != NULL, NULL);
150
0
    st->refcount++;
151
0
    return st;
152
0
}
153
/**
154
 * fu_struct_zip_extra_hdr_unref: (skip):
155
 **/
156
void
157
fu_struct_zip_extra_hdr_unref(FuStructZipExtraHdr *st)
158
2.29k
{
159
2.29k
    g_return_if_fail(st != NULL);
160
2.29k
    if (st->refcount == 0) {
161
0
        g_critical("FuStructZipExtraHdr refcount already zero");
162
0
        return;
163
0
    }
164
2.29k
    if (--st->refcount > 0)
165
0
        return;
166
2.29k
    if (st->buf != NULL)
167
2.23k
        g_byte_array_unref(st->buf);
168
2.29k
    g_free(st);
169
2.29k
}
170
/**
171
 * fu_struct_zip_extra_hdr_new_internal: (skip):
172
 **/
173
static FuStructZipExtraHdr *
174
fu_struct_zip_extra_hdr_new_internal(void)
175
2.29k
{
176
2.29k
    FuStructZipExtraHdr *st = g_new0(FuStructZipExtraHdr, 1);
177
2.29k
    st->refcount = 1;
178
2.29k
    return st;
179
2.29k
}
180
181
/* getters */
182
/**
183
 * fu_struct_zip_extra_hdr_get_header_id: (skip):
184
 **/
185
FuZipExtraHeaderId
186
fu_struct_zip_extra_hdr_get_header_id(const FuStructZipExtraHdr *st)
187
0
{
188
0
    g_return_val_if_fail(st != NULL, 0x0);
189
0
    return fu_memread_uint16(st->buf->data + 0, G_LITTLE_ENDIAN);
190
0
}
191
/**
192
 * fu_struct_zip_extra_hdr_get_datasz: (skip):
193
 **/
194
guint16
195
fu_struct_zip_extra_hdr_get_datasz(const FuStructZipExtraHdr *st)
196
2.22k
{
197
2.22k
    g_return_val_if_fail(st != NULL, 0x0);
198
2.22k
    return fu_memread_uint16(st->buf->data + 2, G_LITTLE_ENDIAN);
199
2.22k
}
200
201
/* setters */
202
/**
203
 * fu_struct_zip_extra_hdr_set_header_id: (skip):
204
 **/
205
void
206
fu_struct_zip_extra_hdr_set_header_id(FuStructZipExtraHdr *st, FuZipExtraHeaderId value)
207
0
{
208
0
    g_return_if_fail(st != NULL);
209
0
    fu_memwrite_uint16(st->buf->data + 0, value, G_LITTLE_ENDIAN);
210
0
}
211
/**
212
 * fu_struct_zip_extra_hdr_set_datasz: (skip):
213
 **/
214
void
215
fu_struct_zip_extra_hdr_set_datasz(FuStructZipExtraHdr *st, guint16 value)
216
0
{
217
0
    g_return_if_fail(st != NULL);
218
0
    fu_memwrite_uint16(st->buf->data + 2, value, G_LITTLE_ENDIAN);
219
0
}
220
/**
221
 * fu_struct_zip_extra_hdr_new: (skip):
222
 **/
223
FuStructZipExtraHdr *
224
fu_struct_zip_extra_hdr_new(void)
225
0
{
226
0
    FuStructZipExtraHdr *st = fu_struct_zip_extra_hdr_new_internal();
227
0
    st->buf = g_byte_array_sized_new(4);
228
0
    fu_byte_array_set_size(st->buf, 4, 0x0);
229
0
    return st;
230
0
}
231
/**
232
 * fu_struct_zip_extra_hdr_to_string: (skip):
233
 **/
234
static gchar *
235
fu_struct_zip_extra_hdr_to_string(const FuStructZipExtraHdr *st)
236
0
{
237
0
    g_autoptr(GString) str = g_string_new("FuStructZipExtraHdr:\n");
238
0
    g_return_val_if_fail(st != NULL, NULL);
239
0
    {
240
0
        const gchar *tmp = fu_zip_extra_header_id_to_string(fu_struct_zip_extra_hdr_get_header_id(st));
241
0
        if (tmp != NULL) {
242
0
            g_string_append_printf(str, "  header_id: 0x%x [%s]\n", (guint) fu_struct_zip_extra_hdr_get_header_id(st), tmp);
243
0
        } else {
244
0
            g_string_append_printf(str, "  header_id: 0x%x\n", (guint) fu_struct_zip_extra_hdr_get_header_id(st));
245
0
        }
246
0
    }
247
0
    g_string_append_printf(str, "  datasz: 0x%x\n",
248
0
                           (guint) fu_struct_zip_extra_hdr_get_datasz(st));
249
0
    if (str->len > 0)
250
0
        g_string_set_size(str, str->len - 1);
251
0
    return g_string_free(g_steal_pointer(&str), FALSE);
252
0
}
253
static gboolean
254
fu_struct_zip_extra_hdr_validate_internal(FuStructZipExtraHdr *st, GError **error)
255
2.22k
{
256
2.22k
    g_return_val_if_fail(st != NULL, FALSE);
257
2.22k
    return TRUE;
258
2.22k
}
259
static gboolean
260
fu_struct_zip_extra_hdr_parse_internal(FuStructZipExtraHdr *st, GError **error)
261
2.22k
{
262
2.22k
    if (g_log_get_debug_enabled()) {
263
0
        g_autofree gchar *str = fu_struct_zip_extra_hdr_to_string(st);
264
0
        g_debug("%s", str);
265
0
    }
266
2.22k
    if (!fu_struct_zip_extra_hdr_validate_internal(st, error))
267
0
        return FALSE;
268
2.22k
    return TRUE;
269
2.22k
}
270
/**
271
 * fu_struct_zip_extra_hdr_parse_stream: (skip):
272
 **/
273
FuStructZipExtraHdr *
274
fu_struct_zip_extra_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
275
2.29k
{
276
2.29k
    g_autoptr(FuStructZipExtraHdr) st = fu_struct_zip_extra_hdr_new_internal();
277
2.29k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 4, NULL, error);
278
2.29k
    if (st->buf == NULL) {
279
61
        g_prefix_error(error, "FuStructZipExtraHdr failed read of 0x%x: ", (guint) 4);
280
61
        return NULL;
281
61
    }
282
2.23k
    if (st->buf->len != 4) {
283
4
        g_set_error(error,
284
4
                    FWUPD_ERROR,
285
4
                    FWUPD_ERROR_INVALID_DATA,
286
4
                    "FuStructZipExtraHdr requested 0x%x and got 0x%x",
287
4
                    (guint) 4,
288
4
                    (guint) st->buf->len);
289
4
        return NULL;
290
4
    }
291
2.22k
    if (!fu_struct_zip_extra_hdr_parse_internal(st, error))
292
0
        return NULL;
293
2.22k
    return g_steal_pointer(&st);
294
2.22k
}
295
/**
296
 * fu_struct_zip_cdfh_ref: (skip):
297
 **/
298
FuStructZipCdfh *
299
fu_struct_zip_cdfh_ref(FuStructZipCdfh *st)
300
0
{
301
0
    g_return_val_if_fail(st != NULL, NULL);
302
0
    st->refcount++;
303
0
    return st;
304
0
}
305
/**
306
 * fu_struct_zip_cdfh_unref: (skip):
307
 **/
308
void
309
fu_struct_zip_cdfh_unref(FuStructZipCdfh *st)
310
10.3k
{
311
10.3k
    g_return_if_fail(st != NULL);
312
10.3k
    if (st->refcount == 0) {
313
0
        g_critical("FuStructZipCdfh refcount already zero");
314
0
        return;
315
0
    }
316
10.3k
    if (--st->refcount > 0)
317
0
        return;
318
10.3k
    if (st->buf != NULL)
319
10.3k
        g_byte_array_unref(st->buf);
320
10.3k
    g_free(st);
321
10.3k
}
322
/**
323
 * fu_struct_zip_cdfh_new_internal: (skip):
324
 **/
325
static FuStructZipCdfh *
326
fu_struct_zip_cdfh_new_internal(void)
327
10.3k
{
328
10.3k
    FuStructZipCdfh *st = g_new0(FuStructZipCdfh, 1);
329
10.3k
    st->refcount = 1;
330
10.3k
    return st;
331
10.3k
}
332
333
/* getters */
334
/**
335
 * fu_struct_zip_cdfh_get_magic: (skip):
336
 **/
337
static gchar *
338
fu_struct_zip_cdfh_get_magic(const FuStructZipCdfh *st)
339
89
{
340
89
    g_return_val_if_fail(st != NULL, NULL);
341
89
    return fu_memstrsafe(st->buf->data, st->buf->len, 0, 4, NULL);
342
89
}
343
/**
344
 * fu_struct_zip_cdfh_get_version_generated: (skip):
345
 **/
346
guint16
347
fu_struct_zip_cdfh_get_version_generated(const FuStructZipCdfh *st)
348
0
{
349
0
    g_return_val_if_fail(st != NULL, 0x0);
350
0
    return fu_memread_uint16(st->buf->data + 4, G_LITTLE_ENDIAN);
351
0
}
352
/**
353
 * fu_struct_zip_cdfh_get_version_extract: (skip):
354
 **/
355
guint16
356
fu_struct_zip_cdfh_get_version_extract(const FuStructZipCdfh *st)
357
0
{
358
0
    g_return_val_if_fail(st != NULL, 0x0);
359
0
    return fu_memread_uint16(st->buf->data + 6, G_LITTLE_ENDIAN);
360
0
}
361
/**
362
 * fu_struct_zip_cdfh_get_flags: (skip):
363
 **/
364
FuZipFlags
365
fu_struct_zip_cdfh_get_flags(const FuStructZipCdfh *st)
366
9.74k
{
367
9.74k
    g_return_val_if_fail(st != NULL, 0x0);
368
9.74k
    return fu_memread_uint16(st->buf->data + 8, G_LITTLE_ENDIAN);
369
9.74k
}
370
/**
371
 * fu_struct_zip_cdfh_get_compression: (skip):
372
 **/
373
FuZipCompression
374
fu_struct_zip_cdfh_get_compression(const FuStructZipCdfh *st)
375
0
{
376
0
    g_return_val_if_fail(st != NULL, 0x0);
377
0
    return fu_memread_uint16(st->buf->data + 10, G_LITTLE_ENDIAN);
378
0
}
379
/**
380
 * fu_struct_zip_cdfh_get_file_time: (skip):
381
 **/
382
guint16
383
fu_struct_zip_cdfh_get_file_time(const FuStructZipCdfh *st)
384
0
{
385
0
    g_return_val_if_fail(st != NULL, 0x0);
386
0
    return fu_memread_uint16(st->buf->data + 12, G_LITTLE_ENDIAN);
387
0
}
388
/**
389
 * fu_struct_zip_cdfh_get_file_date: (skip):
390
 **/
391
guint16
392
fu_struct_zip_cdfh_get_file_date(const FuStructZipCdfh *st)
393
0
{
394
0
    g_return_val_if_fail(st != NULL, 0x0);
395
0
    return fu_memread_uint16(st->buf->data + 14, G_LITTLE_ENDIAN);
396
0
}
397
/**
398
 * fu_struct_zip_cdfh_get_uncompressed_crc: (skip):
399
 **/
400
guint32
401
fu_struct_zip_cdfh_get_uncompressed_crc(const FuStructZipCdfh *st)
402
345
{
403
345
    g_return_val_if_fail(st != NULL, 0x0);
404
345
    return fu_memread_uint32(st->buf->data + 16, G_LITTLE_ENDIAN);
405
345
}
406
/**
407
 * fu_struct_zip_cdfh_get_compressed_size: (skip):
408
 **/
409
guint32
410
fu_struct_zip_cdfh_get_compressed_size(const FuStructZipCdfh *st)
411
850
{
412
850
    g_return_val_if_fail(st != NULL, 0x0);
413
850
    return fu_memread_uint32(st->buf->data + 20, G_LITTLE_ENDIAN);
414
850
}
415
/**
416
 * fu_struct_zip_cdfh_get_uncompressed_size: (skip):
417
 **/
418
guint32
419
fu_struct_zip_cdfh_get_uncompressed_size(const FuStructZipCdfh *st)
420
92
{
421
92
    g_return_val_if_fail(st != NULL, 0x0);
422
92
    return fu_memread_uint32(st->buf->data + 24, G_LITTLE_ENDIAN);
423
92
}
424
/**
425
 * fu_struct_zip_cdfh_get_filename_size: (skip):
426
 **/
427
guint16
428
fu_struct_zip_cdfh_get_filename_size(const FuStructZipCdfh *st)
429
9.15k
{
430
9.15k
    g_return_val_if_fail(st != NULL, 0x0);
431
9.15k
    return fu_memread_uint16(st->buf->data + 28, G_LITTLE_ENDIAN);
432
9.15k
}
433
/**
434
 * fu_struct_zip_cdfh_get_extra_size: (skip):
435
 **/
436
guint16
437
fu_struct_zip_cdfh_get_extra_size(const FuStructZipCdfh *st)
438
18.2k
{
439
18.2k
    g_return_val_if_fail(st != NULL, 0x0);
440
18.2k
    return fu_memread_uint16(st->buf->data + 30, G_LITTLE_ENDIAN);
441
18.2k
}
442
/**
443
 * fu_struct_zip_cdfh_get_comment_size: (skip):
444
 **/
445
guint16
446
fu_struct_zip_cdfh_get_comment_size(const FuStructZipCdfh *st)
447
9.12k
{
448
9.12k
    g_return_val_if_fail(st != NULL, 0x0);
449
9.12k
    return fu_memread_uint16(st->buf->data + 32, G_LITTLE_ENDIAN);
450
9.12k
}
451
/**
452
 * fu_struct_zip_cdfh_get_disk_number: (skip):
453
 **/
454
guint16
455
fu_struct_zip_cdfh_get_disk_number(const FuStructZipCdfh *st)
456
0
{
457
0
    g_return_val_if_fail(st != NULL, 0x0);
458
0
    return fu_memread_uint16(st->buf->data + 34, G_LITTLE_ENDIAN);
459
0
}
460
/**
461
 * fu_struct_zip_cdfh_get_internal_attrs: (skip):
462
 **/
463
guint16
464
fu_struct_zip_cdfh_get_internal_attrs(const FuStructZipCdfh *st)
465
0
{
466
0
    g_return_val_if_fail(st != NULL, 0x0);
467
0
    return fu_memread_uint16(st->buf->data + 36, G_LITTLE_ENDIAN);
468
0
}
469
/**
470
 * fu_struct_zip_cdfh_get_external_attrs: (skip):
471
 **/
472
guint32
473
fu_struct_zip_cdfh_get_external_attrs(const FuStructZipCdfh *st)
474
0
{
475
0
    g_return_val_if_fail(st != NULL, 0x0);
476
0
    return fu_memread_uint32(st->buf->data + 38, G_LITTLE_ENDIAN);
477
0
}
478
/**
479
 * fu_struct_zip_cdfh_get_offset_lfh: (skip):
480
 **/
481
guint32
482
fu_struct_zip_cdfh_get_offset_lfh(const FuStructZipCdfh *st)
483
9.73k
{
484
9.73k
    g_return_val_if_fail(st != NULL, 0x0);
485
9.73k
    return fu_memread_uint32(st->buf->data + 42, G_LITTLE_ENDIAN);
486
9.73k
}
487
488
/* setters */
489
/**
490
 * fu_struct_zip_cdfh_set_magic: (skip):
491
 **/
492
static gboolean
493
fu_struct_zip_cdfh_set_magic(FuStructZipCdfh *st, const gchar *value, GError **error)
494
478
{
495
478
    gsize len;
496
478
    g_return_val_if_fail(st != NULL, FALSE);
497
478
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
498
478
    if (value == NULL) {
499
0
        memset(st->buf->data + 0, 0x0, 4);
500
0
        return TRUE;
501
0
    }
502
478
    len = strlen(value);
503
478
    if (len > 4) {
504
0
        g_set_error(error,
505
0
                    FWUPD_ERROR,
506
0
                    FWUPD_ERROR_INVALID_DATA,
507
0
                    "string '%s' (0x%x bytes) does not fit in FuStructZipCdfh.magic (0x%x bytes)",
508
0
                    value, (guint) len, (guint) 4);
509
0
        return FALSE;
510
0
    }
511
478
    return fu_memcpy_safe(st->buf->data, st->buf->len, 0, (const guint8 *)value, len, 0x0, len, error);
512
478
}
513
/**
514
 * fu_struct_zip_cdfh_set_version_generated: (skip):
515
 **/
516
void
517
fu_struct_zip_cdfh_set_version_generated(FuStructZipCdfh *st, guint16 value)
518
478
{
519
478
    g_return_if_fail(st != NULL);
520
478
    fu_memwrite_uint16(st->buf->data + 4, value, G_LITTLE_ENDIAN);
521
478
}
522
/**
523
 * fu_struct_zip_cdfh_set_version_extract: (skip):
524
 **/
525
void
526
fu_struct_zip_cdfh_set_version_extract(FuStructZipCdfh *st, guint16 value)
527
478
{
528
478
    g_return_if_fail(st != NULL);
529
478
    fu_memwrite_uint16(st->buf->data + 6, value, G_LITTLE_ENDIAN);
530
478
}
531
/**
532
 * fu_struct_zip_cdfh_set_flags: (skip):
533
 **/
534
void
535
fu_struct_zip_cdfh_set_flags(FuStructZipCdfh *st, FuZipFlags value)
536
0
{
537
0
    g_return_if_fail(st != NULL);
538
0
    fu_memwrite_uint16(st->buf->data + 8, value, G_LITTLE_ENDIAN);
539
0
}
540
/**
541
 * fu_struct_zip_cdfh_set_compression: (skip):
542
 **/
543
void
544
fu_struct_zip_cdfh_set_compression(FuStructZipCdfh *st, FuZipCompression value)
545
478
{
546
478
    g_return_if_fail(st != NULL);
547
478
    fu_memwrite_uint16(st->buf->data + 10, value, G_LITTLE_ENDIAN);
548
478
}
549
/**
550
 * fu_struct_zip_cdfh_set_file_time: (skip):
551
 **/
552
void
553
fu_struct_zip_cdfh_set_file_time(FuStructZipCdfh *st, guint16 value)
554
0
{
555
0
    g_return_if_fail(st != NULL);
556
0
    fu_memwrite_uint16(st->buf->data + 12, value, G_LITTLE_ENDIAN);
557
0
}
558
/**
559
 * fu_struct_zip_cdfh_set_file_date: (skip):
560
 **/
561
void
562
fu_struct_zip_cdfh_set_file_date(FuStructZipCdfh *st, guint16 value)
563
0
{
564
0
    g_return_if_fail(st != NULL);
565
0
    fu_memwrite_uint16(st->buf->data + 14, value, G_LITTLE_ENDIAN);
566
0
}
567
/**
568
 * fu_struct_zip_cdfh_set_uncompressed_crc: (skip):
569
 **/
570
void
571
fu_struct_zip_cdfh_set_uncompressed_crc(FuStructZipCdfh *st, guint32 value)
572
478
{
573
478
    g_return_if_fail(st != NULL);
574
478
    fu_memwrite_uint32(st->buf->data + 16, value, G_LITTLE_ENDIAN);
575
478
}
576
/**
577
 * fu_struct_zip_cdfh_set_compressed_size: (skip):
578
 **/
579
void
580
fu_struct_zip_cdfh_set_compressed_size(FuStructZipCdfh *st, guint32 value)
581
478
{
582
478
    g_return_if_fail(st != NULL);
583
478
    fu_memwrite_uint32(st->buf->data + 20, value, G_LITTLE_ENDIAN);
584
478
}
585
/**
586
 * fu_struct_zip_cdfh_set_uncompressed_size: (skip):
587
 **/
588
void
589
fu_struct_zip_cdfh_set_uncompressed_size(FuStructZipCdfh *st, guint32 value)
590
478
{
591
478
    g_return_if_fail(st != NULL);
592
478
    fu_memwrite_uint32(st->buf->data + 24, value, G_LITTLE_ENDIAN);
593
478
}
594
/**
595
 * fu_struct_zip_cdfh_set_filename_size: (skip):
596
 **/
597
void
598
fu_struct_zip_cdfh_set_filename_size(FuStructZipCdfh *st, guint16 value)
599
478
{
600
478
    g_return_if_fail(st != NULL);
601
478
    fu_memwrite_uint16(st->buf->data + 28, value, G_LITTLE_ENDIAN);
602
478
}
603
/**
604
 * fu_struct_zip_cdfh_set_extra_size: (skip):
605
 **/
606
void
607
fu_struct_zip_cdfh_set_extra_size(FuStructZipCdfh *st, guint16 value)
608
0
{
609
0
    g_return_if_fail(st != NULL);
610
0
    fu_memwrite_uint16(st->buf->data + 30, value, G_LITTLE_ENDIAN);
611
0
}
612
/**
613
 * fu_struct_zip_cdfh_set_comment_size: (skip):
614
 **/
615
void
616
fu_struct_zip_cdfh_set_comment_size(FuStructZipCdfh *st, guint16 value)
617
0
{
618
0
    g_return_if_fail(st != NULL);
619
0
    fu_memwrite_uint16(st->buf->data + 32, value, G_LITTLE_ENDIAN);
620
0
}
621
/**
622
 * fu_struct_zip_cdfh_set_disk_number: (skip):
623
 **/
624
void
625
fu_struct_zip_cdfh_set_disk_number(FuStructZipCdfh *st, guint16 value)
626
0
{
627
0
    g_return_if_fail(st != NULL);
628
0
    fu_memwrite_uint16(st->buf->data + 34, value, G_LITTLE_ENDIAN);
629
0
}
630
/**
631
 * fu_struct_zip_cdfh_set_internal_attrs: (skip):
632
 **/
633
void
634
fu_struct_zip_cdfh_set_internal_attrs(FuStructZipCdfh *st, guint16 value)
635
0
{
636
0
    g_return_if_fail(st != NULL);
637
0
    fu_memwrite_uint16(st->buf->data + 36, value, G_LITTLE_ENDIAN);
638
0
}
639
/**
640
 * fu_struct_zip_cdfh_set_external_attrs: (skip):
641
 **/
642
void
643
fu_struct_zip_cdfh_set_external_attrs(FuStructZipCdfh *st, guint32 value)
644
0
{
645
0
    g_return_if_fail(st != NULL);
646
0
    fu_memwrite_uint32(st->buf->data + 38, value, G_LITTLE_ENDIAN);
647
0
}
648
/**
649
 * fu_struct_zip_cdfh_set_offset_lfh: (skip):
650
 **/
651
void
652
fu_struct_zip_cdfh_set_offset_lfh(FuStructZipCdfh *st, guint32 value)
653
478
{
654
478
    g_return_if_fail(st != NULL);
655
478
    fu_memwrite_uint32(st->buf->data + 42, value, G_LITTLE_ENDIAN);
656
478
}
657
/**
658
 * fu_struct_zip_cdfh_new: (skip):
659
 **/
660
FuStructZipCdfh *
661
fu_struct_zip_cdfh_new(void)
662
478
{
663
478
    FuStructZipCdfh *st = fu_struct_zip_cdfh_new_internal();
664
478
    st->buf = g_byte_array_sized_new(46);
665
478
    fu_byte_array_set_size(st->buf, 46, 0x0);
666
478
    fu_struct_zip_cdfh_set_magic(st, "PK\x01\x02", NULL);
667
478
    fu_struct_zip_cdfh_set_version_generated(st, 0xA);
668
478
    fu_struct_zip_cdfh_set_version_extract(st, 0xA);
669
478
    return st;
670
478
}
671
/**
672
 * fu_struct_zip_cdfh_to_string: (skip):
673
 **/
674
static gchar *
675
fu_struct_zip_cdfh_to_string(const FuStructZipCdfh *st)
676
0
{
677
0
    g_autoptr(GString) str = g_string_new("FuStructZipCdfh:\n");
678
0
    g_return_val_if_fail(st != NULL, NULL);
679
0
    {
680
0
        g_autofree gchar *tmp = fu_struct_zip_cdfh_get_magic(st);
681
0
        if (tmp != NULL)
682
0
            g_string_append_printf(str, "  magic: %s\n", tmp);
683
0
    }
684
0
    g_string_append_printf(str, "  version_generated: 0x%x\n",
685
0
                           (guint) fu_struct_zip_cdfh_get_version_generated(st));
686
0
    g_string_append_printf(str, "  version_extract: 0x%x\n",
687
0
                           (guint) fu_struct_zip_cdfh_get_version_extract(st));
688
0
    {
689
0
        g_autofree gchar *tmp = fu_zip_flags_to_string(fu_struct_zip_cdfh_get_flags(st));
690
0
        if (tmp != NULL) {
691
0
            g_string_append_printf(str, "  flags: 0x%x [%s]\n", (guint) fu_struct_zip_cdfh_get_flags(st), tmp);
692
0
        } else {
693
0
            g_string_append_printf(str, "  flags: 0x%x\n", (guint) fu_struct_zip_cdfh_get_flags(st));
694
0
        }
695
0
    }
696
0
    {
697
0
        const gchar *tmp = fu_zip_compression_to_string(fu_struct_zip_cdfh_get_compression(st));
698
0
        if (tmp != NULL) {
699
0
            g_string_append_printf(str, "  compression: 0x%x [%s]\n", (guint) fu_struct_zip_cdfh_get_compression(st), tmp);
700
0
        } else {
701
0
            g_string_append_printf(str, "  compression: 0x%x\n", (guint) fu_struct_zip_cdfh_get_compression(st));
702
0
        }
703
0
    }
704
0
    g_string_append_printf(str, "  file_time: 0x%x\n",
705
0
                           (guint) fu_struct_zip_cdfh_get_file_time(st));
706
0
    g_string_append_printf(str, "  file_date: 0x%x\n",
707
0
                           (guint) fu_struct_zip_cdfh_get_file_date(st));
708
0
    g_string_append_printf(str, "  uncompressed_crc: 0x%x\n",
709
0
                           (guint) fu_struct_zip_cdfh_get_uncompressed_crc(st));
710
0
    g_string_append_printf(str, "  compressed_size: 0x%x\n",
711
0
                           (guint) fu_struct_zip_cdfh_get_compressed_size(st));
712
0
    g_string_append_printf(str, "  uncompressed_size: 0x%x\n",
713
0
                           (guint) fu_struct_zip_cdfh_get_uncompressed_size(st));
714
0
    g_string_append_printf(str, "  filename_size: 0x%x\n",
715
0
                           (guint) fu_struct_zip_cdfh_get_filename_size(st));
716
0
    g_string_append_printf(str, "  extra_size: 0x%x\n",
717
0
                           (guint) fu_struct_zip_cdfh_get_extra_size(st));
718
0
    g_string_append_printf(str, "  comment_size: 0x%x\n",
719
0
                           (guint) fu_struct_zip_cdfh_get_comment_size(st));
720
0
    g_string_append_printf(str, "  disk_number: 0x%x\n",
721
0
                           (guint) fu_struct_zip_cdfh_get_disk_number(st));
722
0
    g_string_append_printf(str, "  internal_attrs: 0x%x\n",
723
0
                           (guint) fu_struct_zip_cdfh_get_internal_attrs(st));
724
0
    g_string_append_printf(str, "  external_attrs: 0x%x\n",
725
0
                           (guint) fu_struct_zip_cdfh_get_external_attrs(st));
726
0
    g_string_append_printf(str, "  offset_lfh: 0x%x\n",
727
0
                           (guint) fu_struct_zip_cdfh_get_offset_lfh(st));
728
0
    if (str->len > 0)
729
0
        g_string_set_size(str, str->len - 1);
730
0
    return g_string_free(g_steal_pointer(&str), FALSE);
731
0
}
732
static gboolean
733
fu_struct_zip_cdfh_validate_internal(FuStructZipCdfh *st, GError **error)
734
9.83k
{
735
9.83k
    g_return_val_if_fail(st != NULL, FALSE);
736
9.83k
    if (strncmp((const gchar *) (st->buf->data + 0), "PK\x01\x02", 4) != 0) {
737
89
        g_autofree gchar *str = fu_struct_zip_cdfh_get_magic(st);
738
89
        g_set_error(error,
739
89
                    FWUPD_ERROR,
740
89
                    FWUPD_ERROR_INVALID_DATA,
741
89
                    "constant FuStructZipCdfh.magic was not valid, "
742
89
                    "expected 'PK\x01\x02' and got '%s'",
743
89
                    str);
744
89
        return FALSE;
745
89
    }
746
9.74k
    return TRUE;
747
9.83k
}
748
static gboolean
749
fu_struct_zip_cdfh_parse_internal(FuStructZipCdfh *st, GError **error)
750
9.83k
{
751
9.83k
    if (g_log_get_debug_enabled()) {
752
0
        g_autofree gchar *str = fu_struct_zip_cdfh_to_string(st);
753
0
        g_debug("%s", str);
754
0
    }
755
9.83k
    if (!fu_struct_zip_cdfh_validate_internal(st, error))
756
89
        return FALSE;
757
9.74k
    return TRUE;
758
9.83k
}
759
/**
760
 * fu_struct_zip_cdfh_parse_stream: (skip):
761
 **/
762
FuStructZipCdfh *
763
fu_struct_zip_cdfh_parse_stream(GInputStream *stream, gsize offset, GError **error)
764
9.91k
{
765
9.91k
    g_autoptr(FuStructZipCdfh) st = fu_struct_zip_cdfh_new_internal();
766
9.91k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 46, NULL, error);
767
9.91k
    if (st->buf == NULL) {
768
71
        g_prefix_error(error, "FuStructZipCdfh failed read of 0x%x: ", (guint) 46);
769
71
        return NULL;
770
71
    }
771
9.84k
    if (st->buf->len != 46) {
772
14
        g_set_error(error,
773
14
                    FWUPD_ERROR,
774
14
                    FWUPD_ERROR_INVALID_DATA,
775
14
                    "FuStructZipCdfh requested 0x%x and got 0x%x",
776
14
                    (guint) 46,
777
14
                    (guint) st->buf->len);
778
14
        return NULL;
779
14
    }
780
9.83k
    if (!fu_struct_zip_cdfh_parse_internal(st, error))
781
89
        return NULL;
782
9.74k
    return g_steal_pointer(&st);
783
9.83k
}
784
/**
785
 * fu_struct_zip_eocd_ref: (skip):
786
 **/
787
FuStructZipEocd *
788
fu_struct_zip_eocd_ref(FuStructZipEocd *st)
789
0
{
790
0
    g_return_val_if_fail(st != NULL, NULL);
791
0
    st->refcount++;
792
0
    return st;
793
0
}
794
/**
795
 * fu_struct_zip_eocd_unref: (skip):
796
 **/
797
void
798
fu_struct_zip_eocd_unref(FuStructZipEocd *st)
799
1.09k
{
800
1.09k
    g_return_if_fail(st != NULL);
801
1.09k
    if (st->refcount == 0) {
802
0
        g_critical("FuStructZipEocd refcount already zero");
803
0
        return;
804
0
    }
805
1.09k
    if (--st->refcount > 0)
806
0
        return;
807
1.09k
    if (st->buf != NULL)
808
1.09k
        g_byte_array_unref(st->buf);
809
1.09k
    g_free(st);
810
1.09k
}
811
/**
812
 * fu_struct_zip_eocd_new_internal: (skip):
813
 **/
814
static FuStructZipEocd *
815
fu_struct_zip_eocd_new_internal(void)
816
1.09k
{
817
1.09k
    FuStructZipEocd *st = g_new0(FuStructZipEocd, 1);
818
1.09k
    st->refcount = 1;
819
1.09k
    return st;
820
1.09k
}
821
822
/* getters */
823
/**
824
 * fu_struct_zip_eocd_get_magic: (skip):
825
 **/
826
static gchar *
827
fu_struct_zip_eocd_get_magic(const FuStructZipEocd *st)
828
0
{
829
0
    g_return_val_if_fail(st != NULL, NULL);
830
0
    return fu_memstrsafe(st->buf->data, st->buf->len, 0, 4, NULL);
831
0
}
832
/**
833
 * fu_struct_zip_eocd_get_disk_number: (skip):
834
 **/
835
guint16
836
fu_struct_zip_eocd_get_disk_number(const FuStructZipEocd *st)
837
993
{
838
993
    g_return_val_if_fail(st != NULL, 0x0);
839
993
    return fu_memread_uint16(st->buf->data + 4, G_LITTLE_ENDIAN);
840
993
}
841
/**
842
 * fu_struct_zip_eocd_get_cd_disk: (skip):
843
 **/
844
guint16
845
fu_struct_zip_eocd_get_cd_disk(const FuStructZipEocd *st)
846
977
{
847
977
    g_return_val_if_fail(st != NULL, 0x0);
848
977
    return fu_memread_uint16(st->buf->data + 6, G_LITTLE_ENDIAN);
849
977
}
850
/**
851
 * fu_struct_zip_eocd_get_cd_number_disk: (skip):
852
 **/
853
guint16
854
fu_struct_zip_eocd_get_cd_number_disk(const FuStructZipEocd *st)
855
959
{
856
959
    g_return_val_if_fail(st != NULL, 0x0);
857
959
    return fu_memread_uint16(st->buf->data + 8, G_LITTLE_ENDIAN);
858
959
}
859
/**
860
 * fu_struct_zip_eocd_get_cd_number: (skip):
861
 **/
862
guint16
863
fu_struct_zip_eocd_get_cd_number(const FuStructZipEocd *st)
864
11.9k
{
865
11.9k
    g_return_val_if_fail(st != NULL, 0x0);
866
11.9k
    return fu_memread_uint16(st->buf->data + 10, G_LITTLE_ENDIAN);
867
11.9k
}
868
/**
869
 * fu_struct_zip_eocd_get_cd_size: (skip):
870
 **/
871
guint32
872
fu_struct_zip_eocd_get_cd_size(const FuStructZipEocd *st)
873
935
{
874
935
    g_return_val_if_fail(st != NULL, 0x0);
875
935
    return fu_memread_uint32(st->buf->data + 12, G_LITTLE_ENDIAN);
876
935
}
877
/**
878
 * fu_struct_zip_eocd_get_cd_offset: (skip):
879
 **/
880
guint32
881
fu_struct_zip_eocd_get_cd_offset(const FuStructZipEocd *st)
882
880
{
883
880
    g_return_val_if_fail(st != NULL, 0x0);
884
880
    return fu_memread_uint32(st->buf->data + 16, G_LITTLE_ENDIAN);
885
880
}
886
/**
887
 * fu_struct_zip_eocd_get_comment_size: (skip):
888
 **/
889
guint16
890
fu_struct_zip_eocd_get_comment_size(const FuStructZipEocd *st)
891
0
{
892
0
    g_return_val_if_fail(st != NULL, 0x0);
893
0
    return fu_memread_uint16(st->buf->data + 20, G_LITTLE_ENDIAN);
894
0
}
895
896
/* setters */
897
/**
898
 * fu_struct_zip_eocd_set_magic: (skip):
899
 **/
900
static gboolean
901
fu_struct_zip_eocd_set_magic(FuStructZipEocd *st, const gchar *value, GError **error)
902
84
{
903
84
    gsize len;
904
84
    g_return_val_if_fail(st != NULL, FALSE);
905
84
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
906
84
    if (value == NULL) {
907
0
        memset(st->buf->data + 0, 0x0, 4);
908
0
        return TRUE;
909
0
    }
910
84
    len = strlen(value);
911
84
    if (len > 4) {
912
0
        g_set_error(error,
913
0
                    FWUPD_ERROR,
914
0
                    FWUPD_ERROR_INVALID_DATA,
915
0
                    "string '%s' (0x%x bytes) does not fit in FuStructZipEocd.magic (0x%x bytes)",
916
0
                    value, (guint) len, (guint) 4);
917
0
        return FALSE;
918
0
    }
919
84
    return fu_memcpy_safe(st->buf->data, st->buf->len, 0, (const guint8 *)value, len, 0x0, len, error);
920
84
}
921
/**
922
 * fu_struct_zip_eocd_set_disk_number: (skip):
923
 **/
924
void
925
fu_struct_zip_eocd_set_disk_number(FuStructZipEocd *st, guint16 value)
926
0
{
927
0
    g_return_if_fail(st != NULL);
928
0
    fu_memwrite_uint16(st->buf->data + 4, value, G_LITTLE_ENDIAN);
929
0
}
930
/**
931
 * fu_struct_zip_eocd_set_cd_disk: (skip):
932
 **/
933
void
934
fu_struct_zip_eocd_set_cd_disk(FuStructZipEocd *st, guint16 value)
935
0
{
936
0
    g_return_if_fail(st != NULL);
937
0
    fu_memwrite_uint16(st->buf->data + 6, value, G_LITTLE_ENDIAN);
938
0
}
939
/**
940
 * fu_struct_zip_eocd_set_cd_number_disk: (skip):
941
 **/
942
void
943
fu_struct_zip_eocd_set_cd_number_disk(FuStructZipEocd *st, guint16 value)
944
80
{
945
80
    g_return_if_fail(st != NULL);
946
80
    fu_memwrite_uint16(st->buf->data + 8, value, G_LITTLE_ENDIAN);
947
80
}
948
/**
949
 * fu_struct_zip_eocd_set_cd_number: (skip):
950
 **/
951
void
952
fu_struct_zip_eocd_set_cd_number(FuStructZipEocd *st, guint16 value)
953
80
{
954
80
    g_return_if_fail(st != NULL);
955
80
    fu_memwrite_uint16(st->buf->data + 10, value, G_LITTLE_ENDIAN);
956
80
}
957
/**
958
 * fu_struct_zip_eocd_set_cd_size: (skip):
959
 **/
960
void
961
fu_struct_zip_eocd_set_cd_size(FuStructZipEocd *st, guint32 value)
962
80
{
963
80
    g_return_if_fail(st != NULL);
964
80
    fu_memwrite_uint32(st->buf->data + 12, value, G_LITTLE_ENDIAN);
965
80
}
966
/**
967
 * fu_struct_zip_eocd_set_cd_offset: (skip):
968
 **/
969
void
970
fu_struct_zip_eocd_set_cd_offset(FuStructZipEocd *st, guint32 value)
971
80
{
972
80
    g_return_if_fail(st != NULL);
973
80
    fu_memwrite_uint32(st->buf->data + 16, value, G_LITTLE_ENDIAN);
974
80
}
975
/**
976
 * fu_struct_zip_eocd_set_comment_size: (skip):
977
 **/
978
void
979
fu_struct_zip_eocd_set_comment_size(FuStructZipEocd *st, guint16 value)
980
0
{
981
0
    g_return_if_fail(st != NULL);
982
0
    fu_memwrite_uint16(st->buf->data + 20, value, G_LITTLE_ENDIAN);
983
0
}
984
/**
985
 * fu_struct_zip_eocd_new: (skip):
986
 **/
987
FuStructZipEocd *
988
fu_struct_zip_eocd_new(void)
989
84
{
990
84
    FuStructZipEocd *st = fu_struct_zip_eocd_new_internal();
991
84
    st->buf = g_byte_array_sized_new(22);
992
84
    fu_byte_array_set_size(st->buf, 22, 0x0);
993
84
    fu_struct_zip_eocd_set_magic(st, "PK\x05\x06", NULL);
994
84
    return st;
995
84
}
996
/**
997
 * fu_struct_zip_eocd_to_string: (skip):
998
 **/
999
static gchar *
1000
fu_struct_zip_eocd_to_string(const FuStructZipEocd *st)
1001
0
{
1002
0
    g_autoptr(GString) str = g_string_new("FuStructZipEocd:\n");
1003
0
    g_return_val_if_fail(st != NULL, NULL);
1004
0
    {
1005
0
        g_autofree gchar *tmp = fu_struct_zip_eocd_get_magic(st);
1006
0
        if (tmp != NULL)
1007
0
            g_string_append_printf(str, "  magic: %s\n", tmp);
1008
0
    }
1009
0
    g_string_append_printf(str, "  disk_number: 0x%x\n",
1010
0
                           (guint) fu_struct_zip_eocd_get_disk_number(st));
1011
0
    g_string_append_printf(str, "  cd_disk: 0x%x\n",
1012
0
                           (guint) fu_struct_zip_eocd_get_cd_disk(st));
1013
0
    g_string_append_printf(str, "  cd_number_disk: 0x%x\n",
1014
0
                           (guint) fu_struct_zip_eocd_get_cd_number_disk(st));
1015
0
    g_string_append_printf(str, "  cd_number: 0x%x\n",
1016
0
                           (guint) fu_struct_zip_eocd_get_cd_number(st));
1017
0
    g_string_append_printf(str, "  cd_size: 0x%x\n",
1018
0
                           (guint) fu_struct_zip_eocd_get_cd_size(st));
1019
0
    g_string_append_printf(str, "  cd_offset: 0x%x\n",
1020
0
                           (guint) fu_struct_zip_eocd_get_cd_offset(st));
1021
0
    g_string_append_printf(str, "  comment_size: 0x%x\n",
1022
0
                           (guint) fu_struct_zip_eocd_get_comment_size(st));
1023
0
    if (str->len > 0)
1024
0
        g_string_set_size(str, str->len - 1);
1025
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1026
0
}
1027
static gboolean
1028
fu_struct_zip_eocd_validate_internal(FuStructZipEocd *st, GError **error)
1029
993
{
1030
993
    g_return_val_if_fail(st != NULL, FALSE);
1031
993
    if (strncmp((const gchar *) (st->buf->data + 0), "PK\x05\x06", 4) != 0) {
1032
0
        g_autofree gchar *str = fu_struct_zip_eocd_get_magic(st);
1033
0
        g_set_error(error,
1034
0
                    FWUPD_ERROR,
1035
0
                    FWUPD_ERROR_INVALID_DATA,
1036
0
                    "constant FuStructZipEocd.magic was not valid, "
1037
0
                    "expected 'PK\x05\x06' and got '%s'",
1038
0
                    str);
1039
0
        return FALSE;
1040
0
    }
1041
993
    return TRUE;
1042
993
}
1043
static gboolean
1044
fu_struct_zip_eocd_parse_internal(FuStructZipEocd *st, GError **error)
1045
993
{
1046
993
    if (g_log_get_debug_enabled()) {
1047
0
        g_autofree gchar *str = fu_struct_zip_eocd_to_string(st);
1048
0
        g_debug("%s", str);
1049
0
    }
1050
993
    if (!fu_struct_zip_eocd_validate_internal(st, error))
1051
0
        return FALSE;
1052
993
    return TRUE;
1053
993
}
1054
/**
1055
 * fu_struct_zip_eocd_parse_stream: (skip):
1056
 **/
1057
FuStructZipEocd *
1058
fu_struct_zip_eocd_parse_stream(GInputStream *stream, gsize offset, GError **error)
1059
1.01k
{
1060
1.01k
    g_autoptr(FuStructZipEocd) st = fu_struct_zip_eocd_new_internal();
1061
1.01k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 22, NULL, error);
1062
1.01k
    if (st->buf == NULL) {
1063
0
        g_prefix_error(error, "FuStructZipEocd failed read of 0x%x: ", (guint) 22);
1064
0
        return NULL;
1065
0
    }
1066
1.01k
    if (st->buf->len != 22) {
1067
17
        g_set_error(error,
1068
17
                    FWUPD_ERROR,
1069
17
                    FWUPD_ERROR_INVALID_DATA,
1070
17
                    "FuStructZipEocd requested 0x%x and got 0x%x",
1071
17
                    (guint) 22,
1072
17
                    (guint) st->buf->len);
1073
17
        return NULL;
1074
17
    }
1075
993
    if (!fu_struct_zip_eocd_parse_internal(st, error))
1076
0
        return NULL;
1077
993
    return g_steal_pointer(&st);
1078
993
}
1079
/**
1080
 * fu_struct_zip_lfh_ref: (skip):
1081
 **/
1082
FuStructZipLfh *
1083
fu_struct_zip_lfh_ref(FuStructZipLfh *st)
1084
0
{
1085
0
    g_return_val_if_fail(st != NULL, NULL);
1086
0
    st->refcount++;
1087
0
    return st;
1088
0
}
1089
/**
1090
 * fu_struct_zip_lfh_unref: (skip):
1091
 **/
1092
void
1093
fu_struct_zip_lfh_unref(FuStructZipLfh *st)
1094
10.2k
{
1095
10.2k
    g_return_if_fail(st != NULL);
1096
10.2k
    if (st->refcount == 0) {
1097
0
        g_critical("FuStructZipLfh refcount already zero");
1098
0
        return;
1099
0
    }
1100
10.2k
    if (--st->refcount > 0)
1101
0
        return;
1102
10.2k
    if (st->buf != NULL)
1103
10.1k
        g_byte_array_unref(st->buf);
1104
10.2k
    g_free(st);
1105
10.2k
}
1106
/**
1107
 * fu_struct_zip_lfh_new_internal: (skip):
1108
 **/
1109
static FuStructZipLfh *
1110
fu_struct_zip_lfh_new_internal(void)
1111
10.2k
{
1112
10.2k
    FuStructZipLfh *st = g_new0(FuStructZipLfh, 1);
1113
10.2k
    st->refcount = 1;
1114
10.2k
    return st;
1115
10.2k
}
1116
1117
/* getters */
1118
/**
1119
 * fu_struct_zip_lfh_get_magic: (skip):
1120
 **/
1121
static gchar *
1122
fu_struct_zip_lfh_get_magic(const FuStructZipLfh *st)
1123
33
{
1124
33
    g_return_val_if_fail(st != NULL, NULL);
1125
33
    return fu_memstrsafe(st->buf->data, st->buf->len, 0, 4, NULL);
1126
33
}
1127
/**
1128
 * fu_struct_zip_lfh_get_version_extract: (skip):
1129
 **/
1130
guint16
1131
fu_struct_zip_lfh_get_version_extract(const FuStructZipLfh *st)
1132
0
{
1133
0
    g_return_val_if_fail(st != NULL, 0x0);
1134
0
    return fu_memread_uint16(st->buf->data + 4, G_LITTLE_ENDIAN);
1135
0
}
1136
/**
1137
 * fu_struct_zip_lfh_get_flags: (skip):
1138
 **/
1139
FuZipFlags
1140
fu_struct_zip_lfh_get_flags(const FuStructZipLfh *st)
1141
9.47k
{
1142
9.47k
    g_return_val_if_fail(st != NULL, 0x0);
1143
9.47k
    return fu_memread_uint16(st->buf->data + 6, G_LITTLE_ENDIAN);
1144
9.47k
}
1145
/**
1146
 * fu_struct_zip_lfh_get_compression: (skip):
1147
 **/
1148
FuZipCompression
1149
fu_struct_zip_lfh_get_compression(const FuStructZipLfh *st)
1150
9.32k
{
1151
9.32k
    g_return_val_if_fail(st != NULL, 0x0);
1152
9.32k
    return fu_memread_uint16(st->buf->data + 8, G_LITTLE_ENDIAN);
1153
9.32k
}
1154
/**
1155
 * fu_struct_zip_lfh_get_file_time: (skip):
1156
 **/
1157
guint16
1158
fu_struct_zip_lfh_get_file_time(const FuStructZipLfh *st)
1159
0
{
1160
0
    g_return_val_if_fail(st != NULL, 0x0);
1161
0
    return fu_memread_uint16(st->buf->data + 10, G_LITTLE_ENDIAN);
1162
0
}
1163
/**
1164
 * fu_struct_zip_lfh_get_file_date: (skip):
1165
 **/
1166
guint16
1167
fu_struct_zip_lfh_get_file_date(const FuStructZipLfh *st)
1168
0
{
1169
0
    g_return_val_if_fail(st != NULL, 0x0);
1170
0
    return fu_memread_uint16(st->buf->data + 12, G_LITTLE_ENDIAN);
1171
0
}
1172
/**
1173
 * fu_struct_zip_lfh_get_uncompressed_crc: (skip):
1174
 **/
1175
guint32
1176
fu_struct_zip_lfh_get_uncompressed_crc(const FuStructZipLfh *st)
1177
9.13k
{
1178
9.13k
    g_return_val_if_fail(st != NULL, 0x0);
1179
9.13k
    return fu_memread_uint32(st->buf->data + 14, G_LITTLE_ENDIAN);
1180
9.13k
}
1181
/**
1182
 * fu_struct_zip_lfh_get_compressed_size: (skip):
1183
 **/
1184
guint32
1185
fu_struct_zip_lfh_get_compressed_size(const FuStructZipLfh *st)
1186
9.47k
{
1187
9.47k
    g_return_val_if_fail(st != NULL, 0x0);
1188
9.47k
    return fu_memread_uint32(st->buf->data + 18, G_LITTLE_ENDIAN);
1189
9.47k
}
1190
/**
1191
 * fu_struct_zip_lfh_get_uncompressed_size: (skip):
1192
 **/
1193
guint32
1194
fu_struct_zip_lfh_get_uncompressed_size(const FuStructZipLfh *st)
1195
9.47k
{
1196
9.47k
    g_return_val_if_fail(st != NULL, 0x0);
1197
9.47k
    return fu_memread_uint32(st->buf->data + 22, G_LITTLE_ENDIAN);
1198
9.47k
}
1199
/**
1200
 * fu_struct_zip_lfh_get_filename_size: (skip):
1201
 **/
1202
guint16
1203
fu_struct_zip_lfh_get_filename_size(const FuStructZipLfh *st)
1204
19.1k
{
1205
19.1k
    g_return_val_if_fail(st != NULL, 0x0);
1206
19.1k
    return fu_memread_uint16(st->buf->data + 26, G_LITTLE_ENDIAN);
1207
19.1k
}
1208
/**
1209
 * fu_struct_zip_lfh_get_extra_size: (skip):
1210
 **/
1211
guint16
1212
fu_struct_zip_lfh_get_extra_size(const FuStructZipLfh *st)
1213
18.9k
{
1214
18.9k
    g_return_val_if_fail(st != NULL, 0x0);
1215
18.9k
    return fu_memread_uint16(st->buf->data + 28, G_LITTLE_ENDIAN);
1216
18.9k
}
1217
1218
/* setters */
1219
/**
1220
 * fu_struct_zip_lfh_set_magic: (skip):
1221
 **/
1222
static gboolean
1223
fu_struct_zip_lfh_set_magic(FuStructZipLfh *st, const gchar *value, GError **error)
1224
484
{
1225
484
    gsize len;
1226
484
    g_return_val_if_fail(st != NULL, FALSE);
1227
484
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1228
484
    if (value == NULL) {
1229
0
        memset(st->buf->data + 0, 0x0, 4);
1230
0
        return TRUE;
1231
0
    }
1232
484
    len = strlen(value);
1233
484
    if (len > 4) {
1234
0
        g_set_error(error,
1235
0
                    FWUPD_ERROR,
1236
0
                    FWUPD_ERROR_INVALID_DATA,
1237
0
                    "string '%s' (0x%x bytes) does not fit in FuStructZipLfh.magic (0x%x bytes)",
1238
0
                    value, (guint) len, (guint) 4);
1239
0
        return FALSE;
1240
0
    }
1241
484
    return fu_memcpy_safe(st->buf->data, st->buf->len, 0, (const guint8 *)value, len, 0x0, len, error);
1242
484
}
1243
/**
1244
 * fu_struct_zip_lfh_set_version_extract: (skip):
1245
 **/
1246
void
1247
fu_struct_zip_lfh_set_version_extract(FuStructZipLfh *st, guint16 value)
1248
484
{
1249
484
    g_return_if_fail(st != NULL);
1250
484
    fu_memwrite_uint16(st->buf->data + 4, value, G_LITTLE_ENDIAN);
1251
484
}
1252
/**
1253
 * fu_struct_zip_lfh_set_flags: (skip):
1254
 **/
1255
void
1256
fu_struct_zip_lfh_set_flags(FuStructZipLfh *st, FuZipFlags value)
1257
0
{
1258
0
    g_return_if_fail(st != NULL);
1259
0
    fu_memwrite_uint16(st->buf->data + 6, value, G_LITTLE_ENDIAN);
1260
0
}
1261
/**
1262
 * fu_struct_zip_lfh_set_compression: (skip):
1263
 **/
1264
void
1265
fu_struct_zip_lfh_set_compression(FuStructZipLfh *st, FuZipCompression value)
1266
480
{
1267
480
    g_return_if_fail(st != NULL);
1268
480
    fu_memwrite_uint16(st->buf->data + 8, value, G_LITTLE_ENDIAN);
1269
480
}
1270
/**
1271
 * fu_struct_zip_lfh_set_file_time: (skip):
1272
 **/
1273
void
1274
fu_struct_zip_lfh_set_file_time(FuStructZipLfh *st, guint16 value)
1275
0
{
1276
0
    g_return_if_fail(st != NULL);
1277
0
    fu_memwrite_uint16(st->buf->data + 10, value, G_LITTLE_ENDIAN);
1278
0
}
1279
/**
1280
 * fu_struct_zip_lfh_set_file_date: (skip):
1281
 **/
1282
void
1283
fu_struct_zip_lfh_set_file_date(FuStructZipLfh *st, guint16 value)
1284
0
{
1285
0
    g_return_if_fail(st != NULL);
1286
0
    fu_memwrite_uint16(st->buf->data + 12, value, G_LITTLE_ENDIAN);
1287
0
}
1288
/**
1289
 * fu_struct_zip_lfh_set_uncompressed_crc: (skip):
1290
 **/
1291
void
1292
fu_struct_zip_lfh_set_uncompressed_crc(FuStructZipLfh *st, guint32 value)
1293
480
{
1294
480
    g_return_if_fail(st != NULL);
1295
480
    fu_memwrite_uint32(st->buf->data + 14, value, G_LITTLE_ENDIAN);
1296
480
}
1297
/**
1298
 * fu_struct_zip_lfh_set_compressed_size: (skip):
1299
 **/
1300
void
1301
fu_struct_zip_lfh_set_compressed_size(FuStructZipLfh *st, guint32 value)
1302
480
{
1303
480
    g_return_if_fail(st != NULL);
1304
480
    fu_memwrite_uint32(st->buf->data + 18, value, G_LITTLE_ENDIAN);
1305
480
}
1306
/**
1307
 * fu_struct_zip_lfh_set_uncompressed_size: (skip):
1308
 **/
1309
void
1310
fu_struct_zip_lfh_set_uncompressed_size(FuStructZipLfh *st, guint32 value)
1311
480
{
1312
480
    g_return_if_fail(st != NULL);
1313
480
    fu_memwrite_uint32(st->buf->data + 22, value, G_LITTLE_ENDIAN);
1314
480
}
1315
/**
1316
 * fu_struct_zip_lfh_set_filename_size: (skip):
1317
 **/
1318
void
1319
fu_struct_zip_lfh_set_filename_size(FuStructZipLfh *st, guint16 value)
1320
480
{
1321
480
    g_return_if_fail(st != NULL);
1322
480
    fu_memwrite_uint16(st->buf->data + 26, value, G_LITTLE_ENDIAN);
1323
480
}
1324
/**
1325
 * fu_struct_zip_lfh_set_extra_size: (skip):
1326
 **/
1327
void
1328
fu_struct_zip_lfh_set_extra_size(FuStructZipLfh *st, guint16 value)
1329
0
{
1330
0
    g_return_if_fail(st != NULL);
1331
0
    fu_memwrite_uint16(st->buf->data + 28, value, G_LITTLE_ENDIAN);
1332
0
}
1333
/**
1334
 * fu_struct_zip_lfh_new: (skip):
1335
 **/
1336
FuStructZipLfh *
1337
fu_struct_zip_lfh_new(void)
1338
484
{
1339
484
    FuStructZipLfh *st = fu_struct_zip_lfh_new_internal();
1340
484
    st->buf = g_byte_array_sized_new(30);
1341
484
    fu_byte_array_set_size(st->buf, 30, 0x0);
1342
484
    fu_struct_zip_lfh_set_magic(st, "PK\x03\x04", NULL);
1343
484
    fu_struct_zip_lfh_set_version_extract(st, 0xA);
1344
484
    return st;
1345
484
}
1346
/**
1347
 * fu_struct_zip_lfh_to_string: (skip):
1348
 **/
1349
static gchar *
1350
fu_struct_zip_lfh_to_string(const FuStructZipLfh *st)
1351
0
{
1352
0
    g_autoptr(GString) str = g_string_new("FuStructZipLfh:\n");
1353
0
    g_return_val_if_fail(st != NULL, NULL);
1354
0
    {
1355
0
        g_autofree gchar *tmp = fu_struct_zip_lfh_get_magic(st);
1356
0
        if (tmp != NULL)
1357
0
            g_string_append_printf(str, "  magic: %s\n", tmp);
1358
0
    }
1359
0
    g_string_append_printf(str, "  version_extract: 0x%x\n",
1360
0
                           (guint) fu_struct_zip_lfh_get_version_extract(st));
1361
0
    {
1362
0
        g_autofree gchar *tmp = fu_zip_flags_to_string(fu_struct_zip_lfh_get_flags(st));
1363
0
        if (tmp != NULL) {
1364
0
            g_string_append_printf(str, "  flags: 0x%x [%s]\n", (guint) fu_struct_zip_lfh_get_flags(st), tmp);
1365
0
        } else {
1366
0
            g_string_append_printf(str, "  flags: 0x%x\n", (guint) fu_struct_zip_lfh_get_flags(st));
1367
0
        }
1368
0
    }
1369
0
    {
1370
0
        const gchar *tmp = fu_zip_compression_to_string(fu_struct_zip_lfh_get_compression(st));
1371
0
        if (tmp != NULL) {
1372
0
            g_string_append_printf(str, "  compression: 0x%x [%s]\n", (guint) fu_struct_zip_lfh_get_compression(st), tmp);
1373
0
        } else {
1374
0
            g_string_append_printf(str, "  compression: 0x%x\n", (guint) fu_struct_zip_lfh_get_compression(st));
1375
0
        }
1376
0
    }
1377
0
    g_string_append_printf(str, "  file_time: 0x%x\n",
1378
0
                           (guint) fu_struct_zip_lfh_get_file_time(st));
1379
0
    g_string_append_printf(str, "  file_date: 0x%x\n",
1380
0
                           (guint) fu_struct_zip_lfh_get_file_date(st));
1381
0
    g_string_append_printf(str, "  uncompressed_crc: 0x%x\n",
1382
0
                           (guint) fu_struct_zip_lfh_get_uncompressed_crc(st));
1383
0
    g_string_append_printf(str, "  compressed_size: 0x%x\n",
1384
0
                           (guint) fu_struct_zip_lfh_get_compressed_size(st));
1385
0
    g_string_append_printf(str, "  uncompressed_size: 0x%x\n",
1386
0
                           (guint) fu_struct_zip_lfh_get_uncompressed_size(st));
1387
0
    g_string_append_printf(str, "  filename_size: 0x%x\n",
1388
0
                           (guint) fu_struct_zip_lfh_get_filename_size(st));
1389
0
    g_string_append_printf(str, "  extra_size: 0x%x\n",
1390
0
                           (guint) fu_struct_zip_lfh_get_extra_size(st));
1391
0
    if (str->len > 0)
1392
0
        g_string_set_size(str, str->len - 1);
1393
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1394
0
}
1395
static gboolean
1396
fu_struct_zip_lfh_validate_internal(FuStructZipLfh *st, GError **error)
1397
9.68k
{
1398
9.68k
    g_return_val_if_fail(st != NULL, FALSE);
1399
9.68k
    if (strncmp((const gchar *) (st->buf->data + 0), "PK\x03\x04", 4) != 0) {
1400
33
        g_autofree gchar *str = fu_struct_zip_lfh_get_magic(st);
1401
33
        g_set_error(error,
1402
33
                    FWUPD_ERROR,
1403
33
                    FWUPD_ERROR_INVALID_DATA,
1404
33
                    "constant FuStructZipLfh.magic was not valid, "
1405
33
                    "expected 'PK\x03\x04' and got '%s'",
1406
33
                    str);
1407
33
        return FALSE;
1408
33
    }
1409
9.65k
    return TRUE;
1410
9.68k
}
1411
static gboolean
1412
fu_struct_zip_lfh_parse_internal(FuStructZipLfh *st, GError **error)
1413
9.68k
{
1414
9.68k
    if (g_log_get_debug_enabled()) {
1415
0
        g_autofree gchar *str = fu_struct_zip_lfh_to_string(st);
1416
0
        g_debug("%s", str);
1417
0
    }
1418
9.68k
    if (!fu_struct_zip_lfh_validate_internal(st, error))
1419
33
        return FALSE;
1420
9.65k
    return TRUE;
1421
9.68k
}
1422
/**
1423
 * fu_struct_zip_lfh_parse_stream: (skip):
1424
 **/
1425
FuStructZipLfh *
1426
fu_struct_zip_lfh_parse_stream(GInputStream *stream, gsize offset, GError **error)
1427
9.73k
{
1428
9.73k
    g_autoptr(FuStructZipLfh) st = fu_struct_zip_lfh_new_internal();
1429
9.73k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 30, NULL, error);
1430
9.73k
    if (st->buf == NULL) {
1431
43
        g_prefix_error(error, "FuStructZipLfh failed read of 0x%x: ", (guint) 30);
1432
43
        return NULL;
1433
43
    }
1434
9.69k
    if (st->buf->len != 30) {
1435
6
        g_set_error(error,
1436
6
                    FWUPD_ERROR,
1437
6
                    FWUPD_ERROR_INVALID_DATA,
1438
6
                    "FuStructZipLfh requested 0x%x and got 0x%x",
1439
6
                    (guint) 30,
1440
6
                    (guint) st->buf->len);
1441
6
        return NULL;
1442
6
    }
1443
9.68k
    if (!fu_struct_zip_lfh_parse_internal(st, error))
1444
33
        return NULL;
1445
9.65k
    return g_steal_pointer(&st);
1446
9.68k
}