1/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and 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-----------------------------------------------------------------------------
12Redistribution and use in source and binary forms, with or without
13modification, 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
26THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36POSSIBILITY OF SUCH DAMAGE.
37-----------------------------------------------------------------------------
38*/
39
40
41/* This module contains internal functions for comparing and finding the length
42of 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
54for non 8 bit characters.
55
56Arguments:
57 str1 first string
58 str2 second string
59
60Returns: 0 if both string are equal (like strcmp), 1 otherwise
61*/
62
63int
64PRIV(strcmp_uc_uc)(const pcre_uchar *str1, const pcre_uchar *str2)
65{
66pcre_uchar c1;
67pcre_uchar c2;
68
69while (*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. */
77return 0;
78}
79
80#ifdef COMPILE_PCRE32
81
82int
83PRIV(strcmp_uc_uc_utf)(const pcre_uchar *str1, const pcre_uchar *str2)
84{
85pcre_uchar c1;
86pcre_uchar c2;
87
88while (*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. */
96return 0;
97}
98
99#endif /* COMPILE_PCRE32 */
100
101int
102PRIV(strcmp_uc_c8)(const pcre_uchar *str1, const char *str2)
103{
104const pcre_uint8 *ustr2 = (pcre_uint8 *)str2;
105pcre_uchar c1;
106pcre_uchar c2;
107
108while (*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. */
116return 0;
117}
118
119#ifdef COMPILE_PCRE32
120
121int
122PRIV(strcmp_uc_c8_utf)(const pcre_uchar *str1, const char *str2)
123{
124const pcre_uint8 *ustr2 = (pcre_uint8 *)str2;
125pcre_uchar c1;
126pcre_uchar c2;
127
128while (*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. */
136return 0;
137}
138
139#endif /* COMPILE_PCRE32 */
140
141/* The following two functions compares two, fixed length
142strings. Basically an strncmp for non 8 bit characters.
143
144Arguments:
145 str1 first string
146 str2 second string
147 num size of the string
148
149Returns: 0 if both string are equal (like strcmp), 1 otherwise
150*/
151
152int
153PRIV(strncmp_uc_uc)(const pcre_uchar *str1, const pcre_uchar *str2, unsigned int num)
154{
155pcre_uchar c1;
156pcre_uchar c2;
157
158while (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. */
166return 0;
167}
168
169int
170PRIV(strncmp_uc_c8)(const pcre_uchar *str1, const char *str2, unsigned int num)
171{
172const pcre_uint8 *ustr2 = (pcre_uint8 *)str2;
173pcre_uchar c1;
174pcre_uchar c2;
175
176while (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. */
184return 0;
185}
186
187/* The following function returns with the length of
188a zero terminated string. Basically an strlen for non 8 bit characters.
189
190Arguments:
191 str string
192
193Returns: length of the string
194*/
195
196unsigned int
197PRIV(strlen_uc)(const pcre_uchar *str)
198{
199unsigned int len = 0;
200while (*str++ != 0)
201 len++;
202return len;
203}
204
205#endif /* !COMPILE_PCRE8 */
206
207/* End of pcre_string_utils.c */
208