summaryrefslogtreecommitdiff
path: root/core/fxcrt/cfx_memorystream.cpp
blob: dcd21f073f245dcf05d4852da74f138af51306bc (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2017 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/cfx_memorystream.h"

#include <algorithm>

#include "core/fxcrt/fx_safe_types.h"

namespace {

const int32_t kBlockSize = 64 * 1024;

}  // namespace

CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive)
    : m_nTotalSize(0),
      m_nCurSize(0),
      m_nCurPos(0),
      m_nGrowSize(kBlockSize),
      m_dwFlags(Type::kTakeOver | (bConsecutive ? Type::kConsecutive : 0)) {}

CFX_MemoryStream::CFX_MemoryStream(uint8_t* pBuffer,
                                   size_t nSize,
                                   bool bTakeOver)
    : m_nTotalSize(nSize),
      m_nCurSize(nSize),
      m_nCurPos(0),
      m_nGrowSize(kBlockSize),
      m_dwFlags(Type::kConsecutive | (bTakeOver ? Type::kTakeOver : 0)) {
  m_Blocks.push_back(pBuffer);
}

CFX_MemoryStream::~CFX_MemoryStream() {
  if (m_dwFlags & Type::kTakeOver) {
    for (uint8_t* pBlock : m_Blocks)
      FX_Free(pBlock);
  }
}

FX_FILESIZE CFX_MemoryStream::GetSize() {
  return static_cast<FX_FILESIZE>(m_nCurSize);
}

bool CFX_MemoryStream::IsEOF() {
  return m_nCurPos >= static_cast<size_t>(GetSize());
}

FX_FILESIZE CFX_MemoryStream::GetPosition() {
  return static_cast<FX_FILESIZE>(m_nCurPos);
}

bool CFX_MemoryStream::Flush() {
  return true;
}

bool CFX_MemoryStream::ReadBlock(void* buffer,
                                 FX_FILESIZE offset,
                                 size_t size) {
  if (!buffer || !size || offset < 0)
    return false;

  FX_SAFE_SIZE_T newPos = size;
  newPos += offset;
  if (!newPos.IsValid() || newPos.ValueOrDefault(0) == 0 ||
      newPos.ValueOrDie() > m_nCurSize) {
    return false;
  }

  m_nCurPos = newPos.ValueOrDie();
  if (m_dwFlags & Type::kConsecutive) {
    memcpy(buffer, m_Blocks[0] + static_cast<size_t>(offset), size);
    return true;
  }

  size_t nStartBlock = static_cast<size_t>(offset) / m_nGrowSize;
  offset -= static_cast<FX_FILESIZE>(nStartBlock * m_nGrowSize);
  while (size) {
    size_t nRead = std::min(size, m_nGrowSize - static_cast<size_t>(offset));
    memcpy(buffer, m_Blocks[nStartBlock] + offset, nRead);
    buffer = static_cast<uint8_t*>(buffer) + nRead;
    size -= nRead;
    ++nStartBlock;
    offset = 0;
  }
  return true;
}

size_t CFX_MemoryStream::ReadBlock(void* buffer, size_t size) {
  if (m_nCurPos >= m_nCurSize)
    return 0;

  size_t nRead = std::min(size, m_nCurSize - m_nCurPos);
  if (!ReadBlock(buffer, static_cast<int32_t>(m_nCurPos), nRead))
    return 0;

  return nRead;
}

bool CFX_MemoryStream::WriteBlock(const void* buffer,
                                  FX_FILESIZE offset,
                                  size_t size) {
  if (!buffer || !size)
    return false;

  if (m_dwFlags & Type::kConsecutive) {
    FX_SAFE_SIZE_T newPos = size;
    newPos += offset;
    if (!newPos.IsValid())
      return false;

    m_nCurPos = newPos.ValueOrDie();
    if (m_nCurPos > m_nTotalSize) {
      m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize;
      if (m_Blocks.empty())
        m_Blocks.push_back(FX_Alloc(uint8_t, m_nTotalSize));
      else
        m_Blocks[0] = FX_Realloc(uint8_t, m_Blocks[0], m_nTotalSize);
    }

    memcpy(m_Blocks[0] + offset, buffer, size);
    m_nCurSize = std::max(m_nCurSize, m_nCurPos);

    return true;
  }

  FX_SAFE_SIZE_T newPos = size;
  newPos += offset;
  if (!newPos.IsValid())
    return false;
  if (!ExpandBlocks(newPos.ValueOrDie()))
    return false;

  m_nCurPos = newPos.ValueOrDie();
  size_t nStartBlock = static_cast<size_t>(offset) / m_nGrowSize;
  offset -= static_cast<FX_FILESIZE>(nStartBlock * m_nGrowSize);
  while (size) {
    size_t nWrite = std::min(size, m_nGrowSize - static_cast<size_t>(offset));
    memcpy(m_Blocks[nStartBlock] + offset, buffer, nWrite);
    buffer = static_cast<const uint8_t*>(buffer) + nWrite;
    size -= nWrite;
    ++nStartBlock;
    offset = 0;
  }
  return true;
}

bool CFX_MemoryStream::Seek(size_t pos) {
  if (pos > m_nCurSize)
    return false;

  m_nCurPos = pos;
  return true;
}

void CFX_MemoryStream::EstimateSize(size_t nInitSize, size_t nGrowSize) {
  if (m_dwFlags & Type::kConsecutive) {
    if (m_Blocks.empty()) {
      m_Blocks.push_back(
          FX_Alloc(uint8_t, std::max(nInitSize, static_cast<size_t>(4096))));
    }
    m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096));
  } else if (m_Blocks.empty()) {
    m_nGrowSize = std::max(nGrowSize, static_cast<size_t>(4096));
  }
}

void CFX_MemoryStream::AttachBuffer(uint8_t* pBuffer, size_t nSize) {
  if (!(m_dwFlags & Type::kConsecutive))
    return;

  m_Blocks.clear();
  m_Blocks.push_back(pBuffer);
  m_nTotalSize = nSize;
  m_nCurSize = nSize;
  m_nCurPos = 0;
  m_dwFlags = Type::kConsecutive;
}

void CFX_MemoryStream::DetachBuffer() {
  if (!(m_dwFlags & Type::kConsecutive))
    return;

  m_Blocks.clear();
  m_nTotalSize = 0;
  m_nCurSize = 0;
  m_nCurPos = 0;
  m_dwFlags = Type::kTakeOver;
}

bool CFX_MemoryStream::ExpandBlocks(size_t size) {
  m_nCurSize = std::max(m_nCurSize, size);
  if (size <= m_nTotalSize)
    return true;

  size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize;
  size_t iCount = m_Blocks.size();
  m_Blocks.resize(iCount + size);
  while (size--) {
    m_Blocks[iCount++] = FX_Alloc(uint8_t, m_nGrowSize);
    m_nTotalSize += m_nGrowSize;
  }
  return true;
}