From 28b583d6b8ddca2f8ef550ef95096d5fbed49454 Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Sun, 9 Jun 2024 22:48:37 +0200 Subject: [PATCH] reintroduced solve function --- app/Main.hs | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 6ee2a04..740f357 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -9,7 +9,7 @@ import System.Console.CmdArgs --import qualified Data.ByteString.Char8 as C import qualified Data.ByteString as B import qualified Data.ByteString.Char8 as BC ---import Text.Regex.TDFA +import Text.Regex.TDFA import Encoding.Base2 (enc2, dec2) import Encoding.Base8 (enc8, dec8) import Encoding.Base10 (enc10, dec10) @@ -47,8 +47,8 @@ data Based = Decode { uu :: Bool, xx :: Bool, yenc :: Bool, - rot :: Maybe Int --- solve :: Bool + rot :: Maybe Int, + solve :: Bool } | Encode { b91 :: Bool, @@ -137,6 +137,41 @@ data Based = Decode { -- then "Not able to solve the encoding.\n" -- else unlines results +base91Regex = "^[!-~]*$" +base85Regex = "^[0-9A-Za-z!#$%&()*+,-;<=>?@^_`{|}~]+$" +base64Regex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$" +base58Regex = "^[1-9A-HJ-NP-Za-km-z]+$" -- incorrect +base32Regex = "^(?:[A-Z2-7]{8})*(?:[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}=)?$" +base16Regex = "^[0-9A-FXx]*$" +base10Regex = "^[0-9]*$" +base8Regex = "^[0-7]*$" +base2Regex = "^[01]*$" +urlRegex = "^[a-zA-Z0-9%]*$" + +solveEnc :: BC.ByteString -> BC.ByteString +solveEnc input = + let isBase91 = input =~ base91Regex :: Bool + isBase85 = input =~ base85Regex :: Bool + isBase64 = input =~ base64Regex :: Bool + isBase58 = input =~ base58Regex :: Bool + isBase32 = input =~ base32Regex :: Bool + isBase16 = input =~ base16Regex :: Bool + isBase10 = input =~ base10Regex :: Bool + isBase8 = input =~ base8Regex :: Bool + isBase2 = input =~ base2Regex :: Bool + isURL = input =~ urlRegex :: Bool + base91Result = if isBase91 then BC.pack "\nTrying base91:\n" `BC.append` dec91 input else BC.empty + base85Result = if isBase85 then BC.pack "\nTrying base85:\n" `BC.append` dec85 input else BC.empty + base64Result = if isBase64 then BC.pack "\nTrying base64:\n" `BC.append` dec64 input else BC.empty + base58Result = if isBase58 then BC.pack "\nTrying base58:\n" `BC.append` dec58 input else BC.empty + base32Result = if isBase64 then BC.pack "\nTrying base32:\n" `BC.append` dec32 input else BC.empty + base16Result = if isBase16 then BC.pack "\nTrying base16:\n" `BC.append` dec16 input else BC.empty + base10Result = if isBase10 then BC.pack "\nTrying base10:\n" `BC.append` dec10 input else BC.empty + base2Result = if isBase2 then BC.pack "\nTrying base2:\n" `BC.append` dec2 input else BC.empty + base8Result = if isBase8 then BC.pack "\nTrying base8:\n" `BC.append` dec8 input else BC.empty + urlResult = if isURL then BC.pack "\nTrying URL decode:\n" `BC.append` decurl input else BC.empty + in BC.concat [base91Result, base85Result, base64Result, base58Result, base32Result, base16Result, base10Result, base8Result, base2Result, urlResult] + -- -- -- -- | input =~ base64Regex = dec64 input -- -- | input =~ base32Regex = dec32 input @@ -187,7 +222,7 @@ optionHandler Decode{yenc=True} = decy optionHandler Encode{yenc=True} = ency optionHandler Encode{rot=Just n} = rotate n optionHandler Decode{rot=Just n} = rotate n ---optionHandler Decode{solve=True} = solveEnc +optionHandler Decode{solve=True} = solveEnc decodeMode :: Based decodeMode = Decode { @@ -208,8 +243,8 @@ decodeMode = Decode { uu = def &= help "decode UnixToUnix", xx = def &= help "decode xx", yenc = def &= help "decode yEncode", - rot = def &= help "rotate characters by n positions" --- solve = def &= help "solve encoding" + rot = def &= help "rotate characters by n positions", + solve = def &= help "solve encoding" } &= help "Decode chosen base" &=auto encodeMode :: Based