Library bin

A problem script authors often face is the necessity of encoding valuesinto binary data. For example after analyzing a protocol the startingpoint to write a script could be a hex dump, which serves as a preambleto every sent packet. Although it is possible to work with thefunctionality Lua provides, it's not very convenient. Therefore NSE includesBinlib, based on lpack (http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/)by Luiz Henrique de Figueiredo.

The Binlib functions take a format string to encode and decode binarydata. Packing and unpacking are controlled by the following operatorcharacters:

  • H hex string
  • B bit string
  • x null byte
  • z zero-terminated string
  • p string preceded by 1-byte integer length
  • P string preceded by 2-byte integer length
  • a string preceded by 4-byte integer length
  • A string
  • f float
  • d double
  • n Lua number
  • c char (1-byte integer)
  • C byte = unsigned char (1-byte unsigned integer)
  • s short (2-byte integer)
  • S unsigned short (2-byte unsigned integer)
  • i int (4-byte integer)
  • I unsigned int (4-byte unsigned integer)
  • l long (8-byte integer)
  • L unsigned long (8-byte unsigned integer)
  • < little endian modifier
  • > big endian modifier
  • = native endian modifier

Note that the endian operators work as modifiers to all thecharacters following them in the format string.

Functions

pack (format, ...)

Returns a binary packed string.

unpack (format, data, init)

Returns values read from the binary packed data string.



Functions

pack (format, ...)

Returns a binary packed string.

The format string describes how the parameters (p1,...) will be interpreted. Numerical values following operatorsstand for operator repetitions and need an according amount of parameters.Operators expect appropriate parameter types.

Note: on Windows packing of 64-bit values > 2^63 currentlyresults in packing exactly 2^63.

Parameters

  • format: Format string, used to pack following arguments.
  • ...: The values to pack.

Return value:

String containing packed data.
unpack (format, data, init)

Returns values read from the binary packed data string.

The first return value of this function is the position at which unpackingstopped. This can be used as the init value for subsequentcalls. The following return values are the values according to the formatstring. Numerical values in the format string are interpreted as repetitionslike in pack, except if used with A,B, or H, in which cases the number tellsunpack how many bytes to read. unpack stops ifeither the format string or the binary data string are exhausted.

Parameters

  • format: Format string, used to unpack values out of data string.
  • data: String containing packed data.
  • init: Optional starting position within the string.

Return values:

  1. Position in the data string where unpacking stopped.
  2. All unpacked values.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值