OLD | NEW |
| (Empty) |
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 #ifndef CORE_FXCRT_INCLUDE_FX_BASIC_H_ | |
8 #define CORE_FXCRT_INCLUDE_FX_BASIC_H_ | |
9 | |
10 #include <algorithm> | |
11 #include <memory> | |
12 | |
13 #include "core/fxcrt/include/fx_memory.h" | |
14 #include "core/fxcrt/include/fx_stream.h" | |
15 #include "core/fxcrt/include/fx_string.h" | |
16 #include "core/fxcrt/include/fx_system.h" | |
17 | |
18 class CFX_BinaryBuf { | |
19 public: | |
20 CFX_BinaryBuf(); | |
21 explicit CFX_BinaryBuf(FX_STRSIZE size); | |
22 ~CFX_BinaryBuf(); | |
23 | |
24 uint8_t* GetBuffer() const { return m_pBuffer.get(); } | |
25 FX_STRSIZE GetSize() const { return m_DataSize; } | |
26 | |
27 void Clear(); | |
28 void EstimateSize(FX_STRSIZE size, FX_STRSIZE alloc_step = 0); | |
29 void AppendBlock(const void* pBuf, FX_STRSIZE size); | |
30 void AppendString(const CFX_ByteString& str) { | |
31 AppendBlock(str.c_str(), str.GetLength()); | |
32 } | |
33 | |
34 void AppendByte(uint8_t byte) { | |
35 ExpandBuf(1); | |
36 m_pBuffer.get()[m_DataSize++] = byte; | |
37 } | |
38 | |
39 void InsertBlock(FX_STRSIZE pos, const void* pBuf, FX_STRSIZE size); | |
40 void Delete(int start_index, int count); | |
41 | |
42 // Takes ownership of |pBuf|. | |
43 void AttachData(uint8_t* pBuf, FX_STRSIZE size); | |
44 | |
45 // Releases ownership of |m_pBuffer| and returns it. | |
46 uint8_t* DetachBuffer(); | |
47 | |
48 protected: | |
49 void ExpandBuf(FX_STRSIZE size); | |
50 | |
51 FX_STRSIZE m_AllocStep; | |
52 FX_STRSIZE m_AllocSize; | |
53 FX_STRSIZE m_DataSize; | |
54 std::unique_ptr<uint8_t, FxFreeDeleter> m_pBuffer; | |
55 }; | |
56 | |
57 class CFX_ByteTextBuf : public CFX_BinaryBuf { | |
58 public: | |
59 FX_STRSIZE GetLength() const { return m_DataSize; } | |
60 CFX_ByteString MakeString() const { | |
61 return CFX_ByteString(m_pBuffer.get(), m_DataSize); | |
62 } | |
63 CFX_ByteStringC AsStringC() const { | |
64 return CFX_ByteStringC(m_pBuffer.get(), m_DataSize); | |
65 } | |
66 | |
67 void AppendChar(int ch) { AppendByte(static_cast<uint8_t>(ch)); } | |
68 CFX_ByteTextBuf& operator<<(int i); | |
69 CFX_ByteTextBuf& operator<<(uint32_t i); | |
70 CFX_ByteTextBuf& operator<<(double f); | |
71 CFX_ByteTextBuf& operator<<(const FX_CHAR* pStr) { | |
72 return *this << CFX_ByteStringC(pStr); | |
73 } | |
74 CFX_ByteTextBuf& operator<<(const CFX_ByteString& str) { | |
75 return *this << str.AsStringC(); | |
76 } | |
77 CFX_ByteTextBuf& operator<<(const CFX_ByteStringC& lpsz); | |
78 CFX_ByteTextBuf& operator<<(const CFX_ByteTextBuf& buf); | |
79 }; | |
80 | |
81 class CFX_WideTextBuf : public CFX_BinaryBuf { | |
82 public: | |
83 void AppendChar(FX_WCHAR wch); | |
84 FX_STRSIZE GetLength() const { return m_DataSize / sizeof(FX_WCHAR); } | |
85 FX_WCHAR* GetBuffer() const { | |
86 return reinterpret_cast<FX_WCHAR*>(m_pBuffer.get()); | |
87 } | |
88 | |
89 CFX_WideStringC AsStringC() const { | |
90 return CFX_WideStringC(reinterpret_cast<const FX_WCHAR*>(m_pBuffer.get()), | |
91 m_DataSize / sizeof(FX_WCHAR)); | |
92 } | |
93 CFX_WideString MakeString() const { | |
94 return CFX_WideString(reinterpret_cast<const FX_WCHAR*>(m_pBuffer.get()), | |
95 m_DataSize / sizeof(FX_WCHAR)); | |
96 } | |
97 | |
98 void Delete(int start_index, int count) { | |
99 CFX_BinaryBuf::Delete(start_index * sizeof(FX_WCHAR), | |
100 count * sizeof(FX_WCHAR)); | |
101 } | |
102 | |
103 CFX_WideTextBuf& operator<<(int i); | |
104 CFX_WideTextBuf& operator<<(double f); | |
105 CFX_WideTextBuf& operator<<(const FX_WCHAR* lpsz); | |
106 CFX_WideTextBuf& operator<<(const CFX_WideStringC& str); | |
107 CFX_WideTextBuf& operator<<(const CFX_WideString& str); | |
108 CFX_WideTextBuf& operator<<(const CFX_WideTextBuf& buf); | |
109 }; | |
110 | |
111 class CFX_FileBufferArchive { | |
112 public: | |
113 CFX_FileBufferArchive(); | |
114 ~CFX_FileBufferArchive(); | |
115 | |
116 void Clear(); | |
117 bool Flush(); | |
118 int32_t AppendBlock(const void* pBuf, size_t size); | |
119 int32_t AppendByte(uint8_t byte); | |
120 int32_t AppendDWord(uint32_t i); | |
121 int32_t AppendString(const CFX_ByteStringC& lpsz); | |
122 | |
123 // |pFile| must outlive the CFX_FileBufferArchive. | |
124 void AttachFile(IFX_StreamWrite* pFile); | |
125 | |
126 private: | |
127 static const size_t kBufSize = 32768; | |
128 | |
129 size_t m_Length; | |
130 std::unique_ptr<uint8_t, FxFreeDeleter> m_pBuffer; | |
131 IFX_StreamWrite* m_pFile; | |
132 }; | |
133 | |
134 class CFX_CharMap { | |
135 public: | |
136 static CFX_ByteString GetByteString(uint16_t codepage, | |
137 const CFX_WideStringC& wstr); | |
138 | |
139 static CFX_WideString GetWideString(uint16_t codepage, | |
140 const CFX_ByteStringC& bstr); | |
141 | |
142 CFX_CharMap() = delete; | |
143 }; | |
144 | |
145 class CFX_UTF8Decoder { | |
146 public: | |
147 CFX_UTF8Decoder() { m_PendingBytes = 0; } | |
148 | |
149 void Clear(); | |
150 | |
151 void Input(uint8_t byte); | |
152 | |
153 void AppendChar(uint32_t ch); | |
154 | |
155 void ClearStatus() { m_PendingBytes = 0; } | |
156 | |
157 CFX_WideStringC GetResult() const { return m_Buffer.AsStringC(); } | |
158 | |
159 protected: | |
160 int m_PendingBytes; | |
161 | |
162 uint32_t m_PendingChar; | |
163 | |
164 CFX_WideTextBuf m_Buffer; | |
165 }; | |
166 | |
167 class CFX_UTF8Encoder { | |
168 public: | |
169 CFX_UTF8Encoder() {} | |
170 | |
171 void Input(FX_WCHAR unicode); | |
172 void AppendStr(const CFX_ByteStringC& str) { m_Buffer << str; } | |
173 CFX_ByteStringC GetResult() const { return m_Buffer.AsStringC(); } | |
174 | |
175 protected: | |
176 CFX_ByteTextBuf m_Buffer; | |
177 }; | |
178 | |
179 class CFX_BasicArray { | |
180 protected: | |
181 explicit CFX_BasicArray(int unit_size); | |
182 CFX_BasicArray(const CFX_BasicArray&) = delete; | |
183 ~CFX_BasicArray(); | |
184 | |
185 FX_BOOL SetSize(int nNewSize); | |
186 FX_BOOL Append(const CFX_BasicArray& src); | |
187 FX_BOOL Copy(const CFX_BasicArray& src); | |
188 uint8_t* InsertSpaceAt(int nIndex, int nCount); | |
189 FX_BOOL RemoveAt(int nIndex, int nCount); | |
190 FX_BOOL InsertAt(int nStartIndex, const CFX_BasicArray* pNewArray); | |
191 const void* GetDataPtr(int index) const; | |
192 | |
193 protected: | |
194 uint8_t* m_pData; | |
195 int m_nSize; | |
196 int m_nMaxSize; | |
197 int m_nUnitSize; | |
198 }; | |
199 | |
200 template <class TYPE> | |
201 class CFX_ArrayTemplate : public CFX_BasicArray { | |
202 public: | |
203 CFX_ArrayTemplate() : CFX_BasicArray(sizeof(TYPE)) {} | |
204 | |
205 int GetSize() const { return m_nSize; } | |
206 | |
207 int GetUpperBound() const { return m_nSize - 1; } | |
208 | |
209 FX_BOOL SetSize(int nNewSize) { return CFX_BasicArray::SetSize(nNewSize); } | |
210 | |
211 void RemoveAll() { SetSize(0); } | |
212 | |
213 const TYPE GetAt(int nIndex) const { | |
214 if (nIndex < 0 || nIndex >= m_nSize) { | |
215 PDFIUM_IMMEDIATE_CRASH(); | |
216 } | |
217 return ((const TYPE*)m_pData)[nIndex]; | |
218 } | |
219 | |
220 FX_BOOL SetAt(int nIndex, TYPE newElement) { | |
221 if (nIndex < 0 || nIndex >= m_nSize) { | |
222 return FALSE; | |
223 } | |
224 ((TYPE*)m_pData)[nIndex] = newElement; | |
225 return TRUE; | |
226 } | |
227 | |
228 TYPE& ElementAt(int nIndex) { | |
229 if (nIndex < 0 || nIndex >= m_nSize) { | |
230 PDFIUM_IMMEDIATE_CRASH(); | |
231 } | |
232 return ((TYPE*)m_pData)[nIndex]; | |
233 } | |
234 | |
235 const TYPE* GetData() const { return (const TYPE*)m_pData; } | |
236 | |
237 TYPE* GetData() { return (TYPE*)m_pData; } | |
238 | |
239 FX_BOOL SetAtGrow(int nIndex, TYPE newElement) { | |
240 if (nIndex < 0) | |
241 return FALSE; | |
242 | |
243 if (nIndex >= m_nSize && !SetSize(nIndex + 1)) | |
244 return FALSE; | |
245 | |
246 ((TYPE*)m_pData)[nIndex] = newElement; | |
247 return TRUE; | |
248 } | |
249 | |
250 FX_BOOL Add(TYPE newElement) { | |
251 if (m_nSize < m_nMaxSize) { | |
252 m_nSize++; | |
253 } else if (!SetSize(m_nSize + 1)) { | |
254 return FALSE; | |
255 } | |
256 ((TYPE*)m_pData)[m_nSize - 1] = newElement; | |
257 return TRUE; | |
258 } | |
259 | |
260 FX_BOOL Append(const CFX_ArrayTemplate& src) { | |
261 return CFX_BasicArray::Append(src); | |
262 } | |
263 | |
264 FX_BOOL Copy(const CFX_ArrayTemplate& src) { | |
265 return CFX_BasicArray::Copy(src); | |
266 } | |
267 | |
268 TYPE* GetDataPtr(int index) { | |
269 return (TYPE*)CFX_BasicArray::GetDataPtr(index); | |
270 } | |
271 | |
272 TYPE* AddSpace() { return (TYPE*)CFX_BasicArray::InsertSpaceAt(m_nSize, 1); } | |
273 | |
274 TYPE* InsertSpaceAt(int nIndex, int nCount) { | |
275 return (TYPE*)CFX_BasicArray::InsertSpaceAt(nIndex, nCount); | |
276 } | |
277 | |
278 const TYPE operator[](int nIndex) const { | |
279 if (nIndex < 0 || nIndex >= m_nSize) { | |
280 *(volatile char*)0 = '\0'; | |
281 } | |
282 return ((const TYPE*)m_pData)[nIndex]; | |
283 } | |
284 | |
285 TYPE& operator[](int nIndex) { | |
286 if (nIndex < 0 || nIndex >= m_nSize) { | |
287 *(volatile char*)0 = '\0'; | |
288 } | |
289 return ((TYPE*)m_pData)[nIndex]; | |
290 } | |
291 | |
292 FX_BOOL InsertAt(int nIndex, TYPE newElement, int nCount = 1) { | |
293 if (!InsertSpaceAt(nIndex, nCount)) { | |
294 return FALSE; | |
295 } | |
296 while (nCount--) { | |
297 ((TYPE*)m_pData)[nIndex++] = newElement; | |
298 } | |
299 return TRUE; | |
300 } | |
301 | |
302 FX_BOOL RemoveAt(int nIndex, int nCount = 1) { | |
303 return CFX_BasicArray::RemoveAt(nIndex, nCount); | |
304 } | |
305 | |
306 FX_BOOL InsertAt(int nStartIndex, const CFX_BasicArray* pNewArray) { | |
307 return CFX_BasicArray::InsertAt(nStartIndex, pNewArray); | |
308 } | |
309 | |
310 int Find(TYPE data, int iStart = 0) const { | |
311 if (iStart < 0) { | |
312 return -1; | |
313 } | |
314 for (; iStart < (int)m_nSize; iStart++) | |
315 if (((TYPE*)m_pData)[iStart] == data) { | |
316 return iStart; | |
317 } | |
318 return -1; | |
319 } | |
320 }; | |
321 | |
322 #ifdef PDF_ENABLE_XFA | |
323 typedef CFX_ArrayTemplate<CFX_WideStringC> CFX_WideStringCArray; | |
324 typedef CFX_ArrayTemplate<FX_FLOAT> CFX_FloatArray; | |
325 typedef CFX_ArrayTemplate<uint8_t> CFX_ByteArray; | |
326 typedef CFX_ArrayTemplate<int32_t> CFX_Int32Array; | |
327 | |
328 template <class ObjectClass> | |
329 class CFX_ObjectArray : public CFX_BasicArray { | |
330 public: | |
331 CFX_ObjectArray() : CFX_BasicArray(sizeof(ObjectClass)) {} | |
332 | |
333 ~CFX_ObjectArray() { RemoveAll(); } | |
334 | |
335 void Add(const ObjectClass& data) { | |
336 new ((void*)InsertSpaceAt(m_nSize, 1)) ObjectClass(data); | |
337 } | |
338 | |
339 ObjectClass& Add() { | |
340 return *(ObjectClass*)new ((void*)InsertSpaceAt(m_nSize, 1)) ObjectClass(); | |
341 } | |
342 | |
343 void* AddSpace() { return InsertSpaceAt(m_nSize, 1); } | |
344 | |
345 int32_t Append(const CFX_ObjectArray& src, | |
346 int32_t nStart = 0, | |
347 int32_t nCount = -1) { | |
348 if (nCount == 0) { | |
349 return 0; | |
350 } | |
351 int32_t nSize = src.GetSize(); | |
352 if (!nSize) { | |
353 return 0; | |
354 } | |
355 ASSERT(nStart > -1 && nStart < nSize); | |
356 if (nCount < 0) { | |
357 nCount = nSize; | |
358 } | |
359 if (nStart + nCount > nSize) { | |
360 nCount = nSize - nStart; | |
361 } | |
362 if (nCount < 1) { | |
363 return 0; | |
364 } | |
365 nSize = m_nSize; | |
366 InsertSpaceAt(m_nSize, nCount); | |
367 ObjectClass* pStartObj = (ObjectClass*)GetDataPtr(nSize); | |
368 nSize = nStart + nCount; | |
369 for (int32_t i = nStart; i < nSize; i++, pStartObj++) { | |
370 new ((void*)pStartObj) ObjectClass(src[i]); | |
371 } | |
372 return nCount; | |
373 } | |
374 | |
375 int32_t Copy(const CFX_ObjectArray& src, | |
376 int32_t nStart = 0, | |
377 int32_t nCount = -1) { | |
378 if (nCount == 0) { | |
379 return 0; | |
380 } | |
381 int32_t nSize = src.GetSize(); | |
382 if (!nSize) { | |
383 return 0; | |
384 } | |
385 ASSERT(nStart > -1 && nStart < nSize); | |
386 if (nCount < 0) { | |
387 nCount = nSize; | |
388 } | |
389 if (nStart + nCount > nSize) { | |
390 nCount = nSize - nStart; | |
391 } | |
392 if (nCount < 1) { | |
393 return 0; | |
394 } | |
395 RemoveAll(); | |
396 SetSize(nCount); | |
397 ObjectClass* pStartObj = (ObjectClass*)m_pData; | |
398 nSize = nStart + nCount; | |
399 for (int32_t i = nStart; i < nSize; i++, pStartObj++) { | |
400 new ((void*)pStartObj) ObjectClass(src[i]); | |
401 } | |
402 return nCount; | |
403 } | |
404 | |
405 int GetSize() const { return m_nSize; } | |
406 | |
407 ObjectClass& operator[](int index) const { | |
408 ASSERT(index < m_nSize); | |
409 return *(ObjectClass*)CFX_BasicArray::GetDataPtr(index); | |
410 } | |
411 | |
412 ObjectClass* GetDataPtr(int index) { | |
413 return (ObjectClass*)CFX_BasicArray::GetDataPtr(index); | |
414 } | |
415 | |
416 void RemoveAt(int index) { | |
417 ASSERT(index < m_nSize); | |
418 ((ObjectClass*)GetDataPtr(index))->~ObjectClass(); | |
419 CFX_BasicArray::RemoveAt(index, 1); | |
420 } | |
421 | |
422 void RemoveAll() { | |
423 for (int i = 0; i < m_nSize; i++) { | |
424 ((ObjectClass*)GetDataPtr(i))->~ObjectClass(); | |
425 } | |
426 CFX_BasicArray::SetSize(0); | |
427 } | |
428 }; | |
429 typedef CFX_ObjectArray<CFX_ByteString> CFX_ByteStringArray; | |
430 typedef CFX_ObjectArray<CFX_WideString> CFX_WideStringArray; | |
431 #endif // PDF_ENABLE_XFA | |
432 | |
433 template <class DataType, int FixedSize> | |
434 class CFX_FixedBufGrow { | |
435 public: | |
436 explicit CFX_FixedBufGrow(int data_size) { | |
437 if (data_size > FixedSize) { | |
438 m_pGrowData.reset(FX_Alloc(DataType, data_size)); | |
439 return; | |
440 } | |
441 FXSYS_memset(m_FixedData, 0, sizeof(DataType) * FixedSize); | |
442 } | |
443 operator DataType*() { return m_pGrowData ? m_pGrowData.get() : m_FixedData; } | |
444 | |
445 private: | |
446 DataType m_FixedData[FixedSize]; | |
447 std::unique_ptr<DataType, FxFreeDeleter> m_pGrowData; | |
448 }; | |
449 | |
450 #ifdef PDF_ENABLE_XFA | |
451 class CFX_MapPtrToPtr { | |
452 protected: | |
453 struct CAssoc { | |
454 CAssoc* pNext; | |
455 void* key; | |
456 void* value; | |
457 }; | |
458 | |
459 public: | |
460 CFX_MapPtrToPtr(int nBlockSize = 10); | |
461 ~CFX_MapPtrToPtr(); | |
462 | |
463 int GetCount() const { return m_nCount; } | |
464 bool IsEmpty() const { return m_nCount == 0; } | |
465 | |
466 FX_BOOL Lookup(void* key, void*& rValue) const; | |
467 | |
468 void* GetValueAt(void* key) const; | |
469 | |
470 void*& operator[](void* key); | |
471 | |
472 void SetAt(void* key, void* newValue) { (*this)[key] = newValue; } | |
473 | |
474 FX_BOOL RemoveKey(void* key); | |
475 | |
476 void RemoveAll(); | |
477 | |
478 FX_POSITION GetStartPosition() const { | |
479 return m_nCount == 0 ? nullptr : (FX_POSITION)-1; | |
480 } | |
481 | |
482 void GetNextAssoc(FX_POSITION& rNextPosition, | |
483 void*& rKey, | |
484 void*& rValue) const; | |
485 | |
486 uint32_t GetHashTableSize() const { return m_nHashTableSize; } | |
487 | |
488 void InitHashTable(uint32_t hashSize, FX_BOOL bAllocNow = TRUE); | |
489 | |
490 protected: | |
491 CAssoc** m_pHashTable; | |
492 | |
493 uint32_t m_nHashTableSize; | |
494 | |
495 int m_nCount; | |
496 | |
497 CAssoc* m_pFreeList; | |
498 | |
499 struct CFX_Plex* m_pBlocks; | |
500 | |
501 int m_nBlockSize; | |
502 | |
503 uint32_t HashKey(void* key) const; | |
504 | |
505 CAssoc* NewAssoc(); | |
506 | |
507 void FreeAssoc(CAssoc* pAssoc); | |
508 | |
509 CAssoc* GetAssocAt(void* key, uint32_t& hash) const; | |
510 }; | |
511 | |
512 template <class KeyType, class ValueType> | |
513 class CFX_MapPtrTemplate : public CFX_MapPtrToPtr { | |
514 public: | |
515 CFX_MapPtrTemplate() : CFX_MapPtrToPtr(10) {} | |
516 | |
517 FX_BOOL Lookup(KeyType key, ValueType& rValue) const { | |
518 void* pValue = nullptr; | |
519 if (!CFX_MapPtrToPtr::Lookup((void*)(uintptr_t)key, pValue)) { | |
520 return FALSE; | |
521 } | |
522 rValue = (ValueType)(uintptr_t)pValue; | |
523 return TRUE; | |
524 } | |
525 | |
526 ValueType& operator[](KeyType key) { | |
527 return (ValueType&)CFX_MapPtrToPtr::operator[]((void*)(uintptr_t)key); | |
528 } | |
529 | |
530 void SetAt(KeyType key, ValueType newValue) { | |
531 CFX_MapPtrToPtr::SetAt((void*)(uintptr_t)key, (void*)(uintptr_t)newValue); | |
532 } | |
533 | |
534 FX_BOOL RemoveKey(KeyType key) { | |
535 return CFX_MapPtrToPtr::RemoveKey((void*)(uintptr_t)key); | |
536 } | |
537 | |
538 void GetNextAssoc(FX_POSITION& rNextPosition, | |
539 KeyType& rKey, | |
540 ValueType& rValue) const { | |
541 void* pKey = nullptr; | |
542 void* pValue = nullptr; | |
543 CFX_MapPtrToPtr::GetNextAssoc(rNextPosition, pKey, pValue); | |
544 rKey = (KeyType)(uintptr_t)pKey; | |
545 rValue = (ValueType)(uintptr_t)pValue; | |
546 } | |
547 }; | |
548 #endif // PDF_ENABLE_XFA | |
549 | |
550 class CFX_PtrList { | |
551 protected: | |
552 struct CNode { | |
553 CNode* pNext; | |
554 CNode* pPrev; | |
555 void* data; | |
556 }; | |
557 | |
558 public: | |
559 CFX_PtrList(int nBlockSize = 10); | |
560 | |
561 FX_POSITION GetHeadPosition() const { return (FX_POSITION)m_pNodeHead; } | |
562 FX_POSITION GetTailPosition() const { return (FX_POSITION)m_pNodeTail; } | |
563 | |
564 void* GetNext(FX_POSITION& rPosition) const { | |
565 CNode* pNode = (CNode*)rPosition; | |
566 rPosition = (FX_POSITION)pNode->pNext; | |
567 return pNode->data; | |
568 } | |
569 | |
570 void* GetPrev(FX_POSITION& rPosition) const { | |
571 CNode* pNode = (CNode*)rPosition; | |
572 rPosition = (FX_POSITION)pNode->pPrev; | |
573 return pNode->data; | |
574 } | |
575 | |
576 FX_POSITION GetNextPosition(FX_POSITION pos) const { | |
577 return ((CNode*)pos)->pNext; | |
578 } | |
579 | |
580 FX_POSITION GetPrevPosition(FX_POSITION pos) const { | |
581 return ((CNode*)pos)->pPrev; | |
582 } | |
583 | |
584 void* GetAt(FX_POSITION rPosition) const { | |
585 CNode* pNode = (CNode*)rPosition; | |
586 return pNode->data; | |
587 } | |
588 | |
589 int GetCount() const { return m_nCount; } | |
590 FX_POSITION AddTail(void* newElement); | |
591 FX_POSITION AddHead(void* newElement); | |
592 | |
593 void SetAt(FX_POSITION pos, void* newElement) { | |
594 CNode* pNode = (CNode*)pos; | |
595 pNode->data = newElement; | |
596 } | |
597 FX_POSITION InsertAfter(FX_POSITION pos, void* newElement); | |
598 | |
599 FX_POSITION Find(void* searchValue, FX_POSITION startAfter = nullptr) const; | |
600 FX_POSITION FindIndex(int index) const; | |
601 | |
602 void RemoveAt(FX_POSITION pos); | |
603 void RemoveAll(); | |
604 | |
605 protected: | |
606 CNode* m_pNodeHead; | |
607 CNode* m_pNodeTail; | |
608 int m_nCount; | |
609 CNode* m_pNodeFree; | |
610 struct CFX_Plex* m_pBlocks; | |
611 int m_nBlockSize; | |
612 | |
613 CNode* NewNode(CNode* pPrev, CNode* pNext); | |
614 void FreeNode(CNode* pNode); | |
615 | |
616 public: | |
617 ~CFX_PtrList(); | |
618 }; | |
619 | |
620 #ifdef PDF_ENABLE_XFA | |
621 typedef void (*PD_CALLBACK_FREEDATA)(void* pData); | |
622 #endif // PDF_ENABLE_XFA | |
623 | |
624 class CFX_BitStream { | |
625 public: | |
626 void Init(const uint8_t* pData, uint32_t dwSize); | |
627 | |
628 uint32_t GetBits(uint32_t nBits); | |
629 | |
630 void ByteAlign(); | |
631 | |
632 FX_BOOL IsEOF() { return m_BitPos >= m_BitSize; } | |
633 | |
634 void SkipBits(uint32_t nBits) { m_BitPos += nBits; } | |
635 | |
636 void Rewind() { m_BitPos = 0; } | |
637 | |
638 uint32_t GetPos() const { return m_BitPos; } | |
639 | |
640 uint32_t BitsRemaining() const { | |
641 return m_BitSize >= m_BitPos ? m_BitSize - m_BitPos : 0; | |
642 } | |
643 | |
644 protected: | |
645 uint32_t m_BitPos; | |
646 | |
647 uint32_t m_BitSize; | |
648 | |
649 const uint8_t* m_pData; | |
650 }; | |
651 | |
652 class IFX_Pause { | |
653 public: | |
654 virtual ~IFX_Pause() {} | |
655 virtual FX_BOOL NeedToPauseNow() = 0; | |
656 }; | |
657 | |
658 template <typename T> | |
659 class CFX_AutoRestorer { | |
660 public: | |
661 explicit CFX_AutoRestorer(T* location) | |
662 : m_Location(location), m_OldValue(*location) {} | |
663 ~CFX_AutoRestorer() { *m_Location = m_OldValue; } | |
664 | |
665 private: | |
666 T* const m_Location; | |
667 const T m_OldValue; | |
668 }; | |
669 | |
670 #define FX_DATALIST_LENGTH 1024 | |
671 template <size_t unit> | |
672 class CFX_SortListArray { | |
673 protected: | |
674 struct DataList { | |
675 int32_t start; | |
676 | |
677 int32_t count; | |
678 uint8_t* data; | |
679 }; | |
680 | |
681 public: | |
682 CFX_SortListArray() : m_CurList(0) {} | |
683 | |
684 ~CFX_SortListArray() { Clear(); } | |
685 | |
686 void Clear() { | |
687 for (int32_t i = m_DataLists.GetUpperBound(); i >= 0; i--) { | |
688 DataList list = m_DataLists.ElementAt(i); | |
689 FX_Free(list.data); | |
690 } | |
691 m_DataLists.RemoveAll(); | |
692 m_CurList = 0; | |
693 } | |
694 | |
695 void Append(int32_t nStart, int32_t nCount) { | |
696 if (nStart < 0) { | |
697 return; | |
698 } | |
699 while (nCount > 0) { | |
700 int32_t temp_count = std::min(nCount, FX_DATALIST_LENGTH); | |
701 DataList list; | |
702 list.data = FX_Alloc2D(uint8_t, temp_count, unit); | |
703 list.start = nStart; | |
704 list.count = temp_count; | |
705 Append(list); | |
706 nCount -= temp_count; | |
707 nStart += temp_count; | |
708 } | |
709 } | |
710 | |
711 uint8_t* GetAt(int32_t nIndex) { | |
712 if (nIndex < 0) { | |
713 return nullptr; | |
714 } | |
715 if (m_CurList < 0 || m_CurList >= m_DataLists.GetSize()) { | |
716 return nullptr; | |
717 } | |
718 DataList* pCurList = m_DataLists.GetDataPtr(m_CurList); | |
719 if (!pCurList || nIndex < pCurList->start || | |
720 nIndex >= pCurList->start + pCurList->count) { | |
721 pCurList = nullptr; | |
722 int32_t iStart = 0; | |
723 int32_t iEnd = m_DataLists.GetUpperBound(); | |
724 int32_t iMid = 0; | |
725 while (iStart <= iEnd) { | |
726 iMid = (iStart + iEnd) / 2; | |
727 DataList* list = m_DataLists.GetDataPtr(iMid); | |
728 if (nIndex < list->start) { | |
729 iEnd = iMid - 1; | |
730 } else if (nIndex >= list->start + list->count) { | |
731 iStart = iMid + 1; | |
732 } else { | |
733 pCurList = list; | |
734 m_CurList = iMid; | |
735 break; | |
736 } | |
737 } | |
738 } | |
739 return pCurList ? pCurList->data + (nIndex - pCurList->start) * unit | |
740 : nullptr; | |
741 } | |
742 | |
743 protected: | |
744 void Append(const DataList& list) { | |
745 int32_t iStart = 0; | |
746 int32_t iEnd = m_DataLists.GetUpperBound(); | |
747 int32_t iFind = 0; | |
748 while (iStart <= iEnd) { | |
749 int32_t iMid = (iStart + iEnd) / 2; | |
750 DataList* cur_list = m_DataLists.GetDataPtr(iMid); | |
751 if (list.start < cur_list->start + cur_list->count) { | |
752 iEnd = iMid - 1; | |
753 } else { | |
754 if (iMid == iEnd) { | |
755 iFind = iMid + 1; | |
756 break; | |
757 } | |
758 DataList* next_list = m_DataLists.GetDataPtr(iMid + 1); | |
759 if (list.start < next_list->start) { | |
760 iFind = iMid + 1; | |
761 break; | |
762 } else { | |
763 iStart = iMid + 1; | |
764 } | |
765 } | |
766 } | |
767 m_DataLists.InsertAt(iFind, list); | |
768 } | |
769 int32_t m_CurList; | |
770 CFX_ArrayTemplate<DataList> m_DataLists; | |
771 }; | |
772 template <typename T1, typename T2> | |
773 class CFX_ListArrayTemplate { | |
774 public: | |
775 void Clear() { m_Data.Clear(); } | |
776 | |
777 void Add(int32_t nStart, int32_t nCount) { m_Data.Append(nStart, nCount); } | |
778 | |
779 T2& operator[](int32_t nIndex) { | |
780 uint8_t* data = m_Data.GetAt(nIndex); | |
781 ASSERT(data); | |
782 return (T2&)(*(volatile T2*)data); | |
783 } | |
784 | |
785 T2* GetPtrAt(int32_t nIndex) { return (T2*)m_Data.GetAt(nIndex); } | |
786 | |
787 protected: | |
788 T1 m_Data; | |
789 }; | |
790 typedef CFX_ListArrayTemplate<CFX_SortListArray<sizeof(FX_FILESIZE)>, | |
791 FX_FILESIZE> CFX_FileSizeListArray; | |
792 | |
793 #ifdef PDF_ENABLE_XFA | |
794 class IFX_Retainable { | |
795 public: | |
796 virtual uint32_t Retain() = 0; | |
797 virtual uint32_t Release() = 0; | |
798 | |
799 protected: | |
800 virtual ~IFX_Retainable() {} | |
801 }; | |
802 #define FX_IsOdd(a) ((a)&1) | |
803 #endif // PDF_ENABLE_XFA | |
804 | |
805 class CFX_Vector_3by1 { | |
806 public: | |
807 CFX_Vector_3by1() : a(0.0f), b(0.0f), c(0.0f) {} | |
808 | |
809 CFX_Vector_3by1(FX_FLOAT a1, FX_FLOAT b1, FX_FLOAT c1) | |
810 : a(a1), b(b1), c(c1) {} | |
811 | |
812 FX_FLOAT a; | |
813 FX_FLOAT b; | |
814 FX_FLOAT c; | |
815 }; | |
816 class CFX_Matrix_3by3 { | |
817 public: | |
818 CFX_Matrix_3by3() | |
819 : a(0.0f), | |
820 b(0.0f), | |
821 c(0.0f), | |
822 d(0.0f), | |
823 e(0.0f), | |
824 f(0.0f), | |
825 g(0.0f), | |
826 h(0.0f), | |
827 i(0.0f) {} | |
828 | |
829 CFX_Matrix_3by3(FX_FLOAT a1, | |
830 FX_FLOAT b1, | |
831 FX_FLOAT c1, | |
832 FX_FLOAT d1, | |
833 FX_FLOAT e1, | |
834 FX_FLOAT f1, | |
835 FX_FLOAT g1, | |
836 FX_FLOAT h1, | |
837 FX_FLOAT i1) | |
838 : a(a1), b(b1), c(c1), d(d1), e(e1), f(f1), g(g1), h(h1), i(i1) {} | |
839 | |
840 CFX_Matrix_3by3 Inverse(); | |
841 | |
842 CFX_Matrix_3by3 Multiply(const CFX_Matrix_3by3& m); | |
843 | |
844 CFX_Vector_3by1 TransformVector(const CFX_Vector_3by1& v); | |
845 | |
846 FX_FLOAT a; | |
847 FX_FLOAT b; | |
848 FX_FLOAT c; | |
849 FX_FLOAT d; | |
850 FX_FLOAT e; | |
851 FX_FLOAT f; | |
852 FX_FLOAT g; | |
853 FX_FLOAT h; | |
854 FX_FLOAT i; | |
855 }; | |
856 | |
857 uint32_t GetBits32(const uint8_t* pData, int bitpos, int nbits); | |
858 | |
859 #endif // CORE_FXCRT_INCLUDE_FX_BASIC_H_ | |
OLD | NEW |