-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
namespace Ambermoon | ||
{ | ||
public class PngCrc | ||
{ | ||
uint[] table = null; | ||
uint lastCrc = 0; | ||
|
||
void EnsureTable() | ||
{ | ||
if (table != null) | ||
return; | ||
|
||
table = new uint[256]; | ||
uint c; | ||
uint k; | ||
|
||
for (uint n = 0; n < 256; ++n) | ||
{ | ||
c = n; | ||
|
||
for (k = 0; k < 8; ++k) | ||
{ | ||
if ((c & 1) != 0) | ||
c = 0xedb88320 ^ (c >> 1); | ||
else | ||
c >>= 1; | ||
} | ||
|
||
table[n] = c; | ||
} | ||
} | ||
|
||
uint Update(uint crc, byte[] buffer, int length) | ||
{ | ||
EnsureTable(); | ||
uint c = crc; | ||
|
||
for (int n = 0; n < length; ++n) | ||
{ | ||
c = table[(c ^ buffer[n]) & 0xff] ^ (c >> 8); | ||
} | ||
|
||
return c; | ||
} | ||
|
||
public uint Calculate(byte[] buffer) | ||
{ | ||
return Calculate(buffer, buffer.Length); | ||
} | ||
|
||
public uint Calculate(byte[] buffer, int length) | ||
{ | ||
return lastCrc = Update(lastCrc ^ 0xffffffff, buffer, length) ^ 0xffffffff; | ||
} | ||
|
||
public uint Calculate(uint crc, byte[] buffer) | ||
{ | ||
return Calculate(crc, buffer, buffer.Length); | ||
} | ||
|
||
public uint Calculate(uint crc, byte[] buffer, int length) | ||
{ | ||
lastCrc = crc; | ||
return Calculate(buffer, length); | ||
} | ||
} | ||
} |