public bool UnCompressData() { bool Result = true; if (((int)this.DataFormatIdentifier & 0xF0) == 0x10) { List<VBFBlock> NewBlocks = new List<VBFBlock>(); for (int i = 0; i < this.DataBlocks.Count; i++) { byte[] tmp = Library.LZSS.LZSS_ExpandData(this.DataBlocks[i].Data); if (tmp != null && tmp.Length != 0) { VBFBlock t = new VBFBlock(); t.Data = tmp; t.Length = (uint)tmp.Length; NewBlocks.Add(t); } else { Result = false; } } if (Result == true) { for (int i = 0; i < this.DataBlocks.Count; i++) { this.DataBlocks[i].Data = NewBlocks[i].Data; this.DataBlocks[i].Length = NewBlocks[i].Length; } } UpdateEraseString(); HeaderNew = this.SetStrParameter(HeaderNew, "data_format_identifier", "0x00"); this.DataFormatIdentifier = 0x00; this.UpdateCheckSum(); } else { Result = false; } return Result; }
public bool ImportRaw(string ImportFileName, bool isForceImport) { bool Result = true; this.ImportResult = ""; try { int Mark1 = (int)'{'; int Mark2 = (int)'}'; int Current = 0x00; int ReadPointer = 0; int WritePointer = 0; FileStream theFile = new FileStream(ImportFileName, FileMode.Open); byte[] ReadBuffer = new byte[theFile.Length]; //Generate Header Length int Balance = 0; bool FindFirstMark = false; while (FindFirstMark == false || Balance != 0) { Current = theFile.ReadByte(); ReadPointer++; ReadBuffer[WritePointer] = (byte)Current; WritePointer++; if (Current == Mark1) { FindFirstMark = true; Balance++; } else if (Current == Mark2) Balance--; } this.HeaderOri = Encoding.ASCII.GetString(Library.SubByteArray(ReadBuffer, 0, ReadPointer)); this.setHeaderPrarm(this.HeaderOri); while (ReadPointer <= (theFile.Length - 10)) { uint StartAddress = 0; for (int i = 0; i < 4; i++) { StartAddress = StartAddress + (uint)(theFile.ReadByte() << ((3 - i) * 8)); ReadPointer++; } uint Length = 0; for (int i = 0; i < 4; i++) { Length = Length + (uint)(theFile.ReadByte() << ((3 - i) * 8)); ReadPointer++; } if (Length > (theFile.Length - ReadPointer - 2)) { throw new Exception("Next VDF Block Lenth Error, Import Stop"); } else { byte[] VDFData = new byte[Length + 10]; Library.intToByte(StartAddress, 4).CopyTo(VDFData, 0); Library.intToByte(Length, 4).CopyTo(VDFData, 4); //Read VDFData WritePointer = 8; for (ulong i = 0; i < Length; i++) { VDFData[WritePointer] = (byte)theFile.ReadByte(); ReadPointer++; WritePointer++; } //Read VDFCheckSum; for (int i = 0; i < 2; i++) { VDFData[WritePointer] = (byte)theFile.ReadByte(); ReadPointer++; WritePointer++; } VBFBlock theVBFBlock = new VBFBlock(); bool VBFImportResult = theVBFBlock.ImportBlock(VDFData, this.DataFormatIdentifier); if (VBFImportResult) { this.DataBlocks.Add(theVBFBlock); } else { ImportResult = ImportResult + "Block " + this.DataBlocks.Count.ToString() + " Import Error. Checksum Read: " + Library.FormatUintToHex(theVBFBlock.CheckSum, 4) + "; Checsum Calculate: " + Library.FormatUintToHex(theVBFBlock.GenerateCRC(), 4) + "\r\n"; if (isForceImport) { Result = false; this.DataBlocks.Add(theVBFBlock); } else { throw new Exception(); } } } } theFile.Close(); } catch(Exception Error) { ImportResult = ImportResult + Error.Message + "\r\n"; Result = false; } return Result; }
private void btnSaveVBF_Click(object sender, EventArgs e) { try { List<VBFCreationData> VBFData = GetVBFCreationData(cbxCarType.Text); if (VBFData == null || VBFData.Count == 0) throw new Exception("Car Type Not Supportted"); if (sfdSaveVBF.ShowDialog() == DialogResult.OK) { string OutputPath = Path.GetDirectoryName(sfdSaveVBF.FileName); string FileName = Path.GetFileNameWithoutExtension(sfdSaveVBF.FileName); if (!OutputPath.EndsWith("\\")) OutputPath = OutputPath + "\\"; foreach(VBFCreationData t in VBFData) { VBFFile p = new VBFFile(); p.setHeaderPrarm("vbf_version = 2.2;\r\nheader{\r\n\tsw_part_number = \"\";\r\n" + "\tsw_part_type =" + t.sw_part_type + ";\r\n" + "\tnetwork = " + t.network + " ;\r\n" + "\tecu_address = " + "0x" + Convert.ToString(t.ecu_address, 16) + ";\r\n" + "\tframe_format = CAN_STANDARD;}\r\n"); CCFBin.CCFBinaryLength = Convert.ToInt32(t.parameter_erase_length); VBFBlock q = new VBFBlock(t.parameter_start_address, CCFBin.ExportBytes()); p.DataBlocks.Add(q); string OutputFile = OutputPath + FileName + "_" + t.NodeName + ".VBF"; bool Result = p.Export(OutputFile); if (!Result) throw new Exception("VBF Generate Failed"); if (cbx_Setting_ExportSBL.Checked) { try { string sourceSBL = new DirectoryInfo(tbx_Setting_SBLFoler.Text).FullName + "\\" + t.SBL + ".vbf"; string TargetSBL = OutputPath + t.SBL + ".vbf"; File.Copy(sourceSBL, TargetSBL); } catch { } } } string SDSfile = OutputPath + FileName + ".sds"; if (File.Exists(SDSfile)) File.Delete(SDSfile); StreamWriter SDSStream = new StreamWriter(SDSfile); SDSStream.Write(Library.GetSDSString(cbxCarType.Text, FileName, VBFData)); SDSStream.Close(); MessageBox.Show("File Creation Finished", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception Error) { MessageBox.Show(Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }