Skip to content

Commit

Permalink
Changed qar and pftxs files to write unnormalized file names to XML.
Browse files Browse the repository at this point in the history
  • Loading branch information
Atvaark committed Oct 8, 2015
1 parent b881d05 commit f51b99d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 42 deletions.
3 changes: 2 additions & 1 deletion GzsTool/Pftxs/PftxsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Xml.Serialization;
using GzsTool.Common;
using GzsTool.Common.Interfaces;
using GzsTool.Utility;

namespace GzsTool.Pftxs
{
Expand Down Expand Up @@ -69,7 +70,7 @@ public override IEnumerable<FileDataStreamContainer> ExportFiles(Stream input)
yield return new FileDataStreamContainer
{
DataStream = () => new MemoryStream(localEntry.Data),
FileName = entry.FilePath
FileName = Hashing.NormalizeFilePath(entry.FilePath)
};
}
}
Expand Down
13 changes: 2 additions & 11 deletions GzsTool/Pftxs/PftxsFtexFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ public class PftxsFtexFile
{
public const int HeaderSize = 32;

[XmlIgnore]
public string FileName { get; set; }

[XmlIgnore]
public long DataOffset { get; set; }

[XmlAttribute("FilePath")]
public string FilePath { get; set; }

[XmlAttribute("Hash")]
public ulong Hash { get; set; }

Expand Down Expand Up @@ -64,7 +55,7 @@ public void Read(Stream input)

string name;
entry.FileNameFound = Hashing.TryGetFileNameFromHash(entry.Hash, out name);
entry.FilePath = Hashing.NormalizeFilePath(name);
entry.FilePath = name;
Entries.Add(entry);
}

Expand All @@ -83,7 +74,7 @@ public void WriteData(BinaryWriter writer, IDirectory inputDirectory)
foreach (var entry in Entries)
{
entry.CalculateHash();
var data = inputDirectory.ReadFile(entry.FilePath);
var data = inputDirectory.ReadFile(Hashing.NormalizeFilePath(entry.FilePath));
entry.Offset = Convert.ToInt32(writer.BaseStream.Position - ftexHeaderPosition);
entry.Size = Convert.ToInt32(data.Length);
writer.Write(data);
Expand Down
47 changes: 22 additions & 25 deletions GzsTool/Qar/QarEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public void CalculateHash()
private void DebugAssertHashMatches()
{
ulong newHash = Hashing.HashFileNameWithExtension(FilePath);
Debug.Assert(Hash == newHash);
if (Hash != newHash)
{
Debug.WriteLine("Hash mismatch '{0}' {1:x}!={2:x}", FilePath, newHash, Hash);
}
}

public void Read(BinaryReader reader)
Expand All @@ -87,7 +90,7 @@ public void Read(BinaryReader reader)
uint md54 = reader.ReadUInt32() ^ xorMask2;

string filePath;
FileNameFound = TryGetFilePath(out filePath);
FileNameFound = Hashing.TryGetFileNameFromHash(Hash, out filePath);
FilePath = filePath;
DataOffset = reader.BaseStream.Position;
}
Expand All @@ -97,7 +100,7 @@ public FileDataStreamContainer Export(Stream input)
FileDataStreamContainer fileDataStreamContainer = new FileDataStreamContainer
{
DataStream = ReadDataLazy(input),
FileName = FilePath
FileName = Hashing.NormalizeFilePath(FilePath)
};
return fileDataStreamContainer;
}
Expand All @@ -118,42 +121,36 @@ private Stream ReadData(Stream input)
input.Position = DataOffset;
BinaryReader reader = new BinaryReader(input, Encoding.Default, true);

byte[] sectionData = reader.ReadBytes((int)UncompressedSize);
Decrypt1(sectionData, hashLow: (uint) (Hash & 0xFFFFFFFF));
uint magicEntry = BitConverter.ToUInt32(sectionData, 0);
byte[] data = reader.ReadBytes((int)UncompressedSize);
Decrypt1(data, hashLow: (uint) (Hash & 0xFFFFFFFF));
uint magicEntry = BitConverter.ToUInt32(data, 0);
if (magicEntry == 0xA0F8EFE6)
{
const int headerSize = 8;
Key = BitConverter.ToUInt32(sectionData, 4);
Key = BitConverter.ToUInt32(data, 4);
UncompressedSize -= headerSize;
byte[] newSectionData = new byte[UncompressedSize];
Array.Copy(sectionData, headerSize, newSectionData, 0, UncompressedSize);
Decrypt2(newSectionData, Key);
byte[] newData = new byte[UncompressedSize];
Array.Copy(data, headerSize, newData, 0, UncompressedSize);
Decrypt2(newData, Key);
data = newData;
}
else if (magicEntry == 0xE3F8EFE6)
{
const int headerSize = 16;
Key = BitConverter.ToUInt32(sectionData, 4);
Key = BitConverter.ToUInt32(data, 4);
UncompressedSize -= headerSize;
byte[] newSectionData = new byte[UncompressedSize];
Array.Copy(sectionData, headerSize, newSectionData, 0, UncompressedSize);
Decrypt2(newSectionData, Key);
sectionData = newSectionData;
byte[] newData = new byte[UncompressedSize];
Array.Copy(data, headerSize, newData, 0, UncompressedSize);
Decrypt2(newData, Key);
data = newData;
}

if (Compressed)
{
sectionData = Compression.Inflate(sectionData);
data = Compression.Uncompress(data);
}

return new MemoryStream(sectionData);
}

private bool TryGetFilePath(out string filePath)
{
bool filePathFound = Hashing.TryGetFileNameFromHash(Hash, out filePath);
filePath = Hashing.NormalizeFilePath(filePath);
return filePathFound;
return new MemoryStream(data);
}

private void Decrypt1(byte[] sectionData, uint hashLow)
Expand Down Expand Up @@ -274,7 +271,7 @@ public void Write(Stream output, IDirectory inputDirectory)
uint compressedSize;
if (Compressed)
{
data = Compression.Deflate(data);
data = Compression.Compress(data);
compressedSize = (uint) data.Length;
}
else
Expand Down
13 changes: 8 additions & 5 deletions GzsTool/Utility/Compression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ namespace GzsTool.Utility
{
internal static class Compression
{
internal static byte[] Inflate(byte[] buffer)
internal static byte[] Uncompress(byte[] buffer)
{
return ZlibStream.UncompressBuffer(buffer);
}

internal static byte[] Deflate(byte[] buffer)
internal static byte[] Compress(byte[] buffer)
{
using (Stream input = new MemoryStream(buffer))
using (Stream zlibInput = new ZlibStream(input, CompressionMode.Compress, CompressionLevel.Default))
using (var output = new MemoryStream())
{
return zlibInput.ToArray();
using (Stream compressor = new ZlibStream(output, CompressionMode.Compress, CompressionLevel.BestCompression))
{
compressor.Write(buffer, 0, buffer.Length);
}
return output.ToArray();
}
}
}
Expand Down

0 comments on commit f51b99d

Please sign in to comment.