From 034f77c6a7465d50fe284e84b7621c9541fadfdc Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Fri, 27 Sep 2013 01:22:07 +0400 Subject: Implement base32hex codec --- src/Data/ByteString/Base32/Hex.hs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Data/ByteString/Base32/Hex.hs b/src/Data/ByteString/Base32/Hex.hs index dad222f..762d471 100644 --- a/src/Data/ByteString/Base32/Hex.hs +++ b/src/Data/ByteString/Base32/Hex.hs @@ -12,6 +12,7 @@ -- Data.ByteString.Base32.Hex as Base32Hex@ to avoid name clashes -- with @Data.ByteString.Base32@. -- +{-# LANGUAGE BangPatterns #-} module Data.ByteString.Base32.Hex ( encode , decode @@ -19,15 +20,38 @@ module Data.ByteString.Base32.Hex ) where import Data.ByteString as BS +import Data.ByteString.Base32.Internal +import Data.List as L +encW5 :: Word5 -> Word8 +encW5 !x + | x <= 9 = 48 {- c2w '0' -} + x + | otherwise = 55 {- c2w 'A' - 10 -} + x + +encTable :: EncTable +encTable = BS.pack $ L.map encW5 [0..31] + -- | Encode a bytestring into base32hex form. encode :: ByteString -> ByteString -encode = undefined +encode = unpack5 encTable + +decW5 :: Word8 -> Word5 +decW5 !x + | x < 48 {- c2w '0' -} = invIx + | x <= 57 {- c2w '9' -} = x - 48 {- c2w '0' -} + | x < 65 {- c2w 'A' -} = invIx + | x <= 86 {- c2w 'V' -} = x - 55 {- c2w 'A' + 10 -} + | x < 97 {- c2w 'a' -} = invIx + | x <= 118 {- c2w 'v' -} = x - 87 {- c2w 'a' + 10 -} + | otherwise = invIx + +decTable :: DecTable +decTable = BS.pack $ L.map decW5 [minBound .. maxBound] -- | Decode a base32hex encoded bytestring. decode :: ByteString -> ByteString -decode = undefined +decode = pack5 decTable decodeLenient :: ByteString -> ByteString decodeLenient = undefined \ No newline at end of file -- cgit v1.2.3