changed functions from last commit, because these were pretty much bullshit. decode base64 has erro handlin now

This commit is contained in:
Stefan Friese 2024-04-20 01:20:53 +02:00
parent 50de1c5558
commit 27e600673b
3 changed files with 43 additions and 38 deletions

BIN
.Main.hs.swp Normal file

Binary file not shown.

69
Main.hs
View File

@ -73,28 +73,29 @@ data Based = Decode {
-- helper functions
convertToByteString :: String -> Either String BS.ByteString
convertToByteString str =
case BSU.fromString str of
Just bs -> Right bs
Nothing -> Left "Failed to convert string to ByteString."
-- convertToByteString :: String -> Either String C.ByteString
-- convertToByteString str =
-- case BSU.fromString str of
-- Just bs -> Right bs
-- Nothing -> Left "Failed to convert string to ByteString."
decodeFromBase91 :: C.ByteString -> Either String BS.ByteString
case B91.decode bs =
Just bs -> Right decoded
Nothing -> Left "Failed to decode from Base91."
-- decodeFromBase91 :: C.ByteString -> Either String BS.ByteString
-- decodeToBase91 decoded =
-- case B91.decode of
-- Just bs -> Right decoded
-- Nothing -> Left "Failed to decode from Base91."
encodeToBase64 :: C.ByteString -> Either String BS.ByteString
encodeToBase64 bs =
case B64.encode bs of
encoded | BS.null encoded -> Left "Failed to encode base64."
| otherwise -> Right encoded
-- encodeToBase64 :: C.ByteString -> Either String BS.ByteString
-- encodeToBase64 bs =
-- case B64.encode bs of
-- encoded | BS.null encoded -> Left "Failed to encode base64."
-- | otherwise -> Right encoded
decodeFromBase64 :: C.ByteString -> Either String BS.ByteString
decodeFromBase64 :: C.ByteString -> Either String C.ByteString
decodeFromBase64 bs =
case B64L.decodeLenient bs of
decoded | BS.null decoded -> Left "Failed to decode from base64."
decoded | C.null decoded -> Left "Failed to decode from base64."
| otherwise -> Right decoded
-- | otherwise -> Right (BSU.toString decoded)
-- Left err -> Left $ "Failed to decode from base64: " ++ err
@ -111,31 +112,31 @@ 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
-- dec91 = C.unpack . B91.decode
dec91 :: C.ByteString -> Either String String
dec91 input = do
decoded <- decodeFromBase91 input
-- return $ C.unpack decoded
-- is the same as
return (C.unpack decoded)
dec91 = C.unpack . B91.decode
-- dec91 :: C.ByteString -> Either String String
-- dec91 input = do
-- decoded <- decodeFromBase91 input
-- -- return $ C.unpack decoded
-- -- is the same as
-- return (C.unpack decoded)
enc91 = B91.encode . BSU.fromString
dec85 = C.unpack . U.fromRight . B85.decode . BSU.fromString
enc85 = C.unpack . B85.encode . BSU.fromString
-- dec64 = C.unpack . B64L.decodeLenient . BSU.fromString
dec64 :: String -> Either String String
dec64 input = do
byteString <- convertToByteString input
decoded <- decodeFromBase64 byteString
return $ BSU.toString decoded
dec64 :: String -> String
dec64 input =
case decodeFromBase64 (BSU.fromString input) of
Right byteString -> C.unpack byteString
Left errMsg -> "Error: " ++ errMsg
-- enc64 = C.unpack . B64.encode . BSU.fromString
enc64 :: String -> Either String String
enc64 input = do
byteString <- convertToByteString input
encoded <- encodeToBase64 byteString
return $ BS.unpack encoded
enc64 = C.unpack . B64.encode . BSU.fromString
-- enc64 :: String -> Either String String
-- enc64 input = do
-- byteString <- convertToByteString input
-- encoded <- encodeToBase64 byteString
-- return $ BS.unpack encoded
dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString
enc64url = C.unpack . B64U.encode . BSU.fromString

View File

@ -11,8 +11,10 @@ library
other-modules:
-- Data.Bytes.Text.Ascii
build-depends:
base ^>= 4.13.0.0,
byteslice == 0.2.6.0,
-- base ^>= 4.13.0.0,
base,
-- byteslice == 0.2.6.0,
byteslice,
-- hs-source-dirs:
default-language: Haskell2010
@ -20,14 +22,16 @@ executable based
main-is: Main.hs
other-modules: MyLib
build-depends:
base ^>= 4.13.0.0,
-- base ^>= 4.13.0.0,
base,
cmdargs,
sandi,
base62,
base91,
utf8-string,
bytestring,
byteslice == 0.2.6.0,
-- byteslice == 0.2.6.0,
byteslice,
either-unwrap,
text-show,
hxt,