<commit_before>#include "dfu.h"
#include "dfu-internal.h"

enum ihex_record_type {
	IHEX_DATA = 0,
	IHEX_EOF = 1,
	IHEX_EXT_SEG_ADDRESS = 2,
	IHEX_START_SEG_ADDRESS = 3,
	IHEX_EXT_LINEAR_ADDRESS = 4,
	IHEX_START_LINEAR_ADDRESS = 5,
};

struct ihex_line_data {
	int start_index;
	int data_start_index;
	int byte_count;
	int address;
	enum ihex_record_type record_type;
};

struct ihex_format_data {
	uint32_t curr_addr;
};

/* Just one instance for the moment */
static struct ihex_format_data ihdata;

static inline uint32_t _hi_addr(uint32_t a)
{
	return a & 0xffff0000;
}

static inline int _go_on(struct dfu_binary_file *f, int index, int amount)
{
	return (index + amount) & (f->max_size - 1);
}

static inline int _next(struct dfu_binary_file *f, int index)
{
	return _go_on(f, index, 1);
}

static inline int _hex_to_int(char c)
{
	if (c >= '0' && c <= '9')
		return c - '0';
	if (c >= 'a' && c <= 'f')
		return c - 'a' + 10;
	if (c >= 'A' && c <= 'F')
		return c - 'A' + 10;
	return -1;
}

/*
 * Decode given number of ascii chars (nbytes), part of a single multibyte
 * (big endian) quantity or of a buffer of bytes, taking data from an
 * input buffer starting at offset *index.
 * Update *index and stop at buffer's head or when all bytes have
 * been decoded. Return number of bytes decoded ( > 0), not enough input
 * bytes ( == 0) or error ( < 0)
 */
static int _decode_hex(struct dfu_binary_file *f, int *index, int nbytes,
		       int *out, void *buf_out)
{
	int i, stat = 0, ret;
	char *ptr = f->buf, *dst = buf_out;
	int _out = 0;

	if (out && (nbytes > sizeof(*out) * 2))
		return -1;
	for (i = 0; i < nbytes && *index != f->head;
	     i++, *index = _next(f, *index)) {
		stat = _hex_to_int(ptr[*index]);
		if (stat < 0)
			return stat;
		/* big endian */
		_out |= (stat << (4 * (nbytes - (i + 1))));
		if (dst) {
			if (!(i & 0x1)) {
				dst[i/2] = (stat << 4);
				continue;
			}
			dst[i/2] |= stat;
		}
	}
	if (out)
		*out = _out;
	ret = *index == f->head ? 0 : i;
	dfu_dbg("%s returns %d\n", __func__, ret);
	return ret;
}

static inline int _decode_hex_byte(struct dfu_binary_file *f, int *index,
				   int *out)
{
	return _decode_hex(f, index, 2, out, NULL);
}

static inline int _decode_hex_word(struct dfu_binary_file *f, int *index,
				   int *out)
{
	return _decode_hex(f, index, 4, out, NULL);
}

static inline int _decode_hex_dword(struct dfu_binary_file *f, int *index,
				    int *out)
{
	return _decode_hex(f, index, 8, out, NULL);
}

static inline int _decode_hex_buf(struct dfu_binary_file *f, int *index,
				  char *buf, int len)
{
	return _decode_hex(f, index, len, NULL, buf);
}


/*
 * Check line's checksum
 * Assumes f->tail points to checksum start and that the checksum field
 * in *ld is up to date
 * Updates bf->tail
 */
static int _check_line(struct dfu_binary_file *bf, struct ihex_line_data *ld)
{
	return 0;
}

/*
 * Search for the beginning of a line, update index and return number of
 * bytes read before finding the ':'
 */
static inline int _search_line(struct dfu_binary_file *f, int *index)<commit_msg>fix, _check_line(): return number of chars in checksum anyway<commit_after>#include "dfu.h"
#include "dfu-internal.h"

enum ihex_record_type {
	IHEX_DATA = 0,
	IHEX_EOF = 1,
	IHEX_EXT_SEG_ADDRESS = 2,
	IHEX_START_SEG_ADDRESS = 3,
	IHEX_EXT_LINEAR_ADDRESS = 4,
	IHEX_START_LINEAR_ADDRESS = 5,
};

struct ihex_line_data {
	int start_index;
	int data_start_index;
	int byte_count;
	int address;
	enum ihex_record_type record_type;
};

struct ihex_format_data {
	uint32_t curr_addr;
};

/* Just one instance for the moment */
static struct ihex_format_data ihdata;

static inline uint32_t _hi_addr(uint32_t a)
{
	return a & 0xffff0000;
}

static inline int _go_on(struct dfu_binary_file *f, int index, int amount)
{
	return (index + amount) & (f->max_size - 1);
}

static inline int _next(struct dfu_binary_file *f, int index)
{
	return _go_on(f, index, 1);
}

static inline int _hex_to_int(char c)
{
	if (c >= '0' && c <= '9')
		return c - '0';
	if (c >= 'a' && c <= 'f')
		return c - 'a' + 10;
	if (c >= 'A' && c <= 'F')
		return c - 'A' + 10;
	return -1;
}

/*
 * Decode given number of ascii chars (nbytes), part of a single multibyte
 * (big endian) quantity or of a buffer of bytes, taking data from an
 * input buffer starting at offset *index.
 * Update *index and stop at buffer's head or when all bytes have
 * been decoded. Return number of bytes decoded ( > 0), not enough input
 * bytes ( == 0) or error ( < 0)
 */
static int _decode_hex(struct dfu_binary_file *f, int *index, int nbytes,
		       int *out, void *buf_out)
{
	int i, stat = 0, ret;
	char *ptr = f->buf, *dst = buf_out;
	int _out = 0;

	if (out && (nbytes > sizeof(*out) * 2))
		return -1;
	for (i = 0; i < nbytes && *index != f->head;
	     i++, *index = _next(f, *index)) {
		stat = _hex_to_int(ptr[*index]);
		if (stat < 0)
			return stat;
		/* big endian */
		_out |= (stat << (4 * (nbytes - (i + 1))));
		if (dst) {
			if (!(i & 0x1)) {
				dst[i/2] = (stat << 4);
				continue;
			}
			dst[i/2] |= stat;
		}
	}
	if (out)
		*out = _out;
	ret = *index == f->head ? 0 : i;
	dfu_dbg("%s returns %d\n", __func__, ret);
	return ret;
}

static inline int _decode_hex_byte(struct dfu_binary_file *f, int *index,
				   int *out)
{
	return _decode_hex(f, index, 2, out, NULL);
}

static inline int _decode_hex_word(struct dfu_binary_file *f, int *index,
				   int *out)
{
	return _decode_hex(f, index, 4, out, NULL);
}

static inline int _decode_hex_dword(struct dfu_binary_file *f, int *index,
				    int *out)
{
	return _decode_hex(f, index, 8, out, NULL);
}

static inline int _decode_hex_buf(struct dfu_binary_file *f, int *index,
				  char *buf, int len)
{
	return _decode_hex(f, index, len, NULL, buf);
}


/*
 * Check line's checksum
 * Assumes f->tail points to checksum start and that the checksum field
 * in *ld is up to date
 * Updates bf->tail
 */
static int _check_line(struct dfu_binary_file *bf, struct ihex_line_data *ld)
{
	return 2;
}

/*
 * Search for the beginning of a line, update index and return number of
 * bytes read before finding the ':'
 */
static inline int _search_line(struct dfu_binary_file *f, int *index)
{'fraction_non_alphanumeric': 0.13490779682950502, 'fraction_numerical': 0.01552895503073439, 'mean_word_length': 3.5429831006612784, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, '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': '1172354', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2407, 'n_words': 771}