Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,3 +1,10 @@
|
|||
Demonstrate a method of deriving the [[wp:Cyclic_Redundancy_Check|Cyclic Redundancy Check]] from within the language. The result should be in accordance with ISO 3309, [http://www.itu.int/rec/T-REC-V.42-200203-I/en ITU-T V.42], [http://tools.ietf.org/html/rfc1952 Gzip] and [http://www.w3.org/TR/2003/REC-PNG-20031110/ PNG]. Algorithms are described on [[wp:Computation of CRC|Computation of CRC]] in Wikipedia. This variant of CRC-32 uses LSB-first order, sets the initial CRC to FFFFFFFF<sub>16</sub>, and complements the final CRC.
|
||||
{{omit from|GUISS}}
|
||||
{{omit from|Lilypond}}
|
||||
{{omit from|TPP}}
|
||||
|
||||
Demonstrate a method of deriving the [[wp:Cyclic_Redundancy_Check|Cyclic Redundancy Check]] from within the language. <br>
|
||||
The result should be in accordance with ISO 3309, [http://www.itu.int/rec/T-REC-V.42-200203-I/en ITU-T V.42], [http://tools.ietf.org/html/rfc1952 Gzip] and [http://www.w3.org/TR/2003/REC-PNG-20031110/ PNG]. <br>
|
||||
Algorithms are described on [[wp:Computation of CRC|Computation of CRC]] in Wikipedia.
|
||||
This variant of CRC-32 uses LSB-first order, sets the initial CRC to FFFFFFFF<sub>16</sub>, and complements the final CRC.
|
||||
|
||||
For the purpose of this task, generate a CRC-32 checksum for the ASCII encoded string "The quick brown fox jumps over the lazy dog" (without quotes).
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Checksums
|
||||
note: Checksums
|
||||
|
|
|
|||
|
|
@ -1,49 +1,61 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <numeric>
|
||||
|
||||
class CRC32
|
||||
// These headers are only needed for main(), to demonstrate.
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
// Generates a lookup table for the checksums of all 8-bit values.
|
||||
std::array<std::uint_fast32_t, 256> generate_crc_lookup_table() noexcept
|
||||
{
|
||||
public:
|
||||
CRC32()
|
||||
auto const reversed_polynomial = std::uint_fast32_t{0xEDB88320uL};
|
||||
|
||||
// This is a function object that calculates the checksum for a value,
|
||||
// then increments the value, starting from zero.
|
||||
struct byte_checksum
|
||||
{
|
||||
std::uint_fast32_t operator()() noexcept
|
||||
{
|
||||
generateTable();
|
||||
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;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
uint32_t get( T begin, T end )
|
||||
{
|
||||
uint32_t nCRC = ~static_cast<uint32_t>(0);
|
||||
return ~std::accumulate( begin, end, 0xFFFFFFFF, [&](uint32_t nCRC, uint32_t nVal)
|
||||
{ return (nCRC >> 8) ^ m_pTable[(nCRC & 0xff) ^ nVal]; } );
|
||||
}
|
||||
unsigned n = 0;
|
||||
};
|
||||
|
||||
private:
|
||||
void generateTable()
|
||||
{
|
||||
int nCount = 0;
|
||||
// fill the table with 0..255
|
||||
std::generate( m_pTable.begin(), m_pTable.end(), [&nCount](){ return nCount++; } );
|
||||
auto table = std::array<std::uint_fast32_t, 256>{};
|
||||
std::generate(table.begin(), table.end(), byte_checksum{});
|
||||
|
||||
// calculate the crc table
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
std::transform( m_pTable.begin(), m_pTable.end(), m_pTable.begin(),
|
||||
[] ( uint32_t &nValue ) { return (nValue>>1)^((nValue&1)*0xedb88320); } );
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<uint32_t, 256> m_pTable;
|
||||
};
|
||||
// Calculates the CRC for any sequence of values. (You could use type traits and a
|
||||
// static assert to ensure the values can be converted to 8 bits.)
|
||||
template <typename InputIterator>
|
||||
std::uint_fast32_t crc(InputIterator first, InputIterator last)
|
||||
{
|
||||
// Generate lookup table only on first use then cache it - this is thread-safe.
|
||||
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()
|
||||
{
|
||||
CRC32 oCrc;
|
||||
std::string str( "The quick brown fox jumps over the lazy dog" );
|
||||
std::cout << "Checksum: " << std::hex << oCrc.get( str.begin(), str.end() ) << std::endl;
|
||||
return 0;
|
||||
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()) << '\n';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio, std.digest.crc;
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.digest.crc;
|
||||
|
||||
"The quick brown fox jumps over the lazy dog"
|
||||
.crc32Of().crcHexString().writeln();
|
||||
.crc32Of.crcHexString.writeln;
|
||||
}
|
||||
|
|
|
|||
11
Task/CRC-32/Oberon-2/crc-32.oberon-2
Normal file
11
Task/CRC-32/Oberon-2/crc-32.oberon-2
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
MODULE CRC32;
|
||||
IMPORT
|
||||
NPCT:Zlib,
|
||||
Strings,
|
||||
Out;
|
||||
VAR
|
||||
s: ARRAY 128 OF CHAR;
|
||||
BEGIN
|
||||
COPY("The quick brown fox jumps over the lazy dog",s);
|
||||
Out.Hex(Zlib.CRC32(0,s,0,Strings.Length(s)),0);Out.Ln
|
||||
END CRC32.
|
||||
27
Task/CRC-32/PicoLisp/crc-32.l
Normal file
27
Task/CRC-32/PicoLisp/crc-32.l
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(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)
|
||||
|
|
@ -1,34 +1,37 @@
|
|||
/*REXX program computes the CRC-32 (32 bit Cylic Redundancy Check), */
|
||||
/* checksum for a given string [as described in ISO 3309, ITU-T V.42].*/
|
||||
call show "The quick brown fox jumps over the lazy dog"
|
||||
call show 'Generate CRC32 Checksum For Byte Array Example'
|
||||
/*──────────────────────────────────────────────────────────────────────*/
|
||||
call show 'The quick brown fox jumps over the lazy dog' /*1st str*/
|
||||
call show 'Generate CRC32 Checksum For Byte Array Example' /*2nd str*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SHOW subroutine─────────────────────*/
|
||||
show: procedure; parse arg Xstring; numeric digits 12; say; say
|
||||
checksum=CRC_32(Xstring) /*invoke CRC_32 to create a CRC*/
|
||||
say center(' input string [length of' length(Xstring) "bytes] ", 79, '─')
|
||||
say Xstring /*show the string on its own line*/
|
||||
say
|
||||
checksum=bitxor(checksum,'ffFFffFF'x) /*final convolution for checksum.*/
|
||||
say 'hex CRC-32 checksum =' c2x(checksum) left('',15),
|
||||
"dec CRC-32 checksum =" c2d(checksum) /*show CRC-32 in hex & dec*/
|
||||
return
|
||||
/*──────────────────────────────────CRC_32 subroutine───────────────────*/
|
||||
CRC_32: procedure; parse arg !,$ /*2nd arg is for multi-invokes. */
|
||||
CRC_32: procedure; parse arg !,$ /*2nd arg: repeated─invocations.*/
|
||||
/* [↓] build 8─bit indexed table*/
|
||||
do i=0 for 256; z=d2c(i) /* one byte at a time. */
|
||||
r=right(z, 4, '0'x) /*insure the "R" is 32 bits. */
|
||||
|
||||
do i=0 for 256; z=d2c(i) /*build the 8-bit indexed table.*/
|
||||
r=right(z,4,'0'x) /*insure the R is 32 bits. */
|
||||
do j=0 for 8 /*handle each bit of rightmost 8.*/
|
||||
rb=x2b(c2x(r)) /*convert char ──► hex ──► binary*/
|
||||
_=right(rb,1) /*remember right-most bit for IF.*/
|
||||
r=x2c(b2x(0 || left(rb,31))) /*shift it right (unsigned) 1 bit*/
|
||||
if _\==0 then r=bitxor(r,'edb88320'x) /*bit XOR grunt-work.*/
|
||||
end /*j*/
|
||||
!.z=r
|
||||
end /*i*/
|
||||
do j=0 for 8 /*handle each bit of rightmost 8.*/
|
||||
rb=x2b(c2x(r)) /*convert char ──► hex ──► binary*/
|
||||
_=right(rb,1) /*remember right-most bit for IF.*/
|
||||
r=x2c(b2x(0 || left(rb, 31))) /*shift it right (unsigned) 1 bit*/
|
||||
if _\==0 then r=bitxor(r, 'edb88320'x) /*bit XOR grunt─work.*/
|
||||
end /*j*/
|
||||
!.z=r /*assign to an 8─bit index table.*/
|
||||
end /*i*/
|
||||
|
||||
$=bitxor(word($ '0000000'x,1),'ffFFffFF'x) /*use user's CRC or default.*/
|
||||
do k=1 for length(!) /*start crunching the input data.*/
|
||||
?=bitxor(right($,1),substr(!,k,1)); $=bitxor('0'x||left($,3),!.?)
|
||||
end /*k*/
|
||||
?=bitxor(right($, 1), substr(!, k, 1))
|
||||
$=bitxor('0'x || left($, 3), !.?)
|
||||
end /*k*/
|
||||
return $ /*return with da money to invoker*/
|
||||
/*──────────────────────────────────SHOW subroutine─────────────────────*/
|
||||
show: procedure; parse arg Xstring; numeric digits 12; say; say
|
||||
checksum=CRC_32(Xstring) /*invoke CRC_32 to create a CRC*/
|
||||
checksum=bitxor(checksum,'ffFFffFF'x) /*final convolution for checksum.*/
|
||||
say center(' input string [length of' length(Xstring) "bytes] ", 79, '═')
|
||||
say Xstring /*show the string on its own line*/
|
||||
say /* ↓↓↓↓↓↓↓↓↓↓↓↓ is 15 blanks.*/
|
||||
say 'hex CRC-32 checksum =' c2x(checksum) left('', 15),
|
||||
"dec CRC-32 checksum =" c2d(checksum) /*show CRC-32 in hex & dec.*/
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue