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

#include "config.h"

#include <glib.h>

#include "fu-qc-s5gen2-fw-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_struct_qc_fw_update_hdr_ref: (skip):
 **/
FuStructQcFwUpdateHdr *
fu_struct_qc_fw_update_hdr_ref(FuStructQcFwUpdateHdr *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_qc_fw_update_hdr_unref: (skip):
 **/
void
fu_struct_qc_fw_update_hdr_unref(FuStructQcFwUpdateHdr *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructQcFwUpdateHdr refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_qc_fw_update_hdr_new_internal: (skip):
 **/
static FuStructQcFwUpdateHdr *
fu_struct_qc_fw_update_hdr_new_internal(void)
{
    FuStructQcFwUpdateHdr *st = g_new0(FuStructQcFwUpdateHdr, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_qc_fw_update_hdr_get_magic1: (skip):
 **/
static guint32
fu_struct_qc_fw_update_hdr_get_magic1(const FuStructQcFwUpdateHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->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 FuStructQcFwUpdateHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->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 FuStructQcFwUpdateHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[6];
}
/**
 * fu_struct_qc_fw_update_hdr_get_protocol: (skip):
 **/
guint8
fu_struct_qc_fw_update_hdr_get_protocol(const FuStructQcFwUpdateHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[7];
}
/**
 * fu_struct_qc_fw_update_hdr_get_length: (skip):
 **/
guint32
fu_struct_qc_fw_update_hdr_get_length(const FuStructQcFwUpdateHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->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 FuStructQcFwUpdateHdr *st, gsize *bufsz)
{
    g_return_val_if_fail(st != NULL, NULL);
    if (bufsz != NULL)
        *bufsz = 8;
    return st->buf->data + 12;
}
/**
 * fu_struct_qc_fw_update_hdr_get_major: (skip):
 **/
guint16
fu_struct_qc_fw_update_hdr_get_major(const FuStructQcFwUpdateHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 20, G_BIG_ENDIAN);
}
/**
 * fu_struct_qc_fw_update_hdr_get_minor: (skip):
 **/
guint16
fu_struct_qc_fw_update_hdr_get_minor(const FuStructQcFwUpdateHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 22, G_BIG_ENDIAN);
}
/**
 * fu_struct_qc_fw_update_hdr_get_upgrades: (skip):
 **/
guint16
fu_struct_qc_fw_update_hdr_get_upgrades(const FuStructQcFwUpdateHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->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 FuStructQcFwUpdateHdr *st)
{
    g_autoptr(GString) str = g_string_new("FuStructQcFwUpdateHdr:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  magic1: 0x%x\n",
                           (guint) fu_struct_qc_fw_update_hdr_get_magic1(st));
    g_string_append_printf(str, "  magic2: 0x%x\n",
                           (guint) fu_struct_qc_fw_update_hdr_get_magic2(st));
    g_string_append_printf(str, "  magic3: 0x%x\n",
                           (guint) fu_struct_qc_fw_update_hdr_get_magic3(st));
    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);
}
static gboolean
fu_struct_qc_fw_update_hdr_validate_internal(FuStructQcFwUpdateHdr *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    if (fu_struct_qc_fw_update_hdr_get_magic1(st) != 0x41505055) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructQcFwUpdateHdr.magic1 was not valid, "
                    "expected 0x%x and got 0x%x",
                    (guint) 0x41505055,
                    (guint) fu_struct_qc_fw_update_hdr_get_magic1(st));
        return FALSE;
    }
    if (fu_struct_qc_fw_update_hdr_get_magic2(st) != 0x4844) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructQcFwUpdateHdr.magic2 was not valid, "
                    "expected 0x%x and got 0x%x",
                    (guint) 0x4844,
                    (guint) fu_struct_qc_fw_update_hdr_get_magic2(st));
        return FALSE;
    }
    if (fu_struct_qc_fw_update_hdr_get_magic3(st) != 0x52) {
        g_set_error_literal(error,
                            FWUPD_ERROR,
                            FWUPD_ERROR_INVALID_DATA,
                            "constant FuStructQcFwUpdateHdr.magic3 was not valid");
        return FALSE;
    }
    return TRUE;
}
/**
 * fu_struct_qc_fw_update_hdr_validate_stream: (skip):
 **/
gboolean
fu_struct_qc_fw_update_hdr_validate_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructQcFwUpdateHdr) st = fu_struct_qc_fw_update_hdr_new_internal();
    g_return_val_if_fail(G_IS_INPUT_STREAM(stream), FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
    st->buf = fu_input_stream_read_byte_array(stream, offset, 26, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructQcFwUpdateHdr failed read of 0x%x: ", (guint) 26);
        return FALSE;
    }
    if (st->buf->len != 26) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructQcFwUpdateHdr requested 0x%x and got 0x%x",
                    (guint) 26,
                    (guint) st->buf->len);
        return FALSE;
    }
    return fu_struct_qc_fw_update_hdr_validate_internal(st, error);
}
static gboolean
fu_struct_qc_fw_update_hdr_parse_internal(FuStructQcFwUpdateHdr *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_qc_fw_update_hdr_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_qc_fw_update_hdr_validate_internal(st, error))
        return FALSE;
    return TRUE;
}
/**
 * fu_struct_qc_fw_update_hdr_parse_stream: (skip):
 **/
FuStructQcFwUpdateHdr *
fu_struct_qc_fw_update_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructQcFwUpdateHdr) st = fu_struct_qc_fw_update_hdr_new_internal();
    st->buf = fu_input_stream_read_byte_array(stream, offset, 26, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructQcFwUpdateHdr failed read of 0x%x: ", (guint) 26);
        return NULL;
    }
    if (st->buf->len != 26) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructQcFwUpdateHdr requested 0x%x and got 0x%x",
                    (guint) 26,
                    (guint) st->buf->len);
        return NULL;
    }
    if (!fu_struct_qc_fw_update_hdr_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
