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

#include "fu-uf2-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_uf2_get_magic0: (skip):
 **/
static guint32
fu_struct_uf2_get_magic0(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 0, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_get_magic1: (skip):
 **/
static guint32
fu_struct_uf2_get_magic1(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 4, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_get_flags: (skip):
 **/
guint32
fu_struct_uf2_get_flags(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 8, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_get_target_addr: (skip):
 **/
guint32
fu_struct_uf2_get_target_addr(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 12, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_get_payload_size: (skip):
 **/
guint32
fu_struct_uf2_get_payload_size(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 16, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_get_block_no: (skip):
 **/
guint32
fu_struct_uf2_get_block_no(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 20, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_get_num_blocks: (skip):
 **/
guint32
fu_struct_uf2_get_num_blocks(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 24, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_get_family_id: (skip):
 **/
guint32
fu_struct_uf2_get_family_id(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 28, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_get_data: (skip):
 **/
const guint8 *
fu_struct_uf2_get_data(const GByteArray *st, gsize *bufsz)
{
    g_return_val_if_fail(st != NULL, NULL);
    if (bufsz != NULL)
        *bufsz = 476;
    return st->data + 32;
}
/**
 * fu_struct_uf2_get_magic_end: (skip):
 **/
static guint32
fu_struct_uf2_get_magic_end(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 508, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_uf2_set_magic0: (skip):
 **/
static void
fu_struct_uf2_set_magic0(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 0, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_set_magic1: (skip):
 **/
static void
fu_struct_uf2_set_magic1(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 4, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_set_flags: (skip):
 **/
void
fu_struct_uf2_set_flags(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 8, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_set_target_addr: (skip):
 **/
void
fu_struct_uf2_set_target_addr(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 12, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_set_payload_size: (skip):
 **/
void
fu_struct_uf2_set_payload_size(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 16, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_set_block_no: (skip):
 **/
void
fu_struct_uf2_set_block_no(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 20, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_set_num_blocks: (skip):
 **/
void
fu_struct_uf2_set_num_blocks(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 24, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_set_family_id: (skip):
 **/
void
fu_struct_uf2_set_family_id(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 28, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_set_data: (skip):
 **/
gboolean
fu_struct_uf2_set_data(GByteArray *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->data, st->len, 32, buf, bufsz, 0x0, bufsz, error);
}
/**
 * fu_struct_uf2_set_magic_end: (skip):
 **/
static void
fu_struct_uf2_set_magic_end(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 508, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_uf2_new: (skip):
 **/
GByteArray *
fu_struct_uf2_new(void)
{
    GByteArray *st = g_byte_array_sized_new(512);
    fu_byte_array_set_size(st, 512, 0x0);
    fu_struct_uf2_set_magic0(st, 0x0A324655);
    fu_struct_uf2_set_magic1(st, 0x9E5D5157);
    fu_struct_uf2_set_magic_end(st, 0x0AB16F30);
    return st;
}
/**
 * fu_struct_uf2_to_string: (skip):
 **/
static gchar *
fu_struct_uf2_to_string(const GByteArray *st)
{
    g_autoptr(GString) str = g_string_new("Uf2:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  flags: 0x%x\n",
                           (guint) fu_struct_uf2_get_flags(st));
    g_string_append_printf(str, "  target_addr: 0x%x\n",
                           (guint) fu_struct_uf2_get_target_addr(st));
    g_string_append_printf(str, "  payload_size: 0x%x\n",
                           (guint) fu_struct_uf2_get_payload_size(st));
    g_string_append_printf(str, "  block_no: 0x%x\n",
                           (guint) fu_struct_uf2_get_block_no(st));
    g_string_append_printf(str, "  num_blocks: 0x%x\n",
                           (guint) fu_struct_uf2_get_num_blocks(st));
    g_string_append_printf(str, "  family_id: 0x%x\n",
                           (guint) fu_struct_uf2_get_family_id(st));
    {
        gsize bufsz = 0;
        const guint8 *buf = fu_struct_uf2_get_data(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, "  data: 0x%s\n", tmp->str);
    }
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
/**
 * fu_struct_uf2_parse: (skip):
 **/
GByteArray *
fu_struct_uf2_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, 512, error)) {
        g_prefix_error(error, "invalid struct Uf2: ");
        return NULL;
    }
    g_byte_array_append(st, buf + offset, 512);
    if (fu_struct_uf2_get_magic0(st) != 0x0A324655) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant Uf2.magic0 was not valid, expected 0x0A324655");
        return NULL;
    }
    if (fu_struct_uf2_get_magic1(st) != 0x9E5D5157) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant Uf2.magic1 was not valid, expected 0x9E5D5157");
        return NULL;
    }
    if (fu_struct_uf2_get_magic_end(st) != 0x0AB16F30) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant Uf2.magic_end was not valid, expected 0x0AB16F30");
        return NULL;
    }
    str = fu_struct_uf2_to_string(st);
    g_debug("%s", str);
    return g_steal_pointer(&st);
}
