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