Ptr Word8 to [Word8]

97 views Asked by At

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?

1

There are 1 answers

0
melpomene On BEST ANSWER

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