Chromium Code Reviews| 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 #ifndef _FXCRT_EXTENSION_IMP_ | 7 #ifndef _FXCRT_EXTENSION_IMP_ |
| 8 #define _FXCRT_EXTENSION_IMP_ | 8 #define _FXCRT_EXTENSION_IMP_ |
| 9 | 9 |
| 10 #include "../../../third_party/numerics/safe_math.h" | |
| 11 | |
| 12 class IFXCRT_FileAccess | 10 class IFXCRT_FileAccess |
| 13 { | 11 { |
| 14 public: | 12 public: |
| 15 virtual ~IFXCRT_FileAccess() {} | 13 virtual ~IFXCRT_FileAccess() {} |
| 16 virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode) = 0; | 14 virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode) = 0; |
| 17 virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode) = 0; | 15 virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode) = 0; |
| 18 virtual void Close() = 0; | 16 virtual void Close() = 0; |
| 19 virtual void Release() = 0; | 17 virtual void Release() = 0; |
| 20 virtual FX_FILESIZE GetSize() const = 0; | 18 virtual FX_FILESIZE GetSize() const = 0; |
| 21 virtual FX_FILESIZE GetPosition() const = 0; | 19 virtual FX_FILESIZE GetPosition() const = 0; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 virtual FX_FILESIZE GetPosition() | 59 virtual FX_FILESIZE GetPosition() |
| 62 { | 60 { |
| 63 FX_FILESIZE pos = m_pFile->GetPosition(); | 61 FX_FILESIZE pos = m_pFile->GetPosition(); |
| 64 if (m_bUseRange) { | 62 if (m_bUseRange) { |
| 65 pos -= m_nOffset; | 63 pos -= m_nOffset; |
| 66 } | 64 } |
| 67 return pos; | 65 return pos; |
| 68 } | 66 } |
| 69 virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_ FILESIZE size) | 67 virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_ FILESIZE size) |
| 70 { | 68 { |
| 71 if (offset < 0 || offset + size > m_pFile->GetSize()) { | 69 FX_SAFE_FILESIZE pos = size; |
|
Tom Sepez
2014/07/30 18:50:24
nit: I would write this as
if (offset < 0 || size
| |
| 70 pos += offset; | |
| 71 | |
| 72 if (!pos.IsValid() || offset < 0 || size < 0 || pos.ValueOrDie() >= m_pF ile->GetSize()) { | |
| 72 return FALSE; | 73 return FALSE; |
| 73 } | 74 } |
| 75 | |
| 74 m_nOffset = offset, m_nSize = size; | 76 m_nOffset = offset, m_nSize = size; |
| 75 m_bUseRange = TRUE; | 77 m_bUseRange = TRUE; |
| 76 m_pFile->SetPosition(m_nOffset); | 78 m_pFile->SetPosition(m_nOffset); |
| 77 return TRUE; | 79 return TRUE; |
| 78 } | 80 } |
| 79 virtual void ClearRange() | 81 virtual void ClearRange() |
| 80 { | 82 { |
| 81 m_bUseRange = FALSE; | 83 m_bUseRange = FALSE; |
| 82 } | 84 } |
| 83 virtual FX_BOOL ReadBlock(void* buffer, FX_FILES IZE offset, size_t size) | 85 virtual FX_BOOL ReadBlock(void* buffer, FX_FILES IZE offset, size_t size) |
| 84 { | 86 { |
| 87 FX_SAFE_FILESIZE pos = offset; | |
| 88 | |
| 85 if (m_bUseRange) { | 89 if (m_bUseRange) { |
| 86 if (offset + size > (size_t)GetSize()) { | 90 pos += m_nOffset; |
| 91 if (!pos.IsValid() || pos.ValueOrDie() >= (size_t)GetSize()) { | |
| 87 return FALSE; | 92 return FALSE; |
| 88 } | 93 } |
| 89 offset += m_nOffset; | |
| 90 } | 94 } |
| 91 return (FX_BOOL)m_pFile->ReadPos(buffer, size, offset); | 95 return (FX_BOOL)m_pFile->ReadPos(buffer, size, pos.ValueOrDie()); |
| 92 } | 96 } |
| 93 virtual size_t ReadBlock(void* buffer, size_t s ize) | 97 virtual size_t ReadBlock(void* buffer, size_t s ize) |
| 94 { | 98 { |
| 95 if (m_bUseRange) { | 99 if (m_bUseRange) { |
| 96 FX_FILESIZE availSize = m_nOffset + m_nSize - m_pFile->GetPosition() ; | 100 FX_FILESIZE availSize = m_nOffset + m_nSize - m_pFile->GetPosition() ; |
| 97 if ((size_t)availSize < size) { | 101 if ((size_t)availSize < size) { |
| 98 size -= size - (size_t)availSize; | 102 size -= size - (size_t)availSize; |
| 99 } | 103 } |
| 100 } | 104 } |
| 101 return m_pFile->Read(buffer, size); | 105 return m_pFile->Read(buffer, size); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 virtual FX_FILESIZE GetPosition() | 181 virtual FX_FILESIZE GetPosition() |
| 178 { | 182 { |
| 179 FX_FILESIZE pos = (FX_FILESIZE)m_nCurPos; | 183 FX_FILESIZE pos = (FX_FILESIZE)m_nCurPos; |
| 180 if (m_bUseRange) { | 184 if (m_bUseRange) { |
| 181 pos -= (FX_FILESIZE)m_nOffset; | 185 pos -= (FX_FILESIZE)m_nOffset; |
| 182 } | 186 } |
| 183 return pos; | 187 return pos; |
| 184 } | 188 } |
| 185 virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_ FILESIZE size) | 189 virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_ FILESIZE size) |
| 186 { | 190 { |
| 187 base::CheckedNumeric<FX_FILESIZE> range = size; | 191 FX_SAFE_FILESIZE range = size; |
| 188 range += size; | 192 range += offset; |
| 189 | 193 if (!range.IsValid() || offset <= 0 || size <= 0 || range.ValueOrDie() > = m_nCurSize) { |
|
Tom Sepez
2014/07/30 18:50:24
nit: again, I'd make the <= 0 checks first before
| |
| 190 if (!range.IsValid() || offset <= 0 || size <= 0 || range.ValueOrDie() > m_nCurSize) { | |
| 191 return FALSE; | 194 return FALSE; |
| 192 } | 195 } |
| 193 | 196 |
| 194 m_nOffset = (size_t)offset, m_nSize = (size_t)size; | 197 m_nOffset = (size_t)offset, m_nSize = (size_t)size; |
| 195 m_bUseRange = TRUE; | 198 m_bUseRange = TRUE; |
| 196 m_nCurPos = m_nOffset; | 199 m_nCurPos = m_nOffset; |
| 197 return TRUE; | 200 return TRUE; |
| 198 } | 201 } |
| 199 virtual void ClearRange() | 202 virtual void ClearRange() |
| 200 { | 203 { |
| 201 m_bUseRange = FALSE; | 204 m_bUseRange = FALSE; |
| 202 } | 205 } |
| 203 virtual FX_BOOL ReadBlock(void* buffer, FX_FILES IZE offset, size_t size) | 206 virtual FX_BOOL ReadBlock(void* buffer, FX_FILES IZE offset, size_t size) |
| 204 { | 207 { |
| 205 if (!buffer || !size) { | 208 if (!buffer || !size) { |
| 206 return FALSE; | 209 return FALSE; |
| 207 } | 210 } |
| 208 | 211 |
| 209 base::CheckedNumeric<FX_FILESIZE> safeOffset = offset; | 212 FX_SAFE_FILESIZE safeOffset = offset; |
| 210 if (m_bUseRange) { | 213 if (m_bUseRange) { |
| 211 safeOffset += m_nOffset; | 214 safeOffset += m_nOffset; |
| 212 } | 215 } |
| 213 | 216 |
| 214 if (!safeOffset.IsValid()) { | 217 if (!safeOffset.IsValid()) { |
| 215 return FALSE; | 218 return FALSE; |
| 216 } | 219 } |
| 217 | 220 |
| 218 offset = safeOffset.ValueOrDie(); | 221 offset = safeOffset.ValueOrDie(); |
| 219 | 222 |
| 220 base::CheckedNumeric<size_t> newPos = size; | 223 FX_SAFE_SIZET newPos = size; |
| 221 newPos += offset; | 224 newPos += offset; |
| 222 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || newPos.ValueOr Die() > m_nCurSize) { | 225 if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 || newPos.ValueOr Die() >= m_nCurSize) { |
| 223 return FALSE; | 226 return FALSE; |
| 224 } | 227 } |
| 225 | 228 |
| 226 m_nCurPos = newPos.ValueOrDie(); | 229 m_nCurPos = newPos.ValueOrDie(); |
| 227 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 230 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 228 FXSYS_memcpy32(buffer, (FX_LPBYTE)m_Blocks[0] + (size_t)offset, size ); | 231 FXSYS_memcpy32(buffer, (FX_LPBYTE)m_Blocks[0] + (size_t)offset, size ); |
| 229 return TRUE; | 232 return TRUE; |
| 230 } | 233 } |
| 231 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 234 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 232 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 235 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 262 } | 265 } |
| 263 virtual FX_BOOL WriteBlock(const void* buffer, F X_FILESIZE offset, size_t size) | 266 virtual FX_BOOL WriteBlock(const void* buffer, F X_FILESIZE offset, size_t size) |
| 264 { | 267 { |
| 265 if (!buffer || !size) { | 268 if (!buffer || !size) { |
| 266 return FALSE; | 269 return FALSE; |
| 267 } | 270 } |
| 268 if (m_bUseRange) { | 271 if (m_bUseRange) { |
| 269 offset += (FX_FILESIZE)m_nOffset; | 272 offset += (FX_FILESIZE)m_nOffset; |
| 270 } | 273 } |
| 271 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 274 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 272 base::CheckedNumeric<size_t> newPos = size; | 275 FX_SAFE_SIZET newPos = size; |
| 273 newPos += offset; | 276 newPos += offset; |
| 274 if (!newPos.IsValid()) | 277 if (!newPos.IsValid()) |
| 275 return FALSE; | 278 return FALSE; |
| 276 | 279 |
| 277 m_nCurPos = newPos.ValueOrDie(); | 280 m_nCurPos = newPos.ValueOrDie(); |
| 278 if (m_nCurPos > m_nTotalSize) { | 281 if (m_nCurPos > m_nTotalSize) { |
| 279 m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_n GrowSize; | 282 m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_n GrowSize; |
| 280 if (m_Blocks.GetSize() < 1) { | 283 if (m_Blocks.GetSize() < 1) { |
| 281 void* block = FX_Alloc(FX_BYTE, m_nTotalSize); | 284 void* block = FX_Alloc(FX_BYTE, m_nTotalSize); |
| 282 m_Blocks.Add(block); | 285 m_Blocks.Add(block); |
| 283 } else { | 286 } else { |
| 284 m_Blocks[0] = FX_Realloc(FX_BYTE, m_Blocks[0], m_nTotalSize) ; | 287 m_Blocks[0] = FX_Realloc(FX_BYTE, m_Blocks[0], m_nTotalSize) ; |
| 285 } | 288 } |
| 286 if (!m_Blocks[0]) { | 289 if (!m_Blocks[0]) { |
| 287 m_Blocks.RemoveAll(); | 290 m_Blocks.RemoveAll(); |
| 288 return FALSE; | 291 return FALSE; |
| 289 } | 292 } |
| 290 } | 293 } |
| 291 FXSYS_memcpy32((FX_LPBYTE)m_Blocks[0] + (size_t)offset, buffer, size ); | 294 FXSYS_memcpy32((FX_LPBYTE)m_Blocks[0] + (size_t)offset, buffer, size ); |
| 292 if (m_nCurSize < m_nCurPos) { | 295 if (m_nCurSize < m_nCurPos) { |
| 293 m_nCurSize = m_nCurPos; | 296 m_nCurSize = m_nCurPos; |
| 294 } | 297 } |
| 295 return TRUE; | 298 return TRUE; |
| 296 } | 299 } |
| 297 | 300 |
| 298 base::CheckedNumeric<size_t> newPos = size; | 301 FX_SAFE_SIZET newPos = size; |
| 299 newPos += offset; | 302 newPos += offset; |
| 300 if (!newPos.IsValid()) | 303 if (!newPos.IsValid()) |
|
Tom Sepez
2014/07/30 18:50:24
nit: braces here around single-line if to match th
| |
| 301 return FALSE; | 304 return FALSE; |
| 302 | 305 |
| 303 if (!ExpandBlocks(newPos.ValueOrDie())) { | 306 if (!ExpandBlocks(newPos.ValueOrDie())) { |
| 304 return FALSE; | 307 return FALSE; |
| 305 } | 308 } |
| 306 m_nCurPos = newPos.ValueOrDie(); | 309 m_nCurPos = newPos.ValueOrDie(); |
| 307 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 310 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 308 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 311 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| 309 while (size) { | 312 while (size) { |
| 310 size_t nWrite = m_nGrowSize - (size_t)offset; | 313 size_t nWrite = m_nGrowSize - (size_t)offset; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 FX_DWORD mt[MT_N]; | 422 FX_DWORD mt[MT_N]; |
| 420 } FX_MTRANDOMCONTEXT, * FX_LPMTRANDOMCONTEXT; | 423 } FX_MTRANDOMCONTEXT, * FX_LPMTRANDOMCONTEXT; |
| 421 typedef FX_MTRANDOMCONTEXT const * FX_LPCMTRANDOMCONTEXT; | 424 typedef FX_MTRANDOMCONTEXT const * FX_LPCMTRANDOMCONTEXT; |
| 422 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 425 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 423 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount); | 426 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount); |
| 424 #endif | 427 #endif |
| 425 #ifdef __cplusplus | 428 #ifdef __cplusplus |
| 426 } | 429 } |
| 427 #endif | 430 #endif |
| 428 #endif | 431 #endif |
| OLD | NEW |