2 ** This file is part of fsusb_picdem
4 ** fsusb_picdem is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU General Public License as
6 ** published by the Free Software Foundation; either version 2 of the
7 ** License, or (at your option) any later version.
9 ** fsusb_picdem is distributed in the hope that it will be useful, but
10 ** WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with fsusb_picdem; if not, write to the Free Software
16 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 * hex_open: Create a hex_file from a FILE *
32 * f is assumed to already be open for reading, with the
33 * pointer at the start of the file.
35 hex_file *hex_open(FILE *f)
43 r=malloc(sizeof(hex_file));
55 * hex_raw_read: Create a hex_record from the next line of f
57 * f is assumed to already be open for reading, with the
58 * pointer at the start of the line to parse.
60 hex_record *hex_raw_read(FILE *f)
68 unsigned char check=0;
76 r=malloc(sizeof(hex_record));
83 getline(&s, &ssize, f);
88 if(strlen(s)<1 || s[0] != ':') {
97 if(strlen(s)<3 || !isxdigit(s[1]) || !isxdigit(s[2])) {
102 sprintf(temps, "0x%c%c", s[1], s[2]);
103 r->datlen=strtol(temps, NULL, 16);
108 if(strlen(s) < r->datlen*2 + 11) {
114 for(i=3;i<r->datlen*2+11;i++) {
115 if(!isxdigit(s[i])) {
121 tempr=realloc(r, sizeof(hex_record) + r->datlen*2);
133 sprintf(temps, "0x%c%c%c%c", s[3], s[4], s[5], s[6]);
134 r->addr=strtol(temps, NULL, 16);
136 sprintf(temps, "0x%c%c", s[3], s[4]);
137 check+=strtol(temps, NULL, 16);
138 sprintf(temps, "0x%c%c", s[5], s[6]);
139 check+=strtol(temps, NULL, 16);
145 sprintf(temps, "0x%c%c", s[7], s[8]);
146 r->type=strtol(temps, NULL, 16);
153 for(i=0;i<r->datlen;i++) {
154 sprintf(temps, "0x%c%c", s[9+2*i], s[10+2*i]);
155 r->data[i]=strtol(temps, NULL, 16);
162 sprintf(temps, "0x%c%c", s[r->datlen*2+9], s[r->datlen*2+10]);
163 r->checksum=strtol(temps, NULL, 16);
165 // printf("check is %x, 2c of check is %x\n", check, (unsigned char)(-((int)check)));
166 // printf("checksum wanted is %x\n", r->checksum);
170 if((unsigned char)(-((int)check)) != r->checksum) {
171 printf("hex_raw_read(): BAD CHECKSUM: got %x, wanted %x\n",
172 (unsigned char)(-((int)check)), r->checksum);
183 * hex_read: Return the next hex_record from f
185 hex_record *hex_read(hex_file *f)
197 r=hex_raw_read(f->f);
212 * Do nothing, although something could be done on these
214 * It'll only get more data past this on a funny file,
215 * and the assumption is that user-supplied files are usually ok
216 * (which may not be a good assumption)
221 case 2: // hex86 address
222 f->addr = (r->data[0] << 12) + (r->data[1] << 4); // endianness?
226 case 4: // hex386 address
227 f->addr = (r->data[0] << 24) + (r->data[1] << 16); // endianness?