Byte-compiled chunk safe in C string?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Byte-compiled chunk safe in C string?

Philip Bock
I'm designing a binary file format, and I'd like to be able to embed byte-compiled 
Lua chunks in it. I looked in the refman, but I wasn't able to find anything on it. 
So, can I store byte-compiled chunks in null-terminated C strings? Specifically, 
will there be any stray nulls in a byte-compiled chunk?

Thanks, Philip Bock


Reply | Threaded
Open this post in threaded view
|

Re: Byte-compiled chunk safe in C string?

Luiz Henrique de Figueiredo
>So, can I store byte-compiled chunks in null-terminated C strings?

No.

>Specifically, will there be any stray nulls in a byte-compiled chunk?

Yes, lots of them.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Byte-compiled chunk safe in C string?

Sean Middleditch
In reply to this post by Philip Bock
On Tue, 2003-02-18 at 12:31, [hidden email] wrote:
> I'm designing a binary file format, and I'd like to be able to embed byte-compiled 
> Lua chunks in it. I looked in the refman, but I wasn't able to find anything on it. 
> So, can I store byte-compiled chunks in null-terminated C strings? Specifically, 
> will there be any stray nulls in a byte-compiled chunk?
> 

This definitely won't work; if you're storing a binary file, you could
store a length field and then the bytes.  Null-terminated definitely
won't work tho.  For binary files, even for normal strings it's probably
better to put the size of the string first, since it makes parsing and
loading the file a lot more efficient (since you can allocate the
necessary space up front and just read() the bytes into it).

> Thanks, Philip Bock
-- 
Sean Middleditch <[hidden email]>



Reply | Threaded
Open this post in threaded view
|

RE: Byte-compiled chunk safe in C string?

Peter Prade-2
In reply to this post by Philip Bock
> I'm designing a binary file format, and I'd like to be able to 
> embed byte-compiled 
> Lua chunks in it. I looked in the refman, but I wasn't able to 
> find anything on it. 
> So, can I store byte-compiled chunks in null-terminated C 
> strings? Specifically, 
> will there be any stray nulls in a byte-compiled chunk?

No. you can't rely on that. imagine this script:
s = "\0" -- this string as a constant, will be part of the chunk!

Cheers,
Peter

P.S.
Lua strings can contain binary data with 0's in them.


Reply | Threaded
Open this post in threaded view
|

Re: Byte-compiled chunk safe in C string?

Philip Bock
In reply to this post by Philip Bock
Thanks for everyone's responses. I'll do as suggested, and add a length parameter.

Thanks, Philip Bock