Back to index

php5  5.3.10
pcre_try_flipped.c
Go to the documentation of this file.
00001 /*************************************************
00002 *      Perl-Compatible Regular Expressions       *
00003 *************************************************/
00004 
00005 /* PCRE is a library of functions to support regular expressions whose syntax
00006 and semantics are as close as possible to those of the Perl 5 language.
00007 
00008                        Written by Philip Hazel
00009            Copyright (c) 1997-2009 University of Cambridge
00010 
00011 -----------------------------------------------------------------------------
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014 
00015     * Redistributions of source code must retain the above copyright notice,
00016       this list of conditions and the following disclaimer.
00017 
00018     * Redistributions in binary form must reproduce the above copyright
00019       notice, this list of conditions and the following disclaimer in the
00020       documentation and/or other materials provided with the distribution.
00021 
00022     * Neither the name of the University of Cambridge nor the names of its
00023       contributors may be used to endorse or promote products derived from
00024       this software without specific prior written permission.
00025 
00026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036 POSSIBILITY OF SUCH DAMAGE.
00037 -----------------------------------------------------------------------------
00038 */
00039 
00040 
00041 /* This module contains an internal function that tests a compiled pattern to
00042 see if it was compiled with the opposite endianness. If so, it uses an
00043 auxiliary local function to flip the appropriate bytes. */
00044 
00045 
00046 #include "config.h"
00047 
00048 #include "pcre_internal.h"
00049 
00050 
00051 /*************************************************
00052 *         Flip bytes in an integer               *
00053 *************************************************/
00054 
00055 /* This function is called when the magic number in a regex doesn't match, in
00056 order to flip its bytes to see if we are dealing with a pattern that was
00057 compiled on a host of different endianness. If so, this function is used to
00058 flip other byte values.
00059 
00060 Arguments:
00061   value        the number to flip
00062   n            the number of bytes to flip (assumed to be 2 or 4)
00063 
00064 Returns:       the flipped value
00065 */
00066 
00067 static unsigned long int
00068 byteflip(unsigned long int value, int n)
00069 {
00070 if (n == 2) return ((value & 0x00ff) << 8) | ((value & 0xff00) >> 8);
00071 return ((value & 0x000000ff) << 24) |
00072        ((value & 0x0000ff00) <<  8) |
00073        ((value & 0x00ff0000) >>  8) |
00074        ((value & 0xff000000) >> 24);
00075 }
00076 
00077 
00078 
00079 /*************************************************
00080 *       Test for a byte-flipped compiled regex   *
00081 *************************************************/
00082 
00083 /* This function is called from pcre_exec(), pcre_dfa_exec(), and also from
00084 pcre_fullinfo(). Its job is to test whether the regex is byte-flipped - that
00085 is, it was compiled on a system of opposite endianness. The function is called
00086 only when the native MAGIC_NUMBER test fails. If the regex is indeed flipped,
00087 we flip all the relevant values into a different data block, and return it.
00088 
00089 Arguments:
00090   re               points to the regex
00091   study            points to study data, or NULL
00092   internal_re      points to a new regex block
00093   internal_study   points to a new study block
00094 
00095 Returns:           the new block if is is indeed a byte-flipped regex
00096                    NULL if it is not
00097 */
00098 
00099 real_pcre *
00100 _pcre_try_flipped(const real_pcre *re, real_pcre *internal_re,
00101   const pcre_study_data *study, pcre_study_data *internal_study)
00102 {
00103 if (byteflip(re->magic_number, sizeof(re->magic_number)) != MAGIC_NUMBER)
00104   return NULL;
00105 
00106 *internal_re = *re;           /* To copy other fields */
00107 internal_re->size = byteflip(re->size, sizeof(re->size));
00108 internal_re->options = byteflip(re->options, sizeof(re->options));
00109 internal_re->flags = (pcre_uint16)byteflip(re->flags, sizeof(re->flags));
00110 internal_re->top_bracket =
00111   (pcre_uint16)byteflip(re->top_bracket, sizeof(re->top_bracket));
00112 internal_re->top_backref =
00113   (pcre_uint16)byteflip(re->top_backref, sizeof(re->top_backref));
00114 internal_re->first_byte =
00115   (pcre_uint16)byteflip(re->first_byte, sizeof(re->first_byte));
00116 internal_re->req_byte =
00117   (pcre_uint16)byteflip(re->req_byte, sizeof(re->req_byte));
00118 internal_re->name_table_offset =
00119   (pcre_uint16)byteflip(re->name_table_offset, sizeof(re->name_table_offset));
00120 internal_re->name_entry_size =
00121   (pcre_uint16)byteflip(re->name_entry_size, sizeof(re->name_entry_size));
00122 internal_re->name_count =
00123   (pcre_uint16)byteflip(re->name_count, sizeof(re->name_count));
00124 
00125 if (study != NULL)
00126   {
00127   *internal_study = *study;   /* To copy other fields */
00128   internal_study->size = byteflip(study->size, sizeof(study->size));
00129   internal_study->flags = byteflip(study->flags, sizeof(study->flags));
00130   internal_study->minlength = byteflip(study->minlength,
00131     sizeof(study->minlength));
00132   }
00133 
00134 return internal_re;
00135 }
00136 
00137 /* End of pcre_tryflipped.c */