Coverage Report

Created: 2026-02-26 06:27

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
47
{
33
47
    if (val == FU_ZIP_COMPRESSION_NONE)
34
0
        return "none";
35
47
    if (val == FU_ZIP_COMPRESSION_SHRUNK)
36
1
        return "shrunk";
37
46
    if (val == FU_ZIP_COMPRESSION_IMPLODE)
38
1
        return "implode";
39
45
    if (val == FU_ZIP_COMPRESSION_TOKENIZE)
40
1
        return "tokenize";
41
44
    if (val == FU_ZIP_COMPRESSION_DEFLATE)
42
0
        return "deflate";
43
44
    if (val == FU_ZIP_COMPRESSION_DEFLATE64)
44
1
        return "deflate64";
45
43
    if (val == FU_ZIP_COMPRESSION_BZIP2)
46
1
        return "bzip2";
47
42
    if (val == FU_ZIP_COMPRESSION_LZMA)
48
1
        return "lzma";
49
41
    if (val == FU_ZIP_COMPRESSION_TERSE)
50
1
        return "terse";
51
40
    if (val == FU_ZIP_COMPRESSION_ZSTANDARD)
52
1
        return "zstandard";
53
39
    if (val == FU_ZIP_COMPRESSION_XZ)
54
1
        return "xz";
55
38
    return NULL;
56
39
}
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
17.0k
{
159
17.0k
    g_return_if_fail(st != NULL);
160
17.0k
    if (st->refcount == 0) {
161
0
        g_critical("FuStructZipExtraHdr refcount already zero");
162
0
        return;
163
0
    }
164
17.0k
    if (--st->refcount > 0)
165
0
        return;
166
17.0k
    if (st->buf != NULL)
167
16.9k
        g_byte_array_unref(st->buf);
168
17.0k
    g_free(st);
169
17.0k
}
170
/**
171
 * fu_struct_zip_extra_hdr_new_internal: (skip):
172
 **/
173
static FuStructZipExtraHdr *
174
fu_struct_zip_extra_hdr_new_internal(void)
175
17.0k
{
176
17.0k
    FuStructZipExtraHdr *st = g_new0(FuStructZipExtraHdr, 1);
177
17.0k
    st->refcount = 1;
178
17.0k
    return st;
179
17.0k
}
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
16.8k
{
197
16.8k
    g_return_val_if_fail(st != NULL, 0x0);
198
16.8k
    return fu_memread_uint16(st->buf->data + 2, G_LITTLE_ENDIAN);
199
16.8k
}
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
16.8k
{
256
16.8k
    g_return_val_if_fail(st != NULL, FALSE);
257
16.8k
    return TRUE;
258
16.8k
}
259
static gboolean
260
fu_struct_zip_extra_hdr_parse_internal(FuStructZipExtraHdr *st, GError **error)
261
16.8k
{
262
16.8k
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
263
0
        g_autofree gchar *str = fu_struct_zip_extra_hdr_to_string(st);
264
0
        g_debug("%s", str);
265
0
    }
266
16.8k
    if (!fu_struct_zip_extra_hdr_validate_internal(st, error))
267
0
        return FALSE;
268
16.8k
    return TRUE;
269
16.8k
}
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
17.0k
{
276
17.0k
    g_autoptr(FuStructZipExtraHdr) st = fu_struct_zip_extra_hdr_new_internal();
277
17.0k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 4, NULL, error);
278
17.0k
    if (st->buf == NULL) {
279
100
        g_prefix_error(error, "FuStructZipExtraHdr failed read of 0x%x: ", (guint) 4);
280
100
        return NULL;
281
100
    }
282
16.9k
    if (st->buf->len != 4) {
283
23
        g_set_error(error,
284
23
                    FWUPD_ERROR,
285
23
                    FWUPD_ERROR_INVALID_DATA,
286
23
                    "FuStructZipExtraHdr requested 0x%x and got 0x%x",
287
23
                    (guint) 4,
288
23
                    (guint) st->buf->len);
289
23
        return NULL;
290
23
    }
291
16.8k
    if (!fu_struct_zip_extra_hdr_parse_internal(st, error))
292
0
        return NULL;
293
16.8k
    return g_steal_pointer(&st);
294
16.8k
}
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
16.8k
{
311
16.8k
    g_return_if_fail(st != NULL);
312
16.8k
    if (st->refcount == 0) {
313
0
        g_critical("FuStructZipCdfh refcount already zero");
314
0
        return;
315
0
    }
316
16.8k
    if (--st->refcount > 0)
317
0
        return;
318
16.8k
    if (st->buf != NULL)
319
16.6k
        g_byte_array_unref(st->buf);
320
16.8k
    g_free(st);
321
16.8k
}
322
/**
323
 * fu_struct_zip_cdfh_new_internal: (skip):
324
 **/
325
static FuStructZipCdfh *
326
fu_struct_zip_cdfh_new_internal(void)
327
16.8k
{
328
16.8k
    FuStructZipCdfh *st = g_new0(FuStructZipCdfh, 1);
329
16.8k
    st->refcount = 1;
330
16.8k
    return st;
331
16.8k
}
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
75
{
340
75
    g_return_val_if_fail(st != NULL, NULL);
341
75
    return fu_memstrsafe(st->buf->data, st->buf->len, 0, 4, NULL);
342
75
}
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
16.1k
{
367
16.1k
    g_return_val_if_fail(st != NULL, 0x0);
368
16.1k
    return fu_memread_uint16(st->buf->data + 8, G_LITTLE_ENDIAN);
369
16.1k
}
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
674
{
403
674
    g_return_val_if_fail(st != NULL, 0x0);
404
674
    return fu_memread_uint32(st->buf->data + 16, G_LITTLE_ENDIAN);
405
674
}
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
4.45k
{
412
4.45k
    g_return_val_if_fail(st != NULL, 0x0);
413
4.45k
    return fu_memread_uint32(st->buf->data + 20, G_LITTLE_ENDIAN);
414
4.45k
}
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
3.46k
{
421
3.46k
    g_return_val_if_fail(st != NULL, 0x0);
422
3.46k
    return fu_memread_uint32(st->buf->data + 24, G_LITTLE_ENDIAN);
423
3.46k
}
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
15.3k
{
430
15.3k
    g_return_val_if_fail(st != NULL, 0x0);
431
15.3k
    return fu_memread_uint16(st->buf->data + 28, G_LITTLE_ENDIAN);
432
15.3k
}
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
30.6k
{
439
30.6k
    g_return_val_if_fail(st != NULL, 0x0);
440
30.6k
    return fu_memread_uint16(st->buf->data + 30, G_LITTLE_ENDIAN);
441
30.6k
}
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
15.2k
{
448
15.2k
    g_return_val_if_fail(st != NULL, 0x0);
449
15.2k
    return fu_memread_uint16(st->buf->data + 32, G_LITTLE_ENDIAN);
450
15.2k
}
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
16.1k
{
484
16.1k
    g_return_val_if_fail(st != NULL, 0x0);
485
16.1k
    return fu_memread_uint32(st->buf->data + 42, G_LITTLE_ENDIAN);
486
16.1k
}
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
408
{
495
408
    gsize len;
496
408
    g_return_val_if_fail(st != NULL, FALSE);
497
408
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
498
408
    if (value == NULL) {
499
0
        memset(st->buf->data + 0, 0x0, 4);
500
0
        return TRUE;
501
0
    }
502
408
    len = strlen(value);
503
408
    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
408
    return fu_memcpy_safe(st->buf->data, st->buf->len, 0, (const guint8 *)value, len, 0x0, len, error);
512
408
}
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
408
{
519
408
    g_return_if_fail(st != NULL);
520
408
    fu_memwrite_uint16(st->buf->data + 4, value, G_LITTLE_ENDIAN);
521
408
}
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
408
{
528
408
    g_return_if_fail(st != NULL);
529
408
    fu_memwrite_uint16(st->buf->data + 6, value, G_LITTLE_ENDIAN);
530
408
}
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
408
{
546
408
    g_return_if_fail(st != NULL);
547
408
    fu_memwrite_uint16(st->buf->data + 10, value, G_LITTLE_ENDIAN);
548
408
}
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
408
{
573
408
    g_return_if_fail(st != NULL);
574
408
    fu_memwrite_uint32(st->buf->data + 16, value, G_LITTLE_ENDIAN);
575
408
}
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
408
{
582
408
    g_return_if_fail(st != NULL);
583
408
    fu_memwrite_uint32(st->buf->data + 20, value, G_LITTLE_ENDIAN);
584
408
}
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
408
{
591
408
    g_return_if_fail(st != NULL);
592
408
    fu_memwrite_uint32(st->buf->data + 24, value, G_LITTLE_ENDIAN);
593
408
}
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
408
{
600
408
    g_return_if_fail(st != NULL);
601
408
    fu_memwrite_uint16(st->buf->data + 28, value, G_LITTLE_ENDIAN);
602
408
}
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
408
{
654
408
    g_return_if_fail(st != NULL);
655
408
    fu_memwrite_uint32(st->buf->data + 42, value, G_LITTLE_ENDIAN);
656
408
}
657
/**
658
 * fu_struct_zip_cdfh_new: (skip):
659
 **/
660
FuStructZipCdfh *
661
fu_struct_zip_cdfh_new(void)
662
408
{
663
408
    FuStructZipCdfh *st = fu_struct_zip_cdfh_new_internal();
664
408
    st->buf = g_byte_array_sized_new(46);
665
408
    fu_byte_array_set_size(st->buf, 46, 0x0);
666
408
    fu_struct_zip_cdfh_set_magic(st, "PK\x01\x02", NULL);
667
408
    fu_struct_zip_cdfh_set_version_generated(st, 0xA);
668
408
    fu_struct_zip_cdfh_set_version_extract(st, 0xA);
669
408
    return st;
670
408
}
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
16.2k
{
735
16.2k
    g_return_val_if_fail(st != NULL, FALSE);
736
16.2k
    if (strncmp((const gchar *) (st->buf->data + 0), "PK\x01\x02", 4) != 0) {
737
75
        g_autofree gchar *str = fu_struct_zip_cdfh_get_magic(st);
738
75
        g_set_error(error,
739
75
                    FWUPD_ERROR,
740
75
                    FWUPD_ERROR_INVALID_DATA,
741
75
                    "constant FuStructZipCdfh.magic was not valid, "
742
75
                    "expected 'PK\x01\x02' and got '%s'",
743
75
                    str);
744
75
        return FALSE;
745
75
    }
746
16.1k
    return TRUE;
747
16.2k
}
748
static gboolean
749
fu_struct_zip_cdfh_parse_internal(FuStructZipCdfh *st, GError **error)
750
16.2k
{
751
16.2k
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
752
0
        g_autofree gchar *str = fu_struct_zip_cdfh_to_string(st);
753
0
        g_debug("%s", str);
754
0
    }
755
16.2k
    if (!fu_struct_zip_cdfh_validate_internal(st, error))
756
75
        return FALSE;
757
16.1k
    return TRUE;
758
16.2k
}
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
16.4k
{
765
16.4k
    g_autoptr(FuStructZipCdfh) st = fu_struct_zip_cdfh_new_internal();
766
16.4k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 46, NULL, error);
767
16.4k
    if (st->buf == NULL) {
768
165
        g_prefix_error(error, "FuStructZipCdfh failed read of 0x%x: ", (guint) 46);
769
165
        return NULL;
770
165
    }
771
16.2k
    if (st->buf->len != 46) {
772
49
        g_set_error(error,
773
49
                    FWUPD_ERROR,
774
49
                    FWUPD_ERROR_INVALID_DATA,
775
49
                    "FuStructZipCdfh requested 0x%x and got 0x%x",
776
49
                    (guint) 46,
777
49
                    (guint) st->buf->len);
778
49
        return NULL;
779
49
    }
780
16.2k
    if (!fu_struct_zip_cdfh_parse_internal(st, error))
781
75
        return NULL;
782
16.1k
    return g_steal_pointer(&st);
783
16.2k
}
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.32k
{
800
1.32k
    g_return_if_fail(st != NULL);
801
1.32k
    if (st->refcount == 0) {
802
0
        g_critical("FuStructZipEocd refcount already zero");
803
0
        return;
804
0
    }
805
1.32k
    if (--st->refcount > 0)
806
0
        return;
807
1.32k
    if (st->buf != NULL)
808
1.32k
        g_byte_array_unref(st->buf);
809
1.32k
    g_free(st);
810
1.32k
}
811
/**
812
 * fu_struct_zip_eocd_new_internal: (skip):
813
 **/
814
static FuStructZipEocd *
815
fu_struct_zip_eocd_new_internal(void)
816
1.32k
{
817
1.32k
    FuStructZipEocd *st = g_new0(FuStructZipEocd, 1);
818
1.32k
    st->refcount = 1;
819
1.32k
    return st;
820
1.32k
}
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
1.25k
{
838
1.25k
    g_return_val_if_fail(st != NULL, 0x0);
839
1.25k
    return fu_memread_uint16(st->buf->data + 4, G_LITTLE_ENDIAN);
840
1.25k
}
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
1.23k
{
847
1.23k
    g_return_val_if_fail(st != NULL, 0x0);
848
1.23k
    return fu_memread_uint16(st->buf->data + 6, G_LITTLE_ENDIAN);
849
1.23k
}
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
1.21k
{
856
1.21k
    g_return_val_if_fail(st != NULL, 0x0);
857
1.21k
    return fu_memread_uint16(st->buf->data + 8, G_LITTLE_ENDIAN);
858
1.21k
}
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
17.6k
{
865
17.6k
    g_return_val_if_fail(st != NULL, 0x0);
866
17.6k
    return fu_memread_uint16(st->buf->data + 10, G_LITTLE_ENDIAN);
867
17.6k
}
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
1.19k
{
874
1.19k
    g_return_val_if_fail(st != NULL, 0x0);
875
1.19k
    return fu_memread_uint32(st->buf->data + 12, G_LITTLE_ENDIAN);
876
1.19k
}
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
1.19k
{
883
1.19k
    g_return_val_if_fail(st != NULL, 0x0);
884
1.19k
    return fu_memread_uint32(st->buf->data + 16, G_LITTLE_ENDIAN);
885
1.19k
}
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
55
{
903
55
    gsize len;
904
55
    g_return_val_if_fail(st != NULL, FALSE);
905
55
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
906
55
    if (value == NULL) {
907
0
        memset(st->buf->data + 0, 0x0, 4);
908
0
        return TRUE;
909
0
    }
910
55
    len = strlen(value);
911
55
    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
55
    return fu_memcpy_safe(st->buf->data, st->buf->len, 0, (const guint8 *)value, len, 0x0, len, error);
920
55
}
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
45
{
945
45
    g_return_if_fail(st != NULL);
946
45
    fu_memwrite_uint16(st->buf->data + 8, value, G_LITTLE_ENDIAN);
947
45
}
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
45
{
954
45
    g_return_if_fail(st != NULL);
955
45
    fu_memwrite_uint16(st->buf->data + 10, value, G_LITTLE_ENDIAN);
956
45
}
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
45
{
963
45
    g_return_if_fail(st != NULL);
964
45
    fu_memwrite_uint32(st->buf->data + 12, value, G_LITTLE_ENDIAN);
965
45
}
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
45
{
972
45
    g_return_if_fail(st != NULL);
973
45
    fu_memwrite_uint32(st->buf->data + 16, value, G_LITTLE_ENDIAN);
974
45
}
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
55
{
990
55
    FuStructZipEocd *st = fu_struct_zip_eocd_new_internal();
991
55
    st->buf = g_byte_array_sized_new(22);
992
55
    fu_byte_array_set_size(st->buf, 22, 0x0);
993
55
    fu_struct_zip_eocd_set_magic(st, "PK\x05\x06", NULL);
994
55
    return st;
995
55
}
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
1.25k
{
1030
1.25k
    g_return_val_if_fail(st != NULL, FALSE);
1031
1.25k
    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
1.25k
    return TRUE;
1042
1.25k
}
1043
static gboolean
1044
fu_struct_zip_eocd_parse_internal(FuStructZipEocd *st, GError **error)
1045
1.25k
{
1046
1.25k
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
1047
0
        g_autofree gchar *str = fu_struct_zip_eocd_to_string(st);
1048
0
        g_debug("%s", str);
1049
0
    }
1050
1.25k
    if (!fu_struct_zip_eocd_validate_internal(st, error))
1051
0
        return FALSE;
1052
1.25k
    return TRUE;
1053
1.25k
}
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.26k
{
1060
1.26k
    g_autoptr(FuStructZipEocd) st = fu_struct_zip_eocd_new_internal();
1061
1.26k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 22, NULL, error);
1062
1.26k
    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.26k
    if (st->buf->len != 22) {
1067
18
        g_set_error(error,
1068
18
                    FWUPD_ERROR,
1069
18
                    FWUPD_ERROR_INVALID_DATA,
1070
18
                    "FuStructZipEocd requested 0x%x and got 0x%x",
1071
18
                    (guint) 22,
1072
18
                    (guint) st->buf->len);
1073
18
        return NULL;
1074
18
    }
1075
1.25k
    if (!fu_struct_zip_eocd_parse_internal(st, error))
1076
0
        return NULL;
1077
1.25k
    return g_steal_pointer(&st);
1078
1.25k
}
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
17.3k
{
1095
17.3k
    g_return_if_fail(st != NULL);
1096
17.3k
    if (st->refcount == 0) {
1097
0
        g_critical("FuStructZipLfh refcount already zero");
1098
0
        return;
1099
0
    }
1100
17.3k
    if (--st->refcount > 0)
1101
0
        return;
1102
17.3k
    if (st->buf != NULL)
1103
17.3k
        g_byte_array_unref(st->buf);
1104
17.3k
    g_free(st);
1105
17.3k
}
1106
/**
1107
 * fu_struct_zip_lfh_new_internal: (skip):
1108
 **/
1109
static FuStructZipLfh *
1110
fu_struct_zip_lfh_new_internal(void)
1111
17.3k
{
1112
17.3k
    FuStructZipLfh *st = g_new0(FuStructZipLfh, 1);
1113
17.3k
    st->refcount = 1;
1114
17.3k
    return st;
1115
17.3k
}
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
44
{
1124
44
    g_return_val_if_fail(st != NULL, NULL);
1125
44
    return fu_memstrsafe(st->buf->data, st->buf->len, 0, 4, NULL);
1126
44
}
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
0
{
1142
0
    g_return_val_if_fail(st != NULL, 0x0);
1143
0
    return fu_memread_uint16(st->buf->data + 6, G_LITTLE_ENDIAN);
1144
0
}
1145
/**
1146
 * fu_struct_zip_lfh_get_compression: (skip):
1147
 **/
1148
FuZipCompression
1149
fu_struct_zip_lfh_get_compression(const FuStructZipLfh *st)
1150
15.6k
{
1151
15.6k
    g_return_val_if_fail(st != NULL, 0x0);
1152
15.6k
    return fu_memread_uint16(st->buf->data + 8, G_LITTLE_ENDIAN);
1153
15.6k
}
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
15.8k
{
1178
15.8k
    g_return_val_if_fail(st != NULL, 0x0);
1179
15.8k
    return fu_memread_uint32(st->buf->data + 14, G_LITTLE_ENDIAN);
1180
15.8k
}
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
15.8k
{
1187
15.8k
    g_return_val_if_fail(st != NULL, 0x0);
1188
15.8k
    return fu_memread_uint32(st->buf->data + 18, G_LITTLE_ENDIAN);
1189
15.8k
}
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
15.8k
{
1196
15.8k
    g_return_val_if_fail(st != NULL, 0x0);
1197
15.8k
    return fu_memread_uint32(st->buf->data + 22, G_LITTLE_ENDIAN);
1198
15.8k
}
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
31.9k
{
1205
31.9k
    g_return_val_if_fail(st != NULL, 0x0);
1206
31.9k
    return fu_memread_uint16(st->buf->data + 26, G_LITTLE_ENDIAN);
1207
31.9k
}
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
31.6k
{
1214
31.6k
    g_return_val_if_fail(st != NULL, 0x0);
1215
31.6k
    return fu_memread_uint16(st->buf->data + 28, G_LITTLE_ENDIAN);
1216
31.6k
}
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
1.26k
{
1225
1.26k
    gsize len;
1226
1.26k
    g_return_val_if_fail(st != NULL, FALSE);
1227
1.26k
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1228
1.26k
    if (value == NULL) {
1229
0
        memset(st->buf->data + 0, 0x0, 4);
1230
0
        return TRUE;
1231
0
    }
1232
1.26k
    len = strlen(value);
1233
1.26k
    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
1.26k
    return fu_memcpy_safe(st->buf->data, st->buf->len, 0, (const guint8 *)value, len, 0x0, len, error);
1242
1.26k
}
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
1.26k
{
1249
1.26k
    g_return_if_fail(st != NULL);
1250
1.26k
    fu_memwrite_uint16(st->buf->data + 4, value, G_LITTLE_ENDIAN);
1251
1.26k
}
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
1.25k
{
1267
1.25k
    g_return_if_fail(st != NULL);
1268
1.25k
    fu_memwrite_uint16(st->buf->data + 8, value, G_LITTLE_ENDIAN);
1269
1.25k
}
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
1.25k
{
1294
1.25k
    g_return_if_fail(st != NULL);
1295
1.25k
    fu_memwrite_uint32(st->buf->data + 14, value, G_LITTLE_ENDIAN);
1296
1.25k
}
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
1.25k
{
1303
1.25k
    g_return_if_fail(st != NULL);
1304
1.25k
    fu_memwrite_uint32(st->buf->data + 18, value, G_LITTLE_ENDIAN);
1305
1.25k
}
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
1.25k
{
1312
1.25k
    g_return_if_fail(st != NULL);
1313
1.25k
    fu_memwrite_uint32(st->buf->data + 22, value, G_LITTLE_ENDIAN);
1314
1.25k
}
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
1.25k
{
1321
1.25k
    g_return_if_fail(st != NULL);
1322
1.25k
    fu_memwrite_uint16(st->buf->data + 26, value, G_LITTLE_ENDIAN);
1323
1.25k
}
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
1.26k
{
1339
1.26k
    FuStructZipLfh *st = fu_struct_zip_lfh_new_internal();
1340
1.26k
    st->buf = g_byte_array_sized_new(30);
1341
1.26k
    fu_byte_array_set_size(st->buf, 30, 0x0);
1342
1.26k
    fu_struct_zip_lfh_set_magic(st, "PK\x03\x04", NULL);
1343
1.26k
    fu_struct_zip_lfh_set_version_extract(st, 0xA);
1344
1.26k
    return st;
1345
1.26k
}
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
16.0k
{
1398
16.0k
    g_return_val_if_fail(st != NULL, FALSE);
1399
16.0k
    if (strncmp((const gchar *) (st->buf->data + 0), "PK\x03\x04", 4) != 0) {
1400
44
        g_autofree gchar *str = fu_struct_zip_lfh_get_magic(st);
1401
44
        g_set_error(error,
1402
44
                    FWUPD_ERROR,
1403
44
                    FWUPD_ERROR_INVALID_DATA,
1404
44
                    "constant FuStructZipLfh.magic was not valid, "
1405
44
                    "expected 'PK\x03\x04' and got '%s'",
1406
44
                    str);
1407
44
        return FALSE;
1408
44
    }
1409
16.0k
    return TRUE;
1410
16.0k
}
1411
static gboolean
1412
fu_struct_zip_lfh_parse_internal(FuStructZipLfh *st, GError **error)
1413
16.0k
{
1414
16.0k
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
1415
0
        g_autofree gchar *str = fu_struct_zip_lfh_to_string(st);
1416
0
        g_debug("%s", str);
1417
0
    }
1418
16.0k
    if (!fu_struct_zip_lfh_validate_internal(st, error))
1419
44
        return FALSE;
1420
16.0k
    return TRUE;
1421
16.0k
}
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
16.1k
{
1428
16.1k
    g_autoptr(FuStructZipLfh) st = fu_struct_zip_lfh_new_internal();
1429
16.1k
    st->buf = fu_input_stream_read_byte_array(stream, offset, 30, NULL, error);
1430
16.1k
    if (st->buf == NULL) {
1431
39
        g_prefix_error(error, "FuStructZipLfh failed read of 0x%x: ", (guint) 30);
1432
39
        return NULL;
1433
39
    }
1434
16.0k
    if (st->buf->len != 30) {
1435
7
        g_set_error(error,
1436
7
                    FWUPD_ERROR,
1437
7
                    FWUPD_ERROR_INVALID_DATA,
1438
7
                    "FuStructZipLfh requested 0x%x and got 0x%x",
1439
7
                    (guint) 30,
1440
7
                    (guint) st->buf->len);
1441
7
        return NULL;
1442
7
    }
1443
16.0k
    if (!fu_struct_zip_lfh_parse_internal(st, error))
1444
44
        return NULL;
1445
16.0k
    return g_steal_pointer(&st);
1446
16.0k
}