{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
-- |
-- Module       : Data.ByteString.Base16.Internal.W16.Loop
-- Copyright 	: (c) 2020 Emily Pillmore
-- License	: BSD-style
--
-- Maintainer	: Emily Pillmore <emilypi@cohomolo.gy>
-- Stability	: Experimental
-- Portability	: portable
--
-- Encoding loop optimized for 'Word16' architectures
--
module Data.ByteString.Base16.Internal.W16.Loop
( innerLoop
, decodeLoop
) where


import Data.Bits
import Data.ByteString.Internal
import Data.ByteString.Base16.Internal.Utils
import Data.Text (Text)
import qualified Data.Text as T

import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Storable

import GHC.Word


-- | Hex encoding inner loop optimized for 16-bit architectures
--
innerLoop
    :: Ptr Word16
    -> Ptr Word8
    -> Ptr Word8
    -> IO ()
innerLoop :: Ptr Word16 -> Ptr Word8 -> Ptr Word8 -> IO ()
innerLoop !Ptr Word16
dptr !Ptr Word8
sptr !Ptr Word8
end = Ptr Word16 -> Ptr Word8 -> IO ()
forall b.
(Storable b, Num b, Bits b) =>
Ptr b -> Ptr Word8 -> IO ()
go Ptr Word16
dptr Ptr Word8
sptr
  where
    lix :: a -> Word8
lix !a
a = Word8 -> Addr# -> Word8
aix (a -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
a Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0x0f) Addr#
alphabet
    {-# INLINE lix #-}

    !alphabet :: Addr#
alphabet = "0123456789abcdef"#

    go :: Ptr b -> Ptr Word8 -> IO ()
go !Ptr b
dst !Ptr Word8
src
      | Ptr Word8
src Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      | Bool
otherwise = do
        !Word8
t <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
src

        let !a :: b
a = Word8 -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8
forall a. Integral a => a -> Word8
lix (Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
unsafeShiftR Word8
t 4))
            !b :: b
b = Word8 -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8
forall a. Integral a => a -> Word8
lix Word8
t)

        let !w :: b
w = b
a b -> b -> b
forall a. Bits a => a -> a -> a
.|. (b -> Int -> b
forall a. Bits a => a -> Int -> a
unsafeShiftL b
b 8)

        Ptr b -> b -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr b
dst b
w

        Ptr b -> Ptr Word8 -> IO ()
go (Ptr b -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr b
dst 2) (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
src 1)
{-# INLINE innerLoop #-}

-- | Hex decoding loop optimized for 16-bit architectures
--
decodeLoop
  :: ForeignPtr Word8
  -> Ptr Word8
  -> Ptr Word8
  -> Ptr Word8
  -> Ptr Word8
  -> Ptr Word8
  -> IO (Either Text ByteString)
decodeLoop :: ForeignPtr Word8
-> Ptr Word8
-> Ptr Word8
-> Ptr Word8
-> Ptr Word8
-> Ptr Word8
-> IO (Either Text ByteString)
decodeLoop !ForeignPtr Word8
dfp !Ptr Word8
hi !Ptr Word8
lo !Ptr Word8
dptr !Ptr Word8
sptr !Ptr Word8
end = Ptr Word8 -> Ptr Word8 -> Int -> IO (Either Text ByteString)
forall b.
(Storable b, Num b, Bits b) =>
Ptr b -> Ptr Word8 -> Int -> IO (Either Text ByteString)
go Ptr Word8
dptr Ptr Word8
sptr 0
  where
    go :: Ptr b -> Ptr Word8 -> Int -> IO (Either Text ByteString)
go !Ptr b
dst !Ptr Word8
src !Int
n
      | Ptr Word8
src Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = Either Text ByteString -> IO (Either Text ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Either Text ByteString
forall a b. b -> Either a b
Right (ForeignPtr Word8 -> Int -> Int -> ByteString
PS ForeignPtr Word8
dfp 0 Int
n))
      | Bool
otherwise = do
        !Word8
x <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek @Word8 Ptr Word8
src
        !Word8
y <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek @Word8 (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
src 1)

        !b
a <- Ptr Word8 -> Int -> IO b
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
hi (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
x)
        !b
b <- Ptr Word8 -> Int -> IO b
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
lo (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
y)

        if b
a b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff Bool -> Bool -> Bool
|| b
b b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff
        then Either Text ByteString -> IO (Either Text ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either Text ByteString -> IO (Either Text ByteString))
-> (String -> Either Text ByteString)
-> String
-> IO (Either Text ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text ByteString
forall a b. a -> Either a b
Left (Text -> Either Text ByteString)
-> (String -> Text) -> String -> Either Text ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
          (String -> IO (Either Text ByteString))
-> String -> IO (Either Text ByteString)
forall a b. (a -> b) -> a -> b
$ "invalid character at offset: "
          String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Ptr Word8
src Ptr Word8 -> Ptr Word8 -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
sptr)
        else do
          Ptr b -> b -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr b
dst (b
a b -> b -> b
forall a. Bits a => a -> a -> a
.|. b
b)
          Ptr b -> Ptr Word8 -> Int -> IO (Either Text ByteString)
go (Ptr b -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr b
dst 1) (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
src 2) (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1)