private static OffsetSeekResult SeekForOffset(IntelHexFile hexFile) { var records = hexFile._records.ToArray(); var offsetStart = 0; for (var index = 0; index < records.Length; index++) { var record = records[index]; if (RecordIsOffset(record)) { continue; } if (index == offsetStart) { offsetStart++; continue; } return(new OffsetSeekResult(offsetStart, index)); } return(new OffsetSeekResult(offsetStart, offsetStart)); }
/// <summary> /// Creates a new IntelHexFile out of an Intel Hexfile string /// </summary> /// <param name="intelHexString">The string representation of an intel hexfile</param> /// <returns>An new IntelHexFile representation</returns> public static IntelHexFile CreateFrom(string intelHexString, int baseAddress = 0x00000000) { var hexFile = new IntelHexFile(baseAddress); var reader = new StringReader(intelHexString); string line; while ((line = reader.ReadLine()) != null) { hexFile._records.Add(RecordParser.ParseRecord(line)); } //Calculate addresses var currentOffsetAddress = 0x00000000; foreach (var record in hexFile._records) { if (record.GetOffsetAddress() > currentOffsetAddress) { currentOffsetAddress = record.GetOffsetAddress(); continue; } record.Address += currentOffsetAddress; } return(hexFile); }
/// <summary> /// Create a new IntelHexFile out of the binary data /// </summary> /// <param name="binaryData">The binary source code representation</param> /// <returns>An new IntelHexFile representation</returns> public static IntelHexFile CreateFrom(byte[] binaryData, int baseAddress = 0x00000000) { var hexFile = new IntelHexFile(baseAddress); hexFile._records.AddRange(CreateDataRecords(binaryData)); hexFile._records.ForEach(record => record.AddAddress(baseAddress)); var seekResult = SeekForOffset(hexFile); while (seekResult.Match) { var fromRecord = hexFile._records[seekResult.FromIndex]; var toRecord = hexFile._records[seekResult.ToIndex]; hexFile._records.RemoveRange(seekResult.FromIndex, seekResult.ToIndex - seekResult.FromIndex); var addressDelta = toRecord.Address; if (addressDelta > short.MaxValue) { var highOffsetPart = (short)((toRecord.Address >> 16) & 0xFFFF); hexFile._records.Insert(seekResult.FromIndex, new OffsetRecord(highOffsetPart, 0)); } seekResult = SeekForOffset(hexFile); } foreach (var dataRecord in hexFile._records.OfType <DataRecord>()) { dataRecord.TrimEnd(); } var insertCount = 0; for (var index = 0; index + insertCount + 1 < hexFile._records.Count; index++) { IntelHexRecord previousRecord = null; if (index != 0) { previousRecord = hexFile._records[index + insertCount - 1]; } var nextRecord = hexFile._records[index + insertCount]; if (nextRecord is OffsetRecord || previousRecord is OffsetRecord) { continue; } var previousOffsetAddressPart = (short)((previousRecord?.Address ?? 0) >> 16); var nextOffsetAddressPart = (short)(nextRecord.Address >> 16); if (previousOffsetAddressPart == nextOffsetAddressPart) { continue; } hexFile._records.Insert(index + insertCount, new OffsetRecord(nextOffsetAddressPart, 0)); insertCount++; } return(hexFile); }