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