/*
 * This file is auto-generated, do not modify!
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <glib.h>

#include "fu-tpm-struct.h"
#include "fu-byte-array.h"
#include "fu-bytes.h"
#include "fu-mem-private.h"
#include "fu-string.h"

#ifdef G_LOG_DOMAIN
  #undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "FuStruct"

/**
 * fu_tpm_alg_to_string:
 * @val: value, e.g. %FU_TPM_ALG_SHA1
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
const gchar *
fu_tpm_alg_to_string(FuTpmAlg val)
{
    if (val == FU_TPM_ALG_SHA1)
        return "sha1";
    if (val == FU_TPM_ALG_SHA256)
        return "sha256";
    if (val == FU_TPM_ALG_SHA384)
        return "sha384";
    if (val == FU_TPM_ALG_SHA512)
        return "sha512";
    if (val == FU_TPM_ALG_SM3_256)
        return "sm3-256";
    if (val == FU_TPM_ALG_SHA3_256)
        return "sha3-256";
    if (val == FU_TPM_ALG_SHA3_384)
        return "sha3-384";
    if (val == FU_TPM_ALG_SHA3_512)
        return "sha3-512";
    return NULL;
}
/**
 * fu_tpm_alg_from_string:
 * @val: (nullable): a string, e.g. `sha1`
 *
 * Converts a string to an enumerated value.
 *
 * Returns: enumerated value
 **/
FuTpmAlg
fu_tpm_alg_from_string(const gchar *val)
{
    if (g_strcmp0(val, "sha1") == 0)
        return FU_TPM_ALG_SHA1;
    if (g_strcmp0(val, "sha256") == 0)
        return FU_TPM_ALG_SHA256;
    if (g_strcmp0(val, "sha384") == 0)
        return FU_TPM_ALG_SHA384;
    if (g_strcmp0(val, "sha512") == 0)
        return FU_TPM_ALG_SHA512;
    if (g_strcmp0(val, "sm3-256") == 0)
        return FU_TPM_ALG_SM3_256;
    if (g_strcmp0(val, "sha3-256") == 0)
        return FU_TPM_ALG_SHA3_256;
    if (g_strcmp0(val, "sha3-384") == 0)
        return FU_TPM_ALG_SHA3_384;
    if (g_strcmp0(val, "sha3-512") == 0)
        return FU_TPM_ALG_SHA3_512;
    return FU_TPM_ALG_UNKNOWN;
}

/**
 * fu_tpm_eventlog_item_kind_to_string:
 * @val: value, e.g. %FU_TPM_EVENTLOG_ITEM_KIND_POST_CODE
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
const gchar *
fu_tpm_eventlog_item_kind_to_string(FuTpmEventlogItemKind val)
{
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_PREBOOT_CERT)
        return "preboot-cert";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_POST_CODE)
        return "post-code";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_NO_ACTION)
        return "no-action";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_SEPARATOR)
        return "separator";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_ACTION)
        return "action";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EVENT_TAG)
        return "event-tag";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_S_CRTM_CONTENTS)
        return "s-crtm-contents";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_S_CRTM_VERSION)
        return "s-crtm-version";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_CPU_MICROCODE)
        return "cpu-microcode";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_PLATFORM_CONFIG_FLAGS)
        return "platform-config-flags";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_TABLE_OF_DEVICES)
        return "table-of-devices";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_COMPACT_HASH)
        return "compact-hash";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_NONHOST_CODE)
        return "nonhost-code";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_NONHOST_CONFIG)
        return "nonhost-config";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_NONHOST_INFO)
        return "nonhost-info";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_OMIT_BOOT_DEVICE_EVENTS)
        return "omit-boot-device-events";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_EVENT_BASE)
        return "efi-event-base";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_VARIABLE_DRIVER_CONFIG)
        return "efi-variable-driver-config";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_VARIABLE_BOOT)
        return "efi-variable-boot";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_BOOT_SERVICES_APPLICATION)
        return "efi-boot-services-application";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_BOOT_SERVICES_DRIVER)
        return "efi-boot-services-driver";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_RUNTIME_SERVICES_DRIVER)
        return "efi-runtime-services-driver";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_GPT_EVENT)
        return "efi-gpt-event";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_ACTION)
        return "efi-action";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_PLATFORM_FIRMWARE_BLOB)
        return "efi-platform-firmware-blob";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_HANDOFF_TABLES)
        return "efi-handoff-tables";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_HCRTM_EVENT)
        return "efi-hcrtm-event";
    if (val == FU_TPM_EVENTLOG_ITEM_KIND_EFI_VARIABLE_AUTHORITY)
        return "efi-variable-authority";
    return NULL;
}
/**
 * fu_tpm_eventlog_item_kind_from_string:
 * @val: (nullable): a string, e.g. `post-code`
 *
 * Converts a string to an enumerated value.
 *
 * Returns: enumerated value
 **/
FuTpmEventlogItemKind
fu_tpm_eventlog_item_kind_from_string(const gchar *val)
{
    if (g_strcmp0(val, "preboot-cert") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_PREBOOT_CERT;
    if (g_strcmp0(val, "post-code") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_POST_CODE;
    if (g_strcmp0(val, "no-action") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_NO_ACTION;
    if (g_strcmp0(val, "separator") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_SEPARATOR;
    if (g_strcmp0(val, "action") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_ACTION;
    if (g_strcmp0(val, "event-tag") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EVENT_TAG;
    if (g_strcmp0(val, "s-crtm-contents") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_S_CRTM_CONTENTS;
    if (g_strcmp0(val, "s-crtm-version") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_S_CRTM_VERSION;
    if (g_strcmp0(val, "cpu-microcode") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_CPU_MICROCODE;
    if (g_strcmp0(val, "platform-config-flags") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_PLATFORM_CONFIG_FLAGS;
    if (g_strcmp0(val, "table-of-devices") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_TABLE_OF_DEVICES;
    if (g_strcmp0(val, "compact-hash") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_COMPACT_HASH;
    if (g_strcmp0(val, "nonhost-code") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_NONHOST_CODE;
    if (g_strcmp0(val, "nonhost-config") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_NONHOST_CONFIG;
    if (g_strcmp0(val, "nonhost-info") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_NONHOST_INFO;
    if (g_strcmp0(val, "omit-boot-device-events") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_OMIT_BOOT_DEVICE_EVENTS;
    if (g_strcmp0(val, "efi-event-base") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_EVENT_BASE;
    if (g_strcmp0(val, "efi-variable-driver-config") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_VARIABLE_DRIVER_CONFIG;
    if (g_strcmp0(val, "efi-variable-boot") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_VARIABLE_BOOT;
    if (g_strcmp0(val, "efi-boot-services-application") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_BOOT_SERVICES_APPLICATION;
    if (g_strcmp0(val, "efi-boot-services-driver") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_BOOT_SERVICES_DRIVER;
    if (g_strcmp0(val, "efi-runtime-services-driver") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_RUNTIME_SERVICES_DRIVER;
    if (g_strcmp0(val, "efi-gpt-event") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_GPT_EVENT;
    if (g_strcmp0(val, "efi-action") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_ACTION;
    if (g_strcmp0(val, "efi-platform-firmware-blob") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_PLATFORM_FIRMWARE_BLOB;
    if (g_strcmp0(val, "efi-handoff-tables") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_HANDOFF_TABLES;
    if (g_strcmp0(val, "efi-hcrtm-event") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_HCRTM_EVENT;
    if (g_strcmp0(val, "efi-variable-authority") == 0)
        return FU_TPM_EVENTLOG_ITEM_KIND_EFI_VARIABLE_AUTHORITY;
    return FU_TPM_EVENTLOG_ITEM_KIND_PREBOOT_CERT;
}
/**
 * fu_struct_tpm_efi_startup_locality_event_ref: (skip):
 **/
FuStructTpmEfiStartupLocalityEvent *
fu_struct_tpm_efi_startup_locality_event_ref(FuStructTpmEfiStartupLocalityEvent *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_tpm_efi_startup_locality_event_unref: (skip):
 **/
void
fu_struct_tpm_efi_startup_locality_event_unref(FuStructTpmEfiStartupLocalityEvent *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructTpmEfiStartupLocalityEvent refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_tpm_efi_startup_locality_event_new_internal: (skip):
 **/
static FuStructTpmEfiStartupLocalityEvent *
fu_struct_tpm_efi_startup_locality_event_new_internal(void)
{
    FuStructTpmEfiStartupLocalityEvent *st = g_new0(FuStructTpmEfiStartupLocalityEvent, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_tpm_efi_startup_locality_event_get_signature: (skip):
 **/
static gchar *
fu_struct_tpm_efi_startup_locality_event_get_signature(const FuStructTpmEfiStartupLocalityEvent *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    return fu_memstrsafe(st->buf->data, st->buf->len, 0, 16, NULL);
}
/**
 * fu_struct_tpm_efi_startup_locality_event_get_locality: (skip):
 **/
guint8
fu_struct_tpm_efi_startup_locality_event_get_locality(const FuStructTpmEfiStartupLocalityEvent *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[16];
}

/* setters */
/**
 * fu_struct_tpm_efi_startup_locality_event_to_string: (skip):
 **/
static gchar *
fu_struct_tpm_efi_startup_locality_event_to_string(const FuStructTpmEfiStartupLocalityEvent *st)
{
    g_autoptr(GString) str = g_string_new("FuStructTpmEfiStartupLocalityEvent:\n");
    g_return_val_if_fail(st != NULL, NULL);
    {
        g_autofree gchar *tmp = fu_struct_tpm_efi_startup_locality_event_get_signature(st);
        if (tmp != NULL)
            g_string_append_printf(str, "  signature: %s\n", tmp);
    }
    g_string_append_printf(str, "  locality: 0x%x\n",
                           (guint) fu_struct_tpm_efi_startup_locality_event_get_locality(st));
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
static gboolean
fu_struct_tpm_efi_startup_locality_event_validate_internal(FuStructTpmEfiStartupLocalityEvent *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    if (strncmp((const gchar *) (st->buf->data + 0), "StartupLocality", 16) != 0) {
        g_autofree gchar *str = fu_struct_tpm_efi_startup_locality_event_get_signature(st);
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructTpmEfiStartupLocalityEvent.signature was not valid, "
                    "expected 'StartupLocality' and got '%s'",
                    str);
        return FALSE;
    }
    return TRUE;
}
static gboolean
fu_struct_tpm_efi_startup_locality_event_parse_internal(FuStructTpmEfiStartupLocalityEvent *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_tpm_efi_startup_locality_event_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_tpm_efi_startup_locality_event_validate_internal(st, error))
        return FALSE;
    return TRUE;
}

/**
 * fu_struct_tpm_efi_startup_locality_event_parse: (skip):
 **/
static FuStructTpmEfiStartupLocalityEvent *
fu_struct_tpm_efi_startup_locality_event_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(FuStructTpmEfiStartupLocalityEvent) st = fu_struct_tpm_efi_startup_locality_event_new_internal();
    g_return_val_if_fail(buf != NULL, NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
    if (!fu_memchk_read(bufsz, offset, 17, error)) {
        g_prefix_error_literal(error, "invalid struct FuStructTpmEfiStartupLocalityEvent: ");
        return NULL;
    }
    st->buf = g_byte_array_new();
    g_byte_array_append(st->buf, buf + offset, 17);
    if (!fu_struct_tpm_efi_startup_locality_event_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
/**
 * fu_struct_tpm_efi_startup_locality_event_parse_bytes: (skip):
 **/
FuStructTpmEfiStartupLocalityEvent *
fu_struct_tpm_efi_startup_locality_event_parse_bytes(GBytes *blob, gsize offset, GError **error)
{
    gsize bufsz = 0;
    const guint8 *buf = fu_bytes_get_data_safe(blob, &bufsz, error);
    if (buf == NULL)
        return NULL;
    return fu_struct_tpm_efi_startup_locality_event_parse(buf, bufsz, offset, error);
}
/**
 * fu_struct_tpm_event_log1_item_ref: (skip):
 **/
FuStructTpmEventLog1Item *
fu_struct_tpm_event_log1_item_ref(FuStructTpmEventLog1Item *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_tpm_event_log1_item_unref: (skip):
 **/
void
fu_struct_tpm_event_log1_item_unref(FuStructTpmEventLog1Item *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructTpmEventLog1Item refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_tpm_event_log1_item_new_internal: (skip):
 **/
static FuStructTpmEventLog1Item *
fu_struct_tpm_event_log1_item_new_internal(void)
{
    FuStructTpmEventLog1Item *st = g_new0(FuStructTpmEventLog1Item, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_tpm_event_log1_item_get_pcr: (skip):
 **/
guint32
fu_struct_tpm_event_log1_item_get_pcr(const FuStructTpmEventLog1Item *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 0, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log1_item_get_type: (skip):
 **/
FuTpmEventlogItemKind
fu_struct_tpm_event_log1_item_get_type(const FuStructTpmEventLog1Item *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 4, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log1_item_get_digest: (skip):
 **/
const guint8 *
fu_struct_tpm_event_log1_item_get_digest(const FuStructTpmEventLog1Item *st, gsize *bufsz)
{
    g_return_val_if_fail(st != NULL, NULL);
    if (bufsz != NULL)
        *bufsz = 20;
    return st->buf->data + 8;
}
/**
 * fu_struct_tpm_event_log1_item_get_datasz: (skip):
 **/
guint32
fu_struct_tpm_event_log1_item_get_datasz(const FuStructTpmEventLog1Item *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 28, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_tpm_event_log1_item_set_pcr: (skip):
 **/
void
fu_struct_tpm_event_log1_item_set_pcr(FuStructTpmEventLog1Item *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 0, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log1_item_set_type: (skip):
 **/
void
fu_struct_tpm_event_log1_item_set_type(FuStructTpmEventLog1Item *st, FuTpmEventlogItemKind value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 4, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log1_item_set_digest: (skip):
 **/
gboolean
fu_struct_tpm_event_log1_item_set_digest(FuStructTpmEventLog1Item *st, const guint8 *buf, gsize bufsz, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    g_return_val_if_fail(buf != NULL, FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
    return fu_memcpy_safe(st->buf->data, st->buf->len, 8, buf, bufsz, 0x0, bufsz, error);
}
/**
 * fu_struct_tpm_event_log1_item_set_datasz: (skip):
 **/
void
fu_struct_tpm_event_log1_item_set_datasz(FuStructTpmEventLog1Item *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 28, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log1_item_new: (skip):
 **/
FuStructTpmEventLog1Item *
fu_struct_tpm_event_log1_item_new(void)
{
    FuStructTpmEventLog1Item *st = fu_struct_tpm_event_log1_item_new_internal();
    st->buf = g_byte_array_sized_new(32);
    fu_byte_array_set_size(st->buf, 32, 0x0);
    return st;
}
/**
 * fu_struct_tpm_event_log1_item_to_string: (skip):
 **/
static gchar *
fu_struct_tpm_event_log1_item_to_string(const FuStructTpmEventLog1Item *st)
{
    g_autoptr(GString) str = g_string_new("FuStructTpmEventLog1Item:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  pcr: 0x%x\n",
                           (guint) fu_struct_tpm_event_log1_item_get_pcr(st));
    {
        const gchar *tmp = fu_tpm_eventlog_item_kind_to_string(fu_struct_tpm_event_log1_item_get_type(st));
        if (tmp != NULL) {
            g_string_append_printf(str, "  type: 0x%x [%s]\n", (guint) fu_struct_tpm_event_log1_item_get_type(st), tmp);
        } else {
            g_string_append_printf(str, "  type: 0x%x\n", (guint) fu_struct_tpm_event_log1_item_get_type(st));
        }
    }
    {
        gsize bufsz = 0;
        const guint8 *buf = fu_struct_tpm_event_log1_item_get_digest(st, &bufsz);
        g_autoptr(GString) tmp = g_string_new(NULL);
        for (gsize i = 0; i < bufsz; i++)
            g_string_append_printf(tmp, "%02X", buf[i]);
        g_string_append_printf(str, "  digest: 0x%s\n", tmp->str);
    }
    g_string_append_printf(str, "  datasz: 0x%x\n",
                           (guint) fu_struct_tpm_event_log1_item_get_datasz(st));
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
static gboolean
fu_struct_tpm_event_log1_item_validate_internal(FuStructTpmEventLog1Item *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    return TRUE;
}
static gboolean
fu_struct_tpm_event_log1_item_parse_internal(FuStructTpmEventLog1Item *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_tpm_event_log1_item_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_tpm_event_log1_item_validate_internal(st, error))
        return FALSE;
    return TRUE;
}
/**
 * fu_struct_tpm_event_log1_item_parse_stream: (skip):
 **/
FuStructTpmEventLog1Item *
fu_struct_tpm_event_log1_item_parse_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructTpmEventLog1Item) st = fu_struct_tpm_event_log1_item_new_internal();
    st->buf = fu_input_stream_read_byte_array(stream, offset, 32, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructTpmEventLog1Item failed read of 0x%x: ", (guint) 32);
        return NULL;
    }
    if (st->buf->len != 32) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructTpmEventLog1Item requested 0x%x and got 0x%x",
                    (guint) 32,
                    (guint) st->buf->len);
        return NULL;
    }
    if (!fu_struct_tpm_event_log1_item_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
/**
 * fu_struct_tpm_event_log2_hdr_ref: (skip):
 **/
FuStructTpmEventLog2Hdr *
fu_struct_tpm_event_log2_hdr_ref(FuStructTpmEventLog2Hdr *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_tpm_event_log2_hdr_unref: (skip):
 **/
void
fu_struct_tpm_event_log2_hdr_unref(FuStructTpmEventLog2Hdr *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructTpmEventLog2Hdr refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_tpm_event_log2_hdr_new_internal: (skip):
 **/
static FuStructTpmEventLog2Hdr *
fu_struct_tpm_event_log2_hdr_new_internal(void)
{
    FuStructTpmEventLog2Hdr *st = g_new0(FuStructTpmEventLog2Hdr, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_tpm_event_log2_hdr_get_pcr: (skip):
 **/
static guint32
fu_struct_tpm_event_log2_hdr_get_pcr(const FuStructTpmEventLog2Hdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 0, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_hdr_get_type: (skip):
 **/
static FuTpmEventlogItemKind
fu_struct_tpm_event_log2_hdr_get_type(const FuStructTpmEventLog2Hdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 4, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_hdr_get_datasz: (skip):
 **/
guint32
fu_struct_tpm_event_log2_hdr_get_datasz(const FuStructTpmEventLog2Hdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 28, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_hdr_get_data: (skip):
 **/
static gchar *
fu_struct_tpm_event_log2_hdr_get_data(const FuStructTpmEventLog2Hdr *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    return fu_memstrsafe(st->buf->data, st->buf->len, 32, 15, NULL);
}

/* setters */
/**
 * fu_struct_tpm_event_log2_hdr_set_pcr: (skip):
 **/
static void
fu_struct_tpm_event_log2_hdr_set_pcr(FuStructTpmEventLog2Hdr *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 0, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_hdr_set_type: (skip):
 **/
static void
fu_struct_tpm_event_log2_hdr_set_type(FuStructTpmEventLog2Hdr *st, FuTpmEventlogItemKind value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 4, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_hdr_set_datasz: (skip):
 **/
void
fu_struct_tpm_event_log2_hdr_set_datasz(FuStructTpmEventLog2Hdr *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 28, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_hdr_set_data: (skip):
 **/
static gboolean
fu_struct_tpm_event_log2_hdr_set_data(FuStructTpmEventLog2Hdr *st, const gchar *value, GError **error)
{
    gsize len;
    g_return_val_if_fail(st != NULL, FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
    if (value == NULL) {
        memset(st->buf->data + 32, 0x0, 15);
        return TRUE;
    }
    len = strlen(value);
    if (len > 15) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "string '%s' (0x%x bytes) does not fit in FuStructTpmEventLog2Hdr.data (0x%x bytes)",
                    value, (guint) len, (guint) 15);
        return FALSE;
    }
    return fu_memcpy_safe(st->buf->data, st->buf->len, 32, (const guint8 *)value, len, 0x0, len, error);
}
/**
 * fu_struct_tpm_event_log2_hdr_new: (skip):
 **/
FuStructTpmEventLog2Hdr *
fu_struct_tpm_event_log2_hdr_new(void)
{
    FuStructTpmEventLog2Hdr *st = fu_struct_tpm_event_log2_hdr_new_internal();
    st->buf = g_byte_array_sized_new(47);
    fu_byte_array_set_size(st->buf, 47, 0x0);
    fu_struct_tpm_event_log2_hdr_set_pcr(st, 0x0);
    fu_struct_tpm_event_log2_hdr_set_type(st, FU_TPM_EVENTLOG_ITEM_KIND_NO_ACTION);
    fu_struct_tpm_event_log2_hdr_set_datasz(st, 15);
    fu_struct_tpm_event_log2_hdr_set_data(st, "Spec ID Event03", NULL);
    return st;
}
/**
 * fu_struct_tpm_event_log2_hdr_to_string: (skip):
 **/
static gchar *
fu_struct_tpm_event_log2_hdr_to_string(const FuStructTpmEventLog2Hdr *st)
{
    g_autoptr(GString) str = g_string_new("FuStructTpmEventLog2Hdr:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  pcr: 0x%x\n",
                           (guint) fu_struct_tpm_event_log2_hdr_get_pcr(st));
    {
        const gchar *tmp = fu_tpm_eventlog_item_kind_to_string(fu_struct_tpm_event_log2_hdr_get_type(st));
        if (tmp != NULL) {
            g_string_append_printf(str, "  type: 0x%x [%s]\n", (guint) fu_struct_tpm_event_log2_hdr_get_type(st), tmp);
        } else {
            g_string_append_printf(str, "  type: 0x%x\n", (guint) fu_struct_tpm_event_log2_hdr_get_type(st));
        }
    }
    g_string_append_printf(str, "  datasz: 0x%x\n",
                           (guint) fu_struct_tpm_event_log2_hdr_get_datasz(st));
    {
        g_autofree gchar *tmp = fu_struct_tpm_event_log2_hdr_get_data(st);
        if (tmp != NULL)
            g_string_append_printf(str, "  data: %s\n", tmp);
    }
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
static gboolean
fu_struct_tpm_event_log2_hdr_validate_internal(FuStructTpmEventLog2Hdr *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    if (fu_struct_tpm_event_log2_hdr_get_pcr(st) != 0x0) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructTpmEventLog2Hdr.pcr was not valid, "
                    "expected 0x%x and got 0x%x",
                    (guint) 0x0,
                    (guint) fu_struct_tpm_event_log2_hdr_get_pcr(st));
        return FALSE;
    }
    if (fu_struct_tpm_event_log2_hdr_get_type(st) != FU_TPM_EVENTLOG_ITEM_KIND_NO_ACTION) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructTpmEventLog2Hdr.type was not valid, "
                    "expected 'FU_TPM_EVENTLOG_ITEM_KIND_NO_ACTION' and got 0x%x '%s'",
                    fu_struct_tpm_event_log2_hdr_get_type(st),
                    fu_tpm_eventlog_item_kind_to_string(fu_struct_tpm_event_log2_hdr_get_type(st)));
        return FALSE;
    }
    if (strncmp((const gchar *) (st->buf->data + 32), "Spec ID Event03", 15) != 0) {
        g_autofree gchar *str = fu_struct_tpm_event_log2_hdr_get_data(st);
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructTpmEventLog2Hdr.data was not valid, "
                    "expected 'Spec ID Event03' and got '%s'",
                    str);
        return FALSE;
    }
    return TRUE;
}
static gboolean
fu_struct_tpm_event_log2_hdr_parse_internal(FuStructTpmEventLog2Hdr *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_tpm_event_log2_hdr_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_tpm_event_log2_hdr_validate_internal(st, error))
        return FALSE;
    return TRUE;
}
/**
 * fu_struct_tpm_event_log2_hdr_parse_stream: (skip):
 **/
FuStructTpmEventLog2Hdr *
fu_struct_tpm_event_log2_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructTpmEventLog2Hdr) st = fu_struct_tpm_event_log2_hdr_new_internal();
    st->buf = fu_input_stream_read_byte_array(stream, offset, 47, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructTpmEventLog2Hdr failed read of 0x%x: ", (guint) 47);
        return NULL;
    }
    if (st->buf->len != 47) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructTpmEventLog2Hdr requested 0x%x and got 0x%x",
                    (guint) 47,
                    (guint) st->buf->len);
        return NULL;
    }
    if (!fu_struct_tpm_event_log2_hdr_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
/**
 * fu_struct_tpm_event_log2_ref: (skip):
 **/
FuStructTpmEventLog2 *
fu_struct_tpm_event_log2_ref(FuStructTpmEventLog2 *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_tpm_event_log2_unref: (skip):
 **/
void
fu_struct_tpm_event_log2_unref(FuStructTpmEventLog2 *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructTpmEventLog2 refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_tpm_event_log2_new_internal: (skip):
 **/
static FuStructTpmEventLog2 *
fu_struct_tpm_event_log2_new_internal(void)
{
    FuStructTpmEventLog2 *st = g_new0(FuStructTpmEventLog2, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_tpm_event_log2_get_pcr: (skip):
 **/
guint32
fu_struct_tpm_event_log2_get_pcr(const FuStructTpmEventLog2 *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 0, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_get_type: (skip):
 **/
FuTpmEventlogItemKind
fu_struct_tpm_event_log2_get_type(const FuStructTpmEventLog2 *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 4, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_get_digest_count: (skip):
 **/
guint32
fu_struct_tpm_event_log2_get_digest_count(const FuStructTpmEventLog2 *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 8, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_tpm_event_log2_set_pcr: (skip):
 **/
void
fu_struct_tpm_event_log2_set_pcr(FuStructTpmEventLog2 *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 0, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_set_type: (skip):
 **/
void
fu_struct_tpm_event_log2_set_type(FuStructTpmEventLog2 *st, FuTpmEventlogItemKind value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 4, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_set_digest_count: (skip):
 **/
void
fu_struct_tpm_event_log2_set_digest_count(FuStructTpmEventLog2 *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 8, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_tpm_event_log2_new: (skip):
 **/
FuStructTpmEventLog2 *
fu_struct_tpm_event_log2_new(void)
{
    FuStructTpmEventLog2 *st = fu_struct_tpm_event_log2_new_internal();
    st->buf = g_byte_array_sized_new(12);
    fu_byte_array_set_size(st->buf, 12, 0x0);
    return st;
}
/**
 * fu_struct_tpm_event_log2_to_string: (skip):
 **/
static gchar *
fu_struct_tpm_event_log2_to_string(const FuStructTpmEventLog2 *st)
{
    g_autoptr(GString) str = g_string_new("FuStructTpmEventLog2:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  pcr: 0x%x\n",
                           (guint) fu_struct_tpm_event_log2_get_pcr(st));
    {
        const gchar *tmp = fu_tpm_eventlog_item_kind_to_string(fu_struct_tpm_event_log2_get_type(st));
        if (tmp != NULL) {
            g_string_append_printf(str, "  type: 0x%x [%s]\n", (guint) fu_struct_tpm_event_log2_get_type(st), tmp);
        } else {
            g_string_append_printf(str, "  type: 0x%x\n", (guint) fu_struct_tpm_event_log2_get_type(st));
        }
    }
    g_string_append_printf(str, "  digest_count: 0x%x\n",
                           (guint) fu_struct_tpm_event_log2_get_digest_count(st));
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
static gboolean
fu_struct_tpm_event_log2_validate_internal(FuStructTpmEventLog2 *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    return TRUE;
}
static gboolean
fu_struct_tpm_event_log2_parse_internal(FuStructTpmEventLog2 *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_tpm_event_log2_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_tpm_event_log2_validate_internal(st, error))
        return FALSE;
    return TRUE;
}
/**
 * fu_struct_tpm_event_log2_parse_stream: (skip):
 **/
FuStructTpmEventLog2 *
fu_struct_tpm_event_log2_parse_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructTpmEventLog2) st = fu_struct_tpm_event_log2_new_internal();
    st->buf = fu_input_stream_read_byte_array(stream, offset, 12, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructTpmEventLog2 failed read of 0x%x: ", (guint) 12);
        return NULL;
    }
    if (st->buf->len != 12) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructTpmEventLog2 requested 0x%x and got 0x%x",
                    (guint) 12,
                    (guint) st->buf->len);
        return NULL;
    }
    if (!fu_struct_tpm_event_log2_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
