CRC-32: Difference between revisions

1,842 bytes added ,  1 month ago
m
→‎{{header|11l}}: `(-)` -> `~`
m (syntax highlighting fixup automation)
m (→‎{{header|11l}}: `(-)` -> `~`)
 
(11 intermediate revisions by 10 users not shown)
Line 33:
 
F crc32(buf, =crc = UInt32(0))
crc = (-)~crc
L(k) buf
crc = (crc >> 8) (+) :crc_table[(crc [&] F'F) (+) k.code]
R (-)~crc
 
print(hex(crc32(‘The quick brown fox jumps over the lazy dog’)))</syntaxhighlight>
Line 388:
{{out}}
<pre>0x414fa339</pre>
 
=={{header|Bait}}==
<syntaxhighlight lang="bait">
import hash.crc32
 
fun main() {
text := 'The quick brown fox jumps over the lazy dog'
sum := crc32.checksum(text.bytes())
println(sum.hex())
}
</syntaxhighlight>
 
{{out}}
<pre>
414fa339
</pre>
 
=={{header|C}}==
Line 543 ⟶ 559:
=={{header|C++}}==
 
<syntaxhighlight lang=cpp>#include <algorithmarray>
#include <arrayranges>
#include <cstdint>
#include <numeric>
#include <concepts>
#include <algorithm>
 
// These headers are only needed for main(), to demonstrate.
#include <iomanip>
#include <iostream>
#include <stringstring_view>
 
inline constexpr auto crc_table = []() {
// Generates a lookup table for the checksums of all 8-bit values.
std::array<std::uint_fast32_tuint32_t, 256> generate_crc_lookup_table() noexceptretval{};
std::generate(retval.begin(), retval.end(),
{
[n = std::uint32_t{ 0 }]() mutable {
auto const reversed_polynomial = std::uint_fast32_t{0xEDB88320uL};
auto c = n++;
for (std::uint8_t k = 0; k < 8; ++k) {
// This is a function object that calculates the checksum for a value,
if (c & 1) c = std::uint32_t{ 0xedb88320 } ^ (c >> 1);
// then increments the value, starting from zero.
else c >>= 1;
struct byte_checksum
}
{
return c;
std::uint_fast32_t operator()() noexcept
{ });
return retval;
auto checksum = static_cast<std::uint_fast32_t>(n++);
}();
for (auto i = 0; i < 8; ++i)
checksum = (checksum >> 1) ^ ((checksum & 0x1u) ? reversed_polynomial : 0);
return checksum;
}
unsigned n = 0;
};
auto table = std::array<std::uint_fast32_t, 256>{};
std::generate(table.begin(), table.end(), byte_checksum{});
return table;
}
 
 
// Calculates the CRC for any sequence of values. (You could use type traits and a
[[nodiscard]] constexpr std::uint32_t crc(const std::ranges::input_range auto& rng)
// static assert to ensure the values can be converted to 8 bits.)
noexcept requires std::convertible_to<std::ranges::range_value_t<decltype(rng)>, std::uint8_t> {
template <typename InputIterator>
return ~std::accumulate(std::ranges::begin(rng), std::ranges::end(rng),
std::uint_fast32_t crc(InputIterator first, InputIterator last)
~std::uint32_t{ 0 } & std::uint32_t{ 0xff'ff'ff'ffu },
{
[](std::uint32_t checksum, std::uint8_t value)
// Generate lookup table only on first use then cache it - this is thread-safe.
{ return crc_table[(checksum ^ value) & 0xff] ^ (checksum >> 8); });
static auto const table = generate_crc_lookup_table();
// Calculate the checksum - make sure to clip to 32 bits, for systems that don't
// have a true (fast) 32-bit type.
return std::uint_fast32_t{0xFFFFFFFFuL} &
~std::accumulate(first, last,
~std::uint_fast32_t{0} & std::uint_fast32_t{0xFFFFFFFFuL},
[](std::uint_fast32_t checksum, std::uint_fast8_t value)
{ return table[(checksum ^ value) & 0xFFu] ^ (checksum >> 8); });
}
 
int main() {
constexpr std::string_view str = "The quick brown fox jumps over the lazy dog";
{
auto const s = std::string{"The quick brown fox jumps over the lazy dog"};
std::cout << std::hex << std::setw(8) << std::setfill('0') << crc(s.begin(), s.end()str) << '\n';
}</syntaxhighlight>
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,197 ⟶ 1,191:
 
=={{header|Java}}==
<syntaxhighlight lang=Java>import java.util.zip.* ;
import java.util.zip.CRC32;
 
</syntaxhighlight>
public class CRCMaker {
<syntaxhighlight lang=Java>
public static void main( String[ ] args ) {
public static void main(String[] args) throws IOException {
String toBeEncoded = new String( "The quick brown fox jumps over the lazy dog" ) ;
String string = "The quick brown fox jumps over the lazy dog";
CRC32 myCRC = new CRC32( ) ;
CRC32 crc = new CRC32();
myCRC.update( toBeEncoded.getBytes( ) ) ;
crc.update(string.getBytes());
System.out.println( "The CRC-32 value is : " + Long.toHexString( myCRC.getValue( ) ) + " !" ) ;
System.out.printf("%x", crc.getValue());
}
}
}</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
<pre>The CRC-32 value is : 414fa339 !</pre>
414fa339
</pre>
 
=={{header|JavaScript}}==
Line 1,822 ⟶ 1,819:
printf("%0x\n", crc32(0, s, #s))
</syntaxhighlight>
 
Output:
<pre>414fa339</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang=pascal>
Program CheckCRC;
{$IFDEF fpc}{$mode Delphi}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils,crc;
function CrcString(const mystring: string) : longword;
var
crcvalue: longword;
begin
crcvalue := crc32(0,nil,0);
result := crc32(crcvalue, @mystring[1], length(mystring));
end;
 
var
mytext: string;
begin
myText := 'The quick brown fox jumps over the lazy dog';
writeln('crc32 = ', IntToHex(CrcString(mytext), 8));
end.</syntaxhighlight>
Output:
<pre>crc32 = 414FA339</pre>
 
=={{header|Perl}}==
Line 1,842 ⟶ 1,863:
=={{header|Phix}}==
Included as demo\rosetta\crc32.exw, which also includes a thread-safe version
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">table</span>
Line 2,393 ⟶ 2,414:
 
hex CRC-32 checksum = D1370232 dec CRC-32 checksum = 3510043186
</pre>
 
=={{header|RPL}}==
{{trans|FreeBASIC}}
≪ → string
≪ <span style="color:red">32</span> STWS <span style="color:grey">@ set binary word size to 32</span>
'''IFERR''' ‘<span style="color:green">CRCtable</span>’ RCL '''THEN'''
{ }
<span style="color:red">0 255</span> '''FOR''' j
j R→B
<span style="color:red">0 7</span> '''START'''
SR '''IF''' LAST <span style="color:red">#1</span> AND B→R '''THEN''' <span style="color:red">#EDB88320h</span> XOR '''END'''
'''NEXT''' + '''NEXT'''
‘<span style="color:green">CRCtable</span>’ STO
'''END'''
DROP <span style="color:red">#0</span> NOT
<span style="color:red">1</span> string SIZE '''FOR''' j
SRB SWAP
<span style="color:red">#FFh</span> AND string j DUP SUB NUM R→B XOR
B→R <span style="color:red">1</span> + ‘<span style="color:green">CRCtable</span>’ SWAP GET XOR
'''NEXT'''
NOT
≫ ≫ ‘<span style="color:blue">CRC32</span>’ STO
 
<span style="color:red">"The quick brown fox jumps over the lazy dog"</span> <span style="color:blue">CRC32</span>
{{out}}
<pre>
1: # 414FA339h
</pre>
 
Line 2,945 ⟶ 2,994:
Crc32 = 414FA339
Crc32 = 414FA339</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import hash.crc32
 
fn main() {
text := "The quick brown fox jumps over the lazy dog"
result := crc32.sum(text.bytes())
println(result.hex())
}
</syntaxhighlight>
 
{{out}}
<pre>
414fa339
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang=ecmascript"wren">import "./fmt" for Conv
 
class CRC32 {
Line 3,018 ⟶ 3,082:
ZLib.calcCRC32(Data(Void,"The quick brown fox jumps over the lazy dog"));
//-->0x414fa339</syntaxhighlight>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
const Crc32Ieee = std.hash.Crc32;
 
pub fn main() !void {
var res: u32 = Crc32Ieee.hash("The quick brown fox jumps over the lazy dog");
std.debug.print("{x}\n", .{res});
}
</syntaxhighlight>
 
{{out}}
<pre>
414fa339
</pre>
1,453

edits