Migrate bin to string

From SecWiki
Jump to: navigation, search

NSE development: Migrating from bin.lua to string calls

When migrating scripts that use NSE library bin.lua to string.lua in Lua 5.3, keep in mind the following pointers:

  • The return values are in the opposite order: local value, pos = string.unpack(fmt, data, pos)
  • Always use an endianness specifier: < or >
  • Use stdnse.tohex or stdnse.fromhex instead of the "H" format from bin.
  • Use ipOps.str_to_ip and ipOps.ip_to_str to unpack and pack IP addresses (requires that you extract the correct length substring first).
  • Always use unsigned (capital letter) formats unless the format is explicitly a signed integer.
  • The fixed-size string format "c" always needs a length. The old bin format "A" only needed a length to unpack, but not to pack. You can just use string concatenation instead, if you prefer: ..
  • Whitespace in the format string is ignored, so it can be used to make the format more readable.