module Rattletrap.Utility.Bytes
  ( encodeLatin1
  , padBytes
  , reverseBytes
  )
where

import qualified Data.Bits as Bits
import qualified Data.ByteString as Bytes
import qualified Data.ByteString.Char8 as Bytes8
import qualified Data.Text as Text
import qualified Data.Word as Word

encodeLatin1 :: Text.Text -> Bytes.ByteString
encodeLatin1 :: Text -> ByteString
encodeLatin1 Text
text = String -> ByteString
Bytes8.pack (Text -> String
Text.unpack Text
text)

padBytes :: Integral a => a -> Bytes.ByteString -> Bytes.ByteString
padBytes :: a -> ByteString -> ByteString
padBytes a
size ByteString
bytes =
  ByteString
bytes ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Int -> Word8 -> ByteString
Bytes.replicate (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
size Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
Bytes.length ByteString
bytes) Word8
0x00

reverseByte :: Word.Word8 -> Word.Word8
reverseByte :: Word8 -> Word8
reverseByte Word8
byte =
  Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
Bits.shiftR (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
Bits..&. Int -> Word8
forall a. Bits a => Int -> a
Bits.bit Int
7) Int
7
    Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
Bits.shiftR (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
Bits..&. Int -> Word8
forall a. Bits a => Int -> a
Bits.bit Int
6) Int
5
    Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
Bits.shiftR (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
Bits..&. Int -> Word8
forall a. Bits a => Int -> a
Bits.bit Int
5) Int
3
    Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
Bits.shiftR (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
Bits..&. Int -> Word8
forall a. Bits a => Int -> a
Bits.bit Int
4) Int
1
    Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
Bits.shiftL (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
Bits..&. Int -> Word8
forall a. Bits a => Int -> a
Bits.bit Int
3) Int
1
    Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
Bits.shiftL (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
Bits..&. Int -> Word8
forall a. Bits a => Int -> a
Bits.bit Int
2) Int
3
    Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
Bits.shiftL (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
Bits..&. Int -> Word8
forall a. Bits a => Int -> a
Bits.bit Int
1) Int
5
    Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
Bits.shiftL (Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
Bits..&. Int -> Word8
forall a. Bits a => Int -> a
Bits.bit Int
0) Int
7

reverseBytes :: Bytes.ByteString -> Bytes.ByteString
reverseBytes :: ByteString -> ByteString
reverseBytes = (Word8 -> Word8) -> ByteString -> ByteString
Bytes.map Word8 -> Word8
reverseByte