Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections;
using System.Diagnostics;

#if HOST_MODEL
namespace Microsoft.NET.HostModel.Win32Resources
Expand All @@ -26,6 +27,8 @@ private void AddResourceInternal(object name, object type, ushort language, byte
}
else
{
Debug.Assert(type is string);
type = ToUpperForResource((string)type);
if (!_resTypeHeadName.TryGetValue((string)type, out resType))
{
resType = new ResType();
Expand All @@ -45,6 +48,8 @@ private void AddResourceInternal(object name, object type, ushort language, byte
}
else
{
Debug.Assert(name is string);
name = ToUpperForResource((string)name);
if (!resType.NameHeadName.TryGetValue((string)name, out resName))
{
resName = new ResName();
Expand All @@ -57,28 +62,32 @@ private void AddResourceInternal(object name, object type, ushort language, byte

private byte[] FindResourceInternal(object name, object type, ushort language)
{
ResType resType = null;
ResType resType;

if (type is ushort)
{
_resTypeHeadID.TryGetValue((ushort)type, out resType);
}
if (type is string)
else
{
Debug.Assert(type is string);
type = ToUpperForResource((string)type);
_resTypeHeadName.TryGetValue((string)type, out resType);
}

if (resType == null)
return null;

ResName resName = null;
ResName resName;

if (name is ushort)
{
resType.NameHeadID.TryGetValue((ushort)name, out resName);
}
if (name is string)
else
{
Debug.Assert(name is string);
name = ToUpperForResource((string)name);
resType.NameHeadName.TryGetValue((string)name, out resName);
}

Expand All @@ -90,5 +99,18 @@ private byte[] FindResourceInternal(object name, object type, ushort language)

return (byte[])resLanguage.DataEntry.Clone();
}

private static string ToUpperForResource(string str)
{
// Undocumented semantic for Win32 Resource APIs
// This ToUpper logic should only be applied to the
// latin range from 'a' to 'z'.
System.Text.StringBuilder builder = new(str.Length);
foreach (char c in str)
{
builder.Append('a' <= c && c <= 'z' ? (char)(c + ('A' - 'a')) : c);
}
return builder.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void Create(
if (tlbFileBytes.Length == 0)
throw new InvalidTypeLibraryException(typeLibrary.Value);

updater.AddResource(tlbFileBytes, "typelib", (IntPtr)typeLibrary.Key);
updater.AddResource(tlbFileBytes, "TYPELIB", (IntPtr)typeLibrary.Key);
}
catch (FileNotFoundException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,37 @@ void AddResource_AddToPEWithoutRsrc()
}
}

[Fact]
void AddResource_DifferentCasingInAPIs()
{
using var tempFile = CreateTestPEFileWithoutRsrc();

using (var updater = new ResourceUpdater(tempFile.Stream, true))
{
updater.AddResource("Test Resource1"u8.ToArray(), "testtype1", 0);
updater.AddResource("Test Resource2"u8.ToArray(), "TESTTYPE2", 0);
updater.AddResource("Test Resource3"u8.ToArray(), "\u0165ESTTYPE3", 0);
updater.Update();
}

tempFile.Stream.Seek(0, SeekOrigin.Begin);

using (var reader = new PEReader(tempFile.Stream, PEStreamOptions.LeaveOpen))
{
var resourceReader = new ResourceData(reader);
byte[]? testType1 = resourceReader.FindResource(0, "TESTTYPE1", 0);
Assert.Equal("Test Resource1"u8.ToArray(), testType1);
byte[]? testType2 = resourceReader.FindResource(0, "testtype2", 0);
Assert.Equal("Test Resource2"u8.ToArray(), testType2);
byte[]? testType3 = resourceReader.FindResource(0, "\u0165esttype3", 0);
Assert.Equal("Test Resource3"u8.ToArray(), testType3);

// Characters out of 'a' - 'z' are left unaltered
byte[]? missing = resourceReader.FindResource(0, "\u0164ESTTYPE3", 0);
Assert.Null(missing);
}
}

[Fact]
void AddResource_AddToExistingRsrc()
{
Expand Down