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