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

  1. --- Buffered network I/O helper functions.
  2. --
  3. -- The functions in this module can be used for delimiting data received by the
  4. -- <code>nmap.receive_buf</code> function in the Network I/O API (which see).
  5. -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
  6.  
  7. module(... or "match",  package.seeall)
  8. require "pcre"
  9.  
  10. --various functions for use with nse's nsock:receive_buf - function
  11.  
  12. -- e.g. 
  13. -- sock:receive_buf(regex("myregexpattern")) - does a match using pcre- regular-
  14. --                                           - expressions
  15. -- sock:receive_buf(numbytes(80)) - is the buffered version of 
  16. --                                  sock:receive_bytes(80) - i.e. it returns
  17. --                                  exactly 80 bytes and no more 
  18.  
  19. --- Return a function that allows delimiting with a regular expression.
  20. --
  21. -- This function is a wrapper around <code>pcre.exec</code>. Its purpose is to
  22. -- give script developers the ability to use regular expressions for delimiting
  23. -- instead of Lua's string patterns.
  24. -- @param pattern The regex.
  25. -- @usage sock:receive_buf(match.regex("myregexpattern"))
  26. -- @see nmap.receive_buf
  27. -- @see pcre.exec
  28. regex = function(pattern)
  29.     local r = pcre.new(pattern, 0,"C")
  30.  
  31.     return function(buf)
  32.         s,e = r:exec(buf, 0,0);
  33.         return s,e
  34.     end
  35. end
  36.  
  37. --- Return a function that allows delimiting at a certain number of bytes.
  38. --
  39. -- This function can be used to get a buffered version of
  40. -- <code>sock:receive_bytes(n)</code> in case a script requires more than one
  41. -- fixed-size chunk, as the unbuffered version may return more bytes than
  42. -- requested and thus would require you to do the parsing on your own. 
  43. -- @param num Number of bytes.
  44. -- @usage sock:receive_buf(match.numbytes(80))
  45. -- @see nmap.receive_buf
  46. numbytes = function(num)
  47.     local n = num
  48.     return function(buf)
  49.         if(string.len(buf) >=n) then
  50.             return n, n
  51.         end
  52.         return nil
  53.     end
  54. end
  55.  
  56.