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 <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> |
8 | 9 |
9 #include "core/include/fxcrt/fx_basic.h" | 10 #include "core/include/fxcrt/fx_basic.h" |
| 11 #include "core/include/fxcrt/fx_safe_types.h" |
10 | 12 |
11 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); | |
12 CFX_BinaryBuf::CFX_BinaryBuf() | 13 CFX_BinaryBuf::CFX_BinaryBuf() |
13 : m_AllocStep(0), m_pBuffer(NULL), m_DataSize(0), m_AllocSize(0) {} | 14 : m_AllocStep(0), m_AllocSize(0), m_DataSize(0) {} |
| 15 |
14 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) | 16 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) |
15 : m_AllocStep(0), m_DataSize(size), m_AllocSize(size) { | 17 : m_AllocStep(0), m_AllocSize(size), m_DataSize(size) { |
16 m_pBuffer = FX_Alloc(uint8_t, size); | 18 m_pBuffer.reset(FX_Alloc(uint8_t, size)); |
17 } | 19 } |
18 CFX_BinaryBuf::~CFX_BinaryBuf() { | 20 |
19 FX_Free(m_pBuffer); | |
20 } | |
21 void CFX_BinaryBuf::Delete(int start_index, int count) { | 21 void CFX_BinaryBuf::Delete(int start_index, int count) { |
22 if (!m_pBuffer || start_index < 0 || start_index + count > m_DataSize) { | 22 if (!m_pBuffer || start_index < 0 || count < 0 || count > m_DataSize || |
| 23 start_index > m_DataSize - count) { |
23 return; | 24 return; |
24 } | 25 } |
25 FXSYS_memmove(m_pBuffer + start_index, m_pBuffer + start_index + count, | 26 FXSYS_memmove(m_pBuffer.get() + start_index, |
| 27 m_pBuffer.get() + start_index + count, |
26 m_DataSize - start_index - count); | 28 m_DataSize - start_index - count); |
27 m_DataSize -= count; | 29 m_DataSize -= count; |
28 } | 30 } |
| 31 |
29 void CFX_BinaryBuf::Clear() { | 32 void CFX_BinaryBuf::Clear() { |
30 m_DataSize = 0; | 33 m_DataSize = 0; |
31 } | 34 } |
32 void CFX_BinaryBuf::DetachBuffer() { | 35 |
| 36 uint8_t* CFX_BinaryBuf::DetachBuffer() { |
33 m_DataSize = 0; | 37 m_DataSize = 0; |
34 m_pBuffer = NULL; | |
35 m_AllocSize = 0; | 38 m_AllocSize = 0; |
| 39 return m_pBuffer.release(); |
36 } | 40 } |
37 void CFX_BinaryBuf::AttachData(void* buffer, FX_STRSIZE size) { | 41 |
38 FX_Free(m_pBuffer); | 42 void CFX_BinaryBuf::AttachData(uint8_t* buffer, FX_STRSIZE size) { |
| 43 m_pBuffer.reset(buffer); |
39 m_DataSize = size; | 44 m_DataSize = size; |
40 m_pBuffer = (uint8_t*)buffer; | |
41 m_AllocSize = size; | 45 m_AllocSize = size; |
42 } | 46 } |
43 void CFX_BinaryBuf::TakeOver(CFX_BinaryBuf& other) { | 47 |
44 AttachData(other.GetBuffer(), other.GetSize()); | |
45 other.DetachBuffer(); | |
46 } | |
47 void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) { | 48 void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) { |
48 m_AllocStep = step; | 49 m_AllocStep = step; |
49 if (m_AllocSize >= size) { | 50 if (m_AllocSize < size) |
| 51 ExpandBuf(size - m_DataSize); |
| 52 } |
| 53 |
| 54 void CFX_BinaryBuf::ExpandBuf(FX_STRSIZE add_size) { |
| 55 FX_SAFE_STRSIZE new_size = m_DataSize; |
| 56 new_size += add_size; |
| 57 if (m_AllocSize >= new_size.ValueOrDie()) |
50 return; | 58 return; |
51 } | 59 |
52 ExpandBuf(size - m_DataSize); | 60 int alloc_step = std::min(128, m_AllocStep ? m_AllocStep : m_AllocSize / 4); |
| 61 new_size += alloc_step - 1; // Quantize, don't combine these lines. |
| 62 new_size /= alloc_step; |
| 63 new_size *= alloc_step; |
| 64 m_AllocSize = new_size.ValueOrDie(); |
| 65 m_pBuffer.reset(m_pBuffer |
| 66 ? FX_Realloc(uint8_t, m_pBuffer.release(), m_AllocSize) |
| 67 : FX_Alloc(uint8_t, m_AllocSize)); |
53 } | 68 } |
54 void CFX_BinaryBuf::ExpandBuf(FX_STRSIZE add_size) { | 69 |
55 FX_STRSIZE new_size = add_size + m_DataSize; | 70 void CFX_BinaryBuf::AppendBlock(const void* pBuf, FX_STRSIZE size) { |
56 if (m_AllocSize >= new_size) { | 71 if (size <= 0) |
57 return; | 72 return; |
58 } | 73 |
59 int alloc_step; | 74 ExpandBuf(size); |
60 if (m_AllocStep == 0) { | 75 if (pBuf) { |
61 alloc_step = m_AllocSize / 4; | 76 FXSYS_memcpy(m_pBuffer.get() + m_DataSize, pBuf, size); |
62 if (alloc_step < 128) { | |
63 alloc_step = 128; | |
64 } | |
65 } else { | 77 } else { |
66 alloc_step = m_AllocStep; | 78 FXSYS_memset(m_pBuffer.get() + m_DataSize, 0, size); |
67 } | |
68 new_size = (new_size + alloc_step - 1) / alloc_step * alloc_step; | |
69 uint8_t* pNewBuffer = m_pBuffer; | |
70 if (pNewBuffer) { | |
71 pNewBuffer = FX_Realloc(uint8_t, m_pBuffer, new_size); | |
72 } else { | |
73 pNewBuffer = FX_Alloc(uint8_t, new_size); | |
74 } | |
75 m_pBuffer = pNewBuffer; | |
76 m_AllocSize = new_size; | |
77 } | |
78 void CFX_BinaryBuf::CopyData(const void* pStr, FX_STRSIZE size) { | |
79 if (size == 0) { | |
80 m_DataSize = 0; | |
81 return; | |
82 } | |
83 if (m_AllocSize < size) { | |
84 ExpandBuf(size - m_DataSize); | |
85 } | |
86 if (!m_pBuffer) { | |
87 return; | |
88 } | |
89 FXSYS_memcpy(m_pBuffer, pStr, size); | |
90 m_DataSize = size; | |
91 } | |
92 void CFX_BinaryBuf::AppendBlock(const void* pBuf, FX_STRSIZE size) { | |
93 ExpandBuf(size); | |
94 if (pBuf && m_pBuffer) { | |
95 FXSYS_memcpy(m_pBuffer + m_DataSize, pBuf, size); | |
96 } | 79 } |
97 m_DataSize += size; | 80 m_DataSize += size; |
98 } | 81 } |
| 82 |
99 void CFX_BinaryBuf::InsertBlock(FX_STRSIZE pos, | 83 void CFX_BinaryBuf::InsertBlock(FX_STRSIZE pos, |
100 const void* pBuf, | 84 const void* pBuf, |
101 FX_STRSIZE size) { | 85 FX_STRSIZE size) { |
| 86 if (size <= 0) |
| 87 return; |
| 88 |
102 ExpandBuf(size); | 89 ExpandBuf(size); |
103 if (!m_pBuffer) { | 90 FXSYS_memmove(m_pBuffer.get() + pos + size, m_pBuffer.get() + pos, |
104 return; | 91 m_DataSize - pos); |
105 } | |
106 FXSYS_memmove(m_pBuffer + pos + size, m_pBuffer + pos, m_DataSize - pos); | |
107 if (pBuf) { | 92 if (pBuf) { |
108 FXSYS_memcpy(m_pBuffer + pos, pBuf, size); | 93 FXSYS_memcpy(m_pBuffer.get() + pos, pBuf, size); |
| 94 } else { |
| 95 FXSYS_memset(m_pBuffer.get() + pos, 0, size); |
109 } | 96 } |
110 m_DataSize += size; | 97 m_DataSize += size; |
111 } | 98 } |
112 void CFX_BinaryBuf::AppendFill(uint8_t byte, FX_STRSIZE count) { | 99 |
113 ExpandBuf(count); | 100 CFX_ByteStringC CFX_ByteTextBuf::GetByteString() const { |
114 if (!m_pBuffer) { | 101 return CFX_ByteStringC(m_pBuffer.get(), m_DataSize); |
115 return; | |
116 } | |
117 FXSYS_memset(m_pBuffer + m_DataSize, byte, count); | |
118 m_DataSize += count; | |
119 } | 102 } |
120 CFX_ByteStringC CFX_BinaryBuf::GetByteString() const { | 103 |
121 return CFX_ByteStringC(m_pBuffer, m_DataSize); | |
122 } | |
123 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(const CFX_ByteStringC& lpsz) { | 104 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(const CFX_ByteStringC& lpsz) { |
124 AppendBlock(lpsz.GetPtr(), lpsz.GetLength()); | 105 AppendBlock(lpsz.GetPtr(), lpsz.GetLength()); |
125 return *this; | 106 return *this; |
126 } | 107 } |
| 108 |
127 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(int i) { | 109 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(int i) { |
128 char buf[32]; | 110 char buf[32]; |
129 FXSYS_itoa(i, buf, 10); | 111 FXSYS_itoa(i, buf, 10); |
130 AppendBlock(buf, FXSYS_strlen(buf)); | 112 AppendBlock(buf, FXSYS_strlen(buf)); |
131 return *this; | 113 return *this; |
132 } | 114 } |
| 115 |
133 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(FX_DWORD i) { | 116 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(FX_DWORD i) { |
134 char buf[32]; | 117 char buf[32]; |
135 FXSYS_itoa(i, buf, 10); | 118 FXSYS_itoa(i, buf, 10); |
136 AppendBlock(buf, FXSYS_strlen(buf)); | 119 AppendBlock(buf, FXSYS_strlen(buf)); |
137 return *this; | 120 return *this; |
138 } | 121 } |
| 122 |
139 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(double f) { | 123 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(double f) { |
140 char buf[32]; | 124 char buf[32]; |
141 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); | 125 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); |
142 AppendBlock(buf, len); | 126 AppendBlock(buf, len); |
143 return *this; | 127 return *this; |
144 } | 128 } |
| 129 |
145 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(const CFX_ByteTextBuf& buf) { | 130 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(const CFX_ByteTextBuf& buf) { |
146 AppendBlock(buf.m_pBuffer, buf.m_DataSize); | 131 AppendBlock(buf.m_pBuffer.get(), buf.m_DataSize); |
147 return *this; | 132 return *this; |
148 } | 133 } |
149 void CFX_ByteTextBuf::operator=(const CFX_ByteStringC& str) { | 134 |
150 CopyData(str.GetPtr(), str.GetLength()); | |
151 } | |
152 void CFX_WideTextBuf::AppendChar(FX_WCHAR ch) { | 135 void CFX_WideTextBuf::AppendChar(FX_WCHAR ch) { |
153 if (m_AllocSize < m_DataSize + (FX_STRSIZE)sizeof(FX_WCHAR)) { | 136 ExpandBuf(sizeof(FX_WCHAR)); |
154 ExpandBuf(sizeof(FX_WCHAR)); | 137 *(FX_WCHAR*)(m_pBuffer.get() + m_DataSize) = ch; |
155 } | |
156 ASSERT(m_pBuffer); | |
157 *(FX_WCHAR*)(m_pBuffer + m_DataSize) = ch; | |
158 m_DataSize += sizeof(FX_WCHAR); | 138 m_DataSize += sizeof(FX_WCHAR); |
159 } | 139 } |
| 140 |
160 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideStringC& str) { | 141 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideStringC& str) { |
161 AppendBlock(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); | 142 AppendBlock(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); |
162 return *this; | 143 return *this; |
163 } | 144 } |
| 145 |
164 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideString& str) { | 146 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideString& str) { |
165 AppendBlock(str.c_str(), str.GetLength() * sizeof(FX_WCHAR)); | 147 AppendBlock(str.c_str(), str.GetLength() * sizeof(FX_WCHAR)); |
166 return *this; | 148 return *this; |
167 } | 149 } |
| 150 |
168 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(int i) { | 151 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(int i) { |
169 char buf[32]; | 152 char buf[32]; |
170 FXSYS_itoa(i, buf, 10); | 153 FXSYS_itoa(i, buf, 10); |
171 FX_STRSIZE len = FXSYS_strlen(buf); | 154 FX_STRSIZE len = FXSYS_strlen(buf); |
172 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { | 155 ExpandBuf(len * sizeof(FX_WCHAR)); |
173 ExpandBuf(len * sizeof(FX_WCHAR)); | 156 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer.get() + m_DataSize); |
174 } | |
175 ASSERT(m_pBuffer); | |
176 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer + m_DataSize); | |
177 for (FX_STRSIZE j = 0; j < len; j++) { | 157 for (FX_STRSIZE j = 0; j < len; j++) { |
178 *str++ = buf[j]; | 158 *str++ = buf[j]; |
179 } | 159 } |
180 m_DataSize += len * sizeof(FX_WCHAR); | 160 m_DataSize += len * sizeof(FX_WCHAR); |
181 return *this; | 161 return *this; |
182 } | 162 } |
| 163 |
183 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(double f) { | 164 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(double f) { |
184 char buf[32]; | 165 char buf[32]; |
185 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); | 166 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); |
186 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { | 167 ExpandBuf(len * sizeof(FX_WCHAR)); |
187 ExpandBuf(len * sizeof(FX_WCHAR)); | 168 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer.get() + m_DataSize); |
188 } | |
189 ASSERT(m_pBuffer); | |
190 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer + m_DataSize); | |
191 for (FX_STRSIZE i = 0; i < len; i++) { | 169 for (FX_STRSIZE i = 0; i < len; i++) { |
192 *str++ = buf[i]; | 170 *str++ = buf[i]; |
193 } | 171 } |
194 m_DataSize += len * sizeof(FX_WCHAR); | 172 m_DataSize += len * sizeof(FX_WCHAR); |
195 return *this; | 173 return *this; |
196 } | 174 } |
| 175 |
197 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const FX_WCHAR* lpsz) { | 176 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const FX_WCHAR* lpsz) { |
198 AppendBlock(lpsz, FXSYS_wcslen(lpsz) * sizeof(FX_WCHAR)); | 177 AppendBlock(lpsz, FXSYS_wcslen(lpsz) * sizeof(FX_WCHAR)); |
199 return *this; | 178 return *this; |
200 } | 179 } |
| 180 |
201 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideTextBuf& buf) { | 181 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideTextBuf& buf) { |
202 AppendBlock(buf.m_pBuffer, buf.m_DataSize); | 182 AppendBlock(buf.m_pBuffer.get(), buf.m_DataSize); |
203 return *this; | 183 return *this; |
204 } | 184 } |
205 void CFX_WideTextBuf::operator=(const CFX_WideStringC& str) { | 185 |
206 CopyData(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); | |
207 } | |
208 CFX_WideStringC CFX_WideTextBuf::GetWideString() const { | 186 CFX_WideStringC CFX_WideTextBuf::GetWideString() const { |
209 return CFX_WideStringC((const FX_WCHAR*)m_pBuffer, | 187 return CFX_WideStringC((const FX_WCHAR*)m_pBuffer.get(), |
210 m_DataSize / sizeof(FX_WCHAR)); | 188 m_DataSize / sizeof(FX_WCHAR)); |
211 } | 189 } |
212 | 190 |
213 #ifdef PDF_ENABLE_XFA | 191 #ifdef PDF_ENABLE_XFA |
214 CFX_ArchiveSaver& CFX_ArchiveSaver::operator<<(uint8_t i) { | 192 CFX_ArchiveSaver& CFX_ArchiveSaver::operator<<(uint8_t i) { |
215 if (m_pStream) { | 193 if (m_pStream) { |
216 m_pStream->WriteBlock(&i, 1); | 194 m_pStream->WriteBlock(&i, 1); |
217 } else { | 195 } else { |
218 m_SavingBuf.AppendByte(i); | 196 m_SavingBuf.AppendByte(i); |
219 } | 197 } |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 } | 432 } |
455 FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) { | 433 FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) { |
456 if (!m_pFile) { | 434 if (!m_pFile) { |
457 return FALSE; | 435 return FALSE; |
458 } | 436 } |
459 if (!pBuf || size < 1) { | 437 if (!pBuf || size < 1) { |
460 return TRUE; | 438 return TRUE; |
461 } | 439 } |
462 return m_pFile->WriteBlock(pBuf, size); | 440 return m_pFile->WriteBlock(pBuf, size); |
463 } | 441 } |
OLD | NEW |