| 1 | /************************************************* | 
|---|
| 2 | *      Perl-Compatible Regular Expressions       * | 
|---|
| 3 | *************************************************/ | 
|---|
| 4 |  | 
|---|
| 5 | /* PCRE is a library of functions to support regular expressions whose syntax | 
|---|
| 6 | and semantics are as close as possible to those of the Perl 5 language. | 
|---|
| 7 |  | 
|---|
| 8 | Written by Philip Hazel | 
|---|
| 9 | Original API code Copyright (c) 1997-2012 University of Cambridge | 
|---|
| 10 | New API code Copyright (c) 2016 University of Cambridge | 
|---|
| 11 |  | 
|---|
| 12 | ----------------------------------------------------------------------------- | 
|---|
| 13 | Redistribution and use in source and binary forms, with or without | 
|---|
| 14 | modification, are permitted provided that the following conditions are met: | 
|---|
| 15 |  | 
|---|
| 16 | * Redistributions of source code must retain the above copyright notice, | 
|---|
| 17 | this list of conditions and the following disclaimer. | 
|---|
| 18 |  | 
|---|
| 19 | * Redistributions in binary form must reproduce the above copyright | 
|---|
| 20 | notice, this list of conditions and the following disclaimer in the | 
|---|
| 21 | documentation and/or other materials provided with the distribution. | 
|---|
| 22 |  | 
|---|
| 23 | * Neither the name of the University of Cambridge nor the names of its | 
|---|
| 24 | contributors may be used to endorse or promote products derived from | 
|---|
| 25 | this software without specific prior written permission. | 
|---|
| 26 |  | 
|---|
| 27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
|---|
| 28 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 29 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 30 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | 
|---|
| 31 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 
|---|
| 32 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 
|---|
| 33 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 
|---|
| 34 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 
|---|
| 35 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 
|---|
| 36 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
|---|
| 37 | POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 38 | ----------------------------------------------------------------------------- | 
|---|
| 39 | */ | 
|---|
| 40 |  | 
|---|
| 41 |  | 
|---|
| 42 | /* This module contains internal functions for testing newlines when more than | 
|---|
| 43 | one kind of newline is to be recognized. When a newline is found, its length is | 
|---|
| 44 | returned. In principle, we could implement several newline "types", each | 
|---|
| 45 | referring to a different set of newline characters. At present, PCRE2 supports | 
|---|
| 46 | only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF, | 
|---|
| 47 | and NLTYPE_ANY. The full list of Unicode newline characters is taken from | 
|---|
| 48 | http://unicode.org/unicode/reports/tr18/. */ | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | #ifdef HAVE_CONFIG_H | 
|---|
| 52 | #include "config.h" | 
|---|
| 53 | #endif | 
|---|
| 54 |  | 
|---|
| 55 | #include "pcre2_internal.h" | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 |  | 
|---|
| 59 | /************************************************* | 
|---|
| 60 | *      Check for newline at given position       * | 
|---|
| 61 | *************************************************/ | 
|---|
| 62 |  | 
|---|
| 63 | /* This function is called only via the IS_NEWLINE macro, which does so only | 
|---|
| 64 | when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed | 
|---|
| 65 | newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the code unit | 
|---|
| 66 | pointed to by ptr is less than the end of the string. | 
|---|
| 67 |  | 
|---|
| 68 | Arguments: | 
|---|
| 69 | ptr          pointer to possible newline | 
|---|
| 70 | type         the newline type | 
|---|
| 71 | endptr       pointer to the end of the string | 
|---|
| 72 | lenptr       where to return the length | 
|---|
| 73 | utf          TRUE if in utf mode | 
|---|
| 74 |  | 
|---|
| 75 | Returns:       TRUE or FALSE | 
|---|
| 76 | */ | 
|---|
| 77 |  | 
|---|
| 78 | BOOL | 
|---|
| 79 | PRIV(is_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR endptr, | 
|---|
| 80 | uint32_t *lenptr, BOOL utf) | 
|---|
| 81 | { | 
|---|
| 82 | uint32_t c; | 
|---|
| 83 |  | 
|---|
| 84 | #ifdef SUPPORT_UNICODE | 
|---|
| 85 | if (utf) { GETCHAR(c, ptr); } else c = *ptr; | 
|---|
| 86 | #else | 
|---|
| 87 | (void)utf; | 
|---|
| 88 | c = *ptr; | 
|---|
| 89 | #endif  /* SUPPORT_UNICODE */ | 
|---|
| 90 |  | 
|---|
| 91 | if (type == NLTYPE_ANYCRLF) switch(c) | 
|---|
| 92 | { | 
|---|
| 93 | case CHAR_LF: | 
|---|
| 94 | *lenptr = 1; | 
|---|
| 95 | return TRUE; | 
|---|
| 96 |  | 
|---|
| 97 | case CHAR_CR: | 
|---|
| 98 | *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1; | 
|---|
| 99 | return TRUE; | 
|---|
| 100 |  | 
|---|
| 101 | default: | 
|---|
| 102 | return FALSE; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | /* NLTYPE_ANY */ | 
|---|
| 106 |  | 
|---|
| 107 | else switch(c) | 
|---|
| 108 | { | 
|---|
| 109 | #ifdef EBCDIC | 
|---|
| 110 | case CHAR_NEL: | 
|---|
| 111 | #endif | 
|---|
| 112 | case CHAR_LF: | 
|---|
| 113 | case CHAR_VT: | 
|---|
| 114 | case CHAR_FF: | 
|---|
| 115 | *lenptr = 1; | 
|---|
| 116 | return TRUE; | 
|---|
| 117 |  | 
|---|
| 118 | case CHAR_CR: | 
|---|
| 119 | *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1; | 
|---|
| 120 | return TRUE; | 
|---|
| 121 |  | 
|---|
| 122 | #ifndef EBCDIC | 
|---|
| 123 | #if PCRE2_CODE_UNIT_WIDTH == 8 | 
|---|
| 124 | case CHAR_NEL: | 
|---|
| 125 | *lenptr = utf? 2 : 1; | 
|---|
| 126 | return TRUE; | 
|---|
| 127 |  | 
|---|
| 128 | case 0x2028:   /* LS */ | 
|---|
| 129 | case 0x2029:   /* PS */ | 
|---|
| 130 | *lenptr = 3; | 
|---|
| 131 | return TRUE; | 
|---|
| 132 |  | 
|---|
| 133 | #else  /* 16-bit or 32-bit code units */ | 
|---|
| 134 | case CHAR_NEL: | 
|---|
| 135 | case 0x2028:   /* LS */ | 
|---|
| 136 | case 0x2029:   /* PS */ | 
|---|
| 137 | *lenptr = 1; | 
|---|
| 138 | return TRUE; | 
|---|
| 139 | #endif | 
|---|
| 140 | #endif /* Not EBCDIC */ | 
|---|
| 141 |  | 
|---|
| 142 | default: | 
|---|
| 143 | return FALSE; | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 | /************************************************* | 
|---|
| 150 | *     Check for newline at previous position     * | 
|---|
| 151 | *************************************************/ | 
|---|
| 152 |  | 
|---|
| 153 | /* This function is called only via the WAS_NEWLINE macro, which does so only | 
|---|
| 154 | when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed | 
|---|
| 155 | newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the initial | 
|---|
| 156 | value of ptr is greater than the start of the string that is being processed. | 
|---|
| 157 |  | 
|---|
| 158 | Arguments: | 
|---|
| 159 | ptr          pointer to possible newline | 
|---|
| 160 | type         the newline type | 
|---|
| 161 | startptr     pointer to the start of the string | 
|---|
| 162 | lenptr       where to return the length | 
|---|
| 163 | utf          TRUE if in utf mode | 
|---|
| 164 |  | 
|---|
| 165 | Returns:       TRUE or FALSE | 
|---|
| 166 | */ | 
|---|
| 167 |  | 
|---|
| 168 | BOOL | 
|---|
| 169 | PRIV(was_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR startptr, | 
|---|
| 170 | uint32_t *lenptr, BOOL utf) | 
|---|
| 171 | { | 
|---|
| 172 | uint32_t c; | 
|---|
| 173 | ptr--; | 
|---|
| 174 |  | 
|---|
| 175 | #ifdef SUPPORT_UNICODE | 
|---|
| 176 | if (utf) | 
|---|
| 177 | { | 
|---|
| 178 | BACKCHAR(ptr); | 
|---|
| 179 | GETCHAR(c, ptr); | 
|---|
| 180 | } | 
|---|
| 181 | else c = *ptr; | 
|---|
| 182 | #else | 
|---|
| 183 | (void)utf; | 
|---|
| 184 | c = *ptr; | 
|---|
| 185 | #endif  /* SUPPORT_UNICODE */ | 
|---|
| 186 |  | 
|---|
| 187 | if (type == NLTYPE_ANYCRLF) switch(c) | 
|---|
| 188 | { | 
|---|
| 189 | case CHAR_LF: | 
|---|
| 190 | *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1; | 
|---|
| 191 | return TRUE; | 
|---|
| 192 |  | 
|---|
| 193 | case CHAR_CR: | 
|---|
| 194 | *lenptr = 1; | 
|---|
| 195 | return TRUE; | 
|---|
| 196 |  | 
|---|
| 197 | default: | 
|---|
| 198 | return FALSE; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | /* NLTYPE_ANY */ | 
|---|
| 202 |  | 
|---|
| 203 | else switch(c) | 
|---|
| 204 | { | 
|---|
| 205 | case CHAR_LF: | 
|---|
| 206 | *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1; | 
|---|
| 207 | return TRUE; | 
|---|
| 208 |  | 
|---|
| 209 | #ifdef EBCDIC | 
|---|
| 210 | case CHAR_NEL: | 
|---|
| 211 | #endif | 
|---|
| 212 | case CHAR_VT: | 
|---|
| 213 | case CHAR_FF: | 
|---|
| 214 | case CHAR_CR: | 
|---|
| 215 | *lenptr = 1; | 
|---|
| 216 | return TRUE; | 
|---|
| 217 |  | 
|---|
| 218 | #ifndef EBCDIC | 
|---|
| 219 | #if PCRE2_CODE_UNIT_WIDTH == 8 | 
|---|
| 220 | case CHAR_NEL: | 
|---|
| 221 | *lenptr = utf? 2 : 1; | 
|---|
| 222 | return TRUE; | 
|---|
| 223 |  | 
|---|
| 224 | case 0x2028:   /* LS */ | 
|---|
| 225 | case 0x2029:   /* PS */ | 
|---|
| 226 | *lenptr = 3; | 
|---|
| 227 | return TRUE; | 
|---|
| 228 |  | 
|---|
| 229 | #else /* 16-bit or 32-bit code units */ | 
|---|
| 230 | case CHAR_NEL: | 
|---|
| 231 | case 0x2028:   /* LS */ | 
|---|
| 232 | case 0x2029:   /* PS */ | 
|---|
| 233 | *lenptr = 1; | 
|---|
| 234 | return TRUE; | 
|---|
| 235 | #endif | 
|---|
| 236 | #endif /* Not EBCDIC */ | 
|---|
| 237 |  | 
|---|
| 238 | default: | 
|---|
| 239 | return FALSE; | 
|---|
| 240 | } | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | /* End of pcre2_newline.c */ | 
|---|
| 244 |  | 
|---|