Skip to content

Commit

Permalink
Fix #3001: Support new resources format in ResourcesFile/ResXResource…
Browse files Browse the repository at this point in the history
…Writer
  • Loading branch information
siegfriedpammer committed Oct 6, 2024
1 parent f5c27c3 commit f9ae51b
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@
<Compile Include="ProjectDecompiler\TargetFrameworkTests.cs" />
<Compile Include="TestAssemblyResolver.cs" />
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />
<Compile Include="Util\ResourceReaderWriterTests.cs" />
<None Include="TestCases\VBPretty\VBAutomaticEvents.vb" />
<Compile Include="TestCases\VBPretty\VBAutomaticEvents.cs" />
<Compile Include="TestCases\VBPretty\VBNonGenericForEach.cs" />
Expand Down Expand Up @@ -356,7 +355,6 @@
<None Include="TestCases\VBPretty\VBCompoundAssign.vb" />
<None Include="TestCases\VBPretty\Async.vb" />
<None Include="TestCases\VBPretty\VBPropertiesTest.vb" />
<EmbeddedResource Include="Util\Test.resources" />
</ItemGroup>

</Project>
6 changes: 4 additions & 2 deletions ICSharpCode.Decompiler/Util/ResXResourceWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
using System.Text;
using System.Xml;

using ICSharpCode.Decompiler.Metadata;

namespace ICSharpCode.Decompiler.Util
{
#if INSIDE_SYSTEM_WEB
Expand Down Expand Up @@ -150,7 +152,7 @@ void WriteBytes(string name, string type, byte[] value, int offset, int length,
{
writer.WriteAttributeString("mimetype", BinSerializedObjectMimeType);
writer.WriteStartElement("value");
writer.WriteBase64(value, offset, length);
WriteNiceBase64(value, offset, length);
}

writer.WriteEndElement();
Expand Down Expand Up @@ -274,7 +276,7 @@ private void AddResource(string name, object value, string comment)
break;
case ResourceSerializedObject rso:
var bytes = rso.GetBytes();
WriteBytes(name, null, bytes, 0, bytes.Length, comment);
WriteBytes(name, rso.TypeName, bytes, 0, bytes.Length, comment);
break;
default:
throw new NotSupportedException($"Value '{value}' of type {value.GetType().FullName} is not supported by this version of ResXResourceWriter. Use byte arrays or streams instead.");
Expand Down
36 changes: 27 additions & 9 deletions ICSharpCode.Decompiler/Util/ResourcesFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,21 @@ enum ResourceTypeCode
StartOfUserTypes = 0x40
}

enum SerializationFormat
{
BinaryFormatter = 1,
TypeConverterByteArray = 2,
TypeConverterString = 3,
ActivatorStream = 4
}

/// <summary>Holds the number used to identify resource files.</summary>
public const int MagicNumber = unchecked((int)0xBEEFCACE);
const int ResourceSetVersion = 2;

readonly MyBinaryReader reader;
readonly int version;
readonly bool usesSerializationFormat;
readonly int numResources;
readonly string[] typeTable;
readonly int[] namePositions;
Expand All @@ -94,7 +103,7 @@ enum ResourceTypeCode
/// Creates a new ResourcesFile.
/// </summary>
/// <param name="stream">Input stream.</param>
/// <param name="leaveOpen">Whether the stream should be help open when the ResourcesFile is disposed.</param>
/// <param name="leaveOpen">Whether the stream should be held open when the ResourcesFile is disposed.</param>
/// <remarks>
/// The stream is must be held open while the ResourcesFile is in use.
/// The stream must be seekable; any operation using the ResourcesFile will end up seeking the stream.
Expand Down Expand Up @@ -130,7 +139,8 @@ public ResourcesFile(Stream stream, bool leaveOpen = true)
// We don't care about numBytesToSkip; read the rest of the header

// readerType:
reader.ReadString();
string readerType = reader.ReadString();
usesSerializationFormat = readerType == "System.Resources.Extensions.DeserializingResourceReader, System.Resources.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51";
// resourceSetType:
reader.ReadString();
}
Expand Down Expand Up @@ -369,7 +379,7 @@ string FindType(int typeIndex)
bits[i] = reader.ReadInt32();
return new decimal(bits);
default:
return new ResourceSerializedObject(FindType(typeIndex), this, reader.BaseStream.Position);
return new ResourceSerializedObject(FindType(typeIndex), this, reader.BaseStream.Position, usesSerializationFormat);
}
}

Expand Down Expand Up @@ -461,7 +471,7 @@ string FindType(int typeIndex)
{
throw new BadImageFormatException("Invalid typeCode");
}
return new ResourceSerializedObject(FindType(typeCode - ResourceTypeCode.StartOfUserTypes), this, reader.BaseStream.Position);
return new ResourceSerializedObject(FindType(typeCode - ResourceTypeCode.StartOfUserTypes), this, reader.BaseStream.Position, usesSerializationFormat);
}
}

Expand Down Expand Up @@ -509,7 +519,7 @@ long[] GetStartPositions()
}
}

internal byte[] GetBytesForSerializedObject(long pos)
internal byte[] GetBytesForSerializedObject(long pos, bool usesSerializationFormat)
{
long[] positions = GetStartPositions();
int i = Array.BinarySearch(positions, pos);
Expand All @@ -535,6 +545,12 @@ internal byte[] GetBytesForSerializedObject(long pos)
}
int len = (int)(endPos - pos);
reader.Seek(pos, SeekOrigin.Begin);
if (usesSerializationFormat)
{
int kind = reader.Read7BitEncodedInt();
Debug.Assert(Enum.IsDefined(typeof(SerializationFormat), kind));
len = reader.Read7BitEncodedInt();
}
return reader.ReadBytes(len);
}
}
Expand All @@ -545,28 +561,30 @@ public class ResourceSerializedObject
public string? TypeName { get; }
readonly ResourcesFile file;
readonly long position;
readonly bool usesSerializationFormat;

internal ResourceSerializedObject(string? typeName, ResourcesFile file, long position)
internal ResourceSerializedObject(string? typeName, ResourcesFile file, long position, bool usesSerializationFormat)
{
this.TypeName = typeName;
this.TypeName = usesSerializationFormat ? typeName : null;
this.file = file;
this.position = position;
this.usesSerializationFormat = usesSerializationFormat;
}

/// <summary>
/// Gets a stream that starts with the serialized object data.
/// </summary>
public Stream GetStream()
{
return new MemoryStream(file.GetBytesForSerializedObject(position), writable: false);
return new MemoryStream(file.GetBytesForSerializedObject(position, usesSerializationFormat), writable: false);
}

/// <summary>
/// Gets the serialized object data.
/// </summary>
public byte[] GetBytes()
{
return file.GetBytesForSerializedObject(position);
return file.GetBytesForSerializedObject(position, usesSerializationFormat);
}
}
}
Binary file added ILSpy.Tests/BitmapContainer.resources
Binary file not shown.
13 changes: 13 additions & 0 deletions ILSpy.Tests/ILSpy.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
<Compile Include="Analyzers\TestCases\MainAssembly.cs" />
<Compile Include="Analyzers\TypeUsedByAnalyzerTests.cs" />
<Compile Include="CommandLineArgumentsTests.cs" />
<Compile Include="ResourceReaderWriterTests.cs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Icon.ico" />
<EmbeddedResource Include="Test.resources" />
<EmbeddedResource Include="IconContainer.resx">
<Generator>ResXCodeGenerator</Generator>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="BitmapContainer.resources" />
</ItemGroup>

<ItemGroup>
Expand Down
Binary file added ILSpy.Tests/Icon.ico
Binary file not shown.
124 changes: 124 additions & 0 deletions ILSpy.Tests/IconContainer.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="Icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Icon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Loading

0 comments on commit f9ae51b

Please sign in to comment.