diff --git a/TwinpackShared/LibraryPropertyReader.cs b/TwinpackShared/LibraryPropertyReader.cs index b7d1b88..26ee3ee 100644 --- a/TwinpackShared/LibraryPropertyReader.cs +++ b/TwinpackShared/LibraryPropertyReader.cs @@ -25,11 +25,13 @@ using NLog; using NLog.LayoutRenderers; using System; +using System.Collections; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Runtime.InteropServices; +using System.Runtime.Remoting.Messaging; using System.Text; using System.Text.RegularExpressions; using Twinpack.Models; @@ -59,36 +61,36 @@ public static LibraryInfo Read(byte[] libraryBinary) { var values = new List(); + int ReadLength(BinaryReader reader) + { + byte lengthByte = reader.ReadByte(); + int length = lengthByte; + if (length > 128) // check if last bit is set + { + lengthByte = reader.ReadByte(); + length = (length - 128) + lengthByte * 128; + } + + return length; + } + using (var memoryStream = new MemoryStream(libraryBinary)) using(var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Read)) { var stream = zipArchive.Entries.Where(x => x.Name == "__shared_data_storage_string_table__.auxiliary")?.FirstOrDefault().Open(); - byte index = 0; try { using (var reader = new BinaryReader(stream)) { - var buffer = new List(); - - // read until we find index=0, which indicates we found the first string - while (true) - { - var b = reader.ReadByte(); - if (b == 0) - break; - - buffer.Add(b); - } - - // now we know the number of strings and can iterate over them, storing them in a list of strings - // todo: buffer[0] is sufficent to get the data we need, - var objects = buffer[0] - 1; - while (true) + int objects = ReadLength(reader); + var index = reader.ReadByte(); + while (index < objects && index < 128) { - byte length = reader.ReadByte(); - string val = Encoding.ASCII.GetString(reader.ReadBytes(length)); - values.Add(val); + var length = ReadLength(reader); + byte[] data = reader.ReadBytes(length); + string str = Encoding.UTF8.GetString(data); + values.Add(str); var nextIndex = reader.ReadByte(); diff --git a/TwinpackTests/LibraryPropertyReaderTest.cs b/TwinpackTests/LibraryPropertyReaderTest.cs index c4be28c..25b5009 100644 --- a/TwinpackTests/LibraryPropertyReaderTest.cs +++ b/TwinpackTests/LibraryPropertyReaderTest.cs @@ -40,5 +40,24 @@ public void TestWithPlaceholder() Assert.AreEqual("*", libraryInfo.Dependencies[3].Version); Assert.AreEqual("Tc3_Module", libraryInfo.Dependencies[3].Name); } + + [TestMethod] + public void TestWithLongDescription() + { + var library = File.ReadAllBytes(@"assets\Untitled1.library"); + var libraryInfo = Twinpack.LibraryPropertyReader.Read(library); + + Assert.IsNotNull(library); + Assert.AreEqual("XY", libraryInfo.Company); + Assert.AreEqual("XY", libraryInfo.Title); + Assert.AreEqual("1.0.0.0", libraryInfo.Version); + Assert.AreEqual(@"123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz 123456790abcdefghijklmnopqrstuvwxyz ", libraryInfo.Description); + Assert.AreEqual("", libraryInfo.Author); + Assert.AreEqual("XY", libraryInfo.DefaultNamespace); + Assert.AreEqual(1, libraryInfo.Dependencies.Count); + Assert.AreEqual("Beckhoff Automation GmbH", libraryInfo.Dependencies[3].DistributorName); + Assert.AreEqual("*", libraryInfo.Dependencies[3].Version); + Assert.AreEqual("Tc3_Module", libraryInfo.Dependencies[3].Name); + } } } diff --git a/TwinpackTests/TwinpackTests.csproj b/TwinpackTests/TwinpackTests.csproj index 2792bf9..5b234c6 100644 --- a/TwinpackTests/TwinpackTests.csproj +++ b/TwinpackTests/TwinpackTests.csproj @@ -104,6 +104,11 @@ PreserveNewest + + + PreserveNewest + + diff --git a/TwinpackTests/assets/Untitled1.library b/TwinpackTests/assets/Untitled1.library new file mode 100644 index 0000000..1b462f8 Binary files /dev/null and b/TwinpackTests/assets/Untitled1.library differ