| 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/include/fx_basic.h" | 7 #include "core/fxcrt/include/fx_basic.h" |
| 8 #include "third_party/base/numerics/safe_math.h" | 8 #include "third_party/base/numerics/safe_math.h" |
| 9 | 9 |
| 10 CFX_BasicArray::CFX_BasicArray(int unit_size) | 10 CFX_BasicArray::CFX_BasicArray(int unit_size) |
| 11 : m_pData(NULL), m_nSize(0), m_nMaxSize(0) { | 11 : m_pData(nullptr), m_nSize(0), m_nMaxSize(0) { |
| 12 if (unit_size < 0 || unit_size > (1 << 28)) { | 12 if (unit_size < 0 || unit_size > (1 << 28)) { |
| 13 m_nUnitSize = 4; | 13 m_nUnitSize = 4; |
| 14 } else { | 14 } else { |
| 15 m_nUnitSize = unit_size; | 15 m_nUnitSize = unit_size; |
| 16 } | 16 } |
| 17 } | 17 } |
| 18 CFX_BasicArray::~CFX_BasicArray() { | 18 CFX_BasicArray::~CFX_BasicArray() { |
| 19 FX_Free(m_pData); | 19 FX_Free(m_pData); |
| 20 } | 20 } |
| 21 FX_BOOL CFX_BasicArray::SetSize(int nNewSize) { | 21 FX_BOOL CFX_BasicArray::SetSize(int nNewSize) { |
| 22 if (nNewSize <= 0) { | 22 if (nNewSize <= 0) { |
| 23 FX_Free(m_pData); | 23 FX_Free(m_pData); |
| 24 m_pData = NULL; | 24 m_pData = nullptr; |
| 25 m_nSize = m_nMaxSize = 0; | 25 m_nSize = m_nMaxSize = 0; |
| 26 return 0 == nNewSize; | 26 return 0 == nNewSize; |
| 27 } | 27 } |
| 28 | 28 |
| 29 if (!m_pData) { | 29 if (!m_pData) { |
| 30 pdfium::base::CheckedNumeric<int> totalSize = nNewSize; | 30 pdfium::base::CheckedNumeric<int> totalSize = nNewSize; |
| 31 totalSize *= m_nUnitSize; | 31 totalSize *= m_nUnitSize; |
| 32 if (!totalSize.IsValid()) { | 32 if (!totalSize.IsValid()) { |
| 33 m_nSize = m_nMaxSize = 0; | 33 m_nSize = m_nMaxSize = 0; |
| 34 return FALSE; | 34 return FALSE; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 } | 75 } |
| 76 FX_BOOL CFX_BasicArray::Copy(const CFX_BasicArray& src) { | 76 FX_BOOL CFX_BasicArray::Copy(const CFX_BasicArray& src) { |
| 77 if (!SetSize(src.m_nSize)) { | 77 if (!SetSize(src.m_nSize)) { |
| 78 return FALSE; | 78 return FALSE; |
| 79 } | 79 } |
| 80 FXSYS_memcpy(m_pData, src.m_pData, src.m_nSize * m_nUnitSize); | 80 FXSYS_memcpy(m_pData, src.m_pData, src.m_nSize * m_nUnitSize); |
| 81 return TRUE; | 81 return TRUE; |
| 82 } | 82 } |
| 83 uint8_t* CFX_BasicArray::InsertSpaceAt(int nIndex, int nCount) { | 83 uint8_t* CFX_BasicArray::InsertSpaceAt(int nIndex, int nCount) { |
| 84 if (nIndex < 0 || nCount <= 0) { | 84 if (nIndex < 0 || nCount <= 0) { |
| 85 return NULL; | 85 return nullptr; |
| 86 } | 86 } |
| 87 if (nIndex >= m_nSize) { | 87 if (nIndex >= m_nSize) { |
| 88 if (!SetSize(nIndex + nCount)) { | 88 if (!SetSize(nIndex + nCount)) { |
| 89 return NULL; | 89 return nullptr; |
| 90 } | 90 } |
| 91 } else { | 91 } else { |
| 92 int nOldSize = m_nSize; | 92 int nOldSize = m_nSize; |
| 93 if (!SetSize(m_nSize + nCount)) { | 93 if (!SetSize(m_nSize + nCount)) { |
| 94 return NULL; | 94 return nullptr; |
| 95 } | 95 } |
| 96 FXSYS_memmove(m_pData + (nIndex + nCount) * m_nUnitSize, | 96 FXSYS_memmove(m_pData + (nIndex + nCount) * m_nUnitSize, |
| 97 m_pData + nIndex * m_nUnitSize, | 97 m_pData + nIndex * m_nUnitSize, |
| 98 (nOldSize - nIndex) * m_nUnitSize); | 98 (nOldSize - nIndex) * m_nUnitSize); |
| 99 FXSYS_memset(m_pData + nIndex * m_nUnitSize, 0, nCount * m_nUnitSize); | 99 FXSYS_memset(m_pData + nIndex * m_nUnitSize, 0, nCount * m_nUnitSize); |
| 100 } | 100 } |
| 101 return m_pData + nIndex * m_nUnitSize; | 101 return m_pData + nIndex * m_nUnitSize; |
| 102 } | 102 } |
| 103 FX_BOOL CFX_BasicArray::RemoveAt(int nIndex, int nCount) { | 103 FX_BOOL CFX_BasicArray::RemoveAt(int nIndex, int nCount) { |
| 104 if (nIndex < 0 || nCount <= 0 || m_nSize < nIndex + nCount) { | 104 if (nIndex < 0 || nCount <= 0 || m_nSize < nIndex + nCount) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 123 } | 123 } |
| 124 if (!InsertSpaceAt(nStartIndex, pNewArray->m_nSize)) { | 124 if (!InsertSpaceAt(nStartIndex, pNewArray->m_nSize)) { |
| 125 return FALSE; | 125 return FALSE; |
| 126 } | 126 } |
| 127 FXSYS_memcpy(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData, | 127 FXSYS_memcpy(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData, |
| 128 pNewArray->m_nSize * m_nUnitSize); | 128 pNewArray->m_nSize * m_nUnitSize); |
| 129 return TRUE; | 129 return TRUE; |
| 130 } | 130 } |
| 131 const void* CFX_BasicArray::GetDataPtr(int index) const { | 131 const void* CFX_BasicArray::GetDataPtr(int index) const { |
| 132 if (index < 0 || index >= m_nSize || !m_pData) { | 132 if (index < 0 || index >= m_nSize || !m_pData) { |
| 133 return NULL; | 133 return nullptr; |
| 134 } | 134 } |
| 135 return m_pData + index * m_nUnitSize; | 135 return m_pData + index * m_nUnitSize; |
| 136 } | 136 } |
| OLD | NEW |