replaced ZLibNet with .Net DeflateStream class.

This commit is contained in:
morkt 2015-07-28 12:20:20 +04:00
parent 8eea67faee
commit 002298bb93
21 changed files with 51 additions and 61 deletions

View File

@ -33,7 +33,7 @@ using System.Globalization;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using ZLibNet; using GameRes.Compression;
using GameRes.Formats.Strings; using GameRes.Formats.Strings;
using GameRes.Formats.Properties; using GameRes.Formats.Properties;
@ -303,6 +303,7 @@ namespace GameRes.Formats
throw new FileFormatException ("GRP image encoder not available"); throw new FileFormatException ("GRP image encoder not available");
bool is_grp = grp.Signature == FormatCatalog.ReadSignature (input); bool is_grp = grp.Signature == FormatCatalog.ReadSignature (input);
input.Position = 0; input.Position = 0;
var start = output.Position;
using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true)) using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true))
{ {
if (is_grp) if (is_grp)
@ -317,9 +318,8 @@ namespace GameRes.Formats
grp.Write (zstream, image); grp.Write (zstream, image);
entry.UnpackedSize = (uint)zstream.TotalIn; entry.UnpackedSize = (uint)zstream.TotalIn;
} }
zstream.Flush();
return (uint)zstream.TotalOut;
} }
return (uint)(output.Position - start);
} }
public override ResourceOptions GetDefaultOptions () public override ResourceOptions GetDefaultOptions ()

View File

@ -27,9 +27,9 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using GameRes.Compression;
using GameRes.Formats.Strings; using GameRes.Formats.Strings;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
namespace GameRes.Formats.AZSys namespace GameRes.Formats.AZSys
{ {

View File

@ -29,8 +29,8 @@ using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using GameRes.Compression;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
namespace GameRes.Formats.Selen namespace GameRes.Formats.Selen
{ {

View File

@ -51,6 +51,7 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" /> <Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Xaml" /> <Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
@ -58,10 +59,6 @@
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
<Reference Include="zlibnet, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\zlib\zlibnet.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ArcADPACK.cs" /> <Compile Include="ArcADPACK.cs" />
@ -125,6 +122,7 @@
<Compile Include="ArcRPA.cs" /> <Compile Include="ArcRPA.cs" />
<Compile Include="ArcS25.cs" /> <Compile Include="ArcS25.cs" />
<Compile Include="ArcSAF.cs" /> <Compile Include="ArcSAF.cs" />
<Compile Include="ZLibStream.cs" />
<None Include="ArcSeraph.cs" /> <None Include="ArcSeraph.cs" />
<Compile Include="ArcSPack.cs" /> <Compile Include="ArcSPack.cs" />
<Compile Include="ArcSteinsGate.cs" /> <Compile Include="ArcSteinsGate.cs" />
@ -290,14 +288,6 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="..\zlib\zlib32.dll">
<Link>zlib32.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\zlib\zlib64.dll">
<Link>zlib64.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<EmbeddedResource Include="Resources\ShiinaRio4.jpg" /> <EmbeddedResource Include="Resources\ShiinaRio4.jpg" />
<EmbeddedResource Include="Resources\ShiinaRio1.png" /> <EmbeddedResource Include="Resources\ShiinaRio1.png" />
<EmbeddedResource Include="Resources\ShiinaRio2.png" /> <EmbeddedResource Include="Resources\ShiinaRio2.png" />

View File

@ -31,7 +31,7 @@ using System.ComponentModel.Composition;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using ZLibNet; using GameRes.Compression;
using GameRes.Formats.Strings; using GameRes.Formats.Strings;
using GameRes.Formats.Properties; using GameRes.Formats.Properties;
@ -200,13 +200,13 @@ namespace GameRes.Formats.NitroPlus
{ {
if (entry.IsPacked) if (entry.IsPacked)
{ {
var start = destination.Position;
using (var zstream = new ZLibStream (destination, CompressionMode.Compress, using (var zstream = new ZLibStream (destination, CompressionMode.Compress,
CompressionLevel.Level9, true)) CompressionLevel.Level9, true))
{ {
file.CopyTo (zstream); file.CopyTo (zstream);
zstream.Flush();
entry.Size = (uint)zstream.TotalOut;
} }
entry.Size = (uint)(destination.Position - start);
} }
else else
{ {

View File

@ -32,7 +32,6 @@ using System.ComponentModel.Composition;
using GameRes.Compression; using GameRes.Compression;
using GameRes.Formats.Strings; using GameRes.Formats.Strings;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
namespace GameRes.Formats.NeXAS namespace GameRes.Formats.NeXAS
{ {

View File

@ -28,8 +28,8 @@ using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Text; using System.Text;
using GameRes.Compression;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
namespace GameRes.Formats.NitroPlus namespace GameRes.Formats.NitroPlus
{ {

View File

@ -31,9 +31,9 @@ using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Text; using System.Text;
using GameRes.Compression;
using GameRes.Formats.Properties; using GameRes.Formats.Properties;
using GameRes.Formats.Strings; using GameRes.Formats.Strings;
using ZLibNet;
namespace GameRes.Formats.RenPy namespace GameRes.Formats.RenPy
{ {

View File

@ -27,8 +27,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using GameRes.Compression;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
namespace GameRes.Formats.Lune namespace GameRes.Formats.Lune
{ {

View File

@ -28,7 +28,7 @@ using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using ZLibNet; using GameRes.Compression;
namespace GameRes.Formats.Seraphim namespace GameRes.Formats.Seraphim
{ {

View File

@ -31,10 +31,10 @@ using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows; using System.Windows;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using GameRes.Compression;
using GameRes.Formats.Properties; using GameRes.Formats.Properties;
using GameRes.Formats.Strings; using GameRes.Formats.Strings;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
namespace GameRes.Formats.ShiinaRio // 椎名里緒 namespace GameRes.Formats.ShiinaRio // 椎名里緒
{ {
@ -97,7 +97,7 @@ namespace GameRes.Formats.ShiinaRio // 椎名里緒
Stream index; Stream index;
if (version >= 170) if (version >= 170)
{ {
if (0x78 != enc_index[8]) // FIXME: check if it looks like ZLib stream if (0x78 != enc_index[8]) // check if it looks like ZLib stream
return null; return null;
var zindex = new MemoryStream (enc_index, 8, (int)index_length-8); var zindex = new MemoryStream (enc_index, 8, (int)index_length-8);
index = new ZLibStream (zindex, CompressionMode.Decompress); index = new ZLibStream (zindex, CompressionMode.Decompress);

View File

@ -27,6 +27,7 @@ using System;
using System.IO; using System.IO;
using System.IO.MemoryMappedFiles; using System.IO.MemoryMappedFiles;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
@ -34,8 +35,8 @@ using System.Runtime.InteropServices;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Diagnostics; using System.Diagnostics;
using GameRes.Compression;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
using GameRes.Formats.Strings; using GameRes.Formats.Strings;
using GameRes.Formats.Properties; using GameRes.Formats.Properties;
@ -174,7 +175,7 @@ namespace GameRes.Formats.KiriKiri
entry.IsEncrypted = 0 != header.ReadUInt32(); entry.IsEncrypted = 0 != header.ReadUInt32();
long file_size = header.ReadInt64(); long file_size = header.ReadInt64();
long packed_size = header.ReadInt64(); long packed_size = header.ReadInt64();
if (file_size > uint.MaxValue || packed_size > uint.MaxValue) if (file_size > uint.MaxValue || packed_size > uint.MaxValue || packed_size > file.MaxOffset)
{ {
goto NextEntry; goto NextEntry;
} }
@ -193,7 +194,7 @@ namespace GameRes.Formats.KiriKiri
entry.Cipher = NoCryptAlgorithm; entry.Cipher = NoCryptAlgorithm;
char[] name = header.ReadChars (name_size); char[] name = header.ReadChars (name_size);
entry.Name = new string (name); entry.Name = NormalizePath (new string (name));
entry.Type = FormatCatalog.Instance.GetTypeFromName (entry.Name); entry.Type = FormatCatalog.Instance.GetTypeFromName (entry.Name);
break; break;
} }
@ -246,6 +247,13 @@ NextEntry:
return new ArcFile (file, this, dir); return new ArcFile (file, this, dir);
} }
static readonly Regex PathRe = new Regex (@"[^\\/]+[\\/]\.\.[\\/]");
private static string NormalizePath (string filename)
{
return PathRe.Replace (filename, "");
}
private long SkipExeHeader (ArcView file) private long SkipExeHeader (ArcView file)
{ {
long offset = 0x10; long offset = 0x10;
@ -507,13 +515,13 @@ NextEntry:
}; };
if (compress) if (compress)
{ {
var start = output.Position;
using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true)) using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true))
{ {
xp3entry.Hash = CheckedCopy (file, zstream); xp3entry.Hash = CheckedCopy (file, zstream);
zstream.Flush();
segment.PackedSize = (uint)zstream.TotalOut;
xp3entry.Size = segment.PackedSize;
} }
segment.PackedSize = (uint)(output.Position - start);
xp3entry.Size = segment.PackedSize;
} }
else else
{ {
@ -542,11 +550,9 @@ NextEntry:
PackedSize = unpacked_size, PackedSize = unpacked_size,
}; };
xp3entry.Segments.Add (segment); xp3entry.Segments.Add (segment);
bool need_output_dispose = false;
if (compress) if (compress)
{ {
output = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true); output = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true);
need_output_dispose = true;
} }
unsafe unsafe
{ {
@ -573,18 +579,17 @@ NextEntry:
} }
if (hash_after_crypt) if (hash_after_crypt)
xp3entry.Hash = checksum.Value; xp3entry.Hash = checksum.Value;
if (compress)
{
output.Flush();
segment.PackedSize = (uint)(output as ZLibStream).TotalOut;
xp3entry.Size = segment.PackedSize;
}
} }
finally finally
{ {
view.SafeMemoryMappedViewHandle.ReleasePointer(); view.SafeMemoryMappedViewHandle.ReleasePointer();
if (need_output_dispose) if (compress)
{
var dest = (output as ZLibStream).BaseStream;
output.Dispose(); output.Dispose();
segment.PackedSize = (uint)(dest.Position - segment.Offset);
xp3entry.Size = segment.PackedSize;
}
} }
} }
} }

View File

@ -30,7 +30,7 @@ using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using ZLibNet; using GameRes.Compression;
using GameRes.Formats.Strings; using GameRes.Formats.Strings;
using GameRes.Formats.Properties; using GameRes.Formats.Properties;
using GameRes.Utility; using GameRes.Utility;
@ -184,13 +184,13 @@ namespace GameRes.Formats.YuRis
{ {
if (entry.IsPacked) if (entry.IsPacked)
{ {
var start = output.Position;
using (var zstream = new ZLibStream (checked_stream, CompressionMode.Compress, using (var zstream = new ZLibStream (checked_stream, CompressionMode.Compress,
CompressionLevel.Level9, true)) CompressionLevel.Level9, true))
{ {
input.CopyTo (zstream); input.CopyTo (zstream);
zstream.Flush();
entry.Size = (uint)zstream.TotalOut;
} }
entry.Size = (uint)(output.Position - start);
} }
else else
{ {

View File

@ -26,8 +26,8 @@
using System; using System;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using GameRes.Compression;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
namespace GameRes.Formats.Circus namespace GameRes.Formats.Circus
{ {

View File

@ -25,7 +25,7 @@
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using ZLibNet; using GameRes.Compression;
namespace GameRes.Formats.ScenePlayer namespace GameRes.Formats.ScenePlayer
{ {

View File

@ -27,7 +27,7 @@ using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Text; using System.Text;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet; using GameRes.Compression;
namespace GameRes.Formats.BlackRainbow namespace GameRes.Formats.BlackRainbow
{ {

View File

@ -29,7 +29,7 @@ using System.IO;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using ZLibNet; using GameRes.Compression;
namespace GameRes.Formats.AZSys namespace GameRes.Formats.AZSys
{ {

View File

@ -28,7 +28,7 @@ using System.IO;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Media; using System.Windows.Media;
using ZLibNet; using GameRes.Compression;
using GameRes.Utility; using GameRes.Utility;
namespace GameRes.Formats namespace GameRes.Formats

View File

@ -26,8 +26,8 @@
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Text; using System.Text;
using GameRes.Compression;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
namespace GameRes.Formats.ScenePlayer namespace GameRes.Formats.ScenePlayer
{ {
@ -50,10 +50,6 @@ namespace GameRes.Formats.ScenePlayer
int first = stream.ReadByte() ^ 0x21; int first = stream.ReadByte() ^ 0x21;
if (first != 0x78) // doesn't look like zlib stream if (first != 0x78) // doesn't look like zlib stream
return null; return null;
int flg = stream.ReadByte() ^ 0x21;
int fcheck = first << 8 | flg;
if (fcheck % 0x1f != 0)
return null;
stream.Position = 0; stream.Position = 0;
using (var input = new XoredStream (stream, 0x21, true)) using (var input = new XoredStream (stream, 0x21, true))

View File

@ -29,8 +29,8 @@ using System.IO;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using GameRes.Compression;
using GameRes.Utility; using GameRes.Utility;
using ZLibNet;
namespace GameRes.Formats.AliceSoft namespace GameRes.Formats.AliceSoft
{ {

View File

@ -5,7 +5,7 @@
using System; using System;
using System.IO; using System.IO;
using ZLibNet; using System.IO.Compression;
class Inflate class Inflate
{ {
@ -16,8 +16,8 @@ class Inflate
try try
{ {
var input = File.Open (args[0], FileMode.Open, FileAccess.Read); var input = File.Open (args[0], FileMode.Open, FileAccess.Read);
// input.Position = 2; input.Position = 2;
using (var stream = new ZLibStream (input, CompressionMode.Decompress)) using (var stream = new DeflateStream (input, CompressionMode.Decompress))
using (var output = File.Create (args[1])) using (var output = File.Create (args[1]))
stream.CopyTo (output); stream.CopyTo (output);
Console.WriteLine ("{0} => {1}", args[0], args[1]); Console.WriteLine ("{0} => {1}", args[0], args[1]);