{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Rank2Types #-}
module Data.Memory.Encoding.Base32
( toBase32
, unBase32Length
, fromBase32
) where
import Data.Memory.Internal.Compat
import Data.Word
import Basement.Bits
import Basement.IntegralConv
import GHC.Prim
import GHC.Word
import Control.Monad
import Foreign.Storable
import Foreign.Ptr (Ptr)
toBase32 :: Ptr Word8
-> Ptr Word8
-> Int
-> IO ()
toBase32 :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
toBase32 Ptr Word8
dst Ptr Word8
src Int
len = Int -> Int -> IO ()
loop Int
0 Int
0
where
eqChar :: Word8
eqChar :: Word8
eqChar = Word8
0x3d
peekOrZero :: Int -> IO Word8
peekOrZero :: Int -> IO Word8
peekOrZero Int
i
| Int
i forall a. Ord a => a -> a -> Bool
>= Int
len = forall (m :: * -> *) a. Monad m => a -> m a
return Word8
0
| Bool
otherwise = forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src Int
i
pokeOrPadding :: Int
-> Int
-> Word8
-> IO ()
pokeOrPadding :: Int -> Int -> Word8 -> IO ()
pokeOrPadding Int
i Int
di Word8
v
| Int
i forall a. Ord a => a -> a -> Bool
< Int
len = forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst Int
di Word8
v
| Bool
otherwise = forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst Int
di Word8
eqChar
loop :: Int
-> Int
-> IO ()
loop :: Int -> Int -> IO ()
loop Int
i Int
di
| Int
i forall a. Ord a => a -> a -> Bool
>= Int
len = forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do
Word8
i1 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src Int
i
Word8
i2 <- Int -> IO Word8
peekOrZero (Int
i forall a. Num a => a -> a -> a
+ Int
1)
Word8
i3 <- Int -> IO Word8
peekOrZero (Int
i forall a. Num a => a -> a -> a
+ Int
2)
Word8
i4 <- Int -> IO Word8
peekOrZero (Int
i forall a. Num a => a -> a -> a
+ Int
3)
Word8
i5 <- Int -> IO Word8
peekOrZero (Int
i forall a. Num a => a -> a -> a
+ Int
4)
let (Word8
o1,Word8
o2,Word8
o3,Word8
o4,Word8
o5,Word8
o6,Word8
o7,Word8
o8) = (Word8, Word8, Word8, Word8, Word8)
-> (Word8, Word8, Word8, Word8, Word8, Word8, Word8, Word8)
toBase32Per5Bytes (Word8
i1, Word8
i2, Word8
i3, Word8
i4, Word8
i5)
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst Int
di Word8
o1
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst (Int
di forall a. Num a => a -> a -> a
+ Int
1) Word8
o2
Int -> Int -> Word8 -> IO ()
pokeOrPadding (Int
i forall a. Num a => a -> a -> a
+ Int
1) (Int
di forall a. Num a => a -> a -> a
+ Int
2) Word8
o3
Int -> Int -> Word8 -> IO ()
pokeOrPadding (Int
i forall a. Num a => a -> a -> a
+ Int
1) (Int
di forall a. Num a => a -> a -> a
+ Int
3) Word8
o4
Int -> Int -> Word8 -> IO ()
pokeOrPadding (Int
i forall a. Num a => a -> a -> a
+ Int
2) (Int
di forall a. Num a => a -> a -> a
+ Int
4) Word8
o5
Int -> Int -> Word8 -> IO ()
pokeOrPadding (Int
i forall a. Num a => a -> a -> a
+ Int
3) (Int
di forall a. Num a => a -> a -> a
+ Int
5) Word8
o6
Int -> Int -> Word8 -> IO ()
pokeOrPadding (Int
i forall a. Num a => a -> a -> a
+ Int
3) (Int
di forall a. Num a => a -> a -> a
+ Int
6) Word8
o7
Int -> Int -> Word8 -> IO ()
pokeOrPadding (Int
i forall a. Num a => a -> a -> a
+ Int
4) (Int
di forall a. Num a => a -> a -> a
+ Int
7) Word8
o8
Int -> Int -> IO ()
loop (Int
iforall a. Num a => a -> a -> a
+Int
5) (Int
diforall a. Num a => a -> a -> a
+Int
8)
toBase32Per5Bytes :: (Word8, Word8, Word8, Word8, Word8)
-> (Word8, Word8, Word8, Word8, Word8, Word8, Word8, Word8)
toBase32Per5Bytes :: (Word8, Word8, Word8, Word8, Word8)
-> (Word8, Word8, Word8, Word8, Word8, Word8, Word8, Word8)
toBase32Per5Bytes (!Word8
i1, !Word8
i2, !Word8
i3, !Word8
i4, !Word8
i5) =
(Word8 -> Word8
index Word8
o1, Word8 -> Word8
index Word8
o2, Word8 -> Word8
index Word8
o3, Word8 -> Word8
index Word8
o4, Word8 -> Word8
index Word8
o5, Word8 -> Word8
index Word8
o6, Word8 -> Word8
index Word8
o7, Word8 -> Word8
index Word8
o8)
where
!o1 :: Word8
o1 = (Word8
i1 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0xF8) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.>>. CountOf Bool
3
!o2 :: Word8
o2 = ((Word8
i1 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0x07) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.<<. CountOf Bool
2) forall bits. BitOps bits => bits -> bits -> bits
.|. ((Word8
i2 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0xC0) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.>>. CountOf Bool
6)
!o3 :: Word8
o3 = ((Word8
i2 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0x3E) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.>>. CountOf Bool
1)
!o4 :: Word8
o4 = ((Word8
i2 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0x01) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.<<. CountOf Bool
4) forall bits. BitOps bits => bits -> bits -> bits
.|. ((Word8
i3 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0xF0) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.>>. CountOf Bool
4)
!o5 :: Word8
o5 = ( (Word8
i3 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0x0F) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.<<. CountOf Bool
1) forall bits. BitOps bits => bits -> bits -> bits
.|. ((Word8
i4 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0x80) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.>>. CountOf Bool
7)
!o6 :: Word8
o6 = (Word8
i4 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0x7C) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.>>. CountOf Bool
2
!o7 :: Word8
o7 = ((Word8
i4 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0x03) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.<<. CountOf Bool
3) forall bits. BitOps bits => bits -> bits -> bits
.|. ((Word8
i5 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0xE0) forall bits. BitOps bits => bits -> CountOf Bool -> bits
.>>. CountOf Bool
5)
!o8 :: Word8
o8 = Word8
i5 forall bits. BitOps bits => bits -> bits -> bits
.&. Word8
0x1F
!set :: Addr#
set = Addr#
"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"#
index :: Word8 -> Word8
index :: Word8 -> Word8
index Word8
idx = Word8# -> Word8
W8# (Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
set (Word# -> Int#
word2Int# Word#
widx))
where !(W# Word#
widx) = forall a b. IntegralUpsize a b => a -> b
integralUpsize Word8
idx
unBase32Length :: Ptr Word8 -> Int -> IO (Maybe Int)
unBase32Length :: Ptr Word8 -> Int -> IO (Maybe Int)
unBase32Length Ptr Word8
src Int
len
| Int
len forall a. Ord a => a -> a -> Bool
< Int
1 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Int
0
| (Int
len forall a. Integral a => a -> a -> a
`mod` Int
8) forall a. Eq a => a -> a -> Bool
/= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
| Bool
otherwise = do
Word8
last1Byte <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
len forall a. Num a => a -> a -> a
- Int
1)
Word8
last2Byte <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
len forall a. Num a => a -> a -> a
- Int
2)
Word8
last3Byte <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
len forall a. Num a => a -> a -> a
- Int
3)
Word8
last4Byte <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
len forall a. Num a => a -> a -> a
- Int
4)
Word8
last5Byte <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
len forall a. Num a => a -> a -> a
- Int
5)
Word8
last6Byte <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
len forall a. Num a => a -> a -> a
- Int
6)
let dstLen :: Int
dstLen = Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Int
caseByte Word8
last1Byte Word8
last2Byte Word8
last3Byte Word8
last4Byte Word8
last5Byte Word8
last6Byte
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ (Int
len forall a. Integral a => a -> a -> a
`div` Int
8) forall a. Num a => a -> a -> a
* Int
5 forall a. Num a => a -> a -> a
- Int
dstLen
where
caseByte :: Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Int
caseByte :: Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Int
caseByte Word8
last1 Word8
last2 Word8
last3 Word8
last4 Word8
last5 Word8
last6
| Word8
last6 forall a. Eq a => a -> a -> Bool
== Word8
eqAscii = Int
4
| Word8
last5 forall a. Eq a => a -> a -> Bool
== Word8
eqAscii = Int
3
| Word8
last4 forall a. Eq a => a -> a -> Bool
== Word8
eqAscii = Int
3
| Word8
last3 forall a. Eq a => a -> a -> Bool
== Word8
eqAscii = Int
2
| Word8
last2 forall a. Eq a => a -> a -> Bool
== Word8
eqAscii = Int
1
| Word8
last1 forall a. Eq a => a -> a -> Bool
== Word8
eqAscii = Int
1
| Bool
otherwise = Int
0
eqAscii :: Word8
eqAscii :: Word8
eqAscii = Word8
0x3D
fromBase32 :: Ptr Word8 -> Ptr Word8 -> Int -> IO (Maybe Int)
fromBase32 :: Ptr Word8 -> Ptr Word8 -> Int -> IO (Maybe Int)
fromBase32 Ptr Word8
dst Ptr Word8
src Int
len
| Int
len forall a. Eq a => a -> a -> Bool
== Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
| Bool
otherwise = Int -> Int -> IO (Maybe Int)
loop Int
0 Int
0
where
loop :: Int
-> Int
-> IO (Maybe Int)
loop :: Int -> Int -> IO (Maybe Int)
loop Int
di Int
i
| Int
i forall a. Eq a => a -> a -> Bool
== (Int
len forall a. Num a => a -> a -> a
- Int
8) = do
Word8
i1 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src Int
i
Word8
i2 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
1)
Word8
i3 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
2)
Word8
i4 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
3)
Word8
i5 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
4)
Word8
i6 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
5)
Word8
i7 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
6)
Word8
i8 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
7)
let (Int
nbBytes, Word8
i3', Word8
i4', Word8
i5', Word8
i6', Word8
i7', Word8
i8') =
case (Word8
i3, Word8
i4, Word8
i5, Word8
i6, Word8
i7, Word8
i8) of
(Word8
0x3D, Word8
0x3D, Word8
0x3D, Word8
0x3D, Word8
0x3D, Word8
0x3D) -> (Int
6, Word8
0x41, Word8
0x41, Word8
0x41, Word8
0x41, Word8
0x41, Word8
0x41)
(Word8
0x3D, Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ ) -> (Int
0, Word8
i3, Word8
i4, Word8
i5, Word8
i6, Word8
i7, Word8
i8)
(Word8
_ , Word8
0x3D, Word8
0x3D, Word8
0x3D, Word8
0x3D, Word8
0x3D) -> (Int
5, Word8
i3 , Word8
0x41, Word8
0x41, Word8
0x41, Word8
0x41, Word8
0x41)
(Word8
_ , Word8
0x3D, Word8
_ , Word8
_ , Word8
_ , Word8
_ ) -> (Int
0, Word8
i3, Word8
i4, Word8
i5, Word8
i6, Word8
i7, Word8
i8)
(Word8
_ , Word8
_ , Word8
0x3D, Word8
0x3D, Word8
0x3D, Word8
0x3D) -> (Int
4, Word8
i3 , Word8
i4 , Word8
0x41, Word8
0x41, Word8
0x41, Word8
0x41)
(Word8
_ , Word8
_ , Word8
0x3D, Word8
_ , Word8
_ , Word8
_ ) -> (Int
0, Word8
i3, Word8
i4, Word8
i5, Word8
i6, Word8
i7, Word8
i8)
(Word8
_ , Word8
_ , Word8
_ , Word8
0x3D, Word8
0x3D, Word8
0x3D) -> (Int
3, Word8
i3 , Word8
i4 , Word8
i5 , Word8
0x41, Word8
0x41, Word8
0x41)
(Word8
_ , Word8
_ , Word8
_ , Word8
0x3D, Word8
_ , Word8
_ ) -> (Int
0, Word8
i3, Word8
i4, Word8
i5, Word8
i6, Word8
i7, Word8
i8)
(Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
0x3D, Word8
0x3D) -> (Int
2, Word8
i3 , Word8
i4 , Word8
i5 , Word8
i6 , Word8
0x41, Word8
0x41)
(Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
0x3D, Word8
_ ) -> (Int
0, Word8
i3, Word8
i4, Word8
i5, Word8
i6, Word8
i7, Word8
i8)
(Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
0x3D) -> (Int
1, Word8
i3 , Word8
i4 , Word8
i5 , Word8
i6 , Word8
i7 , Word8
0x41)
(Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ ) -> (Int
0 :: Int, Word8
i3, Word8
i4, Word8
i5, Word8
i6, Word8
i7, Word8
i8)
case (Word8, Word8, Word8, Word8, Word8, Word8, Word8, Word8)
-> Either Int (Word8, Word8, Word8, Word8, Word8)
fromBase32Per8Bytes (Word8
i1, Word8
i2, Word8
i3', Word8
i4', Word8
i5', Word8
i6', Word8
i7', Word8
i8') of
Left Int
ofs -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (Int
i forall a. Num a => a -> a -> a
+ Int
ofs)
Right (Word8
o1, Word8
o2, Word8
o3, Word8
o4, Word8
o5) -> do
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst Int
di Word8
o1
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst (Int
diforall a. Num a => a -> a -> a
+Int
1) Word8
o2
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
nbBytes forall a. Ord a => a -> a -> Bool
< Int
5) forall a b. (a -> b) -> a -> b
$ forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst (Int
diforall a. Num a => a -> a -> a
+Int
2) Word8
o3
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
nbBytes forall a. Ord a => a -> a -> Bool
< Int
4) forall a b. (a -> b) -> a -> b
$ forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst (Int
diforall a. Num a => a -> a -> a
+Int
3) Word8
o4
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
nbBytes forall a. Ord a => a -> a -> Bool
< Int
2) forall a b. (a -> b) -> a -> b
$ forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst (Int
diforall a. Num a => a -> a -> a
+Int
4) Word8
o5
forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
| Bool
otherwise = do
Word8
i1 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src Int
i
Word8
i2 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
1)
Word8
i3 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
2)
Word8
i4 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
3)
Word8
i5 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
4)
Word8
i6 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
5)
Word8
i7 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
6)
Word8
i8 <- forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
src (Int
i forall a. Num a => a -> a -> a
+ Int
7)
case (Word8, Word8, Word8, Word8, Word8, Word8, Word8, Word8)
-> Either Int (Word8, Word8, Word8, Word8, Word8)
fromBase32Per8Bytes (Word8
i1, Word8
i2, Word8
i3, Word8
i4, Word8
i5, Word8
i6, Word8
i7, Word8
i8) of
Left Int
ofs -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (Int
i forall a. Num a => a -> a -> a
+ Int
ofs)
Right (Word8
o1, Word8
o2, Word8
o3, Word8
o4, Word8
o5) -> do
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst Int
di Word8
o1
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst (Int
diforall a. Num a => a -> a -> a
+Int
1) Word8
o2
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst (Int
diforall a. Num a => a -> a -> a
+Int
2) Word8
o3
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst (Int
diforall a. Num a => a -> a -> a
+Int
3) Word8
o4
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
dst (Int
diforall a. Num a => a -> a -> a
+Int
4) Word8
o5
Int -> Int -> IO (Maybe Int)
loop (Int
diforall a. Num a => a -> a -> a
+Int
5) (Int
iforall a. Num a => a -> a -> a
+Int
8)
fromBase32Per8Bytes :: (Word8, Word8, Word8, Word8, Word8, Word8, Word8, Word8)
-> Either Int (Word8, Word8, Word8, Word8, Word8)
fromBase32Per8Bytes :: (Word8, Word8, Word8, Word8, Word8, Word8, Word8, Word8)
-> Either Int (Word8, Word8, Word8, Word8, Word8)
fromBase32Per8Bytes (Word8
i1, Word8
i2, Word8
i3, Word8
i4, Word8
i5, Word8
i6, Word8
i7, Word8
i8) =
case (Word8 -> Word8
rset Word8
i1, Word8 -> Word8
rset Word8
i2, Word8 -> Word8
rset Word8
i3, Word8 -> Word8
rset Word8
i4, Word8 -> Word8
rset Word8
i5, Word8 -> Word8
rset Word8
i6, Word8 -> Word8
rset Word8
i7, Word8 -> Word8
rset Word8
i8) of
(Word8
0xFF, Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ ) -> forall a b. a -> Either a b
Left Int
0
(Word8
_ , Word8
0xFF, Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ ) -> forall a b. a -> Either a b
Left Int
1
(Word8
_ , Word8
_ , Word8
0xFF, Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ ) -> forall a b. a -> Either a b
Left Int
2
(Word8
_ , Word8
_ , Word8
_ , Word8
0xFF, Word8
_ , Word8
_ , Word8
_ , Word8
_ ) -> forall a b. a -> Either a b
Left Int
3
(Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
0xFF, Word8
_ , Word8
_ , Word8
_ ) -> forall a b. a -> Either a b
Left Int
4
(Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
0xFF, Word8
_ , Word8
_ ) -> forall a b. a -> Either a b
Left Int
5
(Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
0xFF, Word8
_ ) -> forall a b. a -> Either a b
Left Int
6
(Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
_ , Word8
0xFF) -> forall a b. a -> Either a b
Left Int
7
(Word8
ri1 , Word8
ri2 , Word8
ri3 , Word8
ri4 , Word8
ri5 , Word8
ri6 , Word8
ri7 , Word8
ri8 ) ->
let o1 :: Word8
o1 = (Word8
ri1 forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
3) forall bits. BitOps bits => bits -> bits -> bits
.|. (Word8
ri2 forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
2)
o2 :: Word8
o2 = (Word8
ri2 forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
6) forall bits. BitOps bits => bits -> bits -> bits
.|. (Word8
ri3 forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
1) forall bits. BitOps bits => bits -> bits -> bits
.|. (Word8
ri4 forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
4)
o3 :: Word8
o3 = (Word8
ri4 forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
4) forall bits. BitOps bits => bits -> bits -> bits
.|. (Word8
ri5 forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
1)
o4 :: Word8
o4 = (Word8
ri5 forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
7) forall bits. BitOps bits => bits -> bits -> bits
.|. (Word8
ri6 forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
2) forall bits. BitOps bits => bits -> bits -> bits
.|. (Word8
ri7 forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
3)
o5 :: Word8
o5 = (Word8
ri7 forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
5) forall bits. BitOps bits => bits -> bits -> bits
.|. Word8
ri8
in forall a b. b -> Either a b
Right (Word8
o1, Word8
o2, Word8
o3, Word8
o4, Word8
o5)
where
rset :: Word8 -> Word8
rset :: Word8 -> Word8
rset Word8
w = Word8# -> Word8
W8# (Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
rsetTable (Word# -> Int#
word2Int# Word#
widx))
where !(W# Word#
widx) = forall a b. IntegralUpsize a b => a -> b
integralUpsize Word8
w
!rsetTable :: Addr#
rsetTable = Addr#
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\x1A\x1B\x1C\x1D\x1E\x1F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\
\\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\
\\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"#