got it, tks!
On 4 Jul 2017, at 9:25 PM, Szabó Antal <szabo.antal.92@xxxxxxxxx> wrote:
Hi,
On Tue, Jul 4, 2017 at 1:08 PM, Jason Gian <gianjason@xxxxxxxxx> wrote:
local bar = test.direct_return(foo)
print(foo)
print("======================")
print(ffi.string(bar))
It seems that the string had changed when I send it to the c function.
This is not a LuaJIT issue. The file you are testing with is a binary
file, which contains a 0 byte near the beginning (hexdump):
00000000: 5041 4e44 4f52 4124 0100 0301 018a 1198 PANDORA$........
C strings are defined to be 0 terminated, which means that the string
ends at the first 0 byte. Thus, when LuaJIT converts the C string back
to a Lua string, it correctly terminates after the 0 byte, giving you
the beginning of the file contents.
To pass arbitrary (binary) data around in C, the usual way is to pass
a pointer and a length together to each function.
If you read the documentation of ffi.string, it actually mentions the
issue (http://luajit.org/ext_ffi_api.html):
str = ffi.string(ptr [,len])
Creates an interned Lua string from the data pointed to by ptr.
If the optional argument len is missing, ptr is converted to a "char
*" and the data is assumed to be zero-terminated. The length of the
string is computed with strlen().
Otherwise ptr is converted to a "void *" and len gives the length of
the data. The data may contain embedded zeros and need not be
byte-oriented (though this may cause endianess issues).
So you can pass a len parameter to ffi.string, which would solve this issue.
Hope this helps!
Antal