├── .gitattributes
├── Orion2-Repacker
├── icon.ico
├── Resources
│ ├── Pfim.dll
│ └── ScintillaNET.dll
├── App.config
├── Properties
│ ├── Settings.settings
│ ├── Settings.Designer.cs
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ └── Resources.resx
├── Program.cs
├── Crypto
│ ├── Stream
│ │ ├── dds
│ │ │ ├── DDS.cs
│ │ │ └── DDSImage.cs
│ │ ├── PackFileHeaderVerBase.cs
│ │ ├── PackStreamVerBase.cs
│ │ ├── PackFileHeaderVer2.cs
│ │ ├── PackStreamVer2.cs
│ │ ├── PackFileHeaderVer3.cs
│ │ ├── PackStreamVer1.cs
│ │ ├── PackStreamVer3.cs
│ │ ├── PackFileHeaderVer1.cs
│ │ └── zlib
│ │ │ ├── ZlibConstants.cs
│ │ │ ├── Tree.cs
│ │ │ ├── Zlib.cs
│ │ │ ├── InfTree.cs
│ │ │ └── ZlibBaseStream.cs
│ ├── Common
│ │ ├── PackVer.cs
│ │ └── PackFileEntry.cs
│ ├── AESCipher.cs
│ └── CryptoMan.cs
├── Window
│ ├── Common
│ │ ├── PackNodeList.cs
│ │ └── PackNode.cs
│ ├── ProgressWindow.cs
│ ├── ProgressWindow.Designer.cs
│ ├── About.cs
│ └── About.Designer.cs
└── Orion2-Repacker.csproj
├── Orion2-Repacker.sln
├── README.md
└── .gitignore
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/Orion2-Repacker/icon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EricSoftTM/Orion2-Repacker/HEAD/Orion2-Repacker/icon.ico
--------------------------------------------------------------------------------
/Orion2-Repacker/Resources/Pfim.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EricSoftTM/Orion2-Repacker/HEAD/Orion2-Repacker/Resources/Pfim.dll
--------------------------------------------------------------------------------
/Orion2-Repacker/Resources/ScintillaNET.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EricSoftTM/Orion2-Repacker/HEAD/Orion2-Repacker/Resources/ScintillaNET.dll
--------------------------------------------------------------------------------
/Orion2-Repacker/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace Orion.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.7.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Orion2-Repacker.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2035
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orion2-Repacker", "Orion2-Repacker\Orion2-Repacker.csproj", "{54C07F0A-A88D-45AA-BFBF-9FA2BA2AD9B5}"
7 | EndProject
8 | Global
9 | GlobalSection(Performance) = preSolution
10 | HasPerformanceSessions = true
11 | EndGlobalSection
12 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
13 | Debug|Any CPU = Debug|Any CPU
14 | Release|Any CPU = Release|Any CPU
15 | EndGlobalSection
16 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
17 | {54C07F0A-A88D-45AA-BFBF-9FA2BA2AD9B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18 | {54C07F0A-A88D-45AA-BFBF-9FA2BA2AD9B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
19 | {54C07F0A-A88D-45AA-BFBF-9FA2BA2AD9B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
20 | {54C07F0A-A88D-45AA-BFBF-9FA2BA2AD9B5}.Release|Any CPU.Build.0 = Release|Any CPU
21 | EndGlobalSection
22 | GlobalSection(SolutionProperties) = preSolution
23 | HideSolutionNode = FALSE
24 | EndGlobalSection
25 | GlobalSection(ExtensibilityGlobals) = postSolution
26 | SolutionGuid = {A318C7FC-8F04-474F-B20A-CE1766AC67D6}
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Window;
19 | using System;
20 | using System.Windows.Forms;
21 |
22 | namespace Orion
23 | {
24 | static class Program
25 | {
26 | ///
27 | /// The main entry point for the application.
28 | ///
29 | [STAThread]
30 | static void Main()
31 | {
32 | Application.EnableVisualStyles();
33 | Application.SetCompatibleTextRenderingDefault(false);
34 | Application.Run(new MainWindow());
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | // General Information about an assembly is controlled through the following
5 | // set of attributes. Change these attribute values to modify the information
6 | // associated with an assembly.
7 | [assembly: AssemblyTitle("Orion2 Repacker")]
8 | [assembly: AssemblyDescription("")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("Orion2 Repacker")]
12 | [assembly: AssemblyCopyright("Copyright © 2018")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("54c07f0a-a88d-45aa-bfbf-9fa2ba2ad9b5")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("2.0.0.0")]
35 | [assembly: AssemblyFileVersion("2.0.0.0")]
36 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Orion2 Repacker
2 | Orion2 Repacker - A HaRepacker for MapleStory2!
3 |
4 | [](https://github.com/EricSoftTM/Orion2-Repacker)
5 | ----------------------------------------------------------------------
6 | ## General Features
7 | * Supports all MS2 file formats (MS2F, NS2F, OS2F, and PS2F)
8 | * Loads the file list into a expandable tree view for easy access
9 | * Selecting a node will render it, as well as display actions for modifying it
10 | * Double-clicking a node will expand the directory and add the new entries to tree
11 | * Ability to add, remove, copy, paste, and modify any of the data
12 | * Ability to export the selected node's file data to disk
13 | * Full saving support for all file formats
14 | ## Rendering
15 | Currently the Orion2 Repacker is able to display the following formats:
16 | * Initialization Files (.ini)
17 | * N-Triple Files (.nt)
18 | * LUA Files (.lua)
19 | * XML Files (.xml)
20 | * FLAT Files (.flat)
21 | * XBlock Files (.xblock)
22 | * Database Diagram Files (.diagram)
23 | * Preset Files (.preset)
24 | * PNG Image Files (.png)
25 | * DDS Image Files (.dds)
26 |
27 | The following are currently unable to be rendered, but these external applications can:
28 | * USM Movie Files (.usm) - [VGMToolbox](https://sourceforge.net/projects/vgmtoolbox/)
29 | * Gamebryo NIF/KF Files (.nif, .kf, .kfm) - [Noesis](https://richwhitehouse.com/index.php?content=inc_projects.php&showproject=91)
30 | ## Dependencies
31 | The Orion2 Repacker utilizes these great libraries:
32 | * [ScintillaNET](https://github.com/jacobslusser/ScintillaNET) WinForm wrapper of the Scintilla source code editor
33 | * [Pfim](https://github.com/nickbabcock/Pfim) DDS decoder
34 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/dds/DDS.cs:
--------------------------------------------------------------------------------
1 | using System.Drawing;
2 | using System.IO;
3 |
4 | namespace Orion.Crypto.Stream.DDS
5 | {
6 | ///
7 | /// This is the main class of the library. All static methods are contained within.
8 | ///
9 | public class DDS
10 | {
11 | ///
12 | /// Loads a DDS image from a byte array, and returns a Bitmap object of the image.
13 | ///
14 | /// The image data, as a byte array.
15 | /// The Bitmap representation of the image.
16 | public static Bitmap LoadImage(byte[] data)
17 | {
18 | DDSImage im = new DDSImage(data);
19 | return im.BitmapImage;
20 | }
21 |
22 | ///
23 | /// Loads a DDS image from a file, and returns a Bitmap object of the image.
24 | ///
25 | /// The image file.
26 | /// The Bitmap representation of the image.
27 | public static Bitmap LoadImage(string file)
28 | {
29 | byte[] data = File.ReadAllBytes(file);
30 | DDSImage im = new DDSImage(data);
31 | return im.BitmapImage;
32 | }
33 |
34 | ///
35 | /// Loads a DDS image from a Stream, and returns a Bitmap object of the image.
36 | ///
37 | /// The stream to read the image data from.
38 | /// The Bitmap representation of the image.
39 | public static Bitmap LoadImage(System.IO.Stream stream)
40 | {
41 | DDSImage im = new DDSImage(stream);
42 | return im.BitmapImage;
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/dds/DDSImage.cs:
--------------------------------------------------------------------------------
1 | using Pfim;
2 | using System;
3 | using System.Drawing;
4 | using System.Drawing.Imaging;
5 |
6 | namespace Orion.Crypto.Stream.DDS
7 | {
8 | public class DDSImage : IDisposable
9 | {
10 | private readonly IImage _image;
11 | private Bitmap _bitmap;
12 |
13 | public Bitmap BitmapImage
14 | {
15 | get { return _bitmap; }
16 | }
17 |
18 | public DDSImage(byte[] ddsImage)
19 | {
20 | if (ddsImage == null)
21 | return;
22 |
23 | if (ddsImage.Length == 0)
24 | return;
25 |
26 | _image = Dds.Create(ddsImage, new PfimConfig());
27 | Parse();
28 | }
29 |
30 | public DDSImage(System.IO.Stream ddsImage)
31 | {
32 | if (ddsImage == null)
33 | return;
34 |
35 | if (!ddsImage.CanRead)
36 | return;
37 |
38 | _image = Dds.Create(ddsImage, new PfimConfig());
39 | Parse();
40 | }
41 |
42 | public void Dispose()
43 | {
44 | if (_bitmap != null)
45 | {
46 | _bitmap.Dispose();
47 | _bitmap = null;
48 | }
49 | }
50 |
51 | private void Parse()
52 | {
53 | if (_image == null)
54 | throw new Exception("Image data failed to create within Pfim");
55 |
56 | if (_image.Compressed)
57 | _image.Decompress();
58 |
59 | _bitmap = CreateBitmap(_image);
60 | }
61 |
62 | private Bitmap CreateBitmap(IImage image)
63 | {
64 | var pxFormat = PixelFormat.Format24bppRgb;
65 | if (image.Format == Pfim.ImageFormat.Rgba32)
66 | pxFormat = PixelFormat.Format32bppArgb;
67 |
68 | unsafe
69 | {
70 | fixed (byte* bytePtr = image.Data)
71 | {
72 | return new Bitmap(image.Width, image.Height, image.Stride, pxFormat, (IntPtr) bytePtr);
73 | }
74 | }
75 | }
76 | }
77 | }
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Common/PackVer.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Stream;
19 | using System;
20 | using System.IO;
21 |
22 | namespace Orion.Crypto.Common
23 | {
24 | public class PackVer
25 | {
26 | public const uint
27 | MS2F = 0x4632534D, //Ver1
28 | NS2F = 0x4632534E, //Ver2
29 | OS2F = 0x4632534F, //Ver3
30 | PS2F = 0x46325350 //Ver3
31 | ;
32 |
33 | /*
34 | * Creates a new packed stream based on the type of version.
35 | *
36 | * @param pHeader The stream to read the pack version from
37 | *
38 | * @return A packed stream with header sizes decoded
39 | *
40 | */
41 | public static PackStreamVerBase CreatePackVer(BinaryReader pHeader)
42 | {
43 | uint uVer = pHeader.ReadUInt32();
44 | switch (uVer)
45 | {
46 | case PackVer.MS2F:
47 | return PackStreamVer1.ParseHeader(pHeader);
48 | case PackVer.NS2F:
49 | return PackStreamVer2.ParseHeader(pHeader);
50 | case PackVer.OS2F:
51 | case PackVer.PS2F:
52 | return PackStreamVer3.ParseHeader(pHeader, uVer);
53 | }
54 |
55 | throw new Exception(string.Format("Unknown file version read from stream ({0})", uVer));
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/PackFileHeaderVerBase.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using System.IO;
19 |
20 | namespace Orion.Crypto.Stream
21 | {
22 | public interface PackFileHeaderVerBase
23 | {
24 | uint GetVer(); // Represents the format of the packed stream (MS2F/NS2F/etc)
25 |
26 | int GetFileIndex(); // The index of this file located within the lookup table
27 |
28 | uint GetBufferFlag(); // The flag that determines buffer manipulation
29 |
30 | ulong GetOffset(); // The start offset of this file's data within the m2d file
31 |
32 | uint GetEncodedFileSize(); // The total (base64) encoded size of the file
33 | ulong GetCompressedFileSize(); // The total compressed size of the (raw) file
34 | ulong GetFileSize(); // The total size of the raw (decoded, decompressed) file
35 |
36 | void Encode(BinaryWriter pWriter); // Encodes the contents of this file to stream
37 |
38 | void SetFileIndex(int nIndex); // Updates this file's index within the lookup table
39 |
40 | void SetOffset(ulong uOffset); // Updates this file's initial offset within the m2d file
41 |
42 | void SetEncodedFileSize(uint uEncoded); // Updates this file's encoded base64 length
43 | void SetCompressedFileSize(ulong uCompressed); // Updates this file's compressed file size
44 | void SetFileSize(ulong uSize); // Updates this file's raw (uncompressed) file size
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Window/Common/PackNodeList.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Common;
19 | using System;
20 | using System.Collections.Generic;
21 |
22 | namespace Orion.Window.Common
23 | {
24 | [Serializable]
25 | public class PackNodeList
26 | {
27 | public const string DATA_FORMAT = "Pack.Node.FileList";
28 |
29 | private readonly Dictionary mChildren; //
30 | private readonly Dictionary mEntries; //
31 |
32 | public Dictionary Children { get { return mChildren; } }
33 | public Dictionary Entries { get { return mEntries; } }
34 | public string Directory { get; private set; }
35 |
36 | public PackNodeList(string sDir)
37 | {
38 | this.Directory = sDir;
39 | this.mChildren = new Dictionary();
40 | this.mEntries = new Dictionary();
41 | }
42 |
43 | /*
44 | * Recursively clear all children/entries within this node list.
45 | *
46 | */
47 | public void InternalRelease()
48 | {
49 | mEntries.Clear();
50 |
51 | foreach (PackNodeList pChild in mChildren.Values)
52 | {
53 | pChild.InternalRelease();
54 | }
55 | mChildren.Clear();
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Window/ProgressWindow.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Stream;
19 | using System.Diagnostics;
20 | using System.Windows.Forms;
21 |
22 | namespace Orion.Window
23 | {
24 | public partial class ProgressWindow : Form
25 | {
26 | private string sPath;
27 | private Stopwatch pStopWatch;
28 |
29 | public string FileName { get; set; }
30 | public PackStreamVerBase Stream { get; set; }
31 | public long ElapsedTime { get; set; }
32 |
33 | public ProgressWindow()
34 | {
35 | InitializeComponent();
36 | }
37 |
38 | public void UpdateProgressBar(int nProgress)
39 | {
40 | this.pProgressBar.Value = nProgress;
41 | this.pSaveInfo.Text = string.Format("Saving {0} ... {1}%", this.FileName, this.pProgressBar.Value);
42 | }
43 |
44 | public void Start()
45 | {
46 | this.pStopWatch = Stopwatch.StartNew();
47 | }
48 |
49 | public void Finish()
50 | {
51 | this.ElapsedTime = this.pStopWatch.ElapsedMilliseconds;
52 | this.pStopWatch.Stop();
53 | }
54 |
55 | public string Path
56 | {
57 | get
58 | {
59 | return sPath;
60 | }
61 | set
62 | {
63 | this.sPath = value;
64 |
65 | this.FileName = this.sPath.Substring(this.sPath.LastIndexOf('/') + 1).Split('.')[0];
66 | }
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/PackStreamVerBase.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Common;
19 | using System.Collections.Generic;
20 | using System.IO;
21 |
22 | namespace Orion.Crypto.Stream
23 | {
24 | public interface PackStreamVerBase
25 | {
26 |
27 | uint GetVer(); // Represents the format of the packed stream (MS2F/NS2F/etc)
28 |
29 | ulong GetCompressedHeaderSize(); // The total compressed size of the (raw) file list
30 | ulong GetEncodedHeaderSize(); // The total (base64) encoded size of the file list
31 | ulong GetHeaderSize(); // The total size of the raw (decoded, decompressed) file list
32 |
33 | ulong GetCompressedDataSize(); // The total compressed size of the (raw) file table
34 | ulong GetEncodedDataSize(); // The total (base64) encoded size of the file table
35 | ulong GetDataSize(); // The total size of the raw (decoded, decompressed) file table
36 |
37 | ulong GetFileListCount(); // The total count of files within the data file
38 |
39 | List GetFileList(); // Represents a list of fileinfo containers (,,)
40 |
41 | void Encode(BinaryWriter pWriter); // Encodes the header/data pack sizes to stream
42 |
43 | void SetCompressedHeaderSize(ulong uCompressed); // Updates the compressed file string size
44 | void SetEncodedHeaderSize(ulong uEncoded); // Updates the base64 encoded file string length
45 | void SetHeaderSize(ulong uSize); // Updates the raw (uncompressed) file string size
46 |
47 | void SetCompressedDataSize(ulong uCompressed); // Updates the compressed file table size
48 | void SetEncodedDataSize(ulong uEncoded); // Updates the base64 encoded file table length
49 | void SetDataSize(ulong uSize); // Updates the raw (uncompressed) file table size
50 |
51 | void SetFileListCount(ulong uCount); // Updates the total count of files within this stream
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Window/Common/PackNode.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Common;
19 | using System.Text;
20 | using System.Windows.Forms;
21 |
22 | namespace Orion.Window.Common
23 | {
24 | public class PackNode : TreeNode
25 | {
26 | private byte[] pData;
27 |
28 | public PackNode(object pItem, string sName)
29 | : base()
30 | {
31 | this.Name = sName;
32 | this.Text = sName;
33 | this.Tag = pItem;
34 | }
35 |
36 | /* Generate the full current path of this node within the tree */
37 | public string Path
38 | {
39 | get
40 | {
41 | string[] aPath = new string[this.Level];
42 |
43 | TreeNode pNode = this;
44 | for (int i = 0; i < aPath.Length; i++)
45 | {
46 | aPath[i] = pNode.Name;
47 |
48 | pNode = pNode.Parent;
49 | if (pNode == null)
50 | {
51 | break;
52 | }
53 | }
54 |
55 | StringBuilder sPath = new StringBuilder();
56 | for (int i = aPath.Length - 1; i >= 0; i--)
57 | {
58 | sPath.Append(aPath[i]);
59 | }
60 |
61 | return sPath.ToString();
62 | }
63 | }
64 |
65 | /* Return the decrypted data block from the entry */
66 | public byte[] Data
67 | {
68 | get
69 | {
70 | if (this.Tag is PackFileEntry)
71 | {
72 | return (this.Tag as PackFileEntry).Data;
73 | } else
74 | {
75 | return pData;
76 | }
77 | }
78 | set
79 | {
80 | if (this.Tag is PackFileEntry)
81 | {
82 | (this.Tag as PackFileEntry).Data = value;
83 | } else
84 | {
85 | this.pData = value;
86 | }
87 | }
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace Orion.Properties {
12 | using System;
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources {
26 |
27 | private static global::System.Resources.ResourceManager resourceMan;
28 |
29 | private static global::System.Globalization.CultureInfo resourceCulture;
30 |
31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
32 | internal Resources() {
33 | }
34 |
35 | ///
36 | /// Returns the cached ResourceManager instance used by this class.
37 | ///
38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
39 | internal static global::System.Resources.ResourceManager ResourceManager {
40 | get {
41 | if (object.ReferenceEquals(resourceMan, null)) {
42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Orion.Properties.Resources", typeof(Resources).Assembly);
43 | resourceMan = temp;
44 | }
45 | return resourceMan;
46 | }
47 | }
48 |
49 | ///
50 | /// Overrides the current thread's CurrentUICulture property for all
51 | /// resource lookups using this strongly typed resource class.
52 | ///
53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
54 | internal static global::System.Globalization.CultureInfo Culture {
55 | get {
56 | return resourceCulture;
57 | }
58 | set {
59 | resourceCulture = value;
60 | }
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/AESCipher.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using System.Security.Cryptography;
19 |
20 | namespace Orion.Crypto
21 | {
22 | public class AESCipher
23 | {
24 | private readonly byte[] aCounter;
25 | private readonly SymmetricAlgorithm pAlgorithm;
26 | private readonly ICryptoTransform pCounterEncryptor;
27 |
28 | public int BlockSize { get { return pAlgorithm.BlockSize / 8; } }
29 |
30 | /*
31 | * Constructs a new AES-CTR cipher.
32 | *
33 | * @param aUserKey A 32-byte User Key
34 | * @param aIV A 16-byte IV Chain
35 | *
36 | */
37 | public AESCipher(byte[] aUserKey, byte[] aIV)
38 | {
39 | this.aCounter = aIV;
40 |
41 | this.pAlgorithm = new AesManaged
42 | {
43 | Mode = CipherMode.ECB,
44 | Padding = PaddingMode.None
45 | };
46 |
47 | this.pCounterEncryptor = pAlgorithm.CreateEncryptor(aUserKey, new byte[BlockSize]);
48 | }
49 |
50 | /*
51 | * Transforms a block, encrypting/decrypting the specified data.
52 | *
53 | * @param pSrc The raw buffer (of either encrypted or decrypted data)
54 | * @param uOffset The initial offset of the source buffer
55 | * @param uLen The length of the block (in bytes) to be transformed
56 | * @param pDest The destination buffer (of now-decrypted or now-encrypted data)
57 | * @param Dst The initial offset of the destination buffer
58 | *
59 | * @return The length of the block that was transformed
60 | *
61 | */
62 | public uint TransformBlock(byte[] pSrc, int uOffset, uint uLen, byte[] pDest, int Dst)
63 | {
64 | for (int i = 0; i < uLen; i += BlockSize)
65 | {
66 | byte[] pXORBlock = new byte[BlockSize];
67 | pCounterEncryptor.TransformBlock(aCounter, 0, aCounter.Length, pXORBlock, 0);
68 | IncrementCounter();
69 |
70 | for (int j = 0; j < pXORBlock.Length; j++)
71 | {
72 | if ((i + j) >= pDest.Length)
73 | {
74 | break;
75 | }
76 | pDest[Dst + i + j] = (byte)(pSrc[uOffset + i + j] ^ pXORBlock[j]);
77 | }
78 | }
79 |
80 | return uLen;
81 | }
82 |
83 | /*
84 | * Increments the XOR block counter.
85 | *
86 | */
87 | private void IncrementCounter()
88 | {
89 | for (int i = aCounter.Length - 1; i >= 0; i--)
90 | {
91 | if (++aCounter[i] != 0)
92 | break;
93 | }
94 | }
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Window/ProgressWindow.Designer.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | namespace Orion.Window
19 | {
20 | partial class ProgressWindow
21 | {
22 | ///
23 | /// Required designer variable.
24 | ///
25 | private System.ComponentModel.IContainer components = null;
26 |
27 | ///
28 | /// Clean up any resources being used.
29 | ///
30 | /// true if managed resources should be disposed; otherwise, false.
31 | protected override void Dispose(bool disposing)
32 | {
33 | if (disposing && (components != null))
34 | {
35 | components.Dispose();
36 | }
37 | base.Dispose(disposing);
38 | }
39 |
40 | #region Windows Form Designer generated code
41 |
42 | ///
43 | /// Required method for Designer support - do not modify
44 | /// the contents of this method with the code editor.
45 | ///
46 | private void InitializeComponent()
47 | {
48 | System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ProgressWindow));
49 | this.pSaveInfo = new System.Windows.Forms.Label();
50 | this.pProgressBar = new System.Windows.Forms.ProgressBar();
51 | this.SuspendLayout();
52 | //
53 | // pSaveInfo
54 | //
55 | this.pSaveInfo.Location = new System.Drawing.Point(12, 9);
56 | this.pSaveInfo.Name = "pSaveInfo";
57 | this.pSaveInfo.Size = new System.Drawing.Size(299, 22);
58 | this.pSaveInfo.TabIndex = 0;
59 | this.pSaveInfo.Text = "Saving ...";
60 | this.pSaveInfo.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
61 | //
62 | // pProgressBar
63 | //
64 | this.pProgressBar.Location = new System.Drawing.Point(12, 34);
65 | this.pProgressBar.Name = "pProgressBar";
66 | this.pProgressBar.Size = new System.Drawing.Size(299, 33);
67 | this.pProgressBar.Step = 1;
68 | this.pProgressBar.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
69 | this.pProgressBar.TabIndex = 1;
70 | //
71 | // ProgressWindow
72 | //
73 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
74 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
75 | this.ClientSize = new System.Drawing.Size(323, 79);
76 | this.ControlBox = false;
77 | this.Controls.Add(this.pProgressBar);
78 | this.Controls.Add(this.pSaveInfo);
79 | this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
80 | this.MaximizeBox = false;
81 | this.MinimizeBox = false;
82 | this.Name = "ProgressWindow";
83 | this.ShowInTaskbar = false;
84 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
85 | this.Text = "Saving...";
86 | this.TopMost = true;
87 | this.ResumeLayout(false);
88 |
89 | }
90 |
91 | #endregion
92 |
93 | private System.Windows.Forms.Label pSaveInfo;
94 | private System.Windows.Forms.ProgressBar pProgressBar;
95 | }
96 | }
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/PackFileHeaderVer2.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Common;
19 | using System.IO;
20 |
21 | namespace Orion.Crypto.Stream
22 | {
23 | public class PackFileHeaderVer2 : PackFileHeaderVerBase
24 | {
25 | private uint dwBufferFlag;
26 | private int nFileIndex;
27 | private uint uEncodedFileSize;
28 | private ulong uCompressedFileSize;
29 | private ulong uFileSize;
30 | private ulong uOffset;
31 |
32 | private PackFileHeaderVer2()
33 | {
34 | // Interesting.. no reserved bytes stored in Ver2.
35 | }
36 |
37 | public PackFileHeaderVer2(BinaryReader pReader)
38 | : this()
39 | {
40 | this.dwBufferFlag = pReader.ReadUInt32(); //[ecx+8]
41 | this.nFileIndex = pReader.ReadInt32(); //[ecx+12]
42 | this.uEncodedFileSize = pReader.ReadUInt32(); //[ecx+16]
43 | this.uCompressedFileSize = pReader.ReadUInt64(); //[ecx+20] | [ecx+24]
44 | this.uFileSize = pReader.ReadUInt64(); //[ecx+28] | [ecx+32]
45 | this.uOffset = pReader.ReadUInt64(); //[ecx+36] | [ecx+40]
46 | }
47 |
48 | public static PackFileHeaderVer2 CreateHeader(int nIndex, uint dwFlag, ulong uOffset, byte[] pData)
49 | {
50 | uint uLen, uCompressedLen, uEncodedLen;
51 |
52 | CryptoMan.Encrypt(PackVer.NS2F, pData, dwFlag, out uLen, out uCompressedLen, out uEncodedLen);
53 |
54 | return new PackFileHeaderVer2
55 | {
56 | dwBufferFlag = dwFlag,
57 | nFileIndex = nIndex,
58 | uEncodedFileSize = uEncodedLen,
59 | uCompressedFileSize = uCompressedLen,
60 | uFileSize = uLen,
61 | uOffset = uOffset
62 | };
63 | }
64 |
65 | public void Encode(BinaryWriter pWriter)
66 | {
67 | pWriter.Write(this.dwBufferFlag);
68 | pWriter.Write(this.nFileIndex);
69 | pWriter.Write(this.uEncodedFileSize);
70 | pWriter.Write(this.uCompressedFileSize);
71 | pWriter.Write(this.uFileSize);
72 | pWriter.Write(this.uOffset);
73 | }
74 |
75 | public uint GetVer()
76 | {
77 | return PackVer.NS2F;
78 | }
79 |
80 | public int GetFileIndex()
81 | {
82 | return nFileIndex;
83 | }
84 |
85 | public uint GetBufferFlag()
86 | {
87 | return dwBufferFlag;
88 | }
89 |
90 | public ulong GetOffset()
91 | {
92 | return uOffset;
93 | }
94 |
95 | public uint GetEncodedFileSize()
96 | {
97 | return uEncodedFileSize;
98 | }
99 |
100 | public ulong GetCompressedFileSize()
101 | {
102 | return uCompressedFileSize;
103 | }
104 |
105 | public ulong GetFileSize()
106 | {
107 | return uFileSize;
108 | }
109 |
110 | public void SetFileIndex(int nIndex)
111 | {
112 | this.nFileIndex = nIndex;
113 | }
114 |
115 | public void SetOffset(ulong uOffset)
116 | {
117 | this.uOffset = uOffset;
118 | }
119 |
120 | public void SetEncodedFileSize(uint uEncoded)
121 | {
122 | this.uEncodedFileSize = uEncoded;
123 | }
124 |
125 | public void SetCompressedFileSize(ulong uCompressed)
126 | {
127 | this.uCompressedFileSize = uCompressed;
128 | }
129 |
130 | public void SetFileSize(ulong uSize)
131 | {
132 | this.uFileSize = uSize;
133 | }
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/PackStreamVer2.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Common;
19 | using System.Collections.Generic;
20 | using System.IO;
21 |
22 | namespace Orion.Crypto.Stream
23 | {
24 | public class PackStreamVer2 : PackStreamVerBase
25 | {
26 | private uint dwFileListCount;
27 | private ulong dwCompressedDataSize;
28 | private ulong dwEncodedDataSize;
29 | private ulong dwHeaderSize;
30 | private ulong dwCompressedHeaderSize;
31 | private ulong dwEncodedHeaderSize;
32 | private ulong dwDataSize;
33 | private readonly List aFileList;
34 |
35 | private PackStreamVer2()
36 | {
37 | this.aFileList = new List();
38 | }
39 |
40 | public static PackStreamVer2 ParseHeader(BinaryReader pReader)
41 | {
42 | return new PackStreamVer2
43 | {
44 | dwFileListCount = pReader.ReadUInt32(),
45 | dwCompressedDataSize = pReader.ReadUInt64(),
46 | dwEncodedDataSize = pReader.ReadUInt64(),
47 | dwHeaderSize = pReader.ReadUInt64(),
48 | dwCompressedHeaderSize = pReader.ReadUInt64(),
49 | dwEncodedHeaderSize = pReader.ReadUInt64(),
50 | dwDataSize = pReader.ReadUInt64()
51 | };
52 | }
53 |
54 | public void Encode(BinaryWriter pWriter)
55 | {
56 | pWriter.Write(this.dwFileListCount);
57 | pWriter.Write(this.dwCompressedDataSize);
58 | pWriter.Write(this.dwEncodedDataSize);
59 | pWriter.Write(this.dwHeaderSize);
60 | pWriter.Write(this.dwCompressedHeaderSize);
61 | pWriter.Write(this.dwEncodedHeaderSize);
62 | pWriter.Write(this.dwDataSize);
63 | }
64 |
65 | public uint GetVer()
66 | {
67 | return PackVer.NS2F;
68 | }
69 |
70 | public ulong GetCompressedHeaderSize()
71 | {
72 | return dwCompressedHeaderSize;
73 | }
74 |
75 | public ulong GetEncodedHeaderSize()
76 | {
77 | return dwEncodedHeaderSize;
78 | }
79 |
80 | public ulong GetHeaderSize()
81 | {
82 | return dwHeaderSize;
83 | }
84 |
85 | public ulong GetCompressedDataSize()
86 | {
87 | return dwCompressedDataSize;
88 | }
89 |
90 | public ulong GetEncodedDataSize()
91 | {
92 | return dwEncodedDataSize;
93 | }
94 |
95 | public ulong GetDataSize()
96 | {
97 | return dwDataSize;
98 | }
99 |
100 | public ulong GetFileListCount()
101 | {
102 | return dwFileListCount;
103 | }
104 |
105 | public List GetFileList()
106 | {
107 | return aFileList;
108 | }
109 |
110 | public void SetCompressedHeaderSize(ulong uCompressed)
111 | {
112 | this.dwCompressedHeaderSize = uCompressed;
113 | }
114 |
115 | public void SetEncodedHeaderSize(ulong uEncoded)
116 | {
117 | this.dwEncodedHeaderSize = uEncoded;
118 | }
119 |
120 | public void SetHeaderSize(ulong uSize)
121 | {
122 | this.dwHeaderSize = uSize;
123 | }
124 |
125 | public void SetCompressedDataSize(ulong uCompressed)
126 | {
127 | this.dwCompressedDataSize = uCompressed;
128 | }
129 |
130 | public void SetEncodedDataSize(ulong uEncoded)
131 | {
132 | this.dwEncodedDataSize = uEncoded;
133 | }
134 |
135 | public void SetDataSize(ulong uSize)
136 | {
137 | this.dwDataSize = uSize;
138 | }
139 |
140 | public void SetFileListCount(ulong uCount)
141 | {
142 | this.dwFileListCount = (uint) uCount;
143 | }
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/PackFileHeaderVer3.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using System.IO;
19 |
20 | namespace Orion.Crypto.Stream
21 | {
22 | public class PackFileHeaderVer3 : PackFileHeaderVerBase
23 | {
24 | private readonly uint uVer;
25 | private uint dwBufferFlag;
26 | private int nFileIndex;
27 | private uint uEncodedFileSize;
28 | private int[] Reserved;
29 | private ulong uCompressedFileSize;
30 | private ulong uFileSize;
31 | private ulong uOffset;
32 |
33 | private PackFileHeaderVer3(uint uVer)
34 | {
35 | this.uVer = uVer;
36 | this.Reserved = new int[1];
37 | }
38 |
39 | public PackFileHeaderVer3(uint uVer, BinaryReader pReader)
40 | : this(uVer)
41 | {
42 | this.dwBufferFlag = pReader.ReadUInt32(); //[ecx+8]
43 | this.nFileIndex = pReader.ReadInt32(); //[ecx+12]
44 | this.uEncodedFileSize = pReader.ReadUInt32(); //[ecx+16]
45 | this.Reserved[0] = pReader.ReadInt32(); //[ecx+20]
46 | this.uCompressedFileSize = pReader.ReadUInt64(); //[ecx+24] | [ecx+28]
47 | this.uFileSize = pReader.ReadUInt64(); //[ecx+32] | [ecx+36]
48 | this.uOffset = pReader.ReadUInt64(); //[ecx+40] | [ecx+44]
49 | }
50 |
51 | public static PackFileHeaderVer3 CreateHeader(uint uVer, int nIndex, uint dwFlag, ulong uOffset, byte[] pData)
52 | {
53 | uint uLen, uCompressedLen, uEncodedLen;
54 |
55 | CryptoMan.Encrypt(uVer, pData, dwFlag, out uLen, out uCompressedLen, out uEncodedLen);
56 |
57 | return new PackFileHeaderVer3(uVer)
58 | {
59 | dwBufferFlag = dwFlag,
60 | nFileIndex = nIndex,
61 | uEncodedFileSize = uEncodedLen,
62 | uCompressedFileSize = uCompressedLen,
63 | uFileSize = uLen,
64 | uOffset = uOffset
65 | };
66 | }
67 |
68 | public void Encode(BinaryWriter pWriter)
69 | {
70 | pWriter.Write(this.dwBufferFlag);
71 | pWriter.Write(this.nFileIndex);
72 | pWriter.Write(this.uEncodedFileSize);
73 | pWriter.Write(this.Reserved[0]);
74 | pWriter.Write(this.uCompressedFileSize);
75 | pWriter.Write(this.uFileSize);
76 | pWriter.Write(this.uOffset);
77 | }
78 |
79 | public uint GetVer()
80 | {
81 | return uVer;
82 | }
83 |
84 | public int GetFileIndex()
85 | {
86 | return nFileIndex;
87 | }
88 |
89 | public uint GetBufferFlag()
90 | {
91 | return dwBufferFlag;
92 | }
93 |
94 | public ulong GetOffset()
95 | {
96 | return uOffset;
97 | }
98 |
99 | public uint GetEncodedFileSize()
100 | {
101 | return uEncodedFileSize;
102 | }
103 |
104 | public ulong GetCompressedFileSize()
105 | {
106 | return uCompressedFileSize;
107 | }
108 |
109 | public ulong GetFileSize()
110 | {
111 | return uFileSize;
112 | }
113 |
114 | public void SetFileIndex(int nIndex)
115 | {
116 | this.nFileIndex = nIndex;
117 | }
118 |
119 | public void SetOffset(ulong uOffset)
120 | {
121 | this.uOffset = uOffset;
122 | }
123 |
124 | public void SetEncodedFileSize(uint uEncoded)
125 | {
126 | this.uEncodedFileSize = uEncoded;
127 | }
128 |
129 | public void SetCompressedFileSize(ulong uCompressed)
130 | {
131 | this.uCompressedFileSize = uCompressed;
132 | }
133 |
134 | public void SetFileSize(ulong uSize)
135 | {
136 | this.uFileSize = uSize;
137 | }
138 | }
139 | }
140 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Common/PackFileEntry.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Stream;
19 | using System;
20 | using System.Collections.Generic;
21 |
22 | namespace Orion.Crypto.Common
23 | {
24 | [Serializable]
25 | public class PackFileEntry : IComparable
26 | {
27 | public const string DATA_FORMAT = "Pack.Node.FileEntry";
28 |
29 | public int Index { get; set; } // The index of the file in the lookup table
30 | public string Hash { get; set; } // A hash assigned to all files in the directory
31 | public string Name { get; set; } // The full name of the file (path/name.ext)
32 | public string TreeName { get; set; } // The visual name displayed in the tree (name.ext)
33 | public PackFileHeaderVerBase FileHeader { get; set; } // The file information (size, offset, etc.)
34 | public byte[] Data { get; set; } // The raw, decrypted, and current data buffer of the file
35 | public bool Changed { get; set; } // If the data has been modified in the repacker
36 |
37 | public PackFileEntry CreateCopy(byte[] pData = null)
38 | {
39 | return new PackFileEntry
40 | {
41 | Index = int.MaxValue,
42 | Hash = this.Hash,
43 | Name = this.Name,
44 | TreeName = this.TreeName,
45 | //FileHeader = this.FileHeader,
46 | Data = (pData == null ? this.Data : pData),
47 | Changed = true
48 | };
49 | }
50 |
51 | public int CompareTo(PackFileEntry pObj)
52 | {
53 | if (this.Index == pObj.Index) return 0;
54 | if (this.Index > pObj.Index) return 1;
55 | return -1;
56 | }
57 |
58 | public override string ToString()
59 | {
60 | if (string.IsNullOrEmpty(Hash))
61 | {
62 | return string.Format("{0},{1}\r\n", Index, Name);
63 | }
64 | return string.Format("{0},{1},{2}\r\n", Index, Hash, Name);
65 | }
66 |
67 | /*
68 | * Creates a collection of pack file entries from the file string.
69 | *
70 | * @param sFileString The string containing a table of of files
71 | *
72 | * @return A list of file entries with their index/hash/name loaded
73 | *
74 | */
75 | public static List CreateFileList(string sFileString)
76 | {
77 | List aFileList = new List();
78 |
79 | string[] aEntries = sFileString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
80 | foreach (string sEntry in aEntries)
81 | {
82 | int nProperties = 0;
83 | foreach (char c in sEntry)
84 | {
85 | if (c == ',')
86 | ++nProperties;
87 | }
88 |
89 | string sIndex, sName;
90 | if (nProperties == 1)
91 | {
92 | sIndex = sEntry.Split(',')[0]; //strtok(pStr, ",")
93 | sName = sEntry.Split(',')[1]; //strtok(pStr, ",")
94 |
95 | aFileList.Add(new PackFileEntry
96 | {
97 | Index = int.Parse(sIndex), //atoi(sIndex)
98 | Name = sName
99 | });
100 | } else if (nProperties == 2)
101 | {
102 | sIndex = sEntry.Split(',')[0]; //strtok(pStr, ",")
103 | sName = sEntry.Split(',')[2]; //if (nPropertyIdx == 1)
104 |
105 | aFileList.Add(new PackFileEntry
106 | {
107 | Index = int.Parse(sIndex), //atoi(sIndex)
108 | Hash = sEntry.Split(',')[1], //if (!nPropertyIdx)
109 | Name = sName
110 | });
111 | }
112 | }
113 |
114 | return aFileList;
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/PackStreamVer1.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Common;
19 | using System.Collections.Generic;
20 | using System.IO;
21 |
22 | namespace Orion.Crypto.Stream
23 | {
24 | public class PackStreamVer1 : PackStreamVerBase
25 | {
26 | private uint uReserved;
27 | private ulong dwCompressedDataSize;
28 | private ulong dwEncodedDataSize;
29 | private ulong dwHeaderSize;
30 | private ulong dwCompressedHeaderSize;
31 | private ulong dwEncodedHeaderSize;
32 | private ulong dwFileListCount;
33 | private ulong dwDataSize;
34 | private readonly List aFileList;
35 |
36 | private PackStreamVer1()
37 | {
38 | this.aFileList = new List();
39 | }
40 |
41 | public static PackStreamVer1 ParseHeader(BinaryReader pReader)
42 | {
43 | return new PackStreamVer1
44 | {
45 | uReserved = pReader.ReadUInt32(),
46 | dwCompressedDataSize = pReader.ReadUInt64(),
47 | dwEncodedDataSize = pReader.ReadUInt64(),
48 | dwHeaderSize = pReader.ReadUInt64(),
49 | dwCompressedHeaderSize = pReader.ReadUInt64(),
50 | dwEncodedHeaderSize = pReader.ReadUInt64(),
51 | dwFileListCount = pReader.ReadUInt64(),
52 | dwDataSize = pReader.ReadUInt64()
53 | };
54 | }
55 |
56 | public void Encode(BinaryWriter pWriter)
57 | {
58 | pWriter.Write(this.uReserved);
59 | pWriter.Write(this.dwCompressedDataSize);
60 | pWriter.Write(this.dwEncodedDataSize);
61 | pWriter.Write(this.dwHeaderSize);
62 | pWriter.Write(this.dwCompressedHeaderSize);
63 | pWriter.Write(this.dwEncodedHeaderSize);
64 | pWriter.Write(this.dwFileListCount);
65 | pWriter.Write(this.dwDataSize);
66 | }
67 |
68 | public uint GetVer()
69 | {
70 | return PackVer.MS2F;
71 | }
72 |
73 | public ulong GetCompressedHeaderSize()
74 | {
75 | return dwCompressedHeaderSize;
76 | }
77 |
78 | public ulong GetEncodedHeaderSize()
79 | {
80 | return dwEncodedHeaderSize;
81 | }
82 |
83 | public ulong GetHeaderSize()
84 | {
85 | return dwHeaderSize;
86 | }
87 |
88 | public ulong GetCompressedDataSize()
89 | {
90 | return dwCompressedDataSize;
91 | }
92 |
93 | public ulong GetEncodedDataSize()
94 | {
95 | return dwEncodedDataSize;
96 | }
97 |
98 | public ulong GetDataSize()
99 | {
100 | return dwDataSize;
101 | }
102 |
103 | public ulong GetFileListCount()
104 | {
105 | return dwFileListCount;
106 | }
107 |
108 | public List GetFileList()
109 | {
110 | return aFileList;
111 | }
112 |
113 | public void SetCompressedHeaderSize(ulong uCompressed)
114 | {
115 | this.dwCompressedHeaderSize = uCompressed;
116 | }
117 |
118 | public void SetEncodedHeaderSize(ulong uEncoded)
119 | {
120 | this.dwEncodedHeaderSize = uEncoded;
121 | }
122 |
123 | public void SetHeaderSize(ulong uSize)
124 | {
125 | this.dwHeaderSize = uSize;
126 | }
127 |
128 | public void SetCompressedDataSize(ulong uCompressed)
129 | {
130 | this.dwCompressedDataSize = uCompressed;
131 | }
132 |
133 | public void SetEncodedDataSize(ulong uEncoded)
134 | {
135 | this.dwEncodedDataSize = uEncoded;
136 | }
137 |
138 | public void SetDataSize(ulong uSize)
139 | {
140 | this.dwDataSize = uSize;
141 | }
142 |
143 | public void SetFileListCount(ulong uCount)
144 | {
145 | this.dwFileListCount = uCount;
146 | }
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Window/About.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using System;
19 | using System.Reflection;
20 | using System.Windows.Forms;
21 |
22 | namespace Orion.Window
23 | {
24 | partial class About : Form
25 | {
26 | public About()
27 | {
28 | InitializeComponent();
29 |
30 | this.Text = String.Format("About {0}", AssemblyTitle);
31 | this.labelProductName.Text = AssemblyProduct;
32 | this.labelVersion.Text = String.Format(" Version {0}", AssemblyVersion);
33 | this.labelCreator.Text = "Created by Eric";
34 | this.labelSpecialThanks.Text = "Special Thanks: @inumedia, @DinoChiesa, @andburn";
35 | this.textBoxDescription.Text = "Orion2 Repacker - A MapleStory2 Data File Utility"
36 | + "\r\n\r\nFeatures:"
37 | + "\r\n* Loads ANY MS2 header(.m2h)/data(.m2d) file"
38 | + "\r\n* Renders the data from the selected XML/INI/PNG/DDS/etc. file inside the repacker"
39 | + "\r\n* Ability to extract the selected file(s) to disk"
40 | + "\r\n* Edit/Add data within the files"
41 | + "\r\n* Save your changes by re-packing the data to file";
42 | }
43 |
44 | #region Assembly Attribute Accessors
45 |
46 | public string AssemblyTitle
47 | {
48 | get
49 | {
50 | object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
51 | if (attributes.Length > 0)
52 | {
53 | AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
54 | if (titleAttribute.Title != "")
55 | {
56 | return titleAttribute.Title;
57 | }
58 | }
59 | return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
60 | }
61 | }
62 |
63 | public string AssemblyVersion
64 | {
65 | get
66 | {
67 | return Assembly.GetExecutingAssembly().GetName().Version.ToString();
68 | }
69 | }
70 |
71 | public string AssemblyDescription
72 | {
73 | get
74 | {
75 | object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
76 | if (attributes.Length == 0)
77 | {
78 | return "";
79 | }
80 | return ((AssemblyDescriptionAttribute)attributes[0]).Description;
81 | }
82 | }
83 |
84 | public string AssemblyProduct
85 | {
86 | get
87 | {
88 | object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
89 | if (attributes.Length == 0)
90 | {
91 | return "";
92 | }
93 | return ((AssemblyProductAttribute)attributes[0]).Product;
94 | }
95 | }
96 |
97 | public string AssemblyCopyright
98 | {
99 | get
100 | {
101 | object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
102 | if (attributes.Length == 0)
103 | {
104 | return "";
105 | }
106 | return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
107 | }
108 | }
109 |
110 | public string AssemblyCompany
111 | {
112 | get
113 | {
114 | object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
115 | if (attributes.Length == 0)
116 | {
117 | return "";
118 | }
119 | return ((AssemblyCompanyAttribute)attributes[0]).Company;
120 | }
121 | }
122 | #endregion
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/PackStreamVer3.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Common;
19 | using System.Collections.Generic;
20 | using System.IO;
21 |
22 | namespace Orion.Crypto.Stream
23 | {
24 | public class PackStreamVer3 : PackStreamVerBase
25 | {
26 | private readonly uint uVer;
27 | private uint dwFileListCount;
28 | private uint dwReserved;
29 | private ulong dwCompressedDataSize;
30 | private ulong dwEncodedDataSize;
31 | private ulong dwHeaderSize;
32 | private ulong dwCompressedHeaderSize;
33 | private ulong dwEncodedHeaderSize;
34 | private ulong dwDataSize;
35 | private readonly List aFileList;
36 |
37 | private PackStreamVer3(uint uVer)
38 | {
39 | this.uVer = uVer;
40 | this.aFileList = new List();
41 | }
42 |
43 | public static PackStreamVer3 ParseHeader(BinaryReader pReader, uint uVer)
44 | {
45 | return new PackStreamVer3(uVer)
46 | {
47 | dwFileListCount = pReader.ReadUInt32(),
48 | dwReserved = pReader.ReadUInt32(),
49 | dwCompressedDataSize = pReader.ReadUInt64(),
50 | dwEncodedDataSize = pReader.ReadUInt64(),
51 | dwCompressedHeaderSize = pReader.ReadUInt64(),
52 | dwEncodedHeaderSize = pReader.ReadUInt64(),
53 | dwDataSize = pReader.ReadUInt64(),
54 | dwHeaderSize = pReader.ReadUInt64()
55 | };
56 | }
57 |
58 | public void Encode(BinaryWriter pWriter)
59 | {
60 | pWriter.Write(this.dwFileListCount);
61 | pWriter.Write(this.dwReserved);
62 | pWriter.Write(this.dwCompressedDataSize);
63 | pWriter.Write(this.dwEncodedDataSize);
64 | pWriter.Write(this.dwCompressedHeaderSize);
65 | pWriter.Write(this.dwEncodedHeaderSize);
66 | pWriter.Write(this.dwDataSize);
67 | pWriter.Write(this.dwHeaderSize);
68 | }
69 |
70 | public uint GetVer()
71 | {
72 | return uVer;//OS2F/PS2F
73 | }
74 |
75 | public ulong GetCompressedHeaderSize()
76 | {
77 | return dwCompressedHeaderSize;
78 | }
79 |
80 | public ulong GetEncodedHeaderSize()
81 | {
82 | return dwEncodedHeaderSize;
83 | }
84 |
85 | public ulong GetHeaderSize()
86 | {
87 | return dwHeaderSize;
88 | }
89 |
90 | public ulong GetCompressedDataSize()
91 | {
92 | return dwCompressedDataSize;
93 | }
94 |
95 | public ulong GetEncodedDataSize()
96 | {
97 | return dwEncodedDataSize;
98 | }
99 |
100 | public ulong GetDataSize()
101 | {
102 | return dwDataSize;
103 | }
104 |
105 | public ulong GetFileListCount()
106 | {
107 | return dwFileListCount;
108 | }
109 |
110 | public List GetFileList()
111 | {
112 | return aFileList;
113 | }
114 |
115 | public void SetCompressedHeaderSize(ulong uCompressed)
116 | {
117 | this.dwCompressedHeaderSize = uCompressed;
118 | }
119 |
120 | public void SetEncodedHeaderSize(ulong uEncoded)
121 | {
122 | this.dwEncodedHeaderSize = uEncoded;
123 | }
124 |
125 | public void SetHeaderSize(ulong uSize)
126 | {
127 | this.dwHeaderSize = uSize;
128 | }
129 |
130 | public void SetCompressedDataSize(ulong uCompressed)
131 | {
132 | this.dwCompressedDataSize = uCompressed;
133 | }
134 |
135 | public void SetEncodedDataSize(ulong uEncoded)
136 | {
137 | this.dwEncodedDataSize = uEncoded;
138 | }
139 |
140 | public void SetDataSize(ulong uSize)
141 | {
142 | this.dwDataSize = uSize;
143 | }
144 |
145 | public void SetFileListCount(ulong uCount)
146 | {
147 | this.dwFileListCount = (uint) uCount;
148 | }
149 | }
150 | }
151 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/PackFileHeaderVer1.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Common;
19 | using System.IO;
20 |
21 | namespace Orion.Crypto.Stream
22 | {
23 | public class PackFileHeaderVer1 : PackFileHeaderVerBase
24 | {
25 | private byte[] aPackingDef; //A "Packing Definition", unused.
26 | private int nFileIndex;
27 | private uint dwBufferFlag;
28 | private int[] Reserved;
29 | private ulong uOffset;
30 | private uint uEncodedFileSize;
31 | private ulong uCompressedFileSize;
32 | private ulong uFileSize;
33 |
34 | private PackFileHeaderVer1()
35 | {
36 | this.aPackingDef = new byte[4];
37 | this.Reserved = new int[2];
38 | }
39 |
40 | public PackFileHeaderVer1(BinaryReader pReader)
41 | : this()
42 | {
43 | this.aPackingDef = pReader.ReadBytes(4); //[ecx+16]
44 | this.nFileIndex = pReader.ReadInt32(); //[ecx+20]
45 | this.dwBufferFlag = pReader.ReadUInt32(); //[ecx+24]
46 | this.Reserved[0] = pReader.ReadInt32(); //[ecx+28]
47 | this.uOffset = pReader.ReadUInt64(); //[ecx+32] | [ecx+36]
48 | this.uEncodedFileSize = pReader.ReadUInt32(); //[ecx+40]
49 | this.Reserved[1] = pReader.ReadInt32(); //[ecx+44]
50 | this.uCompressedFileSize = pReader.ReadUInt64(); //[ecx+48] | [ecx+52]
51 | this.uFileSize = pReader.ReadUInt64(); //[ecx+56] | [ecx+60]
52 | }
53 |
54 | public static PackFileHeaderVer1 CreateHeader(int nIndex, uint dwFlag, ulong uOffset, byte[] pData)
55 | {
56 | uint uLen, uCompressedLen, uEncodedLen;
57 |
58 | CryptoMan.Encrypt(PackVer.MS2F, pData, dwFlag, out uLen, out uCompressedLen, out uEncodedLen);
59 |
60 | return new PackFileHeaderVer1
61 | {
62 | nFileIndex = nIndex,
63 | dwBufferFlag = dwFlag,
64 | uOffset = uOffset,
65 | uEncodedFileSize = uEncodedLen,
66 | uCompressedFileSize = uCompressedLen,
67 | uFileSize = uLen
68 | };
69 | }
70 |
71 | public void Encode(BinaryWriter pWriter)
72 | {
73 | pWriter.Write(this.aPackingDef);
74 | pWriter.Write(this.nFileIndex);
75 | pWriter.Write(this.dwBufferFlag);
76 | pWriter.Write(this.Reserved[0]);
77 | pWriter.Write(this.uOffset);
78 | pWriter.Write(this.uEncodedFileSize);
79 | pWriter.Write(this.Reserved[1]);
80 | pWriter.Write(this.uCompressedFileSize);
81 | pWriter.Write(this.uFileSize);
82 | }
83 |
84 | public uint GetVer()
85 | {
86 | return PackVer.MS2F;
87 | }
88 |
89 | public int GetFileIndex()
90 | {
91 | return nFileIndex;
92 | }
93 |
94 | public uint GetBufferFlag()
95 | {
96 | return dwBufferFlag;
97 | }
98 |
99 | public ulong GetOffset()
100 | {
101 | return uOffset;
102 | }
103 |
104 | public uint GetEncodedFileSize()
105 | {
106 | return uEncodedFileSize;
107 | }
108 |
109 | public ulong GetCompressedFileSize()
110 | {
111 | return uCompressedFileSize;
112 | }
113 |
114 | public ulong GetFileSize()
115 | {
116 | return uFileSize;
117 | }
118 |
119 | public void SetFileIndex(int nIndex)
120 | {
121 | this.nFileIndex = nIndex;
122 | }
123 |
124 | public void SetOffset(ulong uOffset)
125 | {
126 | this.uOffset = uOffset;
127 | }
128 |
129 | public void SetEncodedFileSize(uint uEncoded)
130 | {
131 | this.uEncodedFileSize = uEncoded;
132 | }
133 |
134 | public void SetCompressedFileSize(ulong uCompressed)
135 | {
136 | this.uCompressedFileSize = uCompressed;
137 | }
138 |
139 | public void SetFileSize(ulong uSize)
140 | {
141 | this.uFileSize = uSize;
142 | }
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/zlib/ZlibConstants.cs:
--------------------------------------------------------------------------------
1 | // ZlibConstants.cs
2 | // ------------------------------------------------------------------
3 | //
4 | // Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
5 | // All rights reserved.
6 | //
7 | // This code module is part of DotNetZip, a zipfile class library.
8 | //
9 | // ------------------------------------------------------------------
10 | //
11 | // This code is licensed under the Microsoft Public License.
12 | // See the file License.txt for the license details.
13 | // More info on: http://dotnetzip.codeplex.com
14 | //
15 | // ------------------------------------------------------------------
16 | //
17 | // last saved (in emacs):
18 | // Time-stamp: <2009-November-03 18:50:19>
19 | //
20 | // ------------------------------------------------------------------
21 | //
22 | // This module defines constants used by the zlib class library. This
23 | // code is derived from the jzlib implementation of zlib, but
24 | // significantly modified. In keeping with the license for jzlib, the
25 | // copyright to that code is included here.
26 | //
27 | // ------------------------------------------------------------------
28 | //
29 | // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
30 | //
31 | // Redistribution and use in source and binary forms, with or without
32 | // modification, are permitted provided that the following conditions are met:
33 | //
34 | // 1. Redistributions of source code must retain the above copyright notice,
35 | // this list of conditions and the following disclaimer.
36 | //
37 | // 2. Redistributions in binary form must reproduce the above copyright
38 | // notice, this list of conditions and the following disclaimer in
39 | // the documentation and/or other materials provided with the distribution.
40 | //
41 | // 3. The names of the authors may not be used to endorse or promote products
42 | // derived from this software without specific prior written permission.
43 | //
44 | // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
45 | // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
46 | // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
47 | // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
48 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
50 | // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
51 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
53 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 | //
55 | // -----------------------------------------------------------------------
56 | //
57 | // This program is based on zlib-1.1.3; credit to authors
58 | // Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
59 | // and contributors of zlib.
60 | //
61 | // -----------------------------------------------------------------------
62 |
63 | using System;
64 | using System.Collections.Generic;
65 | using System.Linq;
66 | using System.Text;
67 | using System.Threading.Tasks;
68 |
69 | namespace Orion.Crypto.Stream.zlib
70 | {
71 | ///
72 | /// A bunch of constants used in the Zlib interface.
73 | ///
74 | public static class ZlibConstants
75 | {
76 | ///
77 | /// The maximum number of window bits for the Deflate algorithm.
78 | ///
79 | public const int WindowBitsMax = 15; // 32K LZ77 window
80 |
81 | ///
82 | /// The default number of window bits for the Deflate algorithm.
83 | ///
84 | public const int WindowBitsDefault = WindowBitsMax;
85 |
86 | ///
87 | /// indicates everything is A-OK
88 | ///
89 | public const int Z_OK = 0;
90 |
91 | ///
92 | /// Indicates that the last operation reached the end of the stream.
93 | ///
94 | public const int Z_STREAM_END = 1;
95 |
96 | ///
97 | /// The operation ended in need of a dictionary.
98 | ///
99 | public const int Z_NEED_DICT = 2;
100 |
101 | ///
102 | /// There was an error with the stream - not enough data, not open and readable, etc.
103 | ///
104 | public const int Z_STREAM_ERROR = -2;
105 |
106 | ///
107 | /// There was an error with the data - not enough data, bad data, etc.
108 | ///
109 | public const int Z_DATA_ERROR = -3;
110 |
111 | ///
112 | /// There was an error with the working buffer.
113 | ///
114 | public const int Z_BUF_ERROR = -5;
115 |
116 | ///
117 | /// The size of the working buffer used in the ZlibCodec class. Defaults to 8192 bytes.
118 | ///
119 | #if NETCF
120 | public const int WorkingBufferSizeDefault = 8192;
121 | #else
122 | public const int WorkingBufferSizeDefault = 16384;
123 | #endif
124 | ///
125 | /// The minimum size of the working buffer used in the ZlibCodec class. Currently it is 128 bytes.
126 | ///
127 | public const int WorkingBufferSizeMin = 1024;
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | project.fragment.lock.json
46 | artifacts/
47 |
48 | *_i.c
49 | *_p.c
50 | *_i.h
51 | *.ilk
52 | *.meta
53 | *.obj
54 | *.pch
55 | *.pdb
56 | *.pgc
57 | *.pgd
58 | *.rsp
59 | *.sbr
60 | *.tlb
61 | *.tli
62 | *.tlh
63 | *.tmp
64 | *.tmp_proj
65 | *.log
66 | *.vspscc
67 | *.vssscc
68 | .builds
69 | *.pidb
70 | *.svclog
71 | *.scc
72 |
73 | # Chutzpah Test files
74 | _Chutzpah*
75 |
76 | # Visual C++ cache files
77 | ipch/
78 | *.aps
79 | *.ncb
80 | *.opendb
81 | *.opensdf
82 | *.sdf
83 | *.cachefile
84 | *.VC.db
85 | *.VC.VC.opendb
86 |
87 | # Visual Studio profiler
88 | *.psess
89 | *.vsp
90 | *.vspx
91 | *.sap
92 |
93 | # TFS 2012 Local Workspace
94 | $tf/
95 |
96 | # Guidance Automation Toolkit
97 | *.gpState
98 |
99 | # ReSharper is a .NET coding add-in
100 | _ReSharper*/
101 | *.[Rr]e[Ss]harper
102 | *.DotSettings.user
103 |
104 | # JustCode is a .NET coding add-in
105 | .JustCode
106 |
107 | # TeamCity is a build add-in
108 | _TeamCity*
109 |
110 | # DotCover is a Code Coverage Tool
111 | *.dotCover
112 |
113 | # NCrunch
114 | _NCrunch_*
115 | .*crunch*.local.xml
116 | nCrunchTemp_*
117 |
118 | # MightyMoose
119 | *.mm.*
120 | AutoTest.Net/
121 |
122 | # Web workbench (sass)
123 | .sass-cache/
124 |
125 | # Installshield output folder
126 | [Ee]xpress/
127 |
128 | # DocProject is a documentation generator add-in
129 | DocProject/buildhelp/
130 | DocProject/Help/*.HxT
131 | DocProject/Help/*.HxC
132 | DocProject/Help/*.hhc
133 | DocProject/Help/*.hhk
134 | DocProject/Help/*.hhp
135 | DocProject/Help/Html2
136 | DocProject/Help/html
137 |
138 | # Click-Once directory
139 | publish/
140 |
141 | # Publish Web Output
142 | *.[Pp]ublish.xml
143 | *.azurePubxml
144 | # TODO: Comment the next line if you want to checkin your web deploy settings
145 | # but database connection strings (with potential passwords) will be unencrypted
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
150 | # checkin your Azure Web App publish settings, but sensitive information contained
151 | # in these scripts will be unencrypted
152 | PublishScripts/
153 |
154 | # NuGet Packages
155 | *.nupkg
156 | # The packages folder can be ignored because of Package Restore
157 | **/packages/*
158 | # except build/, which is used as an MSBuild target.
159 | !**/packages/build/
160 | # Uncomment if necessary however generally it will be regenerated when needed
161 | #!**/packages/repositories.config
162 | # NuGet v3's project.json files produces more ignoreable files
163 | *.nuget.props
164 | *.nuget.targets
165 |
166 | # Microsoft Azure Build Output
167 | csx/
168 | *.build.csdef
169 |
170 | # Microsoft Azure Emulator
171 | ecf/
172 | rcf/
173 |
174 | # Windows Store app package directories and files
175 | AppPackages/
176 | BundleArtifacts/
177 | Package.StoreAssociation.xml
178 | _pkginfo.txt
179 |
180 | # Visual Studio cache files
181 | # files ending in .cache can be ignored
182 | *.[Cc]ache
183 | # but keep track of directories ending in .cache
184 | !*.[Cc]ache/
185 |
186 | # Others
187 | ClientBin/
188 | ~$*
189 | *~
190 | *.dbmdl
191 | *.dbproj.schemaview
192 | *.jfm
193 | *.pfx
194 | *.publishsettings
195 | node_modules/
196 | orleans.codegen.cs
197 |
198 | # Since there are multiple workflows, uncomment next line to ignore bower_components
199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
200 | #bower_components/
201 |
202 | # RIA/Silverlight projects
203 | Generated_Code/
204 |
205 | # Backup & report files from converting an old project file
206 | # to a newer Visual Studio version. Backup files are not needed,
207 | # because we have git ;-)
208 | _UpgradeReport_Files/
209 | Backup*/
210 | UpgradeLog*.XML
211 | UpgradeLog*.htm
212 |
213 | # SQL Server files
214 | *.mdf
215 | *.ldf
216 |
217 | # Business Intelligence projects
218 | *.rdl.data
219 | *.bim.layout
220 | *.bim_*.settings
221 |
222 | # Microsoft Fakes
223 | FakesAssemblies/
224 |
225 | # GhostDoc plugin setting file
226 | *.GhostDoc.xml
227 |
228 | # Node.js Tools for Visual Studio
229 | .ntvs_analysis.dat
230 |
231 | # Visual Studio 6 build log
232 | *.plg
233 |
234 | # Visual Studio 6 workspace options file
235 | *.opt
236 |
237 | # Visual Studio LightSwitch build output
238 | **/*.HTMLClient/GeneratedArtifacts
239 | **/*.DesktopClient/GeneratedArtifacts
240 | **/*.DesktopClient/ModelManifest.xml
241 | **/*.Server/GeneratedArtifacts
242 | **/*.Server/ModelManifest.xml
243 | _Pvt_Extensions
244 |
245 | # Paket dependency manager
246 | .paket/paket.exe
247 | paket-files/
248 |
249 | # FAKE - F# Make
250 | .fake/
251 |
252 | # JetBrains Rider
253 | .idea/
254 | *.sln.iml
255 |
256 | # CodeRush
257 | .cr/
258 |
259 | # Python Tools for Visual Studio (PTVS)
260 | __pycache__/
261 | *.pyc
--------------------------------------------------------------------------------
/Orion2-Repacker/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 | text/microsoft-resx
110 |
111 |
112 | 2.0
113 |
114 |
115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
119 |
120 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Orion2-Repacker.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {54C07F0A-A88D-45AA-BFBF-9FA2BA2AD9B5}
8 | WinExe
9 | Orion
10 | Orion2-Repacker
11 | v4.6.1
12 | 512
13 | true
14 | publish\
15 | true
16 | Disk
17 | false
18 | Foreground
19 | 7
20 | Days
21 | false
22 | false
23 | true
24 | 0
25 | 1.0.0.%2a
26 | false
27 | false
28 | true
29 |
30 |
31 | AnyCPU
32 | true
33 | full
34 | false
35 | bin\Debug\
36 | DEBUG;TRACE
37 | prompt
38 | 4
39 | true
40 |
41 |
42 | AnyCPU
43 | pdbonly
44 | true
45 | bin\Release\
46 | TRACE
47 | prompt
48 | 4
49 | true
50 |
51 |
52 | icon.ico
53 |
54 |
55 |
56 | Resources\Pfim.dll
57 |
58 |
59 | False
60 | Resources\ScintillaNET.dll
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 | Form
103 |
104 |
105 | About.cs
106 |
107 |
108 |
109 |
110 | Form
111 |
112 |
113 | MainWindow.cs
114 |
115 |
116 |
117 |
118 | Form
119 |
120 |
121 | ProgressWindow.cs
122 |
123 |
124 | About.cs
125 |
126 |
127 | MainWindow.cs
128 |
129 |
130 | ResXFileCodeGenerator
131 | Resources.Designer.cs
132 | Designer
133 |
134 |
135 | True
136 | Resources.resx
137 | True
138 |
139 |
140 | ProgressWindow.cs
141 |
142 |
143 | SettingsSingleFileGenerator
144 | Settings.Designer.cs
145 |
146 |
147 | True
148 | Settings.settings
149 | True
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 | False
158 | Microsoft .NET Framework 4.6.1 %28x86 and x64%29
159 | true
160 |
161 |
162 | False
163 | .NET Framework 3.5 SP1
164 | false
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/CryptoMan.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of Orion2, a MapleStory2 Packaging Library Project.
3 | * Copyright (C) 2018 Eric Smith
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | */
17 |
18 | using Orion.Crypto.Common;
19 | using Orion.Crypto.Stream;
20 | using Orion.Crypto.Stream.zlib;
21 | using System;
22 | using System.IO.MemoryMappedFiles;
23 | using System.Text;
24 |
25 | namespace Orion.Crypto
26 | {
27 | public class CryptoMan
28 | {
29 | public class BufferManipulation
30 | {
31 | public const uint
32 | /*
33 | * Standard crypto: Base64 Encoded + AES Encrypted buffers.
34 | *
35 | * mov [dwBufferFlag], 0
36 | * mov byte ptr [dwBufferFlag+3], 0xEE
37 | */
38 | AES = 0xEE000000,
39 | /*
40 | * AES buffers who have been additionally compressed with zlib.
41 | * NOTE: The first bit is the level of compression used.
42 | *
43 | * mov [dwBufferFlag], 0
44 | * mov byte ptr [dwBufferFlag], 9
45 | * mov byte ptr [dwBufferFlag+3], 0xEE
46 | */
47 | AES_ZLIB = AES | 9,
48 | /*
49 | * Alternative crypto: XOR Encrypted buffers.
50 | *
51 | * mov [dwBufferFlag], 0
52 | * mov byte ptr [dwBufferFlag+3], 0xFF
53 | */
54 | XOR = 0xFF000000,
55 | /*
56 | * XOR buffers who have been additionally compressed with zlib.
57 | * NOTE: The first bit is the level of compression used.
58 | *
59 | * mov [dwBufferFlag], 0
60 | * mov byte ptr [dwBufferFlag], 9
61 | * mov byte ptr [dwBufferFlag+3], 0xFF
62 | */
63 | XOR_ZLIB = XOR | 9
64 | ;
65 | }
66 |
67 | public static byte[] DecryptFileString(PackStreamVerBase pStream, System.IO.Stream pBuffer)
68 | {
69 | if (pStream.GetCompressedHeaderSize() > 0 && pStream.GetEncodedHeaderSize() > 0 && pStream.GetHeaderSize() > 0)
70 | {
71 | byte[] pSrc = new byte[pStream.GetEncodedHeaderSize()];
72 |
73 | if ((ulong)pBuffer.Read(pSrc, 0, (int)pStream.GetEncodedHeaderSize()) == pStream.GetEncodedHeaderSize())
74 | {
75 | return Decrypt(pStream.GetVer(), (uint)pStream.GetEncodedHeaderSize(), (uint)pStream.GetCompressedHeaderSize(), BufferManipulation.AES_ZLIB, pSrc);
76 | }
77 | }
78 |
79 | throw new Exception("ERROR decrypting file list: the size of the list is invalid.");
80 | }
81 |
82 | public static byte[] DecryptFileTable(PackStreamVerBase pStream, System.IO.Stream pBuffer)
83 | {
84 | if (pStream.GetCompressedDataSize() > 0 && pStream.GetEncodedDataSize() > 0 && pStream.GetDataSize() > 0)
85 | {
86 | byte[] pSrc = new byte[pStream.GetEncodedDataSize()];
87 |
88 | if ((ulong)pBuffer.Read(pSrc, 0, (int)pStream.GetEncodedDataSize()) == pStream.GetEncodedDataSize())
89 | {
90 | return Decrypt(pStream.GetVer(), (uint)pStream.GetEncodedDataSize(), (uint)pStream.GetCompressedDataSize(), BufferManipulation.AES_ZLIB, pSrc);
91 | }
92 | }
93 |
94 | throw new Exception("ERROR decrypting file table: the size of the table is invalid.");
95 | }
96 |
97 | public static byte[] DecryptData(PackFileHeaderVerBase pHeader, MemoryMappedFile pData)
98 | {
99 |
100 | if (pHeader.GetCompressedFileSize() > 0 && pHeader.GetEncodedFileSize() > 0 && pHeader.GetFileSize() > 0)
101 | {
102 | using (MemoryMappedViewStream pBuffer = pData.CreateViewStream((long)pHeader.GetOffset(), pHeader.GetEncodedFileSize()))
103 | {
104 | byte[] pSrc = new byte[pHeader.GetEncodedFileSize()];
105 |
106 | if (pBuffer.Read(pSrc, 0, (int)pHeader.GetEncodedFileSize()) == pHeader.GetEncodedFileSize())
107 | {
108 | return Decrypt(pHeader.GetVer(), pHeader.GetEncodedFileSize(), (uint)pHeader.GetCompressedFileSize(), pHeader.GetBufferFlag(), pSrc);
109 | }
110 | }
111 | }
112 |
113 | throw new Exception("ERROR decrypting data file segment: the size of the block is invalid.");
114 | }
115 |
116 | // Decryption Routine: Base64 -> AES -> Zlib
117 | private static byte[] Decrypt(uint uVer, uint uLen, uint uLenCompressed, uint dwBufferFlag, byte[] pSrc)
118 | {
119 | byte[] bits = BitConverter.GetBytes(dwBufferFlag);
120 |
121 | if (!((bits[3] & 1) != 0))
122 | {
123 | byte[] aKey;
124 | byte[] aIV;
125 | // Get the AES Key/IV for transformation
126 | CipherKeys.GetKeyAndIV(uVer, uLenCompressed, out aKey, out aIV);
127 |
128 | // Decode the base64 encoded string
129 | pSrc = Convert.FromBase64String(Encoding.UTF8.GetString(pSrc));
130 |
131 | // Decrypt the AES encrypted block
132 | AESCipher pCipher = new AESCipher(aKey, aIV);
133 | pCipher.TransformBlock(pSrc, 0, uLen, pSrc, 0);
134 | } else
135 | {
136 | // Decrypt the XOR encrypted block
137 | pSrc = EncryptXOR(uVer, pSrc, uLen, uLenCompressed);
138 | }
139 |
140 | if (bits[0] != 0)
141 | {
142 | return ZlibStream.UncompressBuffer(pSrc);
143 | }
144 |
145 | return pSrc;
146 | }
147 |
148 | // Encryption Routine: Zlib -> AES -> Base64
149 | public static byte[] Encrypt(uint uVer, byte[] pSrc, uint dwBufferFlag, out uint uLen, out uint uLenCompressed, out uint uLenEncoded)
150 | {
151 | byte[] bits = BitConverter.GetBytes(dwBufferFlag);
152 |
153 | byte[] pEncrypted;
154 | if (bits[0] != 0)
155 | {
156 | pEncrypted = ZlibStream.CompressBuffer(pSrc);
157 | } else
158 | {
159 | pEncrypted = new byte[pSrc.Length];
160 | Buffer.BlockCopy(pSrc, 0, pEncrypted, 0, pSrc.Length);
161 | }
162 |
163 | uLen = (uint) pSrc.Length;
164 | uLenCompressed = (uint) pEncrypted.Length;
165 |
166 | if (!((bits[3] & 1) != 0))
167 | {
168 | byte[] aKey;
169 | byte[] aIV;
170 | // Get the AES Key/IV for transformation
171 | CipherKeys.GetKeyAndIV(uVer, uLenCompressed, out aKey, out aIV);
172 |
173 | // Perform AES block encryption
174 | AESCipher pCipher = new AESCipher(aKey, aIV);
175 | pCipher.TransformBlock(pEncrypted, 0, uLen, pEncrypted, 0);
176 |
177 | // Encode the encrypted data into a base64 encoded string
178 | pEncrypted = Encoding.UTF8.GetBytes(Convert.ToBase64String(pEncrypted));
179 | } else
180 | {
181 | // Perform XOR block encryption
182 | pEncrypted = EncryptXOR(uVer, pEncrypted, uLen, uLenCompressed);
183 | }
184 |
185 | uLenEncoded = (uint) pEncrypted.Length;
186 |
187 | return pEncrypted;
188 | }
189 |
190 | private static byte[] EncryptXOR(uint uVer, byte[] pSrc, uint uLen, uint uLenCompressed)
191 | {
192 | byte[] aKey;
193 |
194 | CipherKeys.GetXORKey(uVer, out aKey);
195 |
196 | uint uBlock = uLen >> 2;
197 | uint uBlockOffset = 0;
198 | int nKeyOffset = 0;
199 |
200 | if (uBlock != 0)
201 | {
202 | while (uBlockOffset < uBlock)
203 | {
204 | /*
205 | * _begin:
206 | * mov eax, [ebp+pKey]
207 | * mov eax, [eax+edx*4]
208 | * xor [ebx+ecx*4], eax
209 | * inc edx
210 | * inc ecx
211 | * and edx, 1FFh
212 | * cmp ecx, esi
213 | * jb _begin
214 | * _end:
215 | * mov eax, [ebp+uLen]
216 | */
217 |
218 | uint pBlockData = BitConverter.ToUInt32(pSrc, (int)(4 * uBlockOffset)) ^ BitConverter.ToUInt32(aKey, 4 * nKeyOffset);
219 | Buffer.BlockCopy(BitConverter.GetBytes(pBlockData), 0, pSrc, (int)(4 * uBlockOffset), sizeof(uint));
220 |
221 | nKeyOffset = ((ushort)nKeyOffset + 1) & 0x1FF;
222 | uBlockOffset++;
223 | }
224 | }
225 |
226 | uBlock = (uLen & 3);
227 | if (uBlock != 0)
228 | {
229 | int nStart = (int)(4 * uBlockOffset);
230 |
231 | uBlockOffset = 0;
232 | nKeyOffset = 0;
233 |
234 | while (uBlockOffset < uBlock)
235 | {
236 | pSrc[nStart + uBlockOffset++] ^= (byte)(aKey[nKeyOffset]);
237 |
238 | nKeyOffset = ((ushort)nKeyOffset + 1) & 0x7FF;
239 | }
240 | }
241 |
242 | return pSrc;
243 | }
244 | }
245 | }
246 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Window/About.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace Orion.Window
2 | {
3 | partial class About
4 | {
5 | ///
6 | /// Required designer variable.
7 | ///
8 | private System.ComponentModel.IContainer components = null;
9 |
10 | ///
11 | /// Clean up any resources being used.
12 | ///
13 | protected override void Dispose(bool disposing)
14 | {
15 | if (disposing && (components != null))
16 | {
17 | components.Dispose();
18 | }
19 | base.Dispose(disposing);
20 | }
21 |
22 | #region Windows Form Designer generated code
23 |
24 | ///
25 | /// Required method for Designer support - do not modify
26 | /// the contents of this method with the code editor.
27 | ///
28 | private void InitializeComponent()
29 | {
30 | System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(About));
31 | this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
32 | this.logoPictureBox = new System.Windows.Forms.PictureBox();
33 | this.labelProductName = new System.Windows.Forms.Label();
34 | this.labelVersion = new System.Windows.Forms.Label();
35 | this.labelCreator = new System.Windows.Forms.Label();
36 | this.labelSpecialThanks = new System.Windows.Forms.Label();
37 | this.textBoxDescription = new System.Windows.Forms.TextBox();
38 | this.okButton = new System.Windows.Forms.Button();
39 | this.tableLayoutPanel.SuspendLayout();
40 | ((System.ComponentModel.ISupportInitialize)(this.logoPictureBox)).BeginInit();
41 | this.SuspendLayout();
42 | //
43 | // tableLayoutPanel
44 | //
45 | this.tableLayoutPanel.ColumnCount = 2;
46 | this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33F));
47 | this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 67F));
48 | this.tableLayoutPanel.Controls.Add(this.logoPictureBox, 0, 0);
49 | this.tableLayoutPanel.Controls.Add(this.labelProductName, 1, 0);
50 | this.tableLayoutPanel.Controls.Add(this.labelVersion, 1, 1);
51 | this.tableLayoutPanel.Controls.Add(this.labelCreator, 1, 2);
52 | this.tableLayoutPanel.Controls.Add(this.labelSpecialThanks, 1, 3);
53 | this.tableLayoutPanel.Controls.Add(this.textBoxDescription, 1, 4);
54 | this.tableLayoutPanel.Controls.Add(this.okButton, 1, 5);
55 | this.tableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
56 | this.tableLayoutPanel.Location = new System.Drawing.Point(9, 9);
57 | this.tableLayoutPanel.Name = "tableLayoutPanel";
58 | this.tableLayoutPanel.RowCount = 6;
59 | this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
60 | this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
61 | this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
62 | this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
63 | this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
64 | this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
65 | this.tableLayoutPanel.Size = new System.Drawing.Size(417, 265);
66 | this.tableLayoutPanel.TabIndex = 0;
67 | //
68 | // logoPictureBox
69 | //
70 | this.logoPictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
71 | this.logoPictureBox.Image = ((System.Drawing.Image)(resources.GetObject("logoPictureBox.Image")));
72 | this.logoPictureBox.Location = new System.Drawing.Point(3, 3);
73 | this.logoPictureBox.Name = "logoPictureBox";
74 | this.tableLayoutPanel.SetRowSpan(this.logoPictureBox, 6);
75 | this.logoPictureBox.Size = new System.Drawing.Size(131, 259);
76 | this.logoPictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
77 | this.logoPictureBox.TabIndex = 12;
78 | this.logoPictureBox.TabStop = false;
79 | //
80 | // labelProductName
81 | //
82 | this.labelProductName.Dock = System.Windows.Forms.DockStyle.Fill;
83 | this.labelProductName.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
84 | this.labelProductName.ForeColor = System.Drawing.Color.Black;
85 | this.labelProductName.Location = new System.Drawing.Point(143, 0);
86 | this.labelProductName.Margin = new System.Windows.Forms.Padding(6, 0, 3, 0);
87 | this.labelProductName.MaximumSize = new System.Drawing.Size(0, 21);
88 | this.labelProductName.Name = "labelProductName";
89 | this.labelProductName.Size = new System.Drawing.Size(271, 21);
90 | this.labelProductName.TabIndex = 19;
91 | this.labelProductName.Text = "Orion2 Repacker";
92 | this.labelProductName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
93 | //
94 | // labelVersion
95 | //
96 | this.labelVersion.Dock = System.Windows.Forms.DockStyle.Fill;
97 | this.labelVersion.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
98 | this.labelVersion.ForeColor = System.Drawing.Color.Black;
99 | this.labelVersion.Location = new System.Drawing.Point(143, 26);
100 | this.labelVersion.Margin = new System.Windows.Forms.Padding(6, 0, 3, 0);
101 | this.labelVersion.MaximumSize = new System.Drawing.Size(0, 17);
102 | this.labelVersion.Name = "labelVersion";
103 | this.labelVersion.Size = new System.Drawing.Size(271, 17);
104 | this.labelVersion.TabIndex = 0;
105 | this.labelVersion.Text = " Version";
106 | this.labelVersion.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
107 | //
108 | // labelCreator
109 | //
110 | this.labelCreator.Dock = System.Windows.Forms.DockStyle.Fill;
111 | this.labelCreator.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
112 | this.labelCreator.ForeColor = System.Drawing.Color.Black;
113 | this.labelCreator.Location = new System.Drawing.Point(143, 52);
114 | this.labelCreator.Margin = new System.Windows.Forms.Padding(6, 0, 3, 0);
115 | this.labelCreator.MaximumSize = new System.Drawing.Size(0, 17);
116 | this.labelCreator.Name = "labelCreator";
117 | this.labelCreator.Size = new System.Drawing.Size(271, 17);
118 | this.labelCreator.TabIndex = 21;
119 | this.labelCreator.Text = "Created by Eric";
120 | this.labelCreator.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
121 | //
122 | // labelSpecialThanks
123 | //
124 | this.labelSpecialThanks.Dock = System.Windows.Forms.DockStyle.Fill;
125 | this.labelSpecialThanks.ForeColor = System.Drawing.Color.Black;
126 | this.labelSpecialThanks.Location = new System.Drawing.Point(143, 78);
127 | this.labelSpecialThanks.Margin = new System.Windows.Forms.Padding(6, 0, 3, 0);
128 | this.labelSpecialThanks.MaximumSize = new System.Drawing.Size(0, 17);
129 | this.labelSpecialThanks.Name = "labelSpecialThanks";
130 | this.labelSpecialThanks.Size = new System.Drawing.Size(271, 17);
131 | this.labelSpecialThanks.TabIndex = 22;
132 | this.labelSpecialThanks.Text = "Special Thanks";
133 | this.labelSpecialThanks.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
134 | //
135 | // textBoxDescription
136 | //
137 | this.textBoxDescription.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(180)))), ((int)(((byte)(180)))));
138 | this.textBoxDescription.Dock = System.Windows.Forms.DockStyle.Fill;
139 | this.textBoxDescription.ForeColor = System.Drawing.Color.Black;
140 | this.textBoxDescription.Location = new System.Drawing.Point(143, 107);
141 | this.textBoxDescription.Margin = new System.Windows.Forms.Padding(6, 3, 3, 3);
142 | this.textBoxDescription.Multiline = true;
143 | this.textBoxDescription.Name = "textBoxDescription";
144 | this.textBoxDescription.ReadOnly = true;
145 | this.textBoxDescription.ScrollBars = System.Windows.Forms.ScrollBars.Both;
146 | this.textBoxDescription.Size = new System.Drawing.Size(271, 126);
147 | this.textBoxDescription.TabIndex = 23;
148 | this.textBoxDescription.TabStop = false;
149 | this.textBoxDescription.Text = "About Orion2 Repacker";
150 | //
151 | // okButton
152 | //
153 | this.okButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
154 | this.okButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
155 | this.okButton.ForeColor = System.Drawing.Color.Black;
156 | this.okButton.Location = new System.Drawing.Point(339, 239);
157 | this.okButton.Name = "okButton";
158 | this.okButton.Size = new System.Drawing.Size(75, 23);
159 | this.okButton.TabIndex = 24;
160 | this.okButton.Text = "&OK";
161 | //
162 | // About
163 | //
164 | this.AcceptButton = this.okButton;
165 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
166 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
167 | this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(180)))), ((int)(((byte)(180)))));
168 | this.ClientSize = new System.Drawing.Size(435, 283);
169 | this.Controls.Add(this.tableLayoutPanel);
170 | this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
171 | this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
172 | this.MaximizeBox = false;
173 | this.MinimizeBox = false;
174 | this.Name = "About";
175 | this.Padding = new System.Windows.Forms.Padding(9);
176 | this.ShowIcon = false;
177 | this.ShowInTaskbar = false;
178 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
179 | this.Text = "About";
180 | this.tableLayoutPanel.ResumeLayout(false);
181 | this.tableLayoutPanel.PerformLayout();
182 | ((System.ComponentModel.ISupportInitialize)(this.logoPictureBox)).EndInit();
183 | this.ResumeLayout(false);
184 |
185 | }
186 |
187 | #endregion
188 |
189 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel;
190 | private System.Windows.Forms.PictureBox logoPictureBox;
191 | private System.Windows.Forms.Label labelProductName;
192 | private System.Windows.Forms.Label labelVersion;
193 | private System.Windows.Forms.Label labelCreator;
194 | private System.Windows.Forms.TextBox textBoxDescription;
195 | private System.Windows.Forms.Button okButton;
196 | private System.Windows.Forms.Label labelSpecialThanks;
197 | }
198 | }
199 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/zlib/Tree.cs:
--------------------------------------------------------------------------------
1 | // Tree.cs
2 | // ------------------------------------------------------------------
3 | //
4 | // Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
5 | // All rights reserved.
6 | //
7 | // This code module is part of DotNetZip, a zipfile class library.
8 | //
9 | // ------------------------------------------------------------------
10 | //
11 | // This code is licensed under the Microsoft Public License.
12 | // See the file License.txt for the license details.
13 | // More info on: http://dotnetzip.codeplex.com
14 | //
15 | // ------------------------------------------------------------------
16 | //
17 | // last saved (in emacs):
18 | // Time-stamp: <2009-October-28 13:29:50>
19 | //
20 | // ------------------------------------------------------------------
21 | //
22 | // This module defines classes for zlib compression and
23 | // decompression. This code is derived from the jzlib implementation of
24 | // zlib. In keeping with the license for jzlib, the copyright to that
25 | // code is below.
26 | //
27 | // ------------------------------------------------------------------
28 | //
29 | // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
30 | //
31 | // Redistribution and use in source and binary forms, with or without
32 | // modification, are permitted provided that the following conditions are met:
33 | //
34 | // 1. Redistributions of source code must retain the above copyright notice,
35 | // this list of conditions and the following disclaimer.
36 | //
37 | // 2. Redistributions in binary form must reproduce the above copyright
38 | // notice, this list of conditions and the following disclaimer in
39 | // the documentation and/or other materials provided with the distribution.
40 | //
41 | // 3. The names of the authors may not be used to endorse or promote products
42 | // derived from this software without specific prior written permission.
43 | //
44 | // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
45 | // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
46 | // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
47 | // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
48 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
50 | // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
51 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
53 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 | //
55 | // -----------------------------------------------------------------------
56 | //
57 | // This program is based on zlib-1.1.3; credit to authors
58 | // Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
59 | // and contributors of zlib.
60 | //
61 | // -----------------------------------------------------------------------
62 |
63 | using System;
64 |
65 | namespace Orion.Crypto.Stream.zlib
66 | {
67 | sealed class Tree
68 | {
69 | private static readonly int HEAP_SIZE = (2 * InternalConstants.L_CODES + 1);
70 |
71 | // extra bits for each length code
72 | internal static readonly int[] ExtraLengthBits = new int[]
73 | {
74 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
75 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
76 | };
77 |
78 | // extra bits for each distance code
79 | internal static readonly int[] ExtraDistanceBits = new int[]
80 | {
81 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
82 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
83 | };
84 |
85 | // extra bits for each bit length code
86 | internal static readonly int[] extra_blbits = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 };
87 |
88 | internal static readonly sbyte[] bl_order = new sbyte[] { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
89 |
90 |
91 | // The lengths of the bit length codes are sent in order of decreasing
92 | // probability, to avoid transmitting the lengths for unused bit
93 | // length codes.
94 |
95 | internal const int Buf_size = 8 * 2;
96 |
97 | // see definition of array dist_code below
98 | //internal const int DIST_CODE_LEN = 512;
99 |
100 | private static readonly sbyte[] _dist_code = new sbyte[]
101 | {
102 | 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
103 | 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
104 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
105 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
106 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
107 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
108 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
109 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
110 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
111 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
112 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
113 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
114 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
115 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
116 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
117 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
118 | 0, 0, 16, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
119 | 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
120 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
121 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
122 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
123 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
124 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
125 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
126 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
127 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
128 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
129 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
130 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
131 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
132 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
133 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
134 | };
135 |
136 | internal static readonly sbyte[] LengthCode = new sbyte[]
137 | {
138 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11,
139 | 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15,
140 | 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17,
141 | 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19,
142 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
143 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
144 | 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
145 | 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
146 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
147 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
148 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
149 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
150 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
151 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
152 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
153 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
154 | };
155 |
156 |
157 | internal static readonly int[] LengthBase = new int[]
158 | {
159 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28,
160 | 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0
161 | };
162 |
163 |
164 | internal static readonly int[] DistanceBase = new int[]
165 | {
166 | 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
167 | 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
168 | };
169 |
170 |
171 | ///
172 | /// Map from a distance to a distance code.
173 | ///
174 | ///
175 | /// No side effects. _dist_code[256] and _dist_code[257] are never used.
176 | ///
177 | internal static int DistanceCode(int dist)
178 | {
179 | return (dist < 256)
180 | ? _dist_code[dist]
181 | : _dist_code[256 + SharedUtils.URShift(dist, 7)];
182 | }
183 |
184 | internal short[] dyn_tree; // the dynamic tree
185 | internal int max_code; // largest code with non zero frequency
186 | internal StaticTree staticTree; // the corresponding static tree
187 |
188 | // Compute the optimal bit lengths for a tree and update the total bit length
189 | // for the current block.
190 | // IN assertion: the fields freq and dad are set, heap[heap_max] and
191 | // above are the tree nodes sorted by increasing frequency.
192 | // OUT assertions: the field len is set to the optimal bit length, the
193 | // array bl_count contains the frequencies for each bit length.
194 | // The length opt_len is updated; static_len is also updated if stree is
195 | // not null.
196 | internal void gen_bitlen(DeflateManager s)
197 | {
198 | short[] tree = dyn_tree;
199 | short[] stree = staticTree.treeCodes;
200 | int[] extra = staticTree.extraBits;
201 | int base_Renamed = staticTree.extraBase;
202 | int max_length = staticTree.maxLength;
203 | int h; // heap index
204 | int n, m; // iterate over the tree elements
205 | int bits; // bit length
206 | int xbits; // extra bits
207 | short f; // frequency
208 | int overflow = 0; // number of elements with bit length too large
209 |
210 | for (bits = 0; bits <= InternalConstants.MAX_BITS; bits++)
211 | s.bl_count[bits] = 0;
212 |
213 | // In a first pass, compute the optimal bit lengths (which may
214 | // overflow in the case of the bit length tree).
215 | tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap
216 |
217 | for (h = s.heap_max + 1; h < HEAP_SIZE; h++)
218 | {
219 | n = s.heap[h];
220 | bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
221 | if (bits > max_length)
222 | {
223 | bits = max_length; overflow++;
224 | }
225 | tree[n * 2 + 1] = (short)bits;
226 | // We overwrite tree[n*2+1] which is no longer needed
227 |
228 | if (n > max_code)
229 | continue; // not a leaf node
230 |
231 | s.bl_count[bits]++;
232 | xbits = 0;
233 | if (n >= base_Renamed)
234 | xbits = extra[n - base_Renamed];
235 | f = tree[n * 2];
236 | s.opt_len += f * (bits + xbits);
237 | if (stree != null)
238 | s.static_len += f * (stree[n * 2 + 1] + xbits);
239 | }
240 | if (overflow == 0)
241 | return;
242 |
243 | // This happens for example on obj2 and pic of the Calgary corpus
244 | // Find the first bit length which could increase:
245 | do
246 | {
247 | bits = max_length - 1;
248 | while (s.bl_count[bits] == 0)
249 | bits--;
250 | s.bl_count[bits]--; // move one leaf down the tree
251 | s.bl_count[bits + 1] = (short)(s.bl_count[bits + 1] + 2); // move one overflow item as its brother
252 | s.bl_count[max_length]--;
253 | // The brother of the overflow item also moves one step up,
254 | // but this does not affect bl_count[max_length]
255 | overflow -= 2;
256 | }
257 | while (overflow > 0);
258 |
259 | for (bits = max_length; bits != 0; bits--)
260 | {
261 | n = s.bl_count[bits];
262 | while (n != 0)
263 | {
264 | m = s.heap[--h];
265 | if (m > max_code)
266 | continue;
267 | if (tree[m * 2 + 1] != bits)
268 | {
269 | s.opt_len = (int)(s.opt_len + ((long)bits - (long)tree[m * 2 + 1]) * (long)tree[m * 2]);
270 | tree[m * 2 + 1] = (short)bits;
271 | }
272 | n--;
273 | }
274 | }
275 | }
276 |
277 | // Construct one Huffman tree and assigns the code bit strings and lengths.
278 | // Update the total bit length for the current block.
279 | // IN assertion: the field freq is set for all tree elements.
280 | // OUT assertions: the fields len and code are set to the optimal bit length
281 | // and corresponding code. The length opt_len is updated; static_len is
282 | // also updated if stree is not null. The field max_code is set.
283 | internal void build_tree(DeflateManager s)
284 | {
285 | short[] tree = dyn_tree;
286 | short[] stree = staticTree.treeCodes;
287 | int elems = staticTree.elems;
288 | int n, m; // iterate over heap elements
289 | int max_code = -1; // largest code with non zero frequency
290 | int node; // new node being created
291 |
292 | // Construct the initial heap, with least frequent element in
293 | // heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
294 | // heap[0] is not used.
295 | s.heap_len = 0;
296 | s.heap_max = HEAP_SIZE;
297 |
298 | for (n = 0; n < elems; n++)
299 | {
300 | if (tree[n * 2] != 0)
301 | {
302 | s.heap[++s.heap_len] = max_code = n;
303 | s.depth[n] = 0;
304 | }
305 | else
306 | {
307 | tree[n * 2 + 1] = 0;
308 | }
309 | }
310 |
311 | // The pkzip format requires that at least one distance code exists,
312 | // and that at least one bit should be sent even if there is only one
313 | // possible code. So to avoid special checks later on we force at least
314 | // two codes of non zero frequency.
315 | while (s.heap_len < 2)
316 | {
317 | node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
318 | tree[node * 2] = 1;
319 | s.depth[node] = 0;
320 | s.opt_len--;
321 | if (stree != null)
322 | s.static_len -= stree[node * 2 + 1];
323 | // node is 0 or 1 so it does not have extra bits
324 | }
325 | this.max_code = max_code;
326 |
327 | // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
328 | // establish sub-heaps of increasing lengths:
329 |
330 | for (n = s.heap_len / 2; n >= 1; n--)
331 | s.pqdownheap(tree, n);
332 |
333 | // Construct the Huffman tree by repeatedly combining the least two
334 | // frequent nodes.
335 |
336 | node = elems; // next internal node of the tree
337 | do
338 | {
339 | // n = node of least frequency
340 | n = s.heap[1];
341 | s.heap[1] = s.heap[s.heap_len--];
342 | s.pqdownheap(tree, 1);
343 | m = s.heap[1]; // m = node of next least frequency
344 |
345 | s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
346 | s.heap[--s.heap_max] = m;
347 |
348 | // Create a new node father of n and m
349 | tree[node * 2] = unchecked((short)(tree[n * 2] + tree[m * 2]));
350 | s.depth[node] = (sbyte)(System.Math.Max((byte)s.depth[n], (byte)s.depth[m]) + 1);
351 | tree[n * 2 + 1] = tree[m * 2 + 1] = (short)node;
352 |
353 | // and insert the new node in the heap
354 | s.heap[1] = node++;
355 | s.pqdownheap(tree, 1);
356 | }
357 | while (s.heap_len >= 2);
358 |
359 | s.heap[--s.heap_max] = s.heap[1];
360 |
361 | // At this point, the fields freq and dad are set. We can now
362 | // generate the bit lengths.
363 |
364 | gen_bitlen(s);
365 |
366 | // The field len is now set, we can generate the bit codes
367 | gen_codes(tree, max_code, s.bl_count);
368 | }
369 |
370 | // Generate the codes for a given tree and bit counts (which need not be
371 | // optimal).
372 | // IN assertion: the array bl_count contains the bit length statistics for
373 | // the given tree and the field len is set for all tree elements.
374 | // OUT assertion: the field code is set for all tree elements of non
375 | // zero code length.
376 | internal static void gen_codes(short[] tree, int max_code, short[] bl_count)
377 | {
378 | short[] next_code = new short[InternalConstants.MAX_BITS + 1]; // next code value for each bit length
379 | short code = 0; // running code value
380 | int bits; // bit index
381 | int n; // code index
382 |
383 | // The distribution counts are first used to generate the code values
384 | // without bit reversal.
385 | for (bits = 1; bits <= InternalConstants.MAX_BITS; bits++)
386 | unchecked
387 | {
388 | next_code[bits] = code = (short)((code + bl_count[bits - 1]) << 1);
389 | }
390 |
391 | // Check that the bit counts in bl_count are consistent. The last code
392 | // must be all ones.
393 | //Assert (code + bl_count[MAX_BITS]-1 == (1<>= 1; //SharedUtils.URShift(code, 1);
417 | res <<= 1;
418 | }
419 | while (--len > 0);
420 | return res >> 1;
421 | }
422 | }
423 | }
424 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/zlib/Zlib.cs:
--------------------------------------------------------------------------------
1 | // Zlib.cs
2 | // ------------------------------------------------------------------
3 | //
4 | // Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
5 | // All rights reserved.
6 | //
7 | // This code module is part of DotNetZip, a zipfile class library.
8 | //
9 | // ------------------------------------------------------------------
10 | //
11 | // This code is licensed under the Microsoft Public License.
12 | // See the file License.txt for the license details.
13 | // More info on: http://dotnetzip.codeplex.com
14 | //
15 | // ------------------------------------------------------------------
16 | //
17 | // Last Saved: <2011-August-03 19:52:28>
18 | //
19 | // ------------------------------------------------------------------
20 | //
21 | // This module defines classes for ZLIB compression and
22 | // decompression. This code is derived from the jzlib implementation of
23 | // zlib, but significantly modified. The object model is not the same,
24 | // and many of the behaviors are new or different. Nonetheless, in
25 | // keeping with the license for jzlib, the copyright to that code is
26 | // included below.
27 | //
28 | // ------------------------------------------------------------------
29 | //
30 | // The following notice applies to jzlib:
31 | //
32 | // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
33 | //
34 | // Redistribution and use in source and binary forms, with or without
35 | // modification, are permitted provided that the following conditions are met:
36 | //
37 | // 1. Redistributions of source code must retain the above copyright notice,
38 | // this list of conditions and the following disclaimer.
39 | //
40 | // 2. Redistributions in binary form must reproduce the above copyright
41 | // notice, this list of conditions and the following disclaimer in
42 | // the documentation and/or other materials provided with the distribution.
43 | //
44 | // 3. The names of the authors may not be used to endorse or promote products
45 | // derived from this software without specific prior written permission.
46 | //
47 | // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
48 | // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49 | // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
50 | // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
51 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
53 | // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
54 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
56 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 | //
58 | // -----------------------------------------------------------------------
59 | //
60 | // jzlib is based on zlib-1.1.3.
61 | //
62 | // The following notice applies to zlib:
63 | //
64 | // -----------------------------------------------------------------------
65 | //
66 | // Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
67 | //
68 | // The ZLIB software is provided 'as-is', without any express or implied
69 | // warranty. In no event will the authors be held liable for any damages
70 | // arising from the use of this software.
71 | //
72 | // Permission is granted to anyone to use this software for any purpose,
73 | // including commercial applications, and to alter it and redistribute it
74 | // freely, subject to the following restrictions:
75 | //
76 | // 1. The origin of this software must not be misrepresented; you must not
77 | // claim that you wrote the original software. If you use this software
78 | // in a product, an acknowledgment in the product documentation would be
79 | // appreciated but is not required.
80 | // 2. Altered source versions must be plainly marked as such, and must not be
81 | // misrepresented as being the original software.
82 | // 3. This notice may not be removed or altered from any source distribution.
83 | //
84 | // Jean-loup Gailly jloup@gzip.org
85 | // Mark Adler madler@alumni.caltech.edu
86 | //
87 | // -----------------------------------------------------------------------
88 |
89 | using System;
90 | using Interop = System.Runtime.InteropServices;
91 |
92 | namespace Orion.Crypto.Stream.zlib
93 | {
94 | ///
95 | /// Describes how to flush the current deflate operation.
96 | ///
97 | ///
98 | /// The different FlushType values are useful when using a Deflate in a streaming application.
99 | ///
100 | public enum FlushType
101 | {
102 | /// No flush at all.
103 | None = 0,
104 |
105 | /// Closes the current block, but doesn't flush it to
106 | /// the output. Used internally only in hypothetical
107 | /// scenarios. This was supposed to be removed by Zlib, but it is
108 | /// still in use in some edge cases.
109 | ///
110 | Partial,
111 |
112 | ///
113 | /// Use this during compression to specify that all pending output should be
114 | /// flushed to the output buffer and the output should be aligned on a byte
115 | /// boundary. You might use this in a streaming communication scenario, so that
116 | /// the decompressor can get all input data available so far. When using this
117 | /// with a ZlibCodec, AvailableBytesIn will be zero after the call if
118 | /// enough output space has been provided before the call. Flushing will
119 | /// degrade compression and so it should be used only when necessary.
120 | ///
121 | Sync,
122 |
123 | ///
124 | /// Use this during compression to specify that all output should be flushed, as
125 | /// with FlushType.Sync, but also, the compression state should be reset
126 | /// so that decompression can restart from this point if previous compressed
127 | /// data has been damaged or if random access is desired. Using
128 | /// FlushType.Full too often can significantly degrade the compression.
129 | ///
130 | Full,
131 |
132 | /// Signals the end of the compression/decompression stream.
133 | Finish,
134 | }
135 |
136 |
137 | ///
138 | /// The compression level to be used when using a DeflateStream or ZlibStream with CompressionMode.Compress.
139 | ///
140 | public enum CompressionLevel
141 | {
142 | ///
143 | /// None means that the data will be simply stored, with no change at all.
144 | /// If you are producing ZIPs for use on Mac OSX, be aware that archives produced with CompressionLevel.None
145 | /// cannot be opened with the default zip reader. Use a different CompressionLevel.
146 | ///
147 | None = 0,
148 | ///
149 | /// Same as None.
150 | ///
151 | Level0 = 0,
152 |
153 | ///
154 | /// The fastest but least effective compression.
155 | ///
156 | BestSpeed = 1,
157 |
158 | ///
159 | /// A synonym for BestSpeed.
160 | ///
161 | Level1 = 1,
162 |
163 | ///
164 | /// A little slower, but better, than level 1.
165 | ///
166 | Level2 = 2,
167 |
168 | ///
169 | /// A little slower, but better, than level 2.
170 | ///
171 | Level3 = 3,
172 |
173 | ///
174 | /// A little slower, but better, than level 3.
175 | ///
176 | Level4 = 4,
177 |
178 | ///
179 | /// A little slower than level 4, but with better compression.
180 | ///
181 | Level5 = 5,
182 |
183 | ///
184 | /// The default compression level, with a good balance of speed and compression efficiency.
185 | ///
186 | Default = 6,
187 | ///
188 | /// A synonym for Default.
189 | ///
190 | Level6 = 6,
191 |
192 | ///
193 | /// Pretty good compression!
194 | ///
195 | Level7 = 7,
196 |
197 | ///
198 | /// Better compression than Level7!
199 | ///
200 | Level8 = 8,
201 |
202 | ///
203 | /// The "best" compression, where best means greatest reduction in size of the input data stream.
204 | /// This is also the slowest compression.
205 | ///
206 | BestCompression = 9,
207 |
208 | ///
209 | /// A synonym for BestCompression.
210 | ///
211 | Level9 = 9,
212 | }
213 |
214 | ///
215 | /// Describes options for how the compression algorithm is executed. Different strategies
216 | /// work better on different sorts of data. The strategy parameter can affect the compression
217 | /// ratio and the speed of compression but not the correctness of the compresssion.
218 | ///
219 | public enum CompressionStrategy
220 | {
221 | ///
222 | /// The default strategy is probably the best for normal data.
223 | ///
224 | Default = 0,
225 |
226 | ///
227 | /// The Filtered strategy is intended to be used most effectively with data produced by a
228 | /// filter or predictor. By this definition, filtered data consists mostly of small
229 | /// values with a somewhat random distribution. In this case, the compression algorithm
230 | /// is tuned to compress them better. The effect of Filtered is to force more Huffman
231 | /// coding and less string matching; it is a half-step between Default and HuffmanOnly.
232 | ///
233 | Filtered = 1,
234 |
235 | ///
236 | /// Using HuffmanOnly will force the compressor to do Huffman encoding only, with no
237 | /// string matching.
238 | ///
239 | HuffmanOnly = 2,
240 | }
241 |
242 |
243 | ///
244 | /// An enum to specify the direction of transcoding - whether to compress or decompress.
245 | ///
246 | public enum CompressionMode
247 | {
248 | ///
249 | /// Used to specify that the stream should compress the data.
250 | ///
251 | Compress = 0,
252 | ///
253 | /// Used to specify that the stream should decompress the data.
254 | ///
255 | Decompress = 1,
256 | }
257 |
258 |
259 | ///
260 | /// A general purpose exception class for exceptions in the Zlib library.
261 | ///
262 | [Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000E")]
263 | public class ZlibException : System.Exception
264 | {
265 | ///
266 | /// The ZlibException class captures exception information generated
267 | /// by the Zlib library.
268 | ///
269 | public ZlibException()
270 | : base()
271 | {
272 | }
273 |
274 | ///
275 | /// This ctor collects a message attached to the exception.
276 | ///
277 | /// the message for the exception.
278 | public ZlibException(System.String s)
279 | : base(s)
280 | {
281 | }
282 | }
283 |
284 |
285 | internal class SharedUtils
286 | {
287 | ///
288 | /// Performs an unsigned bitwise right shift with the specified number
289 | ///
290 | /// Number to operate on
291 | /// Ammount of bits to shift
292 | /// The resulting number from the shift operation
293 | public static int URShift(int number, int bits)
294 | {
295 | return (int)((uint)number >> bits);
296 | }
297 |
298 | #if NOT
299 | ///
300 | /// Performs an unsigned bitwise right shift with the specified number
301 | ///
302 | /// Number to operate on
303 | /// Ammount of bits to shift
304 | /// The resulting number from the shift operation
305 | public static long URShift(long number, int bits)
306 | {
307 | return (long) ((UInt64)number >> bits);
308 | }
309 | #endif
310 |
311 | ///
312 | /// Reads a number of characters from the current source TextReader and writes
313 | /// the data to the target array at the specified index.
314 | ///
315 | ///
316 | /// The source TextReader to read from
317 | /// Contains the array of characteres read from the source TextReader.
318 | /// The starting index of the target array.
319 | /// The maximum number of characters to read from the source TextReader.
320 | ///
321 | ///
322 | /// The number of characters read. The number will be less than or equal to
323 | /// count depending on the data available in the source TextReader. Returns -1
324 | /// if the end of the stream is reached.
325 | ///
326 | public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, byte[] target, int start, int count)
327 | {
328 | // Returns 0 bytes if not enough space in target
329 | if (target.Length == 0) return 0;
330 |
331 | char[] charArray = new char[target.Length];
332 | int bytesRead = sourceTextReader.Read(charArray, start, count);
333 |
334 | // Returns -1 if EOF
335 | if (bytesRead == 0) return -1;
336 |
337 | for (int index = start; index < start + bytesRead; index++)
338 | target[index] = (byte)charArray[index];
339 |
340 | return bytesRead;
341 | }
342 |
343 |
344 | internal static byte[] ToByteArray(System.String sourceString)
345 | {
346 | return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
347 | }
348 |
349 |
350 | internal static char[] ToCharArray(byte[] byteArray)
351 | {
352 | return System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
353 | }
354 | }
355 |
356 | internal static class InternalConstants
357 | {
358 | internal static readonly int MAX_BITS = 15;
359 | internal static readonly int BL_CODES = 19;
360 | internal static readonly int D_CODES = 30;
361 | internal static readonly int LITERALS = 256;
362 | internal static readonly int LENGTH_CODES = 29;
363 | internal static readonly int L_CODES = (LITERALS + 1 + LENGTH_CODES);
364 |
365 | // Bit length codes must not exceed MAX_BL_BITS bits
366 | internal static readonly int MAX_BL_BITS = 7;
367 |
368 | // repeat previous bit length 3-6 times (2 bits of repeat count)
369 | internal static readonly int REP_3_6 = 16;
370 |
371 | // repeat a zero length 3-10 times (3 bits of repeat count)
372 | internal static readonly int REPZ_3_10 = 17;
373 |
374 | // repeat a zero length 11-138 times (7 bits of repeat count)
375 | internal static readonly int REPZ_11_138 = 18;
376 |
377 | }
378 |
379 | internal sealed class StaticTree
380 | {
381 | internal static readonly short[] lengthAndLiteralsTreeCodes = new short[] {
382 | 12, 8, 140, 8, 76, 8, 204, 8, 44, 8, 172, 8, 108, 8, 236, 8,
383 | 28, 8, 156, 8, 92, 8, 220, 8, 60, 8, 188, 8, 124, 8, 252, 8,
384 | 2, 8, 130, 8, 66, 8, 194, 8, 34, 8, 162, 8, 98, 8, 226, 8,
385 | 18, 8, 146, 8, 82, 8, 210, 8, 50, 8, 178, 8, 114, 8, 242, 8,
386 | 10, 8, 138, 8, 74, 8, 202, 8, 42, 8, 170, 8, 106, 8, 234, 8,
387 | 26, 8, 154, 8, 90, 8, 218, 8, 58, 8, 186, 8, 122, 8, 250, 8,
388 | 6, 8, 134, 8, 70, 8, 198, 8, 38, 8, 166, 8, 102, 8, 230, 8,
389 | 22, 8, 150, 8, 86, 8, 214, 8, 54, 8, 182, 8, 118, 8, 246, 8,
390 | 14, 8, 142, 8, 78, 8, 206, 8, 46, 8, 174, 8, 110, 8, 238, 8,
391 | 30, 8, 158, 8, 94, 8, 222, 8, 62, 8, 190, 8, 126, 8, 254, 8,
392 | 1, 8, 129, 8, 65, 8, 193, 8, 33, 8, 161, 8, 97, 8, 225, 8,
393 | 17, 8, 145, 8, 81, 8, 209, 8, 49, 8, 177, 8, 113, 8, 241, 8,
394 | 9, 8, 137, 8, 73, 8, 201, 8, 41, 8, 169, 8, 105, 8, 233, 8,
395 | 25, 8, 153, 8, 89, 8, 217, 8, 57, 8, 185, 8, 121, 8, 249, 8,
396 | 5, 8, 133, 8, 69, 8, 197, 8, 37, 8, 165, 8, 101, 8, 229, 8,
397 | 21, 8, 149, 8, 85, 8, 213, 8, 53, 8, 181, 8, 117, 8, 245, 8,
398 | 13, 8, 141, 8, 77, 8, 205, 8, 45, 8, 173, 8, 109, 8, 237, 8,
399 | 29, 8, 157, 8, 93, 8, 221, 8, 61, 8, 189, 8, 125, 8, 253, 8,
400 | 19, 9, 275, 9, 147, 9, 403, 9, 83, 9, 339, 9, 211, 9, 467, 9,
401 | 51, 9, 307, 9, 179, 9, 435, 9, 115, 9, 371, 9, 243, 9, 499, 9,
402 | 11, 9, 267, 9, 139, 9, 395, 9, 75, 9, 331, 9, 203, 9, 459, 9,
403 | 43, 9, 299, 9, 171, 9, 427, 9, 107, 9, 363, 9, 235, 9, 491, 9,
404 | 27, 9, 283, 9, 155, 9, 411, 9, 91, 9, 347, 9, 219, 9, 475, 9,
405 | 59, 9, 315, 9, 187, 9, 443, 9, 123, 9, 379, 9, 251, 9, 507, 9,
406 | 7, 9, 263, 9, 135, 9, 391, 9, 71, 9, 327, 9, 199, 9, 455, 9,
407 | 39, 9, 295, 9, 167, 9, 423, 9, 103, 9, 359, 9, 231, 9, 487, 9,
408 | 23, 9, 279, 9, 151, 9, 407, 9, 87, 9, 343, 9, 215, 9, 471, 9,
409 | 55, 9, 311, 9, 183, 9, 439, 9, 119, 9, 375, 9, 247, 9, 503, 9,
410 | 15, 9, 271, 9, 143, 9, 399, 9, 79, 9, 335, 9, 207, 9, 463, 9,
411 | 47, 9, 303, 9, 175, 9, 431, 9, 111, 9, 367, 9, 239, 9, 495, 9,
412 | 31, 9, 287, 9, 159, 9, 415, 9, 95, 9, 351, 9, 223, 9, 479, 9,
413 | 63, 9, 319, 9, 191, 9, 447, 9, 127, 9, 383, 9, 255, 9, 511, 9,
414 | 0, 7, 64, 7, 32, 7, 96, 7, 16, 7, 80, 7, 48, 7, 112, 7,
415 | 8, 7, 72, 7, 40, 7, 104, 7, 24, 7, 88, 7, 56, 7, 120, 7,
416 | 4, 7, 68, 7, 36, 7, 100, 7, 20, 7, 84, 7, 52, 7, 116, 7,
417 | 3, 8, 131, 8, 67, 8, 195, 8, 35, 8, 163, 8, 99, 8, 227, 8
418 | };
419 |
420 | internal static readonly short[] distTreeCodes = new short[] {
421 | 0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5,
422 | 2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, 30, 5,
423 | 1, 5, 17, 5, 9, 5, 25, 5, 5, 5, 21, 5, 13, 5, 29, 5,
424 | 3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 };
425 |
426 | internal static readonly StaticTree Literals;
427 | internal static readonly StaticTree Distances;
428 | internal static readonly StaticTree BitLengths;
429 |
430 | internal short[] treeCodes; // static tree or null
431 | internal int[] extraBits; // extra bits for each code or null
432 | internal int extraBase; // base index for extra_bits
433 | internal int elems; // max number of elements in the tree
434 | internal int maxLength; // max bit length for the codes
435 |
436 | private StaticTree(short[] treeCodes, int[] extraBits, int extraBase, int elems, int maxLength)
437 | {
438 | this.treeCodes = treeCodes;
439 | this.extraBits = extraBits;
440 | this.extraBase = extraBase;
441 | this.elems = elems;
442 | this.maxLength = maxLength;
443 | }
444 | static StaticTree()
445 | {
446 | Literals = new StaticTree(lengthAndLiteralsTreeCodes, Tree.ExtraLengthBits, InternalConstants.LITERALS + 1, InternalConstants.L_CODES, InternalConstants.MAX_BITS);
447 | Distances = new StaticTree(distTreeCodes, Tree.ExtraDistanceBits, 0, InternalConstants.D_CODES, InternalConstants.MAX_BITS);
448 | BitLengths = new StaticTree(null, Tree.extra_blbits, 0, InternalConstants.BL_CODES, InternalConstants.MAX_BL_BITS);
449 | }
450 | }
451 |
452 |
453 |
454 | ///
455 | /// Computes an Adler-32 checksum.
456 | ///
457 | ///
458 | /// The Adler checksum is similar to a CRC checksum, but faster to compute, though less
459 | /// reliable. It is used in producing RFC1950 compressed streams. The Adler checksum
460 | /// is a required part of the "ZLIB" standard. Applications will almost never need to
461 | /// use this class directly.
462 | ///
463 | ///
464 | ///
465 | public sealed class Adler
466 | {
467 | // largest prime smaller than 65536
468 | private static readonly uint BASE = 65521;
469 | // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
470 | private static readonly int NMAX = 5552;
471 |
472 |
473 | #pragma warning disable 3001
474 | #pragma warning disable 3002
475 |
476 | ///
477 | /// Calculates the Adler32 checksum.
478 | ///
479 | ///
480 | ///
481 | /// This is used within ZLIB. You probably don't need to use this directly.
482 | ///
483 | ///
484 | ///
485 | /// To compute an Adler32 checksum on a byte array:
486 | ///
487 | /// var adler = Adler.Adler32(0, null, 0, 0);
488 | /// adler = Adler.Adler32(adler, buffer, index, length);
489 | ///
490 | ///
491 | public static uint Adler32(uint adler, byte[] buf, int index, int len)
492 | {
493 | if (buf == null)
494 | return 1;
495 |
496 | uint s1 = (uint)(adler & 0xffff);
497 | uint s2 = (uint)((adler >> 16) & 0xffff);
498 |
499 | while (len > 0)
500 | {
501 | int k = len < NMAX ? len : NMAX;
502 | len -= k;
503 | while (k >= 16)
504 | {
505 | //s1 += (buf[index++] & 0xff); s2 += s1;
506 | s1 += buf[index++]; s2 += s1;
507 | s1 += buf[index++]; s2 += s1;
508 | s1 += buf[index++]; s2 += s1;
509 | s1 += buf[index++]; s2 += s1;
510 | s1 += buf[index++]; s2 += s1;
511 | s1 += buf[index++]; s2 += s1;
512 | s1 += buf[index++]; s2 += s1;
513 | s1 += buf[index++]; s2 += s1;
514 | s1 += buf[index++]; s2 += s1;
515 | s1 += buf[index++]; s2 += s1;
516 | s1 += buf[index++]; s2 += s1;
517 | s1 += buf[index++]; s2 += s1;
518 | s1 += buf[index++]; s2 += s1;
519 | s1 += buf[index++]; s2 += s1;
520 | s1 += buf[index++]; s2 += s1;
521 | s1 += buf[index++]; s2 += s1;
522 | k -= 16;
523 | }
524 | if (k != 0)
525 | {
526 | do
527 | {
528 | s1 += buf[index++];
529 | s2 += s1;
530 | }
531 | while (--k != 0);
532 | }
533 | s1 %= BASE;
534 | s2 %= BASE;
535 | }
536 | return (uint)((s2 << 16) | s1);
537 | }
538 | #pragma warning restore 3001
539 | #pragma warning restore 3002
540 |
541 | }
542 | }
543 |
--------------------------------------------------------------------------------
/Orion2-Repacker/Crypto/Stream/zlib/InfTree.cs:
--------------------------------------------------------------------------------
1 | // Inftree.cs
2 | // ------------------------------------------------------------------
3 | //
4 | // Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
5 | // All rights reserved.
6 | //
7 | // This code module is part of DotNetZip, a zipfile class library.
8 | //
9 | // ------------------------------------------------------------------
10 | //
11 | // This code is licensed under the Microsoft Public License.
12 | // See the file License.txt for the license details.
13 | // More info on: http://dotnetzip.codeplex.com
14 | //
15 | // ------------------------------------------------------------------
16 | //
17 | // last saved (in emacs):
18 | // Time-stamp: <2009-October-28 12:43:54>
19 | //
20 | // ------------------------------------------------------------------
21 | //
22 | // This module defines classes used in decompression. This code is derived
23 | // from the jzlib implementation of zlib. In keeping with the license for jzlib,
24 | // the copyright to that code is below.
25 | //
26 | // ------------------------------------------------------------------
27 | //
28 | // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
29 | //
30 | // Redistribution and use in source and binary forms, with or without
31 | // modification, are permitted provided that the following conditions are met:
32 | //
33 | // 1. Redistributions of source code must retain the above copyright notice,
34 | // this list of conditions and the following disclaimer.
35 | //
36 | // 2. Redistributions in binary form must reproduce the above copyright
37 | // notice, this list of conditions and the following disclaimer in
38 | // the documentation and/or other materials provided with the distribution.
39 | //
40 | // 3. The names of the authors may not be used to endorse or promote products
41 | // derived from this software without specific prior written permission.
42 | //
43 | // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
44 | // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
45 | // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
46 | // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
47 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
49 | // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
50 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
52 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 | //
54 | // -----------------------------------------------------------------------
55 | //
56 | // This program is based on zlib-1.1.3; credit to authors
57 | // Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
58 | // and contributors of zlib.
59 | //
60 | // -----------------------------------------------------------------------
61 |
62 | using System;
63 |
64 | namespace Orion.Crypto.Stream.zlib
65 | {
66 | sealed class InfTree
67 | {
68 |
69 | private const int MANY = 1440;
70 |
71 | private const int Z_OK = 0;
72 | private const int Z_STREAM_END = 1;
73 | private const int Z_NEED_DICT = 2;
74 | private const int Z_ERRNO = -1;
75 | private const int Z_STREAM_ERROR = -2;
76 | private const int Z_DATA_ERROR = -3;
77 | private const int Z_MEM_ERROR = -4;
78 | private const int Z_BUF_ERROR = -5;
79 | private const int Z_VERSION_ERROR = -6;
80 |
81 | internal const int fixed_bl = 9;
82 | internal const int fixed_bd = 5;
83 |
84 | //UPGRADE_NOTE: Final was removed from the declaration of 'fixed_tl'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
85 | internal static readonly int[] fixed_tl = new int[]{96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186,
86 | 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8,
87 | 14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255};
88 | //UPGRADE_NOTE: Final was removed from the declaration of 'fixed_td'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
89 | internal static readonly int[] fixed_td = new int[] { 80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5, 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5, 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577 };
90 |
91 | // Tables for deflate from PKZIP's appnote.txt.
92 | //UPGRADE_NOTE: Final was removed from the declaration of 'cplens'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
93 | internal static readonly int[] cplens = new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 };
94 |
95 | // see note #13 above about 258
96 | //UPGRADE_NOTE: Final was removed from the declaration of 'cplext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
97 | internal static readonly int[] cplext = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112 };
98 |
99 | //UPGRADE_NOTE: Final was removed from the declaration of 'cpdist'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
100 | internal static readonly int[] cpdist = new int[] { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 };
101 |
102 | //UPGRADE_NOTE: Final was removed from the declaration of 'cpdext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
103 | internal static readonly int[] cpdext = new int[] { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
104 |
105 | // If BMAX needs to be larger than 16, then h and x[] should be uLong.
106 | internal const int BMAX = 15; // maximum bit length of any code
107 |
108 | internal int[] hn = null; // hufts used in space
109 | internal int[] v = null; // work area for huft_build
110 | internal int[] c = null; // bit length count table
111 | internal int[] r = null; // table entry for structure assignment
112 | internal int[] u = null; // table stack
113 | internal int[] x = null; // bit offsets, then code stack
114 |
115 | private int huft_build(int[] b, int bindex, int n, int s, int[] d, int[] e, int[] t, int[] m, int[] hp, int[] hn, int[] v)
116 | {
117 | // Given a list of code lengths and a maximum table size, make a set of
118 | // tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
119 | // if the given code set is incomplete (the tables are still built in this
120 | // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
121 | // lengths), or Z_MEM_ERROR if not enough memory.
122 |
123 | int a; // counter for codes of length k
124 | int f; // i repeats in table every f entries
125 | int g; // maximum code length
126 | int h; // table level
127 | int i; // counter, current code
128 | int j; // counter
129 | int k; // number of bits in current code
130 | int l; // bits per table (returned in m)
131 | int mask; // (1 << w) - 1, to avoid cc -O bug on HP
132 | int p; // pointer into c[], b[], or v[]
133 | int q; // points to current table
134 | int w; // bits before this table == (l * h)
135 | int xp; // pointer into x
136 | int y; // number of dummy codes added
137 | int z; // number of entries in current table
138 |
139 | // Generate counts for each bit length
140 |
141 | p = 0; i = n;
142 | do
143 | {
144 | c[b[bindex + p]]++; p++; i--; // assume all entries <= BMAX
145 | }
146 | while (i != 0);
147 |
148 | if (c[0] == n)
149 | {
150 | // null input--all zero length codes
151 | t[0] = -1;
152 | m[0] = 0;
153 | return Z_OK;
154 | }
155 |
156 | // Find minimum and maximum length, bound *m by those
157 | l = m[0];
158 | for (j = 1; j <= BMAX; j++)
159 | if (c[j] != 0)
160 | break;
161 | k = j; // minimum code length
162 | if (l < j)
163 | {
164 | l = j;
165 | }
166 | for (i = BMAX; i != 0; i--)
167 | {
168 | if (c[i] != 0)
169 | break;
170 | }
171 | g = i; // maximum code length
172 | if (l > i)
173 | {
174 | l = i;
175 | }
176 | m[0] = l;
177 |
178 | // Adjust last length count to fill out codes, if needed
179 | for (y = 1 << j; j < i; j++, y <<= 1)
180 | {
181 | if ((y -= c[j]) < 0)
182 | {
183 | return Z_DATA_ERROR;
184 | }
185 | }
186 | if ((y -= c[i]) < 0)
187 | {
188 | return Z_DATA_ERROR;
189 | }
190 | c[i] += y;
191 |
192 | // Generate starting offsets into the value table for each length
193 | x[1] = j = 0;
194 | p = 1; xp = 2;
195 | while (--i != 0)
196 | {
197 | // note that i == g from above
198 | x[xp] = (j += c[p]);
199 | xp++;
200 | p++;
201 | }
202 |
203 | // Make a table of values in order of bit lengths
204 | i = 0; p = 0;
205 | do
206 | {
207 | if ((j = b[bindex + p]) != 0)
208 | {
209 | v[x[j]++] = i;
210 | }
211 | p++;
212 | }
213 | while (++i < n);
214 | n = x[g]; // set n to length of v
215 |
216 | // Generate the Huffman codes and for each, make the table entries
217 | x[0] = i = 0; // first Huffman code is zero
218 | p = 0; // grab values in bit order
219 | h = -1; // no tables yet--level -1
220 | w = -l; // bits decoded == (l * h)
221 | u[0] = 0; // just to keep compilers happy
222 | q = 0; // ditto
223 | z = 0; // ditto
224 |
225 | // go through the bit lengths (k already is bits in shortest code)
226 | for (; k <= g; k++)
227 | {
228 | a = c[k];
229 | while (a-- != 0)
230 | {
231 | // here i is the Huffman code of length k bits for value *p
232 | // make tables up to required level
233 | while (k > w + l)
234 | {
235 | h++;
236 | w += l; // previous table always l bits
237 | // compute minimum size table less than or equal to l bits
238 | z = g - w;
239 | z = (z > l) ? l : z; // table size upper limit
240 | if ((f = 1 << (j = k - w)) > a + 1)
241 | {
242 | // try a k-w bit table
243 | // too few codes for k-w bit table
244 | f -= (a + 1); // deduct codes from patterns left
245 | xp = k;
246 | if (j < z)
247 | {
248 | while (++j < z)
249 | {
250 | // try smaller tables up to z bits
251 | if ((f <<= 1) <= c[++xp])
252 | break; // enough codes to use up j bits
253 | f -= c[xp]; // else deduct codes from patterns
254 | }
255 | }
256 | }
257 | z = 1 << j; // table entries for j-bit table
258 |
259 | // allocate new table
260 | if (hn[0] + z > MANY)
261 | {
262 | // (note: doesn't matter for fixed)
263 | return Z_DATA_ERROR; // overflow of MANY
264 | }
265 | u[h] = q = hn[0]; // DEBUG
266 | hn[0] += z;
267 |
268 | // connect to last table, if there is one
269 | if (h != 0)
270 | {
271 | x[h] = i; // save pattern for backing up
272 | r[0] = (sbyte)j; // bits in this table
273 | r[1] = (sbyte)l; // bits to dump before this table
274 | j = SharedUtils.URShift(i, (w - l));
275 | r[2] = (int)(q - u[h - 1] - j); // offset to this table
276 | Array.Copy(r, 0, hp, (u[h - 1] + j) * 3, 3); // connect to last table
277 | }
278 | else
279 | {
280 | t[0] = q; // first table is returned result
281 | }
282 | }
283 |
284 | // set up table entry in r
285 | r[1] = (sbyte)(k - w);
286 | if (p >= n)
287 | {
288 | r[0] = 128 + 64; // out of values--invalid code
289 | }
290 | else if (v[p] < s)
291 | {
292 | r[0] = (sbyte)(v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block
293 | r[2] = v[p++]; // simple code is just the value
294 | }
295 | else
296 | {
297 | r[0] = (sbyte)(e[v[p] - s] + 16 + 64); // non-simple--look up in lists
298 | r[2] = d[v[p++] - s];
299 | }
300 |
301 | // fill code-like entries with r
302 | f = 1 << (k - w);
303 | for (j = SharedUtils.URShift(i, w); j < z; j += f)
304 | {
305 | Array.Copy(r, 0, hp, (q + j) * 3, 3);
306 | }
307 |
308 | // backwards increment the k-bit code i
309 | for (j = 1 << (k - 1); (i & j) != 0; j = SharedUtils.URShift(j, 1))
310 | {
311 | i ^= j;
312 | }
313 | i ^= j;
314 |
315 | // backup over finished tables
316 | mask = (1 << w) - 1; // needed on HP, cc -O bug
317 | while ((i & mask) != x[h])
318 | {
319 | h--; // don't need to update q
320 | w -= l;
321 | mask = (1 << w) - 1;
322 | }
323 | }
324 | }
325 | // Return Z_BUF_ERROR if we were given an incomplete table
326 | return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
327 | }
328 |
329 | internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z)
330 | {
331 | int result;
332 | initWorkArea(19);
333 | hn[0] = 0;
334 | result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
335 |
336 | if (result == Z_DATA_ERROR)
337 | {
338 | z.Message = "oversubscribed dynamic bit lengths tree";
339 | }
340 | else if (result == Z_BUF_ERROR || bb[0] == 0)
341 | {
342 | z.Message = "incomplete dynamic bit lengths tree";
343 | result = Z_DATA_ERROR;
344 | }
345 | return result;
346 | }
347 |
348 | internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
349 | {
350 | int result;
351 |
352 | // build literal/length tree
353 | initWorkArea(288);
354 | hn[0] = 0;
355 | result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
356 | if (result != Z_OK || bl[0] == 0)
357 | {
358 | if (result == Z_DATA_ERROR)
359 | {
360 | z.Message = "oversubscribed literal/length tree";
361 | }
362 | else if (result != Z_MEM_ERROR)
363 | {
364 | z.Message = "incomplete literal/length tree";
365 | result = Z_DATA_ERROR;
366 | }
367 | return result;
368 | }
369 |
370 | // build distance tree
371 | initWorkArea(288);
372 | result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
373 |
374 | if (result != Z_OK || (bd[0] == 0 && nl > 257))
375 | {
376 | if (result == Z_DATA_ERROR)
377 | {
378 | z.Message = "oversubscribed distance tree";
379 | }
380 | else if (result == Z_BUF_ERROR)
381 | {
382 | z.Message = "incomplete distance tree";
383 | result = Z_DATA_ERROR;
384 | }
385 | else if (result != Z_MEM_ERROR)
386 | {
387 | z.Message = "empty distance tree with lengths";
388 | result = Z_DATA_ERROR;
389 | }
390 | return result;
391 | }
392 |
393 | return Z_OK;
394 | }
395 |
396 | internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
397 | {
398 | bl[0] = fixed_bl;
399 | bd[0] = fixed_bd;
400 | tl[0] = fixed_tl;
401 | td[0] = fixed_td;
402 | return Z_OK;
403 | }
404 |
405 | private void initWorkArea(int vsize)
406 | {
407 | if (hn == null)
408 | {
409 | hn = new int[1];
410 | v = new int[vsize];
411 | c = new int[BMAX + 1];
412 | r = new int[3];
413 | u = new int[BMAX];
414 | x = new int[BMAX + 1];
415 | }
416 | else
417 | {
418 | if (v.Length < vsize)
419 | {
420 | v = new int[vsize];
421 | }
422 | Array.Clear(v, 0, vsize);
423 | Array.Clear(c, 0, BMAX + 1);
424 | r[0] = 0; r[1] = 0; r[2] = 0;
425 | // for(int i=0; i
19 | //
20 | // ------------------------------------------------------------------
21 | //
22 | // This module defines the ZlibBaseStream class, which is an intnernal
23 | // base class for DeflateStream, ZlibStream and GZipStream.
24 | //
25 | // ------------------------------------------------------------------
26 |
27 | using System;
28 | using System.IO;
29 |
30 | namespace Orion.Crypto.Stream.zlib
31 | {
32 | internal enum ZlibStreamFlavor { ZLIB = 1950, DEFLATE = 1951, GZIP = 1952 }
33 |
34 | internal class ZlibBaseStream : System.IO.Stream
35 | {
36 | protected internal ZlibCodec _z = null; // deferred init... new ZlibCodec();
37 |
38 | protected internal StreamMode _streamMode = StreamMode.Undefined;
39 | protected internal FlushType _flushMode;
40 | protected internal ZlibStreamFlavor _flavor;
41 | protected internal CompressionMode _compressionMode;
42 | protected internal CompressionLevel _level;
43 | protected internal bool _leaveOpen;
44 | protected internal byte[] _workingBuffer;
45 | protected internal int _bufferSize = ZlibConstants.WorkingBufferSizeDefault;
46 | protected internal byte[] _buf1 = new byte[1];
47 |
48 | protected internal System.IO.Stream _stream;
49 | protected internal CompressionStrategy Strategy = CompressionStrategy.Default;
50 |
51 | // workitem 7159
52 | CRC32 crc;
53 | protected internal int _gzipHeaderByteCount;
54 |
55 | internal int Crc32 { get { if (crc == null) return 0; return crc.Crc32Result; } }
56 |
57 | public ZlibBaseStream(System.IO.Stream stream,
58 | CompressionMode compressionMode,
59 | CompressionLevel level,
60 | ZlibStreamFlavor flavor,
61 | bool leaveOpen)
62 | : base()
63 | {
64 | this._flushMode = FlushType.None;
65 | //this._workingBuffer = new byte[WORKING_BUFFER_SIZE_DEFAULT];
66 | this._stream = stream;
67 | this._leaveOpen = leaveOpen;
68 | this._compressionMode = compressionMode;
69 | this._flavor = flavor;
70 | this._level = level;
71 | // workitem 7159
72 | if (flavor == ZlibStreamFlavor.GZIP)
73 | {
74 | this.crc = new CRC32();
75 | }
76 | }
77 |
78 |
79 | protected internal bool _wantCompress
80 | {
81 | get
82 | {
83 | return (this._compressionMode == CompressionMode.Compress);
84 | }
85 | }
86 |
87 | private ZlibCodec z
88 | {
89 | get
90 | {
91 | if (_z == null)
92 | {
93 | bool wantRfc1950Header = (this._flavor == ZlibStreamFlavor.ZLIB);
94 | _z = new ZlibCodec();
95 | if (this._compressionMode == CompressionMode.Decompress)
96 | {
97 | _z.InitializeInflate(wantRfc1950Header);
98 | }
99 | else
100 | {
101 | _z.Strategy = Strategy;
102 | _z.InitializeDeflate(this._level, wantRfc1950Header);
103 | }
104 | }
105 | return _z;
106 | }
107 | }
108 |
109 |
110 |
111 | private byte[] workingBuffer
112 | {
113 | get
114 | {
115 | if (_workingBuffer == null)
116 | _workingBuffer = new byte[_bufferSize];
117 | return _workingBuffer;
118 | }
119 | }
120 |
121 |
122 |
123 | public override void Write(System.Byte[] buffer, int offset, int count)
124 | {
125 | // workitem 7159
126 | // calculate the CRC on the unccompressed data (before writing)
127 | if (crc != null)
128 | crc.SlurpBlock(buffer, offset, count);
129 |
130 | if (_streamMode == StreamMode.Undefined)
131 | _streamMode = StreamMode.Writer;
132 | else if (_streamMode != StreamMode.Writer)
133 | throw new ZlibException("Cannot Write after Reading.");
134 |
135 | if (count == 0)
136 | return;
137 |
138 | // first reference of z property will initialize the private var _z
139 | z.InputBuffer = buffer;
140 | _z.NextIn = offset;
141 | _z.AvailableBytesIn = count;
142 | bool done = false;
143 | do
144 | {
145 | _z.OutputBuffer = workingBuffer;
146 | _z.NextOut = 0;
147 | _z.AvailableBytesOut = _workingBuffer.Length;
148 | int rc = (_wantCompress)
149 | ? _z.Deflate(_flushMode)
150 | : _z.Inflate(_flushMode);
151 | if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
152 | throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message);
153 |
154 | //if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
155 | _stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
156 |
157 | done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;
158 |
159 | // If GZIP and de-compress, we're done when 8 bytes remain.
160 | if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
161 | done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
162 |
163 | }
164 | while (!done);
165 | }
166 |
167 |
168 |
169 | private void finish()
170 | {
171 | if (_z == null) return;
172 |
173 | if (_streamMode == StreamMode.Writer)
174 | {
175 | bool done = false;
176 | do
177 | {
178 | _z.OutputBuffer = workingBuffer;
179 | _z.NextOut = 0;
180 | _z.AvailableBytesOut = _workingBuffer.Length;
181 | int rc = (_wantCompress)
182 | ? _z.Deflate(FlushType.Finish)
183 | : _z.Inflate(FlushType.Finish);
184 |
185 | if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
186 | {
187 | string verb = (_wantCompress ? "de" : "in") + "flating";
188 | if (_z.Message == null)
189 | throw new ZlibException(String.Format("{0}: (rc = {1})", verb, rc));
190 | else
191 | throw new ZlibException(verb + ": " + _z.Message);
192 | }
193 |
194 | if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
195 | {
196 | _stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
197 | }
198 |
199 | done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;
200 | // If GZIP and de-compress, we're done when 8 bytes remain.
201 | if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
202 | done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
203 |
204 | }
205 | while (!done);
206 |
207 | Flush();
208 |
209 | // workitem 7159
210 | if (_flavor == ZlibStreamFlavor.GZIP)
211 | {
212 | if (_wantCompress)
213 | {
214 | // Emit the GZIP trailer: CRC32 and size mod 2^32
215 | int c1 = crc.Crc32Result;
216 | _stream.Write(BitConverter.GetBytes(c1), 0, 4);
217 | int c2 = (Int32)(crc.TotalBytesRead & 0x00000000FFFFFFFF);
218 | _stream.Write(BitConverter.GetBytes(c2), 0, 4);
219 | }
220 | else
221 | {
222 | throw new ZlibException("Writing with decompression is not supported.");
223 | }
224 | }
225 | }
226 | // workitem 7159
227 | else if (_streamMode == StreamMode.Reader)
228 | {
229 | if (_flavor == ZlibStreamFlavor.GZIP)
230 | {
231 | if (!_wantCompress)
232 | {
233 | // workitem 8501: handle edge case (decompress empty stream)
234 | if (_z.TotalBytesOut == 0L)
235 | return;
236 |
237 | // Read and potentially verify the GZIP trailer:
238 | // CRC32 and size mod 2^32
239 | byte[] trailer = new byte[8];
240 |
241 | // workitems 8679 & 12554
242 | if (_z.AvailableBytesIn < 8)
243 | {
244 | // Make sure we have read to the end of the stream
245 | Array.Copy(_z.InputBuffer, _z.NextIn, trailer, 0, _z.AvailableBytesIn);
246 | int bytesNeeded = 8 - _z.AvailableBytesIn;
247 | int bytesRead = _stream.Read(trailer,
248 | _z.AvailableBytesIn,
249 | bytesNeeded);
250 | if (bytesNeeded != bytesRead)
251 | {
252 | throw new ZlibException(String.Format("Missing or incomplete GZIP trailer. Expected 8 bytes, got {0}.",
253 | _z.AvailableBytesIn + bytesRead));
254 | }
255 | }
256 | else
257 | {
258 | Array.Copy(_z.InputBuffer, _z.NextIn, trailer, 0, trailer.Length);
259 | }
260 |
261 | Int32 crc32_expected = BitConverter.ToInt32(trailer, 0);
262 | Int32 crc32_actual = crc.Crc32Result;
263 | Int32 isize_expected = BitConverter.ToInt32(trailer, 4);
264 | Int32 isize_actual = (Int32)(_z.TotalBytesOut & 0x00000000FFFFFFFF);
265 |
266 | if (crc32_actual != crc32_expected)
267 | throw new ZlibException(String.Format("Bad CRC32 in GZIP trailer. (actual({0:X8})!=expected({1:X8}))", crc32_actual, crc32_expected));
268 |
269 | if (isize_actual != isize_expected)
270 | throw new ZlibException(String.Format("Bad size in GZIP trailer. (actual({0})!=expected({1}))", isize_actual, isize_expected));
271 |
272 | }
273 | else
274 | {
275 | throw new ZlibException("Reading with compression is not supported.");
276 | }
277 | }
278 | }
279 | }
280 |
281 |
282 | private void end()
283 | {
284 | if (z == null)
285 | return;
286 | if (_wantCompress)
287 | {
288 | _z.EndDeflate();
289 | }
290 | else
291 | {
292 | _z.EndInflate();
293 | }
294 | _z = null;
295 | }
296 |
297 |
298 | public override void Close()
299 | {
300 | if (_stream == null) return;
301 | try
302 | {
303 | finish();
304 | }
305 | finally
306 | {
307 | end();
308 | if (!_leaveOpen) _stream.Close();
309 | _stream = null;
310 | }
311 | }
312 |
313 | public override void Flush()
314 | {
315 | _stream.Flush();
316 | }
317 |
318 | public override System.Int64 Seek(System.Int64 offset, System.IO.SeekOrigin origin)
319 | {
320 | throw new NotImplementedException();
321 | //_outStream.Seek(offset, origin);
322 | }
323 | public override void SetLength(System.Int64 value)
324 | {
325 | _stream.SetLength(value);
326 | }
327 |
328 |
329 | #if NOT
330 | public int Read()
331 | {
332 | if (Read(_buf1, 0, 1) == 0)
333 | return 0;
334 | // calculate CRC after reading
335 | if (crc!=null)
336 | crc.SlurpBlock(_buf1,0,1);
337 | return (_buf1[0] & 0xFF);
338 | }
339 | #endif
340 |
341 | private bool nomoreinput = false;
342 |
343 |
344 |
345 | private string ReadZeroTerminatedString()
346 | {
347 | /*
348 | var list = new System.Collections.Generic.List();
349 | bool done = false;
350 | do
351 | {
352 | // workitem 7740
353 | int n = _stream.Read(_buf1, 0, 1);
354 | if (n != 1)
355 | throw new ZlibException("Unexpected EOF reading GZIP header.");
356 | else
357 | {
358 | if (_buf1[0] == 0)
359 | done = true;
360 | else
361 | list.Add(_buf1[0]);
362 | }
363 | } while (!done);
364 | byte[] a = list.ToArray();
365 | return GZipStream.iso8859dash1.GetString(a, 0, a.Length);
366 | */
367 |
368 | return "";
369 | }
370 |
371 |
372 | private int _ReadAndValidateGzipHeader()
373 | {
374 | /*
375 | int totalBytesRead = 0;
376 | // read the header on the first read
377 | byte[] header = new byte[10];
378 | int n = _stream.Read(header, 0, header.Length);
379 |
380 | // workitem 8501: handle edge case (decompress empty stream)
381 | if (n == 0)
382 | return 0;
383 |
384 | if (n != 10)
385 | throw new ZlibException("Not a valid GZIP stream.");
386 |
387 | if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8)
388 | throw new ZlibException("Bad GZIP header.");
389 |
390 | Int32 timet = BitConverter.ToInt32(header, 4);
391 | _GzipMtime = GZipStream._unixEpoch.AddSeconds(timet);
392 | totalBytesRead += n;
393 | if ((header[3] & 0x04) == 0x04)
394 | {
395 | // read and discard extra field
396 | n = _stream.Read(header, 0, 2); // 2-byte length field
397 | totalBytesRead += n;
398 |
399 | Int16 extraLength = (Int16)(header[0] + header[1] * 256);
400 | byte[] extra = new byte[extraLength];
401 | n = _stream.Read(extra, 0, extra.Length);
402 | if (n != extraLength)
403 | throw new ZlibException("Unexpected end-of-file reading GZIP header.");
404 | totalBytesRead += n;
405 | }
406 | if ((header[3] & 0x08) == 0x08)
407 | _GzipFileName = ReadZeroTerminatedString();
408 | if ((header[3] & 0x10) == 0x010)
409 | _GzipComment = ReadZeroTerminatedString();
410 | if ((header[3] & 0x02) == 0x02)
411 | Read(_buf1, 0, 1); // CRC16, ignore
412 |
413 | return totalBytesRead;*/
414 |
415 | return 0;
416 | }
417 |
418 |
419 |
420 | public override System.Int32 Read(System.Byte[] buffer, System.Int32 offset, System.Int32 count)
421 | {
422 | // According to MS documentation, any implementation of the IO.Stream.Read function must:
423 | // (a) throw an exception if offset & count reference an invalid part of the buffer,
424 | // or if count < 0, or if buffer is null
425 | // (b) return 0 only upon EOF, or if count = 0
426 | // (c) if not EOF, then return at least 1 byte, up to bytes
427 |
428 | if (_streamMode == StreamMode.Undefined)
429 | {
430 | if (!this._stream.CanRead) throw new ZlibException("The stream is not readable.");
431 | // for the first read, set up some controls.
432 | _streamMode = StreamMode.Reader;
433 | // (The first reference to _z goes through the private accessor which
434 | // may initialize it.)
435 | z.AvailableBytesIn = 0;
436 | if (_flavor == ZlibStreamFlavor.GZIP)
437 | {
438 | _gzipHeaderByteCount = _ReadAndValidateGzipHeader();
439 | // workitem 8501: handle edge case (decompress empty stream)
440 | if (_gzipHeaderByteCount == 0)
441 | return 0;
442 | }
443 | }
444 |
445 | if (_streamMode != StreamMode.Reader)
446 | throw new ZlibException("Cannot Read after Writing.");
447 |
448 | if (count == 0) return 0;
449 | if (nomoreinput && _wantCompress) return 0; // workitem 8557
450 | if (buffer == null) throw new ArgumentNullException("buffer");
451 | if (count < 0) throw new ArgumentOutOfRangeException("count");
452 | if (offset < buffer.GetLowerBound(0)) throw new ArgumentOutOfRangeException("offset");
453 | if ((offset + count) > buffer.GetLength(0)) throw new ArgumentOutOfRangeException("count");
454 |
455 | int rc = 0;
456 |
457 | // set up the output of the deflate/inflate codec:
458 | _z.OutputBuffer = buffer;
459 | _z.NextOut = offset;
460 | _z.AvailableBytesOut = count;
461 |
462 | // This is necessary in case _workingBuffer has been resized. (new byte[])
463 | // (The first reference to _workingBuffer goes through the private accessor which
464 | // may initialize it.)
465 | _z.InputBuffer = workingBuffer;
466 |
467 | do
468 | {
469 | // need data in _workingBuffer in order to deflate/inflate. Here, we check if we have any.
470 | if ((_z.AvailableBytesIn == 0) && (!nomoreinput))
471 | {
472 | // No data available, so try to Read data from the captive stream.
473 | _z.NextIn = 0;
474 | _z.AvailableBytesIn = _stream.Read(_workingBuffer, 0, _workingBuffer.Length);
475 | if (_z.AvailableBytesIn == 0)
476 | nomoreinput = true;
477 |
478 | }
479 | // we have data in InputBuffer; now compress or decompress as appropriate
480 | rc = (_wantCompress)
481 | ? _z.Deflate(_flushMode)
482 | : _z.Inflate(_flushMode);
483 |
484 | if (nomoreinput && (rc == ZlibConstants.Z_BUF_ERROR))
485 | return 0;
486 |
487 | if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
488 | throw new ZlibException(String.Format("{0}flating: rc={1} msg={2}", (_wantCompress ? "de" : "in"), rc, _z.Message));
489 |
490 | if ((nomoreinput || rc == ZlibConstants.Z_STREAM_END) && (_z.AvailableBytesOut == count))
491 | break; // nothing more to read
492 | }
493 | //while (_z.AvailableBytesOut == count && rc == ZlibConstants.Z_OK);
494 | while (_z.AvailableBytesOut > 0 && !nomoreinput && rc == ZlibConstants.Z_OK);
495 |
496 |
497 | // workitem 8557
498 | // is there more room in output?
499 | if (_z.AvailableBytesOut > 0)
500 | {
501 | if (rc == ZlibConstants.Z_OK && _z.AvailableBytesIn == 0)
502 | {
503 | // deferred
504 | }
505 |
506 | // are we completely done reading?
507 | if (nomoreinput)
508 | {
509 | // and in compression?
510 | if (_wantCompress)
511 | {
512 | // no more input data available; therefore we flush to
513 | // try to complete the read
514 | rc = _z.Deflate(FlushType.Finish);
515 |
516 | if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
517 | throw new ZlibException(String.Format("Deflating: rc={0} msg={1}", rc, _z.Message));
518 | }
519 | }
520 | }
521 |
522 |
523 | rc = (count - _z.AvailableBytesOut);
524 |
525 | // calculate CRC after reading
526 | if (crc != null)
527 | crc.SlurpBlock(buffer, offset, rc);
528 |
529 | return rc;
530 | }
531 |
532 |
533 |
534 | public override System.Boolean CanRead
535 | {
536 | get { return this._stream.CanRead; }
537 | }
538 |
539 | public override System.Boolean CanSeek
540 | {
541 | get { return this._stream.CanSeek; }
542 | }
543 |
544 | public override System.Boolean CanWrite
545 | {
546 | get { return this._stream.CanWrite; }
547 | }
548 |
549 | public override System.Int64 Length
550 | {
551 | get { return _stream.Length; }
552 | }
553 |
554 | public override long Position
555 | {
556 | get { throw new NotImplementedException(); }
557 | set { throw new NotImplementedException(); }
558 | }
559 |
560 | internal enum StreamMode
561 | {
562 | Writer,
563 | Reader,
564 | Undefined,
565 | }
566 |
567 |
568 | public static void CompressString(String s, System.IO.Stream compressor)
569 | {
570 | byte[] uncompressed = System.Text.Encoding.UTF8.GetBytes(s);
571 | using (compressor)
572 | {
573 | compressor.Write(uncompressed, 0, uncompressed.Length);
574 | }
575 | }
576 |
577 | public static void CompressBuffer(byte[] b, System.IO.Stream compressor)
578 | {
579 | // workitem 8460
580 | using (compressor)
581 | {
582 | compressor.Write(b, 0, b.Length);
583 | }
584 | }
585 |
586 | public static String UncompressString(byte[] compressed, System.IO.Stream decompressor)
587 | {
588 | // workitem 8460
589 | byte[] working = new byte[1024];
590 | var encoding = System.Text.Encoding.UTF8;
591 | using (var output = new MemoryStream())
592 | {
593 | using (decompressor)
594 | {
595 | int n;
596 | while ((n = decompressor.Read(working, 0, working.Length)) != 0)
597 | {
598 | output.Write(working, 0, n);
599 | }
600 | }
601 |
602 | // reset to allow read from start
603 | output.Seek(0, SeekOrigin.Begin);
604 | var sr = new StreamReader(output, encoding);
605 | return sr.ReadToEnd();
606 | }
607 | }
608 |
609 | public static byte[] UncompressBuffer(byte[] compressed, System.IO.Stream decompressor)
610 | {
611 | // workitem 8460
612 | byte[] working = new byte[1024];
613 | using (var output = new MemoryStream())
614 | {
615 | using (decompressor)
616 | {
617 | int n;
618 | while ((n = decompressor.Read(working, 0, working.Length)) != 0)
619 | {
620 | output.Write(working, 0, n);
621 | }
622 | }
623 | return output.ToArray();
624 | }
625 | }
626 | }
627 | }
628 |
--------------------------------------------------------------------------------