diff --git a/ArcFormats/ArcAMI.cs b/ArcFormats/ArcAMI.cs index 5202940e..43e56b8f 100644 --- a/ArcFormats/ArcAMI.cs +++ b/ArcFormats/ArcAMI.cs @@ -33,7 +33,7 @@ using System.Globalization; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; -using ZLibNet; +using GameRes.Compression; using GameRes.Formats.Strings; using GameRes.Formats.Properties; @@ -303,6 +303,7 @@ namespace GameRes.Formats throw new FileFormatException ("GRP image encoder not available"); bool is_grp = grp.Signature == FormatCatalog.ReadSignature (input); input.Position = 0; + var start = output.Position; using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true)) { if (is_grp) @@ -317,9 +318,8 @@ namespace GameRes.Formats grp.Write (zstream, image); entry.UnpackedSize = (uint)zstream.TotalIn; } - zstream.Flush(); - return (uint)zstream.TotalOut; } + return (uint)(output.Position - start); } public override ResourceOptions GetDefaultOptions () diff --git a/ArcFormats/ArcAZSys.cs b/ArcFormats/ArcAZSys.cs index 753fa651..a8076eba 100644 --- a/ArcFormats/ArcAZSys.cs +++ b/ArcFormats/ArcAZSys.cs @@ -27,9 +27,9 @@ using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; +using GameRes.Compression; using GameRes.Formats.Strings; using GameRes.Utility; -using ZLibNet; namespace GameRes.Formats.AZSys { diff --git a/ArcFormats/ArcCCT.cs b/ArcFormats/ArcCCT.cs index b53fa2bd..7bb226f7 100644 --- a/ArcFormats/ArcCCT.cs +++ b/ArcFormats/ArcCCT.cs @@ -29,8 +29,8 @@ using System.ComponentModel.Composition; using System.IO; using System.Linq; using System.Text; +using GameRes.Compression; using GameRes.Utility; -using ZLibNet; namespace GameRes.Formats.Selen { diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index a1280158..9f491937 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -51,6 +51,7 @@ + @@ -58,10 +59,6 @@ - - False - ..\zlib\zlibnet.dll - @@ -125,6 +122,7 @@ + @@ -290,14 +288,6 @@ - - zlib32.dll - PreserveNewest - - - zlib64.dll - PreserveNewest - diff --git a/ArcFormats/ArcNPA.cs b/ArcFormats/ArcNPA.cs index 92b5bc11..39a3c4d1 100644 --- a/ArcFormats/ArcNPA.cs +++ b/ArcFormats/ArcNPA.cs @@ -31,7 +31,7 @@ using System.ComponentModel.Composition; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; -using ZLibNet; +using GameRes.Compression; using GameRes.Formats.Strings; using GameRes.Formats.Properties; @@ -200,13 +200,13 @@ namespace GameRes.Formats.NitroPlus { if (entry.IsPacked) { + var start = destination.Position; using (var zstream = new ZLibStream (destination, CompressionMode.Compress, CompressionLevel.Level9, true)) { file.CopyTo (zstream); - zstream.Flush(); - entry.Size = (uint)zstream.TotalOut; } + entry.Size = (uint)(destination.Position - start); } else { diff --git a/ArcFormats/ArcNexas.cs b/ArcFormats/ArcNexas.cs index 1e877d10..be9588ed 100644 --- a/ArcFormats/ArcNexas.cs +++ b/ArcFormats/ArcNexas.cs @@ -32,7 +32,6 @@ using System.ComponentModel.Composition; using GameRes.Compression; using GameRes.Formats.Strings; using GameRes.Utility; -using ZLibNet; namespace GameRes.Formats.NeXAS { diff --git a/ArcFormats/ArcNitro.cs b/ArcFormats/ArcNitro.cs index ccf17a43..eb883cdd 100644 --- a/ArcFormats/ArcNitro.cs +++ b/ArcFormats/ArcNitro.cs @@ -28,8 +28,8 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; using System.Text; +using GameRes.Compression; using GameRes.Utility; -using ZLibNet; namespace GameRes.Formats.NitroPlus { diff --git a/ArcFormats/ArcRPA.cs b/ArcFormats/ArcRPA.cs index 8ebfa4fe..641a55b5 100644 --- a/ArcFormats/ArcRPA.cs +++ b/ArcFormats/ArcRPA.cs @@ -31,9 +31,9 @@ using System.Diagnostics; using System.Globalization; using System.IO; using System.Text; +using GameRes.Compression; using GameRes.Formats.Properties; using GameRes.Formats.Strings; -using ZLibNet; namespace GameRes.Formats.RenPy { diff --git a/ArcFormats/ArcSAF.cs b/ArcFormats/ArcSAF.cs index b928198d..750d4be8 100644 --- a/ArcFormats/ArcSAF.cs +++ b/ArcFormats/ArcSAF.cs @@ -27,8 +27,8 @@ using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; +using GameRes.Compression; using GameRes.Utility; -using ZLibNet; namespace GameRes.Formats.Lune { diff --git a/ArcFormats/ArcSeraph.cs b/ArcFormats/ArcSeraph.cs index ce8592b0..50865115 100644 --- a/ArcFormats/ArcSeraph.cs +++ b/ArcFormats/ArcSeraph.cs @@ -28,7 +28,7 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; using System.Text.RegularExpressions; -using ZLibNet; +using GameRes.Compression; namespace GameRes.Formats.Seraphim { diff --git a/ArcFormats/ArcWARC.cs b/ArcFormats/ArcWARC.cs index d29f903b..5b72dea3 100644 --- a/ArcFormats/ArcWARC.cs +++ b/ArcFormats/ArcWARC.cs @@ -31,10 +31,10 @@ using System.Linq; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Media.Imaging; +using GameRes.Compression; using GameRes.Formats.Properties; using GameRes.Formats.Strings; using GameRes.Utility; -using ZLibNet; namespace GameRes.Formats.ShiinaRio // 椎名里緒 { @@ -97,7 +97,7 @@ namespace GameRes.Formats.ShiinaRio // 椎名里緒 Stream index; 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; var zindex = new MemoryStream (enc_index, 8, (int)index_length-8); index = new ZLibStream (zindex, CompressionMode.Decompress); diff --git a/ArcFormats/ArcXP3.cs b/ArcFormats/ArcXP3.cs index 3b3ee818..078ab60d 100644 --- a/ArcFormats/ArcXP3.cs +++ b/ArcFormats/ArcXP3.cs @@ -27,6 +27,7 @@ using System; using System.IO; using System.IO.MemoryMappedFiles; using System.Text; +using System.Text.RegularExpressions; using System.Linq; using System.Collections.Generic; using System.ComponentModel.Composition; @@ -34,8 +35,8 @@ using System.Runtime.InteropServices; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Diagnostics; +using GameRes.Compression; using GameRes.Utility; -using ZLibNet; using GameRes.Formats.Strings; using GameRes.Formats.Properties; @@ -174,7 +175,7 @@ namespace GameRes.Formats.KiriKiri entry.IsEncrypted = 0 != header.ReadUInt32(); long file_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; } @@ -193,7 +194,7 @@ namespace GameRes.Formats.KiriKiri entry.Cipher = NoCryptAlgorithm; char[] name = header.ReadChars (name_size); - entry.Name = new string (name); + entry.Name = NormalizePath (new string (name)); entry.Type = FormatCatalog.Instance.GetTypeFromName (entry.Name); break; } @@ -246,6 +247,13 @@ NextEntry: 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) { long offset = 0x10; @@ -507,13 +515,13 @@ NextEntry: }; if (compress) { + var start = output.Position; using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true)) { 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 { @@ -542,11 +550,9 @@ NextEntry: PackedSize = unpacked_size, }; xp3entry.Segments.Add (segment); - bool need_output_dispose = false; if (compress) { output = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true); - need_output_dispose = true; } unsafe { @@ -573,18 +579,17 @@ NextEntry: } if (hash_after_crypt) xp3entry.Hash = checksum.Value; - if (compress) - { - output.Flush(); - segment.PackedSize = (uint)(output as ZLibStream).TotalOut; - xp3entry.Size = segment.PackedSize; - } } finally { view.SafeMemoryMappedViewHandle.ReleasePointer(); - if (need_output_dispose) + if (compress) + { + var dest = (output as ZLibStream).BaseStream; output.Dispose(); + segment.PackedSize = (uint)(dest.Position - segment.Offset); + xp3entry.Size = segment.PackedSize; + } } } } diff --git a/ArcFormats/ArcYPF.cs b/ArcFormats/ArcYPF.cs index efcfd011..cff26c65 100644 --- a/ArcFormats/ArcYPF.cs +++ b/ArcFormats/ArcYPF.cs @@ -30,7 +30,7 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.Windows.Media; using System.Windows.Media.Imaging; -using ZLibNet; +using GameRes.Compression; using GameRes.Formats.Strings; using GameRes.Formats.Properties; using GameRes.Utility; @@ -184,13 +184,13 @@ namespace GameRes.Formats.YuRis { if (entry.IsPacked) { + var start = output.Position; using (var zstream = new ZLibStream (checked_stream, CompressionMode.Compress, CompressionLevel.Level9, true)) { input.CopyTo (zstream); - zstream.Flush(); - entry.Size = (uint)zstream.TotalOut; } + entry.Size = (uint)(output.Position - start); } else { diff --git a/ArcFormats/AudioPCM.cs b/ArcFormats/AudioPCM.cs index 1a2743b8..154066b4 100644 --- a/ArcFormats/AudioPCM.cs +++ b/ArcFormats/AudioPCM.cs @@ -26,8 +26,8 @@ using System; using System.ComponentModel.Composition; using System.IO; +using GameRes.Compression; using GameRes.Utility; -using ZLibNet; namespace GameRes.Formats.Circus { diff --git a/ArcFormats/AudioPMW.cs b/ArcFormats/AudioPMW.cs index f0dda172..16612265 100644 --- a/ArcFormats/AudioPMW.cs +++ b/ArcFormats/AudioPMW.cs @@ -25,7 +25,7 @@ using System.ComponentModel.Composition; using System.IO; -using ZLibNet; +using GameRes.Compression; namespace GameRes.Formats.ScenePlayer { diff --git a/ArcFormats/ImageBMZ.cs b/ArcFormats/ImageBMZ.cs index f04d4f78..d5b69715 100644 --- a/ArcFormats/ImageBMZ.cs +++ b/ArcFormats/ImageBMZ.cs @@ -27,7 +27,7 @@ using System.ComponentModel.Composition; using System.IO; using System.Text; using GameRes.Utility; -using ZLibNet; +using GameRes.Compression; namespace GameRes.Formats.BlackRainbow { diff --git a/ArcFormats/ImageCPB.cs b/ArcFormats/ImageCPB.cs index 7f970484..35c7d327 100644 --- a/ArcFormats/ImageCPB.cs +++ b/ArcFormats/ImageCPB.cs @@ -29,7 +29,7 @@ using System.IO; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; -using ZLibNet; +using GameRes.Compression; namespace GameRes.Formats.AZSys { diff --git a/ArcFormats/ImageHG3.cs b/ArcFormats/ImageHG3.cs index c01d2255..6b201540 100644 --- a/ArcFormats/ImageHG3.cs +++ b/ArcFormats/ImageHG3.cs @@ -28,7 +28,7 @@ using System.IO; using System.ComponentModel.Composition; using System.Windows.Media.Imaging; using System.Windows.Media; -using ZLibNet; +using GameRes.Compression; using GameRes.Utility; namespace GameRes.Formats diff --git a/ArcFormats/ImagePMP.cs b/ArcFormats/ImagePMP.cs index daea6d9e..870d3f42 100644 --- a/ArcFormats/ImagePMP.cs +++ b/ArcFormats/ImagePMP.cs @@ -26,8 +26,8 @@ using System.ComponentModel.Composition; using System.IO; using System.Text; +using GameRes.Compression; using GameRes.Utility; -using ZLibNet; namespace GameRes.Formats.ScenePlayer { @@ -50,10 +50,6 @@ namespace GameRes.Formats.ScenePlayer int first = stream.ReadByte() ^ 0x21; if (first != 0x78) // doesn't look like zlib stream return null; - int flg = stream.ReadByte() ^ 0x21; - int fcheck = first << 8 | flg; - if (fcheck % 0x1f != 0) - return null; stream.Position = 0; using (var input = new XoredStream (stream, 0x21, true)) diff --git a/ArcFormats/ImageQNT.cs b/ArcFormats/ImageQNT.cs index e8ff7c17..5da97bb9 100644 --- a/ArcFormats/ImageQNT.cs +++ b/ArcFormats/ImageQNT.cs @@ -29,8 +29,8 @@ using System.IO; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; +using GameRes.Compression; using GameRes.Utility; -using ZLibNet; namespace GameRes.Formats.AliceSoft { diff --git a/inflate.cs b/inflate.cs index 3c717c8a..3e847158 100644 --- a/inflate.cs +++ b/inflate.cs @@ -5,7 +5,7 @@ using System; using System.IO; -using ZLibNet; +using System.IO.Compression; class Inflate { @@ -16,10 +16,10 @@ class Inflate try { var input = File.Open (args[0], FileMode.Open, FileAccess.Read); -// input.Position = 2; - using (var stream = new ZLibStream (input, CompressionMode.Decompress)) - using (var output = File.Create (args[1])) - stream.CopyTo (output); + input.Position = 2; + using (var stream = new DeflateStream (input, CompressionMode.Decompress)) + using (var output = File.Create (args[1])) + stream.CopyTo (output); Console.WriteLine ("{0} => {1}", args[0], args[1]); } catch (Exception X)