caerbannog-1.0.0.2: That rabbit's got a vicious streak a mile wide!
Safe HaskellSafe-Inferred
LanguageHaskell98

Data.Binary.Bits.Get

Description

Parse bits easily. Parsing can be done either in a monadic style, or more efficiently, using the Applicative style.

For the monadic style, write your parser as a BitGet monad using the

functions and run it with runBitGet.

For the applicative style, compose the fuctions

to make a Block. Use block to turn it into the BitGet monad to be able to run it with runBitGet.

Synopsis

Documentation

data BitGet a Source #

Instances

Instances details
MonadFail BitGet Source # 
Instance details

Defined in Data.Binary.Bits.Get

Methods

fail :: String -> BitGet a #

Alternative BitGet Source # 
Instance details

Defined in Data.Binary.Bits.Get

Methods

empty :: BitGet a #

(<|>) :: BitGet a -> BitGet a -> BitGet a #

some :: BitGet a -> BitGet [a] #

many :: BitGet a -> BitGet [a] #

Applicative BitGet Source # 
Instance details

Defined in Data.Binary.Bits.Get

Methods

pure :: a -> BitGet a #

(<*>) :: BitGet (a -> b) -> BitGet a -> BitGet b #

liftA2 :: (a -> b -> c) -> BitGet a -> BitGet b -> BitGet c #

(*>) :: BitGet a -> BitGet b -> BitGet b #

(<*) :: BitGet a -> BitGet b -> BitGet a #

Functor BitGet Source # 
Instance details

Defined in Data.Binary.Bits.Get

Methods

fmap :: (a -> b) -> BitGet a -> BitGet b #

(<$) :: a -> BitGet b -> BitGet a #

Monad BitGet Source # 
Instance details

Defined in Data.Binary.Bits.Get

Methods

(>>=) :: BitGet a -> (a -> BitGet b) -> BitGet b #

(>>) :: BitGet a -> BitGet b -> BitGet b #

return :: a -> BitGet a #

runBitGet :: BitGet a -> Get a Source #

Run a BitGet within the Binary packages Get monad. If a byte has been partially consumed it will be discarded once runBitGet is finished.

Get bytes

getBool :: BitGet Bool Source #

Get 1 bit as a Bool.

getWord8 :: Int -> BitGet Word8 Source #

Get n bits as a Word8. n must be within [0..8].

getWord16be :: Int -> BitGet Word16 Source #

Get n bits as a Word16. n must be within [0..16].

getWord32be :: Int -> BitGet Word32 Source #

Get n bits as a Word32. n must be within [0..32].

getWord64be :: Int -> BitGet Word64 Source #

Get n bits as a Word64. n must be within [0..64].

Blocks

Parse more efficiently in blocks. Each block is read with only one boundry check (checking that there is enough input) as the size of the block can be calculated statically. This is somewhat limiting as you cannot make the parsing depend on the input being parsed.

data IPV6Header = IPV6Header {
     ipv6Version :: Word8
   , ipv6TrafficClass :: Word8
   , ipv6FlowLabel :: 'Word32
   , ipv6PayloadLength :: Word16
   , ipv6NextHeader :: Word8
   , ipv6HopLimit :: Word8
   , ipv6SourceAddress :: ByteString
   , ipv6DestinationAddress :: ByteString
 }

 ipv6headerblock =
         IPV6Header <$> word8 4
                    <*> word8 8
                    <*> word32be 24
                    <*> word16be 16
                    <*> word8 8
                    <*> word8 8
                    <*> byteString 16
                    <*> byteString 16

ipv6Header :: Get IPV6Header
ipv6Header = runBitGet (block ipv6headerblock)
 

data Block a Source #

A block that will be read with only one boundry check. Needs to know the number of bits in advance.

Instances

Instances details
Applicative Block Source # 
Instance details

Defined in Data.Binary.Bits.Get

Methods

pure :: a -> Block a #

(<*>) :: Block (a -> b) -> Block a -> Block b #

liftA2 :: (a -> b -> c) -> Block a -> Block b -> Block c #

(*>) :: Block a -> Block b -> Block b #

(<*) :: Block a -> Block b -> Block a #

Functor Block Source # 
Instance details

Defined in Data.Binary.Bits.Get

Methods

fmap :: (a -> b) -> Block a -> Block b #

(<$) :: a -> Block b -> Block a #

block :: Block a -> BitGet a Source #

Get a block. Will be read with one single boundry check, and therefore requires a statically known number of bits. Build blocks using bool, word8, word16be, word32be, word64be, byteString and Applicative.

Read in Blocks

bool :: Block Bool Source #

Read a 1 bit Bool.

word8 :: Int -> Block Word8 Source #

Read n bits as a Word8. n must be within [0..8].

word16be :: Int -> Block Word16 Source #

Read n bits as a Word16. n must be within [0..16].

word32be :: Int -> Block Word32 Source #

Read n bits as a Word32. n must be within [0..32].

word64be :: Int -> Block Word64 Source #

Read n bits as a Word64. n must be within [0..64].

byteString :: Int -> Block ByteString Source #

Read n bytes as a ByteString.

getLazyByteString :: Int -> BitGet ByteString Source #

Get n bytes as a lazy ByteString.

isEmpty :: BitGet Bool Source #

Test whether all input has been consumed, i.e. there are no remaining undecoded bytes.