module Data.Iteratee.Binary (
Endian (..),
endianRead2,
endianRead3,
endianRead4
)
where
import Data.Iteratee.Base.StreamChunk (StreamChunk)
import qualified Data.Iteratee.Base as It
import Data.Word
import Data.Bits
import Data.Int
data Endian = MSB
| LSB
deriving (Eq, Ord, Show, Enum)
endianRead2 :: (StreamChunk s Word8, Monad m) => Endian ->
It.IterateeG s Word8 m Word16
endianRead2 e = do
c1 <- It.head
c2 <- It.head
case e of
MSB -> return $ (fromIntegral c1 `shiftL` 8) .|. fromIntegral c2
LSB -> return $ (fromIntegral c2 `shiftL` 8) .|. fromIntegral c1
endianRead3 :: (StreamChunk s Word8, Monad m) => Endian ->
It.IterateeG s Word8 m Word32
endianRead3 e = do
c1 <- It.head
c2 <- It.head
c3 <- It.head
case e of
MSB -> return $ (((fromIntegral c1
`shiftL` 8) .|. fromIntegral c2)
`shiftL` 8) .|. fromIntegral c3
LSB ->
let m :: Int32
m = shiftR (shiftL (fromIntegral c3) 24) 8 in
return $ (((fromIntegral c3
`shiftL` 8) .|. fromIntegral c2)
`shiftL` 8) .|. fromIntegral m
endianRead4 :: (StreamChunk s Word8, Monad m) => Endian ->
It.IterateeG s Word8 m Word32
endianRead4 e = do
c1 <- It.head
c2 <- It.head
c3 <- It.head
c4 <- It.head
case e of
MSB -> return $
(((((fromIntegral c1
`shiftL` 8) .|. fromIntegral c2)
`shiftL` 8) .|. fromIntegral c3)
`shiftL` 8) .|. fromIntegral c4
LSB -> return $
(((((fromIntegral c4
`shiftL` 8) .|. fromIntegral c3)
`shiftL` 8) .|. fromIntegral c2)
`shiftL` 8) .|. fromIntegral c1