Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Don't crash with invalid files.
Browse files Browse the repository at this point in the history
  • Loading branch information
yretenai committed Apr 7, 2022
1 parent a265607 commit aa0f107
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
23 changes: 14 additions & 9 deletions HFSExtract.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ public static int Main(string[] args) {
var output = args[1];

foreach (var file in Directory.GetFiles(args[0], "*.hfs", SearchOption.TopDirectoryOnly)) {
using var hfs = new HFSArchive(File.OpenRead(file), Path.GetFileName(file));
foreach (var filename in hfs.Files.Keys) {
var target = Path.Combine(output, filename);
var dir = Path.GetDirectoryName(target) ?? output;
if (!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
Console.WriteLine(Path.GetFileName(file));
try {
using var hfs = new HFSArchive(File.OpenRead(file), Path.GetFileName(file));
foreach (var filename in hfs.Files.Keys) {
var target = Path.Combine(output, filename);
var dir = Path.GetDirectoryName(target) ?? output;
if (!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
}

Console.WriteLine(filename);
File.WriteAllBytes(target, hfs.ReadFile(filename).ToArray());
}

Console.WriteLine(filename);
File.WriteAllBytes(target, hfs.ReadFile(filename).ToArray());
} catch (Exception e) {
Console.Error.WriteLine(e.Message);
}
}

Expand Down
5 changes: 5 additions & 0 deletions HFSExtract/HFSArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;

namespace HFSExtract {
Expand Down Expand Up @@ -48,6 +49,10 @@ public HFSArchive(Stream stream, string fileName) {
var marshal = new CursoredMemoryMarshal(buffer.ToArray());
for (var i = 0; i < Header.Count; ++i) {
var resourceNameLength = marshal.Read<int>();
if (resourceNameLength is < 0 or > 127) {
throw new CryptographicException();
}

var resourceName = Encoding.Unicode.GetString(marshal.Copy(resourceNameLength * 2).Span);
var file = marshal.Read<HFSFile>();
var hash = marshal.Copy(16).ToArray();
Expand Down

0 comments on commit aa0f107

Please sign in to comment.