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