added letter to number encoding, WIP. Non Ascii character throw an error right, now. Maybe gonna change it so bytestring is output unchanged
This commit is contained in:
		
							parent
							
								
									8664170b0d
								
							
						
					
					
						commit
						e0d97a4084
					
				
							
								
								
									
										13
									
								
								app/Main.hs
								
								
								
								
							
							
						
						
									
										13
									
								
								app/Main.hs
								
								
								
								
							| 
						 | 
					@ -21,6 +21,7 @@ import Encoding.Xx              (encxx, decxx)
 | 
				
			||||||
import Encoding.QuotedPrintable (encqp, decqp)
 | 
					import Encoding.QuotedPrintable (encqp, decqp)
 | 
				
			||||||
import Encoding.UnixToUnix      (encuu, decuu)
 | 
					import Encoding.UnixToUnix      (encuu, decuu)
 | 
				
			||||||
import Encoding.Yenc            (ency, decy)
 | 
					import Encoding.Yenc            (ency, decy)
 | 
				
			||||||
 | 
					import Encoding.LetterToNumber  (encltn, decltn)
 | 
				
			||||||
import Encoding.Rotate          (rotate)
 | 
					import Encoding.Rotate          (rotate)
 | 
				
			||||||
import Encoding.Solve           (solveEnc)
 | 
					import Encoding.Solve           (solveEnc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -43,6 +44,7 @@ data Based = Decode {
 | 
				
			||||||
             uu  :: Bool,
 | 
					             uu  :: Bool,
 | 
				
			||||||
             xx  :: Bool,
 | 
					             xx  :: Bool,
 | 
				
			||||||
             yenc :: Bool,
 | 
					             yenc :: Bool,
 | 
				
			||||||
 | 
					             a1z26 :: Bool,
 | 
				
			||||||
             rot :: Maybe Int,
 | 
					             rot :: Maybe Int,
 | 
				
			||||||
             solve :: Bool
 | 
					             solve :: Bool
 | 
				
			||||||
                   }
 | 
					                   }
 | 
				
			||||||
| 
						 | 
					@ -64,6 +66,7 @@ data Based = Decode {
 | 
				
			||||||
             uu  :: Bool,
 | 
					             uu  :: Bool,
 | 
				
			||||||
             xx  :: Bool,
 | 
					             xx  :: Bool,
 | 
				
			||||||
             yenc :: Bool,
 | 
					             yenc :: Bool,
 | 
				
			||||||
 | 
					             a1z26 :: Bool,
 | 
				
			||||||
             rot :: Maybe Int
 | 
					             rot :: Maybe Int
 | 
				
			||||||
                   } 
 | 
					                   } 
 | 
				
			||||||
            deriving(Show, Data, Typeable) 
 | 
					            deriving(Show, Data, Typeable) 
 | 
				
			||||||
| 
						 | 
					@ -102,10 +105,12 @@ optionHandler Encode{uu=True}  = encuu
 | 
				
			||||||
optionHandler Decode{uu=True}  = decuu
 | 
					optionHandler Decode{uu=True}  = decuu
 | 
				
			||||||
optionHandler Decode{xx=True}  = decxx
 | 
					optionHandler Decode{xx=True}  = decxx
 | 
				
			||||||
optionHandler Encode{xx=True}  = encxx
 | 
					optionHandler Encode{xx=True}  = encxx
 | 
				
			||||||
optionHandler Decode{yenc=True}   = decy
 | 
					optionHandler Decode{yenc=True}  = decy
 | 
				
			||||||
optionHandler Encode{yenc=True}   = ency
 | 
					optionHandler Encode{yenc=True}  = ency
 | 
				
			||||||
optionHandler Encode{rot=Just n} = rotate n
 | 
					optionHandler Decode{a1z26=True} = decltn
 | 
				
			||||||
 | 
					optionHandler Encode{a1z26=True} = encltn
 | 
				
			||||||
optionHandler Decode{rot=Just n} = rotate n
 | 
					optionHandler Decode{rot=Just n} = rotate n
 | 
				
			||||||
 | 
					optionHandler Encode{rot=Just n} = rotate n
 | 
				
			||||||
optionHandler Decode{solve=True} = solveEnc
 | 
					optionHandler Decode{solve=True} = solveEnc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
decodeMode :: Based
 | 
					decodeMode :: Based
 | 
				
			||||||
| 
						 | 
					@ -127,6 +132,7 @@ decodeMode = Decode {
 | 
				
			||||||
           uu  = def &= help "decode UnixToUnix",
 | 
					           uu  = def &= help "decode UnixToUnix",
 | 
				
			||||||
           xx  = def &= help "decode xx, without padding",
 | 
					           xx  = def &= help "decode xx, without padding",
 | 
				
			||||||
           yenc = def &= help "decode yEncode",
 | 
					           yenc = def &= help "decode yEncode",
 | 
				
			||||||
 | 
					           a1z26 = def &= help "decode letter to number",
 | 
				
			||||||
           rot = def &= help "rotate characters by n positions",
 | 
					           rot = def &= help "rotate characters by n positions",
 | 
				
			||||||
           solve = def &= help "solve encoding"
 | 
					           solve = def &= help "solve encoding"
 | 
				
			||||||
       }  &= help "Decode chosen base" &=auto
 | 
					       }  &= help "Decode chosen base" &=auto
 | 
				
			||||||
| 
						 | 
					@ -150,6 +156,7 @@ encodeMode = Encode {
 | 
				
			||||||
           uu  = def &= help "encode UnixToUnix",
 | 
					           uu  = def &= help "encode UnixToUnix",
 | 
				
			||||||
           xx  = def &= help "encode xx, without padding",
 | 
					           xx  = def &= help "encode xx, without padding",
 | 
				
			||||||
           yenc = def &= help "encode yEncode",
 | 
					           yenc = def &= help "encode yEncode",
 | 
				
			||||||
 | 
					           a1z26 = def &= help "encode letter to number",
 | 
				
			||||||
           rot = def &= help "rotate characters by n positions" 
 | 
					           rot = def &= help "rotate characters by n positions" 
 | 
				
			||||||
      } &= help "Encode chosen base"
 | 
					      } &= help "Encode chosen base"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,6 +25,7 @@ library
 | 
				
			||||||
    Encoding.Xx
 | 
					    Encoding.Xx
 | 
				
			||||||
    Encoding.Yenc
 | 
					    Encoding.Yenc
 | 
				
			||||||
    Encoding.Rotate
 | 
					    Encoding.Rotate
 | 
				
			||||||
 | 
					    Encoding.LetterToNumber
 | 
				
			||||||
    Encoding.Solve
 | 
					    Encoding.Solve
 | 
				
			||||||
  other-modules:
 | 
					  other-modules:
 | 
				
			||||||
    -- Data.Bytes.Text.Ascii
 | 
					    -- Data.Bytes.Text.Ascii
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,59 @@
 | 
				
			||||||
 | 
					module Encoding.LetterToNumber
 | 
				
			||||||
 | 
					  ( encltn
 | 
				
			||||||
 | 
					  , decltn
 | 
				
			||||||
 | 
					  ) where 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import Data.Char (ord, chr, isAscii, isAlpha, isSpace, isDigit, toUpper, isLatin1)
 | 
				
			||||||
 | 
					import Data.List (intercalate)
 | 
				
			||||||
 | 
					import Data.ByteString (ByteString)
 | 
				
			||||||
 | 
					import qualified Data.ByteString.Char8 as BC
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					charToNumber :: Char -> ByteString
 | 
				
			||||||
 | 
					charToNumber c
 | 
				
			||||||
 | 
					  | isAlpha c = BC.pack $ show $ ord (toUpper c) - ord 'A' + 1
 | 
				
			||||||
 | 
					  | isSpace c = BC.pack "0"
 | 
				
			||||||
 | 
					  | otherwise = BC.singleton c
 | 
				
			||||||
 | 
					  -- | isAlpha c = show $ ord (toUpper c) - ord 'A' + 1
 | 
				
			||||||
 | 
					  -- | isSpace c = "0"
 | 
				
			||||||
 | 
					  -- -- | isLatin1 c = [c]
 | 
				
			||||||
 | 
					  -- | otherwise = [c]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					numberToChar :: String -> Char
 | 
				
			||||||
 | 
					numberToChar s
 | 
				
			||||||
 | 
					  | s == "0" = ' '
 | 
				
			||||||
 | 
					  | all isDigit s = chr $ read s + ord 'A' -1
 | 
				
			||||||
 | 
					  | otherwise = head s
 | 
				
			||||||
 | 
					  -- | otherwise = chr $ read s + ord 'A' - 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					encltn :: ByteString -> ByteString
 | 
				
			||||||
 | 
					encltn input = go (BC.unpack input) []
 | 
				
			||||||
 | 
					  where
 | 
				
			||||||
 | 
					    go :: String -> [ByteString] -> ByteString 
 | 
				
			||||||
 | 
					    go [] acc = BC.intercalate (BC.pack "-") (reverse acc)
 | 
				
			||||||
 | 
					    go (x:xs) acc
 | 
				
			||||||
 | 
					      -- | not (isAscii x) = go xs (BC.singleton x : acc)
 | 
				
			||||||
 | 
					      | not (isAscii x) = error "Non-ASCII character detected"
 | 
				
			||||||
 | 
					      | otherwise = go xs (charToNumber x : acc)
 | 
				
			||||||
 | 
					-- encltn input = BC.intercalate (BC.pack "-") $ map charToNumber' $  BC.unpack input
 | 
				
			||||||
 | 
					--   where
 | 
				
			||||||
 | 
					--     charToNumber' c
 | 
				
			||||||
 | 
					--       | not (isAscii c)= BC.singleton c
 | 
				
			||||||
 | 
					--       | otherwise = charToNumber c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					decltn :: ByteString -> ByteString
 | 
				
			||||||
 | 
					decltn input = BC.pack $ concatMap (processSegment .  BC.unpack) $ BC.split '-' input
 | 
				
			||||||
 | 
					  where
 | 
				
			||||||
 | 
					    processSegment :: String -> String
 | 
				
			||||||
 | 
					    processSegment s
 | 
				
			||||||
 | 
					      | all isDigit s = [numberToChar s]
 | 
				
			||||||
 | 
					      | otherwise = s
 | 
				
			||||||
 | 
					-- decltn input = BC.pack $ go $ BC.split '-' input
 | 
				
			||||||
 | 
					--   where 
 | 
				
			||||||
 | 
					--     go :: [ByteString] -> String
 | 
				
			||||||
 | 
					--     go [] = []
 | 
				
			||||||
 | 
					--     go (x:xs) = 
 | 
				
			||||||
 | 
					--       let str = BC.unpack x
 | 
				
			||||||
 | 
					--        in numberToChar str ++ go xs
 | 
				
			||||||
 | 
					       -- in case all isDigit str of
 | 
				
			||||||
 | 
					            -- True -> numberToChar str : go xs
 | 
				
			||||||
 | 
					            -- False -> str ++ go xs
 | 
				
			||||||
		Loading…
	
		Reference in New Issue