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

#include "config.h"

#include <glib.h>

#include "fu-egis-moc-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_egis_moc_cmd_req_ref: (skip):
 **/
FuStructEgisMocCmdReq *
fu_struct_egis_moc_cmd_req_ref(FuStructEgisMocCmdReq *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_egis_moc_cmd_req_unref: (skip):
 **/
void
fu_struct_egis_moc_cmd_req_unref(FuStructEgisMocCmdReq *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructEgisMocCmdReq refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_egis_moc_cmd_req_new_internal: (skip):
 **/
static FuStructEgisMocCmdReq *
fu_struct_egis_moc_cmd_req_new_internal(void)
{
    FuStructEgisMocCmdReq *st = g_new0(FuStructEgisMocCmdReq, 1);
    st->refcount = 1;
    return st;
}

/* getters */

/* setters */
/**
 * fu_struct_egis_moc_cmd_req_set_cla: (skip):
 **/
static void
fu_struct_egis_moc_cmd_req_set_cla(FuStructEgisMocCmdReq *st, FuEgisMocCla value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[0] = value;
}
/**
 * fu_struct_egis_moc_cmd_req_set_ins: (skip):
 **/
void
fu_struct_egis_moc_cmd_req_set_ins(FuStructEgisMocCmdReq *st, FuEgisMocCmd value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[1] = value;
}
/**
 * fu_struct_egis_moc_cmd_req_set_lc3: (skip):
 **/
void
fu_struct_egis_moc_cmd_req_set_lc3(FuStructEgisMocCmdReq *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[6] = value;
}
/**
 * fu_struct_egis_moc_cmd_req_new: (skip):
 **/
FuStructEgisMocCmdReq *
fu_struct_egis_moc_cmd_req_new(void)
{
    FuStructEgisMocCmdReq *st = fu_struct_egis_moc_cmd_req_new_internal();
    st->buf = g_byte_array_sized_new(7);
    fu_byte_array_set_size(st->buf, 7, 0x0);
    fu_struct_egis_moc_cmd_req_set_cla(st, FU_EGIS_MOC_CLA_APDU_GENERAL);
    return st;
}
/**
 * fu_struct_egis_moc_version_info_ref: (skip):
 **/
FuStructEgisMocVersionInfo *
fu_struct_egis_moc_version_info_ref(FuStructEgisMocVersionInfo *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_egis_moc_version_info_unref: (skip):
 **/
void
fu_struct_egis_moc_version_info_unref(FuStructEgisMocVersionInfo *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructEgisMocVersionInfo refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}

/* getters */

/* setters */
/**
 * fu_struct_egis_moc_pkg_header_ref: (skip):
 **/
FuStructEgisMocPkgHeader *
fu_struct_egis_moc_pkg_header_ref(FuStructEgisMocPkgHeader *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_egis_moc_pkg_header_unref: (skip):
 **/
void
fu_struct_egis_moc_pkg_header_unref(FuStructEgisMocPkgHeader *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructEgisMocPkgHeader refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_egis_moc_pkg_header_new_internal: (skip):
 **/
static FuStructEgisMocPkgHeader *
fu_struct_egis_moc_pkg_header_new_internal(void)
{
    FuStructEgisMocPkgHeader *st = g_new0(FuStructEgisMocPkgHeader, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_egis_moc_pkg_header_get_sync: (skip):
 **/
guint32
fu_struct_egis_moc_pkg_header_get_sync(const FuStructEgisMocPkgHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 0, G_BIG_ENDIAN);
}
/**
 * fu_struct_egis_moc_pkg_header_get_id: (skip):
 **/
guint32
fu_struct_egis_moc_pkg_header_get_id(const FuStructEgisMocPkgHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 4, G_BIG_ENDIAN);
}
/**
 * fu_struct_egis_moc_pkg_header_get_chksum: (skip):
 **/
guint16
fu_struct_egis_moc_pkg_header_get_chksum(const FuStructEgisMocPkgHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 8, G_BIG_ENDIAN);
}
/**
 * fu_struct_egis_moc_pkg_header_get_len: (skip):
 **/
guint32
fu_struct_egis_moc_pkg_header_get_len(const FuStructEgisMocPkgHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 10, G_BIG_ENDIAN);
}

/* setters */
/**
 * fu_struct_egis_moc_pkg_header_set_sync: (skip):
 **/
void
fu_struct_egis_moc_pkg_header_set_sync(FuStructEgisMocPkgHeader *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 0, value, G_BIG_ENDIAN);
}
/**
 * fu_struct_egis_moc_pkg_header_set_id: (skip):
 **/
void
fu_struct_egis_moc_pkg_header_set_id(FuStructEgisMocPkgHeader *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 4, value, G_BIG_ENDIAN);
}
/**
 * fu_struct_egis_moc_pkg_header_set_chksum: (skip):
 **/
void
fu_struct_egis_moc_pkg_header_set_chksum(FuStructEgisMocPkgHeader *st, guint16 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->buf->data + 8, value, G_BIG_ENDIAN);
}
/**
 * fu_struct_egis_moc_pkg_header_set_len: (skip):
 **/
void
fu_struct_egis_moc_pkg_header_set_len(FuStructEgisMocPkgHeader *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 10, value, G_BIG_ENDIAN);
}
/**
 * fu_struct_egis_moc_pkg_header_new: (skip):
 **/
FuStructEgisMocPkgHeader *
fu_struct_egis_moc_pkg_header_new(void)
{
    FuStructEgisMocPkgHeader *st = fu_struct_egis_moc_pkg_header_new_internal();
    st->buf = g_byte_array_sized_new(14);
    fu_byte_array_set_size(st->buf, 14, 0x0);
    return st;
}
/**
 * fu_struct_egis_moc_pkg_header_to_string: (skip):
 **/
static gchar *
fu_struct_egis_moc_pkg_header_to_string(const FuStructEgisMocPkgHeader *st)
{
    g_autoptr(GString) str = g_string_new("FuStructEgisMocPkgHeader:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  sync: 0x%x\n",
                           (guint) fu_struct_egis_moc_pkg_header_get_sync(st));
    g_string_append_printf(str, "  id: 0x%x\n",
                           (guint) fu_struct_egis_moc_pkg_header_get_id(st));
    g_string_append_printf(str, "  chksum: 0x%x\n",
                           (guint) fu_struct_egis_moc_pkg_header_get_chksum(st));
    g_string_append_printf(str, "  len: 0x%x\n",
                           (guint) fu_struct_egis_moc_pkg_header_get_len(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_egis_moc_pkg_header_validate_internal(FuStructEgisMocPkgHeader *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    return TRUE;
}
static gboolean
fu_struct_egis_moc_pkg_header_parse_internal(FuStructEgisMocPkgHeader *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_egis_moc_pkg_header_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_egis_moc_pkg_header_validate_internal(st, error))
        return FALSE;
    return TRUE;
}

/**
 * fu_struct_egis_moc_pkg_header_parse: (skip):
 **/
FuStructEgisMocPkgHeader *
fu_struct_egis_moc_pkg_header_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(FuStructEgisMocPkgHeader) st = fu_struct_egis_moc_pkg_header_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, 14, error)) {
        g_prefix_error_literal(error, "invalid struct FuStructEgisMocPkgHeader: ");
        return NULL;
    }
    st->buf = g_byte_array_new();
    g_byte_array_append(st->buf, buf + offset, 14);
    if (!fu_struct_egis_moc_pkg_header_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
