Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
## 0.21.0

+ `Data.ByteArray.Encoding`: replaced custom Base16/Base32/Base64 encode/decode
with `base16`, `base32`, and `base64` library calls. Input is converted to
`ByteString` via `B.convert`, the library function is applied, and the result
is converted back. `Base64OpenBSD` retains its custom implementation (no
library equivalent exists).
+ `Data.ByteArray.Bytes`: replaced low-level `GHC.Prim` `MutableByteArray#`
implementation with `newtype Bytes = Bytes ByteString`. Both use GHC's pinned
allocator; `ByteString` already implements `ByteArrayAccess` / `ByteArray`.
+ `Data.Memory.Hash.FNV`: replaced `readWord8OffAddr#` (GHC.Prim) with
`Foreign.Storable.peekByteOff` — portable and equivalent.
+ `Data.Memory.Internal.CompatPrim64`: deleted (was entirely unreferenced).
+ New dependencies: `base16 >=1.0 && <2`, `base32 >=0.4 && <1`,
`base64 >=1.0 && <2`, `text >=1.0 && <3`.
+ Net reduction: ~350 lines removed.

## 0.20.1
+ Remove `WITH_BYTESTRING_SUPPORT` CPP flag. `ByteString` instances for
`ByteArrayAccess` and `ByteArray` are now always compiled in, since
Expand Down
3 changes: 0 additions & 3 deletions Data/ByteArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
--
-- This module should be imported qualified.
--
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.ByteArray
(
Expand Down
193 changes: 26 additions & 167 deletions Data/ByteArray/Bytes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,191 +7,50 @@
--
-- Simple and efficient byte array types
--
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE DeriveDataTypeable #-}
module Data.ByteArray.Bytes
( Bytes
) where

import Control.DeepSeq(NFData(..))
import GHC.Exts (unsafeCoerce#)
import GHC.Word
import GHC.Char (chr)
import GHC.Types
import GHC.Prim
import GHC.Ptr
import Data.Semigroup
import Data.Foldable (toList)
import Data.Memory.PtrMethods
import Data.Memory.Internal.CompatPrim
import Data.Memory.Internal.Compat (unsafeDoIO)
import Control.DeepSeq (NFData(..))
import Data.ByteArray.Types
import Data.Char (chr)
import Data.Foldable (toList)
import Data.Semigroup
import Data.Typeable

import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BSI
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Ptr (castPtr)

-- | Simplest Byte Array
data Bytes = Bytes (MutableByteArray# RealWorld)
newtype Bytes = Bytes BS.ByteString
deriving (Typeable)

instance NFData Bytes where
rnf b = b `seq` ()

instance Show Bytes where
showsPrec p b r = showsPrec p (bytesUnpackChars b []) r
showsPrec p (Bytes b) = showsPrec p (map (chr . fromIntegral) (BS.unpack b))

instance Eq Bytes where
(==) = bytesEq
Bytes a == Bytes b = a == b

instance Ord Bytes where
compare = bytesCompare
compare (Bytes a) (Bytes b) = compare a b

instance Semigroup Bytes where
b1 <> b2 = unsafeDoIO $ bytesAppend b1 b2
sconcat = unsafeDoIO . bytesConcat . toList
Bytes a <> Bytes b = Bytes (a <> b)
sconcat bs = Bytes $ BS.concat [b | Bytes b <- toList bs]

instance Monoid Bytes where
mempty = unsafeDoIO (newBytes 0)
mempty = Bytes BS.empty

instance ByteArrayAccess Bytes where
length = bytesLength
withByteArray = withBytes
instance ByteArray Bytes where
allocRet = bytesAllocRet

------------------------------------------------------------------------
newBytes :: Int -> IO Bytes
newBytes (I# sz)
| booleanPrim (sz <# 0#) = error "Bytes: size must be >= 0"
| otherwise = IO $ \s ->
case newAlignedPinnedByteArray# sz 8# s of
(# s', mbarr #) -> (# s', Bytes mbarr #)

touchBytes :: Bytes -> IO ()
touchBytes (Bytes mba) = IO $ \s -> case touch# mba s of s' -> (# s', () #)
{-# INLINE touchBytes #-}

sizeofBytes :: Bytes -> Int
sizeofBytes (Bytes mba) = I# (sizeofMutableByteArray# mba)
{-# INLINE sizeofBytes #-}

withPtr :: Bytes -> (Ptr p -> IO a) -> IO a
withPtr b@(Bytes mba) f = do
a <- f (Ptr (byteArrayContents# (unsafeCoerce# mba)))
touchBytes b
return a
------------------------------------------------------------------------

bytesAlloc :: Int -> (Ptr p -> IO ()) -> IO Bytes
bytesAlloc sz f = do
ba <- newBytes sz
withPtr ba f
return ba

bytesConcat :: [Bytes] -> IO Bytes
bytesConcat l = bytesAlloc retLen (copy l)
where
!retLen = sum $ map bytesLength l
length (Bytes b) = BS.length b
withByteArray (Bytes b) = withByteArray b

copy [] _ = return ()
copy (x:xs) dst = do
withPtr x $ \src -> memCopy dst src chunkLen
copy xs (dst `plusPtr` chunkLen)
where
!chunkLen = bytesLength x

bytesAppend :: Bytes -> Bytes -> IO Bytes
bytesAppend b1 b2 = bytesAlloc retLen $ \dst -> do
withPtr b1 $ \s1 -> memCopy dst s1 len1
withPtr b2 $ \s2 -> memCopy (dst `plusPtr` len1) s2 len2
where
!len1 = bytesLength b1
!len2 = bytesLength b2
!retLen = len1 + len2

bytesAllocRet :: Int -> (Ptr p -> IO a) -> IO (a, Bytes)
bytesAllocRet sz f = do
ba <- newBytes sz
r <- withPtr ba f
return (r, ba)

bytesLength :: Bytes -> Int
bytesLength = sizeofBytes
{-# LANGUAGE bytesLength #-}

withBytes :: Bytes -> (Ptr p -> IO a) -> IO a
withBytes = withPtr

bytesEq :: Bytes -> Bytes -> Bool
bytesEq b1@(Bytes m1) b2@(Bytes m2)
| l1 /= l2 = False
| otherwise = unsafeDoIO $ IO $ \s -> loop 0# s
where
!l1@(I# len) = bytesLength b1
!l2 = bytesLength b2

loop i s
| booleanPrim (i ==# len) = (# s, True #)
| otherwise =
case readWord8Array# m1 i s of
(# s', e1 #) -> case readWord8Array# m2 i s' of
(# s'', e2 #) ->
if (W8# e1) == (W8# e2)
then loop (i +# 1#) s''
else (# s'', False #)
{-# INLINE loop #-}

bytesCompare :: Bytes -> Bytes -> Ordering
bytesCompare b1@(Bytes m1) b2@(Bytes m2) = unsafeDoIO $ loop 0
where
!l1 = bytesLength b1
!l2 = bytesLength b2
!len = min l1 l2

loop !i
| i == len =
if l1 == l2
then pure EQ
else if l1 > l2 then pure GT
else pure LT
| otherwise = do
e1 <- read8 m1 i
e2 <- read8 m2 i
if e1 == e2
then loop (i+1)
else if e1 < e2 then pure LT
else pure GT

read8 m (I# i) = IO $ \s -> case readWord8Array# m i s of
(# s2, e #) -> (# s2, W8# e #)

bytesUnpackChars :: Bytes -> String -> String
bytesUnpackChars (Bytes mba) xs = chunkLoop 0#
where
!len = sizeofMutableByteArray# mba
-- chunk 64 bytes at a time
chunkLoop :: Int# -> [Char]
chunkLoop idx
| booleanPrim (len ==# idx) = []
| booleanPrim ((len -# idx) ># 63#) =
bytesLoop idx 64# (chunkLoop (idx +# 64#))
| otherwise =
bytesLoop idx (len -# idx) xs

bytesLoop idx chunkLenM1 paramAcc = unsafeDoIO $
loop (idx +# chunkLenM1 -# 1#) paramAcc
where loop i acc
| booleanPrim (i ==# idx) = do
c <- rChar i
return (c : acc)
| otherwise = do
c <- rChar i
loop (i -# 1#) (c : acc)

rChar :: Int# -> IO Char
rChar idx = IO $ \s ->
case readWord8Array# mba idx s of
(# s2, w #) -> (# s2, chr (fromIntegral (W8# w)) #)

{-
bytesShowHex :: Bytes -> String
bytesShowHex b = showHexadecimal (withPtr b) (bytesLength b)
{-# NOINLINE bytesShowHex #-}
-}
instance ByteArray Bytes where
allocRet n f = do
fptr <- BSI.mallocByteString n
r <- withForeignPtr fptr (f . castPtr)
return (r, Bytes (BSI.PS fptr 0 n))
120 changes: 46 additions & 74 deletions Data/ByteArray/Encoding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ module Data.ByteArray.Encoding
import Data.ByteArray.Types
import qualified Data.ByteArray.Types as B
import qualified Data.ByteArray.Methods as B
import Data.Memory.Internal.Compat
import Data.Memory.Encoding.Base16
import Data.Memory.Encoding.Base32
import Data.Memory.Encoding.Base64
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Base32 as B32
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Base64.URL as B64URL
import Data.Base16.Types (extractBase16)
import Data.Base64.Types (extractBase64)
import qualified Data.Text as T
import Data.Memory.Encoding.Base64 (toBase64OpenBSD, fromBase64OpenBSD,
unBase64LengthUnpadded)
import Data.Memory.Internal.Compat (unsafeDoIO)

-- $setup
-- >>> :set -XOverloadedStrings
Expand Down Expand Up @@ -74,27 +81,20 @@ data Base = Base16 -- ^ similar to hexadecimal
-- "Zm9vYmFy"
--
convertToBase :: (ByteArrayAccess bin, ByteArray bout) => Base -> bin -> bout
convertToBase base b = case base of
Base16 -> doConvert (binLength * 2) toHexadecimal
Base32 -> let (q,r) = binLength `divMod` 5
outLen = 8 * (if r == 0 then q else q + 1)
in doConvert outLen toBase32
Base64 -> doConvert base64Length toBase64
-- Base64URL -> doConvert base64Length (toBase64URL True)
Base64URLUnpadded -> doConvert base64UnpaddedLength (toBase64URL False)
Base64OpenBSD -> doConvert base64UnpaddedLength toBase64OpenBSD
where
binLength = B.length b

base64Length = let (q,r) = binLength `divMod` 3
in 4 * (if r == 0 then q else q+1)

base64UnpaddedLength = let (q,r) = binLength `divMod` 3
in 4 * q + (if r == 0 then 0 else r+1)
doConvert l f =
B.unsafeCreate l $ \bout ->
B.withByteArray b $ \bin ->
f bout bin binLength
convertToBase base b =
let bs = B.convert b :: BS.ByteString
in case base of
Base16 -> B.convert $ extractBase16 (B16.encodeBase16' bs)
Base32 -> B.convert $ B32.encodeBase32' bs
Base64 -> B.convert $ extractBase64 (B64.encodeBase64' bs)
Base64URLUnpadded -> B.convert $ extractBase64 (B64URL.encodeBase64Unpadded' bs)
Base64OpenBSD ->
let binLength = B.length b
(q, r) = binLength `divMod` 3
outLen = 4 * q + (if r == 0 then 0 else r + 1)
in B.unsafeCreate outLen $ \bout ->
withByteArray b $ \bin ->
toBase64OpenBSD bout bin binLength

-- | Try to decode some bytes from the equivalent representation in a specific 'Base'.
--
Expand All @@ -108,55 +108,27 @@ convertToBase base b = case base of
-- Trying to decode invalid data will return an error string:
--
-- >>> convertFromBase Base64 ("!!!" :: ByteString) :: Either String ByteString
-- Left "base64: input: invalid length"
-- Left "Base64-encoded bytestring requires padding"
--
convertFromBase :: (ByteArrayAccess bin, ByteArray bout) => Base -> bin -> Either String bout
convertFromBase Base16 b
| odd (B.length b) = Left "base16: input: invalid length"
| otherwise = unsafeDoIO $ do
(ret, out) <-
B.allocRet (B.length b `div` 2) $ \bout ->
B.withByteArray b $ \bin ->
fromHexadecimal bout bin (B.length b)
case ret of
Nothing -> return $ Right out
Just ofs -> return $ Left ("base16: input: invalid encoding at offset: " ++ show ofs)
convertFromBase Base32 b = unsafeDoIO $
withByteArray b $ \bin -> do
mDstLen <- unBase32Length bin (B.length b)
case mDstLen of
Nothing -> return $ Left "base32: input: invalid length"
Just dstLen -> do
(ret, out) <- B.allocRet dstLen $ \bout -> fromBase32 bout bin (B.length b)
case ret of
Nothing -> return $ Right out
Just ofs -> return $ Left ("base32: input: invalid encoding at offset: " ++ show ofs)
convertFromBase Base64 b = unsafeDoIO $
withByteArray b $ \bin -> do
mDstLen <- unBase64Length bin (B.length b)
case mDstLen of
Nothing -> return $ Left "base64: input: invalid length"
Just dstLen -> do
(ret, out) <- B.allocRet dstLen $ \bout -> fromBase64 bout bin (B.length b)
case ret of
Nothing -> return $ Right out
Just ofs -> return $ Left ("base64: input: invalid encoding at offset: " ++ show ofs)
convertFromBase Base64URLUnpadded b = unsafeDoIO $
withByteArray b $ \bin ->
case unBase64LengthUnpadded (B.length b) of
Nothing -> return $ Left "base64URL unpadded: input: invalid length"
Just dstLen -> do
(ret, out) <- B.allocRet dstLen $ \bout -> fromBase64URLUnpadded bout bin (B.length b)
case ret of
Nothing -> return $ Right out
Just ofs -> return $ Left ("base64URL unpadded: input: invalid encoding at offset: " ++ show ofs)
convertFromBase Base64OpenBSD b = unsafeDoIO $
withByteArray b $ \bin ->
case unBase64LengthUnpadded (B.length b) of
Nothing -> return $ Left "base64 unpadded: input: invalid length"
Just dstLen -> do
(ret, out) <- B.allocRet dstLen $ \bout -> fromBase64OpenBSD bout bin (B.length b)
case ret of
Nothing -> return $ Right out
Just ofs -> return $ Left ("base64 unpadded: input: invalid encoding at offset: " ++ show ofs)
convertFromBase base b =
let bs = B.convert b :: BS.ByteString
run = fmap B.convert . mapLeft T.unpack
in case base of
Base16 -> run $ B16.decodeBase16Untyped bs
Base32 -> run $ B32.decodeBase32 bs
Base64 -> run $ B64.decodeBase64Untyped bs
Base64URLUnpadded -> run $ B64URL.decodeBase64UnpaddedUntyped bs
Base64OpenBSD -> unsafeDoIO $
withByteArray b $ \bin ->
case unBase64LengthUnpadded (B.length b) of
Nothing -> return $ Left "base64 unpadded: input: invalid length"
Just dstLen -> do
(ret, out) <- B.allocRet dstLen $ \bout -> fromBase64OpenBSD bout bin (B.length b)
return $ case ret of
Nothing -> Right out
Just ofs -> Left ("base64 unpadded: input: invalid encoding at offset: " ++ show ofs)

mapLeft :: (a -> b) -> Either a c -> Either b c
mapLeft f (Left a) = Left (f a)
mapLeft _ (Right c) = Right c
Loading
Loading