| 1 | /************************************************* | 
|---|
| 2 | *      Perl-Compatible Regular Expressions       * | 
|---|
| 3 | *************************************************/ | 
|---|
| 4 |  | 
|---|
| 5 | /* PCRE is a library of functions to support regular expressions whose syntax | 
|---|
| 6 | and semantics are as close as possible to those of the Perl 5 language. | 
|---|
| 7 |  | 
|---|
| 8 | Written by Philip Hazel | 
|---|
| 9 | Copyright (c) 1997-2012 University of Cambridge | 
|---|
| 10 |  | 
|---|
| 11 | ----------------------------------------------------------------------------- | 
|---|
| 12 | Redistribution and use in source and binary forms, with or without | 
|---|
| 13 | modification, are permitted provided that the following conditions are met: | 
|---|
| 14 |  | 
|---|
| 15 | * Redistributions of source code must retain the above copyright notice, | 
|---|
| 16 | this list of conditions and the following disclaimer. | 
|---|
| 17 |  | 
|---|
| 18 | * Redistributions in binary form must reproduce the above copyright | 
|---|
| 19 | notice, this list of conditions and the following disclaimer in the | 
|---|
| 20 | documentation and/or other materials provided with the distribution. | 
|---|
| 21 |  | 
|---|
| 22 | * Neither the name of the University of Cambridge nor the names of its | 
|---|
| 23 | contributors may be used to endorse or promote products derived from | 
|---|
| 24 | this software without specific prior written permission. | 
|---|
| 25 |  | 
|---|
| 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
|---|
| 27 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 28 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 29 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | 
|---|
| 30 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 
|---|
| 31 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 
|---|
| 32 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 
|---|
| 33 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 
|---|
| 34 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 
|---|
| 35 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
|---|
| 36 | POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 37 | ----------------------------------------------------------------------------- | 
|---|
| 38 | */ | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | /* This module contains the external function pcre_maketables(), which builds | 
|---|
| 42 | character tables for PCRE in the current locale. The file is compiled on its | 
|---|
| 43 | own as part of the PCRE library. However, it is also included in the | 
|---|
| 44 | compilation of dftables.c, in which case the macro DFTABLES is defined. */ | 
|---|
| 45 |  | 
|---|
| 46 | #pragma warning( disable : 4244)  // conversion from 'int' to 'unsigned short', possible loss of data | 
|---|
| 47 |  | 
|---|
| 48 | #ifndef DFTABLES | 
|---|
| 49 | #  include "pcre_config.h" | 
|---|
| 50 | #  include "pcre_internal.h" | 
|---|
| 51 | #endif | 
|---|
| 52 |  | 
|---|
| 53 |  | 
|---|
| 54 | /************************************************* | 
|---|
| 55 | *           Create PCRE character tables         * | 
|---|
| 56 | *************************************************/ | 
|---|
| 57 |  | 
|---|
| 58 | /* This function builds a set of character tables for use by PCRE and returns | 
|---|
| 59 | a pointer to them. They are build using the ctype functions, and consequently | 
|---|
| 60 | their contents will depend upon the current locale setting. When compiled as | 
|---|
| 61 | part of the library, the store is obtained via PUBL(malloc)(), but when | 
|---|
| 62 | compiled inside dftables, use malloc(). | 
|---|
| 63 |  | 
|---|
| 64 | Arguments:   none | 
|---|
| 65 | Returns:     pointer to the contiguous block of data | 
|---|
| 66 | */ | 
|---|
| 67 |  | 
|---|
| 68 | #if defined COMPILE_PCRE8 | 
|---|
| 69 | const unsigned char * | 
|---|
| 70 | pcre_maketables(void) | 
|---|
| 71 | #elif defined COMPILE_PCRE16 | 
|---|
| 72 | const unsigned char * | 
|---|
| 73 | pcre16_maketables(void) | 
|---|
| 74 | #elif defined COMPILE_PCRE32 | 
|---|
| 75 | const unsigned char * | 
|---|
| 76 | pcre32_maketables(void) | 
|---|
| 77 | #endif | 
|---|
| 78 | { | 
|---|
| 79 | unsigned char *yield, *p; | 
|---|
| 80 | int i; | 
|---|
| 81 |  | 
|---|
| 82 | #ifndef DFTABLES | 
|---|
| 83 | yield = (unsigned char*)(PUBL(malloc))(tables_length); | 
|---|
| 84 | #else | 
|---|
| 85 | yield = (unsigned char*)malloc(tables_length); | 
|---|
| 86 | #endif | 
|---|
| 87 |  | 
|---|
| 88 | if (yield == NULL) return NULL; | 
|---|
| 89 | p = yield; | 
|---|
| 90 |  | 
|---|
| 91 | /* First comes the lower casing table */ | 
|---|
| 92 |  | 
|---|
| 93 | for (i = 0; i < 256; i++) *p++ = tolower(i); | 
|---|
| 94 |  | 
|---|
| 95 | /* Next the case-flipping table */ | 
|---|
| 96 |  | 
|---|
| 97 | for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i); | 
|---|
| 98 |  | 
|---|
| 99 | /* Then the character class tables. Don't try to be clever and save effort on | 
|---|
| 100 | exclusive ones - in some locales things may be different. | 
|---|
| 101 |  | 
|---|
| 102 | Note that the table for "space" includes everything "isspace" gives, including | 
|---|
| 103 | VT in the default locale. This makes it work for the POSIX class [:space:]. | 
|---|
| 104 | From release 8.34 is is also correct for Perl space, because Perl added VT at | 
|---|
| 105 | release 5.18. | 
|---|
| 106 |  | 
|---|
| 107 | Note also that it is possible for a character to be alnum or alpha without | 
|---|
| 108 | being lower or upper, such as "male and female ordinals" (\xAA and \xBA) in the | 
|---|
| 109 | fr_FR locale (at least under Debian Linux's locales as of 12/2005). So we must | 
|---|
| 110 | test for alnum specially. */ | 
|---|
| 111 |  | 
|---|
| 112 | memset(p, 0, cbit_length); | 
|---|
| 113 | for (i = 0; i < 256; i++) | 
|---|
| 114 | { | 
|---|
| 115 | if (isdigit(i)) p[cbit_digit  + i/8] |= 1 << (i&7); | 
|---|
| 116 | if (isupper(i)) p[cbit_upper  + i/8] |= 1 << (i&7); | 
|---|
| 117 | if (islower(i)) p[cbit_lower  + i/8] |= 1 << (i&7); | 
|---|
| 118 | if (isalnum(i)) p[cbit_word   + i/8] |= 1 << (i&7); | 
|---|
| 119 | if (i == '_')   p[cbit_word   + i/8] |= 1 << (i&7); | 
|---|
| 120 | if (isspace(i)) p[cbit_space  + i/8] |= 1 << (i&7); | 
|---|
| 121 | if (isxdigit(i))p[cbit_xdigit + i/8] |= 1 << (i&7); | 
|---|
| 122 | if (isgraph(i)) p[cbit_graph  + i/8] |= 1 << (i&7); | 
|---|
| 123 | if (isprint(i)) p[cbit_print  + i/8] |= 1 << (i&7); | 
|---|
| 124 | if (ispunct(i)) p[cbit_punct  + i/8] |= 1 << (i&7); | 
|---|
| 125 | if (iscntrl(i)) p[cbit_cntrl  + i/8] |= 1 << (i&7); | 
|---|
| 126 | } | 
|---|
| 127 | p += cbit_length; | 
|---|
| 128 |  | 
|---|
| 129 | /* Finally, the character type table. In this, we used to exclude VT from the | 
|---|
| 130 | white space chars, because Perl didn't recognize it as such for \s and for | 
|---|
| 131 | comments within regexes. However, Perl changed at release 5.18, so PCRE changed | 
|---|
| 132 | at release 8.34. */ | 
|---|
| 133 |  | 
|---|
| 134 | for (i = 0; i < 256; i++) | 
|---|
| 135 | { | 
|---|
| 136 | int x = 0; | 
|---|
| 137 | if (isspace(i)) x += ctype_space; | 
|---|
| 138 | if (isalpha(i)) x += ctype_letter; | 
|---|
| 139 | if (isdigit(i)) x += ctype_digit; | 
|---|
| 140 | if (isxdigit(i)) x += ctype_xdigit; | 
|---|
| 141 | if (isalnum(i) || i == '_') x += ctype_word; | 
|---|
| 142 |  | 
|---|
| 143 | /* Note: strchr includes the terminating zero in the characters it considers. | 
|---|
| 144 | In this instance, that is ok because we want binary zero to be flagged as a | 
|---|
| 145 | meta-character, which in this sense is any character that terminates a run | 
|---|
| 146 | of data characters. */ | 
|---|
| 147 |  | 
|---|
| 148 | if (strchr( "\\*+?{^.$|()[", i) != 0) x += ctype_meta; | 
|---|
| 149 | *p++ = x; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | return yield; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | /* End of pcre_maketables.c */ | 
|---|
| 156 |  | 
|---|