@@ -4,6 +4,7 @@
5-- Module : Data.Binary.Bits.Get
5-- Module : Data.Binary.Bits.Get
6-- Copyright : (c) Lennart Kolmodin 2010-2011
6-- Copyright : (c) Lennart Kolmodin 2010-2011
7+-- (c) Sylvain Henry 2015
7-- License : BSD3-style (see LICENSE)
8-- License : BSD3-style (see LICENSE)
9-- Maintainer : kolmodin@gmail.com
10-- Maintainer : kolmodin@gmail.com
@@ -55,13 +56,15 @@ module Data.Binary.Bits.Get
55 -- $bitget
56 -- $bitget
59 , runBitGet
59 , runBitGet
62 -- ** Get bytes
61 -- ** Get bytes
64 , getWord8
64 , getWord8
65 , getWord16be
68 , getWord16be
66 , getWord32be
69 , getWord32be
67 , getWord64be
70 , getWord64be
@@ -79,6 +82,9 @@ module Data.Binary.Bits.Get
79 -- ** Read in Blocks
82 -- ** Read in Blocks
82 , word16be
88 , word16be
83 , word32be
89 , word32be
84 , word64be
90 , word64be
@@ -91,6 +97,9 @@ module Data.Binary.Bits.Get
92import Data.Binary.Get as B ( Get, getLazyByteString, isEmpty )
98import Data.Binary.Get as B ( Get, getLazyByteString, isEmpty )
93import Data.Binary.Get.Internal as B ( get, put, ensureN )
99import Data.Binary.Get.Internal as B ( get, put, ensureN )
100+import Data.Binary.Bits.BitOrder
101+import Data.Binary.Bits.Internal
102+import Data.Binary.Bits.Alignment
95import Data.ByteString as B
104import Data.ByteString as B
96import qualified Data.ByteString.Lazy as L
105import qualified Data.ByteString.Lazy as L
@@ -106,12 +115,8 @@ import Control.Applicative
106import Control.Monad (when,foldM_)
115import Control.Monad (when,foldM_)
108import Prelude as P
117import Prelude as P
110−#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
112import GHC.Word
118import GHC.Word
113−#endif
119+import GHC.Base (uncheckedShiftLWord8#, wordToWord8#, Int (..), andWord8#, Word8#, (-#), Int#, xorI#, eqWord8#, TYPE)
116-- $bitget
121-- $bitget
117-- Parse bits using a monad.
122-- Parse bits using a monad.
@@ -164,16 +169,6 @@ data S = S {-# UNPACK #-} !ByteString -- Input
164 !BitOrder -- Bit order
169 !BitOrder -- Bit order
165 deriving (Show)
170 deriving (Show)
167−-- | Compute bit offset (equivalent to x `mod` 8 but faster)
168−bit_offset :: Int -> Int
169−bit_offset n = make_mask 3 .&. n
170−{-# INLINE bit_offset #-}
172−-- | Compute byte offset (equivalent to x `div` 8 but faster)
173−byte_offset :: Int -> Int
174−byte_offset n = n `shiftR` 3
175−{-# INLINE byte_offset #-}
177-- | Increment the current bit offset
172-- | Increment the current bit offset
178incS :: Int -> S -> S
173incS :: Int -> S -> S
179incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
174incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
@@ -182,83 +177,70 @@ incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
182 !d = byte_offset o'
177 !d = byte_offset o'
183 !n' = bit_offset o'
178 !n' = bit_offset o'
185−-- | Select the bit ordering
186−selectBitOrderS :: BitOrder -> S -> S
187−selectBitOrderS bo (S bs o _) = S bs o bo
189−-- | make_mask 3 = 00000111
190−make_mask :: (Bits a, Num a) => Int -> a
191−make_mask n = (1 `shiftL` fromIntegral n) - 1
192−{-# SPECIALIZE make_mask :: Int -> Int #-}
193−{-# SPECIALIZE make_mask :: Int -> Word #-}
194−{-# SPECIALIZE make_mask :: Int -> Word8 #-}
195−{-# SPECIALIZE make_mask :: Int -> Word16 #-}
196−{-# SPECIALIZE make_mask :: Int -> Word32 #-}
197−{-# SPECIALIZE make_mask :: Int -> Word64 #-}
199−-- | Keep only the n least-significant bits of the given value
200−mask :: (Bits a, Num a) => Int -> a -> a
201−mask n v = v .&. make_mask n
206−-- E.g. two words of 5 bits: ABCDE, VWXYZ
207−-- - BB: ABCDEVWX YZxxxxxx
208−-- - LB: XYZABCDE xxxxxxVW
209−-- - BL: EDCBAZYX WVxxxxxx -- not implemented
210−-- - LL: XWVEDCBA xxxxxxZY -- not implemented
211−data BitOrder = BB | LB deriving (Show)
214-- | Read a single bit
180-- | Read a single bit
215readBool :: S -> Bool
181readBool :: S -> Bool
216readBool (S bs o bo) = case bo of
182readBool (S bs o bo) = case bo of
217 BB -> testBit (unsafeHead bs) (7-o)
183 BB -> testBit (unsafeHead bs) (7-o)
184+ BL -> testBit (unsafeHead bs) (7-o)
185+ LL -> testBit (unsafeHead bs) o
218 LB -> testBit (unsafeHead bs) o
186 LB -> testBit (unsafeHead bs) o
188+-- | Read a single bit
189+readBool# :: S -> Int
190+readBool# (S bs (I# o) bo) = case bo of
191+ BB -> testBit# (7# -# o)
192+ BL -> testBit# (7# -# o)
196+ !(W8# w) = unsafeHead bs
197+ testBit# n = I# (((w `andWord8#` (wordToWord8# 1## `uncheckedShiftLWord8#` n)) `eqWord8#` wordToWord8# 0##) `xorI#` 1#)
220-- | Extract a range of bits from (ws :: ByteString)
200-- | Extract a range of bits from (ws :: ByteString)
222-- Constraint: 8 * (length ws -1 ) < o+n <= 8 * length ws
202-- Constraint: 8 * (length ws -1 ) < o+n <= 8 * length ws
223−extract :: (Num a, Bits a, FastBits a) => BitOrder -> ByteString -> Int -> Int -> a
203+extract :: (Num a, FastBits a) => BitOrder -> ByteString -> Int -> Int -> a
224extract bo bs o n
204extract bo bs o n
225 | n == 0 = 0
205 | n == 0 = 0
226 | B.length bs == 0 = error "Empty ByteString"
206 | B.length bs == 0 = error "Empty ByteString"
227− | otherwise = mask n (foldlWithIndex' f 0 bs)
207+ | otherwise = rev . mask n . foldlWithIndex' f 0 $ bs
229 -- B.foldl' with index
209 -- B.foldl' with index
230 foldlWithIndex' op b = fst . B.foldl' g (b,0)
210 foldlWithIndex' op b = fst . B.foldl' g (b,0)
231 where g (b',i) w = (op b' w i, (i+1))
211 where g (b',i) w = (op b' w i, (i+1))
233 -- 'or' correctly shifted words
213 -- 'or' correctly shifted words
234− f b w i = b .|. (fromIntegral w `fastShift` off i)
214+ f b w i = b .|. (fromIntegral w `fastShift` off i)
236 -- co-offset
216 -- co-offset
237− r = B.length bs * 8 - (o + n)
217+ r = B.length bs * 8 - (o + n)
239 -- shift offset depending on the byte position (0..B.length-1)
219 -- shift offset depending on the byte position (0..B.length-1)
240− off i = case bo of
220+ off i = case bo of
241 LB -> 8*i - o
222 LB -> 8*i - o
242 BB -> (B.length bs -1 - i) * 8 - r
223 BB -> (B.length bs -1 - i) * 8 - r
224+ BL -> (B.length bs -1 - i) * 8 - r
226+ -- reverse bits if necessary
245-- | Generic readWord
234-- | Generic readWord
246−readWord :: (Num a, Bits a, FastBits a) => Int -> S -> a
235+readWord :: (Num a, FastBits a) => Int -> S -> a
247readWord n (S bs o bo)
236readWord n (S bs o bo)
248 | n == 0 = 0
237 | n == 0 = 0
249− | o+n <= 8 = extract bo (unsafeTake 1 bs) o n
250− | o+n <= 16 = extract bo (unsafeTake 2 bs) o n
251− | o+n <= 24 = extract bo (unsafeTake 3 bs) o n
252− | o+n <= 32 = extract bo (unsafeTake 4 bs) o n
253− | o+n <= 40 = extract bo (unsafeTake 5 bs) o n
254− | o+n <= 48 = extract bo (unsafeTake 6 bs) o n
255− | o+n <= 56 = extract bo (unsafeTake 7 bs) o n
256− | o+n <= 64 = extract bo (unsafeTake 8 bs) o n
257− | otherwise = extract bo (unsafeTake ((o+n+7) `div` 8) bs) o n
238+ | otherwise = extract bo (unsafeTake nbytes bs) o n
239+ where nbytes = byte_offset (o+n+7)
259-- | Check that the number of bits to read is not greater than the first parameter
241-- | Check that the number of bits to read is not greater than the first parameter
260{-# INLINE readWordChecked #-}
242{-# INLINE readWordChecked #-}
261−readWordChecked :: (Num a, Bits a, FastBits a) => Int -> Int -> S -> a
243+readWordChecked :: (Num a, FastBits a) => Int -> Int -> S -> a
262readWordChecked m n s
244readWordChecked m n s
263 | n > m = error $ "Tried to read more than " ++ show m ++ " bits (" ++ show n ++")"
245 | n > m = error $ "Tried to read more than " ++ show m ++ " bits (" ++ show n ++")"
264 | otherwise = readWord n s
246 | otherwise = readWord n s
@@ -266,36 +248,37 @@ readWordChecked m n s
266-- | Read the given number of bytes and return them in Big-Endian order
248-- | Read the given number of bytes and return them in Big-Endian order
268-- Examples:
250-- Examples:
269−-- BB: xxxABCDE FGHIJKLM NOPQRxxx -> ABCDEFGH IJKLMNOP QRxxxxxx
251+-- BB: xxxABCDE FGHIJKLM NOPxxxxx -> ABCDEFGH IJKLMNOP
270−-- LB: NOPQRxxx FGHIJKLM xxxABCDE -> ABCDEFGH IJKLMNOP QRxxxxxx
252+-- LL: LMNOPxxx DEFGHIJK xxxxxABC -> ABCDEFGH IJKLMNOP
253+-- BL: xxxPONML KJIHGFED CBAxxxxx -> ABCDEFGH IJKLMNOP
254+-- LB: EDCBAxxx MLKJIHGF xxxxxPON -> ABCDEFGH IJKLMNOP
271readByteString :: Int -> S -> ByteString
255readByteString :: Int -> S -> ByteString
272−readByteString n (S bs o bo)
256+readByteString n (S bs o bo) =
258+ bs' = unsafeTake (n+1) bs
273− | o == 0 = unsafeTake n bs
259+ bs'' = unsafeTake n bs
260+ rev = B.map (reverseBits 8)
263+ (0,LL) -> B.reverse bs''
265+ (0,BL) -> rev . B.reverse $ bs''
266+ (_,LL) -> readByteString n (S (B.reverse bs') (8-o) BB)
267+ (_,BL) -> rev . B.reverse $ readByteString n (S bs' o BB)
268+ (_,LB) -> rev . B.reverse $ readByteString n (S bs' o LL)
274− | otherwise = unsafePerformIO $ do
269+ (_,BB) -> unsafePerformIO $ do
275− let len = n+1
270+ let len = n+1
276− ptr <- mallocBytes len
271+ ptr <- mallocBytes len
279− let f r i = do
272+ let f r i = do
281− w = unsafeIndex bs (len-i)
274+ w = unsafeIndex bs (len-i)
282− w' = (w `fastShiftL` o) .|. r
275+ w' = (w `fastShiftL` o) .|. r
283− r' = w `fastShiftR` (8-o)
276+ r' = w `fastShiftR` (8-o)
284− poke (ptr `plusPtr` (len-i)) w'
277+ poke (ptr `plusPtr` (len-i)) w'
285− return r'
278+ return r'
286− foldM_ f 0 [1..len]
279+ foldM_ f 0 [1..len]
287− unsafeInit <$> unsafePackMallocCStringLen (ptr,len)
280+ unsafeInit <$> unsafePackMallocCStringLen (ptr,len)
292− w = unsafeIndex bs (i-1)
293− w' = (w `fastShiftL` (8-o)) .|. r
294− r' = w `fastShiftR` o
295− poke (ptr `plusPtr` (len-i)) w'
298− unsafeTail <$> unsafePackMallocCStringLen (ptr,len)
300------------------------------------------------------------------------
283------------------------------------------------------------------------
301-- | 'BitGet' is a monad, applicative and a functor. See 'runBitGet'
284-- | 'BitGet' is a monad, applicative and a functor. See 'runBitGet'
@@ -304,7 +287,6 @@ newtype BitGet a = B { runState :: S -> Get (S,a) }
305instance Monad BitGet where
288instance Monad BitGet where
306 return x = B $ \s -> return (s,x)
289 return x = B $ \s -> return (s,x)
307− fail str = B $ \s -> putBackState s >> fail str
308 (B f) >>= g = B $ \s -> do (s',a) <- f s
290 (B f) >>= g = B $ \s -> do (s',a) <- f s
309 runState (g a) s'
291 runState (g a) s'
@@ -315,6 +297,29 @@ instance Applicative BitGet where
315 pure x = return x
297 pure x = return x
316 fm <*> m = fm >>= \f -> m >>= \v -> return (f v)
298 fm <*> m = fm >>= \f -> m >>= \v -> return (f v)
300+instance BitOrderable BitGet where
302+ (S bs o _) <- getState
303+ putState (S bs o bo)
306+ (S _ _ bo) <- getState
309+instance Alignable BitGet where
310+ -- | Skip the given number of bits
315+ -- | Skip bits if necessary to align to the next byte
317+ (S _ o _) <- getState
318-- | Run a 'BitGet' within the Binary packages 'Get' monad. If a byte has
323-- | Run a 'BitGet' within the Binary packages 'Get' monad. If a byte has
319-- been partially consumed it will be discarded once 'runBitGet' is finished.
324-- been partially consumed it will be discarded once 'runBitGet' is finished.
320runBitGet :: BitGet a -> Get a
325runBitGet :: BitGet a -> Get a
@@ -362,23 +367,6 @@ ensureBits n = do
362 put B.empty
367 put B.empty
363 return (S (bs`append`bs') o bo, ())
368 return (S (bs`append`bs') o bo, ())
365−-- | Skip the given number of bits
366−skipBits :: Int -> BitGet ()
371−-- | Skip bits if necessary to align to the next byte
372−alignByte :: BitGet ()
374− (S _ o _) <- getState
378−-- | Select the bit ordering
379−selectBitOrder :: BitOrder -> BitGet ()
380−selectBitOrder = withState . selectBitOrderS
382-- | Test whether all input has been consumed, i.e. there are no remaining
370-- | Test whether all input has been consumed, i.e. there are no remaining
383-- undecoded bytes.
371-- undecoded bytes.
384isEmpty :: BitGet Bool
372isEmpty :: BitGet Bool
@@ -390,21 +378,37 @@ isEmpty = B $ \ (S bs o bo) -> if B.null bs
390getBool :: BitGet Bool
378getBool :: BitGet Bool
391getBool = block bool
379getBool = block bool
381+-- | Get 1 bit as a 'Bool'.
382+getBool# :: BitGet Int
383+getBool# = block bool#
393-- | Get @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
385-- | Get @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
394getWord8 :: Int -> BitGet Word8
386getWord8 :: Int -> BitGet Word8
395getWord8 n = block (word8 n)
387getWord8 n = block (word8 n)
397-- | Get @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
389-- | Get @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
390+getWord16 :: Int -> BitGet Word16
391+getWord16 n = block (word16 n)
398getWord16be :: Int -> BitGet Word16
393getWord16be :: Int -> BitGet Word16
399−getWord16be n = block (word16be n)
394+getWord16be = getWord16
395+{-# DEPRECATED getWord16be "Use 'getWord16' instead" #-}
401-- | Get @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
397-- | Get @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
398+getWord32 :: Int -> BitGet Word32
399+getWord32 n = block (word32 n)
402getWord32be :: Int -> BitGet Word32
401getWord32be :: Int -> BitGet Word32
403−getWord32be n = block (word32be n)
402+getWord32be = getWord32
403+{-# DEPRECATED getWord32be "Use 'getWord32' instead" #-}
405-- | Get @n@ bits as a 'Word64'. @n@ must be within @[0..64]@.
405-- | Get @n@ bits as a 'Word64'. @n@ must be within @[0..64]@.
406+getWord64 :: Int -> BitGet Word64
407+getWord64 n = block (word64 n)
406getWord64be :: Int -> BitGet Word64
409getWord64be :: Int -> BitGet Word64
407−getWord64be n = block (word64be n)
410+getWord64be = getWord64
411+{-# DEPRECATED getWord64be "Use 'getWord64' instead" #-}
409-- | Get @n@ bytes as a 'ByteString'.
413-- | Get @n@ bytes as a 'ByteString'.
410getByteString :: Int -> BitGet ByteString
414getByteString :: Int -> BitGet ByteString
@@ -453,105 +457,40 @@ block (Block i p) = do
453bool :: Block Bool
457bool :: Block Bool
454bool = Block 1 readBool
458bool = Block 1 readBool
460+-- | Read a 1 bit 'Bool'.
462+bool# = Block 1 readBool#
456-- | Read @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
464-- | Read @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
457word8 :: Int -> Block Word8
465word8 :: Int -> Block Word8
458word8 n = Block n (readWordChecked 8 n)
466word8 n = Block n (readWordChecked 8 n)
460-- | Read @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
468-- | Read @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
469+word16 :: Int -> Block Word16
470+word16 n = Block n (readWordChecked 16 n)
461word16be :: Int -> Block Word16
472word16be :: Int -> Block Word16
462−word16be n = Block n (readWordChecked 16 n)
473+word16be = word16
474+{-# DEPRECATED word16be "Use 'word16' instead" #-}
464-- | Read @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
476-- | Read @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
477+word32 :: Int -> Block Word32
478+word32 n = Block n (readWordChecked 32 n)
465word32be :: Int -> Block Word32
480word32be :: Int -> Block Word32
466−word32be n = Block n (readWordChecked 32 n)
481+word32be = word32
482+{-# DEPRECATED word32be "Use 'word32' instead" #-}
468-- | Read @n@ bits as a 'Word64'. @n@ must be within @[0..64]@.
484-- | Read @n@ bits as a 'Word64'. @n@ must be within @[0..64]@.
485+word64 :: Int -> Block Word64
486+word64 n = Block n (readWordChecked 64 n)
469word64be :: Int -> Block Word64
488word64be :: Int -> Block Word64
470−word64be n = Block n (readWordChecked 64 n)
489+word64be = word64
490+{-# DEPRECATED word64be "Use 'word64' instead" #-}
472-- | Read @n@ bytes as a 'ByteString'.
492-- | Read @n@ bytes as a 'ByteString'.
473byteString :: Int -> Block ByteString
493byteString :: Int -> Block ByteString
474byteString n | n > 0 = Block (n*8) (readByteString n)
494byteString n | n > 0 = Block (n*8) (readByteString n)
475 | otherwise = Block 0 (\_ -> B.empty)
495 | otherwise = Block 0 (\_ -> B.empty)
478−---------------------------------------------------------------------
479−-- Unchecked shifts, from the "binary" package
481−-- | Class for types supporting fast bit shifting
482−class FastBits a where
483− fastShiftR :: a -> Int -> a
484− fastShiftL :: a -> Int -> a
486− {-# INLINE fastShift #-}
487− fastShift :: a -> Int -> a
489− | n > 0 = fastShiftL x n
490− | n < 0 = fastShiftR x (negate n)
493−instance FastBits Word8 where
494− fastShiftR = shiftr_w8
495− fastShiftL = shiftl_w8
497−instance FastBits Word16 where
498− fastShiftR = shiftr_w16
499− fastShiftL = shiftl_w16
501−instance FastBits Word32 where
502− fastShiftR = shiftr_w32
503− fastShiftL = shiftl_w32
505−instance FastBits Word64 where
506− fastShiftR = shiftr_w64
507− fastShiftL = shiftl_w64
510−shiftl_w8 :: Word8 -> Int -> Word8
511−shiftl_w16 :: Word16 -> Int -> Word16
512−shiftl_w32 :: Word32 -> Int -> Word32
513−shiftl_w64 :: Word64 -> Int -> Word64
515−shiftr_w8 :: Word8 -> Int -> Word8
516−shiftr_w16 :: Word16 -> Int -> Word16
517−shiftr_w32 :: Word32 -> Int -> Word32
518−shiftr_w64 :: Word64 -> Int -> Word64
520−#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
521−shiftl_w8 (W8# w) (I# i) = W8# (w `uncheckedShiftL#` i)
522−shiftl_w16 (W16# w) (I# i) = W16# (w `uncheckedShiftL#` i)
523−shiftl_w32 (W32# w) (I# i) = W32# (w `uncheckedShiftL#` i)
525−shiftr_w8 (W8# w) (I# i) = W8# (w `uncheckedShiftRL#` i)
526−shiftr_w16 (W16# w) (I# i) = W16# (w `uncheckedShiftRL#` i)
527−shiftr_w32 (W32# w) (I# i) = W32# (w `uncheckedShiftRL#` i)
530−#if WORD_SIZE_IN_BITS < 64
531−shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL64#` i)
532−shiftr_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftRL64#` i)
534−#if __GLASGOW_HASKELL__ <= 606
535−-- Exported by GHC.Word in GHC 6.8 and higher
536−foreign import ccall unsafe "stg_uncheckedShiftL64"
537− uncheckedShiftL64# :: Word64# -> Int# -> Word64#
538−foreign import ccall unsafe "stg_uncheckedShiftRL64"
539− uncheckedShiftRL64# :: Word64# -> Int# -> Word64#
543−shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL#` i)
544−shiftr_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftRL#` i)