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 "xfa/fgas/crt/fgas_stream.h" | 7 #include "xfa/fgas/crt/fgas_stream.h" |
8 | 8 |
9 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \ | 9 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \ |
10 _FX_OS_ == _FX_WIN64_ | 10 _FX_OS_ == _FX_WIN64_ |
11 #include <io.h> | 11 #include <io.h> |
12 #endif | 12 #endif |
13 | 13 |
14 #include <algorithm> | 14 #include <algorithm> |
15 #include <memory> | 15 #include <memory> |
16 | 16 |
| 17 #include "third_party/base/ptr_util.h" |
17 #include "xfa/fgas/crt/fgas_codepage.h" | 18 #include "xfa/fgas/crt/fgas_codepage.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 class IFGAS_StreamImp { | 22 class IFGAS_StreamImp { |
22 public: | 23 public: |
23 virtual ~IFGAS_StreamImp() {} | 24 virtual ~IFGAS_StreamImp() {} |
24 | 25 |
25 virtual int32_t GetLength() const = 0; | 26 virtual int32_t GetLength() const = 0; |
26 virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) = 0; | 27 virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) = 0; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 int32_t m_iTotalSize; | 94 int32_t m_iTotalSize; |
94 int32_t m_iPosition; | 95 int32_t m_iPosition; |
95 int32_t m_iLength; | 96 int32_t m_iLength; |
96 }; | 97 }; |
97 | 98 |
98 class CFGAS_FileReadStreamImp : public IFGAS_StreamImp { | 99 class CFGAS_FileReadStreamImp : public IFGAS_StreamImp { |
99 public: | 100 public: |
100 CFGAS_FileReadStreamImp(); | 101 CFGAS_FileReadStreamImp(); |
101 ~CFGAS_FileReadStreamImp() override {} | 102 ~CFGAS_FileReadStreamImp() override {} |
102 | 103 |
103 bool LoadFileRead(IFX_SeekableReadStream* pFileRead, uint32_t dwAccess); | 104 bool LoadFileRead(const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, |
| 105 uint32_t dwAccess); |
104 | 106 |
105 // IFGAS_StreamImp: | 107 // IFGAS_StreamImp: |
106 int32_t GetLength() const override; | 108 int32_t GetLength() const override; |
107 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; | 109 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; |
108 int32_t GetPosition() override { return m_iPosition; } | 110 int32_t GetPosition() override { return m_iPosition; } |
109 bool IsEOF() const override; | 111 bool IsEOF() const override; |
110 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; | 112 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; |
111 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override; | 113 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override; |
112 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override { | 114 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override { |
113 return 0; | 115 return 0; |
114 } | 116 } |
115 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override { | 117 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override { |
116 return 0; | 118 return 0; |
117 } | 119 } |
118 void Flush() override {} | 120 void Flush() override {} |
119 bool SetLength(int32_t iLength) override { return false; } | 121 bool SetLength(int32_t iLength) override { return false; } |
120 | 122 |
121 protected: | 123 protected: |
122 IFX_SeekableReadStream* m_pFileRead; | 124 CFX_RetainPtr<IFX_SeekableReadStream> m_pFileRead; |
123 int32_t m_iPosition; | 125 int32_t m_iPosition; |
124 int32_t m_iLength; | 126 int32_t m_iLength; |
125 }; | 127 }; |
126 | 128 |
127 class CFGAS_BufferReadStreamImp : public IFGAS_StreamImp { | 129 class CFGAS_BufferReadStreamImp : public IFGAS_StreamImp { |
128 public: | 130 public: |
129 CFGAS_BufferReadStreamImp(); | 131 CFGAS_BufferReadStreamImp(); |
130 ~CFGAS_BufferReadStreamImp() override; | 132 ~CFGAS_BufferReadStreamImp() override; |
131 | 133 |
132 bool LoadBufferRead(IFX_BufferedReadStream* pBufferRead, | 134 bool LoadBufferRead(const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, |
133 int32_t iFileSize, | 135 int32_t iFileSize, |
134 uint32_t dwAccess, | 136 uint32_t dwAccess); |
135 bool bReleaseBufferRead); | |
136 | 137 |
137 // IFGAS_StreamImp: | 138 // IFGAS_StreamImp: |
138 int32_t GetLength() const override; | 139 int32_t GetLength() const override; |
139 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; | 140 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; |
140 int32_t GetPosition() override { return m_iPosition; } | 141 int32_t GetPosition() override { return m_iPosition; } |
141 bool IsEOF() const override; | 142 bool IsEOF() const override; |
142 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; | 143 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; |
143 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override; | 144 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override; |
144 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override { | 145 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override { |
145 return 0; | 146 return 0; |
146 } | 147 } |
147 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override { | 148 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override { |
148 return 0; | 149 return 0; |
149 } | 150 } |
150 void Flush() override {} | 151 void Flush() override {} |
151 bool SetLength(int32_t iLength) override { return false; } | 152 bool SetLength(int32_t iLength) override { return false; } |
152 | 153 |
153 private: | 154 private: |
154 IFX_BufferedReadStream* m_pBufferRead; | 155 CFX_RetainPtr<IFX_BufferedReadStream> m_pBufferRead; |
155 bool m_bReleaseBufferRead; | |
156 int32_t m_iPosition; | 156 int32_t m_iPosition; |
157 int32_t m_iBufferSize; | 157 int32_t m_iBufferSize; |
158 }; | 158 }; |
159 | 159 |
160 class CFGAS_FileWriteStreamImp : public IFGAS_StreamImp { | 160 class CFGAS_FileWriteStreamImp : public IFGAS_StreamImp { |
161 public: | 161 public: |
162 CFGAS_FileWriteStreamImp(); | 162 CFGAS_FileWriteStreamImp(); |
163 ~CFGAS_FileWriteStreamImp() override {} | 163 ~CFGAS_FileWriteStreamImp() override {} |
164 | 164 |
165 bool LoadFileWrite(IFX_SeekableWriteStream* pFileWrite, uint32_t dwAccess); | 165 bool LoadFileWrite(const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, |
| 166 uint32_t dwAccess); |
166 | 167 |
167 // IFGAS_StreamImp: | 168 // IFGAS_StreamImp: |
168 int32_t GetLength() const override; | 169 int32_t GetLength() const override; |
169 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; | 170 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; |
170 int32_t GetPosition() override { return m_iPosition; } | 171 int32_t GetPosition() override { return m_iPosition; } |
171 bool IsEOF() const override; | 172 bool IsEOF() const override; |
172 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override { return 0; } | 173 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override { return 0; } |
173 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override { | 174 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override { |
174 return 0; | 175 return 0; |
175 } | 176 } |
176 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; | 177 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; |
177 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; | 178 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; |
178 void Flush() override; | 179 void Flush() override; |
179 bool SetLength(int32_t iLength) override { return false; } | 180 bool SetLength(int32_t iLength) override { return false; } |
180 | 181 |
181 protected: | 182 protected: |
182 IFX_SeekableWriteStream* m_pFileWrite; | 183 CFX_RetainPtr<IFX_SeekableWriteStream> m_pFileWrite; |
183 int32_t m_iPosition; | 184 int32_t m_iPosition; |
184 }; | 185 }; |
185 | 186 |
186 enum FX_STREAMTYPE { | 187 enum FX_STREAMTYPE { |
187 FX_SREAMTYPE_Unknown = 0, | 188 FX_SREAMTYPE_Unknown = 0, |
188 FX_STREAMTYPE_File, | 189 FX_STREAMTYPE_File, |
189 FX_STREAMTYPE_Buffer, | 190 FX_STREAMTYPE_Buffer, |
190 FX_STREAMTYPE_Stream, | 191 FX_STREAMTYPE_Stream, |
191 FX_STREAMTYPE_BufferRead, | 192 FX_STREAMTYPE_BufferRead, |
192 }; | 193 }; |
193 | 194 |
194 class CFGAS_Stream : public IFGAS_Stream { | 195 class CFGAS_Stream : public IFGAS_Stream { |
195 public: | 196 public: |
196 CFGAS_Stream(); | 197 CFGAS_Stream(); |
197 ~CFGAS_Stream() override; | 198 ~CFGAS_Stream() override; |
198 | 199 |
199 bool LoadFile(const FX_WCHAR* pszSrcFileName, uint32_t dwAccess); | 200 bool LoadFile(const FX_WCHAR* pszSrcFileName, uint32_t dwAccess); |
200 bool LoadBuffer(uint8_t* pData, int32_t iTotalSize, uint32_t dwAccess); | 201 bool LoadBuffer(uint8_t* pData, int32_t iTotalSize, uint32_t dwAccess); |
201 bool LoadFileRead(IFX_SeekableReadStream* pFileRead, uint32_t dwAccess); | 202 bool LoadFileRead(const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, |
202 bool LoadFileWrite(IFX_SeekableWriteStream* pFileWrite, uint32_t dwAccess); | 203 uint32_t dwAccess); |
203 bool LoadBufferRead(IFX_BufferedReadStream* pBufferRead, | 204 bool LoadFileWrite(const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, |
| 205 uint32_t dwAccess); |
| 206 bool LoadBufferRead(const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, |
204 int32_t iFileSize, | 207 int32_t iFileSize, |
205 uint32_t dwAccess, | 208 uint32_t dwAccess); |
206 bool bReleaseBufferRead); | |
207 | 209 |
208 // IFGAS_Stream | 210 // IFGAS_Stream |
209 void Release() override; | 211 void Release() override; |
210 IFGAS_Stream* Retain() override; | 212 IFGAS_Stream* Retain() override; |
211 uint32_t GetAccessModes() const override; | 213 uint32_t GetAccessModes() const override; |
212 int32_t GetLength() const override; | 214 int32_t GetLength() const override; |
213 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; | 215 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; |
214 int32_t GetPosition() override; | 216 int32_t GetPosition() override; |
215 bool IsEOF() const override; | 217 bool IsEOF() const override; |
216 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; | 218 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 uint8_t* m_pBuf; | 272 uint8_t* m_pBuf; |
271 int32_t m_iBufSize; | 273 int32_t m_iBufSize; |
272 bool m_bDelStream; | 274 bool m_bDelStream; |
273 IFGAS_Stream* m_pStreamImp; | 275 IFGAS_Stream* m_pStreamImp; |
274 int32_t m_iRefCount; | 276 int32_t m_iRefCount; |
275 void InitStream(); | 277 void InitStream(); |
276 }; | 278 }; |
277 | 279 |
278 class CFGAS_FileRead : public IFX_SeekableReadStream { | 280 class CFGAS_FileRead : public IFX_SeekableReadStream { |
279 public: | 281 public: |
280 CFGAS_FileRead(IFGAS_Stream* pStream, bool bReleaseStream); | 282 static CFX_RetainPtr<CFGAS_FileRead> Create(IFGAS_Stream* pStream, |
| 283 bool bReleaseStream); |
281 ~CFGAS_FileRead() override; | 284 ~CFGAS_FileRead() override; |
282 | 285 |
283 // IFX_SeekableReadStream | 286 // IFX_SeekableReadStream |
284 void Release() override; | |
285 FX_FILESIZE GetSize() override; | 287 FX_FILESIZE GetSize() override; |
286 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; | 288 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
287 | 289 |
288 protected: | 290 protected: |
| 291 CFGAS_FileRead(IFGAS_Stream* pStream, bool bReleaseStream); |
| 292 |
289 bool m_bReleaseStream; | 293 bool m_bReleaseStream; |
290 IFGAS_Stream* m_pStream; | 294 IFGAS_Stream* m_pStream; |
291 }; | 295 }; |
292 | 296 |
293 int32_t FileLength(FXSYS_FILE* file) { | 297 int32_t FileLength(FXSYS_FILE* file) { |
294 ASSERT(file); | 298 ASSERT(file); |
295 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_ | 299 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_ |
296 return _filelength(_fileno(file)); | 300 return _filelength(_fileno(file)); |
297 #else | 301 #else |
298 int32_t iPos = FXSYS_ftell(file); | 302 int32_t iPos = FXSYS_ftell(file); |
(...skipping 16 matching lines...) Expand all Loading... |
315 ::SetFilePointer(hFile, (int32_t)dwPos, 0, FILE_BEGIN); | 319 ::SetFilePointer(hFile, (int32_t)dwPos, 0, FILE_BEGIN); |
316 return bRet; | 320 return bRet; |
317 #else | 321 #else |
318 return false; | 322 return false; |
319 #endif | 323 #endif |
320 } | 324 } |
321 | 325 |
322 } // namespace | 326 } // namespace |
323 | 327 |
324 // static | 328 // static |
325 IFGAS_Stream* IFGAS_Stream::CreateStream(IFX_SeekableReadStream* pFileRead, | 329 IFGAS_Stream* IFGAS_Stream::CreateStream( |
326 uint32_t dwAccess) { | 330 const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, |
| 331 uint32_t dwAccess) { |
327 CFGAS_Stream* pSR = new CFGAS_Stream; | 332 CFGAS_Stream* pSR = new CFGAS_Stream; |
328 if (!pSR->LoadFileRead(pFileRead, dwAccess)) { | 333 if (!pSR->LoadFileRead(pFileRead, dwAccess)) { |
329 pSR->Release(); | 334 pSR->Release(); |
330 return nullptr; | 335 return nullptr; |
331 } | 336 } |
332 if (dwAccess & FX_STREAMACCESS_Text) | 337 if (dwAccess & FX_STREAMACCESS_Text) |
333 return new CFGAS_TextStream(pSR, true); | 338 return new CFGAS_TextStream(pSR, true); |
334 | 339 |
335 return pSR; | 340 return pSR; |
336 } | 341 } |
337 | 342 |
338 // static | 343 // static |
339 IFGAS_Stream* IFGAS_Stream::CreateStream(IFX_SeekableWriteStream* pFileWrite, | 344 IFGAS_Stream* IFGAS_Stream::CreateStream( |
340 uint32_t dwAccess) { | 345 const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, |
| 346 uint32_t dwAccess) { |
341 CFGAS_Stream* pSR = new CFGAS_Stream; | 347 CFGAS_Stream* pSR = new CFGAS_Stream; |
342 if (!pSR->LoadFileWrite(pFileWrite, dwAccess)) { | 348 if (!pSR->LoadFileWrite(pFileWrite, dwAccess)) { |
343 pSR->Release(); | 349 pSR->Release(); |
344 return nullptr; | 350 return nullptr; |
345 } | 351 } |
346 if (dwAccess & FX_STREAMACCESS_Text) | 352 if (dwAccess & FX_STREAMACCESS_Text) |
347 return new CFGAS_TextStream(pSR, true); | 353 return new CFGAS_TextStream(pSR, true); |
348 | 354 |
349 return pSR; | 355 return pSR; |
350 } | 356 } |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 void CFGAS_FileStreamImp::Flush() { | 533 void CFGAS_FileStreamImp::Flush() { |
528 ASSERT(m_hFile && (GetAccessModes() & FX_STREAMACCESS_Write) != 0); | 534 ASSERT(m_hFile && (GetAccessModes() & FX_STREAMACCESS_Write) != 0); |
529 FXSYS_fflush(m_hFile); | 535 FXSYS_fflush(m_hFile); |
530 } | 536 } |
531 bool CFGAS_FileStreamImp::SetLength(int32_t iLength) { | 537 bool CFGAS_FileStreamImp::SetLength(int32_t iLength) { |
532 ASSERT(m_hFile && (GetAccessModes() & FX_STREAMACCESS_Write) != 0); | 538 ASSERT(m_hFile && (GetAccessModes() & FX_STREAMACCESS_Write) != 0); |
533 bool bRet = FileSetSize(m_hFile, iLength); | 539 bool bRet = FileSetSize(m_hFile, iLength); |
534 m_iLength = FileLength(m_hFile); | 540 m_iLength = FileLength(m_hFile); |
535 return bRet; | 541 return bRet; |
536 } | 542 } |
| 543 |
537 CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp() | 544 CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp() |
538 : m_pFileRead(nullptr), m_iPosition(0), m_iLength(0) {} | 545 : m_pFileRead(nullptr), m_iPosition(0), m_iLength(0) {} |
539 bool CFGAS_FileReadStreamImp::LoadFileRead(IFX_SeekableReadStream* pFileRead, | 546 |
540 uint32_t dwAccess) { | 547 bool CFGAS_FileReadStreamImp::LoadFileRead( |
| 548 const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, |
| 549 uint32_t dwAccess) { |
541 ASSERT(!m_pFileRead && pFileRead); | 550 ASSERT(!m_pFileRead && pFileRead); |
542 if (dwAccess & FX_STREAMACCESS_Write) { | 551 if (dwAccess & FX_STREAMACCESS_Write) |
543 return false; | 552 return false; |
544 } | 553 |
545 m_pFileRead = pFileRead; | 554 m_pFileRead = pFileRead; |
546 m_iLength = m_pFileRead->GetSize(); | 555 m_iLength = m_pFileRead->GetSize(); |
547 return true; | 556 return true; |
548 } | 557 } |
| 558 |
549 int32_t CFGAS_FileReadStreamImp::GetLength() const { | 559 int32_t CFGAS_FileReadStreamImp::GetLength() const { |
550 return m_iLength; | 560 return m_iLength; |
551 } | 561 } |
552 int32_t CFGAS_FileReadStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { | 562 int32_t CFGAS_FileReadStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { |
553 switch (eSeek) { | 563 switch (eSeek) { |
554 case FX_STREAMSEEK_Begin: | 564 case FX_STREAMSEEK_Begin: |
555 m_iPosition = iOffset; | 565 m_iPosition = iOffset; |
556 break; | 566 break; |
557 case FX_STREAMSEEK_Current: | 567 case FX_STREAMSEEK_Current: |
558 m_iPosition += iOffset; | 568 m_iPosition += iOffset; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 if (iMaxLength <= 0) { | 603 if (iMaxLength <= 0) { |
594 return 0; | 604 return 0; |
595 } | 605 } |
596 int32_t i = 0; | 606 int32_t i = 0; |
597 while (i < iMaxLength && pStr[i] != L'\0') { | 607 while (i < iMaxLength && pStr[i] != L'\0') { |
598 ++i; | 608 ++i; |
599 } | 609 } |
600 bEOS = (m_iPosition >= m_iLength) || pStr[i] == L'\0'; | 610 bEOS = (m_iPosition >= m_iLength) || pStr[i] == L'\0'; |
601 return i; | 611 return i; |
602 } | 612 } |
| 613 |
603 CFGAS_BufferReadStreamImp::CFGAS_BufferReadStreamImp() | 614 CFGAS_BufferReadStreamImp::CFGAS_BufferReadStreamImp() |
604 : m_pBufferRead(nullptr), | 615 : m_iPosition(0), m_iBufferSize(0) {} |
605 m_bReleaseBufferRead(false), | 616 |
606 m_iPosition(0), | 617 CFGAS_BufferReadStreamImp::~CFGAS_BufferReadStreamImp() {} |
607 m_iBufferSize(0) {} | 618 |
608 CFGAS_BufferReadStreamImp::~CFGAS_BufferReadStreamImp() { | |
609 if (m_bReleaseBufferRead && m_pBufferRead) { | |
610 m_pBufferRead->Release(); | |
611 } | |
612 } | |
613 bool CFGAS_BufferReadStreamImp::LoadBufferRead( | 619 bool CFGAS_BufferReadStreamImp::LoadBufferRead( |
614 IFX_BufferedReadStream* pBufferRead, | 620 const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, |
615 int32_t iFileSize, | 621 int32_t iFileSize, |
616 uint32_t dwAccess, | 622 uint32_t dwAccess) { |
617 bool bReleaseBufferRead) { | |
618 ASSERT(!m_pBufferRead && pBufferRead); | 623 ASSERT(!m_pBufferRead && pBufferRead); |
619 if (dwAccess & FX_STREAMACCESS_Write) { | 624 if (dwAccess & FX_STREAMACCESS_Write) |
620 return false; | 625 return false; |
621 } | 626 |
622 m_bReleaseBufferRead = bReleaseBufferRead; | |
623 m_pBufferRead = pBufferRead; | 627 m_pBufferRead = pBufferRead; |
624 m_iBufferSize = iFileSize; | 628 m_iBufferSize = iFileSize; |
625 if (m_iBufferSize >= 0) { | 629 if (m_iBufferSize >= 0) |
626 return true; | 630 return true; |
627 } | 631 |
628 if (!m_pBufferRead->ReadNextBlock(true)) { | 632 if (!m_pBufferRead->ReadNextBlock(true)) |
629 return false; | 633 return false; |
630 } | 634 |
631 m_iBufferSize = m_pBufferRead->GetBlockSize(); | 635 m_iBufferSize = m_pBufferRead->GetBlockSize(); |
632 while (!m_pBufferRead->IsEOF()) { | 636 while (!m_pBufferRead->IsEOF()) { |
633 m_pBufferRead->ReadNextBlock(false); | 637 m_pBufferRead->ReadNextBlock(false); |
634 m_iBufferSize += m_pBufferRead->GetBlockSize(); | 638 m_iBufferSize += m_pBufferRead->GetBlockSize(); |
635 } | 639 } |
636 return true; | 640 return true; |
637 } | 641 } |
638 int32_t CFGAS_BufferReadStreamImp::GetLength() const { | 642 int32_t CFGAS_BufferReadStreamImp::GetLength() const { |
639 return m_iBufferSize; | 643 return m_iBufferSize; |
640 } | 644 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
726 } | 730 } |
727 int32_t i = 0; | 731 int32_t i = 0; |
728 while (i < iMaxLength && pStr[i] != L'\0') { | 732 while (i < iMaxLength && pStr[i] != L'\0') { |
729 ++i; | 733 ++i; |
730 } | 734 } |
731 bEOS = (m_iPosition >= GetLength()) || pStr[i] == L'\0'; | 735 bEOS = (m_iPosition >= GetLength()) || pStr[i] == L'\0'; |
732 return i; | 736 return i; |
733 } | 737 } |
734 CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp() | 738 CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp() |
735 : m_pFileWrite(nullptr), m_iPosition(0) {} | 739 : m_pFileWrite(nullptr), m_iPosition(0) {} |
| 740 |
736 bool CFGAS_FileWriteStreamImp::LoadFileWrite( | 741 bool CFGAS_FileWriteStreamImp::LoadFileWrite( |
737 IFX_SeekableWriteStream* pFileWrite, | 742 const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, |
738 uint32_t dwAccess) { | 743 uint32_t dwAccess) { |
739 ASSERT(!m_pFileWrite && pFileWrite); | 744 ASSERT(!m_pFileWrite && pFileWrite); |
740 if (dwAccess & FX_STREAMACCESS_Read) { | 745 if (dwAccess & FX_STREAMACCESS_Read) |
741 return false; | 746 return false; |
742 } | 747 |
743 if (dwAccess & FX_STREAMACCESS_Append) { | 748 if (dwAccess & FX_STREAMACCESS_Append) |
744 m_iPosition = pFileWrite->GetSize(); | 749 m_iPosition = pFileWrite->GetSize(); |
745 } | 750 |
746 m_pFileWrite = pFileWrite; | 751 m_pFileWrite = pFileWrite; |
747 return true; | 752 return true; |
748 } | 753 } |
| 754 |
749 int32_t CFGAS_FileWriteStreamImp::GetLength() const { | 755 int32_t CFGAS_FileWriteStreamImp::GetLength() const { |
750 if (!m_pFileWrite) { | 756 if (!m_pFileWrite) |
751 return 0; | 757 return 0; |
752 } | 758 |
753 return (int32_t)m_pFileWrite->GetSize(); | 759 return (int32_t)m_pFileWrite->GetSize(); |
754 } | 760 } |
755 int32_t CFGAS_FileWriteStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { | 761 int32_t CFGAS_FileWriteStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { |
756 int32_t iLength = GetLength(); | 762 int32_t iLength = GetLength(); |
757 switch (eSeek) { | 763 switch (eSeek) { |
758 case FX_STREAMSEEK_Begin: | 764 case FX_STREAMSEEK_Begin: |
759 m_iPosition = iOffset; | 765 m_iPosition = iOffset; |
760 break; | 766 break; |
761 case FX_STREAMSEEK_Current: | 767 case FX_STREAMSEEK_Current: |
762 m_iPosition += iOffset; | 768 m_iPosition += iOffset; |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1152 if (!pImp->LoadFile(pszSrcFileName, dwAccess)) | 1158 if (!pImp->LoadFile(pszSrcFileName, dwAccess)) |
1153 return false; | 1159 return false; |
1154 | 1160 |
1155 m_pStreamImp = pImp.release(); | 1161 m_pStreamImp = pImp.release(); |
1156 m_eStreamType = FX_STREAMTYPE_File; | 1162 m_eStreamType = FX_STREAMTYPE_File; |
1157 m_dwAccess = dwAccess; | 1163 m_dwAccess = dwAccess; |
1158 m_iLength = m_pStreamImp->GetLength(); | 1164 m_iLength = m_pStreamImp->GetLength(); |
1159 return true; | 1165 return true; |
1160 } | 1166 } |
1161 | 1167 |
1162 bool CFGAS_Stream::LoadFileRead(IFX_SeekableReadStream* pFileRead, | 1168 bool CFGAS_Stream::LoadFileRead( |
1163 uint32_t dwAccess) { | 1169 const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, |
| 1170 uint32_t dwAccess) { |
1164 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp) | 1171 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp) |
1165 return false; | 1172 return false; |
1166 | 1173 |
1167 if (!pFileRead) | 1174 if (!pFileRead) |
1168 return false; | 1175 return false; |
1169 | 1176 |
1170 std::unique_ptr<CFGAS_FileReadStreamImp> pImp(new CFGAS_FileReadStreamImp()); | 1177 std::unique_ptr<CFGAS_FileReadStreamImp> pImp(new CFGAS_FileReadStreamImp()); |
1171 if (!pImp->LoadFileRead(pFileRead, dwAccess)) | 1178 if (!pImp->LoadFileRead(pFileRead, dwAccess)) |
1172 return false; | 1179 return false; |
1173 | 1180 |
1174 m_pStreamImp = pImp.release(); | 1181 m_pStreamImp = pImp.release(); |
1175 m_eStreamType = FX_STREAMTYPE_File; | 1182 m_eStreamType = FX_STREAMTYPE_File; |
1176 m_dwAccess = dwAccess; | 1183 m_dwAccess = dwAccess; |
1177 m_iLength = m_pStreamImp->GetLength(); | 1184 m_iLength = m_pStreamImp->GetLength(); |
1178 return true; | 1185 return true; |
1179 } | 1186 } |
1180 | 1187 |
1181 bool CFGAS_Stream::LoadFileWrite(IFX_SeekableWriteStream* pFileWrite, | 1188 bool CFGAS_Stream::LoadFileWrite( |
1182 uint32_t dwAccess) { | 1189 const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, |
| 1190 uint32_t dwAccess) { |
1183 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp) | 1191 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp) |
1184 return false; | 1192 return false; |
1185 | 1193 |
1186 if (!pFileWrite) | 1194 if (!pFileWrite) |
1187 return false; | 1195 return false; |
1188 | 1196 |
1189 std::unique_ptr<CFGAS_FileWriteStreamImp> pImp( | 1197 auto pImp = pdfium::MakeUnique<CFGAS_FileWriteStreamImp>(); |
1190 new CFGAS_FileWriteStreamImp()); | |
1191 if (!pImp->LoadFileWrite(pFileWrite, dwAccess)) | 1198 if (!pImp->LoadFileWrite(pFileWrite, dwAccess)) |
1192 return false; | 1199 return false; |
1193 | 1200 |
1194 m_pStreamImp = pImp.release(); | 1201 m_pStreamImp = pImp.release(); |
1195 m_eStreamType = FX_STREAMTYPE_File; | 1202 m_eStreamType = FX_STREAMTYPE_File; |
1196 m_dwAccess = dwAccess; | 1203 m_dwAccess = dwAccess; |
1197 m_iLength = m_pStreamImp->GetLength(); | 1204 m_iLength = m_pStreamImp->GetLength(); |
1198 return true; | 1205 return true; |
1199 } | 1206 } |
1200 | 1207 |
(...skipping 10 matching lines...) Expand all Loading... |
1211 if (!pImp->LoadBuffer(pData, iTotalSize, dwAccess)) | 1218 if (!pImp->LoadBuffer(pData, iTotalSize, dwAccess)) |
1212 return false; | 1219 return false; |
1213 | 1220 |
1214 m_pStreamImp = pImp.release(); | 1221 m_pStreamImp = pImp.release(); |
1215 m_eStreamType = FX_STREAMTYPE_Buffer; | 1222 m_eStreamType = FX_STREAMTYPE_Buffer; |
1216 m_dwAccess = dwAccess; | 1223 m_dwAccess = dwAccess; |
1217 m_iLength = m_pStreamImp->GetLength(); | 1224 m_iLength = m_pStreamImp->GetLength(); |
1218 return true; | 1225 return true; |
1219 } | 1226 } |
1220 | 1227 |
1221 bool CFGAS_Stream::LoadBufferRead(IFX_BufferedReadStream* pBufferRead, | 1228 bool CFGAS_Stream::LoadBufferRead( |
1222 int32_t iFileSize, | 1229 const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, |
1223 uint32_t dwAccess, | 1230 int32_t iFileSize, |
1224 bool bReleaseBufferRead) { | 1231 uint32_t dwAccess) { |
1225 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp) | 1232 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp) |
1226 return false; | 1233 return false; |
1227 | 1234 |
1228 if (!pBufferRead) | 1235 if (!pBufferRead) |
1229 return false; | 1236 return false; |
1230 | 1237 |
1231 std::unique_ptr<CFGAS_BufferReadStreamImp> pImp( | 1238 auto pImp = pdfium::MakeUnique<CFGAS_BufferReadStreamImp>(); |
1232 new CFGAS_BufferReadStreamImp); | 1239 if (!pImp->LoadBufferRead(pBufferRead, iFileSize, dwAccess)) |
1233 if (!pImp->LoadBufferRead(pBufferRead, iFileSize, dwAccess, | |
1234 bReleaseBufferRead)) | |
1235 return false; | 1240 return false; |
1236 | 1241 |
1237 m_pStreamImp = pImp.release(); | 1242 m_pStreamImp = pImp.release(); |
1238 m_eStreamType = FX_STREAMTYPE_BufferRead; | 1243 m_eStreamType = FX_STREAMTYPE_BufferRead; |
1239 m_dwAccess = dwAccess; | 1244 m_dwAccess = dwAccess; |
1240 m_iLength = m_pStreamImp->GetLength(); | 1245 m_iLength = m_pStreamImp->GetLength(); |
1241 return true; | 1246 return true; |
1242 } | 1247 } |
1243 | 1248 |
1244 void CFGAS_Stream::Release() { | 1249 void CFGAS_Stream::Release() { |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1471 pShared->m_iTotalSize = iLength; | 1476 pShared->m_iTotalSize = iLength; |
1472 pShared->m_iPosition = iStart; | 1477 pShared->m_iPosition = iStart; |
1473 pShared->m_iStart = iStart; | 1478 pShared->m_iStart = iStart; |
1474 pShared->m_iLength = (dwAccess & FX_STREAMACCESS_Write) != 0 ? 0 : iLength; | 1479 pShared->m_iLength = (dwAccess & FX_STREAMACCESS_Write) != 0 ? 0 : iLength; |
1475 if (dwAccess & FX_STREAMACCESS_Text) | 1480 if (dwAccess & FX_STREAMACCESS_Text) |
1476 return IFGAS_Stream::CreateTextStream(pShared, true); | 1481 return IFGAS_Stream::CreateTextStream(pShared, true); |
1477 | 1482 |
1478 return pShared; | 1483 return pShared; |
1479 } | 1484 } |
1480 | 1485 |
1481 IFX_SeekableReadStream* IFGAS_Stream::MakeSeekableReadStream() { | 1486 CFX_RetainPtr<IFX_SeekableReadStream> IFGAS_Stream::MakeSeekableReadStream() { |
1482 return new CFGAS_FileRead(this, false); | 1487 return CFGAS_FileRead::Create(this, false); |
| 1488 } |
| 1489 |
| 1490 CFX_RetainPtr<CFGAS_FileRead> CFGAS_FileRead::Create(IFGAS_Stream* pStream, |
| 1491 bool bReleaseStream) { |
| 1492 return CFX_RetainPtr<CFGAS_FileRead>( |
| 1493 new CFGAS_FileRead(pStream, bReleaseStream)); |
1483 } | 1494 } |
1484 | 1495 |
1485 CFGAS_FileRead::CFGAS_FileRead(IFGAS_Stream* pStream, bool bReleaseStream) | 1496 CFGAS_FileRead::CFGAS_FileRead(IFGAS_Stream* pStream, bool bReleaseStream) |
1486 : m_bReleaseStream(bReleaseStream), m_pStream(pStream) { | 1497 : m_bReleaseStream(bReleaseStream), m_pStream(pStream) { |
1487 ASSERT(m_pStream); | 1498 ASSERT(m_pStream); |
1488 } | 1499 } |
| 1500 |
1489 CFGAS_FileRead::~CFGAS_FileRead() { | 1501 CFGAS_FileRead::~CFGAS_FileRead() { |
1490 if (m_bReleaseStream) { | 1502 if (m_bReleaseStream) { |
1491 m_pStream->Release(); | 1503 m_pStream->Release(); |
1492 } | 1504 } |
1493 } | 1505 } |
1494 FX_FILESIZE CFGAS_FileRead::GetSize() { | 1506 FX_FILESIZE CFGAS_FileRead::GetSize() { |
1495 return (FX_FILESIZE)m_pStream->GetLength(); | 1507 return (FX_FILESIZE)m_pStream->GetLength(); |
1496 } | 1508 } |
1497 | 1509 |
1498 bool CFGAS_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) { | 1510 bool CFGAS_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) { |
1499 m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset); | 1511 m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset); |
1500 int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); | 1512 int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); |
1501 return iLen == (int32_t)size; | 1513 return iLen == (int32_t)size; |
1502 } | 1514 } |
1503 | |
1504 void CFGAS_FileRead::Release() { | |
1505 delete this; | |
1506 } | |
OLD | NEW |