| 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 |
| 10 #include "../../../third_party/numerics/safe_math.h" |
| 11 |
| 9 class IFXCRT_FileAccess | 12 class IFXCRT_FileAccess |
| 10 { | 13 { |
| 11 public: | 14 public: |
| 12 virtual ~IFXCRT_FileAccess() {} | 15 virtual ~IFXCRT_FileAccess() {} |
| 13 virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode) = 0; | 16 virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode) = 0; |
| 14 virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode) = 0; | 17 virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode) = 0; |
| 15 virtual void Close() = 0; | 18 virtual void Close() = 0; |
| 16 virtual void Release() = 0; | 19 virtual void Release() = 0; |
| 17 virtual FX_FILESIZE GetSize() const = 0; | 20 virtual FX_FILESIZE GetSize() const = 0; |
| 18 virtual FX_FILESIZE GetPosition() const = 0; | 21 virtual FX_FILESIZE GetPosition() const = 0; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 virtual FX_FILESIZE GetPosition() | 177 virtual FX_FILESIZE GetPosition() |
| 175 { | 178 { |
| 176 FX_FILESIZE pos = (FX_FILESIZE)m_nCurPos; | 179 FX_FILESIZE pos = (FX_FILESIZE)m_nCurPos; |
| 177 if (m_bUseRange) { | 180 if (m_bUseRange) { |
| 178 pos -= (FX_FILESIZE)m_nOffset; | 181 pos -= (FX_FILESIZE)m_nOffset; |
| 179 } | 182 } |
| 180 return pos; | 183 return pos; |
| 181 } | 184 } |
| 182 virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_
FILESIZE size) | 185 virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_
FILESIZE size) |
| 183 { | 186 { |
| 184 if (offset < 0 || (size_t)(offset + size) > m_nCurSize) { | 187 base::CheckedNumeric<FX_FILESIZE> safe_offset = offset; |
| 188 safe_offset += size; |
| 189 |
| 190 if (offset <= 0 || size <= 0 || !safe_offset.IsValid() || safe_offset.Va
lueOrDie() > m_nCurSize) { |
| 185 return FALSE; | 191 return FALSE; |
| 186 } | 192 } |
| 193 |
| 187 m_nOffset = (size_t)offset, m_nSize = (size_t)size; | 194 m_nOffset = (size_t)offset, m_nSize = (size_t)size; |
| 188 m_bUseRange = TRUE; | 195 m_bUseRange = TRUE; |
| 189 m_nCurPos = m_nOffset; | 196 m_nCurPos = m_nOffset; |
| 190 return TRUE; | 197 return TRUE; |
| 191 } | 198 } |
| 192 virtual void ClearRange() | 199 virtual void ClearRange() |
| 193 { | 200 { |
| 194 m_bUseRange = FALSE; | 201 m_bUseRange = FALSE; |
| 195 } | 202 } |
| 196 virtual FX_BOOL ReadBlock(void* buffer, FX_FILES
IZE offset, size_t size) | 203 virtual FX_BOOL ReadBlock(void* buffer, FX_FILES
IZE offset, size_t size) |
| 197 { | 204 { |
| 198 if (!buffer || !size) { | 205 if (!buffer || !size) { |
| 199 return FALSE; | 206 return FALSE; |
| 200 } | 207 } |
| 208 |
| 209 base::CheckedNumeric<FX_FILESIZE> safe_offset = offset; |
| 201 if (m_bUseRange) { | 210 if (m_bUseRange) { |
| 202 offset += (FX_FILESIZE)m_nOffset; | 211 safe_offset += m_nOffset; |
| 203 } | 212 } |
| 204 if ((size_t)offset + size > m_nCurSize) { | 213 |
| 214 if (!safe_offset.IsValid()) |
| 215 return FALSE; |
| 216 |
| 217 base::CheckedNumeric<size_t> safe_size = size; |
| 218 safe_size += safe_offset.ValueOrDie(); |
| 219 if (!safe_size.IsValid() || safe_size.ValueOrDie() > m_nCurSize) { |
| 205 return FALSE; | 220 return FALSE; |
| 206 } | 221 } |
| 207 m_nCurPos = (size_t)offset + size; | 222 |
| 223 m_nCurPos = safe_size.ValueOrDie(); |
| 208 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 224 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 209 FXSYS_memcpy32(buffer, (FX_LPBYTE)m_Blocks[0] + (size_t)offset, size
); | 225 FXSYS_memcpy32(buffer, (FX_LPBYTE)m_Blocks[0] + (size_t)offset, size
); |
| 210 return TRUE; | 226 return TRUE; |
| 211 } | 227 } |
| 212 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 228 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 213 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 229 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| 214 while (size) { | 230 while (size) { |
| 215 size_t nRead = m_nGrowSize - (size_t)offset; | 231 size_t nRead = m_nGrowSize - (size_t)offset; |
| 216 if (nRead > size) { | 232 if (nRead > size) { |
| 217 nRead = size; | 233 nRead = size; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 243 } | 259 } |
| 244 virtual FX_BOOL WriteBlock(const void* buffer, F
X_FILESIZE offset, size_t size) | 260 virtual FX_BOOL WriteBlock(const void* buffer, F
X_FILESIZE offset, size_t size) |
| 245 { | 261 { |
| 246 if (!buffer || !size) { | 262 if (!buffer || !size) { |
| 247 return FALSE; | 263 return FALSE; |
| 248 } | 264 } |
| 249 if (m_bUseRange) { | 265 if (m_bUseRange) { |
| 250 offset += (FX_FILESIZE)m_nOffset; | 266 offset += (FX_FILESIZE)m_nOffset; |
| 251 } | 267 } |
| 252 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { | 268 if (m_dwFlags & FX_MEMSTREAM_Consecutive) { |
| 253 m_nCurPos = (size_t)offset + size; | 269 base::CheckedNumeric<size_t> safe_size = size; |
| 270 safe_size += offset; |
| 271 if (!safe_size.IsValid()) |
| 272 return FALSE; |
| 273 |
| 274 m_nCurPos = safe_size.ValueOrDie(); |
| 254 if (m_nCurPos > m_nTotalSize) { | 275 if (m_nCurPos > m_nTotalSize) { |
| 255 m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_n
GrowSize; | 276 m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_n
GrowSize; |
| 256 if (m_Blocks.GetSize() < 1) { | 277 if (m_Blocks.GetSize() < 1) { |
| 257 void* block = FX_Alloc(FX_BYTE, m_nTotalSize); | 278 void* block = FX_Alloc(FX_BYTE, m_nTotalSize); |
| 258 m_Blocks.Add(block); | 279 m_Blocks.Add(block); |
| 259 } else { | 280 } else { |
| 260 m_Blocks[0] = FX_Realloc(FX_BYTE, m_Blocks[0], m_nTotalSize)
; | 281 m_Blocks[0] = FX_Realloc(FX_BYTE, m_Blocks[0], m_nTotalSize)
; |
| 261 } | 282 } |
| 262 if (!m_Blocks[0]) { | 283 if (!m_Blocks[0]) { |
| 263 m_Blocks.RemoveAll(); | 284 m_Blocks.RemoveAll(); |
| 264 return FALSE; | 285 return FALSE; |
| 265 } | 286 } |
| 266 } | 287 } |
| 267 FXSYS_memcpy32((FX_LPBYTE)m_Blocks[0] + (size_t)offset, buffer, size
); | 288 FXSYS_memcpy32((FX_LPBYTE)m_Blocks[0] + (size_t)offset, buffer, size
); |
| 268 if (m_nCurSize < m_nCurPos) { | 289 if (m_nCurSize < m_nCurPos) { |
| 269 m_nCurSize = m_nCurPos; | 290 m_nCurSize = m_nCurPos; |
| 270 } | 291 } |
| 271 return TRUE; | 292 return TRUE; |
| 272 } | 293 } |
| 273 if (!ExpandBlocks((size_t)offset + size)) { | 294 |
| 295 base::CheckedNumeric<size_t> safe_size = size; |
| 296 safe_size += offset; |
| 297 if (!safe_size.IsValid()) |
| 298 return FALSE; |
| 299 |
| 300 if (!ExpandBlocks(safe_size.ValueOrDie())) { |
| 274 return FALSE; | 301 return FALSE; |
| 275 } | 302 } |
| 276 m_nCurPos = (size_t)offset + size; | 303 m_nCurPos = safe_size.ValueOrDie(); |
| 277 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 304 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 278 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 305 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| 279 while (size) { | 306 while (size) { |
| 280 size_t nWrite = m_nGrowSize - (size_t)offset; | 307 size_t nWrite = m_nGrowSize - (size_t)offset; |
| 281 if (nWrite > size) { | 308 if (nWrite > size) { |
| 282 nWrite = size; | 309 nWrite = size; |
| 283 } | 310 } |
| 284 FXSYS_memcpy32((FX_LPBYTE)m_Blocks[(int)nStartBlock] + (size_t)offse
t, buffer, nWrite); | 311 FXSYS_memcpy32((FX_LPBYTE)m_Blocks[(int)nStartBlock] + (size_t)offse
t, buffer, nWrite); |
| 285 buffer = ((FX_LPBYTE)buffer) + nWrite; | 312 buffer = ((FX_LPBYTE)buffer) + nWrite; |
| 286 size -= nWrite; | 313 size -= nWrite; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 FX_DWORD mt[MT_N]; | 416 FX_DWORD mt[MT_N]; |
| 390 } FX_MTRANDOMCONTEXT, * FX_LPMTRANDOMCONTEXT; | 417 } FX_MTRANDOMCONTEXT, * FX_LPMTRANDOMCONTEXT; |
| 391 typedef FX_MTRANDOMCONTEXT const * FX_LPCMTRANDOMCONTEXT; | 418 typedef FX_MTRANDOMCONTEXT const * FX_LPCMTRANDOMCONTEXT; |
| 392 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 419 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 393 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount); | 420 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount); |
| 394 #endif | 421 #endif |
| 395 #ifdef __cplusplus | 422 #ifdef __cplusplus |
| 396 } | 423 } |
| 397 #endif | 424 #endif |
| 398 #endif | 425 #endif |
| OLD | NEW |