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

  1. --- Perl Compatible Regular Expressions.
  2. --
  3. -- One of Lua's quirks is its string patterns. While they have great performance
  4. -- and are tightly integrated into the Lua interpreter, they are very different
  5. -- in syntax and not as powerful as standard regular expressions.  So we have
  6. -- integrated Perl compatible regular expressions into Lua using PCRE and a
  7. -- modified version of the Lua PCRE library written by Reuben Thomas and Shmuel
  8. -- Zeigerman. These are the same sort of regular expressions used by Nmap
  9. -- version detection.  The main modification to their library is that the NSE
  10. -- version only supports PCRE expressions instead of both PCRE and POSIX
  11. -- patterns. In order to maintain a high script execution speed, the library
  12. -- interfacing with PCRE is kept very thin. It is not integrated as seamlessly
  13. -- as the Lua string pattern API. This allows script authors to decide when to
  14. -- use PCRE expressions versus Lua patterns.  The use of PCRE involves a
  15. -- separate pattern compilation step, which saves execution time when patterns
  16. -- are reused.  Compiled patterns can be cached in the NSE registry and reused
  17. -- by other scripts.
  18. --
  19. -- The documentation for this module is derived from that supplied by the PCRE
  20. -- Lua lib.
  21. --
  22. -- Warning: PCRE has a history of security vulnerabilities allowing attackers
  23. -- who are able to compile arbitrary regular expressions to execute arbitrary
  24. -- code.  More such vulnerabilities may be discovered in the future.  These have
  25. -- never affected Nmap because it doesn't give attackers any control over the
  26. -- regular expressions it uses.  Similarly, NSE scripts should never build
  27. -- regular expressions with untrusted network input.  Matching hardcoded regular
  28. -- expressions against the untrusted input is fine.
  29. -- @author Reuben Thomas
  30. -- @author Shmuel Zeigerman
  31.  
  32. module "pcre"
  33.  
  34. --- Returns a compiled regular expression.
  35. --
  36. -- The resulting compiled regular expression is ready to be matched against
  37. -- strings. Compiled regular expressions are subject to Lua's garbage
  38. -- collection.
  39. --
  40. -- The compilation flags are set bitwise. If you want to set the 3rd
  41. -- (corresponding to the number 4) and the 1st (corresponding to 1) bit for
  42. -- example you would pass the number 5 as a second argument. The compilation
  43. -- flags accepted are those of the PCRE C library. These include flags for case
  44. -- insensitive matching (<code>1</code>), matching line beginnings
  45. -- (<code>^</code>) and endings (<code>$</code>) even in multiline strings
  46. -- (i.e. strings containing newlines) (<code>2</code>) and a flag for matching
  47. -- across line boundaries (<code>4</code>). No compilation flags yield a
  48. -- default value of <code>0</code>.
  49. -- @param pattern a string describing the pattern, such as <code>"^foo$"</code>.
  50. -- @param flags a number describing which compilation flags are set.
  51. -- @param locale a string describing the locale which should be used to compile
  52. -- the regular expression (optional). The value is a string which is passed to
  53. -- the C standard library function <code>setlocale</code>.  For more
  54. -- information on this argument refer to the documentation of
  55. -- <code>setlocale</code>.
  56. -- @usage local regex = pcre.new("pcre-pattern",0,"C")
  57. function new(pattern, flags, locale)
  58.  
  59. --- Returns a table of the available PCRE option flags (numbers) keyed by their
  60. -- names (strings).
  61. --
  62. -- Possible names of the available strings can be retrieved from the
  63. -- documentation of the PCRE library used to link against Nmap. The key is the
  64. -- option name in the manual minus the <code>PCRE_</code> prefix.
  65. -- <code>PCRE_CASELESS</code> becomes <code>CASELESS</code> for example.
  66. function flags()
  67.  
  68. --- Returns the version of the PCRE library in use as a string.
  69. --
  70. -- For example <code>"6.4 05-Sep-2005"</code>.
  71. function version()
  72.  
  73. --- Matches a string against a compiled regular expression.
  74. --
  75. -- Returns the start point and the end point of the first match of the compiled
  76. -- regular expression in the string.
  77. -- @param string the string to match against.
  78. -- @param start where to start the match in the string (optional).
  79. -- @param flags execution flags (optional).
  80. -- @return <code>nil</code> if no match, otherwise the start point of the first
  81. -- match.
  82. -- @return the end point of the first match.
  83. -- @return a table which contains false in the positions where the pattern did
  84. -- not match. If named sub-patterns were used, the table also contains substring
  85. -- matches keyed by their sub-pattern name.
  86. -- @usage
  87. -- i, j = regex:match("string to be searched", 0, 0)
  88. -- if (i) then ... end
  89. function match(string, start, flags)
  90.  
  91. --- Matches a string against a compiled regular expression, returning positions
  92. -- of substring matches.
  93. --
  94. -- This function is like <code>match</code> except that a table returned as a
  95. -- third result contains offsets of substring matches rather than substring
  96. -- matches themselves. That table will not contain string keys, even if named
  97. -- sub-patterns are used.  For example, if the whole match is at offsets 10, 20
  98. -- and substring matches are at offsets 12, 14 and 16, 19 then the function
  99. -- returns <code>10, 20, {12,14,16,19}</code>.
  100. -- @param string the string to match against.
  101. -- @param start where to start the match in the string (optional).
  102. -- @param flags execution flags (optional).
  103. -- @return <code>nil</code> if no match, otherwise the start point of the match
  104. -- of the whole string.
  105. -- @return the end point of the match of the whole string.
  106. -- @return a table containing a list of substring match start and end positions.
  107. -- @usage
  108. -- i, j, substrings = regex:exec("string to be searched", 0, 0)
  109. -- if (i) then ... end
  110. function exec(string, start, flags)
  111.  
  112. --- Matches a string against a regular expression multiple times.
  113. --
  114. -- Tries to match the regular expression pcre_obj against string up to
  115. -- <code>n</code> times (or as many as possible if <code>n</code> is not given
  116. -- or is not a positive number), subject to the execution flags
  117. -- <code>ef</code>. Each time there is a match, <code>func</code> is called as
  118. -- <code>func(m, t)</code>, where <code>m</code> is the matched string and
  119. -- <code>t</code> is a table of substring matches. This table contains false in
  120. -- the positions where the corresponding sub-pattern did not match. If named
  121. -- sub-patterns are used then the table also contains substring matches keyed
  122. -- by their correspondent sub-pattern names (strings).  If <code>func</code>
  123. -- returns a true value, then <code>gmatch</code> immediately returns;
  124. -- <code>gmatch</code> returns the number of matches made.
  125. -- @param string the string to match against.
  126. -- @param func the function to call for each match.
  127. -- @param n the maximum number of matches to do (optional).
  128. -- @param ef execution flags (optional).
  129. -- @return the number of matches made.
  130. -- @usage
  131. -- local t = {}
  132. -- local function match(m) t[#t + 1] = m end
  133. -- local n = regex:gmatch("string to be searched", match)
  134. function pcre_obj:gmatch(string, func, n, ef)
  135.