xref: /optee_os/lib/libutils/isoc/newlib/strcmp.c (revision 86d6bc20855b2dece41c97c2984695edc9e2df05)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-3-Clause
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 1994-2009  Red Hat, Inc.
4b0104773SPascal Brand  * All rights reserved.
5b0104773SPascal Brand  *
6b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
7b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
8b0104773SPascal Brand  *
9b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
10b0104773SPascal Brand  * this list of conditions and the following disclaimer.
11b0104773SPascal Brand  *
12b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
13b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
14b0104773SPascal Brand  * and/or other materials provided with the distribution.
15b0104773SPascal Brand  *
16b0104773SPascal Brand  * 3. Neither the name of the copyright holder nor the names of its
17b0104773SPascal Brand  * contributors may be used to endorse or promote products derived from this
18b0104773SPascal Brand  * software without specific prior written permission.
19b0104773SPascal Brand  *
20b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
31b0104773SPascal Brand  */
32b0104773SPascal Brand 
33b0104773SPascal Brand /* This file is copied from newlib-1.19 */
34b0104773SPascal Brand 
35b0104773SPascal Brand /*
36b0104773SPascal Brand FUNCTION
37b0104773SPascal Brand 	<<strcmp>>---character string compare
38b0104773SPascal Brand 
39b0104773SPascal Brand INDEX
40b0104773SPascal Brand 	strcmp
41b0104773SPascal Brand 
42b0104773SPascal Brand ANSI_SYNOPSIS
43b0104773SPascal Brand 	#include <string.h>
44b0104773SPascal Brand 	int strcmp(const char *<[a]>, const char *<[b]>);
45b0104773SPascal Brand 
46b0104773SPascal Brand TRAD_SYNOPSIS
47b0104773SPascal Brand 	#include <string.h>
48b0104773SPascal Brand 	int strcmp(<[a]>, <[b]>)
49b0104773SPascal Brand 	char *<[a]>;
50b0104773SPascal Brand 	char *<[b]>;
51b0104773SPascal Brand 
52b0104773SPascal Brand DESCRIPTION
53b0104773SPascal Brand 	<<strcmp>> compares the string at <[a]> to
54b0104773SPascal Brand 	the string at <[b]>.
55b0104773SPascal Brand 
56b0104773SPascal Brand RETURNS
57b0104773SPascal Brand 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
58b0104773SPascal Brand 	<<strcmp>> returns a number greater than zero.  If the two
59b0104773SPascal Brand 	strings match, <<strcmp>> returns zero.  If <<*<[a]>>>
60b0104773SPascal Brand 	sorts lexicographically before <<*<[b]>>>, <<strcmp>> returns a
61b0104773SPascal Brand 	number less than zero.
62b0104773SPascal Brand 
63b0104773SPascal Brand PORTABILITY
64b0104773SPascal Brand <<strcmp>> is ANSI C.
65b0104773SPascal Brand 
66b0104773SPascal Brand <<strcmp>> requires no supporting OS subroutines.
67b0104773SPascal Brand 
68b0104773SPascal Brand QUICKREF
69b0104773SPascal Brand 	strcmp ansi pure
70b0104773SPascal Brand */
71b0104773SPascal Brand 
72b0104773SPascal Brand #include "_ansi.h"
73b0104773SPascal Brand #include <string.h>
74b0104773SPascal Brand #include <limits.h>
75b0104773SPascal Brand 
76b0104773SPascal Brand /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
77b0104773SPascal Brand #define UNALIGNED(X, Y) \
78b0104773SPascal Brand 	(((long)X & (sizeof(long) - 1)) | ((long)Y & (sizeof(long) - 1)))
79b0104773SPascal Brand 
80b0104773SPascal Brand /* DETECTNULL returns nonzero if (long)X contains a NULL byte. */
81b0104773SPascal Brand #if LONG_MAX == 2147483647L
82ba6d8df9SJens Wiklander #define DETECTNULL(X) (((X) - 0x01010101L) & ~(X) & 0x80808080UL)
83b0104773SPascal Brand #else
84b0104773SPascal Brand #if LONG_MAX == 9223372036854775807L
85ba6d8df9SJens Wiklander #define DETECTNULL(X) (((X) - 0x0101010101010101L) & ~(X) & \
86ba6d8df9SJens Wiklander 		       0x8080808080808080UL)
87b0104773SPascal Brand #else
88b0104773SPascal Brand #error long int is not a 32bit or 64bit type.
89b0104773SPascal Brand #endif
90b0104773SPascal Brand #endif
91b0104773SPascal Brand 
92b0104773SPascal Brand #ifndef DETECTNULL
93b0104773SPascal Brand #error long int is not a 32bit or 64bit byte
94b0104773SPascal Brand #endif
95b0104773SPascal Brand 
96b0104773SPascal Brand int _DEFUN(strcmp, (s1, s2), _CONST char *s1 _AND _CONST char *s2)
97b0104773SPascal Brand {
98*86d6bc20SAleksandr Iashchenko #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) || \
99*86d6bc20SAleksandr Iashchenko     defined(CFG_CORE_SANITIZE_KADDRESS)
100b0104773SPascal Brand 	while (*s1 != '\0' && *s1 == *s2) {
101b0104773SPascal Brand 		s1++;
102b0104773SPascal Brand 		s2++;
103b0104773SPascal Brand 	}
104b0104773SPascal Brand 
105b0104773SPascal Brand 	return (*(unsigned char *)s1) - (*(unsigned char *)s2);
106b0104773SPascal Brand #else
107b0104773SPascal Brand 	unsigned long *a1;
108b0104773SPascal Brand 	unsigned long *a2;
109b0104773SPascal Brand 
110b0104773SPascal Brand 	/* If s1 or s2 are unaligned, then compare bytes. */
111b0104773SPascal Brand 	if (!UNALIGNED(s1, s2)) {
112b0104773SPascal Brand 		/*
113b0104773SPascal Brand 		 * If s1 and s2 are word-aligned, compare them a word at a time.
114b0104773SPascal Brand 		 */
115b0104773SPascal Brand 		a1 = (unsigned long *)s1;
116b0104773SPascal Brand 		a2 = (unsigned long *)s2;
117b0104773SPascal Brand 		while (*a1 == *a2) {
118b0104773SPascal Brand 			/*
119b0104773SPascal Brand 			 * To get here, *a1 == *a2, thus if we find a null in
120b0104773SPascal Brand 			 * *a1, then the strings must be equal, so return zero.
121b0104773SPascal Brand 			 */
122b0104773SPascal Brand 			if (DETECTNULL(*a1))
123b0104773SPascal Brand 				return 0;
124b0104773SPascal Brand 
125b0104773SPascal Brand 			a1++;
126b0104773SPascal Brand 			a2++;
127b0104773SPascal Brand 		}
128b0104773SPascal Brand 
129b0104773SPascal Brand 		/*
130b0104773SPascal Brand 		 * A difference was detected in last few bytes of s1, so search
131b0104773SPascal Brand 		 * bytewise.
132b0104773SPascal Brand 		 */
133b0104773SPascal Brand 		s1 = (char *)a1;
134b0104773SPascal Brand 		s2 = (char *)a2;
135b0104773SPascal Brand 	}
136b0104773SPascal Brand 
137b0104773SPascal Brand 	while (*s1 != '\0' && *s1 == *s2) {
138b0104773SPascal Brand 		s1++;
139b0104773SPascal Brand 		s2++;
140b0104773SPascal Brand 	}
141b0104773SPascal Brand 	return (*(unsigned char *)s1) - (*(unsigned char *)s2);
142b0104773SPascal Brand #endif /* not PREFER_SIZE_OVER_SPEED */
143b0104773SPascal Brand }
144