This repository was archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBcdCompression.cs
More file actions
41 lines (34 loc) · 1.47 KB
/
BcdCompression.cs
File metadata and controls
41 lines (34 loc) · 1.47 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
using System;
using System.IO;
using System.IO.Compression;
namespace CSystemArc
{
internal static class BcdCompression
{
public static byte[] Decompress(Stream stream)
{
int uncompressedSize = Bcd.Read(stream);
int compressedSize = Bcd.Read(stream);
long offset = stream.Position;
byte[] decompressed = new byte[uncompressedSize];
using LzssStream lzss = new LzssStream(stream, CompressionMode.Decompress, true);
lzss.Read(decompressed, 0, uncompressedSize);
stream.Position = offset + compressedSize;
return decompressed;
}
public static void Compress(byte[] uncompressedData, Stream stream)
{
Compress(new ArraySegment<byte>(uncompressedData), stream);
}
public static void Compress(ArraySegment<byte> uncompressedData, Stream stream)
{
using MemoryStream compressedStream = new MemoryStream();
using LzssStream lzss = new LzssStream(compressedStream, CompressionMode.Compress);
lzss.Write(uncompressedData.Array, uncompressedData.Offset, uncompressedData.Count);
Bcd.Write(stream, uncompressedData.Count);
Bcd.Write(stream, (int)compressedStream.Length);
compressedStream.TryGetBuffer(out ArraySegment<byte> compressedData);
stream.Write(compressedData.Array, compressedData.Offset, compressedData.Count);
}
}
}