1 /***
2 * mbschr.c - Search MBCS string for character
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 * Search MBCS string for a character
8 *
9 *******************************************************************************/
10 #ifndef _MBCS
11 #error This file should only be compiled with _MBCS defined
12 #endif
13
14 #include <corecrt_internal_mbstring.h>
15 #include <locale.h>
16 #include <stddef.h>
17 #include <string.h>
18
19 #pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018
20
21 /***
22 * _mbschr - Search MBCS string for character
23 *
24 *Purpose:
25 * Search the given string for the specified character.
26 * MBCS characters are handled correctly.
27 *
28 *Entry:
29 * unsigned char *string = string to search
30 * int c = character to search for
31 *
32 *Exit:
33 * returns a pointer to the first occurence of the specified char
34 * within the string.
35 *
36 * returns nullptr if the character is not found n the string.
37 *
38 *Exceptions:
39 * Input parameters are validated. Refer to the validation section of the function.
40 *
41 *******************************************************************************/
42
43
_mbschr_l(const unsigned char * string,unsigned int c,_locale_t plocinfo)44 extern "C" _CONST_RETURN unsigned char * __cdecl _mbschr_l(
45 const unsigned char *string,
46 unsigned int c,
47 _locale_t plocinfo
48 )
49 {
50 unsigned short cc = '\0';
51 _LocaleUpdate _loc_update(plocinfo);
52
53 /* validation section */
54 _VALIDATE_RETURN(string != nullptr, EINVAL, nullptr);
55
56 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
57 return (_CONST_RETURN unsigned char *)strchr((const char *)string, (int)c);
58
59 for (; (cc = *string) != '\0'; string++)
60 {
61 if ( _ismbblead_l(cc, _loc_update.GetLocaleT()) )
62 {
63 if (*++string == '\0')
64 return nullptr; /* error */
65 if ( c == (unsigned int)((cc << 8) | *string) ) /* DBCS match */
66 return (unsigned char *)(string - 1);
67 }
68 else if (c == (unsigned int)cc)
69 break; /* SBCS match */
70 }
71
72 if (c == (unsigned int)cc) /* check for SBCS match--handles NUL char */
73 return (unsigned char *)(string);
74
75 return nullptr;
76 }
77
78 extern "C" _CONST_RETURN unsigned char * (__cdecl _mbschr)(
79 const unsigned char *string,
80 unsigned int c
81 )
82 {
83 return _mbschr_l(string, c, nullptr);
84 }
85