Skip to content

Commit

Permalink
feat: Decode MSI Stream names (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
learn-more authored Nov 3, 2021
1 parent b4db2bb commit 309f163
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
55 changes: 54 additions & 1 deletion src/LessMsi.Core/OleStorage/OleStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,58 @@ internal static bool IsCabStream(Stream bits)
bits.Seek(0, SeekOrigin.Begin);
return cabHeaderBits.SequenceEqual(buffer);
}


static char MimeToChar(int val)
{
if (val < 0)
{
return (char)('0' + val);
}
else if (val < (10 + 26))
{
return (char)('A' + val - 10);
}
else if (val < (10 + 26 + 26))
{
return (char)('a' + val - 10 - 26);
}
else if (val == (10 + 26 + 26))
{
return '.';
}
return '_';
}

// Based on ReactOS's / Wine's decode_streamname from msi/table.c
public static string DecodeName(string name)
{
var sb = new StringBuilder(name.Length * 2);
for (int n = 0; n < name.Length; ++n)
{
char ch = name[n];
if (ch >= 0x3800 && ch < 0x4800)
{
int c = ch - 0x3800;
sb.Append(MimeToChar(c & 0x3f));
sb.Append(MimeToChar((c >> 6) & 0x3f));
}
else if (ch >= 0x4800 && ch < 0x4840)
{
sb.Append(MimeToChar(ch - 0x4800));
}
else if (n == 0 && (ch == 0x4840 || ch == 0x5))
{
// The code from msi/table.c suggests that a table is always prefixed with 0x4840,
// but since we just want to display the name, skip it.
// The ENQ char (0x5) is the first char from some other tables, might as well skip that..
}
else
{
sb.Append(ch);
}
}
return sb.ToString();
}
}
}
}
9 changes: 8 additions & 1 deletion src/LessMsi.Gui/Model/StreamInfoView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace LessMsi.Gui.Model
internal sealed class StreamInfoView
{
private readonly string _name;
private readonly string _displayName;

public static StreamInfoView FromStream(System.IO.Packaging.StreamInfo si)
{
Expand All @@ -39,6 +40,7 @@ public static StreamInfoView FromStream(System.IO.Packaging.StreamInfo si)
private StreamInfoView(string name, bool isCabStream)
{
_name = name;
_displayName = OleStorage.OleStorageFile.DecodeName(name);
IsCabStream = isCabStream;
}

Expand All @@ -47,11 +49,16 @@ public string Name
get { return _name; }
}

public string DisplayName
{
get { return _displayName; }
}

public bool IsCabStream { get; private set; }

public string Label
{
get { return this.IsCabStream ? this.Name + " (CAB)" : this.Name; }
get { return this.IsCabStream ? this.DisplayName + " (CAB)" : this.DisplayName; }
}
}
}
1 change: 1 addition & 0 deletions src/Lessmsi.Tests/LessMsi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="MiscTests.cs" />
<Compile Include="MiscTestsNUnit.cs" />
<Compile Include="MspTests.cs" />
<Compile Include="OleStorageTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestBase.cs" />
</ItemGroup>
Expand Down
50 changes: 50 additions & 0 deletions src/Lessmsi.Tests/OleStorageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Diagnostics;
using System.Text;
using Xunit;

namespace LessMsi.Tests
{
public class OleStorageTests: TestBase
{
[DebuggerHidden]
static string FromRawBytes(byte[] data)
{
// Do not rely on encoding, build it manually!
Assert.Equal(0, data.Length % 2);
var sb = new StringBuilder(data.Length / 2);
for (int n = 0; n < data.Length; n+= 2)
{
char ch = (char)(data[n] << 8);
ch = (char)(ch | data[n + 1]);
sb.Append(ch);
}
return sb.ToString();
}

[Fact]
public void TestNameDecode()
{
// All names extracted from WPF2_32.msp using a c++ scratch project
var testdata = new[]
{
new { Result = "_Columns", Data = new byte[]{0x48, 0x40, 0x3b, 0x3f, 0x43, 0xf2, 0x44, 0x38, 0x45, 0xb1} }, // STGTY_STREAM
new { Result = "_Tables", Data = new byte[]{0x48, 0x40, 0x3f, 0x7f, 0x41, 0x64, 0x42, 0x2f, 0x48, 0x36 } }, // STGTY_STREAM
new { Result = "T1ToU1", Data = new byte[]{0x00, 0x54, 0x00, 0x31, 0x00, 0x54, 0x00, 0x6f, 0x00, 0x55, 0x00, 0x31 } }, // STGTY_STORAGE
new { Result = "#T1ToU1", Data = new byte[]{0x00, 0x23, 0x00, 0x54, 0x00, 0x31, 0x00, 0x54, 0x00, 0x6f, 0x00, 0x55, 0x00, 0x31 } }, // STGTY_STORAGE
new { Result = "PCW_CAB_NetFX", Data = new byte[]{0x3b, 0x19, 0x47, 0xe0, 0x3a, 0x8c, 0x47, 0xcb, 0x42, 0x17, 0x3b, 0xf7, 0x48, 0x21 } }, // STGTY_STREAM
new { Result = "_StringData", Data = new byte[]{0x48, 0x40, 0x3f, 0x3f, 0x45, 0x77, 0x44, 0x6c, 0x3b, 0x6a, 0x45, 0xe4, 0x48, 0x24 } }, // STGTY_STREAM
new { Result = "_StringPool", Data = new byte[]{0x48, 0x40, 0x3f, 0x3f, 0x45, 0x77, 0x44, 0x6c, 0x3e, 0x6a, 0x44, 0xb2, 0x48, 0x2f } }, // STGTY_STREAM
new { Result = "MsiPatchMetadata", Data = new byte[]{0x48, 0x40, 0x45, 0x96, 0x3e, 0x6c, 0x45, 0xe4, 0x42, 0xe6, 0x42, 0x16, 0x41, 0x37, 0x41, 0x27, 0x41, 0x37 } }, // STGTY_STREAM
new { Result = "MsiPatchSequence", Data = new byte[]{0x48, 0x40, 0x45, 0x96, 0x3e, 0x6c, 0x45, 0xe4, 0x42, 0xe6, 0x42, 0x1c, 0x46, 0x34, 0x44, 0x68, 0x42, 0x26 } }, // STGTY_STREAM
new { Result = "DigitalSignature", Data = new byte[]{0x00, 0x05, 0x00, 0x44, 0x00, 0x69, 0x00, 0x67, 0x00, 0x69, 0x00, 0x74, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x53, 0x00, 0x69, 0x00, 0x67, 0x00, 0x6e, 0x00, 0x61, 0x00, 0x74, 0x00, 0x75, 0x00, 0x72, 0x00, 0x65 } }, // STGTY_STREAM
new { Result = "SummaryInformation", Data = new byte[]{0x00, 0x05, 0x00, 0x53, 0x00, 0x75, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x61, 0x00, 0x72, 0x00, 0x79, 0x00, 0x49, 0x00, 0x6e, 0x00, 0x66, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x6d, 0x00, 0x61, 0x00, 0x74, 0x00, 0x69, 0x00, 0x6f, 0x00, 0x6e } }, // STGTY_STREAM
};

foreach(var entry in testdata)
{
string tmp = FromRawBytes(entry.Data);
Assert.Equal(entry.Result, OleStorage.OleStorageFile.DecodeName(tmp));
}
}
}
}

0 comments on commit 309f163

Please sign in to comment.