From 5aa3917ad2beb08214f40c49eb4fcab514903646 Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Sun, 19 May 2024 13:23:39 +0200 Subject: [PATCH] moved all encodings to their own files under src/Encoding/ and commented the unused imports, will delete them in an upcoming commit --- MyLib.hs | 4 - app/Main.hs | 216 +++++++++----------------------- based.cabal | 47 ++++--- src/Encoding/Base32.hs | 19 +++ src/Encoding/Base58.hs | 19 +++ src/Encoding/Base62.hs | 32 +++++ src/Encoding/Base85.hs | 18 +++ src/Encoding/QuotedPrintable.hs | 18 +++ src/Encoding/UnixToUnix.hs | 19 +++ src/Encoding/Url.hs | 13 ++ src/Encoding/Xx.hs | 17 +++ src/Encoding/Yenc.hs | 20 +++ 12 files changed, 262 insertions(+), 180 deletions(-) delete mode 100644 MyLib.hs create mode 100644 src/Encoding/Base32.hs create mode 100644 src/Encoding/Base58.hs create mode 100644 src/Encoding/Base62.hs create mode 100644 src/Encoding/Base85.hs create mode 100644 src/Encoding/QuotedPrintable.hs create mode 100644 src/Encoding/UnixToUnix.hs create mode 100644 src/Encoding/Url.hs create mode 100644 src/Encoding/Xx.hs create mode 100644 src/Encoding/Yenc.hs diff --git a/MyLib.hs b/MyLib.hs deleted file mode 100644 index e657c44..0000000 --- a/MyLib.hs +++ /dev/null @@ -1,4 +0,0 @@ -module MyLib (someFunc) where - -someFunc :: IO () -someFunc = putStrLn "someFunc" diff --git a/app/Main.hs b/app/Main.hs index 8dfcf06..54c3678 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -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 diff --git a/based.cabal b/based.cabal index d99cdea..1119065 100644 --- a/based.cabal +++ b/based.cabal @@ -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 diff --git a/src/Encoding/Base32.hs b/src/Encoding/Base32.hs new file mode 100644 index 0000000..09e5089 --- /dev/null +++ b/src/Encoding/Base32.hs @@ -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 diff --git a/src/Encoding/Base58.hs b/src/Encoding/Base58.hs new file mode 100644 index 0000000..dfbb9a5 --- /dev/null +++ b/src/Encoding/Base58.hs @@ -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 diff --git a/src/Encoding/Base62.hs b/src/Encoding/Base62.hs new file mode 100644 index 0000000..65971bb --- /dev/null +++ b/src/Encoding/Base62.hs @@ -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 diff --git a/src/Encoding/Base85.hs b/src/Encoding/Base85.hs new file mode 100644 index 0000000..e902761 --- /dev/null +++ b/src/Encoding/Base85.hs @@ -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 + diff --git a/src/Encoding/QuotedPrintable.hs b/src/Encoding/QuotedPrintable.hs new file mode 100644 index 0000000..de6fbbd --- /dev/null +++ b/src/Encoding/QuotedPrintable.hs @@ -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 + diff --git a/src/Encoding/UnixToUnix.hs b/src/Encoding/UnixToUnix.hs new file mode 100644 index 0000000..25120e0 --- /dev/null +++ b/src/Encoding/UnixToUnix.hs @@ -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 + diff --git a/src/Encoding/Url.hs b/src/Encoding/Url.hs new file mode 100644 index 0000000..e037855 --- /dev/null +++ b/src/Encoding/Url.hs @@ -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 + diff --git a/src/Encoding/Xx.hs b/src/Encoding/Xx.hs new file mode 100644 index 0000000..d2cd36a --- /dev/null +++ b/src/Encoding/Xx.hs @@ -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 diff --git a/src/Encoding/Yenc.hs b/src/Encoding/Yenc.hs new file mode 100644 index 0000000..fc4a330 --- /dev/null +++ b/src/Encoding/Yenc.hs @@ -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