From 7527464e7f1d61731367c607edebcb16f57d85fd Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 26 Jul 2023 09:39:24 +0300 Subject: [PATCH 01/34] Update ChannelBlock.cs --- ASAM.MDF/Libary/ChannelBlock.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 9a0b230..4056d58 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -146,6 +146,10 @@ public static ChannelBlock Read(Mdf mdf, ulong position) block.MinValue = mdf.ReadDouble(); block.MaxValue = mdf.ReadDouble(); } + else + { + mdf.UpdatePosition(mdf.position + 16); + } block.SampleRate = mdf.ReadDouble(); From ea192c59083e9e2cf372f108bc5a261fb5e527e6 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 26 Jul 2023 10:17:17 +0300 Subject: [PATCH 02/34] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 37362a9..ee8986c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # ASAM-MDF -A .NET 3.5 implementation of the ASAM MDF Standard +A .NET 6.0 implementation of the ASAM MDF Standard From 8a8cc412e16cb7a8d167d1419571ccfb6f12e286 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 29 Nov 2023 14:08:29 +0300 Subject: [PATCH 03/34] Update ChannelBlock.cs --- ASAM.MDF/Libary/ChannelBlock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 4056d58..ec9623b 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -171,7 +171,7 @@ public static ChannelBlock Read(Mdf mdf, ulong position) if (block.ptrTextBlockComment != 0) block.Comment = TextBlock.Read(mdf, block.ptrTextBlockComment); - if (block.ptrLongSignalName != 0) + if (block.ptrLongSignalName != 0 && (ulong)mdf.Data.Length > block.ptrLongSignalName) block.LongSignalName = TextBlock.Read(mdf, block.ptrLongSignalName); if (block.channelConversion == null && block.ptrChannelConversionBlock != 0) From 6ce3685286831d2522dca62e3e7577724089fa49 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 30 Nov 2023 14:58:11 +0300 Subject: [PATCH 04/34] Update to fix for uncorrect position --- ASAM.MDF/Libary/ChannelBlock.cs | 2 +- ASAM.MDF/Libary/TextBlock.cs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index ec9623b..4056d58 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -171,7 +171,7 @@ public static ChannelBlock Read(Mdf mdf, ulong position) if (block.ptrTextBlockComment != 0) block.Comment = TextBlock.Read(mdf, block.ptrTextBlockComment); - if (block.ptrLongSignalName != 0 && (ulong)mdf.Data.Length > block.ptrLongSignalName) + if (block.ptrLongSignalName != 0) block.LongSignalName = TextBlock.Read(mdf, block.ptrLongSignalName); if (block.channelConversion == null && block.ptrChannelConversionBlock != 0) diff --git a/ASAM.MDF/Libary/TextBlock.cs b/ASAM.MDF/Libary/TextBlock.cs index 5baa71a..45afbc2 100644 --- a/ASAM.MDF/Libary/TextBlock.cs +++ b/ASAM.MDF/Libary/TextBlock.cs @@ -2,6 +2,7 @@ { using System; using System.IO; + using System.Linq; using System.Text; public class TextBlock : Block @@ -53,6 +54,9 @@ public override string ToString() internal static TextBlock Read(Mdf mdf, ulong position) { + if ((ulong)mdf.Data.Length < position) + return null; + mdf.UpdatePosition(position); var block = new TextBlock(mdf); From 06f9e121ef83138aa97beae8c56ef1bb6f94c3ae Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 21 Dec 2023 16:27:32 +0300 Subject: [PATCH 05/34] get support encoding 1251 --- ASAM.MDF/Libary/IdentificationBlock.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index b6e832d..dce1333 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -117,6 +117,8 @@ public ushort CodePage set { codePage = value; + if (value == 1251) + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Encoding = value == 0 ? Encoding.ASCII : Encoding.GetEncoding(value); } } From 907a9827a463551acc461a718f8b2c8e3e82b435 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Mon, 25 Dec 2023 12:52:32 +0300 Subject: [PATCH 06/34] Merge branch 'master' of https://github.com/BrightCaster/ASAM-MDF.NET --- ASAM.MDF/Libary/IdentificationBlock.cs | 2 ++ ASAM.MDF/Libary/TextBlock.cs | 4 ++++ README.md | 6 +++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index b6e832d..dce1333 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -117,6 +117,8 @@ public ushort CodePage set { codePage = value; + if (value == 1251) + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Encoding = value == 0 ? Encoding.ASCII : Encoding.GetEncoding(value); } } diff --git a/ASAM.MDF/Libary/TextBlock.cs b/ASAM.MDF/Libary/TextBlock.cs index 5baa71a..45afbc2 100644 --- a/ASAM.MDF/Libary/TextBlock.cs +++ b/ASAM.MDF/Libary/TextBlock.cs @@ -2,6 +2,7 @@ { using System; using System.IO; + using System.Linq; using System.Text; public class TextBlock : Block @@ -53,6 +54,9 @@ public override string ToString() internal static TextBlock Read(Mdf mdf, ulong position) { + if ((ulong)mdf.Data.Length < position) + return null; + mdf.UpdatePosition(position); var block = new TextBlock(mdf); diff --git a/README.md b/README.md index ee8986c..e8678af 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # ASAM-MDF -A .NET 6.0 implementation of the ASAM MDF Standard +A .NET CORE 6 implementation of the ASAM MDF. Some blocks are still not implemented. + +Supported versions: +- 3.20 (read/write); +- 4.00, 4.10 (read); From b766dc7dbf63c0a429a67ba0617af0dc9e0551d8 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Mon, 25 Dec 2023 13:07:19 +0300 Subject: [PATCH 07/34] Update ChannelBlock.cs --- ASAM.MDF/Libary/ChannelBlock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 4056d58..8a6b0a7 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -44,7 +44,7 @@ public ChannelBlock Next { get { - if (next == null && ptrNextChannelBlock != 0) + if (next == null && ptrNextChannelBlock != 0 && ptrNextChannelBlock < (ulong)Mdf.Data.Length) next = Read(Mdf, ptrNextChannelBlock); return next; From 0a0701ed8cc139b90bef4285e7ed796afcc3bcb8 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 21 Feb 2024 11:04:31 +0300 Subject: [PATCH 08/34] add validate address --- ASAM.MDF/Libary/ChannelBlock.cs | 30 +++++++++--------- ASAM.MDF/Libary/ChannelConversionBlock.cs | 6 ++-- ASAM.MDF/Libary/ChannelGroupBlock.cs | 18 +++++------ ASAM.MDF/Libary/DataGroupBlock.cs | 16 +++++----- ASAM.MDF/Libary/HeaderBlock.cs | 10 +++--- ASAM.MDF/Libary/StreamReadHelper.cs | 37 +++++++++++++++++++++++ 6 files changed, 77 insertions(+), 40 deletions(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 8a6b0a7..e2307b9 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -128,11 +128,11 @@ public static ChannelBlock Read(Mdf mdf, ulong position) ReadV4(mdf, block); return block; } - block.ptrNextChannelBlock = mdf.ReadU32(); - block.ptrChannelConversionBlock = mdf.ReadU32(); - block.ptrChannelExtensionBlock = mdf.ReadU32(); - block.ptrChannelDependencyBlock = mdf.ReadU32(); - block.ptrChannelComment = mdf.ReadU32(); + block.ptrNextChannelBlock = mdf.ReadU32().ValidateAddress(mdf); + block.ptrChannelConversionBlock = mdf.ReadU32().ValidateAddress(mdf); + block.ptrChannelExtensionBlock = mdf.ReadU32().ValidateAddress(mdf); + block.ptrChannelDependencyBlock = mdf.ReadU32().ValidateAddress(mdf); + block.ptrChannelComment = mdf.ReadU32().ValidateAddress(mdf); block.TypeV3 = (ChannelTypeV3)mdf.ReadU16(); block.SignalName = mdf.GetString(32); block.SignalDescription = mdf.GetString(128); @@ -154,10 +154,10 @@ public static ChannelBlock Read(Mdf mdf, ulong position) block.SampleRate = mdf.ReadDouble(); if (mdf.IDBlock.Version >= MIN_VERSION_LONG_SIGNAL_NAME) - block.ptrLongSignalName = mdf.ReadU32(); + block.ptrLongSignalName = mdf.ReadU32().ValidateAddress(mdf); if (mdf.IDBlock.Version >= MIN_VERSION_DISPLAY_NAME) - block.ptrDisplayName = mdf.ReadU32(); + block.ptrDisplayName = mdf.ReadU32().ValidateAddress(mdf); if (mdf.IDBlock.Version >= MIN_VERSION_ADDITIONAL_BYTE_OFFSET) block.AdditionalByteOffset = mdf.ReadU16(); @@ -188,14 +188,14 @@ public static ChannelBlock Read(Mdf mdf, ulong position) private static void ReadV4(Mdf mdf, ChannelBlock block) { - block.ptrNextChannelBlock = mdf.ReadU64(); - block.ConponentAddress = mdf.ReadU64(); - block.TextBlockChanelName = mdf.ReadU64(); - block.ptrChannelExtensionBlock = mdf.ReadU64(); - block.ptrChannelConversionBlock = mdf.ReadU64(); - block.ptrDataBlockSignal = mdf.ReadU64(); - block.ptrUnit = mdf.ReadU64(); - block.ptrTextBlockComment = mdf.ReadU64(); + block.ptrNextChannelBlock = mdf.ReadU64().ValidateAddress(mdf); + block.ConponentAddress = mdf.ReadU64().ValidateAddress(mdf); + block.TextBlockChanelName = mdf.ReadU64().ValidateAddress(mdf); + block.ptrChannelExtensionBlock = mdf.ReadU64().ValidateAddress(mdf); + block.ptrChannelConversionBlock = mdf.ReadU64().ValidateAddress(mdf); + block.ptrDataBlockSignal = mdf.ReadU64().ValidateAddress(mdf); + block.ptrUnit = mdf.ReadU64().ValidateAddress(mdf); + block.ptrTextBlockComment = mdf.ReadU64().ValidateAddress(mdf); //block.ptrAttachment = mdf.ReadU64(); //block.ptrDefaultDGBlock = mdf.ReadU64(); //block.ptrDefaultCGBlock = mdf.ReadU64(); diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index a9f438d..ac93ceb 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -61,9 +61,9 @@ internal static ChannelConversionBlock Read(Mdf mdf, ulong position) if (mdf.IDBlock.Version >= 400) { - block.TextBlockName = mdf.ReadU64(); - block.TextBlockUnit = mdf.ReadU64(); - block.ptrFileComment = mdf.ReadU64(); + block.TextBlockName = mdf.ReadU64().ValidateAddress(mdf); + block.TextBlockUnit = mdf.ReadU64().ValidateAddress(mdf); + block.ptrFileComment = mdf.ReadU64().ValidateAddress(mdf); block.InverseConversion = mdf.ReadU64(); var lastPosAddress = mdf.position; diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index 832223b..ee34958 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -72,9 +72,9 @@ internal static ChannelGroupBlock Read(Mdf mdf, ulong position) ReadV4(mdf, block); return block; } - block.ptrNextChannelGroup = mdf.ReadU32(); - block.ptrFirstChannelBlock = mdf.ReadU32(); - block.ptrTextBlock = mdf.ReadU32(); + block.ptrNextChannelGroup = mdf.ReadU32().ValidateAddress(mdf); + block.ptrFirstChannelBlock = mdf.ReadU32().ValidateAddress(mdf); + block.ptrTextBlock = mdf.ReadU32().ValidateAddress(mdf); block.RecordID = mdf.ReadU16(); block.NumChannels = mdf.ReadU16(); block.RecordSize = mdf.ReadU16(); @@ -103,12 +103,12 @@ internal static ChannelGroupBlock Read(Mdf mdf, ulong position) private static void ReadV4(Mdf mdf, ChannelGroupBlock block) { - block.ptrNextChannelGroup = mdf.ReadU64(); - block.ptrFirstChannelBlock = mdf.ReadU64(); - block.ptrTextName = mdf.ReadU64(); - block.ptrSourceInfo = mdf.ReadU64(); - block.ptrFirstSampleReductionBlock = mdf.ReadU64(); - block.ptrTextBlock = mdf.ReadU64(); + block.ptrNextChannelGroup = mdf.ReadU64().ValidateAddress(mdf); + block.ptrFirstChannelBlock = mdf.ReadU64().ValidateAddress(mdf); + block.ptrTextName = mdf.ReadU64().ValidateAddress(mdf); + block.ptrSourceInfo = mdf.ReadU64().ValidateAddress(mdf); + block.ptrFirstSampleReductionBlock = mdf.ReadU64().ValidateAddress(mdf); + block.ptrTextBlock = mdf.ReadU64().ValidateAddress(mdf); block.RecordID = mdf.ReadU64(); block.CycleCount = mdf.ReadU64(); block.Flags = mdf.ReadU16(); diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index 9b2dcaf..a08fff7 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -82,10 +82,10 @@ public static DataGroupBlock Read(Mdf mdf, ulong position) return block; } - block.ptrNextDataGroup = mdf.ReadU32(); - block.ptrFirstChannelGroupBlock = mdf.ReadU32(); - block.ptrTriggerBlock = mdf.ReadU32(); - block.ptrDataBlock = mdf.ReadU32(); + block.ptrNextDataGroup = mdf.ReadU32().ValidateAddress(mdf); + block.ptrFirstChannelGroupBlock = mdf.ReadU32().ValidateAddress(mdf); + block.ptrTriggerBlock = mdf.ReadU32().ValidateAddress(mdf); + block.ptrDataBlock = mdf.ReadU32().ValidateAddress(mdf); block.NumChannelGroups = mdf.ReadU16(); block.NumRecordIds = mdf.ReadU16(); @@ -120,10 +120,10 @@ public static DataGroupBlock Read(Mdf mdf, ulong position) private static void ReadV4(Mdf mdf, DataGroupBlock block) { - block.ptrNextDataGroup = mdf.ReadU64(); - block.ptrFirstChannelGroupBlock = mdf.ReadU64(); - block.ptrDataBlock = mdf.ReadU64(); - block.ptrTextBlock = mdf.ReadU64(); + block.ptrNextDataGroup = mdf.ReadU64().ValidateAddress(mdf); + block.ptrFirstChannelGroupBlock = mdf.ReadU64().ValidateAddress(mdf); + block.ptrDataBlock = mdf.ReadU64().ValidateAddress(mdf); + block.ptrTextBlock = mdf.ReadU64().ValidateAddress(mdf); block.NumRecordIds = mdf.ReadByte(); block.Reserved1 = mdf.ReadByte(); diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index 2e89369..c30d51f 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -223,9 +223,9 @@ internal static HeaderBlock Read(Mdf mdf) return block; } - block.ptrFirstDataGroup = mdf.ReadU32(); - block.ptrTextBlock = mdf.ReadU32(); - block.ptrProgramBlock = mdf.ReadU32(); + block.ptrFirstDataGroup = mdf.ReadU32().ValidateAddress(mdf); + block.ptrTextBlock = mdf.ReadU32().ValidateAddress(mdf); + block.ptrProgramBlock = mdf.ReadU32().ValidateAddress(mdf); block.DataGroupsCount = mdf.ReadU16(); @@ -350,7 +350,7 @@ internal void WriteProgramBlock(byte[] array, ref int index, int baseIndex) private static void ReadV4(Mdf mdf, HeaderBlock block) { - block.ptrFirstDataGroup = mdf.ReadU64(); //Adress DataGroup + block.ptrFirstDataGroup = mdf.ReadU64().ValidateAddress(mdf); //Adress DataGroup //skiped: FileHistoryBlock (not used) +8 //skiped: Chanel... (not used) +8 //skiped: AttachmentBlock (not used) +8 @@ -358,7 +358,7 @@ private static void ReadV4(Mdf mdf, HeaderBlock block) ulong skippedCount = 8 * 4; mdf.UpdatePosition(mdf.position + skippedCount); - block.ptrTextBlock = mdf.ReadU64(); + block.ptrTextBlock = mdf.ReadU64().ValidateAddress(mdf); block.StartTimeNs = mdf.ReadU64(); block.TimeZoneOffsetMinutes = mdf.Read16(); block.DstOffsetMinutes = mdf.Read16(); diff --git a/ASAM.MDF/Libary/StreamReadHelper.cs b/ASAM.MDF/Libary/StreamReadHelper.cs index 4f1770d..92ccd2f 100644 --- a/ASAM.MDF/Libary/StreamReadHelper.cs +++ b/ASAM.MDF/Libary/StreamReadHelper.cs @@ -112,5 +112,42 @@ public static string GetString(this Mdf mdf, ulong count) return value; } + + public static ushort ValidateAddress(this ushort value, Mdf mdf) + { + if (value >= mdf.Data.Length) + return 0; + return value; + } + public static uint ValidateAddress(this uint value, Mdf mdf) + { + if (value >= mdf.Data.Length) + return 0; + return value; + } + public static ulong ValidateAddress(this ulong value, Mdf mdf) + { + if (value >= (ulong)mdf.Data.Length) + return 0; + return value; + } + public static short ValidateAddress(this short value, Mdf mdf) + { + if (value >= mdf.Data.Length) + return 0; + return value; + } + public static int ValidateAddress(this int value, Mdf mdf) + { + if (value >= mdf.Data.Length) + return 0; + return value; + } + public static long ValidateAddress(this long value, Mdf mdf) + { + if (value >= mdf.Data.Length) + return 0; + return value; + } } } From fc15a91ee1f49cf0827c436213ed0beafd30911f Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 4 Apr 2024 20:42:33 +0300 Subject: [PATCH 09/34] Try update Asam mdf to deep copy --- ASAM.MDF.Tests/MdfTests.cs | 39 +++++++++++++++++++++ ASAM.MDF/Libary/Block.cs | 7 ++++ ASAM.MDF/Libary/ChannelBlock.cs | 15 ++++++++ ASAM.MDF/Libary/ChannelCollection.cs | 13 +++++++ ASAM.MDF/Libary/ChannelConversionBlock.cs | 24 ++++++++++++- ASAM.MDF/Libary/ChannelDependencyBlock.cs | 5 ++- ASAM.MDF/Libary/ChannelExtensionBlock.cs | 7 ++++ ASAM.MDF/Libary/ChannelGroupBlock.cs | 10 ++++++ ASAM.MDF/Libary/ChannelGroupCollection.cs | 12 +++++++ ASAM.MDF/Libary/ConversionData.cs | 6 ++++ ASAM.MDF/Libary/DataBlock.cs | 8 +++++ ASAM.MDF/Libary/DataGroupBlock.cs | 12 +++++++ ASAM.MDF/Libary/DataGroupCollection.cs | 13 +++++++ ASAM.MDF/Libary/DataList.cs | 8 +++++ ASAM.MDF/Libary/DataListCollection.cs | 14 ++++++++ ASAM.MDF/Libary/DataZippedBlock.cs | 5 +++ ASAM.MDF/Libary/DimBlockSupplement.cs | 4 +++ ASAM.MDF/Libary/HeaderBlock.cs | 7 ++++ ASAM.MDF/Libary/IdentificationBlock.cs | 7 ++++ ASAM.MDF/Libary/Mdf.cs | 10 +++++- ASAM.MDF/Libary/TriggerBlock.cs | 8 +++++ ASAM.MDF/Libary/VectorCanBlockSupplement.cs | 5 +++ 22 files changed, 236 insertions(+), 3 deletions(-) diff --git a/ASAM.MDF.Tests/MdfTests.cs b/ASAM.MDF.Tests/MdfTests.cs index 44e4ef0..7a60265 100644 --- a/ASAM.MDF.Tests/MdfTests.cs +++ b/ASAM.MDF.Tests/MdfTests.cs @@ -13,6 +13,45 @@ [TestFixture] public class MdfTests { + [Test] + public void Test() + { + var filename = "C:\\Users\\Михаил\\Desktop\\Работа\\EU.dat"; + var bytes = File.ReadAllBytes(filename); + var mdf = new Mdf(bytes); + var cloned = mdf.Clone(); + + CheckHashCodes(mdf, cloned); + } + + private void CheckHashCodes(Mdf mdf, Mdf cloned) + { + var listMdf = RecursiveObjectData(mdf); + var listCloned = RecursiveObjectData(cloned); + + + } + private List RecursiveObjectData(object value, List list = null) + { + if (list ==null) + list = new List(); + + var type = value.GetType(); + var fields = type.GetFields(); + var properties = type.GetProperties(); + foreach ( var prop in properties ) + { + list.Add(prop); + list = RecursiveObjectData(prop, list); + } + foreach ( var field in fields) + { + list.Add(field); + list = RecursiveObjectData(field, list); + } + return list; + } + [Test] public void BaseWriteReadTest() { diff --git a/ASAM.MDF/Libary/Block.cs b/ASAM.MDF/Libary/Block.cs index c22ad8d..19c0d4e 100644 --- a/ASAM.MDF/Libary/Block.cs +++ b/ASAM.MDF/Libary/Block.cs @@ -97,5 +97,12 @@ private void ReadV4() Size = Mdf.ReadU64(); LinksCount = Mdf.ReadU64(); } + + public virtual Block Clone(Mdf mdf) + { + var block = MemberwiseClone() as Block; + block.Mdf = mdf; + return block; + } } } diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index e2307b9..dbdbebb 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -2,6 +2,7 @@ { using System; using System.IO; + using System.Reflection; using System.Text; using ASAM.MDF.Libary.Types; @@ -319,5 +320,19 @@ internal void WriteNextChannelLink(byte[] array, int index, int blockIndex) Array.Copy(bytesNextChannelLink, 0, array, blockIndex + 4, bytesNextChannelLink.Length); } + public override Block Clone(Mdf mdf) + { + var Cb = base.Clone(mdf) as ChannelBlock; + Cb.ChannelConversion = ChannelConversion.Clone(mdf) as ChannelConversionBlock; + Cb.Comment = Comment.Clone(mdf) as TextBlock; + Cb.Dependency = Dependency.Clone(mdf) as ChannelDependencyBlock; + Cb.DisplayName = DisplayName.Clone(mdf) as TextBlock; + Cb.LongSignalName = LongSignalName.Clone(mdf) as TextBlock; + Cb.next = next.Clone(mdf) as ChannelBlock; + Cb.SourceDepending = SourceDepending.Clone(mdf) as ChannelExtensionBlock; + Cb.Unit = Unit.Clone(mdf) as TextBlock; + + return Cb; + } } } diff --git a/ASAM.MDF/Libary/ChannelCollection.cs b/ASAM.MDF/Libary/ChannelCollection.cs index db3d0de..bbfd258 100644 --- a/ASAM.MDF/Libary/ChannelCollection.cs +++ b/ASAM.MDF/Libary/ChannelCollection.cs @@ -101,5 +101,18 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw new NotImplementedException(); } + public ChannelCollection Clone(Mdf mdf) + { + var Cc = MemberwiseClone() as ChannelCollection; + var list = new List(); + + foreach (var item in items) + { + list.Add(item.Clone(mdf) as ChannelBlock); + } + Cc.items = list; + Cc.Mdf = mdf; + return Cc; + } } } diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index ac93ceb..6e10696 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -3,7 +3,7 @@ using System; using System.Collections.Generic; using System.IO; - + using System.Reflection.Metadata.Ecma335; using ASAM.MDF.Libary.Types; public class ChannelConversionBlock : Block @@ -147,5 +147,27 @@ internal override void Write(byte[] array, ref int index) index += GetSize(); } + public override Block Clone(Mdf mdf) + { + var cc = base.Clone(mdf) as ChannelConversionBlock; + cc.AdditionalConversionData = AdditionalConversionData.Clone(mdf) as ConversionData; + cc.ConversionName = ConversionName.Clone(mdf) as TextBlock; + cc.ConversionUnit = ConversionUnit.Clone(mdf) as TextBlock; + cc.FileComment = FileComment.Clone(mdf) as TextBlock; + var tabT = new List(); + foreach (TextBlock tb in cc.ConvTabT) + { + tabT.Add(tb.Clone(mdf) as TextBlock); + } + cc.ConvTabT = tabT; + var tabTValue = new List(); + foreach (double tb in cc.ConvTabTValue) + { + tabTValue.Add(tb); + } + cc.ConvTabTValue = tabTValue; + + return cc; + } } } diff --git a/ASAM.MDF/Libary/ChannelDependencyBlock.cs b/ASAM.MDF/Libary/ChannelDependencyBlock.cs index 4e4f553..0b866cc 100644 --- a/ASAM.MDF/Libary/ChannelDependencyBlock.cs +++ b/ASAM.MDF/Libary/ChannelDependencyBlock.cs @@ -4,7 +4,10 @@ namespace ASAM.MDF.Libary /// /// TODO: Incomplete /// - public class ChannelDependencyBlock// : Block + public class ChannelDependencyBlock : Block { + public ChannelDependencyBlock(Mdf mdf) : base(mdf) + { + } } } diff --git a/ASAM.MDF/Libary/ChannelExtensionBlock.cs b/ASAM.MDF/Libary/ChannelExtensionBlock.cs index a3aef21..4cabcf1 100644 --- a/ASAM.MDF/Libary/ChannelExtensionBlock.cs +++ b/ASAM.MDF/Libary/ChannelExtensionBlock.cs @@ -29,5 +29,12 @@ public ChannelExtensionBlock(Mdf mdf, ulong position) VectorCanBlockSupplement = new VectorCanBlockSupplement(mdf); } } + public override Block Clone(Mdf mdf) + { + var cheB = base.Clone(mdf) as ChannelExtensionBlock; + cheB.DimBlockSupplement = DimBlockSupplement.Clone(mdf); + cheB.VectorCanBlockSupplement = VectorCanBlockSupplement.Clone(mdf); + return cheB; + } } } diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index ee34958..085da33 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -186,5 +186,15 @@ internal void WriteNextChannelGroupBlockLink(byte[] array, int index, int blockI Array.Copy(bytesNextChannelGroupBlockLink, 0, array, blockIndex + 4, bytesNextChannelGroupBlockLink.Length); } + public override Block Clone(Mdf mdf) + { + var cgb = base.Clone(mdf) as ChannelGroupBlock; + cgb.Channels = Channels.Clone(mdf); + cgb.next = next.Clone(mdf) as ChannelGroupBlock; + cgb.Comment = Comment.Clone(mdf) as TextBlock; + cgb.TextName = TextName.Clone(mdf) as TextBlock; + + return cgb; + } } } diff --git a/ASAM.MDF/Libary/ChannelGroupCollection.cs b/ASAM.MDF/Libary/ChannelGroupCollection.cs index 3825176..7dac1f3 100644 --- a/ASAM.MDF/Libary/ChannelGroupCollection.cs +++ b/ASAM.MDF/Libary/ChannelGroupCollection.cs @@ -103,5 +103,17 @@ public ChannelGroupBlock Find(Predicate predicate) { return items.Find(predicate); } + public ChannelGroupCollection Clone(Mdf mdf) + { + var List = new List(); + foreach (ChannelGroupBlock item in items) + { + List.Add(item.Clone(mdf) as ChannelGroupBlock); + } + var cl = MemberwiseClone() as ChannelGroupCollection; + cl.items = List; + cl.Mdf = mdf; + return cl; + } } } diff --git a/ASAM.MDF/Libary/ConversionData.cs b/ASAM.MDF/Libary/ConversionData.cs index b9f6a2a..03476aa 100644 --- a/ASAM.MDF/Libary/ConversionData.cs +++ b/ASAM.MDF/Libary/ConversionData.cs @@ -127,5 +127,11 @@ public void SetParameters(params double[] parameters) Array.Copy(pBytes, 0, Data, i * 8, pBytes.Length); } } + public object Clone(Mdf mdf) + { + var c = MemberwiseClone() as ConversionData; + c.Parent = Parent.Clone(mdf) as ChannelConversionBlock; + return c; + } } } diff --git a/ASAM.MDF/Libary/DataBlock.cs b/ASAM.MDF/Libary/DataBlock.cs index 32047f3..6e92693 100644 --- a/ASAM.MDF/Libary/DataBlock.cs +++ b/ASAM.MDF/Libary/DataBlock.cs @@ -18,5 +18,13 @@ public static DataBlock Read(Mdf mdf, ulong position) return block; } + public override Block Clone(Mdf mdf) + { + var db = base.Clone(mdf) as DataBlock; + db.DataOfBlock = new byte[DataOfBlock.Length]; + DataOfBlock.CopyTo(db.DataOfBlock, 0); + + return db; + } } } \ No newline at end of file diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index a08fff7..d849711 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -291,5 +291,17 @@ internal void WriteRecords(byte[] array, ref int index, int blockIndex) index += r.Data.Length; } } + public override Block Clone(Mdf mdf) + { + var dt = base.Clone(mdf) as DataGroupBlock; + dt.ChannelGroups = ChannelGroups.Clone(mdf); + dt.nextBlock = nextBlock.Clone(mdf) as DataGroupBlock; + dt.DataListColl = DataListColl.Clone(mdf); + dt.DataZipped=DataZipped.Clone(mdf) as DataZippedBlock; + dt.FileComment = FileComment.Clone(mdf) as TextBlock; + dt.Trigger = Trigger.Clone(mdf) as TriggerBlock; + + return base.Clone(mdf); + } } } diff --git a/ASAM.MDF/Libary/DataGroupCollection.cs b/ASAM.MDF/Libary/DataGroupCollection.cs index 5d9e298..f266a3c 100644 --- a/ASAM.MDF/Libary/DataGroupCollection.cs +++ b/ASAM.MDF/Libary/DataGroupCollection.cs @@ -120,5 +120,18 @@ public DataGroupBlock Find(Predicate predicate) { return items.Find(predicate); } + public DataGroupCollection Clone(Mdf mdf) + { + var dC = MemberwiseClone() as DataGroupCollection; + var list = new List(); + + foreach (var item in items) + { + list.Add(item.Clone(mdf) as DataGroupBlock); + } + dC.items = list; + dC.Mdf = mdf; + return dC; + } } } diff --git a/ASAM.MDF/Libary/DataList.cs b/ASAM.MDF/Libary/DataList.cs index 53cc039..f9197a0 100644 --- a/ASAM.MDF/Libary/DataList.cs +++ b/ASAM.MDF/Libary/DataList.cs @@ -52,5 +52,13 @@ public static DataList Read(Mdf mdf, ulong position) return block; } + public override Block Clone(Mdf mdf) + { + var dl = base.Clone(mdf) as DataList; + dl.DataBlock = DataBlock.Clone(mdf) as DataBlock; + dl.nextBlock = nextBlock.Clone(mdf) as DataList; + + return dl; + } } } diff --git a/ASAM.MDF/Libary/DataListCollection.cs b/ASAM.MDF/Libary/DataListCollection.cs index 5110cfc..6bba04e 100644 --- a/ASAM.MDF/Libary/DataListCollection.cs +++ b/ASAM.MDF/Libary/DataListCollection.cs @@ -82,5 +82,19 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw new NotImplementedException(); } + public DataListCollection Clone(Mdf mdf) + { + var dlc = MemberwiseClone() as DataListCollection; + var list = new List(); + foreach ( DataList item in this.items ) + { + list.Add(item.Clone(mdf) as DataList); + } + dlc.items = list; + dlc.Mdf = mdf; + dlc.DataGroupBlock = DataGroupBlock.Clone(mdf) as DataGroupBlock; + + return dlc; + } } } \ No newline at end of file diff --git a/ASAM.MDF/Libary/DataZippedBlock.cs b/ASAM.MDF/Libary/DataZippedBlock.cs index a79a975..81abe40 100644 --- a/ASAM.MDF/Libary/DataZippedBlock.cs +++ b/ASAM.MDF/Libary/DataZippedBlock.cs @@ -65,5 +65,10 @@ private void DecompressData() Mdf.data[k] = trData; } } + public override Block Clone(Mdf mdf) + { + var dz = base.Clone(mdf) as DataZippedBlock; + return dz; + } } } diff --git a/ASAM.MDF/Libary/DimBlockSupplement.cs b/ASAM.MDF/Libary/DimBlockSupplement.cs index a4a4a84..88dce94 100644 --- a/ASAM.MDF/Libary/DimBlockSupplement.cs +++ b/ASAM.MDF/Libary/DimBlockSupplement.cs @@ -21,5 +21,9 @@ public DimBlockSupplement(Mdf mdf) Description = Encoding.GetEncoding(mdf.IDBlock.CodePage).GetString(data, 6, 80); IdentificationOfEcu = Encoding.GetEncoding(mdf.IDBlock.CodePage).GetString(data, 86, 32); } + public DimBlockSupplement Clone(Mdf mdf) + { + return MemberwiseClone() as DimBlockSupplement; + } } } diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index c30d51f..5e49556 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -380,5 +380,12 @@ private static void ReadV4(Mdf mdf, HeaderBlock block) mdf.DataGroups.Read(DataGroupBlock.Read(mdf, block.ptrFirstDataGroup)); } } + public override Block Clone(Mdf mdf) + { + var hd = base.Clone(mdf) as HeaderBlock; + hd.FileComment = FileComment?.Clone(mdf) as TextBlock; + hd.ProgramBlock = ProgramBlock?.Clone(mdf) as ProgramBlock; + return hd; + } } } diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index dce1333..61d50fd 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -238,5 +238,12 @@ private static string GetString(Mdf mdf, ulong count) { return Encoding.UTF8.GetString(mdf.Data, mdf.AdvanceIndex(count), (int)count); } + + public IdentificationBlock Clone(Mdf mdf) + { + var iB = MemberwiseClone() as IdentificationBlock; + iB.Mdf = mdf; + return iB; + } } } diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index f3bf259..2d563b2 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -15,7 +15,7 @@ public class Mdf public Mdf(byte[] bytes) { data = bytes; - + DataGroups = new DataGroupCollection(this); IDBlock = IdentificationBlock.Read(this); HDBlock = HeaderBlock.Read(this); @@ -96,5 +96,13 @@ internal string GetNameBlock(ulong position) var name = IDBlock.Encoding.GetString(Data, (int)index, 2); return name; } + public Mdf Clone() + { + var cloned = MemberwiseClone() as Mdf; + cloned.HDBlock = HDBlock.Clone(cloned) as HeaderBlock; + cloned.DataGroups = DataGroups.Clone(cloned); + cloned.IDBlock = IDBlock.Clone(cloned); + return cloned; + } } } diff --git a/ASAM.MDF/Libary/TriggerBlock.cs b/ASAM.MDF/Libary/TriggerBlock.cs index 9a8eb98..bf1bcd2 100644 --- a/ASAM.MDF/Libary/TriggerBlock.cs +++ b/ASAM.MDF/Libary/TriggerBlock.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection.Metadata.Ecma335; namespace ASAM.MDF.Libary { @@ -14,5 +15,12 @@ public TriggerBlock(Mdf mdf) : base(mdf) { } + public override Block Clone(Mdf mdf) + { + var tr = base.Clone(mdf) as TriggerBlock; + tr.Comment = Comment.Clone(mdf) as TextBlock; + + return tr; + } } } diff --git a/ASAM.MDF/Libary/VectorCanBlockSupplement.cs b/ASAM.MDF/Libary/VectorCanBlockSupplement.cs index 18ac2b4..b3317c6 100644 --- a/ASAM.MDF/Libary/VectorCanBlockSupplement.cs +++ b/ASAM.MDF/Libary/VectorCanBlockSupplement.cs @@ -21,5 +21,10 @@ public VectorCanBlockSupplement(Mdf mdf) NameOfMessage = Encoding.GetEncoding(mdf.IDBlock.CodePage).GetString(data, 8, 36); NameOfSender = Encoding.GetEncoding(mdf.IDBlock.CodePage).GetString(data, 44, 36); } + + internal VectorCanBlockSupplement Clone(Mdf mdf) + { + return MemberwiseClone() as VectorCanBlockSupplement; + } } } From f2835c5e4a27521b6b788599d4220e284bc3dd20 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 5 Apr 2024 11:14:10 +0300 Subject: [PATCH 10/34] Fix some problem recurse --- ASAM.MDF.Tests/MdfTests.cs | 100 +++++++++++++++++++--- ASAM.MDF/Libary/ChannelBlock.cs | 16 ++-- ASAM.MDF/Libary/ChannelConversionBlock.cs | 22 +---- ASAM.MDF/Libary/ChannelExtensionBlock.cs | 4 +- ASAM.MDF/Libary/ChannelGroupBlock.cs | 8 +- ASAM.MDF/Libary/ConversionData.cs | 2 +- ASAM.MDF/Libary/DataGroupBlock.cs | 16 ++-- ASAM.MDF/Libary/DataList.cs | 4 +- ASAM.MDF/Libary/DataListCollection.cs | 4 +- ASAM.MDF/Libary/HeaderBlock.cs | 1 + ASAM.MDF/Libary/IdentificationBlock.cs | 2 +- ASAM.MDF/Libary/Mdf.cs | 10 ++- ASAM.MDF/Libary/TriggerBlock.cs | 2 +- 13 files changed, 129 insertions(+), 62 deletions(-) diff --git a/ASAM.MDF.Tests/MdfTests.cs b/ASAM.MDF.Tests/MdfTests.cs index 7a60265..3be54ea 100644 --- a/ASAM.MDF.Tests/MdfTests.cs +++ b/ASAM.MDF.Tests/MdfTests.cs @@ -2,7 +2,10 @@ { using System; using System.Collections.Generic; + using System.Diagnostics; using System.IO; + using System.Linq; + using System.Reflection; using System.Text; using ASAM.MDF.Libary; @@ -16,7 +19,7 @@ public class MdfTests [Test] public void Test() { - var filename = "C:\\Users\\Михаил\\Desktop\\Работа\\EU.dat"; + var filename = "\\\\Foond\\Проекты\\Autogramma\\Материалы разработки\\Примеры файлов измерений\\EU.dat"; var bytes = File.ReadAllBytes(filename); var mdf = new Mdf(bytes); var cloned = mdf.Clone(); @@ -26,28 +29,101 @@ public void Test() private void CheckHashCodes(Mdf mdf, Mdf cloned) { - var listMdf = RecursiveObjectData(mdf); - var listCloned = RecursiveObjectData(cloned); + var listMdf = RecursiveObjectData(mdf, null, new HashSet() { mdf }).ToList(); + var listCloned = RecursiveObjectData(cloned, null, new HashSet() { cloned }).ToList(); + + for (int i = 0; i < listMdf.Count; i++) + { + var m1 = listMdf[i]; + var m2 = listCloned[i]; + if (m1.GetHashCode() == m2.GetHashCode()) + throw new Exception($"index are equals: {i}, \n{m1}\n{m2}"); + } } - private List RecursiveObjectData(object value, List list = null) + private HashSet RecursiveObjectData(object value, Type typeV = null, HashSet list = null) { - if (list ==null) - list = new List(); + if (list == null) + list = new HashSet(); + + if (value == null) + return list; var type = value.GetType(); + if (typeV != null) + type = typeV; + var fields = type.GetFields(); var properties = type.GetProperties(); - foreach ( var prop in properties ) + foreach (var prop in properties) { - list.Add(prop); - list = RecursiveObjectData(prop, list); + var attributes = prop.GetCustomAttribute(true); + if (attributes != null) + continue; + + try + { + var propValue = prop.GetValue(value); + if (prop.PropertyType.IsValueType) + { + if (!list.Contains(propValue)) + list.Add(propValue); + } + else if (propValue is string) + { + if (!list.Contains(propValue)) + list.Add(propValue); + } + else + { + if (!list.Contains(propValue)) + { + list.Add(propValue); + list = RecursiveObjectData(propValue, prop.PropertyType, list); + } + } + } + catch (Exception e) + { + Debug.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + Debug.Print(e.Message); + Debug.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + } + finally + { + Debug.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + Debug.Print(prop.ToString() + "| Count:" + list.Count.ToString()); + Debug.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + } + + } - foreach ( var field in fields) + foreach (var field in fields) { - list.Add(field); - list = RecursiveObjectData(field, list); + var attributes = field.GetCustomAttribute(true); + if (attributes != null) + continue; + + var fieldValue = field.GetValue(value); + if (field.FieldType.IsValueType) + { + if (!list.Contains(fieldValue)) + list.Add(fieldValue); + } + else if (fieldValue is string) + { + if (!list.Contains(fieldValue)) + list.Add(fieldValue); + } + else + { + if (!list.Contains(fieldValue)) + { + list.Add(fieldValue); + list = RecursiveObjectData(fieldValue, field.FieldType, list); + } + } } return list; } diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index dbdbebb..19e839b 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -323,14 +323,14 @@ internal void WriteNextChannelLink(byte[] array, int index, int blockIndex) public override Block Clone(Mdf mdf) { var Cb = base.Clone(mdf) as ChannelBlock; - Cb.ChannelConversion = ChannelConversion.Clone(mdf) as ChannelConversionBlock; - Cb.Comment = Comment.Clone(mdf) as TextBlock; - Cb.Dependency = Dependency.Clone(mdf) as ChannelDependencyBlock; - Cb.DisplayName = DisplayName.Clone(mdf) as TextBlock; - Cb.LongSignalName = LongSignalName.Clone(mdf) as TextBlock; - Cb.next = next.Clone(mdf) as ChannelBlock; - Cb.SourceDepending = SourceDepending.Clone(mdf) as ChannelExtensionBlock; - Cb.Unit = Unit.Clone(mdf) as TextBlock; + Cb.ChannelConversion = ChannelConversion?.Clone(mdf) as ChannelConversionBlock; + Cb.Comment = Comment?.Clone(mdf) as TextBlock; + Cb.Dependency = Dependency?.Clone(mdf) as ChannelDependencyBlock; + Cb.DisplayName = DisplayName?.Clone(mdf) as TextBlock; + Cb.LongSignalName = LongSignalName?.Clone(mdf) as TextBlock; + Cb.next = next?.Clone(mdf) as ChannelBlock; + Cb.SourceDepending = SourceDepending?.Clone(mdf) as ChannelExtensionBlock; + Cb.Unit = Unit?.Clone(mdf) as TextBlock; return Cb; } diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index 6e10696..d65caa7 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -40,8 +40,6 @@ public string PhysicalUnit public TextBlock FileComment { get; private set; } public TextBlock ConversionUnit { get; private set; } public TextBlock ConversionName { get; private set; } - public List ConvTabT { get; internal set; } - public List ConvTabTValue { get; internal set; } public static ChannelConversionBlock Create(Mdf mdf) { @@ -150,22 +148,10 @@ internal override void Write(byte[] array, ref int index) public override Block Clone(Mdf mdf) { var cc = base.Clone(mdf) as ChannelConversionBlock; - cc.AdditionalConversionData = AdditionalConversionData.Clone(mdf) as ConversionData; - cc.ConversionName = ConversionName.Clone(mdf) as TextBlock; - cc.ConversionUnit = ConversionUnit.Clone(mdf) as TextBlock; - cc.FileComment = FileComment.Clone(mdf) as TextBlock; - var tabT = new List(); - foreach (TextBlock tb in cc.ConvTabT) - { - tabT.Add(tb.Clone(mdf) as TextBlock); - } - cc.ConvTabT = tabT; - var tabTValue = new List(); - foreach (double tb in cc.ConvTabTValue) - { - tabTValue.Add(tb); - } - cc.ConvTabTValue = tabTValue; + cc.AdditionalConversionData = AdditionalConversionData?.Clone(mdf) as ConversionData; + cc.ConversionName = ConversionName?.Clone(mdf) as TextBlock; + cc.ConversionUnit = ConversionUnit?.Clone(mdf) as TextBlock; + cc.FileComment = FileComment?.Clone(mdf) as TextBlock; return cc; } diff --git a/ASAM.MDF/Libary/ChannelExtensionBlock.cs b/ASAM.MDF/Libary/ChannelExtensionBlock.cs index 4cabcf1..f17dcdc 100644 --- a/ASAM.MDF/Libary/ChannelExtensionBlock.cs +++ b/ASAM.MDF/Libary/ChannelExtensionBlock.cs @@ -32,8 +32,8 @@ public ChannelExtensionBlock(Mdf mdf, ulong position) public override Block Clone(Mdf mdf) { var cheB = base.Clone(mdf) as ChannelExtensionBlock; - cheB.DimBlockSupplement = DimBlockSupplement.Clone(mdf); - cheB.VectorCanBlockSupplement = VectorCanBlockSupplement.Clone(mdf); + cheB.DimBlockSupplement = DimBlockSupplement?.Clone(mdf); + cheB.VectorCanBlockSupplement = VectorCanBlockSupplement?.Clone(mdf); return cheB; } } diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index 085da33..6069f7e 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -189,10 +189,10 @@ internal void WriteNextChannelGroupBlockLink(byte[] array, int index, int blockI public override Block Clone(Mdf mdf) { var cgb = base.Clone(mdf) as ChannelGroupBlock; - cgb.Channels = Channels.Clone(mdf); - cgb.next = next.Clone(mdf) as ChannelGroupBlock; - cgb.Comment = Comment.Clone(mdf) as TextBlock; - cgb.TextName = TextName.Clone(mdf) as TextBlock; + cgb.Channels = Channels?.Clone(mdf); + cgb.next = next?.Clone(mdf) as ChannelGroupBlock; + cgb.Comment = Comment?.Clone(mdf) as TextBlock; + cgb.TextName = TextName?.Clone(mdf) as TextBlock; return cgb; } diff --git a/ASAM.MDF/Libary/ConversionData.cs b/ASAM.MDF/Libary/ConversionData.cs index 03476aa..32e4267 100644 --- a/ASAM.MDF/Libary/ConversionData.cs +++ b/ASAM.MDF/Libary/ConversionData.cs @@ -130,7 +130,7 @@ public void SetParameters(params double[] parameters) public object Clone(Mdf mdf) { var c = MemberwiseClone() as ConversionData; - c.Parent = Parent.Clone(mdf) as ChannelConversionBlock; + //c.Parent = Parent?.Clone(mdf) as ChannelConversionBlock; return c; } } diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index d849711..b748dc9 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -294,14 +294,14 @@ internal void WriteRecords(byte[] array, ref int index, int blockIndex) public override Block Clone(Mdf mdf) { var dt = base.Clone(mdf) as DataGroupBlock; - dt.ChannelGroups = ChannelGroups.Clone(mdf); - dt.nextBlock = nextBlock.Clone(mdf) as DataGroupBlock; - dt.DataListColl = DataListColl.Clone(mdf); - dt.DataZipped=DataZipped.Clone(mdf) as DataZippedBlock; - dt.FileComment = FileComment.Clone(mdf) as TextBlock; - dt.Trigger = Trigger.Clone(mdf) as TriggerBlock; - - return base.Clone(mdf); + dt.ChannelGroups = ChannelGroups?.Clone(mdf); + dt.nextBlock = Next?.Clone(mdf) as DataGroupBlock; + dt.DataListColl = DataListColl?.Clone(mdf); + dt.DataZipped = DataZipped?.Clone(mdf) as DataZippedBlock; + dt.FileComment = FileComment?.Clone(mdf) as TextBlock; + dt.Trigger = Trigger?.Clone(mdf) as TriggerBlock; + + return dt; } } } diff --git a/ASAM.MDF/Libary/DataList.cs b/ASAM.MDF/Libary/DataList.cs index f9197a0..485c8ca 100644 --- a/ASAM.MDF/Libary/DataList.cs +++ b/ASAM.MDF/Libary/DataList.cs @@ -55,8 +55,8 @@ public static DataList Read(Mdf mdf, ulong position) public override Block Clone(Mdf mdf) { var dl = base.Clone(mdf) as DataList; - dl.DataBlock = DataBlock.Clone(mdf) as DataBlock; - dl.nextBlock = nextBlock.Clone(mdf) as DataList; + dl.DataBlock = DataBlock?.Clone(mdf) as DataBlock; + dl.nextBlock = nextBlock?.Clone(mdf) as DataList; return dl; } diff --git a/ASAM.MDF/Libary/DataListCollection.cs b/ASAM.MDF/Libary/DataListCollection.cs index 6bba04e..9e1f17e 100644 --- a/ASAM.MDF/Libary/DataListCollection.cs +++ b/ASAM.MDF/Libary/DataListCollection.cs @@ -86,13 +86,13 @@ public DataListCollection Clone(Mdf mdf) { var dlc = MemberwiseClone() as DataListCollection; var list = new List(); - foreach ( DataList item in this.items ) + foreach ( DataList item in items ) { list.Add(item.Clone(mdf) as DataList); } dlc.items = list; dlc.Mdf = mdf; - dlc.DataGroupBlock = DataGroupBlock.Clone(mdf) as DataGroupBlock; + //dlc.DataGroupBlock = DataGroupBlock?.Clone(mdf) as DataGroupBlock; return dlc; } diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index 5e49556..2b9cf11 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -385,6 +385,7 @@ public override Block Clone(Mdf mdf) var hd = base.Clone(mdf) as HeaderBlock; hd.FileComment = FileComment?.Clone(mdf) as TextBlock; hd.ProgramBlock = ProgramBlock?.Clone(mdf) as ProgramBlock; + return hd; } } diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index 61d50fd..428ef5c 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -26,7 +26,7 @@ private IdentificationBlock() } public Mdf Mdf { get; private set; } - + [NonSerialize] public Encoding Encoding { get; private set; } /// diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index 2d563b2..e9e1ab7 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -99,9 +99,13 @@ internal string GetNameBlock(ulong position) public Mdf Clone() { var cloned = MemberwiseClone() as Mdf; - cloned.HDBlock = HDBlock.Clone(cloned) as HeaderBlock; - cloned.DataGroups = DataGroups.Clone(cloned); - cloned.IDBlock = IDBlock.Clone(cloned); + cloned.HDBlock = HDBlock?.Clone(cloned) as HeaderBlock; + cloned.DataGroups = DataGroups?.Clone(cloned); + cloned.IDBlock = IDBlock?.Clone(cloned); + + cloned.data = new byte[data.Length]; + data.CopyTo(cloned.data, 0); + return cloned; } } diff --git a/ASAM.MDF/Libary/TriggerBlock.cs b/ASAM.MDF/Libary/TriggerBlock.cs index bf1bcd2..de4899f 100644 --- a/ASAM.MDF/Libary/TriggerBlock.cs +++ b/ASAM.MDF/Libary/TriggerBlock.cs @@ -18,7 +18,7 @@ public TriggerBlock(Mdf mdf) public override Block Clone(Mdf mdf) { var tr = base.Clone(mdf) as TriggerBlock; - tr.Comment = Comment.Clone(mdf) as TextBlock; + tr.Comment = Comment?.Clone(mdf) as TextBlock; return tr; } From b96b75d10b79e54de83a2f48ee655838563277cd Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 5 Apr 2024 16:47:37 +0300 Subject: [PATCH 11/34] Revert "Fix some problem recurse" This reverts commit f2835c5e4a27521b6b788599d4220e284bc3dd20. --- ASAM.MDF.Tests/MdfTests.cs | 100 +++------------------- ASAM.MDF/Libary/ChannelBlock.cs | 16 ++-- ASAM.MDF/Libary/ChannelConversionBlock.cs | 22 ++++- ASAM.MDF/Libary/ChannelExtensionBlock.cs | 4 +- ASAM.MDF/Libary/ChannelGroupBlock.cs | 8 +- ASAM.MDF/Libary/ConversionData.cs | 2 +- ASAM.MDF/Libary/DataGroupBlock.cs | 16 ++-- ASAM.MDF/Libary/DataList.cs | 4 +- ASAM.MDF/Libary/DataListCollection.cs | 4 +- ASAM.MDF/Libary/HeaderBlock.cs | 1 - ASAM.MDF/Libary/IdentificationBlock.cs | 2 +- ASAM.MDF/Libary/Mdf.cs | 10 +-- ASAM.MDF/Libary/TriggerBlock.cs | 2 +- 13 files changed, 62 insertions(+), 129 deletions(-) diff --git a/ASAM.MDF.Tests/MdfTests.cs b/ASAM.MDF.Tests/MdfTests.cs index 3be54ea..7a60265 100644 --- a/ASAM.MDF.Tests/MdfTests.cs +++ b/ASAM.MDF.Tests/MdfTests.cs @@ -2,10 +2,7 @@ { using System; using System.Collections.Generic; - using System.Diagnostics; using System.IO; - using System.Linq; - using System.Reflection; using System.Text; using ASAM.MDF.Libary; @@ -19,7 +16,7 @@ public class MdfTests [Test] public void Test() { - var filename = "\\\\Foond\\Проекты\\Autogramma\\Материалы разработки\\Примеры файлов измерений\\EU.dat"; + var filename = "C:\\Users\\Михаил\\Desktop\\Работа\\EU.dat"; var bytes = File.ReadAllBytes(filename); var mdf = new Mdf(bytes); var cloned = mdf.Clone(); @@ -29,101 +26,28 @@ public void Test() private void CheckHashCodes(Mdf mdf, Mdf cloned) { - var listMdf = RecursiveObjectData(mdf, null, new HashSet() { mdf }).ToList(); - var listCloned = RecursiveObjectData(cloned, null, new HashSet() { cloned }).ToList(); - - for (int i = 0; i < listMdf.Count; i++) - { - var m1 = listMdf[i]; - var m2 = listCloned[i]; - if (m1.GetHashCode() == m2.GetHashCode()) - throw new Exception($"index are equals: {i}, \n{m1}\n{m2}"); - } + var listMdf = RecursiveObjectData(mdf); + var listCloned = RecursiveObjectData(cloned); } - private HashSet RecursiveObjectData(object value, Type typeV = null, HashSet list = null) + private List RecursiveObjectData(object value, List list = null) { - if (list == null) - list = new HashSet(); - - if (value == null) - return list; + if (list ==null) + list = new List(); var type = value.GetType(); - if (typeV != null) - type = typeV; - var fields = type.GetFields(); var properties = type.GetProperties(); - foreach (var prop in properties) + foreach ( var prop in properties ) { - var attributes = prop.GetCustomAttribute(true); - if (attributes != null) - continue; - - try - { - var propValue = prop.GetValue(value); - if (prop.PropertyType.IsValueType) - { - if (!list.Contains(propValue)) - list.Add(propValue); - } - else if (propValue is string) - { - if (!list.Contains(propValue)) - list.Add(propValue); - } - else - { - if (!list.Contains(propValue)) - { - list.Add(propValue); - list = RecursiveObjectData(propValue, prop.PropertyType, list); - } - } - } - catch (Exception e) - { - Debug.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - Debug.Print(e.Message); - Debug.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - } - finally - { - Debug.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - Debug.Print(prop.ToString() + "| Count:" + list.Count.ToString()); - Debug.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - } - - + list.Add(prop); + list = RecursiveObjectData(prop, list); } - foreach (var field in fields) + foreach ( var field in fields) { - var attributes = field.GetCustomAttribute(true); - if (attributes != null) - continue; - - var fieldValue = field.GetValue(value); - if (field.FieldType.IsValueType) - { - if (!list.Contains(fieldValue)) - list.Add(fieldValue); - } - else if (fieldValue is string) - { - if (!list.Contains(fieldValue)) - list.Add(fieldValue); - } - else - { - if (!list.Contains(fieldValue)) - { - list.Add(fieldValue); - list = RecursiveObjectData(fieldValue, field.FieldType, list); - } - } + list.Add(field); + list = RecursiveObjectData(field, list); } return list; } diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 19e839b..dbdbebb 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -323,14 +323,14 @@ internal void WriteNextChannelLink(byte[] array, int index, int blockIndex) public override Block Clone(Mdf mdf) { var Cb = base.Clone(mdf) as ChannelBlock; - Cb.ChannelConversion = ChannelConversion?.Clone(mdf) as ChannelConversionBlock; - Cb.Comment = Comment?.Clone(mdf) as TextBlock; - Cb.Dependency = Dependency?.Clone(mdf) as ChannelDependencyBlock; - Cb.DisplayName = DisplayName?.Clone(mdf) as TextBlock; - Cb.LongSignalName = LongSignalName?.Clone(mdf) as TextBlock; - Cb.next = next?.Clone(mdf) as ChannelBlock; - Cb.SourceDepending = SourceDepending?.Clone(mdf) as ChannelExtensionBlock; - Cb.Unit = Unit?.Clone(mdf) as TextBlock; + Cb.ChannelConversion = ChannelConversion.Clone(mdf) as ChannelConversionBlock; + Cb.Comment = Comment.Clone(mdf) as TextBlock; + Cb.Dependency = Dependency.Clone(mdf) as ChannelDependencyBlock; + Cb.DisplayName = DisplayName.Clone(mdf) as TextBlock; + Cb.LongSignalName = LongSignalName.Clone(mdf) as TextBlock; + Cb.next = next.Clone(mdf) as ChannelBlock; + Cb.SourceDepending = SourceDepending.Clone(mdf) as ChannelExtensionBlock; + Cb.Unit = Unit.Clone(mdf) as TextBlock; return Cb; } diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index d65caa7..6e10696 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -40,6 +40,8 @@ public string PhysicalUnit public TextBlock FileComment { get; private set; } public TextBlock ConversionUnit { get; private set; } public TextBlock ConversionName { get; private set; } + public List ConvTabT { get; internal set; } + public List ConvTabTValue { get; internal set; } public static ChannelConversionBlock Create(Mdf mdf) { @@ -148,10 +150,22 @@ internal override void Write(byte[] array, ref int index) public override Block Clone(Mdf mdf) { var cc = base.Clone(mdf) as ChannelConversionBlock; - cc.AdditionalConversionData = AdditionalConversionData?.Clone(mdf) as ConversionData; - cc.ConversionName = ConversionName?.Clone(mdf) as TextBlock; - cc.ConversionUnit = ConversionUnit?.Clone(mdf) as TextBlock; - cc.FileComment = FileComment?.Clone(mdf) as TextBlock; + cc.AdditionalConversionData = AdditionalConversionData.Clone(mdf) as ConversionData; + cc.ConversionName = ConversionName.Clone(mdf) as TextBlock; + cc.ConversionUnit = ConversionUnit.Clone(mdf) as TextBlock; + cc.FileComment = FileComment.Clone(mdf) as TextBlock; + var tabT = new List(); + foreach (TextBlock tb in cc.ConvTabT) + { + tabT.Add(tb.Clone(mdf) as TextBlock); + } + cc.ConvTabT = tabT; + var tabTValue = new List(); + foreach (double tb in cc.ConvTabTValue) + { + tabTValue.Add(tb); + } + cc.ConvTabTValue = tabTValue; return cc; } diff --git a/ASAM.MDF/Libary/ChannelExtensionBlock.cs b/ASAM.MDF/Libary/ChannelExtensionBlock.cs index f17dcdc..4cabcf1 100644 --- a/ASAM.MDF/Libary/ChannelExtensionBlock.cs +++ b/ASAM.MDF/Libary/ChannelExtensionBlock.cs @@ -32,8 +32,8 @@ public ChannelExtensionBlock(Mdf mdf, ulong position) public override Block Clone(Mdf mdf) { var cheB = base.Clone(mdf) as ChannelExtensionBlock; - cheB.DimBlockSupplement = DimBlockSupplement?.Clone(mdf); - cheB.VectorCanBlockSupplement = VectorCanBlockSupplement?.Clone(mdf); + cheB.DimBlockSupplement = DimBlockSupplement.Clone(mdf); + cheB.VectorCanBlockSupplement = VectorCanBlockSupplement.Clone(mdf); return cheB; } } diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index 6069f7e..085da33 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -189,10 +189,10 @@ internal void WriteNextChannelGroupBlockLink(byte[] array, int index, int blockI public override Block Clone(Mdf mdf) { var cgb = base.Clone(mdf) as ChannelGroupBlock; - cgb.Channels = Channels?.Clone(mdf); - cgb.next = next?.Clone(mdf) as ChannelGroupBlock; - cgb.Comment = Comment?.Clone(mdf) as TextBlock; - cgb.TextName = TextName?.Clone(mdf) as TextBlock; + cgb.Channels = Channels.Clone(mdf); + cgb.next = next.Clone(mdf) as ChannelGroupBlock; + cgb.Comment = Comment.Clone(mdf) as TextBlock; + cgb.TextName = TextName.Clone(mdf) as TextBlock; return cgb; } diff --git a/ASAM.MDF/Libary/ConversionData.cs b/ASAM.MDF/Libary/ConversionData.cs index 32e4267..03476aa 100644 --- a/ASAM.MDF/Libary/ConversionData.cs +++ b/ASAM.MDF/Libary/ConversionData.cs @@ -130,7 +130,7 @@ public void SetParameters(params double[] parameters) public object Clone(Mdf mdf) { var c = MemberwiseClone() as ConversionData; - //c.Parent = Parent?.Clone(mdf) as ChannelConversionBlock; + c.Parent = Parent.Clone(mdf) as ChannelConversionBlock; return c; } } diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index b748dc9..d849711 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -294,14 +294,14 @@ internal void WriteRecords(byte[] array, ref int index, int blockIndex) public override Block Clone(Mdf mdf) { var dt = base.Clone(mdf) as DataGroupBlock; - dt.ChannelGroups = ChannelGroups?.Clone(mdf); - dt.nextBlock = Next?.Clone(mdf) as DataGroupBlock; - dt.DataListColl = DataListColl?.Clone(mdf); - dt.DataZipped = DataZipped?.Clone(mdf) as DataZippedBlock; - dt.FileComment = FileComment?.Clone(mdf) as TextBlock; - dt.Trigger = Trigger?.Clone(mdf) as TriggerBlock; - - return dt; + dt.ChannelGroups = ChannelGroups.Clone(mdf); + dt.nextBlock = nextBlock.Clone(mdf) as DataGroupBlock; + dt.DataListColl = DataListColl.Clone(mdf); + dt.DataZipped=DataZipped.Clone(mdf) as DataZippedBlock; + dt.FileComment = FileComment.Clone(mdf) as TextBlock; + dt.Trigger = Trigger.Clone(mdf) as TriggerBlock; + + return base.Clone(mdf); } } } diff --git a/ASAM.MDF/Libary/DataList.cs b/ASAM.MDF/Libary/DataList.cs index 485c8ca..f9197a0 100644 --- a/ASAM.MDF/Libary/DataList.cs +++ b/ASAM.MDF/Libary/DataList.cs @@ -55,8 +55,8 @@ public static DataList Read(Mdf mdf, ulong position) public override Block Clone(Mdf mdf) { var dl = base.Clone(mdf) as DataList; - dl.DataBlock = DataBlock?.Clone(mdf) as DataBlock; - dl.nextBlock = nextBlock?.Clone(mdf) as DataList; + dl.DataBlock = DataBlock.Clone(mdf) as DataBlock; + dl.nextBlock = nextBlock.Clone(mdf) as DataList; return dl; } diff --git a/ASAM.MDF/Libary/DataListCollection.cs b/ASAM.MDF/Libary/DataListCollection.cs index 9e1f17e..6bba04e 100644 --- a/ASAM.MDF/Libary/DataListCollection.cs +++ b/ASAM.MDF/Libary/DataListCollection.cs @@ -86,13 +86,13 @@ public DataListCollection Clone(Mdf mdf) { var dlc = MemberwiseClone() as DataListCollection; var list = new List(); - foreach ( DataList item in items ) + foreach ( DataList item in this.items ) { list.Add(item.Clone(mdf) as DataList); } dlc.items = list; dlc.Mdf = mdf; - //dlc.DataGroupBlock = DataGroupBlock?.Clone(mdf) as DataGroupBlock; + dlc.DataGroupBlock = DataGroupBlock.Clone(mdf) as DataGroupBlock; return dlc; } diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index 2b9cf11..5e49556 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -385,7 +385,6 @@ public override Block Clone(Mdf mdf) var hd = base.Clone(mdf) as HeaderBlock; hd.FileComment = FileComment?.Clone(mdf) as TextBlock; hd.ProgramBlock = ProgramBlock?.Clone(mdf) as ProgramBlock; - return hd; } } diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index 428ef5c..61d50fd 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -26,7 +26,7 @@ private IdentificationBlock() } public Mdf Mdf { get; private set; } - [NonSerialize] + public Encoding Encoding { get; private set; } /// diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index e9e1ab7..2d563b2 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -99,13 +99,9 @@ internal string GetNameBlock(ulong position) public Mdf Clone() { var cloned = MemberwiseClone() as Mdf; - cloned.HDBlock = HDBlock?.Clone(cloned) as HeaderBlock; - cloned.DataGroups = DataGroups?.Clone(cloned); - cloned.IDBlock = IDBlock?.Clone(cloned); - - cloned.data = new byte[data.Length]; - data.CopyTo(cloned.data, 0); - + cloned.HDBlock = HDBlock.Clone(cloned) as HeaderBlock; + cloned.DataGroups = DataGroups.Clone(cloned); + cloned.IDBlock = IDBlock.Clone(cloned); return cloned; } } diff --git a/ASAM.MDF/Libary/TriggerBlock.cs b/ASAM.MDF/Libary/TriggerBlock.cs index de4899f..bf1bcd2 100644 --- a/ASAM.MDF/Libary/TriggerBlock.cs +++ b/ASAM.MDF/Libary/TriggerBlock.cs @@ -18,7 +18,7 @@ public TriggerBlock(Mdf mdf) public override Block Clone(Mdf mdf) { var tr = base.Clone(mdf) as TriggerBlock; - tr.Comment = Comment?.Clone(mdf) as TextBlock; + tr.Comment = Comment.Clone(mdf) as TextBlock; return tr; } From 9c6911a1e11d98e78a2320756968833f736c3450 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 5 Apr 2024 16:47:48 +0300 Subject: [PATCH 12/34] Revert "Try update Asam mdf to deep copy" This reverts commit fc15a91ee1f49cf0827c436213ed0beafd30911f. --- ASAM.MDF.Tests/MdfTests.cs | 39 --------------------- ASAM.MDF/Libary/Block.cs | 7 ---- ASAM.MDF/Libary/ChannelBlock.cs | 15 -------- ASAM.MDF/Libary/ChannelCollection.cs | 13 ------- ASAM.MDF/Libary/ChannelConversionBlock.cs | 24 +------------ ASAM.MDF/Libary/ChannelDependencyBlock.cs | 5 +-- ASAM.MDF/Libary/ChannelExtensionBlock.cs | 7 ---- ASAM.MDF/Libary/ChannelGroupBlock.cs | 10 ------ ASAM.MDF/Libary/ChannelGroupCollection.cs | 12 ------- ASAM.MDF/Libary/ConversionData.cs | 6 ---- ASAM.MDF/Libary/DataBlock.cs | 8 ----- ASAM.MDF/Libary/DataGroupBlock.cs | 12 ------- ASAM.MDF/Libary/DataGroupCollection.cs | 13 ------- ASAM.MDF/Libary/DataList.cs | 8 ----- ASAM.MDF/Libary/DataListCollection.cs | 14 -------- ASAM.MDF/Libary/DataZippedBlock.cs | 5 --- ASAM.MDF/Libary/DimBlockSupplement.cs | 4 --- ASAM.MDF/Libary/HeaderBlock.cs | 7 ---- ASAM.MDF/Libary/IdentificationBlock.cs | 7 ---- ASAM.MDF/Libary/Mdf.cs | 10 +----- ASAM.MDF/Libary/TriggerBlock.cs | 8 ----- ASAM.MDF/Libary/VectorCanBlockSupplement.cs | 5 --- 22 files changed, 3 insertions(+), 236 deletions(-) diff --git a/ASAM.MDF.Tests/MdfTests.cs b/ASAM.MDF.Tests/MdfTests.cs index 7a60265..44e4ef0 100644 --- a/ASAM.MDF.Tests/MdfTests.cs +++ b/ASAM.MDF.Tests/MdfTests.cs @@ -13,45 +13,6 @@ [TestFixture] public class MdfTests { - [Test] - public void Test() - { - var filename = "C:\\Users\\Михаил\\Desktop\\Работа\\EU.dat"; - var bytes = File.ReadAllBytes(filename); - var mdf = new Mdf(bytes); - var cloned = mdf.Clone(); - - CheckHashCodes(mdf, cloned); - } - - private void CheckHashCodes(Mdf mdf, Mdf cloned) - { - var listMdf = RecursiveObjectData(mdf); - var listCloned = RecursiveObjectData(cloned); - - - } - private List RecursiveObjectData(object value, List list = null) - { - if (list ==null) - list = new List(); - - var type = value.GetType(); - var fields = type.GetFields(); - var properties = type.GetProperties(); - foreach ( var prop in properties ) - { - list.Add(prop); - list = RecursiveObjectData(prop, list); - } - foreach ( var field in fields) - { - list.Add(field); - list = RecursiveObjectData(field, list); - } - return list; - } - [Test] public void BaseWriteReadTest() { diff --git a/ASAM.MDF/Libary/Block.cs b/ASAM.MDF/Libary/Block.cs index 19c0d4e..c22ad8d 100644 --- a/ASAM.MDF/Libary/Block.cs +++ b/ASAM.MDF/Libary/Block.cs @@ -97,12 +97,5 @@ private void ReadV4() Size = Mdf.ReadU64(); LinksCount = Mdf.ReadU64(); } - - public virtual Block Clone(Mdf mdf) - { - var block = MemberwiseClone() as Block; - block.Mdf = mdf; - return block; - } } } diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index dbdbebb..e2307b9 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -2,7 +2,6 @@ { using System; using System.IO; - using System.Reflection; using System.Text; using ASAM.MDF.Libary.Types; @@ -320,19 +319,5 @@ internal void WriteNextChannelLink(byte[] array, int index, int blockIndex) Array.Copy(bytesNextChannelLink, 0, array, blockIndex + 4, bytesNextChannelLink.Length); } - public override Block Clone(Mdf mdf) - { - var Cb = base.Clone(mdf) as ChannelBlock; - Cb.ChannelConversion = ChannelConversion.Clone(mdf) as ChannelConversionBlock; - Cb.Comment = Comment.Clone(mdf) as TextBlock; - Cb.Dependency = Dependency.Clone(mdf) as ChannelDependencyBlock; - Cb.DisplayName = DisplayName.Clone(mdf) as TextBlock; - Cb.LongSignalName = LongSignalName.Clone(mdf) as TextBlock; - Cb.next = next.Clone(mdf) as ChannelBlock; - Cb.SourceDepending = SourceDepending.Clone(mdf) as ChannelExtensionBlock; - Cb.Unit = Unit.Clone(mdf) as TextBlock; - - return Cb; - } } } diff --git a/ASAM.MDF/Libary/ChannelCollection.cs b/ASAM.MDF/Libary/ChannelCollection.cs index bbfd258..db3d0de 100644 --- a/ASAM.MDF/Libary/ChannelCollection.cs +++ b/ASAM.MDF/Libary/ChannelCollection.cs @@ -101,18 +101,5 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw new NotImplementedException(); } - public ChannelCollection Clone(Mdf mdf) - { - var Cc = MemberwiseClone() as ChannelCollection; - var list = new List(); - - foreach (var item in items) - { - list.Add(item.Clone(mdf) as ChannelBlock); - } - Cc.items = list; - Cc.Mdf = mdf; - return Cc; - } } } diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index 6e10696..ac93ceb 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -3,7 +3,7 @@ using System; using System.Collections.Generic; using System.IO; - using System.Reflection.Metadata.Ecma335; + using ASAM.MDF.Libary.Types; public class ChannelConversionBlock : Block @@ -147,27 +147,5 @@ internal override void Write(byte[] array, ref int index) index += GetSize(); } - public override Block Clone(Mdf mdf) - { - var cc = base.Clone(mdf) as ChannelConversionBlock; - cc.AdditionalConversionData = AdditionalConversionData.Clone(mdf) as ConversionData; - cc.ConversionName = ConversionName.Clone(mdf) as TextBlock; - cc.ConversionUnit = ConversionUnit.Clone(mdf) as TextBlock; - cc.FileComment = FileComment.Clone(mdf) as TextBlock; - var tabT = new List(); - foreach (TextBlock tb in cc.ConvTabT) - { - tabT.Add(tb.Clone(mdf) as TextBlock); - } - cc.ConvTabT = tabT; - var tabTValue = new List(); - foreach (double tb in cc.ConvTabTValue) - { - tabTValue.Add(tb); - } - cc.ConvTabTValue = tabTValue; - - return cc; - } } } diff --git a/ASAM.MDF/Libary/ChannelDependencyBlock.cs b/ASAM.MDF/Libary/ChannelDependencyBlock.cs index 0b866cc..4e4f553 100644 --- a/ASAM.MDF/Libary/ChannelDependencyBlock.cs +++ b/ASAM.MDF/Libary/ChannelDependencyBlock.cs @@ -4,10 +4,7 @@ namespace ASAM.MDF.Libary /// /// TODO: Incomplete /// - public class ChannelDependencyBlock : Block + public class ChannelDependencyBlock// : Block { - public ChannelDependencyBlock(Mdf mdf) : base(mdf) - { - } } } diff --git a/ASAM.MDF/Libary/ChannelExtensionBlock.cs b/ASAM.MDF/Libary/ChannelExtensionBlock.cs index 4cabcf1..a3aef21 100644 --- a/ASAM.MDF/Libary/ChannelExtensionBlock.cs +++ b/ASAM.MDF/Libary/ChannelExtensionBlock.cs @@ -29,12 +29,5 @@ public ChannelExtensionBlock(Mdf mdf, ulong position) VectorCanBlockSupplement = new VectorCanBlockSupplement(mdf); } } - public override Block Clone(Mdf mdf) - { - var cheB = base.Clone(mdf) as ChannelExtensionBlock; - cheB.DimBlockSupplement = DimBlockSupplement.Clone(mdf); - cheB.VectorCanBlockSupplement = VectorCanBlockSupplement.Clone(mdf); - return cheB; - } } } diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index 085da33..ee34958 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -186,15 +186,5 @@ internal void WriteNextChannelGroupBlockLink(byte[] array, int index, int blockI Array.Copy(bytesNextChannelGroupBlockLink, 0, array, blockIndex + 4, bytesNextChannelGroupBlockLink.Length); } - public override Block Clone(Mdf mdf) - { - var cgb = base.Clone(mdf) as ChannelGroupBlock; - cgb.Channels = Channels.Clone(mdf); - cgb.next = next.Clone(mdf) as ChannelGroupBlock; - cgb.Comment = Comment.Clone(mdf) as TextBlock; - cgb.TextName = TextName.Clone(mdf) as TextBlock; - - return cgb; - } } } diff --git a/ASAM.MDF/Libary/ChannelGroupCollection.cs b/ASAM.MDF/Libary/ChannelGroupCollection.cs index 7dac1f3..3825176 100644 --- a/ASAM.MDF/Libary/ChannelGroupCollection.cs +++ b/ASAM.MDF/Libary/ChannelGroupCollection.cs @@ -103,17 +103,5 @@ public ChannelGroupBlock Find(Predicate predicate) { return items.Find(predicate); } - public ChannelGroupCollection Clone(Mdf mdf) - { - var List = new List(); - foreach (ChannelGroupBlock item in items) - { - List.Add(item.Clone(mdf) as ChannelGroupBlock); - } - var cl = MemberwiseClone() as ChannelGroupCollection; - cl.items = List; - cl.Mdf = mdf; - return cl; - } } } diff --git a/ASAM.MDF/Libary/ConversionData.cs b/ASAM.MDF/Libary/ConversionData.cs index 03476aa..b9f6a2a 100644 --- a/ASAM.MDF/Libary/ConversionData.cs +++ b/ASAM.MDF/Libary/ConversionData.cs @@ -127,11 +127,5 @@ public void SetParameters(params double[] parameters) Array.Copy(pBytes, 0, Data, i * 8, pBytes.Length); } } - public object Clone(Mdf mdf) - { - var c = MemberwiseClone() as ConversionData; - c.Parent = Parent.Clone(mdf) as ChannelConversionBlock; - return c; - } } } diff --git a/ASAM.MDF/Libary/DataBlock.cs b/ASAM.MDF/Libary/DataBlock.cs index 6e92693..32047f3 100644 --- a/ASAM.MDF/Libary/DataBlock.cs +++ b/ASAM.MDF/Libary/DataBlock.cs @@ -18,13 +18,5 @@ public static DataBlock Read(Mdf mdf, ulong position) return block; } - public override Block Clone(Mdf mdf) - { - var db = base.Clone(mdf) as DataBlock; - db.DataOfBlock = new byte[DataOfBlock.Length]; - DataOfBlock.CopyTo(db.DataOfBlock, 0); - - return db; - } } } \ No newline at end of file diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index d849711..a08fff7 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -291,17 +291,5 @@ internal void WriteRecords(byte[] array, ref int index, int blockIndex) index += r.Data.Length; } } - public override Block Clone(Mdf mdf) - { - var dt = base.Clone(mdf) as DataGroupBlock; - dt.ChannelGroups = ChannelGroups.Clone(mdf); - dt.nextBlock = nextBlock.Clone(mdf) as DataGroupBlock; - dt.DataListColl = DataListColl.Clone(mdf); - dt.DataZipped=DataZipped.Clone(mdf) as DataZippedBlock; - dt.FileComment = FileComment.Clone(mdf) as TextBlock; - dt.Trigger = Trigger.Clone(mdf) as TriggerBlock; - - return base.Clone(mdf); - } } } diff --git a/ASAM.MDF/Libary/DataGroupCollection.cs b/ASAM.MDF/Libary/DataGroupCollection.cs index f266a3c..5d9e298 100644 --- a/ASAM.MDF/Libary/DataGroupCollection.cs +++ b/ASAM.MDF/Libary/DataGroupCollection.cs @@ -120,18 +120,5 @@ public DataGroupBlock Find(Predicate predicate) { return items.Find(predicate); } - public DataGroupCollection Clone(Mdf mdf) - { - var dC = MemberwiseClone() as DataGroupCollection; - var list = new List(); - - foreach (var item in items) - { - list.Add(item.Clone(mdf) as DataGroupBlock); - } - dC.items = list; - dC.Mdf = mdf; - return dC; - } } } diff --git a/ASAM.MDF/Libary/DataList.cs b/ASAM.MDF/Libary/DataList.cs index f9197a0..53cc039 100644 --- a/ASAM.MDF/Libary/DataList.cs +++ b/ASAM.MDF/Libary/DataList.cs @@ -52,13 +52,5 @@ public static DataList Read(Mdf mdf, ulong position) return block; } - public override Block Clone(Mdf mdf) - { - var dl = base.Clone(mdf) as DataList; - dl.DataBlock = DataBlock.Clone(mdf) as DataBlock; - dl.nextBlock = nextBlock.Clone(mdf) as DataList; - - return dl; - } } } diff --git a/ASAM.MDF/Libary/DataListCollection.cs b/ASAM.MDF/Libary/DataListCollection.cs index 6bba04e..5110cfc 100644 --- a/ASAM.MDF/Libary/DataListCollection.cs +++ b/ASAM.MDF/Libary/DataListCollection.cs @@ -82,19 +82,5 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw new NotImplementedException(); } - public DataListCollection Clone(Mdf mdf) - { - var dlc = MemberwiseClone() as DataListCollection; - var list = new List(); - foreach ( DataList item in this.items ) - { - list.Add(item.Clone(mdf) as DataList); - } - dlc.items = list; - dlc.Mdf = mdf; - dlc.DataGroupBlock = DataGroupBlock.Clone(mdf) as DataGroupBlock; - - return dlc; - } } } \ No newline at end of file diff --git a/ASAM.MDF/Libary/DataZippedBlock.cs b/ASAM.MDF/Libary/DataZippedBlock.cs index 81abe40..a79a975 100644 --- a/ASAM.MDF/Libary/DataZippedBlock.cs +++ b/ASAM.MDF/Libary/DataZippedBlock.cs @@ -65,10 +65,5 @@ private void DecompressData() Mdf.data[k] = trData; } } - public override Block Clone(Mdf mdf) - { - var dz = base.Clone(mdf) as DataZippedBlock; - return dz; - } } } diff --git a/ASAM.MDF/Libary/DimBlockSupplement.cs b/ASAM.MDF/Libary/DimBlockSupplement.cs index 88dce94..a4a4a84 100644 --- a/ASAM.MDF/Libary/DimBlockSupplement.cs +++ b/ASAM.MDF/Libary/DimBlockSupplement.cs @@ -21,9 +21,5 @@ public DimBlockSupplement(Mdf mdf) Description = Encoding.GetEncoding(mdf.IDBlock.CodePage).GetString(data, 6, 80); IdentificationOfEcu = Encoding.GetEncoding(mdf.IDBlock.CodePage).GetString(data, 86, 32); } - public DimBlockSupplement Clone(Mdf mdf) - { - return MemberwiseClone() as DimBlockSupplement; - } } } diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index 5e49556..c30d51f 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -380,12 +380,5 @@ private static void ReadV4(Mdf mdf, HeaderBlock block) mdf.DataGroups.Read(DataGroupBlock.Read(mdf, block.ptrFirstDataGroup)); } } - public override Block Clone(Mdf mdf) - { - var hd = base.Clone(mdf) as HeaderBlock; - hd.FileComment = FileComment?.Clone(mdf) as TextBlock; - hd.ProgramBlock = ProgramBlock?.Clone(mdf) as ProgramBlock; - return hd; - } } } diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index 61d50fd..dce1333 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -238,12 +238,5 @@ private static string GetString(Mdf mdf, ulong count) { return Encoding.UTF8.GetString(mdf.Data, mdf.AdvanceIndex(count), (int)count); } - - public IdentificationBlock Clone(Mdf mdf) - { - var iB = MemberwiseClone() as IdentificationBlock; - iB.Mdf = mdf; - return iB; - } } } diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index 2d563b2..f3bf259 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -15,7 +15,7 @@ public class Mdf public Mdf(byte[] bytes) { data = bytes; - + DataGroups = new DataGroupCollection(this); IDBlock = IdentificationBlock.Read(this); HDBlock = HeaderBlock.Read(this); @@ -96,13 +96,5 @@ internal string GetNameBlock(ulong position) var name = IDBlock.Encoding.GetString(Data, (int)index, 2); return name; } - public Mdf Clone() - { - var cloned = MemberwiseClone() as Mdf; - cloned.HDBlock = HDBlock.Clone(cloned) as HeaderBlock; - cloned.DataGroups = DataGroups.Clone(cloned); - cloned.IDBlock = IDBlock.Clone(cloned); - return cloned; - } } } diff --git a/ASAM.MDF/Libary/TriggerBlock.cs b/ASAM.MDF/Libary/TriggerBlock.cs index bf1bcd2..9a8eb98 100644 --- a/ASAM.MDF/Libary/TriggerBlock.cs +++ b/ASAM.MDF/Libary/TriggerBlock.cs @@ -1,5 +1,4 @@ using System; -using System.Reflection.Metadata.Ecma335; namespace ASAM.MDF.Libary { @@ -15,12 +14,5 @@ public TriggerBlock(Mdf mdf) : base(mdf) { } - public override Block Clone(Mdf mdf) - { - var tr = base.Clone(mdf) as TriggerBlock; - tr.Comment = Comment.Clone(mdf) as TextBlock; - - return tr; - } } } diff --git a/ASAM.MDF/Libary/VectorCanBlockSupplement.cs b/ASAM.MDF/Libary/VectorCanBlockSupplement.cs index b3317c6..18ac2b4 100644 --- a/ASAM.MDF/Libary/VectorCanBlockSupplement.cs +++ b/ASAM.MDF/Libary/VectorCanBlockSupplement.cs @@ -21,10 +21,5 @@ public VectorCanBlockSupplement(Mdf mdf) NameOfMessage = Encoding.GetEncoding(mdf.IDBlock.CodePage).GetString(data, 8, 36); NameOfSender = Encoding.GetEncoding(mdf.IDBlock.CodePage).GetString(data, 44, 36); } - - internal VectorCanBlockSupplement Clone(Mdf mdf) - { - return MemberwiseClone() as VectorCanBlockSupplement; - } } } From 2fea3796ebaaffdc0885d61d3c5b6f8818bafb9e Mon Sep 17 00:00:00 2001 From: Mikhail Date: Mon, 8 Apr 2024 17:03:12 +0300 Subject: [PATCH 13/34] Try update to remove some channels --- ASAM.MDF.sln | 13 ++-- ASAM.MDF/ASAM.MDF.csproj | 2 +- ASAM.MDF/Libary/Block.cs | 7 +- ASAM.MDF/Libary/ChannelBlock.cs | 45 ++++++++---- ASAM.MDF/Libary/ChannelCollection.cs | 7 +- ASAM.MDF/Libary/ChannelConversionBlock.cs | 2 +- ASAM.MDF/Libary/ChannelGroupBlock.cs | 25 ++++--- ASAM.MDF/Libary/Common.cs | 8 ++- ASAM.MDF/Libary/DataGroupBlock.cs | 7 +- ASAM.MDF/Libary/DataList.cs | 7 +- ASAM.MDF/Libary/HeaderBlock.cs | 7 -- ASAM.MDF/Libary/INext.cs | 5 ++ ASAM.MDF/Libary/IdentificationBlock.cs | 5 +- ASAM.MDF/Libary/ProgramBlock.cs | 2 +- ASAM.MDF/Libary/TextBlock.cs | 4 +- DebugOpenFileMdf/DebugOpenFileMdf.csproj | 14 ++++ DebugOpenFileMdf/Program.cs | 86 +++++++++++++++++++++++ 17 files changed, 193 insertions(+), 53 deletions(-) create mode 100644 DebugOpenFileMdf/DebugOpenFileMdf.csproj create mode 100644 DebugOpenFileMdf/Program.cs diff --git a/ASAM.MDF.sln b/ASAM.MDF.sln index 66b0601..ffe36f5 100644 --- a/ASAM.MDF.sln +++ b/ASAM.MDF.sln @@ -1,11 +1,12 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2018 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34622.214 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASAM.MDF", "ASAM.MDF\ASAM.MDF.csproj", "{B48EFA8A-CCE6-486F-BD55-F8B8EB44E8DC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASAM.MDF.Tests", "ASAM.MDF.Tests\ASAM.MDF.Tests.csproj", "{8092B28D-5006-45E1-A489-32AE0FEBEE13}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASAM.MDF.Tests", "ASAM.MDF.Tests\ASAM.MDF.Tests.csproj", "{8092B28D-5006-45E1-A489-32AE0FEBEE13}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DebugOpenFileMdf", "DebugOpenFileMdf\DebugOpenFileMdf.csproj", "{E24CDFA8-18C8-47E5-ACFA-D1829C6A120F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +22,10 @@ Global {8092B28D-5006-45E1-A489-32AE0FEBEE13}.Debug|Any CPU.Build.0 = Debug|Any CPU {8092B28D-5006-45E1-A489-32AE0FEBEE13}.Release|Any CPU.ActiveCfg = Release|Any CPU {8092B28D-5006-45E1-A489-32AE0FEBEE13}.Release|Any CPU.Build.0 = Release|Any CPU + {E24CDFA8-18C8-47E5-ACFA-D1829C6A120F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E24CDFA8-18C8-47E5-ACFA-D1829C6A120F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E24CDFA8-18C8-47E5-ACFA-D1829C6A120F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E24CDFA8-18C8-47E5-ACFA-D1829C6A120F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ASAM.MDF/ASAM.MDF.csproj b/ASAM.MDF/ASAM.MDF.csproj index 507ac53..3c73593 100644 --- a/ASAM.MDF/ASAM.MDF.csproj +++ b/ASAM.MDF/ASAM.MDF.csproj @@ -1,6 +1,6 @@  - net6.0-windows + net6.0 Library false diff --git a/ASAM.MDF/Libary/Block.cs b/ASAM.MDF/Libary/Block.cs index c22ad8d..4c7b92a 100644 --- a/ASAM.MDF/Libary/Block.cs +++ b/ASAM.MDF/Libary/Block.cs @@ -22,14 +22,14 @@ protected Block(Mdf mdf) public string Identifier { get; protected set; } [MdfVersion(400, 0)] public uint Reserved { get; set; } - public ulong Size { get; private set; } + public ulong Size { get; protected set; } [MdfVersion(400, 0)] public ulong LinksCount { get; private set; } public ulong BlockAddress { get; private set; } internal virtual ushort GetSize() { - return 0; + return (ushort)Size; } internal virtual int GetSizeTotal() { @@ -45,9 +45,6 @@ internal void Read() } Identifier = Mdf.GetString(2); // blockaddress = 0 Size = Mdf.ReadU16(); - - if (Size <= 4) - throw new FormatException(); } internal virtual void Write(byte[] array, ref int index) { diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index e2307b9..bbb6bc6 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -2,12 +2,15 @@ { using System; using System.IO; + using System.Linq; using System.Text; using ASAM.MDF.Libary.Types; - public class ChannelBlock : Block, INext + public class ChannelBlock : Block, INext, IPrevious { + public delegate void ChanelHandlerRemovedAddress(ChannelBlock block, byte[] bytes); + private const int MIN_VERSION_LONG_SIGNAL_NAME = 212; private const int MIN_VERSION_DISPLAY_NAME = 300; private const int MIN_VERSION_ADDITIONAL_BYTE_OFFSET = 300; @@ -39,6 +42,7 @@ public class ChannelBlock : Block, INext private ChannelBlock(Mdf mdf) : base(mdf) { } + public event ChanelHandlerRemovedAddress ChanelsRemovedAddress; public ChannelBlock Next { @@ -50,6 +54,8 @@ public ChannelBlock Next return next; } } + public Action Action { get; set; } + public ChannelBlock Previous { get; set; } public ChannelConversionBlock ChannelConversion { get @@ -233,25 +239,36 @@ private static void ReadV4(Mdf mdf, ChannelBlock block) if (block.channelConversion == null && block.ptrChannelConversionBlock != 0) block.ChannelConversion = ChannelConversionBlock.Read(block.Mdf, block.ptrChannelConversionBlock); } + /// + /// Set this address 0 for previous channel. Lost address + /// + /// Copied modified the entire array of mdf bytes + public byte[] Remove() + { + var bytes = new byte[Mdf.Data.Length]; + Array.Copy(Mdf.Data, bytes, Mdf.Data.Length); + + var previous = Previous; + var blockPrevAddress = previous.BlockAddress; + var thisPointer = blockPrevAddress + 4; + + var newbytes = BitConverter.GetBytes((int)ptrNextChannelBlock); + Array.Copy(newbytes, 0, bytes, (int)thisPointer, newbytes.Length); + Array.Copy(new byte[(int)Size - 4 - newbytes.Length], 0, bytes, (int)BlockAddress + 4 + newbytes.Length, (int)Size - 4 - newbytes.Length); + + previous.ptrNextChannelBlock = ptrNextChannelBlock; + previous.next = next; + + ChanelsRemovedAddress?.Invoke(this, bytes); + + return bytes.ToArray(); + } public override string ToString() { return SignalName; } - internal override ushort GetSize() - { - // Base size. - if (Mdf.IDBlock.Version < 212) - return 218; - - // 2.12 - if (Mdf.IDBlock.Version < 300) - return 222; - - // 3.00 - return 228; - } internal override int GetSizeTotal() { var size = base.GetSizeTotal(); diff --git a/ASAM.MDF/Libary/ChannelCollection.cs b/ASAM.MDF/Libary/ChannelCollection.cs index db3d0de..f3be03a 100644 --- a/ASAM.MDF/Libary/ChannelCollection.cs +++ b/ASAM.MDF/Libary/ChannelCollection.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Linq; public class ChannelCollection : IList { @@ -35,9 +36,13 @@ public ChannelBlock this[int index] } } - internal void Read(ChannelBlock cnBlock) + internal void Read(ChannelBlock cnBlock, ChannelBlock.ChanelHandlerRemovedAddress action) { items = Common.BuildBlockList(null, cnBlock); + foreach (var item in items) + { + item.ChanelsRemovedAddress += (ch, bytes) => action(ch, bytes); + } } internal void Write(byte[] array, ref int index) { diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index ac93ceb..97eed22 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -114,7 +114,7 @@ internal static ChannelConversionBlock Read(Mdf mdf, ulong position) internal override ushort GetSize() { - ushort size = 46; + ushort size = (ushort)Size; if (AdditionalConversionData.Data != null) size += (ushort)AdditionalConversionData.Data.Length; diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index ee34958..b91cc72 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -3,7 +3,7 @@ using System; using System.IO; - public class ChannelGroupBlock : Block, INext + public class ChannelGroupBlock : Block, INext, IPrevious { private ulong ptrNextChannelGroup; private ulong ptrFirstChannelBlock; @@ -30,6 +30,7 @@ public ChannelGroupBlock Next } } + public ChannelGroupBlock Previous { get; set; } public ChannelCollection Channels { get; private set; } public TextBlock Comment { get; set; } public ulong RecordID { get; private set; } @@ -90,8 +91,12 @@ internal static ChannelGroupBlock Read(Mdf mdf, ulong position) block.TextName = TextBlock.Read(mdf, block.ptrTextName); if (block.ptrFirstChannelBlock != 0) - block.Channels.Read(ChannelBlock.Read(mdf, block.ptrFirstChannelBlock)); - + { + var chBlock = ChannelBlock.Read(mdf, block.ptrFirstChannelBlock); + chBlock.ChanelsRemovedAddress += (ch, bytes) => block.ChBlock_ChanelsRemovedAddress(ch,bytes); + + block.Channels.Read(chBlock, block.ChBlock_ChanelsRemovedAddress); + } //if (m_ptrFirstSampleReductionBlock != 0) //{ // mdf.Data.Position = m_ptrFirstSampleReductionBlock; @@ -101,6 +106,14 @@ internal static ChannelGroupBlock Read(Mdf mdf, ulong position) return block; } + private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, byte[] bytes) + { + NumChannels -= 1; + var addressNumChannels = BlockAddress + 4 + 4/*ptrNextChannelGroup*/ + 4/*ptrFirstChannelBlock*/ + 4/*ptrTextBlock*/ + 2/*RecordID*/; + var newbytes = BitConverter.GetBytes(NumChannels); + Array.Copy(newbytes, 0, bytes, (int)addressNumChannels, newbytes.Length); + } + private static void ReadV4(Mdf mdf, ChannelGroupBlock block) { block.ptrNextChannelGroup = mdf.ReadU64().ValidateAddress(mdf); @@ -124,13 +137,9 @@ private static void ReadV4(Mdf mdf, ChannelGroupBlock block) block.TextName = TextBlock.Read(mdf, block.ptrTextName); if (block.ptrFirstChannelBlock != 0) - block.Channels.Read(ChannelBlock.Read(mdf, block.ptrFirstChannelBlock)); + block.Channels.Read(ChannelBlock.Read(mdf, block.ptrFirstChannelBlock), null); } - internal override ushort GetSize() - { - return 30; - } internal override int GetSizeTotal() { var size = base.GetSizeTotal(); diff --git a/ASAM.MDF/Libary/Common.cs b/ASAM.MDF/Libary/Common.cs index 0f086fd..06291fc 100644 --- a/ASAM.MDF/Libary/Common.cs +++ b/ASAM.MDF/Libary/Common.cs @@ -1,10 +1,11 @@ namespace ASAM.MDF.Libary { + using System; using System.Collections.Generic; internal static class Common { - internal static List BuildBlockList(List list, T first) where T : INext + internal static List BuildBlockList(List list, T first) where T : INext, IPrevious { if (list == null) { @@ -13,7 +14,12 @@ internal static List BuildBlockList(List list, T first) where T : INext while (current != null) { list.Add(current); + + var prevCurrent = current; current = current.Next; + + if (current != null) + current.Previous = prevCurrent; } } diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index a08fff7..dd88585 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.IO; - public class DataGroupBlock : Block, INext + public class DataGroupBlock : Block, INext, IPrevious { private DataGroupBlock nextBlock; private ulong ptrNextDataGroup; @@ -30,6 +30,7 @@ public DataGroupBlock Next return nextBlock; } } + public DataGroupBlock Previous { get; set; } public ChannelGroupCollection ChannelGroups { get; private set; } public DataListCollection DataListColl { get; private set; } public TriggerBlock Trigger { get; set; } @@ -218,10 +219,6 @@ internal DataRecord[] ReadRecords() return recordsList.ToArray(); } - internal override ushort GetSize() - { - return 28; - } internal override int GetSizeTotal() { var size = base.GetSizeTotal(); diff --git a/ASAM.MDF/Libary/DataList.cs b/ASAM.MDF/Libary/DataList.cs index 53cc039..2fd545c 100644 --- a/ASAM.MDF/Libary/DataList.cs +++ b/ASAM.MDF/Libary/DataList.cs @@ -1,6 +1,8 @@ -namespace ASAM.MDF.Libary +using System; + +namespace ASAM.MDF.Libary { - public class DataList : Block, INext + public class DataList : Block, INext, IPrevious { private ulong ptrNextDL; @@ -26,6 +28,7 @@ public DataList Next return nextBlock; } } + public DataList Previous { get; set; } public DataBlock DataBlock { get; private set; } diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index c30d51f..ea4bdbb 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -273,13 +273,6 @@ internal static HeaderBlock Read(Mdf mdf) } - internal override ushort GetSize() - { - if (Mdf.IDBlock.Version >= 320) - return 208; - - return 164; - } internal override int GetSizeTotal() { return GetSize() + FileComment.GetSizeSafe() + ProgramBlock.GetSizeSafe(); diff --git a/ASAM.MDF/Libary/INext.cs b/ASAM.MDF/Libary/INext.cs index 644d6a6..dcdf505 100644 --- a/ASAM.MDF/Libary/INext.cs +++ b/ASAM.MDF/Libary/INext.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; @@ -10,4 +11,8 @@ public interface INext { T Next { get; } } + public interface IPrevious where T : IPrevious + { + T Previous { get; set; } + } } diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index dce1333..345045e 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -28,6 +28,7 @@ private IdentificationBlock() public Mdf Mdf { get; private set; } public Encoding Encoding { get; private set; } + public int Size { get; set; } /// /// The file identifier always contains "MDF". ("MDF" followed by five spaces) @@ -167,6 +168,7 @@ public static IdentificationBlock Create(Mdf mdf) public static IdentificationBlock Read(Mdf mdf) { var block = new IdentificationBlock(); + var prevPos = mdf.position; block.Mdf = mdf; block.fileIdentifier = GetString(mdf, 8).Humanize(); @@ -178,13 +180,14 @@ public static IdentificationBlock Read(Mdf mdf) block.CodePage = mdf.ReadU16(); block.reserved1 = GetString(mdf, 2).Humanize(); block.reserved2 = GetString(mdf, 30).Humanize(); + block.Size = (int)(mdf.position - prevPos); return block; } internal int GetSize() { - return 64; + return Size; } internal void Write(byte[] array, ref int index) { diff --git a/ASAM.MDF/Libary/ProgramBlock.cs b/ASAM.MDF/Libary/ProgramBlock.cs index ee1c6c8..38840db 100644 --- a/ASAM.MDF/Libary/ProgramBlock.cs +++ b/ASAM.MDF/Libary/ProgramBlock.cs @@ -58,7 +58,7 @@ internal static ProgramBlock Read(Mdf mdf, ulong position) internal override ushort GetSize() { - return (ushort)(4 + Data.Length); + return (ushort)((int)Size + Data.Length); } internal override void Write(byte[] array, ref int index) { diff --git a/ASAM.MDF/Libary/TextBlock.cs b/ASAM.MDF/Libary/TextBlock.cs index 45afbc2..6f601bf 100644 --- a/ASAM.MDF/Libary/TextBlock.cs +++ b/ASAM.MDF/Libary/TextBlock.cs @@ -62,14 +62,14 @@ internal static TextBlock Read(Mdf mdf, ulong position) var block = new TextBlock(mdf); block.Read(); - block.Text = mdf.GetString(block.Size - mdf.position + block.BlockAddress); + block.Text = mdf.GetString(block.Size - (mdf.position - block.BlockAddress)); return block; } internal override ushort GetSize() { - return (ushort)(4 + Text.Length); + return (ushort)((int)Size + Text.Length); } internal override void Write(byte[] array, ref int index) { diff --git a/DebugOpenFileMdf/DebugOpenFileMdf.csproj b/DebugOpenFileMdf/DebugOpenFileMdf.csproj new file mode 100644 index 0000000..3d7adec --- /dev/null +++ b/DebugOpenFileMdf/DebugOpenFileMdf.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0-windows + enable + enable + + + + + + + diff --git a/DebugOpenFileMdf/Program.cs b/DebugOpenFileMdf/Program.cs new file mode 100644 index 0000000..70f2528 --- /dev/null +++ b/DebugOpenFileMdf/Program.cs @@ -0,0 +1,86 @@ +using ASAM.MDF.Libary; +using System.Runtime.InteropServices; + +var filename = ShowDialog(); +try +{ + var bytes = File.ReadAllBytes(filename); + var mdf = new Mdf(bytes); + var dest = mdf.DataGroups[0].ChannelGroups[0].Channels[2]; + var list = new List(); + + + for (int i = 0; i < mdf.DataGroups.Count; i++) + { + var group = mdf.DataGroups[i]; + for (int j = 0; j < group.ChannelGroups.Count; j++) + { + var channelGroup = group.ChannelGroups[j]; + for (int k = 0; k < channelGroup.Channels.Count; k++) + { + var channelBlock = channelGroup.Channels[k]; + list.Add(channelBlock); + if (channelBlock == dest) + { + bytes = channelBlock.Remove(); + } + } + } + } + list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); + var ex = Path.GetExtension(filename); + var file = Path.GetFileNameWithoutExtension(filename) + "Test"; + var path = Path.GetDirectoryName(filename) + "\\" + file + ex; + File.WriteAllBytes(path, bytes); +} +catch (Exception e) +{ + Console.WriteLine(e.Message); + Console.WriteLine(); + throw; +} + +static string ShowDialog() +{ + var ofn = new OpenFileName(); + ofn.lStructSize = Marshal.SizeOf(ofn); + ofn.lpstrFile = new string(new char[256]); + ofn.nMaxFile = ofn.lpstrFile.Length; + ofn.lpstrFileTitle = new string(new char[64]); + ofn.nMaxFileTitle = ofn.lpstrFileTitle.Length; + ofn.lpstrTitle = "Open File Dialog..."; + if (GetOpenFileName(ref ofn)) + return ofn.lpstrFile; + return string.Empty; +} + +[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)] +static extern bool GetOpenFileName(ref OpenFileName ofn); + +[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] +public struct OpenFileName +{ + public int lStructSize; + public IntPtr hwndOwner; + public IntPtr hInstance; + public string lpstrFilter; + public string lpstrCustomFilter; + public int nMaxCustFilter; + public int nFilterIndex; + public string lpstrFile; + public int nMaxFile; + public string lpstrFileTitle; + public int nMaxFileTitle; + public string lpstrInitialDir; + public string lpstrTitle; + public int Flags; + public short nFileOffset; + public short nFileExtension; + public string lpstrDefExt; + public IntPtr lCustData; + public IntPtr lpfnHook; + public string lpTemplateName; + public IntPtr pvReserved; + public int dwReserved; + public int flagsEx; +} \ No newline at end of file From c3bddebac41dcadcb743647f89e1daa18b808818 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Tue, 9 Apr 2024 16:05:20 +0300 Subject: [PATCH 14/34] refactoring and add removing channels --- ASAM.MDF/Libary/Block.cs | 17 +- ASAM.MDF/Libary/ChannelBlock.cs | 250 +++++++++++----------- ASAM.MDF/Libary/ChannelCollection.cs | 23 +- ASAM.MDF/Libary/ChannelConversionBlock.cs | 107 ++++----- ASAM.MDF/Libary/ChannelGroupBlock.cs | 153 ++++++------- ASAM.MDF/Libary/ChannelGroupCollection.cs | 8 +- ASAM.MDF/Libary/Common.cs | 4 +- ASAM.MDF/Libary/DataBlock.cs | 16 +- ASAM.MDF/Libary/DataGroupBlock.cs | 94 ++++---- ASAM.MDF/Libary/DataGroupCollection.cs | 2 +- ASAM.MDF/Libary/DataList.cs | 40 ++-- ASAM.MDF/Libary/DataListCollection.cs | 8 +- ASAM.MDF/Libary/DataZippedBlock.cs | 4 +- ASAM.MDF/Libary/HeaderBlock.cs | 109 +++++----- ASAM.MDF/Libary/INext.cs | 18 -- ASAM.MDF/Libary/IdentificationBlock.cs | 9 +- ASAM.MDF/Libary/Mdf.cs | 11 +- ASAM.MDF/Libary/ProgramBlock.cs | 16 +- ASAM.MDF/Libary/SourceInformation.cs | 44 ++-- ASAM.MDF/Libary/StreamReadHelper.cs | 28 +-- ASAM.MDF/Libary/TextBlock.cs | 6 +- DebugOpenFileMdf/Program.cs | 24 ++- 22 files changed, 517 insertions(+), 474 deletions(-) delete mode 100644 ASAM.MDF/Libary/INext.cs diff --git a/ASAM.MDF/Libary/Block.cs b/ASAM.MDF/Libary/Block.cs index 4c7b92a..7ba90ea 100644 --- a/ASAM.MDF/Libary/Block.cs +++ b/ASAM.MDF/Libary/Block.cs @@ -25,7 +25,7 @@ protected Block(Mdf mdf) public ulong Size { get; protected set; } [MdfVersion(400, 0)] public ulong LinksCount { get; private set; } - public ulong BlockAddress { get; private set; } + public int BlockAddress { get; private set; } internal virtual ushort GetSize() { @@ -35,17 +35,22 @@ internal virtual int GetSizeTotal() { return GetSize(); } - internal void Read() + internal virtual void Read() { BlockAddress = Mdf.position; + if (Mdf.IDBlock.Version >= 400) - { ReadV4(); - return; - } + else + ReadV23(); + } + + internal virtual void ReadV23() + { Identifier = Mdf.GetString(2); // blockaddress = 0 Size = Mdf.ReadU16(); } + internal virtual void Write(byte[] array, ref int index) { var bytesIdentifier = Encoding.UTF8.GetBytes(Identifier); @@ -86,7 +91,7 @@ protected MdfVersionAttribute RequiredVersion(Type type, string property) return (MdfVersionAttribute)Attribute.GetCustomAttribute(type.GetProperty(property), typeof(MdfVersionAttribute)); } - private void ReadV4() + internal virtual void ReadV4() { IdHash = Mdf.ReadU16(); Identifier = Mdf.GetString(2); // blockaddress = 0 diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index bbb6bc6..a0bf856 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -1,13 +1,11 @@ namespace ASAM.MDF.Libary { using System; - using System.IO; using System.Linq; - using System.Text; using ASAM.MDF.Libary.Types; - public class ChannelBlock : Block, INext, IPrevious + public class ChannelBlock : Block, INext, IPrevious, IParent { public delegate void ChanelHandlerRemovedAddress(ChannelBlock block, byte[] bytes); @@ -16,10 +14,6 @@ public class ChannelBlock : Block, INext, IPrevious private const int MIN_VERSION_ADDITIONAL_BYTE_OFFSET = 300; private ulong ptrNextChannelBlock; - - public ulong ConponentAddress { get; private set; } - public ulong TextBlockChanelName { get; private set; } - private ulong ptrChannelConversionBlock; private ulong ptrDataBlockSignal; private ulong ptrUnit; @@ -35,13 +29,14 @@ public class ChannelBlock : Block, INext, IPrevious private ulong ptrDisplayName; private string signalName; private string signalDescription; - + private byte ptrSyncType; + private byte ptrDataType; private ChannelConversionBlock channelConversion; private ChannelBlock next; private ChannelBlock(Mdf mdf) : base(mdf) - { - } + { } + public event ChanelHandlerRemovedAddress ChanelsRemovedAddress; public ChannelBlock Next @@ -49,30 +44,19 @@ public ChannelBlock Next get { if (next == null && ptrNextChannelBlock != 0 && ptrNextChannelBlock < (ulong)Mdf.Data.Length) - next = Read(Mdf, ptrNextChannelBlock); + next = Read(Mdf, (int)ptrNextChannelBlock); return next; } } - public Action Action { get; set; } public ChannelBlock Previous { get; set; } - public ChannelConversionBlock ChannelConversion - { - get - { - return channelConversion; - } - set { channelConversion = value; } - } + public ChannelConversionBlock ChannelConversion { get => channelConversion; set => channelConversion = value; } public ChannelExtensionBlock SourceDepending { get; private set; } public ChannelDependencyBlock Dependency { get; private set; } public TextBlock Comment { get; private set; } public ChannelTypeV3 TypeV3 { get; set; } public ChannelTypeV4 TypeV4 { get; set; } - private byte ptrSyncType; - private byte ptrDataType; - public string SignalName { get { return signalName; } @@ -107,6 +91,9 @@ public string SignalDescription public TextBlock DisplayName { get; private set; } public uint AdditionalByteOffset { get; set; } public TextBlock Unit { get; private set; } + public ulong ConponentAddress { get; private set; } + public ulong TextBlockChanelName { get; private set; } + public ChannelGroupBlock Parent { get; set; } public static ChannelBlock Create(Mdf mdf) { @@ -117,148 +104,169 @@ public static ChannelBlock Create(Mdf mdf) SignalDescription = "", }; } - public static ChannelBlock Read(Mdf mdf, ulong position) + public static ChannelBlock Read(Mdf mdf, int position) { mdf.UpdatePosition(position); var block = new ChannelBlock(mdf); - block.Read(); - block.next = null; block.SourceDepending = null; block.Dependency = null; block.Comment = null; - if (mdf.IDBlock.Version >= 400) - { - ReadV4(mdf, block); - return block; - } - block.ptrNextChannelBlock = mdf.ReadU32().ValidateAddress(mdf); - block.ptrChannelConversionBlock = mdf.ReadU32().ValidateAddress(mdf); - block.ptrChannelExtensionBlock = mdf.ReadU32().ValidateAddress(mdf); - block.ptrChannelDependencyBlock = mdf.ReadU32().ValidateAddress(mdf); - block.ptrChannelComment = mdf.ReadU32().ValidateAddress(mdf); - block.TypeV3 = (ChannelTypeV3)mdf.ReadU16(); - block.SignalName = mdf.GetString(32); - block.SignalDescription = mdf.GetString(128); - block.BitOffset = mdf.ReadU16(); - block.NumberOfBits = mdf.ReadU16(); - block.SignalTypeV3 = (SignalTypeV3)mdf.ReadU16(); - block.ValueRange = mdf.ReadBoolean(); - - if (block.ValueRange) + block.Read(); + return block; + } + internal override void ReadV23() + { + base.ReadV23(); + + ptrNextChannelBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrChannelConversionBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrChannelExtensionBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrChannelDependencyBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrChannelComment = Mdf.ReadU32().ValidateAddress(Mdf); + TypeV3 = (ChannelTypeV3)Mdf.ReadU16(); + SignalName = Mdf.GetString(32); + SignalDescription = Mdf.GetString(128); + BitOffset = Mdf.ReadU16(); + NumberOfBits = Mdf.ReadU16(); + SignalTypeV3 = (SignalTypeV3)Mdf.ReadU16(); + ValueRange = Mdf.ReadBoolean(); + + if (ValueRange) { - block.MinValue = mdf.ReadDouble(); - block.MaxValue = mdf.ReadDouble(); + MinValue = Mdf.ReadDouble(); + MaxValue = Mdf.ReadDouble(); } else { - mdf.UpdatePosition(mdf.position + 16); + Mdf.UpdatePosition(Mdf.position + 16); } + SampleRate = Mdf.ReadDouble(); - block.SampleRate = mdf.ReadDouble(); - - if (mdf.IDBlock.Version >= MIN_VERSION_LONG_SIGNAL_NAME) - block.ptrLongSignalName = mdf.ReadU32().ValidateAddress(mdf); + if (Mdf.IDBlock.Version >= MIN_VERSION_LONG_SIGNAL_NAME) + ptrLongSignalName = Mdf.ReadU32().ValidateAddress(Mdf); - if (mdf.IDBlock.Version >= MIN_VERSION_DISPLAY_NAME) - block.ptrDisplayName = mdf.ReadU32().ValidateAddress(mdf); + if (Mdf.IDBlock.Version >= MIN_VERSION_DISPLAY_NAME) + ptrDisplayName = Mdf.ReadU32().ValidateAddress(Mdf); - if (mdf.IDBlock.Version >= MIN_VERSION_ADDITIONAL_BYTE_OFFSET) - block.AdditionalByteOffset = mdf.ReadU16(); + if (Mdf.IDBlock.Version >= MIN_VERSION_ADDITIONAL_BYTE_OFFSET) + AdditionalByteOffset = Mdf.ReadU16(); - if (block.TextBlockChanelName != 0) - block.LongSignalName = TextBlock.Read(mdf, block.TextBlockChanelName); + if (TextBlockChanelName != 0) + LongSignalName = TextBlock.Read(Mdf, (int)TextBlockChanelName); - if (block.ptrUnit != 0) - block.Unit = TextBlock.Read(mdf, block.ptrUnit); + if (ptrUnit != 0) + Unit = TextBlock.Read(Mdf, (int)ptrUnit); - if (block.ptrTextBlockComment != 0) - block.Comment = TextBlock.Read(mdf, block.ptrTextBlockComment); + if (ptrTextBlockComment != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlockComment); - if (block.ptrLongSignalName != 0) - block.LongSignalName = TextBlock.Read(mdf, block.ptrLongSignalName); + if (ptrLongSignalName != 0) + LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalName); - if (block.channelConversion == null && block.ptrChannelConversionBlock != 0) - block.ChannelConversion = ChannelConversionBlock.Read(block.Mdf, block.ptrChannelConversionBlock); - //if (block.ptrChannelExtensionBlock != 0) + if (channelConversion == null && ptrChannelConversionBlock != 0) + ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlock); + //if (ptrChannelExtensionBlock != 0) //{ - // if (mdf.IDBlock.Version == 400) + // if (Mdf.IDBlock.Version == 400) - // block.SourceDepending = new ChannelExtensionBlock(mdf, block.ptrChannelExtensionBlock); + // SourceDepending = new ChannelExtensionBlock(Mdf, ptrChannelExtensionBlock); //} - - return block; } - private static void ReadV4(Mdf mdf, ChannelBlock block) + internal override void ReadV4() { - block.ptrNextChannelBlock = mdf.ReadU64().ValidateAddress(mdf); - block.ConponentAddress = mdf.ReadU64().ValidateAddress(mdf); - block.TextBlockChanelName = mdf.ReadU64().ValidateAddress(mdf); - block.ptrChannelExtensionBlock = mdf.ReadU64().ValidateAddress(mdf); - block.ptrChannelConversionBlock = mdf.ReadU64().ValidateAddress(mdf); - block.ptrDataBlockSignal = mdf.ReadU64().ValidateAddress(mdf); - block.ptrUnit = mdf.ReadU64().ValidateAddress(mdf); - block.ptrTextBlockComment = mdf.ReadU64().ValidateAddress(mdf); - //block.ptrAttachment = mdf.ReadU64(); - //block.ptrDefaultDGBlock = mdf.ReadU64(); - //block.ptrDefaultCGBlock = mdf.ReadU64(); - //block.ptrDefaultCurrentChanelBlock = mdf.ReadU64(); - block.TypeV4 = (ChannelTypeV4)mdf.ReadByte(); - block.ptrSyncType = mdf.ReadByte(); - block.SignalTypeV4 = (SignalTypeV4)mdf.ReadByte(); - block.BitOffset = mdf.ReadByte(); - block.AdditionalByteOffset = mdf.ReadU32(); - block.NumberOfBits = (ushort)mdf.ReadU32(); - block.ChannelFlags = mdf.ReadU32(); - block.InvalidBitPos = mdf.ReadU32(); - block.Precision = mdf.ReadByte(); - block.Reserved1 = mdf.ReadByte(); - block.AttachmentCount = mdf.ReadU16(); - block.ValRangeMin = mdf.ReadDouble(); - block.ValRangeMax = mdf.ReadDouble(); - block.LimitMin = mdf.ReadDouble(); - block.LimitMax = mdf.ReadDouble(); - block.LimitMinExt = mdf.ReadDouble(); - block.LimitMaxExt = mdf.ReadDouble(); - - if (block.TextBlockChanelName != 0) - block.LongSignalName = TextBlock.Read(mdf, block.TextBlockChanelName); - - if (block.ptrUnit != 0) - block.Unit = TextBlock.Read(mdf, block.ptrUnit); - - if (block.ptrTextBlockComment != 0) - block.Comment = TextBlock.Read(mdf, block.ptrTextBlockComment); - - if (block.ptrLongSignalName != 0) - block.LongSignalName = TextBlock.Read(mdf, block.ptrLongSignalName); - - if (block.channelConversion == null && block.ptrChannelConversionBlock != 0) - block.ChannelConversion = ChannelConversionBlock.Read(block.Mdf, block.ptrChannelConversionBlock); + base.ReadV4(); + + ptrNextChannelBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ConponentAddress = Mdf.ReadU64().ValidateAddress(Mdf); + TextBlockChanelName = Mdf.ReadU64().ValidateAddress(Mdf); + ptrChannelExtensionBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ptrChannelConversionBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ptrDataBlockSignal = Mdf.ReadU64().ValidateAddress(Mdf); + ptrUnit = Mdf.ReadU64().ValidateAddress(Mdf); + ptrTextBlockComment = Mdf.ReadU64().ValidateAddress(Mdf); + //ptrAttachment = Mdf.ReadU64(); + //ptrDefaultDGBlock = Mdf.ReadU64(); + //ptrDefaultCGBlock = Mdf.ReadU64(); + //ptrDefaultCurrentChanelBlock = Mdf.ReadU64(); + TypeV4 = (ChannelTypeV4)Mdf.ReadByte(); + ptrSyncType = Mdf.ReadByte(); + SignalTypeV4 = (SignalTypeV4)Mdf.ReadByte(); + BitOffset = Mdf.ReadByte(); + AdditionalByteOffset = Mdf.ReadU32(); + NumberOfBits = (ushort)Mdf.ReadU32(); + ChannelFlags = Mdf.ReadU32(); + InvalidBitPos = Mdf.ReadU32(); + Precision = Mdf.ReadByte(); + Reserved1 = Mdf.ReadByte(); + AttachmentCount = Mdf.ReadU16(); + ValRangeMin = Mdf.ReadDouble(); + ValRangeMax = Mdf.ReadDouble(); + LimitMin = Mdf.ReadDouble(); + LimitMax = Mdf.ReadDouble(); + LimitMinExt = Mdf.ReadDouble(); + LimitMaxExt = Mdf.ReadDouble(); + + if (TextBlockChanelName != 0) + LongSignalName = TextBlock.Read(Mdf, (int)TextBlockChanelName); + + if (ptrUnit != 0) + Unit = TextBlock.Read(Mdf, (int)ptrUnit); + + if (ptrTextBlockComment != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlockComment); + + if (ptrLongSignalName != 0) + LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalName); + + if (channelConversion == null && ptrChannelConversionBlock != 0) + ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlock); } /// /// Set this address 0 for previous channel. Lost address /// /// Copied modified the entire array of mdf bytes - public byte[] Remove() + public byte[] Remove(byte[] bytes) { - var bytes = new byte[Mdf.Data.Length]; - Array.Copy(Mdf.Data, bytes, Mdf.Data.Length); + if (bytes.Length == 0) + { + bytes = new byte[Mdf.Data.Length]; + Array.Copy(Mdf.Data, bytes, Mdf.Data.Length); + } + if (TypeV3 == ChannelTypeV3.Time) + return bytes; var previous = Previous; + if (previous == null && next != null)// first of list node channels: [X channel]->[1 channel]->[2 channel]->...->[n channel] + { + ChanelsRemovedAddress?.Invoke(this, bytes); + + next.Previous = null; + + return bytes.ToArray(); + } + else if (previous == null && next == null) + { + ChanelsRemovedAddress?.Invoke(this, bytes); + + return bytes.ToArray(); + } var blockPrevAddress = previous.BlockAddress; var thisPointer = blockPrevAddress + 4; var newbytes = BitConverter.GetBytes((int)ptrNextChannelBlock); - Array.Copy(newbytes, 0, bytes, (int)thisPointer, newbytes.Length); - Array.Copy(new byte[(int)Size - 4 - newbytes.Length], 0, bytes, (int)BlockAddress + 4 + newbytes.Length, (int)Size - 4 - newbytes.Length); + Array.Copy(newbytes, 0, bytes, thisPointer, newbytes.Length);//changing the pointer to this block from the previous block, to the next of this block + Array.Copy(new byte[(int)Size - 4 - newbytes.Length], 0, bytes, BlockAddress + 4 + newbytes.Length, (int)Size - 4 - newbytes.Length); //set empty(?) data after address of next block previous.ptrNextChannelBlock = ptrNextChannelBlock; previous.next = next; + if (next != null) + next.Previous = previous; + ChanelsRemovedAddress?.Invoke(this, bytes); return bytes.ToArray(); diff --git a/ASAM.MDF/Libary/ChannelCollection.cs b/ASAM.MDF/Libary/ChannelCollection.cs index f3be03a..64e6ba4 100644 --- a/ASAM.MDF/Libary/ChannelCollection.cs +++ b/ASAM.MDF/Libary/ChannelCollection.cs @@ -8,22 +8,23 @@ public class ChannelCollection : IList { private List items = new List(); - public ChannelCollection(Mdf mdf) + public ChannelCollection(Mdf mdf, ChannelGroupBlock parent) { if (mdf == null) throw new ArgumentNullException("mdf"); - } - public Mdf Mdf { get; private set; } - public int Count - { - get { return items.Count; } + Parent = parent; } + + public Mdf Mdf { get; } + public int Count => items.Count; public bool IsReadOnly { get { throw new NotImplementedException(); } } + ChannelGroupBlock Parent { get; } + public ChannelBlock this[int index] { get @@ -38,11 +39,11 @@ public ChannelBlock this[int index] internal void Read(ChannelBlock cnBlock, ChannelBlock.ChanelHandlerRemovedAddress action) { - items = Common.BuildBlockList(null, cnBlock); - foreach (var item in items) - { - item.ChanelsRemovedAddress += (ch, bytes) => action(ch, bytes); - } + items = Common.BuildBlockList(null, cnBlock, Parent); + + if (action != null) + foreach (var item in items) + item.ChanelsRemovedAddress += (ch, bytes) => action(ch, bytes); } internal void Write(byte[] array, ref int index) { diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index 97eed22..3ad4ad0 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -3,7 +3,7 @@ using System; using System.Collections.Generic; using System.IO; - + using System.Security.Cryptography; using ASAM.MDF.Libary.Types; public class ChannelConversionBlock : Block @@ -51,67 +51,78 @@ public static ChannelConversionBlock Create(Mdf mdf) PhysicalUnit = "", }; } - internal static ChannelConversionBlock Read(Mdf mdf, ulong position) + internal static ChannelConversionBlock Read(Mdf mdf, int position) { mdf.position = position; var block = new ChannelConversionBlock(mdf); + block.Read(); + return block; + } + + internal override void ReadV23() + { + base.ReadV23(); - if (mdf.IDBlock.Version >= 400) + PhysicalValueRangeValid = Mdf.Read16() != 0; + MinPhysicalValue = Mdf.ReadDouble(); + MaxPhysicalValue = Mdf.ReadDouble(); + PhysicalUnit = Mdf.GetString(20); + ConversionType = (ConversionType)Mdf.ReadU16(); + SizeInformation = Mdf.ReadU16(); + + if (SizeInformation > 0) { - - block.TextBlockName = mdf.ReadU64().ValidateAddress(mdf); - block.TextBlockUnit = mdf.ReadU64().ValidateAddress(mdf); - block.ptrFileComment = mdf.ReadU64().ValidateAddress(mdf); - block.InverseConversion = mdf.ReadU64(); - var lastPosAddress = mdf.position; + AdditionalConversionData.Data = new byte[ConversionData.GetEstimatedParametersSize(ConversionType)]; + Array.Copy(Mdf.Data, (int)Mdf.position, AdditionalConversionData.Data, 0, AdditionalConversionData.Data.Length); + } + if (ptrFileComment != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrFileComment); - if (block.LinksCount > 4) - mdf.UpdatePosition(lastPosAddress + (block.LinksCount - 4) * 8); + if (TextBlockName != 0) + ConversionName = TextBlock.Read(Mdf, (int)TextBlockName); - block.ConversionType4 = (ConversionType4)mdf.ReadByte(); - block.Precision = mdf.ReadByte(); - block.Flags = mdf.ReadU16(); - block.SizeInformation = mdf.ReadU16(); - block.ValParamCount = mdf.ReadU16(); - block.MinPhysicalValue = mdf.ReadDouble(); - block.MaxPhysicalValue = mdf.ReadDouble(); + if (TextBlockUnit != 0) + ConversionUnit = TextBlock.Read(Mdf, (int)TextBlockUnit); + } - block.indexPointer = (int)mdf.position; + internal override void ReadV4() + { + base.ReadV4(); - block.AdditionalConversionData.Data = new byte[block.ValParamCount * 8]; + TextBlockName = Mdf.ReadU64().ValidateAddress(Mdf); + TextBlockUnit = Mdf.ReadU64().ValidateAddress(Mdf); + ptrFileComment = Mdf.ReadU64().ValidateAddress(Mdf); + InverseConversion = Mdf.ReadU64(); + var lastPosAddress = Mdf.position; - Array.Copy(mdf.Data, block.indexPointer, block.AdditionalConversionData.Data, 0, block.AdditionalConversionData.Data.Length); - } - else - { - block.PhysicalValueRangeValid = mdf.Read16() != 0; - block.MinPhysicalValue = mdf.ReadDouble(); - block.MaxPhysicalValue = mdf.ReadDouble(); - block.PhysicalUnit = mdf.GetString(20); - block.ConversionType = (ConversionType)mdf.ReadU16(); - block.SizeInformation = mdf.ReadU16(); - - if (block.SizeInformation > 0) - { - block.AdditionalConversionData.Data = new byte[ConversionData.GetEstimatedParametersSize(block.ConversionType)]; - - Array.Copy(mdf.Data, (int)mdf.position, block.AdditionalConversionData.Data, 0, block.AdditionalConversionData.Data.Length); - } - } - if (block.ptrFileComment != 0) - block.FileComment = TextBlock.Read(mdf, block.ptrFileComment); - - if (block.TextBlockName != 0) - block.ConversionName = TextBlock.Read(mdf, block.TextBlockName); - - if (block.TextBlockUnit != 0) - block.ConversionUnit = TextBlock.Read(mdf, block.TextBlockUnit); + if (LinksCount > 4) + Mdf.UpdatePosition(lastPosAddress + ((int)LinksCount - 4) * 8); - return block; - } + ConversionType4 = (ConversionType4)Mdf.ReadByte(); + Precision = Mdf.ReadByte(); + Flags = Mdf.ReadU16(); + SizeInformation = Mdf.ReadU16(); + ValParamCount = Mdf.ReadU16(); + MinPhysicalValue = Mdf.ReadDouble(); + MaxPhysicalValue = Mdf.ReadDouble(); + + indexPointer = (int)Mdf.position; + AdditionalConversionData.Data = new byte[ValParamCount * 8]; + + Array.Copy(Mdf.Data, indexPointer, AdditionalConversionData.Data, 0, AdditionalConversionData.Data.Length); + + if (ptrFileComment != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrFileComment); + + if (TextBlockName != 0) + ConversionName = TextBlock.Read(Mdf, (int)TextBlockName); + + if (TextBlockUnit != 0) + ConversionUnit = TextBlock.Read(Mdf, (int)TextBlockUnit); + } internal override ushort GetSize() { ushort size = (ushort)Size; diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index b91cc72..7996247 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -2,8 +2,9 @@ { using System; using System.IO; + using System.Security.Cryptography; - public class ChannelGroupBlock : Block, INext, IPrevious + public class ChannelGroupBlock : Block, INext, IPrevious, IParent { private ulong ptrNextChannelGroup; private ulong ptrFirstChannelBlock; @@ -17,6 +18,7 @@ public class ChannelGroupBlock : Block, INext, IPrevious.Parent { get; set; } public static ChannelGroupBlock Create(Mdf mdf) { - return new ChannelGroupBlock(mdf) - { - Channels = new ChannelCollection(mdf), - Identifier = "CG", - }; + return new ChannelGroupBlock(mdf) { Identifier = "CG" }; } - internal static ChannelGroupBlock Read(Mdf mdf, ulong position) + internal static ChannelGroupBlock Read(Mdf mdf, int position) { mdf.UpdatePosition(position); var block = new ChannelGroupBlock(mdf); - block.Read(); - block.next = null; - block.Channels = new ChannelCollection(mdf); block.Comment = null; block.SampleReductions = null; - if (mdf.IDBlock.Version >= 400) - { - ReadV4(mdf, block); - return block; - } - block.ptrNextChannelGroup = mdf.ReadU32().ValidateAddress(mdf); - block.ptrFirstChannelBlock = mdf.ReadU32().ValidateAddress(mdf); - block.ptrTextBlock = mdf.ReadU32().ValidateAddress(mdf); - block.RecordID = mdf.ReadU16(); - block.NumChannels = mdf.ReadU16(); - block.RecordSize = mdf.ReadU16(); - block.NumRecords = mdf.ReadU32(); + block.Read(); - if (block.Size >= 26) - block.ptrFirstSampleReductionBlock = mdf.ReadU32(); + return block; + } + internal override void ReadV23() + { + base.ReadV23(); - if (block.ptrTextBlock != 0) - block.Comment = TextBlock.Read(mdf, block.ptrTextBlock); + ptrNextChannelGroup = Mdf.ReadU32().ValidateAddress(Mdf); + ptrFirstChannelBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrTextBlock = Mdf.ReadU32().ValidateAddress(Mdf); + RecordID = Mdf.ReadU16(); + NumChannels = Mdf.ReadU16(); + RecordSize = Mdf.ReadU16(); + NumRecords = Mdf.ReadU32(); - if (block.ptrTextName != 0) - block.TextName = TextBlock.Read(mdf, block.ptrTextName); + if (Size >= 26) + ptrFirstSampleReductionBlock = Mdf.ReadU32(); - if (block.ptrFirstChannelBlock != 0) + if (ptrTextBlock != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlock); + + if (ptrTextName != 0) + TextName = TextBlock.Read(Mdf, (int)ptrTextName); + + if (ptrFirstChannelBlock != 0) { - var chBlock = ChannelBlock.Read(mdf, block.ptrFirstChannelBlock); - chBlock.ChanelsRemovedAddress += (ch, bytes) => block.ChBlock_ChanelsRemovedAddress(ch,bytes); - - block.Channels.Read(chBlock, block.ChBlock_ChanelsRemovedAddress); - } - //if (m_ptrFirstSampleReductionBlock != 0) - //{ - // mdf.Data.Position = m_ptrFirstSampleReductionBlock; - // SampleReductions = new SampleReductionCollection(mdf, new SampleReductionBlock(mdf)); - //} + var chBlock = ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlock); + chBlock.ChanelsRemovedAddress += (ch, bytes) => ChBlock_ChanelsRemovedAddress(ch, bytes); - return block; + Channels.Read(chBlock, ChBlock_ChanelsRemovedAddress); + } + } + internal override void ReadV4() + { + base.ReadV4(); + + ptrNextChannelGroup = Mdf.ReadU64().ValidateAddress(Mdf); + ptrFirstChannelBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ptrTextName = Mdf.ReadU64().ValidateAddress(Mdf); + ptrSourceInfo = Mdf.ReadU64().ValidateAddress(Mdf); + ptrFirstSampleReductionBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ptrTextBlock = Mdf.ReadU64().ValidateAddress(Mdf); + RecordID = Mdf.ReadU64(); + CycleCount = Mdf.ReadU64(); + Flags = Mdf.ReadU16(); + pathSeparator = Mdf.ReadChar(); + Reserved1 = Mdf.ReadU32(); + DataBytes = Mdf.ReadU32(); + InvalidBytes = Mdf.ReadU32(); + + if (ptrTextBlock != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlock); + + if (ptrTextName != 0) + TextName = TextBlock.Read(Mdf, (int)ptrTextName); + + if (ptrFirstChannelBlock != 0) + Channels.Read(ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlock), null); } - private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, byte[] bytes) { + if (Channels[0] == block) //change first channel address on channelGroup + { + var nextBlock = block.Next; + if (nextBlock == null) + ptrFirstChannelBlock = 0; + else + ptrFirstChannelBlock = (ulong)block.Next.BlockAddress; + + var addressOfFirstChannel = BlockAddress + 4 + 4/*ptrNextChannelGroup*/; + var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock); + Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); + } + if (block.Previous == null && block.Next == null) + { + ptrFirstChannelBlock = 0; + var addressOfFirstChannel = BlockAddress + 4 + 4/*ptrNextChannelGroup*/; + var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock); + Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); + } NumChannels -= 1; + var addressNumChannels = BlockAddress + 4 + 4/*ptrNextChannelGroup*/ + 4/*ptrFirstChannelBlock*/ + 4/*ptrTextBlock*/ + 2/*RecordID*/; var newbytes = BitConverter.GetBytes(NumChannels); - Array.Copy(newbytes, 0, bytes, (int)addressNumChannels, newbytes.Length); + Array.Copy(newbytes, 0, bytes, addressNumChannels, newbytes.Length); } - - private static void ReadV4(Mdf mdf, ChannelGroupBlock block) - { - block.ptrNextChannelGroup = mdf.ReadU64().ValidateAddress(mdf); - block.ptrFirstChannelBlock = mdf.ReadU64().ValidateAddress(mdf); - block.ptrTextName = mdf.ReadU64().ValidateAddress(mdf); - block.ptrSourceInfo = mdf.ReadU64().ValidateAddress(mdf); - block.ptrFirstSampleReductionBlock = mdf.ReadU64().ValidateAddress(mdf); - block.ptrTextBlock = mdf.ReadU64().ValidateAddress(mdf); - block.RecordID = mdf.ReadU64(); - block.CycleCount = mdf.ReadU64(); - block.Flags = mdf.ReadU16(); - block.pathSeparator = mdf.ReadChar(); - block.Reserved1 = mdf.ReadU32(); - block.DataBytes = mdf.ReadU32(); - block.InvalidBytes = mdf.ReadU32(); - - if (block.ptrTextBlock != 0) - block.Comment = TextBlock.Read(mdf, block.ptrTextBlock); - - if (block.ptrTextName != 0) - block.TextName = TextBlock.Read(mdf, block.ptrTextName); - - if (block.ptrFirstChannelBlock != 0) - block.Channels.Read(ChannelBlock.Read(mdf, block.ptrFirstChannelBlock), null); - } - internal override int GetSizeTotal() { var size = base.GetSizeTotal(); diff --git a/ASAM.MDF/Libary/ChannelGroupCollection.cs b/ASAM.MDF/Libary/ChannelGroupCollection.cs index 3825176..2164d82 100644 --- a/ASAM.MDF/Libary/ChannelGroupCollection.cs +++ b/ASAM.MDF/Libary/ChannelGroupCollection.cs @@ -7,16 +7,16 @@ public class ChannelGroupCollection : IList { private List items = new List(); - public ChannelGroupCollection(Mdf mdf, DataGroupBlock dataGroupBlock) + public ChannelGroupCollection(Mdf mdf, DataGroupBlock parent) { if (mdf == null) throw new ArgumentNullException("mdf"); Mdf = mdf; - DataGroupBlock = dataGroupBlock; + Parent = parent; } - public DataGroupBlock DataGroupBlock { get; internal set; } + DataGroupBlock Parent { get; } public Mdf Mdf { get; private set; } public int Count { @@ -35,7 +35,7 @@ public ChannelGroupBlock this[int index] internal void Read(ChannelGroupBlock channelGroupBlock) { - items = Common.BuildBlockList(null, channelGroupBlock); + items = Common.BuildBlockList(null, channelGroupBlock, Parent); } internal void Write(byte[] array, ref int index) { diff --git a/ASAM.MDF/Libary/Common.cs b/ASAM.MDF/Libary/Common.cs index 06291fc..2fedfb5 100644 --- a/ASAM.MDF/Libary/Common.cs +++ b/ASAM.MDF/Libary/Common.cs @@ -2,10 +2,11 @@ { using System; using System.Collections.Generic; + using System.Security; internal static class Common { - internal static List BuildBlockList(List list, T first) where T : INext, IPrevious + internal static List BuildBlockList(List list, T first, U parent) where T : INext, IPrevious, IParent { if (list == null) { @@ -13,6 +14,7 @@ internal static List BuildBlockList(List list, T first) where T : INext T current = first; while (current != null) { + current.Parent = parent; list.Add(current); var prevCurrent = current; diff --git a/ASAM.MDF/Libary/DataBlock.cs b/ASAM.MDF/Libary/DataBlock.cs index 32047f3..1b10f77 100644 --- a/ASAM.MDF/Libary/DataBlock.cs +++ b/ASAM.MDF/Libary/DataBlock.cs @@ -1,4 +1,6 @@ -namespace ASAM.MDF.Libary +using System.Security.Cryptography; + +namespace ASAM.MDF.Libary { public class DataBlock : Block { @@ -7,16 +9,20 @@ public DataBlock(Mdf mdf) : base(mdf) public byte[] DataOfBlock { get; private set; } - public static DataBlock Read(Mdf mdf, ulong position) + public static DataBlock Read(Mdf mdf, int position) { mdf.UpdatePosition(position); var block = new DataBlock(mdf); - block.Read(); - - block.DataOfBlock = mdf.ReadBytes((int)block.Size - 24); + block.Read(); return block; } + internal override void ReadV4() + { + base.ReadV4(); + + DataOfBlock = Mdf.ReadBytes((int)Size - 24); + } } } \ No newline at end of file diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index dd88585..3950d4a 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -3,8 +3,9 @@ using System; using System.Collections.Generic; using System.IO; + using System.Security.Cryptography; - public class DataGroupBlock : Block, INext, IPrevious + public class DataGroupBlock : Block, INext, IPrevious, IParent { private DataGroupBlock nextBlock; private ulong ptrNextDataGroup; @@ -25,7 +26,7 @@ public DataGroupBlock Next get { if (nextBlock == null && ptrNextDataGroup != 0) - nextBlock = Read(Mdf, ptrNextDataGroup); + nextBlock = Read(Mdf, (int)ptrNextDataGroup); return nextBlock; } @@ -56,6 +57,7 @@ public DataRecord[] Records public TextBlock FileComment { get; private set; } internal DataZippedBlock DataZipped { get; private set; } + public Mdf Parent { get; set; } //public uint Reserved { get; set; } @@ -66,88 +68,84 @@ public static DataGroupBlock Create(Mdf mdf) Identifier = "DG" }; } - public static DataGroupBlock Read(Mdf mdf, ulong position) + public static DataGroupBlock Read(Mdf mdf, int position) { mdf.UpdatePosition(position); var block = new DataGroupBlock(mdf); - block.Read(); - block.nextBlock = null; block.Trigger = null; block.Reserved = 0; - if (mdf.IDBlock.Version >= 400) - { - ReadV4(mdf, block); - return block; - } + block.Read(); + block.Records = block.ReadRecords(); + + return block; + } + internal override void ReadV23() + { + base.ReadV23(); - block.ptrNextDataGroup = mdf.ReadU32().ValidateAddress(mdf); - block.ptrFirstChannelGroupBlock = mdf.ReadU32().ValidateAddress(mdf); - block.ptrTriggerBlock = mdf.ReadU32().ValidateAddress(mdf); - block.ptrDataBlock = mdf.ReadU32().ValidateAddress(mdf); - block.NumChannelGroups = mdf.ReadU16(); - block.NumRecordIds = mdf.ReadU16(); + ptrNextDataGroup = Mdf.ReadU32().ValidateAddress(Mdf); + ptrFirstChannelGroupBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrTriggerBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrDataBlock = Mdf.ReadU32().ValidateAddress(Mdf); + NumChannelGroups = Mdf.ReadU16(); + NumRecordIds = Mdf.ReadU16(); - if (block.Size >= 24) - block.Reserved = mdf.ReadU32(); + if (Size >= 24) + Reserved = Mdf.ReadU32(); - if (block.ptrTextBlock != 0) - block.FileComment = TextBlock.Read(mdf, block.ptrTextBlock); + if (ptrTextBlock != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock); - if (block.ptrFirstChannelGroupBlock != 0) - block.ChannelGroups.Read(ChannelGroupBlock.Read(mdf, block.ptrFirstChannelGroupBlock)); + if (ptrFirstChannelGroupBlock != 0) + ChannelGroups.Read(ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlock)); /// TODO: Call Trigger Blocks //if (m_ptrTriggerBlock != 0) //{ // Mdf.Data.Position = m_ptrTriggerBlock; - // Trigger = new TriggerBlock(mdf); + // Trigger = new TriggerBlock(Mdf); //} /// TODO: Call ProgramsBlock ? //if (ptrProgramBlock != 0) //{ // Mdf.Data.Position = ptrProgramBlock; - // ProgramBlock = new ProgramBlock(mdf); + // ProgramBlock = new ProgramBlock(Mdf); //} - - block.Records = block.ReadRecords(); - - return block; } - - private static void ReadV4(Mdf mdf, DataGroupBlock block) + internal override void ReadV4() { - block.ptrNextDataGroup = mdf.ReadU64().ValidateAddress(mdf); - block.ptrFirstChannelGroupBlock = mdf.ReadU64().ValidateAddress(mdf); - block.ptrDataBlock = mdf.ReadU64().ValidateAddress(mdf); - block.ptrTextBlock = mdf.ReadU64().ValidateAddress(mdf); - block.NumRecordIds = mdf.ReadByte(); - block.Reserved1 = mdf.ReadByte(); + base.ReadV4(); + + ptrNextDataGroup = Mdf.ReadU64().ValidateAddress(Mdf); + ptrFirstChannelGroupBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ptrDataBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ptrTextBlock = Mdf.ReadU64().ValidateAddress(Mdf); + NumRecordIds = Mdf.ReadByte(); + Reserved1 = Mdf.ReadByte(); - if (block.ptrTextBlock != 0) - block.FileComment = TextBlock.Read(mdf, block.ptrTextBlock); + if (ptrTextBlock != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock); - if (block.ptrFirstChannelGroupBlock != 0) - block.ChannelGroups.Read(ChannelGroupBlock.Read(mdf, block.ptrFirstChannelGroupBlock)); + if (ptrFirstChannelGroupBlock != 0) + ChannelGroups.Read(ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlock)); - if (block.ptrDataBlock != 0) + if (ptrDataBlock != 0) { - var indentificator = mdf.GetNameBlock(block.ptrDataBlock); + var indentificator = Mdf.GetNameBlock((int)ptrDataBlock); if (indentificator == "DZ") - block.DataZipped = DataZippedBlock.Read(mdf, block.ptrDataBlock); + DataZipped = DataZippedBlock.Read(Mdf, (int)ptrDataBlock); if (indentificator == "DL") { - block.DataListColl.Read(DataList.Read(mdf, block.ptrDataBlock)); + DataListColl.Read(DataList.Read(Mdf, (int)ptrDataBlock)); } } - - block.Records = block.ReadRecords(); } internal DataRecord[] ReadRecords() @@ -155,7 +153,7 @@ internal DataRecord[] ReadRecords() var recordsList = new List(); var dataList = new List(); - var indentificator = Mdf.GetNameBlock(ptrDataBlock); + var indentificator = Mdf.GetNameBlock((int)ptrDataBlock); if (indentificator == "DL") { @@ -165,7 +163,7 @@ internal DataRecord[] ReadRecords() } } - Mdf.UpdatePosition(ptrDataBlock); + Mdf.UpdatePosition((int)ptrDataBlock); if (Mdf.IDBlock.Version >= 400) { diff --git a/ASAM.MDF/Libary/DataGroupCollection.cs b/ASAM.MDF/Libary/DataGroupCollection.cs index 5d9e298..6698a33 100644 --- a/ASAM.MDF/Libary/DataGroupCollection.cs +++ b/ASAM.MDF/Libary/DataGroupCollection.cs @@ -27,7 +27,7 @@ public bool IsReadOnly internal void Read(DataGroupBlock block) { - items = Common.BuildBlockList(null, block); + items = Common.BuildBlockList(null, block, Mdf); } internal void Write(byte[] array, ref int index) { diff --git a/ASAM.MDF/Libary/DataList.cs b/ASAM.MDF/Libary/DataList.cs index 2fd545c..5e6288c 100644 --- a/ASAM.MDF/Libary/DataList.cs +++ b/ASAM.MDF/Libary/DataList.cs @@ -1,8 +1,9 @@ using System; +using System.Security.Cryptography; namespace ASAM.MDF.Libary { - public class DataList : Block, INext, IPrevious + public class DataList : Block, INext, IPrevious, IParent { private ulong ptrNextDL; @@ -16,14 +17,14 @@ public class DataList : Block, INext, IPrevious public uint BlockCount { get; private set; } public long BlockOffset { get; private set; } public DataList(Mdf mdf) : base(mdf) - { - } + { } + public DataList Next { get { if (nextBlock == null && ptrNextDL != 0) - nextBlock = Read(Mdf, ptrNextDL); + nextBlock = Read(Mdf, (int)ptrNextDL); return nextBlock; } @@ -31,29 +32,34 @@ public DataList Next public DataList Previous { get; set; } public DataBlock DataBlock { get; private set; } + public DataGroupBlock Parent { get; set; } - public static DataList Read(Mdf mdf, ulong position) + public static DataList Read(Mdf mdf, int position) { mdf.UpdatePosition(position); var block = new DataList(mdf); block.Read(); - block.ptrNextDL = mdf.ReadU64(); - block.DataBlockAddress = mdf.ReadU64(); - block.Flags = (ListFlags)mdf.ReadU16(); - block.Reserved1 = mdf.ReadU16(); - block.BlockCount = mdf.ReadU32(); + return block; + } + internal override void ReadV4() + { + base.ReadV4(); - if (block.Flags == ListFlags.EqualLength) - block.ptrDataBlockLen = mdf.ReadU64(); - else - block.BlockOffset = mdf.Read64(); + ptrNextDL = Mdf.ReadU64(); + DataBlockAddress = Mdf.ReadU64(); + Flags = (ListFlags)Mdf.ReadU16(); + Reserved1 = Mdf.ReadU16(); + BlockCount = Mdf.ReadU32(); - if (block.DataBlockAddress != 0) - block.DataBlock = DataBlock.Read(mdf, block.DataBlockAddress); + if (Flags == ListFlags.EqualLength) + ptrDataBlockLen = Mdf.ReadU64(); + else + BlockOffset = Mdf.Read64(); - return block; + if (DataBlockAddress != 0) + DataBlock = DataBlock.Read(Mdf, (int)DataBlockAddress); } } } diff --git a/ASAM.MDF/Libary/DataListCollection.cs b/ASAM.MDF/Libary/DataListCollection.cs index 5110cfc..7177162 100644 --- a/ASAM.MDF/Libary/DataListCollection.cs +++ b/ASAM.MDF/Libary/DataListCollection.cs @@ -8,17 +8,17 @@ namespace ASAM.MDF.Libary public class DataListCollection : IList { private List items = new List(); - public DataListCollection(Mdf mdf, DataGroupBlock dataGroupBlock) + public DataListCollection(Mdf mdf, DataGroupBlock parent) { if (mdf == null) throw new ArgumentNullException("mdf"); Mdf = mdf; - DataGroupBlock = dataGroupBlock; + Parent = parent; } public Mdf Mdf { get; private set; } - public DataGroupBlock DataGroupBlock { get; internal set; } + DataGroupBlock Parent { get; } public int Count { get { return items.Count; } @@ -36,7 +36,7 @@ public DataList this[int index] internal void Read(DataList datalistBlock) { - items = Common.BuildBlockList(null, datalistBlock); + items = Common.BuildBlockList(null, datalistBlock, Parent); } diff --git a/ASAM.MDF/Libary/DataZippedBlock.cs b/ASAM.MDF/Libary/DataZippedBlock.cs index a79a975..20abfef 100644 --- a/ASAM.MDF/Libary/DataZippedBlock.cs +++ b/ASAM.MDF/Libary/DataZippedBlock.cs @@ -8,7 +8,7 @@ namespace ASAM.MDF.Libary { internal class DataZippedBlock : Block { - private ulong indexStart; // start compressed data index + private int indexStart; // start compressed data index public DataZippedBlock(Mdf mdf) : base(mdf) { } @@ -21,7 +21,7 @@ public DataZippedBlock(Mdf mdf) : base(mdf) public byte ZHeader { get; private set; } public ZCompressionInfo CompressionInfo { get; private set; } - public static DataZippedBlock Read(Mdf mdf, ulong position) + public static DataZippedBlock Read(Mdf mdf, int position) { mdf.UpdatePosition(position); diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index ea4bdbb..8f60658 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -22,8 +22,7 @@ public class HeaderBlock : Block /// The MDF. /// private HeaderBlock(Mdf mdf) : base(mdf) - { - } + { } [MdfVersion(400, 0)] public ulong StartTimeNs { get; set; } @@ -216,63 +215,62 @@ internal static HeaderBlock Create(Mdf mdf) internal static HeaderBlock Read(Mdf mdf) { var block = new HeaderBlock(mdf); + block.Read(); - if (mdf.IDBlock.Version >= 400) - { - ReadV4(mdf, block); - return block; - } + return block; + } + + internal override void ReadV23() + { + base.ReadV23(); - block.ptrFirstDataGroup = mdf.ReadU32().ValidateAddress(mdf); - block.ptrTextBlock = mdf.ReadU32().ValidateAddress(mdf); - block.ptrProgramBlock = mdf.ReadU32().ValidateAddress(mdf); + ptrFirstDataGroup = Mdf.ReadU32().ValidateAddress(Mdf); + ptrTextBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrProgramBlock = Mdf.ReadU32().ValidateAddress(Mdf); - block.DataGroupsCount = mdf.ReadU16(); + DataGroupsCount = Mdf.ReadU16(); - block.Date = mdf.GetString(10); - block.Time = mdf.GetString(8); - block.Author = mdf.GetString(32); - block.Organization = mdf.GetString(32); - block.Project = mdf.GetString(32); - block.Subject = mdf.GetString(32); + Date = Mdf.GetString(10); + Time = Mdf.GetString(8); + Author = Mdf.GetString(32); + Organization = Mdf.GetString(32); + Project = Mdf.GetString(32); + Subject = Mdf.GetString(32); - if (mdf.IDBlock.Version == 320) + if (Mdf.IDBlock.Version == 320) { - block.TimeStamp = mdf.ReadU64(); - block.UTCTimeOffset = mdf.Read16(); - block.TimeQuality = (TimeQuality)mdf.ReadU16(); - block.TimerIdentification = mdf.GetString(32); + TimeStamp = Mdf.ReadU64(); + UTCTimeOffset = Mdf.Read16(); + TimeQuality = (TimeQuality)Mdf.ReadU16(); + TimerIdentification = Mdf.GetString(32); } else { - block.TimeStamp = 0; - block.UTCTimeOffset = 0; - block.TimeQuality = 0; - block.TimerIdentification = ""; + TimeStamp = 0; + UTCTimeOffset = 0; + TimeQuality = 0; + TimerIdentification = ""; } // Check if ptrTextBlock is null - if (block.ptrTextBlock != 0) + if (ptrTextBlock != 0) { - block.FileComment = TextBlock.Read(mdf, block.ptrTextBlock); + FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock); } // Check if ptrProgramBlock is null - if (block.ptrProgramBlock != 0) + if (ptrProgramBlock != 0) { - block.ProgramBlock = ProgramBlock.Read(mdf, block.ptrProgramBlock); + ProgramBlock = ProgramBlock.Read(Mdf, (int)ptrProgramBlock); } // Check if ptrFirstDataGroup is null - if (block.ptrFirstDataGroup != 0) + if (ptrFirstDataGroup != 0) { - mdf.DataGroups.Read(DataGroupBlock.Read(mdf, block.ptrFirstDataGroup)); + Mdf.DataGroups.Read(DataGroupBlock.Read(Mdf, (int)ptrFirstDataGroup)); } - - return block; } - internal override int GetSizeTotal() { return GetSize() + FileComment.GetSizeSafe() + ProgramBlock.GetSizeSafe(); @@ -340,37 +338,38 @@ internal void WriteProgramBlock(byte[] array, ref int index, int baseIndex) ProgramBlock.Write(array, ref index); } - private static void ReadV4(Mdf mdf, HeaderBlock block) + internal override void ReadV4() { + base.ReadV4(); - block.ptrFirstDataGroup = mdf.ReadU64().ValidateAddress(mdf); //Adress DataGroup + ptrFirstDataGroup = Mdf.ReadU64().ValidateAddress(Mdf); //Adress DataGroup //skiped: FileHistoryBlock (not used) +8 //skiped: Chanel... (not used) +8 //skiped: AttachmentBlock (not used) +8 //skiped: EventBlock (not used) +8 - ulong skippedCount = 8 * 4; - mdf.UpdatePosition(mdf.position + skippedCount); - - block.ptrTextBlock = mdf.ReadU64().ValidateAddress(mdf); - block.StartTimeNs = mdf.ReadU64(); - block.TimeZoneOffsetMinutes = mdf.Read16(); - block.DstOffsetMinutes = mdf.Read16(); - block.TimeFlags = mdf.ReadByte(); - block.TimeClass = mdf.ReadByte(); - block.Flags = mdf.ReadByte(); - block.Reserved1 = mdf.ReadByte(); - block.StartAngle = mdf.ReadDouble(); - block.StartDistance = mdf.ReadDouble(); - - if (block.ptrTextBlock != 0) + var skippedCount = 8 * 4; + Mdf.UpdatePosition(Mdf.position + skippedCount); + + ptrTextBlock = Mdf.ReadU64().ValidateAddress(Mdf); + StartTimeNs = Mdf.ReadU64(); + TimeZoneOffsetMinutes = Mdf.Read16(); + DstOffsetMinutes = Mdf.Read16(); + TimeFlags = Mdf.ReadByte(); + TimeClass = Mdf.ReadByte(); + Flags = Mdf.ReadByte(); + Reserved1 = Mdf.ReadByte(); + StartAngle = Mdf.ReadDouble(); + StartDistance = Mdf.ReadDouble(); + + if (ptrTextBlock != 0) { - block.FileComment = TextBlock.Read(mdf, block.ptrTextBlock); + FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock); } // Check if ptrFirstDataGroup is null - if (block.ptrFirstDataGroup != 0) + if (ptrFirstDataGroup != 0) { - mdf.DataGroups.Read(DataGroupBlock.Read(mdf, block.ptrFirstDataGroup)); + Mdf.DataGroups.Read(DataGroupBlock.Read(Mdf, (int)ptrFirstDataGroup)); } } } diff --git a/ASAM.MDF/Libary/INext.cs b/ASAM.MDF/Libary/INext.cs deleted file mode 100644 index dcdf505..0000000 --- a/ASAM.MDF/Libary/INext.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Text; - -namespace ASAM.MDF.Libary -{ - public interface INext - where T : INext - { - T Next { get; } - } - public interface IPrevious where T : IPrevious - { - T Previous { get; set; } - } -} diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index 345045e..a4544d3 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -22,8 +22,7 @@ public class IdentificationBlock private string reserved2; private IdentificationBlock() - { - } + { } public Mdf Mdf { get; private set; } @@ -180,7 +179,7 @@ public static IdentificationBlock Read(Mdf mdf) block.CodePage = mdf.ReadU16(); block.reserved1 = GetString(mdf, 2).Humanize(); block.reserved2 = GetString(mdf, 30).Humanize(); - block.Size = (int)(mdf.position - prevPos); + block.Size = mdf.position - prevPos; return block; } @@ -237,9 +236,9 @@ private void SetStringValue(ref string target, string value, int maxLength) target = value; } } - private static string GetString(Mdf mdf, ulong count) + private static string GetString(Mdf mdf, int count) { - return Encoding.UTF8.GetString(mdf.Data, mdf.AdvanceIndex(count), (int)count); + return Encoding.UTF8.GetString(mdf.Data, mdf.AdvanceIndex(count), count); } } } diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index f3bf259..6daa10f 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -1,11 +1,10 @@ namespace ASAM.MDF.Libary { using System; - using System.IO; public class Mdf { - internal ulong position; + internal int position; internal byte[] data; /// @@ -74,9 +73,9 @@ internal byte[] ReadBytes(int recordSize) { var value = new byte[recordSize]; - Array.Copy(data, (int)position, value, 0, value.Length); + Array.Copy(data, position, value, 0, value.Length); - position += (ulong)value.Length; + position += value.Length; return value; } @@ -90,10 +89,10 @@ internal byte[] ReadBytes(byte[] data, int recordSize, ref int position) return value; } - internal string GetNameBlock(ulong position) + internal string GetNameBlock(int position) { var index = position + 2; - var name = IDBlock.Encoding.GetString(Data, (int)index, 2); + var name = IDBlock.Encoding.GetString(Data, index, 2); return name; } } diff --git a/ASAM.MDF/Libary/ProgramBlock.cs b/ASAM.MDF/Libary/ProgramBlock.cs index 38840db..9484ad2 100644 --- a/ASAM.MDF/Libary/ProgramBlock.cs +++ b/ASAM.MDF/Libary/ProgramBlock.cs @@ -2,14 +2,14 @@ { using System; using System.Linq; + using System.Security.Cryptography; public class ProgramBlock : Block { private byte[] data; private ProgramBlock(Mdf mdf) : base(mdf) - { - } + { } public byte[] Data { @@ -44,17 +44,21 @@ public override string ToString() return "{PRBLOCK: Data[" + Data.Length + "]}"; } - internal static ProgramBlock Read(Mdf mdf, ulong position) + internal static ProgramBlock Read(Mdf mdf, int position) { mdf.UpdatePosition(position); var block = new ProgramBlock(mdf); - block.Read(); - - Array.Copy(mdf.Data, (int)mdf.position, block.Data, 0, (int)block.Size); + block.Read(); return block; } + internal override void ReadV23() + { + base.ReadV23(); + + Array.Copy(Mdf.Data, Mdf.position, Data, 0, (int)Size); + } internal override ushort GetSize() { diff --git a/ASAM.MDF/Libary/SourceInformation.cs b/ASAM.MDF/Libary/SourceInformation.cs index 68a99f6..3c94a63 100644 --- a/ASAM.MDF/Libary/SourceInformation.cs +++ b/ASAM.MDF/Libary/SourceInformation.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ASAM.MDF.Libary +namespace ASAM.MDF.Libary { public class SourceInformation : Block { @@ -23,31 +17,35 @@ public class SourceInformation : Block public SourceInformation(Mdf mdf) : base(mdf) { } - public static SourceInformation Read(Mdf mdf, ulong position) + public static SourceInformation Read(Mdf mdf, int position) { mdf.UpdatePosition(position); var block = new SourceInformation(mdf); - block.Read(); - block.ptrTextName = mdf.ReadU64(); - block.ptrTextPath = mdf.ReadU64(); - block.ptrTextComment = mdf.ReadU64(); - block.SourceType = mdf.ReadByte(); - block.BusType = mdf.ReadByte(); - block.SiFlags = mdf.ReadByte(); - block.Reserved1 = mdf.ReadByte(); + block.Read(); + return block; + } + internal override void ReadV4() + { + base.ReadV4(); - if (block.ptrTextComment != 0) - block.FileComment = TextBlock.Read(mdf, block.ptrTextComment); + ptrTextName = Mdf.ReadU64().ValidateAddress(Mdf); + ptrTextPath = Mdf.ReadU64().ValidateAddress(Mdf); + ptrTextComment = Mdf.ReadU64().ValidateAddress(Mdf); + SourceType = Mdf.ReadByte(); + BusType = Mdf.ReadByte(); + SiFlags = Mdf.ReadByte(); + Reserved1 = Mdf.ReadByte(); - if (block.ptrTextName != 0) - block.TextBlockName = TextBlock.Read(mdf, block.ptrTextName); + if (ptrTextComment != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrTextComment); - if (block.ptrTextPath != 0) - block.TextBlockPath = TextBlock.Read(mdf, block.ptrTextPath); + if (ptrTextName != 0) + TextBlockName = TextBlock.Read(Mdf, (int)ptrTextName); - return block; + if (ptrTextPath != 0) + TextBlockPath = TextBlock.Read(Mdf, (int)ptrTextPath); } } } diff --git a/ASAM.MDF/Libary/StreamReadHelper.cs b/ASAM.MDF/Libary/StreamReadHelper.cs index 92ccd2f..fff4484 100644 --- a/ASAM.MDF/Libary/StreamReadHelper.cs +++ b/ASAM.MDF/Libary/StreamReadHelper.cs @@ -16,7 +16,7 @@ public static byte ReadByte(this Mdf mdf) public static ushort ReadU16(this Mdf mdf) { - var value = BitConverter.ToUInt16(mdf.data, (int)mdf.position); + var value = BitConverter.ToUInt16(mdf.data, mdf.position); mdf.position += 2; @@ -24,7 +24,7 @@ public static ushort ReadU16(this Mdf mdf) } public static uint ReadU32(this Mdf mdf) { - var value = BitConverter.ToUInt32(mdf.data, (int)mdf.position); + var value = BitConverter.ToUInt32(mdf.data, mdf.position); mdf.position += 4; @@ -32,7 +32,7 @@ public static uint ReadU32(this Mdf mdf) } public static ulong ReadU64(this Mdf mdf) { - var value = BitConverter.ToUInt64(mdf.data, (int)mdf.position); + var value = BitConverter.ToUInt64(mdf.data, mdf.position); mdf.position += 8; @@ -40,7 +40,7 @@ public static ulong ReadU64(this Mdf mdf) } public static short Read16(this Mdf mdf) { - var value = BitConverter.ToInt16(mdf.data, (int)mdf.position); + var value = BitConverter.ToInt16(mdf.data, mdf.position); mdf.position += 2; @@ -48,7 +48,7 @@ public static short Read16(this Mdf mdf) } public static int Read32(this Mdf mdf) { - var value = BitConverter.ToInt32(mdf.data, (int)mdf.position); + var value = BitConverter.ToInt32(mdf.data, mdf.position); mdf.position += 4; @@ -56,7 +56,7 @@ public static int Read32(this Mdf mdf) } public static long Read64(this Mdf mdf) { - var value = BitConverter.ToInt64(mdf.data, (int)mdf.position); + var value = BitConverter.ToInt64(mdf.data, mdf.position); mdf.position += 8; @@ -64,7 +64,7 @@ public static long Read64(this Mdf mdf) } public static char ReadChar(this Mdf mdf) { - var value = mdf.IDBlock.Encoding.GetString(mdf.data, (int)mdf.position, 2); + var value = mdf.IDBlock.Encoding.GetString(mdf.data, mdf.position, 2); mdf.position += 2; @@ -74,7 +74,7 @@ public static char ReadChar(this Mdf mdf) public static bool ReadBoolean(this Mdf mdf) { - var value = BitConverter.ToUInt16(mdf.Data, (int)mdf.position); + var value = BitConverter.ToUInt16(mdf.Data, mdf.position); mdf.position += 2; @@ -82,7 +82,7 @@ public static bool ReadBoolean(this Mdf mdf) } public static double ReadDouble(this Mdf mdf) { - var value = BitConverter.ToDouble(mdf.Data, (int)mdf.position); + var value = BitConverter.ToDouble(mdf.Data, mdf.position); mdf.position += 8; @@ -90,22 +90,22 @@ public static double ReadDouble(this Mdf mdf) } - public static void UpdatePosition(this Mdf mdf, ulong address) + public static void UpdatePosition(this Mdf mdf, int address) { mdf.position = address; } - public static int AdvanceIndex(this Mdf mdf, ulong count) + public static int AdvanceIndex(this Mdf mdf, int count) { var index = mdf.position; mdf.position += count; - return (int)index; + return index; } - public static string GetString(this Mdf mdf, ulong count) + public static string GetString(this Mdf mdf, int count) { - var value = mdf.IDBlock.Encoding.GetString(mdf.Data, mdf.AdvanceIndex(count), (int)count); + var value = mdf.IDBlock.Encoding.GetString(mdf.Data, mdf.AdvanceIndex(count), count); var indexLastSymbol = value.IndexOf('\0'); if (indexLastSymbol != -1) value = value.Remove(indexLastSymbol); diff --git a/ASAM.MDF/Libary/TextBlock.cs b/ASAM.MDF/Libary/TextBlock.cs index 6f601bf..f6f5b8e 100644 --- a/ASAM.MDF/Libary/TextBlock.cs +++ b/ASAM.MDF/Libary/TextBlock.cs @@ -52,9 +52,9 @@ public override string ToString() return "{TXBLOCK: " + Text + "}"; } - internal static TextBlock Read(Mdf mdf, ulong position) + internal static TextBlock Read(Mdf mdf, int position) { - if ((ulong)mdf.Data.Length < position) + if (mdf.Data.Length < position) return null; mdf.UpdatePosition(position); @@ -62,7 +62,7 @@ internal static TextBlock Read(Mdf mdf, ulong position) var block = new TextBlock(mdf); block.Read(); - block.Text = mdf.GetString(block.Size - (mdf.position - block.BlockAddress)); + block.Text = mdf.GetString((int)block.Size - (mdf.position - block.BlockAddress)); return block; } diff --git a/DebugOpenFileMdf/Program.cs b/DebugOpenFileMdf/Program.cs index 70f2528..8ec3919 100644 --- a/DebugOpenFileMdf/Program.cs +++ b/DebugOpenFileMdf/Program.cs @@ -5,8 +5,9 @@ try { var bytes = File.ReadAllBytes(filename); + var samebytes = new byte[0]; var mdf = new Mdf(bytes); - var dest = mdf.DataGroups[0].ChannelGroups[0].Channels[2]; + var list = new List(); @@ -20,9 +21,24 @@ { var channelBlock = channelGroup.Channels[k]; list.Add(channelBlock); - if (channelBlock == dest) + } + } + } + list.RemoveAt(0); + for (int i = 0; i < mdf.DataGroups.Count; i++) + { + var group = mdf.DataGroups[i]; + for (int j = 0; j < group.ChannelGroups.Count; j++) + { + var channelGroup = group.ChannelGroups[j]; + for (int k = 0; k < channelGroup.Channels.Count; k++) + { + var channelBlock = channelGroup.Channels[k]; + if (list.Contains(channelBlock)) { - bytes = channelBlock.Remove(); + samebytes = channelBlock.Remove(samebytes); + if (channelBlock.TypeV3 != ASAM.MDF.Libary.Types.ChannelTypeV3.Time) + list.Remove(channelBlock); } } } @@ -31,7 +47,7 @@ var ex = Path.GetExtension(filename); var file = Path.GetFileNameWithoutExtension(filename) + "Test"; var path = Path.GetDirectoryName(filename) + "\\" + file + ex; - File.WriteAllBytes(path, bytes); + File.WriteAllBytes(path, samebytes); } catch (Exception e) { From 4edea274e5b6311942d1bd7c42412c7ede47c1e7 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 10 Apr 2024 09:48:14 +0300 Subject: [PATCH 15/34] Create INext.cs --- ASAM.MDF/Libary/INext.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ASAM.MDF/Libary/INext.cs diff --git a/ASAM.MDF/Libary/INext.cs b/ASAM.MDF/Libary/INext.cs new file mode 100644 index 0000000..01bb1e2 --- /dev/null +++ b/ASAM.MDF/Libary/INext.cs @@ -0,0 +1,15 @@ +namespace ASAM.MDF.Libary +{ + public interface INext where T : INext + { + T Next { get; } + } + public interface IPrevious where T : IPrevious + { + T Previous { get; set; } + } + public interface IParent + { + T Parent { get; set; } + } +} \ No newline at end of file From 3643a5128846d6293a053b5a1b0f499f5b9bd856 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 10 Apr 2024 17:04:12 +0300 Subject: [PATCH 16/34] refactoring --- ASAM.MDF/Libary/ChannelBlock.cs | 218 +++++++++++++++------- ASAM.MDF/Libary/ChannelConversionBlock.cs | 43 ++--- ASAM.MDF/Libary/ChannelGroupBlock.cs | 111 +++++++---- ASAM.MDF/Libary/DataGroupBlock.cs | 114 +++++++---- ASAM.MDF/Libary/DataList.cs | 17 +- ASAM.MDF/Libary/HeaderBlock.cs | 36 ++-- ASAM.MDF/Libary/INext.cs | 6 +- ASAM.MDF/Libary/Mdf.cs | 62 ++++++ ASAM.MDF/Libary/SourceInformation.cs | 32 ++-- DebugOpenFileMdf/Program.cs | 22 +-- 10 files changed, 435 insertions(+), 226 deletions(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index a0bf856..4e1c824 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -1,6 +1,7 @@ namespace ASAM.MDF.Libary { using System; + using System.IO; using System.Linq; using ASAM.MDF.Libary.Types; @@ -13,24 +14,24 @@ public class ChannelBlock : Block, INext, IPrevious, private const int MIN_VERSION_DISPLAY_NAME = 300; private const int MIN_VERSION_ADDITIONAL_BYTE_OFFSET = 300; - private ulong ptrNextChannelBlock; - private ulong ptrChannelConversionBlock; - private ulong ptrDataBlockSignal; - private ulong ptrUnit; - private ulong ptrTextBlockComment; - private ulong ptrAttachment; - private ulong ptrDefaultDGBlock; - private ulong ptrDefaultCGBlock; - private ulong ptrDefaultCurrentChanelBlock; - private ulong ptrChannelExtensionBlock; - private ulong ptrChannelDependencyBlock; - private ulong ptrChannelComment; - private ulong ptrLongSignalName; - private ulong ptrDisplayName; + internal (ulong address, int offset) ptrNextChannelBlock; + internal (ulong address, int offset) ptrChannelConversionBlock; + internal (ulong address, int offset) ptrDataBlockSignal; + internal (ulong address, int offset) ptrUnit; + internal (ulong address, int offset) ptrTextBlockComment; + internal (ulong address, int offset) ptrAttachment; + internal (ulong address, int offset) ptrChannelExtensionBlock; + internal (ulong address, int offset) ptrChannelDependencyBlock; + internal (ulong address, int offset) ptrChannelComment; + internal (ulong address, int offset) ptrLongSignalName; + internal (ulong address, int offset) ptrDisplayName; + internal (ulong address, int offset) ptrComponentAddress; + internal (ulong address, int offset) ptrTextBlockChanelName; + private string signalName; private string signalDescription; - private byte ptrSyncType; - private byte ptrDataType; + private byte SyncType; + private byte DataType; private ChannelConversionBlock channelConversion; private ChannelBlock next; @@ -43,8 +44,8 @@ public ChannelBlock Next { get { - if (next == null && ptrNextChannelBlock != 0 && ptrNextChannelBlock < (ulong)Mdf.Data.Length) - next = Read(Mdf, (int)ptrNextChannelBlock); + if (next == null && ptrNextChannelBlock.address != 0 && ptrNextChannelBlock.address < (ulong)Mdf.Data.Length) + next = Read(Mdf, (int)ptrNextChannelBlock.address); return next; } @@ -91,8 +92,6 @@ public string SignalDescription public TextBlock DisplayName { get; private set; } public uint AdditionalByteOffset { get; set; } public TextBlock Unit { get; private set; } - public ulong ConponentAddress { get; private set; } - public ulong TextBlockChanelName { get; private set; } public ChannelGroupBlock Parent { get; set; } public static ChannelBlock Create(Mdf mdf) @@ -121,11 +120,11 @@ internal override void ReadV23() { base.ReadV23(); - ptrNextChannelBlock = Mdf.ReadU32().ValidateAddress(Mdf); - ptrChannelConversionBlock = Mdf.ReadU32().ValidateAddress(Mdf); - ptrChannelExtensionBlock = Mdf.ReadU32().ValidateAddress(Mdf); - ptrChannelDependencyBlock = Mdf.ReadU32().ValidateAddress(Mdf); - ptrChannelComment = Mdf.ReadU32().ValidateAddress(Mdf); + ptrNextChannelBlock = (Mdf.ReadU32().ValidateAddress(Mdf), 4); + ptrChannelConversionBlock = (Mdf.ReadU32().ValidateAddress(Mdf),ptrNextChannelBlock.offset + 4); + ptrChannelExtensionBlock = (Mdf.ReadU32().ValidateAddress(Mdf),ptrChannelConversionBlock.offset + 4); + ptrChannelDependencyBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelExtensionBlock.offset + 4); + ptrChannelComment = (Mdf.ReadU32().ValidateAddress(Mdf),ptrChannelDependencyBlock.offset + 4); TypeV3 = (ChannelTypeV3)Mdf.ReadU16(); SignalName = Mdf.GetString(32); SignalDescription = Mdf.GetString(128); @@ -145,29 +144,32 @@ internal override void ReadV23() } SampleRate = Mdf.ReadDouble(); + var offset = 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8; if (Mdf.IDBlock.Version >= MIN_VERSION_LONG_SIGNAL_NAME) - ptrLongSignalName = Mdf.ReadU32().ValidateAddress(Mdf); - + { + ptrLongSignalName = (Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelComment.offset + offset); + offset += 8; + } if (Mdf.IDBlock.Version >= MIN_VERSION_DISPLAY_NAME) - ptrDisplayName = Mdf.ReadU32().ValidateAddress(Mdf); + ptrDisplayName = (Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelComment.offset + offset); if (Mdf.IDBlock.Version >= MIN_VERSION_ADDITIONAL_BYTE_OFFSET) AdditionalByteOffset = Mdf.ReadU16(); - if (TextBlockChanelName != 0) - LongSignalName = TextBlock.Read(Mdf, (int)TextBlockChanelName); + if (ptrTextBlockChanelName.address != 0) + LongSignalName = TextBlock.Read(Mdf, (int)ptrTextBlockChanelName.address); - if (ptrUnit != 0) - Unit = TextBlock.Read(Mdf, (int)ptrUnit); + if (ptrUnit.address != 0) + Unit = TextBlock.Read(Mdf, (int)ptrUnit.address); - if (ptrTextBlockComment != 0) - Comment = TextBlock.Read(Mdf, (int)ptrTextBlockComment); + if (ptrTextBlockComment.address != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlockComment.address); - if (ptrLongSignalName != 0) - LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalName); + if (ptrLongSignalName.address != 0) + LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalName.address); - if (channelConversion == null && ptrChannelConversionBlock != 0) - ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlock); + if (channelConversion == null && ptrChannelConversionBlock.address != 0) + ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlock.address); //if (ptrChannelExtensionBlock != 0) //{ // if (Mdf.IDBlock.Version == 400) @@ -180,20 +182,20 @@ internal override void ReadV4() { base.ReadV4(); - ptrNextChannelBlock = Mdf.ReadU64().ValidateAddress(Mdf); - ConponentAddress = Mdf.ReadU64().ValidateAddress(Mdf); - TextBlockChanelName = Mdf.ReadU64().ValidateAddress(Mdf); - ptrChannelExtensionBlock = Mdf.ReadU64().ValidateAddress(Mdf); - ptrChannelConversionBlock = Mdf.ReadU64().ValidateAddress(Mdf); - ptrDataBlockSignal = Mdf.ReadU64().ValidateAddress(Mdf); - ptrUnit = Mdf.ReadU64().ValidateAddress(Mdf); - ptrTextBlockComment = Mdf.ReadU64().ValidateAddress(Mdf); + ptrNextChannelBlock = (Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrComponentAddress = (Mdf.ReadU64().ValidateAddress(Mdf), ptrNextChannelBlock.offset + 8); + ptrTextBlockChanelName = (Mdf.ReadU64().ValidateAddress(Mdf), ptrComponentAddress.offset + 8); + ptrChannelExtensionBlock = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextBlockChanelName.offset + 8); + ptrChannelConversionBlock = (Mdf.ReadU64().ValidateAddress(Mdf), ptrChannelExtensionBlock.offset + 8); + ptrDataBlockSignal = (Mdf.ReadU64().ValidateAddress(Mdf), ptrChannelConversionBlock.offset + 8); + ptrUnit = (Mdf.ReadU64().ValidateAddress(Mdf), ptrDataBlockSignal.offset + 8); + ptrTextBlockComment = (Mdf.ReadU64().ValidateAddress(Mdf), ptrUnit.offset + 8); //ptrAttachment = Mdf.ReadU64(); //ptrDefaultDGBlock = Mdf.ReadU64(); //ptrDefaultCGBlock = Mdf.ReadU64(); //ptrDefaultCurrentChanelBlock = Mdf.ReadU64(); TypeV4 = (ChannelTypeV4)Mdf.ReadByte(); - ptrSyncType = Mdf.ReadByte(); + SyncType = Mdf.ReadByte(); SignalTypeV4 = (SignalTypeV4)Mdf.ReadByte(); BitOffset = Mdf.ReadByte(); AdditionalByteOffset = Mdf.ReadU32(); @@ -210,20 +212,20 @@ internal override void ReadV4() LimitMinExt = Mdf.ReadDouble(); LimitMaxExt = Mdf.ReadDouble(); - if (TextBlockChanelName != 0) - LongSignalName = TextBlock.Read(Mdf, (int)TextBlockChanelName); + if (ptrTextBlockChanelName.address != 0) + LongSignalName = TextBlock.Read(Mdf, (int)ptrTextBlockChanelName.address); - if (ptrUnit != 0) - Unit = TextBlock.Read(Mdf, (int)ptrUnit); + if (ptrUnit.address != 0) + Unit = TextBlock.Read(Mdf, (int)ptrUnit.address); - if (ptrTextBlockComment != 0) - Comment = TextBlock.Read(Mdf, (int)ptrTextBlockComment); + if (ptrTextBlockComment.address != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlockComment.address); - if (ptrLongSignalName != 0) - LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalName); + if (ptrLongSignalName.address != 0) + LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalName.address); - if (channelConversion == null && ptrChannelConversionBlock != 0) - ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlock); + if (channelConversion == null && ptrChannelConversionBlock.address != 0) + ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlock.address); } /// /// Set this address 0 for previous channel. Lost address @@ -231,11 +233,6 @@ internal override void ReadV4() /// Copied modified the entire array of mdf bytes public byte[] Remove(byte[] bytes) { - if (bytes.Length == 0) - { - bytes = new byte[Mdf.Data.Length]; - Array.Copy(Mdf.Data, bytes, Mdf.Data.Length); - } if (TypeV3 == ChannelTypeV3.Time) return bytes; @@ -246,20 +243,20 @@ public byte[] Remove(byte[] bytes) next.Previous = null; - return bytes.ToArray(); + return bytes; } else if (previous == null && next == null) { ChanelsRemovedAddress?.Invoke(this, bytes); - return bytes.ToArray(); + return bytes; } var blockPrevAddress = previous.BlockAddress; var thisPointer = blockPrevAddress + 4; - var newbytes = BitConverter.GetBytes((int)ptrNextChannelBlock); + var newbytes = BitConverter.GetBytes((int)ptrNextChannelBlock.address); Array.Copy(newbytes, 0, bytes, thisPointer, newbytes.Length);//changing the pointer to this block from the previous block, to the next of this block - Array.Copy(new byte[(int)Size - 4 - newbytes.Length], 0, bytes, BlockAddress + 4 + newbytes.Length, (int)Size - 4 - newbytes.Length); //set empty(?) data after address of next block + //Array.Copy(new byte[(int)Size - 4 - newbytes.Length], 0, bytes, BlockAddress + 4 + newbytes.Length, (int)Size - 4 - newbytes.Length); //set empty(?) data after address of next block previous.ptrNextChannelBlock = ptrNextChannelBlock; previous.next = next; @@ -268,8 +265,11 @@ public byte[] Remove(byte[] bytes) next.Previous = previous; ChanelsRemovedAddress?.Invoke(this, bytes); - - return bytes.ToArray(); + + var removedBytes = bytes.Where((x, index) => index < BlockAddress || BlockAddress + (int)Size < index).ToArray(); + Array.Copy(removedBytes, bytes, removedBytes.Length); + Array.Resize(ref bytes, removedBytes.Length); + return bytes; } public override string ToString() @@ -344,5 +344,87 @@ internal void WriteNextChannelLink(byte[] array, int index, int blockIndex) Array.Copy(bytesNextChannelLink, 0, array, blockIndex + 4, bytesNextChannelLink.Length); } + + internal void ChannelUpdateAddress(int indexDeleted, byte[] bytes, ulong countDeleted) + { + if ((int)ptrAttachment.address > indexDeleted) + { + ptrAttachment.address -= countDeleted; + + this.CopyAddress(ptrAttachment, bytes); + } + if ((int)ptrChannelComment.address > indexDeleted) + { + ptrChannelComment.address -= countDeleted; + + this.CopyAddress(ptrChannelComment, bytes); + } + if ((int)ptrChannelConversionBlock.address > indexDeleted) + { + ptrChannelConversionBlock.address -= countDeleted; + + this.CopyAddress(ptrChannelConversionBlock, bytes); + } + if ((int)ptrChannelDependencyBlock.address > indexDeleted) + { + ptrChannelDependencyBlock.address -= countDeleted; + + this.CopyAddress(ptrChannelDependencyBlock, bytes); + } + if ((int)ptrChannelExtensionBlock.address > indexDeleted) + { + ptrChannelExtensionBlock.address -= countDeleted; + + this.CopyAddress(ptrChannelExtensionBlock, bytes); + } + if ((int)ptrComponentAddress.address > indexDeleted) + { + ptrComponentAddress.address -= countDeleted; + + this.CopyAddress(ptrComponentAddress, bytes); + } + if ((int)ptrDataBlockSignal.address > indexDeleted) + { + ptrDataBlockSignal.address -= countDeleted; + + this.CopyAddress(ptrDataBlockSignal, bytes); + } + if ((int)ptrDisplayName.address > indexDeleted) + { + ptrDisplayName.address -= countDeleted; + + this.CopyAddress(ptrDisplayName, bytes); + } + if ((int)ptrLongSignalName.address > indexDeleted) + { + ptrLongSignalName.address -= countDeleted; + + this.CopyAddress(ptrLongSignalName, bytes); + } + if ((int)ptrNextChannelBlock.address > indexDeleted) + { + ptrNextChannelBlock.address -= countDeleted; + + this.CopyAddress(ptrNextChannelBlock, bytes); + } + if ((int)ptrTextBlockChanelName.address > indexDeleted) + { + ptrTextBlockChanelName.address -= countDeleted; + + this.CopyAddress(ptrTextBlockChanelName, bytes); + } + if ((int)ptrTextBlockComment.address > indexDeleted) + { + ptrTextBlockComment.address -= countDeleted; + + this.CopyAddress(ptrTextBlockComment, bytes); + } + if ((int)ptrUnit.address > indexDeleted) + { + ptrUnit.address -= countDeleted; + + this.CopyAddress(ptrUnit, bytes); + } + } } } diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index 3ad4ad0..dd45343 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -8,7 +8,13 @@ public class ChannelConversionBlock : Block { + internal (ulong address, int offset) ptrTextBlockName; + internal (ulong address, int offset) ptrTextBlockUnit; + internal (ulong address, int offset) ptrFileComment; + internal (ulong address, int offset) ptrInverseConversion; + private string physicalUnit; + private int indexPointer; private ChannelConversionBlock(Mdf mdf) : base(mdf) { @@ -30,13 +36,6 @@ public string PhysicalUnit public ushort SizeInformation { get; private set; } public ushort ValParamCount { get; private set; } public ConversionData AdditionalConversionData { get; internal set; } - public ulong TextBlockName { get; private set; } - public ulong TextBlockUnit { get; private set; } - - private ulong ptrFileComment; - private int indexPointer; - - public ulong InverseConversion { get; private set; } public TextBlock FileComment { get; private set; } public TextBlock ConversionUnit { get; private set; } public TextBlock ConversionName { get; private set; } @@ -75,26 +74,18 @@ internal override void ReadV23() if (SizeInformation > 0) { AdditionalConversionData.Data = new byte[ConversionData.GetEstimatedParametersSize(ConversionType)]; - Array.Copy(Mdf.Data, (int)Mdf.position, AdditionalConversionData.Data, 0, AdditionalConversionData.Data.Length); + Array.Copy(Mdf.Data, Mdf.position, AdditionalConversionData.Data, 0, AdditionalConversionData.Data.Length); } - if (ptrFileComment != 0) - FileComment = TextBlock.Read(Mdf, (int)ptrFileComment); - - if (TextBlockName != 0) - ConversionName = TextBlock.Read(Mdf, (int)TextBlockName); - - if (TextBlockUnit != 0) - ConversionUnit = TextBlock.Read(Mdf, (int)TextBlockUnit); } internal override void ReadV4() { base.ReadV4(); - TextBlockName = Mdf.ReadU64().ValidateAddress(Mdf); - TextBlockUnit = Mdf.ReadU64().ValidateAddress(Mdf); - ptrFileComment = Mdf.ReadU64().ValidateAddress(Mdf); - InverseConversion = Mdf.ReadU64(); + ptrTextBlockName = (Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrTextBlockUnit = (Mdf.ReadU64().ValidateAddress(Mdf),ptrTextBlockName.offset + 8); + ptrFileComment = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextBlockUnit.offset + 8); + ptrInverseConversion = (Mdf.ReadU64().ValidateAddress(Mdf), ptrFileComment.offset + 8); var lastPosAddress = Mdf.position; if (LinksCount > 4) @@ -114,14 +105,14 @@ internal override void ReadV4() Array.Copy(Mdf.Data, indexPointer, AdditionalConversionData.Data, 0, AdditionalConversionData.Data.Length); - if (ptrFileComment != 0) - FileComment = TextBlock.Read(Mdf, (int)ptrFileComment); + if (ptrFileComment.address != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrFileComment.address); - if (TextBlockName != 0) - ConversionName = TextBlock.Read(Mdf, (int)TextBlockName); + if (ptrTextBlockName.address != 0) + ConversionName = TextBlock.Read(Mdf, (int)ptrTextBlockName.address); - if (TextBlockUnit != 0) - ConversionUnit = TextBlock.Read(Mdf, (int)TextBlockUnit); + if (ptrTextBlockUnit.address != 0) + ConversionUnit = TextBlock.Read(Mdf, (int)ptrTextBlockUnit.address); } internal override ushort GetSize() { diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index 7996247..46f8521 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -6,14 +6,15 @@ public class ChannelGroupBlock : Block, INext, IPrevious, IParent { - private ulong ptrNextChannelGroup; - private ulong ptrFirstChannelBlock; - private ulong ptrTextName; - private ulong ptrTextBlock; - private ulong ptrSourceInfo; + internal (ulong address, int offset) ptrNextChannelGroup; + internal (ulong address, int offset) ptrFirstChannelBlock; + internal (ulong address, int offset) ptrTextName; + internal (ulong address, int offset) ptrTextBlock; + internal (ulong address, int offset) ptrSourceInfo; + internal (ulong address, int offset) ptrFirstSampleReductionBlock; + private char pathSeparator; - private ulong ptrFirstSampleReductionBlock; private ChannelGroupBlock next; private ChannelGroupBlock(Mdf mdf) : base(mdf) @@ -25,8 +26,8 @@ public ChannelGroupBlock Next { get { - if (next == null && ptrNextChannelGroup != 0) - next = Read(Mdf, (int)ptrNextChannelGroup); + if (next == null && ptrNextChannelGroup.address != 0) + next = Read(Mdf, (int)ptrNextChannelGroup.address); return next; } @@ -70,26 +71,26 @@ internal override void ReadV23() { base.ReadV23(); - ptrNextChannelGroup = Mdf.ReadU32().ValidateAddress(Mdf); - ptrFirstChannelBlock = Mdf.ReadU32().ValidateAddress(Mdf); - ptrTextBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrNextChannelGroup = (Mdf.ReadU32().ValidateAddress(Mdf), 4); + ptrFirstChannelBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrNextChannelGroup.offset + 4); + ptrTextBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstChannelBlock.offset + 4); RecordID = Mdf.ReadU16(); NumChannels = Mdf.ReadU16(); RecordSize = Mdf.ReadU16(); NumRecords = Mdf.ReadU32(); if (Size >= 26) - ptrFirstSampleReductionBlock = Mdf.ReadU32(); + ptrFirstSampleReductionBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrTextBlock.offset + 2 + 2 + 2 + 4); - if (ptrTextBlock != 0) - Comment = TextBlock.Read(Mdf, (int)ptrTextBlock); + if (ptrTextBlock.address != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlock.address); - if (ptrTextName != 0) - TextName = TextBlock.Read(Mdf, (int)ptrTextName); + if (ptrTextName.address != 0) + TextName = TextBlock.Read(Mdf, (int)ptrTextName.address); - if (ptrFirstChannelBlock != 0) + if (ptrFirstChannelBlock.address != 0) { - var chBlock = ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlock); + var chBlock = ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlock.address); chBlock.ChanelsRemovedAddress += (ch, bytes) => ChBlock_ChanelsRemovedAddress(ch, bytes); Channels.Read(chBlock, ChBlock_ChanelsRemovedAddress); @@ -99,12 +100,12 @@ internal override void ReadV4() { base.ReadV4(); - ptrNextChannelGroup = Mdf.ReadU64().ValidateAddress(Mdf); - ptrFirstChannelBlock = Mdf.ReadU64().ValidateAddress(Mdf); - ptrTextName = Mdf.ReadU64().ValidateAddress(Mdf); - ptrSourceInfo = Mdf.ReadU64().ValidateAddress(Mdf); - ptrFirstSampleReductionBlock = Mdf.ReadU64().ValidateAddress(Mdf); - ptrTextBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ptrNextChannelGroup = (Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrFirstChannelBlock = (Mdf.ReadU64().ValidateAddress(Mdf),ptrNextChannelGroup.offset + 8); + ptrTextName = (Mdf.ReadU64().ValidateAddress(Mdf), ptrFirstChannelBlock.offset + 8); + ptrSourceInfo = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextName.offset + 8); + ptrFirstSampleReductionBlock = (Mdf.ReadU64().ValidateAddress(Mdf), ptrSourceInfo.offset + 8); + ptrTextBlock = (Mdf.ReadU64().ValidateAddress(Mdf), ptrFirstSampleReductionBlock.offset + 8); RecordID = Mdf.ReadU64(); CycleCount = Mdf.ReadU64(); Flags = Mdf.ReadU16(); @@ -113,14 +114,14 @@ internal override void ReadV4() DataBytes = Mdf.ReadU32(); InvalidBytes = Mdf.ReadU32(); - if (ptrTextBlock != 0) - Comment = TextBlock.Read(Mdf, (int)ptrTextBlock); + if (ptrTextBlock.address != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlock.address); - if (ptrTextName != 0) - TextName = TextBlock.Read(Mdf, (int)ptrTextName); + if (ptrTextName.address != 0) + TextName = TextBlock.Read(Mdf, (int)ptrTextName.address); - if (ptrFirstChannelBlock != 0) - Channels.Read(ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlock), null); + if (ptrFirstChannelBlock.address != 0) + Channels.Read(ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlock.address), null); } private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, byte[] bytes) { @@ -128,19 +129,19 @@ private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, byte[] bytes) { var nextBlock = block.Next; if (nextBlock == null) - ptrFirstChannelBlock = 0; + ptrFirstChannelBlock.address = 0; else - ptrFirstChannelBlock = (ulong)block.Next.BlockAddress; + ptrFirstChannelBlock.address = (ulong)block.Next.BlockAddress; var addressOfFirstChannel = BlockAddress + 4 + 4/*ptrNextChannelGroup*/; - var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock); + var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock.address); Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); } if (block.Previous == null && block.Next == null) { - ptrFirstChannelBlock = 0; + ptrFirstChannelBlock.address = 0; var addressOfFirstChannel = BlockAddress + 4 + 4/*ptrNextChannelGroup*/; - var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock); + var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock.address); Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); } NumChannels -= 1; @@ -204,5 +205,45 @@ internal void WriteNextChannelGroupBlockLink(byte[] array, int index, int blockI Array.Copy(bytesNextChannelGroupBlockLink, 0, array, blockIndex + 4, bytesNextChannelGroupBlockLink.Length); } + + internal void ChannelGroupUpdateAddress(int indexDeleted, byte[] bytes, ulong countDeleted) + { + if ((int)ptrFirstChannelBlock.address > indexDeleted) + { + ptrFirstChannelBlock.address -= countDeleted; + + this.CopyAddress(ptrFirstChannelBlock, bytes); + } + if ((int)ptrFirstSampleReductionBlock.address > indexDeleted) + { + ptrFirstSampleReductionBlock.address -= countDeleted; + + this.CopyAddress(ptrFirstSampleReductionBlock, bytes); + } + if ((int)ptrNextChannelGroup.address > indexDeleted) + { + ptrNextChannelGroup.address -= countDeleted; + + this.CopyAddress(ptrNextChannelGroup, bytes); + } + if ((int)ptrSourceInfo.address > indexDeleted) + { + ptrSourceInfo.address -= countDeleted; + + this.CopyAddress(ptrSourceInfo, bytes); + } + if ((int)ptrTextBlock.address > indexDeleted) + { + ptrTextBlock.address -= countDeleted; + + this.CopyAddress(ptrTextBlock, bytes); + } + if ((int)ptrTextName.address > indexDeleted) + { + ptrTextName.address -= countDeleted; + + this.CopyAddress(ptrTextName, bytes); + } + } } } diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index 3950d4a..f1d06ed 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -2,18 +2,23 @@ { using System; using System.Collections.Generic; - using System.IO; - using System.Security.Cryptography; public class DataGroupBlock : Block, INext, IPrevious, IParent { - private DataGroupBlock nextBlock; - private ulong ptrNextDataGroup; - private ulong ptrFirstChannelGroupBlock; - private ulong ptrTriggerBlock; - private ulong ptrDataBlock; - private ulong ptrTextBlock; + internal PointerAddress ptrNextDataGroup; + internal PointerAddress ptrFirstChannelGroupBlock; + internal PointerAddress ptrTriggerBlock; + internal PointerAddress ptrDataBlock; + internal PointerAddress ptrTextBlock; + + internal PointerAddress ptrNextDataGroupV4; + internal PointerAddress ptrFirstChannelGroupBlockV4; + internal PointerAddress ptrTriggerBlockV4; + internal PointerAddress ptrDataBlockV4; + internal PointerAddress ptrTextBlockV4; + private DataRecord[] records; + private DataGroupBlock nextBlock; private DataGroupBlock(Mdf mdf) : base(mdf) { @@ -25,8 +30,13 @@ public DataGroupBlock Next { get { - if (nextBlock == null && ptrNextDataGroup != 0) - nextBlock = Read(Mdf, (int)ptrNextDataGroup); + if (Mdf.IDBlock.Version >= 4) + if (nextBlock == null && ptrNextDataGroup.address != 0) + nextBlock = Read(Mdf, (int)ptrNextDataGroup.address); + + if (Mdf.IDBlock.Version < 4) + if (nextBlock == null && ptrNextDataGroupV4.address != 0) + nextBlock = Read(Mdf, (int)ptrNextDataGroupV4.address); return nextBlock; } @@ -86,10 +96,10 @@ internal override void ReadV23() { base.ReadV23(); - ptrNextDataGroup = Mdf.ReadU32().ValidateAddress(Mdf); - ptrFirstChannelGroupBlock = Mdf.ReadU32().ValidateAddress(Mdf); - ptrTriggerBlock = Mdf.ReadU32().ValidateAddress(Mdf); - ptrDataBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrNextDataGroup = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), 4); + ptrFirstChannelGroupBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrNextDataGroup.offset + 4); + ptrTriggerBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstChannelGroupBlock.offset + 4); + ptrDataBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrTriggerBlock.offset + 4); NumChannelGroups = Mdf.ReadU16(); NumRecordIds = Mdf.ReadU16(); @@ -97,11 +107,11 @@ internal override void ReadV23() Reserved = Mdf.ReadU32(); - if (ptrTextBlock != 0) - FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock); + if (ptrTextBlock.address != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock.address); - if (ptrFirstChannelGroupBlock != 0) - ChannelGroups.Read(ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlock)); + if (ptrFirstChannelGroupBlock.address != 0) + ChannelGroups.Read(ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlock.address)); /// TODO: Call Trigger Blocks //if (m_ptrTriggerBlock != 0) @@ -121,29 +131,29 @@ internal override void ReadV4() { base.ReadV4(); - ptrNextDataGroup = Mdf.ReadU64().ValidateAddress(Mdf); - ptrFirstChannelGroupBlock = Mdf.ReadU64().ValidateAddress(Mdf); - ptrDataBlock = Mdf.ReadU64().ValidateAddress(Mdf); - ptrTextBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ptrNextDataGroupV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrFirstChannelGroupBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrNextDataGroupV4.offset + 8); + ptrDataBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf),ptrFirstChannelGroupBlockV4.offset + 8); + ptrTextBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrDataBlockV4.offset + 8); NumRecordIds = Mdf.ReadByte(); Reserved1 = Mdf.ReadByte(); - if (ptrTextBlock != 0) - FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock); + if (ptrTextBlockV4.address != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrTextBlockV4.address); - if (ptrFirstChannelGroupBlock != 0) - ChannelGroups.Read(ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlock)); + if (ptrFirstChannelGroupBlockV4.address != 0) + ChannelGroups.Read(ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlockV4.address)); - if (ptrDataBlock != 0) + if (ptrDataBlockV4.address != 0) { - var indentificator = Mdf.GetNameBlock((int)ptrDataBlock); + var indentificator = Mdf.GetNameBlock((int)ptrDataBlockV4.address); if (indentificator == "DZ") - DataZipped = DataZippedBlock.Read(Mdf, (int)ptrDataBlock); + DataZipped = DataZippedBlock.Read(Mdf, (int)ptrDataBlockV4.address); if (indentificator == "DL") { - DataListColl.Read(DataList.Read(Mdf, (int)ptrDataBlock)); + DataListColl.Read(DataList.Read(Mdf, (int)ptrDataBlockV4.address)); } } } @@ -153,7 +163,10 @@ internal DataRecord[] ReadRecords() var recordsList = new List(); var dataList = new List(); - var indentificator = Mdf.GetNameBlock((int)ptrDataBlock); + var indentificator = Mdf.GetNameBlock((int)ptrDataBlock.address); + + if (Mdf.IDBlock.Version >= 4) + indentificator = Mdf.GetNameBlock((int)ptrDataBlockV4.address); if (indentificator == "DL") { @@ -163,7 +176,10 @@ internal DataRecord[] ReadRecords() } } - Mdf.UpdatePosition((int)ptrDataBlock); + Mdf.UpdatePosition((int)ptrDataBlock.address); + + if (Mdf.IDBlock.Version >= 4) + Mdf.UpdatePosition((int)ptrDataBlockV4.address);// if (Mdf.IDBlock.Version >= 400) { @@ -286,5 +302,39 @@ internal void WriteRecords(byte[] array, ref int index, int blockIndex) index += r.Data.Length; } } + + internal void DataGroupUpdateAddress(int indexDeleted, byte[] bytes, ulong countDeleted) + { + if ((int)ptrDataBlock.address > indexDeleted) + { + ptrDataBlock.address -= countDeleted; + + this.CopyAddress(ptrDataBlock, bytes); + } + if ((int)ptrFirstChannelGroupBlock.address > indexDeleted) + { + ptrFirstChannelGroupBlock.address -= countDeleted; + + this.CopyAddress(ptrFirstChannelGroupBlock, bytes); + } + if ((int)ptrNextDataGroup.address > indexDeleted) + { + ptrNextDataGroup.address -= countDeleted; + + this.CopyAddress(ptrNextDataGroup, bytes); + } + if ((int)ptrTextBlock.address > indexDeleted) + { + ptrTextBlock.address -= countDeleted; + + this.CopyAddress(ptrTextBlock, bytes); + } + if ((int)ptrTriggerBlock.address > indexDeleted) + { + ptrTriggerBlock.address -= countDeleted; + + this.CopyAddress(ptrTriggerBlock, bytes); + } + } } } diff --git a/ASAM.MDF/Libary/DataList.cs b/ASAM.MDF/Libary/DataList.cs index 5e6288c..1cacaa5 100644 --- a/ASAM.MDF/Libary/DataList.cs +++ b/ASAM.MDF/Libary/DataList.cs @@ -5,9 +5,8 @@ namespace ASAM.MDF.Libary { public class DataList : Block, INext, IPrevious, IParent { - private ulong ptrNextDL; - - public ulong DataBlockAddress { get; private set; } + internal (ulong address, int offset) ptrNextDL; + internal (ulong address, int offset) ptrDataBlockAddress; private ulong ptrDataBlockLen; private DataList nextBlock; @@ -23,8 +22,8 @@ public DataList Next { get { - if (nextBlock == null && ptrNextDL != 0) - nextBlock = Read(Mdf, (int)ptrNextDL); + if (nextBlock == null && ptrNextDL.address != 0) + nextBlock = Read(Mdf, (int)ptrNextDL.address); return nextBlock; } @@ -47,8 +46,8 @@ internal override void ReadV4() { base.ReadV4(); - ptrNextDL = Mdf.ReadU64(); - DataBlockAddress = Mdf.ReadU64(); + ptrNextDL = (Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrDataBlockAddress = (Mdf.ReadU64().ValidateAddress(Mdf),ptrNextDL.offset + 8); Flags = (ListFlags)Mdf.ReadU16(); Reserved1 = Mdf.ReadU16(); BlockCount = Mdf.ReadU32(); @@ -58,8 +57,8 @@ internal override void ReadV4() else BlockOffset = Mdf.Read64(); - if (DataBlockAddress != 0) - DataBlock = DataBlock.Read(Mdf, (int)DataBlockAddress); + if (ptrDataBlockAddress.address != 0) + DataBlock = DataBlock.Read(Mdf, (int)ptrDataBlockAddress.address); } } } diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index 8f60658..81f4225 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -12,9 +12,9 @@ public class HeaderBlock : Block private string subject; private string time; private string timerIdentification; - private ulong ptrFirstDataGroup; - private ulong ptrTextBlock; - private uint ptrProgramBlock; + internal (ulong address, int offest) ptrFirstDataGroup; + internal (ulong address, int offest) ptrTextBlock; + internal (ulong address, int offest) ptrProgramBlock; /// /// Initializes a new instance of the class. @@ -224,9 +224,9 @@ internal override void ReadV23() { base.ReadV23(); - ptrFirstDataGroup = Mdf.ReadU32().ValidateAddress(Mdf); - ptrTextBlock = Mdf.ReadU32().ValidateAddress(Mdf); - ptrProgramBlock = Mdf.ReadU32().ValidateAddress(Mdf); + ptrFirstDataGroup = (Mdf.ReadU32().ValidateAddress(Mdf), 4); + ptrTextBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstDataGroup.offest + 4); + ptrProgramBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstDataGroup.offest + 4); DataGroupsCount = Mdf.ReadU16(); @@ -253,21 +253,21 @@ internal override void ReadV23() } // Check if ptrTextBlock is null - if (ptrTextBlock != 0) + if (ptrTextBlock.address != 0) { - FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock); + FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock.address); } // Check if ptrProgramBlock is null - if (ptrProgramBlock != 0) + if (ptrProgramBlock.address != 0) { - ProgramBlock = ProgramBlock.Read(Mdf, (int)ptrProgramBlock); + ProgramBlock = ProgramBlock.Read(Mdf, (int)ptrProgramBlock.address); } // Check if ptrFirstDataGroup is null - if (ptrFirstDataGroup != 0) + if (ptrFirstDataGroup.address != 0) { - Mdf.DataGroups.Read(DataGroupBlock.Read(Mdf, (int)ptrFirstDataGroup)); + Mdf.DataGroups.Read(DataGroupBlock.Read(Mdf, (int)ptrFirstDataGroup.address)); } } @@ -342,7 +342,7 @@ internal override void ReadV4() { base.ReadV4(); - ptrFirstDataGroup = Mdf.ReadU64().ValidateAddress(Mdf); //Adress DataGroup + ptrFirstDataGroup = (Mdf.ReadU64().ValidateAddress(Mdf), 24); //Adress DataGroup //skiped: FileHistoryBlock (not used) +8 //skiped: Chanel... (not used) +8 //skiped: AttachmentBlock (not used) +8 @@ -350,7 +350,7 @@ internal override void ReadV4() var skippedCount = 8 * 4; Mdf.UpdatePosition(Mdf.position + skippedCount); - ptrTextBlock = Mdf.ReadU64().ValidateAddress(Mdf); + ptrTextBlock = (Mdf.ReadU64().ValidateAddress(Mdf), skippedCount + ptrFirstDataGroup.offest); StartTimeNs = Mdf.ReadU64(); TimeZoneOffsetMinutes = Mdf.Read16(); DstOffsetMinutes = Mdf.Read16(); @@ -361,15 +361,15 @@ internal override void ReadV4() StartAngle = Mdf.ReadDouble(); StartDistance = Mdf.ReadDouble(); - if (ptrTextBlock != 0) + if (ptrTextBlock.address != 0) { - FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock); + FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock.address); } // Check if ptrFirstDataGroup is null - if (ptrFirstDataGroup != 0) + if (ptrFirstDataGroup.address != 0) { - Mdf.DataGroups.Read(DataGroupBlock.Read(Mdf, (int)ptrFirstDataGroup)); + Mdf.DataGroups.Read(DataGroupBlock.Read(Mdf, (int)ptrFirstDataGroup.address)); } } } diff --git a/ASAM.MDF/Libary/INext.cs b/ASAM.MDF/Libary/INext.cs index 01bb1e2..c296330 100644 --- a/ASAM.MDF/Libary/INext.cs +++ b/ASAM.MDF/Libary/INext.cs @@ -1,14 +1,14 @@ namespace ASAM.MDF.Libary { - public interface INext where T : INext + internal interface INext where T : INext { T Next { get; } } - public interface IPrevious where T : IPrevious + internal interface IPrevious where T : IPrevious { T Previous { get; set; } } - public interface IParent + internal interface IParent { T Parent { get; set; } } diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index 6daa10f..690b3f6 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -1,6 +1,7 @@ namespace ASAM.MDF.Libary { using System; + using System.Linq; public class Mdf { @@ -33,6 +34,36 @@ public Mdf() internal byte[] Data => data; + public byte[] RemoveChannel(ChannelBlock[] channelBlocks) + { + var bytes = new byte[Data.Length]; + Array.Copy(Data, bytes, Data.Length); + + for (int i = 0; i < DataGroups.Count; i++) + { + var dataGroup = DataGroups[i]; + + for (int j = 0; j < dataGroup.ChannelGroups.Count; j++) + { + var channelGroup = dataGroup.ChannelGroups[j]; + + for (int k = 0; k < channelGroup.Channels.Count; k++) + { + var channel = channelGroup.Channels[k]; + + if (channelBlocks.Contains(channel)) + { + bytes = channel.Remove(bytes); + + UpdateAddresses(bytes, Data, channel.BlockAddress); + } + } + } + } + + return bytes; + } + public byte[] GetBytes() { var array = new byte[GetSize()]; @@ -55,6 +86,37 @@ public byte[] GetBytes() return array; } + /// + /// + /// + /// None deleted data + /// Index start deleted on prevDataBytes + internal void UpdateAddresses(byte[] data, byte[] prevDataBytes, int indexDeleted) + { + var bytes = data; + var countDeleted = (ulong)(prevDataBytes.Length - bytes.Length); + if (countDeleted == 0) + return; + + for (int i = 0; i < DataGroups.Count; i++) + { + var dataGroup = DataGroups[i]; + + dataGroup.DataGroupUpdateAddress(indexDeleted, bytes, countDeleted); + for (int j = 0; j < dataGroup.ChannelGroups.Count; j++) + { + var channelGroup = dataGroup.ChannelGroups[j]; + + channelGroup.ChannelGroupUpdateAddress(indexDeleted, bytes, countDeleted); + for (int k = 0; k < channelGroup.Channels.Count; k++) + { + var channel = channelGroup.Channels[k]; + + channel.ChannelUpdateAddress(indexDeleted, bytes, countDeleted); + } + } + } + } internal int GetSize() { diff --git a/ASAM.MDF/Libary/SourceInformation.cs b/ASAM.MDF/Libary/SourceInformation.cs index 3c94a63..7c539b4 100644 --- a/ASAM.MDF/Libary/SourceInformation.cs +++ b/ASAM.MDF/Libary/SourceInformation.cs @@ -2,9 +2,9 @@ { public class SourceInformation : Block { - private ulong ptrTextName; - private ulong ptrTextPath; - private ulong ptrTextComment; + internal (ulong address, int offset) ptrTextName; + internal (ulong address, int offset) ptrTextPath; + internal (ulong address, int offset) ptrTextComment; public byte SourceType { get; private set; } public byte BusType { get; private set; } @@ -30,22 +30,22 @@ internal override void ReadV4() { base.ReadV4(); - ptrTextName = Mdf.ReadU64().ValidateAddress(Mdf); - ptrTextPath = Mdf.ReadU64().ValidateAddress(Mdf); - ptrTextComment = Mdf.ReadU64().ValidateAddress(Mdf); - SourceType = Mdf.ReadByte(); - BusType = Mdf.ReadByte(); - SiFlags = Mdf.ReadByte(); - Reserved1 = Mdf.ReadByte(); + ptrTextName = (Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrTextPath = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextName.offset + 8); + ptrTextComment = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextPath.offset + 8); + SourceType = Mdf.ReadByte(); + BusType = Mdf.ReadByte(); + SiFlags = Mdf.ReadByte(); + Reserved1 = Mdf.ReadByte(); - if (ptrTextComment != 0) - FileComment = TextBlock.Read(Mdf, (int)ptrTextComment); + if (ptrTextComment.address != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrTextComment.address); - if (ptrTextName != 0) - TextBlockName = TextBlock.Read(Mdf, (int)ptrTextName); + if (ptrTextName.address != 0) + TextBlockName = TextBlock.Read(Mdf, (int)ptrTextName.address); - if (ptrTextPath != 0) - TextBlockPath = TextBlock.Read(Mdf, (int)ptrTextPath); + if (ptrTextPath.address != 0) + TextBlockPath = TextBlock.Read(Mdf, (int)ptrTextPath.address); } } } diff --git a/DebugOpenFileMdf/Program.cs b/DebugOpenFileMdf/Program.cs index 8ec3919..2c38118 100644 --- a/DebugOpenFileMdf/Program.cs +++ b/DebugOpenFileMdf/Program.cs @@ -5,6 +5,7 @@ try { var bytes = File.ReadAllBytes(filename); + //var lists = new List(bytes); var samebytes = new byte[0]; var mdf = new Mdf(bytes); @@ -24,25 +25,8 @@ } } } - list.RemoveAt(0); - for (int i = 0; i < mdf.DataGroups.Count; i++) - { - var group = mdf.DataGroups[i]; - for (int j = 0; j < group.ChannelGroups.Count; j++) - { - var channelGroup = group.ChannelGroups[j]; - for (int k = 0; k < channelGroup.Channels.Count; k++) - { - var channelBlock = channelGroup.Channels[k]; - if (list.Contains(channelBlock)) - { - samebytes = channelBlock.Remove(samebytes); - if (channelBlock.TypeV3 != ASAM.MDF.Libary.Types.ChannelTypeV3.Time) - list.Remove(channelBlock); - } - } - } - } + samebytes = mdf.RemoveChannel(list.ToArray()); + list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); var ex = Path.GetExtension(filename); var file = Path.GetFileNameWithoutExtension(filename) + "Test"; From b5b26ce3781c7b21a65166b82753697e99b41ddc Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 11 Apr 2024 10:53:18 +0300 Subject: [PATCH 17/34] . --- ASAM.MDF/Libary/Extensions.cs | 28 ++++++++++++++++++++++++++++ ASAM.MDF/Libary/PointerAddress.cs | 14 ++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 ASAM.MDF/Libary/Extensions.cs create mode 100644 ASAM.MDF/Libary/PointerAddress.cs diff --git a/ASAM.MDF/Libary/Extensions.cs b/ASAM.MDF/Libary/Extensions.cs new file mode 100644 index 0000000..b8605e2 --- /dev/null +++ b/ASAM.MDF/Libary/Extensions.cs @@ -0,0 +1,28 @@ +using System; + +namespace ASAM.MDF.Libary +{ + internal static class Extensions + { + /// + /// Copy address data to byte massive + /// + /// + /// Block coping + /// pointer to change + /// byte massive for copy + internal static void CopyAddress(this T block, PointerAddress pointerData, byte[] bytes) where T : Block + { + if (pointerData.version < 4) + { + var newbytes = BitConverter.GetBytes(pointerData.AddressV23); + Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); + } + else + { + var newbytes = BitConverter.GetBytes(pointerData.AddressV4); + Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); + } + } + } +} diff --git a/ASAM.MDF/Libary/PointerAddress.cs b/ASAM.MDF/Libary/PointerAddress.cs new file mode 100644 index 0000000..759b8e0 --- /dev/null +++ b/ASAM.MDF/Libary/PointerAddress.cs @@ -0,0 +1,14 @@ +namespace ASAM.MDF.Libary +{ + internal class PointerAddress where T : struct + { + internal readonly int offset; + internal readonly T address; + + internal PointerAddress(T ptr, int offset) + { + this.address = ptr; + this.offset = offset; + } + } +} From b39f28ac99eb4b6f3ccc4440b7860847bfc807db Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 12 Apr 2024 15:54:22 +0300 Subject: [PATCH 18/34] Update pointer --- ASAM.MDF/Libary/ChannelBlock.cs | 284 ++++++++++++---------- ASAM.MDF/Libary/ChannelConversionBlock.cs | 21 +- ASAM.MDF/Libary/ChannelGroupBlock.cs | 185 +++++++++----- ASAM.MDF/Libary/DataGroupBlock.cs | 88 ++++--- ASAM.MDF/Libary/DataList.cs | 16 +- ASAM.MDF/Libary/Extensions.cs | 20 +- ASAM.MDF/Libary/PointerAddress.cs | 4 +- DebugOpenFileMdf/Program.cs | 5 +- 8 files changed, 362 insertions(+), 261 deletions(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 4e1c824..a27a910 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -1,7 +1,7 @@ namespace ASAM.MDF.Libary { using System; - using System.IO; + using System.Collections.Generic; using System.Linq; using ASAM.MDF.Libary.Types; @@ -14,19 +14,36 @@ public class ChannelBlock : Block, INext, IPrevious, private const int MIN_VERSION_DISPLAY_NAME = 300; private const int MIN_VERSION_ADDITIONAL_BYTE_OFFSET = 300; - internal (ulong address, int offset) ptrNextChannelBlock; - internal (ulong address, int offset) ptrChannelConversionBlock; - internal (ulong address, int offset) ptrDataBlockSignal; - internal (ulong address, int offset) ptrUnit; - internal (ulong address, int offset) ptrTextBlockComment; - internal (ulong address, int offset) ptrAttachment; - internal (ulong address, int offset) ptrChannelExtensionBlock; - internal (ulong address, int offset) ptrChannelDependencyBlock; - internal (ulong address, int offset) ptrChannelComment; - internal (ulong address, int offset) ptrLongSignalName; - internal (ulong address, int offset) ptrDisplayName; - internal (ulong address, int offset) ptrComponentAddress; - internal (ulong address, int offset) ptrTextBlockChanelName; + private List> listAddressesV23; + private List> listAddressesV4; + + internal PointerAddress ptrNextChannelBlock; + internal PointerAddress ptrChannelConversionBlock; + internal PointerAddress ptrDataBlockSignal; + internal PointerAddress ptrUnit; + internal PointerAddress ptrTextBlockComment; + internal PointerAddress ptrAttachment; + internal PointerAddress ptrChannelExtensionBlock; + internal PointerAddress ptrChannelDependencyBlock; + internal PointerAddress ptrChannelComment; + internal PointerAddress ptrLongSignalName; + internal PointerAddress ptrDisplayName; + internal PointerAddress ptrComponentAddress; + internal PointerAddress ptrTextBlockChanelName; + + internal PointerAddress ptrNextChannelBlockV4; + internal PointerAddress ptrChannelConversionBlockV4; + internal PointerAddress ptrDataBlockSignalV4; + internal PointerAddress ptrUnitV4; + internal PointerAddress ptrTextBlockCommentV4; + internal PointerAddress ptrAttachmentV4; + internal PointerAddress ptrChannelExtensionBlockV4; + internal PointerAddress ptrChannelDependencyBlockV4; + internal PointerAddress ptrChannelCommentV4; + internal PointerAddress ptrLongSignalNameV4; + internal PointerAddress ptrDisplayNameV4; + internal PointerAddress ptrComponentAddressV4; + internal PointerAddress ptrTextBlockChanelNameV4; private string signalName; private string signalDescription; @@ -44,7 +61,12 @@ public ChannelBlock Next { get { - if (next == null && ptrNextChannelBlock.address != 0 && ptrNextChannelBlock.address < (ulong)Mdf.Data.Length) + if (Mdf.IDBlock.Version >= 400) + { + if (next == null && ptrNextChannelBlockV4 != null && ptrNextChannelBlockV4.address != 0 && ptrNextChannelBlockV4.address < (ulong)Mdf.Data.Length) + next = Read(Mdf, (int)ptrNextChannelBlockV4.address); + } + else if (next == null && ptrNextChannelBlock != null && ptrNextChannelBlock.address != 0 && ptrNextChannelBlock.address < (uint)Mdf.Data.Length) next = Read(Mdf, (int)ptrNextChannelBlock.address); return next; @@ -120,11 +142,13 @@ internal override void ReadV23() { base.ReadV23(); - ptrNextChannelBlock = (Mdf.ReadU32().ValidateAddress(Mdf), 4); - ptrChannelConversionBlock = (Mdf.ReadU32().ValidateAddress(Mdf),ptrNextChannelBlock.offset + 4); - ptrChannelExtensionBlock = (Mdf.ReadU32().ValidateAddress(Mdf),ptrChannelConversionBlock.offset + 4); - ptrChannelDependencyBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelExtensionBlock.offset + 4); - ptrChannelComment = (Mdf.ReadU32().ValidateAddress(Mdf),ptrChannelDependencyBlock.offset + 4); + listAddressesV23 = new List>(); + + ptrNextChannelBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), 4); + ptrChannelConversionBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrNextChannelBlock.offset + 4); + ptrChannelExtensionBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelConversionBlock.offset + 4); + ptrChannelDependencyBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelExtensionBlock.offset + 4); + ptrChannelComment = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelDependencyBlock.offset + 4); TypeV3 = (ChannelTypeV3)Mdf.ReadU16(); SignalName = Mdf.GetString(32); SignalDescription = Mdf.GetString(128); @@ -133,6 +157,15 @@ internal override void ReadV23() SignalTypeV3 = (SignalTypeV3)Mdf.ReadU16(); ValueRange = Mdf.ReadBoolean(); + listAddressesV23.AddRange(new PointerAddress[] + { + ptrNextChannelBlock, + ptrChannelConversionBlock, + ptrChannelExtensionBlock, + ptrChannelDependencyBlock, + ptrChannelComment + }); + if (ValueRange) { MinValue = Mdf.ReadDouble(); @@ -147,25 +180,20 @@ internal override void ReadV23() var offset = 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8; if (Mdf.IDBlock.Version >= MIN_VERSION_LONG_SIGNAL_NAME) { - ptrLongSignalName = (Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelComment.offset + offset); - offset += 8; + ptrLongSignalName = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelComment.offset + offset); + offset += 4; + listAddressesV23.Add(ptrLongSignalName); + } if (Mdf.IDBlock.Version >= MIN_VERSION_DISPLAY_NAME) - ptrDisplayName = (Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelComment.offset + offset); - + { + ptrDisplayName = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelComment.offset + offset); + listAddressesV23.Add(ptrDisplayName); + } if (Mdf.IDBlock.Version >= MIN_VERSION_ADDITIONAL_BYTE_OFFSET) AdditionalByteOffset = Mdf.ReadU16(); - if (ptrTextBlockChanelName.address != 0) - LongSignalName = TextBlock.Read(Mdf, (int)ptrTextBlockChanelName.address); - - if (ptrUnit.address != 0) - Unit = TextBlock.Read(Mdf, (int)ptrUnit.address); - - if (ptrTextBlockComment.address != 0) - Comment = TextBlock.Read(Mdf, (int)ptrTextBlockComment.address); - - if (ptrLongSignalName.address != 0) + if (ptrLongSignalName != null && ptrLongSignalName.address != 0) LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalName.address); if (channelConversion == null && ptrChannelConversionBlock.address != 0) @@ -182,14 +210,16 @@ internal override void ReadV4() { base.ReadV4(); - ptrNextChannelBlock = (Mdf.ReadU64().ValidateAddress(Mdf), 24); - ptrComponentAddress = (Mdf.ReadU64().ValidateAddress(Mdf), ptrNextChannelBlock.offset + 8); - ptrTextBlockChanelName = (Mdf.ReadU64().ValidateAddress(Mdf), ptrComponentAddress.offset + 8); - ptrChannelExtensionBlock = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextBlockChanelName.offset + 8); - ptrChannelConversionBlock = (Mdf.ReadU64().ValidateAddress(Mdf), ptrChannelExtensionBlock.offset + 8); - ptrDataBlockSignal = (Mdf.ReadU64().ValidateAddress(Mdf), ptrChannelConversionBlock.offset + 8); - ptrUnit = (Mdf.ReadU64().ValidateAddress(Mdf), ptrDataBlockSignal.offset + 8); - ptrTextBlockComment = (Mdf.ReadU64().ValidateAddress(Mdf), ptrUnit.offset + 8); + listAddressesV4 = new List>(); + + ptrNextChannelBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrComponentAddressV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrNextChannelBlockV4.offset + 8); + ptrTextBlockChanelNameV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrComponentAddressV4.offset + 8); + ptrChannelExtensionBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrTextBlockChanelNameV4.offset + 8); + ptrChannelConversionBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrChannelExtensionBlockV4.offset + 8); + ptrDataBlockSignalV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrChannelConversionBlockV4.offset + 8); + ptrUnitV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrDataBlockSignalV4.offset + 8); + ptrTextBlockCommentV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrUnitV4.offset + 8); //ptrAttachment = Mdf.ReadU64(); //ptrDefaultDGBlock = Mdf.ReadU64(); //ptrDefaultCGBlock = Mdf.ReadU64(); @@ -212,20 +242,32 @@ internal override void ReadV4() LimitMinExt = Mdf.ReadDouble(); LimitMaxExt = Mdf.ReadDouble(); - if (ptrTextBlockChanelName.address != 0) - LongSignalName = TextBlock.Read(Mdf, (int)ptrTextBlockChanelName.address); - - if (ptrUnit.address != 0) - Unit = TextBlock.Read(Mdf, (int)ptrUnit.address); - - if (ptrTextBlockComment.address != 0) - Comment = TextBlock.Read(Mdf, (int)ptrTextBlockComment.address); - - if (ptrLongSignalName.address != 0) - LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalName.address); - - if (channelConversion == null && ptrChannelConversionBlock.address != 0) - ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlock.address); + listAddressesV4.AddRange(new PointerAddress[] + { + ptrNextChannelBlockV4, + ptrComponentAddressV4, + ptrTextBlockChanelNameV4, + ptrChannelExtensionBlockV4, + ptrChannelConversionBlockV4, + ptrDataBlockSignalV4, + ptrUnitV4, + ptrTextBlockCommentV4 + }); + + if (ptrTextBlockChanelNameV4.address != 0) + LongSignalName = TextBlock.Read(Mdf, (int)ptrTextBlockChanelNameV4.address); + + if (ptrUnitV4.address != 0) + Unit = TextBlock.Read(Mdf, (int)ptrUnitV4.address); + + if (ptrTextBlockCommentV4.address != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlockCommentV4.address); + + if (ptrLongSignalNameV4.address != 0) + LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalNameV4.address); + + if (channelConversion == null && ptrChannelConversionBlockV4.address != 0) + ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlockV4.address); } /// /// Set this address 0 for previous channel. Lost address @@ -251,14 +293,12 @@ public byte[] Remove(byte[] bytes) return bytes; } - var blockPrevAddress = previous.BlockAddress; - var thisPointer = blockPrevAddress + 4; - var newbytes = BitConverter.GetBytes((int)ptrNextChannelBlock.address); - Array.Copy(newbytes, 0, bytes, thisPointer, newbytes.Length);//changing the pointer to this block from the previous block, to the next of this block - //Array.Copy(new byte[(int)Size - 4 - newbytes.Length], 0, bytes, BlockAddress + 4 + newbytes.Length, (int)Size - 4 - newbytes.Length); //set empty(?) data after address of next block + if (Mdf.IDBlock.Version >= 400) + bytes = RemoveV4(bytes,previous); + else + bytes = RemoveV23(bytes, previous); - previous.ptrNextChannelBlock = ptrNextChannelBlock; previous.next = next; if (next != null) @@ -266,9 +306,33 @@ public byte[] Remove(byte[] bytes) ChanelsRemovedAddress?.Invoke(this, bytes); - var removedBytes = bytes.Where((x, index) => index < BlockAddress || BlockAddress + (int)Size < index).ToArray(); - Array.Copy(removedBytes, bytes, removedBytes.Length); + var removedBytes = bytes.Where((x, index) => index <= BlockAddress || BlockAddress + (int)Size < index).ToArray(); Array.Resize(ref bytes, removedBytes.Length); + Array.Copy(removedBytes, bytes, removedBytes.Length); + return bytes; + } + + private byte[] RemoveV23(byte[] bytes, ChannelBlock previous) + { + var thisPointer = BlockAddress + previous.ptrNextChannelBlock.offset; + + var newbytes = BitConverter.GetBytes(ptrNextChannelBlock.address); + Array.Copy(newbytes, 0, bytes, thisPointer, newbytes.Length);//changing the pointer to this block from the previous block, to the next of this block + + previous.ptrNextChannelBlock = ptrNextChannelBlock; + + return bytes; + } + + private byte[] RemoveV4(byte[] bytes, ChannelBlock previous) + { + var thisPointer = BlockAddress + previous.ptrNextChannelBlockV4.offset; + + var newbytes = BitConverter.GetBytes(ptrNextChannelBlockV4.address); + Array.Copy(newbytes, 0, bytes, thisPointer, newbytes.Length);//changing the pointer to this block from the previous block, to the next of this block + + previous.ptrNextChannelBlockV4 = ptrNextChannelBlockV4; + return bytes; } @@ -347,83 +411,35 @@ internal void WriteNextChannelLink(byte[] array, int index, int blockIndex) internal void ChannelUpdateAddress(int indexDeleted, byte[] bytes, ulong countDeleted) { - if ((int)ptrAttachment.address > indexDeleted) - { - ptrAttachment.address -= countDeleted; - - this.CopyAddress(ptrAttachment, bytes); - } - if ((int)ptrChannelComment.address > indexDeleted) - { - ptrChannelComment.address -= countDeleted; - - this.CopyAddress(ptrChannelComment, bytes); - } - if ((int)ptrChannelConversionBlock.address > indexDeleted) - { - ptrChannelConversionBlock.address -= countDeleted; - - this.CopyAddress(ptrChannelConversionBlock, bytes); - } - if ((int)ptrChannelDependencyBlock.address > indexDeleted) - { - ptrChannelDependencyBlock.address -= countDeleted; - - this.CopyAddress(ptrChannelDependencyBlock, bytes); - } - if ((int)ptrChannelExtensionBlock.address > indexDeleted) - { - ptrChannelExtensionBlock.address -= countDeleted; - - this.CopyAddress(ptrChannelExtensionBlock, bytes); - } - if ((int)ptrComponentAddress.address > indexDeleted) - { - ptrComponentAddress.address -= countDeleted; - - this.CopyAddress(ptrComponentAddress, bytes); - } - if ((int)ptrDataBlockSignal.address > indexDeleted) - { - ptrDataBlockSignal.address -= countDeleted; - - this.CopyAddress(ptrDataBlockSignal, bytes); - } - if ((int)ptrDisplayName.address > indexDeleted) - { - ptrDisplayName.address -= countDeleted; - - this.CopyAddress(ptrDisplayName, bytes); - } - if ((int)ptrLongSignalName.address > indexDeleted) - { - ptrLongSignalName.address -= countDeleted; - - this.CopyAddress(ptrLongSignalName, bytes); - } - if ((int)ptrNextChannelBlock.address > indexDeleted) - { - ptrNextChannelBlock.address -= countDeleted; + if (Mdf.IDBlock.Version >= 400) + ChannelUpdateAddressV4(indexDeleted,bytes, countDeleted); + else + ChannelUpdateAddressV23(indexDeleted, bytes, (uint)countDeleted); + } - this.CopyAddress(ptrNextChannelBlock, bytes); - } - if ((int)ptrTextBlockChanelName.address > indexDeleted) + private void ChannelUpdateAddressV23(int indexDeleted, byte[] bytes, uint countDeleted) + { + foreach (var ptr in listAddressesV23) { - ptrTextBlockChanelName.address -= countDeleted; + if ((int)ptr.address > indexDeleted) + { + ptr.address -= countDeleted; - this.CopyAddress(ptrTextBlockChanelName, bytes); + this.CopyAddress(ptr, bytes); + } } - if ((int)ptrTextBlockComment.address > indexDeleted) - { - ptrTextBlockComment.address -= countDeleted; + } - this.CopyAddress(ptrTextBlockComment, bytes); - } - if ((int)ptrUnit.address > indexDeleted) + private void ChannelUpdateAddressV4(int indexDeleted, byte[] bytes, ulong countDeleted) + { + foreach (var ptr in listAddressesV4) { - ptrUnit.address -= countDeleted; + if ((int)ptr.address > indexDeleted) + { + ptr.address -= countDeleted; - this.CopyAddress(ptrUnit, bytes); + this.CopyAddress(ptr, bytes); + } } } } diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index dd45343..4874b91 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -8,10 +8,15 @@ public class ChannelConversionBlock : Block { - internal (ulong address, int offset) ptrTextBlockName; - internal (ulong address, int offset) ptrTextBlockUnit; - internal (ulong address, int offset) ptrFileComment; - internal (ulong address, int offset) ptrInverseConversion; + internal PointerAddress ptrTextBlockName; + internal PointerAddress ptrTextBlockUnit; + internal PointerAddress ptrFileComment; + internal PointerAddress ptrInverseConversion; + + internal PointerAddress ptrTextBlockNameV4; + internal PointerAddress ptrTextBlockUnitV4; + internal PointerAddress ptrFileCommentV4; + internal PointerAddress ptrInverseConversionV4; private string physicalUnit; private int indexPointer; @@ -82,10 +87,10 @@ internal override void ReadV4() { base.ReadV4(); - ptrTextBlockName = (Mdf.ReadU64().ValidateAddress(Mdf), 24); - ptrTextBlockUnit = (Mdf.ReadU64().ValidateAddress(Mdf),ptrTextBlockName.offset + 8); - ptrFileComment = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextBlockUnit.offset + 8); - ptrInverseConversion = (Mdf.ReadU64().ValidateAddress(Mdf), ptrFileComment.offset + 8); + ptrTextBlockNameV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrTextBlockUnitV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf),ptrTextBlockName.offset + 8); + ptrFileCommentV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrTextBlockUnit.offset + 8); + ptrInverseConversionV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrFileComment.offset + 8); var lastPosAddress = Mdf.position; if (LinksCount > 4) diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index 46f8521..83ebebc 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -1,17 +1,26 @@ namespace ASAM.MDF.Libary { using System; - using System.IO; - using System.Security.Cryptography; + using System.Collections.Generic; public class ChannelGroupBlock : Block, INext, IPrevious, IParent { - internal (ulong address, int offset) ptrNextChannelGroup; - internal (ulong address, int offset) ptrFirstChannelBlock; - internal (ulong address, int offset) ptrTextName; - internal (ulong address, int offset) ptrTextBlock; - internal (ulong address, int offset) ptrSourceInfo; - internal (ulong address, int offset) ptrFirstSampleReductionBlock; + private List> listAddressesV23; + private List> listAddressesV4; + + internal PointerAddress ptrNextChannelGroup; + internal PointerAddress ptrFirstChannelBlock; + internal PointerAddress ptrTextName; + internal PointerAddress ptrTextBlock; + internal PointerAddress ptrSourceInfo; + internal PointerAddress ptrFirstSampleReductionBlock; + + internal PointerAddress ptrNextChannelGroupV4; + internal PointerAddress ptrFirstChannelBlockV4; + internal PointerAddress ptrTextNameV4; + internal PointerAddress ptrTextBlockV4; + internal PointerAddress ptrSourceInfoV4; + internal PointerAddress ptrFirstSampleReductionBlockV4; private char pathSeparator; @@ -26,7 +35,12 @@ public ChannelGroupBlock Next { get { - if (next == null && ptrNextChannelGroup.address != 0) + if (Mdf.IDBlock.Version >= 400) + { + if (next == null && ptrNextChannelGroupV4 != null && ptrNextChannelGroupV4.address != 0) + next = Read(Mdf, (int)ptrNextChannelGroupV4.address); + } + else if (next == null && ptrNextChannelGroup != null && ptrNextChannelGroup.address != 0) next = Read(Mdf, (int)ptrNextChannelGroup.address); return next; @@ -71,23 +85,32 @@ internal override void ReadV23() { base.ReadV23(); - ptrNextChannelGroup = (Mdf.ReadU32().ValidateAddress(Mdf), 4); - ptrFirstChannelBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrNextChannelGroup.offset + 4); - ptrTextBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstChannelBlock.offset + 4); + listAddressesV23 = new List>(); + + ptrNextChannelGroup = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), 4); + ptrFirstChannelBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrNextChannelGroup.offset + 4); + ptrTextBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstChannelBlock.offset + 4); RecordID = Mdf.ReadU16(); NumChannels = Mdf.ReadU16(); RecordSize = Mdf.ReadU16(); NumRecords = Mdf.ReadU32(); + listAddressesV23.AddRange(new PointerAddress[] + { + ptrNextChannelGroup, + ptrFirstChannelBlock, + ptrTextBlock, + }); + if (Size >= 26) - ptrFirstSampleReductionBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrTextBlock.offset + 2 + 2 + 2 + 4); + { + ptrFirstSampleReductionBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrTextBlock.offset + 2 + 2 + 2 + 4); + listAddressesV23.Add(ptrFirstSampleReductionBlock); + } if (ptrTextBlock.address != 0) Comment = TextBlock.Read(Mdf, (int)ptrTextBlock.address); - if (ptrTextName.address != 0) - TextName = TextBlock.Read(Mdf, (int)ptrTextName.address); - if (ptrFirstChannelBlock.address != 0) { var chBlock = ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlock.address); @@ -100,12 +123,14 @@ internal override void ReadV4() { base.ReadV4(); - ptrNextChannelGroup = (Mdf.ReadU64().ValidateAddress(Mdf), 24); - ptrFirstChannelBlock = (Mdf.ReadU64().ValidateAddress(Mdf),ptrNextChannelGroup.offset + 8); - ptrTextName = (Mdf.ReadU64().ValidateAddress(Mdf), ptrFirstChannelBlock.offset + 8); - ptrSourceInfo = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextName.offset + 8); - ptrFirstSampleReductionBlock = (Mdf.ReadU64().ValidateAddress(Mdf), ptrSourceInfo.offset + 8); - ptrTextBlock = (Mdf.ReadU64().ValidateAddress(Mdf), ptrFirstSampleReductionBlock.offset + 8); + listAddressesV4 = new List>(); + + ptrNextChannelGroupV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrFirstChannelBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrNextChannelGroupV4.offset + 8); + ptrTextNameV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrFirstChannelBlockV4.offset + 8); + ptrSourceInfoV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrTextNameV4.offset + 8); + ptrFirstSampleReductionBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrSourceInfoV4.offset + 8); + ptrTextBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrFirstSampleReductionBlockV4.offset + 8); RecordID = Mdf.ReadU64(); CycleCount = Mdf.ReadU64(); Flags = Mdf.ReadU16(); @@ -114,16 +139,34 @@ internal override void ReadV4() DataBytes = Mdf.ReadU32(); InvalidBytes = Mdf.ReadU32(); - if (ptrTextBlock.address != 0) - Comment = TextBlock.Read(Mdf, (int)ptrTextBlock.address); - - if (ptrTextName.address != 0) - TextName = TextBlock.Read(Mdf, (int)ptrTextName.address); - - if (ptrFirstChannelBlock.address != 0) - Channels.Read(ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlock.address), null); + listAddressesV4.AddRange(new PointerAddress[] + { + ptrNextChannelGroupV4, + ptrFirstChannelBlockV4, + ptrTextNameV4, + ptrSourceInfoV4, + ptrFirstSampleReductionBlockV4, + ptrTextBlockV4, + }); + + if (ptrTextBlockV4.address != 0) + Comment = TextBlock.Read(Mdf, (int)ptrTextBlockV4.address); + + if (ptrTextNameV4.address != 0) + TextName = TextBlock.Read(Mdf, (int)ptrTextNameV4.address); + + if (ptrFirstChannelBlockV4.address != 0) + Channels.Read(ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlockV4.address), null); } private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, byte[] bytes) + { + if (Mdf.IDBlock.Version >= 400) + RemoveChannelsV4(block, bytes); + else + RemoveChannelsV23(block, bytes); + } + + private void RemoveChannelsV23(ChannelBlock block, byte[] bytes) { if (Channels[0] == block) //change first channel address on channelGroup { @@ -131,25 +174,49 @@ private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, byte[] bytes) if (nextBlock == null) ptrFirstChannelBlock.address = 0; else - ptrFirstChannelBlock.address = (ulong)block.Next.BlockAddress; + ptrFirstChannelBlock.address = (uint)block.Next.BlockAddress; - var addressOfFirstChannel = BlockAddress + 4 + 4/*ptrNextChannelGroup*/; + var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlock.offset; var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock.address); Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); } if (block.Previous == null && block.Next == null) { ptrFirstChannelBlock.address = 0; - var addressOfFirstChannel = BlockAddress + 4 + 4/*ptrNextChannelGroup*/; + var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlock.offset; var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock.address); Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); } NumChannels -= 1; - var addressNumChannels = BlockAddress + 4 + 4/*ptrNextChannelGroup*/ + 4/*ptrFirstChannelBlock*/ + 4/*ptrTextBlock*/ + 2/*RecordID*/; + var addressNumChannels = ptrTextBlock.offset + 2/*RecordID*/; var newbytes = BitConverter.GetBytes(NumChannels); Array.Copy(newbytes, 0, bytes, addressNumChannels, newbytes.Length); } + + private void RemoveChannelsV4(ChannelBlock block, byte[] bytes) + { + if (Channels[0] == block) //change first channel address on channelGroup + { + var nextBlock = block.Next; + if (nextBlock == null) + ptrFirstChannelBlockV4.address = 0; + else + ptrFirstChannelBlockV4.address = (ulong)block.Next.BlockAddress; + + var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlockV4.offset; + var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlockV4.address); + Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); + } + if (block.Previous == null && block.Next == null) + { + ptrFirstChannelBlockV4.address = 0; + var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlockV4.offset; + var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlockV4.address); + Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); + } + } + internal override int GetSizeTotal() { var size = base.GetSizeTotal(); @@ -208,41 +275,35 @@ internal void WriteNextChannelGroupBlockLink(byte[] array, int index, int blockI internal void ChannelGroupUpdateAddress(int indexDeleted, byte[] bytes, ulong countDeleted) { - if ((int)ptrFirstChannelBlock.address > indexDeleted) - { - ptrFirstChannelBlock.address -= countDeleted; - - this.CopyAddress(ptrFirstChannelBlock, bytes); - } - if ((int)ptrFirstSampleReductionBlock.address > indexDeleted) - { - ptrFirstSampleReductionBlock.address -= countDeleted; - - this.CopyAddress(ptrFirstSampleReductionBlock, bytes); - } - if ((int)ptrNextChannelGroup.address > indexDeleted) - { - ptrNextChannelGroup.address -= countDeleted; + if (Mdf.IDBlock.Version >= 400) + ChannelGroupUpdateAddressV4(indexDeleted, bytes, countDeleted); + else + ChannelGroupUpdateAddressV23(indexDeleted, bytes, (uint)countDeleted); + } - this.CopyAddress(ptrNextChannelGroup, bytes); - } - if ((int)ptrSourceInfo.address > indexDeleted) + private void ChannelGroupUpdateAddressV23(int indexDeleted, byte[] bytes, uint countDeleted) + { + foreach (var ptr in listAddressesV23) { - ptrSourceInfo.address -= countDeleted; + if ((int)ptr.address > indexDeleted) + { + ptr.address -= countDeleted; - this.CopyAddress(ptrSourceInfo, bytes); + this.CopyAddress(ptr, bytes); + } } - if ((int)ptrTextBlock.address > indexDeleted) - { - ptrTextBlock.address -= countDeleted; + } - this.CopyAddress(ptrTextBlock, bytes); - } - if ((int)ptrTextName.address > indexDeleted) + private void ChannelGroupUpdateAddressV4(int indexDeleted, byte[] bytes, ulong countDeleted) + { + foreach (var ptr in listAddressesV4) { - ptrTextName.address -= countDeleted; + if ((int)ptr.address > indexDeleted) + { + ptr.address -= countDeleted; - this.CopyAddress(ptrTextName, bytes); + this.CopyAddress(ptr, bytes); + } } } } diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index f1d06ed..af1cd33 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -5,11 +5,13 @@ public class DataGroupBlock : Block, INext, IPrevious, IParent { + internal List> listAddressesV23; + internal List> listAddressesV4; + internal PointerAddress ptrNextDataGroup; internal PointerAddress ptrFirstChannelGroupBlock; internal PointerAddress ptrTriggerBlock; internal PointerAddress ptrDataBlock; - internal PointerAddress ptrTextBlock; internal PointerAddress ptrNextDataGroupV4; internal PointerAddress ptrFirstChannelGroupBlockV4; @@ -30,13 +32,13 @@ public DataGroupBlock Next { get { - if (Mdf.IDBlock.Version >= 4) - if (nextBlock == null && ptrNextDataGroup.address != 0) - nextBlock = Read(Mdf, (int)ptrNextDataGroup.address); - - if (Mdf.IDBlock.Version < 4) - if (nextBlock == null && ptrNextDataGroupV4.address != 0) + if (Mdf.IDBlock.Version >= 400) + { + if (nextBlock == null && ptrNextDataGroupV4 != null && ptrNextDataGroupV4?.address != 0) nextBlock = Read(Mdf, (int)ptrNextDataGroupV4.address); + } + else if (nextBlock == null && ptrNextDataGroup != null && ptrNextDataGroup.address != 0) + nextBlock = Read(Mdf, (int)ptrNextDataGroup.address); return nextBlock; } @@ -96,6 +98,8 @@ internal override void ReadV23() { base.ReadV23(); + listAddressesV23 = new List>(); + ptrNextDataGroup = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), 4); ptrFirstChannelGroupBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrNextDataGroup.offset + 4); ptrTriggerBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstChannelGroupBlock.offset + 4); @@ -106,13 +110,17 @@ internal override void ReadV23() if (Size >= 24) Reserved = Mdf.ReadU32(); - - if (ptrTextBlock.address != 0) - FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock.address); - if (ptrFirstChannelGroupBlock.address != 0) ChannelGroups.Read(ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlock.address)); + listAddressesV23.AddRange(new PointerAddress[] + { + ptrNextDataGroup, + ptrFirstChannelGroupBlock, + ptrTriggerBlock, + ptrDataBlock, + }); + /// TODO: Call Trigger Blocks //if (m_ptrTriggerBlock != 0) //{ @@ -131,6 +139,8 @@ internal override void ReadV4() { base.ReadV4(); + listAddressesV4 = new List>(); + ptrNextDataGroupV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); ptrFirstChannelGroupBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrNextDataGroupV4.offset + 8); ptrDataBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf),ptrFirstChannelGroupBlockV4.offset + 8); @@ -156,6 +166,14 @@ internal override void ReadV4() DataListColl.Read(DataList.Read(Mdf, (int)ptrDataBlockV4.address)); } } + + listAddressesV4.AddRange(new PointerAddress[] + { + ptrNextDataGroupV4, + ptrDataBlockV4, + ptrFirstChannelGroupBlockV4, + ptrTextBlockV4, + }); } internal DataRecord[] ReadRecords() @@ -165,7 +183,7 @@ internal DataRecord[] ReadRecords() var indentificator = Mdf.GetNameBlock((int)ptrDataBlock.address); - if (Mdf.IDBlock.Version >= 4) + if (Mdf.IDBlock.Version >= 400) indentificator = Mdf.GetNameBlock((int)ptrDataBlockV4.address); if (indentificator == "DL") @@ -178,7 +196,7 @@ internal DataRecord[] ReadRecords() Mdf.UpdatePosition((int)ptrDataBlock.address); - if (Mdf.IDBlock.Version >= 4) + if (Mdf.IDBlock.Version >= 400) Mdf.UpdatePosition((int)ptrDataBlockV4.address);// if (Mdf.IDBlock.Version >= 400) @@ -305,35 +323,35 @@ internal void WriteRecords(byte[] array, ref int index, int blockIndex) internal void DataGroupUpdateAddress(int indexDeleted, byte[] bytes, ulong countDeleted) { - if ((int)ptrDataBlock.address > indexDeleted) - { - ptrDataBlock.address -= countDeleted; - - this.CopyAddress(ptrDataBlock, bytes); - } - if ((int)ptrFirstChannelGroupBlock.address > indexDeleted) - { - ptrFirstChannelGroupBlock.address -= countDeleted; + if (Mdf.IDBlock.Version >= 400) + DataGroupUpdateAddressV4(indexDeleted, bytes, countDeleted); + else + DataGroupUpdateAddressV23(indexDeleted, bytes, (uint)countDeleted); + } - this.CopyAddress(ptrFirstChannelGroupBlock, bytes); - } - if ((int)ptrNextDataGroup.address > indexDeleted) + private void DataGroupUpdateAddressV23(int indexDeleted, byte[] bytes, uint countDeleted) + { + foreach (var ptr in listAddressesV23) { - ptrNextDataGroup.address -= countDeleted; + if ((int)ptr.address > indexDeleted) + { + ptr.address -= countDeleted; - this.CopyAddress(ptrNextDataGroup, bytes); + this.CopyAddress(ptr, bytes); + } } - if ((int)ptrTextBlock.address > indexDeleted) - { - ptrTextBlock.address -= countDeleted; + } - this.CopyAddress(ptrTextBlock, bytes); - } - if ((int)ptrTriggerBlock.address > indexDeleted) + private void DataGroupUpdateAddressV4(int indexDeleted, byte[] bytes, ulong countDeleted) + { + foreach (var ptr in listAddressesV4) { - ptrTriggerBlock.address -= countDeleted; + if ((int)ptr.address > indexDeleted) + { + ptr.address -= countDeleted; - this.CopyAddress(ptrTriggerBlock, bytes); + this.CopyAddress(ptr, bytes); + } } } } diff --git a/ASAM.MDF/Libary/DataList.cs b/ASAM.MDF/Libary/DataList.cs index 1cacaa5..30d1326 100644 --- a/ASAM.MDF/Libary/DataList.cs +++ b/ASAM.MDF/Libary/DataList.cs @@ -5,8 +5,8 @@ namespace ASAM.MDF.Libary { public class DataList : Block, INext, IPrevious, IParent { - internal (ulong address, int offset) ptrNextDL; - internal (ulong address, int offset) ptrDataBlockAddress; + internal PointerAddress ptrNextDLV4; + internal PointerAddress ptrDataBlockAddressV4; private ulong ptrDataBlockLen; private DataList nextBlock; @@ -22,8 +22,8 @@ public DataList Next { get { - if (nextBlock == null && ptrNextDL.address != 0) - nextBlock = Read(Mdf, (int)ptrNextDL.address); + if (nextBlock == null && ptrNextDLV4.address != 0) + nextBlock = Read(Mdf, (int)ptrNextDLV4.address); return nextBlock; } @@ -46,8 +46,8 @@ internal override void ReadV4() { base.ReadV4(); - ptrNextDL = (Mdf.ReadU64().ValidateAddress(Mdf), 24); - ptrDataBlockAddress = (Mdf.ReadU64().ValidateAddress(Mdf),ptrNextDL.offset + 8); + ptrNextDLV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrDataBlockAddressV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf),ptrNextDLV4.offset + 8); Flags = (ListFlags)Mdf.ReadU16(); Reserved1 = Mdf.ReadU16(); BlockCount = Mdf.ReadU32(); @@ -57,8 +57,8 @@ internal override void ReadV4() else BlockOffset = Mdf.Read64(); - if (ptrDataBlockAddress.address != 0) - DataBlock = DataBlock.Read(Mdf, (int)ptrDataBlockAddress.address); + if (ptrDataBlockAddressV4.address != 0) + DataBlock = DataBlock.Read(Mdf, (int)ptrDataBlockAddressV4.address); } } } diff --git a/ASAM.MDF/Libary/Extensions.cs b/ASAM.MDF/Libary/Extensions.cs index b8605e2..9ce0ff2 100644 --- a/ASAM.MDF/Libary/Extensions.cs +++ b/ASAM.MDF/Libary/Extensions.cs @@ -11,18 +11,16 @@ internal static class Extensions /// Block coping /// pointer to change /// byte massive for copy - internal static void CopyAddress(this T block, PointerAddress pointerData, byte[] bytes) where T : Block + internal static void CopyAddress(this T block, PointerAddress pointerData, byte[] bytes) where T : Block { - if (pointerData.version < 4) - { - var newbytes = BitConverter.GetBytes(pointerData.AddressV23); - Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); - } - else - { - var newbytes = BitConverter.GetBytes(pointerData.AddressV4); - Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); - } + var newbytes = BitConverter.GetBytes(pointerData.address); + Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); + + } + internal static void CopyAddress(this T block, PointerAddress pointerData, byte[] bytes) where T : Block + { + var newbytes = BitConverter.GetBytes(pointerData.address); + Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); } } } diff --git a/ASAM.MDF/Libary/PointerAddress.cs b/ASAM.MDF/Libary/PointerAddress.cs index 759b8e0..13cf686 100644 --- a/ASAM.MDF/Libary/PointerAddress.cs +++ b/ASAM.MDF/Libary/PointerAddress.cs @@ -3,11 +3,11 @@ internal class PointerAddress where T : struct { internal readonly int offset; - internal readonly T address; + internal T address; internal PointerAddress(T ptr, int offset) { - this.address = ptr; + address = ptr; this.offset = offset; } } diff --git a/DebugOpenFileMdf/Program.cs b/DebugOpenFileMdf/Program.cs index 2c38118..b311ee6 100644 --- a/DebugOpenFileMdf/Program.cs +++ b/DebugOpenFileMdf/Program.cs @@ -25,9 +25,12 @@ } } } + list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); + list.RemoveAt(1); + samebytes = mdf.RemoveChannel(list.ToArray()); + var newmdf = new Mdf(samebytes); - list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); var ex = Path.GetExtension(filename); var file = Path.GetFileNameWithoutExtension(filename) + "Test"; var path = Path.GetDirectoryName(filename) + "\\" + file + ex; From b479eab5b01ee17cb08e34e0a0f1039d32338edd Mon Sep 17 00:00:00 2001 From: Mikhail Date: Tue, 16 Apr 2024 19:56:00 +0300 Subject: [PATCH 19/34] Try to cut some channels --- ASAM.MDF/Libary/ChannelBlock.cs | 34 ++++----- ASAM.MDF/Libary/ChannelGroupBlock.cs | 57 +++++++++----- ASAM.MDF/Libary/ChannelGroupCollection.cs | 6 +- ASAM.MDF/Libary/DataGroupBlock.cs | 93 +++++++++++++++++++++-- ASAM.MDF/Libary/Extensions.cs | 15 +++- ASAM.MDF/Libary/Mdf.cs | 22 +++--- DebugOpenFileMdf/Program.cs | 22 +++++- 7 files changed, 186 insertions(+), 63 deletions(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index a27a910..1a695c9 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -8,7 +8,7 @@ public class ChannelBlock : Block, INext, IPrevious, IParent { - public delegate void ChanelHandlerRemovedAddress(ChannelBlock block, byte[] bytes); + public delegate void ChanelHandlerRemovedAddress(ChannelBlock block, List bytes); private const int MIN_VERSION_LONG_SIGNAL_NAME = 212; private const int MIN_VERSION_DISPLAY_NAME = 300; @@ -273,11 +273,8 @@ internal override void ReadV4() /// Set this address 0 for previous channel. Lost address /// /// Copied modified the entire array of mdf bytes - public byte[] Remove(byte[] bytes) + public List Remove(List bytes) { - if (TypeV3 == ChannelTypeV3.Time) - return bytes; - var previous = Previous; if (previous == null && next != null)// first of list node channels: [X channel]->[1 channel]->[2 channel]->...->[n channel] { @@ -287,7 +284,7 @@ public byte[] Remove(byte[] bytes) return bytes; } - else if (previous == null && next == null) + else if (previous == null && next == null) // [null] -> [X channel] -> [null] { ChanelsRemovedAddress?.Invoke(this, bytes); @@ -305,31 +302,30 @@ public byte[] Remove(byte[] bytes) next.Previous = previous; ChanelsRemovedAddress?.Invoke(this, bytes); - - var removedBytes = bytes.Where((x, index) => index <= BlockAddress || BlockAddress + (int)Size < index).ToArray(); - Array.Resize(ref bytes, removedBytes.Length); - Array.Copy(removedBytes, bytes, removedBytes.Length); + return bytes; } - private byte[] RemoveV23(byte[] bytes, ChannelBlock previous) + private List RemoveV23(List bytes, ChannelBlock previous) { - var thisPointer = BlockAddress + previous.ptrNextChannelBlock.offset; + var thisPointer = previous.BlockAddress + previous.ptrNextChannelBlock.offset; var newbytes = BitConverter.GetBytes(ptrNextChannelBlock.address); - Array.Copy(newbytes, 0, bytes, thisPointer, newbytes.Length);//changing the pointer to this block from the previous block, to the next of this block + for (int i = thisPointer, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; previous.ptrNextChannelBlock = ptrNextChannelBlock; return bytes; } - private byte[] RemoveV4(byte[] bytes, ChannelBlock previous) + private List RemoveV4(List bytes, ChannelBlock previous) { - var thisPointer = BlockAddress + previous.ptrNextChannelBlockV4.offset; + var thisPointer = previous.BlockAddress + previous.ptrNextChannelBlockV4.offset; // this pointer on prev channel next var newbytes = BitConverter.GetBytes(ptrNextChannelBlockV4.address); - Array.Copy(newbytes, 0, bytes, thisPointer, newbytes.Length);//changing the pointer to this block from the previous block, to the next of this block + for (int i = thisPointer, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; previous.ptrNextChannelBlockV4 = ptrNextChannelBlockV4; @@ -409,7 +405,7 @@ internal void WriteNextChannelLink(byte[] array, int index, int blockIndex) Array.Copy(bytesNextChannelLink, 0, array, blockIndex + 4, bytesNextChannelLink.Length); } - internal void ChannelUpdateAddress(int indexDeleted, byte[] bytes, ulong countDeleted) + internal void ChannelUpdateAddress(int indexDeleted, List bytes, ulong countDeleted) { if (Mdf.IDBlock.Version >= 400) ChannelUpdateAddressV4(indexDeleted,bytes, countDeleted); @@ -417,7 +413,7 @@ internal void ChannelUpdateAddress(int indexDeleted, byte[] bytes, ulong countDe ChannelUpdateAddressV23(indexDeleted, bytes, (uint)countDeleted); } - private void ChannelUpdateAddressV23(int indexDeleted, byte[] bytes, uint countDeleted) + private void ChannelUpdateAddressV23(int indexDeleted, List bytes, uint countDeleted) { foreach (var ptr in listAddressesV23) { @@ -430,7 +426,7 @@ private void ChannelUpdateAddressV23(int indexDeleted, byte[] bytes, uint countD } } - private void ChannelUpdateAddressV4(int indexDeleted, byte[] bytes, ulong countDeleted) + private void ChannelUpdateAddressV4(int indexDeleted, List bytes, ulong countDeleted) { foreach (var ptr in listAddressesV4) { diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index 83ebebc..bd375d3 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -5,6 +5,8 @@ public class ChannelGroupBlock : Block, INext, IPrevious, IParent { + public delegate void ChannelGroupBlockHandler(ChannelGroupBlock block, List bytes); + private List> listAddressesV23; private List> listAddressesV4; @@ -46,6 +48,7 @@ public ChannelGroupBlock Next return next; } } + public event ChannelGroupBlockHandler ChannelGroupRemove; public ChannelGroupBlock Previous { get; set; } public ChannelCollection Channels { get; private set; } @@ -61,7 +64,7 @@ public ChannelGroupBlock Next public uint NumRecords { get; set; } public SampleReductionCollection SampleReductions { get; private set; } public TextBlock TextName { get; private set; } - DataGroupBlock IParent.Parent { get; set; } + public DataGroupBlock Parent { get; set; } public static ChannelGroupBlock Create(Mdf mdf) { @@ -114,7 +117,7 @@ internal override void ReadV23() if (ptrFirstChannelBlock.address != 0) { var chBlock = ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlock.address); - chBlock.ChanelsRemovedAddress += (ch, bytes) => ChBlock_ChanelsRemovedAddress(ch, bytes); + //chBlock.ChanelsRemovedAddress += (ch, bytes) => ChBlock_ChanelsRemovedAddress(ch, bytes); Channels.Read(chBlock, ChBlock_ChanelsRemovedAddress); } @@ -156,9 +159,9 @@ internal override void ReadV4() TextName = TextBlock.Read(Mdf, (int)ptrTextNameV4.address); if (ptrFirstChannelBlockV4.address != 0) - Channels.Read(ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlockV4.address), null); + Channels.Read(ChannelBlock.Read(Mdf, (int)ptrFirstChannelBlockV4.address), ChBlock_ChanelsRemovedAddress); } - private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, byte[] bytes) + private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, List bytes) { if (Mdf.IDBlock.Version >= 400) RemoveChannelsV4(block, bytes); @@ -166,9 +169,9 @@ private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, byte[] bytes) RemoveChannelsV23(block, bytes); } - private void RemoveChannelsV23(ChannelBlock block, byte[] bytes) + private void RemoveChannelsV23(ChannelBlock block, List bytes) { - if (Channels[0] == block) //change first channel address on channelGroup + if (Channels[1] == block) //change first channel address on channelGroup { var nextBlock = block.Next; if (nextBlock == null) @@ -178,23 +181,37 @@ private void RemoveChannelsV23(ChannelBlock block, byte[] bytes) var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlock.offset; var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock.address); - Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); + + for (int i = addressOfFirstChannel, j = 0; j < bytesFirstChannelAddress.Length; i++, j++) + bytes[i] = bytesFirstChannelAddress[j]; } + if (block.Previous == null && block.Next == null) { ptrFirstChannelBlock.address = 0; var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlock.offset; var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock.address); - Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); + + for (int i = addressOfFirstChannel, j = 0; j < bytesFirstChannelAddress.Length; i++, j++) + bytes[i] = bytesFirstChannelAddress[j]; + } + NumChannels -= 1; - var addressNumChannels = ptrTextBlock.offset + 2/*RecordID*/; - var newbytes = BitConverter.GetBytes(NumChannels); - Array.Copy(newbytes, 0, bytes, addressNumChannels, newbytes.Length); + if (NumChannels > 1) + { + var addressNumChannels = BlockAddress + ptrTextBlock.offset + 2/*RecordID*/; + var newbytes = BitConverter.GetBytes(NumChannels); + + for (int i = addressNumChannels, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + } + else + ChannelGroupRemove?.Invoke(this, bytes); } - private void RemoveChannelsV4(ChannelBlock block, byte[] bytes) + private void RemoveChannelsV4(ChannelBlock block, List bytes) { if (Channels[0] == block) //change first channel address on channelGroup { @@ -206,14 +223,20 @@ private void RemoveChannelsV4(ChannelBlock block, byte[] bytes) var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlockV4.offset; var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlockV4.address); - Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); + + for (int i = addressOfFirstChannel, j = 0; j < bytesFirstChannelAddress.Length; i++, j++) + bytes[i] = bytesFirstChannelAddress[j]; + } + if (block.Previous == null && block.Next == null) { ptrFirstChannelBlockV4.address = 0; var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlockV4.offset; var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlockV4.address); - Array.Copy(bytesFirstChannelAddress, 0, bytes, addressOfFirstChannel, bytesFirstChannelAddress.Length); + + for (int i = addressOfFirstChannel, j = 0; j < bytesFirstChannelAddress.Length; i++, j++) + bytes[i] = bytesFirstChannelAddress[j]; } } @@ -273,7 +296,7 @@ internal void WriteNextChannelGroupBlockLink(byte[] array, int index, int blockI Array.Copy(bytesNextChannelGroupBlockLink, 0, array, blockIndex + 4, bytesNextChannelGroupBlockLink.Length); } - internal void ChannelGroupUpdateAddress(int indexDeleted, byte[] bytes, ulong countDeleted) + internal void ChannelGroupUpdateAddress(int indexDeleted, List bytes, ulong countDeleted) { if (Mdf.IDBlock.Version >= 400) ChannelGroupUpdateAddressV4(indexDeleted, bytes, countDeleted); @@ -281,7 +304,7 @@ internal void ChannelGroupUpdateAddress(int indexDeleted, byte[] bytes, ulong co ChannelGroupUpdateAddressV23(indexDeleted, bytes, (uint)countDeleted); } - private void ChannelGroupUpdateAddressV23(int indexDeleted, byte[] bytes, uint countDeleted) + private void ChannelGroupUpdateAddressV23(int indexDeleted, List bytes, uint countDeleted) { foreach (var ptr in listAddressesV23) { @@ -294,7 +317,7 @@ private void ChannelGroupUpdateAddressV23(int indexDeleted, byte[] bytes, uint c } } - private void ChannelGroupUpdateAddressV4(int indexDeleted, byte[] bytes, ulong countDeleted) + private void ChannelGroupUpdateAddressV4(int indexDeleted, List bytes, ulong countDeleted) { foreach (var ptr in listAddressesV4) { diff --git a/ASAM.MDF/Libary/ChannelGroupCollection.cs b/ASAM.MDF/Libary/ChannelGroupCollection.cs index 2164d82..f239656 100644 --- a/ASAM.MDF/Libary/ChannelGroupCollection.cs +++ b/ASAM.MDF/Libary/ChannelGroupCollection.cs @@ -33,9 +33,13 @@ public ChannelGroupBlock this[int index] set { throw new NotImplementedException(); } } - internal void Read(ChannelGroupBlock channelGroupBlock) + internal void Read(ChannelGroupBlock channelGroupBlock, ChannelGroupBlock.ChannelGroupBlockHandler action) { items = Common.BuildBlockList(null, channelGroupBlock, Parent); + + if (action != null) + foreach (ChannelGroupBlock block in items) + block.ChannelGroupRemove += (cg, bytes) => action(cg, bytes); } internal void Write(byte[] array, ref int index) { diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index af1cd33..6ef34a9 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -111,7 +111,11 @@ internal override void ReadV23() Reserved = Mdf.ReadU32(); if (ptrFirstChannelGroupBlock.address != 0) - ChannelGroups.Read(ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlock.address)); + { + var CGBlock = ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlock.address); + //CGBlock.ChannelGroupRemove += CGBlock_ChannelGroupRemove; + ChannelGroups.Read(CGBlock, CGBlock_ChannelGroupRemove); + } listAddressesV23.AddRange(new PointerAddress[] { @@ -152,7 +156,11 @@ internal override void ReadV4() FileComment = TextBlock.Read(Mdf, (int)ptrTextBlockV4.address); if (ptrFirstChannelGroupBlockV4.address != 0) - ChannelGroups.Read(ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlockV4.address)); + { + var CGBlock = ChannelGroupBlock.Read(Mdf, (int)ptrFirstChannelGroupBlockV4.address); + //CGBlock.ChannelGroupRemove += CGBlock_ChannelGroupRemove; + ChannelGroups.Read(CGBlock, CGBlock_ChannelGroupRemove); + } if (ptrDataBlockV4.address != 0) { @@ -176,6 +184,79 @@ internal override void ReadV4() }); } + private void CGBlock_ChannelGroupRemove(ChannelGroupBlock block, List bytes) + { + // нужно удалить channelgroup, удалить из массива байтов + // также нужно удалить данные из data record. + RemoveChannelGroups(block, bytes); + + RemoveDataRecords(block, bytes); + + } + + private void RemoveChannelGroups(ChannelGroupBlock block, List bytes) + { + if (ptrFirstChannelGroupBlock.address == block.BlockAddress && NumChannelGroups == 1)// переписали указатель, который указывал на наш первый в списке channelGroup + { + var ptrOffset = BlockAddress + ptrFirstChannelGroupBlock.offset; + var newbytes = BitConverter.GetBytes((uint)0); + for (int i = ptrOffset, j = 0; i < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + } + else if (ptrFirstChannelGroupBlock.address == block.BlockAddress) + { + if (block.Next != null) + { + var ptrOffset = BlockAddress + ptrFirstChannelGroupBlock.offset; + var newbytes = BitConverter.GetBytes((uint)block.Next.BlockAddress); + for (int i = ptrOffset, j = 0; i < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + } + else + { + var ptrOffset = BlockAddress + ptrFirstChannelGroupBlock.offset; + var newbytes = BitConverter.GetBytes((uint)0); + for (int i = ptrOffset, j = 0; i < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + } + } + + //bytes.RemoveRange(block.BlockAddress, (int)block.Size);// удалили channelGroup + + //Mdf.UpdateAddresses(bytes, block.Size, block.BlockAddress); + + if (NumChannelGroups > 0) + { + NumChannelGroups -= 1; + + var numChannelGroupsOffset = BlockAddress + ptrDataBlock.offset; + var value = BitConverter.GetBytes((uint)NumChannelGroups); + for (int i = numChannelGroupsOffset, j = 0; i < value.Length; i++, j++) + bytes[i] = value[j]; + } + } + + private void RemoveDataRecords(ChannelGroupBlock block, List bytes) + { + var byteOffset = 0; + for (int i = 0; i < NumChannelGroups; i++) + { + var cg = ChannelGroups[i]; + if (cg == block) + break; + + byteOffset += (int)cg.NumRecords * cg.RecordSize; + } + + var countRecord = (int)block.NumRecords * block.RecordSize; + + var indexStarted = (int)ptrDataBlock.address + byteOffset; + + bytes.RemoveRange(indexStarted, countRecord);// удалили из файла данные + + Mdf.UpdateAddresses(bytes, (ulong)countRecord, indexStarted); + } + internal DataRecord[] ReadRecords() { var recordsList = new List(); @@ -197,7 +278,7 @@ internal DataRecord[] ReadRecords() Mdf.UpdatePosition((int)ptrDataBlock.address); if (Mdf.IDBlock.Version >= 400) - Mdf.UpdatePosition((int)ptrDataBlockV4.address);// + Mdf.UpdatePosition((int)ptrDataBlockV4.address); if (Mdf.IDBlock.Version >= 400) { @@ -321,7 +402,7 @@ internal void WriteRecords(byte[] array, ref int index, int blockIndex) } } - internal void DataGroupUpdateAddress(int indexDeleted, byte[] bytes, ulong countDeleted) + internal void DataGroupUpdateAddress(int indexDeleted, List bytes, ulong countDeleted) { if (Mdf.IDBlock.Version >= 400) DataGroupUpdateAddressV4(indexDeleted, bytes, countDeleted); @@ -329,7 +410,7 @@ internal void DataGroupUpdateAddress(int indexDeleted, byte[] bytes, ulong count DataGroupUpdateAddressV23(indexDeleted, bytes, (uint)countDeleted); } - private void DataGroupUpdateAddressV23(int indexDeleted, byte[] bytes, uint countDeleted) + private void DataGroupUpdateAddressV23(int indexDeleted, List bytes, uint countDeleted) { foreach (var ptr in listAddressesV23) { @@ -342,7 +423,7 @@ private void DataGroupUpdateAddressV23(int indexDeleted, byte[] bytes, uint coun } } - private void DataGroupUpdateAddressV4(int indexDeleted, byte[] bytes, ulong countDeleted) + private void DataGroupUpdateAddressV4(int indexDeleted, List bytes, ulong countDeleted) { foreach (var ptr in listAddressesV4) { diff --git a/ASAM.MDF/Libary/Extensions.cs b/ASAM.MDF/Libary/Extensions.cs index 9ce0ff2..29ea194 100644 --- a/ASAM.MDF/Libary/Extensions.cs +++ b/ASAM.MDF/Libary/Extensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace ASAM.MDF.Libary { @@ -11,16 +12,22 @@ internal static class Extensions /// Block coping /// pointer to change /// byte massive for copy - internal static void CopyAddress(this T block, PointerAddress pointerData, byte[] bytes) where T : Block + internal static void CopyAddress(this T block, PointerAddress pointerData, List bytes) where T : Block { var newbytes = BitConverter.GetBytes(pointerData.address); - Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); + + for (int i = block.BlockAddress + pointerData.offset, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + //Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); } - internal static void CopyAddress(this T block, PointerAddress pointerData, byte[] bytes) where T : Block + internal static void CopyAddress(this T block, PointerAddress pointerData, List bytes) where T : Block { var newbytes = BitConverter.GetBytes(pointerData.address); - Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); + + for (int i = block.BlockAddress + pointerData.offset, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + //Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); } } } diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index 690b3f6..6345eba 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -1,6 +1,7 @@ namespace ASAM.MDF.Libary { using System; + using System.Collections.Generic; using System.Linq; public class Mdf @@ -36,12 +37,14 @@ public Mdf() public byte[] RemoveChannel(ChannelBlock[] channelBlocks) { - var bytes = new byte[Data.Length]; - Array.Copy(Data, bytes, Data.Length); + //var bytes = new byte[Data.Length]; + var bytes = new List(Data); + var copiedMDF = new Mdf(bytes.ToArray()); + var BlockAddresses = channelBlocks.Select(x => x.BlockAddress); - for (int i = 0; i < DataGroups.Count; i++) + for (int i = 0; i < copiedMDF.DataGroups.Count; i++) { - var dataGroup = DataGroups[i]; + var dataGroup = copiedMDF.DataGroups[i]; for (int j = 0; j < dataGroup.ChannelGroups.Count; j++) { @@ -51,17 +54,13 @@ public byte[] RemoveChannel(ChannelBlock[] channelBlocks) { var channel = channelGroup.Channels[k]; - if (channelBlocks.Contains(channel)) - { + if (BlockAddresses.Contains(channel.BlockAddress)) bytes = channel.Remove(bytes); - - UpdateAddresses(bytes, Data, channel.BlockAddress); - } } } } - return bytes; + return bytes.ToArray(); } public byte[] GetBytes() @@ -91,10 +90,9 @@ public byte[] GetBytes() /// /// None deleted data /// Index start deleted on prevDataBytes - internal void UpdateAddresses(byte[] data, byte[] prevDataBytes, int indexDeleted) + internal void UpdateAddresses(List data, ulong countDeleted, int indexDeleted) { var bytes = data; - var countDeleted = (ulong)(prevDataBytes.Length - bytes.Length); if (countDeleted == 0) return; diff --git a/DebugOpenFileMdf/Program.cs b/DebugOpenFileMdf/Program.cs index b311ee6..86fb52a 100644 --- a/DebugOpenFileMdf/Program.cs +++ b/DebugOpenFileMdf/Program.cs @@ -25,11 +25,25 @@ } } } - list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); - list.RemoveAt(1); + //list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); + //list.RemoveAt(1); - samebytes = mdf.RemoveChannel(list.ToArray()); - var newmdf = new Mdf(samebytes); + samebytes = mdf.RemoveChannel(new ChannelBlock[] { list[9] }); + //var newmdf = new Mdf(samebytes); + var listValues = new List(); + + //for (int i = 0; i < mdf.DataGroups.Count; i++) + //{ + // var group = mdf.DataGroups[i]; + // var records = group.Records; + // for (int j = 0; j < records.Length; j++) + // { + // var value = records[j].GetValue(list[1]); + // if (value == null) + // continue; + // listValues.Add(Convert.ToDouble(value)); + // } + //} var ex = Path.GetExtension(filename); var file = Path.GetFileNameWithoutExtension(filename) + "Test"; From f80e9f0fc165849fa5c77aaad6e79021800f0aae Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 17 Apr 2024 17:18:06 +0300 Subject: [PATCH 20/34] Update to removing channelBlocks --- ASAM.MDF/Libary/ChannelBlock.cs | 18 ++++--- ASAM.MDF/Libary/ChannelGroupBlock.cs | 74 ++++++++++++++-------------- ASAM.MDF/Libary/DataGroupBlock.cs | 66 +++++++++++++------------ ASAM.MDF/Libary/Extensions.cs | 16 ++++-- ASAM.MDF/Libary/Mdf.cs | 8 ++- DebugOpenFileMdf/Program.cs | 9 +++- 6 files changed, 108 insertions(+), 83 deletions(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 1a695c9..80ce661 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -275,6 +275,9 @@ internal override void ReadV4() /// Copied modified the entire array of mdf bytes public List Remove(List bytes) { + if (TypeV3 == ChannelTypeV3.Time) + return bytes; + var previous = Previous; if (previous == null && next != null)// first of list node channels: [X channel]->[1 channel]->[2 channel]->...->[n channel] { @@ -292,7 +295,7 @@ public List Remove(List bytes) } if (Mdf.IDBlock.Version >= 400) - bytes = RemoveV4(bytes,previous); + bytes = RemoveV4(bytes, previous); else bytes = RemoveV23(bytes, previous); @@ -314,7 +317,8 @@ private List RemoveV23(List bytes, ChannelBlock previous) for (int i = thisPointer, j = 0; j < newbytes.Length; i++, j++) bytes[i] = newbytes[j]; - previous.ptrNextChannelBlock = ptrNextChannelBlock; + previous.ptrNextChannelBlock.address = ptrNextChannelBlock.address; + ptrNextChannelBlock.address = 0; return bytes; } @@ -327,7 +331,7 @@ private List RemoveV4(List bytes, ChannelBlock previous) for (int i = thisPointer, j = 0; j < newbytes.Length; i++, j++) bytes[i] = newbytes[j]; - previous.ptrNextChannelBlockV4 = ptrNextChannelBlockV4; + previous.ptrNextChannelBlockV4.address = ptrNextChannelBlockV4.address; return bytes; } @@ -417,11 +421,11 @@ private void ChannelUpdateAddressV23(int indexDeleted, List bytes, uint co { foreach (var ptr in listAddressesV23) { - if ((int)ptr.address > indexDeleted) + if ((int)ptr.address >= indexDeleted) { ptr.address -= countDeleted; - this.CopyAddress(ptr, bytes); + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); } } } @@ -430,11 +434,11 @@ private void ChannelUpdateAddressV4(int indexDeleted, List bytes, ulong co { foreach (var ptr in listAddressesV4) { - if ((int)ptr.address > indexDeleted) + if ((int)ptr.address >= indexDeleted) { ptr.address -= countDeleted; - this.CopyAddress(ptr, bytes); + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); } } } diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index bd375d3..c7685ad 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Net; public class ChannelGroupBlock : Block, INext, IPrevious, IParent { @@ -171,37 +172,17 @@ private void ChBlock_ChanelsRemovedAddress(ChannelBlock block, List bytes) private void RemoveChannelsV23(ChannelBlock block, List bytes) { - if (Channels[1] == block) //change first channel address on channelGroup - { - var nextBlock = block.Next; - if (nextBlock == null) - ptrFirstChannelBlock.address = 0; - else - ptrFirstChannelBlock.address = (uint)block.Next.BlockAddress; - - var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlock.offset; - var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock.address); - - for (int i = addressOfFirstChannel, j = 0; j < bytesFirstChannelAddress.Length; i++, j++) - bytes[i] = bytesFirstChannelAddress[j]; - } - - if (block.Previous == null && block.Next == null) - { - ptrFirstChannelBlock.address = 0; - var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlock.offset; - var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlock.address); - - for (int i = addressOfFirstChannel, j = 0; j < bytesFirstChannelAddress.Length; i++, j++) - bytes[i] = bytesFirstChannelAddress[j]; - - } - NumChannels -= 1; if (NumChannels > 1) { - var addressNumChannels = BlockAddress + ptrTextBlock.offset + 2/*RecordID*/; + var thisPointer = BlockAddress; + if (Previous != null) + thisPointer = (int)Previous.ptrNextChannelGroup.address; + else + thisPointer = (int)Parent.ptrFirstChannelGroupBlock.address; + + var addressNumChannels = thisPointer + ptrTextBlock.offset + 4 + 2/*RecordID*/; var newbytes = BitConverter.GetBytes(NumChannels); for (int i = addressNumChannels, j = 0; j < newbytes.Length; i++, j++) @@ -216,13 +197,13 @@ private void RemoveChannelsV4(ChannelBlock block, List bytes) if (Channels[0] == block) //change first channel address on channelGroup { var nextBlock = block.Next; - if (nextBlock == null) - ptrFirstChannelBlockV4.address = 0; - else - ptrFirstChannelBlockV4.address = (ulong)block.Next.BlockAddress; + var ptrFirst = 0ul; + + if (nextBlock != null) + ptrFirst = (ulong)block.Next.BlockAddress; var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlockV4.offset; - var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlockV4.address); + var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirst); for (int i = addressOfFirstChannel, j = 0; j < bytesFirstChannelAddress.Length; i++, j++) bytes[i] = bytesFirstChannelAddress[j]; @@ -231,9 +212,9 @@ private void RemoveChannelsV4(ChannelBlock block, List bytes) if (block.Previous == null && block.Next == null) { - ptrFirstChannelBlockV4.address = 0; + var ptrFirst = 0ul; var addressOfFirstChannel = BlockAddress + ptrFirstChannelBlockV4.offset; - var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirstChannelBlockV4.address); + var bytesFirstChannelAddress = BitConverter.GetBytes(ptrFirst); for (int i = addressOfFirstChannel, j = 0; j < bytesFirstChannelAddress.Length; i++, j++) bytes[i] = bytesFirstChannelAddress[j]; @@ -308,11 +289,11 @@ private void ChannelGroupUpdateAddressV23(int indexDeleted, List bytes, ui { foreach (var ptr in listAddressesV23) { - if ((int)ptr.address > indexDeleted) + if ((int)ptr.address >= indexDeleted) { ptr.address -= countDeleted; - this.CopyAddress(ptr, bytes); + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); } } } @@ -321,13 +302,30 @@ private void ChannelGroupUpdateAddressV4(int indexDeleted, List bytes, ulo { foreach (var ptr in listAddressesV4) { - if ((int)ptr.address > indexDeleted) + if ((int)ptr.address >= indexDeleted) { ptr.address -= countDeleted; - this.CopyAddress(ptr, bytes); + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); } } } + + internal void ClearDataType(List bytes) + { + RecordSize = 0; + var addressRecordSize = BlockAddress + ptrTextBlock.offset + 4 + 2/*RecordID*/ + 2/*NumChannels*/; + var newbytes = BitConverter.GetBytes(RecordSize); + + for (int i = addressRecordSize, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + + NumRecords = 0; + var addressNumRecords = BlockAddress + ptrTextBlock.offset + 4 + 2/*RecordID*/ + 2/*NumChannels*/ + 2/*RecordSize*/; + newbytes = BitConverter.GetBytes(NumRecords); + + for (int i = addressNumRecords, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + } } } diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index 6ef34a9..a9cbb45 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -198,42 +198,30 @@ private void RemoveChannelGroups(ChannelGroupBlock block, List bytes) { if (ptrFirstChannelGroupBlock.address == block.BlockAddress && NumChannelGroups == 1)// переписали указатель, который указывал на наш первый в списке channelGroup { - var ptrOffset = BlockAddress + ptrFirstChannelGroupBlock.offset; - var newbytes = BitConverter.GetBytes((uint)0); - for (int i = ptrOffset, j = 0; i < newbytes.Length; i++, j++) + var ptrFirst = 0u; + var thisPointer = BlockAddress; + if (Previous != null) + thisPointer = (int)Previous.ptrNextDataGroup.address; + + var ptrOffset = thisPointer + ptrFirstChannelGroupBlock.offset; + var newbytes = BitConverter.GetBytes(ptrFirst); + for (int i = ptrOffset, j = 0; j < newbytes.Length; i++, j++) bytes[i] = newbytes[j]; } else if (ptrFirstChannelGroupBlock.address == block.BlockAddress) { - if (block.Next != null) - { - var ptrOffset = BlockAddress + ptrFirstChannelGroupBlock.offset; - var newbytes = BitConverter.GetBytes((uint)block.Next.BlockAddress); - for (int i = ptrOffset, j = 0; i < newbytes.Length; i++, j++) - bytes[i] = newbytes[j]; - } - else - { - var ptrOffset = BlockAddress + ptrFirstChannelGroupBlock.offset; - var newbytes = BitConverter.GetBytes((uint)0); - for (int i = ptrOffset, j = 0; i < newbytes.Length; i++, j++) - bytes[i] = newbytes[j]; - } + var ptrFirst = (uint)block.Next.BlockAddress; + + var ptrOffset = BlockAddress + ptrFirstChannelGroupBlock.offset; + var newbytes = BitConverter.GetBytes(ptrFirst); + for (int i = ptrOffset, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + } //bytes.RemoveRange(block.BlockAddress, (int)block.Size);// удалили channelGroup //Mdf.UpdateAddresses(bytes, block.Size, block.BlockAddress); - - if (NumChannelGroups > 0) - { - NumChannelGroups -= 1; - - var numChannelGroupsOffset = BlockAddress + ptrDataBlock.offset; - var value = BitConverter.GetBytes((uint)NumChannelGroups); - for (int i = numChannelGroupsOffset, j = 0; i < value.Length; i++, j++) - bytes[i] = value[j]; - } } private void RemoveDataRecords(ChannelGroupBlock block, List bytes) @@ -248,8 +236,20 @@ private void RemoveDataRecords(ChannelGroupBlock block, List bytes) byteOffset += (int)cg.NumRecords * cg.RecordSize; } + if (NumChannelGroups > 0) + { + NumChannelGroups -= 1; + + var numChannelGroupsOffset = BlockAddress + ptrDataBlock.offset + 4; + var value = BitConverter.GetBytes(NumChannelGroups); + for (int i = numChannelGroupsOffset, j = 0; j < value.Length; i++, j++) + bytes[i] = value[j]; + } + var countRecord = (int)block.NumRecords * block.RecordSize; + block.ClearDataType(bytes); + var indexStarted = (int)ptrDataBlock.address + byteOffset; bytes.RemoveRange(indexStarted, countRecord);// удалили из файла данные @@ -319,13 +319,17 @@ internal DataRecord[] ReadRecords() for (int i = 0; i < NumChannelGroups; i++) { var group = ChannelGroups[i]; + var countData = 0; for (int k = 0; k < group.NumRecords; k++) { var recordData = Mdf.ReadBytes(group.RecordSize); recordsList.Add(new DataRecord(group, recordData)); + + countData += group.RecordSize; } + countData = 0; } } @@ -414,11 +418,11 @@ private void DataGroupUpdateAddressV23(int indexDeleted, List bytes, uint { foreach (var ptr in listAddressesV23) { - if ((int)ptr.address > indexDeleted) + if ((int)ptr.address >= indexDeleted) { ptr.address -= countDeleted; - this.CopyAddress(ptr, bytes); + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); } } } @@ -427,11 +431,11 @@ private void DataGroupUpdateAddressV4(int indexDeleted, List bytes, ulong { foreach (var ptr in listAddressesV4) { - if ((int)ptr.address > indexDeleted) + if ((int)ptr.address >= indexDeleted) { ptr.address -= countDeleted; - this.CopyAddress(ptr, bytes); + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); } } } diff --git a/ASAM.MDF/Libary/Extensions.cs b/ASAM.MDF/Libary/Extensions.cs index 29ea194..6ce865e 100644 --- a/ASAM.MDF/Libary/Extensions.cs +++ b/ASAM.MDF/Libary/Extensions.cs @@ -12,20 +12,28 @@ internal static class Extensions /// Block coping /// pointer to change /// byte massive for copy - internal static void CopyAddress(this T block, PointerAddress pointerData, List bytes) where T : Block + internal static void CopyAddress(this T block, PointerAddress pointerData, List bytes, int index, uint count) where T : Block { + var newBlockAddress = block.BlockAddress; + if (newBlockAddress > index) + newBlockAddress -= (int)count; + var newbytes = BitConverter.GetBytes(pointerData.address); - for (int i = block.BlockAddress + pointerData.offset, j = 0; j < newbytes.Length; i++, j++) + for (int i = newBlockAddress + pointerData.offset, j = 0; j < newbytes.Length; i++, j++) bytes[i] = newbytes[j]; //Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); } - internal static void CopyAddress(this T block, PointerAddress pointerData, List bytes) where T : Block + internal static void CopyAddress(this T block, PointerAddress pointerData, List bytes, int index, ulong count) where T : Block { + var newBlockAddress = block.BlockAddress; + if (newBlockAddress > index) + newBlockAddress -= (int)count; + var newbytes = BitConverter.GetBytes(pointerData.address); - for (int i = block.BlockAddress + pointerData.offset, j = 0; j < newbytes.Length; i++, j++) + for (int i = newBlockAddress + pointerData.offset, j = 0; j < newbytes.Length; i++, j++) bytes[i] = newbytes[j]; //Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); } diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index 6345eba..ce6a831 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -37,10 +37,11 @@ public Mdf() public byte[] RemoveChannel(ChannelBlock[] channelBlocks) { + var array = CheckChannelTimes(channelBlocks); //var bytes = new byte[Data.Length]; var bytes = new List(Data); var copiedMDF = new Mdf(bytes.ToArray()); - var BlockAddresses = channelBlocks.Select(x => x.BlockAddress); + var BlockAddresses = array.Select(x => x.BlockAddress); for (int i = 0; i < copiedMDF.DataGroups.Count; i++) { @@ -63,6 +64,11 @@ public byte[] RemoveChannel(ChannelBlock[] channelBlocks) return bytes.ToArray(); } + private ChannelBlock[] CheckChannelTimes(ChannelBlock[] channelBlocks) + { + return channelBlocks.Where(x => x.TypeV3 != Types.ChannelTypeV3.Time).ToArray(); + } + public byte[] GetBytes() { var array = new byte[GetSize()]; diff --git a/DebugOpenFileMdf/Program.cs b/DebugOpenFileMdf/Program.cs index 86fb52a..0ab18ed 100644 --- a/DebugOpenFileMdf/Program.cs +++ b/DebugOpenFileMdf/Program.cs @@ -1,5 +1,6 @@ using ASAM.MDF.Libary; using System.Runtime.InteropServices; +using System.Threading.Channels; var filename = ShowDialog(); try @@ -25,10 +26,14 @@ } } } - //list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); + list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); //list.RemoveAt(1); + var indexs = new int[] { 2, 8, 12 }; + var listRemoved = list.Where((x, i) => indexs.Contains(i)).ToArray(); + var anotherChannels = list.Where(x => !listRemoved.Contains(x)).ToArray(); - samebytes = mdf.RemoveChannel(new ChannelBlock[] { list[9] }); + + samebytes = mdf.RemoveChannel(anotherChannels); //var newmdf = new Mdf(samebytes); var listValues = new List(); From 063d5c48354c14680c00b615fd7b9c1cd893d946 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 18 Apr 2024 16:21:02 +0300 Subject: [PATCH 21/34] Added func removing channels --- ASAM.MDF/Libary/Block.cs | 5 +- ASAM.MDF/Libary/ChannelBlock.cs | 5 +- ASAM.MDF/Libary/ChannelConversionBlock.cs | 44 +++++++++--- ASAM.MDF/Libary/ChannelExtensionBlock.cs | 17 +++-- ASAM.MDF/Libary/ChannelGroupBlock.cs | 9 ++- ASAM.MDF/Libary/DataGroupBlock.cs | 84 +++++++++++++++++++---- ASAM.MDF/Libary/DataList.cs | 28 ++++++++ ASAM.MDF/Libary/Extensions.cs | 6 +- ASAM.MDF/Libary/HeaderBlock.cs | 82 ++++++++++++++++++---- ASAM.MDF/Libary/Mdf.cs | 20 ++++++ ASAM.MDF/Libary/SourceInformation.cs | 55 +++++++++++---- DebugOpenFileMdf/Program.cs | 2 +- 12 files changed, 288 insertions(+), 69 deletions(-) diff --git a/ASAM.MDF/Libary/Block.cs b/ASAM.MDF/Libary/Block.cs index 7ba90ea..2d96215 100644 --- a/ASAM.MDF/Libary/Block.cs +++ b/ASAM.MDF/Libary/Block.cs @@ -1,6 +1,7 @@ namespace ASAM.MDF.Libary { using System; + using System.Collections.Generic; using System.Text; /// @@ -8,6 +9,8 @@ /// public abstract class Block { + internal List> listAddressesV23; + internal List> listAddressesV4; protected Block(Mdf mdf) { if (mdf == null) @@ -25,7 +28,7 @@ protected Block(Mdf mdf) public ulong Size { get; protected set; } [MdfVersion(400, 0)] public ulong LinksCount { get; private set; } - public int BlockAddress { get; private set; } + public int BlockAddress { get; set; } internal virtual ushort GetSize() { diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 80ce661..3fd1057 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -14,9 +14,6 @@ public class ChannelBlock : Block, INext, IPrevious, private const int MIN_VERSION_DISPLAY_NAME = 300; private const int MIN_VERSION_ADDITIONAL_BYTE_OFFSET = 300; - private List> listAddressesV23; - private List> listAddressesV4; - internal PointerAddress ptrNextChannelBlock; internal PointerAddress ptrChannelConversionBlock; internal PointerAddress ptrDataBlockSignal; @@ -415,6 +412,8 @@ internal void ChannelUpdateAddress(int indexDeleted, List bytes, ulong cou ChannelUpdateAddressV4(indexDeleted,bytes, countDeleted); else ChannelUpdateAddressV23(indexDeleted, bytes, (uint)countDeleted); + + ChannelConversion?.ChannelConversionUpdateAddress(indexDeleted, bytes, countDeleted); } private void ChannelUpdateAddressV23(int indexDeleted, List bytes, uint countDeleted) diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index 4874b91..e5e2df9 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -19,7 +19,6 @@ public class ChannelConversionBlock : Block internal PointerAddress ptrInverseConversionV4; private string physicalUnit; - private int indexPointer; private ChannelConversionBlock(Mdf mdf) : base(mdf) { @@ -87,6 +86,8 @@ internal override void ReadV4() { base.ReadV4(); + listAddressesV4 = new List>(); + ptrTextBlockNameV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); ptrTextBlockUnitV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf),ptrTextBlockName.offset + 8); ptrFileCommentV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrTextBlockUnit.offset + 8); @@ -104,20 +105,28 @@ internal override void ReadV4() MinPhysicalValue = Mdf.ReadDouble(); MaxPhysicalValue = Mdf.ReadDouble(); - indexPointer = (int)Mdf.position; + listAddressesV4.AddRange(new PointerAddress[] + { + ptrTextBlockNameV4, + ptrTextBlockUnitV4, + ptrFileCommentV4, + ptrInverseConversionV4, + }); + + var indexPointer = Mdf.position; AdditionalConversionData.Data = new byte[ValParamCount * 8]; Array.Copy(Mdf.Data, indexPointer, AdditionalConversionData.Data, 0, AdditionalConversionData.Data.Length); - if (ptrFileComment.address != 0) - FileComment = TextBlock.Read(Mdf, (int)ptrFileComment.address); + if (ptrFileCommentV4.address != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrFileCommentV4.address); - if (ptrTextBlockName.address != 0) - ConversionName = TextBlock.Read(Mdf, (int)ptrTextBlockName.address); + if (ptrTextBlockNameV4.address != 0) + ConversionName = TextBlock.Read(Mdf, (int)ptrTextBlockNameV4.address); - if (ptrTextBlockUnit.address != 0) - ConversionUnit = TextBlock.Read(Mdf, (int)ptrTextBlockUnit.address); + if (ptrTextBlockUnitV4.address != 0) + ConversionUnit = TextBlock.Read(Mdf, (int)ptrTextBlockUnitV4.address); } internal override ushort GetSize() { @@ -154,5 +163,24 @@ internal override void Write(byte[] array, ref int index) index += GetSize(); } + + internal void ChannelConversionUpdateAddress(int indexDeleted, List bytes, ulong countDeleted) + { + if (Mdf.IDBlock.Version >= 400) + ChannelConversionUpdateAddressV4(indexDeleted, bytes, countDeleted); + } + + private void ChannelConversionUpdateAddressV4(int indexDeleted, List bytes, ulong countDeleted) + { + foreach (var ptr in listAddressesV4) + { + if ((int)ptr.address >= indexDeleted) + { + ptr.address -= countDeleted; + + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); + } + } + } } } diff --git a/ASAM.MDF/Libary/ChannelExtensionBlock.cs b/ASAM.MDF/Libary/ChannelExtensionBlock.cs index a3aef21..54550d7 100644 --- a/ASAM.MDF/Libary/ChannelExtensionBlock.cs +++ b/ASAM.MDF/Libary/ChannelExtensionBlock.cs @@ -12,8 +12,7 @@ public class ChannelExtensionBlock : Block public DimBlockSupplement DimBlockSupplement { get; private set; } public VectorCanBlockSupplement VectorCanBlockSupplement { get; private set; } - public ChannelExtensionBlock(Mdf mdf, ulong position) - : base(mdf) + public ChannelExtensionBlock(Mdf mdf, ulong position) : base(mdf) { byte[] data = new byte[Size - 4]; data = Mdf.Data.Take(data.Length).ToArray(); @@ -21,13 +20,13 @@ public ChannelExtensionBlock(Mdf mdf, ulong position) Type = (ExtensionType)BitConverter.ToUInt16(data, 0); if (Type == ExtensionType.DIM) - { - DimBlockSupplement = new DimBlockSupplement(mdf); - } - else if (Type == ExtensionType.VectorCAN) - { - VectorCanBlockSupplement = new VectorCanBlockSupplement(mdf); - } + { + DimBlockSupplement = new DimBlockSupplement(mdf); + } + else if (Type == ExtensionType.VectorCAN) + { + VectorCanBlockSupplement = new VectorCanBlockSupplement(mdf); + } } } } diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index c7685ad..c31a167 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -8,9 +8,6 @@ public class ChannelGroupBlock : Block, INext, IPrevious bytes); - private List> listAddressesV23; - private List> listAddressesV4; - internal PointerAddress ptrNextChannelGroup; internal PointerAddress ptrFirstChannelBlock; internal PointerAddress ptrTextName; @@ -66,6 +63,7 @@ public ChannelGroupBlock Next public SampleReductionCollection SampleReductions { get; private set; } public TextBlock TextName { get; private set; } public DataGroupBlock Parent { get; set; } + public SourceInformation SourceInformation { get; private set; } public static ChannelGroupBlock Create(Mdf mdf) { @@ -153,6 +151,9 @@ internal override void ReadV4() ptrTextBlockV4, }); + if (ptrSourceInfoV4.address != 0) + SourceInformation = SourceInformation.Read(Mdf, (int)ptrSourceInfoV4.address); + if (ptrTextBlockV4.address != 0) Comment = TextBlock.Read(Mdf, (int)ptrTextBlockV4.address); @@ -283,6 +284,8 @@ internal void ChannelGroupUpdateAddress(int indexDeleted, List bytes, ulon ChannelGroupUpdateAddressV4(indexDeleted, bytes, countDeleted); else ChannelGroupUpdateAddressV23(indexDeleted, bytes, (uint)countDeleted); + + SourceInformation?.SourceInformationUpdateAddress(indexDeleted, bytes, countDeleted); } private void ChannelGroupUpdateAddressV23(int indexDeleted, List bytes, uint countDeleted) diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index a9cbb45..276e5ec 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -5,9 +5,6 @@ public class DataGroupBlock : Block, INext, IPrevious, IParent { - internal List> listAddressesV23; - internal List> listAddressesV4; - internal PointerAddress ptrNextDataGroup; internal PointerAddress ptrFirstChannelGroupBlock; internal PointerAddress ptrTriggerBlock; @@ -186,17 +183,22 @@ internal override void ReadV4() private void CGBlock_ChannelGroupRemove(ChannelGroupBlock block, List bytes) { - // нужно удалить channelgroup, удалить из массива байтов - // также нужно удалить данные из data record. - RemoveChannelGroups(block, bytes); - - RemoveDataRecords(block, bytes); + if (Mdf.IDBlock.Version >= 400) + { + RemoveChannelGroupsV4(block, bytes); + RemoveDataRecordsV4(block, bytes); + } + else + { + RemoveChannelGroups(block, bytes); + RemoveDataRecords(block, bytes); + } } private void RemoveChannelGroups(ChannelGroupBlock block, List bytes) { - if (ptrFirstChannelGroupBlock.address == block.BlockAddress && NumChannelGroups == 1)// переписали указатель, который указывал на наш первый в списке channelGroup + if (ptrFirstChannelGroupBlock.address == block.BlockAddress && NumChannelGroups == 1) { var ptrFirst = 0u; var thisPointer = BlockAddress; @@ -218,10 +220,6 @@ private void RemoveChannelGroups(ChannelGroupBlock block, List bytes) bytes[i] = newbytes[j]; } - - //bytes.RemoveRange(block.BlockAddress, (int)block.Size);// удалили channelGroup - - //Mdf.UpdateAddresses(bytes, block.Size, block.BlockAddress); } private void RemoveDataRecords(ChannelGroupBlock block, List bytes) @@ -252,7 +250,65 @@ private void RemoveDataRecords(ChannelGroupBlock block, List bytes) var indexStarted = (int)ptrDataBlock.address + byteOffset; - bytes.RemoveRange(indexStarted, countRecord);// удалили из файла данные + bytes.RemoveRange(indexStarted, countRecord); + + Mdf.UpdateAddresses(bytes, (ulong)countRecord, indexStarted); + } + private void RemoveChannelGroupsV4(ChannelGroupBlock block, List bytes) + { + if (ptrFirstChannelGroupBlockV4.address == (ulong)block.BlockAddress && NumChannelGroups == 1) + { + var ptrFirst = 0u; + var thisPointer = BlockAddress; + if (Previous != null) + thisPointer = (int)Previous.ptrNextDataGroup.address; + + var ptrOffset = thisPointer + ptrFirstChannelGroupBlockV4.offset; + var newbytes = BitConverter.GetBytes(ptrFirst); + for (int i = ptrOffset, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + } + else if (ptrFirstChannelGroupBlockV4.address == (ulong)block.BlockAddress) + { + var ptrFirst = (uint)block.Next.BlockAddress; + + var ptrOffset = BlockAddress + ptrFirstChannelGroupBlockV4.offset; + var newbytes = BitConverter.GetBytes(ptrFirst); + for (int i = ptrOffset, j = 0; j < newbytes.Length; i++, j++) + bytes[i] = newbytes[j]; + + } + } + + private void RemoveDataRecordsV4(ChannelGroupBlock block, List bytes) + { + var byteOffset = 0; + for (int i = 0; i < NumChannelGroups; i++) + { + var cg = ChannelGroups[i]; + if (cg == block) + break; + + byteOffset += (int)cg.NumRecords * cg.RecordSize; + } + + if (NumChannelGroups > 0) + { + NumChannelGroups -= 1; + + var numChannelGroupsOffset = BlockAddress + ptrDataBlockV4.offset + 4; + var value = BitConverter.GetBytes(NumChannelGroups); + for (int i = numChannelGroupsOffset, j = 0; j < value.Length; i++, j++) + bytes[i] = value[j]; + } + + var countRecord = (int)block.NumRecords * block.RecordSize; + + block.ClearDataType(bytes); + + var indexStarted = (int)ptrDataBlockV4.address + byteOffset; + + bytes.RemoveRange(indexStarted, countRecord); Mdf.UpdateAddresses(bytes, (ulong)countRecord, indexStarted); } diff --git a/ASAM.MDF/Libary/DataList.cs b/ASAM.MDF/Libary/DataList.cs index 30d1326..575585c 100644 --- a/ASAM.MDF/Libary/DataList.cs +++ b/ASAM.MDF/Libary/DataList.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Security.Cryptography; namespace ASAM.MDF.Libary @@ -46,6 +47,8 @@ internal override void ReadV4() { base.ReadV4(); + listAddressesV4 = new List>(); + ptrNextDLV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); ptrDataBlockAddressV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf),ptrNextDLV4.offset + 8); Flags = (ListFlags)Mdf.ReadU16(); @@ -57,8 +60,33 @@ internal override void ReadV4() else BlockOffset = Mdf.Read64(); + listAddressesV4.AddRange(new PointerAddress[] + { + ptrNextDLV4, + ptrDataBlockAddressV4, + }); + if (ptrDataBlockAddressV4.address != 0) DataBlock = DataBlock.Read(Mdf, (int)ptrDataBlockAddressV4.address); } + + internal void DataListUpdateAddress(int indexDeleted, List bytes, ulong countDeleted) + { + if (Mdf.IDBlock.Version >= 400) + DataListUpdateAddressV4(indexDeleted, bytes, countDeleted); + } + + private void DataListUpdateAddressV4(int indexDeleted, List bytes, ulong countDeleted) + { + foreach (var ptr in listAddressesV4) + { + if ((int)ptr.address >= indexDeleted) + { + ptr.address -= countDeleted; + + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); + } + } + } } } diff --git a/ASAM.MDF/Libary/Extensions.cs b/ASAM.MDF/Libary/Extensions.cs index 6ce865e..82568c4 100644 --- a/ASAM.MDF/Libary/Extensions.cs +++ b/ASAM.MDF/Libary/Extensions.cs @@ -14,13 +14,9 @@ internal static class Extensions /// byte massive for copy internal static void CopyAddress(this T block, PointerAddress pointerData, List bytes, int index, uint count) where T : Block { - var newBlockAddress = block.BlockAddress; - if (newBlockAddress > index) - newBlockAddress -= (int)count; - var newbytes = BitConverter.GetBytes(pointerData.address); - for (int i = newBlockAddress + pointerData.offset, j = 0; j < newbytes.Length; i++, j++) + for (int i = block.BlockAddress + pointerData.offset, j = 0; j < newbytes.Length; i++, j++) bytes[i] = newbytes[j]; //Array.Copy(newbytes, 0, bytes, block.BlockAddress + pointerData.offset, newbytes.Length); diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index 81f4225..1f38256 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -1,10 +1,14 @@ namespace ASAM.MDF.Libary { using System; + using System.Collections.Generic; using ASAM.MDF.Libary.Types; public class HeaderBlock : Block { + internal List> listAddressesV23; + internal List> listAddressesV4; + private string author; private string date; private string organization; @@ -12,9 +16,13 @@ public class HeaderBlock : Block private string subject; private string time; private string timerIdentification; - internal (ulong address, int offest) ptrFirstDataGroup; - internal (ulong address, int offest) ptrTextBlock; - internal (ulong address, int offest) ptrProgramBlock; + internal PointerAddress ptrFirstDataGroup; + internal PointerAddress ptrTextBlock; + internal PointerAddress ptrProgramBlock; + + internal PointerAddress ptrFirstDataGroupV4; + internal PointerAddress ptrTextBlockV4; + internal PointerAddress ptrProgramBlockV4; /// /// Initializes a new instance of the class. @@ -224,9 +232,11 @@ internal override void ReadV23() { base.ReadV23(); - ptrFirstDataGroup = (Mdf.ReadU32().ValidateAddress(Mdf), 4); - ptrTextBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstDataGroup.offest + 4); - ptrProgramBlock = (Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstDataGroup.offest + 4); + listAddressesV23 = new List>(); + + ptrFirstDataGroup = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), 4); + ptrTextBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrFirstDataGroup.offset+ 4); + ptrProgramBlock = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrTextBlock.offset + 4); DataGroupsCount = Mdf.ReadU16(); @@ -237,6 +247,13 @@ internal override void ReadV23() Project = Mdf.GetString(32); Subject = Mdf.GetString(32); + listAddressesV23.AddRange(new PointerAddress[] + { + ptrFirstDataGroup, + ptrTextBlock, + ptrProgramBlock, + }); + if (Mdf.IDBlock.Version == 320) { TimeStamp = Mdf.ReadU64(); @@ -342,7 +359,9 @@ internal override void ReadV4() { base.ReadV4(); - ptrFirstDataGroup = (Mdf.ReadU64().ValidateAddress(Mdf), 24); //Adress DataGroup + listAddressesV4 = new List>(); + + ptrFirstDataGroupV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); //Adress DataGroup //skiped: FileHistoryBlock (not used) +8 //skiped: Chanel... (not used) +8 //skiped: AttachmentBlock (not used) +8 @@ -350,7 +369,7 @@ internal override void ReadV4() var skippedCount = 8 * 4; Mdf.UpdatePosition(Mdf.position + skippedCount); - ptrTextBlock = (Mdf.ReadU64().ValidateAddress(Mdf), skippedCount + ptrFirstDataGroup.offest); + ptrTextBlockV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), skippedCount + ptrFirstDataGroupV4.offset); StartTimeNs = Mdf.ReadU64(); TimeZoneOffsetMinutes = Mdf.Read16(); DstOffsetMinutes = Mdf.Read16(); @@ -361,15 +380,54 @@ internal override void ReadV4() StartAngle = Mdf.ReadDouble(); StartDistance = Mdf.ReadDouble(); - if (ptrTextBlock.address != 0) + listAddressesV4.AddRange(new PointerAddress[] { - FileComment = TextBlock.Read(Mdf, (int)ptrTextBlock.address); + ptrFirstDataGroupV4, + ptrTextBlockV4, + }); + + if (ptrTextBlockV4.address != 0) + { + FileComment = TextBlock.Read(Mdf, (int)ptrTextBlockV4.address); } // Check if ptrFirstDataGroup is null - if (ptrFirstDataGroup.address != 0) + if (ptrFirstDataGroupV4.address != 0) { - Mdf.DataGroups.Read(DataGroupBlock.Read(Mdf, (int)ptrFirstDataGroup.address)); + Mdf.DataGroups.Read(DataGroupBlock.Read(Mdf, (int)ptrFirstDataGroupV4.address)); + } + } + internal void HeaderUpdateAddress(int indexDeleted, List bytes, ulong countDeleted) + { + if (Mdf.IDBlock.Version >= 400) + HeaderUpdateAddressV4(indexDeleted, bytes, countDeleted); + else + HeaderUpdateAddressV23(indexDeleted, bytes, (uint)countDeleted); + } + + private void HeaderUpdateAddressV23(int indexDeleted, List bytes, uint countDeleted) + { + foreach (var ptr in listAddressesV23) + { + if ((int)ptr.address >= indexDeleted) + { + ptr.address -= countDeleted; + + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); + } + } + } + + private void HeaderUpdateAddressV4(int indexDeleted, List bytes, ulong countDeleted) + { + foreach (var ptr in listAddressesV4) + { + if ((int)ptr.address >= indexDeleted) + { + ptr.address -= countDeleted; + + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); + } } } } diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index ce6a831..6640049 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -102,23 +102,43 @@ internal void UpdateAddresses(List data, ulong countDeleted, int indexDele if (countDeleted == 0) return; + HDBlock.HeaderUpdateAddress(indexDeleted, bytes, countDeleted); + for (int i = 0; i < DataGroups.Count; i++) { var dataGroup = DataGroups[i]; + if (dataGroup.BlockAddress > indexDeleted) + dataGroup.BlockAddress -= (int)countDeleted; + dataGroup.DataGroupUpdateAddress(indexDeleted, bytes, countDeleted); for (int j = 0; j < dataGroup.ChannelGroups.Count; j++) { var channelGroup = dataGroup.ChannelGroups[j]; + if (channelGroup.BlockAddress > indexDeleted) + channelGroup.BlockAddress -= (int)countDeleted; + channelGroup.ChannelGroupUpdateAddress(indexDeleted, bytes, countDeleted); for (int k = 0; k < channelGroup.Channels.Count; k++) { var channel = channelGroup.Channels[k]; + if (channel.BlockAddress > indexDeleted) + channel.BlockAddress -= (int)countDeleted; + channel.ChannelUpdateAddress(indexDeleted, bytes, countDeleted); } } + for (int j = 0; j < dataGroup.DataListColl.Count; j++) + { + var dataList = dataGroup.DataListColl[j]; + + if (dataList.BlockAddress > indexDeleted) + dataList.BlockAddress -= (int)countDeleted; + + dataList.DataListUpdateAddress(indexDeleted, bytes, countDeleted); + } } } diff --git a/ASAM.MDF/Libary/SourceInformation.cs b/ASAM.MDF/Libary/SourceInformation.cs index 7c539b4..ec0dae3 100644 --- a/ASAM.MDF/Libary/SourceInformation.cs +++ b/ASAM.MDF/Libary/SourceInformation.cs @@ -1,10 +1,12 @@ -namespace ASAM.MDF.Libary +using System.Collections.Generic; + +namespace ASAM.MDF.Libary { public class SourceInformation : Block { - internal (ulong address, int offset) ptrTextName; - internal (ulong address, int offset) ptrTextPath; - internal (ulong address, int offset) ptrTextComment; + internal PointerAddress ptrTextNameV4; + internal PointerAddress ptrTextPathV4; + internal PointerAddress ptrTextCommentV4; public byte SourceType { get; private set; } public byte BusType { get; private set; } @@ -30,22 +32,49 @@ internal override void ReadV4() { base.ReadV4(); - ptrTextName = (Mdf.ReadU64().ValidateAddress(Mdf), 24); - ptrTextPath = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextName.offset + 8); - ptrTextComment = (Mdf.ReadU64().ValidateAddress(Mdf), ptrTextPath.offset + 8); + listAddressesV4 = new List>(); + + ptrTextNameV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), 24); + ptrTextPathV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrTextNameV4.offset + 8); + ptrTextCommentV4 = new PointerAddress(Mdf.ReadU64().ValidateAddress(Mdf), ptrTextPathV4.offset + 8); SourceType = Mdf.ReadByte(); BusType = Mdf.ReadByte(); SiFlags = Mdf.ReadByte(); Reserved1 = Mdf.ReadByte(); - if (ptrTextComment.address != 0) - FileComment = TextBlock.Read(Mdf, (int)ptrTextComment.address); + listAddressesV4.AddRange(new PointerAddress[] + { + ptrTextNameV4, + ptrTextPathV4, + ptrTextCommentV4, + }); + + if (ptrTextCommentV4.address != 0) + FileComment = TextBlock.Read(Mdf, (int)ptrTextCommentV4.address); - if (ptrTextName.address != 0) - TextBlockName = TextBlock.Read(Mdf, (int)ptrTextName.address); + if (ptrTextNameV4.address != 0) + TextBlockName = TextBlock.Read(Mdf, (int)ptrTextNameV4.address); + + if (ptrTextPathV4.address != 0) + TextBlockPath = TextBlock.Read(Mdf, (int)ptrTextPathV4.address); + } + internal void SourceInformationUpdateAddress(int indexDeleted, List bytes, ulong countDeleted) + { + if (Mdf.IDBlock.Version >= 400) + SourceInformationUpdateAddressV4(indexDeleted, bytes, countDeleted); + } + + private void SourceInformationUpdateAddressV4(int indexDeleted, List bytes, ulong countDeleted) + { + foreach (var ptr in listAddressesV4) + { + if ((int)ptr.address >= indexDeleted) + { + ptr.address -= countDeleted; - if (ptrTextPath.address != 0) - TextBlockPath = TextBlock.Read(Mdf, (int)ptrTextPath.address); + this.CopyAddress(ptr, bytes, indexDeleted, countDeleted); + } + } } } } diff --git a/DebugOpenFileMdf/Program.cs b/DebugOpenFileMdf/Program.cs index 0ab18ed..688e134 100644 --- a/DebugOpenFileMdf/Program.cs +++ b/DebugOpenFileMdf/Program.cs @@ -28,7 +28,7 @@ } list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); //list.RemoveAt(1); - var indexs = new int[] { 2, 8, 12 }; + var indexs = new int[] { 1, 2, 3, 4, 5 }; var listRemoved = list.Where((x, i) => indexs.Contains(i)).ToArray(); var anotherChannels = list.Where(x => !listRemoved.Contains(x)).ToArray(); From 3bec7210c7a6f59c1b9f6c0b6967104f592591a4 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 4 Jul 2024 12:55:17 +0300 Subject: [PATCH 22/34] Fix some problem with TextBlock --- ASAM.MDF/Libary/ChannelBlock.cs | 4 +-- ASAM.MDF/Libary/StreamReadHelper.cs | 4 +-- ASAM.MDF/Libary/TextBlock.cs | 4 +++ DebugOpenFileMdf/Program.cs | 47 ----------------------------- 4 files changed, 8 insertions(+), 51 deletions(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 3fd1057..32a0aff 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -174,10 +174,10 @@ internal override void ReadV23() } SampleRate = Mdf.ReadDouble(); - var offset = 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8; + var offset = ptrChannelComment.offset + 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8; if (Mdf.IDBlock.Version >= MIN_VERSION_LONG_SIGNAL_NAME) { - ptrLongSignalName = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), ptrChannelComment.offset + offset); + ptrLongSignalName = new PointerAddress(Mdf.ReadU32().ValidateAddress(Mdf), offset); offset += 4; listAddressesV23.Add(ptrLongSignalName); diff --git a/ASAM.MDF/Libary/StreamReadHelper.cs b/ASAM.MDF/Libary/StreamReadHelper.cs index fff4484..6600b77 100644 --- a/ASAM.MDF/Libary/StreamReadHelper.cs +++ b/ASAM.MDF/Libary/StreamReadHelper.cs @@ -74,11 +74,11 @@ public static char ReadChar(this Mdf mdf) public static bool ReadBoolean(this Mdf mdf) { - var value = BitConverter.ToUInt16(mdf.Data, mdf.position); + var value = BitConverter.ToBoolean(mdf.Data, mdf.position); mdf.position += 2; - return value != 0; + return value; } public static double ReadDouble(this Mdf mdf) { diff --git a/ASAM.MDF/Libary/TextBlock.cs b/ASAM.MDF/Libary/TextBlock.cs index f6f5b8e..5fb5e29 100644 --- a/ASAM.MDF/Libary/TextBlock.cs +++ b/ASAM.MDF/Libary/TextBlock.cs @@ -1,6 +1,7 @@ namespace ASAM.MDF.Libary { using System; + using System.Drawing; using System.IO; using System.Linq; using System.Text; @@ -62,6 +63,9 @@ internal static TextBlock Read(Mdf mdf, int position) var block = new TextBlock(mdf); block.Read(); + if (block.Size > (ulong)mdf.data.Length || block.Size <= 4) + return null; + block.Text = mdf.GetString((int)block.Size - (mdf.position - block.BlockAddress)); return block; diff --git a/DebugOpenFileMdf/Program.cs b/DebugOpenFileMdf/Program.cs index 688e134..78fc418 100644 --- a/DebugOpenFileMdf/Program.cs +++ b/DebugOpenFileMdf/Program.cs @@ -6,54 +6,7 @@ try { var bytes = File.ReadAllBytes(filename); - //var lists = new List(bytes); - var samebytes = new byte[0]; var mdf = new Mdf(bytes); - - var list = new List(); - - - for (int i = 0; i < mdf.DataGroups.Count; i++) - { - var group = mdf.DataGroups[i]; - for (int j = 0; j < group.ChannelGroups.Count; j++) - { - var channelGroup = group.ChannelGroups[j]; - for (int k = 0; k < channelGroup.Channels.Count; k++) - { - var channelBlock = channelGroup.Channels[k]; - list.Add(channelBlock); - } - } - } - list.Sort((x, y) => x.ToString().CompareTo(y.ToString())); - //list.RemoveAt(1); - var indexs = new int[] { 1, 2, 3, 4, 5 }; - var listRemoved = list.Where((x, i) => indexs.Contains(i)).ToArray(); - var anotherChannels = list.Where(x => !listRemoved.Contains(x)).ToArray(); - - - samebytes = mdf.RemoveChannel(anotherChannels); - //var newmdf = new Mdf(samebytes); - var listValues = new List(); - - //for (int i = 0; i < mdf.DataGroups.Count; i++) - //{ - // var group = mdf.DataGroups[i]; - // var records = group.Records; - // for (int j = 0; j < records.Length; j++) - // { - // var value = records[j].GetValue(list[1]); - // if (value == null) - // continue; - // listValues.Add(Convert.ToDouble(value)); - // } - //} - - var ex = Path.GetExtension(filename); - var file = Path.GetFileNameWithoutExtension(filename) + "Test"; - var path = Path.GetDirectoryName(filename) + "\\" + file + ex; - File.WriteAllBytes(path, samebytes); } catch (Exception e) { From 01f7a59b08e650b7b02159f0b42e46efd915d6b7 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Tue, 16 Jul 2024 18:37:47 +0300 Subject: [PATCH 23/34] Try create new writer mdf --- ASAM.MDF/Libary/Block.cs | 11 +++++ ASAM.MDF/Libary/DataGroupBlock.cs | 44 +++++++++++++++++++ ASAM.MDF/Libary/DataGroupCollection.cs | 33 +++++++++++++++ ASAM.MDF/Libary/HeaderBlock.cs | 58 ++++++++++++++++++++++++++ ASAM.MDF/Libary/IdentificationBlock.cs | 24 +++++++++++ ASAM.MDF/Libary/Mdf.cs | 22 +++++++++- ASAM.MDF/Libary/TextBlock.cs | 13 ++++++ 7 files changed, 204 insertions(+), 1 deletion(-) diff --git a/ASAM.MDF/Libary/Block.cs b/ASAM.MDF/Libary/Block.cs index 2d96215..3696ea6 100644 --- a/ASAM.MDF/Libary/Block.cs +++ b/ASAM.MDF/Libary/Block.cs @@ -63,6 +63,17 @@ internal virtual void Write(byte[] array, ref int index) Array.Copy(bytesSize, 0, array, index + 2, bytesSize.Length); } + internal virtual void Write(List array) + { + var size = array.Count; + + var bytesIdentifier = Encoding.UTF8.GetBytes(Identifier); + var bytesSize = BitConverter.GetBytes(size); + + array.InsertRange(0, bytesIdentifier); + array.InsertRange(1, bytesSize); + } + /// /// Sets the string value. /// diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index 276e5ec..371a85f 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -425,6 +425,44 @@ internal override void Write(byte[] array, ref int index) index += GetSize(); } + internal override void Write(List array) + { + var newList = new List(); + + var bytesNextGroupBlock = BitConverter.GetBytes(ptrNextDataGroup.address); + newList.AddRange(bytesNextGroupBlock); + + WriteFirstChannelGroupBlockLink(newList); + WriteTriggerBlockLink(newList); + WriteDataBlockLink(newList); + + var bytesNumChannelGroups = BitConverter.GetBytes(ChannelGroups.Count); + var bytesNumRecordsIds = BitConverter.GetBytes(NumRecordIds); + var bytesReserved = BitConverter.GetBytes(Reserved); + + newList.AddRange(bytesNumChannelGroups); + newList.AddRange(bytesNumRecordsIds); + newList.AddRange(bytesReserved); + + base.Write(newList); + + array.AddRange(newList); + } + + internal void WriteDataBlockLink(List newList) + { + var bytesDataBlockLink = BitConverter.GetBytes(ptrDataBlock.address); + + newList.AddRange(bytesDataBlockLink); + } + + internal void WriteTriggerBlockLink(List newList) + { + var bytesTriggerBlockLink = BitConverter.GetBytes(ptrTriggerBlock.address); + + newList.AddRange(bytesTriggerBlockLink); + } + internal void WriteChannelGroups(byte[] array, ref int index) { ChannelGroups.Write(array, ref index); @@ -435,6 +473,12 @@ internal void WriteFirstChannelGroupBlockLink(byte[] array, int index, int block Array.Copy(bytesFirst, 0, array, blockIndex + 8, bytesFirst.Length); } + internal void WriteFirstChannelGroupBlockLink(List array) + { + var bytesFirst = BitConverter.GetBytes(ptrFirstChannelGroupBlock.address); + + array.AddRange(bytesFirst); + } internal void WriteNextBlockLink(byte[] array, int index, int blockIndex) { var bytesNextBlockLink = BitConverter.GetBytes(index); diff --git a/ASAM.MDF/Libary/DataGroupCollection.cs b/ASAM.MDF/Libary/DataGroupCollection.cs index 6698a33..c293c3d 100644 --- a/ASAM.MDF/Libary/DataGroupCollection.cs +++ b/ASAM.MDF/Libary/DataGroupCollection.cs @@ -69,6 +69,39 @@ internal void Write(byte[] array, ref int index) block.WriteRecords(array, ref index, blockIndexes[i]); } } + internal void Write(List array) + { + var blockIndexes = new int[Count]; + + DataGroupBlock prev = null; + int prevIndex = 0; + + // Write block info. + for (int i = 0; i < Count; i++) + { + var block = items[i]; + + block.Write(array); + } + + // Write channel groups. + var firstChannelGroupIndex = index; + for (int i = 0; i < Count; i++) + { + var block = items[i]; + + block.WriteChannelGroups(array, ref index); + block.WriteFirstChannelGroupBlockLink(array, firstChannelGroupIndex, blockIndexes[i]); + } + + // Write records. + for (int i = 0; i < Count; i++) + { + var block = items[i]; + + block.WriteRecords(array); + } + } // IList. public int IndexOf(DataGroupBlock item) diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index 1f38256..c0e3bff 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -329,6 +329,44 @@ internal override void Write(byte[] array, ref int index) index += 44; } } + internal override void Write(List array) + { + var newList = new List(); + + var bytesFirstDataGroup = BitConverter.GetBytes(ptrFirstDataGroup.address); + newList.AddRange(bytesFirstDataGroup); + + WriteFileComment(newList); + WriteProgramBlock(newList); + + var bytesDataGroupsCount = BitConverter.GetBytes(Mdf.DataGroups.Count); + var bytesDate = Mdf.IDBlock.Encoding.GetBytes(Date); + var bytesTime = Mdf.IDBlock.Encoding.GetBytes(Time); + var bytesAuthor = Mdf.IDBlock.Encoding.GetBytes(Author); + var bytesOrganization = Mdf.IDBlock.Encoding.GetBytes(Organization); + var bytesProject = Mdf.IDBlock.Encoding.GetBytes(Project); + var bytesSubject = Mdf.IDBlock.Encoding.GetBytes(Subject); + var bytesTimestamp = BitConverter.GetBytes(TimeStamp); + var bytesUtcTimeOffset = BitConverter.GetBytes(UTCTimeOffset); + var bytesTimeQuality = BitConverter.GetBytes((ushort)TimeQuality); + var bytesTimeIdentification = Mdf.IDBlock.Encoding.GetBytes(TimerIdentification); + + newList.AddRange(bytesDataGroupsCount); + newList.AddRange(bytesDate); + newList.AddRange(bytesTime); + newList.AddRange(bytesAuthor); + newList.AddRange(bytesOrganization); + newList.AddRange(bytesProject); + newList.AddRange(bytesSubject); + newList.AddRange(bytesTimestamp); + newList.AddRange(bytesUtcTimeOffset); + newList.AddRange(bytesTimeQuality); + newList.AddRange(bytesTimeIdentification); + + base.Write(newList); + + array.AddRange(newList); + } internal void WriteFirstDataGroupLink(byte[] array, int index, int baseIndex) { var bytesFirstDataGroupLink = BitConverter.GetBytes(index); @@ -345,6 +383,16 @@ internal void WriteFileComment(byte[] array, ref int index, int baseIndex) FileComment.Write(array, ref index); } + internal void WriteFileComment(List array) + { + if (FileComment == null) return; + + var bytesFileCommentLink = BitConverter.GetBytes(FileComment.BlockAddress); + + array.AddRange(bytesFileCommentLink); + + FileComment.Write(array); + } internal void WriteProgramBlock(byte[] array, ref int index, int baseIndex) { if (ProgramBlock == null) return; @@ -355,6 +403,16 @@ internal void WriteProgramBlock(byte[] array, ref int index, int baseIndex) ProgramBlock.Write(array, ref index); } + internal void WriteProgramBlock(List array) + { + if (ProgramBlock == null) return; + + var bytesProgramLink = BitConverter.GetBytes(ProgramBlock.BlockAddress); + + array.AddRange(bytesProgramLink); + + ProgramBlock.Write(array); + } internal override void ReadV4() { base.ReadV4(); diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index a4544d3..e24cd58 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -1,6 +1,7 @@ namespace ASAM.MDF.Libary { using System; + using System.Collections.Generic; using System.IO; using System.Text; @@ -213,6 +214,29 @@ internal void Write(byte[] array, ref int index) index += GetSize(); } + internal void Write(List array) + { + var bytesFileIdentifier = Encoding.UTF8.GetBytes(FileIdentifier); + var bytesFormatIdentifier = Encoding.UTF8.GetBytes(FormatIdentifier); + var bytesProgramIdentifier = Encoding.UTF8.GetBytes(ProgramIdentifier); + var bytesByteOrder = BitConverter.GetBytes((ushort)ByteOrder); + var bytesFloatingPointFormat = BitConverter.GetBytes((ushort)FloatingPointFormat); + var bytesVersion = BitConverter.GetBytes(Version); + var bytesCodePage = BitConverter.GetBytes(CodePage); + var bytesReserved1 = Encoding.UTF8.GetBytes(Reserved1); + var bytesReserved2 = Encoding.UTF8.GetBytes(Reserved2); + + array.AddRange(bytesFileIdentifier); + array.AddRange(bytesFormatIdentifier); + array.AddRange(bytesProgramIdentifier); + array.AddRange(bytesByteOrder); + array.AddRange(bytesFloatingPointFormat); + array.AddRange(bytesVersion); + array.AddRange(bytesCodePage); + array.AddRange(bytesReserved1); + array.AddRange(bytesReserved2); + } + /// /// Sets the string value. /// diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index 6640049..d8970cf 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -88,9 +88,29 @@ public byte[] GetBytes() // DGBLOCKs. DataGroups.Write(array, ref index); - + + return array; + } + + public byte[] GetBytesMdf() + { + int index = 0; + var arrayList = new List(); + + IDBlock.Write(arrayList); + + int hdBlockIndex = index; + + // HDBLOCK. + HDBlock.Write(arrayList); + + // DGBLOCKs. + DataGroups.Write(arrayList); + return array; } + + /// /// /// diff --git a/ASAM.MDF/Libary/TextBlock.cs b/ASAM.MDF/Libary/TextBlock.cs index 5fb5e29..3eae031 100644 --- a/ASAM.MDF/Libary/TextBlock.cs +++ b/ASAM.MDF/Libary/TextBlock.cs @@ -1,6 +1,7 @@ namespace ASAM.MDF.Libary { using System; + using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; @@ -85,5 +86,17 @@ internal override void Write(byte[] array, ref int index) index += GetSize(); } + internal override void Write(List array) + { + var newList = new List(); + + var bytesText = Mdf.IDBlock.Encoding.GetBytes(Text); + + newList.AddRange(bytesText); + + base.Write(newList); + + array.AddRange(newList); + } } } From a363724b686b8cc8121ac0b9eafdb45b1ba1ff58 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 19 Jul 2024 15:52:46 +0300 Subject: [PATCH 24/34] return get size static --- ASAM.MDF/Libary/Block.cs | 2 +- ASAM.MDF/Libary/ChannelBlock.cs | 13 ++++++++++ ASAM.MDF/Libary/ChannelConversionBlock.cs | 2 +- ASAM.MDF/Libary/ChannelGroupBlock.cs | 4 +++ ASAM.MDF/Libary/DataGroupBlock.cs | 5 ++++ ASAM.MDF/Libary/DataGroupCollection.cs | 30 ----------------------- ASAM.MDF/Libary/HeaderBlock.cs | 7 ++++++ ASAM.MDF/Libary/IdentificationBlock.cs | 2 +- ASAM.MDF/Libary/Mdf.cs | 18 -------------- 9 files changed, 32 insertions(+), 51 deletions(-) diff --git a/ASAM.MDF/Libary/Block.cs b/ASAM.MDF/Libary/Block.cs index 3696ea6..78f176f 100644 --- a/ASAM.MDF/Libary/Block.cs +++ b/ASAM.MDF/Libary/Block.cs @@ -32,7 +32,7 @@ protected Block(Mdf mdf) internal virtual ushort GetSize() { - return (ushort)Size; + return 0; } internal virtual int GetSizeTotal() { diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 32a0aff..42ff26c 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -337,6 +337,19 @@ public override string ToString() { return SignalName; } + internal override ushort GetSize() + { + // Base size. + if (Mdf.IDBlock.Version < 212) + return 218; + + // 2.12 + if (Mdf.IDBlock.Version < 300) + return 222; + + // 3.00 + return 228; + } internal override int GetSizeTotal() { diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index e5e2df9..73af73d 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -130,7 +130,7 @@ internal override void ReadV4() } internal override ushort GetSize() { - ushort size = (ushort)Size; + ushort size = 46; if (AdditionalConversionData.Data != null) size += (ushort)AdditionalConversionData.Data.Length; diff --git a/ASAM.MDF/Libary/ChannelGroupBlock.cs b/ASAM.MDF/Libary/ChannelGroupBlock.cs index c31a167..4fd750a 100644 --- a/ASAM.MDF/Libary/ChannelGroupBlock.cs +++ b/ASAM.MDF/Libary/ChannelGroupBlock.cs @@ -222,6 +222,10 @@ private void RemoveChannelsV4(ChannelBlock block, List bytes) } } + internal override ushort GetSize() + { + return 30; + } internal override int GetSizeTotal() { var size = base.GetSizeTotal(); diff --git a/ASAM.MDF/Libary/DataGroupBlock.cs b/ASAM.MDF/Libary/DataGroupBlock.cs index 371a85f..ed718b8 100644 --- a/ASAM.MDF/Libary/DataGroupBlock.cs +++ b/ASAM.MDF/Libary/DataGroupBlock.cs @@ -392,6 +392,11 @@ internal DataRecord[] ReadRecords() return recordsList.ToArray(); } + internal override ushort GetSize() + { + return 28; + } + internal override int GetSizeTotal() { var size = base.GetSizeTotal(); diff --git a/ASAM.MDF/Libary/DataGroupCollection.cs b/ASAM.MDF/Libary/DataGroupCollection.cs index c293c3d..07a6d8b 100644 --- a/ASAM.MDF/Libary/DataGroupCollection.cs +++ b/ASAM.MDF/Libary/DataGroupCollection.cs @@ -71,36 +71,6 @@ internal void Write(byte[] array, ref int index) } internal void Write(List array) { - var blockIndexes = new int[Count]; - - DataGroupBlock prev = null; - int prevIndex = 0; - - // Write block info. - for (int i = 0; i < Count; i++) - { - var block = items[i]; - - block.Write(array); - } - - // Write channel groups. - var firstChannelGroupIndex = index; - for (int i = 0; i < Count; i++) - { - var block = items[i]; - - block.WriteChannelGroups(array, ref index); - block.WriteFirstChannelGroupBlockLink(array, firstChannelGroupIndex, blockIndexes[i]); - } - - // Write records. - for (int i = 0; i < Count; i++) - { - var block = items[i]; - - block.WriteRecords(array); - } } // IList. diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index c0e3bff..5d45768 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -288,6 +288,13 @@ internal override void ReadV23() } } + internal override ushort GetSize() + { + if (Mdf.IDBlock.Version >= 320) + return 208; + + return 164; + } internal override int GetSizeTotal() { return GetSize() + FileComment.GetSizeSafe() + ProgramBlock.GetSizeSafe(); diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index e24cd58..ad64c76 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -187,7 +187,7 @@ public static IdentificationBlock Read(Mdf mdf) internal int GetSize() { - return Size; + return 64; } internal void Write(byte[] array, ref int index) { diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index d8970cf..d6631e4 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -92,24 +92,6 @@ public byte[] GetBytes() return array; } - public byte[] GetBytesMdf() - { - int index = 0; - var arrayList = new List(); - - IDBlock.Write(arrayList); - - int hdBlockIndex = index; - - // HDBLOCK. - HDBlock.Write(arrayList); - - // DGBLOCKs. - DataGroups.Write(arrayList); - - return array; - } - /// /// From e843637327d7c1474c63d7578e895e57032b40e3 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Tue, 23 Jul 2024 15:31:09 +0300 Subject: [PATCH 25/34] Update ChannelBlock.cs --- ASAM.MDF/Libary/ChannelBlock.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 42ff26c..a7c58d6 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -260,8 +260,8 @@ internal override void ReadV4() if (ptrTextBlockCommentV4.address != 0) Comment = TextBlock.Read(Mdf, (int)ptrTextBlockCommentV4.address); - if (ptrLongSignalNameV4.address != 0) - LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalNameV4.address); + //if (ptrLongSignalNameV4.address != 0) + //LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalNameV4.address); if (channelConversion == null && ptrChannelConversionBlockV4.address != 0) ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlockV4.address); From c82be1c03a74d2a0995d83addcaebc6c8c2715ca Mon Sep 17 00:00:00 2001 From: Mikhail Date: Tue, 23 Jul 2024 16:48:38 +0300 Subject: [PATCH 26/34] Update TextBlock.cs --- ASAM.MDF/Libary/TextBlock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ASAM.MDF/Libary/TextBlock.cs b/ASAM.MDF/Libary/TextBlock.cs index 3eae031..cf0838f 100644 --- a/ASAM.MDF/Libary/TextBlock.cs +++ b/ASAM.MDF/Libary/TextBlock.cs @@ -74,7 +74,7 @@ internal static TextBlock Read(Mdf mdf, int position) internal override ushort GetSize() { - return (ushort)((int)Size + Text.Length); + return (ushort)(4 + Text.Length); } internal override void Write(byte[] array, ref int index) { From 15719093e7892b72bb0b6a8c3261bbb290f86610 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 24 Jul 2024 10:14:41 +0300 Subject: [PATCH 27/34] Update DataRecord.cs --- ASAM.MDF/Libary/DataRecord.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ASAM.MDF/Libary/DataRecord.cs b/ASAM.MDF/Libary/DataRecord.cs index 41f2651..71823b1 100644 --- a/ASAM.MDF/Libary/DataRecord.cs +++ b/ASAM.MDF/Libary/DataRecord.cs @@ -108,7 +108,8 @@ public object GetValue(ChannelBlock channel) break; case SignalTypeV3.String: - value = ChannelGroup.Mdf.IDBlock.Encoding.GetString(Data, byteOffset, channel.NumberOfBits / 8); + var valueString = ChannelGroup.Mdf.IDBlock.Encoding.GetString(Data, byteOffset, channel.NumberOfBits / 8).Replace("\0", ""); + value = string.IsNullOrEmpty(valueString) ? null : valueString; break; } From eb9cf36965982863ff002edfff626c7fe3acdf26 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 25 Jul 2024 15:16:06 +0300 Subject: [PATCH 28/34] Added write longsignalName --- ASAM.MDF/Libary/ChannelBlock.cs | 10 ++++++++++ ASAM.MDF/Libary/ChannelCollection.cs | 1 + 2 files changed, 11 insertions(+) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index a7c58d6..763d839 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -412,6 +412,16 @@ internal void WriteChannelConversion(byte[] array, ref int index, int blockIndex ChannelConversion.Write(array, ref index); } + internal void WriteLongSignalName(byte[] array, ref int index, int blockIndex) + { + if (ptrLongSignalName == null || Mdf.IDBlock.Version < 212) + return; + var bytesptrLongSignalName = BitConverter.GetBytes(index); + + Array.Copy(bytesptrLongSignalName, 0, array, blockIndex + ptrChannelComment.offset + 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8, bytesptrLongSignalName.Length); + + LongSignalName.Write(array, ref index); + } internal void WriteNextChannelLink(byte[] array, int index, int blockIndex) { var bytesNextChannelLink = BitConverter.GetBytes(index); diff --git a/ASAM.MDF/Libary/ChannelCollection.cs b/ASAM.MDF/Libary/ChannelCollection.cs index 64e6ba4..750079a 100644 --- a/ASAM.MDF/Libary/ChannelCollection.cs +++ b/ASAM.MDF/Libary/ChannelCollection.cs @@ -62,6 +62,7 @@ internal void Write(byte[] array, ref int index) block.Write(array, ref index); block.WriteChannelConversion(array, ref index, prevIndex); + block.WriteLongSignalName(array, ref index, prevIndex); } } From 7e0795a9ca087833133f6705c72e54e2b475ba61 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 26 Jul 2024 09:48:46 +0300 Subject: [PATCH 29/34] Added write DisplayName block --- ASAM.MDF/Libary/ChannelBlock.cs | 28 +++++++++++++++++----------- ASAM.MDF/Libary/ChannelCollection.cs | 1 + 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 763d839..0b49436 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -2,7 +2,6 @@ { using System; using System.Collections.Generic; - using System.Linq; using ASAM.MDF.Libary.Types; @@ -37,7 +36,6 @@ public class ChannelBlock : Block, INext, IPrevious, internal PointerAddress ptrChannelExtensionBlockV4; internal PointerAddress ptrChannelDependencyBlockV4; internal PointerAddress ptrChannelCommentV4; - internal PointerAddress ptrLongSignalNameV4; internal PointerAddress ptrDisplayNameV4; internal PointerAddress ptrComponentAddressV4; internal PointerAddress ptrTextBlockChanelNameV4; @@ -260,9 +258,6 @@ internal override void ReadV4() if (ptrTextBlockCommentV4.address != 0) Comment = TextBlock.Read(Mdf, (int)ptrTextBlockCommentV4.address); - //if (ptrLongSignalNameV4.address != 0) - //LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalNameV4.address); - if (channelConversion == null && ptrChannelConversionBlockV4.address != 0) ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlockV4.address); } @@ -386,11 +381,6 @@ internal override void Write(byte[] array, ref int index) Array.Copy(bytesMaxValue, 0, array, index + 202, bytesMaxValue.Length); Array.Copy(bytesSampleRate, 0, array, index + 210, bytesSampleRate.Length); - if (Mdf.IDBlock.Version >= 212) - { - // TODO: LongSignalName. - } - if (Mdf.IDBlock.Version >= 300) { // TODO: DisplayName. @@ -416,12 +406,28 @@ internal void WriteLongSignalName(byte[] array, ref int index, int blockIndex) { if (ptrLongSignalName == null || Mdf.IDBlock.Version < 212) return; + var bytesptrLongSignalName = BitConverter.GetBytes(index); - Array.Copy(bytesptrLongSignalName, 0, array, blockIndex + ptrChannelComment.offset + 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8, bytesptrLongSignalName.Length); + Array.Copy(bytesptrLongSignalName, 0, array, blockIndex + ptrLongSignalName.offset, bytesptrLongSignalName.Length); LongSignalName.Write(array, ref index); } + internal void WriteDisplayName(byte[] array, ref int index, int blockIndex) + { + if (ptrDisplayName == null || Mdf.IDBlock.Version < 300) + return; + + var bytesptrDisplayName = BitConverter.GetBytes(index); + var offset = ptrChannelComment.offset + 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8; + + if (ptrLongSignalName != null && ptrLongSignalName.address != 0) + offset += 4; + + Array.Copy(bytesptrDisplayName, 0, array, blockIndex + offset, bytesptrDisplayName.Length); + + DisplayName.Write(array, ref index); + } internal void WriteNextChannelLink(byte[] array, int index, int blockIndex) { var bytesNextChannelLink = BitConverter.GetBytes(index); diff --git a/ASAM.MDF/Libary/ChannelCollection.cs b/ASAM.MDF/Libary/ChannelCollection.cs index 750079a..522b255 100644 --- a/ASAM.MDF/Libary/ChannelCollection.cs +++ b/ASAM.MDF/Libary/ChannelCollection.cs @@ -63,6 +63,7 @@ internal void Write(byte[] array, ref int index) block.Write(array, ref index); block.WriteChannelConversion(array, ref index, prevIndex); block.WriteLongSignalName(array, ref index, prevIndex); + block.WriteDisplayName(array, ref index, prevIndex); } } From 1d41b09857180044f0007709766a8e58acb46597 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 26 Jul 2024 09:49:08 +0300 Subject: [PATCH 30/34] Update ChannelBlock.cs --- ASAM.MDF/Libary/ChannelBlock.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 0b49436..c909564 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -383,7 +383,6 @@ internal override void Write(byte[] array, ref int index) if (Mdf.IDBlock.Version >= 300) { - // TODO: DisplayName. var bytesAdditionalOffset = BitConverter.GetBytes(AdditionalByteOffset); Array.Copy(bytesAdditionalOffset, 0, array, index + 226, bytesAdditionalOffset.Length); From b83de2eeea8533bcf5ccb5aced4c99eb3cd5c705 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 26 Jul 2024 10:00:50 +0300 Subject: [PATCH 31/34] update read comment block and change setter property for write --- ASAM.MDF/Libary/ChannelBlock.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index c909564..6479906 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -71,7 +71,7 @@ public ChannelBlock Next public ChannelConversionBlock ChannelConversion { get => channelConversion; set => channelConversion = value; } public ChannelExtensionBlock SourceDepending { get; private set; } public ChannelDependencyBlock Dependency { get; private set; } - public TextBlock Comment { get; private set; } + public TextBlock Comment { get; set; } public ChannelTypeV3 TypeV3 { get; set; } public ChannelTypeV4 TypeV4 { get; set; } @@ -191,6 +191,9 @@ internal override void ReadV23() if (ptrLongSignalName != null && ptrLongSignalName.address != 0) LongSignalName = TextBlock.Read(Mdf, (int)ptrLongSignalName.address); + if (ptrChannelComment.address != 0) + Comment = TextBlock.Read(Mdf, (int)ptrChannelComment.address); + if (channelConversion == null && ptrChannelConversionBlock.address != 0) ChannelConversion = ChannelConversionBlock.Read(Mdf, (int)ptrChannelConversionBlock.address); //if (ptrChannelExtensionBlock != 0) From 58f8eb6fe4094e3d6f75bf64f860a58acddddf23 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 26 Jul 2024 11:17:07 +0300 Subject: [PATCH 32/34] fix writer method for channel properties Now correct wrtie size textblock for write. --- ASAM.MDF/Libary/ChannelBlock.cs | 32 +++++++++++++++++++++++----- ASAM.MDF/Libary/ChannelCollection.cs | 1 + ASAM.MDF/Libary/TextBlock.cs | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 6479906..4460e19 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -356,6 +356,15 @@ internal override int GetSizeTotal() if (channelConversion != null) size += channelConversion.GetSizeTotal(); + if (Comment != null) + size += Comment.GetSizeTotal(); + + if (LongSignalName != null) + size += LongSignalName.GetSizeTotal(); + + if (DisplayName != null) + size += DisplayName.GetSizeTotal(); + return size; } internal override void Write(byte[] array, ref int index) @@ -393,6 +402,17 @@ internal override void Write(byte[] array, ref int index) index += GetSize(); } + internal void WriteComment(byte[] array, ref int index, int blockIndex) + { + if (Comment == null) + return; + + var bytesComment = BitConverter.GetBytes(index); + + Array.Copy(bytesComment, 0, array, blockIndex + 20, bytesComment.Length); + + Comment.Write(array, ref index); + } internal void WriteChannelConversion(byte[] array, ref int index, int blockIndex) { if (channelConversion == null) @@ -406,24 +426,26 @@ internal void WriteChannelConversion(byte[] array, ref int index, int blockIndex } internal void WriteLongSignalName(byte[] array, ref int index, int blockIndex) { - if (ptrLongSignalName == null || Mdf.IDBlock.Version < 212) + if (LongSignalName == null || Mdf.IDBlock.Version < 212) return; var bytesptrLongSignalName = BitConverter.GetBytes(index); - Array.Copy(bytesptrLongSignalName, 0, array, blockIndex + ptrLongSignalName.offset, bytesptrLongSignalName.Length); + var offset = 20 + 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8; + + Array.Copy(bytesptrLongSignalName, 0, array, blockIndex + offset, bytesptrLongSignalName.Length); LongSignalName.Write(array, ref index); } internal void WriteDisplayName(byte[] array, ref int index, int blockIndex) { - if (ptrDisplayName == null || Mdf.IDBlock.Version < 300) + if (DisplayName == null || Mdf.IDBlock.Version < 300) return; var bytesptrDisplayName = BitConverter.GetBytes(index); - var offset = ptrChannelComment.offset + 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8; + var offset = 20 + 2 + 32 + 128 + 2 + 2 + 2 + 2 + 16 + 8; - if (ptrLongSignalName != null && ptrLongSignalName.address != 0) + if (LongSignalName != null) offset += 4; Array.Copy(bytesptrDisplayName, 0, array, blockIndex + offset, bytesptrDisplayName.Length); diff --git a/ASAM.MDF/Libary/ChannelCollection.cs b/ASAM.MDF/Libary/ChannelCollection.cs index 522b255..1c37344 100644 --- a/ASAM.MDF/Libary/ChannelCollection.cs +++ b/ASAM.MDF/Libary/ChannelCollection.cs @@ -62,6 +62,7 @@ internal void Write(byte[] array, ref int index) block.Write(array, ref index); block.WriteChannelConversion(array, ref index, prevIndex); + block.WriteComment(array, ref index, prevIndex); block.WriteLongSignalName(array, ref index, prevIndex); block.WriteDisplayName(array, ref index, prevIndex); } diff --git a/ASAM.MDF/Libary/TextBlock.cs b/ASAM.MDF/Libary/TextBlock.cs index cf0838f..ce75a53 100644 --- a/ASAM.MDF/Libary/TextBlock.cs +++ b/ASAM.MDF/Libary/TextBlock.cs @@ -74,7 +74,7 @@ internal static TextBlock Read(Mdf mdf, int position) internal override ushort GetSize() { - return (ushort)(4 + Text.Length); + return (ushort)(4 + Mdf.IDBlock.Encoding.GetBytes(Text).Length); } internal override void Write(byte[] array, ref int index) { From aa1389ebd80937d32dbb8dcc3961bd78b1ed62d1 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 26 Jul 2024 15:14:06 +0300 Subject: [PATCH 33/34] Added validate writer encoding --- ASAM.MDF/Libary/Block.cs | 8 ++++++++ ASAM.MDF/Libary/ChannelBlock.cs | 7 +++++-- ASAM.MDF/Libary/ChannelConversionBlock.cs | 3 +++ ASAM.MDF/Libary/HeaderBlock.cs | 10 ++++++++++ ASAM.MDF/Libary/IdentificationBlock.cs | 12 ++++++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/ASAM.MDF/Libary/Block.cs b/ASAM.MDF/Libary/Block.cs index 78f176f..c286057 100644 --- a/ASAM.MDF/Libary/Block.cs +++ b/ASAM.MDF/Libary/Block.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Linq; using System.Text; /// @@ -59,6 +60,8 @@ internal virtual void Write(byte[] array, ref int index) var bytesIdentifier = Encoding.UTF8.GetBytes(Identifier); var bytesSize = BitConverter.GetBytes(GetSize()); + ValidateEncodingLength(ref bytesIdentifier, 2); + Array.Copy(bytesIdentifier, 0, array, index, bytesIdentifier.Length); Array.Copy(bytesSize, 0, array, index + 2, bytesSize.Length); } @@ -113,5 +116,10 @@ internal virtual void ReadV4() Size = Mdf.ReadU64(); LinksCount = Mdf.ReadU64(); } + public void ValidateEncodingLength(ref byte[] bytes, int countConstraints) + { + if (bytes.Length > countConstraints) + bytes = bytes.Take(countConstraints).ToArray(); + } } } diff --git a/ASAM.MDF/Libary/ChannelBlock.cs b/ASAM.MDF/Libary/ChannelBlock.cs index 4460e19..f0c5320 100644 --- a/ASAM.MDF/Libary/ChannelBlock.cs +++ b/ASAM.MDF/Libary/ChannelBlock.cs @@ -2,7 +2,7 @@ { using System; using System.Collections.Generic; - + using System.Linq; using ASAM.MDF.Libary.Types; public class ChannelBlock : Block, INext, IPrevious, IParent @@ -370,7 +370,7 @@ internal override int GetSizeTotal() internal override void Write(byte[] array, ref int index) { base.Write(array, ref index); - + var bytesChannelType = BitConverter.GetBytes((ushort)TypeV3); var bytesSignalName = Mdf.IDBlock.Encoding.GetBytes(SignalName); var bytesSignalDesc = Mdf.IDBlock.Encoding.GetBytes(SignalDescription); @@ -382,6 +382,9 @@ internal override void Write(byte[] array, ref int index) var bytesMaxValue = BitConverter.GetBytes(MaxValue); var bytesSampleRate = BitConverter.GetBytes(SampleRate); + ValidateEncodingLength(ref bytesSignalName, 32); + ValidateEncodingLength(ref bytesSignalDesc, 128); + Array.Copy(bytesChannelType, 0, array, index + 24, bytesChannelType.Length); Array.Copy(bytesSignalName, 0, array, index + 26, bytesSignalName.Length); Array.Copy(bytesSignalDesc, 0, array, index + 58, bytesSignalDesc.Length); diff --git a/ASAM.MDF/Libary/ChannelConversionBlock.cs b/ASAM.MDF/Libary/ChannelConversionBlock.cs index 73af73d..8e7c044 100644 --- a/ASAM.MDF/Libary/ChannelConversionBlock.cs +++ b/ASAM.MDF/Libary/ChannelConversionBlock.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.IO; + using System.Linq; using System.Security.Cryptography; using ASAM.MDF.Libary.Types; @@ -146,6 +147,8 @@ internal override void Write(byte[] array, ref int index) var bytesMaxValue = BitConverter.GetBytes(MaxPhysicalValue); var bytesPhyUnit = Mdf.IDBlock.Encoding.GetBytes(PhysicalUnit); var bytesConversionType = BitConverter.GetBytes((ushort)ConversionType); + + ValidateEncodingLength(ref bytesPhyUnit, 20); Array.Copy(bytesValueRange, 0, array, index + 4, bytesValueRange.Length); Array.Copy(bytesMinValue, 0, array, index + 6, bytesMinValue.Length); diff --git a/ASAM.MDF/Libary/HeaderBlock.cs b/ASAM.MDF/Libary/HeaderBlock.cs index 5d45768..87bbe1e 100644 --- a/ASAM.MDF/Libary/HeaderBlock.cs +++ b/ASAM.MDF/Libary/HeaderBlock.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Linq; using ASAM.MDF.Libary.Types; public class HeaderBlock : Block @@ -316,6 +317,15 @@ internal override void Write(byte[] array, ref int index) var bytesTimeQuality = BitConverter.GetBytes((ushort)TimeQuality); var bytesTimeIdentification = Mdf.IDBlock.Encoding.GetBytes(TimerIdentification); + ValidateEncodingLength(ref bytesDate, 10); + ValidateEncodingLength(ref bytesTime, 8); + ValidateEncodingLength(ref bytesAuthor, 32); + ValidateEncodingLength(ref bytesOrganization, 32); + ValidateEncodingLength(ref bytesProject, 32); + ValidateEncodingLength(ref bytesSubject, 32); + ValidateEncodingLength(ref bytesTimeIdentification, 32); + + Array.Copy(bytesDataGroupsCount, 0, array, index + 16, bytesDataGroupsCount.Length); Array.Copy(bytesDate, 0, array, index + 18, bytesDate.Length); Array.Copy(bytesTime, 0, array, index + 28, bytesTime.Length); diff --git a/ASAM.MDF/Libary/IdentificationBlock.cs b/ASAM.MDF/Libary/IdentificationBlock.cs index ad64c76..0f73bba 100644 --- a/ASAM.MDF/Libary/IdentificationBlock.cs +++ b/ASAM.MDF/Libary/IdentificationBlock.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.IO; + using System.Linq; using System.Text; using ASAM.MDF.Libary.Types; @@ -201,6 +202,12 @@ internal void Write(byte[] array, ref int index) var bytesReserved1 = Encoding.UTF8.GetBytes(Reserved1); var bytesReserved2 = Encoding.UTF8.GetBytes(Reserved2); + ValidateEncodingLength(ref bytesFileIdentifier, 8); + ValidateEncodingLength(ref bytesFormatIdentifier, 8); + ValidateEncodingLength(ref bytesProgramIdentifier, 8); + ValidateEncodingLength(ref bytesReserved1, 2); + ValidateEncodingLength(ref bytesReserved2, 30); + Array.Copy(bytesFileIdentifier, 0, array, index, bytesFileIdentifier.Length); Array.Copy(bytesFormatIdentifier, 0, array, index + 8, bytesFormatIdentifier.Length); Array.Copy(bytesProgramIdentifier, 0, array, index + 16, bytesProgramIdentifier.Length); @@ -264,5 +271,10 @@ private static string GetString(Mdf mdf, int count) { return Encoding.UTF8.GetString(mdf.Data, mdf.AdvanceIndex(count), count); } + public void ValidateEncodingLength(ref byte[] bytes, int countConstraints) + { + if (bytes.Length > countConstraints) + bytes = bytes.Take(countConstraints).ToArray(); + } } } From 5a7570364ac223d545ff8b9d91f3126ba47292ea Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 6 Sep 2024 10:35:20 +0300 Subject: [PATCH 34/34] Update Mdf.cs --- ASAM.MDF/Libary/Mdf.cs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/ASAM.MDF/Libary/Mdf.cs b/ASAM.MDF/Libary/Mdf.cs index d6631e4..e5c47b7 100644 --- a/ASAM.MDF/Libary/Mdf.cs +++ b/ASAM.MDF/Libary/Mdf.cs @@ -6,8 +6,8 @@ public class Mdf { - internal int position; - internal byte[] data; + internal int position; //like stream position + internal byte[] data;// data bytes /// /// Read MDF from stream. @@ -35,10 +35,14 @@ public Mdf() internal byte[] Data => data; + /// + /// Removing channels from mdf + /// + /// An array of channels to delete + /// New file byte array public byte[] RemoveChannel(ChannelBlock[] channelBlocks) { var array = CheckChannelTimes(channelBlocks); - //var bytes = new byte[Data.Length]; var bytes = new List(Data); var copiedMDF = new Mdf(bytes.ToArray()); var BlockAddresses = array.Select(x => x.BlockAddress); @@ -68,7 +72,10 @@ private ChannelBlock[] CheckChannelTimes(ChannelBlock[] channelBlocks) { return channelBlocks.Where(x => x.TypeV3 != Types.ChannelTypeV3.Time).ToArray(); } - + /// + /// Converting an array from mdf to a bytes file. Not supported version 4 + /// + /// byte array public byte[] GetBytes() { var array = new byte[GetSize()]; @@ -94,9 +101,10 @@ public byte[] GetBytes() /// - /// + /// Updating the data addresses of all blocks after deletion /// - /// None deleted data + /// new data bytes + /// count deleted bytes /// Index start deleted on prevDataBytes internal void UpdateAddresses(List data, ulong countDeleted, int indexDeleted) { @@ -143,7 +151,9 @@ internal void UpdateAddresses(List data, ulong countDeleted, int indexDele } } } - + /// + /// We get the maximum possible file size + /// internal int GetSize() { var size = 0;