|
| 1 | +#include "HexToBin.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +QByteArray text; |
| 6 | + |
| 7 | +/******************************************************************************** |
| 8 | +input: |
| 9 | + c:单个字符('0'~'9' 'a'~'f', 'A'~'F') |
| 10 | +output: |
| 11 | + 单个字符转化为单个字符 |
| 12 | +********************************************************************************/ |
| 13 | +static uint8_t HexCharToBinBinChar(char c) |
| 14 | +{ |
| 15 | + if (c >= '0' && c <= '9') |
| 16 | + return c - '0'; |
| 17 | + else if (c >= 'a' && c <= 'z') |
| 18 | + return c - 'a' + 10; |
| 19 | + else if (c >= 'A' && c <= 'Z') |
| 20 | + return c - 'A' + 10; |
| 21 | + return 0xff; |
| 22 | +} |
| 23 | + |
| 24 | +/******************************************************************************** |
| 25 | +input: |
| 26 | + p: 两个文本字符 |
| 27 | +output: |
| 28 | + 转化为1个字节 |
| 29 | +********************************************************************************/ |
| 30 | +static uint8_t Hex2Bin(const char *p) |
| 31 | +{ |
| 32 | + uint8_t tmp = 0; |
| 33 | + tmp = HexCharToBinBinChar(p[0]); |
| 34 | + tmp <<= 4; |
| 35 | + tmp |= HexCharToBinBinChar(p[1]); |
| 36 | + return tmp; |
| 37 | +} |
| 38 | + |
| 39 | +/******************************************************************************** |
| 40 | +input: |
| 41 | + src: hex单行字符串 |
| 42 | + p->type: 如果函数返回结果正确,这里就存着转化后的类型 |
| 43 | + p->len: 如果函数运行正确,这里就存着转化后的bin数据长度 |
| 44 | + p->data: 如果函数运行正确,长度并且不为0,该指针就只想转化后的数据 |
| 45 | + p->addr[0]: 如果函数返回结果正确,这里就存着地址的低字节 |
| 46 | + p->addr[1]: 如果函数返回结果正确,这里就存着地址的低字节 |
| 47 | +output: |
| 48 | + 返回hex格式流分析的结果 |
| 49 | +********************************************************************************/ |
| 50 | +static RESULT_STATUS HexFormatUncode(const char *src, BinFarmat *p) |
| 51 | +{ |
| 52 | + uint8_t check = 0, tmp[4], binLen; |
| 53 | + uint16_t hexLen = strlen(src); |
| 54 | + uint16_t num = 0, offset = 0; |
| 55 | + if (hexLen > HEX_MAX_LENGTH) //数据内容过长 |
| 56 | + return RES_DATA_TOO_LONG; |
| 57 | + if (hexLen < HEX_MIN_LEN) |
| 58 | + return RES_DATA_TOO_SHORT; //数据内容过短 |
| 59 | + if (src[0] != ':') |
| 60 | + return RES_NO_COLON; //没有冒号 |
| 61 | + if ((hexLen - 1) % 2 != 0) |
| 62 | + return RES_LENGTH_ERROR; //hexLen的长度应该为奇数 |
| 63 | + binLen = (hexLen - 1) / 2; //bin总数据的长度,包括长度,地址,类型校验等内容 |
| 64 | + while (num < 4) |
| 65 | + { |
| 66 | + offset = (num << 1) + 1; |
| 67 | + tmp[num] = Hex2Bin(src + offset); |
| 68 | + check += tmp[num]; |
| 69 | + num++; |
| 70 | + } |
| 71 | + p->len = tmp[0]; //把解析的这些数据保存到结构体中 |
| 72 | + p->addr = tmp[1]; |
| 73 | + p->addr <<= 8; |
| 74 | + p->addr += tmp[2]; |
| 75 | + p->type = tmp[3]; |
| 76 | + while (num < binLen) |
| 77 | + { |
| 78 | + offset = (num << 1) + 1; //保存真正的bin格式数据流 |
| 79 | + p->data[num - 4] = Hex2Bin(src + offset); |
| 80 | + check += p->data[num - 4]; |
| 81 | + num++; |
| 82 | + } |
| 83 | + if (p->len != binLen - 5) //检查hex格式流的长度和数据的长度是否一致 |
| 84 | + return RES_LENGTH_ERROR; |
| 85 | + if (check != 0) //检查校验是否通过 |
| 86 | + return RES_CHECK_ERROR; |
| 87 | + return RES_OK; |
| 88 | +} |
| 89 | + |
| 90 | +int insertsomething(QByteArray* desQByte,uint8_t* addr, int num ) |
| 91 | +{ |
| 92 | + QByteArray str="a"; |
| 93 | + |
| 94 | + for(int i=0;i<num;i++) |
| 95 | + { |
| 96 | + str[0] = *(addr+i); |
| 97 | + desQByte->append(str); |
| 98 | + } |
| 99 | + return num; |
| 100 | +} |
| 101 | + |
| 102 | +RESULT_STATUS HexFile2BinFile(char *src, char *dest, uint32_t *addr) |
| 103 | +{ |
| 104 | + FILE *src_file;// *dest_file; |
| 105 | + uint16_t addr_low = 0; |
| 106 | + uint32_t addr_hign = 0; |
| 107 | + char buffer_hex[600]; |
| 108 | + uint8_t buffer_bin[255]; |
| 109 | + BinFarmat gBinFor; |
| 110 | + RESULT_STATUS res; |
| 111 | + |
| 112 | + //QByteArray tset; |
| 113 | + |
| 114 | + gBinFor.data = buffer_bin; |
| 115 | + src_file = fopen(src, "r"); //以文本的形式打开一个hex文件 |
| 116 | + if (!src_file) |
| 117 | + return RES_HEX_FILE_NOT_EXIST; |
| 118 | + //dest_file = fopen(dest, "wb"); //以二进制写的方式打开文件,文件不存在也没影响 |
| 119 | + //if (!dest_file) |
| 120 | + //return RES_BIN_FILE_PATH_ERROR; |
| 121 | + fseek(src_file, 0, SEEK_SET); //定位到开头,准备开始读取数据 |
| 122 | + char beginflag = 1; |
| 123 | + char secondline = 1; |
| 124 | + while (!feof(src_file)) |
| 125 | + { |
| 126 | + fscanf(src_file, "%s\r\n", buffer_hex); |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + //get the begin addr |
| 132 | + unsigned int Low,High,addr_temp = 0; |
| 133 | + |
| 134 | + if(buffer_hex[2] == '2') |
| 135 | + { |
| 136 | + |
| 137 | + for(char i=0;i<4;i++) |
| 138 | + { |
| 139 | + if(buffer_hex[6-i]>='0' && buffer_hex[6-i]<='9') |
| 140 | + { |
| 141 | + Low = buffer_hex[6-i]-'0'; |
| 142 | + } |
| 143 | + else if(buffer_hex[6-i]>='a' && buffer_hex[6-i]<='f') |
| 144 | + { |
| 145 | + Low = buffer_hex[6-i]-'a'+10; |
| 146 | + } |
| 147 | + else if(buffer_hex[6-i]>='A' && buffer_hex[6-i]<='F') |
| 148 | + { |
| 149 | + Low = buffer_hex[6-i]-'A'+10; |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + Low = 0xFF; |
| 154 | + } |
| 155 | + |
| 156 | + Low = Low<<(4*i); |
| 157 | + |
| 158 | + if(buffer_hex[12-i]>='0' && buffer_hex[12-i]<='9') |
| 159 | + { |
| 160 | + High = buffer_hex[12-i]-'0'; |
| 161 | + } |
| 162 | + else if(buffer_hex[12-i]='a' && buffer_hex[12-i]<='f') |
| 163 | + { |
| 164 | + High = buffer_hex[12-i]-'a'+10; |
| 165 | + } |
| 166 | + else if(buffer_hex[12-i]>='A' && buffer_hex[12-i]<='F') |
| 167 | + { |
| 168 | + High = buffer_hex[12-i]-'A'+10; |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + High = 0xFF; |
| 173 | + } |
| 174 | + |
| 175 | + High = High<<(4*i+16); |
| 176 | + |
| 177 | + addr_temp = addr_temp + Low + High; |
| 178 | + } |
| 179 | + //addr_temp= 0x08000000; |
| 180 | + if(beginflag) |
| 181 | + { |
| 182 | + *addr = addr_temp;//only frist time! |
| 183 | + beginflag = 0; |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + //get addr offset |
| 188 | + if(buffer_hex[1] == '1') |
| 189 | + { |
| 190 | + Low =0; |
| 191 | + for(char i=0;i<4;i++) |
| 192 | + { |
| 193 | + if(buffer_hex[6-i]>='0' && buffer_hex[6-i]<='9') |
| 194 | + { |
| 195 | + Low = buffer_hex[6-i]-'0'; |
| 196 | + } |
| 197 | + else if(buffer_hex[6-i]>='a' && buffer_hex[6-i]<='f') |
| 198 | + { |
| 199 | + Low = buffer_hex[6-i]-'a'+10; |
| 200 | + } |
| 201 | + else if(buffer_hex[6-i]>='A' && buffer_hex[6-i]<='F') |
| 202 | + { |
| 203 | + Low = buffer_hex[6-i]-'A'+10; |
| 204 | + } |
| 205 | + else |
| 206 | + { |
| 207 | + Low = 0xFF; |
| 208 | + } |
| 209 | + |
| 210 | + Low = Low<<(4*i); |
| 211 | + } |
| 212 | + //addr_temp= 0x08000000; |
| 213 | + if(secondline) |
| 214 | + { |
| 215 | + *addr = *addr+Low;//only frist time! |
| 216 | + secondline = 0; |
| 217 | + } |
| 218 | + |
| 219 | + } |
| 220 | + |
| 221 | + |
| 222 | + // |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + res = HexFormatUncode(buffer_hex, &gBinFor); |
| 228 | + if (res != RES_OK) |
| 229 | + { |
| 230 | + fclose(src_file); |
| 231 | + //fclose(dest_file); |
| 232 | + return res; |
| 233 | + } |
| 234 | + switch (gBinFor.type) |
| 235 | + { |
| 236 | + case 0: //数据记录 |
| 237 | + addr_low = gBinFor.addr; |
| 238 | + //数据指针偏移 |
| 239 | + // fseek(dest_file, addr_low + addr_hign, SEEK_SET); |
| 240 | + |
| 241 | + |
| 242 | + insertsomething(&text,(uint8_t*)gBinFor.data, gBinFor.len); |
| 243 | + |
| 244 | + /*if (fwrite((const uint8_t*)gBinFor.data, gBinFor.len, 1, dest_file) != 1) |
| 245 | + { |
| 246 | + fclose(src_file); |
| 247 | + fclose(dest_file); |
| 248 | + return RES_WRITE_ERROR; |
| 249 | + }*/ |
| 250 | + break; |
| 251 | + case 1: //数据结束 |
| 252 | + fclose(src_file); |
| 253 | + //fclose(dest_file); |
| 254 | + return RES_OK; |
| 255 | + case 2: |
| 256 | + addr_hign = ((uint32_t)gBinFor.addr) << 2; |
| 257 | + break; |
| 258 | + case 4: //线性段地址 |
| 259 | + addr_hign = ((uint32_t)gBinFor.addr) << 16; |
| 260 | + break; |
| 261 | + default: |
| 262 | + fclose(src_file); |
| 263 | + //fclose(dest_file); |
| 264 | + return RES_TYPE_ERROR; |
| 265 | + } |
| 266 | + } |
| 267 | + fclose(src_file); |
| 268 | + //fclose(dest_file); |
| 269 | + return RES_HEX_FILE_NO_END; |
| 270 | +} |
| 271 | + |
0 commit comments