Repository navigation
Expand file tree
/
Copy pathLZ11BCH.cs
More file actions
55 lines (47 loc) · 1.52 KB
/
Copy pathLZ11BCH.cs
File metadata and controls
55 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.IO;
using Toolbox.Core;
using Toolbox.Core.IO;
namespace CtrLibrary
{
/// <summary>
/// Handles BCH files wrapped in Nintendo LZ11 compression while retaining
/// the original .bch extension.
/// </summary>
public class LZ11BCH : ICompressionFormat
{
public string[] Description { get; } = new[] { "Nintendo LZ11 BCH Compression" };
public string[] Extension { get; } = new[] { "*.bch" };
public bool CanCompress => true;
public bool Identify(Stream stream, string fileName)
{
if (!fileName.EndsWith(".bch", StringComparison.OrdinalIgnoreCase))
return false;
byte[] data = stream.ToArray();
if (data.Length < 5 || data[0] != 0x11)
return false;
try
{
using (var decompressed = new LZSS_N().Decompress(new MemoryStream(data, false)))
{
return decompressed.Length >= 3 &&
decompressed.ReadByte() == 'B' &&
decompressed.ReadByte() == 'C' &&
decompressed.ReadByte() == 'H';
}
}
catch
{
return false;
}
}
public Stream Decompress(Stream stream)
{
return new LZSS_N().Decompress(stream);
}
public Stream Compress(Stream stream)
{
return new LZSS_N().Compress(stream);
}
}
}