Make FPDF_LoadDocument() treat the filename as UTF-8 on Windows.
On Windows, FPDF_LoadDocument() ultimately goes through
CFX_FileAccess_Windows::Open(), which uses win32 CreateFileA(). This
only works with ANSI strings. Meanwhile, there exists an unused
CFX_FileAccess_Windows::Open() variant that uses CreateFileW(). Combine
the two into a version that assumes the input filename is UTF-8,
converts it into a wide string, and passes the converted filename into
CreateFileW().
Since the filename always comes from a byte string, delete the wide
string version of Open() on POSIX as well.
Bug: pdfium:682
Change-Id: Ie1213f1c8bc29d47464c3f9e7e6ce4fa6e2f4b75
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/90693
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/cfx_fileaccess_posix.cpp b/core/fxcrt/cfx_fileaccess_posix.cpp
index f558282..f2264d3 100644
--- a/core/fxcrt/cfx_fileaccess_posix.cpp
+++ b/core/fxcrt/cfx_fileaccess_posix.cpp
@@ -65,10 +65,6 @@
return m_nFD > -1;
}
-bool CFX_FileAccess_Posix::Open(WideStringView fileName, uint32_t dwMode) {
- return Open(FX_UTF8Encode(fileName).AsStringView(), dwMode);
-}
-
void CFX_FileAccess_Posix::Close() {
if (m_nFD < 0) {
return;
diff --git a/core/fxcrt/cfx_fileaccess_posix.h b/core/fxcrt/cfx_fileaccess_posix.h
index b752691..1c31011 100644
--- a/core/fxcrt/cfx_fileaccess_posix.h
+++ b/core/fxcrt/cfx_fileaccess_posix.h
@@ -25,7 +25,6 @@
// FileAccessIface:
bool Open(ByteStringView fileName, uint32_t dwMode) override;
- bool Open(WideStringView fileName, uint32_t dwMode) override;
void Close() override;
FX_FILESIZE GetSize() const override;
FX_FILESIZE GetPosition() const override;
diff --git a/core/fxcrt/cfx_fileaccess_windows.cpp b/core/fxcrt/cfx_fileaccess_windows.cpp
index 3a0fd12..371aae3 100644
--- a/core/fxcrt/cfx_fileaccess_windows.cpp
+++ b/core/fxcrt/cfx_fileaccess_windows.cpp
@@ -44,25 +44,14 @@
if (m_hFile)
return false;
- uint32_t dwAccess, dwShare, dwCreation;
+ uint32_t dwAccess;
+ uint32_t dwShare;
+ uint32_t dwCreation;
GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
- m_hFile = ::CreateFileA(fileName.unterminated_c_str(), dwAccess, dwShare,
- nullptr, dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr);
- if (m_hFile == INVALID_HANDLE_VALUE)
- m_hFile = nullptr;
- return !!m_hFile;
-}
-
-bool CFX_FileAccess_Windows::Open(WideStringView fileName, uint32_t dwMode) {
- if (m_hFile)
- return false;
-
- uint32_t dwAccess, dwShare, dwCreation;
- GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
- m_hFile =
- ::CreateFileW((LPCWSTR)fileName.unterminated_c_str(), dwAccess, dwShare,
- nullptr, dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr);
+ WideString wname = FX_UTF8Decode(fileName);
+ m_hFile = ::CreateFileW(wname.c_str(), dwAccess, dwShare, nullptr, dwCreation,
+ FILE_ATTRIBUTE_NORMAL, nullptr);
if (m_hFile == INVALID_HANDLE_VALUE)
m_hFile = nullptr;
diff --git a/core/fxcrt/cfx_fileaccess_windows.h b/core/fxcrt/cfx_fileaccess_windows.h
index 8a2d8a2..56c580d 100644
--- a/core/fxcrt/cfx_fileaccess_windows.h
+++ b/core/fxcrt/cfx_fileaccess_windows.h
@@ -25,7 +25,6 @@
// FileAccessIface
bool Open(ByteStringView fileName, uint32_t dwMode) override;
- bool Open(WideStringView fileName, uint32_t dwMode) override;
void Close() override;
FX_FILESIZE GetSize() const override;
FX_FILESIZE GetPosition() const override;
diff --git a/core/fxcrt/fileaccess_iface.h b/core/fxcrt/fileaccess_iface.h
index 8493ecb..ae34be5 100644
--- a/core/fxcrt/fileaccess_iface.h
+++ b/core/fxcrt/fileaccess_iface.h
@@ -16,8 +16,8 @@
static std::unique_ptr<FileAccessIface> Create();
virtual ~FileAccessIface() = default;
+ // `fileName` is UTF-8 on all platforms.
virtual bool Open(ByteStringView fileName, uint32_t dwMode) = 0;
- virtual bool Open(WideStringView fileName, uint32_t dwMode) = 0;
virtual void Close() = 0;
virtual FX_FILESIZE GetSize() const = 0;
virtual FX_FILESIZE GetPosition() const = 0;
diff --git a/core/fxcrt/fx_stream.cpp b/core/fxcrt/fx_stream.cpp
index 9a65181..3039744 100644
--- a/core/fxcrt/fx_stream.cpp
+++ b/core/fxcrt/fx_stream.cpp
@@ -77,16 +77,6 @@
}
// static
-RetainPtr<IFX_SeekableStream> IFX_SeekableStream::CreateFromFilename(
- const wchar_t* filename,
- uint32_t dwModes) {
- std::unique_ptr<FileAccessIface> pFA = FileAccessIface::Create();
- if (!pFA->Open(filename, dwModes))
- return nullptr;
- return pdfium::MakeRetain<CFX_CRTFileStream>(std::move(pFA));
-}
-
-// static
RetainPtr<IFX_SeekableReadStream> IFX_SeekableReadStream::CreateFromFilename(
const char* filename) {
return IFX_SeekableStream::CreateFromFilename(filename, FX_FILEMODE_ReadOnly);
diff --git a/core/fxcrt/fx_stream.h b/core/fxcrt/fx_stream.h
index 9239ab4..7658e0b 100644
--- a/core/fxcrt/fx_stream.h
+++ b/core/fxcrt/fx_stream.h
@@ -77,11 +77,6 @@
static RetainPtr<IFX_SeekableStream> CreateFromFilename(const char* filename,
uint32_t dwModes);
- // dwModes is a mask of FX_FILEMODE_* from above.
- static RetainPtr<IFX_SeekableStream> CreateFromFilename(
- const wchar_t* filename,
- uint32_t dwModes);
-
// IFX_SeekableWriteStream:
bool WriteBlock(const void* buffer, size_t size) override;
};
diff --git a/public/fpdfview.h b/public/fpdfview.h
index a06f8f8..d7f6f07 100644
--- a/public/fpdfview.h
+++ b/public/fpdfview.h
@@ -335,6 +335,8 @@
// If this function fails, you can use FPDF_GetLastError() to retrieve
// the reason why it failed.
//
+// The encoding for |file_path| is UTF-8.
+//
// The encoding for |password| can be either UTF-8 or Latin-1. PDFs,
// depending on the security handler revision, will only accept one or
// the other encoding. If |password|'s encoding and the PDF's expected