bugzilla 612 - write recipients list in X-Envelope-To header of MBOX spool file
[exim.git] / src / src / pcre / pcre_maketables.c
CommitLineData
47db1125 1/* $Cambridge: exim/src/src/pcre/pcre_maketables.c,v 1.6 2007/11/12 13:02:20 nm4 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
8and semantics are as close as possible to those of the Perl 5 language.
9
10 Written by Philip Hazel
64f2600a 11 Copyright (c) 1997-2007 University of Cambridge
8ac170f3
PH
12
13-----------------------------------------------------------------------------
14Redistribution and use in source and binary forms, with or without
15modification, 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
28THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38POSSIBILITY OF SUCH DAMAGE.
39-----------------------------------------------------------------------------
40*/
41
42
43/* This module contains the external function pcre_maketables(), which builds
44character tables for PCRE in the current locale. The file is compiled on its
45own as part of the PCRE library. However, it is also included in the
46compilation of dftables.c, in which case the macro DFTABLES is defined. */
47
48
49#ifndef DFTABLES
47db1125
NM
50# ifdef HAVE_CONFIG_H
51# include "config.h"
52# endif
53# include "pcre_internal.h"
8ac170f3
PH
54#endif
55
56
57/*************************************************
58* Create PCRE character tables *
59*************************************************/
60
61/* This function builds a set of character tables for use by PCRE and returns
62a pointer to them. They are build using the ctype functions, and consequently
63their contents will depend upon the current locale setting. When compiled as
64part of the library, the store is obtained via pcre_malloc(), but when compiled
65inside dftables, use malloc().
66
67Arguments: none
68Returns: pointer to the contiguous block of data
69*/
70
71const unsigned char *
72pcre_maketables(void)
73{
74unsigned char *yield, *p;
75int i;
76
77#ifndef DFTABLES
78yield = (unsigned char*)(pcre_malloc)(tables_length);
79#else
80yield = (unsigned char*)malloc(tables_length);
81#endif
82
83if (yield == NULL) return NULL;
84p = yield;
85
86/* First comes the lower casing table */
87
88for (i = 0; i < 256; i++) *p++ = tolower(i);
89
90/* Next the case-flipping table */
91
92for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
93
aa41d2de
PH
94/* Then the character class tables. Don't try to be clever and save effort on
95exclusive ones - in some locales things may be different. Note that the table
96for "space" includes everything "isspace" gives, including VT in the default
97locale. This makes it work for the POSIX class [:space:]. Note also that it is
98possible for a character to be alnum or alpha without being lower or upper,
99such as "male and female ordinals" (\xAA and \xBA) in the fr_FR locale (at
100least under Debian Linux's locales as of 12/2005). So we must test for alnum
101specially. */
8ac170f3
PH
102
103memset(p, 0, cbit_length);
104for (i = 0; i < 256; i++)
105 {
aa41d2de
PH
106 if (isdigit(i)) p[cbit_digit + i/8] |= 1 << (i&7);
107 if (isupper(i)) p[cbit_upper + i/8] |= 1 << (i&7);
108 if (islower(i)) p[cbit_lower + i/8] |= 1 << (i&7);
109 if (isalnum(i)) p[cbit_word + i/8] |= 1 << (i&7);
8ac170f3
PH
110 if (i == '_') p[cbit_word + i/8] |= 1 << (i&7);
111 if (isspace(i)) p[cbit_space + i/8] |= 1 << (i&7);
112 if (isxdigit(i))p[cbit_xdigit + i/8] |= 1 << (i&7);
113 if (isgraph(i)) p[cbit_graph + i/8] |= 1 << (i&7);
114 if (isprint(i)) p[cbit_print + i/8] |= 1 << (i&7);
115 if (ispunct(i)) p[cbit_punct + i/8] |= 1 << (i&7);
116 if (iscntrl(i)) p[cbit_cntrl + i/8] |= 1 << (i&7);
117 }
118p += cbit_length;
119
120/* Finally, the character type table. In this, we exclude VT from the white
121space chars, because Perl doesn't recognize it as such for \s and for comments
122within regexes. */
123
124for (i = 0; i < 256; i++)
125 {
126 int x = 0;
127 if (i != 0x0b && isspace(i)) x += ctype_space;
128 if (isalpha(i)) x += ctype_letter;
129 if (isdigit(i)) x += ctype_digit;
130 if (isxdigit(i)) x += ctype_xdigit;
131 if (isalnum(i) || i == '_') x += ctype_word;
132
133 /* Note: strchr includes the terminating zero in the characters it considers.
134 In this instance, that is ok because we want binary zero to be flagged as a
135 meta-character, which in this sense is any character that terminates a run
136 of data characters. */
137
6bf342e1 138 if (strchr("\\*+?{^.$|()[", i) != 0) x += ctype_meta;
aa41d2de
PH
139 *p++ = x;
140 }
8ac170f3
PH
141
142return yield;
143}
144
145/* End of pcre_maketables.c */