<commit_before>/***********************************
 Intel Hex File Reader
 
 <NAME>
 Signal 11 Software
 2013-04-28
**********************************/

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#include "hex.h"

#define LINE_LENGTH 1024

/* Intel Hex File format record types */
enum {
	REC_DATA = 0,
	REC_EOF = 1,
	REC_EXTENDED_SEGMENT_ADDRESS = 2,
	REC_START_SEGMENT_ADDRESS = 3,
	REC_EXTENDED_LINEAR_ADDRESS = 4,
	REC_START_LINEAR_ADDRESS = 5,
};

/* Character offsets for each piece of data in a record (line). */ 
enum {
	START_INDEX = 0,
	BYTE_COUNT_INDEX = 1,
	ADDRESS_INDEX = 3,
	RECORD_TYPE_INDEX = 7,
	DATA_INDEX = 9,
};


#define MAX(X,Y) ((X)>(Y)? (X): (Y))

static uint8_t read_byte(const char *line, size_t offset)
{
	char chars[3];
	char *endptr;
	uint8_t res;
	
	chars[0] = line[offset];
	chars[1] = line[offset+1];
	chars[2] = '\0';

	res = strtoul(chars, &endptr, 16);
	
	return res;
}

static uint16_t read_short(const char *line, size_t offset)
{
	char chars[5];
	char *endptr;
	uint16_t res;
	
	memcpy(chars, line + offset, 4);
	chars[5] = '\0';

	res = strtoul(chars, &endptr, 16);
	
	return res;
}

static bool
create_update_region(struct hex_data *hd, size_t address, uint8_t len) {
	struct hex_data_region *r;
	bool done = false;

	r = hd->regions;
	while (r) {
		if (address == r->address + r->len) {
			/* Address range is exactly at the end of this region.
			   Extend this region to include it. */
			   r->len += len;
			   done = true;
		}
		else if (address + len == r->address) {
			/* Address range is exactly before this region. Extend
			   this region backward to include it */
			   r->address -= len;
			   r->len += len;
			   done = true;
		}
		else if (address <= r->address && address + len > r->address) {
			/* Address range overlaps the beginning of this region.
			   This is an error. */
			return false;
		}
		else if (address < r->address + r->len) {
			/* Address range overlaps the middle or possibly the
			   the end of this region. This is an error */
			return false;
		}
		
		r = r->next;
	}

	if (!done) {
		/* Need to create a new region */
		r = calloc(1, sizeof(struct hex_data_region));<commit_msg>bootloader sw: Fix region overlap detection logic<commit_after>/***********************************
 Intel Hex File Reader
 
 <NAME>
 Signal 11 Software
 2013-04-28
**********************************/

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#include "hex.h"

#define LINE_LENGTH 1024

/* Intel Hex File format record types */
enum {
	REC_DATA = 0,
	REC_EOF = 1,
	REC_EXTENDED_SEGMENT_ADDRESS = 2,
	REC_START_SEGMENT_ADDRESS = 3,
	REC_EXTENDED_LINEAR_ADDRESS = 4,
	REC_START_LINEAR_ADDRESS = 5,
};

/* Character offsets for each piece of data in a record (line). */ 
enum {
	START_INDEX = 0,
	BYTE_COUNT_INDEX = 1,
	ADDRESS_INDEX = 3,
	RECORD_TYPE_INDEX = 7,
	DATA_INDEX = 9,
};


#define MAX(X,Y) ((X)>(Y)? (X): (Y))

static uint8_t read_byte(const char *line, size_t offset)
{
	char chars[3];
	char *endptr;
	uint8_t res;
	
	chars[0] = line[offset];
	chars[1] = line[offset+1];
	chars[2] = '\0';

	res = strtoul(chars, &endptr, 16);
	
	return res;
}

static uint16_t read_short(const char *line, size_t offset)
{
	char chars[5];
	char *endptr;
	uint16_t res;
	
	memcpy(chars, line + offset, 4);
	chars[5] = '\0';

	res = strtoul(chars, &endptr, 16);
	
	return res;
}

static bool
create_update_region(struct hex_data *hd, size_t address, uint8_t len) {
	struct hex_data_region *r;
	bool done = false;

	r = hd->regions;
	while (r) {
		if (address == r->address + r->len) {
			/* Address range is exactly at the end of this region.
			   Extend this region to include it. */
			   r->len += len;
			   done = true;
		}
		else if (address + len == r->address) {
			/* Address range is exactly before this region. Extend
			   this region backward to include it */
			   r->address -= len;
			   r->len += len;
			   done = true;
		}
		else if (address <= r->address && address + len > r->address) {
			/* Address range overlaps the beginning of this region.
			   This is an error. */
			return false;
		}
		else if (address >= r->address && address < r->address + r->len) {
			/* Address range overlaps the middle or possibly the
			   the end of this region. This is an error */
			return false;
		}
		
		r = r->next;
	}

	if (!done) {
		/* Need to create a new region */
		r = calloc(1, sizeof(struct hex_data_region));
{'fraction_non_alphanumeric': 0.16018457481872117, 'fraction_numerical': 0.02065480114260602, 'mean_word_length': 3.2701688555347093, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 47, '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': '6486756', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1614, 'n_words': 507}