diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-09-26 23:01:25 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-09-26 23:01:25 +0400 |
commit | 4a79ab996010484c38836db33e40d05e81126068 (patch) | |
tree | a3a3cc5d9d6410c483902ae8cacdd166f172ec1e /src/Data | |
parent | 6769babed0d4a027ca4d890c5a2a1f74df59351e (diff) |
Refactor pack5 function
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/ByteString/Base32.hs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/Data/ByteString/Base32.hs b/src/Data/ByteString/Base32.hs index a5835ad..d139156 100644 --- a/src/Data/ByteString/Base32.hs +++ b/src/Data/ByteString/Base32.hs | |||
@@ -152,13 +152,11 @@ encode bs = | |||
152 | -- Decoding | 152 | -- Decoding |
153 | -----------------------------------------------------------------------} | 153 | -----------------------------------------------------------------------} |
154 | 154 | ||
155 | type DecTable = Ptr Word5 | ||
156 | |||
157 | invIx :: Word5 | 155 | invIx :: Word5 |
158 | invIx = 255 | 156 | invIx = 255 |
159 | 157 | ||
160 | pack5 :: DecTable -> ByteString -> ByteString | 158 | pack5Ptr :: Ptr Word5 -> ByteString -> ByteString |
161 | pack5 !tbl bs @ (PS fptr off sz) = | 159 | pack5Ptr !tbl bs @ (PS fptr off sz) = |
162 | unsafePerformIO $ do | 160 | unsafePerformIO $ do |
163 | let packedSize = dstSize $ BS.length bs | 161 | let packedSize = dstSize $ BS.length bs |
164 | BS.createAndTrim packedSize $ \ dst -> do | 162 | BS.createAndTrim packedSize $ \ dst -> do |
@@ -225,6 +223,17 @@ pack5 !tbl bs @ (PS fptr off sz) = | |||
225 | .|. fromIntegral (lookupTable (fromIntegral w8))) | 223 | .|. fromIntegral (lookupTable (fromIntegral w8))) |
226 | (un_cnt + 5) | 224 | (un_cnt + 5) |
227 | 225 | ||
226 | type DecTable = ByteString | ||
227 | |||
228 | pack5 :: DecTable -> ByteString -> ByteString | ||
229 | pack5 (PS fptr off len) bs | ||
230 | | len /= 256 | ||
231 | = error $ "base32: pack5: invalid lookup table size " ++ show len | ||
232 | | otherwise = | ||
233 | unsafePerformIO $ do | ||
234 | withForeignPtr fptr $ \ptr -> | ||
235 | return $ pack5Ptr (ptr `advancePtr` off) bs | ||
236 | |||
228 | decW5 :: Word8 -> Word5 | 237 | decW5 :: Word8 -> Word5 |
229 | decW5 !x | 238 | decW5 !x |
230 | | x < 50 {- c2w '2' -} = invIx | 239 | | x < 50 {- c2w '2' -} = invIx |
@@ -236,15 +245,12 @@ decW5 !x | |||
236 | | otherwise = invIx | 245 | | otherwise = invIx |
237 | {-# INLINE decW5 #-} | 246 | {-# INLINE decW5 #-} |
238 | 247 | ||
239 | decTable :: ForeignPtr Word8 | 248 | decTable :: ByteString |
240 | PS decTable _ _ = BS.pack $ fmap decW5 [minBound .. maxBound] | 249 | decTable = BS.pack $ fmap decW5 [minBound .. maxBound] |
241 | 250 | ||
242 | -- | Decode a base32 encoded bytestring. | 251 | -- | Decode a base32 encoded bytestring. |
243 | decode :: ByteString -> ByteString | 252 | decode :: ByteString -> ByteString |
244 | decode bs = | 253 | decode = pack5 decTable |
245 | unsafePerformIO $ do | ||
246 | withForeignPtr decTable $ \tbl -> | ||
247 | return $ pack5 tbl bs | ||
248 | 254 | ||
249 | {----------------------------------------------------------------------- | 255 | {----------------------------------------------------------------------- |
250 | -- Lenient Decoding | 256 | -- Lenient Decoding |