xref: /reactos/sdk/lib/crt/string/_mbstrnlen.c (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * COPYRIGHT:         See COPYING in the top level directory
3*c2c66affSColin Finck  * PROJECT:           ReactOS CRT
4*c2c66affSColin Finck  * PURPOSE:           Implementation of _mbstrnlen
5*c2c66affSColin Finck  * FILE:              lib/sdk/crt/string/_mbstrnlen.c
6*c2c66affSColin Finck  * PROGRAMMER:        Timo Kreuzer
7*c2c66affSColin Finck  */
8*c2c66affSColin Finck 
9*c2c66affSColin Finck #include <precomp.h>
10*c2c66affSColin Finck #include <mbctype.h>
11*c2c66affSColin Finck #include <specstrings.h>
12*c2c66affSColin Finck 
13*c2c66affSColin Finck _Success_(return>0)
14*c2c66affSColin Finck _Check_return_
15*c2c66affSColin Finck _CRTIMP
16*c2c66affSColin Finck size_t
17*c2c66affSColin Finck __cdecl
_mbstrnlen(_In_z_ const char * pmbstr,_In_ size_t cjMaxLen)18*c2c66affSColin Finck _mbstrnlen(
19*c2c66affSColin Finck     _In_z_ const char *pmbstr,
20*c2c66affSColin Finck     _In_ size_t cjMaxLen)
21*c2c66affSColin Finck {
22*c2c66affSColin Finck     size_t cchCount = 0;
23*c2c66affSColin Finck     unsigned char jMbsByte;
24*c2c66affSColin Finck 
25*c2c66affSColin Finck     /* Check parameters */
26*c2c66affSColin Finck     if (!MSVCRT_CHECK_PMT((pmbstr != 0)) && (cjMaxLen <= INT_MAX))
27*c2c66affSColin Finck     {
28*c2c66affSColin Finck         _set_errno(EINVAL);
29*c2c66affSColin Finck         return -1;
30*c2c66affSColin Finck     }
31*c2c66affSColin Finck 
32*c2c66affSColin Finck     /* Loop while we have bytes to process */
33*c2c66affSColin Finck     while (cjMaxLen-- > 0)
34*c2c66affSColin Finck     {
35*c2c66affSColin Finck         /* Get next mb byte */
36*c2c66affSColin Finck         jMbsByte = *pmbstr++;
37*c2c66affSColin Finck 
38*c2c66affSColin Finck         /* If this is 0, we're done */
39*c2c66affSColin Finck         if (jMbsByte == 0) break;
40*c2c66affSColin Finck 
41*c2c66affSColin Finck         /* if this is a lead byte, continue with next char */
42*c2c66affSColin Finck         if (_ismbblead(jMbsByte))
43*c2c66affSColin Finck         {
44*c2c66affSColin Finck             // FIXME: check if this is a valid char.
45*c2c66affSColin Finck             continue;
46*c2c66affSColin Finck         }
47*c2c66affSColin Finck 
48*c2c66affSColin Finck         /* Count this character */
49*c2c66affSColin Finck         cchCount++;
50*c2c66affSColin Finck     }
51*c2c66affSColin Finck 
52*c2c66affSColin Finck     return cchCount;
53*c2c66affSColin Finck }
54*c2c66affSColin Finck 
55