@@ -4,7 +4,6 @@
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
8-- License : BSD3-style (see LICENSE)
7-- License : BSD3-style (see LICENSE)
10-- Maintainer : kolmodin@gmail.com
9-- Maintainer : kolmodin@gmail.com
@@ -56,15 +55,13 @@ module Data.Binary.Bits.Get
56 -- $bitget
55 -- $bitget
59 , runBitGet
59 , runBitGet
61 -- ** Get bytes
62 -- ** Get bytes
64 , getWord8
64 , getWord8
68 , getWord16be
65 , getWord16be
69 , getWord32be
66 , getWord32be
70 , getWord64be
67 , getWord64be
@@ -82,9 +79,6 @@ module Data.Binary.Bits.Get
82 -- ** Read in Blocks
79 -- ** Read in Blocks
88 , word16be
82 , word16be
89 , word32be
83 , word32be
90 , word64be
84 , word64be
@@ -97,9 +91,6 @@ module Data.Binary.Bits.Get
98import Data.Binary.Get as B ( Get, getLazyByteString, isEmpty )
92import Data.Binary.Get as B ( Get, getLazyByteString, isEmpty )
99import Data.Binary.Get.Internal as B ( get, put, ensureN )
93import 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
104import Data.ByteString as B
95import Data.ByteString as B
105import qualified Data.ByteString.Lazy as L
96import qualified Data.ByteString.Lazy as L
@@ -115,8 +106,12 @@ import Control.Applicative
115import Control.Monad (when,foldM_)
106import Control.Monad (when,foldM_)
117import Prelude as P
108import Prelude as P
110+#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
118import GHC.Word
112import GHC.Word
119−import GHC.Base (uncheckedShiftLWord8#, wordToWord8#, Int (..), andWord8#, Word8#, (-#), Int#, xorI#, eqWord8#, TYPE)
113+#endif
121-- $bitget
116-- $bitget
122-- Parse bits using a monad.
117-- Parse bits using a monad.
@@ -169,6 +164,16 @@ data S = S {-# UNPACK #-} !ByteString -- Input
169 !BitOrder -- Bit order
164 !BitOrder -- Bit order
170 deriving (Show)
165 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 #-}
172-- | Increment the current bit offset
177-- | Increment the current bit offset
173incS :: Int -> S -> S
178incS :: Int -> S -> S
174incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
179incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
@@ -177,70 +182,83 @@ incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
177 !d = byte_offset o'
182 !d = byte_offset o'
178 !n' = bit_offset o'
183 !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)
180-- | Read a single bit
214-- | Read a single bit
181readBool :: S -> Bool
215readBool :: S -> Bool
182readBool (S bs o bo) = case bo of
216readBool (S bs o bo) = case bo of
183 BB -> testBit (unsafeHead bs) (7-o)
217 BB -> testBit (unsafeHead bs) (7-o)
184− BL -> testBit (unsafeHead bs) (7-o)
185− LL -> testBit (unsafeHead bs) o
186 LB -> testBit (unsafeHead bs) o
218 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#)
200-- | Extract a range of bits from (ws :: ByteString)
220-- | Extract a range of bits from (ws :: ByteString)
202-- Constraint: 8 * (length ws -1 ) < o+n <= 8 * length ws
222-- Constraint: 8 * (length ws -1 ) < o+n <= 8 * length ws
203−extract :: (Num a, FastBits a) => BitOrder -> ByteString -> Int -> Int -> a
223+extract :: (Num a, Bits a, FastBits a) => BitOrder -> ByteString -> Int -> Int -> a
204extract bo bs o n
224extract bo bs o n
205 | n == 0 = 0
225 | n == 0 = 0
206 | B.length bs == 0 = error "Empty ByteString"
226 | B.length bs == 0 = error "Empty ByteString"
207− | otherwise = rev . mask n . foldlWithIndex' f 0 $ bs
227+ | otherwise = mask n (foldlWithIndex' f 0 bs)
209 -- B.foldl' with index
229 -- B.foldl' with index
210 foldlWithIndex' op b = fst . B.foldl' g (b,0)
230 foldlWithIndex' op b = fst . B.foldl' g (b,0)
211 where g (b',i) w = (op b' w i, (i+1))
231 where g (b',i) w = (op b' w i, (i+1))
213 -- 'or' correctly shifted words
233 -- 'or' correctly shifted words
214− f b w i = b .|. (fromIntegral w `fastShift` off i)
234+ f b w i = b .|. (fromIntegral w `fastShift` off i)
216 -- co-offset
236 -- co-offset
217− r = B.length bs * 8 - (o + n)
237+ r = B.length bs * 8 - (o + n)
219 -- shift offset depending on the byte position (0..B.length-1)
239 -- shift offset depending on the byte position (0..B.length-1)
220− off i = case bo of
240+ off i = case bo of
222 LB -> 8*i - o
241 LB -> 8*i - o
223 BB -> (B.length bs -1 - i) * 8 - r
242 BB -> (B.length bs -1 - i) * 8 - r
224− BL -> (B.length bs -1 - i) * 8 - r
226− -- reverse bits if necessary
234-- | Generic readWord
245-- | Generic readWord
235−readWord :: (Num a, FastBits a) => Int -> S -> a
246+readWord :: (Num a, Bits a, FastBits a) => Int -> S -> a
236readWord n (S bs o bo)
247readWord n (S bs o bo)
237 | n == 0 = 0
248 | 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
238− | otherwise = extract bo (unsafeTake nbytes bs) o n
257+ | otherwise = extract bo (unsafeTake ((o+n+7) `div` 8) bs) o n
239− where nbytes = byte_offset (o+n+7)
241-- | Check that the number of bits to read is not greater than the first parameter
259-- | Check that the number of bits to read is not greater than the first parameter
242{-# INLINE readWordChecked #-}
260{-# INLINE readWordChecked #-}
243−readWordChecked :: (Num a, FastBits a) => Int -> Int -> S -> a
261+readWordChecked :: (Num a, Bits a, FastBits a) => Int -> Int -> S -> a
244readWordChecked m n s
262readWordChecked m n s
245 | n > m = error $ "Tried to read more than " ++ show m ++ " bits (" ++ show n ++")"
263 | n > m = error $ "Tried to read more than " ++ show m ++ " bits (" ++ show n ++")"
246 | otherwise = readWord n s
264 | otherwise = readWord n s
@@ -248,37 +266,36 @@ readWordChecked m n s
248-- | Read the given number of bytes and return them in Big-Endian order
266-- | Read the given number of bytes and return them in Big-Endian order
250-- Examples:
268-- Examples:
251−-- BB: xxxABCDE FGHIJKLM NOPxxxxx -> ABCDEFGH IJKLMNOP
269+-- BB: xxxABCDE FGHIJKLM NOPQRxxx -> ABCDEFGH IJKLMNOP QRxxxxxx
252−-- LL: LMNOPxxx DEFGHIJK xxxxxABC -> ABCDEFGH IJKLMNOP
270+-- LB: NOPQRxxx FGHIJKLM xxxABCDE -> ABCDEFGH IJKLMNOP QRxxxxxx
253−-- BL: xxxPONML KJIHGFED CBAxxxxx -> ABCDEFGH IJKLMNOP
254−-- LB: EDCBAxxx MLKJIHGF xxxxxPON -> ABCDEFGH IJKLMNOP
255readByteString :: Int -> S -> ByteString
271readByteString :: Int -> S -> ByteString
256−readByteString n (S bs o bo) =
272+readByteString n (S bs o bo)
258− bs' = unsafeTake (n+1) bs
259− bs'' = unsafeTake n bs
273+ | o == 0 = 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)
269− (_,BB) -> unsafePerformIO $ do
274+ | otherwise = unsafePerformIO $ do
270− let len = n+1
275+ let len = n+1
271− ptr <- mallocBytes len
276+ ptr <- mallocBytes len
272− let f r i = do
279+ let f r i = do
274− w = unsafeIndex bs (len-i)
281+ w = unsafeIndex bs (len-i)
275− w' = (w `fastShiftL` o) .|. r
282+ w' = (w `fastShiftL` o) .|. r
276− r' = w `fastShiftR` (8-o)
283+ r' = w `fastShiftR` (8-o)
277− poke (ptr `plusPtr` (len-i)) w'
284+ poke (ptr `plusPtr` (len-i)) w'
278− return r'
285+ return r'
279− foldM_ f 0 [1..len]
286+ foldM_ f 0 [1..len]
280− unsafeInit <$> unsafePackMallocCStringLen (ptr,len)
287+ 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)
283------------------------------------------------------------------------
300------------------------------------------------------------------------
284-- | 'BitGet' is a monad, applicative and a functor. See 'runBitGet'
301-- | 'BitGet' is a monad, applicative and a functor. See 'runBitGet'
@@ -287,6 +304,7 @@ newtype BitGet a = B { runState :: S -> Get (S,a) }
288instance Monad BitGet where
305instance Monad BitGet where
289 return x = B $ \s -> return (s,x)
306 return x = B $ \s -> return (s,x)
307+ fail str = B $ \s -> putBackState s >> fail str
290 (B f) >>= g = B $ \s -> do (s',a) <- f s
308 (B f) >>= g = B $ \s -> do (s',a) <- f s
291 runState (g a) s'
309 runState (g a) s'
@@ -297,29 +315,6 @@ instance Applicative BitGet where
297 pure x = return x
315 pure x = return x
298 fm <*> m = fm >>= \f -> m >>= \v -> return (f v)
316 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
323-- | Run a 'BitGet' within the Binary packages 'Get' monad. If a byte has
318-- | Run a 'BitGet' within the Binary packages 'Get' monad. If a byte has
324-- been partially consumed it will be discarded once 'runBitGet' is finished.
319-- been partially consumed it will be discarded once 'runBitGet' is finished.
325runBitGet :: BitGet a -> Get a
320runBitGet :: BitGet a -> Get a
@@ -367,6 +362,23 @@ ensureBits n = do
367 put B.empty
362 put B.empty
368 return (S (bs`append`bs') o bo, ())
363 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
370-- | Test whether all input has been consumed, i.e. there are no remaining
382-- | Test whether all input has been consumed, i.e. there are no remaining
371-- undecoded bytes.
383-- undecoded bytes.
372isEmpty :: BitGet Bool
384isEmpty :: BitGet Bool
@@ -378,37 +390,21 @@ isEmpty = B $ \ (S bs o bo) -> if B.null bs
378getBool :: BitGet Bool
390getBool :: BitGet Bool
379getBool = block bool
391getBool = block bool
381−-- | Get 1 bit as a 'Bool'.
382−getBool# :: BitGet Int
383−getBool# = block bool#
385-- | Get @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
393-- | Get @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
386getWord8 :: Int -> BitGet Word8
394getWord8 :: Int -> BitGet Word8
387getWord8 n = block (word8 n)
395getWord8 n = block (word8 n)
389-- | Get @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
397-- | Get @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
390−getWord16 :: Int -> BitGet Word16
391−getWord16 n = block (word16 n)
393getWord16be :: Int -> BitGet Word16
398getWord16be :: Int -> BitGet Word16
394−getWord16be = getWord16
399+getWord16be n = block (word16be n)
395−{-# DEPRECATED getWord16be "Use 'getWord16' instead" #-}
397-- | Get @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
401-- | Get @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
398−getWord32 :: Int -> BitGet Word32
399−getWord32 n = block (word32 n)
401getWord32be :: Int -> BitGet Word32
402getWord32be :: Int -> BitGet Word32
402−getWord32be = getWord32
403+getWord32be n = block (word32be n)
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)
409getWord64be :: Int -> BitGet Word64
406getWord64be :: Int -> BitGet Word64
410−getWord64be = getWord64
407+getWord64be n = block (word64be n)
411−{-# DEPRECATED getWord64be "Use 'getWord64' instead" #-}
413-- | Get @n@ bytes as a 'ByteString'.
409-- | Get @n@ bytes as a 'ByteString'.
414getByteString :: Int -> BitGet ByteString
410getByteString :: Int -> BitGet ByteString
@@ -457,40 +453,105 @@ block (Block i p) = do
457bool :: Block Bool
453bool :: Block Bool
458bool = Block 1 readBool
454bool = Block 1 readBool
460−-- | Read a 1 bit 'Bool'.
462−bool# = Block 1 readBool#
464-- | Read @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
456-- | Read @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
465word8 :: Int -> Block Word8
457word8 :: Int -> Block Word8
466word8 n = Block n (readWordChecked 8 n)
458word8 n = Block n (readWordChecked 8 n)
468-- | Read @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
460-- | 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)
472word16be :: Int -> Block Word16
461word16be :: Int -> Block Word16
473−word16be = word16
462+word16be n = Block n (readWordChecked 16 n)
474−{-# DEPRECATED word16be "Use 'word16' instead" #-}
476-- | Read @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
464-- | 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)
480word32be :: Int -> Block Word32
465word32be :: Int -> Block Word32
481−word32be = word32
466+word32be n = Block n (readWordChecked 32 n)
482−{-# DEPRECATED word32be "Use 'word32' instead" #-}
484-- | Read @n@ bits as a 'Word64'. @n@ must be within @[0..64]@.
468-- | 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)
488word64be :: Int -> Block Word64
469word64be :: Int -> Block Word64
489−word64be = word64
470+word64be n = Block n (readWordChecked 64 n)
490−{-# DEPRECATED word64be "Use 'word64' instead" #-}
492-- | Read @n@ bytes as a 'ByteString'.
472-- | Read @n@ bytes as a 'ByteString'.
493byteString :: Int -> Block ByteString
473byteString :: Int -> Block ByteString
494byteString n | n > 0 = Block (n*8) (readByteString n)
474byteString n | n > 0 = Block (n*8) (readByteString n)
495 | otherwise = Block 0 (\_ -> B.empty)
475 | 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)