2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -2,9 +2,16 @@
{{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.
;Task:
Demonstrate a method of deriving the [[wp:Computation of cyclic redundancy checks|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:Cyclic redundancy check|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).
For the purpose of this task, generate a CRC-32 checksum for the ASCII encoded string:
:: <big><big><code>The quick brown fox jumps over the lazy dog</code></big></big>
<br><br>

View file

@ -0,0 +1,9 @@
CRC32(str, enc = "UTF-8")
{
l := (enc = "CP1200" || enc = "UTF-16") ? 2 : 1, s := (StrPut(str, enc) - 1) * l
VarSetCapacity(b, s, 0) && StrPut(str, &b, floor(s / l), enc)
CRC32 := DllCall("ntdll.dll\RtlComputeCrc32", "UInt", 0, "Ptr", &b, "UInt", s)
return Format("{:#x}", CRC32)
}
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")

View file

@ -0,0 +1,16 @@
CRC32(str)
{
static table := []
loop 256 {
crc := A_Index - 1
loop 8
crc := (crc & 1) ? (crc >> 1) ^ 0xEDB88320 : (crc >> 1)
table[A_Index - 1] := crc
}
crc := ~0
loop, parse, str
crc := table[(crc & 0xFF) ^ Asc(A_LoopField)] ^ (crc >> 8)
return Format("{:#x}", ~crc)
}
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")

View file

@ -1,19 +0,0 @@
str := "The quick brown fox jumps over the lazy dog"
MsgBox, % "String:`n" (str) "`n`nCRC32:`n" CRC32(str)
; CRC32 =============================================================================
CRC32(string, encoding = "UTF-8")
{
chrlength := (encoding = "CP1200" || encoding = "UTF-16") ? 2 : 1
length := (StrPut(string, encoding) - 1) * chrlength
VarSetCapacity(data, length, 0)
StrPut(string, &data, floor(length / chrlength), encoding)
SetFormat, Integer, % SubStr((A_FI := A_FormatInteger) "H", 0)
CRC32 := DllCall("NTDLL\RtlComputeCrc32", "UInt", 0, "UInt", &data, "UInt", length, "UInt")
CRC := SubStr(CRC32 | 0x1000000000, -7)
DllCall("User32.dll\CharLower", "Str", CRC)
SetFormat, Integer, %A_FI%
return CRC
}

View file

@ -0,0 +1,37 @@
*> tectonics: cobc -xj crc32-zlib.cob -lz
identification division.
program-id. rosetta-crc32.
environment division.
configuration section.
repository.
function all intrinsic.
data division.
working-storage section.
01 crc32-initial usage binary-c-long.
01 crc32-result usage binary-c-long unsigned.
01 crc32-input.
05 value "The quick brown fox jumps over the lazy dog".
01 crc32-hex usage pointer.
procedure division.
crc32-main.
*> libz crc32
call "crc32" using
by value crc32-initial
by reference crc32-input
by value length(crc32-input)
returning crc32-result
on exception
display "error: no crc32 zlib linkage" upon syserr
end-call
call "printf" using "checksum: %lx" & x"0a" by value crc32-result
*> GnuCOBOL pointers are displayed in hex by default
set crc32-hex up by crc32-result
display 'crc32 of "' crc32-input '" is ' crc32-hex
goback.
end program rosetta-crc32.

View file

@ -0,0 +1,11 @@
: crc/ ( n -- n ) 8 0 do dup 1 rshift swap 1 and if $edb88320 xor then loop ;
: crcfill 256 0 do i crc/ , loop ;
create crctbl crcfill
: crc+ ( crc n -- crc' ) over xor $ff and cells crctbl + @ swap 8 rshift xor ;
: crcbuf ( crc str len -- crc ) bounds ?do i c@ crc+ loop ;
$ffffffff s" The quick brown fox jumps over the lazy dog" crcbuf $ffffffff xor hex. bye \ $414FA339

View file

@ -0,0 +1,45 @@
module crc32_m
use iso_fortran_env
implicit none
integer(int32) :: crc_table(0:255)
contains
subroutine update_crc(a, crc)
integer :: n, i
character(*) :: a
integer(int32) :: crc
crc = not(crc)
n = len(a)
do i = 1, n
crc = ieor(shiftr(crc, 8), crc_table(iand(ieor(crc, iachar(a(i:i))), 255)))
end do
crc = not(crc)
end subroutine
subroutine init_table
integer :: i, j
integer(int32) :: k
do i = 0, 255
k = i
do j = 1, 8
if (btest(k, 0)) then
k = ieor(shiftr(k, 1), -306674912)
else
k = shiftr(k, 1)
end if
end do
crc_table(i) = k
end do
end subroutine
end module
program crc32
use crc32_m
implicit none
integer(int32) :: crc = 0
character(*), parameter :: s = "The quick brown fox jumps over the lazy dog"
call init_table
call update_crc(s, crc)
print "(Z8)", crc
end program

View file

@ -0,0 +1,3 @@
install("crc32", "lLsL", "crc32", "libz.so");
s = "The quick brown fox jumps over the lazy dog";
printf("%0x\n", crc32(0, s, #s))

View file

@ -1,6 +1,6 @@
use NativeCall;
sub crc32(int32 $crc, Str $buf, int32 $len --> int32) is native('/usr/lib/libz.dylib') { * }
sub crc32(int32 $crc, Buf $buf, int32 $len --> int32) is native('z') { * }
my $buf = 'The quick brown fox jumps over the lazy dog';
say crc32(0, $buf, $buf.chars).fmt('%08x');
my $buf = 'The quick brown fox jumps over the lazy dog'.encode;
say crc32(0, $buf, $buf.bytes).fmt('%08x');

View file

@ -8,7 +8,7 @@ sub crc(
:@bitorder = 0..7, # default: eat bytes LSB-first
:@crcorder = 0..$n-1, # default: MSB of checksum is coefficient of x⁰
) {
my @bit = ($buf.list X+& (1 X+< @bitorder))».so».Int, 0 xx $n;
my @bit = flat ($buf.list X+& (1 X+< @bitorder).list)».so».Int, 0 xx $n;
@bit[0 .. $n-1] «+^=» @init;
@bit[$_ ..$_+$n] «+^=» @poly if @bit[$_] for 0..@bit.end-$n;

View file

@ -0,0 +1,10 @@
a$="The quick brown fox jumps over the lazy dog"
UseCRC32Fingerprint() : b$=StringFingerprint(a$, #PB_Cipher_CRC32)
OpenConsole()
PrintN("CRC32 Cecksum [hex] = "+UCase(b$))
PrintN("CRC32 Cecksum [dec] = "+Val("$"+b$))
Input()
End

View file

@ -1,3 +1,8 @@
>>> s = 'The quick brown fox jumps over the lazy dog'
>>> import zlib
>>> hex(zlib.crc32('The quick brown fox jumps over the lazy dog'))
>>> hex(zlib.crc32(s))
'0x414fa339'
>>> import binascii
>>> hex(binascii.crc32(s))
'0x414fa339'

View file

@ -1,3 +1,19 @@
>>> import binascii
>>> hex(binascii.crc32('The quick brown fox jumps over the lazy dog'))
'0x414fa339'
def create_table():
a = []
for i in range(256):
k = i
for j in range(8):
if k & 1:
k ^= 0x1db710640
k >>= 1
a.append(k)
return a
def crc_update(buf, crc):
crc ^= 0xffffffff
for k in buf:
crc = (crc >> 8) ^ crc_table[(crc & 0xff) ^ k]
return crc ^ 0xffffffff
crc_table = create_table()
print(hex(crc_update(b"The quick brown fox jumps over the lazy dog", 0)))

View file

@ -1,37 +1,34 @@
/*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].*/
/*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].*/
call show 'The quick brown fox jumps over the lazy dog' /*the 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 !,$; c='edb88320'x /*2nd arg used for repeated invocations*/
f='ffFFffFF'x /* [↓] 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.*/
/* [↓] handle each rightmost byte bit.*/
do j=0 for 8; rb=x2b(c2x(r)) /*handle each bit of rightmost 8 bits. */
r=x2c(b2x(0 || left(rb, 31))) /*shift it right (an unsigned) 1 bit.*/
if right(rb,1) then r=bitxor(r,c) /*this is a bin bit for XOR grunt─work.*/
end /*j*/
!.z=r /*assign to an eight─bit index table. */
end /*i*/
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 eight─bit index table. */
end /*i*/
$=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 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 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
$=bitxor(word($ '0000000'x, 1), f) /*utilize the user's CRC or a default. */
do k=1 for length(!) /*start number crunching the input data*/
?=bitxor(right($,1), substr(!,k,1))
$=bitxor('0'x || left($, 3), !.?)
end /*k*/
return $ /*return with cyclic redundancy check. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 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

View file

@ -0,0 +1,26 @@
fn crc32_compute_table() -> [u32; 256] {
let mut crc32_table = [0; 256];
for n in 0..256 {
crc32_table[n as usize] = (0..8).fold(n as u32, |acc, _| {
match acc & 1 {
1 => 0xedb88320 ^ (acc >> 1),
_ => acc >> 1,
}
});
}
crc32_table
}
fn crc32(buf: &str) -> u32 {
let crc_table = crc32_compute_table();
!buf.bytes().fold(!0, |acc, octet| {
(acc >> 8) ^ crc_table[((acc & 0xff) ^ octet as u32) as usize]
})
}
fn main() {
println!("{:x}", crc32("The quick brown fox jumps over the lazy dog"));
}