1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 13:11:28 +00:00

Upgraded from libpcre 4.3 to 6.3 - not tested on Windows yet

This commit is contained in:
fyodor
2005-09-11 09:22:55 +00:00
parent ba9cc971d9
commit 79b43cfb96
45 changed files with 14164 additions and 17815 deletions

View File

@@ -2,103 +2,108 @@
* Perl-Compatible Regular Expressions *
*************************************************/
/*
This is a library of functions to support regular expressions whose syntax
and semantics are as close as possible to those of the Perl 5 language. See
the file Tech.Notes for some information on the internals.
/* PCRE is a library of functions to support regular expressions whose syntax
and semantics are as close as possible to those of the Perl 5 language.
This module is a wrapper that provides a POSIX API to the underlying PCRE
functions.
Written by: Philip Hazel <ph10@cam.ac.uk>
Copyright (c) 1997-2003 University of Cambridge
Written by Philip Hazel
Copyright (c) 1997-2005 University of Cambridge
-----------------------------------------------------------------------------
Permission is granted to anyone to use this software for any purpose on any
computer system, and to redistribute it freely, subject to the following
restrictions:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. The origin of this software must not be misrepresented, either by
explicit claim or by omission.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Altered versions must be plainly marked as such, and must not be
misrepresented as being the original software.
* Neither the name of the University of Cambridge nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
4. If PCRE is embedded in any software that is released under the GNU
General Purpose Licence (GPL), then the terms of that licence shall
supersede any condition above with which it is incompatible.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------------
*/
#include "internal.h"
/* This module is a wrapper that provides a POSIX API to the underlying PCRE
functions. */
#include "pcre_internal.h"
#include "pcreposix.h"
#include "stdlib.h"
/* Corresponding tables of PCRE error messages and POSIX error codes. */
/* Table to translate PCRE compile time error codes into POSIX error codes. */
static const char *estring[] = {
ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10,
ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20,
ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR29, ERR29, ERR30,
ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39, ERR40,
ERR41, ERR42, ERR43 };
static int eint[] = {
REG_EESCAPE, /* "\\ at end of pattern" */
REG_EESCAPE, /* "\\c at end of pattern" */
REG_EESCAPE, /* "unrecognized character follows \\" */
REG_BADBR, /* "numbers out of order in {} quantifier" */
REG_BADBR, /* "number too big in {} quantifier" */
REG_EBRACK, /* "missing terminating ] for character class" */
REG_ECTYPE, /* "invalid escape sequence in character class" */
REG_ERANGE, /* "range out of order in character class" */
REG_BADRPT, /* "nothing to repeat" */
REG_BADRPT, /* "operand of unlimited repeat could match the empty string" */
REG_ASSERT, /* "internal error: unexpected repeat" */
REG_BADPAT, /* "unrecognized character after (?" */
REG_BADPAT, /* "POSIX named classes are supported only within a class" */
REG_EPAREN, /* "missing )" */
REG_ESUBREG, /* "reference to non-existent subpattern" */
REG_INVARG, /* "erroffset passed as NULL" */
REG_INVARG, /* "unknown option bit(s) set" */
REG_EPAREN, /* "missing ) after comment" */
REG_ESIZE, /* "parentheses nested too deeply" */
REG_ESIZE, /* "regular expression too large" */
REG_ESPACE, /* "failed to get memory" */
REG_EPAREN, /* "unmatched brackets" */
REG_ASSERT, /* "internal error: code overflow" */
REG_BADPAT, /* "unrecognized character after (?<" */
REG_BADPAT, /* "lookbehind assertion is not fixed length" */
REG_BADPAT, /* "malformed number after (?(" */
REG_BADPAT, /* "conditional group containe more than two branches" */
REG_BADPAT, /* "assertion expected after (?(" */
REG_BADPAT, /* "(?R or (?digits must be followed by )" */
REG_ECTYPE, /* "unknown POSIX class name" */
REG_BADPAT, /* "POSIX collating elements are not supported" */
REG_INVARG, /* "this version of PCRE is not compiled with PCRE_UTF8 support" */
REG_BADPAT, /* "spare error" */
REG_BADPAT, /* "character value in \x{...} sequence is too large" */
REG_BADPAT, /* "invalid condition (?(0)" */
REG_BADPAT, /* "\\C not allowed in lookbehind assertion" */
REG_EESCAPE, /* "PCRE does not support \\L, \\l, \\N, \\P, \\p, \\U, \\u, or \\X" */
REG_BADPAT, /* "number after (?C is > 255" */
REG_BADPAT, /* "closing ) for (?C expected" */
REG_BADPAT, /* "recursive call could loop indefinitely" */
REG_BADPAT, /* "unrecognized character after (?P" */
REG_BADPAT, /* "syntax error after (?P" */
REG_BADPAT /* "two named groups have the same name" */
static const int eint[] = {
0, /* no error */
REG_EESCAPE, /* \ at end of pattern */
REG_EESCAPE, /* \c at end of pattern */
REG_EESCAPE, /* unrecognized character follows \ */
REG_BADBR, /* numbers out of order in {} quantifier */
REG_BADBR, /* number too big in {} quantifier */
REG_EBRACK, /* missing terminating ] for character class */
REG_ECTYPE, /* invalid escape sequence in character class */
REG_ERANGE, /* range out of order in character class */
REG_BADRPT, /* nothing to repeat */
REG_BADRPT, /* operand of unlimited repeat could match the empty string */
REG_ASSERT, /* internal error: unexpected repeat */
REG_BADPAT, /* unrecognized character after (? */
REG_BADPAT, /* POSIX named classes are supported only within a class */
REG_EPAREN, /* missing ) */
REG_ESUBREG, /* reference to non-existent subpattern */
REG_INVARG, /* erroffset passed as NULL */
REG_INVARG, /* unknown option bit(s) set */
REG_EPAREN, /* missing ) after comment */
REG_ESIZE, /* parentheses nested too deeply */
REG_ESIZE, /* regular expression too large */
REG_ESPACE, /* failed to get memory */
REG_EPAREN, /* unmatched brackets */
REG_ASSERT, /* internal error: code overflow */
REG_BADPAT, /* unrecognized character after (?< */
REG_BADPAT, /* lookbehind assertion is not fixed length */
REG_BADPAT, /* malformed number after (?( */
REG_BADPAT, /* conditional group containe more than two branches */
REG_BADPAT, /* assertion expected after (?( */
REG_BADPAT, /* (?R or (?digits must be followed by ) */
REG_ECTYPE, /* unknown POSIX class name */
REG_BADPAT, /* POSIX collating elements are not supported */
REG_INVARG, /* this version of PCRE is not compiled with PCRE_UTF8 support */
REG_BADPAT, /* spare error */
REG_BADPAT, /* character value in \x{...} sequence is too large */
REG_BADPAT, /* invalid condition (?(0) */
REG_BADPAT, /* \C not allowed in lookbehind assertion */
REG_EESCAPE, /* PCRE does not support \L, \l, \N, \U, or \u */
REG_BADPAT, /* number after (?C is > 255 */
REG_BADPAT, /* closing ) for (?C expected */
REG_BADPAT, /* recursive call could loop indefinitely */
REG_BADPAT, /* unrecognized character after (?P */
REG_BADPAT, /* syntax error after (?P */
REG_BADPAT, /* two named groups have the same name */
REG_BADPAT, /* invalid UTF-8 string */
REG_BADPAT, /* support for \P, \p, and \X has not been compiled */
REG_BADPAT, /* malformed \P or \p sequence */
REG_BADPAT /* unknown property name after \P or \p */
};
/* Table of texts corresponding to POSIX error codes */
static const char *pstring[] = {
static const char *const pstring[] = {
"", /* Dummy for value 0 */
"internal error", /* REG_ASSERT */
"invalid repeat counts in {}", /* BADBR */
@@ -122,29 +127,11 @@ static const char *pstring[] = {
/*************************************************
* Translate PCRE text code to int *
*************************************************/
/* PCRE compile-time errors are given as strings defined as macros. We can just
look them up in a table to turn them into POSIX-style error codes. */
static int
pcre_posix_error_code(const char *s)
{
size_t i;
for (i = 0; i < sizeof(estring)/sizeof(char *); i++)
if (strcmp(s, estring[i]) == 0) return eint[i];
return REG_ASSERT;
}
/*************************************************
* Translate error code to string *
*************************************************/
size_t
EXPORT size_t
regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
{
const char *message, *addmessage;
@@ -179,7 +166,7 @@ return length + addlength;
* Free store held by a regex *
*************************************************/
void
EXPORT void
regfree(regex_t *preg)
{
(pcre_free)(preg->re_pcre);
@@ -202,22 +189,25 @@ Returns: 0 on success
various non-zero codes on failure
*/
int
EXPORT int
regcomp(regex_t *preg, const char *pattern, int cflags)
{
const char *errorptr;
int erroffset;
int errorcode;
int options = 0;
if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS;
if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE;
if ((cflags & REG_DOTALL) != 0) options |= PCRE_DOTALL;
preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset, NULL);
preg->re_pcre = pcre_compile2(pattern, options, &errorcode, &errorptr,
&erroffset, NULL);
preg->re_erroffset = erroffset;
if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr);
if (preg->re_pcre == NULL) return eint[errorcode];
preg->re_nsub = pcre_info(preg->re_pcre, NULL, NULL);
preg->re_nsub = pcre_info((const pcre *)preg->re_pcre, NULL, NULL);
return 0;
}
@@ -235,7 +225,7 @@ ints. However, if the number of possible capturing brackets is small, use a
block of store on the stack, to reduce the use of malloc/free. The threshold is
in a macro that can be changed at configure time. */
int
EXPORT int
regexec(const regex_t *preg, const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags)
{
@@ -264,8 +254,8 @@ if (nmatch > 0)
}
}
rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), 0, options,
ovector, nmatch * 3);
rc = pcre_exec((const pcre *)preg->re_pcre, NULL, string, (int)strlen(string),
0, options, ovector, nmatch * 3);
if (rc == 0) rc = nmatch; /* All captured slots were filled in */
@@ -293,6 +283,9 @@ else
case PCRE_ERROR_BADMAGIC: return REG_INVARG;
case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT;
case PCRE_ERROR_NOMEMORY: return REG_ESPACE;
case PCRE_ERROR_MATCHLIMIT: return REG_ESPACE;
case PCRE_ERROR_BADUTF8: return REG_INVARG;
case PCRE_ERROR_BADUTF8_OFFSET: return REG_INVARG;
default: return REG_ASSERT;
}
}