From 37ee336508aa3f675adc75094acbac7612f7916a Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Fri, 3 May 2024 23:36:35 +0200 Subject: [PATCH] added b91 to solver function --- Main.hs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/Main.hs b/Main.hs index 28b6f24..6cef2e2 100644 --- a/Main.hs +++ b/Main.hs @@ -34,9 +34,13 @@ import Data.Bytes.Get (getWord64host) import Data.ByteString.UTF8 as BSU -- from utf8-string -- import Data.ByteString (singleton) import GHC.Word (Word8) +-- import Data.Char import qualified Data.ByteString.Char8 as C -- Regex imports import Text.Regex.TDFA +import Data.Word (Word8) +import Data.Char (ord, chr) +import Data.Maybe (fromMaybe) data Based = Decode { b91 :: Bool, @@ -118,12 +122,26 @@ 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 :: String -> String -dec91 = C.unpack . B91.decode +-- dec91 :: String -> String +-- dec91 = C.unpack . B91.decode --- case singleton . B91.decode input of --- Right decoded -> C.unpack decoded --- Left _ -> "Error decoding Base91.\n" +dec91 :: String -> String +dec91 input = + case B91.decode input :: BSU.ByteString of + decoded -> C.unpack decoded + +-- toWord8 :: String -> [Word8] +-- toWord8 = map (fromIntegral . fromEnum) + +-- dec91 :: String -> String +-- dec91 input = +-- map (chr . fromIntegral) (B91.decode input :: [Word8]) + +-- or + -- case B91.decode input :: [Word8] of + -- Right decoded -> map (toEnum . fromIntegral) decoded + -- Left err -> "Error decoding Base91: " ++ err + enc91 :: String -> String enc91 = B91.encode . BSU.fromString @@ -265,16 +283,17 @@ base16Regex = "^[0-9A-FXx]*$" solveEnc :: String -> String solveEnc input = - -- let isBase91 = BSU.fromString input =~ base91Regex :: Bool - let isBase64 = BSU.fromString input =~ base64Regex :: Bool + let isBase91 = BSU.fromString input =~ base91Regex :: Bool + isBase64 = BSU.fromString input =~ base64Regex :: Bool isBase58 = BSU.fromString input =~ base58Regex :: Bool isBase32 = BSU.fromString input =~ base32Regex :: Bool isBase16 = BSU.fromString input =~ base16Regex :: Bool + base91Result = if isBase91 then "\nTrying base91:\n" ++ dec91 input else "" base64Result = if isBase64 then "\nTrying base64:\n" ++ dec64 input else "" base58Result = if isBase58 then "\nTrying base58:\n" ++ dec58 input else "" base32Result = if isBase64 then "\nTrying base32:\n" ++ dec32 input else "" base16Result = if isBase16 then "\nTrying base16:\n" ++ dec16 input else "" - results = filter (not . null) [base64Result, base32Result, base16Result] + results = filter (not . null) [base91Result, base64Result, base32Result, base16Result] in if null results then "Not able to solve the encoding.\n"