RE: FFI and endian conversion

  • From: William Adams <william_a_adams@xxxxxxx>
  • To: <luajit@xxxxxxxxxxxxx>
  • Date: Wed, 25 Jul 2012 12:41:58 +0000

I wrote a bit on serialization with Lua: 
http://williamaadams.wordpress.com/2012/05/02/serialization-105-the-lazy-programmer-shares-data/

This is on the lines of "pack it by hand", not the most optimal
but fairly complete.

In particular, there's the BinaryStream 'object', which takes
care of mixed endian issues.  Sometimes the stream is in big-endian,
sometimes it's not, and sometimes it's mixed, depending on your situation.

You can find actual code in LAPHLibs: 

https://github.com/Wiladams/LAPHLibs

Which also includes things like CRC32 and MD5, which you might find
useful for network programming tasks.

-- William

________________________________
> Date: Wed, 25 Jul 2012 12:18:46 +0100 
> Subject: Re: FFI and endian conversion 
> From: justin@xxxxxxxxxxxxxxxxxxxxx 
> To: luajit@xxxxxxxxxxxxx 
> 
> 
> On 25 Jul 2012 11:44, "Mike Pall" 
> <mike-1207@xxxxxxxxxx<mailto:mike-1207@xxxxxxxxxx>> wrote: 
> 
> > It's a low-level API, so the magic is limited. But it's easy 
> > enough to add this functionality yourself: 
> > 
> > local ffi = require("ffi") 
> > local bit = require("bit") 
> > 
> > local function n32(x) return x end 
> > local n16 = n32 
> > if ffi.abi("le") then 
> > local shr = bit.rshift 
> > n32 = bit.bswap 
> > function n16(x) return shr(n32(x), 16) end 
> > end 
> > 
> > Then use n32(foo.field) or n16(foo.field) to access fields in 
> > network byte order (aka ntohl or ntohs). 
> > 
> 
> I have been using ffi metatypes to add these accessors so you do not 
> need to explicitly convert. Eg so x.sin_port is in network byte order 
> but you can use x.port to get the host byte order using the metatable. 
> 
> You could also hide any alignment issues using metatables. 
> 
> Justin                                          

Other related posts: