2024-06-07 18:09:20 +02:00
#!/ usr / bin / env runhaskell
2024-05-19 13:23:39 +02:00
2022-05-17 00:23:30 +02:00
{- # LANGUAGE DeriveDataTypeable # -}
module Main where
import System.Console.CmdArgs
2024-06-07 18:09:20 +02:00
import qualified Data.ByteString as B
2024-06-08 22:04:28 +02:00
import Encoding.Base2 ( enc2 , dec2 )
2024-06-08 21:38:01 +02:00
import Encoding.Base8 ( enc8 , dec8 )
2024-05-19 13:23:39 +02:00
import Encoding.Base10 ( enc10 , dec10 )
import Encoding.Base16 ( enc16 , dec16 )
import Encoding.Base32 ( enc32 , dec32 )
2024-06-08 22:04:28 +02:00
import Encoding.Base45 ( enc45 , dec45 )
2024-05-19 13:23:39 +02:00
import Encoding.Base58 ( enc58 , dec58 )
2024-06-09 13:12:23 +02:00
import Encoding.Base62 ( enc62 , dec62 )
2024-05-19 13:23:39 +02:00
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 )
2024-06-23 19:30:10 +02:00
import Encoding.LetterToNumber ( encltn , decltn )
2024-06-08 20:26:16 +02:00
import Encoding.Rotate ( rotate )
2024-06-11 15:21:23 +02:00
import Encoding.Solve ( solveEnc )
2024-05-19 13:23:39 +02:00
2022-05-17 00:23:30 +02:00
data Based = Decode {
2024-06-07 18:09:20 +02:00
b91 :: Bool ,
b85 :: Bool ,
b64 :: Bool ,
b64url :: Bool ,
url :: Bool ,
2024-06-09 13:12:23 +02:00
b62 :: Bool ,
2024-06-07 18:09:20 +02:00
b58 :: Bool ,
2024-06-08 22:04:28 +02:00
b45 :: Bool ,
2024-06-07 18:09:20 +02:00
b32 :: Bool ,
b16 :: Bool ,
b10 :: Bool ,
2024-06-08 21:38:01 +02:00
b8 :: Bool ,
2024-06-08 22:04:28 +02:00
b2 :: Bool ,
2024-06-07 18:09:20 +02:00
qp :: Bool ,
uu :: Bool ,
xx :: Bool ,
2024-06-08 20:26:16 +02:00
yenc :: Bool ,
2024-06-23 19:30:10 +02:00
a1z26 :: Bool ,
2024-06-09 22:48:37 +02:00
rot :: Maybe Int ,
solve :: Bool
2024-06-07 18:09:20 +02:00
}
| Encode {
b91 :: Bool ,
b85 :: Bool ,
b64 :: Bool ,
b64url :: Bool ,
url :: Bool ,
2024-06-09 13:12:23 +02:00
b62 :: Bool ,
2024-06-07 18:09:20 +02:00
b58 :: Bool ,
2024-06-08 22:04:28 +02:00
b45 :: Bool ,
2024-06-07 18:09:20 +02:00
b32 :: Bool ,
b16 :: Bool ,
b10 :: Bool ,
2024-06-08 21:38:01 +02:00
b8 :: Bool ,
2024-06-08 22:04:28 +02:00
b2 :: Bool ,
2024-06-07 18:09:20 +02:00
qp :: Bool ,
uu :: Bool ,
xx :: Bool ,
2024-06-08 20:26:16 +02:00
yenc :: Bool ,
2024-06-23 19:30:10 +02:00
a1z26 :: Bool ,
2024-06-08 20:26:16 +02:00
rot :: Maybe Int
2024-06-07 18:09:20 +02:00
}
deriving ( Show , Data , Typeable )
2024-05-19 13:23:39 +02:00
2024-06-07 18:09:20 +02:00
optionHandler :: Based -> B . ByteString -> B . ByteString
2022-05-17 00:23:30 +02:00
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
2022-05-22 22:48:44 +02:00
optionHandler Decode { b64url = True } = dec64url
optionHandler Encode { b64url = True } = enc64url
2022-09-19 23:11:13 +02:00
optionHandler Decode { url = True } = decurl
optionHandler Encode { url = True } = encurl
2024-06-09 13:12:23 +02:00
optionHandler Decode { b62 = True } = dec62
optionHandler Encode { b62 = True } = enc62
2022-05-19 23:55:26 +02:00
optionHandler Decode { b58 = True } = dec58
optionHandler Encode { b58 = True } = enc58
2024-06-08 22:04:28 +02:00
optionHandler Decode { b45 = True } = dec45
optionHandler Encode { b45 = True } = enc45
2022-05-17 00:23:30 +02:00
optionHandler Decode { b32 = True } = dec32
optionHandler Encode { b32 = True } = enc32
optionHandler Decode { b16 = True } = dec16
optionHandler Encode { b16 = True } = enc16
2022-05-19 23:55:26 +02:00
optionHandler Decode { b10 = True } = dec10
optionHandler Encode { b10 = True } = enc10
2024-06-08 21:38:01 +02:00
optionHandler Decode { b8 = True } = dec8
optionHandler Encode { b8 = True } = enc8
2024-06-08 22:04:28 +02:00
optionHandler Decode { b2 = True } = dec2
optionHandler Encode { b2 = True } = enc2
2022-05-19 23:55:26 +02:00
optionHandler Decode { qp = True } = decqp
optionHandler Encode { qp = True } = encqp
2024-05-05 00:04:19 +02:00
optionHandler Encode { uu = True } = encuu
optionHandler Decode { uu = True } = decuu
2022-05-19 23:55:26 +02:00
optionHandler Decode { xx = True } = decxx
optionHandler Encode { xx = True } = encxx
2024-06-23 19:30:10 +02:00
optionHandler Decode { yenc = True } = decy
optionHandler Encode { yenc = True } = ency
optionHandler Decode { a1z26 = True } = decltn
optionHandler Encode { a1z26 = True } = encltn
2024-06-08 20:26:16 +02:00
optionHandler Decode { rot = Just n } = rotate n
2024-06-23 19:30:10 +02:00
optionHandler Encode { rot = Just n } = rotate n
2024-06-09 22:48:37 +02:00
optionHandler Decode { solve = True } = solveEnc
2022-05-17 00:23:30 +02:00
2023-07-25 23:17:25 +02:00
decodeMode :: Based
decodeMode = Decode {
2024-06-07 18:09:20 +02:00
b91 = def &= help " decode base91 " ,
b85 = def &= help " decode base85 " ,
b64 = def &= help " decode base64 " ,
b64url = def &= help " decode base64Url " ,
2024-06-09 22:27:28 +02:00
url = def &= help " decode URL " ,
2024-06-09 13:12:23 +02:00
b62 = def &= help " decode base62 " ,
2024-06-07 18:09:20 +02:00
b58 = def &= help " decode base58 " ,
2024-06-08 22:04:28 +02:00
b45 = def &= help " decode base45 " ,
2024-06-07 18:09:20 +02:00
b32 = def &= help " decode base32 " ,
b16 = def &= help " decode base16 " ,
2024-06-09 22:27:28 +02:00
b10 = def &= help " decode decimal " ,
b8 = def &= help " decode octal " ,
2024-06-08 22:04:28 +02:00
b2 = def &= help " decode base2 " ,
2024-06-07 18:09:20 +02:00
qp = def &= help " decode quoted-printable " ,
2024-06-09 22:27:28 +02:00
uu = def &= help " decode UnixToUnix " ,
2024-06-10 22:09:18 +02:00
xx = def &= help " decode xx, without padding " ,
2024-06-08 20:26:16 +02:00
yenc = def &= help " decode yEncode " ,
2024-06-23 19:30:10 +02:00
a1z26 = def &= help " decode letter to number " ,
2024-06-09 22:48:37 +02:00
rot = def &= help " rotate characters by n positions " ,
solve = def &= help " solve encoding "
2024-06-07 18:09:20 +02:00
} &= help " Decode chosen base " &= auto
2022-05-17 00:23:30 +02:00
2023-07-25 23:17:25 +02:00
encodeMode :: Based
encodeMode = Encode {
2024-06-07 18:09:20 +02:00
b91 = def &= help " encode base91 " ,
b85 = def &= help " encode base85 " ,
b64 = def &= help " encode base64 " ,
b64url = def &= help " encode base64Url " ,
2024-06-09 22:27:28 +02:00
url = def &= help " encode URL " ,
2024-06-09 13:12:23 +02:00
b62 = def &= help " encode base62 " ,
2024-06-07 18:09:20 +02:00
b58 = def &= help " encode base58 " ,
2024-06-08 22:04:28 +02:00
b45 = def &= help " encode base45 " ,
2024-06-07 18:09:20 +02:00
b32 = def &= help " encode base32 " ,
b16 = def &= help " encode base16 " ,
2024-06-09 22:27:28 +02:00
b10 = def &= help " encode decimal " ,
b8 = def &= help " encode octal " ,
2024-06-08 22:04:28 +02:00
b2 = def &= help " encode base2 " ,
2024-06-07 18:09:20 +02:00
qp = def &= help " encode quoted-printable " ,
2024-06-09 22:27:28 +02:00
uu = def &= help " encode UnixToUnix " ,
2024-06-10 22:09:18 +02:00
xx = def &= help " encode xx, without padding " ,
2024-06-08 20:26:16 +02:00
yenc = def &= help " encode yEncode " ,
2024-06-23 19:30:10 +02:00
a1z26 = def &= help " encode letter to number " ,
2024-06-08 20:26:16 +02:00
rot = def &= help " rotate characters by n positions "
2024-06-07 18:09:20 +02:00
} &= help " Encode chosen base "
2022-05-17 00:23:30 +02:00
2023-07-25 23:17:25 +02:00
main :: IO ()
2024-06-07 18:09:20 +02:00
main = do
args <- cmdArgs ( modes [ decodeMode , encodeMode ] &= help " Based, Binary to text encoding. \ n To see arguments of encode and decode modes use --help=all " &= program " based " &= summary " based v0.4 " )
input <- B . getContents
B . putStr $ optionHandler args input