[luatex] fio library byte order
Henri Menke
henrimenke at gmail.com
Mon Jun 22 02:40:18 CEST 2020
On 20/06/20, 13:26, luigi scarso wrote:
> On Fri, Jun 19, 2020 at 11:17 PM Reinhard Kotucha <reinhard.kotucha at web.de>
> wrote:
>
> > Hi,
> > it's nice that with the fio library LuaTeX can now process binary
> > files. What I'm missing is the ability to specify the byte order
> > (little vs. big endian).
> >
> >
> I don't knwo if it can help,
> but if you can use ffi I think you can know the endianness of the current
> platform with with this code:
> \directlua{
> ffi.cdef [[
> union endianness {
> int32_t i;
> char c[4];
> }
> ]]
> local x = ffi.new('union endianness x',{i=1})
> if(x.c[0]==1) then
> print "little endian"
> else
> print "big endian"
> end
> }
> \end
Type punning is undefined behaviour. When you read from a different
union member than you wrote to, there is no guarantee that you are
actually reading the same memory. It is much safer to just do a memcpy
to an array of char of the correct length.
local x = ffi.new('int[1]',1)
local size = ffi.sizeof(x, 0)
local y = ffi.new('char[' .. size .. ']')
ffi.copy(y, x, ffi.sizeof(x))
if (y[0] == 0x01) then
print "little endian"
elseif (y[size - 1] == 0x01) then
print "big endian"
else
error("could not detect endianness")
end
Cheers, Henri
>
> --
> luigi
More information about the luatex
mailing list.