home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Networking / nmap-5.00-setup.exe / nselib / bin.luadoc < prev    next >
Text File  |  2009-07-06  |  3KB  |  75 lines

  1. --- Pack and unpack binary data.
  2. --
  3. -- A problem script authors often face is the necessity of encoding values
  4. -- into binary data. For example after analyzing a protocol the starting 
  5. -- point to write a script could be a hex dump, which serves as a preamble
  6. -- to every sent packet. Although it is possible to work with the 
  7. -- functionality Lua provides, it's not very convenient. Therefore NSE includes
  8. -- Binlib, based on lpack (http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/)
  9. -- by Luiz Henrique de Figueiredo.
  10. --
  11. -- The Binlib functions take a format string to encode and decode binary
  12. -- data. Packing and unpacking are controlled by the following operator
  13. -- characters:
  14. -- * <code>H</code> hex string
  15. -- * <code>B</code> bit string
  16. -- * <code>x</code> null byte
  17. -- * <code>z</code> zero-terminated string
  18. -- * <code>p</code> string preceded by 1-byte integer length
  19. -- * <code>P</code> string preceded by 2-byte integer length
  20. -- * <code>a</code> string preceded by 4-byte integer length
  21. -- * <code>A</code> string
  22. -- * <code>f</code> float
  23. -- * <code>d</code> double
  24. -- * <code>n</code> Lua number
  25. -- * <code>c</code> char (1-byte integer)
  26. -- * <code>C</code> byte = unsigned char (1-byte unsigned integer)
  27. -- * <code>s</code> short (2-byte integer)
  28. -- * <code>S</code> unsigned short (2-byte unsigned integer)
  29. -- * <code>i</code> int (4-byte integer)
  30. -- * <code>I</code> unsigned int (4-byte unsigned integer)
  31. -- * <code>l</code> long (8-byte integer)
  32. -- * <code>L</code> unsigned long (8-byte unsigned integer)
  33. -- * <code><</code> little endian modifier
  34. -- * <code>></code> big endian modifier
  35. -- * <code>=</code> native endian modifier
  36. --
  37. -- Note that the endian operators work as modifiers to all the 
  38. -- characters following them in the format string.
  39.  
  40. module "bin"
  41.  
  42.  
  43. --- Returns a binary packed string.
  44. --
  45. -- The format string describes how the parameters (<code>p1</code>,
  46. -- <code>...</code>) will be interpreted. Numerical values following operators
  47. -- stand for operator repetitions and need an according amount of parameters.
  48. -- Operators expect appropriate parameter types.
  49. --
  50. -- Note: on Windows packing of 64-bit values > 2^63 currently
  51. -- results in packing exactly 2^63.
  52. -- @param format Format string, used to pack following arguments.
  53. -- @param ... The values to pack.
  54. -- @return String containing packed data.
  55. function pack(format, ...)
  56.  
  57.  
  58. --- Returns values read from the binary packed data string.
  59. --
  60. -- The first return value of this function is the position at which unpacking
  61. -- stopped. This can be used as the <code>init</code> value for subsequent
  62. -- calls. The following return values are the values according to the format
  63. -- string. Numerical values in the format string are interpreted as repetitions
  64. -- like in <code>pack</code>, except if used with <code>A</code>,
  65. -- <code>B</code>, or <code>H</code>, in which cases the number tells
  66. -- <code>unpack</code> how many bytes to read. <code>unpack</code> stops if
  67. -- either the format string or the binary data string are exhausted. 
  68. -- @param format Format string, used to unpack values out of data string.
  69. -- @param data String containing packed data.
  70. -- @param init Optional starting position within the string.
  71. -- @return Position in the data string where unpacking stopped.
  72. -- @return All unpacked values.
  73. function unpack(format, data, init)
  74.  
  75.