more error handling for decoding functions

This commit is contained in:
Stefan Friese 2024-05-01 23:25:57 +02:00
parent 6b950f6738
commit 6800552f65
1 changed files with 34 additions and 17 deletions

51
Main.hs
View File

@ -13,6 +13,7 @@ import TextShow.Data.Integral (showbBin, showbOct)
import Text.Ascii (fromBinDigit, fromOctDigit) import Text.Ascii (fromBinDigit, fromOctDigit)
import Codec.CBOR.Magic (intToWord64) import Codec.CBOR.Magic (intToWord64)
import qualified Data.Either.Unwrap as U 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.Base91 as B91
import qualified Codec.Binary.Base85 as B85 import qualified Codec.Binary.Base85 as B85
import qualified Codec.Binary.Base64 as B64 import qualified Codec.Binary.Base64 as B64
@ -31,6 +32,7 @@ import qualified Data.Bytes as Bytes
import qualified Data.Bytes.Text.Ascii as ASCII import qualified Data.Bytes.Text.Ascii as ASCII
import Data.Bytes.Get (getWord64host) import Data.Bytes.Get (getWord64host)
import Data.ByteString.UTF8 as BSU -- from utf8-string import Data.ByteString.UTF8 as BSU -- from utf8-string
-- import Data.ByteString (singleton)
import qualified Data.ByteString.Char8 as C import qualified Data.ByteString.Char8 as C
data Based = Decode { data Based = Decode {
@ -89,13 +91,13 @@ data Based = Decode {
encodeToBase64 :: C.ByteString -> Either String C.ByteString encodeToBase64 :: C.ByteString -> Either String C.ByteString
encodeToBase64 bs = encodeToBase64 bs =
case B64.encode bs of case B64.encode bs of
encoded | C.null encoded -> Left "Failed to encode base64." encoded | C.null encoded -> Left "Failed to encode Base64.\n"
| otherwise -> Right encoded | otherwise -> Right encoded
decodeFromBase64 :: C.ByteString -> Either String C.ByteString decodeFromBase64 :: C.ByteString -> Either String C.ByteString
decodeFromBase64 bs = decodeFromBase64 bs =
case B64L.decodeLenient bs of case B64L.decodeLenient bs of
decoded | C.null decoded -> Left "Failed to decode from base64." decoded | C.null decoded -> Left "Failed to decode from Base64.\n"
| otherwise -> Right decoded | otherwise -> Right decoded
-- | otherwise -> Right (BSU.toString decoded) -- | otherwise -> Right (BSU.toString decoded)
-- Left err -> Left $ "Failed to decode from base64: " ++ err -- Left err -> Left $ "Failed to decode from base64: " ++ err
@ -114,6 +116,10 @@ octToInt (x : xs) = x + 8 * octToInt xs
dec91 :: String -> String dec91 :: String -> String
dec91 = C.unpack . B91.decode dec91 = C.unpack . B91.decode
-- dec91 input =
-- case singleton . B91.decode input of
-- Right decoded -> C.unpack decoded
-- Left _ -> "Error decoding Base91.\n"
enc91 :: String -> String enc91 :: String -> String
enc91 = B91.encode . BSU.fromString enc91 = B91.encode . BSU.fromString
@ -121,10 +127,9 @@ enc91 = B91.encode . BSU.fromString
-- dec85 = C.unpack . U.fromRight . B85.decode . BSU.fromString -- dec85 = C.unpack . U.fromRight . B85.decode . BSU.fromString
dec85 :: String -> String dec85 :: String -> String
dec85 input = dec85 input =
let decoded = B85.decode (BSU.fromString input) case B85.decode (BSU.fromString input) of
in case decoded of
Right decodedStr -> C.unpack decodedStr Right decodedStr -> C.unpack decodedStr
Left (decodedStr, _) -> C.unpack decodedStr Left _ -> "Error decoding Base85.\n"
enc85 :: String -> String enc85 :: String -> String
enc85 = C.unpack . B85.encode . BSU.fromString enc85 = C.unpack . B85.encode . BSU.fromString
@ -144,27 +149,33 @@ enc64 input =
Left errMsg -> "Error: " ++ errMsg Left errMsg -> "Error: " ++ errMsg
dec64url :: String -> String dec64url :: String -> String
dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString -- dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString
dec64url input =
case B64.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding Base64 for URLs.\n"
enc64url :: String -> String enc64url :: String -> String
enc64url = C.unpack . B64U.encode . BSU.fromString enc64url = C.unpack . B64U.encode . BSU.fromString
decurl :: String -> String decurl :: String -> String
decurl = HB.urlDecode decurl = HB.urlDecode
encurl :: String -> String encurl :: String -> String
encurl = HB.urlEncode encurl = HB.urlEncode
dec62 = show . fromJust . B62.decode128 . (Bytes.fromByteString . BSU.fromString) dec62 :: String -> String
enc62 = C.unpack . BSU.fromString. Bytes.toLatinString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt))) -- dec62 = show . fromJust . B62.decode128 . (Bytes.fromByteString . BSU.fromString)
dec62 input =
maybe "Error decoding Base62.\n" show (B62.decode128 (Bytes.fromByteString (BSU.fromString input)))
enc62 :: String -> String
enc62 = C.unpack . BSU.fromString. Latin1.toString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt)))
dec58 :: String -> String dec58 :: String -> String
-- dec58 = C.unpack . fromJust . B58.decodeBase58 . pack -- dec58 = C.unpack . fromJust . B58.decodeBase58 . pack
dec58 input = dec58 input =
case B58.decodeBase58 (pack input) of maybe "Error decoding Base58.\n" C.unpack (B58.decodeBase58 (pack input))
Just decoded -> (C.unpack decoded)
Nothing -> "Error decoding Base58"
enc58 :: String -> String enc58 :: String -> String
enc58 = unpack . B58.encodeBase58 . BSU.fromString enc58 = unpack . B58.encodeBase58 . BSU.fromString
@ -174,7 +185,7 @@ dec32 :: String -> String
dec32 input = dec32 input =
case B32.decode (BSU.fromString input) of case B32.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded Right decoded -> C.unpack decoded
Left _ -> "Error decoding Base32" Left _ -> "Error decoding Base32.\n"
enc32 :: String -> String enc32 :: String -> String
enc32 = C.unpack . B32.encode . BSU.fromString enc32 = C.unpack . B32.encode . BSU.fromString
@ -184,11 +195,17 @@ dec16 :: String -> String
dec16 input = dec16 input =
case B16.decode (BSU.fromString input) of case B16.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded Right decoded -> C.unpack decoded
Left _ -> "Error decoding Base16" Left _ -> "Error decoding Base16.\n"
enc16 :: String -> String
enc16 = C.unpack . B16.encode . BSU.fromString enc16 = C.unpack . B16.encode . BSU.fromString
dec10:: String -> String
dec10 = show . hexStringToInt dec10 = show . hexStringToInt
enc10 :: String -> String
enc10 = C.unpack . BSU.fromString . intToHexString . decimalStringToInt -- Depending on what you want, do enc10 = show . map ord enc10 = C.unpack . BSU.fromString . intToHexString . decimalStringToInt -- Depending on what you want, do enc10 = show . map ord
dec8 = C.unpack . encodeUtf8 . toText . showbOct . hexStringToInt dec8 = C.unpack . encodeUtf8 . toText . showbOct . hexStringToInt
enc8 = C.unpack . BSU.fromString . intToHexString . octToInt . (reverse . (map fromJust . (map fromOctDigit))) enc8 = C.unpack . BSU.fromString . intToHexString . octToInt . (reverse . (map fromJust . (map fromOctDigit)))
dec2 = C.unpack . encodeUtf8 . toText . showbBin . hexStringToInt dec2 = C.unpack . encodeUtf8 . toText . showbBin . hexStringToInt
@ -199,7 +216,7 @@ decqp :: String -> String
decqp input = decqp input =
case QP.decode (BSU.fromString input) of case QP.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded Right decoded -> C.unpack decoded
Left _ -> "Error decoding QP" Left _ -> "Error decoding QP.\n"
encqp :: String -> String encqp :: String -> String
encqp = C.unpack . QP.encode . BSU.fromString encqp = C.unpack . QP.encode . BSU.fromString
@ -209,7 +226,7 @@ decuu :: String -> String
decuu input = decuu input =
case UU.decode (BSU.fromString input) of case UU.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded Right decoded -> C.unpack decoded
Left _ -> "Error decoding UU." Left _ -> "Error decoding UU.\n"
encuu :: String -> String encuu :: String -> String
encuu = C.unpack . UU.encode . BSU.fromString encuu = C.unpack . UU.encode . BSU.fromString
@ -219,7 +236,7 @@ decxx :: String -> String
decxx input = decxx input =
case XX.decode (BSU.fromString input) of case XX.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded Right decoded -> C.unpack decoded
Left _ -> "Error decoding XX." Left _ -> "Error decoding XX.\n"
encxx :: String -> String encxx :: String -> String
encxx = C.unpack . XX.encode . BSU.fromString encxx = C.unpack . XX.encode . BSU.fromString