diff --git a/app/Main.hs b/app/Main.hs index 4bf0d29..677d2c6 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -21,6 +21,7 @@ import Encoding.Xx (encxx, decxx) import Encoding.QuotedPrintable (encqp, decqp) import Encoding.UnixToUnix (encuu, decuu) import Encoding.Yenc (ency, decy) +import Encoding.LetterToNumber (encltn, decltn) import Encoding.Rotate (rotate) import Encoding.Solve (solveEnc) @@ -43,6 +44,7 @@ data Based = Decode { uu :: Bool, xx :: Bool, yenc :: Bool, + a1z26 :: Bool, rot :: Maybe Int, solve :: Bool } @@ -64,6 +66,7 @@ data Based = Decode { uu :: Bool, xx :: Bool, yenc :: Bool, + a1z26 :: Bool, rot :: Maybe Int } deriving(Show, Data, Typeable) @@ -102,10 +105,12 @@ optionHandler Encode{uu=True} = encuu optionHandler Decode{uu=True} = decuu optionHandler Decode{xx=True} = decxx optionHandler Encode{xx=True} = encxx -optionHandler Decode{yenc=True} = decy -optionHandler Encode{yenc=True} = ency -optionHandler Encode{rot=Just n} = rotate n +optionHandler Decode{yenc=True} = decy +optionHandler Encode{yenc=True} = ency +optionHandler Decode{a1z26=True} = decltn +optionHandler Encode{a1z26=True} = encltn optionHandler Decode{rot=Just n} = rotate n +optionHandler Encode{rot=Just n} = rotate n optionHandler Decode{solve=True} = solveEnc decodeMode :: Based @@ -127,6 +132,7 @@ decodeMode = Decode { uu = def &= help "decode UnixToUnix", xx = def &= help "decode xx, without padding", yenc = def &= help "decode yEncode", + a1z26 = def &= help "decode letter to number", rot = def &= help "rotate characters by n positions", solve = def &= help "solve encoding" } &= help "Decode chosen base" &=auto @@ -150,6 +156,7 @@ encodeMode = Encode { uu = def &= help "encode UnixToUnix", xx = def &= help "encode xx, without padding", yenc = def &= help "encode yEncode", + a1z26 = def &= help "encode letter to number", rot = def &= help "rotate characters by n positions" } &= help "Encode chosen base" diff --git a/based.cabal b/based.cabal index 248dce0..3e21a98 100644 --- a/based.cabal +++ b/based.cabal @@ -25,6 +25,7 @@ library Encoding.Xx Encoding.Yenc Encoding.Rotate + Encoding.LetterToNumber Encoding.Solve other-modules: -- Data.Bytes.Text.Ascii diff --git a/src/Encoding/LetterToNumber.hs b/src/Encoding/LetterToNumber.hs new file mode 100644 index 0000000..325ec8b --- /dev/null +++ b/src/Encoding/LetterToNumber.hs @@ -0,0 +1,59 @@ +module Encoding.LetterToNumber + ( encltn + , decltn + ) where + +import Data.Char (ord, chr, isAscii, isAlpha, isSpace, isDigit, toUpper, isLatin1) +import Data.List (intercalate) +import Data.ByteString (ByteString) +import qualified Data.ByteString.Char8 as BC + +charToNumber :: Char -> ByteString +charToNumber c + | isAlpha c = BC.pack $ show $ ord (toUpper c) - ord 'A' + 1 + | isSpace c = BC.pack "0" + | otherwise = BC.singleton c + -- | isAlpha c = show $ ord (toUpper c) - ord 'A' + 1 + -- | isSpace c = "0" + -- -- | isLatin1 c = [c] + -- | otherwise = [c] + +numberToChar :: String -> Char +numberToChar s + | s == "0" = ' ' + | all isDigit s = chr $ read s + ord 'A' -1 + | otherwise = head s + -- | otherwise = chr $ read s + ord 'A' - 1 + +encltn :: ByteString -> ByteString +encltn input = go (BC.unpack input) [] + where + go :: String -> [ByteString] -> ByteString + go [] acc = BC.intercalate (BC.pack "-") (reverse acc) + go (x:xs) acc + -- | not (isAscii x) = go xs (BC.singleton x : acc) + | not (isAscii x) = error "Non-ASCII character detected" + | otherwise = go xs (charToNumber x : acc) +-- encltn input = BC.intercalate (BC.pack "-") $ map charToNumber' $ BC.unpack input +-- where +-- charToNumber' c +-- | not (isAscii c)= BC.singleton c +-- | otherwise = charToNumber c + +decltn :: ByteString -> ByteString +decltn input = BC.pack $ concatMap (processSegment . BC.unpack) $ BC.split '-' input + where + processSegment :: String -> String + processSegment s + | all isDigit s = [numberToChar s] + | otherwise = s +-- decltn input = BC.pack $ go $ BC.split '-' input +-- where +-- go :: [ByteString] -> String +-- go [] = [] +-- go (x:xs) = +-- let str = BC.unpack x +-- in numberToChar str ++ go xs + -- in case all isDigit str of + -- True -> numberToChar str : go xs + -- False -> str ++ go xs