Question

Lua script:

i=io.read()
print(i)

Command line:

echo -e "sala\x00m" | lua ll.lua

Output:

sala

I want it to print all character from input, similar to this:

salam

in HEX editor:

0000000: 7361 6c61 006d 0a                         sala.m.

How can I print all character from input?

Was it helpful?

Solution

You tripped over one of the few places where the Lua standard library is still not 8-bit-clean.
Specifically, file reading line-by-line is not embedded-0 proof.

The reason it isn't yet is an unfortunate combination of:

  • Only standard C90 or equally portable constructs are allowed for the core, which does not provide for efficient 0-clean text parsing.
  • Every solution discussed to date on the mailinglist under that constraint has considerable overhead.
  • Embedded 0-bytes in text files are quite rare.

Workarounds:

  • Use a modified library, fixing these formats: "*l" "*L" for file:read(...)
  • parse your raw data yourself. (read a block using a number or as much as possible using "*a")
  • Badger the Lua developers/maintainers for a bugfix until they give in.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top