{-# LANGUAGE Safe #-}
{- arch-tag: GZIP CRC32 implementation in pure Haskell
Copyright (c) 2004-2011 John Goerzen <jgoerzen@complete.org>

All rights reserved.

For license and copyright information, see the file LICENSE
-}

{- |
   Module     : Data.Hash.CRC32.GZip
   Copyright  : Copyright (C) 2004-2011 John Goerzen
   SPDX-License-Identifier: BSD-3-Clause

   Stability  : stable
   Portability: portable

CRC32 checksumming using the GZIP\/PKZIP algorithm as used in both
ISO 3309 and section 8.1.1.6.2 of ITU-T V.42 and referenced in
RFC1952.

-}

module Data.Hash.CRC32.GZip where

import safe Data.Array ( Array, array, (!) )
import safe Data.Bits ( Bits(xor, (.&.), shiftR) )
import safe Data.Char ( ord )
import safe Data.Word ( Word32 )

update_crc :: Word32 -> Char -> Word32
update_crc :: Word32 -> Char -> Word32
update_crc Word32
crc Char
ch =
    let c :: Word32
c = Word32
crc Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
`xor` Word32
0xFFFFFFFF
        newval :: Word32
newval = (Array Int Word32
gzipcrctab Array Int Word32 -> Int -> Word32
forall i e. Ix i => Array i e -> i -> e
! Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word32
c Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
`xor` Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord Char
ch)) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word32
0xff))
                 Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
`xor` (Word32
c Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`shiftR` Int
8)
        in
        Word32
newval Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
`xor` Word32
0xFFFFFFFF

update_crc_list :: Word32 -> [Char] -> Word32
update_crc_list :: Word32 -> [Char] -> Word32
update_crc_list Word32
start [Char]
list = (Word32 -> Char -> Word32) -> Word32 -> [Char] -> Word32
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Word32 -> Char -> Word32
update_crc Word32
start [Char]
list

calc_crc32 :: [Char] -> Word32
calc_crc32 :: [Char] -> Word32
calc_crc32 [Char]
s = Word32 -> [Char] -> Word32
update_crc_list Word32
0 [Char]
s

gzipcrctab :: Array Int Word32
gzipcrctab :: Array Int Word32
gzipcrctab = (Int, Int) -> [(Int, Word32)] -> Array Int Word32
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (Int
0,Int
255) ([Int] -> [Word32] -> [(Int, Word32)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..Int
255]
  [
  Word32
0x00000000, Word32
0x77073096, Word32
0xee0e612c, Word32
0x990951ba, Word32
0x076dc419,
  Word32
0x706af48f, Word32
0xe963a535, Word32
0x9e6495a3, Word32
0x0edb8832, Word32
0x79dcb8a4,
  Word32
0xe0d5e91e, Word32
0x97d2d988, Word32
0x09b64c2b, Word32
0x7eb17cbd, Word32
0xe7b82d07,
  Word32
0x90bf1d91, Word32
0x1db71064, Word32
0x6ab020f2, Word32
0xf3b97148, Word32
0x84be41de,
  Word32
0x1adad47d, Word32
0x6ddde4eb, Word32
0xf4d4b551, Word32
0x83d385c7, Word32
0x136c9856,
  Word32
0x646ba8c0, Word32
0xfd62f97a, Word32
0x8a65c9ec, Word32
0x14015c4f, Word32
0x63066cd9,
  Word32
0xfa0f3d63, Word32
0x8d080df5, Word32
0x3b6e20c8, Word32
0x4c69105e, Word32
0xd56041e4,
  Word32
0xa2677172, Word32
0x3c03e4d1, Word32
0x4b04d447, Word32
0xd20d85fd, Word32
0xa50ab56b,
  Word32
0x35b5a8fa, Word32
0x42b2986c, Word32
0xdbbbc9d6, Word32
0xacbcf940, Word32
0x32d86ce3,
  Word32
0x45df5c75, Word32
0xdcd60dcf, Word32
0xabd13d59, Word32
0x26d930ac, Word32
0x51de003a,
  Word32
0xc8d75180, Word32
0xbfd06116, Word32
0x21b4f4b5, Word32
0x56b3c423, Word32
0xcfba9599,
  Word32
0xb8bda50f, Word32
0x2802b89e, Word32
0x5f058808, Word32
0xc60cd9b2, Word32
0xb10be924,
  Word32
0x2f6f7c87, Word32
0x58684c11, Word32
0xc1611dab, Word32
0xb6662d3d, Word32
0x76dc4190,
  Word32
0x01db7106, Word32
0x98d220bc, Word32
0xefd5102a, Word32
0x71b18589, Word32
0x06b6b51f,
  Word32
0x9fbfe4a5, Word32
0xe8b8d433, Word32
0x7807c9a2, Word32
0x0f00f934, Word32
0x9609a88e,
  Word32
0xe10e9818, Word32
0x7f6a0dbb, Word32
0x086d3d2d, Word32
0x91646c97, Word32
0xe6635c01,
  Word32
0x6b6b51f4, Word32
0x1c6c6162, Word32
0x856530d8, Word32
0xf262004e, Word32
0x6c0695ed,
  Word32
0x1b01a57b, Word32
0x8208f4c1, Word32
0xf50fc457, Word32
0x65b0d9c6, Word32
0x12b7e950,
  Word32
0x8bbeb8ea, Word32
0xfcb9887c, Word32
0x62dd1ddf, Word32
0x15da2d49, Word32
0x8cd37cf3,
  Word32
0xfbd44c65, Word32
0x4db26158, Word32
0x3ab551ce, Word32
0xa3bc0074, Word32
0xd4bb30e2,
  Word32
0x4adfa541, Word32
0x3dd895d7, Word32
0xa4d1c46d, Word32
0xd3d6f4fb, Word32
0x4369e96a,
  Word32
0x346ed9fc, Word32
0xad678846, Word32
0xda60b8d0, Word32
0x44042d73, Word32
0x33031de5,
  Word32
0xaa0a4c5f, Word32
0xdd0d7cc9, Word32
0x5005713c, Word32
0x270241aa, Word32
0xbe0b1010,
  Word32
0xc90c2086, Word32
0x5768b525, Word32
0x206f85b3, Word32
0xb966d409, Word32
0xce61e49f,
  Word32
0x5edef90e, Word32
0x29d9c998, Word32
0xb0d09822, Word32
0xc7d7a8b4, Word32
0x59b33d17,
  Word32
0x2eb40d81, Word32
0xb7bd5c3b, Word32
0xc0ba6cad, Word32
0xedb88320, Word32
0x9abfb3b6,
  Word32
0x03b6e20c, Word32
0x74b1d29a, Word32
0xead54739, Word32
0x9dd277af, Word32
0x04db2615,
  Word32
0x73dc1683, Word32
0xe3630b12, Word32
0x94643b84, Word32
0x0d6d6a3e, Word32
0x7a6a5aa8,
  Word32
0xe40ecf0b, Word32
0x9309ff9d, Word32
0x0a00ae27, Word32
0x7d079eb1, Word32
0xf00f9344,
  Word32
0x8708a3d2, Word32
0x1e01f268, Word32
0x6906c2fe, Word32
0xf762575d, Word32
0x806567cb,
  Word32
0x196c3671, Word32
0x6e6b06e7, Word32
0xfed41b76, Word32
0x89d32be0, Word32
0x10da7a5a,
  Word32
0x67dd4acc, Word32
0xf9b9df6f, Word32
0x8ebeeff9, Word32
0x17b7be43, Word32
0x60b08ed5,
  Word32
0xd6d6a3e8, Word32
0xa1d1937e, Word32
0x38d8c2c4, Word32
0x4fdff252, Word32
0xd1bb67f1,
  Word32
0xa6bc5767, Word32
0x3fb506dd, Word32
0x48b2364b, Word32
0xd80d2bda, Word32
0xaf0a1b4c,
  Word32
0x36034af6, Word32
0x41047a60, Word32
0xdf60efc3, Word32
0xa867df55, Word32
0x316e8eef,
  Word32
0x4669be79, Word32
0xcb61b38c, Word32
0xbc66831a, Word32
0x256fd2a0, Word32
0x5268e236,
  Word32
0xcc0c7795, Word32
0xbb0b4703, Word32
0x220216b9, Word32
0x5505262f, Word32
0xc5ba3bbe,
  Word32
0xb2bd0b28, Word32
0x2bb45a92, Word32
0x5cb36a04, Word32
0xc2d7ffa7, Word32
0xb5d0cf31,
  Word32
0x2cd99e8b, Word32
0x5bdeae1d, Word32
0x9b64c2b0, Word32
0xec63f226, Word32
0x756aa39c,
  Word32
0x026d930a, Word32
0x9c0906a9, Word32
0xeb0e363f, Word32
0x72076785, Word32
0x05005713,
  Word32
0x95bf4a82, Word32
0xe2b87a14, Word32
0x7bb12bae, Word32
0x0cb61b38, Word32
0x92d28e9b,
  Word32
0xe5d5be0d, Word32
0x7cdcefb7, Word32
0x0bdbdf21, Word32
0x86d3d2d4, Word32
0xf1d4e242,
  Word32
0x68ddb3f8, Word32
0x1fda836e, Word32
0x81be16cd, Word32
0xf6b9265b, Word32
0x6fb077e1,
  Word32
0x18b74777, Word32
0x88085ae6, Word32
0xff0f6a70, Word32
0x66063bca, Word32
0x11010b5c,
  Word32
0x8f659eff, Word32
0xf862ae69, Word32
0x616bffd3, Word32
0x166ccf45, Word32
0xa00ae278,
  Word32
0xd70dd2ee, Word32
0x4e048354, Word32
0x3903b3c2, Word32
0xa7672661, Word32
0xd06016f7,
  Word32
0x4969474d, Word32
0x3e6e77db, Word32
0xaed16a4a, Word32
0xd9d65adc, Word32
0x40df0b66,
  Word32
0x37d83bf0, Word32
0xa9bcae53, Word32
0xdebb9ec5, Word32
0x47b2cf7f, Word32
0x30b5ffe9,
  Word32
0xbdbdf21c, Word32
0xcabac28a, Word32
0x53b39330, Word32
0x24b4a3a6, Word32
0xbad03605,
  Word32
0xcdd70693, Word32
0x54de5729, Word32
0x23d967bf, Word32
0xb3667a2e, Word32
0xc4614ab8,
  Word32
0x5d681b02, Word32
0x2a6f2b94, Word32
0xb40bbe37, Word32
0xc30c8ea1, Word32
0x5a05df1b,
  Word32
0x2d02ef8d ])