From 6b950f67382e08ec87a45fbce600979688981da2 Mon Sep 17 00:00:00 2001 From: stefan Date: Wed, 1 May 2024 14:28:50 +0200 Subject: [PATCH] error handling for decoding functions --- Main.hs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/Main.hs b/Main.hs index 57d8848..d692250 100644 --- a/Main.hs +++ b/Main.hs @@ -143,18 +143,49 @@ enc64 input = Right byteString -> C.unpack byteString Left errMsg -> "Error: " ++ errMsg - +dec64url :: String -> String dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString + +enc64url :: String -> String enc64url = C.unpack . B64U.encode . BSU.fromString + + +decurl :: String -> String decurl = HB.urlDecode + +encurl :: String -> String encurl = HB.urlEncode + dec62 = show . fromJust . B62.decode128 . (Bytes.fromByteString . BSU.fromString) 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 -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 -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 dec10 = show . hexStringToInt 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))) dec2 = C.unpack . encodeUtf8 . toText . showbBin . hexStringToInt 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 -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 -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 -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 optionHandler Decode{b91=True} = dec91