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