Reactos
at master 84 lines 2.5 kB view raw
1/*** 2*mbslen_s.c - Find length of MBCS string 3* 4* Copyright (c) Microsoft Corporation. All rights reserved. 5* 6*Purpose: 7* Find length of MBCS string 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 <string.h> 18 19/*** 20* _mbsnlen - Find length of MBCS string 21* 22*Purpose: 23* Find the length of the MBCS string (in characters). 24* 25*Entry: 26* unsigned char *s = string 27* size_t maxsize 28* 29*Exit: 30* Returns the number of MBCS chars in the string. 31* Only the first sizeInBytes bytes of the string are inspected: if the null 32* terminator is not found, sizeInBytes is returned. 33* If the string is null terminated in sizeInBytes bytes, the return value 34* will always be less than sizeInBytes. 35* Returns (size_t)-1 if something went wrong. 36* 37*Exceptions: 38* Input parameters are validated. Refer to the validation section of the function. 39* 40*******************************************************************************/ 41 42size_t __cdecl _mbsnlen_l( 43 const unsigned char *s, 44 size_t sizeInBytes, 45 _locale_t plocinfo 46 ) 47{ 48 size_t n, size; 49 _LocaleUpdate _loc_update(plocinfo); 50 51 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0) 52 return strnlen((const char *)s, sizeInBytes); 53 54 /* Note that we do not check if s == nullptr, because we do not 55 * return errno_t... 56 */ 57 58 /* Note that sizeInBytes here is the number of bytes, not mb characters! */ 59 for (n = 0, size = 0; size < sizeInBytes && *s; n++, s++, size++) 60 { 61 if ( _ismbblead_l(*s, _loc_update.GetLocaleT()) ) 62 { 63 size++; 64 if (size >= sizeInBytes) 65 { 66 break; 67 } 68 if (*++s == '\0') 69 { 70 break; 71 } 72 } 73 } 74 75 return (size >= sizeInBytes ? sizeInBytes : n); 76} 77 78size_t __cdecl _mbsnlen( 79 const unsigned char *s, 80 size_t maxsize 81 ) 82{ 83 return _mbsnlen_l(s,maxsize, nullptr); 84}