public RESULT_STATUS BinFile2HexFile(string src, string dest) { HexFormat gHexFor; gHexFor.addr = new byte[4]; gHexFor.data = new byte[MAX_DATA_LEN_OF_PER_LINE_IN_HEX]; //指向需要转换的bin数据流 FileStream fsReader, fsWrite; UInt16 tmp; UInt32 low_addr = 0x20, hign_addr = 0; byte[] buffer_bin = new byte[MAX_DATA_LEN_OF_PER_LINE_IN_HEX]; byte[] buffer_hex = new byte[MAX_MESSAGE_LEN_OF_PER_LINE_IN_HEX]; UInt32 src_file_length; UInt32 src_file_quotient, cur_file_page = 0; byte src_file_remainder; byte[] sha1_check = new byte[20]; int i = 0; fsReader = new FileStream(src, FileMode.Open, FileAccess.Read); //源文件为bin文件,以二进制的形式打开 if (fsReader == null) //这里也是相当于用来检查用户的输入是否准备 { return(RESULT_STATUS.RES_BIN_FILE_NOT_EXIST); } fsWrite = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.Write); //目的文件为hex文件,以文本的形式打开 if (fsWrite == null) { return(RESULT_STATUS.RES_HEX_FILE_PATH_ERROR); } //fseek(src_file, 0, SEEK_END); //定位到文件末 //src_file_length = ftell(src_file); //get total bytes of binary file src_file_length = (UInt32)fsReader.Length; //fseek(src_file, 0, SEEK_SET); //重新定位到开头,准备开始读取数据 fsReader.Seek(0, SeekOrigin.Begin); fsWrite.Seek(0, SeekOrigin.Begin); src_file_quotient = (UInt32)(src_file_length / MAX_DATA_LEN_OF_PER_LINE_IN_HEX); //商,需要读取多少次 src_file_remainder = (byte)(src_file_length % MAX_DATA_LEN_OF_PER_LINE_IN_HEX); //余数,最后一次需要多少个字符 Sha1_cal sha1_cal = new Sha1_cal(); sha1_cal.SHA1Reset(); while (cur_file_page < src_file_quotient) { fsReader.Read(gHexFor.data, 0, MAX_DATA_LEN_OF_PER_LINE_IN_HEX); //fread(buffer_bin, 1, MAX_DATA_LEN_OF_PER_LINE_IN_HEX, src_file); //SHA1Input(&file_sha, buffer_bin, MAX_DATA_LEN_OF_PER_LINE_IN_HEX); sha1_cal.SHA1Input(gHexFor.data, MAX_DATA_LEN_OF_PER_LINE_IN_HEX); if (((low_addr & 0xffff0000) != hign_addr) && ((low_addr & 0xffff0000) > 0)) //只有大于64K以后才写入扩展线性地址,第一次一般是没有 { hign_addr = low_addr & 0xffff0000; //gHexFor.addr[0] = (uint8_t)((hign_addr &0xff000000) >> 24); //gHexFor.addr[1] = (uint8_t)((hign_addr &0xff0000) >> 16); gHexFor.addr[0] = (byte)((low_addr & 0xff00) >> 8); gHexFor.addr[1] = (byte)(low_addr & 0xff); gHexFor.addr[2] = (byte)((hign_addr & 0xff000000) >> 24); gHexFor.addr[3] = (byte)((hign_addr & 0x00ff0000) >> 16); gHexFor.type = 4; gHexFor.len = 2; //记录扩展地址 tmp = BinFormatEncode(ref buffer_hex, gHexFor); //fwrite(buffer_hex, 1, tmp, dest_file); fsWrite.Write(buffer_hex, 0, tmp); //fprintf(dest_file,"\n"); //end-of-line character buffer_hex = System.Text.Encoding.Default.GetBytes("\n"); fsWrite.Write(buffer_hex, 0, 1); } gHexFor.addr[0] = (byte)((low_addr & 0xff00) >> 8); gHexFor.addr[1] = (byte)(low_addr & 0x00ff); gHexFor.type = 0; //数据记录 gHexFor.len = MAX_DATA_LEN_OF_PER_LINE_IN_HEX; tmp = BinFormatEncode(ref buffer_hex, gHexFor); //fwrite(buffer_hex, 1, tmp, dest_file); fsWrite.Write(buffer_hex, 0, tmp); //fprintf(dest_file,"\n"); buffer_hex = System.Text.Encoding.Default.GetBytes("\n"); fsWrite.Write(buffer_hex, 0, 1); cur_file_page++; low_addr += NUMBER_OF_ONE_LINE; } if (src_file_remainder != 0) //最后一次读取的个数不为0,这继续读取 { //fread(buffer_bin,1, src_file_remainder, src_file); //SHA1Input(&file_sha, buffer_bin, src_file_remainder); fsReader.Read(gHexFor.data, 0, src_file_remainder); sha1_cal.SHA1Input(gHexFor.data, src_file_remainder); gHexFor.addr[0] = (byte)((low_addr & 0xff00) >> 8); gHexFor.addr[1] = (byte)(low_addr & 0x00ff); gHexFor.len = src_file_remainder; gHexFor.type = 0; //数据记录 tmp = BinFormatEncode(ref buffer_hex, gHexFor); fsWrite.Write(buffer_hex, 0, tmp); buffer_hex = System.Text.Encoding.Default.GetBytes("\n"); fsWrite.Write(buffer_hex, 0, 1); low_addr += src_file_remainder; } if (((low_addr & 0xffff0000) != hign_addr) && ((low_addr & 0xffff0000) > 0)) //只有大于64K以后才写入扩展线性地址,第一次一般是没有 { hign_addr = low_addr & 0xffff0000; gHexFor.addr[0] = (byte)((low_addr & 0xff00) >> 8); gHexFor.addr[1] = (byte)(low_addr & 0xff); gHexFor.addr[2] = (byte)((hign_addr & 0xff000000) >> 24); gHexFor.addr[3] = (byte)((hign_addr & 0x00ff0000) >> 16); gHexFor.type = 4; gHexFor.len = 2; //记录扩展地址 tmp = BinFormatEncode(ref buffer_hex, gHexFor); fsWrite.Write(buffer_hex, 0, tmp); //fprintf(dest_file, "\n"); //end-of-line character buffer_hex = System.Text.Encoding.Default.GetBytes("\n"); fsWrite.Write(buffer_hex, 0, 1); } gHexFor.addr[0] = (byte)((low_addr & 0xff00) >> 8); gHexFor.addr[1] = (byte)(low_addr & 0x00ff); gHexFor.type = 0; //数据记录 //gHexFor.len = SHA1HashSize; gHexFor.len = 20; //memcpy(buffer_bin, sha1_check, SHA1HashSize); for (i = 0; i < 20; i++) { } tmp = BinFormatEncode(ref buffer_hex, gHexFor); //fwrite(buffer_hex, 1, tmp, dest_file); fsWrite.Write(buffer_hex, 0, tmp); //fprintf(dest_file, "\n"); buffer_hex = System.Text.Encoding.Default.GetBytes("\n"); fsWrite.Write(buffer_hex, 0, 1); gHexFor.addr[0] = 0; gHexFor.addr[1] = 0; gHexFor.type = 1; //结束符 gHexFor.len = 0; tmp = BinFormatEncode(ref buffer_hex, gHexFor); //fwrite(buffer_hex,1, tmp, dest_file); fsWrite.Write(buffer_hex, 0, tmp); //fprintf(dest_file,"\n"); buffer_hex = System.Text.Encoding.Default.GetBytes("\n"); fsWrite.Write(buffer_hex, 0, 1); //fclose(src_file); //fclose(dest_file); fsWrite.Close(); fsWrite.Dispose(); fsReader.Close(); fsReader.Dispose(); return(RESULT_STATUS.RES_OK); }
private void convert_button_Click(object sender, EventArgs e) { if (chip_selection_comboBox.SelectedItem.ToString().Length == 0) { MessageBox.Show("请选择芯片类型", "芯片类型未选择!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (Hex_StartAddr_textBox.TextLength == 0) { MessageBox.Show("未给定Hex文件起始地址", "请设置Hex文件起始地址!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else if ((UInt64.Parse(Hex_StartAddr_textBox.Text, System.Globalization.NumberStyles.HexNumber) > 0xffffffff) || (UInt64.Parse(Hex_StartAddr_textBox.Text, System.Globalization.NumberStyles.HexNumber) < 0x0A0000)) { MessageBox.Show("hex地址越界", "请设置Hex起始地址范围0x0A0000~0xFFFFFFFF!", MessageBoxButtons.OK, MessageBoxIcon.Warning); Hex_StartAddr_textBox.Text = "0"; return; } if (targetfile_full_name_textBox.TextLength == 0) { MessageBox.Show("请设定转换程序完整名称", "转换后的程序名不能为空!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else if (targetfile_full_name_textBox.TextLength >= HEADER_FILE_FULL_NAME_SIZE) { MessageBox.Show("设定转换程序名称过长", "转换后的程序名长度不能超过86字节!", MessageBoxButtons.OK, MessageBoxIcon.Warning); targetfile_full_name_textBox.Text = ""; return; } else { if (targetfile_full_name_textBox.Text.Substring(targetfile_full_name_textBox.Text.Length - 1, 1) == "/") { MessageBox.Show("是文件全名不是文件路径", "文件名格式错误!", MessageBoxButtons.OK, MessageBoxIcon.Warning); targetfile_full_name_textBox.Text = ""; return; } } if (orgfile_select_comboBox.Text.Length == 0) { MessageBox.Show("请选择要转换的文件", "文件名不能为空!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (!File.Exists(orgfile_select_comboBox.Text)) { MessageBox.Show("文件不存在", "请选择要转换的文件!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } FileStream fs = new FileStream(orgfile_select_comboBox.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); file_size_textBox.Text = fs.Length.ToString(); crc_cal crc_check = new crc_cal(); Sha1_cal sha1_check = new Sha1_cal(); byte[] org_file_content = new byte[fs.Length + CUSTOMIZED_HEADER_SIZE + sha1_check.SHA1HashSize]; org_file_content[HEADER_CHIP_TYPE_START] = (byte)chip_selection_comboBox.SelectedIndex; org_file_content[HEADER_FILE_TYPE_START] = (byte)((UInt16.Parse(file_type_comboBox.SelectedItem.ToString()) & 0xff00) >> 8); org_file_content[HEADER_FILE_TYPE_START + 1] = (byte)(UInt16.Parse(file_type_comboBox.SelectedItem.ToString()) & 0xff); org_file_content[HEADER_FILE_BYTES_START] = (byte)((fs.Length & 0xFF000000) >> 24); org_file_content[HEADER_FILE_BYTES_START + 1] = (byte)((fs.Length & 0xFF0000) >> 16); org_file_content[HEADER_FILE_BYTES_START + 2] = (byte)((fs.Length & 0xFF00) >> 8); org_file_content[HEADER_FILE_BYTES_START + 3] = (byte)(fs.Length & 0xFF); UInt32 i = 0; /* fill header file full name with char ‘\0' */ for (i = 0; i < HEADER_FILE_FULL_NAME_SIZE; i++) { org_file_content[HEADER_FILE_FULL_NAME_START + i] = (byte)('\0'); } byte[] targetfile_full_name_array = System.Text.Encoding.Default.GetBytes(targetfile_full_name_textBox.Text); /* copy target file full name to specified address */ for (i = 0; i < targetfile_full_name_array.Length; i++) { org_file_content[HEADER_FILE_FULL_NAME_START + i] = targetfile_full_name_array[i]; } UInt16 crc16_ret = 0; /* calculate CRC16 result for customized header */ crc16_ret = crc_check.crc16(0, org_file_content, CUSTOMIZED_HEADER_SIZE - 2); org_file_content[HEADER_CRC16_START] = (byte)((crc16_ret & 0xff00) >> 8); // high byte of crc16 org_file_content[HEADER_CRC16_START + 1] = (byte)(crc16_ret & 0x00ff); // low byte of crc16 byte[] org_file_array = new byte[fs.Length]; fs.Seek(0, SeekOrigin.Begin); fs.Read(org_file_array, 0, (int)fs.Length); // read all bytes of original file to be converted /* copy file data */ for (i = 0; i < org_file_array.Length; i++) { org_file_content[FILE_CONTENT_START + i] = org_file_array[i]; } /* calculate sha1 */ //sha1_check.SHA1Reset(); //sha1_check.SHA1Input(org_file_content, (uint)(fs.Length + CUSTOMIZED_HEADER_SIZE)); //byte[] sha1_check_result = new byte[sha1_check.SHA1HashSize]; //sha1_check.SHA1Result(ref sha1_check_result); SHA1 sha1 = new SHA1CryptoServiceProvider(); //创建SHA1对象 byte[] bytes_out = sha1.ComputeHash(org_file_content, 0, (int)(fs.Length + CUSTOMIZED_HEADER_SIZE)); //Hash运算 sha1.Dispose(); //释放当前实例使用的所有资源 String result = BitConverter.ToString(bytes_out); //将运算结果转为string类型 /* copy 20 bytes of sha1 result to the file end */ for (i = 0; i < sha1_check.SHA1HashSize; i++) { org_file_content[FILE_CONTENT_START + org_file_array.Length + i] = bytes_out[i]; } string[] strArray = orgfile_select_comboBox.Text.Split('\\'); //'\\'为'\'的转义字符 /* convert bytes to S19 or hex file according to specified format */ string hex_file_name = System.IO.Directory.GetCurrentDirectory(); if ((comboBox_target_file_opt.SelectedItem.ToString() == "s19") || (comboBox_target_file_opt.SelectedItem.ToString() == "S19")) { BinToS19 bin_2_s19 = new BinToS19(); bin_2_s19.BinFile2S19File(UInt32.Parse(Hex_StartAddr_textBox.Text, System.Globalization.NumberStyles.HexNumber), org_file_content, strArray[strArray.Length - 1] + ".s19"); } else if ((comboBox_target_file_opt.SelectedItem.ToString() == "hex") || (comboBox_target_file_opt.SelectedItem.ToString() == "Hex")) { BinToHex bin_2_hex = new BinToHex(); bin_2_hex.BinFile2HexFile(UInt32.Parse(Hex_StartAddr_textBox.Text, System.Globalization.NumberStyles.HexNumber), org_file_content, strArray[strArray.Length - 1] + ".hex"); } fs.Close(); fs.Dispose(); StringBuilder strB = new StringBuilder(); for (i = 0; i < 96; i++) { strB.Append(org_file_content[i].ToString("X2")); } disp_richTextBox.Text = result; // strB.ToString(); }