0

I'm using the library Network.Pcap, and it has the function toBS

toBS :: (PktHdr, Ptr Word8) -> IO (PktHdr, B.ByteString)
toBS (hdr, ptr) = do
    let len = hdrCaptureLength hdr
    s <- B.create (fromIntegral len) $ \p -> B.memcpy p ptr (fromIntegral len)
    return (hdr, s)

Right now I am unpacking the ByteString which gives me the [Word8] I want. I' m looking for a way to avoid having to unpack the ByteString, and get the [Word8] directly.

(1) Does this function exist? (2) If not, could I get some advice on how to proceed?

  • In the end, do you want some function `g :: IO (PktHdr, B.ByteString) -> IO (PktHdr, [Word8])`? – chepner Jan 07 '17 at 21:37

1 Answers1

1

I'm not familiar with those libraries, but can't you just do this?

import Foreign.Marshal.Array

toByteList :: PktHdr -> Ptr Word8 -> IO [Word8]
toByteList hdr ptr = peekArray (fromIntegral (hdrCaptureLength hdr)) ptr
melpomene
  • 84,125
  • 8
  • 85
  • 148