From 1e8dd54c54a04d5f5ee249db22f84001fa16101e Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 31 Aug 2017 16:31:47 -0400 Subject: Rename fxcrt_ and ifxcrt_ files to better names The CFXCRT and IFXCRT prefix was only used on 3 files. This CL renames them to the more common CFX and IFX. The files were renamed as needed. Change-Id: Iccdaa55c5822adb93af7c58aedfb121413a30223 Reviewed-on: https://pdfium-review.googlesource.com/12675 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- core/fxcrt/cfx_fileaccess_posix.cpp | 153 ++++++++++++++++++++++++++ core/fxcrt/cfx_fileaccess_posix.h | 41 +++++++ core/fxcrt/cfx_fileaccess_windows.cpp | 196 ++++++++++++++++++++++++++++++++++ core/fxcrt/cfx_fileaccess_windows.h | 39 +++++++ core/fxcrt/fx_stream.cpp | 10 +- core/fxcrt/fxcrt_posix.cpp | 149 -------------------------- core/fxcrt/fxcrt_posix.h | 41 ------- core/fxcrt/fxcrt_windows.cpp | 192 --------------------------------- core/fxcrt/fxcrt_windows.h | 39 ------- core/fxcrt/ifx_fileaccess.h | 38 +++++++ core/fxcrt/ifxcrt_fileaccess.h | 38 ------- 11 files changed, 472 insertions(+), 464 deletions(-) create mode 100644 core/fxcrt/cfx_fileaccess_posix.cpp create mode 100644 core/fxcrt/cfx_fileaccess_posix.h create mode 100644 core/fxcrt/cfx_fileaccess_windows.cpp create mode 100644 core/fxcrt/cfx_fileaccess_windows.h delete mode 100644 core/fxcrt/fxcrt_posix.cpp delete mode 100644 core/fxcrt/fxcrt_posix.h delete mode 100644 core/fxcrt/fxcrt_windows.cpp delete mode 100644 core/fxcrt/fxcrt_windows.h create mode 100644 core/fxcrt/ifx_fileaccess.h delete mode 100644 core/fxcrt/ifxcrt_fileaccess.h (limited to 'core') diff --git a/core/fxcrt/cfx_fileaccess_posix.cpp b/core/fxcrt/cfx_fileaccess_posix.cpp new file mode 100644 index 0000000000..345b6641ef --- /dev/null +++ b/core/fxcrt/cfx_fileaccess_posix.cpp @@ -0,0 +1,153 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxcrt/cfx_fileaccess_posix.h" + +#include + +#include "third_party/base/ptr_util.h" + +#ifndef O_BINARY +#define O_BINARY 0 +#endif // O_BINARY + +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif // O_LARGEFILE + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \ + _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || \ + _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ + +namespace { + +void GetFileMode(uint32_t dwModes, int32_t& nFlags, int32_t& nMasks) { + nFlags = O_BINARY | O_LARGEFILE; + if (dwModes & FX_FILEMODE_ReadOnly) { + nFlags |= O_RDONLY; + nMasks = 0; + } else { + nFlags |= O_RDWR | O_CREAT; + if (dwModes & FX_FILEMODE_Truncate) { + nFlags |= O_TRUNC; + } + nMasks = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; + } +} + +} // namespace + +// static +std::unique_ptr IFX_FileAccess::Create() { + return pdfium::MakeUnique(); +} + +CFX_FileAccess_Posix::CFX_FileAccess_Posix() : m_nFD(-1) {} + +CFX_FileAccess_Posix::~CFX_FileAccess_Posix() { + Close(); +} + +bool CFX_FileAccess_Posix::Open(const CFX_ByteStringC& fileName, + uint32_t dwMode) { + if (m_nFD > -1) + return false; + + int32_t nFlags; + int32_t nMasks; + GetFileMode(dwMode, nFlags, nMasks); + + // TODO(tsepez): check usage of c_str() below. + m_nFD = open(fileName.unterminated_c_str(), nFlags, nMasks); + return m_nFD > -1; +} + +bool CFX_FileAccess_Posix::Open(const CFX_WideStringC& fileName, + uint32_t dwMode) { + return Open(FX_UTF8Encode(fileName).AsStringC(), dwMode); +} + +void CFX_FileAccess_Posix::Close() { + if (m_nFD < 0) { + return; + } + close(m_nFD); + m_nFD = -1; +} +FX_FILESIZE CFX_FileAccess_Posix::GetSize() const { + if (m_nFD < 0) { + return 0; + } + struct stat s; + memset(&s, 0, sizeof(s)); + fstat(m_nFD, &s); + return s.st_size; +} +FX_FILESIZE CFX_FileAccess_Posix::GetPosition() const { + if (m_nFD < 0) { + return (FX_FILESIZE)-1; + } + return lseek(m_nFD, 0, SEEK_CUR); +} +FX_FILESIZE CFX_FileAccess_Posix::SetPosition(FX_FILESIZE pos) { + if (m_nFD < 0) { + return (FX_FILESIZE)-1; + } + return lseek(m_nFD, pos, SEEK_SET); +} +size_t CFX_FileAccess_Posix::Read(void* pBuffer, size_t szBuffer) { + if (m_nFD < 0) { + return 0; + } + return read(m_nFD, pBuffer, szBuffer); +} +size_t CFX_FileAccess_Posix::Write(const void* pBuffer, size_t szBuffer) { + if (m_nFD < 0) { + return 0; + } + return write(m_nFD, pBuffer, szBuffer); +} +size_t CFX_FileAccess_Posix::ReadPos(void* pBuffer, + size_t szBuffer, + FX_FILESIZE pos) { + if (m_nFD < 0) { + return 0; + } + if (pos >= GetSize()) { + return 0; + } + if (SetPosition(pos) == (FX_FILESIZE)-1) { + return 0; + } + return Read(pBuffer, szBuffer); +} +size_t CFX_FileAccess_Posix::WritePos(const void* pBuffer, + size_t szBuffer, + FX_FILESIZE pos) { + if (m_nFD < 0) { + return 0; + } + if (SetPosition(pos) == (FX_FILESIZE)-1) { + return 0; + } + return Write(pBuffer, szBuffer); +} + +bool CFX_FileAccess_Posix::Flush() { + if (m_nFD < 0) + return false; + + return fsync(m_nFD) > -1; +} + +bool CFX_FileAccess_Posix::Truncate(FX_FILESIZE szFile) { + if (m_nFD < 0) + return false; + + return !ftruncate(m_nFD, szFile); +} + +#endif diff --git a/core/fxcrt/cfx_fileaccess_posix.h b/core/fxcrt/cfx_fileaccess_posix.h new file mode 100644 index 0000000000..c299f6cc34 --- /dev/null +++ b/core/fxcrt/cfx_fileaccess_posix.h @@ -0,0 +1,41 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_CFX_FILEACCESS_POSIX_H_ +#define CORE_FXCRT_CFX_FILEACCESS_POSIX_H_ + +#include "core/fxcrt/ifx_fileaccess.h" + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \ + _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || \ + _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ +class CFX_FileAccess_Posix : public IFX_FileAccess { + public: + CFX_FileAccess_Posix(); + ~CFX_FileAccess_Posix() override; + + // IFX_FileAccess: + bool Open(const CFX_ByteStringC& fileName, uint32_t dwMode) override; + bool Open(const CFX_WideStringC& fileName, uint32_t dwMode) override; + void Close() override; + FX_FILESIZE GetSize() const override; + FX_FILESIZE GetPosition() const override; + FX_FILESIZE SetPosition(FX_FILESIZE pos) override; + size_t Read(void* pBuffer, size_t szBuffer) override; + size_t Write(const void* pBuffer, size_t szBuffer) override; + size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) override; + size_t WritePos(const void* pBuffer, + size_t szBuffer, + FX_FILESIZE pos) override; + bool Flush() override; + bool Truncate(FX_FILESIZE szFile) override; + + protected: + int32_t m_nFD; +}; +#endif + +#endif // CORE_FXCRT_CFX_FILEACCESS_POSIX_H_ diff --git a/core/fxcrt/cfx_fileaccess_windows.cpp b/core/fxcrt/cfx_fileaccess_windows.cpp new file mode 100644 index 0000000000..b39a4d6efc --- /dev/null +++ b/core/fxcrt/cfx_fileaccess_windows.cpp @@ -0,0 +1,196 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxcrt/cfx_fileaccess_windows.h" + +#include + +#include "core/fxcrt/fx_string.h" +#include "third_party/base/ptr_util.h" + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + +namespace { + +void GetFileMode(uint32_t dwMode, + uint32_t& dwAccess, + uint32_t& dwShare, + uint32_t& dwCreation) { + dwAccess = GENERIC_READ; + dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE; + if (!(dwMode & FX_FILEMODE_ReadOnly)) { + dwAccess |= GENERIC_WRITE; + dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS; + } else { + dwCreation = OPEN_EXISTING; + } +} + +} // namespace + +// static +std::unique_ptr IFX_FileAccess::Create() { + return pdfium::MakeUnique(); +} + +#ifdef __cplusplus +extern "C" { +#endif +WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize); +WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, + LARGE_INTEGER liDistanceToMove, + PLARGE_INTEGER lpNewFilePointer, + DWORD dwMoveMethod); +#ifdef __cplusplus +} +#endif + +CFX_FileAccess_Windows::CFX_FileAccess_Windows() : m_hFile(nullptr) {} + +CFX_FileAccess_Windows::~CFX_FileAccess_Windows() { + Close(); +} + +bool CFX_FileAccess_Windows::Open(const CFX_ByteStringC& fileName, + uint32_t dwMode) { + if (m_hFile) + return false; + + uint32_t dwAccess, dwShare, 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(const CFX_WideStringC& 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); + if (m_hFile == INVALID_HANDLE_VALUE) + m_hFile = nullptr; + + return !!m_hFile; +} + +void CFX_FileAccess_Windows::Close() { + if (!m_hFile) + return; + + ::CloseHandle(m_hFile); + m_hFile = nullptr; +} + +FX_FILESIZE CFX_FileAccess_Windows::GetSize() const { + if (!m_hFile) + return 0; + + LARGE_INTEGER size = {}; + if (!::GetFileSizeEx(m_hFile, &size)) + return 0; + + return (FX_FILESIZE)size.QuadPart; +} + +FX_FILESIZE CFX_FileAccess_Windows::GetPosition() const { + if (!m_hFile) + return (FX_FILESIZE)-1; + + LARGE_INTEGER dist = {}; + LARGE_INTEGER newPos = {}; + if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) + return (FX_FILESIZE)-1; + + return (FX_FILESIZE)newPos.QuadPart; +} + +FX_FILESIZE CFX_FileAccess_Windows::SetPosition(FX_FILESIZE pos) { + if (!m_hFile) + return (FX_FILESIZE)-1; + + LARGE_INTEGER dist; + dist.QuadPart = pos; + LARGE_INTEGER newPos = {}; + if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) + return (FX_FILESIZE)-1; + + return (FX_FILESIZE)newPos.QuadPart; +} + +size_t CFX_FileAccess_Windows::Read(void* pBuffer, size_t szBuffer) { + if (!m_hFile) + return 0; + + size_t szRead = 0; + if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, + nullptr)) { + return 0; + } + return szRead; +} + +size_t CFX_FileAccess_Windows::Write(const void* pBuffer, size_t szBuffer) { + if (!m_hFile) + return 0; + + size_t szWrite = 0; + if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, + nullptr)) { + return 0; + } + return szWrite; +} + +size_t CFX_FileAccess_Windows::ReadPos(void* pBuffer, + size_t szBuffer, + FX_FILESIZE pos) { + if (!m_hFile) + return 0; + + if (pos >= GetSize()) + return 0; + + if (SetPosition(pos) == (FX_FILESIZE)-1) + return 0; + + return Read(pBuffer, szBuffer); +} + +size_t CFX_FileAccess_Windows::WritePos(const void* pBuffer, + size_t szBuffer, + FX_FILESIZE pos) { + if (!m_hFile) { + return 0; + } + if (SetPosition(pos) == (FX_FILESIZE)-1) { + return 0; + } + return Write(pBuffer, szBuffer); +} + +bool CFX_FileAccess_Windows::Flush() { + if (!m_hFile) + return false; + + return !!::FlushFileBuffers(m_hFile); +} + +bool CFX_FileAccess_Windows::Truncate(FX_FILESIZE szFile) { + if (SetPosition(szFile) == (FX_FILESIZE)-1) + return false; + + return !!::SetEndOfFile(m_hFile); +} +#endif diff --git a/core/fxcrt/cfx_fileaccess_windows.h b/core/fxcrt/cfx_fileaccess_windows.h new file mode 100644 index 0000000000..692a537a82 --- /dev/null +++ b/core/fxcrt/cfx_fileaccess_windows.h @@ -0,0 +1,39 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_CFX_FILEACCESS_WINDOWS_H_ +#define CORE_FXCRT_CFX_FILEACCESS_WINDOWS_H_ + +#include "core/fxcrt/ifx_fileaccess.h" + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +class CFX_FileAccess_Windows : public IFX_FileAccess { + public: + CFX_FileAccess_Windows(); + ~CFX_FileAccess_Windows() override; + + // IFX_FileAccess + bool Open(const CFX_ByteStringC& fileName, uint32_t dwMode) override; + bool Open(const CFX_WideStringC& fileName, uint32_t dwMode) override; + void Close() override; + FX_FILESIZE GetSize() const override; + FX_FILESIZE GetPosition() const override; + FX_FILESIZE SetPosition(FX_FILESIZE pos) override; + size_t Read(void* pBuffer, size_t szBuffer) override; + size_t Write(const void* pBuffer, size_t szBuffer) override; + size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) override; + size_t WritePos(const void* pBuffer, + size_t szBuffer, + FX_FILESIZE pos) override; + bool Flush() override; + bool Truncate(FX_FILESIZE szFile) override; + + protected: + void* m_hFile; +}; +#endif + +#endif // CORE_FXCRT_CFX_FILEACCESS_WINDOWS_H_ diff --git a/core/fxcrt/fx_stream.cpp b/core/fxcrt/fx_stream.cpp index 0cf34aa22e..c269ec91ed 100644 --- a/core/fxcrt/fx_stream.cpp +++ b/core/fxcrt/fx_stream.cpp @@ -12,7 +12,7 @@ #include #include "core/fxcrt/fx_safe_types.h" -#include "core/fxcrt/ifxcrt_fileaccess.h" +#include "core/fxcrt/ifx_fileaccess.h" #include "third_party/base/ptr_util.h" namespace { @@ -40,11 +40,11 @@ class CFX_CRTFileStream final : public IFX_SeekableStream { bool Flush() override { return m_pFile->Flush(); } private: - explicit CFX_CRTFileStream(std::unique_ptr pFA) + explicit CFX_CRTFileStream(std::unique_ptr pFA) : m_pFile(std::move(pFA)) {} ~CFX_CRTFileStream() override {} - std::unique_ptr m_pFile; + std::unique_ptr m_pFile; }; } // namespace @@ -53,7 +53,7 @@ class CFX_CRTFileStream final : public IFX_SeekableStream { CFX_RetainPtr IFX_SeekableStream::CreateFromFilename( const char* filename, uint32_t dwModes) { - std::unique_ptr pFA = IFXCRT_FileAccess::Create(); + std::unique_ptr pFA = IFX_FileAccess::Create(); if (!pFA->Open(filename, dwModes)) return nullptr; return pdfium::MakeRetain(std::move(pFA)); @@ -63,7 +63,7 @@ CFX_RetainPtr IFX_SeekableStream::CreateFromFilename( CFX_RetainPtr IFX_SeekableStream::CreateFromFilename( const wchar_t* filename, uint32_t dwModes) { - std::unique_ptr pFA = IFXCRT_FileAccess::Create(); + std::unique_ptr pFA = IFX_FileAccess::Create(); if (!pFA->Open(filename, dwModes)) return nullptr; return pdfium::MakeRetain(std::move(pFA)); diff --git a/core/fxcrt/fxcrt_posix.cpp b/core/fxcrt/fxcrt_posix.cpp deleted file mode 100644 index c206268029..0000000000 --- a/core/fxcrt/fxcrt_posix.cpp +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/fxcrt/fxcrt_posix.h" - -#include - -#include "third_party/base/ptr_util.h" - -#ifndef O_BINARY -#define O_BINARY 0 -#endif // O_BINARY - -#ifndef O_LARGEFILE -#define O_LARGEFILE 0 -#endif // O_LARGEFILE - -#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \ - _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || \ - _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ - -// static -std::unique_ptr IFXCRT_FileAccess::Create() { - return pdfium::MakeUnique(); -} - -void FXCRT_Posix_GetFileMode(uint32_t dwModes, - int32_t& nFlags, - int32_t& nMasks) { - nFlags = O_BINARY | O_LARGEFILE; - if (dwModes & FX_FILEMODE_ReadOnly) { - nFlags |= O_RDONLY; - nMasks = 0; - } else { - nFlags |= O_RDWR | O_CREAT; - if (dwModes & FX_FILEMODE_Truncate) { - nFlags |= O_TRUNC; - } - nMasks = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; - } -} -CFXCRT_FileAccess_Posix::CFXCRT_FileAccess_Posix() : m_nFD(-1) {} -CFXCRT_FileAccess_Posix::~CFXCRT_FileAccess_Posix() { - Close(); -} - -bool CFXCRT_FileAccess_Posix::Open(const CFX_ByteStringC& fileName, - uint32_t dwMode) { - if (m_nFD > -1) - return false; - - int32_t nFlags; - int32_t nMasks; - FXCRT_Posix_GetFileMode(dwMode, nFlags, nMasks); - - // TODO(tsepez): check usage of c_str() below. - m_nFD = open(fileName.unterminated_c_str(), nFlags, nMasks); - return m_nFD > -1; -} - -bool CFXCRT_FileAccess_Posix::Open(const CFX_WideStringC& fileName, - uint32_t dwMode) { - return Open(FX_UTF8Encode(fileName).AsStringC(), dwMode); -} - -void CFXCRT_FileAccess_Posix::Close() { - if (m_nFD < 0) { - return; - } - close(m_nFD); - m_nFD = -1; -} -FX_FILESIZE CFXCRT_FileAccess_Posix::GetSize() const { - if (m_nFD < 0) { - return 0; - } - struct stat s; - memset(&s, 0, sizeof(s)); - fstat(m_nFD, &s); - return s.st_size; -} -FX_FILESIZE CFXCRT_FileAccess_Posix::GetPosition() const { - if (m_nFD < 0) { - return (FX_FILESIZE)-1; - } - return lseek(m_nFD, 0, SEEK_CUR); -} -FX_FILESIZE CFXCRT_FileAccess_Posix::SetPosition(FX_FILESIZE pos) { - if (m_nFD < 0) { - return (FX_FILESIZE)-1; - } - return lseek(m_nFD, pos, SEEK_SET); -} -size_t CFXCRT_FileAccess_Posix::Read(void* pBuffer, size_t szBuffer) { - if (m_nFD < 0) { - return 0; - } - return read(m_nFD, pBuffer, szBuffer); -} -size_t CFXCRT_FileAccess_Posix::Write(const void* pBuffer, size_t szBuffer) { - if (m_nFD < 0) { - return 0; - } - return write(m_nFD, pBuffer, szBuffer); -} -size_t CFXCRT_FileAccess_Posix::ReadPos(void* pBuffer, - size_t szBuffer, - FX_FILESIZE pos) { - if (m_nFD < 0) { - return 0; - } - if (pos >= GetSize()) { - return 0; - } - if (SetPosition(pos) == (FX_FILESIZE)-1) { - return 0; - } - return Read(pBuffer, szBuffer); -} -size_t CFXCRT_FileAccess_Posix::WritePos(const void* pBuffer, - size_t szBuffer, - FX_FILESIZE pos) { - if (m_nFD < 0) { - return 0; - } - if (SetPosition(pos) == (FX_FILESIZE)-1) { - return 0; - } - return Write(pBuffer, szBuffer); -} - -bool CFXCRT_FileAccess_Posix::Flush() { - if (m_nFD < 0) - return false; - - return fsync(m_nFD) > -1; -} - -bool CFXCRT_FileAccess_Posix::Truncate(FX_FILESIZE szFile) { - if (m_nFD < 0) - return false; - - return !ftruncate(m_nFD, szFile); -} - -#endif diff --git a/core/fxcrt/fxcrt_posix.h b/core/fxcrt/fxcrt_posix.h deleted file mode 100644 index 109cf34a51..0000000000 --- a/core/fxcrt/fxcrt_posix.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXCRT_FXCRT_POSIX_H_ -#define CORE_FXCRT_FXCRT_POSIX_H_ - -#include "core/fxcrt/ifxcrt_fileaccess.h" - -#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \ - _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || \ - _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ -class CFXCRT_FileAccess_Posix : public IFXCRT_FileAccess { - public: - CFXCRT_FileAccess_Posix(); - ~CFXCRT_FileAccess_Posix() override; - - // IFXCRT_FileAccess: - bool Open(const CFX_ByteStringC& fileName, uint32_t dwMode) override; - bool Open(const CFX_WideStringC& fileName, uint32_t dwMode) override; - void Close() override; - FX_FILESIZE GetSize() const override; - FX_FILESIZE GetPosition() const override; - FX_FILESIZE SetPosition(FX_FILESIZE pos) override; - size_t Read(void* pBuffer, size_t szBuffer) override; - size_t Write(const void* pBuffer, size_t szBuffer) override; - size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) override; - size_t WritePos(const void* pBuffer, - size_t szBuffer, - FX_FILESIZE pos) override; - bool Flush() override; - bool Truncate(FX_FILESIZE szFile) override; - - protected: - int32_t m_nFD; -}; -#endif - -#endif // CORE_FXCRT_FXCRT_POSIX_H_ diff --git a/core/fxcrt/fxcrt_windows.cpp b/core/fxcrt/fxcrt_windows.cpp deleted file mode 100644 index 1a2a36735e..0000000000 --- a/core/fxcrt/fxcrt_windows.cpp +++ /dev/null @@ -1,192 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/fxcrt/fxcrt_windows.h" - -#include - -#include "core/fxcrt/fx_string.h" -#include "third_party/base/ptr_util.h" - -#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - -// static -std::unique_ptr IFXCRT_FileAccess::Create() { - return pdfium::MakeUnique(); -} - -void FXCRT_Windows_GetFileMode(uint32_t dwMode, - uint32_t& dwAccess, - uint32_t& dwShare, - uint32_t& dwCreation) { - dwAccess = GENERIC_READ; - dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE; - if (!(dwMode & FX_FILEMODE_ReadOnly)) { - dwAccess |= GENERIC_WRITE; - dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS; - } else { - dwCreation = OPEN_EXISTING; - } -} - -#ifdef __cplusplus -extern "C" { -#endif -WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize); -WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, - LARGE_INTEGER liDistanceToMove, - PLARGE_INTEGER lpNewFilePointer, - DWORD dwMoveMethod); -#ifdef __cplusplus -} -#endif - -CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64() : m_hFile(nullptr) {} - -CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64() { - Close(); -} - -bool CFXCRT_FileAccess_Win64::Open(const CFX_ByteStringC& fileName, - uint32_t dwMode) { - if (m_hFile) - return false; - - uint32_t dwAccess, dwShare, dwCreation; - FXCRT_Windows_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 CFXCRT_FileAccess_Win64::Open(const CFX_WideStringC& fileName, - uint32_t dwMode) { - if (m_hFile) - return false; - - uint32_t dwAccess, dwShare, dwCreation; - FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); - m_hFile = - ::CreateFileW((LPCWSTR)fileName.unterminated_c_str(), dwAccess, dwShare, - nullptr, dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr); - if (m_hFile == INVALID_HANDLE_VALUE) - m_hFile = nullptr; - - return !!m_hFile; -} - -void CFXCRT_FileAccess_Win64::Close() { - if (!m_hFile) - return; - - ::CloseHandle(m_hFile); - m_hFile = nullptr; -} - -FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const { - if (!m_hFile) - return 0; - - LARGE_INTEGER size = {}; - if (!::GetFileSizeEx(m_hFile, &size)) - return 0; - - return (FX_FILESIZE)size.QuadPart; -} - -FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const { - if (!m_hFile) - return (FX_FILESIZE)-1; - - LARGE_INTEGER dist = {}; - LARGE_INTEGER newPos = {}; - if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) - return (FX_FILESIZE)-1; - - return (FX_FILESIZE)newPos.QuadPart; -} - -FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos) { - if (!m_hFile) - return (FX_FILESIZE)-1; - - LARGE_INTEGER dist; - dist.QuadPart = pos; - LARGE_INTEGER newPos = {}; - if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) - return (FX_FILESIZE)-1; - - return (FX_FILESIZE)newPos.QuadPart; -} - -size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer) { - if (!m_hFile) - return 0; - - size_t szRead = 0; - if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, - nullptr)) { - return 0; - } - return szRead; -} - -size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer) { - if (!m_hFile) - return 0; - - size_t szWrite = 0; - if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, - nullptr)) { - return 0; - } - return szWrite; -} - -size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, - size_t szBuffer, - FX_FILESIZE pos) { - if (!m_hFile) - return 0; - - if (pos >= GetSize()) - return 0; - - if (SetPosition(pos) == (FX_FILESIZE)-1) - return 0; - - return Read(pBuffer, szBuffer); -} - -size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, - size_t szBuffer, - FX_FILESIZE pos) { - if (!m_hFile) { - return 0; - } - if (SetPosition(pos) == (FX_FILESIZE)-1) { - return 0; - } - return Write(pBuffer, szBuffer); -} - -bool CFXCRT_FileAccess_Win64::Flush() { - if (!m_hFile) - return false; - - return !!::FlushFileBuffers(m_hFile); -} - -bool CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile) { - if (SetPosition(szFile) == (FX_FILESIZE)-1) - return false; - - return !!::SetEndOfFile(m_hFile); -} -#endif diff --git a/core/fxcrt/fxcrt_windows.h b/core/fxcrt/fxcrt_windows.h deleted file mode 100644 index 8e800ce4d6..0000000000 --- a/core/fxcrt/fxcrt_windows.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXCRT_FXCRT_WINDOWS_H_ -#define CORE_FXCRT_FXCRT_WINDOWS_H_ - -#include "core/fxcrt/ifxcrt_fileaccess.h" - -#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ -class CFXCRT_FileAccess_Win64 : public IFXCRT_FileAccess { - public: - CFXCRT_FileAccess_Win64(); - ~CFXCRT_FileAccess_Win64() override; - - // IFXCRT_FileAccess - bool Open(const CFX_ByteStringC& fileName, uint32_t dwMode) override; - bool Open(const CFX_WideStringC& fileName, uint32_t dwMode) override; - void Close() override; - FX_FILESIZE GetSize() const override; - FX_FILESIZE GetPosition() const override; - FX_FILESIZE SetPosition(FX_FILESIZE pos) override; - size_t Read(void* pBuffer, size_t szBuffer) override; - size_t Write(const void* pBuffer, size_t szBuffer) override; - size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) override; - size_t WritePos(const void* pBuffer, - size_t szBuffer, - FX_FILESIZE pos) override; - bool Flush() override; - bool Truncate(FX_FILESIZE szFile) override; - - protected: - void* m_hFile; -}; -#endif - -#endif // CORE_FXCRT_FXCRT_WINDOWS_H_ diff --git a/core/fxcrt/ifx_fileaccess.h b/core/fxcrt/ifx_fileaccess.h new file mode 100644 index 0000000000..e818303d85 --- /dev/null +++ b/core/fxcrt/ifx_fileaccess.h @@ -0,0 +1,38 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_IFX_FILEACCESS_H_ +#define CORE_FXCRT_IFX_FILEACCESS_H_ + +#include +#include + +#include "core/fxcrt/fx_safe_types.h" +#include "core/fxcrt/fx_stream.h" +#include "core/fxcrt/fx_string.h" + +class IFX_FileAccess { + public: + static std::unique_ptr Create(); + virtual ~IFX_FileAccess() {} + + virtual bool Open(const CFX_ByteStringC& fileName, uint32_t dwMode) = 0; + virtual bool Open(const CFX_WideStringC& fileName, uint32_t dwMode) = 0; + virtual void Close() = 0; + virtual FX_FILESIZE GetSize() const = 0; + virtual FX_FILESIZE GetPosition() const = 0; + virtual FX_FILESIZE SetPosition(FX_FILESIZE pos) = 0; + virtual size_t Read(void* pBuffer, size_t szBuffer) = 0; + virtual size_t Write(const void* pBuffer, size_t szBuffer) = 0; + virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) = 0; + virtual size_t WritePos(const void* pBuffer, + size_t szBuffer, + FX_FILESIZE pos) = 0; + virtual bool Flush() = 0; + virtual bool Truncate(FX_FILESIZE szFile) = 0; +}; + +#endif // CORE_FXCRT_IFX_FILEACCESS_H_ diff --git a/core/fxcrt/ifxcrt_fileaccess.h b/core/fxcrt/ifxcrt_fileaccess.h deleted file mode 100644 index f4c686abd5..0000000000 --- a/core/fxcrt/ifxcrt_fileaccess.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXCRT_IFXCRT_FILEACCESS_H_ -#define CORE_FXCRT_IFXCRT_FILEACCESS_H_ - -#include -#include - -#include "core/fxcrt/fx_safe_types.h" -#include "core/fxcrt/fx_stream.h" -#include "core/fxcrt/fx_string.h" - -class IFXCRT_FileAccess { - public: - static std::unique_ptr Create(); - virtual ~IFXCRT_FileAccess() {} - - virtual bool Open(const CFX_ByteStringC& fileName, uint32_t dwMode) = 0; - virtual bool Open(const CFX_WideStringC& fileName, uint32_t dwMode) = 0; - virtual void Close() = 0; - virtual FX_FILESIZE GetSize() const = 0; - virtual FX_FILESIZE GetPosition() const = 0; - virtual FX_FILESIZE SetPosition(FX_FILESIZE pos) = 0; - virtual size_t Read(void* pBuffer, size_t szBuffer) = 0; - virtual size_t Write(const void* pBuffer, size_t szBuffer) = 0; - virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) = 0; - virtual size_t WritePos(const void* pBuffer, - size_t szBuffer, - FX_FILESIZE pos) = 0; - virtual bool Flush() = 0; - virtual bool Truncate(FX_FILESIZE szFile) = 0; -}; - -#endif // CORE_FXCRT_IFXCRT_FILEACCESS_H_ -- cgit v1.2.3