added solveEnc function, so that you do not have to know which encoding has been used to decode it
This commit is contained in:
parent
6800552f65
commit
f21e343ef3
48
Main.hs
48
Main.hs
|
@ -33,7 +33,10 @@ import qualified Data.Bytes.Text.Ascii as ASCII
|
|||
import Data.Bytes.Get (getWord64host)
|
||||
import Data.ByteString.UTF8 as BSU -- from utf8-string
|
||||
-- import Data.ByteString (singleton)
|
||||
import GHC.Word (Word8)
|
||||
import qualified Data.ByteString.Char8 as C
|
||||
-- Regex imports
|
||||
import Text.Regex.TDFA
|
||||
|
||||
data Based = Decode {
|
||||
b91 :: Bool,
|
||||
|
@ -51,7 +54,8 @@ data Based = Decode {
|
|||
qp :: Bool,
|
||||
uu :: Bool,
|
||||
xx :: Bool,
|
||||
yenc :: Bool
|
||||
yenc :: Bool,
|
||||
solve :: Bool
|
||||
}
|
||||
| Encode {
|
||||
b91 :: Bool,
|
||||
|
@ -116,7 +120,7 @@ octToInt (x : xs) = x + 8 * octToInt xs
|
|||
|
||||
dec91 :: String -> String
|
||||
dec91 = C.unpack . B91.decode
|
||||
-- dec91 input =
|
||||
|
||||
-- case singleton . B91.decode input of
|
||||
-- Right decoded -> C.unpack decoded
|
||||
-- Left _ -> "Error decoding Base91.\n"
|
||||
|
@ -252,6 +256,42 @@ decy input =
|
|||
ency :: String -> String
|
||||
ency = C.unpack . Y.encode . BSU.fromString
|
||||
|
||||
base91Regex = "^[!-~]*$"
|
||||
-- base85Regex = "^[A-Za-u0-9!\"#$%&((*+,-./;:<=@[]\\`]*$"
|
||||
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]*$"
|
||||
|
||||
solveEnc :: String -> String
|
||||
solveEnc input =
|
||||
-- let isBase91 = BSU.fromString input =~ base91Regex :: Bool
|
||||
let isBase64 = BSU.fromString input =~ base64Regex :: Bool
|
||||
isBase58 = BSU.fromString input =~ base58Regex :: Bool
|
||||
isBase32 = BSU.fromString input =~ base32Regex :: Bool
|
||||
isBase16 = BSU.fromString input =~ base16Regex :: Bool
|
||||
base64Result = if isBase64 then "\nTrying base64:\n" ++ dec64 input else ""
|
||||
base58Result = if isBase58 then "\nTrying base58:\n" ++ dec58 input else ""
|
||||
base32Result = if isBase64 then "\nTrying base32:\n" ++ dec32 input else ""
|
||||
base16Result = if isBase16 then "\nTrying base16:\n" ++ dec16 input else ""
|
||||
results = filter (not . null) [base64Result, base32Result, base16Result]
|
||||
in
|
||||
if null results
|
||||
then "Not able to solve the encoding.\n"
|
||||
else unlines results
|
||||
|
||||
--
|
||||
-- | input =~ base64Regex = dec64 input
|
||||
-- | input =~ base32Regex = dec32 input
|
||||
-- | otherwise = "Cannot decode: " ++ input
|
||||
--
|
||||
-- if BSU.fromString input =~ base64Regex :: Bool
|
||||
-- then dec64 input
|
||||
-- else "Not Base64.\n"
|
||||
-- ++ if BSU.fromString input =~ base32Regex :: Bool
|
||||
-- then dec32 input
|
||||
-- else "Not able to solve the encoding.\n"
|
||||
|
||||
optionHandler Decode{b91=True} = dec91
|
||||
optionHandler Encode{b91=True} = enc91
|
||||
optionHandler Decode{b85=True} = dec85
|
||||
|
@ -284,6 +324,7 @@ optionHandler Decode{xx=True} = decxx
|
|||
optionHandler Encode{xx=True} = encxx
|
||||
optionHandler Decode{yenc=True} = decy
|
||||
optionHandler Encode{yenc=True} = ency
|
||||
optionHandler Decode{solve=True} = solveEnc
|
||||
|
||||
decodeMode :: Based
|
||||
decodeMode = Decode {
|
||||
|
@ -302,7 +343,8 @@ decodeMode = Decode {
|
|||
qp = def &= help "decode quoted-printable",
|
||||
uu = def &= help "decode uu",
|
||||
xx = def &= help "decode xx",
|
||||
yenc = def &= help "decode yEncode"
|
||||
yenc = def &= help "decode yEncode",
|
||||
solve = def &= help "solve encoding"
|
||||
} &= help "Decode chosen base" &=auto
|
||||
|
||||
encodeMode :: Based
|
||||
|
|
Loading…
Reference in New Issue