]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/SafeString.c
MdePkg/BaseLib: Add safe string functions [Ascii]StrnSizeS
[mirror_edk2.git] / MdePkg / Library / BaseLib / SafeString.c
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