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

#include "config.h"

#include <glib.h>

#include "fu-sbatlevel-section-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_sbat_level_section_header_ref: (skip):
 **/
FuStructSbatLevelSectionHeader *
fu_struct_sbat_level_section_header_ref(FuStructSbatLevelSectionHeader *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_sbat_level_section_header_unref: (skip):
 **/
void
fu_struct_sbat_level_section_header_unref(FuStructSbatLevelSectionHeader *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructSbatLevelSectionHeader refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_sbat_level_section_header_new_internal: (skip):
 **/
static FuStructSbatLevelSectionHeader *
fu_struct_sbat_level_section_header_new_internal(void)
{
    FuStructSbatLevelSectionHeader *st = g_new0(FuStructSbatLevelSectionHeader, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_sbat_level_section_header_get_version: (skip):
 **/
static guint32
fu_struct_sbat_level_section_header_get_version(const FuStructSbatLevelSectionHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 0, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_sbat_level_section_header_get_previous: (skip):
 **/
guint32
fu_struct_sbat_level_section_header_get_previous(const FuStructSbatLevelSectionHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 4, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_sbat_level_section_header_get_latest: (skip):
 **/
guint32
fu_struct_sbat_level_section_header_get_latest(const FuStructSbatLevelSectionHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 8, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_sbat_level_section_header_set_version: (skip):
 **/
static void
fu_struct_sbat_level_section_header_set_version(FuStructSbatLevelSectionHeader *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 0, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_sbat_level_section_header_set_previous: (skip):
 **/
void
fu_struct_sbat_level_section_header_set_previous(FuStructSbatLevelSectionHeader *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 4, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_sbat_level_section_header_set_latest: (skip):
 **/
void
fu_struct_sbat_level_section_header_set_latest(FuStructSbatLevelSectionHeader *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 8, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_sbat_level_section_header_new: (skip):
 **/
FuStructSbatLevelSectionHeader *
fu_struct_sbat_level_section_header_new(void)
{
    FuStructSbatLevelSectionHeader *st = fu_struct_sbat_level_section_header_new_internal();
    st->buf = g_byte_array_sized_new(12);
    fu_byte_array_set_size(st->buf, 12, 0x0);
    fu_struct_sbat_level_section_header_set_version(st, 0x0);
    return st;
}
/**
 * fu_struct_sbat_level_section_header_to_string: (skip):
 **/
static gchar *
fu_struct_sbat_level_section_header_to_string(const FuStructSbatLevelSectionHeader *st)
{
    g_autoptr(GString) str = g_string_new("FuStructSbatLevelSectionHeader:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  version: 0x%x\n",
                           (guint) fu_struct_sbat_level_section_header_get_version(st));
    g_string_append_printf(str, "  previous: 0x%x\n",
                           (guint) fu_struct_sbat_level_section_header_get_previous(st));
    g_string_append_printf(str, "  latest: 0x%x\n",
                           (guint) fu_struct_sbat_level_section_header_get_latest(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_sbat_level_section_header_validate_internal(FuStructSbatLevelSectionHeader *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    if (fu_struct_sbat_level_section_header_get_version(st) != 0x0) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructSbatLevelSectionHeader.version was not valid, "
                    "expected 0x%x and got 0x%x",
                    (guint) 0x0,
                    (guint) fu_struct_sbat_level_section_header_get_version(st));
        return FALSE;
    }
    return TRUE;
}
static gboolean
fu_struct_sbat_level_section_header_parse_internal(FuStructSbatLevelSectionHeader *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_sbat_level_section_header_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_sbat_level_section_header_validate_internal(st, error))
        return FALSE;
    return TRUE;
}
/**
 * fu_struct_sbat_level_section_header_parse_stream: (skip):
 **/
FuStructSbatLevelSectionHeader *
fu_struct_sbat_level_section_header_parse_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructSbatLevelSectionHeader) st = fu_struct_sbat_level_section_header_new_internal();
    st->buf = fu_input_stream_read_byte_array(stream, offset, 12, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructSbatLevelSectionHeader failed read of 0x%x: ", (guint) 12);
        return NULL;
    }
    if (st->buf->len != 12) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructSbatLevelSectionHeader requested 0x%x and got 0x%x",
                    (guint) 12,
                    (guint) st->buf->len);
        return NULL;
    }
    if (!fu_struct_sbat_level_section_header_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
