• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "../../include/fxcrt/fx_ext.h"
8 #include "fxcrt_windows.h"
9 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
FX_File_Exist(FX_BSTR fileName)10 FX_BOOL FX_File_Exist(FX_BSTR fileName)
11 {
12     FX_DWORD dwAttri = ::GetFileAttributesA(fileName.GetCStr());
13     if (dwAttri == -1) {
14         return FALSE;
15     }
16     return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0;
17 }
FX_File_Exist(FX_WSTR fileName)18 FX_BOOL FX_File_Exist(FX_WSTR fileName)
19 {
20     FX_DWORD dwAttri = ::GetFileAttributesW((LPCWSTR)fileName.GetPtr());
21     if (dwAttri == -1) {
22         return FALSE;
23     }
24     return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0;
25 }
FXCRT_FileAccess_Create(IFX_Allocator * pAllocator)26 IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator)
27 {
28     if (pAllocator) {
29         return FX_NewAtAllocator(pAllocator) CFXCRT_FileAccess_Win64;
30     } else {
31         return FX_NEW CFXCRT_FileAccess_Win64;
32     }
33 }
FXCRT_Windows_GetFileMode(FX_DWORD dwMode,FX_DWORD & dwAccess,FX_DWORD & dwShare,FX_DWORD & dwCreation)34 void FXCRT_Windows_GetFileMode(FX_DWORD dwMode, FX_DWORD &dwAccess, FX_DWORD &dwShare, FX_DWORD &dwCreation)
35 {
36     dwAccess = GENERIC_READ;
37     dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
38     if (!(dwMode & FX_FILEMODE_ReadOnly)) {
39         dwAccess |= GENERIC_WRITE;
40         dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS;
41     } else {
42         dwCreation = OPEN_EXISTING;
43     }
44 }
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize);
49 WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod);
50 #ifdef __cplusplus
51 }
52 #endif
CFXCRT_FileAccess_Win64()53 CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64()
54     : m_hFile(NULL)
55 {
56 }
~CFXCRT_FileAccess_Win64()57 CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64()
58 {
59     Close();
60 }
Open(FX_BSTR fileName,FX_DWORD dwMode)61 FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_BSTR fileName, FX_DWORD dwMode)
62 {
63     if (m_hFile) {
64         return FALSE;
65     }
66     FX_DWORD dwAccess, dwShare, dwCreation;
67     FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
68     m_hFile = ::CreateFileA(fileName.GetCStr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL);
69     if (m_hFile == INVALID_HANDLE_VALUE) {
70         m_hFile = NULL;
71     }
72     return m_hFile != NULL;
73 }
Open(FX_WSTR fileName,FX_DWORD dwMode)74 FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_WSTR fileName, FX_DWORD dwMode)
75 {
76     if (m_hFile) {
77         return FALSE;
78     }
79     FX_DWORD dwAccess, dwShare, dwCreation;
80     FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
81     m_hFile = ::CreateFileW((LPCWSTR)fileName.GetPtr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL);
82     if (m_hFile == INVALID_HANDLE_VALUE) {
83         m_hFile = NULL;
84     }
85     return m_hFile != NULL;
86 }
Close()87 void CFXCRT_FileAccess_Win64::Close()
88 {
89     if (!m_hFile) {
90         return;
91     }
92     ::CloseHandle(m_hFile);
93     m_hFile = NULL;
94 }
Release(IFX_Allocator * pAllocator)95 void CFXCRT_FileAccess_Win64::Release(IFX_Allocator* pAllocator)
96 {
97     if (pAllocator) {
98         FX_DeleteAtAllocator(this, pAllocator, CFXCRT_FileAccess_Win64);
99     } else {
100         delete this;
101     }
102 }
GetSize() const103 FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const
104 {
105     if (!m_hFile) {
106         return 0;
107     }
108     LARGE_INTEGER size = {0, 0};
109     if (!::GetFileSizeEx(m_hFile, &size)) {
110         return 0;
111     }
112     return (FX_FILESIZE)size.QuadPart;
113 }
GetPosition() const114 FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const
115 {
116     if (!m_hFile) {
117         return (FX_FILESIZE) - 1;
118     }
119     LARGE_INTEGER dist = {0, 0};
120     LARGE_INTEGER newPos = {0, 0};
121     if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) {
122         return (FX_FILESIZE) - 1;
123     }
124     return (FX_FILESIZE)newPos.QuadPart;
125 }
SetPosition(FX_FILESIZE pos)126 FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos)
127 {
128     if (!m_hFile) {
129         return (FX_FILESIZE) - 1;
130     }
131     LARGE_INTEGER dist;
132     dist.QuadPart = pos;
133     LARGE_INTEGER newPos = {0, 0};
134     if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) {
135         return (FX_FILESIZE) - 1;
136     }
137     return (FX_FILESIZE)newPos.QuadPart;
138 }
Read(void * pBuffer,size_t szBuffer)139 size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer)
140 {
141     if (!m_hFile) {
142         return 0;
143     }
144     size_t szRead = 0;
145     if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, NULL)) {
146         return 0;
147     }
148     return szRead;
149 }
Write(const void * pBuffer,size_t szBuffer)150 size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer)
151 {
152     if (!m_hFile) {
153         return 0;
154     }
155     size_t szWrite = 0;
156     if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, NULL)) {
157         return 0;
158     }
159     return szWrite;
160 }
ReadPos(void * pBuffer,size_t szBuffer,FX_FILESIZE pos)161 size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
162 {
163     if (!m_hFile) {
164         return 0;
165     }
166     if (pos >= GetSize()) {
167         return 0;
168     }
169     if (SetPosition(pos) == (FX_FILESIZE) - 1) {
170         return 0;
171     }
172     return Read(pBuffer, szBuffer);
173 }
WritePos(const void * pBuffer,size_t szBuffer,FX_FILESIZE pos)174 size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
175 {
176     if (!m_hFile) {
177         return 0;
178     }
179     if (SetPosition(pos) == (FX_FILESIZE) - 1) {
180         return 0;
181     }
182     return Write(pBuffer, szBuffer);
183 }
Flush()184 FX_BOOL CFXCRT_FileAccess_Win64::Flush()
185 {
186     if (!m_hFile) {
187         return FALSE;
188     }
189     return ::FlushFileBuffers(m_hFile);
190 }
Truncate(FX_FILESIZE szFile)191 FX_BOOL CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile)
192 {
193     if (SetPosition(szFile) == (FX_FILESIZE) - 1) {
194         return FALSE;
195     }
196     return ::SetEndOfFile(m_hFile);
197 }
FX_File_Delete(FX_BSTR fileName)198 FX_BOOL FX_File_Delete(FX_BSTR fileName)
199 {
200     return ::DeleteFileA(fileName.GetCStr());
201 }
FX_File_Delete(FX_WSTR fileName)202 FX_BOOL FX_File_Delete(FX_WSTR fileName)
203 {
204     return ::DeleteFileW((LPCWSTR)fileName.GetPtr());
205 }
FX_File_Copy(FX_BSTR fileNameSrc,FX_BSTR fileNameDst)206 FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
207 {
208     return ::CopyFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr(), FALSE);
209 }
FX_File_Copy(FX_WSTR fileNameSrc,FX_WSTR fileNameDst)210 FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
211 {
212     return ::CopyFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr(), FALSE);
213 }
FX_File_Move(FX_BSTR fileNameSrc,FX_BSTR fileNameDst)214 FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
215 {
216     return ::MoveFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr());
217 }
FX_File_Move(FX_WSTR fileNameSrc,FX_WSTR fileNameDst)218 FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
219 {
220     return ::MoveFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr());
221 }
222 #endif
223