{-# LANGUAGE CPP        #-}
{-# LANGUAGE MagicHash  #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE BangPatterns #-}

-----------------------------------------------------------------------------
-- |
-- Module      : Data.Serialize.Get
-- Copyright   : Lennart Kolmodin, Galois Inc. 2009
-- License     : BSD3-style (see LICENSE)
--
-- Maintainer  : Trevor Elliott <trevor@galois.com>
-- Stability   :
-- Portability :
--
-- The Get monad. A monad for efficiently building structures from
-- strict ByteStrings
--
-----------------------------------------------------------------------------

#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
#include "MachDeps.h"
#endif

module Data.Serialize.Get (

    -- * The Get type
      Get
    , runGet
    , runGetLazy
    , runGetState
    , runGetLazyState

    -- ** Incremental interface
    , Result(..)
    , runGetPartial
    , runGetChunk

    -- * Parsing
    , ensure
    , isolate
    , label
    , skip
    , uncheckedSkip
    , lookAhead
    , lookAheadM
    , lookAheadE
    , uncheckedLookAhead
    , bytesRead

    -- * Utility
    , getBytes
    , remaining
    , isEmpty

    -- * Parsing particular types
    , getWord8
    , getInt8

    -- ** ByteStrings
    , getByteString
    , getLazyByteString
    , getShortByteString

    -- ** Big-endian reads
    , getWord16be
    , getWord32be
    , getWord64be
    , getInt16be
    , getInt32be
    , getInt64be

    -- ** Little-endian reads
    , getWord16le
    , getWord32le
    , getWord64le
    , getInt16le
    , getInt32le
    , getInt64le

    -- ** Host-endian, unaligned reads
    , getWordhost
    , getWord16host
    , getWord32host
    , getWord64host

    -- ** Containers
    , getTwoOf
    , getListOf
    , getIArrayOf
    , getTreeOf
    , getSeqOf
    , getMapOf
    , getIntMapOf
    , getSetOf
    , getIntSetOf
    , getMaybeOf
    , getEitherOf
    , getNested
  ) where

import qualified Control.Applicative as A
import qualified Control.Monad as M
import Control.Monad (unless)
import qualified Control.Monad.Fail as Fail
import Data.Array.IArray (IArray,listArray)
import Data.Ix (Ix)
import Data.List (intercalate)
import Data.Maybe (isNothing,fromMaybe)
import Foreign
import System.IO.Unsafe (unsafeDupablePerformIO)

import qualified Data.ByteString          as B
import qualified Data.ByteString.Internal as B
import qualified Data.ByteString.Unsafe   as B
import qualified Data.ByteString.Lazy     as L
import qualified Data.ByteString.Short    as BS
import qualified Data.IntMap              as IntMap
import qualified Data.IntSet              as IntSet
import qualified Data.Map                 as Map
import qualified Data.Sequence            as Seq
import qualified Data.Set                 as Set
import qualified Data.Tree                as T

#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
import GHC.Base
import GHC.Word
#endif

-- | The result of a parse.
data Result r = Fail String B.ByteString
              -- ^ The parse failed. The 'String' is the
              --   message describing the error, if any.
              | Partial (B.ByteString -> Result r)
              -- ^ Supply this continuation with more input so that
              --   the parser can resume. To indicate that no more
              --   input is available, use an 'B.empty' string.
              | Done r B.ByteString
              -- ^ The parse succeeded.  The 'B.ByteString' is the
              --   input that had not yet been consumed (if any) when
              --   the parse succeeded.

instance Show r => Show (Result r) where
    show :: Result r -> String
show (Fail String
msg ByteString
_) = String
"Fail " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show String
msg
    show (Partial ByteString -> Result r
_)  = String
"Partial _"
    show (Done r
r ByteString
bs)  = String
"Done " String -> ShowS
forall a. [a] -> [a] -> [a]
++ r -> String
forall a. Show a => a -> String
show r
r String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ByteString -> String
forall a. Show a => a -> String
show ByteString
bs

instance Functor Result where
    fmap :: (a -> b) -> Result a -> Result b
fmap a -> b
_ (Fail String
msg ByteString
rest) = String -> ByteString -> Result b
forall r. String -> ByteString -> Result r
Fail String
msg ByteString
rest
    fmap a -> b
f (Partial ByteString -> Result a
k)     = (ByteString -> Result b) -> Result b
forall r. (ByteString -> Result r) -> Result r
Partial ((a -> b) -> Result a -> Result b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Result a -> Result b)
-> (ByteString -> Result a) -> ByteString -> Result b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Result a
k)
    fmap a -> b
f (Done a
r ByteString
bs)     = b -> ByteString -> Result b
forall r. r -> ByteString -> Result r
Done (a -> b
f a
r) ByteString
bs

-- | The Get monad is an Exception and State monad.
newtype Get a = Get
  { Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet :: forall r. Input -> Buffer -> More
                    -> Int -> Failure r
                    -> Success a r -> Result r }

type Input  = B.ByteString
type Buffer = Maybe B.ByteString

emptyBuffer :: Buffer
emptyBuffer :: Buffer
emptyBuffer  = ByteString -> Buffer
forall a. a -> Maybe a
Just ByteString
B.empty

extendBuffer :: Buffer -> B.ByteString -> Buffer
extendBuffer :: Buffer -> ByteString -> Buffer
extendBuffer Buffer
buf ByteString
chunk =
  do ByteString
bs <- Buffer
buf
     ByteString -> Buffer
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Buffer) -> ByteString -> Buffer
forall a b. (a -> b) -> a -> b
$! ByteString
bs ByteString -> ByteString -> ByteString
`B.append` ByteString
chunk
{-# INLINE extendBuffer #-}

append :: Buffer -> Buffer -> Buffer
append :: Buffer -> Buffer -> Buffer
append Buffer
l Buffer
r = ByteString -> ByteString -> ByteString
B.append (ByteString -> ByteString -> ByteString)
-> Buffer -> Maybe (ByteString -> ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Buffer
l Maybe (ByteString -> ByteString) -> Buffer -> Buffer
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
A.<*> Buffer
r
{-# INLINE append #-}

bufferBytes :: Buffer -> B.ByteString
bufferBytes :: Buffer -> ByteString
bufferBytes  = ByteString -> Buffer -> ByteString
forall a. a -> Maybe a -> a
fromMaybe ByteString
B.empty
{-# INLINE bufferBytes #-}

type Failure   r = Input -> Buffer -> More -> [String] -> String -> Result r
type Success a r = Input -> Buffer -> More -> Int      -> a      -> Result r

-- | Have we read all available input?
data More
  = Complete
  | Incomplete (Maybe Int)
    deriving (More -> More -> Bool
(More -> More -> Bool) -> (More -> More -> Bool) -> Eq More
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: More -> More -> Bool
$c/= :: More -> More -> Bool
== :: More -> More -> Bool
$c== :: More -> More -> Bool
Eq)

moreLength :: More -> Int
moreLength :: More -> Int
moreLength More
m = case More
m of
  More
Complete      -> Int
0
  Incomplete Maybe Int
mb -> Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 Maybe Int
mb

instance Functor Get where
    fmap :: (a -> b) -> Get a -> Get b
fmap a -> b
p Get a
m =           (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
  ByteString
  -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
 -> Get b)
-> (forall r.
    ByteString
    -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
      Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success a r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf (Success a r -> Result r) -> Success a r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a
a  -> Success b r
ks ByteString
s1 Buffer
b1 More
m1 Int
w1 (a -> b
p a
a)

instance A.Applicative Get where
    pure :: a -> Get a
pure a
a = (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
  ByteString
  -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
 -> Get a)
-> (forall r.
    ByteString
    -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w Failure r
_ Success a r
ks -> Success a r
ks ByteString
s0 Buffer
b0 More
m0 Int
w a
a
    {-# INLINE pure #-}

    Get (a -> b)
f <*> :: Get (a -> b) -> Get a -> Get b
<*> Get a
x =            (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
  ByteString
  -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
 -> Get b)
-> (forall r.
    ByteString
    -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
      Get (a -> b)
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success (a -> b) r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get (a -> b)
f ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf (Success (a -> b) r -> Result r) -> Success (a -> b) r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a -> b
g     ->
      Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success a r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
x ByteString
s1 Buffer
b1 More
m1 Int
w1 Failure r
kf (Success a r -> Result r) -> Success a r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s2 Buffer
b2 More
m2 Int
w2 a
y  -> Success b r
ks ByteString
s2 Buffer
b2 More
m2 Int
w2 (a -> b
g a
y)
    {-# INLINE (<*>) #-}

    Get a
m *> :: Get a -> Get b -> Get b
*> Get b
k =             (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
  ByteString
  -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
 -> Get b)
-> (forall r.
    ByteString
    -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
      Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success a r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf (Success a r -> Result r) -> Success a r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a
_     -> Get b
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success b r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get b
k ByteString
s1 Buffer
b1 More
m1 Int
w1 Failure r
kf Success b r
ks
    {-# INLINE (*>) #-}

instance A.Alternative Get where
    empty :: Get a
empty = String -> Get a
forall a. String -> Get a
failDesc String
"empty"
    {-# INLINE empty #-}

    <|> :: Get a -> Get a -> Get a
(<|>) = Get a -> Get a -> Get a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
M.mplus
    {-# INLINE (<|>) #-}

-- Definition directly from Control.Monad.State.Strict
instance Monad Get where
    return :: a -> Get a
return = a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
A.pure
    {-# INLINE return #-}

    Get a
m >>= :: Get a -> (a -> Get b) -> Get b
>>= a -> Get b
g  =           (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
  ByteString
  -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
 -> Get b)
-> (forall r.
    ByteString
    -> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
      Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success a r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf (Success a r -> Result r) -> Success a r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a
a     -> Get b
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success b r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet (a -> Get b
g a
a) ByteString
s1 Buffer
b1 More
m1 Int
w1 Failure r
kf Success b r
ks
    {-# INLINE (>>=) #-}

    >> :: Get a -> Get b -> Get b
(>>) = Get a -> Get b -> Get b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(A.*>)
    {-# INLINE (>>) #-}

#if !(MIN_VERSION_base(4,13,0))
    fail     = Fail.fail
    {-# INLINE fail #-}
#endif

instance Fail.MonadFail Get where
    fail :: String -> Get a
fail     = String -> Get a
forall a. String -> Get a
failDesc
    {-# INLINE fail #-}

instance M.MonadPlus Get where
    mzero :: Get a
mzero     = String -> Get a
forall a. String -> Get a
failDesc String
"mzero"
    {-# INLINE mzero #-}
-- TODO: Test this!
    mplus :: Get a -> Get a -> Get a
mplus Get a
a Get a
b =
      (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
  ByteString
  -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
 -> Get a)
-> (forall r.
    ByteString
    -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a b. (a -> b) -> a -> b
$ \ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success a r
ks ->
        let ks' :: Success a r
ks' ByteString
s1 Buffer
b1        = Success a r
ks ByteString
s1 (Buffer
b0 Buffer -> Buffer -> Buffer
`append` Buffer
b1)
            kf' :: p -> Buffer -> More -> [String] -> String -> Result r
kf' p
_  Buffer
b1 More
m1     = Failure r
kf (ByteString
s0 ByteString -> ByteString -> ByteString
`B.append` Buffer -> ByteString
bufferBytes Buffer
b1)
                                  (Buffer
b0 Buffer -> Buffer -> Buffer
`append` Buffer
b1) More
m1
            try :: p -> Buffer -> More -> p -> p -> Result r
try p
_  Buffer
b1 More
m1 p
_ p
_ = Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success a r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
b (ByteString
s0 ByteString -> ByteString -> ByteString
`B.append` Buffer -> ByteString
bufferBytes Buffer
b1)
                                       Buffer
b1 More
m1 Int
w0 Failure r
forall p. p -> Buffer -> More -> [String] -> String -> Result r
kf' Success a r
ks'
         in Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success a r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
a ByteString
s0 Buffer
emptyBuffer More
m0 Int
w0 Failure r
forall p p p. p -> Buffer -> More -> p -> p -> Result r
try Success a r
ks'
    {-# INLINE mplus #-}


------------------------------------------------------------------------

formatTrace :: [String] -> String
formatTrace :: [String] -> String
formatTrace [] = String
"Empty call stack"
formatTrace [String]
ls = String
"From:\t" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n\t" [String]
ls String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"\n"

get :: Get B.ByteString
get :: Get ByteString
get  = (forall r.
 ByteString
 -> Buffer
 -> More
 -> Int
 -> Failure r
 -> Success ByteString r
 -> Result r)
-> Get ByteString
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ByteString
s0 Buffer
b0 More
m0 Int
w Failure r
_ Success ByteString r
k -> Success ByteString r
k ByteString
s0 Buffer
b0 More
m0 Int
w ByteString
s0)
{-# INLINE get #-}

put :: B.ByteString -> Int -> Get ()
put :: ByteString -> Int -> Get ()
put ByteString
s !Int
w = (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success () r -> Result r)
-> Get ()
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ByteString
_ Buffer
b0 More
m Int
_ Failure r
_ Success () r
k -> Success () r
k ByteString
s Buffer
b0 More
m Int
w ())
{-# INLINE put #-}

label :: String -> Get a -> Get a
label :: String -> Get a -> Get a
label String
l Get a
m =
  (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
  ByteString
  -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
 -> Get a)
-> (forall r.
    ByteString
    -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success a r
ks ->
    let kf' :: Failure r
kf' ByteString
s1 Buffer
b1 More
m1 [String]
ls = Failure r
kf ByteString
s1 Buffer
b1 More
m1 (String
lString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
ls)
     in Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success a r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf' Success a r
ks

finalK :: Success a a
finalK :: Success a a
finalK ByteString
s Buffer
_ More
_ Int
_ a
a = a -> ByteString -> Result a
forall r. r -> ByteString -> Result r
Done a
a ByteString
s

failK :: Failure a
failK :: Failure a
failK ByteString
s Buffer
b More
_ [String]
ls String
msg =
  String -> ByteString -> Result a
forall r. String -> ByteString -> Result r
Fail ([String] -> String
unlines [String
msg, [String] -> String
formatTrace [String]
ls]) (ByteString
s ByteString -> ByteString -> ByteString
`B.append` Buffer -> ByteString
bufferBytes Buffer
b)

-- | Run the Get monad applies a 'get'-based parser on the input ByteString
runGet :: Get a -> B.ByteString -> Either String a
runGet :: Get a -> ByteString -> Either String a
runGet Get a
m ByteString
str =
  case Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure a
-> Success a a
-> Result a
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
str Buffer
forall a. Maybe a
Nothing More
Complete Int
0 Failure a
forall a. Failure a
failK Success a a
forall a. Success a a
finalK of
    Fail String
i ByteString
_  -> String -> Either String a
forall a b. a -> Either a b
Left String
i
    Done a
a ByteString
_  -> a -> Either String a
forall a b. b -> Either a b
Right a
a
    Partial{} -> String -> Either String a
forall a b. a -> Either a b
Left String
"Failed reading: Internal error: unexpected Partial."
{-# INLINE runGet #-}

-- | Run the get monad on a single chunk, providing an optional length for the
-- remaining, unseen input, with Nothing indicating that it's not clear how much
-- input is left.  For example, with a lazy ByteString, the optional length
-- represents the sum of the lengths of all remaining chunks.
runGetChunk :: Get a -> Maybe Int -> B.ByteString -> Result a
runGetChunk :: Get a -> Maybe Int -> ByteString -> Result a
runGetChunk Get a
m Maybe Int
mbLen ByteString
str = Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure a
-> Success a a
-> Result a
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
str Buffer
forall a. Maybe a
Nothing (Maybe Int -> More
Incomplete Maybe Int
mbLen) Int
0 Failure a
forall a. Failure a
failK Success a a
forall a. Success a a
finalK
{-# INLINE runGetChunk #-}

-- | Run the Get monad applies a 'get'-based parser on the input ByteString
runGetPartial :: Get a -> B.ByteString -> Result a
runGetPartial :: Get a -> ByteString -> Result a
runGetPartial Get a
m = Get a -> Maybe Int -> ByteString -> Result a
forall a. Get a -> Maybe Int -> ByteString -> Result a
runGetChunk Get a
m Maybe Int
forall a. Maybe a
Nothing
{-# INLINE runGetPartial #-}

-- | Run the Get monad applies a 'get'-based parser on the input
-- ByteString, starting at the specified offset. In addition to the result of get
-- it returns the rest of the input.
runGetState :: Get a -> B.ByteString -> Int
            -> Either String (a, B.ByteString)
runGetState :: Get a -> ByteString -> Int -> Either String (a, ByteString)
runGetState Get a
m ByteString
str Int
off = case Get a -> ByteString -> Int -> (Either String a, ByteString)
forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
str Int
off of
  (Right a
a,ByteString
bs) -> (a, ByteString) -> Either String (a, ByteString)
forall a b. b -> Either a b
Right (a
a,ByteString
bs)
  (Left String
i,ByteString
_)   -> String -> Either String (a, ByteString)
forall a b. a -> Either a b
Left String
i
{-# INLINE runGetState #-}

-- | Run the Get monad applies a 'get'-based parser on the input
-- ByteString, starting at the specified offset. In addition to the result of get
-- it returns the rest of the input, even in the event of a failure.
runGetState' :: Get a -> B.ByteString -> Int
             -> (Either String a, B.ByteString)
runGetState' :: Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
str Int
off =
  case Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure a
-> Success a a
-> Result a
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m (Int -> ByteString -> ByteString
B.drop Int
off ByteString
str) Buffer
forall a. Maybe a
Nothing More
Complete Int
0 Failure a
forall a. Failure a
failK Success a a
forall a. Success a a
finalK of
    Fail String
i ByteString
bs -> (String -> Either String a
forall a b. a -> Either a b
Left String
i,ByteString
bs)
    Done a
a ByteString
bs -> (a -> Either String a
forall a b. b -> Either a b
Right a
a, ByteString
bs)
    Partial{} -> (String -> Either String a
forall a b. a -> Either a b
Left String
"Failed reading: Internal error: unexpected Partial.",ByteString
B.empty)
{-# INLINE runGetState' #-}



-- Lazy Get --------------------------------------------------------------------

runGetLazy' :: Get a -> L.ByteString -> (Either String a,L.ByteString)
runGetLazy' :: Get a -> ByteString -> (Either String a, ByteString)
runGetLazy' Get a
m ByteString
lstr =
  case ByteString -> [ByteString]
L.toChunks ByteString
lstr of
    [ByteString
c]  -> (Either String a, ByteString) -> (Either String a, ByteString)
forall a. (a, ByteString) -> (a, ByteString)
wrapStrict (Get a -> ByteString -> Int -> (Either String a, ByteString)
forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
c       Int
0)
    []   -> (Either String a, ByteString) -> (Either String a, ByteString)
forall a. (a, ByteString) -> (a, ByteString)
wrapStrict (Get a -> ByteString -> Int -> (Either String a, ByteString)
forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
B.empty Int
0)
    ByteString
c:[ByteString]
cs -> Result a -> [ByteString] -> (Either String a, ByteString)
forall b. Result b -> [ByteString] -> (Either String b, ByteString)
loop (Get a -> Maybe Int -> ByteString -> Result a
forall a. Get a -> Maybe Int -> ByteString -> Result a
runGetChunk Get a
m (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
c)) ByteString
c) [ByteString]
cs
  where
  len :: Int
len = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
L.length ByteString
lstr)

  wrapStrict :: (a, ByteString) -> (a, ByteString)
wrapStrict (a
e,ByteString
s) = (a
e,[ByteString] -> ByteString
L.fromChunks [ByteString
s])

  loop :: Result b -> [ByteString] -> (Either String b, ByteString)
loop Result b
result [ByteString]
chunks = case Result b
result of

    Fail String
str ByteString
rest -> (String -> Either String b
forall a b. a -> Either a b
Left String
str, [ByteString] -> ByteString
L.fromChunks (ByteString
rest ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
chunks))
    Partial ByteString -> Result b
k     -> case [ByteString]
chunks of
                       ByteString
c:[ByteString]
cs -> Result b -> [ByteString] -> (Either String b, ByteString)
loop (ByteString -> Result b
k ByteString
c)       [ByteString]
cs
                       []   -> Result b -> [ByteString] -> (Either String b, ByteString)
loop (ByteString -> Result b
k ByteString
B.empty) []

    Done b
r ByteString
rest   -> (b -> Either String b
forall a b. b -> Either a b
Right b
r, [ByteString] -> ByteString
L.fromChunks (ByteString
rest ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
chunks))
{-# INLINE runGetLazy' #-}

-- | Run the Get monad over a Lazy ByteString.  Note that this will not run the
-- Get parser lazily, but will operate on lazy ByteStrings.
runGetLazy :: Get a -> L.ByteString -> Either String a
runGetLazy :: Get a -> ByteString -> Either String a
runGetLazy Get a
m ByteString
lstr = (Either String a, ByteString) -> Either String a
forall a b. (a, b) -> a
fst (Get a -> ByteString -> (Either String a, ByteString)
forall a. Get a -> ByteString -> (Either String a, ByteString)
runGetLazy' Get a
m ByteString
lstr)
{-# INLINE runGetLazy #-}

-- | Run the Get monad over a Lazy ByteString.  Note that this does not run the
-- Get parser lazily, but will operate on lazy ByteStrings.
runGetLazyState :: Get a -> L.ByteString -> Either String (a,L.ByteString)
runGetLazyState :: Get a -> ByteString -> Either String (a, ByteString)
runGetLazyState Get a
m ByteString
lstr = case Get a -> ByteString -> (Either String a, ByteString)
forall a. Get a -> ByteString -> (Either String a, ByteString)
runGetLazy' Get a
m ByteString
lstr of
  (Right a
a,ByteString
rest) -> (a, ByteString) -> Either String (a, ByteString)
forall a b. b -> Either a b
Right (a
a,ByteString
rest)
  (Left String
err,ByteString
_)   -> String -> Either String (a, ByteString)
forall a b. a -> Either a b
Left String
err
{-# INLINE runGetLazyState #-}

------------------------------------------------------------------------

-- | If at least @n@ bytes of input are available, return the current
--   input, otherwise fail.
{-# INLINE ensure #-}
ensure :: Int -> Get B.ByteString
ensure :: Int -> Get ByteString
ensure Int
n0 = Int
n0 Int -> Get ByteString -> Get ByteString
`seq` (forall r.
 ByteString
 -> Buffer
 -> More
 -> Int
 -> Failure r
 -> Success ByteString r
 -> Result r)
-> Get ByteString
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
  ByteString
  -> Buffer
  -> More
  -> Int
  -> Failure r
  -> Success ByteString r
  -> Result r)
 -> Get ByteString)
-> (forall r.
    ByteString
    -> Buffer
    -> More
    -> Int
    -> Failure r
    -> Success ByteString r
    -> Result r)
-> Get ByteString
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success ByteString r
ks -> let
    n' :: Int
n' = Int
n0 Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
s0
    in if Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0
        then Success ByteString r
ks ByteString
s0 Buffer
b0 More
m0 Int
w0 ByteString
s0
        else Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> Int
-> Failure r
-> Success ByteString r
-> Result r
forall t r.
Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
getMore Int
n' ByteString
s0 [] Buffer
b0 More
m0 Int
w0 Failure r
kf Success ByteString r
ks
    where
        -- The "accumulate and concat" pattern here is important not to incur
        -- in quadratic behavior, see <https://github.com/GaloisInc/cereal/issues/48>

        finalInput :: ByteString -> [ByteString] -> ByteString
finalInput ByteString
s0 [ByteString]
ss = [ByteString] -> ByteString
B.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse (ByteString
s0 ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
ss))
        finalBuffer :: Buffer -> ByteString -> [ByteString] -> Buffer
finalBuffer Buffer
b0 ByteString
s0 [ByteString]
ss = Buffer -> ByteString -> Buffer
extendBuffer Buffer
b0 ([ByteString] -> ByteString
B.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse ([ByteString] -> [ByteString]
forall a. [a] -> [a]
init (ByteString
s0 ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
ss))))
        getMore :: Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
getMore !Int
n ByteString
s0 [ByteString]
ss Buffer
b0 More
m0 t
w0 ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks = let
            tooFewBytes :: Result r
tooFewBytes = let
                !s :: ByteString
s = ByteString -> [ByteString] -> ByteString
finalInput ByteString
s0 [ByteString]
ss
                !b :: Buffer
b = Buffer -> ByteString -> [ByteString] -> Buffer
finalBuffer Buffer
b0 ByteString
s0 [ByteString]
ss
                in ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString
s Buffer
b More
m0 [String
"demandInput"] String
"too few bytes"
            in case More
m0 of
                More
Complete -> Result r
tooFewBytes
                Incomplete Maybe Int
mb -> (ByteString -> Result r) -> Result r
forall r. (ByteString -> Result r) -> Result r
Partial ((ByteString -> Result r) -> Result r)
-> (ByteString -> Result r) -> Result r
forall a b. (a -> b) -> a -> b
$ \ByteString
s ->
                    if ByteString -> Bool
B.null ByteString
s
                        then Result r
tooFewBytes
                        else let
                            !mb' :: Maybe Int
mb' = case Maybe Int
mb of
                                Just Int
l -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$! Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
s
                                Maybe Int
Nothing -> Maybe Int
forall a. Maybe a
Nothing
                            in Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
checkIfEnough Int
n ByteString
s (ByteString
s0 ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
ss) Buffer
b0 (Maybe Int -> More
Incomplete Maybe Int
mb') t
w0 ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks

        checkIfEnough :: Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
checkIfEnough !Int
n ByteString
s0 [ByteString]
ss Buffer
b0 More
m0 t
w0 ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks = let
            n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
s0
            in if Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0
                then let
                    !s :: ByteString
s = ByteString -> [ByteString] -> ByteString
finalInput ByteString
s0 [ByteString]
ss
                    !b :: Buffer
b = Buffer -> ByteString -> [ByteString] -> Buffer
finalBuffer Buffer
b0 ByteString
s0 [ByteString]
ss
                    in ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks ByteString
s Buffer
b More
m0 t
w0 ByteString
s
                else Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
getMore Int
n' ByteString
s0 [ByteString]
ss Buffer
b0 More
m0 t
w0 ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks

-- | Isolate an action to operating within a fixed block of bytes.  The action
--   is required to consume all the bytes that it is isolated to.
isolate :: Int -> Get a -> Get a
isolate :: Int -> Get a -> Get a
isolate Int
n Get a
m = do
  Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
M.when (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (String -> Get ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Attempted to isolate a negative number of bytes")
  ByteString
s <- Int -> Get ByteString
ensure Int
n
  let (ByteString
s',ByteString
rest) = Int -> ByteString -> (ByteString, ByteString)
B.splitAt Int
n ByteString
s
  Int
cur <- Get Int
bytesRead
  ByteString -> Int -> Get ()
put ByteString
s' Int
cur
  a
a    <- Get a
m
  ByteString
used <- Get ByteString
get
  Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Bool
B.null ByteString
used) (String -> Get ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"not all bytes parsed in isolate")
  ByteString -> Int -> Get ()
put ByteString
rest (Int
cur Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n)
  a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a

failDesc :: String -> Get a
failDesc :: String -> Get a
failDesc String
err = do
    let msg :: String
msg = String
"Failed reading: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
err
    (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ByteString
s0 Buffer
b0 More
m0 Int
_ Failure r
kf Success a r
_ -> Failure r
kf ByteString
s0 Buffer
b0 More
m0 [] String
msg)

-- | Skip ahead @n@ bytes. Fails if fewer than @n@ bytes are available.
skip :: Int -> Get ()
skip :: Int -> Get ()
skip Int
n = do
  ByteString
s <- Int -> Get ByteString
ensure Int
n
  Int
cur <- Get Int
bytesRead
  ByteString -> Int -> Get ()
put (Int -> ByteString -> ByteString
B.drop Int
n ByteString
s) (Int
cur Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n)

-- | Skip ahead up to @n@ bytes in the current chunk. No error if there aren't
-- enough bytes, or if less than @n@ bytes are skipped.
uncheckedSkip :: Int -> Get ()
uncheckedSkip :: Int -> Get ()
uncheckedSkip Int
n = do
    ByteString
s <- Get ByteString
get
    Int
cur <- Get Int
bytesRead
    ByteString -> Int -> Get ()
put (Int -> ByteString -> ByteString
B.drop Int
n ByteString
s) (Int
cur Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n)

-- | Run @ga@, but return without consuming its input.
-- Fails if @ga@ fails.
lookAhead :: Get a -> Get a
lookAhead :: Get a -> Get a
lookAhead Get a
ga = (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
  ByteString
  -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
 -> Get a)
-> (forall r.
    ByteString
    -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success a r
ks ->
  -- the new continuation extends the old input with the new buffered bytes, and
  -- appends the new buffer to the old one, if there was one.
  let ks' :: p -> Buffer -> More -> Int -> a -> Result r
ks' p
_ Buffer
b1 = Success a r
ks (ByteString
s0 ByteString -> ByteString -> ByteString
`B.append` Buffer -> ByteString
bufferBytes Buffer
b1) (Buffer
b0 Buffer -> Buffer -> Buffer
`append` Buffer
b1)
      kf' :: p -> Buffer -> More -> [String] -> String -> Result r
kf' p
_ Buffer
b1 = Failure r
kf ByteString
s0 (Buffer
b0 Buffer -> Buffer -> Buffer
`append` Buffer
b1)
   in Get a
-> ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success a r
-> Result r
forall a.
Get a
-> forall r.
   ByteString
   -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
ga ByteString
s0 Buffer
emptyBuffer More
m0 Int
w0 Failure r
forall p. p -> Buffer -> More -> [String] -> String -> Result r
kf' Success a r
forall p. p -> Buffer -> More -> Int -> a -> Result r
ks'

-- | Like 'lookAhead', but consume the input if @gma@ returns 'Just _'.
-- Fails if @gma@ fails.
lookAheadM :: Get (Maybe a) -> Get (Maybe a)
lookAheadM :: Get (Maybe a) -> Get (Maybe a)
lookAheadM Get (Maybe a)
gma = do
    ByteString
s <- Get ByteString
get
    Int
pre <- Get Int
bytesRead
    Maybe a
ma <- Get (Maybe a)
gma
    Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
M.when (Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing Maybe a
ma) (ByteString -> Int -> Get ()
put ByteString
s Int
pre)
    Maybe a -> Get (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
ma

-- | Like 'lookAhead', but consume the input if @gea@ returns 'Right _'.
-- Fails if @gea@ fails.
lookAheadE :: Get (Either a b) -> Get (Either a b)
lookAheadE :: Get (Either a b) -> Get (Either a b)
lookAheadE Get (Either a b)
gea = do
    ByteString
s <- Get ByteString
get
    Int
pre <- Get Int
bytesRead
    Either a b
ea <- Get (Either a b)
gea
    case Either a b
ea of
        Left a
_ -> ByteString -> Int -> Get ()
put ByteString
s Int
pre
        Either a b
_      -> () -> Get ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    Either a b -> Get (Either a b)
forall (m :: * -> *) a. Monad m => a -> m a
return Either a b
ea

-- | Get the next up to @n@ bytes as a ByteString until end of this chunk,
-- without consuming them.
uncheckedLookAhead :: Int -> Get B.ByteString
uncheckedLookAhead :: Int -> Get ByteString
uncheckedLookAhead Int
n = do
    ByteString
s <- Get ByteString
get
    ByteString -> Get ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> ByteString -> ByteString
B.take Int
n ByteString
s)

------------------------------------------------------------------------
-- Utility

-- | Get the number of remaining unparsed bytes.  Useful for checking whether
-- all input has been consumed.
--
-- WARNING: when run with @runGetPartial@, remaining will only return the number
-- of bytes that are remaining in the current input.
remaining :: Get Int
remaining :: Get Int
remaining = (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success Int r -> Result r)
-> Get Int
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
_ Success Int r
ks -> Success Int r
ks ByteString
s0 Buffer
b0 More
m0 Int
w0 (ByteString -> Int
B.length ByteString
s0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ More -> Int
moreLength More
m0))

-- | Test whether all input has been consumed.
--
-- WARNING: when run with @runGetPartial@, isEmpty will only tell you if you're
-- at the end of the current chunk.
isEmpty :: Get Bool
isEmpty :: Get Bool
isEmpty = (forall r.
 ByteString
 -> Buffer
 -> More
 -> Int
 -> Failure r
 -> Success Bool r
 -> Result r)
-> Get Bool
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
_ Success Bool r
ks -> Success Bool r
ks ByteString
s0 Buffer
b0 More
m0 Int
w0 (ByteString -> Bool
B.null ByteString
s0 Bool -> Bool -> Bool
&& More -> Int
moreLength More
m0 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0))

------------------------------------------------------------------------
-- Utility with ByteStrings

-- | An efficient 'get' method for strict ByteStrings. Fails if fewer
-- than @n@ bytes are left in the input. This function creates a fresh
-- copy of the underlying bytes.
getByteString :: Int -> Get B.ByteString
getByteString :: Int -> Get ByteString
getByteString Int
n = do
  ByteString
bs <- Int -> Get ByteString
getBytes Int
n
  ByteString -> Get ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Get ByteString) -> ByteString -> Get ByteString
forall a b. (a -> b) -> a -> b
$! ByteString -> ByteString
B.copy ByteString
bs

getLazyByteString :: Int64 -> Get L.ByteString
getLazyByteString :: Int64 -> Get ByteString
getLazyByteString Int64
n = ByteString -> ByteString
f (ByteString -> ByteString) -> Get ByteString -> Get ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int -> Get ByteString
getByteString (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
n)
  where f :: ByteString -> ByteString
f ByteString
bs = [ByteString] -> ByteString
L.fromChunks [ByteString
bs]

getShortByteString :: Int -> Get BS.ShortByteString
getShortByteString :: Int -> Get ShortByteString
getShortByteString Int
n = do
  ByteString
bs <- Int -> Get ByteString
getBytes Int
n
  ShortByteString -> Get ShortByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ShortByteString -> Get ShortByteString)
-> ShortByteString -> Get ShortByteString
forall a b. (a -> b) -> a -> b
$! ByteString -> ShortByteString
BS.toShort ByteString
bs


------------------------------------------------------------------------
-- Helpers

-- | Pull @n@ bytes from the input, as a strict ByteString.
getBytes :: Int -> Get B.ByteString
getBytes :: Int -> Get ByteString
getBytes Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = String -> Get ByteString
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"getBytes: negative length requested"
getBytes Int
n = do
    ByteString
s <- Int -> Get ByteString
ensure Int
n
    let consume :: ByteString
consume = Int -> ByteString -> ByteString
B.unsafeTake Int
n ByteString
s
        rest :: ByteString
rest    = Int -> ByteString -> ByteString
B.unsafeDrop Int
n ByteString
s
        -- (consume,rest) = B.splitAt n s
    Int
cur <- Get Int
bytesRead
    ByteString -> Int -> Get ()
put ByteString
rest (Int
cur Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n)
    ByteString -> Get ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
consume
{-# INLINE getBytes #-}



------------------------------------------------------------------------
-- Primtives

-- helper, get a raw Ptr onto a strict ByteString copied out of the
-- underlying strict byteString.

getPtr :: Storable a => Int -> Get a
getPtr :: Int -> Get a
getPtr Int
n = do
    (ForeignPtr Word8
fp,Int
o,Int
_) <- ByteString -> (ForeignPtr Word8, Int, Int)
B.toForeignPtr (ByteString -> (ForeignPtr Word8, Int, Int))
-> Get ByteString -> Get (ForeignPtr Word8, Int, Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int -> Get ByteString
getBytes Int
n
    let k :: Ptr a -> IO a
k Ptr a
p = Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek (Ptr Any -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr (Ptr a
p Ptr a -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
o))
    a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (IO a -> a
forall a. IO a -> a
unsafeDupablePerformIO (ForeignPtr Word8 -> (Ptr Word8 -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fp Ptr Word8 -> IO a
forall a a. Storable a => Ptr a -> IO a
k))
{-# INLINE getPtr #-}

-----------------------------------------------------------------------

-- | Read a Int8 from the monad state
getInt8 :: Get Int8
getInt8 :: Get Int8
getInt8 = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
1
    Int8 -> Get Int8
forall (m :: * -> *) a. Monad m => a -> m a
return (Int8 -> Get Int8) -> Int8 -> Get Int8
forall a b. (a -> b) -> a -> b
$! Word8 -> Int8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Word8
B.unsafeHead ByteString
s)

-- | Read a Int16 in big endian format
getInt16be :: Get Int16
getInt16be :: Get Int16
getInt16be = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
2
    Int16 -> Get Int16
forall (m :: * -> *) a. Monad m => a -> m a
return (Int16 -> Get Int16) -> Int16 -> Get Int16
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) Int16 -> Int -> Int16
forall a. Bits a => a -> Int -> a
`shiftL` Int
8) Int16 -> Int16 -> Int16
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) )

-- | Read a Int16 in little endian format
getInt16le :: Get Int16
getInt16le :: Get Int16
getInt16le = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
2
    Int16 -> Get Int16
forall (m :: * -> *) a. Monad m => a -> m a
return (Int16 -> Get Int16) -> Int16 -> Get Int16
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Int16 -> Int -> Int16
forall a. Bits a => a -> Int -> a
`shiftL` Int
8) Int16 -> Int16 -> Int16
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )

-- | Read a Int32 in big endian format
getInt32be :: Get Int32
getInt32be :: Get Int32
getInt32be = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
4
    Int32 -> Get Int32
forall (m :: * -> *) a. Monad m => a -> m a
return (Int32 -> Get Int32) -> Int32 -> Get Int32
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`shiftL` Int
24) Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`shiftL` Int
16) Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`shiftL`  Int
8) Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) )

-- | Read a Int32 in little endian format
getInt32le :: Get Int32
getInt32le :: Get Int32
getInt32le = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
4
    Int32 -> Get Int32
forall (m :: * -> *) a. Monad m => a -> m a
return (Int32 -> Get Int32) -> Int32 -> Get Int32
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`shiftL` Int
24) Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`shiftL` Int
16) Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`shiftL`  Int
8) Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )

-- | Read a Int64 in big endian format
getInt64be :: Get Int64
getInt64be :: Get Int64
getInt64be = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
8
    Int64 -> Get Int64
forall (m :: * -> *) a. Monad m => a -> m a
return (Int64 -> Get Int64) -> Int64 -> Get Int64
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
56) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
48) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
40) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
32) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
4) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
24) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
5) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
16) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
6) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL`  Int
8) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
7) )

-- | Read a Int64 in little endian format
getInt64le :: Get Int64
getInt64le :: Get Int64
getInt64le = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
8
    Int64 -> Get Int64
forall (m :: * -> *) a. Monad m => a -> m a
return (Int64 -> Get Int64) -> Int64 -> Get Int64
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
7) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
56) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
6) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
48) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
5) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
40) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
4) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
32) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
24) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL` Int
16) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Int64 -> Int -> Int64
forall a. Bits a => a -> Int -> a
`shiftL`  Int
8) Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )

{-# INLINE getInt8    #-}
{-# INLINE getInt16be #-}
{-# INLINE getInt16le #-}
{-# INLINE getInt32be #-}
{-# INLINE getInt32le #-}
{-# INLINE getInt64be #-}
{-# INLINE getInt64le #-}

------------------------------------------------------------------------

-- | Read a Word8 from the monad state
getWord8 :: Get Word8
getWord8 :: Get Word8
getWord8 = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
1
    Word8 -> Get Word8
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Word8
B.unsafeHead ByteString
s)

-- | Read a Word16 in big endian format
getWord16be :: Get Word16
getWord16be :: Get Word16
getWord16be = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
2
    Word16 -> Get Word16
forall (m :: * -> *) a. Monad m => a -> m a
return (Word16 -> Get Word16) -> Word16 -> Get Word16
forall a b. (a -> b) -> a -> b
$! (Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) Word16 -> Int -> Word16
`shiftl_w16` Int
8) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1))

-- | Read a Word16 in little endian format
getWord16le :: Get Word16
getWord16le :: Get Word16
getWord16le = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
2
    Word16 -> Get Word16
forall (m :: * -> *) a. Monad m => a -> m a
return (Word16 -> Get Word16) -> Word16 -> Get Word16
forall a b. (a -> b) -> a -> b
$! (Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word16 -> Int -> Word16
`shiftl_w16` Int
8) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )

-- | Read a Word32 in big endian format
getWord32be :: Get Word32
getWord32be :: Get Word32
getWord32be = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
4
    Word32 -> Get Word32
forall (m :: * -> *) a. Monad m => a -> m a
return (Word32 -> Get Word32) -> Word32 -> Get Word32
forall a b. (a -> b) -> a -> b
$! (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) Word32 -> Int -> Word32
`shiftl_w32` Int
24) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word32 -> Int -> Word32
`shiftl_w32` Int
16) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Word32 -> Int -> Word32
`shiftl_w32`  Int
8) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) )

-- | Read a Word32 in little endian format
getWord32le :: Get Word32
getWord32le :: Get Word32
getWord32le = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
4
    Word32 -> Get Word32
forall (m :: * -> *) a. Monad m => a -> m a
return (Word32 -> Get Word32) -> Word32 -> Get Word32
forall a b. (a -> b) -> a -> b
$! (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) Word32 -> Int -> Word32
`shiftl_w32` Int
24) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Word32 -> Int -> Word32
`shiftl_w32` Int
16) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word32 -> Int -> Word32
`shiftl_w32`  Int
8) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )

-- | Read a Word64 in big endian format
getWord64be :: Get Word64
getWord64be :: Get Word64
getWord64be = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
8
    Word64 -> Get Word64
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64 -> Get Word64) -> Word64 -> Get Word64
forall a b. (a -> b) -> a -> b
$! (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) Word64 -> Int -> Word64
`shiftl_w64` Int
56) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word64 -> Int -> Word64
`shiftl_w64` Int
48) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Word64 -> Int -> Word64
`shiftl_w64` Int
40) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) Word64 -> Int -> Word64
`shiftl_w64` Int
32) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
4) Word64 -> Int -> Word64
`shiftl_w64` Int
24) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
5) Word64 -> Int -> Word64
`shiftl_w64` Int
16) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
6) Word64 -> Int -> Word64
`shiftl_w64`  Int
8) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
7) )

-- | Read a Word64 in little endian format
getWord64le :: Get Word64
getWord64le :: Get Word64
getWord64le = do
    ByteString
s <- Int -> Get ByteString
getBytes Int
8
    Word64 -> Get Word64
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64 -> Get Word64) -> Word64 -> Get Word64
forall a b. (a -> b) -> a -> b
$! (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
7) Word64 -> Int -> Word64
`shiftl_w64` Int
56) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
6) Word64 -> Int -> Word64
`shiftl_w64` Int
48) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
5) Word64 -> Int -> Word64
`shiftl_w64` Int
40) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
4) Word64 -> Int -> Word64
`shiftl_w64` Int
32) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) Word64 -> Int -> Word64
`shiftl_w64` Int
24) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Word64 -> Int -> Word64
`shiftl_w64` Int
16) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word64 -> Int -> Word64
`shiftl_w64`  Int
8) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|.
              (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )

{-# INLINE getWord8    #-}
{-# INLINE getWord16be #-}
{-# INLINE getWord16le #-}
{-# INLINE getWord32be #-}
{-# INLINE getWord32le #-}
{-# INLINE getWord64be #-}
{-# INLINE getWord64le #-}

------------------------------------------------------------------------
-- Host-endian reads

-- | /O(1)./ Read a single native machine word. The word is read in
-- host order, host endian form, for the machine you're on. On a 64 bit
-- machine the Word is an 8 byte value, on a 32 bit machine, 4 bytes.
getWordhost :: Get Word
getWordhost :: Get Word
getWordhost = Int -> Get Word
forall a. Storable a => Int -> Get a
getPtr (Word -> Int
forall a. Storable a => a -> Int
sizeOf (Word
forall a. HasCallStack => a
undefined :: Word))

-- | /O(1)./ Read a 2 byte Word16 in native host order and host endianness.
getWord16host :: Get Word16
getWord16host :: Get Word16
getWord16host = Int -> Get Word16
forall a. Storable a => Int -> Get a
getPtr (Word16 -> Int
forall a. Storable a => a -> Int
sizeOf (Word16
forall a. HasCallStack => a
undefined :: Word16))

-- | /O(1)./ Read a Word32 in native host order and host endianness.
getWord32host :: Get Word32
getWord32host :: Get Word32
getWord32host = Int -> Get Word32
forall a. Storable a => Int -> Get a
getPtr  (Word32 -> Int
forall a. Storable a => a -> Int
sizeOf (Word32
forall a. HasCallStack => a
undefined :: Word32))

-- | /O(1)./ Read a Word64 in native host order and host endianness.
getWord64host   :: Get Word64
getWord64host :: Get Word64
getWord64host = Int -> Get Word64
forall a. Storable a => Int -> Get a
getPtr  (Word64 -> Int
forall a. Storable a => a -> Int
sizeOf (Word64
forall a. HasCallStack => a
undefined :: Word64))

------------------------------------------------------------------------
-- Unchecked shifts

shiftl_w16 :: Word16 -> Int -> Word16
shiftl_w32 :: Word32 -> Int -> Word32
shiftl_w64 :: Word64 -> Int -> Word64

#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
#if MIN_VERSION_base(4,16,0)
shiftl_w16 (W16# w) (I# i) = W16# (w `uncheckedShiftLWord16#` i)
shiftl_w32 (W32# w) (I# i) = W32# (w `uncheckedShiftLWord32#` i)
#else
shiftl_w16 :: Word16 -> Int -> Word16
shiftl_w16 (W16# Word#
w) (I# Int#
i) = Word# -> Word16
W16# (Word#
w Word# -> Int# -> Word#
`uncheckedShiftL#`   Int#
i)
shiftl_w32 :: Word32 -> Int -> Word32
shiftl_w32 (W32# Word#
w) (I# Int#
i) = Word# -> Word32
W32# (Word#
w Word# -> Int# -> Word#
`uncheckedShiftL#`   Int#
i)
#endif

#if WORD_SIZE_IN_BITS < 64
shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL64#` i)

#if __GLASGOW_HASKELL__ <= 606
-- Exported by GHC.Word in GHC 6.8 and higher
foreign import ccall unsafe "stg_uncheckedShiftL64"
    uncheckedShiftL64#     :: Word64# -> Int# -> Word64#
#endif

#else
#if MIN_VERSION_base(4,17,0)
shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL64#` i)
#else
shiftl_w64 :: Word64 -> Int -> Word64
shiftl_w64 (W64# Word#
w) (I# Int#
i) = Word# -> Word64
W64# (Word#
w Word# -> Int# -> Word#
`uncheckedShiftL#` Int#
i)
#endif
#endif

#else
shiftl_w16 = shiftL
shiftl_w32 = shiftL
shiftl_w64 = shiftL
#endif


-- Containers ------------------------------------------------------------------

getTwoOf :: Get a -> Get b -> Get (a,b)
getTwoOf :: Get a -> Get b -> Get (a, b)
getTwoOf Get a
ma Get b
mb = (a -> b -> (a, b)) -> Get a -> Get b -> Get (a, b)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
M.liftM2 (,) Get a
ma Get b
mb

-- | Get a list in the following format:
--   Word64 (big endian format)
--   element 1
--   ...
--   element n
getListOf :: Get a -> Get [a]
getListOf :: Get a -> Get [a]
getListOf Get a
m = [a] -> Word64 -> Get [a]
forall t. (Eq t, Num t) => [a] -> t -> Get [a]
go [] (Word64 -> Get [a]) -> Get Word64 -> Get [a]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Get Word64
getWord64be
  where
  go :: [a] -> t -> Get [a]
go [a]
as t
0 = [a] -> Get [a]
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> Get [a]) -> [a] -> Get [a]
forall a b. (a -> b) -> a -> b
$! [a] -> [a]
forall a. [a] -> [a]
reverse [a]
as
  go [a]
as t
i = do a
x <- Get a
m
               a
x a -> Get [a] -> Get [a]
`seq` [a] -> t -> Get [a]
go (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
as) (t
i t -> t -> t
forall a. Num a => a -> a -> a
- t
1)

-- | Get an IArray in the following format:
--   index (lower bound)
--   index (upper bound)
--   Word64 (big endian format)
--   element 1
--   ...
--   element n
getIArrayOf :: (Ix i, IArray a e) => Get i -> Get e -> Get (a i e)
getIArrayOf :: Get i -> Get e -> Get (a i e)
getIArrayOf Get i
ix Get e
e = ((i, i) -> [e] -> a i e) -> Get (i, i) -> Get [e] -> Get (a i e)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
M.liftM2 (i, i) -> [e] -> a i e
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (Get i -> Get i -> Get (i, i)
forall a b. Get a -> Get b -> Get (a, b)
getTwoOf Get i
ix Get i
ix) (Get e -> Get [e]
forall a. Get a -> Get [a]
getListOf Get e
e)

-- | Get a sequence in the following format:
--   Word64 (big endian format)
--   element 1
--   ...
--   element n
getSeqOf :: Get a -> Get (Seq.Seq a)
getSeqOf :: Get a -> Get (Seq a)
getSeqOf Get a
m = Seq a -> Word64 -> Get (Seq a)
forall t. (Eq t, Num t) => Seq a -> t -> Get (Seq a)
go Seq a
forall a. Seq a
Seq.empty (Word64 -> Get (Seq a)) -> Get Word64 -> Get (Seq a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Get Word64
getWord64be
  where
  go :: Seq a -> t -> Get (Seq a)
go Seq a
xs t
0 = Seq a -> Get (Seq a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Seq a -> Get (Seq a)) -> Seq a -> Get (Seq a)
forall a b. (a -> b) -> a -> b
$! Seq a
xs
  go Seq a
xs t
n = Seq a
xs Seq a -> Get (Seq a) -> Get (Seq a)
`seq` t
n t -> Get (Seq a) -> Get (Seq a)
`seq` do
              a
x <- Get a
m
              Seq a -> t -> Get (Seq a)
go (Seq a
xs Seq a -> a -> Seq a
forall a. Seq a -> a -> Seq a
Seq.|> a
x) (t
n t -> t -> t
forall a. Num a => a -> a -> a
- t
1)

-- | Read as a list of lists.
getTreeOf :: Get a -> Get (T.Tree a)
getTreeOf :: Get a -> Get (Tree a)
getTreeOf Get a
m = (a -> [Tree a] -> Tree a) -> Get a -> Get [Tree a] -> Get (Tree a)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
M.liftM2 a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
T.Node Get a
m (Get (Tree a) -> Get [Tree a]
forall a. Get a -> Get [a]
getListOf (Get a -> Get (Tree a)
forall a. Get a -> Get (Tree a)
getTreeOf Get a
m))

-- | Read as a list of pairs of key and element.
getMapOf :: Ord k => Get k -> Get a -> Get (Map.Map k a)
getMapOf :: Get k -> Get a -> Get (Map k a)
getMapOf Get k
k Get a
m = [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(k, a)] -> Map k a) -> Get [(k, a)] -> Get (Map k a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get (k, a) -> Get [(k, a)]
forall a. Get a -> Get [a]
getListOf (Get k -> Get a -> Get (k, a)
forall a b. Get a -> Get b -> Get (a, b)
getTwoOf Get k
k Get a
m)

-- | Read as a list of pairs of int and element.
getIntMapOf :: Get Int -> Get a -> Get (IntMap.IntMap a)
getIntMapOf :: Get Int -> Get a -> Get (IntMap a)
getIntMapOf Get Int
i Get a
m = [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
IntMap.fromList ([(Int, a)] -> IntMap a) -> Get [(Int, a)] -> Get (IntMap a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get (Int, a) -> Get [(Int, a)]
forall a. Get a -> Get [a]
getListOf (Get Int -> Get a -> Get (Int, a)
forall a b. Get a -> Get b -> Get (a, b)
getTwoOf Get Int
i Get a
m)

-- | Read as a list of elements.
getSetOf :: Ord a => Get a -> Get (Set.Set a)
getSetOf :: Get a -> Get (Set a)
getSetOf Get a
m = [a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList ([a] -> Set a) -> Get [a] -> Get (Set a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get a -> Get [a]
forall a. Get a -> Get [a]
getListOf Get a
m

-- | Read as a list of ints.
getIntSetOf :: Get Int -> Get IntSet.IntSet
getIntSetOf :: Get Int -> Get IntSet
getIntSetOf Get Int
m = [Int] -> IntSet
IntSet.fromList ([Int] -> IntSet) -> Get [Int] -> Get IntSet
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get Int -> Get [Int]
forall a. Get a -> Get [a]
getListOf Get Int
m

-- | Read in a Maybe in the following format:
--   Word8 (0 for Nothing, anything else for Just)
--   element (when Just)
getMaybeOf :: Get a -> Get (Maybe a)
getMaybeOf :: Get a -> Get (Maybe a)
getMaybeOf Get a
m = do
  Word8
tag <- Get Word8
getWord8
  case Word8
tag of
    Word8
0 -> Maybe a -> Get (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
    Word8
_ -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> Get a -> Get (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get a
m

-- | Read an Either, in the following format:
--   Word8 (0 for Left, anything else for Right)
--   element a when 0, element b otherwise
getEitherOf :: Get a -> Get b -> Get (Either a b)
getEitherOf :: Get a -> Get b -> Get (Either a b)
getEitherOf Get a
ma Get b
mb = do
  Word8
tag <- Get Word8
getWord8
  case Word8
tag of
    Word8
0 -> a -> Either a b
forall a b. a -> Either a b
Left  (a -> Either a b) -> Get a -> Get (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get a
ma
    Word8
_ -> b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b) -> Get b -> Get (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get b
mb

-- | Read in a length and then read a nested structure
--   of that length.
getNested :: Get Int -> Get a -> Get a
getNested :: Get Int -> Get a -> Get a
getNested Get Int
getLen Get a
getVal = do
    Int
n <- Get Int
getLen
    Int -> Get a -> Get a
forall a. Int -> Get a -> Get a
isolate Int
n Get a
getVal

-- | Get the number of bytes read up to this point
bytesRead :: Get Int
bytesRead :: Get Int
bytesRead = (forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success Int r -> Result r)
-> Get Int
forall a.
(forall r.
 ByteString
 -> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ByteString
i Buffer
b More
m Int
w Failure r
_ Success Int r
k -> Success Int r
k ByteString
i Buffer
b More
m Int
w Int
w)