• 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            Copyright (c) 1997-2012 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 an internal function that tests a compiled pattern to
42 see if it was compiled with the opposite endianness. If so, it uses an
43 auxiliary local function to flip the appropriate bytes. */
44 
45 
46 #include "config.h"
47 
48 #include "pcre_internal.h"
49 
50 
51 /*************************************************
52 *             Swap byte functions                *
53 *************************************************/
54 
55 /* The following functions swap the bytes of a pcre_uint16
56 and pcre_uint32 value.
57 
58 Arguments:
59   value        any number
60 
61 Returns:       the byte swapped value
62 */
63 
64 static pcre_uint32
swap_uint32(pcre_uint32 value)65 swap_uint32(pcre_uint32 value)
66 {
67 return ((value & 0x000000ff) << 24) |
68        ((value & 0x0000ff00) <<  8) |
69        ((value & 0x00ff0000) >>  8) |
70        (value >> 24);
71 }
72 
73 static pcre_uint16
swap_uint16(pcre_uint16 value)74 swap_uint16(pcre_uint16 value)
75 {
76 return (value >> 8) | (value << 8);
77 }
78 
79 
80 /*************************************************
81 *       Test for a byte-flipped compiled regex   *
82 *************************************************/
83 
84 /* This function swaps the bytes of a compiled pattern usually
85 loaded form the disk. It also sets the tables pointer, which
86 is likely an invalid pointer after reload.
87 
88 Arguments:
89   argument_re     points to the compiled expression
90   extra_data      points to extra data or is NULL
91   tables          points to the character tables or NULL
92 
93 Returns:          0 if the swap is successful, negative on error
94 */
95 
96 #ifdef COMPILE_PCRE8
pcre_pattern_to_host_byte_order(pcre * argument_re,pcre_extra * extra_data,const unsigned char * tables)97 PCRE_EXP_DECL int pcre_pattern_to_host_byte_order(pcre *argument_re,
98   pcre_extra *extra_data, const unsigned char *tables)
99 #else
100 PCRE_EXP_DECL int pcre16_pattern_to_host_byte_order(pcre16 *argument_re,
101   pcre16_extra *extra_data, const unsigned char *tables)
102 #endif
103 {
104 REAL_PCRE *re = (REAL_PCRE *)argument_re;
105 pcre_study_data *study;
106 #ifndef COMPILE_PCRE8
107 pcre_uchar *ptr;
108 int length;
109 #ifdef SUPPORT_UTF
110 BOOL utf;
111 BOOL utf16_char;
112 #endif /* SUPPORT_UTF */
113 #endif /* !COMPILE_PCRE8 */
114 
115 if (re == NULL) return PCRE_ERROR_NULL;
116 if (re->magic_number == MAGIC_NUMBER)
117   {
118   if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
119   re->tables = tables;
120   return 0;
121   }
122 
123 if (re->magic_number != REVERSED_MAGIC_NUMBER) return PCRE_ERROR_BADMAGIC;
124 if ((swap_uint16(re->flags) & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
125 
126 re->magic_number = MAGIC_NUMBER;
127 re->size = swap_uint32(re->size);
128 re->options = swap_uint32(re->options);
129 re->flags = swap_uint16(re->flags);
130 re->top_bracket = swap_uint16(re->top_bracket);
131 re->top_backref = swap_uint16(re->top_backref);
132 re->first_char = swap_uint16(re->first_char);
133 re->req_char = swap_uint16(re->req_char);
134 re->name_table_offset = swap_uint16(re->name_table_offset);
135 re->name_entry_size = swap_uint16(re->name_entry_size);
136 re->name_count = swap_uint16(re->name_count);
137 re->ref_count = swap_uint16(re->ref_count);
138 re->tables = tables;
139 
140 if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_STUDY_DATA) != 0)
141   {
142   study = (pcre_study_data *)extra_data->study_data;
143   study->size = swap_uint32(study->size);
144   study->flags = swap_uint32(study->flags);
145   study->minlength = swap_uint32(study->minlength);
146   }
147 
148 #ifndef COMPILE_PCRE8
149 ptr = (pcre_uchar *)re + re->name_table_offset;
150 length = re->name_count * re->name_entry_size;
151 #ifdef SUPPORT_UTF
152 utf = (re->options & PCRE_UTF16) != 0;
153 utf16_char = FALSE;
154 #endif
155 
156 while(TRUE)
157   {
158   /* Swap previous characters. */
159   while (length-- > 0)
160     {
161     *ptr = swap_uint16(*ptr);
162     ptr++;
163     }
164 #ifdef SUPPORT_UTF
165   if (utf16_char)
166     {
167     if (HAS_EXTRALEN(ptr[-1]))
168       {
169       /* We know that there is only one extra character in UTF-16. */
170       *ptr = swap_uint16(*ptr);
171       ptr++;
172       }
173     }
174   utf16_char = FALSE;
175 #endif /* SUPPORT_UTF */
176 
177   /* Get next opcode. */
178   length = 0;
179   *ptr = swap_uint16(*ptr);
180   switch (*ptr)
181     {
182     case OP_END:
183     return 0;
184 
185 #ifdef SUPPORT_UTF
186     case OP_CHAR:
187     case OP_CHARI:
188     case OP_NOT:
189     case OP_NOTI:
190     case OP_STAR:
191     case OP_MINSTAR:
192     case OP_PLUS:
193     case OP_MINPLUS:
194     case OP_QUERY:
195     case OP_MINQUERY:
196     case OP_UPTO:
197     case OP_MINUPTO:
198     case OP_EXACT:
199     case OP_POSSTAR:
200     case OP_POSPLUS:
201     case OP_POSQUERY:
202     case OP_POSUPTO:
203     case OP_STARI:
204     case OP_MINSTARI:
205     case OP_PLUSI:
206     case OP_MINPLUSI:
207     case OP_QUERYI:
208     case OP_MINQUERYI:
209     case OP_UPTOI:
210     case OP_MINUPTOI:
211     case OP_EXACTI:
212     case OP_POSSTARI:
213     case OP_POSPLUSI:
214     case OP_POSQUERYI:
215     case OP_POSUPTOI:
216     case OP_NOTSTAR:
217     case OP_NOTMINSTAR:
218     case OP_NOTPLUS:
219     case OP_NOTMINPLUS:
220     case OP_NOTQUERY:
221     case OP_NOTMINQUERY:
222     case OP_NOTUPTO:
223     case OP_NOTMINUPTO:
224     case OP_NOTEXACT:
225     case OP_NOTPOSSTAR:
226     case OP_NOTPOSPLUS:
227     case OP_NOTPOSQUERY:
228     case OP_NOTPOSUPTO:
229     case OP_NOTSTARI:
230     case OP_NOTMINSTARI:
231     case OP_NOTPLUSI:
232     case OP_NOTMINPLUSI:
233     case OP_NOTQUERYI:
234     case OP_NOTMINQUERYI:
235     case OP_NOTUPTOI:
236     case OP_NOTMINUPTOI:
237     case OP_NOTEXACTI:
238     case OP_NOTPOSSTARI:
239     case OP_NOTPOSPLUSI:
240     case OP_NOTPOSQUERYI:
241     case OP_NOTPOSUPTOI:
242     if (utf) utf16_char = TRUE;
243 #endif
244     /* Fall through. */
245 
246     default:
247     length = PRIV(OP_lengths)[*ptr] - 1;
248     break;
249 
250     case OP_CLASS:
251     case OP_NCLASS:
252     /* Skip the character bit map. */
253     ptr += 32/sizeof(pcre_uchar);
254     length = 0;
255     break;
256 
257     case OP_XCLASS:
258     /* Reverse the size of the XCLASS instance. */
259     ptr++;
260     *ptr = swap_uint16(*ptr);
261     if (LINK_SIZE > 1)
262       {
263       /* LINK_SIZE can be 1 or 2 in 16 bit mode. */
264       ptr++;
265       *ptr = swap_uint16(*ptr);
266       }
267     ptr++;
268     length = (GET(ptr, -LINK_SIZE)) - (1 + LINK_SIZE + 1);
269     *ptr = swap_uint16(*ptr);
270     if ((*ptr & XCL_MAP) != 0)
271       {
272       /* Skip the character bit map. */
273       ptr += 32/sizeof(pcre_uchar);
274       length -= 32/sizeof(pcre_uchar);
275       }
276     break;
277     }
278   ptr++;
279   }
280 /* Control should never reach here in 16 bit mode. */
281 #endif /* !COMPILE_PCRE8 */
282 
283 return 0;
284 }
285 
286 /* End of pcre_byte_order.c */
287