]> git.proxmox.com Git - mirror_edk2.git/commitdiff
StdLib: Add multi-byte character support. The normal "narrow" character set is now...
authordarylm503 <darylm503@6f19259b-4bc3-4df7-8a09-765794883524>
Fri, 15 Jun 2012 19:58:39 +0000 (19:58 +0000)
committerdarylm503 <darylm503@6f19259b-4bc3-4df7-8a09-765794883524>
Fri, 15 Jun 2012 19:58:39 +0000 (19:58 +0000)
Add library classes which are required by StdLib, but not commonly defined in Platform DSC files, to StdLib.inc.
Modify MB_LEN_MAX to be 4, the maximum length of UTF-8 characters.
Adjust size of internal buffers to be multiples of MB_LEN_MAX instead of assuming 1-byte characters.
Make the XYoffset object public and move its declaration into EfiSysCall.h.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: daryl.mcdaniel@intel.com
Reviewed-by: erik.c.bjorge@intel.com
Reviewed-by: lee.g.rosenbaum@intel.com
Reviewed-by: leroy.p.leahy@intel.com
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13457 6f19259b-4bc3-4df7-8a09-765794883524

15 files changed:
StdLib/Include/Arm/machine/ansi.h
StdLib/Include/Ia32/machine/ansi.h
StdLib/Include/Ipf/machine/ansi.h
StdLib/Include/X64/machine/ansi.h
StdLib/Include/limits.h
StdLib/Include/stdio.h
StdLib/Include/sys/EfiSysCall.h
StdLib/LibC/Locale/Locale.inf
StdLib/LibC/Locale/multibyte_Utf8.c [new file with mode: 0644]
StdLib/LibC/Stdio/fputwc.c
StdLib/LibC/Stdio/makebuf.c
StdLib/LibC/Uefi/Devices/Console/daConsole.c
StdLib/LibC/Uefi/SysCalls.c
StdLib/StdLib.inc
StdLibPrivateInternalFiles/Include/Device/Console.h

index 4c9e65a3bc2e1b8a7f0ea2beede5bd7b77117bbd..8273905cd362c2d2d51a3789e0255a6bcd256a91 100644 (file)
@@ -1,6 +1,15 @@
-/*  $NetBSD: ansi.h,v 1.7 2006/10/04 13:51:59 tnozaki Exp $ */\r
+/** @file\r
+    Machine dependent ANSI type definitions.\r
+\r
+    Copyright (c) 2010-2012, Intel Corporation. All rights reserved.<BR>\r
+    This program and the accompanying materials are licensed and made available\r
+    under the terms and conditions of the BSD License that accompanies this\r
+    distribution.  The full text of the license may be found at\r
+    http://opensource.org/licenses/bsd-license.php.\r
+\r
+    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-/*\r
  * Copyright (c) 1990, 1993\r
  *  The Regents of the University of California.  All rights reserved.\r
  *\r
@@ -29,8 +38,8 @@
  * SUCH DAMAGE.\r
  *\r
  *  from: @(#)ansi.h  8.2 (Berkeley) 1/4/94\r
- */\r
-\r
+    NetBSD: ansi.h,v 1.7 2006/10/04 13:51:59 tnozaki Exp\r
+**/\r
 #ifndef _ANSI_H_\r
 #define _ANSI_H_\r
 \r
  * mbstate_t is an opaque object to keep conversion state, during multibyte\r
  * stream conversions.  The content must not be referenced by user programs.\r
  */\r
-typedef union {\r
-  __int64_t __mbstateL; /* for alignment */\r
-  char __mbstate8[128];\r
+typedef struct {\r
+  UINT32  A;      // Np;\r
+  UINT32  B;      // U;\r
+  UINT32  E;      // L\r
+  UINT8   C[4];   // n[4]\r
+  UINT16  D[2];   // w[2]\r
 } __mbstate_t;\r
 #define _BSD_MBSTATE_T_   __mbstate_t /* mbstate_t */\r
 \r
index 5872eb36946a7f2ced0eeb6939d924f4c67ca03e..6ef0a740cab7b1b631796a43478ef1d74254198a 100644 (file)
@@ -1,8 +1,17 @@
-/*     $NetBSD: ansi.h,v 1.19 2006/10/04 13:52:00 tnozaki Exp $        */\r
+/** @file\r
+    Machine dependent ANSI type definitions.\r
+\r
+    Copyright (c) 2010-2012, Intel Corporation. All rights reserved.<BR>\r
+    This program and the accompanying materials are licensed and made available\r
+    under the terms and conditions of the BSD License that accompanies this\r
+    distribution.  The full text of the license may be found at\r
+    http://opensource.org/licenses/bsd-license.php.\r
+\r
+    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-/*-\r
  * Copyright (c) 1990, 1993\r
- *     The Regents of the University of California.  All rights reserved.\r
+ *  The Regents of the University of California.  All rights reserved.\r
  *\r
  * Redistribution and use in source and binary forms, with or without\r
  * modification, are permitted provided that the following conditions\r
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
  * SUCH DAMAGE.\r
  *\r
- *     @(#)ansi.h      8.2 (Berkeley) 1/4/94\r
+ *  @(#)ansi.h  8.2 (Berkeley) 1/4/94\r
+    NetBSD: ansi.h,v 1.19 2006/10/04 13:52:00 tnozaki Exp\r
  */\r
-\r
-#ifndef        _ANSI_H_\r
-#define        _ANSI_H_\r
+#ifndef _ANSI_H_\r
+#define _ANSI_H_\r
 \r
 #include  <sys/EfiCdefs.h>\r
 \r
  * Types which are fundamental to the implementation and may appear in\r
  * more than one standard header are defined here.  Standard headers\r
  * then use:\r
- *     #ifdef  _BSD_SIZE_T_\r
- *     typedef _BSD_SIZE_T_ size_t;\r
- *     #undef  _BSD_SIZE_T_\r
- *     #endif\r
+ *  #ifdef  _BSD_SIZE_T_\r
+ *  typedef _BSD_SIZE_T_ size_t;\r
+ *  #undef  _BSD_SIZE_T_\r
+ *  #endif\r
  */\r
 #define _BSD_CLOCK_T_     _EFI_CLOCK_T      /* clock() */\r
 #define _BSD_PTRDIFF_T_   _EFI_PTRDIFF_T_   /* ptr1 - ptr2 */\r
@@ -70,7 +79,7 @@
  * chosen over a long is that the is*() and to*() routines take ints (says\r
  * ANSI C), but they use _RUNE_T_ instead of int.  By changing it here, you\r
  * lose a bit of ANSI conformance, but your programs will still work.\r
- *    \r
+ *\r
  * Note that _WCHAR_T_ and _RUNE_T_ must be of the same type.  When wchar_t\r
  * and rune_t are typedef'd, _WCHAR_T_ will be undef'd, but _RUNE_T remains\r
  * defined for ctype.h.\r
 #define _BSD_WCHAR_T_     _EFI_WCHAR_T    /* wchar_t */\r
 #define _BSD_WINT_T_      _EFI_WINT_T     /* wint_t */\r
 #define _BSD_RUNE_T_      _EFI_WCHAR_T    /* rune_t */\r
-#define _BSD_WCTRANS_T_                void *          /* wctrans_t */\r
+#define _BSD_WCTRANS_T_   void *    /* wctrans_t */\r
 #define _BSD_WCTYPE_T_    unsigned int    /* wctype_t */\r
 \r
 /*\r
  * mbstate_t is an opaque object to keep conversion state, during multibyte\r
  * stream conversions.  The content must not be referenced by user programs.\r
  */\r
-typedef union {\r
-       __int64_t __mbstateL;   /* for alignment */\r
-       char __mbstate8[128];\r
+typedef struct {\r
+  UINT32  A;      // Np;\r
+  UINT32  B;      // U;\r
+  UINT32  E;      // L\r
+  UINT8   C[4];   // n[4]\r
+  UINT16  D[2];   // w[2]\r
 } __mbstate_t;\r
-#define        _BSD_MBSTATE_T_         __mbstate_t     /* mbstate_t */\r
+#define _BSD_MBSTATE_T_   __mbstate_t /* mbstate_t */\r
 \r
-#endif /* _ANSI_H_ */\r
+#endif  /* _ANSI_H_ */\r
index 07a200871726cfe1e372064d08d6fd77f6d32814..bab41b040c6aa0cbedf259559437daa7c98e8eeb 100644 (file)
@@ -1,6 +1,15 @@
-/*  $NetBSD: ansi.h,v 1.3 2006/10/04 13:52:00 tnozaki Exp $ */\r
+/** @file\r
+    Machine dependent ANSI type definitions.\r
+\r
+    Copyright (c) 2010-2012, Intel Corporation. All rights reserved.<BR>\r
+    This program and the accompanying materials are licensed and made available\r
+    under the terms and conditions of the BSD License that accompanies this\r
+    distribution.  The full text of the license may be found at\r
+    http://opensource.org/licenses/bsd-license.php.\r
+\r
+    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-/*-\r
  * Copyright (c) 1990, 1993\r
  *  The Regents of the University of California.  All rights reserved.\r
  *\r
@@ -29,8 +38,8 @@
  * SUCH DAMAGE.\r
  *\r
  *  @(#)ansi.h  8.2 (Berkeley) 1/4/94\r
+    NetBSD: ansi.h,v 1.3 2006/10/04 13:52:00 tnozaki Exp\r
  */\r
-\r
 #ifndef _ANSI_H_\r
 #define _ANSI_H_\r
 \r
  * mbstate_t is an opaque object to keep conversion state, during multibyte\r
  * stream conversions.  The content must not be referenced by user programs.\r
  */\r
-typedef union {\r
-  __int64_t __mbstateL; /* for alignment */\r
-  char __mbstate8[128];\r
+typedef struct {\r
+  UINT32  A;      // Np;\r
+  UINT32  B;      // U;\r
+  UINT32  E;      // L\r
+  UINT8   C[4];   // n[4]\r
+  UINT16  D[2];   // w[2]\r
 } __mbstate_t;\r
 #define _BSD_MBSTATE_T_   __mbstate_t /* mbstate_t */\r
 \r
index 1d276f608c66d8b4cc21c26ca06dc59c477333b9..e6ff195a8207df35589fd4d075258614f9b040f7 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
     Machine dependent ANSI type definitions.\r
 \r
-    Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+    Copyright (c) 2010-2012, Intel Corporation. All rights reserved.<BR>\r
     This program and the accompanying materials are licensed and made available\r
     under the terms and conditions of the BSD License that accompanies this\r
     distribution.  The full text of the license may be found at\r
  * mbstate_t is an opaque object to keep conversion state, during multibyte\r
  * stream conversions.  The content must not be referenced by user programs.\r
  */\r
-typedef union {\r
-  __int64_t __mbstateL; /* for alignment */\r
-  char __mbstate8[128];\r
+typedef struct {\r
+  UINT32  A;      // Np;\r
+  UINT32  B;      // U;\r
+  UINT32  E;      // L\r
+  UINT8   C[4];   // n[4]\r
+  UINT16  D[2];   // w[2]\r
 } __mbstate_t;\r
 #define _BSD_MBSTATE_T_   __mbstate_t /* mbstate_t */\r
 \r
index 4fceca201590801e059c63fe944a0189209cba59..5da4032f02b1141ca4f58ba75f0bd47081465106 100644 (file)
@@ -8,7 +8,7 @@
   type as would an expression that is an object of the corresponding type\r
   converted according to the integer promotions.\r
 \r
-  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available under\r
   the terms and conditions of the BSD License that accompanies this distribution.\r
   The full text of the license may be found at\r
@@ -28,7 +28,7 @@
 /* Define the values required by the ISO/IEC 9899 Specification. */\r
 \r
 /** Maximum number of bytes in a multibyte character, for any supported locale. **/\r
-#define MB_LEN_MAX  2               /* 16-bit UTC-2 */\r
+#define MB_LEN_MAX  4               /* UTF-8 can require up to 4 bytes */\r
 \r
 /** Number of bits comprising the smallest object that is not a bit-field (byte). **/\r
 #define CHAR_BIT    __CHAR_BIT\r
index 59022666bd742c4ed20c9a1a0147917bb1e70894..c0a4deb7837eaeca1ab64edd8d7d8975fe28dad6 100644 (file)
           by a three-character attempt at a mnemonic.\r
 \r
 \r
-  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available under\r
   the terms and conditions of the BSD License that accompanies this distribution.\r
   The full text of the license may be found at\r
@@ -243,8 +243,8 @@ typedef struct __sFILE {
   /*@}*/\r
 \r
   /* tricks to meet minimum requirements even when malloc() fails */\r
-  unsigned char   _ubuf[3];   /**< guarantee an ungetc() buffer */\r
-  unsigned char   _nbuf[1];   /**< guarantee a getc() buffer */\r
+  unsigned char   _ubuf[3 * MB_LEN_MAX];   /**< guarantee an ungetc() buffer */\r
+  unsigned char   _nbuf[1 * MB_LEN_MAX];   /**< guarantee a getc() buffer */\r
 \r
   /** separate buffer for fgetln() when line crosses buffer boundary */\r
   struct  __sbuf  _lb;        /* buffer for fgetln() */\r
index cbaf1d1397e9b1da10f60afbc061f3186701e49b..6f4742875edac4335186e534bef7e5f2e0d42988 100644 (file)
@@ -53,7 +53,7 @@
       BOOLEAN   ValidateFD    (int fd, int IsOpen);\r
 @endverbatim\r
 \r
-  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available under\r
   the terms and conditions of the BSD License that accompanies this distribution.\r
   The full text of the license may be found at\r
@@ -333,4 +333,18 @@ __BEGIN_DECLS
   int       reboot    (int, char *);\r
 __END_DECLS\r
 \r
+/*  The console output stream, stdout, supports cursor positioning via the\r
+    lseek() function call.  The following entities facilitate packing the\r
+    X and Y coordinates into the offset parameter of the lseek call.\r
+*/\r
+typedef struct {\r
+  UINT32    Column;\r
+  UINT32    Row;\r
+} CURSOR_XY;\r
+\r
+typedef union {\r
+  UINT64      Offset;\r
+  CURSOR_XY   XYpos;\r
+} XY_OFFSET;\r
+\r
 #endif  /* _EFI_SYS_CALL_H */\r
index c891e3bfaf2a3a81dbf02b3d078a16d93f598e9b..e0f163cb1313789d8db6e0586be6c817f34d07e1 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 #  Standard C library: Locale implementation.\r
 #\r
-#  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2010-2012, Intel Corporation. All rights reserved.<BR>\r
 #\r
 #  This program and the accompanying materials\r
 #  are licensed and made available under the terms and conditions of the BSD License\r
@@ -10,7 +10,6 @@
 #  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 #\r
-#\r
 ##\r
 \r
 [Defines]\r
   wcstof.c                  #\r
   wcstold.c                 #\r
   wcsxfrm.c                 #\r
+  multibyte_Utf8.c          #\r
 \r
   # Single-byte locale to avoid bringing in citrus\r
   iswctype_sb.c             #\r
-  multibyte_sb.c            #\r
 \r
 [Packages]\r
   StdLib/StdLib.dec\r
diff --git a/StdLib/LibC/Locale/multibyte_Utf8.c b/StdLib/LibC/Locale/multibyte_Utf8.c
new file mode 100644 (file)
index 0000000..3f29f29
--- /dev/null
@@ -0,0 +1,829 @@
+/** @file\r
+  Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+**/\r
+#include  <assert.h>\r
+#include  <string.h>\r
+#include  <errno.h>\r
+#include  <stdlib.h>\r
+#include  <wchar.h>\r
+#include  <sys/types.h>\r
+\r
+typedef      int  ch_UCS4;\r
+\r
+static  mbstate_t         LocalConvState = {0};\r
+\r
+/** Map a UTF-8 encoded prefix byte to a sequence length.\r
+    Zero means illegal prefix, but valid surrogate if < 0xC0.\r
+    One indicates an ASCII-7 equivalent character.\r
+    Two, three, and four are the first byte for 2, 3, and 4 byte sequences, respectively.\r
+    See RFC 3629 for details.\r
+\r
+  TABLE ENCODING:\r
+    Low Nibble decodes the first byte into the number of bytes in the sequence.\r
+      A value of zero indicates an invalid byte.\r
+    The High Nibble encodes a bit mask to be used to match against the high nibble of the second byte.\r
+\r
+    example:\r
+      SequenceLength = code[c0] & 0x0F;\r
+      Mask           = 0x80 | code[c0];\r
+\r
+      Surrogate bytes are valid if: code[cX] & Mask > 0x80;\r
+\r
+*/\r
+static\r
+UINT8 utf8_code_length[256] = {\r
+  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* 00-0F */\r
+  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\r
+  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\r
+  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\r
+  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\r
+  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\r
+  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\r
+  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* 70-7F */\r
+  0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, /* 80-8F */\r
+  0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, /* 90-9F */\r
+  0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, /* A0-AF */\r
+  0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, /* B0-BF */\r
+  0x00, 0x00, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, /* C0-C1 + C2-CF */\r
+  0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, /* D0-DF */\r
+  0x43, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x73, 0x33, 0x73, 0x73, /* E0-EF */\r
+  0x64, 0x74, 0x74, 0x74, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  /* F0-F4 + F5-FF */\r
+};\r
+\r
+/** Process one byte of a multibyte character.\r
+\r
+    @param  ch\r
+    @param  ps\r
+\r
+    @retval   -2\r
+    @retval   -1\r
+    @retval   1:4\r
+**/\r
+static\r
+int\r
+ProcessOneByte(unsigned char ch, mbstate_t *ps)\r
+{\r
+  UINT32    Mask;\r
+  UINT32    Length;\r
+  int       RetVal = 0;\r
+\r
+  if(ps->A > 3) {\r
+    // We are in an invalid state\r
+    ps->A = 0;    // Initial State\r
+  }\r
+  ps->C[ps->A] = ch;  // Save the current character\r
+  Mask = utf8_code_length[ch];\r
+\r
+  if(ps->A == 0) {    // Initial State.  First byte of sequence.\r
+    ps->E   = Mask | 0x80;\r
+    Length  = Mask & 0xF;\r
+    switch(Length) {\r
+      case 0:                       // State 0, Code 0\r
+        errno = EILSEQ;\r
+        RetVal = -1;\r
+        ps->E = 1;        // Consume this character\r
+        break;\r
+      case 1:                       // State 0, Code 1\r
+        // ASCII-7 Character\r
+        ps->B = ps->D[0] = ch;\r
+        RetVal = 1;\r
+        break;\r
+      default:                      // State 0, Code 2, 3, 4\r
+        ps->A = 1;    // Next state is State-1\r
+        RetVal = -2;  // Incomplete but potentially valid character\r
+        break;\r
+    }\r
+  }\r
+  else {\r
+    // We are in state 1, 2, or 3 and processing a surrogate byte\r
+    Length  = ps->E & 0xF;\r
+    if((Mask & ps->E) > 0x80) {\r
+      // This byte is valid\r
+      switch(ps->A) {   // Process based upon our current state\r
+        case 1:             // Second byte of the sequence.\r
+          if(Length == 2) {         // State 1, Code 2\r
+            Length = ((ps->C[0] & 0x1f) << 6) + (ps->C[1] & 0x3f);\r
+            assert ((Length > 0x007F) && (Length <= 0x07FF));\r
+            ps->B = ps->D[0] = (UINT16)Length;\r
+            ps->A = 0;      // Next state is State-0\r
+            RetVal = 2;\r
+          }\r
+          else {    // This isn't the last character, get more.  State 1, Code 3 or 4\r
+            ps->A = 2;\r
+            RetVal = -2;\r
+          }\r
+          break;\r
+        case 2:             // Third byte of the sequence\r
+          if(Length == 3) {\r
+            Length = ((ps->C[0] & 0x0f) << 12) + ((ps->C[1] & 0x3f) << 6) + (ps->C[2] & 0x3f);\r
+            assert ((Length > 0x07FF) && (Length <= 0xFFFF));\r
+            ps->B = ps->D[0] = (UINT16)Length;\r
+            ps->A = 0;      // Next state is State-0\r
+            RetVal = 3;\r
+          }\r
+          else {\r
+            ps->A = 3;\r
+            RetVal = -2;\r
+          }\r
+          break;\r
+        case 3:             // Fourth byte of the sequence\r
+          if(Length == 4) {\r
+            Length = ((ps->C[0] & 0x7) << 18) + ((ps->C[1] & 0x3f) << 12) +\r
+                     ((ps->C[2] & 0x3f) << 6) + (ps->C[3] & 0x3f);\r
+            ps->B = Length;\r
+            assert ((Length > 0xFFFF) && (Length <= 0x10ffff));\r
+\r
+            /*  compute and append the two surrogates: */\r
+\r
+            /*  translate from 10000..10FFFF to 0..FFFF */\r
+            Length -= 0x10000;\r
+\r
+            /*  high surrogate = top 10 bits added to D800 */\r
+            ps->D[0] = (UINT16)(0xD800 + (Length >> 10));\r
+\r
+            /*  low surrogate = bottom 10 bits added to DC00 */\r
+            ps->D[1] = (UINT16)(0xDC00 + (Length & 0x03FF));\r
+            ps->A = 0;      // Next state is State-0\r
+            RetVal = 4;\r
+          }\r
+          else {\r
+            errno = EILSEQ;\r
+            ps->A = 0;\r
+            RetVal = -1;\r
+            ps->E = 4;      // Can't happen, but consume this character anyway\r
+          }\r
+          break;\r
+      }\r
+    }\r
+    else {                // Invalid surrogate character\r
+      errno = EILSEQ;\r
+      ps->A = 0;          // Next is State-0\r
+      RetVal = -1;\r
+      ps->E = 0;            // Don't Consume, it may be an initial byte\r
+    }\r
+  }\r
+  return RetVal;\r
+}\r
+\r
+/** Convert one Multibyte sequence.\r
+\r
+    @param  Dest\r
+    @param  Src\r
+    @param  Len\r
+    @param  pS\r
+\r
+    @retval   -2      Bytes processed comprise an incomplete, but potentially valid, character.\r
+    @retval   -1      An encoding error was encountered.  ps->E indicates the number of bytes consumed.\r
+    @retval   0       Either Src is NULL or it points to a NUL character.\r
+    @retval   1:N     N bytes were consumed producing a valid wide character.\r
+**/\r
+int\r
+DecodeOneStateful(\r
+  wchar_t    *Dest,       // Pointer to output location, or NULL\r
+  const char *Src,        // Multibyte Source (UTF8)\r
+  ssize_t     Len,        // Max Number of bytes to convert\r
+  mbstate_t  *pS          // Pointer to State struct., or NULL\r
+  )\r
+{\r
+  const char   *SrcEnd;\r
+  int           NumConv;\r
+  unsigned char ch;\r
+\r
+  if((Src == NULL) || (*Src == '\0')) {\r
+    return 0;\r
+  }\r
+  if(pS == NULL) {\r
+    pS = &LocalConvState;\r
+  }\r
+  SrcEnd  = Src + Len;\r
+  NumConv = 0;\r
+  while(Src < SrcEnd) {\r
+    ch = (unsigned char)*Src++;\r
+    NumConv = ProcessOneByte(ch, pS);\r
+    if(NumConv != -2)\r
+      break;\r
+  }\r
+  if((NumConv > 0) && (Dest != NULL)) {\r
+    Dest[0] = pS->D[0];\r
+    if(NumConv == 4) {\r
+      Dest[1] = pS->D[1];\r
+    }\r
+  }\r
+  return NumConv;\r
+}\r
+\r
+/** Convert wide characters (UTF16) into multibyte characters (UTF8)\r
+\r
+    @param  s       Pointer to the wide-character string to convert\r
+    @param  size    Number of wide characters in s.  size <= wcslen(s);\r
+\r
+    @return A newly allocated buffer containing the converted string is returned,\r
+            or NULL if an error occurred.  Global variable errno contains more\r
+            information if NULL is returned.\r
+**/\r
+ssize_t\r
+EncodeUtf8(char *Dest, wchar_t *s, ssize_t size)\r
+{\r
+  char       *p;              /* next free byte in build buffer */\r
+  char       *v;              /* next free byte in destination */\r
+  ssize_t     nneeded;        /* number of result bytes needed */\r
+  int         i;              /* index into s of next input byte */\r
+  int         NumInBuff;      // number of bytes in Buff\r
+  char        Buff[4];        // Buffer into which each character is built\r
+\r
+  assert(s != NULL);\r
+  assert(size >= 0);\r
+\r
+  v = Dest;\r
+  nneeded = 0;\r
+  if((size * MB_LEN_MAX) / MB_LEN_MAX != size) {\r
+    // size is too large and resulted in overflow when multiplied by MB_LEN_MAX\r
+    errno = EINVAL;\r
+    return (ssize_t)-1;\r
+  }\r
+\r
+ for (i = 0; i < size;) {\r
+    ch_UCS4 ch = s[i++];\r
+    p = Buff;\r
+\r
+    if (ch < 0x80) {\r
+      /* Encode ASCII -- One Byte */\r
+      *p++ = (char) ch;\r
+    }\r
+    else if (ch < 0x0800) {\r
+      /* Encode Latin-1 -- Two Byte */\r
+      *p++ = (char)(0xc0 | (ch >> 6));\r
+      *p++ = (char)(0x80 | (ch & 0x3f));\r
+    }\r
+    else {\r
+      /* Encode UCS2 Unicode ordinals -- Three Byte */\r
+      /* Special case: check for high surrogate -- Shouldn't happen in UEFI */\r
+      if (0xD800 <= ch && ch <= 0xDBFF && i < size) {\r
+        ch_UCS4 ch2 = s[i];\r
+        /* Check for low surrogate and combine the two to\r
+           form a UCS4 value */\r
+        if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {\r
+          ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000;\r
+          i++;\r
+          /* Encode UCS4 Unicode ordinals -- Four Byte */\r
+          *p++ = (char)(0xf0 | (ch >> 18));\r
+          *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));\r
+          *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));\r
+          *p++ = (char)(0x80 | (ch & 0x3f));\r
+          continue;\r
+        }\r
+        /* Fall through: handles isolated high surrogates */\r
+      }\r
+      *p++ = (char)(0xe0 | (ch >> 12));\r
+      *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));\r
+      *p++ = (char)(0x80 | (ch & 0x3f));\r
+    }\r
+    /*  At this point, Buff holds the converted character which is NumInBuff bytes long.\r
+        NumInBuff is the value 1, 2, 3, or 4\r
+    */\r
+    NumInBuff = (int)(p - Buff);     // Number of bytes in Buff\r
+    if(Dest != NULL) {        // Save character if Dest is not NULL\r
+      memcpy(v, Buff, NumInBuff);\r
+      v += NumInBuff;\r
+    }\r
+    nneeded += NumInBuff;     // Keep track of the number of bytes put into Dest\r
+  }\r
+  if(Dest != NULL) {\r
+    // Terminate the destination string.\r
+    *v = '\0';\r
+  }\r
+  return nneeded;             // Tell the caller\r
+}\r
+\r
+// ########################  Narrow to Wide Conversions #######################\r
+\r
+/** If ps is not a null pointer, the mbsinit function determines whether the\r
+    pointed-to mbstate_t object describes an initial conversion state.\r
+\r
+    @return     The mbsinit function returns nonzero if ps is a null pointer\r
+                or if the pointed-to object describes an initial conversion\r
+                state; otherwise, it returns zero.\r
+\r
+    Declared in: wchar.h\r
+**/\r
+int\r
+mbsinit(const mbstate_t *ps)\r
+{\r
+  if((ps == NULL) || (ps->A == 0)) {\r
+    return 1;\r
+  }\r
+  return 0;\r
+}\r
+\r
+/** The mbrlen function is equivalent to the call:<BR>\r
+@verbatim\r
+    mbrtowc(NULL, s, n, ps != NULL ? ps : &internal)\r
+@endverbatim\r
+    where internal is the mbstate_t object for the mbrlen function, except that\r
+    the expression designated by ps is evaluated only once.\r
+\r
+    @return   The mbrlen function returns a value between zero and n,\r
+              inclusive, (size_t)(-2), or (size_t)(-1).\r
+\r
+    Declared in: wchar.h\r
+**/\r
+size_t\r
+mbrlen(\r
+  const char *s,\r
+  size_t n,\r
+  mbstate_t *ps\r
+  )\r
+{\r
+  return mbrtowc(NULL, s, n, ps);\r
+}\r
+\r
+/** Determine the number of bytes comprising a multibyte character.\r
+\r
+  If S is not a null pointer, the mblen function determines the number of bytes\r
+  contained in the multibyte character pointed to by S. Except that the\r
+  conversion state of the mbtowc function is not affected, it is equivalent to\r
+    mbtowc((wchar_t *)0, S, N);\r
+\r
+  @param[in]  S   NULL to query whether multibyte characters have\r
+                  state-dependent encodings.  Otherwise, points to a\r
+                  multibyte character.\r
+  @param[in]  N   The maximum number of bytes in a multibyte character.\r
+\r
+  @return   If S is a null pointer, the mblen function returns a nonzero or\r
+            zero value, if multibyte character encodings, respectively, do\r
+            or do not have state-dependent encodings. If S is not a null\r
+            pointer, the mblen function either returns 0 (if S points to the\r
+            null character), or returns the number of bytes that are contained\r
+            in the multibyte character (if the next N or fewer bytes form a\r
+            valid multibyte character), or returns -1 (if they do not form a\r
+            valid multibyte character).\r
+\r
+    Declared in: stdlib.h\r
+**/\r
+int\r
+mblen(\r
+  const char *s,\r
+  size_t n\r
+  )\r
+{\r
+  return (int)mbrlen(s, n, NULL);\r
+}\r
+\r
+/**\r
+If S is a null pointer, the mbrtowc function is equivalent to the call:<BR>\r
+@verbatim\r
+        mbrtowc(NULL, "", 1, ps)\r
+@endverbatim\r
+\r
+In this case, the values of the parameters pwc and n are ignored.\r
+\r
+If S is not a null pointer, the mbrtowc function inspects at most n bytes beginning with\r
+the byte pointed to by S to determine the number of bytes needed to complete the next\r
+multibyte character (including any shift sequences). If the function determines that the\r
+next multibyte character is complete and valid, it determines the value of the\r
+corresponding wide character and then, if pwc is not a null pointer, stores that value in\r
+the object pointed to by pwc. If the corresponding wide character is the null wide\r
+character, the resulting state described is the initial conversion state.\r
+\r
+    @retval   0             if the next n or fewer bytes complete the multibyte\r
+                            character that corresponds to the null wide\r
+                            character (which is the value stored).\r
+    @retval   between_1_and_n_inclusive   if the next n or fewer bytes complete\r
+                            a valid multibyte character (which is the value\r
+                            stored); the value returned is the number of bytes\r
+                            that complete the multibyte character.\r
+    @retval   (size_t)(-2)  if the next n bytes contribute to an incomplete\r
+                            (but potentially valid) multibyte character, and\r
+                            all n bytes have been processed (no value is stored).\r
+    @retval   (size_t)(-1)  if an encoding error occurs, in which case the next\r
+                            n or fewer bytes do not contribute to a complete and\r
+                            valid multibyte character (no value is stored); the\r
+                            value of the macro EILSEQ is stored in errno, and\r
+                            the conversion state is unspecified.\r
+\r
+    Declared in: wchar.h\r
+**/\r
+size_t\r
+mbrtowc(\r
+  wchar_t *pwc,\r
+  const char *s,\r
+  size_t n,\r
+  mbstate_t *ps\r
+  )\r
+{\r
+  int     RetVal;\r
+\r
+  RetVal = DecodeOneStateful(pwc, s, (ssize_t)n, ps);\r
+  return (size_t)RetVal;\r
+}\r
+\r
+/** Convert a multibyte character into a wide character.\r
+\r
+    If S is not a null pointer, the mbtowc function inspects at most N bytes\r
+    beginning with the byte pointed to by S to determine the number of bytes\r
+    needed to complete the next multibyte character (including any shift\r
+    sequences). If the function determines that the next multibyte character\r
+    is complete and valid, it determines the value of the corresponding wide\r
+    character and then, if Pwc is not a null pointer, stores that value in\r
+    the object pointed to by Pwc. If the corresponding wide character is the\r
+    null wide character, the function is left in the initial conversion state.\r
+\r
+    @param[out]   Pwc Pointer to a wide-character object to receive the converted character.\r
+    @param[in]    S   Pointer to a multibyte character to convert.\r
+    @param[in]    N   Maximum number of bytes in a multibyte character.\r
+\r
+    @return   If S is a null pointer, the mbtowc function returns a nonzero or\r
+              zero value, if multibyte character encodings, respectively, do\r
+              or do not have state-dependent encodings. If S is not a null\r
+              pointer, the mbtowc function either returns 0 (if S points to\r
+              the null character), or returns the number of bytes that are\r
+              contained in the converted multibyte character (if the next N or\r
+              fewer bytes form a valid multibyte character), or returns -1\r
+              (if they do not form a valid multibyte character).\r
+\r
+              In no case will the value returned be greater than N or the value\r
+              of the MB_CUR_MAX macro.\r
+\r
+    Declared in: stdlib.h\r
+**/\r
+int\r
+mbtowc(\r
+  wchar_t *pwc,\r
+  const char *s,\r
+  size_t n\r
+  )\r
+{\r
+  return (int)mbrtowc(pwc, s, n, NULL);\r
+}\r
+\r
+/**\r
+The mbsrtowcs function converts a sequence of multibyte characters that begins in the\r
+conversion state described by the object pointed to by ps, from the array indirectly\r
+pointed to by src into a sequence of corresponding wide characters. If dst is not a null\r
+pointer, the converted characters are stored into the array pointed to by dst. Conversion\r
+continues up to and including a terminating null character, which is also stored.\r
+Conversion stops earlier in two cases: when a sequence of bytes is encountered that does\r
+not form a valid multibyte character, or (if dst is not a null pointer) when len wide\r
+characters have been stored into the array pointed to by dst. Each conversion takes\r
+place as if by a call to the mbrtowc function.\r
+\r
+If dst is not a null pointer, the pointer object pointed to by src is assigned either a null\r
+pointer (if conversion stopped due to reaching a terminating null character) or the address\r
+just past the last multibyte character converted (if any). If conversion stopped due to\r
+reaching a terminating null character and if dst is not a null pointer, the resulting state\r
+described is the initial conversion state.\r
+\r
+    @return   If the input conversion encounters a sequence of bytes that do\r
+              not form a valid multibyte character, an encoding error occurs:\r
+              the mbsrtowcs function stores the value of the macro EILSEQ in\r
+              errno and returns (size_t)(-1); the conversion state is\r
+              unspecified. Otherwise, it returns the number of multibyte\r
+              characters successfully converted, not including the terminating\r
+              null character (if any).\r
+\r
+    Declared in: wchar.h\r
+**/\r
+size_t\r
+mbsrtowcs(\r
+  wchar_t      *dst,\r
+  const char  **src,\r
+  size_t        len,\r
+  mbstate_t    *ps\r
+  )\r
+{\r
+  int           x;\r
+  size_t        RetVal = 0;\r
+  const char   *MySrc;\r
+\r
+  if((src == NULL) || (*src == NULL) || (**src == '\0')) {\r
+    return 0;\r
+  }\r
+\r
+  MySrc = *src;\r
+  for(x = 1 ; (len != 0) && (x > 0); --len) {\r
+    x = DecodeOneStateful(dst, MySrc, MB_LEN_MAX, ps);\r
+    switch(x) {\r
+      case -2:    // Incomplete character\r
+      case -1:    // Encoding error\r
+        RetVal = (size_t)x;\r
+        break;\r
+      case 0:     // Encountered NUL character: done.\r
+        if(dst != NULL) {\r
+          *dst = 0;\r
+          *src = NULL;\r
+        }\r
+        break;\r
+      default:    // Successfully decoded a character, continue with next\r
+        MySrc += x;\r
+        if(dst != NULL) {\r
+          ++dst;\r
+          if(x == 4) {\r
+            ++dst;\r
+          }\r
+          *src = MySrc;\r
+        }\r
+        ++RetVal;\r
+        break;\r
+    }\r
+  }\r
+  return RetVal;\r
+}\r
+\r
+/** Convert a multibyte character string into a wide-character string.\r
+\r
+    The mbstowcs function converts a sequence of multibyte characters that\r
+    begins in the initial shift state from the array pointed to by Src into\r
+    a sequence of corresponding wide characters and stores not more than limit\r
+    wide characters into the array pointed to by Dest.  No multibyte\r
+    characters that follow a null character (which is converted into a null\r
+    wide character) will be examined or converted. Each multibyte character\r
+    is converted as if by a call to the mbtowc function, except that the\r
+    conversion state of the mbtowc function is not affected.\r
+\r
+    No more than Limit elements will be modified in the array pointed to by Dest.\r
+    If copying takes place between objects that overlap,\r
+    the behavior is undefined.\r
+\r
+    @param[out]   Dest    Pointer to the array to receive the converted string.\r
+    @param[in]    Src     Pointer to the string to be converted.\r
+    @param[in]    Limit   Maximum number of elements to be written to Dest.\r
+\r
+    @return   If an invalid multibyte character is encountered, the mbstowcs\r
+              function returns (size_t)(-1). Otherwise, the mbstowcs function\r
+              returns the number of array elements modified, not including a\r
+              terminating null wide character, if any.\r
+\r
+    Declared in: stdlib.h\r
+**/\r
+size_t\r
+mbstowcs(\r
+  wchar_t *pwcs,\r
+  const char *s,\r
+  size_t n\r
+  )\r
+{\r
+\r
+  /* pwcs may be NULL */\r
+  /* s may be NULL */\r
+\r
+  return mbsrtowcs(pwcs, &s, n, NULL);\r
+}\r
+\r
+/** The btowc function determines whether C constitutes a valid single-byte\r
+    character in the initial shift state.\r
+\r
+    @return   The btowc function returns WEOF if c has the value EOF or if\r
+              (unsigned char)C does not constitute a valid single-byte\r
+              character in the initial shift state. Otherwise, it returns the\r
+              wide character representation of that character.\r
+\r
+    Declared in: wchar.h\r
+**/\r
+wint_t\r
+btowc(int c)\r
+{\r
+  int       x;\r
+  wchar_t   Dest;\r
+  wint_t    RetVal = WEOF;\r
+\r
+  if (c == EOF)\r
+    return WEOF;\r
+  x = DecodeOneStateful(&Dest, (const char *)&c, 1, NULL);\r
+  if(x == 0) {\r
+    RetVal = 0;\r
+  }\r
+  else if(x == 1) {\r
+    RetVal = (wint_t)Dest;\r
+  }\r
+  return RetVal;\r
+}\r
+\r
+// ########################  Wide to Narrow Conversions #######################\r
+\r
+/**\r
+If S is a null pointer, the wcrtomb function is equivalent to the call:<BR>\r
+@verbatim\r
+        wcrtomb(buf, L'\0', ps)\r
+@endverbatim\r
+where buf is an internal buffer.\r
+\r
+If S is not a null pointer, the wcrtomb function determines the number of bytes needed\r
+to represent the multibyte character that corresponds to the wide character given by wc\r
+(including any shift sequences), and stores the multibyte character representation in the\r
+array whose first element is pointed to by S. At most MB_CUR_MAX bytes are stored. If\r
+wc is a null wide character, a null byte is stored, preceded by any shift sequence needed\r
+to restore the initial shift state; the resulting state described is the initial conversion state.\r
+\r
+    @return   The wcrtomb function returns the number of bytes stored in the\r
+              array object (including any shift sequences). When wc is not a\r
+              valid wide character, an encoding error occurs: the function\r
+              stores the value of the macro EILSEQ in errno and\r
+              returns (size_t)(-1); the conversion state is unspecified.\r
+\r
+    Declared in: wchar.h\r
+**/\r
+size_t\r
+wcrtomb(\r
+  char *s,\r
+  wchar_t wchar,\r
+  mbstate_t *ps\r
+  )\r
+{\r
+  size_t    RetVal;\r
+\r
+  /* s may be NULL */\r
+  if (s == NULL) {\r
+    RetVal = 1;\r
+  }\r
+  else {\r
+    if (wchar == L'\0') {\r
+      *s = '\0';\r
+      RetVal = 1;\r
+    }\r
+    else {\r
+      RetVal = EncodeUtf8(s, &wchar, 1);\r
+    }\r
+  }\r
+  return RetVal;\r
+}\r
+\r
+/** Convert a wide character into a multibyte character.\r
+\r
+    The wctomb function determines the number of bytes needed to represent the\r
+    multibyte character corresponding to the wide character given by WC\r
+    (including any shift sequences), and stores the multibyte character\r
+    representation in the array whose first element is pointed to by S (if S is\r
+    not a null pointer). At most MB_CUR_MAX characters are stored. If WC is a\r
+    null wide character, a null byte is stored, preceded by any shift sequence\r
+    needed to restore the initial shift state, and the function is left in the\r
+    initial conversion state.\r
+\r
+    @param[out]   S   Pointer to the object to receive the converted multibyte character.\r
+    @param[in]    WC  Wide character to be converted.\r
+\r
+    @return   If S is a null pointer, the wctomb function returns a nonzero or\r
+              zero value, if multibyte character encodings, respectively, do or\r
+              do not have state-dependent encodings. If S is not a null pointer,\r
+              the wctomb function returns -1 if the value of WC does not\r
+              correspond to a valid multibyte character, or returns the number\r
+              of bytes that are contained in the multibyte character\r
+              corresponding to the value of WC.\r
+\r
+              In no case will the value returned be greater than the value of\r
+              the MB_CUR_MAX macro.\r
+\r
+    Declared in: stdlib.h\r
+**/\r
+int\r
+wctomb(\r
+  char *s,\r
+  wchar_t wchar\r
+  )\r
+{\r
+  /*\r
+    If s is NULL just return whether MB Characters have state\r
+    dependent encodings -- they don't.\r
+  */\r
+  if (s == NULL)\r
+    return 0;\r
+\r
+  return (int)wcrtomb(s, wchar, NULL);\r
+}\r
+\r
+/** The wcsrtombs function converts a sequence of wide characters from the array\r
+    indirectly pointed to by S into a sequence of corresponding multibyte\r
+    characters that begins in the conversion state described by the object\r
+    pointed to by ps.\r
+\r
+    If S is not a null pointer, the converted characters\r
+    are then stored into the array pointed to by S.  Conversion continues\r
+    up to and including a terminating null wide character, which is also\r
+    stored. Conversion stops earlier in two cases: when a wide character is\r
+    reached that does not correspond to a valid multibyte character, or\r
+    (if S is not a null pointer) when the next multibyte character would\r
+    exceed the limit of N total bytes to be stored into the array pointed\r
+    to by S. Each conversion takes place as if by a call to the wcrtomb\r
+    function.)\r
+\r
+    If S is not a null pointer, the pointer object pointed to by pwcs is\r
+    assigned either a null pointer (if conversion stopped due to reaching\r
+    a terminating null wide character) or the address just past the last wide\r
+    character converted (if any). If conversion stopped due to reaching a\r
+    terminating null wide character, the resulting state described is the\r
+    initial conversion state.\r
+\r
+    @return     If conversion stops because a wide character is reached that\r
+                does not correspond to a valid multibyte character, an\r
+                encoding error occurs: the wcsrtombs function stores the\r
+                value of the macro EILSEQ in errno and returns (size_t)(-1);\r
+                the conversion state is unspecified. Otherwise, it returns\r
+                the number of bytes in the resulting multibyte character\r
+                sequence, not including the terminating null character (if any).\r
+\r
+    Declared in: wchar.h\r
+**/\r
+size_t\r
+wcsrtombs(\r
+  char *s,\r
+  const wchar_t **pwcs,\r
+  size_t n,\r
+  mbstate_t *ps\r
+)\r
+{\r
+  int count = 0;\r
+\r
+  /* s may be NULL */\r
+  /* pwcs may be NULL */\r
+  /* ps appears to be unused */\r
+\r
+  if (pwcs == NULL || *pwcs == NULL)\r
+    return (0);\r
+\r
+  if (s == NULL) {\r
+    while (*(*pwcs)++ != 0)\r
+      count++;\r
+    return(count);\r
+  }\r
+\r
+  if (n != 0) {\r
+    do {\r
+      if ((*s++ = (char) *(*pwcs)++) == 0) {\r
+        *pwcs = NULL;\r
+        break;\r
+      }\r
+      count++;\r
+    } while (--n != 0);\r
+  }\r
+\r
+  return count;\r
+}\r
+\r
+/** Convert a wide-character string into a multibyte character string.\r
+\r
+    The wcstombs function converts a sequence of wide characters from the\r
+    array pointed to by Src into a sequence of corresponding multibyte\r
+    characters that begins in the initial shift state, and stores these\r
+    multibyte characters into the array pointed to by Dest, stopping if a\r
+    multibyte character would exceed the limit of Limit total bytes or if a\r
+    null character is stored. Each wide character is converted as if by\r
+    a call to the wctomb function, except that the conversion state of\r
+    the wctomb function is not affected.\r
+\r
+    No more than Limit bytes will be modified in the array pointed to by Dest.\r
+    If copying takes place between objects that overlap,\r
+    the behavior is undefined.\r
+\r
+    @param[out]   Dest    Pointer to the array to receive the converted string.\r
+    @param[in]    Src     Pointer to the string to be converted.\r
+    @param[in]    Limit   Maximum number of elements to be written to Dest.\r
+\r
+    @return   If a wide character is encountered that does not correspond to a\r
+              valid multibyte character, the wcstombs function returns\r
+              (size_t)(-1). Otherwise, the wcstombs function returns the number\r
+              of bytes modified, not including a terminating null character,\r
+              if any.\r
+\r
+    Declared in: stdlib.h\r
+**/\r
+size_t\r
+wcstombs(\r
+  char *s,\r
+  const wchar_t *pwcs,\r
+  size_t n\r
+)\r
+{\r
+  /* s may be NULL */\r
+  return wcsrtombs(s, &pwcs, n, NULL);\r
+}\r
+\r
+/** The wctob function determines whether C corresponds to a member of the extended\r
+    character set whose multibyte character representation is a single byte when in the initial\r
+    shift state.\r
+\r
+    @return     The wctob function returns EOF if C does not correspond to a multibyte\r
+                character with length one in the initial shift state. Otherwise, it\r
+                returns the single-byte representation of that character as an\r
+                unsigned char converted to an int.\r
+\r
+    Declared in: wchar.h\r
+**/\r
+int\r
+wctob(wint_t c)\r
+{\r
+  /*  wctob needs to be consistent with wcrtomb.\r
+      if wcrtomb says that a character is representable in 1 byte,\r
+      which this implementation always says, then wctob needs to\r
+      also represent the character as 1 byte.\r
+  */\r
+  if (c == WEOF) {\r
+    return EOF;\r
+  }\r
+  return (int)(c & 0xFF);\r
+}\r
index 8bbd407362f3346801047380132b9cbdaa074567..76ed839203a02f234acce350f8674f4fa5f9cdd9 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-    Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+    Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
     This program and the accompanying materials are licensed and made available\r
     under the terms and conditions of the BSD License that accompanies this\r
     distribution.  The full text of the license may be found at\r
@@ -81,7 +81,6 @@ __fputwc_unlock(wchar_t wc, FILE *fp)
 \r
   size = wcrtomb(buf, wc, st);\r
   if (size == (size_t)-1) {\r
-    errno = EILSEQ;\r
     return WEOF;\r
   }\r
 \r
index 1b5991489cf513f3646dd41deca7e11adb33d5cb..684f0eebc1c9ea43e9e78bbf7713cef03e5561dd 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
     Implementation of internal file buffer allocation functions.\r
 \r
-    Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+    Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
     This program and the accompanying materials are licensed and made available\r
     under the terms and conditions of the BSD License that accompanies this\r
     distribution.  The full text of the license may be found at\r
@@ -77,11 +77,12 @@ __smakebuf(FILE *fp)
   if (fp != NULL) {\r
   if (fp->_flags & __SNBF) {\r
     fp->_bf._base = fp->_p = fp->_nbuf;\r
-    fp->_bf._size = 1;\r
+    fp->_bf._size = MB_LEN_MAX;\r
     return;\r
   }\r
   flags = __swhatbuf(fp, &size, &couldbetty);\r
   if ((p = malloc(size)) == NULL) {\r
+    // malloc failed, act unbuffered.\r
     fp->_flags |= __SNBF;\r
     fp->_bf._base = fp->_p = fp->_nbuf;\r
     fp->_bf._size = 1;\r
index e9983e993399ca9aad6b2154cad85ad4c9e5393b..655b685214507acce6d899abd3be68ae01ec929b 100644 (file)
@@ -3,7 +3,7 @@
 \r
   Manipulates abstractions for stdin, stdout, stderr.\r
 \r
-  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available under\r
   the terms and conditions of the BSD License that accompanies this distribution.\r
   The full text of the license may be found at\r
@@ -52,27 +52,45 @@ static wchar_t       *ConReadBuf;
 static BOOLEAN        TtyCooked;\r
 static BOOLEAN        TtyEcho;\r
 \r
+/** Convert string from MBCS to WCS and translate \n to \r\n.\r
+\r
+    It is the caller's responsibility to ensure that dest is\r
+    large enough to hold the converted results.  It is guaranteed\r
+    that there will be fewer than n characters placed in dest.\r
+\r
+    @param  dest    WCS buffer to receive the converted string.\r
+    @param  buf     MBCS string to convert to WCS.\r
+    @param  n       Number of BYTES contained in buf.\r
+    @param  Cs      Pointer to the character state object for this stream\r
+\r
+    @return   The number of BYTES consumed from buf.\r
+**/\r
 ssize_t\r
-WideTtyCvt( CHAR16 *dest, const char *buf, size_t n)\r
+WideTtyCvt( CHAR16 *dest, const char *buf, ssize_t n, mbstate_t *Cs)\r
 {\r
-  UINTN   i;\r
-  wint_t  wc;\r
+  ssize_t i     = 0;\r
+  int     numB  = 0;\r
+  wchar_t wc[2];\r
 \r
-  for(i = 0; i < n; ++i) {\r
-    wc = btowc(*buf++);\r
-    if( wc == 0) {\r
+  while(n > 0) {\r
+    numB = (int)mbrtowc(wc, buf, MIN(MB_LEN_MAX,n), Cs);\r
+    if( numB == 0) {\r
       break;\r
     };\r
-    if(wc < 0) {\r
-      wc = BLOCKELEMENT_LIGHT_SHADE;\r
+    if(numB < 0) {\r
+      wc[0] = BLOCKELEMENT_LIGHT_SHADE;\r
     }\r
-    if(wc == L'\n') {\r
+    if(wc[0] == L'\n') {\r
       *dest++ = L'\r';\r
+      ++i;\r
     }\r
-    *dest++ = (CHAR16)wc;\r
+    *dest++ = (CHAR16)wc[0];\r
+    i += numB;\r
+    n -= numB;\r
+    buf += numB;\r
   }\r
   *dest = 0;\r
-  return (ssize_t)i;\r
+  return i;\r
 }\r
 \r
 static\r
@@ -105,7 +123,7 @@ da_ConSeek(
 {\r
   ConInstance                       *Stream;\r
   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL   *Proto;\r
-  XYoffset                           CursorPos;\r
+  XY_OFFSET                          CursorPos;\r
 \r
   Stream = BASE_CR(filp->f_ops, ConInstance, Abstraction);\r
   // Quick check to see if Stream looks reasonable\r
@@ -140,7 +158,7 @@ da_ConSeek(
                               the string couldn't be displayed.\r
   @param[in]      Buffer      The WCS string to be displayed\r
 \r
-  @return   The number of characters written.\r
+  @return   The number of BYTES written.  Because of MBCS, this may be more than number of characters.\r
 */\r
 static\r
 ssize_t\r
@@ -155,8 +173,7 @@ da_ConWrite(
   EFI_STATUS                          Status;\r
   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL    *Proto;\r
   ConInstance                        *Stream;\r
-  ssize_t                             NumChar;\r
-  //XYoffset                            CursorPos;\r
+  ssize_t                             NumBytes;\r
 \r
   Stream = BASE_CR(filp->f_ops, ConInstance, Abstraction);\r
   // Quick check to see if Stream looks reasonable\r
@@ -173,34 +190,21 @@ da_ConWrite(
   Proto = (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)Stream->Dev;\r
 \r
   // Convert string from MBCS to WCS and translate \n to \r\n.\r
-  NumChar = WideTtyCvt(gMD->UString, (const char *)Buffer, BufferSize);\r
-  //if(NumChar > 0) {\r
-  //  BufferSize = (size_t)(NumChar * sizeof(CHAR16));\r
-  //}\r
-  BufferSize = NumChar;\r
-\r
-  //if( Position != NULL) {\r
-  //  CursorPos.Offset = (UINT64)*Position;\r
-\r
-  //  Status = Proto->SetCursorPosition(Proto,\r
-  //                                    (INTN)CursorPos.XYpos.Column,\r
-  //                                    (INTN)CursorPos.XYpos.Row);\r
-  //  if(RETURN_ERROR(Status)) {\r
-  //    return -1;\r
-  //  }\r
-  //}\r
+  NumBytes = WideTtyCvt(gMD->UString, (const char *)Buffer, (ssize_t)BufferSize, &Stream->CharState);\r
+  BufferSize = NumBytes;\r
+\r
 \r
   // Send the Unicode buffer to the console\r
   Status = Proto->OutputString( Proto, gMD->UString);\r
   // Depending on status, update BufferSize and return\r
   if(RETURN_ERROR(Status)) {\r
-    BufferSize = 0;    // We don't really know how many characters made it out\r
+    BufferSize = 0;     // We don't really know how many characters made it out\r
   }\r
   else {\r
-    //BufferSize = NumChar;\r
-    Stream->NumWritten += NumChar;\r
+    //BufferSize = NumBytes;\r
+    Stream->NumWritten += NumBytes;\r
   }\r
-  EFIerrno = Status;\r
+  EFIerrno = Status;      // Make error reason available to caller\r
   return BufferSize;\r
 }\r
 \r
@@ -342,7 +346,8 @@ da_ConStat(
     return -1;\r
   }\r
   // All of our parameters are correct, so fill in the information.\r
-  Buffer->st_blksize = 1;\r
+  Buffer->st_blksize  = 0;   // Character device, not a block device\r
+  Buffer->st_mode     = filp->f_iflags;\r
 \r
 // ConGetPosition\r
   if(Stream->InstanceNum == STDIN_FILENO) {\r
@@ -504,6 +509,7 @@ __Cons_construct(
 \r
     Stream->Cookie      = CON_COOKIE;\r
     Stream->InstanceNum = i;\r
+    Stream->CharState.A = 0;    // Start in the initial state\r
 \r
     switch(i) {\r
       case STDIN_FILENO:\r
index ebae38f3fbfa5b184bb27926cfd8d2e1e5ab584b..5576938f8effe1c5f292a5cd1a4b87f88831aecf 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   EFI versions of NetBSD system calls.\r
 \r
-  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available under\r
   the terms and conditions of the BSD License that accompanies this distribution.\r
   The full text of the license may be found at\r
@@ -557,16 +557,38 @@ mkdir (const char *path, __mode_t perms)
 }\r
 \r
 /** Open a file.\r
+    The open() function establishes the connection between a file and a file\r
+    descriptor.  It creates an open file description that refers to a file\r
+    and a file descriptor that refers to that open file description. The file\r
+    descriptor is used by other I/O functions to refer to that file.\r
+\r
+    The open() function returns a file descriptor for the named file that is\r
+    the lowest file descriptor not currently open for that process. The open\r
+    file description is new, and therefore the file descriptor shall not\r
+    share it with any other process in the system.\r
+\r
+    The file offset used to mark the current position within the file is set\r
+    to the beginning of the file.\r
 \r
     The EFI ShellOpenFileByName() function is used to perform the low-level\r
     file open operation.  The primary task of open() is to translate from the\r
     flags used in the <stdio.h> environment to those used by the EFI function.\r
 \r
+    The file status flags and file access modes of the open file description\r
+    are set according to the value of oflags.\r
+\r
+    Values for oflags are constructed by a bitwise-inclusive OR of flags from\r
+    the following list, defined in <fcntl.h>. Applications shall specify\r
+    exactly one of { O_RDONLY, O_RDWR, O_WRONLY } in the value of oflags.\r
+    Any combination of { O_NONBLOCK, O_APPEND, O_CREAT, O_TRUNC, O_EXCL } may\r
+    also be specified in oflags.\r
+\r
     The only valid flag combinations for ShellOpenFileByName() are:\r
       - Read\r
       - Read/Write\r
       - Create/Read/Write\r
 \r
+    Values for mode specify the access permissions for newly created files.\r
     The mode value is saved in the FD to indicate permissions for further operations.\r
 \r
     O_RDONLY      -- flags = EFI_FILE_MODE_READ -- this is always done\r
@@ -578,6 +600,25 @@ mkdir (const char *path, __mode_t perms)
     O_CREAT       -- flags |= EFI_FILE_MODE_CREATE\r
     O_TRUNC       -- delete first then create new\r
     O_EXCL        -- if O_CREAT is also set, open will fail if the file already exists.\r
+\r
+    @param[in]    Path      The path argument points to a pathname naming the\r
+                            object to be opened.\r
+    @param[in]    oflags    File status flags and file access modes of the\r
+                            open file description.\r
+    @param[in]    mode      File access permission bits as defined in\r
+                            <sys/stat.h>.\r
+\r
+    @return     Upon successful completion, open() opens the file and returns\r
+                a non-negative integer representing the lowest numbered\r
+                unused file descriptor. Otherwise, open returns -1 and sets\r
+                errno to indicate the error. If a negative value is\r
+                returned, no files are created or modified.\r
+\r
+    @retval   EMFILE      No file descriptors available -- Max number already open.\r
+    @retval   EINVAL      Bad value specified for oflags or mode.\r
+    @retval   ENOMEM      Failure allocating memory for internal buffers.\r
+    @retval   EEXIST      File exists and open attempted with (O_EXCL | O_CREAT) set.\r
+    @retval   EIO         UEFI failure.  Check value in EFIerrno.\r
 **/\r
 int\r
 open(\r
index 013977c52a9416b5076910f4826a0526eefb3d22..3dc7e9e38f4a467df86232fc73bdb0f71e3fad83 100644 (file)
   UseSocketDxe|StdLib/UseSocketDxe/UseSocketDxe.inf\r
 \r
 [LibraryClasses.Common.UEFI_APPLICATION]\r
+  ShellLib|ShellPkg/Library/UefiShellLib/UefiShellLib.inf\r
+  FileHandleLib|ShellPkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf\r
+  SortLib|ShellPkg/Library/UefiSortLib/UefiSortLib.inf\r
+  PathLib|ShellPkg/Library/BasePathLib/BasePathLib.inf\r
+  ShellCEntryLib|ShellPkg/Library/UefiShellCEntryLib/UefiShellCEntryLib.inf\r
+\r
   #\r
   # C Standard Libraries\r
   #\r
index e97ee3fd2c693a313b17e75fb3a571ed4af92fdb..9b4eb52d76e17a80f2aeb763896f75ad4fcee635 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Declarations and macros for the console abstraction.\r
 \r
-  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
   distribution.  The full text of the license may be found at\r
 #include  <kfile.h>\r
 #include  <Device/Device.h>\r
 \r
-typedef struct {\r
-  UINT32    Column;\r
-  UINT32    Row;\r
-} CursorXY;\r
-\r
-typedef union {\r
-  UINT64      Offset;\r
-  CursorXY    XYpos;\r
-} XYoffset;\r
-\r
 /*  The members Cookie through Abstraction, inclusive, are the same type and order\r
     for all instance structures.\r
 \r
@@ -43,7 +33,7 @@ typedef struct {
   UINT64                      NumRead;      ///< Number of characters Read.\r
   UINT64                      NumWritten;   ///< Number of characters Written.\r
   EFI_INPUT_KEY               UnGetKey;     ///< One-key pushback, for poll().\r
-  UINT32                      Reserved_2;   // Force the struct to be a multiple of 8-bytes long\r
+  __mbstate_t                 CharState;    ///< Character state for the byte stream passing through this device\r
 } ConInstance;\r
 \r
 __BEGIN_DECLS\r