summaryrefslogtreecommitdiffstats
path: root/ShellPkg/Application
diff options
context:
space:
mode:
authorRuiyu Ni <ruiyu.ni@intel.com>2018-08-08 18:15:54 +0800
committerRuiyu Ni <ruiyu.ni@intel.com>2018-08-16 16:03:15 +0800
commitbc0d3e29122df5fe4b71f6ef62e8652ea29fd6a0 (patch)
treedf1984dd1cbff6d7fcf3fb4b2b90e6f7c75bb608 /ShellPkg/Application
parent7b85a1afa38c13fd40095f277cd26e5aa159b2f5 (diff)
downloadedk2-bc0d3e29122df5fe4b71f6ef62e8652ea29fd6a0.tar.gz
edk2-bc0d3e29122df5fe4b71f6ef62e8652ea29fd6a0.tar.bz2
edk2-bc0d3e29122df5fe4b71f6ef62e8652ea29fd6a0.zip
ShellPkg/redirection: Insert \xFEFF after converting ASCII to Unicode
When "<a" is used to redirect ASCII file to an application, Shell core reads the ASCII file and converts the ASCII to Unicode as the input source of the application. But per Shell spec, the input source should have \xFEFF to indicate it's a Unicode stream. The patch adds the missing \xFEFF. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com> Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
Diffstat (limited to 'ShellPkg/Application')
-rw-r--r--ShellPkg/Application/Shell/FileHandleWrappers.c46
1 files changed, 31 insertions, 15 deletions
diff --git a/ShellPkg/Application/Shell/FileHandleWrappers.c b/ShellPkg/Application/Shell/FileHandleWrappers.c
index 8c62eb5862..655854b25d 100644
--- a/ShellPkg/Application/Shell/FileHandleWrappers.c
+++ b/ShellPkg/Application/Shell/FileHandleWrappers.c
@@ -1924,42 +1924,58 @@ FileInterfaceFileRead(
OUT VOID *Buffer
)
{
+ EFI_STATUS Status;
+ UINT64 Position;
CHAR8 *AsciiStrBuffer;
CHAR16 *UscStrBuffer;
UINTN Size;
- UINTN CharNum;
- EFI_STATUS Status;
if (((EFI_FILE_PROTOCOL_FILE*)This)->Unicode) {
//
// Unicode
+ // There might be different file tag for the Unicode file. We cannot unconditionally insert the \xFEFF.
+ // So we choose to leave the file content as is.
//
return (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, BufferSize, Buffer));
} else {
//
// Ascii
//
- Size = (*BufferSize) / sizeof(CHAR16);
- AsciiStrBuffer = AllocateZeroPool(Size + sizeof(CHAR8));
+ *BufferSize = *BufferSize / sizeof (CHAR16) * sizeof (CHAR16);
+ if (*BufferSize == 0) {
+ return EFI_SUCCESS;
+ }
+ Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->GetPosition (((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Position);
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+ if (Position == 0) {
+ //
+ // First two bytes in Buffer is for the Unicode file tag.
+ //
+ *(CHAR16 *)Buffer = gUnicodeFileTag;
+ Buffer = (CHAR16 *)Buffer + 1;
+ Size = *BufferSize / sizeof (CHAR16) - 1;
+ } else {
+ Size = *BufferSize / sizeof (CHAR16);
+ }
+ AsciiStrBuffer = AllocateZeroPool (Size + 1);
if (AsciiStrBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
- UscStrBuffer = AllocateZeroPool(*BufferSize + sizeof(CHAR16));
+ UscStrBuffer = AllocateZeroPool ((Size + 1) * sizeof(CHAR16));
if (UscStrBuffer== NULL) {
SHELL_FREE_NON_NULL(AsciiStrBuffer);
return EFI_OUT_OF_RESOURCES;
}
- Status = (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiStrBuffer));
+ Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read (((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiStrBuffer);
if (!EFI_ERROR(Status)) {
- CharNum = UnicodeSPrint(UscStrBuffer, *BufferSize + sizeof(CHAR16), L"%a", AsciiStrBuffer);
- if (CharNum == Size) {
- CopyMem (Buffer, UscStrBuffer, *BufferSize);
- } else {
- Status = EFI_UNSUPPORTED;
- }
+ AsciiStrToUnicodeStrS (AsciiStrBuffer, UscStrBuffer, Size + 1);
+ *BufferSize = Size * sizeof (CHAR16);
+ CopyMem (Buffer, UscStrBuffer, *BufferSize);
}
- SHELL_FREE_NON_NULL(AsciiStrBuffer);
- SHELL_FREE_NON_NULL(UscStrBuffer);
- return (Status);
+ SHELL_FREE_NON_NULL (AsciiStrBuffer);
+ SHELL_FREE_NON_NULL (UscStrBuffer);
+ return Status;
}
}