module Data.Attoparsec.Binary
(
anyWord16be
, anyWord16le
, anyWord32be
, anyWord32le
, anyWord64be
, anyWord64le
, word16be
, word16le
, word32be
, word32le
, word64be
, word64le
) where
import Data.Attoparsec
import Data.Bits
import qualified Data.ByteString as B
import Data.Word
byteSize :: (Bits a) => a -> Int
byteSize :: a -> Int
byteSize = (Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8) (Int -> Int) -> (a -> Int) -> a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. Bits a => a -> Int
bitSize
pack :: (Bits a, Num a) => B.ByteString -> a
pack :: ByteString -> a
pack = (a -> Word8 -> a) -> a -> ByteString -> a
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B.foldl' (\a
n Word8
h -> (a
n a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftL` Int
8) a -> a -> a
forall a. Bits a => a -> a -> a
.|. Word8 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
h) a
0
anyWordN :: (Bits a) => (B.ByteString -> a) -> Parser a
anyWordN :: (ByteString -> a) -> Parser a
anyWordN = a -> (ByteString -> a) -> Parser a
forall a. Bits a => a -> (ByteString -> a) -> Parser a
anyWordN' a
forall a. HasCallStack => a
undefined
where anyWordN' :: (Bits a) => a -> (B.ByteString -> a) -> Parser a
anyWordN' :: a -> (ByteString -> a) -> Parser a
anyWordN' a
d = ((ByteString -> a) -> Parser ByteString ByteString -> Parser a)
-> Parser ByteString ByteString -> (ByteString -> a) -> Parser a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (ByteString -> a) -> Parser ByteString ByteString -> Parser a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Parser ByteString ByteString -> (ByteString -> a) -> Parser a)
-> Parser ByteString ByteString -> (ByteString -> a) -> Parser a
forall a b. (a -> b) -> a -> b
$ Int -> Parser ByteString ByteString
Data.Attoparsec.take (Int -> Parser ByteString ByteString)
-> Int -> Parser ByteString ByteString
forall a b. (a -> b) -> a -> b
$ a -> Int
forall a. Bits a => a -> Int
byteSize a
d
anyWord16be :: Parser Word16
anyWord16be :: Parser Word16
anyWord16be = (ByteString -> Word16) -> Parser Word16
forall a. Bits a => (ByteString -> a) -> Parser a
anyWordN ByteString -> Word16
forall a. (Bits a, Num a) => ByteString -> a
pack
anyWord16le :: Parser Word16
anyWord16le :: Parser Word16
anyWord16le = (ByteString -> Word16) -> Parser Word16
forall a. Bits a => (ByteString -> a) -> Parser a
anyWordN ((ByteString -> Word16) -> Parser Word16)
-> (ByteString -> Word16) -> Parser Word16
forall a b. (a -> b) -> a -> b
$ ByteString -> Word16
forall a. (Bits a, Num a) => ByteString -> a
pack (ByteString -> Word16)
-> (ByteString -> ByteString) -> ByteString -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B.reverse
anyWord32be :: Parser Word32
anyWord32be :: Parser Word32
anyWord32be = (ByteString -> Word32) -> Parser Word32
forall a. Bits a => (ByteString -> a) -> Parser a
anyWordN ByteString -> Word32
forall a. (Bits a, Num a) => ByteString -> a
pack
anyWord32le :: Parser Word32
anyWord32le :: Parser Word32
anyWord32le = (ByteString -> Word32) -> Parser Word32
forall a. Bits a => (ByteString -> a) -> Parser a
anyWordN ((ByteString -> Word32) -> Parser Word32)
-> (ByteString -> Word32) -> Parser Word32
forall a b. (a -> b) -> a -> b
$ ByteString -> Word32
forall a. (Bits a, Num a) => ByteString -> a
pack (ByteString -> Word32)
-> (ByteString -> ByteString) -> ByteString -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B.reverse
anyWord64be :: Parser Word64
anyWord64be :: Parser Word64
anyWord64be = (ByteString -> Word64) -> Parser Word64
forall a. Bits a => (ByteString -> a) -> Parser a
anyWordN ByteString -> Word64
forall a. (Bits a, Num a) => ByteString -> a
pack
anyWord64le :: Parser Word64
anyWord64le :: Parser Word64
anyWord64le = (ByteString -> Word64) -> Parser Word64
forall a. Bits a => (ByteString -> a) -> Parser a
anyWordN ((ByteString -> Word64) -> Parser Word64)
-> (ByteString -> Word64) -> Parser Word64
forall a b. (a -> b) -> a -> b
$ ByteString -> Word64
forall a. (Bits a, Num a) => ByteString -> a
pack (ByteString -> Word64)
-> (ByteString -> ByteString) -> ByteString -> Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B.reverse
unpack :: (Bits a, Integral a) => a -> B.ByteString
unpack :: a -> ByteString
unpack a
x = [Word8] -> ByteString
B.pack ([Word8] -> ByteString) -> [Word8] -> ByteString
forall a b. (a -> b) -> a -> b
$ (Int -> Word8) -> [Int] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Word8
forall b. Num b => Int -> b
f ([Int] -> [Word8]) -> [Int] -> [Word8]
forall a b. (a -> b) -> a -> b
$ [Int] -> [Int]
forall a. [a] -> [a]
reverse [Int
0..a -> Int
forall a. Bits a => a -> Int
byteSize a
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
where f :: Int -> b
f Int
s = a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$ a -> Int -> a
forall a. Bits a => a -> Int -> a
shiftR a
x (Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
s)
wordN :: (Bits a) => (a -> B.ByteString) -> a -> Parser a
wordN :: (a -> ByteString) -> a -> Parser a
wordN a -> ByteString
u a
w = ByteString -> Parser ByteString ByteString
string (a -> ByteString
u a
w) Parser ByteString ByteString -> Parser a -> Parser a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return a
w
word16be :: Word16 -> Parser Word16
word16be :: Word16 -> Parser Word16
word16be = (Word16 -> ByteString) -> Word16 -> Parser Word16
forall a. Bits a => (a -> ByteString) -> a -> Parser a
wordN Word16 -> ByteString
forall a. (Bits a, Integral a) => a -> ByteString
unpack
word16le :: Word16 -> Parser Word16
word16le :: Word16 -> Parser Word16
word16le = (Word16 -> ByteString) -> Word16 -> Parser Word16
forall a. Bits a => (a -> ByteString) -> a -> Parser a
wordN ((Word16 -> ByteString) -> Word16 -> Parser Word16)
-> (Word16 -> ByteString) -> Word16 -> Parser Word16
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
B.reverse (ByteString -> ByteString)
-> (Word16 -> ByteString) -> Word16 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> ByteString
forall a. (Bits a, Integral a) => a -> ByteString
unpack
word32be :: Word32 -> Parser Word32
word32be :: Word32 -> Parser Word32
word32be = (Word32 -> ByteString) -> Word32 -> Parser Word32
forall a. Bits a => (a -> ByteString) -> a -> Parser a
wordN Word32 -> ByteString
forall a. (Bits a, Integral a) => a -> ByteString
unpack
word32le :: Word32 -> Parser Word32
word32le :: Word32 -> Parser Word32
word32le = (Word32 -> ByteString) -> Word32 -> Parser Word32
forall a. Bits a => (a -> ByteString) -> a -> Parser a
wordN ((Word32 -> ByteString) -> Word32 -> Parser Word32)
-> (Word32 -> ByteString) -> Word32 -> Parser Word32
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
B.reverse (ByteString -> ByteString)
-> (Word32 -> ByteString) -> Word32 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> ByteString
forall a. (Bits a, Integral a) => a -> ByteString
unpack
word64be :: Word64 -> Parser Word64
word64be :: Word64 -> Parser Word64
word64be = (Word64 -> ByteString) -> Word64 -> Parser Word64
forall a. Bits a => (a -> ByteString) -> a -> Parser a
wordN Word64 -> ByteString
forall a. (Bits a, Integral a) => a -> ByteString
unpack
word64le :: Word64 -> Parser Word64
word64le :: Word64 -> Parser Word64
word64le = (Word64 -> ByteString) -> Word64 -> Parser Word64
forall a. Bits a => (a -> ByteString) -> a -> Parser a
wordN ((Word64 -> ByteString) -> Word64 -> Parser Word64)
-> (Word64 -> ByteString) -> Word64 -> Parser Word64
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
B.reverse (ByteString -> ByteString)
-> (Word64 -> ByteString) -> Word64 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> ByteString
forall a. (Bits a, Integral a) => a -> ByteString
unpack