OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fxcrt/fxcrt_windows.h" | 7 #include "core/fxcrt/fxcrt_windows.h" |
8 | 8 |
9 #include "core/fxcrt/fx_string.h" | 9 #include "core/fxcrt/fx_string.h" |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 uint32_t& dwCreation) { | 21 uint32_t& dwCreation) { |
22 dwAccess = GENERIC_READ; | 22 dwAccess = GENERIC_READ; |
23 dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE; | 23 dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE; |
24 if (!(dwMode & FX_FILEMODE_ReadOnly)) { | 24 if (!(dwMode & FX_FILEMODE_ReadOnly)) { |
25 dwAccess |= GENERIC_WRITE; | 25 dwAccess |= GENERIC_WRITE; |
26 dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS; | 26 dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS; |
27 } else { | 27 } else { |
28 dwCreation = OPEN_EXISTING; | 28 dwCreation = OPEN_EXISTING; |
29 } | 29 } |
30 } | 30 } |
| 31 |
31 #ifdef __cplusplus | 32 #ifdef __cplusplus |
32 extern "C" { | 33 extern "C" { |
33 #endif | 34 #endif |
34 WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize); | 35 WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize); |
35 WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, | 36 WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, |
36 LARGE_INTEGER liDistanceToMove, | 37 LARGE_INTEGER liDistanceToMove, |
37 PLARGE_INTEGER lpNewFilePointer, | 38 PLARGE_INTEGER lpNewFilePointer, |
38 DWORD dwMoveMethod); | 39 DWORD dwMoveMethod); |
39 #ifdef __cplusplus | 40 #ifdef __cplusplus |
40 } | 41 } |
41 #endif | 42 #endif |
| 43 |
42 CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64() : m_hFile(nullptr) {} | 44 CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64() : m_hFile(nullptr) {} |
| 45 |
43 CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64() { | 46 CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64() { |
44 Close(); | 47 Close(); |
45 } | 48 } |
46 FX_BOOL CFXCRT_FileAccess_Win64::Open(const CFX_ByteStringC& fileName, | 49 |
47 uint32_t dwMode) { | 50 bool CFXCRT_FileAccess_Win64::Open(const CFX_ByteStringC& fileName, |
48 if (m_hFile) { | 51 uint32_t dwMode) { |
| 52 if (m_hFile) |
49 return FALSE; | 53 return FALSE; |
50 } | 54 |
51 uint32_t dwAccess, dwShare, dwCreation; | 55 uint32_t dwAccess, dwShare, dwCreation; |
52 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); | 56 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); |
53 m_hFile = ::CreateFileA(fileName.c_str(), dwAccess, dwShare, nullptr, | 57 m_hFile = ::CreateFileA(fileName.c_str(), dwAccess, dwShare, nullptr, |
54 dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr); | 58 dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr); |
55 if (m_hFile == INVALID_HANDLE_VALUE) | 59 if (m_hFile == INVALID_HANDLE_VALUE) |
56 m_hFile = nullptr; | 60 m_hFile = nullptr; |
| 61 |
57 return !!m_hFile; | 62 return !!m_hFile; |
58 } | 63 } |
59 FX_BOOL CFXCRT_FileAccess_Win64::Open(const CFX_WideStringC& fileName, | 64 |
60 uint32_t dwMode) { | 65 bool CFXCRT_FileAccess_Win64::Open(const CFX_WideStringC& fileName, |
61 if (m_hFile) { | 66 uint32_t dwMode) { |
| 67 if (m_hFile) |
62 return FALSE; | 68 return FALSE; |
63 } | 69 |
64 uint32_t dwAccess, dwShare, dwCreation; | 70 uint32_t dwAccess, dwShare, dwCreation; |
65 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); | 71 FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); |
66 m_hFile = ::CreateFileW((LPCWSTR)fileName.c_str(), dwAccess, dwShare, nullptr, | 72 m_hFile = ::CreateFileW((LPCWSTR)fileName.c_str(), dwAccess, dwShare, nullptr, |
67 dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr); | 73 dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr); |
68 if (m_hFile == INVALID_HANDLE_VALUE) | 74 if (m_hFile == INVALID_HANDLE_VALUE) |
69 m_hFile = nullptr; | 75 m_hFile = nullptr; |
| 76 |
70 return !!m_hFile; | 77 return !!m_hFile; |
71 } | 78 } |
| 79 |
72 void CFXCRT_FileAccess_Win64::Close() { | 80 void CFXCRT_FileAccess_Win64::Close() { |
73 if (!m_hFile) { | 81 if (!m_hFile) |
74 return; | 82 return; |
75 } | 83 |
76 ::CloseHandle(m_hFile); | 84 ::CloseHandle(m_hFile); |
77 m_hFile = nullptr; | 85 m_hFile = nullptr; |
78 } | 86 } |
| 87 |
79 FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const { | 88 FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const { |
80 if (!m_hFile) { | 89 if (!m_hFile) |
81 return 0; | 90 return 0; |
82 } | 91 |
83 LARGE_INTEGER size = {}; | 92 LARGE_INTEGER size = {}; |
84 if (!::GetFileSizeEx(m_hFile, &size)) { | 93 if (!::GetFileSizeEx(m_hFile, &size)) |
85 return 0; | 94 return 0; |
86 } | 95 |
87 return (FX_FILESIZE)size.QuadPart; | 96 return (FX_FILESIZE)size.QuadPart; |
88 } | 97 } |
| 98 |
89 FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const { | 99 FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const { |
90 if (!m_hFile) { | 100 if (!m_hFile) |
91 return (FX_FILESIZE)-1; | 101 return (FX_FILESIZE)-1; |
92 } | 102 |
93 LARGE_INTEGER dist = {}; | 103 LARGE_INTEGER dist = {}; |
94 LARGE_INTEGER newPos = {}; | 104 LARGE_INTEGER newPos = {}; |
95 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) { | 105 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) |
96 return (FX_FILESIZE)-1; | 106 return (FX_FILESIZE)-1; |
97 } | 107 |
98 return (FX_FILESIZE)newPos.QuadPart; | 108 return (FX_FILESIZE)newPos.QuadPart; |
99 } | 109 } |
| 110 |
100 FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos) { | 111 FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos) { |
101 if (!m_hFile) { | 112 if (!m_hFile) |
102 return (FX_FILESIZE)-1; | 113 return (FX_FILESIZE)-1; |
103 } | 114 |
104 LARGE_INTEGER dist; | 115 LARGE_INTEGER dist; |
105 dist.QuadPart = pos; | 116 dist.QuadPart = pos; |
106 LARGE_INTEGER newPos = {}; | 117 LARGE_INTEGER newPos = {}; |
107 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) { | 118 if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) |
108 return (FX_FILESIZE)-1; | 119 return (FX_FILESIZE)-1; |
109 } | 120 |
110 return (FX_FILESIZE)newPos.QuadPart; | 121 return (FX_FILESIZE)newPos.QuadPart; |
111 } | 122 } |
| 123 |
112 size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer) { | 124 size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer) { |
113 if (!m_hFile) { | 125 if (!m_hFile) |
114 return 0; | 126 return 0; |
115 } | 127 |
116 size_t szRead = 0; | 128 size_t szRead = 0; |
117 if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, | 129 if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, |
118 nullptr)) { | 130 nullptr)) { |
119 return 0; | 131 return 0; |
120 } | 132 } |
121 return szRead; | 133 return szRead; |
122 } | 134 } |
| 135 |
123 size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer) { | 136 size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer) { |
124 if (!m_hFile) { | 137 if (!m_hFile) |
125 return 0; | 138 return 0; |
126 } | 139 |
127 size_t szWrite = 0; | 140 size_t szWrite = 0; |
128 if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, | 141 if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, |
129 nullptr)) { | 142 nullptr)) { |
130 return 0; | 143 return 0; |
131 } | 144 } |
132 return szWrite; | 145 return szWrite; |
133 } | 146 } |
| 147 |
134 size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, | 148 size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, |
135 size_t szBuffer, | 149 size_t szBuffer, |
136 FX_FILESIZE pos) { | 150 FX_FILESIZE pos) { |
137 if (!m_hFile) { | 151 if (!m_hFile) |
138 return 0; | 152 return 0; |
139 } | 153 |
140 if (pos >= GetSize()) { | 154 if (pos >= GetSize()) |
141 return 0; | 155 return 0; |
142 } | 156 |
143 if (SetPosition(pos) == (FX_FILESIZE)-1) { | 157 if (SetPosition(pos) == (FX_FILESIZE)-1) |
144 return 0; | 158 return 0; |
145 } | 159 |
146 return Read(pBuffer, szBuffer); | 160 return Read(pBuffer, szBuffer); |
147 } | 161 } |
| 162 |
148 size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, | 163 size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, |
149 size_t szBuffer, | 164 size_t szBuffer, |
150 FX_FILESIZE pos) { | 165 FX_FILESIZE pos) { |
151 if (!m_hFile) { | 166 if (!m_hFile) { |
152 return 0; | 167 return 0; |
153 } | 168 } |
154 if (SetPosition(pos) == (FX_FILESIZE)-1) { | 169 if (SetPosition(pos) == (FX_FILESIZE)-1) { |
155 return 0; | 170 return 0; |
156 } | 171 } |
157 return Write(pBuffer, szBuffer); | 172 return Write(pBuffer, szBuffer); |
158 } | 173 } |
159 FX_BOOL CFXCRT_FileAccess_Win64::Flush() { | 174 |
160 if (!m_hFile) { | 175 bool CFXCRT_FileAccess_Win64::Flush() { |
161 return FALSE; | 176 if (!m_hFile) |
162 } | 177 return false; |
163 return ::FlushFileBuffers(m_hFile); | 178 |
| 179 return !!::FlushFileBuffers(m_hFile); |
164 } | 180 } |
165 FX_BOOL CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile) { | 181 |
166 if (SetPosition(szFile) == (FX_FILESIZE)-1) { | 182 bool CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile) { |
167 return FALSE; | 183 if (SetPosition(szFile) == (FX_FILESIZE)-1) |
168 } | 184 return false; |
169 return ::SetEndOfFile(m_hFile); | 185 |
| 186 return !!::SetEndOfFile(m_hFile); |
170 } | 187 } |
171 #endif | 188 #endif |
OLD | NEW |