BrawlCrate v0.41
Wii File Editor
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
BrawlLib.Wii.Compression.LZ77 Class Reference
Inheritance diagram for BrawlLib.Wii.Compression.LZ77:

Public Member Functions

void Dispose ()
 
int Compress (VoidPtr srcAddr, int srcLen, Stream outStream, IProgressTracker progress, bool extFmt)
 

Static Public Member Functions

static int Compact (VoidPtr srcAddr, int srcLen, Stream outStream, ResourceNode r, bool extendedFormat)
 
static void Expand (CompressionHeader *header, VoidPtr dstAddress, int dstLen)
 
static void Expand (VoidPtr data, VoidPtr dstAddress, int dstLen, bool extFmt)
 

Public Attributes

int PatternLength = 18
 

Static Public Attributes

const int WindowMask = 0xFFF
 
const int WindowLength = 4096
 
const int MinMatch = 3
 

Member Function Documentation

◆ Compact()

static int BrawlLib.Wii.Compression.LZ77.Compact ( VoidPtr  srcAddr,
int  srcLen,
Stream  outStream,
ResourceNode  r,
bool  extendedFormat 
)
inlinestatic
251 {
252 using (LZ77 lz = new LZ77())
253 {
255 (extendedFormat ? "Extended " : "") + "LZ77",
256 $"Compressing {r.Name}, please wait...", false))
257 {
258 return lz.Compress(srcAddr, srcLen, outStream, prog, extendedFormat);
259 }
260 }
261 }
Definition: ProgressWindow.cs:8
ResourceNode RootNode
Definition: ResourceNode.cs:175
Form _mainForm
Definition: ResourceNode.cs:130

◆ Compress()

int BrawlLib.Wii.Compression.LZ77.Compress ( VoidPtr  srcAddr,
int  srcLen,
Stream  outStream,
IProgressTracker  progress,
bool  extFmt 
)
inline
51 {
52 int dstLen = 4, bitCount;
53 byte control;
54
55 byte* sPtr = (byte*) srcAddr;
56 int matchLength, matchOffset = 0;
57 PatternLength = extFmt ? 0xFFFF + 0xFF + 0xF + 3 : 0xF + 3;
58
59 //Initialize
60 Memory.Fill(_First, 0x40000, 0xFF);
61 _wIndex = _wLength = 0;
62
63 //Write header
64 CompressionHeader header = new CompressionHeader
65 {
66 Algorithm = CompressionType.LZ77,
67 ExpandedSize = (uint) srcLen,
68 IsExtendedLZ77 = extFmt
69 };
70 outStream.Write(&header, 4 + (header.LargeSize ? 4 : 0));
71
72 List<byte> blockBuffer;
73 int lastUpdate = srcLen;
74 int remaining = srcLen;
75
76 progress?.Begin(0, remaining, 0);
77
78 while (remaining > 0)
79 {
80 blockBuffer = new List<byte> {0};
81 for (bitCount = 0, control = 0; bitCount < 8 && remaining > 0; bitCount++)
82 {
83 control <<= 1;
84 if ((matchLength = FindPattern(sPtr, remaining, ref matchOffset)) != 0)
85 {
86 int length;
87 if (extFmt)
88 {
89 if (matchLength >= 0xFF + 0xF + 3)
90 {
91 length = matchLength - 0xFF - 0xF - 3;
92 blockBuffer.Add((byte) (0x10 | (length >> 12)));
93 blockBuffer.Add((byte) (length >> 4));
94 }
95 else if (matchLength >= 0xF + 2)
96 {
97 length = matchLength - 0xF - 2;
98 blockBuffer.Add((byte) (length >> 4));
99 }
100 else
101 {
102 length = matchLength - 1;
103 }
104 }
105 else
106 {
107 length = matchLength - 3;
108 }
109
110 control |= 1;
111 blockBuffer.Add((byte) ((length << 4) | ((matchOffset - 1) >> 8)));
112 blockBuffer.Add((byte) (matchOffset - 1));
113 }
114 else
115 {
116 matchLength = 1;
117 blockBuffer.Add(*sPtr);
118 }
119
120 Consume(sPtr, matchLength, remaining);
121 sPtr += matchLength;
122 remaining -= matchLength;
123 }
124
125 //Left-align bits
126 control <<= 8 - bitCount;
127
128 //Write buffer
129 blockBuffer[0] = control;
130 outStream.Write(blockBuffer.ToArray(), 0, blockBuffer.Count);
131 dstLen += blockBuffer.Count;
132
133 if (progress != null)
134 {
135 if (lastUpdate - remaining > 0x4000)
136 {
137 lastUpdate = remaining;
138 progress.Update(srcLen - remaining);
139 }
140 }
141 }
142
143 outStream.Flush();
144
145 progress?.Finish();
146
147 return dstLen;
148 }
Definition: Memory.cs:8
int PatternLength
Definition: LZ77.cs:16
void Begin(float min, float max, float current)
CompressionType
Definition: CompressionHeader.cs:9

◆ Dispose()

void BrawlLib.Wii.Compression.LZ77.Dispose ( )
inline
40 {
41 if (_dataAddr)
42 {
43 Marshal.FreeHGlobal(_dataAddr);
44 _dataAddr = 0;
45 }
46
47 GC.SuppressFinalize(this);
48 }

◆ Expand() [1/2]

static void BrawlLib.Wii.Compression.LZ77.Expand ( CompressionHeader header,
VoidPtr  dstAddress,
int  dstLen 
)
inlinestatic
264 {
265 Expand(header->Data, dstAddress, dstLen, header->IsExtendedLZ77);
266 }
static void Expand(CompressionHeader *header, VoidPtr dstAddress, int dstLen)
Definition: LZ77.cs:263

◆ Expand() [2/2]

static void BrawlLib.Wii.Compression.LZ77.Expand ( VoidPtr  data,
VoidPtr  dstAddress,
int  dstLen,
bool  extFmt 
)
inlinestatic
269 {
270 for (byte* srcPtr = (byte*) data, dstPtr = (byte*) dstAddress, ceiling = dstPtr + dstLen; dstPtr < ceiling;)
271 {
272 for (byte control = *srcPtr++, bit = 8; bit-- != 0 && dstPtr != ceiling;)
273 {
274 if ((control & (1 << bit)) == 0)
275 {
276 *dstPtr++ = *srcPtr++;
277 }
278 else
279 {
280 int temp = *srcPtr >> 4,
281 num = !extFmt
282 ? temp + 3
283 : temp == 1
284 ? (((*srcPtr++ & 0x0F) << 12) | (*srcPtr++ << 4) | (*srcPtr >> 4)) + 0xFF + 0xF + 3
285 : temp == 0
286 ? (((*srcPtr++ & 0x0F) << 4) | (*srcPtr >> 4)) + 0xF + 2
287 : temp + 1,
288 offset = (((*srcPtr++ & 0xF) << 8) | *srcPtr++) + 2;
289 while (dstPtr != ceiling && num-- > 0)
290 {
291 *dstPtr++ = dstPtr[-offset];
292 }
293 }
294 }
295 }
296 }

Member Data Documentation

◆ MinMatch

const int BrawlLib.Wii.Compression.LZ77.MinMatch = 3
static

◆ PatternLength

int BrawlLib.Wii.Compression.LZ77.PatternLength = 18

◆ WindowLength

const int BrawlLib.Wii.Compression.LZ77.WindowLength = 4096
static

◆ WindowMask

const int BrawlLib.Wii.Compression.LZ77.WindowMask = 0xFFF
static

The documentation for this class was generated from the following file: