public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] BaseTools/VolInfo: Fix printf issue using '%ls' in format string
@ 2016-12-01 12:39 Hao Wu
  2016-12-02  2:55 ` Gao, Liming
  0 siblings, 1 reply; 3+ messages in thread
From: Hao Wu @ 2016-12-01 12:39 UTC (permalink / raw)
  To: edk2-devel; +Cc: Hao Wu, Liming Gao, Yonghong Zhu

https://bugzilla.tianocore.org/show_bug.cgi?id=257

For GCC compilers, when building with option '-fshort-wchar', wide char
string format '%ls' does not work properly for printf() function. The
string specified by '%ls' will not be printed.

This commit avoids using '%ls' for printf() function and converts the wide
char string to char string for printing.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
---
 BaseTools/Source/C/VolInfo/VolInfo.c | 79 +++++++++++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 1 deletion(-)

diff --git a/BaseTools/Source/C/VolInfo/VolInfo.c b/BaseTools/Source/C/VolInfo/VolInfo.c
index 46c7212..7d63e59 100644
--- a/BaseTools/Source/C/VolInfo/VolInfo.c
+++ b/BaseTools/Source/C/VolInfo/VolInfo.c
@@ -148,6 +148,17 @@ Usage (
   VOID
   );
 
+UINT32
+UnicodeStrLen (
+  IN CHAR16 *String
+  );
+
+VOID
+Unicode2AsciiString (
+  IN  CHAR16 *Source,
+  OUT CHAR8  *Destination
+  );
+
 int
 main (
   int       argc,
@@ -1606,6 +1617,7 @@ Returns:
   UINT32              RealHdrLen;
   CHAR8               *ToolInputFileName;
   CHAR8               *ToolOutputFileName;
+  CHAR8               *UIFileName;
 
   ParsedLength = 0;
   ToolInputFileName = NULL;
@@ -1714,7 +1726,14 @@ Returns:
       break;
 
     case EFI_SECTION_USER_INTERFACE:
-      printf ("  String: %ls\n", (wchar_t *) &((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString);
+      UIFileName = (CHAR8 *) malloc (UnicodeStrLen (((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString) + 1);
+      if (UIFileName == NULL) {
+        Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
+        return EFI_OUT_OF_RESOURCES;
+      }
+      Unicode2AsciiString (((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString, UIFileName);
+      printf ("  String: %s\n", UIFileName);
+      free (UIFileName);
       break;
 
     case EFI_SECTION_FIRMWARE_VOLUME_IMAGE:
@@ -2360,3 +2379,61 @@ Returns:
             Reserved for future use\n");
 }
 
+UINT32
+UnicodeStrLen (
+  IN CHAR16 *String
+  )
+  /*++
+
+  Routine Description:
+
+  Returns the length of a null-terminated unicode string.
+
+  Arguments:
+
+    String - The pointer to a null-terminated unicode string.
+
+  Returns:
+
+    N/A
+
+  --*/
+{
+  UINT32  Length;
+
+  for (Length = 0; *String != L'\0'; String++, Length++) {
+    ;
+  }
+  return Length;
+}
+
+VOID
+Unicode2AsciiString (
+  IN  CHAR16 *Source,
+  OUT CHAR8  *Destination
+  )
+  /*++
+
+  Routine Description:
+
+  Convert a null-terminated unicode string to a null-terminated ascii string.
+
+  Arguments:
+
+    Source      - The pointer to the null-terminated input unicode string.
+    Destination - The pointer to the null-terminated output ascii string.
+
+  Returns:
+
+    N/A
+
+  --*/
+{
+  while (*Source != '\0') {
+    *(Destination++) = (CHAR8) *(Source++);
+  }
+  //
+  // End the ascii with a NULL.
+  //
+  *Destination = '\0';
+}
-- 
1.9.5.msysgit.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] BaseTools/VolInfo: Fix printf issue using '%ls' in format string
  2016-12-01 12:39 [PATCH] BaseTools/VolInfo: Fix printf issue using '%ls' in format string Hao Wu
@ 2016-12-02  2:55 ` Gao, Liming
  2016-12-02  3:02   ` Wu, Hao A
  0 siblings, 1 reply; 3+ messages in thread
From: Gao, Liming @ 2016-12-02  2:55 UTC (permalink / raw)
  To: Wu, Hao A, edk2-devel@lists.01.org

Hao:
  Place move  UnicodeStrLen() and Unicode2AsciiString() implementation before main() function. If so, they are not required to be declared again. 

  Other changes are good. Reviewed-by: Liming Gao <liming.gao@intel.com>

Thanks
Liming
-----Original Message-----
From: Wu, Hao A 
Sent: Thursday, December 01, 2016 8:40 PM
To: edk2-devel@lists.01.org
Cc: Wu, Hao A <hao.a.wu@intel.com>; Gao, Liming <liming.gao@intel.com>; Zhu, Yonghong <yonghong.zhu@intel.com>
Subject: [PATCH] BaseTools/VolInfo: Fix printf issue using '%ls' in format string

https://bugzilla.tianocore.org/show_bug.cgi?id=257

For GCC compilers, when building with option '-fshort-wchar', wide char string format '%ls' does not work properly for printf() function. The string specified by '%ls' will not be printed.

This commit avoids using '%ls' for printf() function and converts the wide char string to char string for printing.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
---
 BaseTools/Source/C/VolInfo/VolInfo.c | 79 +++++++++++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 1 deletion(-)

diff --git a/BaseTools/Source/C/VolInfo/VolInfo.c b/BaseTools/Source/C/VolInfo/VolInfo.c
index 46c7212..7d63e59 100644
--- a/BaseTools/Source/C/VolInfo/VolInfo.c
+++ b/BaseTools/Source/C/VolInfo/VolInfo.c
@@ -148,6 +148,17 @@ Usage (
   VOID
   );
 
+UINT32
+UnicodeStrLen (
+  IN CHAR16 *String
+  );
+
+VOID
+Unicode2AsciiString (
+  IN  CHAR16 *Source,
+  OUT CHAR8  *Destination
+  );
+
 int
 main (
   int       argc,
@@ -1606,6 +1617,7 @@ Returns:
   UINT32              RealHdrLen;
   CHAR8               *ToolInputFileName;
   CHAR8               *ToolOutputFileName;
+  CHAR8               *UIFileName;
 
   ParsedLength = 0;
   ToolInputFileName = NULL;
@@ -1714,7 +1726,14 @@ Returns:
       break;
 
     case EFI_SECTION_USER_INTERFACE:
-      printf ("  String: %ls\n", (wchar_t *) &((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString);
+      UIFileName = (CHAR8 *) malloc (UnicodeStrLen (((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString) + 1);
+      if (UIFileName == NULL) {
+        Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
+        return EFI_OUT_OF_RESOURCES;
+      }
+      Unicode2AsciiString (((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString, UIFileName);
+      printf ("  String: %s\n", UIFileName);
+      free (UIFileName);
       break;
 
     case EFI_SECTION_FIRMWARE_VOLUME_IMAGE:
@@ -2360,3 +2379,61 @@ Returns:
             Reserved for future use\n");  }
 
+UINT32
+UnicodeStrLen (
+  IN CHAR16 *String
+  )
+  /*++
+
+  Routine Description:
+
+  Returns the length of a null-terminated unicode string.
+
+  Arguments:
+
+    String - The pointer to a null-terminated unicode string.
+
+  Returns:
+
+    N/A
+
+  --*/
+{
+  UINT32  Length;
+
+  for (Length = 0; *String != L'\0'; String++, Length++) {
+    ;
+  }
+  return Length;
+}
+
+VOID
+Unicode2AsciiString (
+  IN  CHAR16 *Source,
+  OUT CHAR8  *Destination
+  )
+  /*++
+
+  Routine Description:
+
+  Convert a null-terminated unicode string to a null-terminated ascii string.
+
+  Arguments:
+
+    Source      - The pointer to the null-terminated input unicode string.
+    Destination - The pointer to the null-terminated output ascii string.
+
+  Returns:
+
+    N/A
+
+  --*/
+{
+  while (*Source != '\0') {
+    *(Destination++) = (CHAR8) *(Source++);
+  }
+  //
+  // End the ascii with a NULL.
+  //
+  *Destination = '\0';
+}
--
1.9.5.msysgit.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] BaseTools/VolInfo: Fix printf issue using '%ls' in format string
  2016-12-02  2:55 ` Gao, Liming
@ 2016-12-02  3:02   ` Wu, Hao A
  0 siblings, 0 replies; 3+ messages in thread
From: Wu, Hao A @ 2016-12-02  3:02 UTC (permalink / raw)
  To: Gao, Liming, edk2-devel@lists.01.org

Got it. I will move those 2 implementation before main() before code
check-in.

Best Regards,
Hao Wu


> -----Original Message-----
> From: Gao, Liming
> Sent: Friday, December 02, 2016 10:56 AM
> To: Wu, Hao A; edk2-devel@lists.01.org
> Cc: Zhu, Yonghong
> Subject: RE: [PATCH] BaseTools/VolInfo: Fix printf issue using '%ls' in format
> string
> 
> Hao:
>   Place move  UnicodeStrLen() and Unicode2AsciiString() implementation
> before main() function. If so, they are not required to be declared again.
> 
>   Other changes are good. Reviewed-by: Liming Gao <liming.gao@intel.com>
> 
> Thanks
> Liming
> -----Original Message-----
> From: Wu, Hao A
> Sent: Thursday, December 01, 2016 8:40 PM
> To: edk2-devel@lists.01.org
> Cc: Wu, Hao A <hao.a.wu@intel.com>; Gao, Liming <liming.gao@intel.com>;
> Zhu, Yonghong <yonghong.zhu@intel.com>
> Subject: [PATCH] BaseTools/VolInfo: Fix printf issue using '%ls' in format
> string
> 
> https://bugzilla.tianocore.org/show_bug.cgi?id=257
> 
> For GCC compilers, when building with option '-fshort-wchar', wide char
> string format '%ls' does not work properly for printf() function. The string
> specified by '%ls' will not be printed.
> 
> This commit avoids using '%ls' for printf() function and converts the wide char
> string to char string for printing.
> 
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Yonghong Zhu <yonghong.zhu@intel.com>
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Hao Wu <hao.a.wu@intel.com>
> ---
>  BaseTools/Source/C/VolInfo/VolInfo.c | 79
> +++++++++++++++++++++++++++++++++++-
>  1 file changed, 78 insertions(+), 1 deletion(-)
> 
> diff --git a/BaseTools/Source/C/VolInfo/VolInfo.c
> b/BaseTools/Source/C/VolInfo/VolInfo.c
> index 46c7212..7d63e59 100644
> --- a/BaseTools/Source/C/VolInfo/VolInfo.c
> +++ b/BaseTools/Source/C/VolInfo/VolInfo.c
> @@ -148,6 +148,17 @@ Usage (
>    VOID
>    );
> 
> +UINT32
> +UnicodeStrLen (
> +  IN CHAR16 *String
> +  );
> +
> +VOID
> +Unicode2AsciiString (
> +  IN  CHAR16 *Source,
> +  OUT CHAR8  *Destination
> +  );
> +
>  int
>  main (
>    int       argc,
> @@ -1606,6 +1617,7 @@ Returns:
>    UINT32              RealHdrLen;
>    CHAR8               *ToolInputFileName;
>    CHAR8               *ToolOutputFileName;
> +  CHAR8               *UIFileName;
> 
>    ParsedLength = 0;
>    ToolInputFileName = NULL;
> @@ -1714,7 +1726,14 @@ Returns:
>        break;
> 
>      case EFI_SECTION_USER_INTERFACE:
> -      printf ("  String: %ls\n", (wchar_t *) &((EFI_USER_INTERFACE_SECTION *)
> Ptr)->FileNameString);
> +      UIFileName = (CHAR8 *) malloc (UnicodeStrLen
> (((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString) + 1);
> +      if (UIFileName == NULL) {
> +        Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
> +        return EFI_OUT_OF_RESOURCES;
> +      }
> +      Unicode2AsciiString (((EFI_USER_INTERFACE_SECTION *) Ptr)-
> >FileNameString, UIFileName);
> +      printf ("  String: %s\n", UIFileName);
> +      free (UIFileName);
>        break;
> 
>      case EFI_SECTION_FIRMWARE_VOLUME_IMAGE:
> @@ -2360,3 +2379,61 @@ Returns:
>              Reserved for future use\n");  }
> 
> +UINT32
> +UnicodeStrLen (
> +  IN CHAR16 *String
> +  )
> +  /*++
> +
> +  Routine Description:
> +
> +  Returns the length of a null-terminated unicode string.
> +
> +  Arguments:
> +
> +    String - The pointer to a null-terminated unicode string.
> +
> +  Returns:
> +
> +    N/A
> +
> +  --*/
> +{
> +  UINT32  Length;
> +
> +  for (Length = 0; *String != L'\0'; String++, Length++) {
> +    ;
> +  }
> +  return Length;
> +}
> +
> +VOID
> +Unicode2AsciiString (
> +  IN  CHAR16 *Source,
> +  OUT CHAR8  *Destination
> +  )
> +  /*++
> +
> +  Routine Description:
> +
> +  Convert a null-terminated unicode string to a null-terminated ascii string.
> +
> +  Arguments:
> +
> +    Source      - The pointer to the null-terminated input unicode string.
> +    Destination - The pointer to the null-terminated output ascii string.
> +
> +  Returns:
> +
> +    N/A
> +
> +  --*/
> +{
> +  while (*Source != '\0') {
> +    *(Destination++) = (CHAR8) *(Source++);
> +  }
> +  //
> +  // End the ascii with a NULL.
> +  //
> +  *Destination = '\0';
> +}
> --
> 1.9.5.msysgit.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-12-02  3:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-01 12:39 [PATCH] BaseTools/VolInfo: Fix printf issue using '%ls' in format string Hao Wu
2016-12-02  2:55 ` Gao, Liming
2016-12-02  3:02   ` Wu, Hao A

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox