| 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/extension.h" | 7 #include "core/fxcrt/extension.h" |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 return; | 97 return; |
| 98 } | 98 } |
| 99 delete this; | 99 delete this; |
| 100 } | 100 } |
| 101 | 101 |
| 102 FX_FILESIZE CFX_MemoryStream::GetSize() { | 102 FX_FILESIZE CFX_MemoryStream::GetSize() { |
| 103 return (FX_FILESIZE)m_nCurSize; | 103 return (FX_FILESIZE)m_nCurSize; |
| 104 } | 104 } |
| 105 | 105 |
| 106 bool CFX_MemoryStream::IsEOF() { | 106 bool CFX_MemoryStream::IsEOF() { |
| 107 return m_nCurPos >= (size_t)GetSize(); | 107 return m_nCurPos >= pdfium::base::checked_cast<size_t>(GetSize()); |
| 108 } | 108 } |
| 109 | 109 |
| 110 FX_FILESIZE CFX_MemoryStream::GetPosition() { | 110 FX_FILESIZE CFX_MemoryStream::GetPosition() { |
| 111 return (FX_FILESIZE)m_nCurPos; | 111 return (FX_FILESIZE)m_nCurPos; |
| 112 } | 112 } |
| 113 | 113 |
| 114 bool CFX_MemoryStream::ReadBlock(void* buffer, | 114 size_t CFX_MemoryStream::ReadBlock(void* buffer, |
| 115 FX_FILESIZE offset, | 115 FX_FILESIZE offset, |
| 116 size_t size) { | 116 size_t size) { |
| 117 if (!buffer || !size) | 117 if (!buffer || !size) |
| 118 return false; | 118 return 0; |
| 119 | 119 |
| 120 FX_SAFE_SIZE_T newPos = size; | 120 FX_SAFE_SIZE_T newPos = size; |
| 121 newPos += offset; | 121 newPos += offset; |
| 122 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || | 122 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || |
| 123 newPos.ValueOrDie() > m_nCurSize) { | 123 newPos.ValueOrDie() > m_nCurSize) { |
| 124 return false; | 124 return 0; |
| 125 } | 125 } |
| 126 | 126 |
| 127 m_nCurPos = newPos.ValueOrDie(); | 127 m_nCurPos = newPos.ValueOrDie(); |
| 128 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 128 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 129 FXSYS_memcpy(buffer, m_Blocks[0] + (size_t)offset, size); | 129 FXSYS_memcpy(buffer, m_Blocks[0] + (size_t)offset, size); |
| 130 return true; | 130 return size; |
| 131 } | 131 } |
| 132 size_t readSize = size; |
| 132 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 133 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 133 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 134 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| 134 while (size) { | 135 while (size) { |
| 135 size_t nRead = m_nGrowSize - (size_t)offset; | 136 size_t nRead = m_nGrowSize - (size_t)offset; |
| 136 if (nRead > size) { | 137 if (nRead > size) { |
| 137 nRead = size; | 138 nRead = size; |
| 138 } | 139 } |
| 139 FXSYS_memcpy(buffer, m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); | 140 FXSYS_memcpy(buffer, m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); |
| 140 buffer = ((uint8_t*)buffer) + nRead; | 141 buffer = ((uint8_t*)buffer) + nRead; |
| 141 size -= nRead; | 142 size -= nRead; |
| 142 nStartBlock++; | 143 nStartBlock++; |
| 143 offset = 0; | 144 offset = 0; |
| 144 } | 145 } |
| 145 return true; | 146 return readSize; |
| 146 } | 147 } |
| 147 | 148 |
| 148 size_t CFX_MemoryStream::ReadBlock(void* buffer, size_t size) { | 149 size_t CFX_MemoryStream::ReadBlock(void* buffer, size_t size) { |
| 149 if (m_nCurPos >= m_nCurSize) { | 150 if (m_nCurPos >= m_nCurSize) { |
| 150 return 0; | 151 return 0; |
| 151 } | 152 } |
| 152 size_t nRead = std::min(size, m_nCurSize - m_nCurPos); | 153 size_t nRead = std::min(size, m_nCurSize - m_nCurPos); |
| 153 if (!ReadBlock(buffer, (int32_t)m_nCurPos, nRead)) { | 154 if (ReadBlock(buffer, (int32_t)m_nCurPos, nRead) != nRead && !IsEOF()) { |
| 154 return 0; | 155 return 0; |
| 155 } | 156 } |
| 156 return nRead; | 157 return nRead; |
| 157 } | 158 } |
| 158 | 159 |
| 159 bool CFX_MemoryStream::WriteBlock(const void* buffer, | 160 bool CFX_MemoryStream::WriteBlock(const void* buffer, |
| 160 FX_FILESIZE offset, | 161 FX_FILESIZE offset, |
| 161 size_t size) { | 162 size_t size) { |
| 162 if (!buffer || !size) | 163 if (!buffer || !size) |
| 163 return false; | 164 return false; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 } | 299 } |
| 299 | 300 |
| 300 bool CFX_CRTFileStream::IsEOF() { | 301 bool CFX_CRTFileStream::IsEOF() { |
| 301 return GetPosition() >= GetSize(); | 302 return GetPosition() >= GetSize(); |
| 302 } | 303 } |
| 303 | 304 |
| 304 FX_FILESIZE CFX_CRTFileStream::GetPosition() { | 305 FX_FILESIZE CFX_CRTFileStream::GetPosition() { |
| 305 return m_pFile->GetPosition(); | 306 return m_pFile->GetPosition(); |
| 306 } | 307 } |
| 307 | 308 |
| 308 bool CFX_CRTFileStream::ReadBlock(void* buffer, | 309 size_t CFX_CRTFileStream::ReadBlock(void* buffer, |
| 309 FX_FILESIZE offset, | 310 FX_FILESIZE offset, |
| 310 size_t size) { | 311 size_t size) { |
| 311 return m_pFile->ReadPos(buffer, size, offset) > 0; | 312 return m_pFile->ReadPos(buffer, size, offset); |
| 312 } | 313 } |
| 313 | 314 |
| 314 size_t CFX_CRTFileStream::ReadBlock(void* buffer, size_t size) { | 315 size_t CFX_CRTFileStream::ReadBlock(void* buffer, size_t size) { |
| 315 return m_pFile->Read(buffer, size); | 316 return m_pFile->Read(buffer, size); |
| 316 } | 317 } |
| 317 | 318 |
| 318 bool CFX_CRTFileStream::WriteBlock(const void* buffer, | 319 bool CFX_CRTFileStream::WriteBlock(const void* buffer, |
| 319 FX_FILESIZE offset, | 320 FX_FILESIZE offset, |
| 320 size_t size) { | 321 size_t size) { |
| 321 return !!m_pFile->WritePos(buffer, size, offset); | 322 return !!m_pFile->WritePos(buffer, size, offset); |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 b = ((const uint8_t*)pGUID)[i]; | 611 b = ((const uint8_t*)pGUID)[i]; |
| 611 *pBuf++ = gs_FX_pHexChars[b >> 4]; | 612 *pBuf++ = gs_FX_pHexChars[b >> 4]; |
| 612 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; | 613 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; |
| 613 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { | 614 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { |
| 614 *pBuf++ = L'-'; | 615 *pBuf++ = L'-'; |
| 615 } | 616 } |
| 616 } | 617 } |
| 617 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); | 618 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); |
| 618 } | 619 } |
| 619 #endif // PDF_ENABLE_XFA | 620 #endif // PDF_ENABLE_XFA |
| OLD | NEW |