diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-09-27 01:22:07 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-09-27 01:22:07 +0400 |
commit | 034f77c6a7465d50fe284e84b7621c9541fadfdc (patch) | |
tree | 786e78e3df9e72181bca72e488f88ec4d87f5f33 | |
parent | e50d09be2dc2767429cb833fe4b48885607c654b (diff) |
Implement base32hex codec
-rw-r--r-- | src/Data/ByteString/Base32/Hex.hs | 28 |
1 files 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 @@ | |||
12 | -- Data.ByteString.Base32.Hex as Base32Hex@ to avoid name clashes | 12 | -- Data.ByteString.Base32.Hex as Base32Hex@ to avoid name clashes |
13 | -- with @Data.ByteString.Base32@. | 13 | -- with @Data.ByteString.Base32@. |
14 | -- | 14 | -- |
15 | {-# LANGUAGE BangPatterns #-} | ||
15 | module Data.ByteString.Base32.Hex | 16 | module Data.ByteString.Base32.Hex |
16 | ( encode | 17 | ( encode |
17 | , decode | 18 | , decode |
@@ -19,15 +20,38 @@ module Data.ByteString.Base32.Hex | |||
19 | ) where | 20 | ) where |
20 | 21 | ||
21 | import Data.ByteString as BS | 22 | import Data.ByteString as BS |
23 | import Data.ByteString.Base32.Internal | ||
24 | import Data.List as L | ||
22 | 25 | ||
23 | 26 | ||
27 | encW5 :: Word5 -> Word8 | ||
28 | encW5 !x | ||
29 | | x <= 9 = 48 {- c2w '0' -} + x | ||
30 | | otherwise = 55 {- c2w 'A' - 10 -} + x | ||
31 | |||
32 | encTable :: EncTable | ||
33 | encTable = BS.pack $ L.map encW5 [0..31] | ||
34 | |||
24 | -- | Encode a bytestring into base32hex form. | 35 | -- | Encode a bytestring into base32hex form. |
25 | encode :: ByteString -> ByteString | 36 | encode :: ByteString -> ByteString |
26 | encode = undefined | 37 | encode = unpack5 encTable |
38 | |||
39 | decW5 :: Word8 -> Word5 | ||
40 | decW5 !x | ||
41 | | x < 48 {- c2w '0' -} = invIx | ||
42 | | x <= 57 {- c2w '9' -} = x - 48 {- c2w '0' -} | ||
43 | | x < 65 {- c2w 'A' -} = invIx | ||
44 | | x <= 86 {- c2w 'V' -} = x - 55 {- c2w 'A' + 10 -} | ||
45 | | x < 97 {- c2w 'a' -} = invIx | ||
46 | | x <= 118 {- c2w 'v' -} = x - 87 {- c2w 'a' + 10 -} | ||
47 | | otherwise = invIx | ||
48 | |||
49 | decTable :: DecTable | ||
50 | decTable = BS.pack $ L.map decW5 [minBound .. maxBound] | ||
27 | 51 | ||
28 | -- | Decode a base32hex encoded bytestring. | 52 | -- | Decode a base32hex encoded bytestring. |
29 | decode :: ByteString -> ByteString | 53 | decode :: ByteString -> ByteString |
30 | decode = undefined | 54 | decode = pack5 decTable |
31 | 55 | ||
32 | decodeLenient :: ByteString -> ByteString | 56 | decodeLenient :: ByteString -> ByteString |
33 | decodeLenient = undefined \ No newline at end of file | 57 | decodeLenient = undefined \ No newline at end of file |