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