ReactOS 0.4.16-dev-822-gbcedb53
mbsstr.cpp
Go to the documentation of this file.
1/***
2* mbsstr.c - Search for one MBCS string inside another
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose:
7* Search for one MBCS string inside another
8*
9*******************************************************************************/
10#ifndef _MBCS
11 #error This file should only be compiled with _MBCS defined
12#endif
13
14#include <corecrt_internal.h>
16#include <locale.h>
17#include <stddef.h>
18#include <string.h>
19
20/***
21* _mbsstr - Search for one MBCS string inside another
22*
23*Purpose:
24* Find the first occurrence of str2 in str1.
25*
26*Entry:
27* unsigned char *str1 = beginning of string
28* unsigned char *str2 = string to search for
29*
30*Exit:
31* Returns a pointer to the first occurrence of str2 in
32* str1, or nullptr if str2 does not occur in str1
33*
34*Exceptions:
35* Input parameters are validated. Refer to the validation section of the function.
36*
37*******************************************************************************/
38
39extern "C" _CONST_RETURN unsigned char * __cdecl _mbsstr_l(
40 const unsigned char *str1,
41 const unsigned char *str2,
43 )
44{
45 unsigned char *cp, *s1, *s2, *endp;
46 _LocaleUpdate _loc_update(plocinfo);
47
48 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
49 return (unsigned char *)strstr((const char *)str1, (const char *)str2);
50
51 /* validation section */
52 _VALIDATE_RETURN(str2 != nullptr, EINVAL, 0);
53 if ( *str2 == '\0')
54 return (unsigned char *)str1;
55 _VALIDATE_RETURN(str1 != nullptr, EINVAL, 0);
56
57 cp = (unsigned char *) str1;
58 endp = (unsigned char *) (str1 + (strlen((const char *)str1) - strlen((const char *)str2)));
59
60 while (*cp && (cp <= endp))
61 {
62 s1 = cp;
63 s2 = (unsigned char *) str2;
64
65 /*
66 * MBCS: ok to ++ since doing equality comparison.
67 * [This depends on MBCS strings being "legal".]
68 */
69 while ( *s1 && *s2 && (*s1 == *s2) )
70 s1++, s2++;
71
72 if (!(*s2))
73 return(cp); /* success! */
74
75 /*
76 * bump pointer to next char
77 */
78 if ( _ismbblead_l(*(cp++), _loc_update.GetLocaleT()) )
79 {
80 /* don't move forward if we have leadbyte, EOS
81 means dud string was passed in.
82 Don't assert - too low level
83 */
84 if(*cp!='\0')
85 {
86 cp++;
87 }
88 }
89 }
90
91 return(nullptr);
92
93}
94
95extern "C" _CONST_RETURN unsigned char * (__cdecl _mbsstr)(
96 const unsigned char *str1,
97 const unsigned char *str2
98 )
99{
100 return _mbsstr_l(str1, str2, nullptr);
101}
#define EINVAL
Definition: acclib.h:90
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define __cdecl
Definition: accygwin.h:79
#define _ismbblead_l(_c, p)
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
_locale_t plocinfo
Definition: ismbbyte.cpp:75
POINT cp
Definition: magnifier.c:59
_CONST_RETURN unsigned char *__cdecl _mbsstr_l(const unsigned char *str1, const unsigned char *str2, _locale_t plocinfo)
Definition: mbsstr.cpp:39
_CONST_RETURN unsigned char *__cdecl _mbsstr(const unsigned char *str1, const unsigned char *str2)
Definition: mbsstr.cpp:95
struct S1 s1
struct S2 s2
#define _CONST_RETURN
Definition: memory.h:16