This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,49 @@
#include <string>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <array>
#include <cstdint>
class CRC32
{
public:
CRC32()
{
generateTable();
}
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]; } );
}
private:
void generateTable()
{
int nCount = 0;
// fill the table with 0..255
std::generate( m_pTable.begin(), m_pTable.end(), [&nCount](){ return nCount++; } );
// 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); } );
}
}
private:
std::array<uint32_t, 256> m_pTable;
};
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;
}

View file

@ -0,0 +1,13 @@
#include <boost\crc.hpp>
#include <string>
#include <iostream>
int main()
{
std::string str( "The quick brown fox jumps over the lazy dog" );
boost::crc_32_type crc;
crc.process_bytes( str.data(), str.size() );
std::cout << "Checksum: " << std::hex << crc.checksum() << std::endl;
return 0;
}

View file

@ -7,7 +7,8 @@ rc_crc32(uint32_t crc, const char *buf, size_t len)
{
static uint32_t table[256];
static int have_table = 0;
uint32_t rem, octet;
uint32_t rem;
uint8_t octet;
int i, j;
const char *p, *q;

View file

@ -0,0 +1,4 @@
(let [crc (new java.util.zip.CRC32)
str "The quick brown fox jumps over the lazy dog"]
(. crc update (. str getBytes))
(printf "CRC-32('%s') = %s\n" str (Long/toHexString (. crc getValue))))

View file

@ -0,0 +1,12 @@
MODULE BbtComputeCRC32;
IMPORT ZlibCrc32,StdLog;
PROCEDURE Do*;
VAR
s: ARRAY 128 OF SHORTCHAR;
BEGIN
s := "The quick brown fox jumps over the lazy dog";
StdLog.IntForm(ZlibCrc32.CRC32(0,s,0,LEN(s$)),16,12,'0',TRUE);
StdLog.Ln;
END Do;
END BbtComputeCRC32.

View file

@ -0,0 +1,98 @@
*process source attributes xref or(!) nest;
crct: Proc Options(main);
/*********************************************************************
* 19.08.2013 Walter Pachl derived from REXX
*********************************************************************/
Dcl (LEFT,LENGTH,RIGHT,SUBSTR,UNSPEC) Builtin;
Dcl SYSPRINT Print;
dcl tab(0:255) Bit(32);
Call mk_tab;
Call crc_32('The quick brown fox jumps over the lazy dog');
Call crc_32('Generate CRC32 Checksum For Byte Array Example');
crc_32: Proc(s);
/*********************************************************************
* compute checksum for s
*********************************************************************/
Dcl s Char(*);
Dcl d Bit(32);
Dcl d1 Bit( 8);
Dcl d2 Bit(24);
Dcl cc Char(1);
Dcl ccb Bit(8);
Dcl tib Bit(8);
Dcl ti Bin Fixed(16) Unsigned;
Dcl k Bin Fixed(16) Unsigned;
d=(32)'1'b;
Do k=1 To length(s);
d1=right(d,8);
d2=left(d,24);
cc=substr(s,k,1);
ccb=unspec(cc);
tib=d1^ccb;
Unspec(ti)=tib;
d='00000000'b!!d2^tab(ti);
End;
d=d^(32)'1'b;
Put Edit(s,'CRC_32=',b2x(d))(Skip,a(50),a,a);
Put Edit('decimal ',b2d(d))(skip,x(49),a,f(10));
End;
b2x: proc(b) Returns(char(8));
dcl b bit(32);
dcl b4 bit(4);
dcl i Bin Fixed(31);
dcl r Char(8) Var init('');
Do i=1 To 29 By 4;
b4=substr(b,i,4);
Select(b4);
When('0000'b) r=r!!'0';
When('0001'b) r=r!!'1';
When('0010'b) r=r!!'2';
When('0011'b) r=r!!'3';
When('0100'b) r=r!!'4';
When('0101'b) r=r!!'5';
When('0110'b) r=r!!'6';
When('0111'b) r=r!!'7';
When('1000'b) r=r!!'8';
When('1001'b) r=r!!'9';
When('1010'b) r=r!!'A';
When('1011'b) r=r!!'B';
When('1100'b) r=r!!'C';
When('1101'b) r=r!!'D';
When('1110'b) r=r!!'E';
When('1111'b) r=r!!'F';
End;
End;
Return(r);
End;
b2d: Proc(b) Returns(Dec Fixed(15));
Dcl b Bit(32);
Dcl r Dec Fixed(15) Init(0);
Dcl i Bin Fixed(16);
Do i=1 To 32;
r=r*2
If substr(b,i,1) Then
r=r+1;
End;
Return(r);
End;
mk_tab: Proc;
dcl b32 bit(32);
dcl lb bit( 1);
dcl ccc bit(32) Init('edb88320'bx);
dcl (i,j) Bin Fixed(15);
Do i=0 To 255;
b32=(24)'0'b!!unspec(i);
Do j=0 To 7;
lb=right(b32,1);
b32='0'b!!left(b32,31);
If lb='1'b Then
b32=b32^ccc;
End;
tab(i)=b32;
End;
End;
End;

View file

@ -1,5 +1,5 @@
sub crc(
Buf $buf,
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

View file

@ -1,37 +1,34 @@
/*REXX program to compute the CRC-32 (Cylic Redundancy Check, 32 bit),*/
/*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].*/
a_string = 'The quick brown fox jumps over the lazy dog'
b_string = 'Generate CRC32 Checksum For Byte Array Example'
call show a_string
call show b_string
call show "The quick brown fox jumps over the lazy dog"
call show 'Generate CRC32 Checksum For Byte Array Example'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show: procedure; parse arg Xstring; say
show: procedure; parse arg Xstring; numeric digits 12; say; say
checksum=CRC_32(Xstring) /*invoke CRC_32 to create a CRC*/
say
say center(' input string [length of' length(Xstring) "bytes] ",79,'')
say Xstring /*show the string on its own line*/
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*/
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 is for multi-invokes. */
do i=0 to 255; z=d2c(i) /*build the 8-bit indexed table. */
do i=0 to 255; 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*/
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*/
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*/
end /*k*/
return $ /*return with da money to invoker*/

View file

@ -0,0 +1,4 @@
import java.util.zip.CRC32
val crc=new CRC32
crc.update("The quick brown fox jumps over the lazy dog".getBytes)
println(crc.getValue.toHexString) //> 414fa339