• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/pcre/pcre_try_flipped.c

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 #ifdef HAVE_CONFIG_H
00047 #include "config.h"
00048 #else if defined(_WINDOWS)
00049 #include <spl/configwin32.h>
00050 #endif
00051 
00052 
00053 #include "pcre_internal.h"
00054 
00055 
00056 /*************************************************
00057 *         Flip bytes in an integer               *
00058 *************************************************/
00059 
00060 /* This function is called when the magic number in a regex doesn't match, in
00061 order to flip its bytes to see if we are dealing with a pattern that was
00062 compiled on a host of different endianness. If so, this function is used to
00063 flip other byte values.
00064 
00065 Arguments:
00066   value        the number to flip
00067   n            the number of bytes to flip (assumed to be 2 or 4)
00068 
00069 Returns:       the flipped value
00070 */
00071 
00072 static unsigned long int
00073 byteflip(unsigned long int value, int n)
00074 {
00075 if (n == 2) return ((value & 0x00ff) << 8) | ((value & 0xff00) >> 8);
00076 return ((value & 0x000000ff) << 24) |
00077        ((value & 0x0000ff00) <<  8) |
00078        ((value & 0x00ff0000) >>  8) |
00079        ((value & 0xff000000) >> 24);
00080 }
00081 
00082 
00083 
00084 /*************************************************
00085 *       Test for a byte-flipped compiled regex   *
00086 *************************************************/
00087 
00088 /* This function is called from pcre_exec(), pcre_dfa_exec(), and also from
00089 pcre_fullinfo(). Its job is to test whether the regex is byte-flipped - that
00090 is, it was compiled on a system of opposite endianness. The function is called
00091 only when the native MAGIC_NUMBER test fails. If the regex is indeed flipped,
00092 we flip all the relevant values into a different data block, and return it.
00093 
00094 Arguments:
00095   re               points to the regex
00096   study            points to study data, or NULL
00097   internal_re      points to a new regex block
00098   internal_study   points to a new study block
00099 
00100 Returns:           the new block if is is indeed a byte-flipped regex
00101                    NULL if it is not
00102 */
00103 
00104 real_pcre *
00105 _pcre_try_flipped(const real_pcre *re, real_pcre *internal_re,
00106   const pcre_study_data *study, pcre_study_data *internal_study)
00107 {
00108 if (byteflip(re->magic_number, sizeof(re->magic_number)) != MAGIC_NUMBER)
00109   return NULL;
00110 
00111 *internal_re = *re;           /* To copy other fields */
00112 internal_re->size = byteflip(re->size, sizeof(re->size));
00113 internal_re->options = byteflip(re->options, sizeof(re->options));
00114 internal_re->flags = (pcre_uint16)byteflip(re->flags, sizeof(re->flags));
00115 internal_re->top_bracket =
00116   (pcre_uint16)byteflip(re->top_bracket, sizeof(re->top_bracket));
00117 internal_re->top_backref =
00118   (pcre_uint16)byteflip(re->top_backref, sizeof(re->top_backref));
00119 internal_re->first_byte =
00120   (pcre_uint16)byteflip(re->first_byte, sizeof(re->first_byte));
00121 internal_re->req_byte =
00122   (pcre_uint16)byteflip(re->req_byte, sizeof(re->req_byte));
00123 internal_re->name_table_offset =
00124   (pcre_uint16)byteflip(re->name_table_offset, sizeof(re->name_table_offset));
00125 internal_re->name_entry_size =
00126   (pcre_uint16)byteflip(re->name_entry_size, sizeof(re->name_entry_size));
00127 internal_re->name_count =
00128   (pcre_uint16)byteflip(re->name_count, sizeof(re->name_count));
00129 
00130 if (study != NULL)
00131   {
00132   *internal_study = *study;   /* To copy other fields */
00133   internal_study->size = byteflip(study->size, sizeof(study->size));
00134   internal_study->flags = byteflip(study->flags, sizeof(study->flags));
00135   internal_study->minlength = byteflip(study->minlength,
00136     sizeof(study->minlength));
00137   }
00138 
00139 return internal_re;
00140 }
00141 
00142 /* End of pcre_tryflipped.c */