This commit is contained in:
Michael 2021-02-07 18:57:37 -06:00
parent 25a7878313
commit 894dbce4ab
4 changed files with 93 additions and 44 deletions

68
Lz77.cs
View file

@ -27,26 +27,45 @@ namespace libWiiSharp
//private const int F = 18;
//private const int threshold = 2;
private static readonly uint lz77Magic = 1280980791;
//private readonly int[] leftSon = new int[4097];
//private readonly int[] rightSon = new int[4353];
//private readonly int[] dad = new int[4097];
private readonly int[] leftSon = new int[4097];
private readonly int[] rightSon = new int[4353];
private readonly int[] dad = new int[4097];
private readonly ushort[] textBuffer = new ushort[4113];
//private int matchPosition;
//private int matchLength;
private int matchPosition;
private int matchLength;
/// <summary>
/// Lz77 Magic.
/// </summary>
public static uint Lz77Magic => lz77Magic;
#region Public Functions
/// <summary>
/// Checks whether a file is Lz77 compressed or not.
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static bool IsLz77Compressed(string file)
{
return IsLz77Compressed(File.ReadAllBytes(file));
}
/// <summary>
/// Checks whether a file is Lz77 compressed or not.
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static bool IsLz77Compressed(byte[] file)
{
Headers.HeaderType headerType = Headers.DetectHeader(file);
return (int)Shared.Swap(BitConverter.ToUInt32(file, (int)headerType)) == (int)lz77Magic;
}
/// <summary>
/// Checks whether a file is Lz77 compressed or not.
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static bool IsLz77Compressed(Stream file)
{
Headers.HeaderType headerType = Headers.DetectHeader(file);
@ -56,12 +75,17 @@ namespace libWiiSharp
return (int)Shared.Swap(BitConverter.ToUInt32(buffer, 0)) == (int)lz77Magic;
}
/// <summary>
/// Compresses a file using the Lz77 algorithm.
/// </summary>
/// <param name="inFile"></param>
/// <param name="outFile"></param>
public void Compress(string inFile, string outFile)
{
Stream stream = null;
using (FileStream fileStream = new FileStream(inFile, FileMode.Open))
{
stream = Compress(fileStream);
stream = PrivCompress(fileStream);
}
byte[] buffer = new byte[stream.Length];
@ -77,22 +101,37 @@ namespace libWiiSharp
}
}
/// <summary>
/// Compresses the byte array using the Lz77 algorithm.
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public byte[] Compress(byte[] file)
{
return ((MemoryStream)Compress(new MemoryStream(file))).ToArray();
return ((MemoryStream)PrivCompress(new MemoryStream(file))).ToArray();
}
/// <summary>
/// Compresses the stream using the Lz77 algorithm.
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public Stream Compress(Stream file)
{
return Compress(file);
return PrivCompress(file);
}
/// <summary>
/// Decompresses a file using the Lz77 algorithm.
/// </summary>
/// <param name="inFile"></param>
/// <param name="outFile"></param>
public void Decompress(string inFile, string outFile)
{
Stream stream = null;
using (FileStream fileStream = new FileStream(inFile, FileMode.Open))
{
stream = Decompress(fileStream);
stream = PrivDecompress(fileStream);
}
byte[] buffer = new byte[stream.Length];
@ -108,6 +147,11 @@ namespace libWiiSharp
}
}
/// <summary>
/// Decompresses the byte array using the Lz77 algorithm.
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public byte[] Decompress(byte[] file)
{
return ((MemoryStream)PrivDecompress(new MemoryStream(file))).ToArray();
@ -117,7 +161,9 @@ namespace libWiiSharp
{
return PrivDecompress(file);
}
#endregion
#region Private Functions
private Stream PrivDecompress(Stream inFile)
{
if (!IsLz77Compressed(inFile))
@ -224,7 +270,7 @@ namespace libWiiSharp
label_24:
return memoryStream;
}
/*
private Stream PrivCompress(Stream inFile)
{
if (Lz77.IsLz77Compressed(inFile))
@ -415,6 +461,6 @@ namespace libWiiSharp
this.leftSon[this.dad[p]] = index;
this.dad[p] = 4096;
}
*/
#endregion
}
}

View file

@ -25,6 +25,14 @@ using System.Security.Cryptography;
namespace libWiiSharp
{
public enum StoreType
{
EncryptedContent,
DecryptedContent,
WAD,
All,
}
public class NusClient : IDisposable
{
private const string nusUrl = "http://nus.cdn.shop.wii.com/ccs/download/";
@ -33,12 +41,18 @@ namespace libWiiSharp
private bool continueWithoutTicket;
private bool isDisposed;
/// <summary>
/// If true, existing local files will be used.
/// </summary>
public bool UseLocalFiles
{
get => useLocalFiles;
set => useLocalFiles = value;
}
/// <summary>
/// If true, the download will be continued even if no ticket for the title is avaiable (WAD packaging and decryption are disabled).
/// </summary>
public bool ContinueWithoutTicket
{
get => continueWithoutTicket;
@ -165,11 +179,19 @@ namespace libWiiSharp
FireDebug(" Content ID {0} wasn't found in TMD...", (object)contentId);
throw new Exception("Content ID wasn't found in the TMD!");
}
if (!File.Exists("cetk"))
if (!File.Exists("cetk") && !continueWithoutTicket)
{
FireDebug(" Downloading Ticket...");
//byte[] tikArray = wcNus.DownloadData(str2 + "cetk");
Console.WriteLine("Downloading");
try
{
byte[] tikArray = wcNus.DownloadData(str2 + "cetk");
}
catch(Exception ex)
{
FireDebug(" Downloading Ticket Failed...");
throw new Exception("CETK Doesn't Exist and Downloading Ticket Failed:\n" + ex.Message);
}
}
Console.WriteLine("Continuing");
FireDebug("Parsing Ticket...");
@ -257,6 +279,12 @@ namespace libWiiSharp
break;
}
}
if (ContinueWithoutTicket == true)
{
flag2 = false;
flag1 = true;
flag3 = false;
}
FireDebug(" Checking for Internet connection...");
if (!CheckInet())
{
@ -317,7 +345,8 @@ namespace libWiiSharp
FireDebug(" -> {0} Contents", (object)tmd.NumOfContents);
FireDebug(" Parsing Ticket...");
Ticket tik = Ticket.Load(outputDir + "cetk");
Ticket tik = null;
if (!continueWithoutTicket) { tik = Ticket.Load(outputDir + "cetk"); }
string[] strArray1 = new string[tmd.NumOfContents];
uint contentId;
for (int index1 = 0; index1 < tmd.NumOfContents; ++index1)
@ -427,7 +456,10 @@ namespace libWiiSharp
{
FireDebug(" Deleting TMD and Ticket...");
File.Delete(outputDir + str2);
File.Delete(outputDir + "cetk");
if (ContinueWithoutTicket == false)
{
File.Delete(outputDir + "cetk");
}
}
FireDebug("Downloading Title {0} v{1} Finished...", titleId, string.IsNullOrEmpty(titleVersion) ? "[Latest]" : titleVersion);
FireProgress(100);

View file

@ -1,28 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace libWiiSharp
{
public enum StoreType
{
EncryptedContent,
DecryptedContent,
WAD,
All,
}
}

View file

@ -60,7 +60,6 @@
<Compile Include="MessageEventArgs.cs" />
<Compile Include="NusClient.cs" />
<Compile Include="Shared.cs" />
<Compile Include="StoreType.cs" />
<Compile Include="Ticket.cs" />
<Compile Include="TMD.cs" />
<Compile Include="TPL.cs" />