-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
55 lines (52 loc) · 1.99 KB
/
Program.cs
File metadata and controls
55 lines (52 loc) · 1.99 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;
namespace LZ4Wtf
{
internal class Program
{
static byte[] LZ4(byte[] Data, int Len)
{
var Decomp = new byte[Len];
int CompressedPos = 0, DecompressedPos = 0;
int GetLength(int Length)
{
byte CurByte;
if (Length == 15) do Length += CurByte = Data[CompressedPos++]; while (CurByte == 255);
return Length;
}
do
{
byte Tok = Data[CompressedPos++];
int EncCount = (Tok >> 0) & 15, Lcnt = (Tok >> 4) & 15;
Lcnt = GetLength(Lcnt);
Buffer.BlockCopy(Data, CompressedPos, Decomp, DecompressedPos, Lcnt);
CompressedPos += Lcnt;
DecompressedPos += Lcnt;
if (CompressedPos >= Data.Length) break;
int Bk = Data[CompressedPos++] << 0 | Data[CompressedPos++] << 8;
EncCount = GetLength(EncCount) + 4;
int EncPos = DecompressedPos - Bk;
if (EncCount <= Bk)
{
Buffer.BlockCopy(Decomp, EncPos, Decomp, DecompressedPos, EncCount);
DecompressedPos += EncCount;
}
else while (EncCount-- > 0) Decomp[DecompressedPos++] = Decomp[EncPos++];
}
while (CompressedPos < Data.Length && DecompressedPos < Decomp.Length);
return Decomp;
}
static void Main(string[] args)
{
var Name = new FileInfo(args[0]).Name.Split('.')[0];
using (var FileToDecode = File.OpenRead(args[0]))
{
using (var Rd = new BinaryReader(FileToDecode))
{
var Len = Rd.ReadInt32();
File.WriteAllBytes($"{Name}.nro", LZ4(Rd.ReadBytes((int)FileToDecode.Length - 4), Len));
}
}
}
}
}