From 6ab4cbaa79520fc1565d1142a4128765739f023c Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Sun, 9 Jun 2024 15:32:39 +0200 Subject: [PATCH] moved all encodings to bytestring in- and output --- src/Encoding/Base2.hs | 19 +++++++++++++++++-- src/Encoding/Base45.hs | 8 +++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Encoding/Base2.hs b/src/Encoding/Base2.hs index e44e4d3..2365ee9 100644 --- a/src/Encoding/Base2.hs +++ b/src/Encoding/Base2.hs @@ -37,6 +37,8 @@ import Data.Char (ord, chr, digitToInt, intToDigit) import qualified Data.ByteString.Char8 as BC import Data.ByteString (ByteString) import qualified Data.ByteString as B +import Data.Bits ((.&.), shiftR) +import Data.List (unfoldr) binaryToChar :: ByteString -> Char binaryToChar binStr = chr $ binaryToInt (BC.unpack binStr) @@ -47,10 +49,20 @@ binaryToInt binStr = foldl (\acc x -> acc * 2 + digitToInt x) 0 binStr dec2 :: ByteString -> ByteString dec2 input = BC.pack . map binaryToChar . BC.words $ input +-- charToBinary :: Char -> ByteString +-- charToBinary char = BC.pack $ replicate (7 - length binaryStr) '0' ++ binaryStr +-- where +-- binaryStr = intToBinary (ord char) charToBinary :: Char -> ByteString -charToBinary char = BC.pack $ replicate (7 - length binaryStr) '0' ++ binaryStr +charToBinary char = BC.pack $ replicate (8 - length binaryStr) '0' ++ binaryStr where - binaryStr = intToBinary (ord char) + codePoint = ord char + binaryStr = padToLength 8 (intToBinary' codePoint) + +padToLength :: Int -> String -> String +padToLength len str + | length str >= len = take len str + | otherwise = replicate (len - length str) '0' ++ str intToBinary :: Int -> String intToBinary n = reverse $ decimalToBinary' n @@ -58,5 +70,8 @@ intToBinary n = reverse $ decimalToBinary' n decimalToBinary' 0 = "0" decimalToBinary' m = let (q, r) = m `divMod` 2 in intToDigit r : decimalToBinary' q +intToBinary' :: Int -> String +intToBinary' n = reverse $ take 8 $ unfoldr (\x -> if x == 0 then Nothing else Just (intToDigit $ x .&. 1, x `shiftR` 1)) n + enc2 :: ByteString -> ByteString enc2 input = BC.unwords . map charToBinary . BC.unpack $ input diff --git a/src/Encoding/Base45.hs b/src/Encoding/Base45.hs index 91d9e4b..71970db 100644 --- a/src/Encoding/Base45.hs +++ b/src/Encoding/Base45.hs @@ -142,7 +142,7 @@ encodeChunk [x1] = BC.pack $ map (BC.index base45Alphabet) [b, a] encodeChunk _ = error "Invalid chunk length" dec45 :: ByteString -> ByteString -dec45 encoded = B.pack $ map fromIntegral decodedBytes +dec45 encoded = B.pack decodedBytes where numericValues = map charToNumeric (BC.unpack encoded) groups = groupIntoThrees numericValues @@ -162,8 +162,10 @@ toBase45 :: [Int] -> Int toBase45 [c, d, e] = c + d * 45 + e * (45 * 45) toBase45 _ = error "Invalid Base45 group" -fromBase45 :: Int -> [Int] -fromBase45 n = unfoldr (\x -> if x == 0 then Nothing else Just (x `mod` 256, x `div` 256)) n +-- fromBase45 :: Int -> [Int] +-- fromBase45 n = unfoldr (\x -> if x == 0 then Nothing else Just (x `mod` 256, x `div` 256)) n +fromBase45 :: Int -> [Word8] +fromBase45 n = reverse $ unfoldr (\x -> if x == 0 then Nothing else Just (fromIntegral $ x `mod` 256, x `div` 256)) n bytesToChars :: [Int] -> [Char] bytesToChars = map chr