-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathihex.c
60 lines (50 loc) · 1.07 KB
/
ihex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* ihex.c
*
* (c) 2016 Joachim Naulet <[email protected]>
*
* Created on 3/15/2016 by Joachim Naulet
*
*/
#include "ihex.h"
#include <stdio.h>
#include <string.h>
#include <cintelhex.h>
void *ihex_raw_from_file(const char *path,
unsigned int *size_r,
unsigned int *offset_r) {
int err;
uint8_t *buf;
uint32_t offset;
unsigned int n, i = 0;
ihex_recordset_t *rs;
ihex_record_t *record;
if(!(rs = ihex_rs_from_file(path))){
fprintf(stderr, "%s\n", ihex_error());
return NULL;
}
/* TODO : check */
size_t size = ihex_rs_get_size(rs);
if((buf = (uint8_t*)malloc(size))){
for(n = 0;;){
if((err = ihex_rs_iterate_data(rs, &i, &record, &offset))){
fprintf(stderr, "%s", ihex_error());
goto out;
}
if(!record)
break;
/* Ignore offset */
memcpy(buf + n, record->ihr_data, record->ihr_length);
n += record->ihr_length;
}
/* Copy offset just in case */
*offset_r = offset;
*size_r = size;
}
out:
ihex_rs_free(rs);
return buf;
}
void ihex_free(void *ptr) {
free(ptr);
}