{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Flat.Decoder.Prim (
dBool,
dWord8,
dBE8,
dBE16,
dBE32,
dBE64,
dBEBits8,
dBEBits16,
dBEBits32,
dBEBits64,
dropBits,
dFloat,
dDouble,
getChunksInfo,
dByteString_,
dLazyByteString_,
dByteArray_,
ConsState(..),consOpen,consClose,consBool,consBits
) where
import Control.Monad
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Flat.Decoder.Types
import Flat.Endian
import Flat.Memory
import Data.FloatCast
import Data.Word
import Foreign
data ConsState =
ConsState {-# UNPACK #-} !Word !Int
consOpen :: Get ConsState
consOpen :: Get ConsState
consOpen = (Ptr Word8 -> S -> IO (GetResult ConsState)) -> Get ConsState
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult ConsState)) -> Get ConsState)
-> (Ptr Word8 -> S -> IO (GetResult ConsState)) -> Get ConsState
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
let u :: Int
u = S -> Int
usedBits S
s
Word
w <- case Ptr Word8 -> Ptr Word8 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (S -> Ptr Word8
currPtr S
s) Ptr Word8
endPtr of
Ordering
LT -> do
Word16
w16::Word16 <- Word16 -> Word16
toBE16 (Word16 -> Word16) -> IO Word16 -> IO Word16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word16 -> IO Word16
forall a. Storable a => Ptr a -> IO a
peek (Ptr Word8 -> Ptr Word16
forall a b. Ptr a -> Ptr b
castPtr (Ptr Word8 -> Ptr Word16) -> Ptr Word8 -> Ptr Word16
forall a b. (a -> b) -> a -> b
$ S -> Ptr Word8
currPtr S
s)
Word -> IO Word
forall (m :: * -> *) a. Monad m => a -> m a
return (Word -> IO Word) -> Word -> IO Word
forall a b. (a -> b) -> a -> b
$ Word16 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
w16 Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftL` (Int
uInt -> Int -> Int
forall a. Num a => a -> a -> a
+(Int
wordSizeInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
16))
Ordering
EQ -> do
Word8
w8 :: Word8 <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek (S -> Ptr Word8
currPtr S
s)
Word -> IO Word
forall (m :: * -> *) a. Monad m => a -> m a
return (Word -> IO Word) -> Word -> IO Word
forall a b. (a -> b) -> a -> b
$ Word8 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
w8 Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`unsafeShiftL` (Int
uInt -> Int -> Int
forall a. Num a => a -> a -> a
+(Int
wordSizeInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
8))
Ordering
GT -> Ptr Word8 -> S -> IO Word
forall a. Ptr Word8 -> S -> IO a
notEnoughSpace Ptr Word8
endPtr S
s
GetResult ConsState -> IO (GetResult ConsState)
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult ConsState -> IO (GetResult ConsState))
-> GetResult ConsState -> IO (GetResult ConsState)
forall a b. (a -> b) -> a -> b
$ S -> ConsState -> GetResult ConsState
forall a. S -> a -> GetResult a
GetResult S
s (Word -> Int -> ConsState
ConsState Word
w Int
0)
consClose :: Int -> Get ()
consClose :: Int -> Get ()
consClose Int
n = (Ptr Word8 -> S -> IO (GetResult ())) -> Get ()
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult ())) -> Get ())
-> (Ptr Word8 -> S -> IO (GetResult ())) -> Get ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
let u' :: Int
u' = Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+S -> Int
usedBits S
s
if Int
u' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
8
then GetResult () -> IO (GetResult ())
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult () -> IO (GetResult ()))
-> GetResult () -> IO (GetResult ())
forall a b. (a -> b) -> a -> b
$ S -> () -> GetResult ()
forall a. S -> a -> GetResult a
GetResult (S
s {usedBits :: Int
usedBits=Int
u'}) ()
else if S -> Ptr Word8
currPtr S
s Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr Word8
endPtr
then Ptr Word8 -> S -> IO (GetResult ())
forall a. Ptr Word8 -> S -> IO a
notEnoughSpace Ptr Word8
endPtr S
s
else GetResult () -> IO (GetResult ())
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult () -> IO (GetResult ()))
-> GetResult () -> IO (GetResult ())
forall a b. (a -> b) -> a -> b
$ S -> () -> GetResult ()
forall a. S -> a -> GetResult a
GetResult (S
s {currPtr :: Ptr Word8
currPtr=S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1,usedBits :: Int
usedBits=Int
u'Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
8}) ()
consBool :: ConsState -> (ConsState,Bool)
consBool :: ConsState -> (ConsState, Bool)
consBool ConsState
cs = (Word
0Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
/=) (Word -> Bool) -> (ConsState, Word) -> (ConsState, Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ConsState -> Int -> (ConsState, Word)
consBits ConsState
cs Int
1
consBits :: ConsState -> Int -> (ConsState, Word)
consBits :: ConsState -> Int -> (ConsState, Word)
consBits ConsState
cs Int
3 = ConsState -> Int -> Word -> (ConsState, Word)
consBits_ ConsState
cs Int
3 Word
7
consBits ConsState
cs Int
2 = ConsState -> Int -> Word -> (ConsState, Word)
consBits_ ConsState
cs Int
2 Word
3
consBits ConsState
cs Int
1 = ConsState -> Int -> Word -> (ConsState, Word)
consBits_ ConsState
cs Int
1 Word
1
consBits ConsState
_ Int
_ = [Char] -> (ConsState, Word)
forall a. HasCallStack => [Char] -> a
error [Char]
"unsupported"
consBits_ :: ConsState -> Int -> Word -> (ConsState, Word)
#define CONS_STA
#ifdef CONS_ROT
consBits_ (ConsState w usedBits) numBits mask =
let usedBits' = numBits+usedBits
w' = w `rotateL` numBits
in (ConsState w' usedBits',w' .&. mask)
#endif
#ifdef CONS_SHL
consBits_ (ConsState w usedBits) numBits mask =
let usedBits' = numBits+usedBits
w' = w `unsafeShiftL` numBits
in (ConsState w' usedBits', (w `shR` (wordSize - numBits)) .&. mask)
#endif
#ifdef CONS_STA
consBits_ :: ConsState -> Int -> Word -> (ConsState, Word)
consBits_ (ConsState Word
w Int
usedBits) Int
numBits Word
mask =
let usedBits' :: Int
usedBits' = Int
numBitsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
usedBits
in (Word -> Int -> ConsState
ConsState Word
w Int
usedBits', (Word
w Word -> Int -> Word
forall a. Bits a => a -> Int -> a
`shR` (Int
wordSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
usedBits')) Word -> Word -> Word
forall a. Bits a => a -> a -> a
.&. Word
mask)
#endif
wordSize :: Int
wordSize :: Int
wordSize = Word -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (Word
0 :: Word)
{-# INLINE ensureBits #-}
ensureBits :: Ptr Word8 -> S -> Int -> IO ()
ensureBits :: Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
n = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((Ptr Word8
endPtr Ptr Word8 -> Ptr Word8 -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` S -> Ptr Word8
currPtr S
s) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
- S -> Int
usedBits S
s Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr Word8 -> S -> IO ()
forall a. Ptr Word8 -> S -> IO a
notEnoughSpace Ptr Word8
endPtr S
s
{-# INLINE dropBits #-}
dropBits :: Int -> Get ()
dropBits :: Int -> Get ()
dropBits Int
n
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = (Ptr Word8 -> S -> IO (GetResult ())) -> Get ()
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult ())) -> Get ())
-> (Ptr Word8 -> S -> IO (GetResult ())) -> Get ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
n
GetResult () -> IO (GetResult ())
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult () -> IO (GetResult ()))
-> GetResult () -> IO (GetResult ())
forall a b. (a -> b) -> a -> b
$ S -> () -> GetResult ()
forall a. S -> a -> GetResult a
GetResult (S -> Int -> S
dropBits_ S
s Int
n) ()
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = () -> Get ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = [Char] -> Get ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Get ()) -> [Char] -> Get ()
forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unwords [[Char]
"dropBits",Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n]
{-# INLINE dropBits_ #-}
dropBits_ :: S -> Int -> S
dropBits_ :: S -> Int -> S
dropBits_ S
s Int
n =
let (Int
bytes,Int
bits) = (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+S -> Int
usedBits S
s) Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int
8
in S :: Ptr Word8 -> Int -> S
S {currPtr :: Ptr Word8
currPtr=S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
bytes,usedBits :: Int
usedBits=Int
bits}
{-# INLINE dBool #-}
dBool :: Get Bool
dBool :: Get Bool
dBool = (Ptr Word8 -> S -> IO (GetResult Bool)) -> Get Bool
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult Bool)) -> Get Bool)
-> (Ptr Word8 -> S -> IO (GetResult Bool)) -> Get Bool
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s ->
if S -> Ptr Word8
currPtr S
s Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr Word8
endPtr
then Ptr Word8 -> S -> IO (GetResult Bool)
forall a. Ptr Word8 -> S -> IO a
notEnoughSpace Ptr Word8
endPtr S
s
else do
!Word8
w <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek (S -> Ptr Word8
currPtr S
s)
let !b :: Bool
b = Word8
0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= (Word8
w Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. (Word8
128 Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shR` S -> Int
usedBits S
s))
let !s' :: S
s' = if S -> Int
usedBits S
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
7
then S
s { currPtr :: Ptr Word8
currPtr = S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1, usedBits :: Int
usedBits = Int
0 }
else S
s { usedBits :: Int
usedBits = S -> Int
usedBits S
s Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 }
GetResult Bool -> IO (GetResult Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult Bool -> IO (GetResult Bool))
-> GetResult Bool -> IO (GetResult Bool)
forall a b. (a -> b) -> a -> b
$ S -> Bool -> GetResult Bool
forall a. S -> a -> GetResult a
GetResult S
s' Bool
b
{-# INLINE dBEBits8 #-}
dBEBits8 :: Int -> Get Word8
dBEBits8 :: Int -> Get Word8
dBEBits8 Int
n = (Ptr Word8 -> S -> IO (GetResult Word8)) -> Get Word8
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult Word8)) -> Get Word8)
-> (Ptr Word8 -> S -> IO (GetResult Word8)) -> Get Word8
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
n
S -> Int -> IO (GetResult Word8)
take8 S
s Int
n
{-# INLINE dBEBits16 #-}
dBEBits16 :: Int -> Get Word16
dBEBits16 :: Int -> Get Word16
dBEBits16 Int
n = (Ptr Word8 -> S -> IO (GetResult Word16)) -> Get Word16
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult Word16)) -> Get Word16)
-> (Ptr Word8 -> S -> IO (GetResult Word16)) -> Get Word16
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
n
Int -> S -> IO (GetResult Word16)
forall a. (Num a, Bits a) => Int -> S -> IO (GetResult a)
takeN Int
n S
s
{-# INLINE dBEBits32 #-}
dBEBits32 :: Int -> Get Word32
dBEBits32 :: Int -> Get Word32
dBEBits32 Int
n = (Ptr Word8 -> S -> IO (GetResult Word32)) -> Get Word32
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult Word32)) -> Get Word32)
-> (Ptr Word8 -> S -> IO (GetResult Word32)) -> Get Word32
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
n
Int -> S -> IO (GetResult Word32)
forall a. (Num a, Bits a) => Int -> S -> IO (GetResult a)
takeN Int
n S
s
{-# INLINE dBEBits64 #-}
dBEBits64 :: Int -> Get Word64
dBEBits64 :: Int -> Get Word64
dBEBits64 Int
n = (Ptr Word8 -> S -> IO (GetResult Word64)) -> Get Word64
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult Word64)) -> Get Word64)
-> (Ptr Word8 -> S -> IO (GetResult Word64)) -> Get Word64
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
n
Int -> S -> IO (GetResult Word64)
forall a. (Num a, Bits a) => Int -> S -> IO (GetResult a)
takeN Int
n S
s
{-# INLINE take8 #-}
take8 :: S -> Int -> IO (GetResult Word8)
take8 :: S -> Int -> IO (GetResult Word8)
take8 S
s Int
n = S -> Word8 -> GetResult Word8
forall a. S -> a -> GetResult a
GetResult (S -> Int -> S
dropBits8 S
s Int
n) (Word8 -> GetResult Word8) -> IO Word8 -> IO (GetResult Word8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> S -> Int -> IO Word8
read8 S
s Int
n
where
read8 :: S -> Int -> IO Word8
read8 :: S -> Int -> IO Word8
read8 S
s Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
0 Bool -> Bool -> Bool
&& Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<=Int
8 =
if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
- S -> Int
usedBits S
s
then do
Word8
w <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek (S -> Ptr Word8
currPtr S
s)
Word8 -> IO Word8
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8 -> IO Word8) -> Word8 -> IO Word8
forall a b. (a -> b) -> a -> b
$ (Word8
w Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`unsafeShiftL` S -> Int
usedBits S
s) Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shR` (Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n)
else do
Word16
w::Word16 <- Word16 -> Word16
toBE16 (Word16 -> Word16) -> IO Word16 -> IO Word16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word16 -> IO Word16
forall a. Storable a => Ptr a -> IO a
peek (Ptr Word8 -> Ptr Word16
forall a b. Ptr a -> Ptr b
castPtr (Ptr Word8 -> Ptr Word16) -> Ptr Word8 -> Ptr Word16
forall a b. (a -> b) -> a -> b
$ S -> Ptr Word8
currPtr S
s)
Word8 -> IO Word8
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8 -> IO Word8) -> Word8 -> IO Word8
forall a b. (a -> b) -> a -> b
$ Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Word8) -> Word16 -> Word8
forall a b. (a -> b) -> a -> b
$ (Word16
w Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
`unsafeShiftL` S -> Int
usedBits S
s) Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
`shR` (Int
16 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n)
| Bool
otherwise = [Char] -> IO Word8
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO Word8) -> [Char] -> IO Word8
forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unwords [[Char]
"read8: cannot read",Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n,[Char]
"bits"]
dropBits8 :: S -> Int -> S
dropBits8 :: S -> Int -> S
dropBits8 S
s Int
n =
let u' :: Int
u' = Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+S -> Int
usedBits S
s
in if Int
u' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
8
then S
s {usedBits :: Int
usedBits=Int
u'}
else S
s {currPtr :: Ptr Word8
currPtr=S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1,usedBits :: Int
usedBits=Int
u'Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
8}
{-# INLINE takeN #-}
takeN :: (Num a, Bits a) => Int -> S -> IO (GetResult a)
takeN :: Int -> S -> IO (GetResult a)
takeN Int
n S
s = S -> a -> Int -> Int -> IO (GetResult a)
forall t.
(Bits t, Num t) =>
S -> t -> Int -> Int -> IO (GetResult t)
read S
s a
0 (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
n Int -> Int -> Int
forall a. Ord a => a -> a -> a
`min` Int
8)) Int
n
where
read :: S -> t -> Int -> Int -> IO (GetResult t)
read S
s t
r Int
sh Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<=Int
0 = GetResult t -> IO (GetResult t)
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult t -> IO (GetResult t))
-> GetResult t -> IO (GetResult t)
forall a b. (a -> b) -> a -> b
$ S -> t -> GetResult t
forall a. S -> a -> GetResult a
GetResult S
s t
r
| Bool
otherwise = do
let m :: Int
m = Int
n Int -> Int -> Int
forall a. Ord a => a -> a -> a
`min` Int
8
GetResult S
s' Word8
b <- S -> Int -> IO (GetResult Word8)
take8 S
s Int
m
S -> t -> Int -> Int -> IO (GetResult t)
read S
s' (t
r t -> t -> t
forall a. Bits a => a -> a -> a
.|. (Word8 -> t
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
b t -> Int -> t
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
sh)) ((Int
shInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
8) Int -> Int -> Int
forall a. Ord a => a -> a -> a
`max` Int
0) (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
8)
dWord8 :: Get Word8
dWord8 :: Get Word8
dWord8 = Get Word8
dBE8
{-# INLINE dBE8 #-}
dBE8 :: Get Word8
dBE8 :: Get Word8
dBE8 = (Ptr Word8 -> S -> IO (GetResult Word8)) -> Get Word8
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult Word8)) -> Get Word8)
-> (Ptr Word8 -> S -> IO (GetResult Word8)) -> Get Word8
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
8
!Word8
w1 <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek (S -> Ptr Word8
currPtr S
s)
!Word8
w <- if S -> Int
usedBits S
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Word8 -> IO Word8
forall (m :: * -> *) a. Monad m => a -> m a
return Word8
w1
else do
!Word8
w2 <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek (S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1)
Word8 -> IO Word8
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8 -> IO Word8) -> Word8 -> IO Word8
forall a b. (a -> b) -> a -> b
$ (Word8
w1 Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`unsafeShiftL` S -> Int
usedBits S
s) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. (Word8
w2 Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shR` (Int
8Int -> Int -> Int
forall a. Num a => a -> a -> a
-S -> Int
usedBits S
s))
GetResult Word8 -> IO (GetResult Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult Word8 -> IO (GetResult Word8))
-> GetResult Word8 -> IO (GetResult Word8)
forall a b. (a -> b) -> a -> b
$ S -> Word8 -> GetResult Word8
forall a. S -> a -> GetResult a
GetResult (S
s {currPtr :: Ptr Word8
currPtr=S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1}) Word8
w
{-# INLINE dBE16 #-}
dBE16 :: Get Word16
dBE16 :: Get Word16
dBE16 = (Ptr Word8 -> S -> IO (GetResult Word16)) -> Get Word16
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult Word16)) -> Get Word16)
-> (Ptr Word8 -> S -> IO (GetResult Word16)) -> Get Word16
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
16
!Word16
w1 <- Word16 -> Word16
toBE16 (Word16 -> Word16) -> IO Word16 -> IO Word16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word16 -> IO Word16
forall a. Storable a => Ptr a -> IO a
peek (Ptr Word8 -> Ptr Word16
forall a b. Ptr a -> Ptr b
castPtr (Ptr Word8 -> Ptr Word16) -> Ptr Word8 -> Ptr Word16
forall a b. (a -> b) -> a -> b
$ S -> Ptr Word8
currPtr S
s)
!Word16
w <- if S -> Int
usedBits S
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Word16 -> IO Word16
forall (m :: * -> *) a. Monad m => a -> m a
return Word16
w1
else do
!(Word8
w2::Word8) <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek (S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
2)
Word16 -> IO Word16
forall (m :: * -> *) a. Monad m => a -> m a
return (Word16 -> IO Word16) -> Word16 -> IO Word16
forall a b. (a -> b) -> a -> b
$ Word16
w1 Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
`unsafeShiftL` S -> Int
usedBits S
s Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|. Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w2 Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shR` (Int
8Int -> Int -> Int
forall a. Num a => a -> a -> a
-S -> Int
usedBits S
s))
GetResult Word16 -> IO (GetResult Word16)
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult Word16 -> IO (GetResult Word16))
-> GetResult Word16 -> IO (GetResult Word16)
forall a b. (a -> b) -> a -> b
$ S -> Word16 -> GetResult Word16
forall a. S -> a -> GetResult a
GetResult (S
s {currPtr :: Ptr Word8
currPtr=S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
2}) Word16
w
{-# INLINE dBE32 #-}
dBE32 :: Get Word32
dBE32 :: Get Word32
dBE32 = (Ptr Word8 -> S -> IO (GetResult Word32)) -> Get Word32
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult Word32)) -> Get Word32)
-> (Ptr Word8 -> S -> IO (GetResult Word32)) -> Get Word32
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
32
!Word32
w1 <- Word32 -> Word32
toBE32 (Word32 -> Word32) -> IO Word32 -> IO Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word32 -> IO Word32
forall a. Storable a => Ptr a -> IO a
peek (Ptr Word8 -> Ptr Word32
forall a b. Ptr a -> Ptr b
castPtr (Ptr Word8 -> Ptr Word32) -> Ptr Word8 -> Ptr Word32
forall a b. (a -> b) -> a -> b
$ S -> Ptr Word8
currPtr S
s)
!Word32
w <- if S -> Int
usedBits S
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Word32 -> IO Word32
forall (m :: * -> *) a. Monad m => a -> m a
return Word32
w1
else do
!(Word8
w2::Word8) <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek (S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
4)
Word32 -> IO Word32
forall (m :: * -> *) a. Monad m => a -> m a
return (Word32 -> IO Word32) -> Word32 -> IO Word32
forall a b. (a -> b) -> a -> b
$ Word32
w1 Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` S -> Int
usedBits S
s Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w2 Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shR` (Int
8Int -> Int -> Int
forall a. Num a => a -> a -> a
-S -> Int
usedBits S
s))
GetResult Word32 -> IO (GetResult Word32)
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult Word32 -> IO (GetResult Word32))
-> GetResult Word32 -> IO (GetResult Word32)
forall a b. (a -> b) -> a -> b
$ S -> Word32 -> GetResult Word32
forall a. S -> a -> GetResult a
GetResult (S
s {currPtr :: Ptr Word8
currPtr=S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
4}) Word32
w
{-# INLINE dBE64 #-}
dBE64 :: Get Word64
dBE64 :: Get Word64
dBE64 = (Ptr Word8 -> S -> IO (GetResult Word64)) -> Get Word64
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult Word64)) -> Get Word64)
-> (Ptr Word8 -> S -> IO (GetResult Word64)) -> Get Word64
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
64
!Word64
w1 <- Word64 -> Word64
toBE64 (Word64 -> Word64) -> IO Word64 -> IO Word64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word64 -> IO Word64
peek64 (Ptr Word8 -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr (Ptr Word8 -> Ptr Word64) -> Ptr Word8 -> Ptr Word64
forall a b. (a -> b) -> a -> b
$ S -> Ptr Word8
currPtr S
s)
!Word64
w <- if S -> Int
usedBits S
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Word64 -> IO Word64
forall (m :: * -> *) a. Monad m => a -> m a
return Word64
w1
else do
!(Word8
w2::Word8) <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek (S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8)
Word64 -> IO Word64
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64 -> IO Word64) -> Word64 -> IO Word64
forall a b. (a -> b) -> a -> b
$ Word64
w1 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftL` S -> Int
usedBits S
s Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w2 Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shR` (Int
8Int -> Int -> Int
forall a. Num a => a -> a -> a
-S -> Int
usedBits S
s))
GetResult Word64 -> IO (GetResult Word64)
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult Word64 -> IO (GetResult Word64))
-> GetResult Word64 -> IO (GetResult Word64)
forall a b. (a -> b) -> a -> b
$ S -> Word64 -> GetResult Word64
forall a. S -> a -> GetResult a
GetResult (S
s {currPtr :: Ptr Word8
currPtr=S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8}) Word64
w
where
peek64 :: Ptr Word64 -> IO Word64
peek64 :: Ptr Word64 -> IO Word64
peek64 = Ptr Word64 -> IO Word64
forall a. Storable a => Ptr a -> IO a
peek
{-# INLINE dFloat #-}
dFloat :: Get Float
dFloat :: Get Float
dFloat = Word32 -> Float
wordToFloat (Word32 -> Float) -> Get Word32 -> Get Float
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word32
dBE32
{-# INLINE dDouble #-}
dDouble :: Get Double
dDouble :: Get Double
dDouble = Word64 -> Double
wordToDouble (Word64 -> Double) -> Get Word64 -> Get Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word64
dBE64
dLazyByteString_ :: Get L.ByteString
dLazyByteString_ :: Get ByteString
dLazyByteString_ = ByteString -> ByteString
L.fromStrict (ByteString -> ByteString) -> Get ByteString -> Get ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
dByteString_
dByteString_ :: Get B.ByteString
dByteString_ :: Get ByteString
dByteString_ = (Ptr Word8, [Int]) -> ByteString
chunksToByteString ((Ptr Word8, [Int]) -> ByteString)
-> Get (Ptr Word8, [Int]) -> Get ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (Ptr Word8, [Int])
getChunksInfo
dByteArray_ :: Get (ByteArray,Int)
dByteArray_ :: Get (ByteArray, Int)
dByteArray_ = (Ptr Word8, [Int]) -> (ByteArray, Int)
chunksToByteArray ((Ptr Word8, [Int]) -> (ByteArray, Int))
-> Get (Ptr Word8, [Int]) -> Get (ByteArray, Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (Ptr Word8, [Int])
getChunksInfo
getChunksInfo :: Get (Ptr Word8, [Int])
getChunksInfo :: Get (Ptr Word8, [Int])
getChunksInfo = (Ptr Word8 -> S -> IO (GetResult (Ptr Word8, [Int])))
-> Get (Ptr Word8, [Int])
forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get ((Ptr Word8 -> S -> IO (GetResult (Ptr Word8, [Int])))
-> Get (Ptr Word8, [Int]))
-> (Ptr Word8 -> S -> IO (GetResult (Ptr Word8, [Int])))
-> Get (Ptr Word8, [Int])
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
endPtr S
s -> do
let getChunks :: Ptr b -> ([Int] -> c) -> IO (Ptr b, c)
getChunks Ptr b
srcPtr [Int] -> c
l = do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s Int
8
!Int
n <- b -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (b -> Int) -> IO b -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr b -> IO b
forall a. Storable a => Ptr a -> IO a
peek Ptr b
srcPtr
if Int
nInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0
then (Ptr b, c) -> IO (Ptr b, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr b
srcPtr Ptr b -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1,[Int] -> c
l [])
else do
Ptr Word8 -> S -> Int -> IO ()
ensureBits Ptr Word8
endPtr S
s ((Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
8)
Ptr b -> ([Int] -> c) -> IO (Ptr b, c)
getChunks (Ptr b
srcPtr Ptr b -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)) ([Int] -> c
l ([Int] -> c) -> ([Int] -> [Int]) -> [Int] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int
nInt -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:))
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (S -> Int
usedBits S
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/=Int
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr Word8 -> S -> [Char] -> IO ()
forall a. Ptr Word8 -> S -> [Char] -> IO a
badEncoding Ptr Word8
endPtr S
s [Char]
"usedBits /= 0"
(Ptr Word8
currPtr',[Int]
ns) <- Ptr Word8 -> ([Int] -> [Int]) -> IO (Ptr Word8, [Int])
forall b c b.
(Integral b, Storable b) =>
Ptr b -> ([Int] -> c) -> IO (Ptr b, c)
getChunks (S -> Ptr Word8
currPtr S
s) [Int] -> [Int]
forall a. a -> a
id
GetResult (Ptr Word8, [Int]) -> IO (GetResult (Ptr Word8, [Int]))
forall (m :: * -> *) a. Monad m => a -> m a
return (GetResult (Ptr Word8, [Int]) -> IO (GetResult (Ptr Word8, [Int])))
-> GetResult (Ptr Word8, [Int])
-> IO (GetResult (Ptr Word8, [Int]))
forall a b. (a -> b) -> a -> b
$ S -> (Ptr Word8, [Int]) -> GetResult (Ptr Word8, [Int])
forall a. S -> a -> GetResult a
GetResult (S
s {currPtr :: Ptr Word8
currPtr=Ptr Word8
currPtr'}) (S -> Ptr Word8
currPtr S
s Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1,[Int]
ns)
{-# INLINE shR #-}
shR :: Bits a => a -> Int -> a
#ifdef ghcjs_HOST_OS
shR val 0 = val
shR val n = shift val (-n)
#else
shR :: a -> Int -> a
shR = a -> Int -> a
forall a. Bits a => a -> Int -> a
unsafeShiftR
#endif