static int ihex_parse_single_record(ihex_rdata_t data, unsigned int length, ihex_record_t* record)
{
	uint_t i;
	
	// Records needs to begin with record mark (usually ":")
	if (*(data) != IHEX_CHR_RECORDMARK)
	{
		IHEX_SET_ERROR_RETURN(IHEX_ERR_PARSE_ERROR, "Missing record mark");
	}
	
	// Record layout:
	//               1         2         3         4
	// 0 12 3456 78 90123456789012345678901234567890 12
	// : 10 0100 00 214601360121470136007EFE09D21901 40
	
	record->ihr_length   = (ihex_rlen_t)  length;
	record->ihr_address  = (ihex_addr_t)  ihex_fromhex16(data + 3);
	record->ihr_type     = (ihex_rtype_t) ihex_fromhex8 (data + 7);
	record->ihr_checksum = (ihex_rchks_t) ihex_fromhex8 (data + 9 + record->ihr_length * 2);

	if ((record->ihr_data = (ihex_rdata_t) malloc(record->ihr_length)) == NULL)
	{
		IHEX_SET_ERROR_RETURN(IHEX_ERR_MALLOC_FAILED, "Could not allocate memory");
	}
	
	// Records needs to end with CRLF or LF.
	if (   (   data[11 + record->ihr_length * 2] != 0x0D
	        || data[12 + record->ihr_length * 2] != 0x0A)
	    && (data[11 + record->ihr_length * 2] != 0x0A))
	{
		free(record->ihr_data);
		IHEX_SET_ERROR_RETURN(IHEX_ERR_WRONG_RECORD_LENGTH, "Incorrect record length");
	}
	
	for (i = 0; i < record->ihr_length; i ++)
	{
		if (data[9 + i*2] == 0x0A || data[9 + i*2] == 0x0D)
		{
			free(record->ihr_data);
			IHEX_SET_ERROR_RETURN(IHEX_ERR_WRONG_RECORD_LENGTH, "Unexpected end of line");
		}
		record->ihr_data[i] = ihex_fromhex8(data + 9 + i*2);
	}
	
	if (ihex_check_record(record) != 0)
	{
		free(record->ihr_data);
		IHEX_SET_ERROR_RETURN(IHEX_ERR_INCORRECT_CHECKSUM, "Checksum validation failed");
	}
	
	return 0;
}
Exemple #2
0
void test_can_parse_address_1(void)
{
	CU_ASSERT_EQUAL(ihex_fromhex16((uint8_t*) "1000"), 0x1000);
}