Discussion:
[edk2] [PATCH] MdePkg\Library\UefiFileHandleLib: Make FileHandleWriteLine support both ASCII and UNICODE file.
Qiu Shumin
2015-06-23 06:30:06 UTC
Permalink
When the file is a UNICODE file (with UNICODE file tag) write UNICODE text.
When the file is an ASCII file write ASCII text.
If the file size is zero (without the file tag at the beginning) write ASCII text as default.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Qiu Shumin <***@intel.com>
---
MdePkg/Include/Library/FileHandleLib.h | 10 ++-
.../Library/UefiFileHandleLib/UefiFileHandleLib.c | 97 ++++++++++++++++++++--
2 files changed, 97 insertions(+), 10 deletions(-)

diff --git a/MdePkg/Include/Library/FileHandleLib.h b/MdePkg/Include/Library/FileHandleLib.h
index bfcf8a4..b5ac19a 100644
--- a/MdePkg/Include/Library/FileHandleLib.h
+++ b/MdePkg/Include/Library/FileHandleLib.h
@@ -433,7 +433,13 @@ FileHandleReturnLine(
);

/**
- Function to write a line of unicode text to a file.
+ Function to write a line of text to a file.
+
+ If the file is a Unicode file (with UNICODE file tag) then write the unicode
+ text.
+ If the file is an ASCII file then write the ASCII text.
+ If the size of file is zero (without file tag at the beginning) then write
+ ASCII text as default.

@param[in] Handle FileHandle to write to.
@param[in] Buffer Buffer to write, if NULL the function will
@@ -442,6 +448,8 @@ FileHandleReturnLine(
@retval EFI_SUCCESS The data was written.
Buffer is NULL.
@retval EFI_INVALID_PARAMETER Handle is NULL.
+ @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
+ string due to out of resources.

@sa FileHandleWrite
**/
diff --git a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
index 96f16ca..f6cbfad 100644
--- a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
+++ b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
@@ -1027,7 +1027,13 @@ FileHandleReadLine(
}

/**
- Function to write a line of unicode text to a file.
+ Function to write a line of text to a file.
+
+ If the file is a Unicode file (with UNICODE file tag) then write the unicode
+ text.
+ If the file is an ASCII file then write the ASCII text.
+ If the size of file is zero (without file tag at the beginning) then write
+ ASCII text as default.

@param[in] Handle FileHandle to write to.
@param[in] Buffer Buffer to write, if NULL the function will
@@ -1036,6 +1042,8 @@ FileHandleReadLine(
@retval EFI_SUCCESS The data was written.
Buffer is NULL.
@retval EFI_INVALID_PARAMETER Handle is NULL.
+ @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
+ string due to out of resources.

@sa FileHandleWrite
**/
@@ -1046,8 +1054,14 @@ FileHandleWriteLine(
IN CHAR16 *Buffer
)
{
- EFI_STATUS Status;
- UINTN Size;
+ EFI_STATUS Status;
+ CHAR16 CharBuffer;
+ UINTN Size;
+ UINTN CharSize;
+ UINT64 FileSize;
+ UINT64 OriginalFilePosition;
+ BOOLEAN Ascii;
+ CHAR8 *AsciiBuffer;

if (Buffer == NULL) {
return (EFI_SUCCESS);
@@ -1056,14 +1070,79 @@ FileHandleWriteLine(
if (Handle == NULL) {
return (EFI_INVALID_PARAMETER);
}
-
- Size = StrSize(Buffer) - sizeof(Buffer[0]);
- Status = FileHandleWrite(Handle, &Size, Buffer);
+
+ Ascii = FALSE;
+ AsciiBuffer = NULL;
+
+ Status = FileHandleGetPosition(Handle, &OriginalFilePosition);
if (EFI_ERROR(Status)) {
- return (Status);
+ return Status;
+ }
+
+ Status = FileHandleSetPosition(Handle, 0);
+ if (EFI_ERROR(Status)) {
+ return Status;
}
- Size = StrSize(L"\r\n") - sizeof(CHAR16);
- return FileHandleWrite(Handle, &Size, L"\r\n");
+
+ Status = FileHandleGetSize(Handle, &FileSize);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ if (FileSize == 0) {
+ Ascii = TRUE;
+ } else {
+ CharSize = sizeof (CHAR16);
+ Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
+ ASSERT_EFI_ERROR (Status);
+ if (CharBuffer == gUnicodeFileTag) {
+ Ascii = FALSE;
+ } else {
+ Ascii = TRUE;
+ }
+ }
+
+ Status = FileHandleSetPosition(Handle, OriginalFilePosition);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ if (Ascii) {
+ Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8);
+ AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size);
+ if (AsciiBuffer == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+ UnicodeStrToAsciiStr (Buffer, AsciiBuffer);
+
+ Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8);
+ Status = FileHandleWrite(Handle, &Size, AsciiBuffer);
+ if (EFI_ERROR(Status)) {
+ FreePool (AsciiBuffer);
+ return (Status);
+ }
+ Size = AsciiStrSize("\r\n") - sizeof(CHAR8);
+ Status = FileHandleWrite(Handle, &Size, "\r\n");
+ } else {
+ if (OriginalFilePosition == 0) {
+ Status = FileHandleSetPosition (Handle, sizeof(CHAR16));
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+ }
+ Size = StrSize(Buffer) - sizeof(CHAR16);
+ Status = FileHandleWrite(Handle, &Size, Buffer);
+ if (EFI_ERROR(Status)) {
+ return (Status);
+ }
+ Size = StrSize(L"\r\n") - sizeof(CHAR16);
+ Status = FileHandleWrite(Handle, &Size, L"\r\n");
+ }
+
+ if (AsciiBuffer != NULL) {
+ FreePool (AsciiBuffer);
+ }
+ return Status;
}

/**
--
1.9.5.msysgit.1
Carsey, Jaben
2015-06-23 15:08:33 UTC
Permalink
-----Original Message-----
From: Qiu, Shumin
Sent: Monday, June 22, 2015 11:30 PM
Cc: Carsey, Jaben; Gao, Liming
Subject: [PATCH] MdePkg\Library\UefiFileHandleLib: Make
FileHandleWriteLine support both ASCII and UNICODE file.
Importance: High
When the file is a UNICODE file (with UNICODE file tag) write UNICODE text.
When the file is an ASCII file write ASCII text.
If the file size is zero (without the file tag at the beginning) write ASCII text as default.
Contributed-under: TianoCore Contribution Agreement 1.0
---
MdePkg/Include/Library/FileHandleLib.h | 10 ++-
.../Library/UefiFileHandleLib/UefiFileHandleLib.c | 97
++++++++++++++++++++--
2 files changed, 97 insertions(+), 10 deletions(-)
diff --git a/MdePkg/Include/Library/FileHandleLib.h
b/MdePkg/Include/Library/FileHandleLib.h
index bfcf8a4..b5ac19a 100644
--- a/MdePkg/Include/Library/FileHandleLib.h
+++ b/MdePkg/Include/Library/FileHandleLib.h
@@ -433,7 +433,13 @@ FileHandleReturnLine(
);
/**
- Function to write a line of unicode text to a file.
+ Function to write a line of text to a file.
+
+ If the file is a Unicode file (with UNICODE file tag) then write the unicode
+ text.
+ If the file is an ASCII file then write the ASCII text.
+ If the size of file is zero (without file tag at the beginning) then write
+ ASCII text as default.
@param[in] Handle FileHandle to write to.
@param[in] Buffer Buffer to write, if NULL the function will
@@ -442,6 +448,8 @@ FileHandleReturnLine(
@retval EFI_SUCCESS The data was written.
Buffer is NULL.
@retval EFI_INVALID_PARAMETER Handle is NULL.
+ string due to out of resources.
@sa FileHandleWrite
**/
diff --git a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
index 96f16ca..f6cbfad 100644
--- a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
+++ b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
@@ -1027,7 +1027,13 @@ FileHandleReadLine(
}
/**
- Function to write a line of unicode text to a file.
+ Function to write a line of text to a file.
+
+ If the file is a Unicode file (with UNICODE file tag) then write the unicode
+ text.
+ If the file is an ASCII file then write the ASCII text.
+ If the size of file is zero (without file tag at the beginning) then write
+ ASCII text as default.
@param[in] Handle FileHandle to write to.
@param[in] Buffer Buffer to write, if NULL the function will
@@ -1036,6 +1042,8 @@ FileHandleReadLine(
@retval EFI_SUCCESS The data was written.
Buffer is NULL.
@retval EFI_INVALID_PARAMETER Handle is NULL.
+ string due to out of resources.
@sa FileHandleWrite
**/
@@ -1046,8 +1054,14 @@ FileHandleWriteLine(
IN CHAR16 *Buffer
)
{
- EFI_STATUS Status;
- UINTN Size;
+ EFI_STATUS Status;
+ CHAR16 CharBuffer;
+ UINTN Size;
+ UINTN CharSize;
+ UINT64 FileSize;
+ UINT64 OriginalFilePosition;
+ BOOLEAN Ascii;
+ CHAR8 *AsciiBuffer;
if (Buffer == NULL) {
return (EFI_SUCCESS);
@@ -1056,14 +1070,79 @@ FileHandleWriteLine(
if (Handle == NULL) {
return (EFI_INVALID_PARAMETER);
}
-
- Size = StrSize(Buffer) - sizeof(Buffer[0]);
- Status = FileHandleWrite(Handle, &Size, Buffer);
+
+ Ascii = FALSE;
+ AsciiBuffer = NULL;
+
+ Status = FileHandleGetPosition(Handle, &OriginalFilePosition);
if (EFI_ERROR(Status)) {
- return (Status);
+ return Status;
+ }
+
+ Status = FileHandleSetPosition(Handle, 0);
+ if (EFI_ERROR(Status)) {
+ return Status;
}
- Size = StrSize(L"\r\n") - sizeof(CHAR16);
- return FileHandleWrite(Handle, &Size, L"\r\n");
+
+ Status = FileHandleGetSize(Handle, &FileSize);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ if (FileSize == 0) {
+ Ascii = TRUE;
+ } else {
+ CharSize = sizeof (CHAR16);
+ Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
+ ASSERT_EFI_ERROR (Status);
+ if (CharBuffer == gUnicodeFileTag) {
+ Ascii = FALSE;
+ } else {
+ Ascii = TRUE;
+ }
+ }
+
+ Status = FileHandleSetPosition(Handle, OriginalFilePosition);
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ if (Ascii) {
+ Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8);
+ AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size);
+ if (AsciiBuffer == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+ UnicodeStrToAsciiStr (Buffer, AsciiBuffer);
+
+ Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8);
+ Status = FileHandleWrite(Handle, &Size, AsciiBuffer);
+ if (EFI_ERROR(Status)) {
+ FreePool (AsciiBuffer);
+ return (Status);
+ }
+ Size = AsciiStrSize("\r\n") - sizeof(CHAR8);
+ Status = FileHandleWrite(Handle, &Size, "\r\n");
+ } else {
+ if (OriginalFilePosition == 0) {
+ Status = FileHandleSetPosition (Handle, sizeof(CHAR16));
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+ }
+ Size = StrSize(Buffer) - sizeof(CHAR16);
+ Status = FileHandleWrite(Handle, &Size, Buffer);
+ if (EFI_ERROR(Status)) {
+ return (Status);
+ }
+ Size = StrSize(L"\r\n") - sizeof(CHAR16);
+ Status = FileHandleWrite(Handle, &Size, L"\r\n");
+ }
+
+ if (AsciiBuffer != NULL) {
+ FreePool (AsciiBuffer);
+ }
+ return Status;
}
/**
--
1.9.5.msysgit.1
Gao, Liming
2015-06-24 07:42:54 UTC
Permalink
Reviewed-by: Liming Gao <***@intel.com>

-----Original Message-----
From: Qiu, Shumin
Sent: Tuesday, June 23, 2015 2:30 PM
To: edk2-***@lists.sourceforge.net
Cc: Carsey, Jaben; Gao, Liming
Subject: [PATCH] MdePkg\Library\UefiFileHandleLib: Make FileHandleWriteLine support both ASCII and UNICODE file.

When the file is a UNICODE file (with UNICODE file tag) write UNICODE text.
When the file is an ASCII file write ASCII text.
If the file size is zero (without the file tag at the beginning) write ASCII text as default.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Qiu Shumin <***@intel.com>
---
MdePkg/Include/Library/FileHandleLib.h | 10 ++-
.../Library/UefiFileHandleLib/UefiFileHandleLib.c | 97 ++++++++++++++++++++--
2 files changed, 97 insertions(+), 10 deletions(-)

diff --git a/MdePkg/Include/Library/FileHandleLib.h b/MdePkg/Include/Library/FileHandleLib.h
index bfcf8a4..b5ac19a 100644
--- a/MdePkg/Include/Library/FileHandleLib.h
+++ b/MdePkg/Include/Library/FileHandleLib.h
@@ -433,7 +433,13 @@ FileHandleReturnLine(
);

/**
- Function to write a line of unicode text to a file.
+ Function to write a line of text to a file.
+
+ If the file is a Unicode file (with UNICODE file tag) then write the
+ unicode text.
+ If the file is an ASCII file then write the ASCII text.
+ If the size of file is zero (without file tag at the beginning) then
+ write ASCII text as default.

@param[in] Handle FileHandle to write to.
@param[in] Buffer Buffer to write, if NULL the function will
@@ -442,6 +448,8 @@ FileHandleReturnLine(
@retval EFI_SUCCESS The data was written.
Buffer is NULL.
@retval EFI_INVALID_PARAMETER Handle is NULL.
+ @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
+ string due to out of resources.

@sa FileHandleWrite
**/
diff --git a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
index 96f16ca..f6cbfad 100644
--- a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
+++ b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
@@ -1027,7 +1027,13 @@ FileHandleReadLine( }

/**
- Function to write a line of unicode text to a file.
+ Function to write a line of text to a file.
+
+ If the file is a Unicode file (with UNICODE file tag) then write the
+ unicode text.
+ If the file is an ASCII file then write the ASCII text.
+ If the size of file is zero (without file tag at the beginning) then
+ write ASCII text as default.

@param[in] Handle FileHandle to write to.
@param[in] Buffer Buffer to write, if NULL the function will
@@ -1036,6 +1042,8 @@ FileHandleReadLine(
@retval EFI_SUCCESS The data was written.
Buffer is NULL.
@retval EFI_INVALID_PARAMETER Handle is NULL.
+ @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
+ string due to out of resources.

@sa FileHandleWrite
**/
@@ -1046,8 +1054,14 @@ FileHandleWriteLine(
IN CHAR16 *Buffer
)
{
- EFI_STATUS Status;
- UINTN Size;
+ EFI_STATUS Status;
+ CHAR16 CharBuffer;
+ UINTN Size;
+ UINTN CharSize;
+ UINT64 FileSize;
+ UINT64 OriginalFilePosition;
+ BOOLEAN Ascii;
+ CHAR8 *AsciiBuffer;

if (Buffer == NULL) {
return (EFI_SUCCESS);
@@ -1056,14 +1070,79 @@ FileHandleWriteLine(
if (Handle == NULL) {
return (EFI_INVALID_PARAMETER);
}
-
- Size = StrSize(Buffer) - sizeof(Buffer[0]);
- Status = FileHandleWrite(Handle, &Size, Buffer);
+
+ Ascii = FALSE;
+ AsciiBuffer = NULL;
+
+ Status = FileHandleGetPosition(Handle, &OriginalFilePosition);
if (EFI_ERROR(Status)) {
- return (Status);
+ return Status;
+ }
+
+ Status = FileHandleSetPosition(Handle, 0); if (EFI_ERROR(Status)) {
+ return Status;
}
- Size = StrSize(L"\r\n") - sizeof(CHAR16);
- return FileHandleWrite(Handle, &Size, L"\r\n");
+
+ Status = FileHandleGetSize(Handle, &FileSize); if
+ (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ if (FileSize == 0) {
+ Ascii = TRUE;
+ } else {
+ CharSize = sizeof (CHAR16);
+ Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
+ ASSERT_EFI_ERROR (Status);
+ if (CharBuffer == gUnicodeFileTag) {
+ Ascii = FALSE;
+ } else {
+ Ascii = TRUE;
+ }
+ }
+
+ Status = FileHandleSetPosition(Handle, OriginalFilePosition); if
+ (EFI_ERROR(Status)) {
+ return Status;
+ }
+
+ if (Ascii) {
+ Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8);
+ AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size);
+ if (AsciiBuffer == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+ UnicodeStrToAsciiStr (Buffer, AsciiBuffer);
+
+ Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8);
+ Status = FileHandleWrite(Handle, &Size, AsciiBuffer);
+ if (EFI_ERROR(Status)) {
+ FreePool (AsciiBuffer);
+ return (Status);
+ }
+ Size = AsciiStrSize("\r\n") - sizeof(CHAR8);
+ Status = FileHandleWrite(Handle, &Size, "\r\n"); } else {
+ if (OriginalFilePosition == 0) {
+ Status = FileHandleSetPosition (Handle, sizeof(CHAR16));
+ if (EFI_ERROR(Status)) {
+ return Status;
+ }
+ }
+ Size = StrSize(Buffer) - sizeof(CHAR16);
+ Status = FileHandleWrite(Handle, &Size, Buffer);
+ if (EFI_ERROR(Status)) {
+ return (Status);
+ }
+ Size = StrSize(L"\r\n") - sizeof(CHAR16);
+ Status = FileHandleWrite(Handle, &Size, L"\r\n"); }
+
+ if (AsciiBuffer != NULL) {
+ FreePool (AsciiBuffer);
+ }
+ return Status;
}

/**
--
1.9.5.msysgit.1

Loading...