Coverage Report

Created: 2025-08-29 06:31

/src/pcre2/src/pcre2_string_utils.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) 2018-2021 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 comparing and finding the length
43
of strings. These are used instead of strcmp() etc because the standard
44
functions work only on 8-bit data. */
45
46
47
#include "pcre2_internal.h"
48
49
50
51
/*************************************************
52
*    Emulated memmove() for systems without it   *
53
*************************************************/
54
55
/* This function can make use of bcopy() if it is available. Otherwise do it by
56
steam, as there some non-Unix environments that lack both memmove() and
57
bcopy(). */
58
59
#if !defined(VPCOMPAT) && !defined(HAVE_MEMMOVE)
60
void *
61
PRIV(memmove)(void *d, const void *s, size_t n)
62
{
63
#ifdef HAVE_BCOPY
64
bcopy(s, d, n);
65
return d;
66
#else
67
size_t i;
68
unsigned char *dest = (unsigned char *)d;
69
const unsigned char *src = (const unsigned char *)s;
70
if (dest > src)
71
  {
72
  dest += n;
73
  src += n;
74
  for (i = 0; i < n; ++i) *(--dest) = *(--src);
75
  return (void *)dest;
76
  }
77
else
78
  {
79
  for (i = 0; i < n; ++i) *dest++ = *src++;
80
  return (void *)(dest - n);
81
  }
82
#endif   /* not HAVE_BCOPY */
83
}
84
#endif   /* not VPCOMPAT && not HAVE_MEMMOVE */
85
86
87
/*************************************************
88
*    Compare two zero-terminated PCRE2 strings   *
89
*************************************************/
90
91
/*
92
Arguments:
93
  str1        first string
94
  str2        second string
95
96
Returns:      0, 1, or -1
97
*/
98
99
int
100
PRIV(strcmp)(PCRE2_SPTR str1, PCRE2_SPTR str2)
101
137k
{
102
137k
PCRE2_UCHAR c1, c2;
103
138k
while (*str1 != '\0' || *str2 != '\0')
104
137k
  {
105
137k
  c1 = *str1++;
106
137k
  c2 = *str2++;
107
137k
  if (c1 != c2) return ((c1 > c2) << 1) - 1;
108
137k
  }
109
428
return 0;
110
137k
}
111
112
113
/*************************************************
114
*  Compare zero-terminated PCRE2 & 8-bit strings *
115
*************************************************/
116
117
/* As the 8-bit string is almost always a literal, its type is specified as
118
const char *.
119
120
Arguments:
121
  str1        first string
122
  str2        second string
123
124
Returns:      0, 1, or -1
125
*/
126
127
int
128
PRIV(strcmp_c8)(PCRE2_SPTR str1, const char *str2)
129
130k
{
130
130k
PCRE2_UCHAR c1, c2;
131
230k
while (*str1 != '\0' || *str2 != '\0')
132
212k
  {
133
212k
  c1 = *str1++;
134
212k
  c2 = *str2++;
135
212k
  if (c1 != c2) return ((c1 > c2) << 1) - 1;
136
212k
  }
137
17.7k
return 0;
138
130k
}
139
140
141
/*************************************************
142
*    Compare two PCRE2 strings, given a length   *
143
*************************************************/
144
145
/*
146
Arguments:
147
  str1        first string
148
  str2        second string
149
  len         the length
150
151
Returns:      0, 1, or -1
152
*/
153
154
int
155
PRIV(strncmp)(PCRE2_SPTR str1, PCRE2_SPTR str2, size_t len)
156
194k
{
157
194k
PCRE2_UCHAR c1, c2;
158
366k
for (; len > 0; len--)
159
196k
  {
160
196k
  c1 = *str1++;
161
196k
  c2 = *str2++;
162
196k
  if (c1 != c2) return ((c1 > c2) << 1) - 1;
163
196k
  }
164
170k
return 0;
165
194k
}
166
167
168
/*************************************************
169
* Compare PCRE2 string to 8-bit string by length *
170
*************************************************/
171
172
/* As the 8-bit string is almost always a literal, its type is specified as
173
const char *.
174
175
Arguments:
176
  str1        first string
177
  str2        second string
178
  len         the length
179
180
Returns:      0, 1, or -1
181
*/
182
183
int
184
PRIV(strncmp_c8)(PCRE2_SPTR str1, const char *str2, size_t len)
185
520k
{
186
520k
PCRE2_UCHAR c1, c2;
187
782k
for (; len > 0; len--)
188
735k
  {
189
735k
  c1 = *str1++;
190
735k
  c2 = *str2++;
191
735k
  if (c1 != c2) return ((c1 > c2) << 1) - 1;
192
735k
  }
193
47.2k
return 0;
194
520k
}
195
196
197
/*************************************************
198
*        Find the length of a PCRE2 string       *
199
*************************************************/
200
201
/*
202
Argument:    the string
203
Returns:     the length
204
*/
205
206
PCRE2_SIZE
207
PRIV(strlen)(PCRE2_SPTR str)
208
0
{
209
0
PCRE2_SIZE c = 0;
210
0
while (*str++ != 0) c++;
211
0
return c;
212
0
}
213
214
215
/*************************************************
216
* Copy 8-bit 0-terminated string to PCRE2 string *
217
*************************************************/
218
219
/* Arguments:
220
  str1     buffer to receive the string
221
  str2     8-bit string to be copied
222
223
Returns:   the number of code units used (excluding trailing zero)
224
*/
225
226
PCRE2_SIZE
227
PRIV(strcpy_c8)(PCRE2_UCHAR *str1, const char *str2)
228
0
{
229
0
PCRE2_UCHAR *t = str1;
230
0
while (*str2 != 0) *t++ = *str2++;
231
0
*t = 0;
232
0
return t - str1;
233
0
}
234
235
/* End of pcre2_string_utils.c */