<commit_before>/*
 * Copyright (C) 2016 <NAME> <<EMAIL>>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

#include "config.h"

#include <fwupdplugin.h>

#include <string.h>

#include "fu-logitech-hidpp-bootloader.h"
#include "fu-logitech-hidpp-common.h"
#include "fu-logitech-hidpp-hidpp.h"

typedef struct {
	guint16 flash_addr_lo;
	guint16 flash_addr_hi;
	guint16 flash_blocksize;
} FuLogitechHidPpBootloaderPrivate;

#define FU_UNIFYING_DEVICE_EP1 0x81
#define FU_UNIFYING_DEVICE_EP3 0x83

G_DEFINE_TYPE_WITH_PRIVATE(FuLogitechHidPpBootloader,
			   fu_logitech_hidpp_bootloader,
			   FU_TYPE_HID_DEVICE)

#define GET_PRIVATE(o) (fu_logitech_hidpp_bootloader_get_instance_private(o))

static void
fu_logitech_hidpp_bootloader_to_string(FuDevice *device, guint idt, GString *str)
{
	FuLogitechHidPpBootloader *self = FU_UNIFYING_BOOTLOADER(device);
	FuLogitechHidPpBootloaderPrivate *priv = GET_PRIVATE(self);
	fu_common_string_append_kx(str, idt, "FlashAddrHigh", priv->flash_addr_hi);
	fu_common_string_append_kx(str, idt, "FlashAddrLow", priv->flash_addr_lo);
	fu_common_string_append_kx(str, idt, "FlashBlockSize", priv->flash_blocksize);
}

FuLogitechHidPpBootloaderRequest *
fu_logitech_hidpp_bootloader_request_new(void)
{
	FuLogitechHidPpBootloaderRequest *req = g_new0(FuLogitechHidPpBootloaderRequest, 1);
	return req;
}

GPtrArray *
fu_logitech_hidpp_bootloader_parse_requests(FuLogitechHidPpBootloader *self,
					    GBytes *fw,
					    GError **error)
{
	const gchar *tmp;
	g_auto(GStrv) lines = NULL;
	g_autoptr(GPtrArray) reqs = NULL;
	guint32 last_addr = 0;

	reqs = g_ptr_array_new_with_free_func(g_free);
	tmp = g_bytes_get_data(fw, NULL);
	lines = g_strsplit_set(tmp, "\n\r", -1);
	for (guint i = 0; lines[i] != NULL; i++) {
		g_autoptr(FuLogitechHidPpBootloaderRequest) payload = NULL;
		guint8 rec_type = 0x00;
		guint16 offset = 0x0000;
		gboolean exit = FALSE;
		gsize linesz = strlen(lines[i]);

		/* skip empty lines */
		tmp = lines[i];
		if (linesz < 5)
			continue;

		payload = fu_logitech_hidpp_bootloader_request_new();
		payload->len = fu_logitech_hidpp_buffer_read_uint8(tmp + 0x01);
		if (payload->len > 28) {
			g_set_error(error,
				    G_IO_ERROR,
				    G_IO_ERROR_INVALID_DATA,
				    "firmware data invalid: too large %u bytes",
				    payload->len);
			return NULL;
		}
		if (!fu_firmware_strparse_uint16_safe(tmp, linesz, 0x03, &payload->addr, error))
			return NULL;
		payload->cmd = FU_UNIFYING_BOOTLOADER_CMD_WRITE_RAM_BUFFER;

		rec_type = fu_logitech_hidpp_buffer_read_uint8(tmp + 0x07);

		switch (rec_type) {<commit_msg>trivial: Fix a -Waddress-of-packed-member warning<commit_after>/*
 * Copyright (C) 2016 <NAME> <<EMAIL>>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

#include "config.h"

#include <fwupdplugin.h>

#include <string.h>

#include "fu-logitech-hidpp-bootloader.h"
#include "fu-logitech-hidpp-common.h"
#include "fu-logitech-hidpp-hidpp.h"

typedef struct {
	guint16 flash_addr_lo;
	guint16 flash_addr_hi;
	guint16 flash_blocksize;
} FuLogitechHidPpBootloaderPrivate;

#define FU_UNIFYING_DEVICE_EP1 0x81
#define FU_UNIFYING_DEVICE_EP3 0x83

G_DEFINE_TYPE_WITH_PRIVATE(FuLogitechHidPpBootloader,
			   fu_logitech_hidpp_bootloader,
			   FU_TYPE_HID_DEVICE)

#define GET_PRIVATE(o) (fu_logitech_hidpp_bootloader_get_instance_private(o))

static void
fu_logitech_hidpp_bootloader_to_string(FuDevice *device, guint idt, GString *str)
{
	FuLogitechHidPpBootloader *self = FU_UNIFYING_BOOTLOADER(device);
	FuLogitechHidPpBootloaderPrivate *priv = GET_PRIVATE(self);
	fu_common_string_append_kx(str, idt, "FlashAddrHigh", priv->flash_addr_hi);
	fu_common_string_append_kx(str, idt, "FlashAddrLow", priv->flash_addr_lo);
	fu_common_string_append_kx(str, idt, "FlashBlockSize", priv->flash_blocksize);
}

FuLogitechHidPpBootloaderRequest *
fu_logitech_hidpp_bootloader_request_new(void)
{
	FuLogitechHidPpBootloaderRequest *req = g_new0(FuLogitechHidPpBootloaderRequest, 1);
	return req;
}

GPtrArray *
fu_logitech_hidpp_bootloader_parse_requests(FuLogitechHidPpBootloader *self,
					    GBytes *fw,
					    GError **error)
{
	const gchar *tmp;
	g_auto(GStrv) lines = NULL;
	g_autoptr(GPtrArray) reqs = NULL;
	guint32 last_addr = 0;

	reqs = g_ptr_array_new_with_free_func(g_free);
	tmp = g_bytes_get_data(fw, NULL);
	lines = g_strsplit_set(tmp, "\n\r", -1);
	for (guint i = 0; lines[i] != NULL; i++) {
		g_autoptr(FuLogitechHidPpBootloaderRequest) payload = NULL;
		guint8 rec_type = 0x00;
		guint16 offset = 0x0000;
		guint16 addr = 0x0;
		gboolean exit = FALSE;
		gsize linesz = strlen(lines[i]);

		/* skip empty lines */
		tmp = lines[i];
		if (linesz < 5)
			continue;

		payload = fu_logitech_hidpp_bootloader_request_new();
		payload->len = fu_logitech_hidpp_buffer_read_uint8(tmp + 0x01);
		if (payload->len > 28) {
			g_set_error(error,
				    G_IO_ERROR,
				    G_IO_ERROR_INVALID_DATA,
				    "firmware data invalid: too large %u bytes",
				    payload->len);
			return NULL;
		}
		if (!fu_firmware_strparse_uint16_safe(tmp, linesz, 0x03, &addr, error))
			return NULL;
		payload->addr = addr;
		payload->cmd = FU_UNIFYING_BOOTLOADER_CMD_WRITE_RAM_BUFFER;

		rec_type = fu_logitech_hidpp_buffer_read_uint8(tmp + 0x07);

		switch (rec_type) {
{'fraction_non_alphanumeric': 0.10702661305762971, 'fraction_numerical': 0.02144361478077733, 'mean_word_length': 5.513715710723192, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6715655', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2158, 'n_words': 349}