crc32_hash Interface

public interface crc32_hash

Module Procedures

private function crc32_hash_arr(anything, continue) result(crc_64)

NAME

crc32_hash(3f) - [M_hashkeys] CRC (Cyclic Redundancy Check)
(LICENSE:PD)

SYNOPSIS

function crc32_hash(a,continue) result (crc)

class(*),intent(in)          :: anything(:)
logical,intent(in),optional  :: continue
integer(int64)               :: crc_out

DESCRIPTION

This ia 32-bit version of the Cyclic Redundancy Check(CRC).
This variant of CRC-32 uses LSB-first order, sets the initial CRC to
FFFFFFFF_int32, and complements the final CRC.

The result should be in accordance with ISO 3309, ITU-T V.42, Gzip
and PNG.

OPTIONS

anything  input value to generate a CRC check for. May be a array
          or scalar of numeric or string values of type CHARACTER,
          int8, int16, int32, int64, real32, real64, real128
continue  optional parameter. If not present or .F. starts new
          CRC sum. If .T. continues a CRC starting with last CRC
          calculated.

RETURNS

crc       The calculated CRC sum. It is calculated as a 32-bit value
          but returned as a 64-bit value, as Fortran does not
          currently support unsigned integers.

REFERENCES

Algorithms are described in "Computation of CRC" in Wikipedia.
Also see

   https://en.wikipedia.org/wiki/Cyclic_redundancy_check

AUTHOR

This was derived from an unattributed example on http://rosettacode.org,
but has been modified.

EXAMPLE

Sample program:

program demo_crc32_hash
use,intrinsic :: ISO_FORTRAN_ENV, only : int64
use M_hashkeys, only : crc32_hash
implicit none
integer :: i
integer(int64) :: crc
character(*), parameter :: s = "The quick brown fox jumps over the lazy dog"
   ! string
   crc=crc32_hash(s)
   print "(Z8)", crc
   print "(i0)", crc
   ! character array
   print "(i0)", crc32_hash([ &
           & 'T','h','e',' ',&
           & 'q','u','i','c','k',' ',&
           & 'b','r','o','w','n',' ',&
           & 'f','o','x',' '])
   print "(i0)", crc32_hash([ &
           & 'j','u','m','p','s',' ',&
           & 'o','v','e','r',' ',&
           & 't','h','e',' ',&
           & 'l','a','z','y',' ',&
           & 'd','o','g'],continue=.true.)
   ! numeric array
   print "(i0)", crc32_hash([(i,i=1,100)])
end program demo_crc32_hash

Expected output:

414FA339
1095738169
2293265890
1095738169
1783575711

Arguments

Type IntentOptional Attributes Name
class(*), intent(in) :: anything(:)
logical, intent(in), optional :: continue

Return Value integer(kind=int64)

private function crc32_hash_scalar(anything, continue) result(hash_64)

Arguments

Type IntentOptional Attributes Name
class(*), intent(in) :: anything
logical, intent(in), optional :: continue

Return Value integer(kind=int64)