packages feed

network-byte-order 0.1.9 → 0.1.10

raw patch · 3 files changed

+76/−53 lines, 3 filesdep ~bytestringPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: bytestring

API changes (from Hackage documentation)

+ Network.ByteOrder: unsafeBufferIO :: Buffer -> Int -> (ByteString -> IO a) -> IO a

Files

ChangeLog.md view
@@ -1,5 +1,12 @@ # ChangeLog for network-byte-order +## 0.1.10++* Check bounds in shiftLastN+  [#5](https://github.com/kazu-yamamoto/network-byte-order/pull/5)+* Add unsafeBufferIO and deprecate bufferIO+  [#6](https://github.com/kazu-yamamoto/network-byte-order/pull/6)+ ## 0.1.9  * Security: `word8`, `word16`, `word32` and `word64` read beyond the
Network/ByteOrder.hs view
@@ -1,4 +1,3 @@-{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE RecordWildCards #-}  -- | Peek and poke functions for network byte order.@@ -39,6 +38,7 @@     -- * Utilities     unsafeWithByteString,     copy,+    unsafeBufferIO,     bufferIO,      -- * Class to read a buffer@@ -93,11 +93,10 @@ import Data.ByteString.Short (ShortByteString) import qualified Data.ByteString.Short.Internal as Short import Data.IORef (IORef, newIORef, readIORef, writeIORef)-import Data.Typeable import Data.Word (Word16, Word32, Word64, Word8) import Foreign.ForeignPtr (newForeignPtr_, withForeignPtr) import Foreign.Marshal.Alloc-import Foreign.Marshal.Utils (copyBytes)+import Foreign.Marshal.Utils (copyBytes, moveBytes) import Foreign.Ptr (Ptr, minusPtr, plusPtr) import Foreign.Storable (peek, poke) import System.IO.Unsafe (unsafeDupablePerformIO)@@ -282,6 +281,10 @@     return $ w0 .|. w1 .|. w2 .|. w3 .|. w4 .|. w5 .|. w6 .|. w7 {-# INLINE peek64 #-} +-- | Copying the given length of the buffer to a newly created 'ByteString'.+--+-- >>> unsafeWithByteString "abcdef" $ \ptr off -> peekByteString (ptr `plusPtr` off) 3+-- "abc" peekByteString :: Buffer -> Int -> IO ByteString peekByteString src len = create len $ \dst -> copyBytes dst src len {-# INLINE peekByteString #-}@@ -332,7 +335,7 @@ -- >>> word8 buf -- 1 ----- 'BufferOverrun' is thrown if the length is not enough.+-- t'BufferOverrun' is thrown if the length is not enough. word8 :: ByteString -> Word8 word8 bs     | B.length bs < 1 = throw BufferOverrun@@ -393,21 +396,32 @@ -- "ABC" copy :: Buffer -> ByteString -> IO Buffer copy ptr (PS fp o l) = withForeignPtr fp $ \p -> do-    copyBytes ptr (p `plusPtr` o) (fromIntegral l)+    copyBytes ptr (p `plusPtr` o) l     return $ ptr `plusPtr` l {-# INLINE copy #-}  -- | Converting the part of buffer to 'ByteString' and executing the --   action with it.+--   The 'ByteString' shares the buffer, not copies it.+--   So, it is valid only in the action and must not be returned,+--   stored or evaluated lazily after the action.+--   Use 'B.copy' to keep its contents. -- -- >>> let buf = "abcdef" :: ByteString--- >>> unsafeWithByteString buf $ \ptr _-> bufferIO ptr 2 return+-- >>> unsafeWithByteString buf $ \ptr _-> unsafeBufferIO ptr 2 (return . B.copy) -- "ab"-bufferIO :: Buffer -> Int -> (ByteString -> IO a) -> IO a-bufferIO ptr siz io = do+--+-- @since 0.1.10+unsafeBufferIO :: Buffer -> Int -> (ByteString -> IO a) -> IO a+unsafeBufferIO ptr siz io = do     fptr <- newForeignPtr_ ptr     io $ PS fptr 0 siz +-- | The same as 'unsafeBufferIO'.+bufferIO :: Buffer -> Int -> (ByteString -> IO a) -> IO a+bufferIO = unsafeBufferIO+{-# DEPRECATED bufferIO "Use unsafeBufferIO instead" #-}+ ----------------------------------------------------------------  -- | Read and write buffer.@@ -419,6 +433,8 @@     }  -- | Creating a write buffer with the given buffer.+--   The size must not be negative.+--   The buffer must be valid while the write buffer is used. newWriteBuffer :: Buffer -> BufferSize -> IO WriteBuffer newWriteBuffer buf siz =     WriteBuffer buf (buf `plusPtr` siz) <$> newIORef buf <*> newIORef buf@@ -430,7 +446,7 @@     writeIORef oldoffset start  -- | Write one byte and ff one byte.---   If buffer overrun occurs, 'BufferOverrun' is thrown.+--   If buffer overrun occurs, t'BufferOverrun' is thrown. -- -- >>> withWriteBuffer 1 $ \wbuf -> write8 wbuf 65 -- "A"@@ -443,8 +459,8 @@     writeIORef offset ptr' {-# INLINE write8 #-} --- | Write two bytes and ff one byte.---   If buffer overrun occurs, 'BufferOverrun' is thrown.+-- | Write two bytes and ff two bytes.+--   If buffer overrun occurs, t'BufferOverrun' is thrown. -- -- >>> withWriteBuffer 2 $ \wbuf -> write16 wbuf (65 * 256 + 66) -- "AB"@@ -457,8 +473,8 @@     writeIORef offset ptr' {-# INLINE write16 #-} --- | Write three bytes and ff one byte.---   If buffer overrun occurs, 'BufferOverrun' is thrown.+-- | Write three bytes and ff three bytes.+--   If buffer overrun occurs, t'BufferOverrun' is thrown. -- -- >>> withWriteBuffer 3 $ \wbuf -> write24 wbuf (65 * 256^(2 :: Int) + 66 * 256 + 67) -- "ABC"@@ -471,8 +487,8 @@     writeIORef offset ptr' {-# INLINE write24 #-} --- | Write four bytes and ff one byte.---   If buffer overrun occurs, 'BufferOverrun' is thrown.+-- | Write four bytes and ff four bytes.+--   If buffer overrun occurs, t'BufferOverrun' is thrown. -- -- >>> withWriteBuffer 4 $ \wbuf -> write32 wbuf (65 * 256^(3 :: Int) + 66 * 256^(2 :: Int) + 67 * 256 + 68) -- "ABCD"@@ -485,8 +501,8 @@     writeIORef offset ptr' {-# INLINE write32 #-} --- | Write four bytes and ff one byte.---   If buffer overrun occurs, 'BufferOverrun' is thrown.+-- | Write eight bytes and ff eight bytes.+--   If buffer overrun occurs, t'BufferOverrun' is thrown. write64 :: WriteBuffer -> Word64 -> IO () write64 WriteBuffer{..} w = do     ptr <- readIORef offset@@ -507,39 +523,33 @@ -- "ABCBCD" -- >>> withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "ABCDE" >> shiftLastN wbuf (-2) 3 >> ff wbuf 2 -- "CDEDE"+-- >>> B.drop 12 <$> withWriteBuffer 16 (\wbuf -> copyByteString wbuf "ABCD" >> shiftLastN wbuf 12 4)+-- "ABCD"+-- >>> withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "AB" >> shiftLastN wbuf (-4) 2+-- *** Exception: BufferOverrun+-- ...+-- >>> withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "AB" >> shiftLastN wbuf 1 3+-- *** Exception: BufferOverrun+-- ...+-- >>> withWriteBuffer 16 $ \wbuf -> copyByteString wbuf "AB" >> shiftLastN wbuf 1 (-1)+-- *** Exception: BufferOverrun+-- ... shiftLastN :: WriteBuffer -> Int -> Int -> IO () shiftLastN _ 0 _ = return () shiftLastN WriteBuffer{..} i len = do     ptr <- readIORef offset-    let ptr' = ptr `plusPtr` i-    when (ptr' >= limit) $ throwIO BufferOverrun-    if i < 0-        then do-            let src = ptr `plusPtr` negate len-                dst = src `plusPtr` i-            shiftLeft dst src len-            writeIORef offset ptr'-        else do-            let src = ptr `plusPtr` (-1)-                dst = ptr' `plusPtr` (-1)-            shiftRight dst src len-            writeIORef offset ptr'-  where-    -- copyBytes cannot be used for overlapped areas.-    shiftLeft :: Buffer -> Buffer -> Int -> IO ()-    shiftLeft _ _ 0 = return ()-    shiftLeft dst src n = do-        peek src >>= poke dst-        shiftLeft (dst `plusPtr` 1) (src `plusPtr` 1) (n - 1)-    shiftRight :: Buffer -> Buffer -> Int -> IO ()-    shiftRight _ _ 0 = return ()-    shiftRight dst src n = do-        peek src >>= poke dst-        shiftRight (dst `plusPtr` (-1)) (src `plusPtr` (-1)) (n - 1)+    let src = ptr `plusPtr` negate len+        dst = src `plusPtr` i+        ptr' = ptr `plusPtr` i+    when (len < 0 || src < start || dst < start || ptr' > limit) $+        throwIO BufferOverrun+    -- moveBytes handles overlapped areas.+    moveBytes dst src len+    writeIORef offset ptr' {-# INLINE shiftLastN #-}  -- | Copy the content of 'ByteString' and ff its length.---   If buffer overrun occurs, 'BufferOverrun' is thrown.+--   If buffer overrun occurs, t'BufferOverrun' is thrown. -- -- >>> withWriteBuffer 3 $ \wbuf -> copyByteString wbuf "ABC" -- "ABC"@@ -554,7 +564,7 @@ {-# INLINE copyByteString #-}  -- | Copy the content of 'ShortByteString' and ff its length.---   If buffer overrun occurs, 'BufferOverrun' is thrown.+--   If buffer overrun occurs, t'BufferOverrun' is thrown. -- -- >>> withWriteBuffer 5 $ \wbuf -> copyShortByteString wbuf "ABCEF" -- "ABCEF"@@ -617,7 +627,8 @@     -- | Reading one byte as 'Int' and ff one byte. If buffer overrun occurs, -1 is returned.     readInt8 :: a -> IO Int -    -- | Fast forward the offset pointer. The boundary is not checked.+    -- | Fast forward the offset pointer.+    --   If buffer overrun occurs, t'BufferOverrun' is thrown.     ff :: a -> Offset -> IO ()      -- | Returning the length of the remaining@@ -673,6 +684,7 @@     remainingSize WriteBuffer{..} = do         ptr <- readIORef offset         return $ limit `minusPtr` ptr+    {-# INLINE position #-}     position WriteBuffer{..} = do         ptr <- readIORef offset         return $ ptr `minusPtr` start@@ -717,11 +729,15 @@ newtype ReadBuffer = ReadBuffer WriteBuffer  -- | Creating a read buffer with the given buffer.+--   The size must not be negative.+--   The buffer must be valid while the read buffer is used. newReadBuffer :: Buffer -> BufferSize -> IO ReadBuffer newReadBuffer buf siz = ReadBuffer <$> newWriteBuffer buf siz --- | Converting 'ByteString' to 'ReadBuffer' and run the action+-- | Converting 'ByteString' to t'ReadBuffer' and run the action --   with it.+--   The t'ReadBuffer' is valid only in the action.+--   Neither it nor an action using it must be returned. withReadBuffer :: ByteString -> (ReadBuffer -> IO a) -> IO a withReadBuffer (PS fp off siz) action = withForeignPtr fp $ \ptr -> do     let buf = ptr `plusPtr` off@@ -733,7 +749,7 @@ --   Its length is specified by the 2nd argument. --   If the length is positive, the area after the current pointer is extracted and FF the length finally. --   If the length is negative, the area before the current pointer is extracted and does not FF.---   If buffer overrun occurs, 'BufferOverrun' is thrown.+--   If buffer overrun occurs, t'BufferOverrun' is thrown. -- -- >>> withReadBuffer "abcdefg" $ \rbuf -> ff rbuf 1 >> extractByteString rbuf 2 -- "bc"@@ -765,7 +781,7 @@ --   Its length is specified by the 2nd argument. --   If the length is positive, the area after the current pointer is extracted and FF the length finally. --   If the length is negative, the area before the current pointer is extracted and does not FF.---   If buffer overrun occurs, 'BufferOverrun' is thrown.+--   If buffer overrun occurs, t'BufferOverrun' is thrown. -- -- >>> withReadBuffer "abcdefg" $ \rbuf -> ff rbuf 2 >> extractShortByteString rbuf 3 -- "cde"@@ -822,7 +838,7 @@     return w32 {-# INLINE read32 #-} --- | Reading four bytes as 'Word64' and ff four bytes.+-- | Reading eight bytes as 'Word64' and ff eight bytes. read64 :: Readable a => a -> IO Word64 read64 rbuf = do     checkR rbuf 8@@ -841,6 +857,6 @@ data BufferOverrun     = -- | The buffer size is not enough       BufferOverrun-    deriving (Eq, Show, Typeable)+    deriving (Eq, Show)  instance Exception BufferOverrun
network-byte-order.cabal view
@@ -1,6 +1,6 @@-cabal-version: >=1.10+cabal-version: 2.0 name:          network-byte-order-version:       0.1.9+version:       0.1.10 license:       BSD3 license-file:  LICENSE maintainer:    kazu@iij.ad.jp@@ -22,4 +22,4 @@     ghc-options:        -Wall     build-depends:         base >=4.9 && <5,-        bytestring+        bytestring >=0.10.4