]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Locale/multibyte_Utf8.c
2 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
3 This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <sys/types.h>
21 static mbstate_t LocalConvState
= {0};
23 /** Map a UTF-8 encoded prefix byte to a sequence length.
24 Zero means illegal prefix, but valid surrogate if < 0xC0.
25 One indicates an ASCII-7 equivalent character.
26 Two, three, and four are the first byte for 2, 3, and 4 byte sequences, respectively.
27 See RFC 3629 for details.
30 Low Nibble decodes the first byte into the number of bytes in the sequence.
31 A value of zero indicates an invalid byte.
32 The High Nibble encodes a bit mask to be used to match against the high nibble of the second byte.
35 SequenceLength = code[c0] & 0x0F;
36 Mask = 0x80 | code[c0];
38 Surrogate bytes are valid if: code[cX] & Mask > 0x80;
42 UINT8 utf8_code_length
[256] = {
43 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* 00-0F */
44 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
45 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
46 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
47 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
48 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
49 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
50 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* 70-7F */
51 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, /* 80-8F */
52 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, /* 90-9F */
53 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, /* A0-AF */
54 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, /* B0-BF */
55 0x00, 0x00, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, /* C0-C1 + C2-CF */
56 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, /* D0-DF */
57 0x43, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x33, 0x73, 0x73, /* E0-EF */
58 0x64, 0x74, 0x74, 0x74, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* F0-F4 + F5-FF */
61 /** Process one byte of a multibyte character.
63 @param[in] ch One byte of a multibyte character.
64 @param[in,out] ps Pointer to a conversion state object.
66 @retval -2 ch is an incomplete but potentially valid character.
67 @retval -1 ch is not valid in this context.
68 @retval 1:4 The length, in bytes, of the character ch just completed.
72 ProcessOneByte(unsigned char ch
, mbstate_t *ps
)
79 // We are in an invalid state
80 ps
->A
= 0; // Initial State
82 ps
->C
[ps
->A
] = ch
; // Save the current character
83 Mask
= utf8_code_length
[ch
];
85 if(ps
->A
== 0) { // Initial State. First byte of sequence.
89 case 0: // State 0, Code 0
92 ps
->E
= 1; // Consume this character
94 case 1: // State 0, Code 1
96 ps
->B
= ps
->D
[0] = ch
;
99 default: // State 0, Code 2, 3, 4
100 ps
->A
= 1; // Next state is State-1
101 RetVal
= -2; // Incomplete but potentially valid character
106 // We are in state 1, 2, or 3 and processing a surrogate byte
107 Length
= ps
->E
& 0xF;
108 if((Mask
& ps
->E
) > 0x80) {
109 // This byte is valid
110 switch(ps
->A
) { // Process based upon our current state
111 case 1: // Second byte of the sequence.
112 if(Length
== 2) { // State 1, Code 2
113 Length
= ((ps
->C
[0] & 0x1f) << 6) + (ps
->C
[1] & 0x3f);
114 assert ((Length
> 0x007F) && (Length
<= 0x07FF));
115 ps
->B
= ps
->D
[0] = (UINT16
)Length
;
116 ps
->A
= 0; // Next state is State-0
119 else { // This isn't the last character, get more. State 1, Code 3 or 4
124 case 2: // Third byte of the sequence
126 Length
= ((ps
->C
[0] & 0x0f) << 12) + ((ps
->C
[1] & 0x3f) << 6) + (ps
->C
[2] & 0x3f);
127 assert ((Length
> 0x07FF) && (Length
<= 0xFFFF));
128 ps
->B
= ps
->D
[0] = (UINT16
)Length
;
129 ps
->A
= 0; // Next state is State-0
137 case 3: // Fourth byte of the sequence
139 Length
= ((ps
->C
[0] & 0x7) << 18) + ((ps
->C
[1] & 0x3f) << 12) +
140 ((ps
->C
[2] & 0x3f) << 6) + (ps
->C
[3] & 0x3f);
142 assert ((Length
> 0xFFFF) && (Length
<= 0x10ffff));
144 /* compute and append the two surrogates: */
146 /* translate from 10000..10FFFF to 0..FFFF */
149 /* high surrogate = top 10 bits added to D800 */
150 ps
->D
[0] = (UINT16
)(0xD800 + (Length
>> 10));
152 /* low surrogate = bottom 10 bits added to DC00 */
153 ps
->D
[1] = (UINT16
)(0xDC00 + (Length
& 0x03FF));
154 ps
->A
= 0; // Next state is State-0
161 ps
->E
= 4; // Can't happen, but consume this character anyway
166 else { // Invalid surrogate character
168 ps
->A
= 0; // Next is State-0
170 ps
->E
= 0; // Don't Consume, it may be an initial byte
176 /** Convert one Multibyte sequence.
178 @param[out] Dest Pointer to output location, or NULL
179 @param[in] Src Multibyte Source (UTF8)
180 @param[in] Len Max Number of bytes to convert
181 @param[in] pS Pointer to State struct., or NULL
183 @retval -2 Bytes processed comprise an incomplete, but potentially valid, character.
184 @retval -1 An encoding error was encountered. ps->E indicates the number of bytes consumed.
185 @retval 0 Either Src is NULL or it points to a NUL character.
186 @retval 1:N N bytes were consumed producing a valid wide character.
190 wchar_t *Dest
, // Pointer to output location, or NULL
191 const char *Src
, // Multibyte Source (UTF8)
192 ssize_t Len
, // Max Number of bytes to convert
193 mbstate_t *pS
// Pointer to State struct., or NULL
200 if((Src
== NULL
) || (*Src
== '\0')) {
204 pS
= &LocalConvState
;
208 while(Src
< SrcEnd
) {
209 ch
= (unsigned char)*Src
++;
210 NumConv
= ProcessOneByte(ch
, pS
);
214 if((NumConv
> 0) && (Dest
!= NULL
)) {
223 /* Determine the number of bytes needed to represent a Wide character
226 A single wide character may convert into a one, two, three, or four byte
227 narrow (MBCS or UTF-8) character. The number of MBCS bytes can be determined
230 If WCS char < 0x00000080 One Byte
231 Else if WCS char < 0x0000D800 Two Bytes
234 Since UEFI only supports the Unicode Base Multilingual Plane (BMP),
235 Four-byte characters are not supported.
237 @param[in] InCh Wide character to test.
239 @retval -1 Improperly formed character
240 @retval 0 InCh is 0x0000
241 @retval >0 Number of bytes needed for the MBCS character
245 OneWcToMcLen(const wchar_t InCh
)
249 if(InCh
== 0) { // Is this a NUL, 0x0000 ?
252 else if(InCh
< 0x0080) { // Is this a 1-byte character?
255 else if(InCh
< 0x0800) { // Is this a 2-byte character?
258 else if((InCh
>= 0xD800) && (InCh
< 0xE000)) { // Is this a surrogate?
262 NumBytes
= 3; // Otherwise, it must be a 3-byte character.
264 return (int)NumBytes
; // Return extimate of required bytes.
267 /* Determine the number of bytes needed to represent a Wide character string
268 as a MBCS string of given maximum length. Will optionally return the number
269 of wide characters that would be consumed.
271 A single wide character may convert into a one, two, three, or four byte
272 narrow (MBCS or UTF-8) character. The number of MBCS bytes can be determined
275 If WCS char < 0x00000080 One Byte
276 Else if WCS char < 0x00000800 Two Bytes
277 Else if WCS char < 0x00010000 Three Bytes
280 Since UEFI only supports the Unicode Base Multilingual Plane (BMP),
281 Four-byte characters should not be encountered.
283 @param[in] Src Pointer to a wide character string.
284 @param[in] Limit Maximum number of bytes the converted string may occupy.
285 @param[out] NumChar Pointer to where to store the number of wide characters, or NULL.
287 @return The number of bytes required to convert Src to MBCS,
288 not including the terminating NUL. If NumChar is not NULL, the number
289 of characters represented by the return value will be written to
294 EstimateWtoM(const wchar_t * Src
, size_t Limit
, size_t *NumChar
)
303 EChar
= *Src
++; // Get the initial character and point to next
304 while(((NumBytes
= OneWcToMcLen(EChar
)) > 0) &&
305 ((size_t)(Estimate
+ NumBytes
) < Limit
))
306 { // Until one of the source characters is NUL
307 ++CharCount
; // Count this character.
308 Estimate
+= NumBytes
; // Count the Bytes for this character
309 EChar
= *Src
++; // Get the next source character and point to the next.
311 if(NumChar
!= NULL
) {
312 *NumChar
= CharCount
;
314 return (size_t)Estimate
; // Return esimate of required bytes.
317 /* Determine the number of characters in a MBCS string.
318 MBCS characters are one to four bytes long. By examining the first byte
319 of a MBCS character, one can determine the number of bytes comprising the
327 Since UEFI only supports the Unicode Base Multilingual Plane (BMP),
328 Four-byte characters should not be encountered.
330 @param[in] Src The string to examine
332 @return The number of characters represented by the MBCS string.
336 CountMbcsChars(const char *Src
)
347 else if(EChar
< 0xE0) {
351 else if(EChar
< 0xF0) {
356 // Ill-formed character
363 /** Convert a wide character (UTF16) into a multibyte character (UTF8)
365 Converts a wide character into a corresponding multibyte character that
366 begins in the conversion state described by the object pointed to by ps.
367 If dst is not a null pointer, the converted character is then stored into
368 the array pointed to by dst.
370 It is the caller's responsibility to ensure that Dest is large enough to
371 hold the resulting MBCS sequence.
373 @param s Pointer to the wide-character string to convert
374 @param Dest Pointer to the buffer in which to place the converted sequence, or NULL.
376 @retval -1 An error occurred. The error reason is in errno.
377 @retval >=0 The number of bytes stored into Dest.
380 EncodeUtf8(char *Dest
, wchar_t ch
)
382 char *p
; /* next free byte in build buffer */
383 int NumInBuff
; // number of bytes in Buff
384 char Buff
[4]; // Buffer into which each character is built
390 /* Encode ASCII -- One Byte */
394 else if (ch
< 0x0800) {
395 /* Encode Latin-1 -- Two Byte */
396 *p
++ = (char)(0xc0 | (ch
>> 6));
397 *p
++ = (char)(0x80 | (ch
& 0x3f));
401 /* Encode UCS2 Unicode ordinals -- Three Byte */
402 /* Special case: check for surrogate -- Shouldn't happen in UEFI */
403 if (0xD800 <= ch
&& ch
< 0xE000) {
408 *p
++ = (char)(0xe0 | (ch
>> 12));
409 *p
++ = (char)(0x80 | ((ch
>> 6) & 0x3f));
410 *p
++ = (char)(0x80 | (ch
& 0x3f));
414 /* At this point, Buff holds the converted character which is NumInBuff bytes long.
415 NumInBuff is the value 1, 2, 3, or 4
417 if(Dest
!= NULL
) { // Save character if Dest is not NULL
418 memcpy(Dest
, Buff
, NumInBuff
);
421 // Terminate the destination string.
422 Dest
[NumInBuff
] = '\0';
428 return NumInBuff
; // Tell the caller
431 // ######################## Narrow to Wide Conversions #######################
433 /** If ps is not a null pointer, the mbsinit function determines whether the
434 pointed-to mbstate_t object describes an initial conversion state.
436 @param[in] ps Pointer to the conversion state object to test.
438 @return The mbsinit function returns nonzero if ps is a null pointer
439 or if the pointed-to object describes an initial conversion
440 state; otherwise, it returns zero.
445 mbsinit(const mbstate_t *ps
)
447 if((ps
== NULL
) || (ps
->A
== 0)) {
453 /** The mbrlen function is equivalent to the call:<BR>
455 mbrtowc(NULL, s, n, ps != NULL ? ps : &internal)
457 where internal is the mbstate_t object for the mbrlen function, except that
458 the expression designated by ps is evaluated only once.
460 @param[in] s Pointer to a multibyte character sequence.
461 @param[in] n Maximum number of bytes to examine.
462 @param[in] pS Pointer to the conversion state object.
464 @retval 0 The next n or fewer characters complete a NUL.
465 @retval 1..n The number of bytes that complete the multibyte character.
466 @retval -2 The next n bytes contribute to an incomplete (but potentially valid) multibyte character.
467 @retval -1 An encoding error occurred.
478 return mbrtowc(NULL
, s
, n
, pS
);
481 /** Determine the number of bytes comprising a multibyte character.
483 If S is not a null pointer, the mblen function determines the number of bytes
484 contained in the multibyte character pointed to by S. Except that the
485 conversion state of the mbtowc function is not affected, it is equivalent to
486 mbtowc((wchar_t *)0, S, N);
488 @param[in] S NULL to query whether multibyte characters have
489 state-dependent encodings. Otherwise, points to a
491 @param[in] N The maximum number of bytes in a multibyte character.
493 @return If S is a null pointer, the mblen function returns a nonzero or
494 zero value, if multibyte character encodings, respectively, do
495 or do not have state-dependent encodings. If S is not a null
496 pointer, the mblen function either returns 0 (if S points to the
497 null character), or returns the number of bytes that are contained
498 in the multibyte character (if the next N or fewer bytes form a
499 valid multibyte character), or returns -1 (if they do not form a
500 valid multibyte character).
502 Declared in: stdlib.h
510 return (int)mbrlen(s
, n
, NULL
);
514 If S is a null pointer, the mbrtowc function is equivalent to the call:<BR>
516 mbrtowc(NULL, "", 1, ps)
519 In this case, the values of the parameters pwc and n are ignored.
521 If S is not a null pointer, the mbrtowc function inspects at most n bytes beginning with
522 the byte pointed to by S to determine the number of bytes needed to complete the next
523 multibyte character (including any shift sequences). If the function determines that the
524 next multibyte character is complete and valid, it determines the value of the
525 corresponding wide character and then, if pwc is not a null pointer, stores that value in
526 the object pointed to by pwc. If the corresponding wide character is the null wide
527 character, the resulting state described is the initial conversion state.
529 @param[out] pwc Pointer to where the resulting wide character is to be stored.
530 @param[in] s Pointer to a multibyte character "string".
531 @param[in] n The maximum number of bytes to inspect.
532 @param[in] ps Pointer to a conversion state object.
534 @retval 0 if the next n or fewer bytes complete the multibyte
535 character that corresponds to the null wide
536 character (which is the value stored).
537 @retval between_1_and_n_inclusive if the next n or fewer bytes complete
538 a valid multibyte character (which is the value
539 stored); the value returned is the number of bytes
540 that complete the multibyte character.
541 @retval (size_t)(-2) if the next n bytes contribute to an incomplete
542 (but potentially valid) multibyte character, and
543 all n bytes have been processed (no value is stored).
544 @retval (size_t)(-1) if an encoding error occurs, in which case the next
545 n or fewer bytes do not contribute to a complete and
546 valid multibyte character (no value is stored); the
547 value of the macro EILSEQ is stored in errno, and
548 the conversion state is unspecified.
562 RetVal
= DecodeOneStateful(pwc
, s
, (ssize_t
)n
, ps
);
563 return (size_t)RetVal
;
566 /** Convert a multibyte character into a wide character.
568 If S is not a null pointer, the mbtowc function inspects at most N bytes
569 beginning with the byte pointed to by S to determine the number of bytes
570 needed to complete the next multibyte character (including any shift
571 sequences). If the function determines that the next multibyte character
572 is complete and valid, it determines the value of the corresponding wide
573 character and then, if Pwc is not a null pointer, stores that value in
574 the object pointed to by Pwc. If the corresponding wide character is the
575 null wide character, the function is left in the initial conversion state.
577 @param[out] Pwc Pointer to a wide-character object to receive the converted character.
578 @param[in] S Pointer to a multibyte character to convert.
579 @param[in] N Maximum number of bytes in a multibyte character.
581 @return If S is a null pointer, the mbtowc function returns a nonzero or
582 zero value, if multibyte character encodings, respectively, do
583 or do not have state-dependent encodings. If S is not a null
584 pointer, the mbtowc function either returns 0 (if S points to
585 the null character), or returns the number of bytes that are
586 contained in the converted multibyte character (if the next N or
587 fewer bytes form a valid multibyte character), or returns -1
588 (if they do not form a valid multibyte character).
590 In no case will the value returned be greater than N or the value
591 of the MB_CUR_MAX macro.
593 Declared in: stdlib.h
602 return (int)mbrtowc(pwc
, s
, n
, NULL
);
606 The mbsrtowcs function converts a sequence of multibyte characters that begins in the
607 conversion state described by the object pointed to by ps, from the array indirectly
608 pointed to by src into a sequence of corresponding wide characters. If dst is not a null
609 pointer, the converted characters are stored into the array pointed to by dst. Conversion
610 continues up to and including a terminating null character, which is also stored.
611 Conversion stops earlier in two cases: when a sequence of bytes is encountered that does
612 not form a valid multibyte character, or (if dst is not a null pointer) when len wide
613 characters have been stored into the array pointed to by dst. Each conversion takes
614 place as if by a call to the mbrtowc function.
616 If dst is not a null pointer, the pointer object pointed to by src is assigned either a null
617 pointer (if conversion stopped due to reaching a terminating null character) or the address
618 just past the last multibyte character converted (if any). If conversion stopped due to
619 reaching a terminating null character and if dst is not a null pointer, the resulting state
620 described is the initial conversion state.
622 @param[out] dst Pointer to where the resulting wide character sequence is stored.
623 @param[in] src Pointer to a pointer to the multibyte character sequence to convert.
624 @param[in] len Maximum number of wide characters to be stored into dst.
625 @param[in] ps Pointer to a conversion state object.
627 @return If the input conversion encounters a sequence of bytes that do
628 not form a valid multibyte character, an encoding error occurs:
629 the mbsrtowcs function stores the value of the macro EILSEQ in
630 errno and returns (size_t)(-1); the conversion state is
631 unspecified. Otherwise, it returns the number of multibyte
632 characters successfully converted, not including the terminating
633 null character (if any).
649 if((src
== NULL
) || (*src
== NULL
) || (**src
== '\0')) {
654 for(x
= 1 ; (len
!= 0) && (x
> 0); --len
) {
655 x
= DecodeOneStateful(dst
, MySrc
, MB_LEN_MAX
, ps
);
657 case -2: // Incomplete character
658 case -1: // Encoding error
661 case 0: // Encountered NUL character: done.
667 default: // Successfully decoded a character, continue with next
683 /** Convert a multibyte character string into a wide-character string.
685 The mbstowcs function converts a sequence of multibyte characters that
686 begins in the initial shift state from the array pointed to by Src into
687 a sequence of corresponding wide characters and stores not more than limit
688 wide characters into the array pointed to by Dest. No multibyte
689 characters that follow a null character (which is converted into a null
690 wide character) will be examined or converted. Each multibyte character
691 is converted as if by a call to the mbtowc function, except that the
692 conversion state of the mbtowc function is not affected.
694 No more than Limit elements will be modified in the array pointed to by Dest.
695 If copying takes place between objects that overlap,
696 the behavior is undefined.
698 @param[out] Dest Pointer to the array to receive the converted string.
699 @param[in] Src Pointer to the string to be converted.
700 @param[in] Limit Maximum number of elements to be written to Dest.
702 @return If an invalid multibyte character is encountered, the mbstowcs
703 function returns (size_t)(-1). Otherwise, the mbstowcs function
704 returns the number of array elements modified, not including a
705 terminating null wide character, if any.
707 Declared in: stdlib.h
717 /* Dest may be NULL */
718 /* Src may be NULL */
720 return mbsrtowcs(Dest
, &Src
, Limit
, NULL
);
723 /** The btowc function determines whether C constitutes a valid single-byte
724 character in the initial shift state.
726 @param[in] C A narrow character to test or convert to wide.
728 @return The btowc function returns WEOF if c has the value EOF or if
729 (unsigned char)C does not constitute a valid single-byte
730 character in the initial shift state. Otherwise, it returns the
731 wide character representation of that character.
740 wint_t RetVal
= WEOF
;
744 x
= DecodeOneStateful(&Dest
, (const char *)&c
, 1, NULL
);
749 RetVal
= (wint_t)Dest
;
754 // ######################## Wide to Narrow Conversions #######################
757 If S is a null pointer, the wcrtomb function is equivalent to the call:<BR>
759 wcrtomb(buf, L'\0', ps)
761 where buf is an internal buffer.
763 If S is not a null pointer, the wcrtomb function determines the number of bytes needed
764 to represent the multibyte character that corresponds to the wide character given by wc
765 (including any shift sequences), and stores the multibyte character representation in the
766 array whose first element is pointed to by S. At most MB_CUR_MAX bytes are stored. If
767 wc is a null wide character, a null byte is stored, preceded by any shift sequence needed
768 to restore the initial shift state; the resulting state described is the initial conversion state.
770 @param[out] Dest Pointer to the location in which to store the resulting
771 multibyte character. Otherwise, NULL to reset the
773 @param[in] wchar The wide character to convert.
774 @param[in,out] pS Pointer to a conversion state object, or NULL.
776 @return The wcrtomb function returns the number of bytes stored in the
777 array object (including any shift sequences). When wc is not a
778 valid wide character, an encoding error occurs: the function
779 stores the value of the macro EILSEQ in errno and
780 returns (size_t)(-1); the conversion state is unspecified.
793 /* Dest may be NULL */
798 if (wchar
== L
'\0') {
803 RetVal
= EncodeUtf8(Dest
, wchar
);
807 pS
= &LocalConvState
;
809 pS
->A
= 0; // Set ps to the initial conversion state
814 /** Convert a wide character into a multibyte character.
816 The wctomb function determines the number of bytes needed to represent the
817 multibyte character corresponding to the wide character given by WC
818 (including any shift sequences), and stores the multibyte character
819 representation in the array whose first element is pointed to by S (if S is
820 not a null pointer). At most MB_CUR_MAX characters are stored. If WC is a
821 null wide character, a null byte is stored, preceded by any shift sequence
822 needed to restore the initial shift state, and the function is left in the
823 initial conversion state.
825 @param[out] S Pointer to the object to receive the converted multibyte character.
826 @param[in] WC Wide character to be converted.
828 @return If S is a null pointer, the wctomb function returns a nonzero or
829 zero value, if multibyte character encodings, respectively, do or
830 do not have state-dependent encodings. If S is not a null pointer,
831 the wctomb function returns -1 if the value of WC does not
832 correspond to a valid multibyte character, or returns the number
833 of bytes that are contained in the multibyte character
834 corresponding to the value of WC.
836 In no case will the value returned be greater than the value of
837 the MB_CUR_MAX macro.
839 Declared in: stdlib.h
848 If s is NULL just return whether MB Characters have state
849 dependent encodings -- they don't.
854 return (int)wcrtomb(s
, wchar
, NULL
);
857 /** The wcsrtombs function converts a sequence of wide characters from the array
858 indirectly pointed to by Dest into a sequence of corresponding multibyte
859 characters that begins in the conversion state described by the object
862 If Dest is not a null pointer, the converted characters are stored into the
863 array pointed to by Dest. Conversion continues up to and including a
864 terminating null wide character, which is also stored. Conversion stops
865 earlier in two cases: when a wide character is reached that does not
866 correspond to a valid multibyte character, or (if Dest is not a null
867 pointer) when the next multibyte character would exceed the limit of Limit
868 total bytes to be stored into the array pointed to by Dest. Each conversion
869 takes place as if by a call to the wcrtomb function.)
871 If Dest is not a null pointer, the pointer object pointed to by Src is
872 assigned either a null pointer (if conversion stopped due to reaching
873 a terminating null wide character) or the address just past the last wide
874 character converted (if any). If conversion stopped due to reaching a
875 terminating null wide character, the resulting state described is the
876 initial conversion state.
880 @param[in] Limit Max number of bytes to store in Dest.
883 @return If conversion stops because a wide character is reached that
884 does not correspond to a valid multibyte character, an
885 encoding error occurs: the wcsrtombs function stores the
886 value of the macro EILSEQ in errno and returns (size_t)(-1);
887 the conversion state is unspecified. Otherwise, it returns
888 the number of bytes in the resulting multibyte character
889 sequence, not including the terminating null character (if any).
907 MaxBytes
= (ssize_t
)Limit
;
909 /* Dest may be NULL */
910 /* Src may be NULL */
911 /* ps appears to be unused */
913 if (Src
== NULL
|| *Src
== NULL
)
918 MaxBytes
= ASCII_STRING_MAX
;
920 NumStored
= EstimateWtoM(*Src
, MaxBytes
, NULL
);
923 while (OneWcToMcLen(InCh
= *(*Src
)++) <= MaxBytes
) {
928 count
= (int)wcrtomb(Dest
, InCh
, NULL
);
935 NumStored
= (size_t)(-1);
944 /** Convert a wide-character string into a multibyte character string.
946 The wcstombs function converts a sequence of wide characters from the
947 array pointed to by Src into a sequence of corresponding multibyte
948 characters that begins in the initial shift state, and stores these
949 multibyte characters into the array pointed to by Dest, stopping if a
950 multibyte character would exceed the limit of Limit total bytes or if a
951 null character is stored. Each wide character is converted as if by
952 a call to the wctomb function, except that the conversion state of
953 the wctomb function is not affected.
955 No more than Limit bytes will be modified in the array pointed to by Dest.
956 If copying takes place between objects that overlap,
957 the behavior is undefined.
959 @param[out] Dest Pointer to the array to receive the converted string.
960 @param[in] Src Pointer to the string to be converted.
961 @param[in] Limit Maximum number of elements to be written to Dest.
963 @return If a wide character is encountered that does not correspond to a
964 valid multibyte character, the wcstombs function returns
965 (size_t)(-1). Otherwise, the wcstombs function returns the number
966 of bytes modified, not including a terminating null character,
969 Declared in: stdlib.h
978 /* Dest may be NULL */
979 return wcsrtombs(Dest
, &Src
, Limit
, NULL
);
982 /** The wctob function determines whether C corresponds to a member of the extended
983 character set whose multibyte character representation is a single byte when in the initial
986 wctob needs to be consistent with wcrtomb.
987 If wcrtomb says that a character is representable in 1 byte,
988 then wctob needs to also represent the character as 1 byte.
990 @return The wctob function returns EOF if C does not correspond to a multibyte
991 character with length one in the initial shift state. Otherwise, it
992 returns the single-byte representation of that character as an
993 unsigned char converted to an int.
1006 else if (OneWcToMcLen((const wchar_t)c
) == 1) {
1007 RetVal
= (int)(c
& 0xFF);