Skip to content

Commit

Permalink
Merge branch 'fix/compressv1-index-out-of-bounds'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaplas80 committed Dec 6, 2020
2 parents 14c221a + f162260 commit d5e3cf7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<Product>ParManager</Product>

<Authors>Kaplas</Authors>
Expand Down
12 changes: 12 additions & 0 deletions ParLibrary/Sllz/Compressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ private static DataStream CompressV1(DataStream inputDataStream)
outputData[flagPosition] = 0x00;
outputPosition++;

if (outputPosition >= outputSize)
{
throw new SllzCompressorException("Compressed size is bigger than original size.");
}

while (inputPosition < inputData.Length)
{
uint windowSize = Math.Min(inputPosition, MAX_WINDOW_SIZE);
Expand Down Expand Up @@ -168,6 +173,7 @@ private static DataStream CompressV1(DataStream inputDataStream)
outputData[outputPosition] = inputData[inputPosition];
inputPosition++;
outputPosition++;

if (outputPosition >= outputSize)
{
throw new SllzCompressorException("Compressed size is bigger than original size.");
Expand Down Expand Up @@ -201,6 +207,12 @@ private static DataStream CompressV1(DataStream inputDataStream)

outputData[outputPosition] = (byte)tuple;
outputPosition++;

if (outputPosition >= outputSize)
{
throw new SllzCompressorException("Compressed size is bigger than original size.");
}

outputData[outputPosition] = (byte)(tuple >> 8);
outputPosition++;

Expand Down
2 changes: 1 addition & 1 deletion ParTool/Program.Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static void Add(Options.Add opts)

Console.Write("Reading input directory... ");
string nodeName = new DirectoryInfo(opts.AddDirectory).Name;
Node node = NodeFactory.FromDirectory(opts.AddDirectory, "*", nodeName, true);
Node node = ReadDirectory(opts.AddDirectory, nodeName);
node.TransformWith<ParArchiveWriter, ParArchiveWriterParameters>(writerParameters).TransformWith<ParArchiveReader, ParArchiveReaderParameters>(readerParameters);
Console.WriteLine("DONE!");

Expand Down
2 changes: 1 addition & 1 deletion ParTool/Program.Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void Create(Options.Create opts)

Console.Write("Reading input directory... ");
string nodeName = new DirectoryInfo(opts.InputDirectory).Name;
Node node = NodeFactory.FromDirectory(opts.InputDirectory, "*", nodeName, true);
Node node = ReadDirectory(opts.InputDirectory, nodeName);

#pragma warning disable CA1308 // Normalize strings to uppercase
node.SortChildren((x, y) => string.CompareOrdinal(x.Name.ToLowerInvariant(), y.Name.ToLowerInvariant()));
Expand Down
9 changes: 5 additions & 4 deletions ParTool/Program.Extract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ private static void Extract(Node parNode, string outputFolder)
{
foreach (Node node in Navigator.IterateNodes(parNode))
{
string fileInfoPath = node.Path.Substring(parNode.Path.Length).Replace('/', Path.DirectorySeparatorChar);
string outputPath = Path.Join(outputFolder, fileInfoPath);

var file = node.GetFormatAs<ParFile>();

if (file == null)
{
Directory.CreateDirectory(outputPath);
continue;
}

Console.Write($"Extracting {node.Path}... ");

string fileInfoPath = node.Path.Substring(parNode.Path.Length).Replace('/', Path.DirectorySeparatorChar);
string outputPath = Path.Join(outputFolder, fileInfoPath);
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));

if (file.IsCompressed)
{
node.TransformWith<ParLibrary.Sllz.Decompressor>();
Expand Down
35 changes: 35 additions & 0 deletions ParTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace ParTool
using System.IO;
using CommandLine;
using CommandLine.Text;
using Yarhl.FileSystem;

/// <summary>
/// Main program.
Expand Down Expand Up @@ -78,5 +79,39 @@ private static void WriteHeader()
Console.WriteLine(CommandLine.Text.CopyrightInfo.Default);
Console.WriteLine();
}

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(
"Reliability",
"CA2000:Dispose objects before losing scope",
Justification = "Ownserhip dispose transferred")]
private static Node ReadDirectory(string dirPath, string nodeName = "")
{
dirPath = Path.GetFullPath(dirPath);

if (string.IsNullOrEmpty(nodeName))
{
nodeName = Path.GetFileName(dirPath);
}

Node container = Yarhl.FileSystem.NodeFactory.CreateContainer(nodeName);
var directoryInfo = new DirectoryInfo(dirPath);
container.Tags["DirectoryInfo"] = directoryInfo;

var files = directoryInfo.GetFiles();
foreach (FileInfo file in files)
{
Node fileNode = Yarhl.FileSystem.NodeFactory.FromFile(file.FullName);
container.Add(fileNode);
}

var directories = directoryInfo.GetDirectories();
foreach (DirectoryInfo directory in directories)
{
Node directoryNode = ReadDirectory(directory.FullName);
container.Add(directoryNode);
}

return container;
}
}
}

0 comments on commit d5e3cf7

Please sign in to comment.