base16-bytestring-0.1.1.7/0000755000000000000000000000000007346545000013320 5ustar0000000000000000base16-bytestring-0.1.1.7/CHANGELOG.md0000755000000000000000000000024507346545000015135 0ustar0000000000000000# 0.1.1.7 * Fix some bugs in lazy decoding ([#8](https://github.com/haskell/base16-bytestring/pull/8)). # 0.1.1.6 * Changelog not recorded up to this version. base16-bytestring-0.1.1.7/Data/ByteString/0000755000000000000000000000000007346545000016263 5ustar0000000000000000base16-bytestring-0.1.1.7/Data/ByteString/Base16.hs0000644000000000000000000001462607346545000017651 0ustar0000000000000000{-# LANGUAGE BangPatterns, MagicHash #-} -- | -- Module : Data.ByteString.Base16 -- Copyright : (c) 2011 MailRank, Inc. -- -- License : BSD -- Maintainer : bos@serpentine.com -- Stability : experimental -- Portability : GHC -- -- Fast and efficient encoding and decoding of base16-encoded strings. module Data.ByteString.Base16 ( encode , decode ) where import Data.ByteString.Char8 (empty) import Data.ByteString.Internal (ByteString(..), createAndTrim', unsafeCreate) import Data.Bits (shiftL) import Foreign.ForeignPtr (ForeignPtr, withForeignPtr) import Foreign.Ptr (Ptr, minusPtr, plusPtr) import Foreign.Storable (peek, poke) import System.IO.Unsafe (unsafePerformIO) import GHC.Prim import GHC.Types import GHC.Word -- | Encode a string into base16 form. The result will always be a -- multiple of 2 bytes in length. -- -- Example: -- -- > encode "foo" == "666f6f" encode :: ByteString -> ByteString encode (PS sfp soff slen) | slen > maxBound `div` 2 = error "Data.ByteString.Base16.encode: input too long" | otherwise = unsafeCreate (slen*2) $ \dptr -> withForeignPtr sfp $ \sptr -> enc (sptr `plusPtr` soff) dptr where enc sptr = go sptr where e = sptr `plusPtr` slen go s d | s == e = return () | otherwise = do x <- peek8 s poke d (tlookup tableHi x) poke (d `plusPtr` 1) (tlookup tableLo x) go (s `plusPtr` 1) (d `plusPtr` 2) tlookup :: Addr# -> Int -> Word8 tlookup table (I# index) = W8# (indexWord8OffAddr# table index) !tableLo = "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66\ \\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66"# !tableHi = "\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\ \\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\x31\ \\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\x32\ \\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33\ \\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34\ \\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\x35\ \\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\ \\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\x37\ \\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\x38\ \\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\ \\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\ \\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\ \\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\ \\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\x64\ \\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\x65\ \\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66"# -- | Decode a string from base16 form. The first element of the -- returned tuple contains the decoded data. The second element starts -- at the first invalid base16 sequence in the original string. -- -- Examples: -- -- > decode "666f6f" == ("foo", "") -- > decode "66quux" == ("f", "quux") -- > decode "666quux" == ("f", "6quux") decode :: ByteString -> (ByteString, ByteString) decode (PS sfp soff slen) = unsafePerformIO . createAndTrim' (slen `div` 2) $ \dptr -> withForeignPtr sfp $ \sptr -> dec (sptr `plusPtr` soff) dptr where dec sptr = go sptr where e = sptr `plusPtr` if odd slen then slen - 1 else slen go s d | s == e = let len = e `minusPtr` sptr in return (0, len `div` 2, ps sfp (soff+len) (slen-len)) | otherwise = do hi <- hex `fmap` peek8 s lo <- hex `fmap` peek8 (s `plusPtr` 1) if lo == 0xff || hi == 0xff then let len = s `minusPtr` sptr in return (0, len `div` 2, ps sfp (soff+len) (slen-len)) else do poke d . fromIntegral $ lo + (hi `shiftL` 4) go (s `plusPtr` 2) (d `plusPtr` 1) hex (I# index) = W8# (indexWord8OffAddr# table index) !table = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff\ \\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ \\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"# peek8 :: Ptr Word8 -> IO Int peek8 p = fromIntegral `fmap` peek p ps :: ForeignPtr Word8 -> Int -> Int -> ByteString ps fp off len | len <= 0 = empty | otherwise = PS fp off len base16-bytestring-0.1.1.7/Data/ByteString/Base16/0000755000000000000000000000000007346545000017304 5ustar0000000000000000base16-bytestring-0.1.1.7/Data/ByteString/Base16/Lazy.hs0000644000000000000000000000421407346545000020560 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} -- | -- Module : Data.ByteString.Base16.Lazy -- Copyright : (c) 2011 MailRank, Inc. -- -- License : BSD -- Maintainer : bos@serpentine.com -- Stability : experimental -- Portability : GHC -- -- Fast and efficient encoding and decoding of base16-encoded strings. module Data.ByteString.Base16.Lazy ( encode , decode ) where import Data.Word (Word8) import qualified Data.ByteString.Base16 as B16 import qualified Data.ByteString as B import qualified Data.ByteString.Unsafe as B import qualified Data.ByteString.Lazy as BL import Data.ByteString.Lazy.Internal -- | Encode a string into base16 form. The result will always be a -- multiple of 2 bytes in length. -- -- Example: -- -- > encode "foo" == "666f6f" encode :: ByteString -> ByteString encode (Chunk c cs) = Chunk (B16.encode c) (encode cs) encode Empty = Empty -- | Decode a string from base16 form. The first element of the -- returned tuple contains the decoded data. The second element starts -- at the first invalid base16 sequence in the original string. -- -- This function operates as lazily as possible over the input chunks. -- -- Examples: -- -- > decode "666f6f" == ("foo", "") -- > decode "66quux" == ("f", "quux") -- > decode "666quux" == ("f", "6quux") decode :: ByteString -> (ByteString, ByteString) decode = go Nothing where go :: Maybe Word8 -> ByteString -> (ByteString, ByteString) go Nothing Empty = (Empty, Empty) go (Just w) Empty = (Empty, BL.singleton w) go (Just w) (Chunk c z) = go Nothing (chunk (B.pack [w, B.unsafeHead c]) (chunk (B.unsafeTail c) z)) go Nothing (Chunk c z) | len == 0 = let ~(res,tail') = go Nothing z in (chunk h res, tail') | len == 1 && isHex (B.unsafeHead t) = let ~(res,tail') = go (Just (B.unsafeHead t)) z in (chunk h res, tail') | otherwise = (chunk h Empty, chunk t z) where (h,t) = B16.decode c len = B.length t isHex :: Word8 -> Bool isHex w = (w >= 48 && w <= 57) || (w >= 97 && w <= 102) || (w >= 65 && w <= 70) base16-bytestring-0.1.1.7/LICENSE0000644000000000000000000000266607346545000014337 0ustar0000000000000000Copyright (c) 2011 MailRank, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the author nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. base16-bytestring-0.1.1.7/README.md0000755000000000000000000000261307346545000014604 0ustar0000000000000000# Fast base16 support [![Hackage version](https://img.shields.io/hackage/v/base16-bytestring.svg?label=Hackage)](https://hackage.haskell.org/package/base16-bytestring) [![Stackage version](https://www.stackage.org/package/base16-bytestring/badge/lts?label=Stackage)](https://www.stackage.org/package/base16-bytestring) [![Build Status](https://secure.travis-ci.org/haskell/base16-bytestring.svg?branch=master)](http://travis-ci.org/haskell/base16-bytestring) **Please refer to the [package description on Hackage](https://hackage.haskell.org/package/base16-bytestring#description) for more information.** This package provides a Haskell library for working with base16-encoded data quickly and efficiently, using the `ByteString` type. # Performance This library is written in pure Haskell, and it's fast: * 250 MB/sec encoding * 200 MB/sec strict decoding (per RFC 4648) * 100 MB/sec lenient decoding # Get involved! Please report bugs via the [GitHub issue tracker](http://github.com/haskell/base16-bytestring). Master [Git repository](http://github.com/haskell/base16-bytestring): * `git clone git://github.com/haskell/base16-bytestring.git` # Authors This library is written by [Bryan O'Sullivan](mailto:bos@serpentine.com). It is maintained by [Emily Pillmore](mailto:emilypi@cohomolo.gy), [Herbert Valerio Riedel](mailto:hvr@gnu.org) and [Mikhail Glushenkov](mailto:mikhail.glushenkov@gmail.com). base16-bytestring-0.1.1.7/Setup.hs0000644000000000000000000000005607346545000014755 0ustar0000000000000000import Distribution.Simple main = defaultMain base16-bytestring-0.1.1.7/base16-bytestring.cabal0000644000000000000000000000415107346545000017556 0ustar0000000000000000cabal-version: 1.12 name: base16-bytestring version: 0.1.1.7 synopsis: Fast base16 (hex) encoding and decoding for ByteStrings description: This package provides support for encoding and decoding binary data according to @base16@ (see also ) for strict (see "Data.ByteString.Base16") and lazy @ByteString@s (see "Data.ByteString.Base16.Lazy"). . See also the package which provides an uniform API providing conversion paths between more binary and textual types. homepage: http://github.com/haskell/base16-bytestring bug-reports: http://github.com/haskell/base16-bytestring/issues license: BSD3 license-file: LICENSE copyright: Copyright 2011 MailRank, Inc.; Copyright 2010-2020 Bryan O'Sullivan et al. author: Bryan O'Sullivan maintainer: Herbert Valerio Riedel , Mikhail Glushenkov , Emily Pillmore category: Data build-type: Simple extra-source-files: README.md CHANGELOG.md tested-with: GHC==8.10.1, GHC==8.8.3, GHC==8.6.5, GHC==8.4.4, GHC==8.2.2, GHC==8.0.2, GHC==7.10.3, GHC==7.8.4, GHC==7.6.3, GHC==7.4.2, GHC==7.2.2, GHC==7.0.4 library exposed-modules: Data.ByteString.Base16 Data.ByteString.Base16.Lazy build-depends: base == 4.*, bytestring >= 0.9, ghc-prim ghc-options: -Wall -funbox-strict-fields default-language: Haskell2010 source-repository head type: git location: http://github.com/haskell/base16-bytestring test-suite test type: exitcode-stdio-1.0 hs-source-dirs: tests main-is: Tests.hs default-language: Haskell2010 build-depends: base , base16-bytestring , bytestring base16-bytestring-0.1.1.7/tests/0000755000000000000000000000000007346545000014462 5ustar0000000000000000base16-bytestring-0.1.1.7/tests/Tests.hs0000644000000000000000000000533307346545000016124 0ustar0000000000000000import Data.Char (ord) import Data.Word (Word8) import qualified Data.ByteString as B import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Base16 as Base16 import qualified Data.ByteString.Base16.Lazy as Base16L -- Three-line "test framework" for Haskell. Crude, but at least it tells you -- the line number of the failure without you having to specify it. shouldBe :: (Eq a, Show a) => a -> a -> IO Bool shouldBe a b = return (a == b) infix 0 `shouldBe` c2w :: Char -> Word8 c2w = fromIntegral . ord main :: IO () main = do let hexL = map c2w "0123456789abcdef" hexU = map c2w "0123456789ABCDEF" hexUL = map c2w "0123456789ABCDEFabcdef" notHex = [c | c <- [0..255], c `notElem` hexUL] hexL2 = do a <- hexL; b <- hexL; [a,b] hexU2 = do a <- hexU; b <- hexU; [a,b] bytes = B.pack [0..255] -- Encode every byte True <- Base16.encode bytes `shouldBe` B.pack hexL2 -- Decode every valid hex pair True <- Base16.decode (B.pack hexL2) `shouldBe` (bytes, B.empty) True <- Base16.decode (B.pack hexU2) `shouldBe` (bytes, B.empty) -- Decode every invalid byte paired with a correct byte let bads1 = [B.pack [a,b] | a <- notHex, b <- hexUL] let bads2 = [B.pack [a,b] | a <- hexUL, b <- notHex] True <- map Base16.decode bads1 `shouldBe` map (\s -> (B.empty, s)) bads1 True <- map Base16.decode bads2 `shouldBe` map (\s -> (B.empty, s)) bads2 -- Like above, but start with a correct byte let correctHex = B.pack [97,98] correctBytes = B.pack [171] True <- map (Base16.decode . (correctHex `B.append`)) bads1 `shouldBe` map (\s -> (correctBytes, s)) bads1 True <- map (Base16.decode . (correctHex `B.append`)) bads2 `shouldBe` map (\s -> (correctBytes, s)) bads2 -- Like above, but end with a correct byte True <- map (Base16.decode . (`B.append` correctHex)) bads1 `shouldBe` map (\s -> (B.empty, s `B.append` correctHex)) bads1 True <- map (Base16.decode . (`B.append` correctHex)) bads2 `shouldBe` map (\s -> (B.empty, s `B.append` correctHex)) bads2 -- Lazy decoding also works with odd length chunks let encodedLazy = BL.fromChunks $ map (B.pack . map c2w) ["614","239","6","142","39"] True <- Base16L.decode encodedLazy `shouldBe` (BL.pack . map c2w $ "aB9aB9",BL.empty) -- Lazy decoding is lazy on success let encodedLazy = BL.iterate id 48 True <- (BL.unpack . BL.take 8 . fst . Base16L.decode $ encodedLazy) `shouldBe` [0,0,0,0,0,0,0,0] -- Lazy decoding is lazy on failure let encodedLazy = BL.iterate id 47 True <- (BL.unpack . BL.take 8 . fst . Base16L.decode $ encodedLazy) `shouldBe` [] return ()