Updated embedded PCRE to version 7.4 to avoid 2 CVE issues:-
[exim.git] / src / src / pcre / pcre_try_flipped.c
1 /* $Cambridge: exim/src/src/pcre/pcre_try_flipped.c,v 1.6 2007/11/12 13:02:20 nm4 Exp $ */
2
3 /*************************************************
4 *      Perl-Compatible Regular Expressions       *
5 *************************************************/
6
7 /* PCRE is a library of functions to support regular expressions whose syntax
8 and semantics are as close as possible to those of the Perl 5 language.
9
10                        Written by Philip Hazel
11            Copyright (c) 1997-2007 University of Cambridge
12
13 -----------------------------------------------------------------------------
14 Redistribution and use in source and binary forms, with or without
15 modification, 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
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 POSSIBILITY OF SUCH DAMAGE.
39 -----------------------------------------------------------------------------
40 */
41
42
43 /* This module contains an internal function that tests a compiled pattern to
44 see if it was compiled with the opposite endianness. If so, it uses an
45 auxiliary local function to flip the appropriate bytes. */
46
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include "pcre_internal.h"
53
54
55 /*************************************************
56 *         Flip bytes in an integer               *
57 *************************************************/
58
59 /* This function is called when the magic number in a regex doesn't match, in
60 order to flip its bytes to see if we are dealing with a pattern that was
61 compiled on a host of different endianness. If so, this function is used to
62 flip other byte values.
63
64 Arguments:
65   value        the number to flip
66   n            the number of bytes to flip (assumed to be 2 or 4)
67
68 Returns:       the flipped value
69 */
70
71 static unsigned long int
72 byteflip(unsigned long int value, int n)
73 {
74 if (n == 2) return ((value & 0x00ff) << 8) | ((value & 0xff00) >> 8);
75 return ((value & 0x000000ff) << 24) |
76        ((value & 0x0000ff00) <<  8) |
77        ((value & 0x00ff0000) >>  8) |
78        ((value & 0xff000000) >> 24);
79 }
80
81
82
83 /*************************************************
84 *       Test for a byte-flipped compiled regex   *
85 *************************************************/
86
87 /* This function is called from pcre_exec(), pcre_dfa_exec(), and also from
88 pcre_fullinfo(). Its job is to test whether the regex is byte-flipped - that
89 is, it was compiled on a system of opposite endianness. The function is called
90 only when the native MAGIC_NUMBER test fails. If the regex is indeed flipped,
91 we flip all the relevant values into a different data block, and return it.
92
93 Arguments:
94   re               points to the regex
95   study            points to study data, or NULL
96   internal_re      points to a new regex block
97   internal_study   points to a new study block
98
99 Returns:           the new block if is is indeed a byte-flipped regex
100                    NULL if it is not
101 */
102
103 real_pcre *
104 _pcre_try_flipped(const real_pcre *re, real_pcre *internal_re,
105   const pcre_study_data *study, pcre_study_data *internal_study)
106 {
107 if (byteflip(re->magic_number, sizeof(re->magic_number)) != MAGIC_NUMBER)
108   return NULL;
109
110 *internal_re = *re;           /* To copy other fields */
111 internal_re->size = byteflip(re->size, sizeof(re->size));
112 internal_re->options = byteflip(re->options, sizeof(re->options));
113 internal_re->flags = (pcre_uint16)byteflip(re->flags, sizeof(re->flags));
114 internal_re->top_bracket =
115   (pcre_uint16)byteflip(re->top_bracket, sizeof(re->top_bracket));
116 internal_re->top_backref =
117   (pcre_uint16)byteflip(re->top_backref, sizeof(re->top_backref));
118 internal_re->first_byte =
119   (pcre_uint16)byteflip(re->first_byte, sizeof(re->first_byte));
120 internal_re->req_byte =
121   (pcre_uint16)byteflip(re->req_byte, sizeof(re->req_byte));
122 internal_re->name_table_offset =
123   (pcre_uint16)byteflip(re->name_table_offset, sizeof(re->name_table_offset));
124 internal_re->name_entry_size =
125   (pcre_uint16)byteflip(re->name_entry_size, sizeof(re->name_entry_size));
126 internal_re->name_count =
127   (pcre_uint16)byteflip(re->name_count, sizeof(re->name_count));
128
129 if (study != NULL)
130   {
131   *internal_study = *study;   /* To copy other fields */
132   internal_study->size = byteflip(study->size, sizeof(study->size));
133   internal_study->options = byteflip(study->options, sizeof(study->options));
134   }
135
136 return internal_re;
137 }
138
139 /* End of pcre_tryflipped.c */