memory-0.18.0: memory and related abstraction stuff

License | BSD-style
Maintainer | Vincent Hanquez <vincent@snarc.org>
Stability | stable
Portability | Good
Safe Haskell | None
Language | Haskell2010

Data.ByteArray

Contents

- ByteArray Classes
- ByteArray built-in types
- ByteArray methods

Description

Simple and efficient byte array types

This module should be imported qualified.

Synopsis

- class ByteArrayAccess ba where
  - length :: ba -> Int
  - withByteArray :: ba -> (Ptr p -> IO a) -> IO a
  - copyByteArrayToPtr :: ba -> Ptr p -> IO ()
- class (Eq ba, Ord ba, Monoid ba, ByteArrayAccess ba) => ByteArray ba where
  - allocRet :: Int -> (Ptr p -> IO a) -> IO (a, ba)
- data Bytes
- data ScrubbedBytes
- data MemView = MemView !(Ptr Word8) !Int
- data View bytes
- view :: ByteArrayAccess bytes => bytes -> Int -> Int -> View bytes
- takeView :: ByteArrayAccess bytes => bytes -> Int -> View bytes
- dropView :: ByteArrayAccess bytes => bytes -> Int -> View bytes
- index :: ByteArrayAccess a => a -> Int -> Word8
- concat :: (ByteArrayAccess bin, ByteArray bout) => [bin] -> bout
- empty :: ByteArray a => a
- uncons :: ByteArray a => a -> Maybe (Word8, a)
- null :: ByteArrayAccess a => a -> Bool
- replicate :: ByteArray ba => Int -> Word8 -> ba
- take :: ByteArray bs => Int -> bs -> bs
- drop :: ByteArray bs => Int -> bs -> bs
- splitAt :: ByteArray bs => Int -> bs -> (bs, bs)
- span :: ByteArray bs => (Word8 -> Bool) -> bs -> (bs, bs)
- reverse :: ByteArray bs => bs -> bs
- any :: ByteArrayAccess ba => (Word8 -> Bool) -> ba -> Bool
- all :: ByteArrayAccess ba => (Word8 -> Bool) -> ba -> Bool
- xor :: (ByteArrayAccess a, ByteArrayAccess b, ByteArray c) => a -> b -> c
- singleton :: ByteArray a => Word8 -> a
- cons :: ByteArray a => Word8 -> a -> a
- append :: ByteArray bs => bs -> bs -> bs
- snoc :: ByteArray a => a -> Word8 -> a
- create :: ByteArray ba => Int -> (Ptr p -> IO ()) -> IO ba
- copy :: (ByteArrayAccess bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO ()) -> IO bs2
- unsafeCreate :: ByteArray a => Int -> (Ptr p -> IO ()) -> a
- pack :: ByteArray a => [Word8] -> a
- unpack :: ByteArrayAccess a => a -> [Word8]
- eq :: (ByteArrayAccess bs1, ByteArrayAccess bs2) => bs1 -> bs2 -> Bool
- alloc :: ByteArray ba => Int -> (Ptr p -> IO ()) -> IO ba
- allocAndFreeze :: ByteArray a => Int -> (Ptr p -> IO ()) -> a
- copyRet :: (ByteArrayAccess bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO a) -> IO (a, bs2)
- copyAndFreeze :: (ByteArrayAccess bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO ()) -> bs2
- zero :: ByteArray ba => Int -> ba
- convert :: (ByteArrayAccess bin, ByteArray bout) => bin -> bout
- constEq :: (ByteArrayAccess bs1, ByteArrayAccess bs2) => bs1 -> bs2 -> Bool

# ByteArray Classes

class ByteArrayAccess ba where #

Class to Access size properties and data of a ByteArray

Minimal complete definition

length, withByteArray

Methods

length :: ba -> Int #

Return the length in bytes of a bytearray

withByteArray :: ba -> (Ptr p -> IO a) -> IO a #

Allow to use using a pointer

copyByteArrayToPtr :: ba -> Ptr p -> IO () #

Copy the data of a bytearray to a ptr

#### Instances

Instances details

ByteArrayAccess String # |
Instance details Defined in Data.ByteArray.Types Methods length :: String -> Int # withByteArray :: String -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: String -> Ptr p -> IO () #
ByteArrayAccess ByteString # |
Instance details Defined in Data.ByteArray.Types Methods length :: ByteString -> Int # withByteArray :: ByteString -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: ByteString -> Ptr p -> IO () #
ByteArrayAccess Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods length :: Bytes -> Int # withByteArray :: Bytes -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: Bytes -> Ptr p -> IO () #
ByteArrayAccess MemView # |
Instance details Defined in Data.ByteArray.MemView Methods length :: MemView -> Int # withByteArray :: MemView -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: MemView -> Ptr p -> IO () #
ByteArrayAccess ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods length :: ScrubbedBytes -> Int # withByteArray :: ScrubbedBytes -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: ScrubbedBytes -> Ptr p -> IO () #
PrimType ty => ByteArrayAccess (Block ty) # |
Instance details Defined in Data.ByteArray.Types Methods length :: Block ty -> Int # withByteArray :: Block ty -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: Block ty -> Ptr p -> IO () #
PrimType ty => ByteArrayAccess (UArray ty) # |
Instance details Defined in Data.ByteArray.Types Methods length :: UArray ty -> Int # withByteArray :: UArray ty -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: UArray ty -> Ptr p -> IO () #
ByteArrayAccess bytes => ByteArrayAccess (View bytes) # |
Instance details Defined in Data.ByteArray.View Methods length :: View bytes -> Int # withByteArray :: View bytes -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: View bytes -> Ptr p -> IO () #
(KnownNat n, PrimType ty, Countable ty n) => ByteArrayAccess (BlockN n ty) # |
Instance details Defined in Data.ByteArray.Types Methods length :: BlockN n ty -> Int # withByteArray :: BlockN n ty -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: BlockN n ty -> Ptr p -> IO () #
(ByteArrayAccess ba, KnownNat n) => ByteArrayAccess (SizedByteArray n ba) # |
Instance details Defined in Data.ByteArray.Sized Methods length :: SizedByteArray n ba -> Int # withByteArray :: SizedByteArray n ba -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: SizedByteArray n ba -> Ptr p -> IO () #

class (Eq ba, Ord ba, Monoid ba, ByteArrayAccess ba) => ByteArray ba where #

Class to allocate new ByteArray of specific size

Methods

allocRet #

Arguments

:: Int | number of bytes to allocate. i.e. might not match the size of the given type `ba`.
-> (Ptr p -> IO a) |
-> IO (a, ba) |

allocate `n` bytes and perform the given operation

#### Instances

Instances details

ByteArray ByteString # |
Instance details Defined in Data.ByteArray.Types Methods allocRet :: Int -> (Ptr p -> IO a) -> IO (a, ByteString) #
ByteArray Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods allocRet :: Int -> (Ptr p -> IO a) -> IO (a, Bytes) #
ByteArray ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods allocRet :: Int -> (Ptr p -> IO a) -> IO (a, ScrubbedBytes) #
(Ord ty, PrimType ty) => ByteArray (Block ty) # |
Instance details Defined in Data.ByteArray.Types Methods allocRet :: Int -> (Ptr p -> IO a) -> IO (a, Block ty) #
(Ord ty, PrimType ty) => ByteArray (UArray ty) # |
Instance details Defined in Data.ByteArray.Types Methods allocRet :: Int -> (Ptr p -> IO a) -> IO (a, UArray ty) #

# ByteArray built-in types

data Bytes #

Simplest Byte Array

#### Instances

Instances details

NormalForm Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods toNormalForm :: Bytes -> () #
NFData Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods rnf :: Bytes -> () #
Monoid Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods mempty :: Bytes # mappend :: Bytes -> Bytes -> Bytes # mconcat :: [Bytes] -> Bytes #
Semigroup Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods (<>) :: Bytes -> Bytes -> Bytes # sconcat :: NonEmpty Bytes -> Bytes # stimes :: Integral b => b -> Bytes -> Bytes #
Show Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods showsPrec :: Int -> Bytes -> ShowS # show :: Bytes -> String # showList :: [Bytes] -> ShowS #
Eq Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods (==) :: Bytes -> Bytes -> Bool # (/=) :: Bytes -> Bytes -> Bool #
Ord Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods compare :: Bytes -> Bytes -> Ordering # (<) :: Bytes -> Bytes -> Bool # (<=) :: Bytes -> Bytes -> Bool # (>) :: Bytes -> Bytes -> Bool # (>=) :: Bytes -> Bytes -> Bool # max :: Bytes -> Bytes -> Bytes # min :: Bytes -> Bytes -> Bytes #
ByteArray Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods allocRet :: Int -> (Ptr p -> IO a) -> IO (a, Bytes) #
ByteArrayAccess Bytes # |
Instance details Defined in Data.ByteArray.Bytes Methods length :: Bytes -> Int # withByteArray :: Bytes -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: Bytes -> Ptr p -> IO () #

data ScrubbedBytes #

ScrubbedBytes is a memory chunk which have the properties of:

- Being scrubbed after its goes out of scope.
- A Show instance that doesn't actually show any content
- A Eq instance that is constant time

#### Instances

Instances details

NormalForm ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods toNormalForm :: ScrubbedBytes -> () #
NFData ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods rnf :: ScrubbedBytes -> () #
Monoid ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods mempty :: ScrubbedBytes # mappend :: ScrubbedBytes -> ScrubbedBytes -> ScrubbedBytes # mconcat :: [ScrubbedBytes] -> ScrubbedBytes #
Semigroup ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods (<>) :: ScrubbedBytes -> ScrubbedBytes -> ScrubbedBytes # sconcat :: NonEmpty ScrubbedBytes -> ScrubbedBytes # stimes :: Integral b => b -> ScrubbedBytes -> ScrubbedBytes #
IsString ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods fromString :: String -> ScrubbedBytes #
Show ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods showsPrec :: Int -> ScrubbedBytes -> ShowS # show :: ScrubbedBytes -> String # showList :: [ScrubbedBytes] -> ShowS #
Eq ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods (==) :: ScrubbedBytes -> ScrubbedBytes -> Bool # (/=) :: ScrubbedBytes -> ScrubbedBytes -> Bool #
Ord ScrubbedBytes # |

Instance details

Defined in Data.ByteArray.ScrubbedBytes

Methods

compare :: ScrubbedBytes -> ScrubbedBytes -> Ordering #

(<) :: ScrubbedBytes -> ScrubbedBytes -> Bool #

(<=) :: ScrubbedBytes -> ScrubbedBytes -> Bool #

(>) :: ScrubbedBytes -> ScrubbedBytes -> Bool #

(>=) :: ScrubbedBytes -> ScrubbedBytes -> Bool #

max :: ScrubbedBytes -> ScrubbedBytes -> ScrubbedBytes #

min :: ScrubbedBytes -> ScrubbedBytes -> ScrubbedBytes #

ByteArray ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods allocRet :: Int -> (Ptr p -> IO a) -> IO (a, ScrubbedBytes) #
ByteArrayAccess ScrubbedBytes # |
Instance details Defined in Data.ByteArray.ScrubbedBytes Methods length :: ScrubbedBytes -> Int # withByteArray :: ScrubbedBytes -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: ScrubbedBytes -> Ptr p -> IO () #

data MemView #

A simple abstraction to a piece of memory.

Do beware that garbage collection related to piece of memory could be triggered before this is used.

Only use with the appropriate handler has been used (e.g. withForeignPtr on ForeignPtr)

Constructors

MemView !(Ptr Word8) !Int |

#### Instances

Instances details

Show MemView # |
Instance details Defined in Data.ByteArray.MemView Methods showsPrec :: Int -> MemView -> ShowS # show :: MemView -> String # showList :: [MemView] -> ShowS #
Eq MemView # |
Instance details Defined in Data.ByteArray.MemView Methods (==) :: MemView -> MemView -> Bool # (/=) :: MemView -> MemView -> Bool #
ByteArrayAccess MemView # |
Instance details Defined in Data.ByteArray.MemView Methods length :: MemView -> Int # withByteArray :: MemView -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: MemView -> Ptr p -> IO () #

data View bytes #

a view on a given bytes

Equality test in constant time

#### Instances

Instances details

ByteArrayAccess bytes => Show (View bytes) # |
Instance details Defined in Data.ByteArray.View Methods showsPrec :: Int -> View bytes -> ShowS # show :: View bytes -> String # showList :: [View bytes] -> ShowS #
ByteArrayAccess bytes => Eq (View bytes) # |
Instance details Defined in Data.ByteArray.View Methods (==) :: View bytes -> View bytes -> Bool # (/=) :: View bytes -> View bytes -> Bool #
ByteArrayAccess bytes => Ord (View bytes) # |
Instance details Defined in Data.ByteArray.View Methods compare :: View bytes -> View bytes -> Ordering # (<) :: View bytes -> View bytes -> Bool # (<=) :: View bytes -> View bytes -> Bool # (>) :: View bytes -> View bytes -> Bool # (>=) :: View bytes -> View bytes -> Bool # max :: View bytes -> View bytes -> View bytes # min :: View bytes -> View bytes -> View bytes #
ByteArrayAccess bytes => ByteArrayAccess (View bytes) # |
Instance details Defined in Data.ByteArray.View Methods length :: View bytes -> Int # withByteArray :: View bytes -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: View bytes -> Ptr p -> IO () #

view #

Arguments

:: ByteArrayAccess bytes |
=> bytes | the byte array we put a view on
-> Int | the offset to start the byte array on
-> Int | the size of the view
-> View bytes |

create a view on a given bytearray

This function update the offset and the size in order to guarantee:

- offset >= 0
- size >= 0
- offset < length
- size =< length - offset

takeView #

Arguments

:: ByteArrayAccess bytes |
=> bytes | byte aray
-> Int | size of the view
-> View bytes |

create a view from the given bytearray

dropView #

Arguments

:: ByteArrayAccess bytes |
=> bytes | byte array
-> Int | the number of bytes do dropped before creating the view
-> View bytes |

create a view from the given byte array starting after having dropped the fist n bytes

# ByteArray methods

index :: ByteArrayAccess a => a -> Int -> Word8 #

return a specific byte indexed by a number from 0 in a bytearray

unsafe, no bound checking are done

concat :: (ByteArrayAccess bin, ByteArray bout) => [bin] -> bout #

Concatenate bytearray into a larger bytearray

empty :: ByteArray a => a #

Create an empty byte array

uncons :: ByteArray a => a -> Maybe (Word8, a) #

returns the first byte, and the remaining bytearray if the bytearray is not null

null :: ByteArrayAccess a => a -> Bool #

Check if a byte array is empty

replicate :: ByteArray ba => Int -> Word8 -> ba #

Create a bytearray of a specific size containing a repeated byte value

take :: ByteArray bs => Int -> bs -> bs #

Take the first `n` byte of a bytearray

drop :: ByteArray bs => Int -> bs -> bs #

drop the first `n` byte of a bytearray

splitAt :: ByteArray bs => Int -> bs -> (bs, bs) #

Split a bytearray at a specific length in two bytearray

span :: ByteArray bs => (Word8 -> Bool) -> bs -> (bs, bs) #

Split a bytearray at the point where `pred` becomes invalid

reverse :: ByteArray bs => bs -> bs #

Reverse a bytearray

any :: ByteArrayAccess ba => (Word8 -> Bool) -> ba -> Bool #

Check if any element of a byte array satisfies a predicate

all :: ByteArrayAccess ba => (Word8 -> Bool) -> ba -> Bool #

Check if all elements of a byte array satisfy a predicate

xor :: (ByteArrayAccess a, ByteArrayAccess b, ByteArray c) => a -> b -> c #

Create a xor of bytes between a and b.

the returns byte array is the size of the smallest input.

singleton :: ByteArray a => Word8 -> a #

Create a byte array from a single byte

cons :: ByteArray a => Word8 -> a -> a #

prepend a single byte to a byte array

append :: ByteArray bs => bs -> bs -> bs #

append one bytearray to the other

snoc :: ByteArray a => a -> Word8 -> a #

append a single byte to a byte array

create :: ByteArray ba => Int -> (Ptr p -> IO ()) -> IO ba #

Allocate a new bytearray of specific size, and run the initializer on this memory

copy :: (ByteArrayAccess bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO ()) -> IO bs2 #

Duplicate a bytearray into another bytearray, and run an initializer on it

unsafeCreate :: ByteArray a => Int -> (Ptr p -> IO ()) -> a #

similar to `create` but hide the allocation and initializer in a pure context

pack :: ByteArray a => [Word8] -> a #

Pack a list of bytes into a bytearray

unpack :: ByteArrayAccess a => a -> [Word8] #

Un-pack a bytearray into a list of bytes

eq :: (ByteArrayAccess bs1, ByteArrayAccess bs2) => bs1 -> bs2 -> Bool #

Check if two bytearray are equals

This is not constant time, as soon some byte differs the function will returns. use `constEq` in sensitive context where timing matters.

alloc :: ByteArray ba => Int -> (Ptr p -> IO ()) -> IO ba #

Allocate a new bytearray of specific size, and run the initializer on this memory

allocAndFreeze :: ByteArray a => Int -> (Ptr p -> IO ()) -> a #

similar to `alloc` but hide the allocation and initializer in a pure context

copyRet :: (ByteArrayAccess bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO a) -> IO (a, bs2) #

Similar to `copy` but also provide a way to return a value from the initializer

copyAndFreeze :: (ByteArrayAccess bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO ()) -> bs2 #

Similiar to `copy` but expect the resulting bytearray in a pure context

zero :: ByteArray ba => Int -> ba #

Create a bytearray of a specific size initialized to 0

convert :: (ByteArrayAccess bin, ByteArray bout) => bin -> bout #

Convert a bytearray to another type of bytearray

constEq :: (ByteArrayAccess bs1, ByteArrayAccess bs2) => bs1 -> bs2 -> Bool #

A constant time equality test for 2 ByteArrayAccess values.

If values are of 2 different sizes, the function will abort early without comparing any bytes.

compared to == , this function will go over all the bytes present before yielding a result even when knowing the overall result early in the processing.