summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--erebos.cabal13
-rw-r--r--src/bytearray-internal/Data/ByteArray.hs82
2 files changed, 94 insertions, 1 deletions
diff --git a/erebos.cabal b/erebos.cabal
index a17e140..33137ce 100644
--- a/erebos.cabal
+++ b/erebos.cabal
@@ -55,6 +55,10 @@ flag bytearray-memory
description: Use 'memory' package to provide the ByteArray module
default: False
+flag bytearray-internal
+ description: Use internal implementation of the ByteArray module
+ default: False
+
source-repository head
type: git
@@ -248,14 +252,21 @@ library
build-depends: base < 0
if flag(bytearray-ram)
- if flag(bytearray-memory)
+ if flag(bytearray-memory) || flag(bytearray-internal)
build-depends:
base < 0
build-depends:
ram ^>= { 0.21, 0.22 }
elif flag(bytearray-memory)
+ if flag(bytearray-internal)
+ build-depends:
+ base < 0
build-depends:
memory ^>= { 0.14, 0.15, 0.16, 0.17, 0.18 },
+ elif flag(bytearray-internal)
+ hs-source-dirs: src/bytearray-internal
+ other-modules:
+ Data.ByteArray
else
build-depends:
base < 0
diff --git a/src/bytearray-internal/Data/ByteArray.hs b/src/bytearray-internal/Data/ByteArray.hs
new file mode 100644
index 0000000..3d2a0d1
--- /dev/null
+++ b/src/bytearray-internal/Data/ByteArray.hs
@@ -0,0 +1,82 @@
+module Data.ByteArray (
+ ByteArray(..),
+ ByteArrayAccess(..),
+
+ ScrubbedBytes,
+
+ take,
+ convert,
+) where
+
+import Prelude hiding (length, take)
+
+import Control.Exception
+
+import Data.Bits
+import Data.ByteString (ByteString)
+import Data.ByteString qualified as BS
+import Data.ByteString.Internal qualified as BSI
+import Data.ByteString.Unsafe qualified as BS
+import Data.List (foldl')
+
+import Foreign.ForeignPtr
+import Foreign.Marshal.Alloc
+import Foreign.Marshal.Utils
+import Foreign.Ptr
+
+import System.IO.Unsafe
+
+
+class ByteArrayAccess ba where
+ length :: ba -> Int
+ withByteArray :: ba -> (Ptr p -> IO a) -> IO a
+
+class (Eq ba, Ord ba, Monoid ba, ByteArrayAccess ba) => ByteArray ba where
+ allocRet :: Int -> (Ptr p -> IO a) -> IO ( a, ba )
+
+
+instance ByteArrayAccess BS.ByteString where
+ length = BS.length
+ withByteArray bs f = BS.unsafeUseAsCString bs (f . castPtr)
+
+instance ByteArray BS.ByteString where
+ allocRet len f = do
+ fptr <- BSI.mallocByteString len
+ x <- withForeignPtr fptr $ f . castPtr
+ return ( x, BSI.fromForeignPtr fptr 0 len )
+
+
+newtype ScrubbedBytes = ScrubbedBytes ByteString
+ deriving (Ord, ByteArrayAccess)
+
+instance Eq ScrubbedBytes where
+ ScrubbedBytes a == ScrubbedBytes b =
+ BS.length a == BS.length b && foldl' (.|.) 0 (BS.zipWith xor a b) == 0
+
+instance Semigroup ScrubbedBytes where
+ a <> b = unsafePerformIO $ fmap snd $ allocRet (la + lb) $ \dst -> do
+ withByteArray a $ \src -> copyBytes dst src la
+ withByteArray b $ \src -> copyBytes (dst `plusPtr` la) src lb
+ where
+ la = length a
+ lb = length b
+
+instance Monoid ScrubbedBytes where
+ mempty = ScrubbedBytes BS.empty
+
+instance ByteArray ScrubbedBytes where
+ allocRet len f = mask $ \restore -> do
+ ptr <- mallocBytes len
+ let scrubAndFree = fillBytes ptr 0 len >> free ptr
+ x <- restore (f $ castPtr ptr) `onException` scrubAndFree
+ ( x, ) . ScrubbedBytes <$> BS.unsafePackCStringFinalizer ptr len scrubAndFree
+
+
+take :: Int -> ScrubbedBytes -> ScrubbedBytes
+take n (ScrubbedBytes bs) = ScrubbedBytes (BS.take n bs)
+
+convert :: (ByteArrayAccess bin, ByteArray bout) => bin -> bout
+convert ba = snd $ unsafePerformIO $ allocRet len $ \dst -> do
+ withByteArray ba $ \src -> copyBytes dst src len
+ where
+ len = length ba