summaryrefslogtreecommitdiff
path: root/core/fxcrt/fx_basic_array.cpp
blob: 92df0e00f8d4b9d6789382a0d147dc64fb15f119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "core/fxcrt/fx_basic.h"
#include "third_party/base/numerics/safe_math.h"

CFX_BasicArray::CFX_BasicArray(int unit_size)
    : m_pData(nullptr), m_nSize(0), m_nMaxSize(0) {
  if (unit_size < 0 || unit_size > (1 << 28)) {
    m_nUnitSize = 4;
  } else {
    m_nUnitSize = unit_size;
  }
}
CFX_BasicArray::~CFX_BasicArray() {
  FX_Free(m_pData);
}
bool CFX_BasicArray::SetSize(int nNewSize) {
  if (nNewSize <= 0) {
    FX_Free(m_pData);
    m_pData = nullptr;
    m_nSize = m_nMaxSize = 0;
    return 0 == nNewSize;
  }

  if (!m_pData) {
    pdfium::base::CheckedNumeric<int> totalSize = nNewSize;
    totalSize *= m_nUnitSize;
    if (!totalSize.IsValid()) {
      m_nSize = m_nMaxSize = 0;
      return false;
    }
    m_pData = FX_Alloc(uint8_t, totalSize.ValueOrDie());
    m_nSize = m_nMaxSize = nNewSize;
  } else if (nNewSize <= m_nMaxSize) {
    if (nNewSize > m_nSize) {
      FXSYS_memset(m_pData + m_nSize * m_nUnitSize, 0,
                   (nNewSize - m_nSize) * m_nUnitSize);
    }
    m_nSize = nNewSize;
  } else {
    int nNewMax = nNewSize < m_nMaxSize ? m_nMaxSize : nNewSize;
    pdfium::base::CheckedNumeric<int> totalSize = nNewMax;
    totalSize *= m_nUnitSize;
    if (!totalSize.IsValid() || nNewMax < m_nSize) {
      return false;
    }
    uint8_t* pNewData = FX_Realloc(uint8_t, m_pData, totalSize.ValueOrDie());
    if (!pNewData) {
      return false;
    }
    FXSYS_memset(pNewData + m_nSize * m_nUnitSize, 0,
                 (nNewMax - m_nSize) * m_nUnitSize);
    m_pData = pNewData;
    m_nSize = nNewSize;
    m_nMaxSize = nNewMax;
  }
  return true;
}
bool CFX_BasicArray::Append(const CFX_BasicArray& src) {
  int nOldSize = m_nSize;
  pdfium::base::CheckedNumeric<int> newSize = m_nSize;
  newSize += src.m_nSize;
  if (m_nUnitSize != src.m_nUnitSize || !newSize.IsValid() ||
      !SetSize(newSize.ValueOrDie())) {
    return false;
  }

  FXSYS_memcpy(m_pData + nOldSize * m_nUnitSize, src.m_pData,
               src.m_nSize * m_nUnitSize);
  return true;
}
bool CFX_BasicArray::Copy(const CFX_BasicArray& src) {
  if (!SetSize(src.m_nSize)) {
    return false;
  }
  FXSYS_memcpy(m_pData, src.m_pData, src.m_nSize * m_nUnitSize);
  return true;
}
uint8_t* CFX_BasicArray::InsertSpaceAt(int nIndex, int nCount) {
  if (nIndex < 0 || nCount <= 0) {
    return nullptr;
  }
  if (nIndex >= m_nSize) {
    if (!SetSize(nIndex + nCount)) {
      return nullptr;
    }
  } else {
    int nOldSize = m_nSize;
    if (!SetSize(m_nSize + nCount)) {
      return nullptr;
    }
    FXSYS_memmove(m_pData + (nIndex + nCount) * m_nUnitSize,
                  m_pData + nIndex * m_nUnitSize,
                  (nOldSize - nIndex) * m_nUnitSize);
    FXSYS_memset(m_pData + nIndex * m_nUnitSize, 0, nCount * m_nUnitSize);
  }
  return m_pData + nIndex * m_nUnitSize;
}
bool CFX_BasicArray::RemoveAt(int nIndex, int nCount) {
  if (nIndex < 0 || nCount <= 0 || m_nSize < nIndex + nCount) {
    return false;
  }
  int nMoveCount = m_nSize - (nIndex + nCount);
  if (nMoveCount) {
    FXSYS_memmove(m_pData + nIndex * m_nUnitSize,
                  m_pData + (nIndex + nCount) * m_nUnitSize,
                  nMoveCount * m_nUnitSize);
  }
  m_nSize -= nCount;
  return true;
}
bool CFX_BasicArray::InsertAt(int nStartIndex,
                              const CFX_BasicArray* pNewArray) {
  if (!pNewArray) {
    return false;
  }
  if (pNewArray->m_nSize == 0) {
    return true;
  }
  if (!InsertSpaceAt(nStartIndex, pNewArray->m_nSize)) {
    return false;
  }
  FXSYS_memcpy(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData,
               pNewArray->m_nSize * m_nUnitSize);
  return true;
}
const void* CFX_BasicArray::GetDataPtr(int index) const {
  if (index < 0 || index >= m_nSize || !m_pData) {
    return nullptr;
  }
  return m_pData + index * m_nUnitSize;
}