| 7 |
and semantics are as close as possible to those of the Perl 5 language. |
and semantics are as close as possible to those of the Perl 5 language. |
| 8 |
|
|
| 9 |
Written by Philip Hazel |
Written by Philip Hazel |
| 10 |
Copyright (c) 1997-2009 University of Cambridge |
Copyright (c) 1997-2010 University of Cambridge |
| 11 |
|
|
| 12 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
| 13 |
Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
| 45 |
#ifndef PCRE_INTERNAL_H |
#ifndef PCRE_INTERNAL_H |
| 46 |
#define PCRE_INTERNAL_H |
#define PCRE_INTERNAL_H |
| 47 |
|
|
| 48 |
/* Define DEBUG to get debugging output on stdout. */ |
/* Define PCRE_DEBUG to get debugging output on stdout. */ |
| 49 |
|
|
| 50 |
#if 0 |
#if 0 |
| 51 |
#define DEBUG |
#define PCRE_DEBUG |
| 52 |
#endif |
#endif |
| 53 |
|
|
| 54 |
/* We do not support both EBCDIC and UTF-8 at the same time. The "configure" |
/* We do not support both EBCDIC and UTF-8 at the same time. The "configure" |
| 74 |
be absolutely sure we get our version. */ |
be absolutely sure we get our version. */ |
| 75 |
|
|
| 76 |
#undef DPRINTF |
#undef DPRINTF |
| 77 |
#ifdef DEBUG |
#ifdef PCRE_DEBUG |
| 78 |
#define DPRINTF(p) printf p |
#define DPRINTF(p) printf p |
| 79 |
#else |
#else |
| 80 |
#define DPRINTF(p) /* Nothing */ |
#define DPRINTF(p) /* Nothing */ |
| 86 |
|
|
| 87 |
#include <ctype.h> |
#include <ctype.h> |
| 88 |
#include <limits.h> |
#include <limits.h> |
|
#include <setjmp.h> |
|
|
#include <stdarg.h> |
|
| 89 |
#include <stddef.h> |
#include <stddef.h> |
| 90 |
#include <stdio.h> |
#include <stdio.h> |
| 91 |
#include <stdlib.h> |
#include <stdlib.h> |
| 184 |
#error Cannot determine a type for 32-bit unsigned integers |
#error Cannot determine a type for 32-bit unsigned integers |
| 185 |
#endif |
#endif |
| 186 |
|
|
| 187 |
|
/* When checking for integer overflow in pcre_compile(), we need to handle |
| 188 |
|
large integers. If a 64-bit integer type is available, we can use that. |
| 189 |
|
Otherwise we have to cast to double, which of course requires floating point |
| 190 |
|
arithmetic. Handle this by defining a macro for the appropriate type. If |
| 191 |
|
stdint.h is available, include it; it may define INT64_MAX. Systems that do not |
| 192 |
|
have stdint.h (e.g. Solaris) may have inttypes.h. The macro int64_t may be set |
| 193 |
|
by "configure". */ |
| 194 |
|
|
| 195 |
|
#if HAVE_STDINT_H |
| 196 |
|
#include <stdint.h> |
| 197 |
|
#elif HAVE_INTTYPES_H |
| 198 |
|
#include <inttypes.h> |
| 199 |
|
#endif |
| 200 |
|
|
| 201 |
|
#if defined INT64_MAX || defined int64_t |
| 202 |
|
#define INT64_OR_DOUBLE int64_t |
| 203 |
|
#else |
| 204 |
|
#define INT64_OR_DOUBLE double |
| 205 |
|
#endif |
| 206 |
|
|
| 207 |
/* All character handling must be done as unsigned characters. Otherwise there |
/* All character handling must be done as unsigned characters. Otherwise there |
| 208 |
are problems with top-bit-set characters and functions such as isspace(). |
are problems with top-bit-set characters and functions such as isspace(). |
| 209 |
However, we leave the interface to the outside world as char *, because that |
However, we leave the interface to the outside world as char *, because that |
| 567 |
/* Options for the "extra" block produced by pcre_study(). */ |
/* Options for the "extra" block produced by pcre_study(). */ |
| 568 |
|
|
| 569 |
#define PCRE_STUDY_MAPPED 0x01 /* a map of starting chars exists */ |
#define PCRE_STUDY_MAPPED 0x01 /* a map of starting chars exists */ |
| 570 |
|
#define PCRE_STUDY_MINLEN 0x02 /* a minimum length field exists */ |
| 571 |
|
|
| 572 |
/* Masks for identifying the public options that are permitted at compile |
/* Masks for identifying the public options that are permitted at compile |
| 573 |
time, run time, or study time, respectively. */ |
time, run time, or study time, respectively. */ |
| 1228 |
OP_EOD must correspond in order to the list of escapes immediately above. |
OP_EOD must correspond in order to the list of escapes immediately above. |
| 1229 |
|
|
| 1230 |
*** NOTE NOTE NOTE *** Whenever this list is updated, the two macro definitions |
*** NOTE NOTE NOTE *** Whenever this list is updated, the two macro definitions |
| 1231 |
that follow must also be updated to match. There is also a table called |
that follow must also be updated to match. There are also tables called |
| 1232 |
"coptable" in pcre_dfa_exec.c that must be updated. */ |
"coptable" and "poptable" in pcre_dfa_exec.c that must be updated. */ |
| 1233 |
|
|
| 1234 |
enum { |
enum { |
| 1235 |
OP_END, /* 0 End of pattern */ |
OP_END, /* 0 End of pattern */ |
| 1365 |
OP_SCBRA, /* 98 Start of capturing bracket, check empty */ |
OP_SCBRA, /* 98 Start of capturing bracket, check empty */ |
| 1366 |
OP_SCOND, /* 99 Conditional group, check empty */ |
OP_SCOND, /* 99 Conditional group, check empty */ |
| 1367 |
|
|
| 1368 |
|
/* The next two pairs must (respectively) be kept together. */ |
| 1369 |
|
|
| 1370 |
OP_CREF, /* 100 Used to hold a capture number as condition */ |
OP_CREF, /* 100 Used to hold a capture number as condition */ |
| 1371 |
OP_RREF, /* 101 Used to hold a recursion number as condition */ |
OP_NCREF, /* 101 Same, but generaged by a name reference*/ |
| 1372 |
OP_DEF, /* 102 The DEFINE condition */ |
OP_RREF, /* 102 Used to hold a recursion number as condition */ |
| 1373 |
|
OP_NRREF, /* 103 Same, but generaged by a name reference*/ |
| 1374 |
|
OP_DEF, /* 104 The DEFINE condition */ |
| 1375 |
|
|
| 1376 |
OP_BRAZERO, /* 103 These two must remain together and in this */ |
OP_BRAZERO, /* 105 These two must remain together and in this */ |
| 1377 |
OP_BRAMINZERO, /* 104 order. */ |
OP_BRAMINZERO, /* 106 order. */ |
| 1378 |
|
|
| 1379 |
/* These are backtracking control verbs */ |
/* These are backtracking control verbs */ |
| 1380 |
|
|
| 1381 |
OP_PRUNE, /* 105 */ |
OP_PRUNE, /* 107 */ |
| 1382 |
OP_SKIP, /* 106 */ |
OP_SKIP, /* 108 */ |
| 1383 |
OP_THEN, /* 107 */ |
OP_THEN, /* 109 */ |
| 1384 |
OP_COMMIT, /* 108 */ |
OP_COMMIT, /* 110 */ |
| 1385 |
|
|
| 1386 |
/* These are forced failure and success verbs */ |
/* These are forced failure and success verbs */ |
| 1387 |
|
|
| 1388 |
OP_FAIL, /* 109 */ |
OP_FAIL, /* 111 */ |
| 1389 |
OP_ACCEPT, /* 110 */ |
OP_ACCEPT, /* 112 */ |
| 1390 |
OP_CLOSE, /* 111 Used before OP_ACCEPT to close open captures */ |
OP_CLOSE, /* 113 Used before OP_ACCEPT to close open captures */ |
| 1391 |
|
|
| 1392 |
/* This is used to skip a subpattern with a {0} quantifier */ |
/* This is used to skip a subpattern with a {0} quantifier */ |
| 1393 |
|
|
| 1394 |
OP_SKIPZERO /* 112 */ |
OP_SKIPZERO, /* 114 */ |
| 1395 |
|
|
| 1396 |
|
/* This is not an opcode, but is used to check that tables indexed by opcode |
| 1397 |
|
are the correct length, in order to catch updating errors - there have been |
| 1398 |
|
some in the past. */ |
| 1399 |
|
|
| 1400 |
|
OP_TABLE_LENGTH |
| 1401 |
}; |
}; |
| 1402 |
|
|
| 1403 |
|
/* *** NOTE NOTE NOTE *** Whenever the list above is updated, the two macro |
| 1404 |
|
definitions that follow must also be updated to match. There are also tables |
| 1405 |
|
called "coptable" cna "poptable" in pcre_dfa_exec.c that must be updated. */ |
| 1406 |
|
|
| 1407 |
|
|
| 1408 |
/* This macro defines textual names for all the opcodes. These are used only |
/* This macro defines textual names for all the opcodes. These are used only |
| 1409 |
for debugging. The macro is referenced only in pcre_printint.c. */ |
for debugging. The macro is referenced only in pcre_printint.c. */ |
| 1425 |
"Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", \ |
"Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", \ |
| 1426 |
"AssertB", "AssertB not", "Reverse", \ |
"AssertB", "AssertB not", "Reverse", \ |
| 1427 |
"Once", "Bra", "CBra", "Cond", "SBra", "SCBra", "SCond", \ |
"Once", "Bra", "CBra", "Cond", "SBra", "SCBra", "SCond", \ |
| 1428 |
"Cond ref", "Cond rec", "Cond def", "Brazero", "Braminzero", \ |
"Cond ref", "Cond nref", "Cond rec", "Cond nrec", "Cond def", \ |
| 1429 |
|
"Brazero", "Braminzero", \ |
| 1430 |
"*PRUNE", "*SKIP", "*THEN", "*COMMIT", "*FAIL", "*ACCEPT", \ |
"*PRUNE", "*SKIP", "*THEN", "*COMMIT", "*FAIL", "*ACCEPT", \ |
| 1431 |
"Close", "Skip zero" |
"Close", "Skip zero" |
| 1432 |
|
|
| 1445 |
1, 1, 1, 1, 1, /* \A, \G, \K, \B, \b */ \ |
1, 1, 1, 1, 1, /* \A, \G, \K, \B, \b */ \ |
| 1446 |
1, 1, 1, 1, 1, 1, /* \D, \d, \S, \s, \W, \w */ \ |
1, 1, 1, 1, 1, 1, /* \D, \d, \S, \s, \W, \w */ \ |
| 1447 |
1, 1, 1, /* Any, AllAny, Anybyte */ \ |
1, 1, 1, /* Any, AllAny, Anybyte */ \ |
| 1448 |
3, 3, 1, /* NOTPROP, PROP, EXTUNI */ \ |
3, 3, /* \P, \p */ \ |
| 1449 |
1, 1, 1, 1, 1, /* \R, \H, \h, \V, \v */ \ |
1, 1, 1, 1, 1, /* \R, \H, \h, \V, \v */ \ |
| 1450 |
|
1, /* \X */ \ |
| 1451 |
1, 1, 2, 1, 1, /* \Z, \z, Opt, ^, $ */ \ |
1, 1, 2, 1, 1, /* \Z, \z, Opt, ^, $ */ \ |
| 1452 |
2, /* Char - the minimum length */ \ |
2, /* Char - the minimum length */ \ |
| 1453 |
2, /* Charnc - the minimum length */ \ |
2, /* Charnc - the minimum length */ \ |
| 1489 |
1+LINK_SIZE, /* SBRA */ \ |
1+LINK_SIZE, /* SBRA */ \ |
| 1490 |
3+LINK_SIZE, /* SCBRA */ \ |
3+LINK_SIZE, /* SCBRA */ \ |
| 1491 |
1+LINK_SIZE, /* SCOND */ \ |
1+LINK_SIZE, /* SCOND */ \ |
| 1492 |
3, /* CREF */ \ |
3, 3, /* CREF, NCREF */ \ |
| 1493 |
3, /* RREF */ \ |
3, 3, /* RREF, NRREF */ \ |
| 1494 |
1, /* DEF */ \ |
1, /* DEF */ \ |
| 1495 |
1, 1, /* BRAZERO, BRAMINZERO */ \ |
1, 1, /* BRAZERO, BRAMINZERO */ \ |
| 1496 |
1, 1, 1, 1, /* PRUNE, SKIP, THEN, COMMIT, */ \ |
1, 1, 1, 1, /* PRUNE, SKIP, THEN, COMMIT, */ \ |
| 1497 |
1, 1, 3, 1 /* FAIL, ACCEPT, CLOSE, SKIPZERO */ |
1, 1, 3, 1 /* FAIL, ACCEPT, CLOSE, SKIPZERO */ |
| 1498 |
|
|
| 1499 |
|
|
| 1500 |
/* A magic value for OP_RREF to indicate the "any recursion" condition. */ |
/* A magic value for OP_RREF and OP_NRREF to indicate the "any recursion" |
| 1501 |
|
condition. */ |
| 1502 |
|
|
| 1503 |
#define RREF_ANY 0xffff |
#define RREF_ANY 0xffff |
| 1504 |
|
|
| 1505 |
/* Error code numbers. They are given names so that they can more easily be |
/* Compile time error code numbers. They are given names so that they can more |
| 1506 |
tracked. */ |
easily be tracked. When a new number is added, the table called eint in |
| 1507 |
|
pcreposix.c must be updated. */ |
| 1508 |
|
|
| 1509 |
enum { ERR0, ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, |
enum { ERR0, ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, |
| 1510 |
ERR10, ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, |
ERR10, ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, |
| 1512 |
ERR30, ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39, |
ERR30, ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39, |
| 1513 |
ERR40, ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47, ERR48, ERR49, |
ERR40, ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47, ERR48, ERR49, |
| 1514 |
ERR50, ERR51, ERR52, ERR53, ERR54, ERR55, ERR56, ERR57, ERR58, ERR59, |
ERR50, ERR51, ERR52, ERR53, ERR54, ERR55, ERR56, ERR57, ERR58, ERR59, |
| 1515 |
ERR60, ERR61, ERR62, ERR63, ERR64 }; |
ERR60, ERR61, ERR62, ERR63, ERR64, ERR65, ERRCOUNT }; |
| 1516 |
|
|
| 1517 |
/* The real format of the start of the pcre block; the index of names and the |
/* The real format of the start of the pcre block; the index of names and the |
| 1518 |
code vector run on as long as necessary after the end. We store an explicit |
code vector run on as long as necessary after the end. We store an explicit |
| 1528 |
structure should be made at the end, and something earlier (e.g. a new |
structure should be made at the end, and something earlier (e.g. a new |
| 1529 |
flag in the options or one of the dummy fields) should indicate that the new |
flag in the options or one of the dummy fields) should indicate that the new |
| 1530 |
fields are present. Currently PCRE always sets the dummy fields to zero. |
fields are present. Currently PCRE always sets the dummy fields to zero. |
| 1531 |
NOTE NOTE NOTE: |
NOTE NOTE NOTE |
| 1532 |
*/ |
*/ |
| 1533 |
|
|
| 1534 |
typedef struct real_pcre { |
typedef struct real_pcre { |
| 1555 |
|
|
| 1556 |
typedef struct pcre_study_data { |
typedef struct pcre_study_data { |
| 1557 |
pcre_uint32 size; /* Total that was malloced */ |
pcre_uint32 size; /* Total that was malloced */ |
| 1558 |
pcre_uint32 options; |
pcre_uint32 flags; /* Private flags */ |
| 1559 |
uschar start_bits[32]; |
uschar start_bits[32]; /* Starting char bits */ |
| 1560 |
|
pcre_uint32 minlength; /* Minimum subject length */ |
| 1561 |
} pcre_study_data; |
} pcre_study_data; |
| 1562 |
|
|
| 1563 |
/* Structure for building a chain of open capturing subpatterns during |
/* Structure for building a chain of open capturing subpatterns during |
| 1564 |
compiling, so that instructions to close them can be compiled when (*ACCEPT) is |
compiling, so that instructions to close them can be compiled when (*ACCEPT) is |
| 1565 |
encountered. */ |
encountered. This is also used to identify subpatterns that contain recursive |
| 1566 |
|
back references to themselves, so that they can be made atomic. */ |
| 1567 |
|
|
| 1568 |
typedef struct open_capitem { |
typedef struct open_capitem { |
| 1569 |
struct open_capitem *next; /* Chain link */ |
struct open_capitem *next; /* Chain link */ |
| 1570 |
pcre_uint16 number; /* Capture number */ |
pcre_uint16 number; /* Capture number */ |
| 1571 |
} open_capitem; |
pcre_uint16 flag; /* Set TRUE if recursive back ref */ |
| 1572 |
|
} open_capitem; |
| 1573 |
|
|
| 1574 |
/* Structure for passing "static" information around between the functions |
/* Structure for passing "static" information around between the functions |
| 1575 |
doing the compiling, so that they are thread-safe. */ |
doing the compiling, so that they are thread-safe. */ |
| 1583 |
const uschar *start_code; /* The start of the compiled code */ |
const uschar *start_code; /* The start of the compiled code */ |
| 1584 |
const uschar *start_pattern; /* The start of the pattern */ |
const uschar *start_pattern; /* The start of the pattern */ |
| 1585 |
const uschar *end_pattern; /* The end of the pattern */ |
const uschar *end_pattern; /* The end of the pattern */ |
| 1586 |
open_capitem *open_caps; /* Chain of open capture items */ |
open_capitem *open_caps; /* Chain of open capture items */ |
| 1587 |
uschar *hwm; /* High watermark of workspace */ |
uschar *hwm; /* High watermark of workspace */ |
| 1588 |
uschar *name_table; /* The name/number table */ |
uschar *name_table; /* The name/number table */ |
| 1589 |
int names_found; /* Number of entries so far */ |
int names_found; /* Number of entries so far */ |
| 1596 |
int external_flags; /* External flag bits to be set */ |
int external_flags; /* External flag bits to be set */ |
| 1597 |
int req_varyopt; /* "After variable item" flag for reqbyte */ |
int req_varyopt; /* "After variable item" flag for reqbyte */ |
| 1598 |
BOOL had_accept; /* (*ACCEPT) encountered */ |
BOOL had_accept; /* (*ACCEPT) encountered */ |
| 1599 |
BOOL check_lookbehind; /* Lookbehinds need later checking */ |
BOOL check_lookbehind; /* Lookbehinds need later checking */ |
| 1600 |
int nltype; /* Newline type */ |
int nltype; /* Newline type */ |
| 1601 |
int nllen; /* Newline string length */ |
int nllen; /* Newline string length */ |
| 1602 |
uschar nl[4]; /* Newline string when fixed length */ |
uschar nl[4]; /* Newline string when fixed length */ |
| 1607 |
|
|
| 1608 |
typedef struct branch_chain { |
typedef struct branch_chain { |
| 1609 |
struct branch_chain *outer; |
struct branch_chain *outer; |
| 1610 |
uschar *current; |
uschar *current_branch; |
| 1611 |
} branch_chain; |
} branch_chain; |
| 1612 |
|
|
| 1613 |
/* Structure for items in a linked list that represents an explicit recursive |
/* Structure for items in a linked list that represents an explicit recursive |
| 1620 |
USPTR save_start; /* Old value of mstart */ |
USPTR save_start; /* Old value of mstart */ |
| 1621 |
int *offset_save; /* Pointer to start of saved offsets */ |
int *offset_save; /* Pointer to start of saved offsets */ |
| 1622 |
int saved_max; /* Number of saved offsets */ |
int saved_max; /* Number of saved offsets */ |
| 1623 |
int offset_top; /* Current value of offset_top */ |
int save_offset_top; /* Current value of offset_top */ |
| 1624 |
} recursion_info; |
} recursion_info; |
| 1625 |
|
|
| 1626 |
/* Structure for building a chain of data for holding the values of the subject |
/* Structure for building a chain of data for holding the values of the subject |
| 1645 |
int offset_max; /* The maximum usable for return data */ |
int offset_max; /* The maximum usable for return data */ |
| 1646 |
int nltype; /* Newline type */ |
int nltype; /* Newline type */ |
| 1647 |
int nllen; /* Newline string length */ |
int nllen; /* Newline string length */ |
| 1648 |
|
int name_count; /* Number of names in name table */ |
| 1649 |
|
int name_entry_size; /* Size of entry in names table */ |
| 1650 |
|
uschar *name_table; /* Table of names */ |
| 1651 |
uschar nl[4]; /* Newline string when fixed */ |
uschar nl[4]; /* Newline string when fixed */ |
| 1652 |
const uschar *lcc; /* Points to lower casing table */ |
const uschar *lcc; /* Points to lower casing table */ |
| 1653 |
const uschar *ctypes; /* Points to table of type maps */ |
const uschar *ctypes; /* Points to table of type maps */ |
| 1765 |
one of the exported public functions. They have to be "external" in the C |
one of the exported public functions. They have to be "external" in the C |
| 1766 |
sense, but are not part of the PCRE public API. */ |
sense, but are not part of the PCRE public API. */ |
| 1767 |
|
|
| 1768 |
extern BOOL _pcre_is_newline(const uschar *, int, const uschar *, |
extern const uschar *_pcre_find_bracket(const uschar *, BOOL, int); |
| 1769 |
int *, BOOL); |
extern BOOL _pcre_is_newline(USPTR, int, USPTR, int *, BOOL); |
| 1770 |
extern int _pcre_ord2utf8(int, uschar *); |
extern int _pcre_ord2utf8(int, uschar *); |
| 1771 |
extern real_pcre *_pcre_try_flipped(const real_pcre *, real_pcre *, |
extern real_pcre *_pcre_try_flipped(const real_pcre *, real_pcre *, |
| 1772 |
const pcre_study_data *, pcre_study_data *); |
const pcre_study_data *, pcre_study_data *); |
| 1773 |
extern int _pcre_valid_utf8(const uschar *, int); |
extern int _pcre_valid_utf8(USPTR, int); |
| 1774 |
extern BOOL _pcre_was_newline(const uschar *, int, const uschar *, |
extern BOOL _pcre_was_newline(USPTR, int, USPTR, int *, BOOL); |
| 1775 |
int *, BOOL); |
extern BOOL _pcre_xclass(int, const uschar *); |
|
extern BOOL _pcre_xclass(int, const uschar *); |
|
| 1776 |
|
|
| 1777 |
|
|
| 1778 |
/* Unicode character database (UCD) */ |
/* Unicode character database (UCD) */ |