ihex.c (5056B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2013 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #include <stddef.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <stdio.h> 26 27 #include "ihex.h" 28 #include "context-private.h" 29 #include "checksum.h" 30 #include "array.h" 31 32 struct dc_ihex_file_t { 33 dc_context_t *context; 34 FILE *fp; 35 }; 36 37 dc_status_t 38 dc_ihex_file_open (dc_ihex_file_t **result, dc_context_t *context, const char *filename) 39 { 40 dc_ihex_file_t *file = NULL; 41 42 if (result == NULL || filename == NULL) { 43 ERROR (context, "Invalid arguments."); 44 return DC_STATUS_INVALIDARGS; 45 } 46 47 file = (dc_ihex_file_t *) malloc (sizeof (dc_ihex_file_t)); 48 if (file == NULL) { 49 ERROR (context, "Failed to allocate memory."); 50 return DC_STATUS_NOMEMORY; 51 } 52 53 file->context = context; 54 55 file->fp = fopen (filename, "rb"); 56 if (file->fp == NULL) { 57 ERROR (context, "Failed to open the file."); 58 free (file); 59 return DC_STATUS_IO; 60 } 61 62 *result = file; 63 64 return DC_STATUS_SUCCESS; 65 } 66 67 dc_status_t 68 dc_ihex_file_read (dc_ihex_file_t *file, dc_ihex_entry_t *entry) 69 { 70 unsigned char ascii[9 + 2 * 255 + 2] = {0}; 71 unsigned char data[4 + 255 + 1] = {0}; 72 unsigned int type, length, address; 73 unsigned char csum_a, csum_b; 74 size_t n; 75 76 if (file == NULL || entry == NULL) { 77 ERROR (file ? file->context : NULL, "Invalid arguments."); 78 return DC_STATUS_INVALIDARGS; 79 } 80 81 /* Read the start code. */ 82 while (1) { 83 n = fread (ascii, 1, 1, file->fp); 84 if (n != 1) { 85 if (feof (file->fp)) { 86 return DC_STATUS_DONE; 87 } else { 88 ERROR (file->context, "Failed to read the start code."); 89 return DC_STATUS_IO; 90 } 91 } 92 93 if (ascii[0] == ':') 94 break; 95 96 /* Ignore CR and LF characters. */ 97 if (ascii[0] != '\n' && ascii[0] != '\r') { 98 ERROR (file->context, "Unexpected character (0x%02x).", ascii[0]); 99 return DC_STATUS_DATAFORMAT; 100 } 101 } 102 103 /* Read the record length, address and type. */ 104 n = fread (ascii + 1, 1, 8, file->fp); 105 if (n != 8) { 106 ERROR (file->context, "Failed to read the header."); 107 return DC_STATUS_IO; 108 } 109 110 /* Convert to binary representation. */ 111 if (array_convert_hex2bin (ascii + 1, 8, data, 4) != 0) { 112 ERROR (file->context, "Invalid hexadecimal character."); 113 return DC_STATUS_DATAFORMAT; 114 } 115 116 /* Get the record length. */ 117 length = data[0]; 118 119 /* Read the record payload. */ 120 n = fread (ascii + 9, 1, 2 * length + 2, file->fp); 121 if (n != 2 * length + 2) { 122 ERROR (file->context, "Failed to read the data."); 123 return DC_STATUS_IO; 124 } 125 126 /* Convert to binary representation. */ 127 if (array_convert_hex2bin (ascii + 9, 2 * length + 2, data + 4, length + 1) != 0) { 128 ERROR (file->context, "Invalid hexadecimal character."); 129 return DC_STATUS_DATAFORMAT; 130 } 131 132 /* Verify the checksum. */ 133 csum_a = data[4 + length]; 134 csum_b = ~checksum_add_uint8 (data, 4 + length, 0x00) + 1; 135 if (csum_a != csum_b) { 136 ERROR (file->context, "Unexpected checksum (0x%02x, 0x%02x).", csum_a, csum_b); 137 return DC_STATUS_DATAFORMAT; 138 } 139 140 /* Get the record address. */ 141 address = array_uint16_be (data + 1); 142 143 /* Get the record type. */ 144 type = data[3]; 145 if (type < 0 || type > 5) { 146 ERROR (file->context, "Invalid record type (0x%02x).", type); 147 return DC_STATUS_DATAFORMAT; 148 } 149 150 /* Verify the length and address. */ 151 if (type != 0) { 152 unsigned int len = 0; 153 switch (type) { 154 case 1: /* End of file record. */ 155 len = 0; 156 break; 157 case 2: /* Extended segment address record. */ 158 case 4: /* Extended linear address record. */ 159 len = 2; 160 break; 161 case 3: /* Start segment address record. */ 162 case 5: /* Start linear address record. */ 163 len = 4; 164 break; 165 } 166 if (length != len || address != 0) { 167 ERROR (file->context, "Invalid record length or address."); 168 return DC_STATUS_DATAFORMAT; 169 } 170 } 171 172 /* Set the record fields. */ 173 entry->type = type; 174 entry->address = address; 175 entry->length = length; 176 177 /* Copy the record data. */ 178 memcpy (entry->data, data + 4, entry->length); 179 memset (entry->data + entry->length, 0, sizeof (entry->data) - entry->length); 180 181 return DC_STATUS_SUCCESS; 182 } 183 184 dc_status_t 185 dc_ihex_file_reset (dc_ihex_file_t *file) 186 { 187 if (file == NULL) { 188 ERROR (NULL, "Invalid arguments."); 189 return DC_STATUS_INVALIDARGS; 190 } 191 192 rewind (file->fp); 193 194 return DC_STATUS_SUCCESS; 195 } 196 197 dc_status_t 198 dc_ihex_file_close (dc_ihex_file_t *file) 199 { 200 if (file) { 201 fclose (file->fp); 202 free (file); 203 } 204 205 return DC_STATUS_SUCCESS; 206 }