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 -- helper functions
convertToByteString :: String -> Either String BS.ByteString -- convertToByteString :: String -> Either String C.ByteString
convertToByteString str = -- convertToByteString str =
case BSU.fromString str of -- case BSU.fromString str of
Just bs -> Right bs -- Just bs -> Right bs
Nothing -> Left "Failed to convert string to ByteString." -- Nothing -> Left "Failed to convert string to ByteString."
decodeFromBase91 :: C.ByteString -> Either String BS.ByteString -- decodeFromBase91 :: C.ByteString -> Either String BS.ByteString
case B91.decode bs = -- decodeToBase91 decoded =
Just bs -> Right decoded -- case B91.decode of
Nothing -> Left "Failed to decode from Base91." -- Just bs -> Right decoded
-- Nothing -> Left "Failed to decode from Base91."
encodeToBase64 :: C.ByteString -> Either String BS.ByteString -- encodeToBase64 :: C.ByteString -> Either String BS.ByteString
encodeToBase64 bs = -- encodeToBase64 bs =
case B64.encode bs of -- case B64.encode bs of
encoded | BS.null encoded -> Left "Failed to encode base64." -- encoded | BS.null encoded -> Left "Failed to encode base64."
| otherwise -> Right encoded -- | otherwise -> Right encoded
decodeFromBase64 :: C.ByteString -> Either String BS.ByteString decodeFromBase64 :: C.ByteString -> Either String C.ByteString
decodeFromBase64 bs = decodeFromBase64 bs =
case B64L.decodeLenient bs of 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 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
@ -111,31 +112,31 @@ octToInt (x : xs) = x + 8 * octToInt xs
-- base functions -- base functions
-- without the show func, sequences like \n will not be shown as characters but will be executed as newline -- 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.unpack . B91.decode
dec91 :: C.ByteString -> Either String String -- dec91 :: C.ByteString -> Either String String
dec91 input = do -- dec91 input = do
decoded <- decodeFromBase91 input -- decoded <- decodeFromBase91 input
-- return $ C.unpack decoded -- -- return $ C.unpack decoded
-- is the same as -- -- is the same as
return (C.unpack decoded) -- return (C.unpack decoded)
enc91 = B91.encode . BSU.fromString enc91 = B91.encode . BSU.fromString
dec85 = C.unpack . U.fromRight . B85.decode . BSU.fromString dec85 = C.unpack . U.fromRight . B85.decode . BSU.fromString
enc85 = C.unpack . B85.encode . BSU.fromString enc85 = C.unpack . B85.encode . BSU.fromString
-- dec64 = C.unpack . B64L.decodeLenient . BSU.fromString -- dec64 = C.unpack . B64L.decodeLenient . BSU.fromString
dec64 :: String -> Either String String dec64 :: String -> String
dec64 input = do dec64 input =
byteString <- convertToByteString input case decodeFromBase64 (BSU.fromString input) of
decoded <- decodeFromBase64 byteString Right byteString -> C.unpack byteString
return $ BSU.toString decoded Left errMsg -> "Error: " ++ errMsg
-- enc64 = C.unpack . B64.encode . BSU.fromString enc64 = C.unpack . B64.encode . BSU.fromString
enc64 :: String -> Either String String -- enc64 :: String -> Either String String
enc64 input = do -- enc64 input = do
byteString <- convertToByteString input -- byteString <- convertToByteString input
encoded <- encodeToBase64 byteString -- encoded <- encodeToBase64 byteString
return $ BS.unpack encoded -- return $ BS.unpack encoded
dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString dec64url = C.unpack . U.fromRight . B64U.decode . BSU.fromString
enc64url = C.unpack . B64U.encode . BSU.fromString enc64url = C.unpack . B64U.encode . BSU.fromString

View File

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