]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/Library/UefiFileHandleLib: Make FileHandleReadLine return the right buffer...
authorQiu Shumin <shumin.qiu@intel.com>
Mon, 24 Aug 2015 07:42:27 +0000 (07:42 +0000)
committershenshushi <shenshushi@Edk2>
Mon, 24 Aug 2015 07:42:27 +0000 (07:42 +0000)
1. '\r' char will not return in buffer so buffer size should exclude the number of '\r' char.
2. When 'Truncate' is TRUE return the truncated string with 'EFI_SUCCESS' status.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Qiu Shumin <shumin.qiu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18276 6f19259b-4bc3-4df7-8a09-765794883524

MdePkg/Include/Library/FileHandleLib.h
MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c

index b5ac19ac85de4506a010d659b42889074aee524a..fdbdc3ade476f33485580e44dad673aeb17b80ac 100644 (file)
@@ -379,6 +379,8 @@ FileHandleGetFileName (
 \r
   If the position upon start is 0, then the Ascii Boolean will be set.  This should be\r
   maintained and not changed for all operations with the same file.\r
+  The function will not return the \r and \n character in buffer. When an empty line is\r
+  read a CHAR_NULL character will be returned in buffer.\r
 \r
   @param[in]       Handle        FileHandle to read from.\r
   @param[in, out]  Buffer        The pointer to buffer to read into.\r
index f6cbfada7df513dfbdd57f34eaf307d6582cf626..a31d12b862d760cad6bd44f3e96c36dbb134e344 100644 (file)
@@ -913,6 +913,8 @@ FileHandleReturnLine(
 \r
   If the position upon start is 0, then the Ascii Boolean will be set.  This should be\r
   maintained and not changed for all operations with the same file.\r
+  The function will not return the \r and \n character in buffer. When an empty line is\r
+  read a CHAR_NULL character will be returned in buffer.\r
 \r
   @param[in]       Handle        FileHandle to read from.\r
   @param[in, out]  Buffer        The pointer to buffer to read into.\r
@@ -949,6 +951,7 @@ FileHandleReadLine(
   UINT64      FileSize;\r
   UINTN       CharSize;\r
   UINTN       CountSoFar;\r
+  UINTN       CrCount;\r
   UINT64      OriginalFilePosition;\r
 \r
   if (Handle == NULL\r
@@ -958,14 +961,15 @@ FileHandleReadLine(
     return (EFI_INVALID_PARAMETER);\r
   } \r
   \r
-  if (Buffer != NULL) {\r
+  if (Buffer != NULL && *Size != 0) {\r
     *Buffer = CHAR_NULL;\r
-  }\r
+  } \r
   \r
   Status = FileHandleGetSize (Handle, &FileSize);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   } else if (FileSize == 0) {\r
+    *Ascii = TRUE;\r
     return EFI_SUCCESS;\r
   }  \r
   \r
@@ -982,6 +986,7 @@ FileHandleReadLine(
     }\r
   }\r
 \r
+  CrCount = 0;\r
   for (CountSoFar = 0;;CountSoFar++){\r
     CharBuffer = 0;\r
     if (*Ascii) {\r
@@ -996,31 +1001,38 @@ FileHandleReadLine(
        || (CharBuffer ==  '\n' && *Ascii)\r
      ){\r
       break;\r
+    } else if (\r
+        (CharBuffer == L'\r' && !(*Ascii)) ||\r
+        (CharBuffer ==  '\r' && *Ascii)\r
+      ) {\r
+      CrCount++;\r
+      continue;\r
     }\r
     //\r
     // if we have space save it...\r
     //\r
-    if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r
+    if ((CountSoFar+1-CrCount)*sizeof(CHAR16) < *Size){\r
       ASSERT(Buffer != NULL);\r
-      ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r
-      ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
+      ((CHAR16*)Buffer)[CountSoFar-CrCount] = CharBuffer;\r
+      ((CHAR16*)Buffer)[CountSoFar+1-CrCount] = CHAR_NULL;\r
     }\r
   }\r
 \r
   //\r
   // if we ran out of space tell when...\r
   //\r
-  if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
-    *Size = (CountSoFar+1)*sizeof(CHAR16);\r
+  if ((CountSoFar+1-CrCount)*sizeof(CHAR16) > *Size){\r
+    *Size = (CountSoFar+1-CrCount)*sizeof(CHAR16);\r
     if (!Truncate) {\r
+      if (Buffer != NULL && *Size != 0) {\r
+        ZeroMem(Buffer, *Size);\r
+      }\r
       FileHandleSetPosition(Handle, OriginalFilePosition);\r
+      return (EFI_BUFFER_TOO_SMALL);\r
     } else {\r
       DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));\r
+      return (EFI_SUCCESS);\r
     }\r
-    return (EFI_BUFFER_TOO_SMALL);\r
-  }\r
-  while(Buffer[StrLen(Buffer)-1] == L'\r') {\r
-    Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r
   }\r
 \r
   return (Status);\r