Coverage Report

Created: 2025-07-11 06:31

/work/fu-pefile-struct.c
Line
Count
Source (jump to first uncovered line)
1
/* auto-generated, do not modify */
2
#include "config.h"
3
4
#include "fu-pefile-struct.h"
5
#include "fu-byte-array.h"
6
#include "fu-mem-private.h"
7
#include "fu-string.h"
8
9
#ifdef G_LOG_DOMAIN
10
  #undef G_LOG_DOMAIN
11
#endif
12
0
#define G_LOG_DOMAIN "FuStruct"
13
14
static const gchar *
15
fu_pe_coff_machine_to_string(FuPeCoffMachine val)
16
0
{
17
0
    if (val == FU_PE_COFF_MACHINE_UNKNOWN)
18
0
        return "unknown";
19
0
    if (val == FU_PE_COFF_MACHINE_ALPHA)
20
0
        return "alpha";
21
0
    if (val == FU_PE_COFF_MACHINE_ALPHA64)
22
0
        return "alpha64";
23
0
    if (val == FU_PE_COFF_MACHINE_AM33)
24
0
        return "am33";
25
0
    if (val == FU_PE_COFF_MACHINE_AMD64)
26
0
        return "amd64";
27
0
    if (val == FU_PE_COFF_MACHINE_ARM)
28
0
        return "arm";
29
0
    if (val == FU_PE_COFF_MACHINE_ARM64)
30
0
        return "arm64";
31
0
    if (val == FU_PE_COFF_MACHINE_ARMNT)
32
0
        return "armnt";
33
0
    if (val == FU_PE_COFF_MACHINE_EBC)
34
0
        return "ebc";
35
0
    if (val == FU_PE_COFF_MACHINE_I386)
36
0
        return "i386";
37
0
    if (val == FU_PE_COFF_MACHINE_IA64)
38
0
        return "ia64";
39
0
    if (val == FU_PE_COFF_MACHINE_LOONGARCH32)
40
0
        return "loongarch32";
41
0
    if (val == FU_PE_COFF_MACHINE_LOONGARCH64)
42
0
        return "loongarch64";
43
0
    if (val == FU_PE_COFF_MACHINE_M32R)
44
0
        return "m32r";
45
0
    if (val == FU_PE_COFF_MACHINE_MIPS16)
46
0
        return "mips16";
47
0
    if (val == FU_PE_COFF_MACHINE_MIPSFPU)
48
0
        return "mipsfpu";
49
0
    if (val == FU_PE_COFF_MACHINE_MIPSFPU16)
50
0
        return "mipsfpu16";
51
0
    if (val == FU_PE_COFF_MACHINE_POWERPC)
52
0
        return "powerpc";
53
0
    if (val == FU_PE_COFF_MACHINE_POWERPCFP)
54
0
        return "powerpcfp";
55
0
    if (val == FU_PE_COFF_MACHINE_R4000)
56
0
        return "r4000";
57
0
    if (val == FU_PE_COFF_MACHINE_RISCV32)
58
0
        return "riscv32";
59
0
    if (val == FU_PE_COFF_MACHINE_RISCV64)
60
0
        return "riscv64";
61
0
    if (val == FU_PE_COFF_MACHINE_RISCV128)
62
0
        return "riscv128";
63
0
    if (val == FU_PE_COFF_MACHINE_SH3)
64
0
        return "sh3";
65
0
    if (val == FU_PE_COFF_MACHINE_SH3DSP)
66
0
        return "sh3dsp";
67
0
    if (val == FU_PE_COFF_MACHINE_SH4)
68
0
        return "sh4";
69
0
    if (val == FU_PE_COFF_MACHINE_SH5)
70
0
        return "sh5";
71
0
    if (val == FU_PE_COFF_MACHINE_THUMB)
72
0
        return "thumb";
73
0
    if (val == FU_PE_COFF_MACHINE_WCEMIPSV2)
74
0
        return "wcemipsv2";
75
0
    return NULL;
76
0
}
77
78
static const gchar *
79
fu_pe_coff_magic_to_string(FuPeCoffMagic val)
80
0
{
81
0
    if (val == FU_PE_COFF_MAGIC_PE32)
82
0
        return "pe32";
83
0
    if (val == FU_PE_COFF_MAGIC_PE32_PLUS)
84
0
        return "pe32-plus";
85
0
    return NULL;
86
0
}
87
88
const gchar *
89
fu_coff_subsystem_to_string(FuCoffSubsystem val)
90
0
{
91
0
    if (val == FU_COFF_SUBSYSTEM_UNKNOWN)
92
0
        return "unknown";
93
0
    if (val == FU_COFF_SUBSYSTEM_NATIVE)
94
0
        return "native";
95
0
    if (val == FU_COFF_SUBSYSTEM_WINDOWS_GUI)
96
0
        return "windows-gui";
97
0
    if (val == FU_COFF_SUBSYSTEM_WINDOWS_CUI)
98
0
        return "windows-cui";
99
0
    if (val == FU_COFF_SUBSYSTEM_OS2_CUI)
100
0
        return "os2-cui";
101
0
    if (val == FU_COFF_SUBSYSTEM_POSIX_CUI)
102
0
        return "posix-cui";
103
0
    if (val == FU_COFF_SUBSYSTEM_NATIVE_WINDOWS)
104
0
        return "native-windows";
105
0
    if (val == FU_COFF_SUBSYSTEM_WINDOWS_CE_GUI)
106
0
        return "windows-ce-gui";
107
0
    if (val == FU_COFF_SUBSYSTEM_EFI_APPLICATION)
108
0
        return "efi-application";
109
0
    if (val == FU_COFF_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER)
110
0
        return "efi-boot-service-driver";
111
0
    if (val == FU_COFF_SUBSYSTEM_EFI_RUNTIME_DRIVER)
112
0
        return "efi-runtime-driver";
113
0
    if (val == FU_COFF_SUBSYSTEM_EFI_ROM)
114
0
        return "efi-rom";
115
0
    if (val == FU_COFF_SUBSYSTEM_XBOX)
116
0
        return "xbox";
117
0
    if (val == FU_COFF_SUBSYSTEM_WINDOWS_BOOT_APPLICATION)
118
0
        return "windows-boot-application";
119
0
    return NULL;
120
0
}
121
/* getters */
122
/**
123
 * fu_struct_pe_dos_header_get_magic: (skip):
124
 **/
125
static guint16
126
fu_struct_pe_dos_header_get_magic(const FuStructPeDosHeader *st)
127
0
{
128
0
    g_return_val_if_fail(st != NULL, 0x0);
129
0
    return fu_memread_uint16(st->data + 0, G_LITTLE_ENDIAN);
130
0
}
131
/**
132
 * fu_struct_pe_dos_header_get_cblp: (skip):
133
 **/
134
guint16
135
fu_struct_pe_dos_header_get_cblp(const FuStructPeDosHeader *st)
136
0
{
137
0
    g_return_val_if_fail(st != NULL, 0x0);
138
0
    return fu_memread_uint16(st->data + 2, G_LITTLE_ENDIAN);
139
0
}
140
/**
141
 * fu_struct_pe_dos_header_get_cp: (skip):
142
 **/
143
guint16
144
fu_struct_pe_dos_header_get_cp(const FuStructPeDosHeader *st)
145
0
{
146
0
    g_return_val_if_fail(st != NULL, 0x0);
147
0
    return fu_memread_uint16(st->data + 4, G_LITTLE_ENDIAN);
148
0
}
149
/**
150
 * fu_struct_pe_dos_header_get_crlc: (skip):
151
 **/
152
guint16
153
fu_struct_pe_dos_header_get_crlc(const FuStructPeDosHeader *st)
154
0
{
155
0
    g_return_val_if_fail(st != NULL, 0x0);
156
0
    return fu_memread_uint16(st->data + 6, G_LITTLE_ENDIAN);
157
0
}
158
/**
159
 * fu_struct_pe_dos_header_get_cparhdr: (skip):
160
 **/
161
guint16
162
fu_struct_pe_dos_header_get_cparhdr(const FuStructPeDosHeader *st)
163
0
{
164
0
    g_return_val_if_fail(st != NULL, 0x0);
165
0
    return fu_memread_uint16(st->data + 8, G_LITTLE_ENDIAN);
166
0
}
167
/**
168
 * fu_struct_pe_dos_header_get_minalloc: (skip):
169
 **/
170
guint16
171
fu_struct_pe_dos_header_get_minalloc(const FuStructPeDosHeader *st)
172
0
{
173
0
    g_return_val_if_fail(st != NULL, 0x0);
174
0
    return fu_memread_uint16(st->data + 10, G_LITTLE_ENDIAN);
175
0
}
176
/**
177
 * fu_struct_pe_dos_header_get_maxalloc: (skip):
178
 **/
179
guint16
180
fu_struct_pe_dos_header_get_maxalloc(const FuStructPeDosHeader *st)
181
0
{
182
0
    g_return_val_if_fail(st != NULL, 0x0);
183
0
    return fu_memread_uint16(st->data + 12, G_LITTLE_ENDIAN);
184
0
}
185
/**
186
 * fu_struct_pe_dos_header_get_sp: (skip):
187
 **/
188
guint16
189
fu_struct_pe_dos_header_get_sp(const FuStructPeDosHeader *st)
190
0
{
191
0
    g_return_val_if_fail(st != NULL, 0x0);
192
0
    return fu_memread_uint16(st->data + 16, G_LITTLE_ENDIAN);
193
0
}
194
/**
195
 * fu_struct_pe_dos_header_get_lfarlc: (skip):
196
 **/
197
guint16
198
fu_struct_pe_dos_header_get_lfarlc(const FuStructPeDosHeader *st)
199
0
{
200
0
    g_return_val_if_fail(st != NULL, 0x0);
201
0
    return fu_memread_uint16(st->data + 24, G_LITTLE_ENDIAN);
202
0
}
203
/**
204
 * fu_struct_pe_dos_header_get_lfanew: (skip):
205
 **/
206
guint32
207
fu_struct_pe_dos_header_get_lfanew(const FuStructPeDosHeader *st)
208
0
{
209
0
    g_return_val_if_fail(st != NULL, 0x0);
210
0
    return fu_memread_uint32(st->data + 60, G_LITTLE_ENDIAN);
211
0
}
212
213
/* setters */
214
/**
215
 * fu_struct_pe_dos_header_set_magic: (skip):
216
 **/
217
static void
218
fu_struct_pe_dos_header_set_magic(FuStructPeDosHeader *st, guint16 value)
219
0
{
220
0
    g_return_if_fail(st != NULL);
221
0
    fu_memwrite_uint16(st->data + 0, value, G_LITTLE_ENDIAN);
222
0
}
223
/**
224
 * fu_struct_pe_dos_header_set_cblp: (skip):
225
 **/
226
void
227
fu_struct_pe_dos_header_set_cblp(FuStructPeDosHeader *st, guint16 value)
228
0
{
229
0
    g_return_if_fail(st != NULL);
230
0
    fu_memwrite_uint16(st->data + 2, value, G_LITTLE_ENDIAN);
231
0
}
232
/**
233
 * fu_struct_pe_dos_header_set_cp: (skip):
234
 **/
235
void
236
fu_struct_pe_dos_header_set_cp(FuStructPeDosHeader *st, guint16 value)
237
0
{
238
0
    g_return_if_fail(st != NULL);
239
0
    fu_memwrite_uint16(st->data + 4, value, G_LITTLE_ENDIAN);
240
0
}
241
/**
242
 * fu_struct_pe_dos_header_set_crlc: (skip):
243
 **/
244
void
245
fu_struct_pe_dos_header_set_crlc(FuStructPeDosHeader *st, guint16 value)
246
0
{
247
0
    g_return_if_fail(st != NULL);
248
0
    fu_memwrite_uint16(st->data + 6, value, G_LITTLE_ENDIAN);
249
0
}
250
/**
251
 * fu_struct_pe_dos_header_set_cparhdr: (skip):
252
 **/
253
void
254
fu_struct_pe_dos_header_set_cparhdr(FuStructPeDosHeader *st, guint16 value)
255
0
{
256
0
    g_return_if_fail(st != NULL);
257
0
    fu_memwrite_uint16(st->data + 8, value, G_LITTLE_ENDIAN);
258
0
}
259
/**
260
 * fu_struct_pe_dos_header_set_minalloc: (skip):
261
 **/
262
void
263
fu_struct_pe_dos_header_set_minalloc(FuStructPeDosHeader *st, guint16 value)
264
0
{
265
0
    g_return_if_fail(st != NULL);
266
0
    fu_memwrite_uint16(st->data + 10, value, G_LITTLE_ENDIAN);
267
0
}
268
/**
269
 * fu_struct_pe_dos_header_set_maxalloc: (skip):
270
 **/
271
void
272
fu_struct_pe_dos_header_set_maxalloc(FuStructPeDosHeader *st, guint16 value)
273
0
{
274
0
    g_return_if_fail(st != NULL);
275
0
    fu_memwrite_uint16(st->data + 12, value, G_LITTLE_ENDIAN);
276
0
}
277
/**
278
 * fu_struct_pe_dos_header_set_sp: (skip):
279
 **/
280
void
281
fu_struct_pe_dos_header_set_sp(FuStructPeDosHeader *st, guint16 value)
282
0
{
283
0
    g_return_if_fail(st != NULL);
284
0
    fu_memwrite_uint16(st->data + 16, value, G_LITTLE_ENDIAN);
285
0
}
286
/**
287
 * fu_struct_pe_dos_header_set_lfarlc: (skip):
288
 **/
289
void
290
fu_struct_pe_dos_header_set_lfarlc(FuStructPeDosHeader *st, guint16 value)
291
0
{
292
0
    g_return_if_fail(st != NULL);
293
0
    fu_memwrite_uint16(st->data + 24, value, G_LITTLE_ENDIAN);
294
0
}
295
/**
296
 * fu_struct_pe_dos_header_set_lfanew: (skip):
297
 **/
298
void
299
fu_struct_pe_dos_header_set_lfanew(FuStructPeDosHeader *st, guint32 value)
300
0
{
301
0
    g_return_if_fail(st != NULL);
302
0
    fu_memwrite_uint32(st->data + 60, value, G_LITTLE_ENDIAN);
303
0
}
304
/**
305
 * fu_struct_pe_dos_header_new: (skip):
306
 **/
307
FuStructPeDosHeader *
308
fu_struct_pe_dos_header_new(void)
309
0
{
310
0
    FuStructPeDosHeader *st = g_byte_array_sized_new(128);
311
0
    fu_byte_array_set_size(st, 128, 0x0);
312
0
    fu_struct_pe_dos_header_set_magic(st, 0x5A4D);
313
0
    fu_struct_pe_dos_header_set_cblp(st, 0x90);
314
0
    fu_struct_pe_dos_header_set_cp(st, 0x3);
315
0
    fu_struct_pe_dos_header_set_cparhdr(st, 0x4);
316
0
    fu_struct_pe_dos_header_set_maxalloc(st, 0xFFFF);
317
0
    fu_struct_pe_dos_header_set_sp(st, 0xB8);
318
0
    fu_struct_pe_dos_header_set_lfarlc(st, 0x40);
319
0
    fu_struct_pe_dos_header_set_lfanew(st, 0x80);
320
0
    return st;
321
0
}
322
/**
323
 * fu_struct_pe_dos_header_to_string: (skip):
324
 **/
325
static gchar *
326
fu_struct_pe_dos_header_to_string(const FuStructPeDosHeader *st)
327
0
{
328
0
    g_autoptr(GString) str = g_string_new("FuStructPeDosHeader:\n");
329
0
    g_return_val_if_fail(st != NULL, NULL);
330
0
    g_string_append_printf(str, "  cblp: 0x%x\n",
331
0
                           (guint) fu_struct_pe_dos_header_get_cblp(st));
332
0
    g_string_append_printf(str, "  cp: 0x%x\n",
333
0
                           (guint) fu_struct_pe_dos_header_get_cp(st));
334
0
    g_string_append_printf(str, "  crlc: 0x%x\n",
335
0
                           (guint) fu_struct_pe_dos_header_get_crlc(st));
336
0
    g_string_append_printf(str, "  cparhdr: 0x%x\n",
337
0
                           (guint) fu_struct_pe_dos_header_get_cparhdr(st));
338
0
    g_string_append_printf(str, "  minalloc: 0x%x\n",
339
0
                           (guint) fu_struct_pe_dos_header_get_minalloc(st));
340
0
    g_string_append_printf(str, "  maxalloc: 0x%x\n",
341
0
                           (guint) fu_struct_pe_dos_header_get_maxalloc(st));
342
0
    g_string_append_printf(str, "  sp: 0x%x\n",
343
0
                           (guint) fu_struct_pe_dos_header_get_sp(st));
344
0
    g_string_append_printf(str, "  lfarlc: 0x%x\n",
345
0
                           (guint) fu_struct_pe_dos_header_get_lfarlc(st));
346
0
    g_string_append_printf(str, "  lfanew: 0x%x\n",
347
0
                           (guint) fu_struct_pe_dos_header_get_lfanew(st));
348
0
    if (str->len > 0)
349
0
        g_string_set_size(str, str->len - 1);
350
0
    return g_string_free(g_steal_pointer(&str), FALSE);
351
0
}
352
static gboolean
353
fu_struct_pe_dos_header_validate_internal(FuStructPeDosHeader *st, GError **error)
354
0
{
355
0
    g_return_val_if_fail(st != NULL, FALSE);
356
0
    if (fu_struct_pe_dos_header_get_magic(st) != 0x5A4D) {
357
0
        g_set_error(error,
358
0
                    FWUPD_ERROR,
359
0
                    FWUPD_ERROR_INVALID_DATA,
360
0
                    "constant FuStructPeDosHeader.magic was not valid, "
361
0
                    "expected 0x%x and got 0x%x",
362
0
                    (guint) 0x5A4D,
363
0
                    (guint) fu_struct_pe_dos_header_get_magic(st));
364
0
        return FALSE;
365
0
    }
366
0
    return TRUE;
367
0
}
368
/**
369
 * fu_struct_pe_dos_header_validate_stream: (skip):
370
 **/
371
gboolean
372
fu_struct_pe_dos_header_validate_stream(GInputStream *stream, gsize offset, GError **error)
373
0
{
374
0
    g_autoptr(GByteArray) st = NULL;
375
0
    g_return_val_if_fail(G_IS_INPUT_STREAM(stream), FALSE);
376
0
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
377
0
    st = fu_input_stream_read_byte_array(stream, offset, 128, NULL, error);
378
0
    if (st == NULL) {
379
0
        g_prefix_error(error, "FuStructPeDosHeader failed read of 0x%x: ", (guint) 128);
380
0
        return FALSE;
381
0
    }
382
0
    if (st->len != 128) {
383
0
        g_set_error(error,
384
0
                    FWUPD_ERROR,
385
0
                    FWUPD_ERROR_INVALID_DATA,
386
0
                    "FuStructPeDosHeader requested 0x%x and got 0x%x",
387
0
                    (guint) 128,
388
0
                    (guint) st->len);
389
0
        return FALSE;
390
0
    }
391
0
    return fu_struct_pe_dos_header_validate_internal(st, error);
392
0
}
393
static gboolean
394
fu_struct_pe_dos_header_parse_internal(FuStructPeDosHeader *st, GError **error)
395
0
{
396
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
397
0
        g_autofree gchar *str = fu_struct_pe_dos_header_to_string(st);
398
0
        g_debug("%s", str);
399
0
    }
400
0
    if (!fu_struct_pe_dos_header_validate_internal(st, error))
401
0
        return FALSE;
402
0
    return TRUE;
403
0
}
404
/**
405
 * fu_struct_pe_dos_header_parse_stream: (skip):
406
 **/
407
FuStructPeDosHeader *
408
fu_struct_pe_dos_header_parse_stream(GInputStream *stream, gsize offset, GError **error)
409
0
{
410
0
    g_autoptr(GByteArray) st = NULL;
411
0
    st = fu_input_stream_read_byte_array(stream, offset, 128, NULL, error);
412
0
    if (st == NULL) {
413
0
        g_prefix_error(error, "FuStructPeDosHeader failed read of 0x%x: ", (guint) 128);
414
0
        return NULL;
415
0
    }
416
0
    if (st->len != 128) {
417
0
        g_set_error(error,
418
0
                    FWUPD_ERROR,
419
0
                    FWUPD_ERROR_INVALID_DATA,
420
0
                    "FuStructPeDosHeader requested 0x%x and got 0x%x",
421
0
                    (guint) 128,
422
0
                    (guint) st->len);
423
0
        return NULL;
424
0
    }
425
0
    if (!fu_struct_pe_dos_header_parse_internal(st, error))
426
0
        return NULL;
427
0
    return g_steal_pointer(&st);
428
0
}
429
/* getters */
430
/**
431
 * fu_struct_pe_coff_file_header_get_signature: (skip):
432
 **/
433
static guint32
434
fu_struct_pe_coff_file_header_get_signature(const FuStructPeCoffFileHeader *st)
435
0
{
436
0
    g_return_val_if_fail(st != NULL, 0x0);
437
0
    return fu_memread_uint32(st->data + 0, G_LITTLE_ENDIAN);
438
0
}
439
/**
440
 * fu_struct_pe_coff_file_header_get_machine: (skip):
441
 **/
442
FuPeCoffMachine
443
fu_struct_pe_coff_file_header_get_machine(const FuStructPeCoffFileHeader *st)
444
0
{
445
0
    g_return_val_if_fail(st != NULL, 0x0);
446
0
    return fu_memread_uint16(st->data + 4, G_LITTLE_ENDIAN);
447
0
}
448
/**
449
 * fu_struct_pe_coff_file_header_get_number_of_sections: (skip):
450
 **/
451
guint16
452
fu_struct_pe_coff_file_header_get_number_of_sections(const FuStructPeCoffFileHeader *st)
453
0
{
454
0
    g_return_val_if_fail(st != NULL, 0x0);
455
0
    return fu_memread_uint16(st->data + 6, G_LITTLE_ENDIAN);
456
0
}
457
/**
458
 * fu_struct_pe_coff_file_header_get_pointer_to_symbol_table: (skip):
459
 **/
460
guint32
461
fu_struct_pe_coff_file_header_get_pointer_to_symbol_table(const FuStructPeCoffFileHeader *st)
462
0
{
463
0
    g_return_val_if_fail(st != NULL, 0x0);
464
0
    return fu_memread_uint32(st->data + 12, G_LITTLE_ENDIAN);
465
0
}
466
/**
467
 * fu_struct_pe_coff_file_header_get_number_of_symbols: (skip):
468
 **/
469
guint32
470
fu_struct_pe_coff_file_header_get_number_of_symbols(const FuStructPeCoffFileHeader *st)
471
0
{
472
0
    g_return_val_if_fail(st != NULL, 0x0);
473
0
    return fu_memread_uint32(st->data + 16, G_LITTLE_ENDIAN);
474
0
}
475
/**
476
 * fu_struct_pe_coff_file_header_get_size_of_optional_header: (skip):
477
 **/
478
guint16
479
fu_struct_pe_coff_file_header_get_size_of_optional_header(const FuStructPeCoffFileHeader *st)
480
0
{
481
0
    g_return_val_if_fail(st != NULL, 0x0);
482
0
    return fu_memread_uint16(st->data + 20, G_LITTLE_ENDIAN);
483
0
}
484
/**
485
 * fu_struct_pe_coff_file_header_get_characteristics: (skip):
486
 **/
487
guint16
488
fu_struct_pe_coff_file_header_get_characteristics(const FuStructPeCoffFileHeader *st)
489
0
{
490
0
    g_return_val_if_fail(st != NULL, 0x0);
491
0
    return fu_memread_uint16(st->data + 22, G_LITTLE_ENDIAN);
492
0
}
493
494
/* setters */
495
/**
496
 * fu_struct_pe_coff_file_header_set_signature: (skip):
497
 **/
498
static void
499
fu_struct_pe_coff_file_header_set_signature(FuStructPeCoffFileHeader *st, guint32 value)
500
0
{
501
0
    g_return_if_fail(st != NULL);
502
0
    fu_memwrite_uint32(st->data + 0, value, G_LITTLE_ENDIAN);
503
0
}
504
/**
505
 * fu_struct_pe_coff_file_header_set_machine: (skip):
506
 **/
507
void
508
fu_struct_pe_coff_file_header_set_machine(FuStructPeCoffFileHeader *st, FuPeCoffMachine value)
509
0
{
510
0
    g_return_if_fail(st != NULL);
511
0
    fu_memwrite_uint16(st->data + 4, value, G_LITTLE_ENDIAN);
512
0
}
513
/**
514
 * fu_struct_pe_coff_file_header_set_number_of_sections: (skip):
515
 **/
516
void
517
fu_struct_pe_coff_file_header_set_number_of_sections(FuStructPeCoffFileHeader *st, guint16 value)
518
0
{
519
0
    g_return_if_fail(st != NULL);
520
0
    fu_memwrite_uint16(st->data + 6, value, G_LITTLE_ENDIAN);
521
0
}
522
/**
523
 * fu_struct_pe_coff_file_header_set_pointer_to_symbol_table: (skip):
524
 **/
525
void
526
fu_struct_pe_coff_file_header_set_pointer_to_symbol_table(FuStructPeCoffFileHeader *st, guint32 value)
527
0
{
528
0
    g_return_if_fail(st != NULL);
529
0
    fu_memwrite_uint32(st->data + 12, value, G_LITTLE_ENDIAN);
530
0
}
531
/**
532
 * fu_struct_pe_coff_file_header_set_number_of_symbols: (skip):
533
 **/
534
void
535
fu_struct_pe_coff_file_header_set_number_of_symbols(FuStructPeCoffFileHeader *st, guint32 value)
536
0
{
537
0
    g_return_if_fail(st != NULL);
538
0
    fu_memwrite_uint32(st->data + 16, value, G_LITTLE_ENDIAN);
539
0
}
540
/**
541
 * fu_struct_pe_coff_file_header_set_size_of_optional_header: (skip):
542
 **/
543
void
544
fu_struct_pe_coff_file_header_set_size_of_optional_header(FuStructPeCoffFileHeader *st, guint16 value)
545
0
{
546
0
    g_return_if_fail(st != NULL);
547
0
    fu_memwrite_uint16(st->data + 20, value, G_LITTLE_ENDIAN);
548
0
}
549
/**
550
 * fu_struct_pe_coff_file_header_set_characteristics: (skip):
551
 **/
552
void
553
fu_struct_pe_coff_file_header_set_characteristics(FuStructPeCoffFileHeader *st, guint16 value)
554
0
{
555
0
    g_return_if_fail(st != NULL);
556
0
    fu_memwrite_uint16(st->data + 22, value, G_LITTLE_ENDIAN);
557
0
}
558
/**
559
 * fu_struct_pe_coff_file_header_new: (skip):
560
 **/
561
FuStructPeCoffFileHeader *
562
fu_struct_pe_coff_file_header_new(void)
563
0
{
564
0
    FuStructPeCoffFileHeader *st = g_byte_array_sized_new(24);
565
0
    fu_byte_array_set_size(st, 24, 0x0);
566
0
    fu_struct_pe_coff_file_header_set_signature(st, 0x4550);
567
0
    fu_struct_pe_coff_file_header_set_machine(st, FU_PE_COFF_MACHINE_AMD64);
568
0
    fu_struct_pe_coff_file_header_set_size_of_optional_header(st, 0xf0);
569
0
    fu_struct_pe_coff_file_header_set_characteristics(st, 0x2022);
570
0
    return st;
571
0
}
572
/**
573
 * fu_struct_pe_coff_file_header_to_string: (skip):
574
 **/
575
static gchar *
576
fu_struct_pe_coff_file_header_to_string(const FuStructPeCoffFileHeader *st)
577
0
{
578
0
    g_autoptr(GString) str = g_string_new("FuStructPeCoffFileHeader:\n");
579
0
    g_return_val_if_fail(st != NULL, NULL);
580
0
    {
581
0
        const gchar *tmp = fu_pe_coff_machine_to_string(fu_struct_pe_coff_file_header_get_machine(st));
582
0
        if (tmp != NULL) {
583
0
            g_string_append_printf(str, "  machine: 0x%x [%s]\n", (guint) fu_struct_pe_coff_file_header_get_machine(st), tmp);
584
0
        } else {
585
0
            g_string_append_printf(str, "  machine: 0x%x\n", (guint) fu_struct_pe_coff_file_header_get_machine(st));
586
0
        }
587
0
    }
588
0
    g_string_append_printf(str, "  number_of_sections: 0x%x\n",
589
0
                           (guint) fu_struct_pe_coff_file_header_get_number_of_sections(st));
590
0
    g_string_append_printf(str, "  pointer_to_symbol_table: 0x%x\n",
591
0
                           (guint) fu_struct_pe_coff_file_header_get_pointer_to_symbol_table(st));
592
0
    g_string_append_printf(str, "  number_of_symbols: 0x%x\n",
593
0
                           (guint) fu_struct_pe_coff_file_header_get_number_of_symbols(st));
594
0
    g_string_append_printf(str, "  size_of_optional_header: 0x%x\n",
595
0
                           (guint) fu_struct_pe_coff_file_header_get_size_of_optional_header(st));
596
0
    g_string_append_printf(str, "  characteristics: 0x%x\n",
597
0
                           (guint) fu_struct_pe_coff_file_header_get_characteristics(st));
598
0
    if (str->len > 0)
599
0
        g_string_set_size(str, str->len - 1);
600
0
    return g_string_free(g_steal_pointer(&str), FALSE);
601
0
}
602
static gboolean
603
fu_struct_pe_coff_file_header_validate_internal(FuStructPeCoffFileHeader *st, GError **error)
604
0
{
605
0
    g_return_val_if_fail(st != NULL, FALSE);
606
0
    if (fu_struct_pe_coff_file_header_get_signature(st) != 0x4550) {
607
0
        g_set_error(error,
608
0
                    FWUPD_ERROR,
609
0
                    FWUPD_ERROR_INVALID_DATA,
610
0
                    "constant FuStructPeCoffFileHeader.signature was not valid, "
611
0
                    "expected 0x%x and got 0x%x",
612
0
                    (guint) 0x4550,
613
0
                    (guint) fu_struct_pe_coff_file_header_get_signature(st));
614
0
        return FALSE;
615
0
    }
616
0
    return TRUE;
617
0
}
618
static gboolean
619
fu_struct_pe_coff_file_header_parse_internal(FuStructPeCoffFileHeader *st, GError **error)
620
0
{
621
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
622
0
        g_autofree gchar *str = fu_struct_pe_coff_file_header_to_string(st);
623
0
        g_debug("%s", str);
624
0
    }
625
0
    if (!fu_struct_pe_coff_file_header_validate_internal(st, error))
626
0
        return FALSE;
627
0
    return TRUE;
628
0
}
629
/**
630
 * fu_struct_pe_coff_file_header_parse_stream: (skip):
631
 **/
632
FuStructPeCoffFileHeader *
633
fu_struct_pe_coff_file_header_parse_stream(GInputStream *stream, gsize offset, GError **error)
634
0
{
635
0
    g_autoptr(GByteArray) st = NULL;
636
0
    st = fu_input_stream_read_byte_array(stream, offset, 24, NULL, error);
637
0
    if (st == NULL) {
638
0
        g_prefix_error(error, "FuStructPeCoffFileHeader failed read of 0x%x: ", (guint) 24);
639
0
        return NULL;
640
0
    }
641
0
    if (st->len != 24) {
642
0
        g_set_error(error,
643
0
                    FWUPD_ERROR,
644
0
                    FWUPD_ERROR_INVALID_DATA,
645
0
                    "FuStructPeCoffFileHeader requested 0x%x and got 0x%x",
646
0
                    (guint) 24,
647
0
                    (guint) st->len);
648
0
        return NULL;
649
0
    }
650
0
    if (!fu_struct_pe_coff_file_header_parse_internal(st, error))
651
0
        return NULL;
652
0
    return g_steal_pointer(&st);
653
0
}
654
/* getters */
655
/**
656
 * fu_struct_pe_coff_optional_header64_get_magic: (skip):
657
 **/
658
FuPeCoffMagic
659
fu_struct_pe_coff_optional_header64_get_magic(const FuStructPeCoffOptionalHeader64 *st)
660
0
{
661
0
    g_return_val_if_fail(st != NULL, 0x0);
662
0
    return fu_memread_uint16(st->data + 0, G_LITTLE_ENDIAN);
663
0
}
664
/**
665
 * fu_struct_pe_coff_optional_header64_get_major_linker_version: (skip):
666
 **/
667
guint8
668
fu_struct_pe_coff_optional_header64_get_major_linker_version(const FuStructPeCoffOptionalHeader64 *st)
669
0
{
670
0
    g_return_val_if_fail(st != NULL, 0x0);
671
0
    return st->data[2];
672
0
}
673
/**
674
 * fu_struct_pe_coff_optional_header64_get_minor_linker_version: (skip):
675
 **/
676
guint8
677
fu_struct_pe_coff_optional_header64_get_minor_linker_version(const FuStructPeCoffOptionalHeader64 *st)
678
0
{
679
0
    g_return_val_if_fail(st != NULL, 0x0);
680
0
    return st->data[3];
681
0
}
682
/**
683
 * fu_struct_pe_coff_optional_header64_get_size_of_code: (skip):
684
 **/
685
guint32
686
fu_struct_pe_coff_optional_header64_get_size_of_code(const FuStructPeCoffOptionalHeader64 *st)
687
0
{
688
0
    g_return_val_if_fail(st != NULL, 0x0);
689
0
    return fu_memread_uint32(st->data + 4, G_LITTLE_ENDIAN);
690
0
}
691
/**
692
 * fu_struct_pe_coff_optional_header64_get_size_of_initialized_data: (skip):
693
 **/
694
guint32
695
fu_struct_pe_coff_optional_header64_get_size_of_initialized_data(const FuStructPeCoffOptionalHeader64 *st)
696
0
{
697
0
    g_return_val_if_fail(st != NULL, 0x0);
698
0
    return fu_memread_uint32(st->data + 8, G_LITTLE_ENDIAN);
699
0
}
700
/**
701
 * fu_struct_pe_coff_optional_header64_get_size_of_uninitialized_data: (skip):
702
 **/
703
guint32
704
fu_struct_pe_coff_optional_header64_get_size_of_uninitialized_data(const FuStructPeCoffOptionalHeader64 *st)
705
0
{
706
0
    g_return_val_if_fail(st != NULL, 0x0);
707
0
    return fu_memread_uint32(st->data + 12, G_LITTLE_ENDIAN);
708
0
}
709
/**
710
 * fu_struct_pe_coff_optional_header64_get_addressofentrypoint: (skip):
711
 **/
712
guint32
713
fu_struct_pe_coff_optional_header64_get_addressofentrypoint(const FuStructPeCoffOptionalHeader64 *st)
714
0
{
715
0
    g_return_val_if_fail(st != NULL, 0x0);
716
0
    return fu_memread_uint32(st->data + 16, G_LITTLE_ENDIAN);
717
0
}
718
/**
719
 * fu_struct_pe_coff_optional_header64_get_base_of_code: (skip):
720
 **/
721
guint32
722
fu_struct_pe_coff_optional_header64_get_base_of_code(const FuStructPeCoffOptionalHeader64 *st)
723
0
{
724
0
    g_return_val_if_fail(st != NULL, 0x0);
725
0
    return fu_memread_uint32(st->data + 20, G_LITTLE_ENDIAN);
726
0
}
727
/**
728
 * fu_struct_pe_coff_optional_header64_get_image_base: (skip):
729
 **/
730
guint64
731
fu_struct_pe_coff_optional_header64_get_image_base(const FuStructPeCoffOptionalHeader64 *st)
732
0
{
733
0
    g_return_val_if_fail(st != NULL, 0x0);
734
0
    return fu_memread_uint64(st->data + 24, G_LITTLE_ENDIAN);
735
0
}
736
/**
737
 * fu_struct_pe_coff_optional_header64_get_section_alignment: (skip):
738
 **/
739
guint32
740
fu_struct_pe_coff_optional_header64_get_section_alignment(const FuStructPeCoffOptionalHeader64 *st)
741
0
{
742
0
    g_return_val_if_fail(st != NULL, 0x0);
743
0
    return fu_memread_uint32(st->data + 32, G_LITTLE_ENDIAN);
744
0
}
745
/**
746
 * fu_struct_pe_coff_optional_header64_get_file_alignment: (skip):
747
 **/
748
guint32
749
fu_struct_pe_coff_optional_header64_get_file_alignment(const FuStructPeCoffOptionalHeader64 *st)
750
0
{
751
0
    g_return_val_if_fail(st != NULL, 0x0);
752
0
    return fu_memread_uint32(st->data + 36, G_LITTLE_ENDIAN);
753
0
}
754
/**
755
 * fu_struct_pe_coff_optional_header64_get_size_of_image: (skip):
756
 **/
757
guint32
758
fu_struct_pe_coff_optional_header64_get_size_of_image(const FuStructPeCoffOptionalHeader64 *st)
759
0
{
760
0
    g_return_val_if_fail(st != NULL, 0x0);
761
0
    return fu_memread_uint32(st->data + 56, G_LITTLE_ENDIAN);
762
0
}
763
/**
764
 * fu_struct_pe_coff_optional_header64_get_size_of_headers: (skip):
765
 **/
766
guint32
767
fu_struct_pe_coff_optional_header64_get_size_of_headers(const FuStructPeCoffOptionalHeader64 *st)
768
0
{
769
0
    g_return_val_if_fail(st != NULL, 0x0);
770
0
    return fu_memread_uint32(st->data + 60, G_LITTLE_ENDIAN);
771
0
}
772
/**
773
 * fu_struct_pe_coff_optional_header64_get_checksum: (skip):
774
 **/
775
guint32
776
fu_struct_pe_coff_optional_header64_get_checksum(const FuStructPeCoffOptionalHeader64 *st)
777
0
{
778
0
    g_return_val_if_fail(st != NULL, 0x0);
779
0
    return fu_memread_uint32(st->data + 64, G_LITTLE_ENDIAN);
780
0
}
781
/**
782
 * fu_struct_pe_coff_optional_header64_get_subsystem: (skip):
783
 **/
784
FuCoffSubsystem
785
fu_struct_pe_coff_optional_header64_get_subsystem(const FuStructPeCoffOptionalHeader64 *st)
786
0
{
787
0
    g_return_val_if_fail(st != NULL, 0x0);
788
0
    return fu_memread_uint16(st->data + 68, G_LITTLE_ENDIAN);
789
0
}
790
/**
791
 * fu_struct_pe_coff_optional_header64_get_loader_flags: (skip):
792
 **/
793
guint32
794
fu_struct_pe_coff_optional_header64_get_loader_flags(const FuStructPeCoffOptionalHeader64 *st)
795
0
{
796
0
    g_return_val_if_fail(st != NULL, 0x0);
797
0
    return fu_memread_uint32(st->data + 104, G_LITTLE_ENDIAN);
798
0
}
799
/**
800
 * fu_struct_pe_coff_optional_header64_get_number_of_rva_and_sizes: (skip):
801
 **/
802
guint32
803
fu_struct_pe_coff_optional_header64_get_number_of_rva_and_sizes(const FuStructPeCoffOptionalHeader64 *st)
804
0
{
805
0
    g_return_val_if_fail(st != NULL, 0x0);
806
0
    return fu_memread_uint32(st->data + 108, G_LITTLE_ENDIAN);
807
0
}
808
/**
809
 * fu_struct_pe_coff_optional_header64_get_certificate_table: (skip):
810
 **/
811
guint32
812
fu_struct_pe_coff_optional_header64_get_certificate_table(const FuStructPeCoffOptionalHeader64 *st)
813
0
{
814
0
    g_return_val_if_fail(st != NULL, 0x0);
815
0
    return fu_memread_uint32(st->data + 144, G_LITTLE_ENDIAN);
816
0
}
817
/**
818
 * fu_struct_pe_coff_optional_header64_get_size_of_certificate_table: (skip):
819
 **/
820
guint32
821
fu_struct_pe_coff_optional_header64_get_size_of_certificate_table(const FuStructPeCoffOptionalHeader64 *st)
822
0
{
823
0
    g_return_val_if_fail(st != NULL, 0x0);
824
0
    return fu_memread_uint32(st->data + 148, G_LITTLE_ENDIAN);
825
0
}
826
/**
827
 * fu_struct_pe_coff_optional_header64_get_debug_table: (skip):
828
 **/
829
guint32
830
fu_struct_pe_coff_optional_header64_get_debug_table(const FuStructPeCoffOptionalHeader64 *st)
831
0
{
832
0
    g_return_val_if_fail(st != NULL, 0x0);
833
0
    return fu_memread_uint32(st->data + 152, G_LITTLE_ENDIAN);
834
0
}
835
/**
836
 * fu_struct_pe_coff_optional_header64_get_size_of_debug_table: (skip):
837
 **/
838
guint32
839
fu_struct_pe_coff_optional_header64_get_size_of_debug_table(const FuStructPeCoffOptionalHeader64 *st)
840
0
{
841
0
    g_return_val_if_fail(st != NULL, 0x0);
842
0
    return fu_memread_uint32(st->data + 156, G_LITTLE_ENDIAN);
843
0
}
844
845
/* setters */
846
/**
847
 * fu_struct_pe_coff_optional_header64_set_magic: (skip):
848
 **/
849
void
850
fu_struct_pe_coff_optional_header64_set_magic(FuStructPeCoffOptionalHeader64 *st, FuPeCoffMagic value)
851
0
{
852
0
    g_return_if_fail(st != NULL);
853
0
    fu_memwrite_uint16(st->data + 0, value, G_LITTLE_ENDIAN);
854
0
}
855
/**
856
 * fu_struct_pe_coff_optional_header64_set_major_linker_version: (skip):
857
 **/
858
void
859
fu_struct_pe_coff_optional_header64_set_major_linker_version(FuStructPeCoffOptionalHeader64 *st, guint8 value)
860
0
{
861
0
    g_return_if_fail(st != NULL);
862
0
    st->data[2] = value;
863
0
}
864
/**
865
 * fu_struct_pe_coff_optional_header64_set_minor_linker_version: (skip):
866
 **/
867
void
868
fu_struct_pe_coff_optional_header64_set_minor_linker_version(FuStructPeCoffOptionalHeader64 *st, guint8 value)
869
0
{
870
0
    g_return_if_fail(st != NULL);
871
0
    st->data[3] = value;
872
0
}
873
/**
874
 * fu_struct_pe_coff_optional_header64_set_size_of_code: (skip):
875
 **/
876
void
877
fu_struct_pe_coff_optional_header64_set_size_of_code(FuStructPeCoffOptionalHeader64 *st, guint32 value)
878
0
{
879
0
    g_return_if_fail(st != NULL);
880
0
    fu_memwrite_uint32(st->data + 4, value, G_LITTLE_ENDIAN);
881
0
}
882
/**
883
 * fu_struct_pe_coff_optional_header64_set_size_of_initialized_data: (skip):
884
 **/
885
void
886
fu_struct_pe_coff_optional_header64_set_size_of_initialized_data(FuStructPeCoffOptionalHeader64 *st, guint32 value)
887
0
{
888
0
    g_return_if_fail(st != NULL);
889
0
    fu_memwrite_uint32(st->data + 8, value, G_LITTLE_ENDIAN);
890
0
}
891
/**
892
 * fu_struct_pe_coff_optional_header64_set_size_of_uninitialized_data: (skip):
893
 **/
894
void
895
fu_struct_pe_coff_optional_header64_set_size_of_uninitialized_data(FuStructPeCoffOptionalHeader64 *st, guint32 value)
896
0
{
897
0
    g_return_if_fail(st != NULL);
898
0
    fu_memwrite_uint32(st->data + 12, value, G_LITTLE_ENDIAN);
899
0
}
900
/**
901
 * fu_struct_pe_coff_optional_header64_set_addressofentrypoint: (skip):
902
 **/
903
void
904
fu_struct_pe_coff_optional_header64_set_addressofentrypoint(FuStructPeCoffOptionalHeader64 *st, guint32 value)
905
0
{
906
0
    g_return_if_fail(st != NULL);
907
0
    fu_memwrite_uint32(st->data + 16, value, G_LITTLE_ENDIAN);
908
0
}
909
/**
910
 * fu_struct_pe_coff_optional_header64_set_base_of_code: (skip):
911
 **/
912
void
913
fu_struct_pe_coff_optional_header64_set_base_of_code(FuStructPeCoffOptionalHeader64 *st, guint32 value)
914
0
{
915
0
    g_return_if_fail(st != NULL);
916
0
    fu_memwrite_uint32(st->data + 20, value, G_LITTLE_ENDIAN);
917
0
}
918
/**
919
 * fu_struct_pe_coff_optional_header64_set_image_base: (skip):
920
 **/
921
void
922
fu_struct_pe_coff_optional_header64_set_image_base(FuStructPeCoffOptionalHeader64 *st, guint64 value)
923
0
{
924
0
    g_return_if_fail(st != NULL);
925
0
    fu_memwrite_uint64(st->data + 24, value, G_LITTLE_ENDIAN);
926
0
}
927
/**
928
 * fu_struct_pe_coff_optional_header64_set_section_alignment: (skip):
929
 **/
930
void
931
fu_struct_pe_coff_optional_header64_set_section_alignment(FuStructPeCoffOptionalHeader64 *st, guint32 value)
932
0
{
933
0
    g_return_if_fail(st != NULL);
934
0
    fu_memwrite_uint32(st->data + 32, value, G_LITTLE_ENDIAN);
935
0
}
936
/**
937
 * fu_struct_pe_coff_optional_header64_set_file_alignment: (skip):
938
 **/
939
void
940
fu_struct_pe_coff_optional_header64_set_file_alignment(FuStructPeCoffOptionalHeader64 *st, guint32 value)
941
0
{
942
0
    g_return_if_fail(st != NULL);
943
0
    fu_memwrite_uint32(st->data + 36, value, G_LITTLE_ENDIAN);
944
0
}
945
/**
946
 * fu_struct_pe_coff_optional_header64_set_size_of_image: (skip):
947
 **/
948
void
949
fu_struct_pe_coff_optional_header64_set_size_of_image(FuStructPeCoffOptionalHeader64 *st, guint32 value)
950
0
{
951
0
    g_return_if_fail(st != NULL);
952
0
    fu_memwrite_uint32(st->data + 56, value, G_LITTLE_ENDIAN);
953
0
}
954
/**
955
 * fu_struct_pe_coff_optional_header64_set_size_of_headers: (skip):
956
 **/
957
void
958
fu_struct_pe_coff_optional_header64_set_size_of_headers(FuStructPeCoffOptionalHeader64 *st, guint32 value)
959
0
{
960
0
    g_return_if_fail(st != NULL);
961
0
    fu_memwrite_uint32(st->data + 60, value, G_LITTLE_ENDIAN);
962
0
}
963
/**
964
 * fu_struct_pe_coff_optional_header64_set_checksum: (skip):
965
 **/
966
void
967
fu_struct_pe_coff_optional_header64_set_checksum(FuStructPeCoffOptionalHeader64 *st, guint32 value)
968
0
{
969
0
    g_return_if_fail(st != NULL);
970
0
    fu_memwrite_uint32(st->data + 64, value, G_LITTLE_ENDIAN);
971
0
}
972
/**
973
 * fu_struct_pe_coff_optional_header64_set_subsystem: (skip):
974
 **/
975
void
976
fu_struct_pe_coff_optional_header64_set_subsystem(FuStructPeCoffOptionalHeader64 *st, FuCoffSubsystem value)
977
0
{
978
0
    g_return_if_fail(st != NULL);
979
0
    fu_memwrite_uint16(st->data + 68, value, G_LITTLE_ENDIAN);
980
0
}
981
/**
982
 * fu_struct_pe_coff_optional_header64_set_loader_flags: (skip):
983
 **/
984
void
985
fu_struct_pe_coff_optional_header64_set_loader_flags(FuStructPeCoffOptionalHeader64 *st, guint32 value)
986
0
{
987
0
    g_return_if_fail(st != NULL);
988
0
    fu_memwrite_uint32(st->data + 104, value, G_LITTLE_ENDIAN);
989
0
}
990
/**
991
 * fu_struct_pe_coff_optional_header64_set_number_of_rva_and_sizes: (skip):
992
 **/
993
void
994
fu_struct_pe_coff_optional_header64_set_number_of_rva_and_sizes(FuStructPeCoffOptionalHeader64 *st, guint32 value)
995
0
{
996
0
    g_return_if_fail(st != NULL);
997
0
    fu_memwrite_uint32(st->data + 108, value, G_LITTLE_ENDIAN);
998
0
}
999
/**
1000
 * fu_struct_pe_coff_optional_header64_set_certificate_table: (skip):
1001
 **/
1002
void
1003
fu_struct_pe_coff_optional_header64_set_certificate_table(FuStructPeCoffOptionalHeader64 *st, guint32 value)
1004
0
{
1005
0
    g_return_if_fail(st != NULL);
1006
0
    fu_memwrite_uint32(st->data + 144, value, G_LITTLE_ENDIAN);
1007
0
}
1008
/**
1009
 * fu_struct_pe_coff_optional_header64_set_size_of_certificate_table: (skip):
1010
 **/
1011
void
1012
fu_struct_pe_coff_optional_header64_set_size_of_certificate_table(FuStructPeCoffOptionalHeader64 *st, guint32 value)
1013
0
{
1014
0
    g_return_if_fail(st != NULL);
1015
0
    fu_memwrite_uint32(st->data + 148, value, G_LITTLE_ENDIAN);
1016
0
}
1017
/**
1018
 * fu_struct_pe_coff_optional_header64_set_debug_table: (skip):
1019
 **/
1020
void
1021
fu_struct_pe_coff_optional_header64_set_debug_table(FuStructPeCoffOptionalHeader64 *st, guint32 value)
1022
0
{
1023
0
    g_return_if_fail(st != NULL);
1024
0
    fu_memwrite_uint32(st->data + 152, value, G_LITTLE_ENDIAN);
1025
0
}
1026
/**
1027
 * fu_struct_pe_coff_optional_header64_set_size_of_debug_table: (skip):
1028
 **/
1029
void
1030
fu_struct_pe_coff_optional_header64_set_size_of_debug_table(FuStructPeCoffOptionalHeader64 *st, guint32 value)
1031
0
{
1032
0
    g_return_if_fail(st != NULL);
1033
0
    fu_memwrite_uint32(st->data + 156, value, G_LITTLE_ENDIAN);
1034
0
}
1035
/**
1036
 * fu_struct_pe_coff_optional_header64_new: (skip):
1037
 **/
1038
FuStructPeCoffOptionalHeader64 *
1039
fu_struct_pe_coff_optional_header64_new(void)
1040
0
{
1041
0
    FuStructPeCoffOptionalHeader64 *st = g_byte_array_sized_new(176);
1042
0
    fu_byte_array_set_size(st, 176, 0x0);
1043
0
    fu_struct_pe_coff_optional_header64_set_magic(st, FU_PE_COFF_MAGIC_PE32_PLUS);
1044
0
    fu_struct_pe_coff_optional_header64_set_major_linker_version(st, 0x0e);
1045
0
    fu_struct_pe_coff_optional_header64_set_minor_linker_version(st, 0x0e);
1046
0
    fu_struct_pe_coff_optional_header64_set_section_alignment(st, 0x200);
1047
0
    fu_struct_pe_coff_optional_header64_set_file_alignment(st, 0x200);
1048
0
    fu_struct_pe_coff_optional_header64_set_subsystem(st, FU_COFF_SUBSYSTEM_EFI_APPLICATION);
1049
0
    return st;
1050
0
}
1051
/**
1052
 * fu_struct_pe_coff_optional_header64_to_string: (skip):
1053
 **/
1054
static gchar *
1055
fu_struct_pe_coff_optional_header64_to_string(const FuStructPeCoffOptionalHeader64 *st)
1056
0
{
1057
0
    g_autoptr(GString) str = g_string_new("FuStructPeCoffOptionalHeader64:\n");
1058
0
    g_return_val_if_fail(st != NULL, NULL);
1059
0
    {
1060
0
        const gchar *tmp = fu_pe_coff_magic_to_string(fu_struct_pe_coff_optional_header64_get_magic(st));
1061
0
        if (tmp != NULL) {
1062
0
            g_string_append_printf(str, "  magic: 0x%x [%s]\n", (guint) fu_struct_pe_coff_optional_header64_get_magic(st), tmp);
1063
0
        } else {
1064
0
            g_string_append_printf(str, "  magic: 0x%x\n", (guint) fu_struct_pe_coff_optional_header64_get_magic(st));
1065
0
        }
1066
0
    }
1067
0
    g_string_append_printf(str, "  major_linker_version: 0x%x\n",
1068
0
                           (guint) fu_struct_pe_coff_optional_header64_get_major_linker_version(st));
1069
0
    g_string_append_printf(str, "  minor_linker_version: 0x%x\n",
1070
0
                           (guint) fu_struct_pe_coff_optional_header64_get_minor_linker_version(st));
1071
0
    g_string_append_printf(str, "  size_of_code: 0x%x\n",
1072
0
                           (guint) fu_struct_pe_coff_optional_header64_get_size_of_code(st));
1073
0
    g_string_append_printf(str, "  size_of_initialized_data: 0x%x\n",
1074
0
                           (guint) fu_struct_pe_coff_optional_header64_get_size_of_initialized_data(st));
1075
0
    g_string_append_printf(str, "  size_of_uninitialized_data: 0x%x\n",
1076
0
                           (guint) fu_struct_pe_coff_optional_header64_get_size_of_uninitialized_data(st));
1077
0
    g_string_append_printf(str, "  addressofentrypoint: 0x%x\n",
1078
0
                           (guint) fu_struct_pe_coff_optional_header64_get_addressofentrypoint(st));
1079
0
    g_string_append_printf(str, "  base_of_code: 0x%x\n",
1080
0
                           (guint) fu_struct_pe_coff_optional_header64_get_base_of_code(st));
1081
0
    g_string_append_printf(str, "  image_base: 0x%x\n",
1082
0
                           (guint) fu_struct_pe_coff_optional_header64_get_image_base(st));
1083
0
    g_string_append_printf(str, "  section_alignment: 0x%x\n",
1084
0
                           (guint) fu_struct_pe_coff_optional_header64_get_section_alignment(st));
1085
0
    g_string_append_printf(str, "  file_alignment: 0x%x\n",
1086
0
                           (guint) fu_struct_pe_coff_optional_header64_get_file_alignment(st));
1087
0
    g_string_append_printf(str, "  size_of_image: 0x%x\n",
1088
0
                           (guint) fu_struct_pe_coff_optional_header64_get_size_of_image(st));
1089
0
    g_string_append_printf(str, "  size_of_headers: 0x%x\n",
1090
0
                           (guint) fu_struct_pe_coff_optional_header64_get_size_of_headers(st));
1091
0
    g_string_append_printf(str, "  checksum: 0x%x\n",
1092
0
                           (guint) fu_struct_pe_coff_optional_header64_get_checksum(st));
1093
0
    {
1094
0
        const gchar *tmp = fu_coff_subsystem_to_string(fu_struct_pe_coff_optional_header64_get_subsystem(st));
1095
0
        if (tmp != NULL) {
1096
0
            g_string_append_printf(str, "  subsystem: 0x%x [%s]\n", (guint) fu_struct_pe_coff_optional_header64_get_subsystem(st), tmp);
1097
0
        } else {
1098
0
            g_string_append_printf(str, "  subsystem: 0x%x\n", (guint) fu_struct_pe_coff_optional_header64_get_subsystem(st));
1099
0
        }
1100
0
    }
1101
0
    g_string_append_printf(str, "  loader_flags: 0x%x\n",
1102
0
                           (guint) fu_struct_pe_coff_optional_header64_get_loader_flags(st));
1103
0
    g_string_append_printf(str, "  number_of_rva_and_sizes: 0x%x\n",
1104
0
                           (guint) fu_struct_pe_coff_optional_header64_get_number_of_rva_and_sizes(st));
1105
0
    g_string_append_printf(str, "  certificate_table: 0x%x\n",
1106
0
                           (guint) fu_struct_pe_coff_optional_header64_get_certificate_table(st));
1107
0
    g_string_append_printf(str, "  size_of_certificate_table: 0x%x\n",
1108
0
                           (guint) fu_struct_pe_coff_optional_header64_get_size_of_certificate_table(st));
1109
0
    g_string_append_printf(str, "  debug_table: 0x%x\n",
1110
0
                           (guint) fu_struct_pe_coff_optional_header64_get_debug_table(st));
1111
0
    g_string_append_printf(str, "  size_of_debug_table: 0x%x\n",
1112
0
                           (guint) fu_struct_pe_coff_optional_header64_get_size_of_debug_table(st));
1113
0
    if (str->len > 0)
1114
0
        g_string_set_size(str, str->len - 1);
1115
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1116
0
}
1117
static gboolean
1118
fu_struct_pe_coff_optional_header64_validate_internal(FuStructPeCoffOptionalHeader64 *st, GError **error)
1119
0
{
1120
0
    g_return_val_if_fail(st != NULL, FALSE);
1121
0
    return TRUE;
1122
0
}
1123
static gboolean
1124
fu_struct_pe_coff_optional_header64_parse_internal(FuStructPeCoffOptionalHeader64 *st, GError **error)
1125
0
{
1126
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
1127
0
        g_autofree gchar *str = fu_struct_pe_coff_optional_header64_to_string(st);
1128
0
        g_debug("%s", str);
1129
0
    }
1130
0
    if (!fu_struct_pe_coff_optional_header64_validate_internal(st, error))
1131
0
        return FALSE;
1132
0
    return TRUE;
1133
0
}
1134
/**
1135
 * fu_struct_pe_coff_optional_header64_parse_stream: (skip):
1136
 **/
1137
FuStructPeCoffOptionalHeader64 *
1138
fu_struct_pe_coff_optional_header64_parse_stream(GInputStream *stream, gsize offset, GError **error)
1139
0
{
1140
0
    g_autoptr(GByteArray) st = NULL;
1141
0
    st = fu_input_stream_read_byte_array(stream, offset, 176, NULL, error);
1142
0
    if (st == NULL) {
1143
0
        g_prefix_error(error, "FuStructPeCoffOptionalHeader64 failed read of 0x%x: ", (guint) 176);
1144
0
        return NULL;
1145
0
    }
1146
0
    if (st->len != 176) {
1147
0
        g_set_error(error,
1148
0
                    FWUPD_ERROR,
1149
0
                    FWUPD_ERROR_INVALID_DATA,
1150
0
                    "FuStructPeCoffOptionalHeader64 requested 0x%x and got 0x%x",
1151
0
                    (guint) 176,
1152
0
                    (guint) st->len);
1153
0
        return NULL;
1154
0
    }
1155
0
    if (!fu_struct_pe_coff_optional_header64_parse_internal(st, error))
1156
0
        return NULL;
1157
0
    return g_steal_pointer(&st);
1158
0
}
1159
/* getters */
1160
1161
/* setters */
1162
/* getters */
1163
/**
1164
 * fu_struct_pe_coff_section_get_name: (skip):
1165
 **/
1166
gchar *
1167
fu_struct_pe_coff_section_get_name(const FuStructPeCoffSection *st)
1168
0
{
1169
0
    g_return_val_if_fail(st != NULL, NULL);
1170
0
    return fu_memstrsafe(st->data, st->len, 0, 8, NULL);
1171
0
}
1172
/**
1173
 * fu_struct_pe_coff_section_get_virtual_size: (skip):
1174
 **/
1175
guint32
1176
fu_struct_pe_coff_section_get_virtual_size(const FuStructPeCoffSection *st)
1177
0
{
1178
0
    g_return_val_if_fail(st != NULL, 0x0);
1179
0
    return fu_memread_uint32(st->data + 8, G_LITTLE_ENDIAN);
1180
0
}
1181
/**
1182
 * fu_struct_pe_coff_section_get_virtual_address: (skip):
1183
 **/
1184
guint32
1185
fu_struct_pe_coff_section_get_virtual_address(const FuStructPeCoffSection *st)
1186
0
{
1187
0
    g_return_val_if_fail(st != NULL, 0x0);
1188
0
    return fu_memread_uint32(st->data + 12, G_LITTLE_ENDIAN);
1189
0
}
1190
/**
1191
 * fu_struct_pe_coff_section_get_size_of_raw_data: (skip):
1192
 **/
1193
guint32
1194
fu_struct_pe_coff_section_get_size_of_raw_data(const FuStructPeCoffSection *st)
1195
0
{
1196
0
    g_return_val_if_fail(st != NULL, 0x0);
1197
0
    return fu_memread_uint32(st->data + 16, G_LITTLE_ENDIAN);
1198
0
}
1199
/**
1200
 * fu_struct_pe_coff_section_get_pointer_to_raw_data: (skip):
1201
 **/
1202
guint32
1203
fu_struct_pe_coff_section_get_pointer_to_raw_data(const FuStructPeCoffSection *st)
1204
0
{
1205
0
    g_return_val_if_fail(st != NULL, 0x0);
1206
0
    return fu_memread_uint32(st->data + 20, G_LITTLE_ENDIAN);
1207
0
}
1208
/**
1209
 * fu_struct_pe_coff_section_get_characteristics: (skip):
1210
 **/
1211
guint32
1212
fu_struct_pe_coff_section_get_characteristics(const FuStructPeCoffSection *st)
1213
0
{
1214
0
    g_return_val_if_fail(st != NULL, 0x0);
1215
0
    return fu_memread_uint32(st->data + 36, G_LITTLE_ENDIAN);
1216
0
}
1217
1218
/* setters */
1219
/**
1220
 * fu_struct_pe_coff_section_set_name: (skip):
1221
 **/
1222
gboolean
1223
fu_struct_pe_coff_section_set_name(FuStructPeCoffSection *st, const gchar *value, GError **error)
1224
0
{
1225
0
    gsize len;
1226
0
    g_return_val_if_fail(st != NULL, FALSE);
1227
0
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1228
0
    if (value == NULL) {
1229
0
        memset(st->data + 0, 0x0, 8);
1230
0
        return TRUE;
1231
0
    }
1232
0
    len = strlen(value);
1233
0
    if (len > 8) {
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 FuStructPeCoffSection.name (0x%x bytes)",
1238
0
                    value, (guint) len, (guint) 8);
1239
0
        return FALSE;
1240
0
    }
1241
0
    return fu_memcpy_safe(st->data, st->len, 0, (const guint8 *)value, len, 0x0, len, error);
1242
0
}
1243
/**
1244
 * fu_struct_pe_coff_section_set_virtual_size: (skip):
1245
 **/
1246
void
1247
fu_struct_pe_coff_section_set_virtual_size(FuStructPeCoffSection *st, guint32 value)
1248
0
{
1249
0
    g_return_if_fail(st != NULL);
1250
0
    fu_memwrite_uint32(st->data + 8, value, G_LITTLE_ENDIAN);
1251
0
}
1252
/**
1253
 * fu_struct_pe_coff_section_set_virtual_address: (skip):
1254
 **/
1255
void
1256
fu_struct_pe_coff_section_set_virtual_address(FuStructPeCoffSection *st, guint32 value)
1257
0
{
1258
0
    g_return_if_fail(st != NULL);
1259
0
    fu_memwrite_uint32(st->data + 12, value, G_LITTLE_ENDIAN);
1260
0
}
1261
/**
1262
 * fu_struct_pe_coff_section_set_size_of_raw_data: (skip):
1263
 **/
1264
void
1265
fu_struct_pe_coff_section_set_size_of_raw_data(FuStructPeCoffSection *st, guint32 value)
1266
0
{
1267
0
    g_return_if_fail(st != NULL);
1268
0
    fu_memwrite_uint32(st->data + 16, value, G_LITTLE_ENDIAN);
1269
0
}
1270
/**
1271
 * fu_struct_pe_coff_section_set_pointer_to_raw_data: (skip):
1272
 **/
1273
void
1274
fu_struct_pe_coff_section_set_pointer_to_raw_data(FuStructPeCoffSection *st, guint32 value)
1275
0
{
1276
0
    g_return_if_fail(st != NULL);
1277
0
    fu_memwrite_uint32(st->data + 20, value, G_LITTLE_ENDIAN);
1278
0
}
1279
/**
1280
 * fu_struct_pe_coff_section_set_characteristics: (skip):
1281
 **/
1282
void
1283
fu_struct_pe_coff_section_set_characteristics(FuStructPeCoffSection *st, guint32 value)
1284
0
{
1285
0
    g_return_if_fail(st != NULL);
1286
0
    fu_memwrite_uint32(st->data + 36, value, G_LITTLE_ENDIAN);
1287
0
}
1288
/**
1289
 * fu_struct_pe_coff_section_new: (skip):
1290
 **/
1291
FuStructPeCoffSection *
1292
fu_struct_pe_coff_section_new(void)
1293
0
{
1294
0
    FuStructPeCoffSection *st = g_byte_array_sized_new(40);
1295
0
    fu_byte_array_set_size(st, 40, 0x0);
1296
0
    return st;
1297
0
}
1298
/**
1299
 * fu_struct_pe_coff_section_to_string: (skip):
1300
 **/
1301
static gchar *
1302
fu_struct_pe_coff_section_to_string(const FuStructPeCoffSection *st)
1303
0
{
1304
0
    g_autoptr(GString) str = g_string_new("FuStructPeCoffSection:\n");
1305
0
    g_return_val_if_fail(st != NULL, NULL);
1306
0
    {
1307
0
        g_autofree gchar *tmp = fu_struct_pe_coff_section_get_name(st);
1308
0
        if (tmp != NULL)
1309
0
            g_string_append_printf(str, "  name: %s\n", tmp);
1310
0
    }
1311
0
    g_string_append_printf(str, "  virtual_size: 0x%x\n",
1312
0
                           (guint) fu_struct_pe_coff_section_get_virtual_size(st));
1313
0
    g_string_append_printf(str, "  virtual_address: 0x%x\n",
1314
0
                           (guint) fu_struct_pe_coff_section_get_virtual_address(st));
1315
0
    g_string_append_printf(str, "  size_of_raw_data: 0x%x\n",
1316
0
                           (guint) fu_struct_pe_coff_section_get_size_of_raw_data(st));
1317
0
    g_string_append_printf(str, "  pointer_to_raw_data: 0x%x\n",
1318
0
                           (guint) fu_struct_pe_coff_section_get_pointer_to_raw_data(st));
1319
0
    g_string_append_printf(str, "  characteristics: 0x%x\n",
1320
0
                           (guint) fu_struct_pe_coff_section_get_characteristics(st));
1321
0
    if (str->len > 0)
1322
0
        g_string_set_size(str, str->len - 1);
1323
0
    return g_string_free(g_steal_pointer(&str), FALSE);
1324
0
}
1325
static gboolean
1326
fu_struct_pe_coff_section_validate_internal(FuStructPeCoffSection *st, GError **error)
1327
0
{
1328
0
    g_return_val_if_fail(st != NULL, FALSE);
1329
0
    return TRUE;
1330
0
}
1331
static gboolean
1332
fu_struct_pe_coff_section_parse_internal(FuStructPeCoffSection *st, GError **error)
1333
0
{
1334
0
    if (g_getenv("FWUPD_VERBOSE") != NULL) {
1335
0
        g_autofree gchar *str = fu_struct_pe_coff_section_to_string(st);
1336
0
        g_debug("%s", str);
1337
0
    }
1338
0
    if (!fu_struct_pe_coff_section_validate_internal(st, error))
1339
0
        return FALSE;
1340
0
    return TRUE;
1341
0
}
1342
/**
1343
 * fu_struct_pe_coff_section_parse_stream: (skip):
1344
 **/
1345
FuStructPeCoffSection *
1346
fu_struct_pe_coff_section_parse_stream(GInputStream *stream, gsize offset, GError **error)
1347
0
{
1348
0
    g_autoptr(GByteArray) st = NULL;
1349
0
    st = fu_input_stream_read_byte_array(stream, offset, 40, NULL, error);
1350
0
    if (st == NULL) {
1351
0
        g_prefix_error(error, "FuStructPeCoffSection failed read of 0x%x: ", (guint) 40);
1352
0
        return NULL;
1353
0
    }
1354
0
    if (st->len != 40) {
1355
0
        g_set_error(error,
1356
0
                    FWUPD_ERROR,
1357
0
                    FWUPD_ERROR_INVALID_DATA,
1358
0
                    "FuStructPeCoffSection requested 0x%x and got 0x%x",
1359
0
                    (guint) 40,
1360
0
                    (guint) st->len);
1361
0
        return NULL;
1362
0
    }
1363
0
    if (!fu_struct_pe_coff_section_parse_internal(st, error))
1364
0
        return NULL;
1365
0
    return g_steal_pointer(&st);
1366
0
}