未能读取包含嵌入式零字节的实线 [英] Failure to read full line including embedded zero bytes

查看:177
本文介绍了未能读取包含嵌入式零字节的实线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Lua脚本:

  i = io.read()
print(i)


$ b

> echo -esala\x00m| lua ll.lua

输出:

  sala 

我想从输入中打印所有字符,类似

  salam 

在HEX编辑器中:

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

如何从输入中打印所有字符?

解决方案


具体来说,文件逐行读取不是嵌入式的0证明。

它还不是一个不幸的组合:
$ b


  • 核心只允许使用标准的C90或同样可移植的结构,而不提供高效的0-clean文本解析。 b $ b
  • 这个约束下的邮件列表上讨论的每个解决方案都有相当大的开销。
  • 文本文件中嵌入的0字节是相当少见的。


    解决方法:


    • 使用修改后的库修复这些格式: code>* l * L for file:read(...)

    • 自己解析原始数据。 (使用一个数字或尽可能多地使用* a)读取数据块

    • 獾开发人员/维护人员直到他们放弃。


    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?

    解决方案

    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.

    这篇关于未能读取包含嵌入式零字节的实线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆