Reactos
at master 101 lines 2.9 kB view raw
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> 15#include <corecrt_internal_mbstring.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, 42 _locale_t plocinfo 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}