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()26 IFXCRT_FileAccess* FXCRT_FileAccess_Create()
27 {
28 return new CFXCRT_FileAccess_Win64;
29 }
FXCRT_Windows_GetFileMode(FX_DWORD dwMode,FX_DWORD & dwAccess,FX_DWORD & dwShare,FX_DWORD & dwCreation)30 void FXCRT_Windows_GetFileMode(FX_DWORD dwMode, FX_DWORD &dwAccess, FX_DWORD &dwShare, FX_DWORD &dwCreation)
31 {
32 dwAccess = GENERIC_READ;
33 dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
34 if (!(dwMode & FX_FILEMODE_ReadOnly)) {
35 dwAccess |= GENERIC_WRITE;
36 dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS;
37 } else {
38 dwCreation = OPEN_EXISTING;
39 }
40 }
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize);
45 WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod);
46 #ifdef __cplusplus
47 }
48 #endif
CFXCRT_FileAccess_Win64()49 CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64()
50 : m_hFile(NULL)
51 {
52 }
~CFXCRT_FileAccess_Win64()53 CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64()
54 {
55 Close();
56 }
Open(FX_BSTR fileName,FX_DWORD dwMode)57 FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_BSTR fileName, FX_DWORD dwMode)
58 {
59 if (m_hFile) {
60 return FALSE;
61 }
62 FX_DWORD dwAccess, dwShare, dwCreation;
63 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
64 m_hFile = ::CreateFileA(fileName.GetCStr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL);
65 if (m_hFile == INVALID_HANDLE_VALUE) {
66 m_hFile = NULL;
67 }
68 return m_hFile != NULL;
69 }
Open(FX_WSTR fileName,FX_DWORD dwMode)70 FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_WSTR fileName, FX_DWORD dwMode)
71 {
72 if (m_hFile) {
73 return FALSE;
74 }
75 FX_DWORD dwAccess, dwShare, dwCreation;
76 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
77 m_hFile = ::CreateFileW((LPCWSTR)fileName.GetPtr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL);
78 if (m_hFile == INVALID_HANDLE_VALUE) {
79 m_hFile = NULL;
80 }
81 return m_hFile != NULL;
82 }
Close()83 void CFXCRT_FileAccess_Win64::Close()
84 {
85 if (!m_hFile) {
86 return;
87 }
88 ::CloseHandle(m_hFile);
89 m_hFile = NULL;
90 }
Release()91 void CFXCRT_FileAccess_Win64::Release()
92 {
93 delete this;
94 }
GetSize() const95 FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const
96 {
97 if (!m_hFile) {
98 return 0;
99 }
100 LARGE_INTEGER size = {0, 0};
101 if (!::GetFileSizeEx(m_hFile, &size)) {
102 return 0;
103 }
104 return (FX_FILESIZE)size.QuadPart;
105 }
GetPosition() const106 FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const
107 {
108 if (!m_hFile) {
109 return (FX_FILESIZE) - 1;
110 }
111 LARGE_INTEGER dist = {0, 0};
112 LARGE_INTEGER newPos = {0, 0};
113 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) {
114 return (FX_FILESIZE) - 1;
115 }
116 return (FX_FILESIZE)newPos.QuadPart;
117 }
SetPosition(FX_FILESIZE pos)118 FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos)
119 {
120 if (!m_hFile) {
121 return (FX_FILESIZE) - 1;
122 }
123 LARGE_INTEGER dist;
124 dist.QuadPart = pos;
125 LARGE_INTEGER newPos = {0, 0};
126 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) {
127 return (FX_FILESIZE) - 1;
128 }
129 return (FX_FILESIZE)newPos.QuadPart;
130 }
Read(void * pBuffer,size_t szBuffer)131 size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer)
132 {
133 if (!m_hFile) {
134 return 0;
135 }
136 size_t szRead = 0;
137 if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, NULL)) {
138 return 0;
139 }
140 return szRead;
141 }
Write(const void * pBuffer,size_t szBuffer)142 size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer)
143 {
144 if (!m_hFile) {
145 return 0;
146 }
147 size_t szWrite = 0;
148 if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, NULL)) {
149 return 0;
150 }
151 return szWrite;
152 }
ReadPos(void * pBuffer,size_t szBuffer,FX_FILESIZE pos)153 size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
154 {
155 if (!m_hFile) {
156 return 0;
157 }
158 if (pos >= GetSize()) {
159 return 0;
160 }
161 if (SetPosition(pos) == (FX_FILESIZE) - 1) {
162 return 0;
163 }
164 return Read(pBuffer, szBuffer);
165 }
WritePos(const void * pBuffer,size_t szBuffer,FX_FILESIZE pos)166 size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
167 {
168 if (!m_hFile) {
169 return 0;
170 }
171 if (SetPosition(pos) == (FX_FILESIZE) - 1) {
172 return 0;
173 }
174 return Write(pBuffer, szBuffer);
175 }
Flush()176 FX_BOOL CFXCRT_FileAccess_Win64::Flush()
177 {
178 if (!m_hFile) {
179 return FALSE;
180 }
181 return ::FlushFileBuffers(m_hFile);
182 }
Truncate(FX_FILESIZE szFile)183 FX_BOOL CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile)
184 {
185 if (SetPosition(szFile) == (FX_FILESIZE) - 1) {
186 return FALSE;
187 }
188 return ::SetEndOfFile(m_hFile);
189 }
FX_File_Delete(FX_BSTR fileName)190 FX_BOOL FX_File_Delete(FX_BSTR fileName)
191 {
192 return ::DeleteFileA(fileName.GetCStr());
193 }
FX_File_Delete(FX_WSTR fileName)194 FX_BOOL FX_File_Delete(FX_WSTR fileName)
195 {
196 return ::DeleteFileW((LPCWSTR)fileName.GetPtr());
197 }
FX_File_Copy(FX_BSTR fileNameSrc,FX_BSTR fileNameDst)198 FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
199 {
200 return ::CopyFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr(), FALSE);
201 }
FX_File_Copy(FX_WSTR fileNameSrc,FX_WSTR fileNameDst)202 FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
203 {
204 return ::CopyFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr(), FALSE);
205 }
FX_File_Move(FX_BSTR fileNameSrc,FX_BSTR fileNameDst)206 FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
207 {
208 return ::MoveFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr());
209 }
FX_File_Move(FX_WSTR fileNameSrc,FX_WSTR fileNameDst)210 FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
211 {
212 return ::MoveFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr());
213 }
214 #endif
215