]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BaseLib: Add safe string functions [Ascii]StrnSizeS
authorHao Wu <hao.a.wu@intel.com>
Fri, 9 Dec 2016 07:53:06 +0000 (15:53 +0800)
committerHao Wu <hao.a.wu@intel.com>
Mon, 9 Jan 2017 05:59:01 +0000 (13:59 +0800)
Add StrnSizeS() and AsciiStrnSizeS() APIs.

These 2 safe version APIs are used to replace StrSize() and
AsciiStrSize(). Those two APIs use similar checks as [Ascii]StrnLenS().

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
MdePkg/Include/Library/BaseLib.h
MdePkg/Library/BaseLib/SafeString.c

index b69c7038e4c084e84620f70c14c717255cf298ce..72d1f0bcbede051e1f7370ade62f24da108c2627 100644 (file)
@@ -2,7 +2,7 @@
   Provides string functions, linked list functions, math functions, synchronization\r
   functions, file path functions, and CPU architecture-specific functions.\r
 \r
-Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 Portions copyright (c) 2008 - 2009, Apple Inc. 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
@@ -207,6 +207,34 @@ StrnLenS (
   IN UINTN                     MaxSize\r
   );\r
 \r
+/**\r
+  Returns the size of a Null-terminated Unicode string in bytes, including the\r
+  Null terminator.\r
+\r
+  This function returns the size of the Null-terminated Unicode string\r
+  specified by String in bytes, including the Null terminator.\r
+\r
+  If String is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  @param  String   A pointer to a Null-terminated Unicode string.\r
+  @param  MaxSize  The maximum number of Destination Unicode\r
+                   char, including the Null terminator.\r
+\r
+  @retval 0  If String is NULL.\r
+  @retval (sizeof (CHAR16) * (MaxSize + 1))\r
+             If there is no Null terminator in the first MaxSize characters of\r
+             String.\r
+  @return The size of the Null-terminated Unicode string in bytes, including\r
+          the Null terminator.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+StrnSizeS (\r
+  IN CONST CHAR16              *String,\r
+  IN UINTN                     MaxSize\r
+  );\r
+\r
 /**\r
   Copies the string pointed to by Source (including the terminating null char)\r
   to the array pointed to by Destination.\r
@@ -382,6 +410,32 @@ AsciiStrnLenS (
   IN UINTN                     MaxSize\r
   );\r
 \r
+/**\r
+  Returns the size of a Null-terminated Ascii string in bytes, including the\r
+  Null terminator.\r
+\r
+  This function returns the size of the Null-terminated Ascii string specified\r
+  by String in bytes, including the Null terminator.\r
+\r
+  @param  String   A pointer to a Null-terminated Ascii string.\r
+  @param  MaxSize  The maximum number of Destination Ascii\r
+                   char, including the Null terminator.\r
+\r
+  @retval 0  If String is NULL.\r
+  @retval (sizeof (CHAR8) * (MaxSize + 1))\r
+             If there is no Null terminator in the first MaxSize characters of\r
+             String.\r
+  @return The size of the Null-terminated Ascii string in bytes, including the\r
+          Null terminator.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+AsciiStrnSizeS (\r
+  IN CONST CHAR8               *String,\r
+  IN UINTN                     MaxSize\r
+  );\r
+\r
 /**\r
   Copies the string pointed to by Source (including the terminating null char)\r
   to the array pointed to by Destination.\r
index e4c0759c704adae6d9e329a1c25f44df8ecf9cab..f80db9f780d204eaf19d33aecb4e5a3b80baa4db 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Safe String functions.\r
 \r
-  Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2014 - 2017, 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
@@ -153,6 +153,51 @@ StrnLenS (
   return Length;\r
 }\r
 \r
+/**\r
+  Returns the size of a Null-terminated Unicode string in bytes, including the\r
+  Null terminator.\r
+\r
+  This function returns the size of the Null-terminated Unicode string\r
+  specified by String in bytes, including the Null terminator.\r
+\r
+  If String is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  @param  String   A pointer to a Null-terminated Unicode string.\r
+  @param  MaxSize  The maximum number of Destination Unicode\r
+                   char, including the Null terminator.\r
+\r
+  @retval 0  If String is NULL.\r
+  @retval (sizeof (CHAR16) * (MaxSize + 1))\r
+             If there is no Null terminator in the first MaxSize characters of\r
+             String.\r
+  @return The size of the Null-terminated Unicode string in bytes, including\r
+          the Null terminator.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+StrnSizeS (\r
+  IN CONST CHAR16              *String,\r
+  IN UINTN                     MaxSize\r
+  )\r
+{\r
+  //\r
+  // If String is a null pointer, then the StrnSizeS function returns zero.\r
+  //\r
+  if (String == NULL) {\r
+    return 0;\r
+  }\r
+\r
+  //\r
+  // Otherwise, the StrnSizeS function returns the size of the Null-terminated\r
+  // Unicode string in bytes, including the Null terminator. If there is no\r
+  // Null terminator in the first MaxSize characters of String, then StrnSizeS\r
+  // returns (sizeof (CHAR16) * (MaxSize + 1)) to keep a consistent map with\r
+  // the StrnLenS function.\r
+  //\r
+  return (StrnLenS (String, MaxSize) + 1) * sizeof (*String);\r
+}\r
+\r
 /**\r
   Copies the string pointed to by Source (including the terminating null char)\r
   to the array pointed to by Destination.\r
@@ -585,6 +630,50 @@ AsciiStrnLenS (
   return Length;\r
 }\r
 \r
+/**\r
+  Returns the size of a Null-terminated Ascii string in bytes, including the\r
+  Null terminator.\r
+\r
+  This function returns the size of the Null-terminated Ascii string specified\r
+  by String in bytes, including the Null terminator.\r
+\r
+  @param  String   A pointer to a Null-terminated Ascii string.\r
+  @param  MaxSize  The maximum number of Destination Ascii\r
+                   char, including the Null terminator.\r
+\r
+  @retval 0  If String is NULL.\r
+  @retval (sizeof (CHAR8) * (MaxSize + 1))\r
+             If there is no Null terminator in the first MaxSize characters of\r
+             String.\r
+  @return The size of the Null-terminated Ascii string in bytes, including the\r
+          Null terminator.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+AsciiStrnSizeS (\r
+  IN CONST CHAR8               *String,\r
+  IN UINTN                     MaxSize\r
+  )\r
+{\r
+  //\r
+  // If String is a null pointer, then the AsciiStrnSizeS function returns\r
+  // zero.\r
+  //\r
+  if (String == NULL) {\r
+    return 0;\r
+  }\r
+\r
+  //\r
+  // Otherwise, the AsciiStrnSizeS function returns the size of the\r
+  // Null-terminated Ascii string in bytes, including the Null terminator. If\r
+  // there is no Null terminator in the first MaxSize characters of String,\r
+  // then AsciiStrnSizeS returns (sizeof (CHAR8) * (MaxSize + 1)) to keep a\r
+  // consistent map with the AsciiStrnLenS function.\r
+  //\r
+  return (AsciiStrnLenS (String, MaxSize) + 1) * sizeof (*String);\r
+}\r
+\r
 /**\r
   Copies the string pointed to by Source (including the terminating null char)\r
   to the array pointed to by Destination.\r