/* auto-generated, do not modify */
#include "config.h"

#include "fu-audio-s5gen2-fw-struct.h"
#include "fu-byte-array.h"
#include "fu-mem-private.h"
#include "fu-string.h"

#ifdef G_LOG_DOMAIN
  #undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "FuStruct"
/* getters */
/**
 * fu_struct_qc_fw_update_hdr_get_magic1: (skip):
 **/
static guint32
fu_struct_qc_fw_update_hdr_get_magic1(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 0, G_BIG_ENDIAN);
}
/**
 * fu_struct_qc_fw_update_hdr_get_magic2: (skip):
 **/
static guint16
fu_struct_qc_fw_update_hdr_get_magic2(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 4, G_BIG_ENDIAN);
}
/**
 * fu_struct_qc_fw_update_hdr_get_magic3: (skip):
 **/
static guint8
fu_struct_qc_fw_update_hdr_get_magic3(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->data[6];
}
/**
 * fu_struct_qc_fw_update_hdr_get_protocol: (skip):
 **/
guint8
fu_struct_qc_fw_update_hdr_get_protocol(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->data[7];
}
/**
 * fu_struct_qc_fw_update_hdr_get_length: (skip):
 **/
guint32
fu_struct_qc_fw_update_hdr_get_length(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 8, G_BIG_ENDIAN);
}
/**
 * fu_struct_qc_fw_update_hdr_get_dev_variant: (skip):
 **/
const guint8 *
fu_struct_qc_fw_update_hdr_get_dev_variant(const GByteArray *st, gsize *bufsz)
{
    g_return_val_if_fail(st != NULL, NULL);
    if (bufsz != NULL)
        *bufsz = 8;
    return st->data + 12;
}
/**
 * fu_struct_qc_fw_update_hdr_get_major: (skip):
 **/
guint16
fu_struct_qc_fw_update_hdr_get_major(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 20, G_BIG_ENDIAN);
}
/**
 * fu_struct_qc_fw_update_hdr_get_minor: (skip):
 **/
guint16
fu_struct_qc_fw_update_hdr_get_minor(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 22, G_BIG_ENDIAN);
}
/**
 * fu_struct_qc_fw_update_hdr_get_upgrades: (skip):
 **/
guint16
fu_struct_qc_fw_update_hdr_get_upgrades(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 24, G_BIG_ENDIAN);
}

/* setters */
/**
 * fu_struct_qc_fw_update_hdr_to_string: (skip):
 **/
static gchar *
fu_struct_qc_fw_update_hdr_to_string(const GByteArray *st)
{
    g_autoptr(GString) str = g_string_new("QcFwUpdateHdr:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  protocol: 0x%x\n",
                           (guint) fu_struct_qc_fw_update_hdr_get_protocol(st));
    g_string_append_printf(str, "  length: 0x%x\n",
                           (guint) fu_struct_qc_fw_update_hdr_get_length(st));
    {
        gsize bufsz = 0;
        const guint8 *buf = fu_struct_qc_fw_update_hdr_get_dev_variant(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, "  dev_variant: 0x%s\n", tmp->str);
    }
    g_string_append_printf(str, "  major: 0x%x\n",
                           (guint) fu_struct_qc_fw_update_hdr_get_major(st));
    g_string_append_printf(str, "  minor: 0x%x\n",
                           (guint) fu_struct_qc_fw_update_hdr_get_minor(st));
    g_string_append_printf(str, "  upgrades: 0x%x\n",
                           (guint) fu_struct_qc_fw_update_hdr_get_upgrades(st));
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
/**
 * fu_struct_qc_fw_update_hdr_parse: (skip):
 **/
static GByteArray *
fu_struct_qc_fw_update_hdr_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(GByteArray) st = g_byte_array_new();
    g_autofree gchar *str = NULL;
    g_return_val_if_fail(buf != NULL, NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
    if (!fu_memchk_read(bufsz, offset, 26, error)) {
        g_prefix_error(error, "invalid struct QcFwUpdateHdr: ");
        return NULL;
    }
    g_byte_array_append(st, buf + offset, 26);
    if (fu_struct_qc_fw_update_hdr_get_magic1(st) != 0x41505055) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant QcFwUpdateHdr.magic1 was not valid, expected 0x41505055");
        return NULL;
    }
    if (fu_struct_qc_fw_update_hdr_get_magic2(st) != 0x4844) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant QcFwUpdateHdr.magic2 was not valid, expected 0x4844");
        return NULL;
    }
    if (fu_struct_qc_fw_update_hdr_get_magic3(st) != 0x52) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant QcFwUpdateHdr.magic3 was not valid, expected 0x52");
        return NULL;
    }
    str = fu_struct_qc_fw_update_hdr_to_string(st);
    g_debug("%s", str);
    return g_steal_pointer(&st);
}
/**
 * fu_struct_qc_fw_update_hdr_parse_bytes: (skip):
 **/
GByteArray *
fu_struct_qc_fw_update_hdr_parse_bytes(GBytes *blob, gsize offset, GError **error)
{
    gsize bufsz = 0;
    const guint8 *buf = g_bytes_get_data(blob, &bufsz);
    return fu_struct_qc_fw_update_hdr_parse(buf, bufsz, offset, error);
}
/**
 * fu_struct_qc_fw_update_hdr_validate: (skip):
 **/
static gboolean
fu_struct_qc_fw_update_hdr_validate(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    GByteArray st = {.data = (guint8 *) buf + offset, .len = bufsz - offset, };
    g_return_val_if_fail(buf != NULL, FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
    if (!fu_memchk_read(bufsz, offset, 26, error)) {
        g_prefix_error(error, "invalid struct QcFwUpdateHdr: ");
        return FALSE;
    }
    if (fu_struct_qc_fw_update_hdr_get_magic1(&st) != 0x41505055) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant QcFwUpdateHdr.magic1 was not valid");
        return FALSE;
    }
    if (fu_struct_qc_fw_update_hdr_get_magic2(&st) != 0x4844) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant QcFwUpdateHdr.magic2 was not valid");
        return FALSE;
    }
    if (fu_struct_qc_fw_update_hdr_get_magic3(&st) != 0x52) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant QcFwUpdateHdr.magic3 was not valid");
        return FALSE;
    }
    return TRUE;
}
/**
 * fu_struct_qc_fw_update_hdr_validate_bytes: (skip):
 **/
gboolean
fu_struct_qc_fw_update_hdr_validate_bytes(GBytes *blob, gsize offset, GError **error)
{
    gsize bufsz = 0;
    const guint8 *buf = g_bytes_get_data(blob, &bufsz);
    return fu_struct_qc_fw_update_hdr_validate(buf, bufsz, offset, error);
}
