DinoChiesa / DotNetZip

Library for creating and reading .ZIP files from a .NET Language
Other
120 stars 41 forks source link

Remove duplicated code, replaced with UpdateCRC. Funtion SlurpBlock. #27

Open evo-i opened 3 months ago

evo-i commented 3 months ago

Removed duplicated lines of code. Replaced with UpdateCRC.

public void SlurpBlock(byte[] block, int offset, int count)
{
    if (block == null)
        throw new Exception("The data buffer must not be null.");

    // bzip algorithm
    for (int i = 0; i < count; i++)
    {
        int x = offset + i;
        byte b = block[x];
        if (this.reverseBits)
        {
            UInt32 temp = (_register >> 24) ^ b;
            _register = (_register << 8) ^ crc32Table[temp];
        }
        else
        {
            UInt32 temp = (_register & 0x000000FF) ^ b;
            _register = (_register >> 8) ^ crc32Table[temp];
        }
    }
    _TotalBytesRead += count;
}

Replaces with:

public void SlurpBlock(byte[] block, int offset, int count)
{
    if (block == null)
        throw new Exception("The data buffer must not be null.");

    // bzip algorithm
    for (int i = 0; i < count; i++)
    {
        int x = offset + i;
        byte b = block[x];
        UpdateCRC(b);
    }
    _TotalBytesRead += count;
}

Because UpdateCRC has:

public void UpdateCRC(byte b)
{
    if (this.reverseBits)
    {
        UInt32 temp = (_register >> 24) ^ b;
        _register = (_register << 8) ^ crc32Table[temp];
    }
    else
    {
        UInt32 temp = (_register & 0x000000FF) ^ b;
        _register = (_register >> 8) ^ crc32Table[temp];
    }
}