0 | module Codec.Compression.Zlib.Utilities
  1 |
  2 | import Codec.Compression.Zlib.Proofs
  3 | import Control.Monad.Elin
  4 | import Control.Monad.MCancel
  5 | import Data.Buffer
  6 | import Data.Buffer.Core
  7 | import System.Posix.Timer
  8 | import System.Posix.Timer.Prim
  9 | import System.FFI
 10 | import System.Zlib.Constant
 11 | import System.Zlib.Error
 12 | import System.Zlib.Types
 13 | import System.Zlib.Raw
 14 |
 15 | %default total
 16 |
 17 | --------------------------------------------------------------------------------
 18 | --          Utilities
 19 | --------------------------------------------------------------------------------
 20 |
 21 | ||| Grab CLOCK_MONOTONIC.
 22 | |||
 23 | export
 24 | grabMonotonicTime : Elin World [Errno] (IClock CLOCK_MONOTONIC)
 25 | grabMonotonicTime = getTime CLOCK_MONOTONIC
 26 |
 27 | ||| Grab CLOCK_REALTIME.
 28 | |||
 29 | export
 30 | grabRealTime : Elin World [Errno] (IClock CLOCK_REALTIME)
 31 | grabRealTime = getTime CLOCK_REALTIME
 32 |
 33 | ||| Converts a zlib status code to a human-readable description.
 34 | |||
 35 | export
 36 | zlibStatusMessage :  Int
 37 |                   -> String
 38 | zlibStatusMessage status =
 39 |   if status == (zlibconstantToInt Z_OK) then
 40 |     "Z_OK"
 41 |   else if status == (zlibconstantToInt Z_STREAM_END) then
 42 |     "Z_STREAM_END"
 43 |   else if status == (zlibconstantToInt Z_NEED_DICT) then
 44 |     "Z_NEED_DICT"
 45 |   else if status == (zlibconstantToInt Z_ERRNO) then
 46 |     "Z_ERRNO"
 47 |   else if status == (zlibconstantToInt Z_STREAM_ERROR) then
 48 |     "Z_STREAM_ERROR"
 49 |   else if status == (zlibconstantToInt Z_DATA_ERROR) then
 50 |     "Z_DATA_ERROR"
 51 |   else if status == (zlibconstantToInt Z_MEM_ERROR) then
 52 |     "Z_MEM_ERROR"
 53 |   else if status == (zlibconstantToInt Z_BUF_ERROR) then
 54 |     "Z_BUF_ERROR"
 55 |   else if status == (zlibconstantToInt Z_VERSION_ERROR) then
 56 |     "Z_VERSION_ERROR"
 57 |   else
 58 |     "Unknown zlib status: " ++ show status
 59 |
 60 | ||| Constructs a timestamped zlib error.
 61 | |||
 62 | ||| If obtaining CLOCK_REALTIME itself fails, both the timer error and the
 63 | ||| original zlib error are retained without timestamps.
 64 | |||
 65 | export
 66 | mkError :  String
 67 |         -> String
 68 |         -> IO (List ZlibError)
 69 | mkError location message = do
 70 |   now <- runElinIO grabRealTime
 71 |   case now of
 72 |     Right realtime =>
 73 |       pure [ MkZlibError
 74 |                location
 75 |                (MiscError message)
 76 |                (Just realtime)
 77 |            ]
 78 |     Left timererr  =>
 79 |       pure [ MkZlibError
 80 |                location
 81 |                (MiscError (show timererr))
 82 |                Nothing
 83 |            , MkZlibError
 84 |                location
 85 |                (MiscError message)
 86 |                Nothing
 87 |            ]
 88 |
 89 | ||| Constructs a timestamped `ZlibError` from a raw zlib status code.
 90 | |||
 91 | export
 92 | statusError :  String
 93 |             -> Int
 94 |             -> IO (List ZlibError)
 95 | statusError location status =
 96 |   mkError location (zlibStatusMessage status)
 97 |
 98 | ||| Copy the used prefix of a mutable output buffer into an independently
 99 | ||| owned immutable buffer.
100 | |||
101 | export
102 | freezeOutput :  (used : Nat)
103 |              -> IOBuffer capacity
104 |              -> {auto 0 bounds : LTE used capacity}
105 |              -> IO AnyBuffer
106 | freezeOutput used output = do
107 |   frozen <- mbuffer used
108 |   runIO (copy output 0 0 used {p1 = bounds} {p2 = lteReflexive used} frozen)
109 |   pure
110 |     ( AB used
111 |          (unsafeMakeBuffer
112 |          (unsafeFromMBuffer frozen))
113 |     )
114 |