packages feed

binary-strict 0.2.1 → 0.2.2

raw patch · 5 files changed

+118/−270 lines, 5 files

Files

binary-strict.cabal view
@@ -1,5 +1,5 @@ name:            binary-strict-version:         0.2.1+version:         0.2.2 license:         BSD3 license-file:    LICENSE author:          Lennart Kolmodin <kolmodin@dtek.chalmers.se>@@ -19,6 +19,6 @@                  Data.Binary.Strict.BitGet extensions:      CPP, FlexibleContexts hs-source-dirs:  src-extra-source-files: tests/BitGetTest.hs+extra-source-files: tests/BitGetTest.hs, src/Data/Binary/Strict/Common.h ghc-options:     -Wall build-type:      Simple
src/Data/Binary/Strict/BitGet.hs view
@@ -57,6 +57,8 @@   , getWordhost ) where +#include "Common.h"+ import qualified Data.ByteString as B import qualified Data.ByteString.Internal as B import Foreign@@ -193,7 +195,7 @@  getPtr :: Storable a => Int -> BitGet a getPtr n = do-    (fp, o, _) <- readN BRight n B.toForeignPtr+    (fp, o, _) <- readN BRight (n * 8) B.toForeignPtr     return . B.inlinePerformIO $ withForeignPtr fp $ \p -> peek (castPtr $ p `plusPtr` o) {-# INLINE getPtr #-} @@ -209,40 +211,17 @@ getRightByteString :: Int -> BitGet B.ByteString getRightByteString n = readN BRight n id -getWord8 :: BitGet Word8-getWord8 = getPtr (8 * sizeOf (undefined :: Word8))----------------------------------------------------------------------------- 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 :: BitGet Word-getWordhost = getPtr (8 * sizeOf (undefined :: Word))-{-# INLINE getWordhost #-}---- | /O(1)./ Read a 2 byte Word16 in native host order and host endianness.-getWord16host :: BitGet Word16-getWord16host = getPtr (8 * sizeOf (undefined :: Word16))-{-# INLINE getWord16host #-}---- | /O(1)./ Read a Word32 in native host order and host endianness.-getWord32host :: BitGet Word32-getWord32host = getPtr  (8 * sizeOf (undefined :: Word32))-{-# INLINE getWord32host #-}---- | /O(1)./ Read a Word64 in native host order and host endianess.-getWord64host   :: BitGet Word64-getWord64host = getPtr  (8 * sizeOf (undefined :: Word64))-{-# INLINE getWord64host #-}-+getRightByteStringBytes :: Int -> BitGet B.ByteString+getRightByteStringBytes = getRightByteString . ((*) 8)  leftPad :: Int -> B.ByteString -> B.ByteString leftPad len bs = if B.length bs < len then padded else bs where   padded = (B.pack $ take extraBytes $ repeat 0) `B.append` bs   extraBytes = len - B.length bs +GETWORDS(BitGet, getRightByteStringBytes)+GETHOSTWORDS(BitGet)+ getAsWord8 :: Int -> BitGet Word8 getAsWord8 n = readN BRight n $ (flip B.index) 0 @@ -250,71 +229,22 @@ getAsWord16 :: Int -> BitGet Word16 getAsWord16 n = do     s <- readN BRight n id >>= return . leftPad 2-    return $! (fromIntegral (s `B.index` 0) `shiftl_w16` 8) .|.-              (fromIntegral (s `B.index` 1))---- | Read a Word16 in little endian format-getWord16le :: BitGet Word16-getWord16le = do-    s <- readN BRight 16 id-    return $! (fromIntegral (s `B.index` 1) `shiftl_w16` 8) .|.-              (fromIntegral (s `B.index` 0) )+    return $! DECWORD16BE(s)+{-# INLINE getWord16be #-}  -- | Read a Word32 in big endian format getAsWord32 :: Int -> BitGet Word32 getAsWord32 n = do     s <- readN BRight n id >>= return . leftPad 4-    return $! (fromIntegral (s `B.index` 0) `shiftl_w32` 24) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w32` 16) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w32`  8) .|.-              (fromIntegral (s `B.index` 3) )+    return $! DECWORD32BE(s) {-# INLINE getWord32be #-} --- | Read a Word32 in little endian format-getWord32le :: BitGet Word32-getWord32le = do-    s <- readN BRight 32 id-    return $! (fromIntegral (s `B.index` 3) `shiftl_w32` 24) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w32` 16) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w32`  8) .|.-              (fromIntegral (s `B.index` 0) )-{-# INLINE getWord32le #-}- -- | Read a Word64 in big endian format getAsWord64 :: Int -> BitGet Word64 getAsWord64 n = do     s <- readN BRight n id >>= return . leftPad 8-    return $! (fromIntegral (s `B.index` 0) `shiftl_w64` 56) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w64` 48) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w64` 40) .|.-              (fromIntegral (s `B.index` 3) `shiftl_w64` 32) .|.-              (fromIntegral (s `B.index` 4) `shiftl_w64` 24) .|.-              (fromIntegral (s `B.index` 5) `shiftl_w64` 16) .|.-              (fromIntegral (s `B.index` 6) `shiftl_w64`  8) .|.-              (fromIntegral (s `B.index` 7) )+    return $! DECWORD64BE(s) {-# INLINE getWord64be #-}---- | Read a Word64 in little endian format-getWord64le :: BitGet Word64-getWord64le = do-    s <- readN BRight 64 id-    return $! (fromIntegral (s `B.index` 7) `shiftl_w64` 56) .|.-              (fromIntegral (s `B.index` 6) `shiftl_w64` 48) .|.-              (fromIntegral (s `B.index` 5) `shiftl_w64` 40) .|.-              (fromIntegral (s `B.index` 4) `shiftl_w64` 32) .|.-              (fromIntegral (s `B.index` 3) `shiftl_w64` 24) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w64` 16) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w64`  8) .|.-              (fromIntegral (s `B.index` 0) )--getWord16be :: BitGet Word16-getWord16be = getAsWord16 16--getWord32be :: BitGet Word32-getWord32be = getAsWord32 32--getWord64be :: BitGet Word64-getWord64be = getAsWord64 64  shiftl_w16 :: Word16 -> Int -> Word16 shiftl_w32 :: Word32 -> Int -> Word32
+ src/Data/Binary/Strict/Common.h view
@@ -0,0 +1,96 @@+#define GETHOSTWORD(name, m, type) \+name :: m type ; \+name = getPtr (sizeOf (undefined :: type))++#define GETHOSTWORDS(m) \+GETHOSTWORD(getWord8, m, Word8); \+GETHOSTWORD(getWordhost, m, Word) ; \+GETHOSTWORD(getWord16host, m, Word16) ; \+GETHOSTWORD(getWord32host, m, Word32) ; \+GETHOSTWORD(getWord64host, m, Word64) ; \++#define DECWORD16LE(s) \+  ((fromIntegral (s `B.index` 1) `shiftl_w16` 8) .|. \+  (fromIntegral (s `B.index` 0) ) )++#define DECWORD16BE(s) \+  ((fromIntegral (s `B.index` 0) `shiftl_w16` 8) .|. \+  (fromIntegral (s `B.index` 1) ) )++#define DECWORD32BE(s) \+  ((fromIntegral (s `B.index` 0) `shiftl_w32` 24) .|. \+  (fromIntegral (s `B.index` 1) `shiftl_w32` 16) .|. \+  (fromIntegral (s `B.index` 2) `shiftl_w32`  8) .|. \+  (fromIntegral (s `B.index` 3) ) )++#define DECWORD32LE(s) \+  ((fromIntegral (s `B.index` 3) `shiftl_w32` 24) .|. \+  (fromIntegral (s `B.index` 2) `shiftl_w32` 16) .|. \+  (fromIntegral (s `B.index` 1) `shiftl_w32`  8) .|. \+  (fromIntegral (s `B.index` 0) ) )++#define DECWORD64BE(s) \+  ((fromIntegral (s `B.index` 0) `shiftl_w64` 56) .|. \+  (fromIntegral (s `B.index` 1) `shiftl_w64` 48) .|. \+  (fromIntegral (s `B.index` 2) `shiftl_w64` 40) .|. \+  (fromIntegral (s `B.index` 3) `shiftl_w64` 32) .|. \+  (fromIntegral (s `B.index` 4) `shiftl_w64` 24) .|. \+  (fromIntegral (s `B.index` 5) `shiftl_w64` 16) .|. \+  (fromIntegral (s `B.index` 6) `shiftl_w64`  8) .|. \+  (fromIntegral (s `B.index` 7) ) )++#define DECWORD64LE(s) \+  ((fromIntegral (s `B.index` 7) `shiftl_w64` 56) .|. \+  (fromIntegral (s `B.index` 6) `shiftl_w64` 48) .|. \+  (fromIntegral (s `B.index` 5) `shiftl_w64` 40) .|. \+  (fromIntegral (s `B.index` 4) `shiftl_w64` 32) .|. \+  (fromIntegral (s `B.index` 3) `shiftl_w64` 24) .|. \+  (fromIntegral (s `B.index` 2) `shiftl_w64` 16) .|. \+  (fromIntegral (s `B.index` 1) `shiftl_w64`  8) .|. \+  (fromIntegral (s `B.index` 0) ) )++#define GETWORD16LE(name, m, f) \+name :: m Word16 ; \+name = do { \+  s <- f 2; \+  return $! DECWORD16LE(s) }++#define GETWORD16BE(name, m, f) \+name :: m Word16 ; \+name = do { \+  s <- f 2; \+  return $! DECWORD16BE(s) }++#define GETWORD32LE(name, m, f) \+name :: m Word32 ; \+name  = do { \+  s <- f 4; \+  return $! DECWORD32LE(s) }++#define GETWORD32BE(name, m, f) \+name :: m Word32 ; \+name = do { \+  s <- f 4; \+  return $! DECWORD32BE(s) }++#define GETWORD64LE(name, m, f) \+name :: m Word64 ; \+name = do { \+  s <- f 8; \+  return $! DECWORD64LE(s) }++#define GETWORD64BE(name, m, f) \+name :: m Word64 ; \+name = do { \+  s <- f 8; \+  return $! DECWORD64BE(s) }++#define GETWORDS(m, f) \+GETWORD16LE(getWord16le, m, f); \+GETWORD16BE(getWord16be, m, f); \+GETWORD32LE(getWord32le, m, f); \+GETWORD32BE(getWord32be, m, f); \+GETWORD64LE(getWord64le, m, f); \+GETWORD64BE(getWord64be, m, f);++
src/Data/Binary/Strict/Get.hs view
@@ -33,6 +33,8 @@ #include "MachDeps.h" #endif +#include "Common.h"+ module Data.Binary.Strict.Get (     -- * The Get type       Get@@ -213,100 +215,9 @@     return . B.inlinePerformIO $ withForeignPtr fp $ \p -> peek (castPtr $ p `plusPtr` o) {-# INLINE getPtr #-} -getWord8 :: Get Word8-getWord8 = getPtr (sizeOf (undefined :: Word8))-{-# INLINE getWord8 #-}---- | Read a Word16 in big endian format-getWord16be :: Get Word16-getWord16be = do-    s <- readN 2 id-    return $! (fromIntegral (s `B.index` 0) `shiftl_w16` 8) .|.-              (fromIntegral (s `B.index` 1))---- | Read a Word16 in little endian format-getWord16le :: Get Word16-getWord16le = do-    s <- readN 2 id-    return $! (fromIntegral (s `B.index` 1) `shiftl_w16` 8) .|.-              (fromIntegral (s `B.index` 0) )-{-# INLINE getWord16le #-}---- | Read a Word32 in big endian format-getWord32be :: Get Word32-getWord32be = do-    s <- readN 4 id-    return $! (fromIntegral (s `B.index` 0) `shiftl_w32` 24) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w32` 16) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w32`  8) .|.-              (fromIntegral (s `B.index` 3) )-{-# INLINE getWord32be #-}---- | Read a Word32 in little endian format-getWord32le :: Get Word32-getWord32le = do-    s <- readN 4 id-    return $! (fromIntegral (s `B.index` 3) `shiftl_w32` 24) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w32` 16) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w32`  8) .|.-              (fromIntegral (s `B.index` 0) )-{-# INLINE getWord32le #-}---- | Read a Word64 in big endian format-getWord64be :: Get Word64-getWord64be = do-    s <- readN 8 id-    return $! (fromIntegral (s `B.index` 0) `shiftl_w64` 56) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w64` 48) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w64` 40) .|.-              (fromIntegral (s `B.index` 3) `shiftl_w64` 32) .|.-              (fromIntegral (s `B.index` 4) `shiftl_w64` 24) .|.-              (fromIntegral (s `B.index` 5) `shiftl_w64` 16) .|.-              (fromIntegral (s `B.index` 6) `shiftl_w64`  8) .|.-              (fromIntegral (s `B.index` 7) )-{-# INLINE getWord64be #-}---- | Read a Word64 in little endian format-getWord64le :: Get Word64-getWord64le = do-    s <- readN 8 id-    return $! (fromIntegral (s `B.index` 7) `shiftl_w64` 56) .|.-              (fromIntegral (s `B.index` 6) `shiftl_w64` 48) .|.-              (fromIntegral (s `B.index` 5) `shiftl_w64` 40) .|.-              (fromIntegral (s `B.index` 4) `shiftl_w64` 32) .|.-              (fromIntegral (s `B.index` 3) `shiftl_w64` 24) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w64` 16) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w64`  8) .|.-              (fromIntegral (s `B.index` 0) )----------------------------------------------------------------------------- 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 = getPtr (sizeOf (undefined :: Word))-{-# INLINE getWordhost #-}---- | /O(1)./ Read a 2 byte Word16 in native host order and host endianness.-getWord16host :: Get Word16-getWord16host = getPtr (sizeOf (undefined :: Word16))-{-# INLINE getWord16host #-}---- | /O(1)./ Read a Word32 in native host order and host endianness.-getWord32host :: Get Word32-getWord32host = getPtr  (sizeOf (undefined :: Word32))-{-# INLINE getWord32host #-}---- | /O(1)./ Read a Word64 in native host order and host endianess.-getWord64host   :: Get Word64-getWord64host = getPtr  (sizeOf (undefined :: Word64))-{-# INLINE getWord64host #-}--+GETWORDS(Get, getBytes)+GETHOSTWORDS(Get) -{-# INLINE getWord64le #-} shiftl_w16 :: Word16 -> Int -> Word16 shiftl_w32 :: Word32 -> Int -> Word32 shiftl_w64 :: Word64 -> Int -> Word64
src/Data/Binary/Strict/IncrementalGet.hs view
@@ -42,6 +42,8 @@ #include "MachDeps.h" #endif +#include "Common.h"+ module Data.Binary.Strict.IncrementalGet (     -- * The Get type       Get@@ -192,100 +194,9 @@     return . B.inlinePerformIO $ withForeignPtr fp $ \p -> peek (castPtr $ p `plusPtr` o) {-# INLINE getPtr #-} -getWord8 :: Get r Word8-getWord8 = getPtr (sizeOf (undefined :: Word8))-{-# INLINE getWord8 #-}---- | Read a Word16 in big endian format-getWord16be :: Get r Word16-getWord16be = do-    s <- readN 2 id-    return $! (fromIntegral (s `B.index` 0) `shiftl_w16` 8) .|.-              (fromIntegral (s `B.index` 1))---- | Read a Word16 in little endian format-getWord16le :: Get r Word16-getWord16le = do-    s <- readN 2 id-    return $! (fromIntegral (s `B.index` 1) `shiftl_w16` 8) .|.-              (fromIntegral (s `B.index` 0) )-{-# INLINE getWord16le #-}---- | Read a Word32 in big endian format-getWord32be :: Get r Word32-getWord32be = do-    s <- readN 4 id-    return $! (fromIntegral (s `B.index` 0) `shiftl_w32` 24) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w32` 16) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w32`  8) .|.-              (fromIntegral (s `B.index` 3) )-{-# INLINE getWord32be #-}---- | Read a Word32 in little endian format-getWord32le :: Get r Word32-getWord32le = do-    s <- readN 4 id-    return $! (fromIntegral (s `B.index` 3) `shiftl_w32` 24) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w32` 16) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w32`  8) .|.-              (fromIntegral (s `B.index` 0) )-{-# INLINE getWord32le #-}---- | Read a Word64 in big endian format-getWord64be :: Get r Word64-getWord64be = do-    s <- readN 8 id-    return $! (fromIntegral (s `B.index` 0) `shiftl_w64` 56) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w64` 48) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w64` 40) .|.-              (fromIntegral (s `B.index` 3) `shiftl_w64` 32) .|.-              (fromIntegral (s `B.index` 4) `shiftl_w64` 24) .|.-              (fromIntegral (s `B.index` 5) `shiftl_w64` 16) .|.-              (fromIntegral (s `B.index` 6) `shiftl_w64`  8) .|.-              (fromIntegral (s `B.index` 7) )-{-# INLINE getWord64be #-}---- | Read a Word64 in little endian format-getWord64le :: Get r Word64-getWord64le = do-    s <- readN 8 id-    return $! (fromIntegral (s `B.index` 7) `shiftl_w64` 56) .|.-              (fromIntegral (s `B.index` 6) `shiftl_w64` 48) .|.-              (fromIntegral (s `B.index` 5) `shiftl_w64` 40) .|.-              (fromIntegral (s `B.index` 4) `shiftl_w64` 32) .|.-              (fromIntegral (s `B.index` 3) `shiftl_w64` 24) .|.-              (fromIntegral (s `B.index` 2) `shiftl_w64` 16) .|.-              (fromIntegral (s `B.index` 1) `shiftl_w64`  8) .|.-              (fromIntegral (s `B.index` 0) )----------------------------------------------------------------------------- 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 r Word-getWordhost = getPtr (sizeOf (undefined :: Word))-{-# INLINE getWordhost #-}---- | /O(1)./ Read a 2 byte Word16 in native host order and host endianness.-getWord16host :: Get r Word16-getWord16host = getPtr (sizeOf (undefined :: Word16))-{-# INLINE getWord16host #-}---- | /O(1)./ Read a Word32 in native host order and host endianness.-getWord32host :: Get r Word32-getWord32host = getPtr  (sizeOf (undefined :: Word32))-{-# INLINE getWord32host #-}---- | /O(1)./ Read a Word64 in native host order and host endianess.-getWord64host   :: Get r Word64-getWord64host = getPtr  (sizeOf (undefined :: Word64))-{-# INLINE getWord64host #-}--+GETWORDS(Get r, getBytes)+GETHOSTWORDS(Get r) -{-# INLINE getWord64le #-} shiftl_w16 :: Word16 -> Int -> Word16 shiftl_w32 :: Word32 -> Int -> Word32 shiftl_w64 :: Word64 -> Int -> Word64