#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
#include "MachDeps.h"
#endif
module Data.Binary.Get (
Get
, runGet
, skip
, uncheckedSkip
, lookAhead
, lookAheadM
, lookAheadE
, uncheckedLookAhead
, getBytes
, remaining
, isEmpty
, getWord8
, getByteString
, getLazyByteString
, getWord16be
, getWord16le
, getWord32be
, getWord32le
, getWord64be
, getWord64le
) where
import Control.Monad (liftM,when)
import Data.Maybe (isNothing)
import qualified Data.ByteString as B
import qualified Data.ByteString.Base as B
import qualified Data.ByteString.Lazy as L
import Foreign
#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
import GHC.Base
import GHC.Word
import GHC.Int
#endif
data S = S !L.ByteString
!Int64
newtype Get a = Get { unGet :: S -> (a, S ) }
instance Functor Get where
fmap f m = Get (\s -> let (a, s') = unGet m s
in (f a, s'))
instance Monad Get where
return a = Get (\s -> (a, s))
m >>= k = Get (\s -> let (a, s') = unGet m s
in unGet (k a) s')
fail = failDesc
get :: Get S
get = Get (\s -> (s, s))
put :: S -> Get ()
put s = Get (\_ -> ((), s))
runGet :: Get a -> L.ByteString -> a
runGet m str = case unGet m (S str 0) of (a, _) -> a
failDesc :: String -> Get a
failDesc err = do
S _ bytes <- get
Get (error (err ++ ". Failed reading at byte position " ++ show bytes))
skip :: Int -> Get ()
skip n = readN n (const ())
uncheckedSkip :: Int -> Get ()
uncheckedSkip n = do
S s bytes <- get
let rest = L.drop (fromIntegral n) s
put $! S rest (bytes + (fromIntegral n))
return ()
lookAhead :: Get a -> Get a
lookAhead ga = do
s <- get
a <- ga
put s
return a
lookAheadM :: Get (Maybe a) -> Get (Maybe a)
lookAheadM gma = do
s <- get
ma <- gma
when (isNothing ma) $
put s
return ma
lookAheadE :: Get (Either a b) -> Get (Either a b)
lookAheadE gea = do
s <- get
ea <- gea
case ea of
Left _ -> put s
_ -> return ()
return ea
uncheckedLookAhead :: Int -> Get L.ByteString
uncheckedLookAhead n = do
S s _ <- get
return $ L.take (fromIntegral n) s
remaining :: Get Int64
remaining = do
S s _ <- get
return (L.length s)
isEmpty :: Get Bool
isEmpty = do
S s _ <- get
return (L.null s)
takeExactly :: Int -> L.ByteString -> Get L.ByteString
takeExactly n bs
| l == n = return bs
| otherwise = fail $ concat [ "Data.Binary.Get.takeExactly: Wanted "
, show n, " bytes, found ", show l, "." ]
where l = fromIntegral (L.length bs)
getBytes :: Int -> Get L.ByteString
getBytes n = do
S s bytes <- get
let (consuming, rest) = L.splitAt (fromIntegral n) s
put $! S rest (bytes + (fromIntegral n))
return consuming
readN :: Int -> (L.ByteString -> a) -> Get a
readN n f = liftM f (getBytes n >>= takeExactly n)
getByteString :: Int -> Get B.ByteString
getByteString n = readN (fromIntegral n) (B.concat . L.toChunks)
getLazyByteString :: Int -> Get L.ByteString
getLazyByteString n = readN n id
getWord8 :: Get Word8
getWord8 = readN 1 L.head
getWord16be :: Get Word16
getWord16be = do
s <- readN 2 (L.take 2)
return $! (fromIntegral (s `L.index` 0) `shiftl_w16` 8) .|.
(fromIntegral (s `L.index` 1))
getWord16le :: Get Word16
getWord16le = do
w1 <- liftM fromIntegral getWord8
w2 <- liftM fromIntegral getWord8
return $! w2 `shiftl_w16` 8 .|. w1
getWord32be :: Get Word32
getWord32be = do
s <- readN 4 (L.take 4)
return $! (fromIntegral (s `L.index` 0) `shiftl_w32` 24) .|.
(fromIntegral (s `L.index` 1) `shiftl_w32` 16) .|.
(fromIntegral (s `L.index` 2) `shiftl_w32` 8) .|.
(fromIntegral (s `L.index` 3) )
getWord32le :: Get Word32
getWord32le = do
w1 <- liftM fromIntegral getWord8
w2 <- liftM fromIntegral getWord8
w3 <- liftM fromIntegral getWord8
w4 <- liftM fromIntegral getWord8
return $! (w4 `shiftl_w32` 24) .|.
(w3 `shiftl_w32` 16) .|.
(w2 `shiftl_w32` 8) .|.
(w1)
getWord64be :: Get Word64
getWord64be = do
s <- readN 8 (L.take 8)
return $! (fromIntegral (s `L.index` 0) `shiftl_w64` 56) .|.
(fromIntegral (s `L.index` 1) `shiftl_w64` 48) .|.
(fromIntegral (s `L.index` 2) `shiftl_w64` 40) .|.
(fromIntegral (s `L.index` 3) `shiftl_w64` 32) .|.
(fromIntegral (s `L.index` 4) `shiftl_w64` 24) .|.
(fromIntegral (s `L.index` 5) `shiftl_w64` 16) .|.
(fromIntegral (s `L.index` 6) `shiftl_w64` 8) .|.
(fromIntegral (s `L.index` 7) )
getWord64le :: Get Word64
getWord64le = do
w1 <- liftM fromIntegral getWord8
w2 <- liftM fromIntegral getWord8
w3 <- liftM fromIntegral getWord8
w4 <- liftM fromIntegral getWord8
w5 <- liftM fromIntegral getWord8
w6 <- liftM fromIntegral getWord8
w7 <- liftM fromIntegral getWord8
w8 <- liftM fromIntegral getWord8
return $! (w8 `shiftl_w64` 56) .|.
(w7 `shiftl_w64` 48) .|.
(w6 `shiftl_w64` 40) .|.
(w5 `shiftl_w64` 32) .|.
(w4 `shiftl_w64` 24) .|.
(w3 `shiftl_w64` 16) .|.
(w2 `shiftl_w64` 8) .|.
(w1)
shiftl_w16 :: Word16 -> Int -> Word16
shiftl_w32 :: Word32 -> Int -> Word32
shiftl_w64 :: Word64 -> Int -> Word64
#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
shiftl_w16 (W16# w) (I# i) = W16# (w `uncheckedShiftL#` i)
shiftl_w32 (W32# w) (I# i) = W32# (w `uncheckedShiftL#` i)
#if WORD_SIZE_IN_BITS < 64
shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL64#` i)
foreign import ccall unsafe "stg_uncheckedShiftL64"
uncheckedShiftL64# :: Word64# -> Int# -> Word64#
#else
shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL#` i)
#endif
#else
shiftl_w16 = shiftL
shiftl_w32 = shiftL
shiftl_w64 = shiftL
#endif