diff --git a/app/Main.hs b/app/Main.hs index 27e73af..348b0a7 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -11,7 +11,7 @@ import qualified Data.ByteString as B import qualified Data.ByteString.Char8 as BC --import Text.Regex.TDFA --import Encoding.Base2 (enc2, dec2) ---import Encoding.Base8 (enc8, dec8) +import Encoding.Base8 (enc8, dec8) import Encoding.Base10 (enc10, dec10) import Encoding.Base16 (enc16, dec16) import Encoding.Base32 (enc32, dec32) @@ -41,7 +41,7 @@ data Based = Decode { b32 :: Bool, b16 :: Bool, b10 :: Bool, --- b8 :: Bool, + b8 :: Bool, -- b2 :: Bool, qp :: Bool, uu :: Bool, @@ -62,7 +62,7 @@ data Based = Decode { b32 :: Bool, b16 :: Bool, b10 :: Bool, --- b8 :: Bool, + b8 :: Bool, -- b2 :: Bool, qp :: Bool, uu :: Bool, @@ -173,8 +173,8 @@ optionHandler Decode{b16=True} = dec16 optionHandler Encode{b16=True} = enc16 optionHandler Decode{b10=True} = dec10 optionHandler Encode{b10=True} = enc10 ---optionHandler Decode{b8=True} = dec8 ---optionHandler Encode{b8=True} = enc8 +optionHandler Decode{b8=True} = dec8 +optionHandler Encode{b8=True} = enc8 --optionHandler Decode{b2=True} = dec2 --optionHandler Encode{b2=True} = enc2 optionHandler Decode{qp=True} = decqp @@ -202,7 +202,7 @@ decodeMode = Decode { b32 = def &= help "decode base32", b16 = def &= help "decode base16", b10 = def &= help "decode decimal from hex", --- b8 = def &= help "decode octal from hex", + b8 = def &= help "decode octal from hex", -- b2 = def &= help "decode binary from hex", qp = def &= help "decode quoted-printable", uu = def &= help "decode uu", @@ -225,7 +225,7 @@ encodeMode = Encode { b32 = def &= help "encode base32", b16 = def &= help "encode base16", b10 = def &= help "encode base10 to hex", --- b8 = def &= help "encode octal to hex", + b8 = def &= help "encode octal to hex", -- b2 = def &= help "encode binary to hex", qp = def &= help "encode quoted-printable", uu = def &= help "encode uu", diff --git a/src/Encoding/Base8.hs b/src/Encoding/Base8.hs index 2964e9a..a4faa74 100644 --- a/src/Encoding/Base8.hs +++ b/src/Encoding/Base8.hs @@ -1,30 +1,60 @@ +-- module Encoding.Base8 +-- ( enc8 +-- , dec8 +-- ) where + +-- import Data.Char (ord, chr, intToDigit) + +-- octalToChar :: String -> Char +-- octalToChar octal = chr (read ("0o" ++ octal)) -- Assumes input is in base 8 (octal) + +-- -- Function to split a string into chunks of three characters each +-- chunksOf3 :: String -> [String] +-- chunksOf3 [] = [] +-- chunksOf3 str = Prelude.take 3 str : chunksOf3 (Prelude.drop 3 str) + +-- dec8 :: String -> String +-- dec8 = map octalToChar . words + +-- chunksOf :: Int -> [a] -> [[a]] +-- chunksOf _ [] = [] +-- chunksOf n xs = Prelude.take n xs : chunksOf n (Prelude.drop n xs) + +-- unicodeToOctal :: Char -> [String] +-- unicodeToOctal c = chunksOf 3 $ reverse $ decimalToOctal' (ord c) +-- where +-- decimalToOctal' 0 = "0" +-- decimalToOctal' m = let (q, r) = m `divMod` 8 in intToDigit r : if q == 0 then "" else decimalToOctal' q + +-- enc8 :: String -> String +-- enc8 = unwords . concatMap unicodeToOctal + module Encoding.Base8 ( enc8 , dec8 ) where import Data.Char (ord, chr, intToDigit) +import qualified Data.ByteString.Char8 as BC +import Data.ByteString (ByteString) +import qualified Data.ByteString as B -octalToChar :: String -> Char -octalToChar octal = chr (read ("0o" ++ octal)) -- Assumes input is in base 8 (octal) +octalToChar :: ByteString -> Char +octalToChar octal = chr (read ("0o" ++ BC.unpack octal)) -- Assumes input is in base 8 (octal) --- Function to split a string into chunks of three characters each -chunksOf3 :: String -> [String] -chunksOf3 [] = [] -chunksOf3 str = Prelude.take 3 str : chunksOf3 (Prelude.drop 3 str) +chunksOf :: Int -> ByteString -> [ByteString] +chunksOf n bs + | B.null bs = [] + | otherwise = BC.take n bs : chunksOf n (BC.drop n bs) -dec8 :: String -> String -dec8 = map octalToChar . words +dec8 :: ByteString -> ByteString +dec8 = BC.pack . map octalToChar . BC.words -chunksOf :: Int -> [a] -> [[a]] -chunksOf _ [] = [] -chunksOf n xs = Prelude.take n xs : chunksOf n (Prelude.drop n xs) - -unicodeToOctal :: Char -> [String] -unicodeToOctal c = chunksOf 3 $ reverse $ decimalToOctal' (ord c) +unicodeToOctal :: Char -> [ByteString] +unicodeToOctal c = chunksOf 3 (BC.pack $ reverse $ decimalToOctal' (ord c)) where decimalToOctal' 0 = "0" decimalToOctal' m = let (q, r) = m `divMod` 8 in intToDigit r : if q == 0 then "" else decimalToOctal' q -enc8 :: String -> String -enc8 = unwords . concatMap unicodeToOctal +enc8 :: ByteString -> ByteString +enc8 = BC.unwords . concatMap unicodeToOctal . BC.unpack