module Util.BinaryExtras(
hReadLtd,
initialClockTime,
) where
import System.IO
import Data.IORef
import System.Time
import Util.Binary
import Util.BinaryUtils
import Util.Computation
import Util.ExtendedPrelude
import Util.IOExtras
import Util.BinaryInstances()
hReadLtd :: HasBinary a IO =>
Int
-> Handle -> IO (WithError a)
hReadLtd limit handle =
addFallOutWE (\ break ->
do
lenIORef <- newIORef 0
let
ensure :: Int -> IO ()
ensure i =
do
len1 <- simpleModifyIORef lenIORef
(\ len0 ->
let
len1 = len0 + i
in
(len1,len1)
)
if len1 > limit
then
break "BinaryExtras.hReadLtd: limit exceeded"
else
done
(ReadBinary {readByte = readByte1,readBytes = readBytes1})
= toReadBinaryHandle handle
readByte2 =
do
ensure 1
readByte1
readBytes2 len =
do
ensure len
readBytes1 len
rb2 = ReadBinary {readByte = readByte2,readBytes = readBytes2}
readBin rb2
)
instance Monad m => HasBinary ClockTime m where
writeBin = mapWrite (\ (TOD i j) -> (i,j))
readBin = mapRead (\ (i,j) -> TOD i j)
initialClockTime :: ClockTime
initialClockTime = TOD 1052391874 190946000000