197 lines
7.9 KiB
Haskell
197 lines
7.9 KiB
Haskell
#!/usr/bin/env runhaskell
|
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
|
|
module Main where
|
|
import System.Console.CmdArgs
|
|
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.Base10 (enc10, dec10)
|
|
import Encoding.Base16 (enc16, dec16)
|
|
import Encoding.Base32 (enc32, dec32)
|
|
import Encoding.Base45 (enc45, dec45)
|
|
import Encoding.Base58 (enc58, dec58)
|
|
import Encoding.Base62 (enc62, dec62)
|
|
import Encoding.Base64 (enc64, dec64, enc64url, dec64url)
|
|
import Encoding.Base85 (enc85, dec85)
|
|
import Encoding.Base91 (enc91, dec91)
|
|
import Encoding.Url (encurl, decurl)
|
|
import Encoding.Xx (encxx, decxx)
|
|
import Encoding.QuotedPrintable (encqp, decqp)
|
|
import Encoding.UnixToUnix (encuu, decuu)
|
|
import Encoding.Yenc (ency, decy)
|
|
import Encoding.Rotate (rotate)
|
|
|
|
|
|
data Based = Decode {
|
|
b91 :: Bool,
|
|
b85 :: Bool,
|
|
b64 :: Bool,
|
|
b64url :: Bool,
|
|
url :: Bool,
|
|
b62 :: Bool,
|
|
b58 :: Bool,
|
|
b45 :: Bool,
|
|
b32 :: Bool,
|
|
b16 :: Bool,
|
|
b10 :: Bool,
|
|
b8 :: Bool,
|
|
b2 :: Bool,
|
|
qp :: Bool,
|
|
uu :: Bool,
|
|
xx :: Bool,
|
|
yenc :: Bool,
|
|
rot :: Maybe Int,
|
|
solve :: Bool
|
|
}
|
|
| Encode {
|
|
b91 :: Bool,
|
|
b85 :: Bool,
|
|
b64 :: Bool,
|
|
b64url :: Bool,
|
|
url :: Bool,
|
|
b62 :: Bool,
|
|
b58 :: Bool,
|
|
b45 :: Bool,
|
|
b32 :: Bool,
|
|
b16 :: Bool,
|
|
b10 :: Bool,
|
|
b8 :: Bool,
|
|
b2 :: Bool,
|
|
qp :: Bool,
|
|
uu :: Bool,
|
|
xx :: Bool,
|
|
yenc :: Bool,
|
|
rot :: Maybe Int
|
|
}
|
|
deriving(Show, Data, Typeable)
|
|
|
|
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]
|
|
|
|
|
|
optionHandler :: Based -> B.ByteString -> B.ByteString
|
|
optionHandler Decode{b91=True} = dec91
|
|
optionHandler Encode{b91=True} = enc91
|
|
optionHandler Decode{b85=True} = dec85
|
|
optionHandler Encode{b85=True} = enc85
|
|
optionHandler Decode{b64=True} = dec64
|
|
optionHandler Encode{b64=True} = enc64
|
|
optionHandler Decode{b64url=True} = dec64url
|
|
optionHandler Encode{b64url=True} = enc64url
|
|
optionHandler Decode{url=True} = decurl
|
|
optionHandler Encode{url=True} = encurl
|
|
optionHandler Decode{b62=True} = dec62
|
|
optionHandler Encode{b62=True} = enc62
|
|
optionHandler Decode{b58=True} = dec58
|
|
optionHandler Encode{b58=True} = enc58
|
|
optionHandler Decode{b45=True} = dec45
|
|
optionHandler Encode{b45=True} = enc45
|
|
optionHandler Decode{b32=True} = dec32
|
|
optionHandler Encode{b32=True} = enc32
|
|
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{b2=True} = dec2
|
|
optionHandler Encode{b2=True} = enc2
|
|
optionHandler Decode{qp=True} = decqp
|
|
optionHandler Encode{qp=True} = encqp
|
|
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{rot=Just n} = rotate n
|
|
optionHandler Decode{solve=True} = solveEnc
|
|
|
|
decodeMode :: Based
|
|
decodeMode = Decode {
|
|
b91 = def &= help "decode base91",
|
|
b85 = def &= help "decode base85",
|
|
b64 = def &= help "decode base64",
|
|
b64url = def &= help "decode base64Url",
|
|
url = def &= help "decode URL",
|
|
b62 = def &= help "decode base62",
|
|
b58 = def &= help "decode base58",
|
|
b45 = def &= help "decode base45",
|
|
b32 = def &= help "decode base32",
|
|
b16 = def &= help "decode base16",
|
|
b10 = def &= help "decode decimal",
|
|
b8 = def &= help "decode octal",
|
|
b2 = def &= help "decode base2",
|
|
qp = def &= help "decode quoted-printable",
|
|
uu = def &= help "decode UnixToUnix",
|
|
xx = def &= help "decode xx, without padding",
|
|
yenc = def &= help "decode yEncode",
|
|
rot = def &= help "rotate characters by n positions",
|
|
solve = def &= help "solve encoding"
|
|
} &= help "Decode chosen base" &=auto
|
|
|
|
encodeMode :: Based
|
|
encodeMode = Encode {
|
|
b91 = def &= help "encode base91",
|
|
b85 = def &= help "encode base85",
|
|
b64 = def &= help "encode base64",
|
|
b64url = def &= help "encode base64Url",
|
|
url = def &= help "encode URL",
|
|
b62 = def &= help "encode base62",
|
|
b58 = def &= help "encode base58",
|
|
b45 = def &= help "encode base45",
|
|
b32 = def &= help "encode base32",
|
|
b16 = def &= help "encode base16",
|
|
b10 = def &= help "encode decimal",
|
|
b8 = def &= help "encode octal",
|
|
b2 = def &= help "encode base2",
|
|
qp = def &= help "encode quoted-printable",
|
|
uu = def &= help "encode UnixToUnix",
|
|
xx = def &= help "encode xx, without padding",
|
|
yenc = def &= help "encode yEncode",
|
|
rot = def &= help "rotate characters by n positions"
|
|
} &= help "Encode chosen base"
|
|
|
|
main :: IO()
|
|
main = do
|
|
args <- cmdArgs(modes[decodeMode, encodeMode] &= help "Based, Binary to text encoding.\nTo see arguments of encode and decode modes use --help=all" &= program "based" &= summary "based v0.4")
|
|
input <- B.getContents
|
|
B.putStr $ optionHandler args input
|