xref: /freebsd/contrib/sendmail/libsm/strcaseeq.c (revision 15f0b8c3)
1 /*
2  * Copyright (c) 2020 Proofpoint, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  *
9  */
10 
11 #include <sm/gen.h>
12 #include <sm/sendmail.h>
13 
14 #if USE_EAI
15 #include <sm/string.h>
16 #include <sm/heap.h>
17 #include <sm/ixlen.h>
18 
19 /*
20 **  SM_STRCASEEQ -- are two strings equal (case-insenstive)?
21 **
22 **	Parameters:
23 **		s1 -- string
24 **		s2 -- string
25 **
26 **	Returns:
27 **		true iff s1 == s2
28 */
29 
30 bool
31 sm_strcaseeq(s1, s2)
32 	const char *s1;
33 	const char *s2;
34 {
35 	char *l1, *l2;
36 	char *f1;
37 	bool same;
38 
39 	if (asciistr(s1))
40 	{
41 		if (!asciistr(s2))
42 			return false;
43 		return (sm_strcasecmp(s1, s2) == 0);
44 	}
45 	if (asciistr(s2))
46 		return false;
47 	l1 = sm_lowercase(s1);
48 	if (l1 != s1)
49 	{
50 		f1 = sm_strdup_x(l1);
51 		l1 = f1;
52 	}
53 	else
54 		f1 = NULL;
55 	l2 = sm_lowercase(s2);
56 
57 	while (*l1 == *l2 && '\0' != *l1)
58 		l1++, l2++;
59 	same = *l1 == *l2;
60 
61 	SM_FREE(f1);
62 	return same;
63 }
64 
65 /*
66 **  SM_STRNCASEEQ -- are two strings (up to a length) equal (case-insenstive)?
67 **
68 **	Parameters:
69 **		s1 -- string
70 **		s2 -- string
71 **		n -- maximum length to compare
72 **
73 **	Returns:
74 **		true iff s1 == s2 (for up to the first n char)
75 */
76 
77 bool
78 sm_strncaseeq(s1, s2, n)
79 	const char *s1;
80 	const char *s2;
81 	size_t n;
82 {
83 	char *l1, *l2;
84 	char *f1;
85 	bool same;
86 
87 	if (0 == n)
88 		return true;
89 	if (asciistr(s1))
90 	{
91 		if (!asciistr(s2))
92 			return false;
93 		return (sm_strncasecmp(s1, s2, n) == 0);
94 	}
95 	if (asciistr(s2))
96 		return false;
97 	l1 = sm_lowercase(s1);
98 	if (l1 != s1)
99 	{
100 		f1 = sm_strdup_x(l1);
101 		l1 = f1;
102 	}
103 	else
104 		f1 = NULL;
105 	l2 = sm_lowercase(s2);
106 
107 	while (*l1 == *l2 && '\0' != *l1 && n-- > 0)
108 		l1++, l2++;
109 	same = *l1 == *l2;
110 
111 	SM_FREE(f1);
112 	return same;
113 }
114 #endif /* USE_EAI */
115