Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,54 @@
[0:255]BITS crc_table;
BOOL crc_table_computed := FALSE;
PROC make_crc_table = VOID:
BEGIN
INT n, k;
FOR n FROM 0 TO 255 DO
BITS c := BIN n;
FOR k TO 8 DO
c := IF 32 ELEM c THEN
16redb88320 XOR (c SHR 1)
ELSE
c SHR 1
FI
OD;
crc_table[n] := c
OD;
crc_table_computed := TRUE
END;
PROC update_crc = (BITS crc, STRING buf) BITS:
BEGIN
BITS c := crc XOR 16rffffffff;
INT n;
IF NOT crc_table_computed THEN make_crc_table FI;
FOR n TO UPB buf DO
c := crc_table[ABS ((c XOR BIN ABS buf[n]) AND 16rff)] XOR (c SHR 8)
OD ;
c XOR 16rffffffff
END;
PROC hex = (BITS x) STRING :
BEGIN
PROC hexdig = (BITS x) CHAR: REPR (IF ABS x ≤ 9 THEN ABS x + ABS "0"
ELSE ABS x - 10 + ABS "a"
FI);
STRING h := "";
IF x = 16r0 THEN
h := "0"
ELSE
BITS n := x;
WHILE h := hexdig (n AND 16rf) + h; n ≠ 16r0 DO
n := n SHR 4
OD
FI;
h
END;
PROC crc = (STRING buf) BITS:
update_crc(16r0, buf);
STRING s = "The quick brown fox jumps over the lazy dog";
print(("CRC32 OF ", s, " is: ", hex (crc (s)), newline))

View file

@ -0,0 +1,13 @@
crc32 = do ->
table =
for n in [0..255]
for [0..7]
if n & 1
n = 0xEDB88320 ^ n >>> 1
else
n >>>= 1
n
(str, crc = -1) ->
for c in str
crc = crc >>> 8 ^ table[(crc ^ c.charCodeAt 0) & 255]
(crc ^ -1) >>> 0

View file

@ -0,0 +1 @@
console.log (crc32 'The quick brown fox jumps over the lazy dog').toString 16

View file

@ -0,0 +1 @@
414fa339

View file

@ -0,0 +1,7 @@
defmodule Test do
def crc32(str) do
IO.puts :erlang.crc32(str) |> Integer.to_string(16)
end
end
Test.crc32("The quick brown fox jumps over the lazy dog")

View file

@ -0,0 +1,32 @@
function crc32(crc::Int, str::ASCIIString)
table = zeros(Uint32, 256)
for i=0:255
temp = i
for j=0:7
if temp & 1 == 1
temp >>= 1
temp $= 0xedb88320
else
temp >>= 1
end
end
table[i + 1] = temp
end
crc $= 0xffffffff
for i in map(Uint32, collect(str))
crc = (crc >> 8) $ table[(crc & 0xff) $ i + 1]
end
crc $ 0xffffffff
end
str = "The quick brown fox jumps over the lazy dog"
crc = crc32(0, str)
assert(crc == 0x414fa339)
println("Message: ", str)
println("Checksum: ", hex(crc))

View file

@ -1,37 +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' /*1st str*/
call show 'Generate CRC32 Checksum For Byte Array Example' /*2nd str*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────CRC_32 subroutine───────────────────*/
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. */
/*REXX program computes the CRC─32 (32 bit Cyclic Redundancy Check) checksum*/
/*─────────────────for a given string [as described in ISO 3309, ITU─T V.42].*/
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.*/
call show 'The quick brown fox jumps over the lazy dog' /*1st string.*/
call show 'Generate CRC32 Checksum For Byte Array Example' /*2nd " */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
CRC_32: procedure; parse arg !,$ /*2nd arg: it has repeated─invocations.*/
/* [↓] build an 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 thirty-two bits.*/
do j=0 for 8 /*handle each bit of rightmost 8 bits. */
rb=x2b(c2x(r)) /*convert character ──► hex ──► binary.*/
_=right(rb,1) /*remember the right─most bit for IF. */
r=x2c(b2x(0 || left(rb, 31))) /*shift it right (an unsigned) 1 bit.*/
if _\==0 then r=bitxor(r, 'edb88320'x) /*this is bit XOR grunt─work.*/
end /*j*/
!.z=r /*assign to an 8─bit index table.*/
!.z=r /*assign to an eight─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(word($ '0000000'x,1),'ffFFffFF'x) /*use the user's CRC or a default.*/
do k=1 for length(!) /*start crunching the input data. */
?=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.*/
return $ /*return with da money to the invoker. */
/*────────────────────────────────────────────────────────────────────────────*/
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.*/
say Xstring /*show the string on its own line*/
say /*↓↓↓↓↓↓↓↓↓↓↓↓ is fifteen blanks*/
say 'hex CRC-32 checksum =' c2x(checksum) left('', 15),
"dec CRC-32 checksum =" c2d(checksum) /*show the CRC-32 in hex and dec.*/
return