Compare commits

...

3 Commits

Author SHA1 Message Date
Stefan Friese 56f75e2783 added makefile 2024-12-10 23:43:52 +01:00
Stefan Friese 85b57ef9aa added unary encoding 2024-11-19 18:24:36 +01:00
Stefan Friese e39ac84573 added changelog 2024-09-25 23:59:41 +02:00
6 changed files with 133 additions and 0 deletions

View File

@ -1,5 +1,13 @@
Revision history for based
0.4.5.0 -- 2024-09-25
Added tap code
Removed some bugs of the solve functions
Added bindings for morse code, bug fix
Simplified functions, ignore non ascii characters
Added test for rotate function and yencode/decode
0.4.4.0 -- 2024-06-09
Added tests for every encode function included

53
Makefile Normal file
View File

@ -0,0 +1,53 @@
.DEFAULT_GOAL := build
PACKAGE_NAME := based
CABAL := cabal
BUILD_FLAGS := --builddir=dist-newstyle
BUILD_DEV_FLAGS := --enable-tests --enable-benchmarks
# STATIC_FLAGS := --ghc-options="-optl-static"
STATIC_FLAGS := --enable-executable-static -O2
STRIP := strip
STRIP_FLAGS := --strip-all
INSTALL_DIR := /usr/local/bin
INSTALL_FLAGS := --overwrite-policy=always
BIN_NAME := $(PACKAGE_NAME)
.PHONY: test
build:
$(CABAL) v2-build $(BUILD_FLAGS)
static-build:
$(CABAL) v2-build $(BUILD_FLAGS) $(STATIC_FLAGS)
dev-build:
$(CABAL) v2-build $(BUILD_FLAGS) $(BUILD_DEV_FLAGS)
strip:
$(STRIP) $(STRIP_FLAGS) $(realpath $(which $(PACKAGE_NAME)))
user-install:
$(CABAL) v2-install $(BUILD_FLAGS) $(INSTALL_FLAGS)
$(STRIP) $(STRIP_FLAGS) $(realpath $(which $(PACKAGE_NAME)))
install: user-install
install -m 755 $(shell cabal v2-exec which $(BIN_NAME)) $(INSTALL_DIR)/$(BIN_NAME)
$(STRIP) $(STRIP_FLAGS) $(realpath $(which $(PACKAGE_NAME)))
echo "$(BIN_NAME) installed to $(INSTALL_DIR)"
static-install: static-build user-install
install -m 755 $(shell cabal v2-exec which $(BIN_NAME)) $(INSTALL_DIR)/$(BIN_NAME)
$(STRIP) $(STRIP_FLAGS) $(realpath $(which $(PACKAGE_NAME)))
echo "$(BIN_NAME) installed to $(INSTALL_DIR)"
test:
$(CABAL) v2-test $(BUILD_FLAGS)
clean-artifacts:
$(CABAL) v2-clean
clean: clean-artifacts
rm -f $(INSTALL_DIR)/$(BIN_NAME) \
&& echo "$(BIN_NAME) removed from $(INSTALL_DIR)" \
|| echo "$(BIN_NAME) cannot be found in $(INSTALL_DIR)"

View File

@ -5,6 +5,7 @@
module Main where
import System.Console.CmdArgs
import qualified Data.ByteString as B
import Encoding.Unary (encunary, decunary)
import Encoding.Base2 (enc2, dec2)
import Encoding.Base8 (enc8, dec8)
import Encoding.Base10 (enc10, dec10)
@ -42,6 +43,7 @@ data Based = Decode {
b10 :: Bool,
b8 :: Bool,
b2 :: Bool,
unary :: Bool,
qp :: Bool,
uu :: Bool,
xx :: Bool,
@ -66,6 +68,7 @@ data Based = Decode {
b10 :: Bool,
b8 :: Bool,
b2 :: Bool,
unary :: Bool,
qp :: Bool,
uu :: Bool,
xx :: Bool,
@ -105,6 +108,8 @@ optionHandler Decode{b8=True} = dec8
optionHandler Encode{b8=True} = enc8
optionHandler Decode{b2=True} = dec2
optionHandler Encode{b2=True} = enc2
optionHandler Decode{unary=True} = decunary
optionHandler Encode{unary=True} = encunary
optionHandler Decode{qp=True} = decqp
optionHandler Encode{qp=True} = encqp
optionHandler Encode{uu=True} = encuu
@ -138,6 +143,7 @@ decodeMode = Decode {
b10 = def &= help "decode decimal",
b8 = def &= help "decode octal",
b2 = def &= help "decode base2",
unary = def &= help "decode unary (8 bit) a.k.a. Chuck Norris",
qp = def &= help "decode quoted-printable",
uu = def &= help "decode UnixToUnix",
xx = def &= help "decode xx, without padding",
@ -164,6 +170,7 @@ encodeMode = Encode {
b10 = def &= help "encode decimal",
b8 = def &= help "encode octal",
b2 = def &= help "encode base2",
unary = def &= help "encode unary (8 bit) a.k.a. Chuck Norris",
qp = def &= help "encode quoted-printable",
uu = def &= help "encode UnixToUnix",
xx = def &= help "encode xx, without padding",

View File

@ -8,6 +8,7 @@ author: Stefan Friese
library
hs-source-dirs: src
exposed-modules:
Encoding.Unary
Encoding.Base2
Encoding.Base8
Encoding.Base10

View File

@ -31,6 +31,7 @@
module Encoding.Base2
( enc2
, dec2
, binaryToChar
) where
import Data.Char (ord, chr, digitToInt, intToDigit)

63
src/Encoding/Unary.hs Normal file
View File

@ -0,0 +1,63 @@
module Encoding.Unary
( encunary
, decunary
) where
import Data.ByteString (ByteString)
import Data.List (group)
import qualified Data.ByteString.Char8 as BC
import Encoding.Base2 (dec2, enc2, binaryToChar)
import Debug.Trace (trace)
encodeChuckNorris :: ByteString -> ByteString
encodeChuckNorris binary =
let cleanBinary = BC.filter (/= ' ') binary
in BC.unwords . concatMap encodeRunLength . group $ BC.unpack cleanBinary
where
encodeRunLength :: String -> [ByteString]
encodeRunLength xs@(x:_) = [prefix x, BC.pack (replicate (length xs) '0')]
encodeRunLength [] = []
prefix '0' = BC.pack "00"
prefix '1' = BC.pack "0"
prefix c = error $ "Invalid binary character encoding" ++ show c
pairs :: [a] -> [(a, a)]
pairs (x:y:rest) = (x, y) : pairs rest
pairs _ = []
encunary :: ByteString -> ByteString
encunary input = encodeChuckNorris $ enc2 input
decodeChuckNorris :: ByteString -> ByteString
decodeChuckNorris encoded = BC.pack . concatMap (BC.unpack . decodeRunLength) . pairs . BC.words $ encoded
where
decodeRunLength :: (ByteString, ByteString) -> ByteString
decodeRunLength (prefix, zeros)
| prefix == BC.pack "00" = BC.replicate (BC.length zeros) '0'
| prefix == BC.pack "0" = BC.replicate (BC.length zeros) '1'
| otherwise = error "Invalid Unary encoding"
-- pairs (x:y:rest) = (x, y) : pairs rest
-- pairs _ = []
-- decodeRunLength ("00", zeros) = replicate (BC.length zeros) '0'
-- decodeRunLength ("0", zeros) = replicate (BC.length zeros) '1'
-- decodeRunLength _ = error "Invalid Unary encoding"
decunary :: ByteString -> ByteString
decunary encoded =
let binaryStr = decodeChuckNorris encoded
in BC.pack $ map binaryToChar (chunkBinaryString binaryStr)
where
chunkBinaryString :: ByteString -> [ByteString]
chunkBinaryString bs
| BC.null bs = []
| otherwise = let (chunk, rest) = BC.splitAt 8 bs
in chunk : chunkBinaryString rest
-- encunary input =
-- let binaryStr = enc2 input
-- in trace (show binaryStr) $ encodeChuckNorris binaryStr
-- decunary :: ByteString -> ByteString
-- decunary encoded = dec2 $ decodeChuckNorris encoded