fixes alignment while reading from MIX
git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1098 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
@@ -52,7 +52,6 @@ namespace OpenRa.FileFormats
|
|||||||
}
|
}
|
||||||
|
|
||||||
byte[] data = Convert.FromBase64String( sb.ToString() );
|
byte[] data = Convert.FromBase64String( sb.ToString() );
|
||||||
Console.WriteLine( "Format80 data: {0}", data.Length );
|
|
||||||
|
|
||||||
List<byte[]> chunks = new List<byte[]>();
|
List<byte[]> chunks = new List<byte[]>();
|
||||||
|
|
||||||
@@ -69,7 +68,6 @@ namespace OpenRa.FileFormats
|
|||||||
int actualLength = Format80.DecodeInto( new MemoryStream( src ), dest );
|
int actualLength = Format80.DecodeInto( new MemoryStream( src ), dest );
|
||||||
|
|
||||||
chunks.Add( dest );
|
chunks.Add( dest );
|
||||||
Console.WriteLine( "Chunk length: {0}", actualLength );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( EndOfStreamException ) { }
|
catch( EndOfStreamException ) { }
|
||||||
|
|||||||
@@ -35,6 +35,7 @@
|
|||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Data" />
|
<Reference Include="System.Data" />
|
||||||
<Reference Include="System.Drawing" />
|
<Reference Include="System.Drawing" />
|
||||||
|
<Reference Include="System.Windows.Forms" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="BitmapBuilder.cs" />
|
<Compile Include="BitmapBuilder.cs" />
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace OpenRa.FileFormats
|
|||||||
readonly string filename;
|
readonly string filename;
|
||||||
readonly List<PackageEntry> index;
|
readonly List<PackageEntry> index;
|
||||||
readonly bool isRmix, isEncrypted;
|
readonly bool isRmix, isEncrypted;
|
||||||
|
readonly long dataStart;
|
||||||
|
|
||||||
public ICollection<PackageEntry> Content
|
public ICollection<PackageEntry> Content
|
||||||
{
|
{
|
||||||
@@ -30,26 +31,23 @@ namespace OpenRa.FileFormats
|
|||||||
if (isRmix)
|
if (isRmix)
|
||||||
{
|
{
|
||||||
isEncrypted = 0 != (signature & (uint)MixFileFlags.Encrypted);
|
isEncrypted = 0 != (signature & (uint)MixFileFlags.Encrypted);
|
||||||
index = ParseRaHeader(s);
|
if( isEncrypted )
|
||||||
}
|
{
|
||||||
else
|
index = ParseRaHeader( s, out dataStart );
|
||||||
{
|
return;
|
||||||
isEncrypted = false;
|
}
|
||||||
s.Seek(0, SeekOrigin.Begin);
|
|
||||||
index = ParseTdHeader(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isEncrypted = false;
|
||||||
|
s.Seek(0, SeekOrigin.Begin);
|
||||||
|
index = ParseTdHeader(s, out dataStart);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<PackageEntry> ParseRaHeader(Stream s)
|
const long headerStart = 84;
|
||||||
{
|
|
||||||
if (!isEncrypted)
|
|
||||||
{
|
|
||||||
Console.WriteLine("RA, not encrypted");
|
|
||||||
return ParseTdHeader(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
long headerStart = 84;
|
List<PackageEntry> ParseRaHeader(Stream s, out long dataStart)
|
||||||
|
{
|
||||||
BinaryReader reader = new BinaryReader(s);
|
BinaryReader reader = new BinaryReader(s);
|
||||||
byte[] keyblock = reader.ReadBytes(80);
|
byte[] keyblock = reader.ReadBytes(80);
|
||||||
byte[] blowfishKey = MixDecrypt.MixDecrypt.BlowfishKey(keyblock);
|
byte[] blowfishKey = MixDecrypt.MixDecrypt.BlowfishKey(keyblock);
|
||||||
@@ -57,15 +55,7 @@ namespace OpenRa.FileFormats
|
|||||||
uint[] h = ReadUints(reader, 2);
|
uint[] h = ReadUints(reader, 2);
|
||||||
|
|
||||||
Blowfish fish = new Blowfish(blowfishKey);
|
Blowfish fish = new Blowfish(blowfishKey);
|
||||||
uint[] decrypted = fish.Decrypt(h);
|
MemoryStream ms = Decrypt( h, fish );
|
||||||
|
|
||||||
MemoryStream ms = new MemoryStream();
|
|
||||||
BinaryWriter writer = new BinaryWriter(ms);
|
|
||||||
foreach (uint t in decrypted)
|
|
||||||
writer.Write(t);
|
|
||||||
writer.Flush();
|
|
||||||
|
|
||||||
ms.Position = 0;
|
|
||||||
BinaryReader reader2 = new BinaryReader(ms);
|
BinaryReader reader2 = new BinaryReader(ms);
|
||||||
|
|
||||||
ushort numFiles = reader2.ReadUInt16();
|
ushort numFiles = reader2.ReadUInt16();
|
||||||
@@ -76,18 +66,29 @@ namespace OpenRa.FileFormats
|
|||||||
s.Position = headerStart;
|
s.Position = headerStart;
|
||||||
reader = new BinaryReader(s);
|
reader = new BinaryReader(s);
|
||||||
|
|
||||||
h = ReadUints(reader, 2 + numFiles * PackageEntry.Size / 4);
|
int byteCount = 6 + numFiles * PackageEntry.Size;
|
||||||
decrypted = fish.Decrypt(h);
|
h = ReadUints( reader, ( byteCount + 3 ) / 4 );
|
||||||
|
|
||||||
ms = new MemoryStream();
|
ms = Decrypt( h, fish );
|
||||||
writer = new BinaryWriter(ms);
|
|
||||||
foreach (uint t in decrypted)
|
dataStart = headerStart + byteCount + ( ( ~byteCount + 1 ) & 7 );
|
||||||
writer.Write(t);
|
|
||||||
|
long ds;
|
||||||
|
return ParseTdHeader( ms, out ds );
|
||||||
|
}
|
||||||
|
|
||||||
|
static MemoryStream Decrypt( uint[] h, Blowfish fish )
|
||||||
|
{
|
||||||
|
uint[] decrypted = fish.Decrypt( h );
|
||||||
|
|
||||||
|
MemoryStream ms = new MemoryStream();
|
||||||
|
BinaryWriter writer = new BinaryWriter( ms );
|
||||||
|
foreach( uint t in decrypted )
|
||||||
|
writer.Write( t );
|
||||||
writer.Flush();
|
writer.Flush();
|
||||||
|
|
||||||
ms.Position = 0;
|
ms.Position = 0;
|
||||||
|
return ms;
|
||||||
return ParseTdHeader(ms);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint[] ReadUints(BinaryReader r, int count)
|
uint[] ReadUints(BinaryReader r, int count)
|
||||||
@@ -99,7 +100,7 @@ namespace OpenRa.FileFormats
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<PackageEntry> ParseTdHeader(Stream s)
|
List<PackageEntry> ParseTdHeader(Stream s, out long dataStart)
|
||||||
{
|
{
|
||||||
List<PackageEntry> items = new List<PackageEntry>();
|
List<PackageEntry> items = new List<PackageEntry>();
|
||||||
|
|
||||||
@@ -110,6 +111,7 @@ namespace OpenRa.FileFormats
|
|||||||
for (int i = 0; i < numFiles; i++)
|
for (int i = 0; i < numFiles; i++)
|
||||||
items.Add(new PackageEntry(reader));
|
items.Add(new PackageEntry(reader));
|
||||||
|
|
||||||
|
dataStart = s.Position;
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,16 +122,7 @@ namespace OpenRa.FileFormats
|
|||||||
{
|
{
|
||||||
using (Stream s = File.OpenRead(filename))
|
using (Stream s = File.OpenRead(filename))
|
||||||
{
|
{
|
||||||
s.Seek(2 + 4 + (isRmix ? 4 : 0), SeekOrigin.Begin);
|
s.Seek( dataStart + e.Offset, SeekOrigin.Begin );
|
||||||
|
|
||||||
s.Seek(2, SeekOrigin.Current); //dword align
|
|
||||||
s.Seek( 4, SeekOrigin.Current ); //wtf, i dont know why i need this either :(
|
|
||||||
|
|
||||||
if( isEncrypted )
|
|
||||||
s.Seek(80, SeekOrigin.Current);
|
|
||||||
|
|
||||||
s.Seek( index.Count * PackageEntry.Size + e.Offset, SeekOrigin.Current );
|
|
||||||
|
|
||||||
byte[] data = new byte[ e.Length ];
|
byte[] data = new byte[ e.Length ];
|
||||||
s.Read( data, 0, (int)e.Length );
|
s.Read( data, 0, (int)e.Length );
|
||||||
return new MemoryStream(data);
|
return new MemoryStream(data);
|
||||||
|
|||||||
Reference in New Issue
Block a user