0 | module Codec.Compression.Zlib.Checksum
 1 |
 2 | import Data.Buffer.Core
 3 | import System.Zlib.Raw
 4 |
 5 | %default total
 6 |
 7 | --------------------------------------------------------------------------------
 8 | --          CRC-32
 9 | --------------------------------------------------------------------------------
10 |
11 | ||| Update an existing CRC-32 checksum with the contents of a byte buffer.
12 | |||
13 | ||| This function can be used incrementally by passing the checksum returned
14 | ||| from one invocation as the initial checksum of the next.
15 | |||
16 | ||| The initial CRC-32 value for a new checksum is `0`.
17 | |||
18 | export
19 | crc32Update :  {n : Nat}
20 |             -> Bits32
21 |             -> IBuffer n
22 |             -> IO Bits32
23 | crc32Update crc input =
24 |   primIO (prim__crc32 crc (unsafeGetBuffer input) (cast n))
25 |
26 | ||| Compute the CRC-32 checksum of an immutable byte buffer.
27 | |||
28 | ||| This is equivalent to calling `crc32Update` with zlib's initial CRC-32
29 | ||| value of `0`.
30 | |||
31 | export
32 | crc32 :  {n : Nat}
33 |       -> IBuffer n
34 |       -> IO Bits32
35 | crc32 =
36 |   crc32Update 0
37 |
38 | --------------------------------------------------------------------------------
39 | --          Adler-32
40 | --------------------------------------------------------------------------------
41 |
42 | ||| Update an existing Adler-32 checksum with the contents of a byte buffer.
43 | |||
44 | ||| This function can be used incrementally by passing the checksum returned
45 | ||| from one invocation as the initial checksum of the next.
46 | |||
47 | ||| The initial Adler-32 value for a new checksum is `1`.
48 | |||
49 | export
50 | adler32Update :  {n : Nat}
51 |               -> Bits32
52 |               -> IBuffer n
53 |               -> IO Bits32
54 | adler32Update adler input =
55 |   primIO (prim__adler32 adler (unsafeGetBuffer input) (cast n))
56 |
57 | ||| Compute the Adler-32 checksum of an immutable byte buffer.
58 | |||
59 | ||| This is equivalent to calling `adler32Update` with zlib's initial
60 | ||| Adler-32 value of `1`.
61 | |||
62 | export
63 | adler32 :  {n : Nat}
64 |         -> IBuffer n
65 |         -> IO Bits32
66 | adler32 =
67 |   adler32Update 1
68 |