From 13dc0f086d3d8f217a2818a429ac117c97397e9b Mon Sep 17 00:00:00 2001 From: Sam T Date: Thu, 26 Sep 2013 22:39:29 +0400 Subject: Make decode case insensitive. > 6. Base 32 Encoding > > The Base 32 encoding is designed to represent arbitrary sequences of > octets in a form that _needs to be case insensitive_ but that need not > be human readable. --- src/Data/ByteString/Base32.hs | 12 +++++++----- tests/Data/ByteString/Base32Spec.hs | 8 ++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Data/ByteString/Base32.hs b/src/Data/ByteString/Base32.hs index 5dc025c..a5835ad 100644 --- a/src/Data/ByteString/Base32.hs +++ b/src/Data/ByteString/Base32.hs @@ -227,11 +227,13 @@ pack5 !tbl bs @ (PS fptr off sz) = decW5 :: Word8 -> Word5 decW5 !x - | x < 50 {- c2w '2' -} = invIx - | x <= 55 {- c2w '7' -} = x - 24 - | x < 65 {- c2w 'A' -} = invIx - | x <= 90 {- c2w 'Z' -} = x - 65 - | otherwise = invIx + | x < 50 {- c2w '2' -} = invIx + | x <= 55 {- c2w '7' -} = x - 24 {- c2w '2' - 26 -} + | x < 65 {- c2w 'A' -} = invIx + | x <= 90 {- c2w 'Z' -} = x - 65 {- c2w 'A' -} + | x < 97 {- c2w 'a' -} = invIx + | x <= 122 {- c2w 'z' -} = x - 97 {- c2w 'a' -} + | otherwise = invIx {-# INLINE decW5 #-} decTable :: ForeignPtr Word8 diff --git a/tests/Data/ByteString/Base32Spec.hs b/tests/Data/ByteString/Base32Spec.hs index ff3e13c..60e340c 100644 --- a/tests/Data/ByteString/Base32Spec.hs +++ b/tests/Data/ByteString/Base32Spec.hs @@ -4,8 +4,9 @@ module Data.ByteString.Base32Spec (spec) where import Control.Applicative import Control.Exception import Data.ByteString as BS -import Data.ByteString.Internal as BS +import Data.ByteString.Char8 as BC import Data.ByteString.Base32 as Base32 +import Data.Char import Test.Hspec import Test.QuickCheck @@ -29,7 +30,7 @@ spec = do (BS.length (encode bs) `rem` 8) `shouldBe` 0 it "padding less than" $ property $ \bs -> - count (c2w '=') bs `shouldSatisfy` (< 8) + BC.count '=' bs `shouldSatisfy` (< 8) describe "decode" $ do it "conform RFC examples" $ do @@ -44,6 +45,9 @@ spec = do it "inverse for encode" $ property $ \bs -> decode (encode bs) == bs + it "case insensitive" $ property $ \bs -> + decode (BC.map toLower (encode bs)) == bs + it "fail gracefully if encoded data contains non alphabet chars" $ do evaluate (decode "0=======") `shouldThrow` anyErrorCall evaluate (decode "AAAAAAAA0=======") `shouldThrow` anyErrorCall -- cgit v1.2.3