moved all encodings to their own files under src/Encoding/ and commented the unused imports, will delete them in an upcoming commit

This commit is contained in:
Stefan Friese 2024-05-19 13:23:39 +02:00
parent 792cc4265b
commit 5aa3917ad2
12 changed files with 262 additions and 180 deletions

View File

@ -1,4 +0,0 @@
module MyLib (someFunc) where
someFunc :: IO ()
someFunc = putStrLn "someFunc"

View File

@ -1,60 +1,72 @@
#!/usr/bin/env runhaskell
{-# LANGUAGE DeriveDataTypeable #-}
module Main where
import System.Console.CmdArgs
import Control.Arrow
import Data.Text (pack, unpack, Text)
import Data.Bits
-- import Data.Text (pack, unpack, Text)
-- import Data.Bits
-- import Data.Char
import Text.Read (readMaybe)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
-- import Text.Read (readMaybe)
-- import qualified Data.Text as T
-- import qualified Data.Text.Encoding as T
-- import qualified Data.Text.IO as T
-- import Data.Text (Text)
import qualified Data.Text.Lazy as TL
import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Encoding (decodeUtf8With, decodeUtf8, encodeUtf8)
import Data.Maybe (fromJust, fromMaybe)
import Data.Text.Encoding.Error (lenientDecode)
import TextShow (toText)
import Text.XML.HXT.DOM.Util (hexStringToInt, intToHexString, decimalStringToInt)
import TextShow.Data.Integral (showbBin, showbOct)
import Text.Ascii (fromBinDigit, fromOctDigit)
import Codec.CBOR.Magic (intToWord64)
import qualified Data.Either.Unwrap as U
import Data.Bytes.Text.Latin1 as Latin1
import qualified Codec.Binary.Base91 as B91
import qualified Codec.Binary.Base85 as B85
import qualified Codec.Binary.Base64 as B64
import qualified Data.ByteString.Base64 as B64L
import qualified Codec.Binary.Base64Url as B64U
import qualified Network.HTTP.Base as HB
import qualified Data.Word.Base62 as B62
import qualified Haskoin.Address.Base58 as B58
import qualified Codec.Binary.Base32 as B32
import qualified Codec.Binary.Base16 as B16
import qualified Codec.Binary.QuotedPrintable as QP
import qualified Codec.Binary.Uu as UU
import qualified Codec.Binary.Xx as XX
import qualified Codec.Binary.Yenc as Y
import qualified Data.Bytes as Bytes
import qualified Data.Bytes.Text.Ascii as ASCII
import Data.Bytes.Get (getWord64host)
import Data.ByteString.UTF8 as BSU -- from utf8-string
-- import qualified Data.Text.Lazy as TL
-- import Data.Text.Lazy.Builder (toLazyText)
-- import Data.Text.Encoding (decodeUtf8With, decodeUtf8, encodeUtf8)
-- import Data.Maybe (fromJust, fromMaybe)
-- import Data.Text.Encoding.Error (lenientDecode)
-- import TextShow (toText)
-- import Text.XML.HXT.DOM.Util (hexStringToInt, intToHexString, decimalStringToInt)
-- import TextShow.Data.Integral (showbBin, showbOct)
-- import Text.Ascii (fromBinDigit, fromOctDigit)
-- import Codec.CBOR.Magic (intToWord64)
-- import qualified Data.Either.Unwrap as U
-- import Data.Bytes.Text.Latin1 as Latin1
-- import qualified Codec.Binary.Base91 as B91
-- import qualified Codec.Binary.Base85 as B85
-- import qualified Codec.Binary.Base64 as B64
-- import qualified Data.ByteString.Base64 as B64L
-- import qualified Codec.Binary.Base64Url as B64U
-- import qualified Network.HTTP.Base as HB
-- import qualified Data.Word.Base62 as B62
-- import qualified Haskoin.Address.Base58 as B58
-- import qualified Codec.Binary.Base32 as B32
-- import qualified Codec.Binary.Base16 as B16
-- import qualified Codec.Binary.QuotedPrintable as QP
-- import qualified Codec.Binary.Uu as UU
-- import qualified Codec.Binary.Xx as XX
-- import qualified Codec.Binary.Yenc as Y
-- import qualified Data.Bytes as Bytes
-- import qualified Data.Bytes.Text.Ascii as ASCII
-- import Data.Bytes.Get (getWord64host)
-- import Data.ByteString (singleton)
import GHC.Word (Word8)
-- import GHC.Word (Word8)
-- import Data.Word (Word8)
-- import Data.Char (ord, chr, intToDigit, digitToInt)
import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Data.ByteString.Char8 as C
-- Regex imports
import Text.Regex.TDFA
import Data.Word (Word8)
import Data.Char (ord, chr, intToDigit, digitToInt)
import Encoding.Base2 (enc2, dec2)
import Encoding.Base8 (enc8, dec8)
import Encoding.Base10 (enc10, dec10)
import Encoding.Base16 (enc16, dec16)
import Encoding.Base64 (enc64, dec64, enc64url, dec64url)
import Encoding.Base91 (enc91, dec91)
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.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)
data Based = Decode {
b91 :: Bool,
@ -120,119 +132,6 @@ octToInt (x : xs) = x + 8 * octToInt xs
-- base functions
-- without the show func, sequences like \n will not be shown as characters but will be executed as newline
-- dec85 = C.unpack . U.fromRight . B85.decode . BSU.fromString
dec85 :: String -> String
dec85 input =
case B85.decode (BSU.fromString input) of
Right decodedStr -> C.unpack decodedStr
Left _ -> "Error decoding Base85.\n"
enc85 :: String -> String
enc85 = C.unpack . B85.encode . BSU.fromString
decurl :: String -> String
decurl = HB.urlDecode
encurl :: String -> String
encurl = HB.urlEncode
dec62 :: String -> String
-- dec62 = T.unpack . B62.text128 . fromJust . B62.decode128 . Bytes.fromAsciiString
-- dec62 input =
-- case B62.decode128 (Bytes.fromAsciiString input) of
-- Just decoded -> (T.unpack (B62.text128 decoded))
-- -- Just decoded -> C.unpack decoded
-- Nothing -> "Error decoding Base62.\n"
-- dec62 = show . fromJust . B62.decode128 . (Bytes.fromByteString . BSU.fromString)
dec62 input =
let decoded = B62.decode128 (Bytes.fromByteString (BSU.fromString input))
in fromMaybe "Error decoding Base62.\n" (show <$> decoded)
-- dec62 input =
-- maybe "Error decoding Base62.\n" show (B62.decode128 (Bytes.fromByteString (BSU.fromString input)))
stringToInt :: String -> Maybe Integer
stringToInt = readMaybe
-- enc62 :: String -> String
-- enc62 = C.unpack . BSU.fromString. Latin1.toString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt)))
-- enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt)))
-- enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . (Bytes.fromByteArray . (B62.encode128 . (fromIntegral . ( BSU.fromString . fromJust StringToInt))))
-- working correct:
-- enc62 :: String -> String
-- enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . Bytes.fromByteArray . B62.encode64 . fromIntegral . fromJust . stringToInt
enc62 :: String -> String
enc62 input =
let intValue = fromMaybe (error "Error: Unable to convert input string to integer") (stringToInt input)
encoded = B62.encode64 (fromIntegral intValue)
encodedText = T.decodeUtf8 (BSU.fromString (Latin1.toString (Bytes.fromByteArray encoded)))
in T.unpack encodedText
-- enc62 :: Text -> Text
-- enc62 text = B62.text128 (fromIntegral (T.length text))
dec58 :: String -> String
-- dec58 = C.unpack . fromJust . B58.decodeBase58 . pack
dec58 input =
maybe "Error decoding Base58.\n" C.unpack (B58.decodeBase58 (T.pack input))
enc58 :: String -> String
enc58 = T.unpack . B58.encodeBase58 . BSU.fromString
dec32 :: String -> String
-- dec32 = C.unpack . U.fromRight . B32.decode . BSU.fromString
dec32 input =
case B32.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding Base32.\n"
enc32 :: String -> String
enc32 = C.unpack . B32.encode . BSU.fromString
decqp :: String -> String
-- decqp = C.unpack . U.fromRight . QP.decode . BSU.fromString
decqp input =
case QP.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding QP.\n"
encqp :: String -> String
encqp = C.unpack . QP.encode . BSU.fromString
decuu :: String -> String
-- decuu = C.unpack . U.fromRight . UU.decode . BSU.fromString
decuu input =
case UU.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding UU.\n"
encuu :: String -> String
encuu = C.unpack . UU.encode . BSU.fromString
decxx :: String -> String
-- decxx = C.unpack . U.fromRight . XX.decode . BSU.fromString
decxx input =
case XX.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding XX.\n"
encxx :: String -> String
encxx = C.unpack . XX.encode . BSU.fromString
decy :: String -> String
-- decy = C.unpack . U.fromRight . Y.decode . BSU.fromString
decy input =
case Y.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding Y.\n"
ency :: String -> String
ency = C.unpack . Y.encode . BSU.fromString
base91Regex = "^[!-~]*$"
base85Regex = "^[0-9A-Za-z!#$%&()*+,-;<=>?@^_`{|}~]+$"
-- base85Regex = "^[A-Za-u0-9!\"#$%&((*+,-./;:<=@[]\\`]*$"
@ -285,6 +184,7 @@ solveEnc input =
-- then dec32 input
-- else "Not able to solve the encoding.\n"
-- optionHandler :: EncodeOptions -> Text -> Text
optionHandler Decode{b91=True} = dec91
optionHandler Encode{b91=True} = enc91

View File

@ -12,51 +12,62 @@ library
Encoding.Base8
Encoding.Base10
Encoding.Base16
Encoding.Base32
Encoding.Base58
Encoding.Base62
Encoding.Base64
Encoding.Base85
Encoding.Base91
Encoding.Url
Encoding.QuotedPrintable
Encoding.UnixToUnix
Encoding.Xx
Encoding.Yenc
other-modules:
-- Data.Bytes.Text.Ascii
build-depends:
-- base ^>= 4.13.0.0,
base,
-- byteslice == 0.2.6.0,
base,
sandi,
base62,
base91,
utf8-string,
byteslice,
bytestring,
HTTP,
hxt,
haskoin-core,
text,
base64-bytestring
-- hs-source-dirs:
default-language: Haskell2010
executable based
hs-source-dirs: app
main-is: Main.hs
main-is: Main.hs
-- other-modules: MyLib
build-depends:
-- base ^>= 4.13.0.0,
base,
based,
cmdargs,
sandi,
base62,
base91,
utf8-string,
bytestring,
-- byteslice == 0.2.6.0,
byteslice,
either-unwrap,
text-show,
hxt,
haskoin-core,
text,
bytes,
cborg,
text-latin1,
HTTP,
base64-bytestring,
regex-tdfa
-- sandi,
-- base62,
-- base91,
-- byteslice == 0.2.6.0,
-- byteslice,
-- either-unwrap,
-- text-show,
-- hxt,
-- haskoin-core,
-- text,
-- bytes,
-- cborg,
-- text-latin1,
-- HTTP,
-- base64-bytestring,
-- hs-source-dirs:
default-language: Haskell2010

19
src/Encoding/Base32.hs Normal file
View File

@ -0,0 +1,19 @@
module Encoding.Base32
( enc32
, dec32
) where
import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Data.ByteString.Char8 as C
import qualified Codec.Binary.Base32 as B32
dec32 :: String -> String
-- dec32 = C.unpack . U.fromRight . B32.decode . BSU.fromString
dec32 input =
case B32.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding Base32.\n"
enc32 :: String -> String
enc32 = C.unpack . B32.encode . BSU.fromString

19
src/Encoding/Base58.hs Normal file
View File

@ -0,0 +1,19 @@
module Encoding.Base58
( enc58
, dec58
) where
import qualified Data.ByteString.Char8 as C
import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Haskoin.Address.Base58 as B58
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
dec58 :: String -> String
-- dec58 = C.unpack . fromJust . B58.decodeBase58 . pack
dec58 input =
maybe "Error decoding Base58.\n" C.unpack (B58.decodeBase58 (T.pack input))
enc58 :: String -> String
enc58 = T.unpack . B58.encodeBase58 . BSU.fromString

32
src/Encoding/Base62.hs Normal file
View File

@ -0,0 +1,32 @@
module Encoding.Base62
( enc62
, dec62
) where
import qualified Data.Word.Base62 as B62
import Text.Read (readMaybe)
import Data.Maybe (fromMaybe)
import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Data.Bytes as Bytes
import Data.Bytes.Text.Latin1 as Latin1
import qualified Data.ByteString.Char8 as C
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
-- import qualified Data.Text.IO as T
dec62 :: String -> String
dec62 input =
let decoded = B62.decode128 (Bytes.fromByteString (BSU.fromString input))
in fromMaybe "Error decoding Base62.\n" (show <$> decoded)
stringToInt :: String -> Maybe Integer
stringToInt = readMaybe
enc62 :: String -> String
enc62 input =
let intValue = fromMaybe (error "Error: Unable to convert input string to integer") (stringToInt input)
encoded = B62.encode64 (fromIntegral intValue)
encodedText = T.decodeUtf8 (BSU.fromString (Latin1.toString (Bytes.fromByteArray encoded)))
in T.unpack encodedText

18
src/Encoding/Base85.hs Normal file
View File

@ -0,0 +1,18 @@
module Encoding.Base85
( enc85
, dec85
) where
import qualified Codec.Binary.Base85 as B85
import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Data.ByteString.Char8 as C
dec85 :: String -> String
dec85 input =
case B85.decode (BSU.fromString input) of
Right decodedStr -> C.unpack decodedStr
Left _ -> "Error decoding Base85.\n"
enc85 :: String -> String
enc85 = C.unpack . B85.encode . BSU.fromString

View File

@ -0,0 +1,18 @@
module Encoding.QuotedPrintable
( encqp
, decqp
) where
import qualified Codec.Binary.QuotedPrintable as QP
import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Data.ByteString.Char8 as C
decqp :: String -> String
decqp input =
case QP.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding QP.\n"
encqp :: String -> String
encqp = C.unpack . QP.encode . BSU.fromString

View File

@ -0,0 +1,19 @@
module Encoding.UnixToUnix
( encuu
, decuu
) where
import qualified Codec.Binary.Uu as UU
import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Data.ByteString.Char8 as C
decuu :: String -> String
-- decuu = C.unpack . U.fromRight . UU.decode . BSU.fromString
decuu input =
case UU.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding UU.\n"
encuu :: String -> String
encuu = C.unpack . UU.encode . BSU.fromString

13
src/Encoding/Url.hs Normal file
View File

@ -0,0 +1,13 @@
module Encoding.Url
( encurl
, decurl
) where
import qualified Network.HTTP.Base as HB
decurl :: String -> String
decurl = HB.urlDecode
encurl :: String -> String
encurl = HB.urlEncode

17
src/Encoding/Xx.hs Normal file
View File

@ -0,0 +1,17 @@
module Encoding.Xx
( encxx
, decxx
) where
import qualified Codec.Binary.Xx as XX
import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Data.ByteString.Char8 as C
decxx :: String -> String
decxx input =
case XX.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding XX.\n"
encxx :: String -> String
encxx = C.unpack . XX.encode . BSU.fromString

20
src/Encoding/Yenc.hs Normal file
View File

@ -0,0 +1,20 @@
module Encoding.Yenc
( ency
, decy
) where
import Data.ByteString.UTF8 as BSU -- from utf8-string
import qualified Data.ByteString.Char8 as C
import qualified Codec.Binary.Yenc as Y
decy :: String -> String
-- decy = C.unpack . U.fromRight . Y.decode . BSU.fromString
decy input =
case Y.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding Y.\n"
ency :: String -> String
ency = C.unpack . Y.encode . BSU.fromString