Skip to content

Commit

Permalink
fix: figured out how to properly decode the string table in a library…
Browse files Browse the repository at this point in the history
… file

now we still need to know where the strings are actually used instead of blindly using them
  • Loading branch information
iadonkey committed Dec 6, 2023
1 parent 4a88b9e commit 25dadbb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
42 changes: 22 additions & 20 deletions TwinpackShared/LibraryPropertyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,36 +61,36 @@ public static LibraryInfo Read(byte[] libraryBinary)
{
var values = new List<string>();

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<byte>();

// 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();

Expand Down
19 changes: 19 additions & 0 deletions TwinpackTests/LibraryPropertyReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
5 changes: 5 additions & 0 deletions TwinpackTests/TwinpackTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="assets\Untitled1.library">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="..\TwinpackShared\TwinpackShared.projitems" Label="Shared" />
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
Binary file added TwinpackTests/assets/Untitled1.library
Binary file not shown.

0 comments on commit 25dadbb

Please sign in to comment.