void test_checksum_is_verified_when_correct(void) { uint8_t data[0x10] = {0x21,0x46,0x01,0x36,0x01,0x21,0x47,0x01,0x36,0x00,0x7E,0xFE,0x09,0xD2,0x19,0x01}; ihex_record_t r = { .ihr_length = 0x10, .ihr_type = IHEX_DATA, .ihr_address = 0x0100, .ihr_data = (ihex_rdata_t) &data, .ihr_checksum = 0x40 }; CU_ASSERT_EQUAL(ihex_check_record(&r), 0); } void test_checksum_is_not_verified_when_incorrect(void) { uint8_t data[0x10] = {0x21,0x46,0x01,0x36,0x01,0x21,0x47,0x01,0x36,0x00,0x7E,0xFE,0x09,0xD2,0x19,0x01}; ihex_record_t r = { .ihr_length = 0x10, .ihr_type = IHEX_DATA, .ihr_address = 0x0100, .ihr_data = (ihex_rdata_t) &data, .ihr_checksum = 0x20 }; CU_ASSERT_EQUAL(ihex_check_record(&r), 1); } static void test_can_read_ihex_rs_from_string(char* s) { ihex_recordset_t *records = ihex_rs_from_string(s); CU_ASSERT_PTR_NOT_NULL_FATAL(records); CU_ASSERT_EQUAL_FATAL(records->ihrs_count, 2); CU_ASSERT_EQUAL(records->ihrs_records[0].ihr_length, 0x10); CU_ASSERT_EQUAL(records->ihrs_records[0].ihr_data[0], 0x21); CU_ASSERT_EQUAL(records->ihrs_records[0].ihr_type, IHEX_DATA); CU_ASSERT_EQUAL(records->ihrs_records[0].ihr_address, 0x0100); }
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; }