1 /***
2 *stricmp.cpp - contains case-insensitive string comp routine _stricmp
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 * contains _stricmp()
8 *
9 *******************************************************************************/
10 #include <corecrt_internal.h>
11 #include <ctype.h>
12 #include <locale.h>
13 #include <string.h>
14
15 #pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018 Prefast can't see that we are checking for terminal nul.
16
17 /***
18 *int _stricmp(lhs, rhs) - compare strings, ignore case
19 *
20 *Purpose:
21 * _stricmp/_strcmpi perform a case-insensitive string comparision.
22 * For differences, upper case letters are mapped to lower case.
23 * Thus, "abc_" < "ABCD" since "_" < "d".
24 *
25 *Entry:
26 * char *lhs, *rhs - strings to compare
27 *
28 *Return:
29 * Returns <0 if lhs < rhs
30 * Returns 0 if lhs = rhs
31 * Returns >0 if lhs > rhs
32 * Returns _NLSCMPERROR if something went wrong
33 *
34 *Exceptions:
35 * Input parameters are validated. Refer to the validation section of the function.
36 *
37 *******************************************************************************/
38
_stricmp_l(char const * const lhs,char const * const rhs,_locale_t const plocinfo)39 extern "C" int __cdecl _stricmp_l (
40 char const * const lhs,
41 char const * const rhs,
42 _locale_t const plocinfo
43 )
44 {
45 /* validation section */
46 _VALIDATE_RETURN(lhs != nullptr, EINVAL, _NLSCMPERROR);
47 _VALIDATE_RETURN(rhs != nullptr, EINVAL, _NLSCMPERROR);
48
49 unsigned char const * lhs_ptr = reinterpret_cast<unsigned char const *>(lhs);
50 unsigned char const * rhs_ptr = reinterpret_cast<unsigned char const *>(rhs);
51
52 _LocaleUpdate _loc_update(plocinfo);
53
54 int result;
55 int lhs_value;
56 int rhs_value;
57 do
58 {
59 lhs_value = _tolower_fast_internal(*lhs_ptr++, _loc_update.GetLocaleT());
60 rhs_value = _tolower_fast_internal(*rhs_ptr++, _loc_update.GetLocaleT());
61 result = lhs_value - rhs_value;
62 }
63 while (result == 0 && lhs_value != 0);
64
65 return result;
66 }
67
__ascii_stricmp(char const * const lhs,char const * const rhs)68 extern "C" int __cdecl __ascii_stricmp (
69 char const * const lhs,
70 char const * const rhs
71 )
72 {
73 unsigned char const * lhs_ptr = reinterpret_cast<unsigned char const *>(lhs);
74 unsigned char const * rhs_ptr = reinterpret_cast<unsigned char const *>(rhs);
75
76 int result;
77 int lhs_value;
78 int rhs_value;
79 do
80 {
81 lhs_value = __ascii_tolower(*lhs_ptr++);
82 rhs_value = __ascii_tolower(*rhs_ptr++);
83 result = lhs_value - rhs_value;
84 }
85 while (result == 0 && lhs_value != 0);
86
87 return result;
88 }
89
_stricmp(char const * const lhs,char const * const rhs)90 extern "C" int __cdecl _stricmp (
91 char const * const lhs,
92 char const * const rhs
93 )
94 {
95 if (!__acrt_locale_changed())
96 {
97 /* validation section */
98 _VALIDATE_RETURN(lhs != nullptr, EINVAL, _NLSCMPERROR);
99 _VALIDATE_RETURN(rhs != nullptr, EINVAL, _NLSCMPERROR);
100
101 return __ascii_stricmp(lhs, rhs);
102 }
103
104 return _stricmp_l(lhs, rhs, nullptr);
105 }
106