]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Locale/multibyte_Utf8.c
NetworkPkg: Remove a CopyMem to speed up the HTTP boot download.
[mirror_edk2.git] / StdLib / LibC / Locale / multibyte_Utf8.c
1 /** @file
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
7
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.
10 **/
11 #include <assert.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <wchar.h>
16 #include <sys/types.h>
17 #include <limits.h>
18
19 typedef int ch_UCS4;
20
21 static mbstate_t LocalConvState = {0};
22
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.
28
29 TABLE ENCODING:
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.
33
34 example:
35 SequenceLength = code[c0] & 0x0F;
36 Mask = 0x80 | code[c0];
37
38 Surrogate bytes are valid if: code[cX] & Mask > 0x80;
39
40 */
41 static
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 */
59 };
60
61 /** Process one byte of a multibyte character.
62
63 @param[in] ch One byte of a multibyte character.
64 @param[in,out] ps Pointer to a conversion state object.
65
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.
69 **/
70 static
71 int
72 ProcessOneByte(unsigned char ch, mbstate_t *ps)
73 {
74 UINT32 Mask;
75 UINT32 Length;
76 int RetVal = 0;
77
78 if(ps->A > 3) {
79 // We are in an invalid state
80 ps->A = 0; // Initial State
81 }
82 ps->C[ps->A] = ch; // Save the current character
83 Mask = utf8_code_length[ch];
84
85 if(ps->A == 0) { // Initial State. First byte of sequence.
86 ps->E = Mask | 0x80;
87 Length = Mask & 0xF;
88 switch(Length) {
89 case 0: // State 0, Code 0
90 errno = EILSEQ;
91 RetVal = -1;
92 ps->E = 1; // Consume this character
93 break;
94 case 1: // State 0, Code 1
95 // ASCII-7 Character
96 ps->B = ps->D[0] = ch;
97 RetVal = 1;
98 break;
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
102 break;
103 }
104 }
105 else {
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
117 RetVal = 2;
118 }
119 else { // This isn't the last character, get more. State 1, Code 3 or 4
120 ps->A = 2;
121 RetVal = -2;
122 }
123 break;
124 case 2: // Third byte of the sequence
125 if(Length == 3) {
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
130 RetVal = 3;
131 }
132 else {
133 ps->A = 3;
134 RetVal = -2;
135 }
136 break;
137 case 3: // Fourth byte of the sequence
138 if(Length == 4) {
139 Length = ((ps->C[0] & 0x7) << 18) + ((ps->C[1] & 0x3f) << 12) +
140 ((ps->C[2] & 0x3f) << 6) + (ps->C[3] & 0x3f);
141 ps->B = Length;
142 assert ((Length > 0xFFFF) && (Length <= 0x10ffff));
143
144 /* compute and append the two surrogates: */
145
146 /* translate from 10000..10FFFF to 0..FFFF */
147 Length -= 0x10000;
148
149 /* high surrogate = top 10 bits added to D800 */
150 ps->D[0] = (UINT16)(0xD800 + (Length >> 10));
151
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
155 RetVal = 4;
156 }
157 else {
158 errno = EILSEQ;
159 ps->A = 0;
160 RetVal = -1;
161 ps->E = 4; // Can't happen, but consume this character anyway
162 }
163 break;
164 }
165 }
166 else { // Invalid surrogate character
167 errno = EILSEQ;
168 ps->A = 0; // Next is State-0
169 RetVal = -1;
170 ps->E = 0; // Don't Consume, it may be an initial byte
171 }
172 }
173 return RetVal;
174 }
175
176 /** Convert one Multibyte sequence.
177
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
182
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.
187 **/
188 int
189 DecodeOneStateful(
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
194 )
195 {
196 const char *SrcEnd;
197 int NumConv;
198 unsigned char ch;
199
200 if(pS == NULL) {
201 pS = &LocalConvState;
202 }
203 NumConv = 0;
204 if(Src != NULL) {
205 if(*Src != 0) {
206 SrcEnd = Src + Len;
207 while(Src < SrcEnd) {
208 ch = (unsigned char)*Src++;
209 NumConv = ProcessOneByte(ch, pS);
210 if(NumConv != -2) {
211 break;
212 }
213 }
214 }
215 else if(Dest != NULL) {
216 *Dest = 0;
217 }
218 }
219 if((NumConv > 0) && (Dest != NULL)) {
220 Dest[0] = pS->D[0];
221 if(NumConv == 4) {
222 Dest[1] = pS->D[1];
223 }
224 }
225 return NumConv;
226 }
227
228 /* Determine the number of bytes needed to represent a Wide character
229 as a MBCS character.
230
231 A single wide character may convert into a one, two, three, or four byte
232 narrow (MBCS or UTF-8) character. The number of MBCS bytes can be determined
233 as follows.
234
235 If WCS char < 0x00000080 One Byte
236 Else if WCS char < 0x0000D800 Two Bytes
237 Else Three Bytes
238
239 Since UEFI only supports the Unicode Base Multilingual Plane (BMP),
240 Four-byte characters are not supported.
241
242 @param[in] InCh Wide character to test.
243
244 @retval -1 Improperly formed character
245 @retval 0 InCh is 0x0000
246 @retval >0 Number of bytes needed for the MBCS character
247 */
248 int
249 EFIAPI
250 OneWcToMcLen(const wchar_t InCh)
251 {
252 ssize_t NumBytes;
253
254 if(InCh == 0) { // Is this a NUL, 0x0000 ?
255 NumBytes = 0;
256 }
257 else if(InCh < 0x0080) { // Is this a 1-byte character?
258 NumBytes = 1;
259 }
260 else if(InCh < 0x0800) { // Is this a 2-byte character?
261 NumBytes = 2;
262 }
263 else if((InCh >= 0xD800) && (InCh < 0xE000)) { // Is this a surrogate?
264 NumBytes = -1;
265 }
266 else {
267 NumBytes = 3; // Otherwise, it must be a 3-byte character.
268 }
269 return (int)NumBytes; // Return extimate of required bytes.
270 }
271
272 /* Determine the number of bytes needed to represent a Wide character string
273 as a MBCS string of given maximum length. Will optionally return the number
274 of wide characters that would be consumed.
275
276 A single wide character may convert into a one, two, three, or four byte
277 narrow (MBCS or UTF-8) character. The number of MBCS bytes can be determined
278 as follows.
279
280 If WCS char < 0x00000080 One Byte
281 Else if WCS char < 0x00000800 Two Bytes
282 Else if WCS char < 0x00010000 Three Bytes
283 Else Four Bytes
284
285 Since UEFI only supports the Unicode Base Multilingual Plane (BMP),
286 Four-byte characters should not be encountered.
287
288 @param[in] Src Pointer to a wide character string.
289 @param[in] Limit Maximum number of bytes the converted string may occupy.
290 @param[out] NumChar Pointer to where to store the number of wide characters, or NULL.
291
292 @return The number of bytes required to convert Src to MBCS,
293 not including the terminating NUL. If NumChar is not NULL, the number
294 of characters represented by the return value will be written to
295 where it points.
296 */
297 size_t
298 EFIAPI
299 EstimateWtoM(const wchar_t * Src, size_t Limit, size_t *NumChar)
300 {
301 ssize_t Estimate;
302 size_t CharCount;
303 ssize_t NumBytes;
304 wchar_t EChar;
305
306 Estimate = 0;
307 CharCount = 0;
308 EChar = *Src++; // Get the initial character and point to next
309 while(((NumBytes = OneWcToMcLen(EChar)) > 0) &&
310 ((size_t)(Estimate + NumBytes) < Limit))
311 { // Until one of the source characters is NUL
312 ++CharCount; // Count this character.
313 Estimate += NumBytes; // Count the Bytes for this character
314 EChar = *Src++; // Get the next source character and point to the next.
315 }
316 if(NumChar != NULL) {
317 *NumChar = CharCount;
318 }
319 return (size_t)Estimate; // Return esimate of required bytes.
320 }
321
322 /* Determine the number of characters in a MBCS string.
323 MBCS characters are one to four bytes long. By examining the first byte
324 of a MBCS character, one can determine the number of bytes comprising the
325 character.
326
327 0x00 - 0x7F One
328 0xC0 - 0xDF Two
329 0xE0 - 0xEF Three
330 0xF0 - 0xF7 Four
331
332 Since UEFI only supports the Unicode Base Multilingual Plane (BMP),
333 Four-byte characters should not be encountered.
334
335 @param[in] Src The string to examine
336
337 @return The number of characters represented by the MBCS string.
338 **/
339 size_t
340 EFIAPI
341 CountMbcsChars(const char *Src)
342 {
343 size_t Count;
344 char EChar;
345
346 Count = 0;
347 EChar = *Src++;
348 while(EChar != 0) {
349 if(EChar < 0x80) {
350 ++Count;
351 }
352 else if(EChar < 0xE0) {
353 Count += 2;
354 ++Src;
355 }
356 else if(EChar < 0xF0) {
357 Count += 3;
358 Src += 2;
359 }
360 else {
361 // Ill-formed character
362 break;
363 }
364 }
365 return Count;
366 }
367
368 /** Convert a wide character (UTF16) into a multibyte character (UTF8)
369
370 Converts a wide character into a corresponding multibyte character that
371 begins in the conversion state described by the object pointed to by ps.
372 If dst is not a null pointer, the converted character is then stored into
373 the array pointed to by dst.
374
375 It is the caller's responsibility to ensure that Dest is large enough to
376 hold the resulting MBCS sequence.
377
378 @param s Pointer to the wide-character string to convert
379 @param Dest Pointer to the buffer in which to place the converted sequence, or NULL.
380
381 @retval -1 An error occurred. The error reason is in errno.
382 @retval >=0 The number of bytes stored into Dest.
383 **/
384 ssize_t
385 EncodeUtf8(char *Dest, wchar_t ch)
386 {
387 char *p; /* next free byte in build buffer */
388 int NumInBuff; // number of bytes in Buff
389 char Buff[4]; // Buffer into which each character is built
390
391 p = Buff;
392
393 NumInBuff = 0;
394 if (ch < 0x80) {
395 /* Encode ASCII -- One Byte */
396 *p++ = (char) ch;
397 NumInBuff = 1;
398 }
399 else if (ch < 0x0800) {
400 /* Encode Latin-1 -- Two Byte */
401 *p++ = (char)(0xc0 | (ch >> 6));
402 *p++ = (char)(0x80 | (ch & 0x3f));
403 NumInBuff = 2;
404 }
405 else {
406 /* Encode UCS2 Unicode ordinals -- Three Byte */
407 /* Special case: check for surrogate -- Shouldn't happen in UEFI */
408 if (0xD800 <= ch && ch < 0xE000) {
409 errno = EILSEQ;
410 return -1;
411 }
412 else {
413 *p++ = (char)(0xe0 | (ch >> 12));
414 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
415 *p++ = (char)(0x80 | (ch & 0x3f));
416 NumInBuff = 3;
417 }
418 }
419 /* At this point, Buff holds the converted character which is NumInBuff bytes long.
420 NumInBuff is the value 1, 2, 3, or 4
421 */
422 if(Dest != NULL) { // Save character if Dest is not NULL
423 memcpy(Dest, Buff, NumInBuff);
424 }
425 return NumInBuff; // Tell the caller
426 }
427
428 // ######################## Narrow to Wide Conversions #######################
429
430 /** If ps is not a null pointer, the mbsinit function determines whether the
431 pointed-to mbstate_t object describes an initial conversion state.
432
433 @param[in] ps Pointer to the conversion state object to test.
434
435 @return The mbsinit function returns nonzero if ps is a null pointer
436 or if the pointed-to object describes an initial conversion
437 state; otherwise, it returns zero.
438
439 Declared in: wchar.h
440 **/
441 int
442 mbsinit(const mbstate_t *ps)
443 {
444 if((ps == NULL) || (ps->A == 0)) {
445 return 1;
446 }
447 return 0;
448 }
449
450 /** The mbrlen function is equivalent to the call:<BR>
451 @verbatim
452 mbrtowc(NULL, s, n, ps != NULL ? ps : &internal)
453 @endverbatim
454 where internal is the mbstate_t object for the mbrlen function, except that
455 the expression designated by ps is evaluated only once.
456
457 @param[in] s Pointer to a multibyte character sequence.
458 @param[in] n Maximum number of bytes to examine.
459 @param[in] pS Pointer to the conversion state object.
460
461 @retval 0 The next n or fewer characters complete a NUL.
462 @retval 1..n The number of bytes that complete the multibyte character.
463 @retval -2 The next n bytes contribute to an incomplete (but potentially valid) multibyte character.
464 @retval -1 An encoding error occurred.
465
466 Declared in: wchar.h
467 **/
468 size_t
469 mbrlen(
470 const char *s,
471 size_t n,
472 mbstate_t *pS
473 )
474 {
475 return mbrtowc(NULL, s, n, pS);
476 }
477
478 /** Determine the number of bytes comprising a multibyte character.
479
480 If S is not a null pointer, the mblen function determines the number of bytes
481 contained in the multibyte character pointed to by S. Except that the
482 conversion state of the mbtowc function is not affected, it is equivalent to
483 mbtowc((wchar_t *)0, S, N);
484
485 @param[in] S NULL to query whether multibyte characters have
486 state-dependent encodings. Otherwise, points to a
487 multibyte character.
488 @param[in] N The maximum number of bytes in a multibyte character.
489
490 @return If S is a null pointer, the mblen function returns a nonzero or
491 zero value, if multibyte character encodings, respectively, do
492 or do not have state-dependent encodings. If S is not a null
493 pointer, the mblen function either returns 0 (if S points to the
494 null character), or returns the number of bytes that are contained
495 in the multibyte character (if the next N or fewer bytes form a
496 valid multibyte character), or returns -1 (if they do not form a
497 valid multibyte character).
498
499 Declared in: stdlib.h
500 **/
501 int
502 mblen(
503 const char *s,
504 size_t n
505 )
506 {
507 return (int)mbrlen(s, n, NULL);
508 }
509
510 /**
511 If S is a null pointer, the mbrtowc function is equivalent to the call:<BR>
512 @verbatim
513 mbrtowc(NULL, "", 1, ps)
514 @endverbatim
515
516 In this case, the values of the parameters pwc and n are ignored.
517
518 If S is not a null pointer, the mbrtowc function inspects at most n bytes beginning with
519 the byte pointed to by S to determine the number of bytes needed to complete the next
520 multibyte character (including any shift sequences). If the function determines that the
521 next multibyte character is complete and valid, it determines the value of the
522 corresponding wide character and then, if pwc is not a null pointer, stores that value in
523 the object pointed to by pwc. If the corresponding wide character is the null wide
524 character, the resulting state described is the initial conversion state.
525
526 @param[out] pwc Pointer to where the resulting wide character is to be stored.
527 @param[in] s Pointer to a multibyte character "string".
528 @param[in] n The maximum number of bytes to inspect.
529 @param[in] ps Pointer to a conversion state object.
530
531 @retval 0 if the next n or fewer bytes complete the multibyte
532 character that corresponds to the null wide
533 character (which is the value stored).
534 @retval between_1_and_n_inclusive if the next n or fewer bytes complete
535 a valid multibyte character (which is the value
536 stored); the value returned is the number of bytes
537 that complete the multibyte character.
538 @retval (size_t)(-2) if the next n bytes contribute to an incomplete
539 (but potentially valid) multibyte character, and
540 all n bytes have been processed (no value is stored).
541 @retval (size_t)(-1) if an encoding error occurs, in which case the next
542 n or fewer bytes do not contribute to a complete and
543 valid multibyte character (no value is stored); the
544 value of the macro EILSEQ is stored in errno, and
545 the conversion state is unspecified.
546
547 Declared in: wchar.h
548 **/
549 size_t
550 mbrtowc(
551 wchar_t *pwc,
552 const char *s,
553 size_t n,
554 mbstate_t *ps
555 )
556 {
557 int RetVal;
558
559 RetVal = DecodeOneStateful(pwc, s, (ssize_t)n, ps);
560 return (size_t)RetVal;
561 }
562
563 /** Convert a multibyte character into a wide character.
564
565 If S is not a null pointer, the mbtowc function inspects at most N bytes
566 beginning with the byte pointed to by S to determine the number of bytes
567 needed to complete the next multibyte character (including any shift
568 sequences). If the function determines that the next multibyte character
569 is complete and valid, it determines the value of the corresponding wide
570 character and then, if Pwc is not a null pointer, stores that value in
571 the object pointed to by Pwc. If the corresponding wide character is the
572 null wide character, the function is left in the initial conversion state.
573
574 @param[out] Pwc Pointer to a wide-character object to receive the converted character.
575 @param[in] S Pointer to a multibyte character to convert.
576 @param[in] N Maximum number of bytes in a multibyte character.
577
578 @return If S is a null pointer, the mbtowc function returns a nonzero or
579 zero value, if multibyte character encodings, respectively, do
580 or do not have state-dependent encodings. If S is not a null
581 pointer, the mbtowc function either returns 0 (if S points to
582 the null character), or returns the number of bytes that are
583 contained in the converted multibyte character (if the next N or
584 fewer bytes form a valid multibyte character), or returns -1
585 (if they do not form a valid multibyte character).
586
587 In no case will the value returned be greater than N or the value
588 of the MB_CUR_MAX macro.
589
590 Declared in: stdlib.h
591 **/
592 int
593 mbtowc(
594 wchar_t *pwc,
595 const char *s,
596 size_t n
597 )
598 {
599 return (int)mbrtowc(pwc, s, n, NULL);
600 }
601
602 /**
603 The mbsrtowcs function converts a sequence of multibyte characters that begins in the
604 conversion state described by the object pointed to by ps, from the array indirectly
605 pointed to by src into a sequence of corresponding wide characters. If dst is not a null
606 pointer, the converted characters are stored into the array pointed to by dst. Conversion
607 continues up to and including a terminating null character, which is also stored.
608 Conversion stops earlier in two cases: when a sequence of bytes is encountered that does
609 not form a valid multibyte character, or (if dst is not a null pointer) when len wide
610 characters have been stored into the array pointed to by dst. Each conversion takes
611 place as if by a call to the mbrtowc function.
612
613 If dst is not a null pointer, the pointer object pointed to by src is assigned either a null
614 pointer (if conversion stopped due to reaching a terminating null character) or the address
615 just past the last multibyte character converted (if any). If conversion stopped due to
616 reaching a terminating null character and if dst is not a null pointer, the resulting state
617 described is the initial conversion state.
618
619 @param[out] dst Pointer to where the resulting wide character sequence is stored.
620 @param[in] src Pointer to a pointer to the multibyte character sequence to convert.
621 @param[in] len Maximum number of wide characters to be stored into dst.
622 @param[in] ps Pointer to a conversion state object.
623
624 @return If the input conversion encounters a sequence of bytes that do
625 not form a valid multibyte character, an encoding error occurs:
626 the mbsrtowcs function stores the value of the macro EILSEQ in
627 errno and returns (size_t)(-1); the conversion state is
628 unspecified. Otherwise, it returns the number of multibyte
629 characters successfully converted, not including the terminating
630 null character (if any).
631
632 Declared in: wchar.h
633 **/
634 size_t
635 mbsrtowcs(
636 wchar_t *dst,
637 const char **src,
638 size_t len,
639 mbstate_t *ps
640 )
641 {
642 int x;
643 size_t RetVal = 0;
644 const char *MySrc;
645
646 if((src == NULL) || (*src == NULL)) {
647 return 0;
648 }
649
650 MySrc = *src;
651 for(x = 1 ; (len != 0) && (x > 0); --len) {
652 x = DecodeOneStateful(dst, MySrc, MB_LEN_MAX, ps);
653 switch(x) {
654 case -2: // Incomplete character
655 case -1: // Encoding error
656 RetVal = (size_t)x;
657 break;
658 case 0: // Encountered NUL character: done.
659 if(dst != NULL) {
660 *dst = 0;
661 *src = NULL;
662 }
663 break;
664 default: // Successfully decoded a character, continue with next
665 MySrc += x;
666 if(dst != NULL) {
667 ++dst;
668 if(x == 4) {
669 ++dst;
670 }
671 *src = MySrc;
672 }
673 ++RetVal;
674 break;
675 }
676 }
677 return RetVal;
678 }
679
680 /** Convert a multibyte character string into a wide-character string.
681
682 The mbstowcs function converts a sequence of multibyte characters that
683 begins in the initial shift state from the array pointed to by Src into
684 a sequence of corresponding wide characters and stores not more than limit
685 wide characters into the array pointed to by Dest. No multibyte
686 characters that follow a null character (which is converted into a null
687 wide character) will be examined or converted. Each multibyte character
688 is converted as if by a call to the mbtowc function, except that the
689 conversion state of the mbtowc function is not affected.
690
691 No more than Limit elements will be modified in the array pointed to by Dest.
692 If copying takes place between objects that overlap,
693 the behavior is undefined.
694
695 @param[out] Dest Pointer to the array to receive the converted string.
696 @param[in] Src Pointer to the string to be converted.
697 @param[in] Limit Maximum number of elements to be written to Dest.
698
699 @return If an invalid multibyte character is encountered, the mbstowcs
700 function returns (size_t)(-1). Otherwise, the mbstowcs function
701 returns the number of array elements modified, not including a
702 terminating null wide character, if any.
703
704 Declared in: stdlib.h
705 **/
706 size_t
707 mbstowcs(
708 wchar_t *Dest,
709 const char *Src,
710 size_t Limit
711 )
712 {
713
714 /* Dest may be NULL */
715 /* Src may be NULL */
716
717 return mbsrtowcs(Dest, &Src, Limit, NULL);
718 }
719
720 /** The btowc function determines whether C constitutes a valid single-byte
721 character in the initial shift state.
722
723 @param[in] C A narrow character to test or convert to wide.
724
725 @return The btowc function returns WEOF if c has the value EOF or if
726 (unsigned char)C does not constitute a valid single-byte
727 character in the initial shift state. Otherwise, it returns the
728 wide character representation of that character.
729
730 Declared in: wchar.h
731 **/
732 wint_t
733 btowc(int c)
734 {
735 int x;
736 wchar_t Dest;
737 wint_t RetVal = WEOF;
738
739 if (c == EOF)
740 return WEOF;
741 x = DecodeOneStateful(&Dest, (const char *)&c, 1, NULL);
742 if(x == 0) {
743 RetVal = 0;
744 }
745 else if(x == 1) {
746 RetVal = (wint_t)Dest;
747 }
748 return RetVal;
749 }
750
751 // ######################## Wide to Narrow Conversions #######################
752
753 /**
754 If S is a null pointer, the wcrtomb function is equivalent to the call:<BR>
755 @verbatim
756 wcrtomb(buf, L'\0', ps)
757 @endverbatim
758 where buf is an internal buffer.
759
760 If S is not a null pointer, the wcrtomb function determines the number of bytes needed
761 to represent the multibyte character that corresponds to the wide character given by wc
762 (including any shift sequences), and stores the multibyte character representation in the
763 array whose first element is pointed to by S. At most MB_CUR_MAX bytes are stored. If
764 wc is a null wide character, a null byte is stored, preceded by any shift sequence needed
765 to restore the initial shift state; the resulting state described is the initial conversion state.
766
767 @param[out] Dest Pointer to the location in which to store the resulting
768 multibyte character. Otherwise, NULL to reset the
769 conversion state.
770 @param[in] wchar The wide character to convert.
771 @param[in,out] pS Pointer to a conversion state object, or NULL.
772
773 @return The wcrtomb function returns the number of bytes stored in the
774 array object (including any shift sequences). When wc is not a
775 valid wide character, an encoding error occurs: the function
776 stores the value of the macro EILSEQ in errno and
777 returns (size_t)(-1); the conversion state is unspecified.
778
779 Declared in: wchar.h
780 **/
781 size_t
782 wcrtomb(
783 char *Dest,
784 wchar_t wchar,
785 mbstate_t *pS
786 )
787 {
788 size_t RetVal;
789
790 /* Dest may be NULL */
791 if (Dest == NULL) {
792 RetVal = 1;
793 }
794 else {
795 if (wchar == L'\0') {
796 *Dest = '\0';
797 RetVal = 1;
798 }
799 else {
800 RetVal = EncodeUtf8(Dest, wchar);
801 }
802 }
803 if(pS == NULL) {
804 pS = &LocalConvState;
805 }
806 pS->A = 0; // Set ps to the initial conversion state
807
808 return RetVal;
809 }
810
811 /** Convert a wide character into a multibyte character.
812
813 The wctomb function determines the number of bytes needed to represent the
814 multibyte character corresponding to the wide character given by WC
815 (including any shift sequences), and stores the multibyte character
816 representation in the array whose first element is pointed to by S (if S is
817 not a null pointer). At most MB_CUR_MAX characters are stored. If WC is a
818 null wide character, a null byte is stored, preceded by any shift sequence
819 needed to restore the initial shift state, and the function is left in the
820 initial conversion state.
821
822 @param[out] S Pointer to the object to receive the converted multibyte character.
823 @param[in] WC Wide character to be converted.
824
825 @return If S is a null pointer, the wctomb function returns a nonzero or
826 zero value, if multibyte character encodings, respectively, do or
827 do not have state-dependent encodings. If S is not a null pointer,
828 the wctomb function returns -1 if the value of WC does not
829 correspond to a valid multibyte character, or returns the number
830 of bytes that are contained in the multibyte character
831 corresponding to the value of WC.
832
833 In no case will the value returned be greater than the value of
834 the MB_CUR_MAX macro.
835
836 Declared in: stdlib.h
837 **/
838 int
839 wctomb(
840 char *s,
841 wchar_t wchar
842 )
843 {
844 /*
845 If s is NULL just return whether MB Characters have state
846 dependent encodings -- they don't.
847 */
848 if (s == NULL)
849 return 0;
850
851 return (int)wcrtomb(s, wchar, NULL);
852 }
853
854 /** The wcsrtombs function converts a sequence of wide characters from the array
855 indirectly pointed to by Src into a sequence of corresponding multibyte
856 characters that begins in the conversion state described by the object
857 pointed to by ps.
858
859 If Dest is not a null pointer, the converted characters are stored into the
860 array pointed to by Dest. Conversion continues up to and including a
861 terminating null wide character, which is also stored. Conversion stops
862 earlier in two cases: when a wide character is reached that does not
863 correspond to a valid multibyte character, or (if Dest is not a null
864 pointer) when the next multibyte character would exceed the limit of Limit
865 total bytes to be stored into the array pointed to by Dest. Each conversion
866 takes place as if by a call to the wcrtomb function.)
867
868 If Dest is not a null pointer, the pointer object pointed to by Src is
869 assigned either a null pointer (if conversion stopped due to reaching
870 a terminating null wide character) or the address just past the last wide
871 character converted (if any). If conversion stopped due to reaching a
872 terminating null wide character, the resulting state described is the
873 initial conversion state.
874
875 @param[in] Dest
876 @param[in,out] Src
877 @param[in] Limit Max number of bytes to store in Dest.
878 @param[in,out] ps
879
880 @return If conversion stops because a wide character is reached that
881 does not correspond to a valid multibyte character, an
882 encoding error occurs: the wcsrtombs function stores the
883 value of the macro EILSEQ in errno and returns (size_t)(-1);
884 the conversion state is unspecified. Otherwise, it returns
885 the number of bytes in the resulting multibyte character
886 sequence, not including the terminating null character (if any).
887
888 Declared in: wchar.h
889 **/
890 size_t
891 wcsrtombs(
892 char *Dest,
893 const wchar_t **Src,
894 size_t Limit,
895 mbstate_t *ps
896 )
897 {
898 size_t NumStored;
899 ssize_t MaxBytes;
900 int count;
901 wchar_t InCh;
902
903 NumStored = 0;
904 MaxBytes = (ssize_t)Limit;
905
906 /* Dest may be NULL */
907 /* Src may be NULL */
908 /* ps appears to be unused */
909
910 if (Src == NULL || *Src == NULL)
911 return (0);
912
913 if (Dest == NULL) {
914 NumStored = EstimateWtoM(*Src, ASCII_STRING_MAX, NULL);
915 }
916 else {
917 if((MaxBytes < 0) || (MaxBytes > ASCII_STRING_MAX)) {
918 MaxBytes = ASCII_STRING_MAX;
919 }
920 while ((MaxBytes > 0) && (OneWcToMcLen(InCh = *(*Src)++) <= MaxBytes)) {
921 if(InCh == 0) {
922 *Src = NULL;
923 *Dest = 0; // NUL terminate Dest string, but don't count the NUL
924 break;
925 }
926 count = (int)wcrtomb(Dest, InCh, NULL);
927 if(count >= 0) {
928 Dest += count;
929 MaxBytes -= count;
930 NumStored += count;
931 }
932 else {
933 NumStored = (size_t)(-1);
934 }
935 }
936 }
937
938
939 return NumStored;
940 }
941
942 /** Convert a wide-character string into a multibyte character string.
943
944 The wcstombs function converts a sequence of wide characters from the
945 array pointed to by Src into a sequence of corresponding multibyte
946 characters that begins in the initial shift state, and stores these
947 multibyte characters into the array pointed to by Dest, stopping if a
948 multibyte character would exceed the limit of Limit total bytes or if a
949 null character is stored. Each wide character is converted as if by
950 a call to the wctomb function, except that the conversion state of
951 the wctomb function is not affected.
952
953 No more than Limit bytes will be modified in the array pointed to by Dest.
954 If copying takes place between objects that overlap,
955 the behavior is undefined.
956
957 @param[out] Dest Pointer to the array to receive the converted string.
958 @param[in] Src Pointer to the string to be converted.
959 @param[in] Limit Maximum number of elements to be written to Dest.
960
961 @return If a wide character is encountered that does not correspond to a
962 valid multibyte character, the wcstombs function returns
963 (size_t)(-1). Otherwise, the wcstombs function returns the number
964 of bytes modified, not including a terminating null character,
965 if any.
966
967 Declared in: stdlib.h
968 **/
969 size_t
970 wcstombs(
971 char *Dest,
972 const wchar_t *Src,
973 size_t Limit
974 )
975 {
976 /* Dest may be NULL */
977 return wcsrtombs(Dest, &Src, Limit, NULL);
978 }
979
980 /** The wctob function determines whether C corresponds to a member of the extended
981 character set whose multibyte character representation is a single byte when in the initial
982 shift state.
983
984 wctob needs to be consistent with wcrtomb.
985 If wcrtomb says that a character is representable in 1 byte,
986 then wctob needs to also represent the character as 1 byte.
987
988 @return The wctob function returns EOF if C does not correspond to a multibyte
989 character with length one in the initial shift state. Otherwise, it
990 returns the single-byte representation of that character as an
991 unsigned char converted to an int.
992
993 Declared in: wchar.h
994 **/
995 int
996 wctob(wint_t c)
997 {
998 int RetVal;
999
1000 RetVal = EOF;
1001 if(c == 0) {
1002 RetVal = 0;
1003 }
1004 else if (OneWcToMcLen((const wchar_t)c) == 1) {
1005 RetVal = (int)(c & 0xFF);
1006 }
1007 return RetVal;
1008 }