
I'm toying a bit with Haskell and wondering what's the best way to implement bit fiddling. Most of my applications involve serializing and deserializing small blobs (IP packets, for instance), and after browsing the GHC library documentation, I'm not sure which appraoch I should use. That's why I'd appreciate pointers to sample code. Portability beyond GHC is not required. It's not necessary to update blobs, only to parse them, and serialize new ones. And in the beginning, I'd like to write the boilerplate manually. 8-)

Probably you have seen this already, but I thought I'd mention it on the off-chance you missed it: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data.Bits.html http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data.Word.html Probably you'll want to think about an IOUArray of Word8, or something similar. Its unclear to me exactly what you mean by "bit fiddling", but perhaps this addresses your question. Florian Weimer wrote:
I'm toying a bit with Haskell and wondering what's the best way to implement bit fiddling. Most of my applications involve serializing and deserializing small blobs (IP packets, for instance), and after browsing the GHC library documentation, I'm not sure which appraoch I should use. That's why I'd appreciate pointers to sample code.
Portability beyond GHC is not required. It's not necessary to update blobs, only to parse them, and serialize new ones. And in the beginning, I'd like to write the boilerplate manually. 8-) _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

* robert dockins:
Probably you have seen this already, but I thought I'd mention it on the off-chance you missed it:
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data.Bits.html http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data.Word.html
Probably you'll want to think about an IOUArray of Word8, or something similar.
IOUArray looks indeed interesting, thanks. How can I pass such objects (in particular of type IOUArray Int Word8) to a C routine, preferably as a void */size_t combination?

If you want C compatibility, you need http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data.Array.Storab... which is similar. You then use the "withStorableArray" to call out to your C functions. Florian Weimer wrote:
* robert dockins:
Probably you have seen this already, but I thought I'd mention it on the off-chance you missed it:
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data.Bits.html http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data.Word.html
Probably you'll want to think about an IOUArray of Word8, or something similar.
IOUArray looks indeed interesting, thanks. How can I pass such objects (in particular of type IOUArray Int Word8) to a C routine, preferably as a void */size_t combination?

* robert dockins:
If you want C compatibility, you need
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data.Array.Storab...
which is similar. You then use the "withStorableArray" to call out to your C functions.
Looks exactly like what I need. I'll give it a try and come back if I can't figure out how things fit together. Thanks again.

Hi, Florian Weimer wrote:
I'm toying a bit with Haskell and wondering what's the best way to implement bit fiddling. Most of my applications involve serializing and deserializing small blobs (IP packets, for instance), and after browsing the GHC library documentation, I'm not sure which appraoch I should use. That's why I'd appreciate pointers to sample code.
In the protocol stack in House [1], I have used parsing/unparsing combinators to (de)serialize network packets. This allows the code to be simple, pure and modular, even though you are dealing with data down to the bit level. The way things work at the moment, the network device driver returns incoming packets as unboxed arrays of bytes (values of type UArray Int Word8), and the parsing combinators maintain these along with position information down to the bit level, allowing you to parse input bit by bit. The process is reversed in the unparsing combinators. (What would be nice to have for this is efficient library functions for manipulation of bit vectors of arbitrary dynamic size...) The source code is available on the web, see for example modules Net.Ethernet, Net.ARP, Net.ICMP, Net.IPv4: http://www.cse.ogi.edu/~hallgren/House/kernel/#Net Note though that this is work in progress, there is not much documentation, and the code for several of the protocols is incomplete. -- Thomas H [1] http://www.cse.ogi.edu/~hallgren/House/

Hello Florian, Wednesday, May 18, 2005, 12:44:14 AM, you wrote: FW> I'm toying a bit with Haskell and wondering what's the best way to FW> implement bit fiddling. Most of my applications involve serializing FW> and deserializing small blobs (IP packets, for instance), and after FW> browsing the GHC library documentation, I'm not sure which appraoch I FW> should use. That's why I'd appreciate pointers to sample code. you can find Binary.hs in the GHC 6.4 sources (sources of compiler itself) and close to this ByteStream library on my page http://freearc.narod.ru with help of my library all you need to deserialize data is just: (sign::Word32, block_type::Int16, crc::Word64) <- ByteStream.readMemory buf bufsize there is no such easy serialization, i just don't need it because my program uses only low-level functions, but i can add for you something like: (buf,bufsize) <- ByteStream.writeMemory (sign, block_type, crc) -- Best regards, Bulat mailto:bulatz@HotPOP.com

Hello, There is also Hal Daume III's port of Binary from nhc98 which has some bit-fiddling features: http://www.n-heptane.com/nhlab/ Not sure if it is better than any of the other suggestions, but I thought I would mention it just in case. (note: the debian directory in there is unofficial for now, possibly broken, and the source / binary package names it generates will be changing soon. But the cabal stuff is reasonably up-to-date.) Jeremy Shaw. At Wed, 18 May 2005 12:05:02 +0400, Bulat Ziganshin wrote:
Hello Florian,
Wednesday, May 18, 2005, 12:44:14 AM, you wrote:
FW> I'm toying a bit with Haskell and wondering what's the best way to FW> implement bit fiddling. Most of my applications involve serializing FW> and deserializing small blobs (IP packets, for instance), and after FW> browsing the GHC library documentation, I'm not sure which appraoch I FW> should use. That's why I'd appreciate pointers to sample code.
you can find Binary.hs in the GHC 6.4 sources (sources of compiler itself) and close to this ByteStream library on my page http://freearc.narod.ru
with help of my library all you need to deserialize data is just:
(sign::Word32, block_type::Int16, crc::Word64) <- ByteStream.readMemory buf bufsize
there is no such easy serialization, i just don't need it because my program uses only low-level functions, but i can add for you something like:
(buf,bufsize) <- ByteStream.writeMemory (sign, block_type, crc)
-- Best regards, Bulat mailto:bulatz@HotPOP.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello Jeremy, Wednesday, May 18, 2005, 11:06:23 PM, you wrote: JS> http://www.n-heptane.com/nhlab/ it's the same module i say about :) ================ License I did not write this module, and I am not entirely clear on what exactly the license is. Here is the license reproduced from the header of Binary.hs -- -- (c) The University of Glasgow 2002 -- -- Binary I/O library, with special tweaks for GHC ================
you can find Binary.hs in the GHC 6.4 sources (sources of compiler itself)
-- Best regards, Bulat mailto:bulatz@HotPOP.com
participants (5)
-
Bulat Ziganshin
-
Florian Weimer
-
Jeremy Shaw
-
robert dockins
-
Thomas Hallgren