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            Copyright (c) 1997-2014 University of Cambridge
10 
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14 
15     * Redistributions of source code must retain the above copyright notice,
16       this list of conditions and the following disclaimer.
17 
18     * Redistributions in binary form must reproduce the above copyright
19       notice, this list of conditions and the following disclaimer in the
20       documentation and/or other materials provided with the distribution.
21 
22     * Neither the name of the University of Cambridge nor the names of its
23       contributors may be used to endorse or promote products derived from
24       this software without specific prior written permission.
25 
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37 -----------------------------------------------------------------------------
38 */
39 
40 
41 /* This module contains internal functions for comparing and finding the length
42 of strings for different data item sizes. */
43 
44 #include "pcre_config.h"
45 #include "pcre_internal.h"
46 
47 #ifndef COMPILE_PCRE8
48 
49 /*************************************************
50 *           Compare string utilities             *
51 *************************************************/
52 
53 /* The following two functions compares two strings. Basically a strcmp
54 for non 8 bit characters.
55 
56 Arguments:
57   str1        first string
58   str2        second string
59 
60 Returns:      0 if both string are equal (like strcmp), 1 otherwise
61 */
62 
63 int
PRIV(strcmp_uc_uc)64 PRIV(strcmp_uc_uc)(const pcre_uchar *str1, const pcre_uchar *str2)
65 {
66 pcre_uchar c1;
67 pcre_uchar c2;
68 
69 while (*str1 != '\0' || *str2 != '\0')
70   {
71   c1 = *str1++;
72   c2 = *str2++;
73   if (c1 != c2)
74     return ((c1 > c2) << 1) - 1;
75   }
76 /* Both length and characters must be equal. */
77 return 0;
78 }
79 
80 #ifdef COMPILE_PCRE32
81 
82 int
PRIV(strcmp_uc_uc_utf)83 PRIV(strcmp_uc_uc_utf)(const pcre_uchar *str1, const pcre_uchar *str2)
84 {
85 pcre_uchar c1;
86 pcre_uchar c2;
87 
88 while (*str1 != '\0' || *str2 != '\0')
89   {
90   c1 = UCHAR21INC(str1);
91   c2 = UCHAR21INC(str2);
92   if (c1 != c2)
93     return ((c1 > c2) << 1) - 1;
94   }
95 /* Both length and characters must be equal. */
96 return 0;
97 }
98 
99 #endif /* COMPILE_PCRE32 */
100 
101 int
PRIV(strcmp_uc_c8)102 PRIV(strcmp_uc_c8)(const pcre_uchar *str1, const char *str2)
103 {
104 const pcre_uint8 *ustr2 = (pcre_uint8 *)str2;
105 pcre_uchar c1;
106 pcre_uchar c2;
107 
108 while (*str1 != '\0' || *ustr2 != '\0')
109   {
110   c1 = *str1++;
111   c2 = (pcre_uchar)*ustr2++;
112   if (c1 != c2)
113     return ((c1 > c2) << 1) - 1;
114   }
115 /* Both length and characters must be equal. */
116 return 0;
117 }
118 
119 #ifdef COMPILE_PCRE32
120 
121 int
PRIV(strcmp_uc_c8_utf)122 PRIV(strcmp_uc_c8_utf)(const pcre_uchar *str1, const char *str2)
123 {
124 const pcre_uint8 *ustr2 = (pcre_uint8 *)str2;
125 pcre_uchar c1;
126 pcre_uchar c2;
127 
128 while (*str1 != '\0' || *ustr2 != '\0')
129   {
130   c1 = UCHAR21INC(str1);
131   c2 = (pcre_uchar)*ustr2++;
132   if (c1 != c2)
133     return ((c1 > c2) << 1) - 1;
134   }
135 /* Both length and characters must be equal. */
136 return 0;
137 }
138 
139 #endif /* COMPILE_PCRE32 */
140 
141 /* The following two functions compares two, fixed length
142 strings. Basically an strncmp for non 8 bit characters.
143 
144 Arguments:
145   str1        first string
146   str2        second string
147   num         size of the string
148 
149 Returns:      0 if both string are equal (like strcmp), 1 otherwise
150 */
151 
152 int
PRIV(strncmp_uc_uc)153 PRIV(strncmp_uc_uc)(const pcre_uchar *str1, const pcre_uchar *str2, unsigned int num)
154 {
155 pcre_uchar c1;
156 pcre_uchar c2;
157 
158 while (num-- > 0)
159   {
160   c1 = *str1++;
161   c2 = *str2++;
162   if (c1 != c2)
163     return ((c1 > c2) << 1) - 1;
164   }
165 /* Both length and characters must be equal. */
166 return 0;
167 }
168 
169 int
PRIV(strncmp_uc_c8)170 PRIV(strncmp_uc_c8)(const pcre_uchar *str1, const char *str2, unsigned int num)
171 {
172 const pcre_uint8 *ustr2 = (pcre_uint8 *)str2;
173 pcre_uchar c1;
174 pcre_uchar c2;
175 
176 while (num-- > 0)
177   {
178   c1 = *str1++;
179   c2 = (pcre_uchar)*ustr2++;
180   if (c1 != c2)
181     return ((c1 > c2) << 1) - 1;
182   }
183 /* Both length and characters must be equal. */
184 return 0;
185 }
186 
187 /* The following function returns with the length of
188 a zero terminated string. Basically an strlen for non 8 bit characters.
189 
190 Arguments:
191   str         string
192 
193 Returns:      length of the string
194 */
195 
196 unsigned int
PRIV(strlen_uc)197 PRIV(strlen_uc)(const pcre_uchar *str)
198 {
199 unsigned int len = 0;
200 while (*str++ != 0)
201   len++;
202 return len;
203 }
204 
205 #endif /* !COMPILE_PCRE8 */
206 
207 /* End of pcre_string_utils.c */
208