1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include <algorithm>
8 #include <limits>
9 #include <memory>
10 #include <utility>
11
12 #include "core/fxcrt/fx_basic.h"
13 #include "core/fxcrt/fx_safe_types.h"
14 #include "third_party/base/numerics/safe_conversions.h"
15
CFX_BinaryBuf()16 CFX_BinaryBuf::CFX_BinaryBuf()
17 : m_AllocStep(0), m_AllocSize(0), m_DataSize(0) {}
18
CFX_BinaryBuf(FX_STRSIZE size)19 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size)
20 : m_AllocStep(0), m_AllocSize(size), m_DataSize(size) {
21 m_pBuffer.reset(FX_Alloc(uint8_t, size));
22 }
23
~CFX_BinaryBuf()24 CFX_BinaryBuf::~CFX_BinaryBuf() {}
25
Delete(int start_index,int count)26 void CFX_BinaryBuf::Delete(int start_index, int count) {
27 if (!m_pBuffer || start_index < 0 || count < 0 || count > m_DataSize ||
28 start_index > m_DataSize - count) {
29 return;
30 }
31 FXSYS_memmove(m_pBuffer.get() + start_index,
32 m_pBuffer.get() + start_index + count,
33 m_DataSize - start_index - count);
34 m_DataSize -= count;
35 }
36
Clear()37 void CFX_BinaryBuf::Clear() {
38 m_DataSize = 0;
39 }
40
DetachBuffer()41 std::unique_ptr<uint8_t, FxFreeDeleter> CFX_BinaryBuf::DetachBuffer() {
42 m_DataSize = 0;
43 m_AllocSize = 0;
44 return std::move(m_pBuffer);
45 }
46
EstimateSize(FX_STRSIZE size,FX_STRSIZE step)47 void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) {
48 m_AllocStep = step;
49 if (m_AllocSize < size)
50 ExpandBuf(size - m_DataSize);
51 }
52
ExpandBuf(FX_STRSIZE add_size)53 void CFX_BinaryBuf::ExpandBuf(FX_STRSIZE add_size) {
54 FX_SAFE_STRSIZE new_size = m_DataSize;
55 new_size += add_size;
56 if (m_AllocSize >= new_size.ValueOrDie())
57 return;
58
59 int alloc_step = std::max(128, m_AllocStep ? m_AllocStep : m_AllocSize / 4);
60 new_size += alloc_step - 1; // Quantize, don't combine these lines.
61 new_size /= alloc_step;
62 new_size *= alloc_step;
63 m_AllocSize = new_size.ValueOrDie();
64 m_pBuffer.reset(m_pBuffer
65 ? FX_Realloc(uint8_t, m_pBuffer.release(), m_AllocSize)
66 : FX_Alloc(uint8_t, m_AllocSize));
67 }
68
AppendBlock(const void * pBuf,FX_STRSIZE size)69 void CFX_BinaryBuf::AppendBlock(const void* pBuf, FX_STRSIZE size) {
70 if (size <= 0)
71 return;
72
73 ExpandBuf(size);
74 if (pBuf) {
75 FXSYS_memcpy(m_pBuffer.get() + m_DataSize, pBuf, size);
76 } else {
77 FXSYS_memset(m_pBuffer.get() + m_DataSize, 0, size);
78 }
79 m_DataSize += size;
80 }
81
InsertBlock(FX_STRSIZE pos,const void * pBuf,FX_STRSIZE size)82 void CFX_BinaryBuf::InsertBlock(FX_STRSIZE pos,
83 const void* pBuf,
84 FX_STRSIZE size) {
85 if (size <= 0)
86 return;
87
88 ExpandBuf(size);
89 FXSYS_memmove(m_pBuffer.get() + pos + size, m_pBuffer.get() + pos,
90 m_DataSize - pos);
91 if (pBuf) {
92 FXSYS_memcpy(m_pBuffer.get() + pos, pBuf, size);
93 } else {
94 FXSYS_memset(m_pBuffer.get() + pos, 0, size);
95 }
96 m_DataSize += size;
97 }
98
operator <<(const CFX_ByteStringC & lpsz)99 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(const CFX_ByteStringC& lpsz) {
100 AppendBlock(lpsz.raw_str(), lpsz.GetLength());
101 return *this;
102 }
103
operator <<(int i)104 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(int i) {
105 char buf[32];
106 FXSYS_itoa(i, buf, 10);
107 AppendBlock(buf, FXSYS_strlen(buf));
108 return *this;
109 }
110
operator <<(uint32_t i)111 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(uint32_t i) {
112 char buf[32];
113 FXSYS_itoa(i, buf, 10);
114 AppendBlock(buf, FXSYS_strlen(buf));
115 return *this;
116 }
117
operator <<(double f)118 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(double f) {
119 char buf[32];
120 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf);
121 AppendBlock(buf, len);
122 return *this;
123 }
124
operator <<(const CFX_ByteTextBuf & buf)125 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(const CFX_ByteTextBuf& buf) {
126 AppendBlock(buf.m_pBuffer.get(), buf.m_DataSize);
127 return *this;
128 }
129
AppendChar(FX_WCHAR ch)130 void CFX_WideTextBuf::AppendChar(FX_WCHAR ch) {
131 ExpandBuf(sizeof(FX_WCHAR));
132 *(FX_WCHAR*)(m_pBuffer.get() + m_DataSize) = ch;
133 m_DataSize += sizeof(FX_WCHAR);
134 }
135
operator <<(const CFX_WideStringC & str)136 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideStringC& str) {
137 AppendBlock(str.c_str(), str.GetLength() * sizeof(FX_WCHAR));
138 return *this;
139 }
140
operator <<(const CFX_WideString & str)141 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideString& str) {
142 AppendBlock(str.c_str(), str.GetLength() * sizeof(FX_WCHAR));
143 return *this;
144 }
145
operator <<(int i)146 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(int i) {
147 char buf[32];
148 FXSYS_itoa(i, buf, 10);
149 FX_STRSIZE len = FXSYS_strlen(buf);
150 ExpandBuf(len * sizeof(FX_WCHAR));
151 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer.get() + m_DataSize);
152 for (FX_STRSIZE j = 0; j < len; j++) {
153 *str++ = buf[j];
154 }
155 m_DataSize += len * sizeof(FX_WCHAR);
156 return *this;
157 }
158
operator <<(double f)159 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(double f) {
160 char buf[32];
161 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf);
162 ExpandBuf(len * sizeof(FX_WCHAR));
163 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer.get() + m_DataSize);
164 for (FX_STRSIZE i = 0; i < len; i++) {
165 *str++ = buf[i];
166 }
167 m_DataSize += len * sizeof(FX_WCHAR);
168 return *this;
169 }
170
operator <<(const FX_WCHAR * lpsz)171 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const FX_WCHAR* lpsz) {
172 AppendBlock(lpsz, FXSYS_wcslen(lpsz) * sizeof(FX_WCHAR));
173 return *this;
174 }
175
operator <<(const CFX_WideTextBuf & buf)176 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideTextBuf& buf) {
177 AppendBlock(buf.m_pBuffer.get(), buf.m_DataSize);
178 return *this;
179 }
180
Init(const uint8_t * pData,uint32_t dwSize)181 void CFX_BitStream::Init(const uint8_t* pData, uint32_t dwSize) {
182 m_pData = pData;
183 m_BitSize = dwSize * 8;
184 m_BitPos = 0;
185 }
186
ByteAlign()187 void CFX_BitStream::ByteAlign() {
188 m_BitPos = (m_BitPos + 7) & ~7;
189 }
190
GetBits(uint32_t nBits)191 uint32_t CFX_BitStream::GetBits(uint32_t nBits) {
192 if (nBits > m_BitSize || m_BitPos + nBits > m_BitSize)
193 return 0;
194
195 if (nBits == 1) {
196 int bit = (m_pData[m_BitPos / 8] & (1 << (7 - m_BitPos % 8))) ? 1 : 0;
197 m_BitPos++;
198 return bit;
199 }
200
201 uint32_t byte_pos = m_BitPos / 8;
202 uint32_t bit_pos = m_BitPos % 8;
203 uint32_t bit_left = nBits;
204 uint32_t result = 0;
205 if (bit_pos) {
206 if (8 - bit_pos >= bit_left) {
207 result =
208 (m_pData[byte_pos] & (0xff >> bit_pos)) >> (8 - bit_pos - bit_left);
209 m_BitPos += bit_left;
210 return result;
211 }
212 bit_left -= 8 - bit_pos;
213 result = (m_pData[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left;
214 }
215 while (bit_left >= 8) {
216 bit_left -= 8;
217 result |= m_pData[byte_pos++] << bit_left;
218 }
219 if (bit_left)
220 result |= m_pData[byte_pos] >> (8 - bit_left);
221 m_BitPos += nBits;
222 return result;
223 }
224
CFX_FileBufferArchive()225 CFX_FileBufferArchive::CFX_FileBufferArchive()
226 : m_Length(0), m_pFile(nullptr) {}
227
~CFX_FileBufferArchive()228 CFX_FileBufferArchive::~CFX_FileBufferArchive() {}
229
Clear()230 void CFX_FileBufferArchive::Clear() {
231 m_Length = 0;
232 m_pBuffer.reset();
233 m_pFile.Reset();
234 }
235
Flush()236 bool CFX_FileBufferArchive::Flush() {
237 size_t nRemaining = m_Length;
238 m_Length = 0;
239 if (!m_pFile)
240 return false;
241 if (!m_pBuffer || !nRemaining)
242 return true;
243 return m_pFile->WriteBlock(m_pBuffer.get(), nRemaining);
244 }
245
AppendBlock(const void * pBuf,size_t size)246 int32_t CFX_FileBufferArchive::AppendBlock(const void* pBuf, size_t size) {
247 if (!pBuf || size < 1)
248 return 0;
249
250 if (!m_pBuffer)
251 m_pBuffer.reset(FX_Alloc(uint8_t, kBufSize));
252
253 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(pBuf);
254 size_t temp_size = size;
255 while (temp_size) {
256 size_t buf_size = std::min(kBufSize - m_Length, temp_size);
257 FXSYS_memcpy(m_pBuffer.get() + m_Length, buffer, buf_size);
258 m_Length += buf_size;
259 if (m_Length == kBufSize) {
260 if (!Flush()) {
261 return -1;
262 }
263 }
264 temp_size -= buf_size;
265 buffer += buf_size;
266 }
267 return pdfium::base::checked_cast<int32_t>(size);
268 }
269
AppendByte(uint8_t byte)270 int32_t CFX_FileBufferArchive::AppendByte(uint8_t byte) {
271 return AppendBlock(&byte, 1);
272 }
273
AppendDWord(uint32_t i)274 int32_t CFX_FileBufferArchive::AppendDWord(uint32_t i) {
275 char buf[32];
276 FXSYS_itoa(i, buf, 10);
277 return AppendBlock(buf, (size_t)FXSYS_strlen(buf));
278 }
279
AppendString(const CFX_ByteStringC & lpsz)280 int32_t CFX_FileBufferArchive::AppendString(const CFX_ByteStringC& lpsz) {
281 return AppendBlock(lpsz.raw_str(), lpsz.GetLength());
282 }
283
AttachFile(const CFX_RetainPtr<IFX_WriteStream> & pFile)284 void CFX_FileBufferArchive::AttachFile(
285 const CFX_RetainPtr<IFX_WriteStream>& pFile) {
286 ASSERT(pFile);
287 m_pFile = pFile;
288 }
289