CRC-32: Difference between revisions
Content added Content deleted
(Added Pike implementation) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 225: | Line 225: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
=={{header|C sharp|C#}}== |
|||
<lang Csharp> |
|||
/// <summary> |
|||
/// Performs 32-bit reversed cyclic redundancy checks. |
|||
/// </summary> |
|||
public class Crc32 |
|||
{ |
|||
#region Constants |
|||
/// <summary> |
|||
/// Generator polynomial (modulo 2) for the reversed CRC32 algorithm. |
|||
/// </summary> |
|||
private const UInt32 s_generator = 0xEDB88320; |
|||
#endregion |
|||
#region Constructors |
|||
/// <summary> |
|||
/// Creates a new instance of the Crc32 class. |
|||
/// </summary> |
|||
public Crc32() |
|||
{ |
|||
// Constructs the checksum lookup table. Used to optimize the checksum. |
|||
m_checksumTable = Enumerable.Range(0, 256).Select(i => |
|||
{ |
|||
var tableEntry = (uint)i; |
|||
for (var j = 0; j < 8; ++j) |
|||
{ |
|||
tableEntry = ((tableEntry & 1) != 0) |
|||
? (s_generator ^ (tableEntry >> 1)) |
|||
: (tableEntry >> 1); |
|||
} |
|||
return tableEntry; |
|||
}).ToArray(); |
|||
} |
|||
#endregion |
|||
#region Methods |
|||
/// <summary> |
|||
/// Calculates the checksum of the byte stream. |
|||
/// </summary> |
|||
/// <param name="byteStream">The byte stream to calculate the checksum for.</param> |
|||
/// <returns>A 32-bit reversed checksum.</returns> |
|||
public UInt32 Get<T>(IEnumerable<T> byteStream) |
|||
{ |
|||
try |
|||
{ |
|||
// Initialize checksumRegister to 0xFFFFFFFF and calculate the checksum. |
|||
return ~byteStream.Aggregate(0xFFFFFFFF, (checksumRegister, currentByte) => |
|||
(m_checksumTable[(checksumRegister & 0xFF) ^ Convert.ToByte(currentByte)] ^ (checksumRegister >> 8))); |
|||
} |
|||
catch (FormatException e) |
|||
{ |
|||
throw new CrcException("Could not read the stream out as bytes.", e); |
|||
} |
|||
catch (InvalidCastException e) |
|||
{ |
|||
throw new CrcException("Could not read the stream out as bytes.", e); |
|||
} |
|||
catch (OverflowException e) |
|||
{ |
|||
throw new CrcException("Could not read the stream out as bytes.", e); |
|||
} |
|||
} |
|||
#endregion |
|||
#region Fields |
|||
/// <summary> |
|||
/// Contains a cache of calculated checksum chunks. |
|||
/// </summary> |
|||
private readonly UInt32[] m_checksumTable; |
|||
#endregion |
|||
} |
|||
</lang> |
|||
Test: |
|||
<lang Csharp> |
|||
var arrayOfBytes = Encoding.ASCII.GetBytes("The quick brown fox jumps over the lazy dog"); |
|||
var crc32 = new Crc32(); |
|||
Console.WriteLine(crc32.Get(arrayOfBytes).ToString("X")); |
|||
</lang> |
|||
{{out}} |
|||
414fa339 |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 322: | Line 407: | ||
Checksum: 414fa339 |
Checksum: 414fa339 |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
|||
<lang Csharp> |
|||
/// <summary> |
|||
/// Performs 32-bit reversed cyclic redundancy checks. |
|||
/// </summary> |
|||
public class Crc32 |
|||
{ |
|||
#region Constants |
|||
/// <summary> |
|||
/// Generator polynomial (modulo 2) for the reversed CRC32 algorithm. |
|||
/// </summary> |
|||
private const UInt32 s_generator = 0xEDB88320; |
|||
#endregion |
|||
#region Constructors |
|||
/// <summary> |
|||
/// Creates a new instance of the Crc32 class. |
|||
/// </summary> |
|||
public Crc32() |
|||
{ |
|||
// Constructs the checksum lookup table. Used to optimize the checksum. |
|||
m_checksumTable = Enumerable.Range(0, 256).Select(i => |
|||
{ |
|||
var tableEntry = (uint)i; |
|||
for (var j = 0; j < 8; ++j) |
|||
{ |
|||
tableEntry = ((tableEntry & 1) != 0) |
|||
? (s_generator ^ (tableEntry >> 1)) |
|||
: (tableEntry >> 1); |
|||
} |
|||
return tableEntry; |
|||
}).ToArray(); |
|||
} |
|||
#endregion |
|||
#region Methods |
|||
/// <summary> |
|||
/// Calculates the checksum of the byte stream. |
|||
/// </summary> |
|||
/// <param name="byteStream">The byte stream to calculate the checksum for.</param> |
|||
/// <returns>A 32-bit reversed checksum.</returns> |
|||
public UInt32 Get<T>(IEnumerable<T> byteStream) |
|||
{ |
|||
try |
|||
{ |
|||
// Initialize checksumRegister to 0xFFFFFFFF and calculate the checksum. |
|||
return ~byteStream.Aggregate(0xFFFFFFFF, (checksumRegister, currentByte) => |
|||
(m_checksumTable[(checksumRegister & 0xFF) ^ Convert.ToByte(currentByte)] ^ (checksumRegister >> 8))); |
|||
} |
|||
catch (FormatException e) |
|||
{ |
|||
throw new CrcException("Could not read the stream out as bytes.", e); |
|||
} |
|||
catch (InvalidCastException e) |
|||
{ |
|||
throw new CrcException("Could not read the stream out as bytes.", e); |
|||
} |
|||
catch (OverflowException e) |
|||
{ |
|||
throw new CrcException("Could not read the stream out as bytes.", e); |
|||
} |
|||
} |
|||
#endregion |
|||
#region Fields |
|||
/// <summary> |
|||
/// Contains a cache of calculated checksum chunks. |
|||
/// </summary> |
|||
private readonly UInt32[] m_checksumTable; |
|||
#endregion |
|||
} |
|||
</lang> |
|||
Test: |
|||
<lang Csharp> |
|||
var arrayOfBytes = Encoding.ASCII.GetBytes("The quick brown fox jumps over the lazy dog"); |
|||
var crc32 = new Crc32(); |
|||
Console.WriteLine(crc32.Get(arrayOfBytes).ToString("X")); |
|||
</lang> |
|||
{{out}} |
|||
414fa339 |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 714: | Line 714: | ||
print "(Z8)", crc |
print "(Z8)", crc |
||
end program</lang> |
end program</lang> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|C}} |
{{trans|C}} |
||
Line 831: | Line 832: | ||
Testing: |
Testing: |
||
<lang Groovy>assert '414FA339' == sprintf('%04X', crc32('The quick brown fox jumps over the lazy dog'.bytes))</lang> |
<lang Groovy>assert '414FA339' == sprintf('%04X', crc32('The quick brown fox jumps over the lazy dog'.bytes))</lang> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 1,112: | Line 1,112: | ||
<pre>Message: The quick brown fox jumps over the lazy dog |
<pre>Message: The quick brown fox jumps over the lazy dog |
||
Checksum: 414fa339</pre> |
Checksum: 414fa339</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 1,222: | Line 1,221: | ||
put cx.cx_crc32_string("The quick brown fox jumps over the lazy dog") |
put cx.cx_crc32_string("The quick brown fox jumps over the lazy dog") |
||
-- "414fa339"</lang> |
-- "414fa339"</lang> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 1,359: | Line 1,357: | ||
{{out}} |
{{out}} |
||
<pre>414FA339</pre> |
<pre>414FA339</pre> |
||
=={{header|Objeck}}== |
|||
<lang objeck>class CRC32 { |
|||
function : Main(args : String[]) ~ Nil { |
|||
"The quick brown fox jumps over the lazy dog"->ToByteArray()->CRC32()->PrintLine(); |
|||
} |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
1095738169 |
|||
</pre> |
|||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Line 1,391: | Line 1,376: | ||
<pre> |
<pre> |
||
414FA339 |
414FA339 |
||
</pre> |
|||
=={{header|Objeck}}== |
|||
<lang objeck>class CRC32 { |
|||
function : Main(args : String[]) ~ Nil { |
|||
"The quick brown fox jumps over the lazy dog"->ToByteArray()->CRC32()->PrintLine(); |
|||
} |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
1095738169 |
|||
</pre> |
</pre> |
||
Line 1,451: | Line 1,449: | ||
{{out}} |
{{out}} |
||
<pre>The checksum is 414fa339</pre> |
<pre>The checksum is 414fa339</pre> |
||
=={{header|Perl 6}}== |
|||
=== Call to native function crc32 in zlib === |
|||
<lang perl6>use NativeCall; |
|||
sub crc32(int32 $crc, Buf $buf, int32 $len --> int32) is native('z') { * } |
|||
my $buf = 'The quick brown fox jumps over the lazy dog'.encode; |
|||
say crc32(0, $buf, $buf.bytes).fmt('%08x');</lang> |
|||
The libary name "z" resolves to <tt>/usr/lib/libz.so</tt> on a typical Linux system and <tt>/usr/lib/libz.dylib</tt> on Mac OS X, but may need to be changed for other platforms. Types may be platform-dependent as well. As written, the solution has been tested on Mac OS X 10.5.8 and Arch Linux 2016.08.01 x86_64. |
|||
{{out}} |
|||
<pre>414fa339</pre> |
|||
=== Pure Perl 6 === |
|||
A fairly generic implementation with no regard to execution speed: |
|||
<lang perl6>sub crc( |
|||
Blob $buf, |
|||
# polynomial including leading term, default: ISO 3309/PNG/gzip |
|||
:@poly = (1,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,1), |
|||
:$n = @poly.end, # degree of polynomial |
|||
:@init = 1 xx $n, # initial XOR bits |
|||
:@fini = 1 xx $n, # final XOR bits |
|||
:@bitorder = 0..7, # default: eat bytes LSB-first |
|||
:@crcorder = 0..$n-1, # default: MSB of checksum is coefficient of x⁰ |
|||
) { |
|||
my @bit = flat ($buf.list X+& (1 X+< @bitorder))».so».Int, 0 xx $n; |
|||
@bit[0 .. $n-1] «+^=» @init; |
|||
@bit[$_ ..$_+$n] «+^=» @poly if @bit[$_] for 0..@bit.end-$n; |
|||
@bit[*-$n.. *-1] «+^=» @fini; |
|||
:2[@bit[@bit.end X- @crcorder]]; |
|||
} |
|||
say crc('The quick brown fox jumps over the lazy dog'.encode('ascii')).base(16);</lang> |
|||
{{out}} |
|||
<pre>414FA339</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,549: | Line 1,503: | ||
<pre>414fa339</pre> |
<pre>414fa339</pre> |
||
=={{header|PicoLisp}}== |
|||
Library and implementation. |
|||
<lang PicoLisp>(setq *Table |
|||
(mapcar |
|||
'((N) |
|||
(do 8 |
|||
(setq N |
|||
(if (bit? 1 N) |
|||
(x| (>> 1 N) `(hex "EDB88320")) |
|||
(>> 1 N) ) ) ) ) |
|||
(range 0 255) ) ) |
|||
(de crc32 (Lst) |
|||
(let Crc `(hex "FFFFFFFF") |
|||
(for I (chop Lst) |
|||
(setq Crc |
|||
(x| |
|||
(get |
|||
*Table |
|||
(inc (x| (& Crc 255) (char I))) ) |
|||
(>> 8 Crc) ) ) ) |
|||
(x| `(hex "FFFFFFFF") Crc) ) ) |
|||
(let Str "The quick brown fox jumps over the lazy dog" |
|||
(println (hex (crc32 Str))) |
|||
(println |
|||
(hex (native "libz.so" "crc32" 'N 0 Str (length Str))) ) ) |
|||
(bye)</lang> |
|||
=={{header|Pike}}== |
|||
<lang Pike>string foo = "The quick brown fox jumps over the lazy dog"; |
|||
write("0x%x\n", Gz.crc32(foo));</lang> |
|||
{{Out}} |
|||
<pre> |
|||
0x414fa339 |
|||
</pre> |
|||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
Line 1,655: | Line 1,648: | ||
Generate CRC32 Checksum For Byte Array Example CRC_32=D1370232 |
Generate CRC32 Checksum For Byte Array Example CRC_32=D1370232 |
||
decimal 3510043186 |
decimal 3510043186 |
||
</pre> |
|||
=={{header|PicoLisp}}== |
|||
Library and implementation. |
|||
<lang PicoLisp>(setq *Table |
|||
(mapcar |
|||
'((N) |
|||
(do 8 |
|||
(setq N |
|||
(if (bit? 1 N) |
|||
(x| (>> 1 N) `(hex "EDB88320")) |
|||
(>> 1 N) ) ) ) ) |
|||
(range 0 255) ) ) |
|||
(de crc32 (Lst) |
|||
(let Crc `(hex "FFFFFFFF") |
|||
(for I (chop Lst) |
|||
(setq Crc |
|||
(x| |
|||
(get |
|||
*Table |
|||
(inc (x| (& Crc 255) (char I))) ) |
|||
(>> 8 Crc) ) ) ) |
|||
(x| `(hex "FFFFFFFF") Crc) ) ) |
|||
(let Str "The quick brown fox jumps over the lazy dog" |
|||
(println (hex (crc32 Str))) |
|||
(println |
|||
(hex (native "libz.so" "crc32" 'N 0 Str (length Str))) ) ) |
|||
(bye)</lang> |
|||
=={{header|Pike}}== |
|||
<lang Pike>string foo = "The quick brown fox jumps over the lazy dog"; |
|||
write("0x%x\n", Gz.crc32(foo));</lang> |
|||
{{Out}} |
|||
<pre> |
|||
0x414fa339 |
|||
</pre> |
</pre> |
||
Line 1,892: | Line 1,846: | ||
{{out}} |
{{out}} |
||
<pre>"414fa339"</pre> |
<pre>"414fa339"</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
=== Call to native function crc32 in zlib === |
|||
<lang perl6>use NativeCall; |
|||
sub crc32(int32 $crc, Buf $buf, int32 $len --> int32) is native('z') { * } |
|||
my $buf = 'The quick brown fox jumps over the lazy dog'.encode; |
|||
say crc32(0, $buf, $buf.bytes).fmt('%08x');</lang> |
|||
The libary name "z" resolves to <tt>/usr/lib/libz.so</tt> on a typical Linux system and <tt>/usr/lib/libz.dylib</tt> on Mac OS X, but may need to be changed for other platforms. Types may be platform-dependent as well. As written, the solution has been tested on Mac OS X 10.5.8 and Arch Linux 2016.08.01 x86_64. |
|||
{{out}} |
|||
<pre>414fa339</pre> |
|||
=== Pure Perl 6 === |
|||
A fairly generic implementation with no regard to execution speed: |
|||
<lang perl6>sub crc( |
|||
Blob $buf, |
|||
# polynomial including leading term, default: ISO 3309/PNG/gzip |
|||
:@poly = (1,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,1), |
|||
:$n = @poly.end, # degree of polynomial |
|||
:@init = 1 xx $n, # initial XOR bits |
|||
:@fini = 1 xx $n, # final XOR bits |
|||
:@bitorder = 0..7, # default: eat bytes LSB-first |
|||
:@crcorder = 0..$n-1, # default: MSB of checksum is coefficient of x⁰ |
|||
) { |
|||
my @bit = flat ($buf.list X+& (1 X+< @bitorder))».so».Int, 0 xx $n; |
|||
@bit[0 .. $n-1] «+^=» @init; |
|||
@bit[$_ ..$_+$n] «+^=» @poly if @bit[$_] for 0..@bit.end-$n; |
|||
@bit[*-$n.. *-1] «+^=» @fini; |
|||
:2[@bit[@bit.end X- @crcorder]]; |
|||
} |
|||
say crc('The quick brown fox jumps over the lazy dog'.encode('ascii')).base(16);</lang> |
|||
{{out}} |
|||
<pre>414FA339</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,208: | Line 2,207: | ||
puts [format "%x" [crc::crc32 $data]]</lang> |
puts [format "%x" [crc::crc32 $data]]</lang> |
||
With the same input data, it produces identical output. |
With the same input data, it produces identical output. |
||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
===Library=== |
===Library=== |