tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: A Library for Converting Data to and from C Structs for Lua



On Sat, Nov 23, 2013 at 11:46 AM, Lourival Vieira Neto
<lourival.neto%gmail.com@localhost> wrote:
> Hi James,
>
> On Sat, Nov 23, 2013 at 1:22 AM, James K. Lowden
> <jklowden%schemamania.org@localhost> wrote:
>> On Mon, 18 Nov 2013 09:07:52 +0100
>> Marc Balmer <marc%msys.ch@localhost> wrote:
>>
>>> After discussion we lneto@ and others we realised that there are
>>> several such libraries around, and that I as well as lneto@ wrote
>>> one.  SO we decided to merge our works and have a library that allows
>>> to encode/decode structured binary data in one call (my work) and that
>>> allows to access individual fields as well (lneto@'s work).
>>
>> How do you deal with the usual issues of alignment and endianism?
>
> I'm working on this approach:
>
> d = data.new{0xF0, 0xFF, 0x00} -- creates a new data object with 3 bytes.
> d:layout{
>   x = { __offset = 0, __length = 3 },
>   y = { __offset = 8, __length = 16, __endian = 'net' },
>   z = { __offset = 0, __step = 9 }
> }
>
> d.x -- returns the 3 most significant bits from d (that is, 7)
> d.y -- returns 16 bits counting from bit-8 most significant.
>       -- in this case, these 2 bytes are converted using ntohs(3),
> that is 0xFF00.
> d.z[1] -- returns the 9 most significant bits from d (that is, 0x1E1).

s/0x1E1/0x1E1 if the platform is big endian or 0x1FF if little/

Note, in this case there is no conversion (default __endian is
'host'). In non-aligned fields, it gets the lesser aligned data amount
that suffices, then masks it to access only the specified length. In
this case, it gets the 16 most significant bits (that could be 0xF0FF
or 0xFFF0, depending on endianness) and masks them to access only the
9 most significant bits (that is, 0x1E1 or 0x1FF).

Regards,
-- 
Lourival Vieira Neto


Home | Main Index | Thread Index | Old Index