3 (c) 2003-2004 Jelmer Vernooij <jelmer@samba.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <sys/types.h>
31 int readhexline(FILE *fd, void **data, size_t *len, long *address)
33 int checksum1, checksum2;
34 int length, addr1, addr2, type;
40 return HEX_FILE_END_OF_FILE;
43 if(fscanf(fd, ":%2x%2x%2x%2x", &length, &addr1, &addr2, &type) < 3) {
44 return HEX_FILE_CORRUPT_LINE;
46 } while(type == 1 || type == 2);
53 *address = addr1 * 0x100 + addr2;
55 *data = malloc(length);
58 for(j = 0; j < length; j++) {
59 if(fscanf(fd, "%2x", &byte) < 1) {
60 return HEX_FILE_CORRUPT_LINE;
64 ((char *)(*data))[j] = byte;
67 if(fscanf(fd, "%2x", &checksum2) < 1) {
68 return HEX_FILE_CORRUPT_LINE;
72 checksum1 = (~checksum1 + 1) & 0xFF;
74 if(checksum1 != checksum2) {
75 return HEX_FILE_ERR_CHECKSUM;
78 /* Find the next newline so we're in position for the next read */
81 if(byte != '\n' && byte != '\r') {