reintroduced solve function

This commit is contained in:
Stefan Friese 2024-06-09 22:48:37 +02:00
parent a5f9862df1
commit 28b583d6b8
1 changed files with 41 additions and 6 deletions

View File

@ -9,7 +9,7 @@ import System.Console.CmdArgs
--import qualified Data.ByteString.Char8 as C --import qualified Data.ByteString.Char8 as C
import qualified Data.ByteString as B import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC import qualified Data.ByteString.Char8 as BC
--import Text.Regex.TDFA import Text.Regex.TDFA
import Encoding.Base2 (enc2, dec2) import Encoding.Base2 (enc2, dec2)
import Encoding.Base8 (enc8, dec8) import Encoding.Base8 (enc8, dec8)
import Encoding.Base10 (enc10, dec10) import Encoding.Base10 (enc10, dec10)
@ -47,8 +47,8 @@ data Based = Decode {
uu :: Bool, uu :: Bool,
xx :: Bool, xx :: Bool,
yenc :: Bool, yenc :: Bool,
rot :: Maybe Int rot :: Maybe Int,
-- solve :: Bool solve :: Bool
} }
| Encode { | Encode {
b91 :: Bool, b91 :: Bool,
@ -137,6 +137,41 @@ data Based = Decode {
-- then "Not able to solve the encoding.\n" -- then "Not able to solve the encoding.\n"
-- else unlines results -- 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 =~ base64Regex = dec64 input
-- -- | input =~ base32Regex = dec32 input -- -- | input =~ base32Regex = dec32 input
@ -187,7 +222,7 @@ optionHandler Decode{yenc=True} = decy
optionHandler Encode{yenc=True} = ency optionHandler Encode{yenc=True} = ency
optionHandler Encode{rot=Just n} = rotate n optionHandler Encode{rot=Just n} = rotate n
optionHandler Decode{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 :: Based
decodeMode = Decode { decodeMode = Decode {
@ -208,8 +243,8 @@ decodeMode = Decode {
uu = def &= help "decode UnixToUnix", uu = def &= help "decode UnixToUnix",
xx = def &= help "decode xx", xx = def &= help "decode xx",
yenc = def &= help "decode yEncode", yenc = def &= help "decode yEncode",
rot = def &= help "rotate characters by n positions" rot = def &= help "rotate characters by n positions",
-- solve = def &= help "solve encoding" solve = def &= help "solve encoding"
} &= help "Decode chosen base" &=auto } &= help "Decode chosen base" &=auto
encodeMode :: Based encodeMode :: Based