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 internal functions for testing newlines when more than 00042 one kind of newline is to be recognized. When a newline is found, its length is 00043 returned. In principle, we could implement several newline "types", each 00044 referring to a different set of newline characters. At present, PCRE supports 00045 only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF, 00046 and NLTYPE_ANY. The full list of Unicode newline characters is taken from 00047 http://unicode.org/unicode/reports/tr18/. */ 00048 00049 00050 #ifdef HAVE_CONFIG_H 00051 #include "config.h" 00052 #endif 00053 00054 #include "pcre_internal.h" 00055 00056 00057 00058 /************************************************* 00059 * Check for newline at given position * 00060 *************************************************/ 00061 00062 /* It is guaranteed that the initial value of ptr is less than the end of the 00063 string that is being processed. 00064 00065 Arguments: 00066 ptr pointer to possible newline 00067 type the newline type 00068 endptr pointer to the end of the string 00069 lenptr where to return the length 00070 utf8 TRUE if in utf8 mode 00071 00072 Returns: TRUE or FALSE 00073 */ 00074 00075 BOOL 00076 _pcre_is_newline(const uschar *ptr, int type, const uschar *endptr, 00077 int *lenptr, BOOL utf8) 00078 { 00079 int c; 00080 if (utf8) { GETCHAR(c, ptr); } else c = *ptr; 00081 00082 if (type == NLTYPE_ANYCRLF) switch(c) 00083 { 00084 case 0x000a: *lenptr = 1; return TRUE; /* LF */ 00085 case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1; 00086 return TRUE; /* CR */ 00087 default: return FALSE; 00088 } 00089 00090 /* NLTYPE_ANY */ 00091 00092 else switch(c) 00093 { 00094 case 0x000a: /* LF */ 00095 case 0x000b: /* VT */ 00096 case 0x000c: *lenptr = 1; return TRUE; /* FF */ 00097 case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1; 00098 return TRUE; /* CR */ 00099 case 0x0085: *lenptr = utf8? 2 : 1; return TRUE; /* NEL */ 00100 case 0x2028: /* LS */ 00101 case 0x2029: *lenptr = 3; return TRUE; /* PS */ 00102 default: return FALSE; 00103 } 00104 } 00105 00106 00107 00108 /************************************************* 00109 * Check for newline at previous position * 00110 *************************************************/ 00111 00112 /* It is guaranteed that the initial value of ptr is greater than the start of 00113 the string that is being processed. 00114 00115 Arguments: 00116 ptr pointer to possible newline 00117 type the newline type 00118 startptr pointer to the start of the string 00119 lenptr where to return the length 00120 utf8 TRUE if in utf8 mode 00121 00122 Returns: TRUE or FALSE 00123 */ 00124 00125 BOOL 00126 _pcre_was_newline(const uschar *ptr, int type, const uschar *startptr, 00127 int *lenptr, BOOL utf8) 00128 { 00129 int c; 00130 ptr--; 00131 #ifdef SUPPORT_UTF8 00132 if (utf8) 00133 { 00134 BACKCHAR(ptr); 00135 GETCHAR(c, ptr); 00136 } 00137 else c = *ptr; 00138 #else /* no UTF-8 support */ 00139 c = *ptr; 00140 #endif /* SUPPORT_UTF8 */ 00141 00142 if (type == NLTYPE_ANYCRLF) switch(c) 00143 { 00144 case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1; 00145 return TRUE; /* LF */ 00146 case 0x000d: *lenptr = 1; return TRUE; /* CR */ 00147 default: return FALSE; 00148 } 00149 00150 else switch(c) 00151 { 00152 case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1; 00153 return TRUE; /* LF */ 00154 case 0x000b: /* VT */ 00155 case 0x000c: /* FF */ 00156 case 0x000d: *lenptr = 1; return TRUE; /* CR */ 00157 case 0x0085: *lenptr = utf8? 2 : 1; return TRUE; /* NEL */ 00158 case 0x2028: /* LS */ 00159 case 0x2029: *lenptr = 3; return TRUE; /* PS */ 00160 default: return FALSE; 00161 } 00162 } 00163 00164 /* End of pcre_newline.c */