(LZ4): use literal constants.

This commit is contained in:
morkt 2017-04-02 10:01:51 +04:00
parent e604ed6607
commit 89de867c2d

View File

@ -187,8 +187,12 @@ namespace GameRes.Compression
// XXX checksum is ignored // XXX checksum is ignored
} }
const int MinMatch = 4;
const int LastLiterals = 5; const int LastLiterals = 5;
const int MFLimit = 12; const int MFLimit = 12;
const int MatchLengthBits = 4;
const int MatchLengthMask = 0xF;
const int RunMask = 0xF;
int DecompressBlock () int DecompressBlock ()
{ {
@ -202,8 +206,8 @@ namespace GameRes.Compression
{ {
/* get literal length */ /* get literal length */
int token = m_block[src++]; int token = m_block[src++];
int length = token >> 4; int length = token >> MatchLengthBits;
if (0xF == length) if (RunMask == length)
{ {
int n; int n;
do do
@ -211,7 +215,7 @@ namespace GameRes.Compression
n = m_block[src++]; n = m_block[src++];
length += n; length += n;
} }
while ((src < iend - 0xF) && (0xFF == n)); while ((src < iend - RunMask) && (0xFF == n));
if (dst + length < dst || src + length < src) // overflow detection if (dst + length < dst || src + length < src) // overflow detection
throw InvalidData(); throw InvalidData();
} }
@ -239,8 +243,8 @@ namespace GameRes.Compression
throw InvalidData(); throw InvalidData();
/* get matchlength */ /* get matchlength */
length = token & 0xF; length = token & MatchLengthMask;
if (0xF == length) if (MatchLengthMask == length)
{ {
int n; int n;
do do
@ -254,7 +258,7 @@ namespace GameRes.Compression
if (dst + length < dst) // overflow detection if (dst + length < dst) // overflow detection
throw InvalidData(); throw InvalidData();
} }
length += 4; length += MinMatch;
/* copy match within block */ /* copy match within block */
Binary.CopyOverlapped (m_data, match, dst, length); Binary.CopyOverlapped (m_data, match, dst, length);