• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* This module contains internal functions for comparing and finding the length
42 of strings. These are used instead of strcmp() etc because the standard
43 functions work only on 8-bit data. */
44 
45 
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49 
50 #include "pcre2_internal.h"
51 
52 
53 /*************************************************
54 *    Compare two zero-terminated PCRE2 strings   *
55 *************************************************/
56 
57 /*
58 Arguments:
59   str1        first string
60   str2        second string
61 
62 Returns:      0, 1, or -1
63 */
64 
65 int
PRIV(strcmp)66 PRIV(strcmp)(PCRE2_SPTR str1, PCRE2_SPTR str2)
67 {
68 PCRE2_UCHAR c1, c2;
69 while (*str1 != '\0' || *str2 != '\0')
70   {
71   c1 = *str1++;
72   c2 = *str2++;
73   if (c1 != c2) return ((c1 > c2) << 1) - 1;
74   }
75 return 0;
76 }
77 
78 
79 /*************************************************
80 *  Compare zero-terminated PCRE2 & 8-bit strings *
81 *************************************************/
82 
83 /* As the 8-bit string is almost always a literal, its type is specified as
84 const char *.
85 
86 Arguments:
87   str1        first string
88   str2        second string
89 
90 Returns:      0, 1, or -1
91 */
92 
93 int
PRIV(strcmp_c8)94 PRIV(strcmp_c8)(PCRE2_SPTR str1, const char *str2)
95 {
96 PCRE2_UCHAR c1, c2;
97 while (*str1 != '\0' || *str2 != '\0')
98   {
99   c1 = *str1++;
100   c2 = *str2++;
101   if (c1 != c2) return ((c1 > c2) << 1) - 1;
102   }
103 return 0;
104 }
105 
106 
107 /*************************************************
108 *    Compare two PCRE2 strings, given a length   *
109 *************************************************/
110 
111 /*
112 Arguments:
113   str1        first string
114   str2        second string
115   len         the length
116 
117 Returns:      0, 1, or -1
118 */
119 
120 int
PRIV(strncmp)121 PRIV(strncmp)(PCRE2_SPTR str1, PCRE2_SPTR str2, size_t len)
122 {
123 PCRE2_UCHAR c1, c2;
124 for (; len > 0; len--)
125   {
126   c1 = *str1++;
127   c2 = *str2++;
128   if (c1 != c2) return ((c1 > c2) << 1) - 1;
129   }
130 return 0;
131 }
132 
133 
134 /*************************************************
135 * Compare PCRE2 string to 8-bit string by length *
136 *************************************************/
137 
138 /* As the 8-bit string is almost always a literal, its type is specified as
139 const char *.
140 
141 Arguments:
142   str1        first string
143   str2        second string
144   len         the length
145 
146 Returns:      0, 1, or -1
147 */
148 
149 int
PRIV(strncmp_c8)150 PRIV(strncmp_c8)(PCRE2_SPTR str1, const char *str2, size_t len)
151 {
152 PCRE2_UCHAR c1, c2;
153 for (; len > 0; len--)
154   {
155   c1 = *str1++;
156   c2 = *str2++;
157   if (c1 != c2) return ((c1 > c2) << 1) - 1;
158   }
159 return 0;
160 }
161 
162 
163 /*************************************************
164 *        Find the length of a PCRE2 string       *
165 *************************************************/
166 
167 /*
168 Argument:    the string
169 Returns:     the length
170 */
171 
172 PCRE2_SIZE
PRIV(strlen)173 PRIV(strlen)(PCRE2_SPTR str)
174 {
175 PCRE2_SIZE c = 0;
176 while (*str++ != 0) c++;
177 return c;
178 }
179 
180 
181 /*************************************************
182 * Copy 8-bit 0-terminated string to PCRE2 string *
183 *************************************************/
184 
185 /* Arguments:
186   str1     buffer to receive the string
187   str2     8-bit string to be copied
188 
189 Returns:   the number of code units used (excluding trailing zero)
190 */
191 
192 PCRE2_SIZE
PRIV(strcpy_c8)193 PRIV(strcpy_c8)(PCRE2_UCHAR *str1, const char *str2)
194 {
195 PCRE2_UCHAR *t = str1;
196 while (*str2 != 0) *t++ = *str2++;
197 *t = 0;
198 return t - str1;
199 }
200 
201 /* End of pcre2_string_utils.c */
202