{-# OPTIONS_HADDOCK hide, prune #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
module Data.ByteString.Base32.Internal (
enc_tab
, dec_tab
, w5_tab
, encode_with
) where
import qualified Data.Bits as B
import Data.Bits ((.&.), (.|.))
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BI
import Data.Word (Word8)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Ptr (Ptr, plusPtr)
import Foreign.Storable (peekElemOff, pokeElemOff)
fi :: (Num a, Integral b) => b -> a
fi :: forall a b. (Num a, Integral b) => b -> a
fi = b -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE fi #-}
enc_tab :: BS.ByteString
enc_tab :: ByteString
enc_tab = ByteString
"qpzry9x8gf2tvdw0s3jn54khce6mua7l"
{-# NOINLINE enc_tab #-}
dec_tab :: BS.ByteString
dec_tab :: ByteString
dec_tab =
ByteString
"\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x2f\x40\x2a\x31\x35\x34\x3a\x3e\x27\x25\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x3d\x40\x38\x2d\x39\x29\x28\x37\x40\x32\x36\x3f\x3b\x33\x40\
\\x21\x20\x23\x30\x2b\x3c\x2c\x2e\x26\x24\x22\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\
\\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40"
{-# NOINLINE dec_tab #-}
w5_tab :: BS.ByteString
w5_tab :: ByteString
w5_tab = [Word8] -> ByteString
BS.pack [Word8
0 .. Word8
31]
{-# NOINLINE w5_tab #-}
encode_with
:: BS.ByteString
-> BS.ByteString
-> BS.ByteString
encode_with :: ByteString -> ByteString -> ByteString
encode_with (BI.PS ForeignPtr Word8
tfp Int
toff Int
_) (BI.PS ForeignPtr Word8
sfp Int
soff Int
l) =
let !outlen :: Int
outlen = (Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`quot` Int
5
in Int -> (Ptr Word8 -> IO ()) -> ByteString
BI.unsafeCreate Int
outlen ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
dst ->
ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
sfp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
sp0 ->
ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
tfp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
tp0 -> do
let !sp :: Ptr Word8
sp = Ptr Word8
sp0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
soff :: Ptr Word8
!tp :: Ptr Word8
tp = Ptr Word8
tp0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
toff :: Ptr Word8
Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> Int -> Int -> Int -> IO ()
encode_loop Ptr Word8
sp Ptr Word8
tp Ptr Word8
dst Int
l Int
0 Int
0
encode_loop
:: Ptr Word8 -> Ptr Word8 -> Ptr Word8
-> Int -> Int -> Int -> IO ()
encode_loop :: Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> Int -> Int -> Int -> IO ()
encode_loop !Ptr Word8
sp !Ptr Word8
tp !Ptr Word8
dst !Int
len !Int
i !Int
j
| Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
len = do
a <- Ptr Word8 -> Int -> IO Word8
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr Word8
sp Int
i
b <- peekElemOff sp (i + 1)
c <- peekElemOff sp (i + 2)
d <- peekElemOff sp (i + 3)
e <- peekElemOff sp (i + 4)
let !w0 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
3) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w1 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
2 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
6) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w2 = (Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
1) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w3 = (Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
4 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
c Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
4) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w4 = (Word8
c Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
1 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
d Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w5 = (Word8
d Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
2) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w6 = (Word8
d Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
3 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
e Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
5) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w7 = Word8
e Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
peekElemOff tp (fi w0) >>= pokeElemOff dst j
peekElemOff tp (fi w1) >>= pokeElemOff dst (j + 1)
peekElemOff tp (fi w2) >>= pokeElemOff dst (j + 2)
peekElemOff tp (fi w3) >>= pokeElemOff dst (j + 3)
peekElemOff tp (fi w4) >>= pokeElemOff dst (j + 4)
peekElemOff tp (fi w5) >>= pokeElemOff dst (j + 5)
peekElemOff tp (fi w6) >>= pokeElemOff dst (j + 6)
peekElemOff tp (fi w7) >>= pokeElemOff dst (j + 7)
encode_loop sp tp dst len (i + 5) (j + 8)
| Bool
otherwise = Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> Int -> Int -> Int -> IO ()
encode_tail Ptr Word8
sp Ptr Word8
tp Ptr Word8
dst Int
len Int
i Int
j
encode_tail
:: Ptr Word8 -> Ptr Word8 -> Ptr Word8
-> Int -> Int -> Int -> IO ()
encode_tail :: Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> Int -> Int -> Int -> IO ()
encode_tail !Ptr Word8
sp !Ptr Word8
tp !Ptr Word8
dst !Int
len !Int
i !Int
j = case Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i of
Int
0 -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Int
1 -> do
a <- Ptr Word8 -> Int -> IO Word8
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr Word8
sp Int
i
let !w0 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
3) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w1 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
2) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
peekElemOff tp (fi w0) >>= pokeElemOff dst j
peekElemOff tp (fi w1) >>= pokeElemOff dst (j + 1)
Int
2 -> do
a <- Ptr Word8 -> Int -> IO Word8
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr Word8
sp Int
i
b <- peekElemOff sp (i + 1)
let !w0 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
3) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w1 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
2 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
6) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w2 = (Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
1) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w3 = (Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
4) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
peekElemOff tp (fi w0) >>= pokeElemOff dst j
peekElemOff tp (fi w1) >>= pokeElemOff dst (j + 1)
peekElemOff tp (fi w2) >>= pokeElemOff dst (j + 2)
peekElemOff tp (fi w3) >>= pokeElemOff dst (j + 3)
Int
3 -> do
a <- Ptr Word8 -> Int -> IO Word8
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr Word8
sp Int
i
b <- peekElemOff sp (i + 1)
c <- peekElemOff sp (i + 2)
let !w0 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
3) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w1 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
2 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
6) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w2 = (Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
1) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w3 = (Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
4 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
c Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
4) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w4 = (Word8
c Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
1) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
peekElemOff tp (fi w0) >>= pokeElemOff dst j
peekElemOff tp (fi w1) >>= pokeElemOff dst (j + 1)
peekElemOff tp (fi w2) >>= pokeElemOff dst (j + 2)
peekElemOff tp (fi w3) >>= pokeElemOff dst (j + 3)
peekElemOff tp (fi w4) >>= pokeElemOff dst (j + 4)
Int
4 -> do
a <- Ptr Word8 -> Int -> IO Word8
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr Word8
sp Int
i
b <- peekElemOff sp (i + 1)
c <- peekElemOff sp (i + 2)
d <- peekElemOff sp (i + 3)
let !w0 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
3) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w1 = (Word8
a Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
2 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
6) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w2 = (Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
1) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w3 = (Word8
b Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
4 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
c Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
4) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w4 = (Word8
c Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
1 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
d Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
7) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w5 = (Word8
d Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftR` Int
2) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
!w6 = (Word8
d Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`B.shiftL` Int
3) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x1f
peekElemOff tp (fi w0) >>= pokeElemOff dst j
peekElemOff tp (fi w1) >>= pokeElemOff dst (j + 1)
peekElemOff tp (fi w2) >>= pokeElemOff dst (j + 2)
peekElemOff tp (fi w3) >>= pokeElemOff dst (j + 3)
peekElemOff tp (fi w4) >>= pokeElemOff dst (j + 4)
peekElemOff tp (fi w5) >>= pokeElemOff dst (j + 5)
peekElemOff tp (fi w6) >>= pokeElemOff dst (j + 6)
Int
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()