Coverage Report

Created: 2025-06-13 06:43

/src/php-src/ext/pcre/pcre2lib/pcre2_newline.c
Line
Count
Source (jump to first uncovered line)
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
0
{
82
0
uint32_t c;
83
84
0
#ifdef SUPPORT_UNICODE
85
0
if (utf) { GETCHAR(c, ptr); } else c = *ptr;
86
#else
87
(void)utf;
88
c = *ptr;
89
#endif  /* SUPPORT_UNICODE */
90
91
0
if (type == NLTYPE_ANYCRLF) switch(c)
92
0
  {
93
0
  case CHAR_LF:
94
0
  *lenptr = 1;
95
0
  return TRUE;
96
97
0
  case CHAR_CR:
98
0
  *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
99
0
  return TRUE;
100
101
0
  default:
102
0
  return FALSE;
103
0
  }
104
105
/* NLTYPE_ANY */
106
107
0
else switch(c)
108
0
  {
109
#ifdef EBCDIC
110
  case CHAR_NEL:
111
#endif
112
0
  case CHAR_LF:
113
0
  case CHAR_VT:
114
0
  case CHAR_FF:
115
0
  *lenptr = 1;
116
0
  return TRUE;
117
118
0
  case CHAR_CR:
119
0
  *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
120
0
  return TRUE;
121
122
0
#ifndef EBCDIC
123
0
#if PCRE2_CODE_UNIT_WIDTH == 8
124
0
  case CHAR_NEL:
125
0
  *lenptr = utf? 2 : 1;
126
0
  return TRUE;
127
128
0
  case 0x2028:   /* LS */
129
0
  case 0x2029:   /* PS */
130
0
  *lenptr = 3;
131
0
  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
0
#endif /* Not EBCDIC */
141
142
0
  default:
143
0
  return FALSE;
144
0
  }
145
0
}
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
0
{
172
0
uint32_t c;
173
0
ptr--;
174
175
0
#ifdef SUPPORT_UNICODE
176
0
if (utf)
177
0
  {
178
0
  BACKCHAR(ptr);
179
0
  GETCHAR(c, ptr);
180
0
  }
181
0
else c = *ptr;
182
#else
183
(void)utf;
184
c = *ptr;
185
#endif  /* SUPPORT_UNICODE */
186
187
0
if (type == NLTYPE_ANYCRLF) switch(c)
188
0
  {
189
0
  case CHAR_LF:
190
0
  *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
191
0
  return TRUE;
192
193
0
  case CHAR_CR:
194
0
  *lenptr = 1;
195
0
  return TRUE;
196
197
0
  default:
198
0
  return FALSE;
199
0
  }
200
201
/* NLTYPE_ANY */
202
203
0
else switch(c)
204
0
  {
205
0
  case CHAR_LF:
206
0
  *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
207
0
  return TRUE;
208
209
#ifdef EBCDIC
210
  case CHAR_NEL:
211
#endif
212
0
  case CHAR_VT:
213
0
  case CHAR_FF:
214
0
  case CHAR_CR:
215
0
  *lenptr = 1;
216
0
  return TRUE;
217
218
0
#ifndef EBCDIC
219
0
#if PCRE2_CODE_UNIT_WIDTH == 8
220
0
  case CHAR_NEL:
221
0
  *lenptr = utf? 2 : 1;
222
0
  return TRUE;
223
224
0
  case 0x2028:   /* LS */
225
0
  case 0x2029:   /* PS */
226
0
  *lenptr = 3;
227
0
  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
0
#endif /* Not EBCDIC */
237
238
0
  default:
239
0
  return FALSE;
240
0
  }
241
0
}
242
243
/* End of pcre2_newline.c */