error handling for decoding functions

This commit is contained in:
Stefan Friese 2024-05-01 14:28:50 +02:00
parent d3289e0997
commit 6b950f6738
1 changed files with 72 additions and 8 deletions

80
Main.hs
View File

@ -143,18 +143,49 @@ enc64 input =
Right byteString -> C.unpack byteString Right byteString -> C.unpack byteString
Left errMsg -> "Error: " ++ errMsg Left errMsg -> "Error: " ++ errMsg
dec64url :: String -> String
dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString
enc64url :: String -> String
enc64url = C.unpack . B64U.encode . BSU.fromString enc64url = C.unpack . B64U.encode . BSU.fromString
decurl :: String -> String
decurl = HB.urlDecode decurl = HB.urlDecode
encurl :: String -> String
encurl = HB.urlEncode encurl = HB.urlEncode
dec62 = show . fromJust . B62.decode128 . (Bytes.fromByteString . BSU.fromString) dec62 = show . fromJust . B62.decode128 . (Bytes.fromByteString . BSU.fromString)
enc62 = C.unpack . BSU.fromString. Bytes.toLatinString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt))) enc62 = C.unpack . BSU.fromString. Bytes.toLatinString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt)))
dec58 = C.unpack . fromJust . B58.decodeBase58 . pack
dec58 :: String -> String
-- dec58 = C.unpack . fromJust . B58.decodeBase58 . pack
dec58 input =
case B58.decodeBase58 (pack input) of
Just decoded -> (C.unpack decoded)
Nothing -> "Error decoding Base58"
enc58 :: String -> String
enc58 = unpack . B58.encodeBase58 . BSU.fromString enc58 = unpack . B58.encodeBase58 . BSU.fromString
dec32 = C.unpack . U.fromRight . B32.decode . 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"
enc32 :: String -> String
enc32 = C.unpack . B32.encode . BSU.fromString enc32 = C.unpack . B32.encode . BSU.fromString
dec16 = C.unpack . U.fromRight . B16.decode . BSU.fromString
dec16 :: String -> String
-- dec16 = C.unpack . U.fromRight . B16.decode . BSU.fromString
dec16 input =
case B16.decode (BSU.fromString input) of
Right decoded -> C.unpack decoded
Left _ -> "Error decoding Base16"
enc16 = C.unpack . B16.encode . BSU.fromString enc16 = C.unpack . B16.encode . BSU.fromString
dec10 = show . hexStringToInt dec10 = show . hexStringToInt
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
@ -162,13 +193,46 @@ 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
enc2 = C.unpack . BSU.fromString . intToHexString . binToInt . (reverse . (map fromJust . (map fromBinDigit))) enc2 = C.unpack . BSU.fromString . intToHexString . binToInt . (reverse . (map fromJust . (map fromBinDigit)))
decqp = C.unpack . U.fromRight . QP.decode . 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"
encqp :: String -> String
encqp = C.unpack . QP.encode . BSU.fromString encqp = C.unpack . QP.encode . BSU.fromString
decuu = C.unpack . U.fromRight . UU.decode . 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."
encuu :: String -> String
encuu = C.unpack . UU.encode . BSU.fromString encuu = C.unpack . UU.encode . BSU.fromString
decxx = C.unpack . U.fromRight . XX.decode . 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."
encxx :: String -> String
encxx = C.unpack . XX.encode . BSU.fromString encxx = C.unpack . XX.encode . BSU.fromString
decy = C.unpack . U.fromRight . Y.decode . 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 ency = C.unpack . Y.encode . BSU.fromString
optionHandler Decode{b91=True} = dec91 optionHandler Decode{b91=True} = dec91