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-2013 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_fullinfo(), which returns
42 information about a compiled pattern. */
43 
44 /* %ExternalCopyright% */
45 
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49 
50 #include "pcre_internal.h"
51 
52 
53 /*************************************************
54 *        Return info about compiled pattern      *
55 *************************************************/
56 
57 /* This is a newer "info" function which has an extensible interface so
58 that additional items can be added compatibly.
59 
60 Arguments:
61   argument_re      points to compiled code
62   extra_data       points extra data, or NULL
63   what             what information is required
64   where            where to put the information
65 
66 Returns:           0 if data returned, negative on error
67 */
68 
69 #if defined COMPILE_PCRE8
70 #if defined(ERLANG_INTEGRATION)
71 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
erts_pcre_fullinfo(const pcre * argument_re,const erts_pcre_extra * extra_data,int what,void * where)72 erts_pcre_fullinfo(const pcre *argument_re, const erts_pcre_extra *extra_data,
73   int what, void *where)
74 #else
75 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
76 pcre_fullinfo(const pcre *argument_re, const pcre_extra *extra_data,
77   int what, void *where)
78 #endif
79 #elif defined COMPILE_PCRE16
80 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
81 pcre16_fullinfo(const pcre16 *argument_re, const pcre16_extra *extra_data,
82   int what, void *where)
83 #elif defined COMPILE_PCRE32
84 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
85 pcre32_fullinfo(const pcre32 *argument_re, const pcre32_extra *extra_data,
86   int what, void *where)
87 #endif
88 {
89 const REAL_PCRE *re = (const REAL_PCRE *)argument_re;
90 const pcre_study_data *study = NULL;
91 
92 if (re == NULL || where == NULL) return PCRE_ERROR_NULL;
93 
94 if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_STUDY_DATA) != 0)
95   study = (const pcre_study_data *)extra_data->study_data;
96 
97 /* Check that the first field in the block is the magic number. If it is not,
98 return with PCRE_ERROR_BADMAGIC. However, if the magic number is equal to
99 REVERSED_MAGIC_NUMBER we return with PCRE_ERROR_BADENDIANNESS, which
100 means that the pattern is likely compiled with different endianness. */
101 
102 if (re->magic_number != MAGIC_NUMBER)
103   return re->magic_number == REVERSED_MAGIC_NUMBER?
104     PCRE_ERROR_BADENDIANNESS:PCRE_ERROR_BADMAGIC;
105 
106 /* Check that this pattern was compiled in the correct bit mode */
107 
108 if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
109 
110 switch (what)
111   {
112   case PCRE_INFO_OPTIONS:
113   *((unsigned long int *)where) = re->options & PUBLIC_COMPILE_OPTIONS;
114   break;
115 
116   case PCRE_INFO_SIZE:
117   *((size_t *)where) = re->size;
118   break;
119 
120   case PCRE_INFO_STUDYSIZE:
121   *((size_t *)where) = (study == NULL)? 0 : study->size;
122   break;
123 
124   case PCRE_INFO_JITSIZE:
125 #ifdef SUPPORT_JIT
126   *((size_t *)where) =
127       (extra_data != NULL &&
128       (extra_data->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
129       extra_data->executable_jit != NULL)?
130     PRIV(jit_get_size)(extra_data->executable_jit) : 0;
131 #else
132   *((size_t *)where) = 0;
133 #endif
134   break;
135 
136   case PCRE_INFO_CAPTURECOUNT:
137   *((int *)where) = re->top_bracket;
138   break;
139 
140   case PCRE_INFO_BACKREFMAX:
141   *((int *)where) = re->top_backref;
142   break;
143 
144   case PCRE_INFO_FIRSTBYTE:
145   *((int *)where) =
146     ((re->flags & PCRE_FIRSTSET) != 0)? (int)re->first_char :
147     ((re->flags & PCRE_STARTLINE) != 0)? -1 : -2;
148   break;
149 
150   case PCRE_INFO_FIRSTCHARACTER:
151     *((pcre_uint32 *)where) =
152       (re->flags & PCRE_FIRSTSET) != 0 ? re->first_char : 0;
153     break;
154 
155   case PCRE_INFO_FIRSTCHARACTERFLAGS:
156     *((int *)where) =
157       ((re->flags & PCRE_FIRSTSET) != 0) ? 1 :
158       ((re->flags & PCRE_STARTLINE) != 0) ? 2 : 0;
159     break;
160 
161   /* Make sure we pass back the pointer to the bit vector in the external
162   block, not the internal copy (with flipped integer fields). */
163 
164   case PCRE_INFO_FIRSTTABLE:
165   *((const pcre_uint8 **)where) =
166     (study != NULL && (study->flags & PCRE_STUDY_MAPPED) != 0)?
167       ((const pcre_study_data *)extra_data->study_data)->start_bits : NULL;
168   break;
169 
170   case PCRE_INFO_MINLENGTH:
171   *((int *)where) =
172     (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0)?
173       (int)(study->minlength) : -1;
174   break;
175 
176   case PCRE_INFO_JIT:
177   *((int *)where) = extra_data != NULL &&
178                     (extra_data->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
179                     extra_data->executable_jit != NULL;
180   break;
181 
182   case PCRE_INFO_LASTLITERAL:
183   *((int *)where) =
184     ((re->flags & PCRE_REQCHSET) != 0)? (int)re->req_char : -1;
185   break;
186 
187   case PCRE_INFO_REQUIREDCHAR:
188     *((pcre_uint32 *)where) =
189       ((re->flags & PCRE_REQCHSET) != 0) ? re->req_char : 0;
190     break;
191 
192   case PCRE_INFO_REQUIREDCHARFLAGS:
193     *((int *)where) =
194       ((re->flags & PCRE_REQCHSET) != 0);
195     break;
196 
197   case PCRE_INFO_NAMEENTRYSIZE:
198   *((int *)where) = re->name_entry_size;
199   break;
200 
201   case PCRE_INFO_NAMECOUNT:
202   *((int *)where) = re->name_count;
203   break;
204 
205   case PCRE_INFO_NAMETABLE:
206   *((const pcre_uchar **)where) = (const pcre_uchar *)re + re->name_table_offset;
207   break;
208 
209   case PCRE_INFO_DEFAULT_TABLES:
210   *((const pcre_uint8 **)where) = (const pcre_uint8 *)(PRIV(default_tables));
211   break;
212 
213   /* From release 8.00 this will always return TRUE because NOPARTIAL is
214   no longer ever set (the restrictions have been removed). */
215 
216   case PCRE_INFO_OKPARTIAL:
217   *((int *)where) = (re->flags & PCRE_NOPARTIAL) == 0;
218   break;
219 
220   case PCRE_INFO_JCHANGED:
221   *((int *)where) = (re->flags & PCRE_JCHANGED) != 0;
222   break;
223 
224   case PCRE_INFO_HASCRORLF:
225   *((int *)where) = (re->flags & PCRE_HASCRORLF) != 0;
226   break;
227 
228   case PCRE_INFO_MAXLOOKBEHIND:
229   *((int *)where) = re->max_lookbehind;
230   break;
231 
232   case PCRE_INFO_MATCHLIMIT:
233   if ((re->flags & PCRE_MLSET) == 0) return PCRE_ERROR_UNSET;
234   *((pcre_uint32 *)where) = re->limit_match;
235   break;
236 
237   case PCRE_INFO_RECURSIONLIMIT:
238   if ((re->flags & PCRE_RLSET) == 0) return PCRE_ERROR_UNSET;
239   *((pcre_uint32 *)where) = re->limit_recursion;
240   break;
241 
242   case PCRE_INFO_MATCH_EMPTY:
243   *((int *)where) = (re->flags & PCRE_MATCH_EMPTY) != 0;
244   break;
245 
246   default: return PCRE_ERROR_BADOPTION;
247   }
248 
249 return 0;
250 }
251 
252 /* End of pcre_fullinfo.c */
253