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_INCLUDE_FXCRT_FX_BASIC_H_ | |
8 #define CORE_INCLUDE_FXCRT_FX_BASIC_H_ | |
9 | |
10 #include <algorithm> | |
11 #include <memory> | |
12 | |
13 #include "core/include/fxcrt/fx_memory.h" | |
14 #include "core/include/fxcrt/fx_stream.h" | |
15 #include "core/include/fxcrt/fx_string.h" | |
16 #include "core/include/fxcrt/fx_system.h" | |
17 | |
18 // The FX_ArraySize(arr) macro returns the # of elements in an array arr. | |
19 // The expression is a compile-time constant, and therefore can be | |
20 // used in defining new arrays, for example. If you use FX_ArraySize on | |
21 // a pointer by mistake, you will get a compile-time error. | |
22 // | |
23 // One caveat is that FX_ArraySize() doesn't accept any array of an | |
24 // anonymous type or a type defined inside a function. | |
25 #define FX_ArraySize(array) (sizeof(ArraySizeHelper(array))) | |
26 | |
27 // This template function declaration is used in defining FX_ArraySize. | |
28 // Note that the function doesn't need an implementation, as we only | |
29 // use its type. | |
30 template <typename T, size_t N> | |
31 char(&ArraySizeHelper(T(&array)[N]))[N]; | |
32 | |
33 // Used with std::unique_ptr to FX_Free raw memory. | |
34 struct FxFreeDeleter { | |
35 inline void operator()(void* ptr) const { FX_Free(ptr); } | |
36 }; | |
37 | |
38 // Used with std::unique_ptr to Release() objects that can't be deleted. | |
39 template <class T> | |
40 struct ReleaseDeleter { | |
41 inline void operator()(T* ptr) const { ptr->Release(); } | |
42 }; | |
43 | |
44 class CFX_BinaryBuf { | |
45 public: | |
46 CFX_BinaryBuf(); | |
47 explicit CFX_BinaryBuf(FX_STRSIZE size); | |
48 ~CFX_BinaryBuf(); | |
49 | |
50 uint8_t* GetBuffer() const { return m_pBuffer.get(); } | |
51 FX_STRSIZE GetSize() const { return m_DataSize; } | |
52 | |
53 void Clear(); | |
54 void EstimateSize(FX_STRSIZE size, FX_STRSIZE alloc_step = 0); | |
55 void AppendBlock(const void* pBuf, FX_STRSIZE size); | |
56 void AppendString(const CFX_ByteStringC& str) { | |
57 AppendBlock(str.GetPtr(), str.GetLength()); | |
58 } | |
59 | |
60 void AppendByte(uint8_t byte) { | |
61 ExpandBuf(1); | |
62 m_pBuffer.get()[m_DataSize++] = byte; | |
63 } | |
64 | |
65 void InsertBlock(FX_STRSIZE pos, const void* pBuf, FX_STRSIZE size); | |
66 void Delete(int start_index, int count); | |
67 | |
68 // Takes ownership of |pBuf|. | |
69 void AttachData(uint8_t* pBuf, FX_STRSIZE size); | |
70 | |
71 // Releases ownership of |m_pBuffer| and returns it. | |
72 uint8_t* DetachBuffer(); | |
73 | |
74 protected: | |
75 void ExpandBuf(FX_STRSIZE size); | |
76 | |
77 FX_STRSIZE m_AllocStep; | |
78 FX_STRSIZE m_AllocSize; | |
79 FX_STRSIZE m_DataSize; | |
80 std::unique_ptr<uint8_t, FxFreeDeleter> m_pBuffer; | |
81 }; | |
82 | |
83 class CFX_ByteTextBuf : public CFX_BinaryBuf { | |
84 public: | |
85 void AppendChar(int ch) { AppendByte((uint8_t)ch); } | |
86 FX_STRSIZE GetLength() const { return m_DataSize; } | |
87 CFX_ByteStringC GetByteString() const; | |
88 | |
89 CFX_ByteTextBuf& operator<<(int i); | |
90 CFX_ByteTextBuf& operator<<(FX_DWORD i); | |
91 CFX_ByteTextBuf& operator<<(double f); | |
92 CFX_ByteTextBuf& operator<<(const CFX_ByteStringC& lpsz); | |
93 CFX_ByteTextBuf& operator<<(const CFX_ByteTextBuf& buf); | |
94 }; | |
95 | |
96 class CFX_WideTextBuf : public CFX_BinaryBuf { | |
97 public: | |
98 void AppendChar(FX_WCHAR wch); | |
99 FX_STRSIZE GetLength() const { return m_DataSize / sizeof(FX_WCHAR); } | |
100 FX_WCHAR* GetBuffer() const { | |
101 return reinterpret_cast<FX_WCHAR*>(m_pBuffer.get()); | |
102 } | |
103 CFX_WideStringC GetWideString() const; | |
104 | |
105 void Delete(int start_index, int count) { | |
106 CFX_BinaryBuf::Delete(start_index * sizeof(FX_WCHAR), | |
107 count * sizeof(FX_WCHAR)); | |
108 } | |
109 | |
110 CFX_WideTextBuf& operator<<(int i); | |
111 CFX_WideTextBuf& operator<<(double f); | |
112 CFX_WideTextBuf& operator<<(const FX_WCHAR* lpsz); | |
113 CFX_WideTextBuf& operator<<(const CFX_WideStringC& str); | |
114 CFX_WideTextBuf& operator<<(const CFX_WideString& str); | |
115 CFX_WideTextBuf& operator<<(const CFX_WideTextBuf& buf); | |
116 }; | |
117 | |
118 #ifdef PDF_ENABLE_XFA | |
119 class CFX_ArchiveSaver { | |
120 public: | |
121 CFX_ArchiveSaver() : m_pStream(NULL) {} | |
122 | |
123 CFX_ArchiveSaver& operator<<(uint8_t i); | |
124 | |
125 CFX_ArchiveSaver& operator<<(int i); | |
126 | |
127 CFX_ArchiveSaver& operator<<(FX_DWORD i); | |
128 | |
129 CFX_ArchiveSaver& operator<<(FX_FLOAT i); | |
130 | |
131 CFX_ArchiveSaver& operator<<(double i); | |
132 | |
133 CFX_ArchiveSaver& operator<<(const CFX_ByteStringC& bstr); | |
134 | |
135 CFX_ArchiveSaver& operator<<(const FX_WCHAR* bstr); | |
136 | |
137 CFX_ArchiveSaver& operator<<(const CFX_WideString& wstr); | |
138 | |
139 void Write(const void* pData, FX_STRSIZE dwSize); | |
140 | |
141 intptr_t GetLength() { return m_SavingBuf.GetSize(); } | |
142 | |
143 const uint8_t* GetBuffer() { return m_SavingBuf.GetBuffer(); } | |
144 | |
145 void SetStream(IFX_FileStream* pStream) { m_pStream = pStream; } | |
146 | |
147 protected: | |
148 CFX_BinaryBuf m_SavingBuf; | |
149 | |
150 IFX_FileStream* m_pStream; | |
151 }; | |
152 class CFX_ArchiveLoader { | |
153 public: | |
154 CFX_ArchiveLoader(const uint8_t* pData, FX_DWORD dwSize); | |
155 | |
156 CFX_ArchiveLoader& operator>>(uint8_t& i); | |
157 | |
158 CFX_ArchiveLoader& operator>>(int& i); | |
159 | |
160 CFX_ArchiveLoader& operator>>(FX_DWORD& i); | |
161 | |
162 CFX_ArchiveLoader& operator>>(FX_FLOAT& i); | |
163 | |
164 CFX_ArchiveLoader& operator>>(double& i); | |
165 | |
166 CFX_ArchiveLoader& operator>>(CFX_ByteString& bstr); | |
167 | |
168 CFX_ArchiveLoader& operator>>(CFX_WideString& wstr); | |
169 | |
170 FX_BOOL IsEOF(); | |
171 | |
172 FX_BOOL Read(void* pBuf, FX_DWORD dwSize); | |
173 | |
174 protected: | |
175 FX_DWORD m_LoadingPos; | |
176 | |
177 const uint8_t* m_pLoadingBuf; | |
178 | |
179 FX_DWORD m_LoadingSize; | |
180 }; | |
181 #endif // PDF_ENABLE_XFA | |
182 | |
183 class CFX_FileBufferArchive { | |
184 public: | |
185 CFX_FileBufferArchive(); | |
186 ~CFX_FileBufferArchive(); | |
187 | |
188 void Clear(); | |
189 bool Flush(); | |
190 int32_t AppendBlock(const void* pBuf, size_t size); | |
191 int32_t AppendByte(uint8_t byte); | |
192 int32_t AppendDWord(FX_DWORD i); | |
193 int32_t AppendString(const CFX_ByteStringC& lpsz); | |
194 | |
195 // |pFile| must outlive the CFX_FileBufferArchive. | |
196 void AttachFile(IFX_StreamWrite* pFile); | |
197 | |
198 private: | |
199 static const size_t kBufSize = 32768; | |
200 | |
201 size_t m_Length; | |
202 std::unique_ptr<uint8_t, FxFreeDeleter> m_pBuffer; | |
203 IFX_StreamWrite* m_pFile; | |
204 }; | |
205 | |
206 class CFX_CharMap { | |
207 public: | |
208 static CFX_ByteString GetByteString(uint16_t codepage, | |
209 const CFX_WideString& wstr); | |
210 | |
211 static CFX_WideString GetWideString(uint16_t codepage, | |
212 const CFX_ByteString& bstr); | |
213 | |
214 CFX_CharMap() = delete; | |
215 }; | |
216 | |
217 class CFX_UTF8Decoder { | |
218 public: | |
219 CFX_UTF8Decoder() { m_PendingBytes = 0; } | |
220 | |
221 void Clear(); | |
222 | |
223 void Input(uint8_t byte); | |
224 | |
225 void AppendChar(FX_DWORD ch); | |
226 | |
227 void ClearStatus() { m_PendingBytes = 0; } | |
228 | |
229 CFX_WideStringC GetResult() const { return m_Buffer.GetWideString(); } | |
230 | |
231 protected: | |
232 int m_PendingBytes; | |
233 | |
234 FX_DWORD m_PendingChar; | |
235 | |
236 CFX_WideTextBuf m_Buffer; | |
237 }; | |
238 | |
239 class CFX_UTF8Encoder { | |
240 public: | |
241 CFX_UTF8Encoder() {} | |
242 | |
243 void Input(FX_WCHAR unicode); | |
244 void AppendStr(const CFX_ByteStringC& str) { m_Buffer << str; } | |
245 CFX_ByteStringC GetResult() const { return m_Buffer.GetByteString(); } | |
246 | |
247 protected: | |
248 CFX_ByteTextBuf m_Buffer; | |
249 }; | |
250 | |
251 class CFX_BasicArray { | |
252 protected: | |
253 CFX_BasicArray(int unit_size); | |
254 | |
255 ~CFX_BasicArray(); | |
256 | |
257 FX_BOOL SetSize(int nNewSize); | |
258 | |
259 FX_BOOL Append(const CFX_BasicArray& src); | |
260 | |
261 FX_BOOL Copy(const CFX_BasicArray& src); | |
262 | |
263 uint8_t* InsertSpaceAt(int nIndex, int nCount); | |
264 | |
265 FX_BOOL RemoveAt(int nIndex, int nCount); | |
266 | |
267 FX_BOOL InsertAt(int nStartIndex, const CFX_BasicArray* pNewArray); | |
268 | |
269 const void* GetDataPtr(int index) const; | |
270 | |
271 protected: | |
272 uint8_t* m_pData; | |
273 | |
274 int m_nSize; | |
275 | |
276 int m_nMaxSize; | |
277 | |
278 int m_nUnitSize; | |
279 }; | |
280 template <class TYPE> | |
281 class CFX_ArrayTemplate : public CFX_BasicArray { | |
282 public: | |
283 CFX_ArrayTemplate() : CFX_BasicArray(sizeof(TYPE)) {} | |
284 | |
285 int GetSize() const { return m_nSize; } | |
286 | |
287 int GetUpperBound() const { return m_nSize - 1; } | |
288 | |
289 FX_BOOL SetSize(int nNewSize) { return CFX_BasicArray::SetSize(nNewSize); } | |
290 | |
291 void RemoveAll() { SetSize(0); } | |
292 | |
293 const TYPE GetAt(int nIndex) const { | |
294 if (nIndex < 0 || nIndex >= m_nSize) { | |
295 return (const TYPE&)(*(volatile const TYPE*)NULL); | |
296 } | |
297 return ((const TYPE*)m_pData)[nIndex]; | |
298 } | |
299 | |
300 FX_BOOL SetAt(int nIndex, TYPE newElement) { | |
301 if (nIndex < 0 || nIndex >= m_nSize) { | |
302 return FALSE; | |
303 } | |
304 ((TYPE*)m_pData)[nIndex] = newElement; | |
305 return TRUE; | |
306 } | |
307 | |
308 TYPE& ElementAt(int nIndex) { | |
309 if (nIndex < 0 || nIndex >= m_nSize) { | |
310 return *(TYPE*)NULL; | |
311 } | |
312 return ((TYPE*)m_pData)[nIndex]; | |
313 } | |
314 | |
315 const TYPE* GetData() const { return (const TYPE*)m_pData; } | |
316 | |
317 TYPE* GetData() { return (TYPE*)m_pData; } | |
318 | |
319 FX_BOOL SetAtGrow(int nIndex, TYPE newElement) { | |
320 if (nIndex < 0) | |
321 return FALSE; | |
322 | |
323 if (nIndex >= m_nSize && !SetSize(nIndex + 1)) | |
324 return FALSE; | |
325 | |
326 ((TYPE*)m_pData)[nIndex] = newElement; | |
327 return TRUE; | |
328 } | |
329 | |
330 FX_BOOL Add(TYPE newElement) { | |
331 if (m_nSize < m_nMaxSize) { | |
332 m_nSize++; | |
333 } else if (!SetSize(m_nSize + 1)) { | |
334 return FALSE; | |
335 } | |
336 ((TYPE*)m_pData)[m_nSize - 1] = newElement; | |
337 return TRUE; | |
338 } | |
339 | |
340 FX_BOOL Append(const CFX_ArrayTemplate& src) { | |
341 return CFX_BasicArray::Append(src); | |
342 } | |
343 | |
344 FX_BOOL Copy(const CFX_ArrayTemplate& src) { | |
345 return CFX_BasicArray::Copy(src); | |
346 } | |
347 | |
348 TYPE* GetDataPtr(int index) { | |
349 return (TYPE*)CFX_BasicArray::GetDataPtr(index); | |
350 } | |
351 | |
352 TYPE* AddSpace() { return (TYPE*)CFX_BasicArray::InsertSpaceAt(m_nSize, 1); } | |
353 | |
354 TYPE* InsertSpaceAt(int nIndex, int nCount) { | |
355 return (TYPE*)CFX_BasicArray::InsertSpaceAt(nIndex, nCount); | |
356 } | |
357 | |
358 const TYPE operator[](int nIndex) const { | |
359 if (nIndex < 0 || nIndex >= m_nSize) { | |
360 *(volatile char*)0 = '\0'; | |
361 } | |
362 return ((const TYPE*)m_pData)[nIndex]; | |
363 } | |
364 | |
365 TYPE& operator[](int nIndex) { | |
366 if (nIndex < 0 || nIndex >= m_nSize) { | |
367 *(volatile char*)0 = '\0'; | |
368 } | |
369 return ((TYPE*)m_pData)[nIndex]; | |
370 } | |
371 | |
372 FX_BOOL InsertAt(int nIndex, TYPE newElement, int nCount = 1) { | |
373 if (!InsertSpaceAt(nIndex, nCount)) { | |
374 return FALSE; | |
375 } | |
376 while (nCount--) { | |
377 ((TYPE*)m_pData)[nIndex++] = newElement; | |
378 } | |
379 return TRUE; | |
380 } | |
381 | |
382 FX_BOOL RemoveAt(int nIndex, int nCount = 1) { | |
383 return CFX_BasicArray::RemoveAt(nIndex, nCount); | |
384 } | |
385 | |
386 FX_BOOL InsertAt(int nStartIndex, const CFX_BasicArray* pNewArray) { | |
387 return CFX_BasicArray::InsertAt(nStartIndex, pNewArray); | |
388 } | |
389 | |
390 int Find(TYPE data, int iStart = 0) const { | |
391 if (iStart < 0) { | |
392 return -1; | |
393 } | |
394 for (; iStart < (int)m_nSize; iStart++) | |
395 if (((TYPE*)m_pData)[iStart] == data) { | |
396 return iStart; | |
397 } | |
398 return -1; | |
399 } | |
400 }; | |
401 typedef CFX_ArrayTemplate<FX_DWORD> CFX_DWordArray; | |
402 | |
403 #ifdef PDF_ENABLE_XFA | |
404 typedef CFX_ArrayTemplate<CFX_WideStringC> CFX_WideStringCArray; | |
405 typedef CFX_ArrayTemplate<FX_FLOAT> CFX_FloatArray; | |
406 typedef CFX_ArrayTemplate<uint8_t> CFX_ByteArray; | |
407 typedef CFX_ArrayTemplate<int32_t> CFX_Int32Array; | |
408 typedef CFX_ArrayTemplate<void*> CFX_PtrArray; | |
409 #endif // PDF_ENABLE_XFA | |
410 | |
411 #ifdef PDF_ENABLE_XFA | |
412 template <class ObjectClass> | |
413 class CFX_ObjectArray : public CFX_BasicArray { | |
414 public: | |
415 CFX_ObjectArray() : CFX_BasicArray(sizeof(ObjectClass)) {} | |
416 | |
417 ~CFX_ObjectArray() { RemoveAll(); } | |
418 | |
419 void Add(const ObjectClass& data) { | |
420 new ((void*)InsertSpaceAt(m_nSize, 1)) ObjectClass(data); | |
421 } | |
422 | |
423 ObjectClass& Add() { | |
424 return *(ObjectClass*)new ((void*)InsertSpaceAt(m_nSize, 1)) ObjectClass(); | |
425 } | |
426 | |
427 void* AddSpace() { return InsertSpaceAt(m_nSize, 1); } | |
428 | |
429 int32_t Append(const CFX_ObjectArray& src, | |
430 int32_t nStart = 0, | |
431 int32_t nCount = -1) { | |
432 if (nCount == 0) { | |
433 return 0; | |
434 } | |
435 int32_t nSize = src.GetSize(); | |
436 if (!nSize) { | |
437 return 0; | |
438 } | |
439 FXSYS_assert(nStart > -1 && nStart < nSize); | |
440 if (nCount < 0) { | |
441 nCount = nSize; | |
442 } | |
443 if (nStart + nCount > nSize) { | |
444 nCount = nSize - nStart; | |
445 } | |
446 if (nCount < 1) { | |
447 return 0; | |
448 } | |
449 nSize = m_nSize; | |
450 InsertSpaceAt(m_nSize, nCount); | |
451 ObjectClass* pStartObj = (ObjectClass*)GetDataPtr(nSize); | |
452 nSize = nStart + nCount; | |
453 for (int32_t i = nStart; i < nSize; i++, pStartObj++) { | |
454 new ((void*)pStartObj) ObjectClass(src[i]); | |
455 } | |
456 return nCount; | |
457 } | |
458 | |
459 int32_t Copy(const CFX_ObjectArray& src, | |
460 int32_t nStart = 0, | |
461 int32_t nCount = -1) { | |
462 if (nCount == 0) { | |
463 return 0; | |
464 } | |
465 int32_t nSize = src.GetSize(); | |
466 if (!nSize) { | |
467 return 0; | |
468 } | |
469 FXSYS_assert(nStart > -1 && nStart < nSize); | |
470 if (nCount < 0) { | |
471 nCount = nSize; | |
472 } | |
473 if (nStart + nCount > nSize) { | |
474 nCount = nSize - nStart; | |
475 } | |
476 if (nCount < 1) { | |
477 return 0; | |
478 } | |
479 RemoveAll(); | |
480 SetSize(nCount); | |
481 ObjectClass* pStartObj = (ObjectClass*)m_pData; | |
482 nSize = nStart + nCount; | |
483 for (int32_t i = nStart; i < nSize; i++, pStartObj++) { | |
484 new ((void*)pStartObj) ObjectClass(src[i]); | |
485 } | |
486 return nCount; | |
487 } | |
488 | |
489 int GetSize() const { return m_nSize; } | |
490 | |
491 ObjectClass& operator[](int index) const { | |
492 FXSYS_assert(index < m_nSize); | |
493 return *(ObjectClass*)CFX_BasicArray::GetDataPtr(index); | |
494 } | |
495 | |
496 ObjectClass* GetDataPtr(int index) { | |
497 return (ObjectClass*)CFX_BasicArray::GetDataPtr(index); | |
498 } | |
499 | |
500 void RemoveAt(int index) { | |
501 FXSYS_assert(index < m_nSize); | |
502 ((ObjectClass*)GetDataPtr(index))->~ObjectClass(); | |
503 CFX_BasicArray::RemoveAt(index, 1); | |
504 } | |
505 | |
506 void RemoveAll() { | |
507 for (int i = 0; i < m_nSize; i++) { | |
508 ((ObjectClass*)GetDataPtr(i))->~ObjectClass(); | |
509 } | |
510 CFX_BasicArray::SetSize(0); | |
511 } | |
512 }; | |
513 typedef CFX_ObjectArray<CFX_ByteString> CFX_ByteStringArray; | |
514 typedef CFX_ObjectArray<CFX_WideString> CFX_WideStringArray; | |
515 class CFX_BaseSegmentedArray { | |
516 public: | |
517 CFX_BaseSegmentedArray(int unit_size = 1, | |
518 int segment_units = 512, | |
519 int index_size = 8); | |
520 | |
521 ~CFX_BaseSegmentedArray(); | |
522 | |
523 void SetUnitSize(int unit_size, int segment_units, int index_size = 8); | |
524 | |
525 void* Add(); | |
526 | |
527 void* GetAt(int index) const; | |
528 | |
529 void RemoveAll(); | |
530 | |
531 void Delete(int index, int count = 1); | |
532 | |
533 int GetSize() const { return m_DataSize; } | |
534 | |
535 int GetSegmentSize() const { return m_SegmentSize; } | |
536 | |
537 int GetUnitSize() const { return m_UnitSize; } | |
538 | |
539 void* Iterate(FX_BOOL (*callback)(void* param, void* pData), | |
540 void* param) const; | |
541 | |
542 private: | |
543 int m_UnitSize; | |
544 | |
545 short m_SegmentSize; | |
546 | |
547 uint8_t m_IndexSize; | |
548 | |
549 uint8_t m_IndexDepth; | |
550 | |
551 int m_DataSize; | |
552 | |
553 void* m_pIndex; | |
554 void** GetIndex(int seg_index) const; | |
555 void* IterateIndex(int level, | |
556 int& start, | |
557 void** pIndex, | |
558 FX_BOOL (*callback)(void* param, void* pData), | |
559 void* param) const; | |
560 void* IterateSegment(const uint8_t* pSegment, | |
561 int count, | |
562 FX_BOOL (*callback)(void* param, void* pData), | |
563 void* param) const; | |
564 }; | |
565 template <class ElementType> | |
566 class CFX_SegmentedArray : public CFX_BaseSegmentedArray { | |
567 public: | |
568 CFX_SegmentedArray(int segment_units, int index_size = 8) | |
569 : CFX_BaseSegmentedArray(sizeof(ElementType), segment_units, index_size) { | |
570 } | |
571 | |
572 void Add(ElementType data) { | |
573 *(ElementType*)CFX_BaseSegmentedArray::Add() = data; | |
574 } | |
575 | |
576 ElementType& operator[](int index) { | |
577 return *(ElementType*)CFX_BaseSegmentedArray::GetAt(index); | |
578 } | |
579 }; | |
580 #endif // PDF_ENABLE_XFA | |
581 | |
582 template <class DataType, int FixedSize> | |
583 class CFX_FixedBufGrow { | |
584 public: | |
585 explicit CFX_FixedBufGrow(int data_size) { | |
586 if (data_size > FixedSize) { | |
587 m_pGrowData.reset(FX_Alloc(DataType, data_size)); | |
588 return; | |
589 } | |
590 FXSYS_memset(m_FixedData, 0, sizeof(DataType) * FixedSize); | |
591 } | |
592 operator DataType*() { return m_pGrowData ? m_pGrowData.get() : m_FixedData; } | |
593 | |
594 private: | |
595 DataType m_FixedData[FixedSize]; | |
596 std::unique_ptr<DataType, FxFreeDeleter> m_pGrowData; | |
597 }; | |
598 | |
599 #ifdef PDF_ENABLE_XFA | |
600 class CFX_MapPtrToPtr { | |
601 protected: | |
602 struct CAssoc { | |
603 CAssoc* pNext; | |
604 void* key; | |
605 void* value; | |
606 }; | |
607 | |
608 public: | |
609 CFX_MapPtrToPtr(int nBlockSize = 10); | |
610 ~CFX_MapPtrToPtr(); | |
611 | |
612 int GetCount() const { return m_nCount; } | |
613 bool IsEmpty() const { return m_nCount == 0; } | |
614 | |
615 FX_BOOL Lookup(void* key, void*& rValue) const; | |
616 | |
617 void* GetValueAt(void* key) const; | |
618 | |
619 void*& operator[](void* key); | |
620 | |
621 void SetAt(void* key, void* newValue) { (*this)[key] = newValue; } | |
622 | |
623 FX_BOOL RemoveKey(void* key); | |
624 | |
625 void RemoveAll(); | |
626 | |
627 FX_POSITION GetStartPosition() const { | |
628 return (m_nCount == 0) ? NULL : (FX_POSITION)-1; | |
629 } | |
630 | |
631 void GetNextAssoc(FX_POSITION& rNextPosition, | |
632 void*& rKey, | |
633 void*& rValue) const; | |
634 | |
635 FX_DWORD GetHashTableSize() const { return m_nHashTableSize; } | |
636 | |
637 void InitHashTable(FX_DWORD hashSize, FX_BOOL bAllocNow = TRUE); | |
638 | |
639 protected: | |
640 CAssoc** m_pHashTable; | |
641 | |
642 FX_DWORD m_nHashTableSize; | |
643 | |
644 int m_nCount; | |
645 | |
646 CAssoc* m_pFreeList; | |
647 | |
648 struct CFX_Plex* m_pBlocks; | |
649 | |
650 int m_nBlockSize; | |
651 | |
652 FX_DWORD HashKey(void* key) const; | |
653 | |
654 CAssoc* NewAssoc(); | |
655 | |
656 void FreeAssoc(CAssoc* pAssoc); | |
657 | |
658 CAssoc* GetAssocAt(void* key, FX_DWORD& hash) const; | |
659 }; | |
660 | |
661 template <class KeyType, class ValueType> | |
662 class CFX_MapPtrTemplate : public CFX_MapPtrToPtr { | |
663 public: | |
664 CFX_MapPtrTemplate() : CFX_MapPtrToPtr(10) {} | |
665 | |
666 FX_BOOL Lookup(KeyType key, ValueType& rValue) const { | |
667 void* pValue = NULL; | |
668 if (!CFX_MapPtrToPtr::Lookup((void*)(uintptr_t)key, pValue)) { | |
669 return FALSE; | |
670 } | |
671 rValue = (ValueType)(uintptr_t)pValue; | |
672 return TRUE; | |
673 } | |
674 | |
675 ValueType& operator[](KeyType key) { | |
676 return (ValueType&)CFX_MapPtrToPtr::operator[]((void*)(uintptr_t)key); | |
677 } | |
678 | |
679 void SetAt(KeyType key, ValueType newValue) { | |
680 CFX_MapPtrToPtr::SetAt((void*)(uintptr_t)key, (void*)(uintptr_t)newValue); | |
681 } | |
682 | |
683 FX_BOOL RemoveKey(KeyType key) { | |
684 return CFX_MapPtrToPtr::RemoveKey((void*)(uintptr_t)key); | |
685 } | |
686 | |
687 void GetNextAssoc(FX_POSITION& rNextPosition, | |
688 KeyType& rKey, | |
689 ValueType& rValue) const { | |
690 void* pKey = NULL; | |
691 void* pValue = NULL; | |
692 CFX_MapPtrToPtr::GetNextAssoc(rNextPosition, pKey, pValue); | |
693 rKey = (KeyType)(uintptr_t)pKey; | |
694 rValue = (ValueType)(uintptr_t)pValue; | |
695 } | |
696 }; | |
697 #endif // PDF_ENABLE_XFA | |
698 | |
699 class CFX_PtrList { | |
700 protected: | |
701 struct CNode { | |
702 CNode* pNext; | |
703 CNode* pPrev; | |
704 void* data; | |
705 }; | |
706 | |
707 public: | |
708 CFX_PtrList(int nBlockSize = 10); | |
709 | |
710 FX_POSITION GetHeadPosition() const { return (FX_POSITION)m_pNodeHead; } | |
711 FX_POSITION GetTailPosition() const { return (FX_POSITION)m_pNodeTail; } | |
712 | |
713 void* GetNext(FX_POSITION& rPosition) const { | |
714 CNode* pNode = (CNode*)rPosition; | |
715 rPosition = (FX_POSITION)pNode->pNext; | |
716 return pNode->data; | |
717 } | |
718 | |
719 void* GetPrev(FX_POSITION& rPosition) const { | |
720 CNode* pNode = (CNode*)rPosition; | |
721 rPosition = (FX_POSITION)pNode->pPrev; | |
722 return pNode->data; | |
723 } | |
724 | |
725 FX_POSITION GetNextPosition(FX_POSITION pos) const { | |
726 return ((CNode*)pos)->pNext; | |
727 } | |
728 | |
729 FX_POSITION GetPrevPosition(FX_POSITION pos) const { | |
730 return ((CNode*)pos)->pPrev; | |
731 } | |
732 | |
733 void* GetAt(FX_POSITION rPosition) const { | |
734 CNode* pNode = (CNode*)rPosition; | |
735 return pNode->data; | |
736 } | |
737 | |
738 int GetCount() const { return m_nCount; } | |
739 FX_POSITION AddTail(void* newElement); | |
740 FX_POSITION AddHead(void* newElement); | |
741 | |
742 void SetAt(FX_POSITION pos, void* newElement) { | |
743 CNode* pNode = (CNode*)pos; | |
744 pNode->data = newElement; | |
745 } | |
746 FX_POSITION InsertAfter(FX_POSITION pos, void* newElement); | |
747 | |
748 FX_POSITION Find(void* searchValue, FX_POSITION startAfter = NULL) const; | |
749 FX_POSITION FindIndex(int index) const; | |
750 | |
751 void RemoveAt(FX_POSITION pos); | |
752 void RemoveAll(); | |
753 | |
754 protected: | |
755 CNode* m_pNodeHead; | |
756 CNode* m_pNodeTail; | |
757 int m_nCount; | |
758 CNode* m_pNodeFree; | |
759 struct CFX_Plex* m_pBlocks; | |
760 int m_nBlockSize; | |
761 | |
762 CNode* NewNode(CNode* pPrev, CNode* pNext); | |
763 void FreeNode(CNode* pNode); | |
764 | |
765 public: | |
766 ~CFX_PtrList(); | |
767 }; | |
768 typedef void (*PD_CALLBACK_FREEDATA)(void* pData); | |
769 | |
770 struct FX_PRIVATEDATA { | |
771 void FreeData(); | |
772 | |
773 void* m_pModuleId; | |
774 void* m_pData; | |
775 PD_CALLBACK_FREEDATA m_pCallback; | |
776 FX_BOOL m_bSelfDestruct; | |
777 }; | |
778 | |
779 class CFX_PrivateData { | |
780 public: | |
781 CFX_PrivateData(); | |
782 ~CFX_PrivateData(); | |
783 | |
784 void ClearAll(); | |
785 | |
786 void SetPrivateData(void* module_id, | |
787 void* pData, | |
788 PD_CALLBACK_FREEDATA callback); | |
789 void SetPrivateObj(void* module_id, CFX_DestructObject* pObj); | |
790 | |
791 void* GetPrivateData(void* module_id); | |
792 FX_BOOL LookupPrivateData(void* module_id, void*& pData) const { | |
793 if (!module_id) { | |
794 return FALSE; | |
795 } | |
796 FX_DWORD nCount = m_DataList.GetSize(); | |
797 for (FX_DWORD n = 0; n < nCount; n++) { | |
798 if (m_DataList[n].m_pModuleId == module_id) { | |
799 pData = m_DataList[n].m_pData; | |
800 return TRUE; | |
801 } | |
802 } | |
803 return FALSE; | |
804 } | |
805 | |
806 FX_BOOL RemovePrivateData(void* module_id); | |
807 | |
808 protected: | |
809 CFX_ArrayTemplate<FX_PRIVATEDATA> m_DataList; | |
810 | |
811 void AddData(void* module_id, | |
812 void* pData, | |
813 PD_CALLBACK_FREEDATA callback, | |
814 FX_BOOL bSelfDestruct); | |
815 }; | |
816 | |
817 class CFX_BitStream { | |
818 public: | |
819 void Init(const uint8_t* pData, FX_DWORD dwSize); | |
820 | |
821 FX_DWORD GetBits(FX_DWORD nBits); | |
822 | |
823 void ByteAlign(); | |
824 | |
825 FX_BOOL IsEOF() { return m_BitPos >= m_BitSize; } | |
826 | |
827 void SkipBits(FX_DWORD nBits) { m_BitPos += nBits; } | |
828 | |
829 void Rewind() { m_BitPos = 0; } | |
830 | |
831 FX_DWORD GetPos() const { return m_BitPos; } | |
832 | |
833 FX_DWORD BitsRemaining() const { | |
834 return m_BitSize >= m_BitPos ? m_BitSize - m_BitPos : 0; | |
835 } | |
836 | |
837 protected: | |
838 FX_DWORD m_BitPos; | |
839 | |
840 FX_DWORD m_BitSize; | |
841 | |
842 const uint8_t* m_pData; | |
843 }; | |
844 template <class ObjClass> | |
845 class CFX_CountRef { | |
846 public: | |
847 typedef CFX_CountRef<ObjClass> Ref; | |
848 | |
849 class CountedObj : public ObjClass { | |
850 public: | |
851 CountedObj() {} | |
852 | |
853 CountedObj(const CountedObj& src) : ObjClass(src) {} | |
854 | |
855 int m_RefCount; | |
856 }; | |
857 | |
858 CFX_CountRef() { m_pObject = NULL; } | |
859 | |
860 CFX_CountRef(const Ref& ref) { | |
861 m_pObject = ref.m_pObject; | |
862 if (m_pObject) { | |
863 m_pObject->m_RefCount++; | |
864 } | |
865 } | |
866 | |
867 ~CFX_CountRef() { | |
868 if (!m_pObject) { | |
869 return; | |
870 } | |
871 m_pObject->m_RefCount--; | |
872 if (m_pObject->m_RefCount <= 0) { | |
873 delete m_pObject; | |
874 } | |
875 } | |
876 | |
877 ObjClass* New() { | |
878 if (m_pObject) { | |
879 m_pObject->m_RefCount--; | |
880 if (m_pObject->m_RefCount <= 0) { | |
881 delete m_pObject; | |
882 } | |
883 } | |
884 m_pObject = new CountedObj; | |
885 m_pObject->m_RefCount = 1; | |
886 return m_pObject; | |
887 } | |
888 | |
889 void operator=(const Ref& ref) { | |
890 if (ref.m_pObject) { | |
891 ref.m_pObject->m_RefCount++; | |
892 } | |
893 if (m_pObject) { | |
894 m_pObject->m_RefCount--; | |
895 if (m_pObject->m_RefCount <= 0) { | |
896 delete m_pObject; | |
897 } | |
898 } | |
899 m_pObject = ref.m_pObject; | |
900 } | |
901 | |
902 void operator=(void* p) { | |
903 FXSYS_assert(p == 0); | |
904 if (!m_pObject) { | |
905 return; | |
906 } | |
907 m_pObject->m_RefCount--; | |
908 if (m_pObject->m_RefCount <= 0) { | |
909 delete m_pObject; | |
910 } | |
911 m_pObject = NULL; | |
912 } | |
913 | |
914 const ObjClass* GetObject() const { return m_pObject; } | |
915 | |
916 operator const ObjClass*() const { return m_pObject; } | |
917 | |
918 FX_BOOL IsNull() const { return !m_pObject; } | |
919 | |
920 FX_BOOL NotNull() const { return !IsNull(); } | |
921 | |
922 ObjClass* GetModify() { | |
923 if (!m_pObject) { | |
924 m_pObject = new CountedObj; | |
925 m_pObject->m_RefCount = 1; | |
926 } else if (m_pObject->m_RefCount > 1) { | |
927 m_pObject->m_RefCount--; | |
928 CountedObj* pOldObject = m_pObject; | |
929 m_pObject = new CountedObj(*pOldObject); | |
930 m_pObject->m_RefCount = 1; | |
931 } | |
932 return m_pObject; | |
933 } | |
934 | |
935 void SetNull() { | |
936 if (!m_pObject) { | |
937 return; | |
938 } | |
939 m_pObject->m_RefCount--; | |
940 if (m_pObject->m_RefCount <= 0) { | |
941 delete m_pObject; | |
942 } | |
943 m_pObject = NULL; | |
944 } | |
945 | |
946 bool operator==(const Ref& ref) const { return m_pObject == ref.m_pObject; } | |
947 | |
948 protected: | |
949 CountedObj* m_pObject; | |
950 }; | |
951 class IFX_Pause { | |
952 public: | |
953 virtual ~IFX_Pause() {} | |
954 virtual FX_BOOL NeedToPauseNow() = 0; | |
955 }; | |
956 | |
957 template <typename T> | |
958 class CFX_AutoRestorer { | |
959 public: | |
960 explicit CFX_AutoRestorer(T* location) | |
961 : m_Location(location), m_OldValue(*location) {} | |
962 ~CFX_AutoRestorer() { *m_Location = m_OldValue; } | |
963 | |
964 private: | |
965 T* const m_Location; | |
966 const T m_OldValue; | |
967 }; | |
968 | |
969 #define FX_DATALIST_LENGTH 1024 | |
970 template <size_t unit> | |
971 class CFX_SortListArray { | |
972 protected: | |
973 struct DataList { | |
974 int32_t start; | |
975 | |
976 int32_t count; | |
977 uint8_t* data; | |
978 }; | |
979 | |
980 public: | |
981 CFX_SortListArray() : m_CurList(0) {} | |
982 | |
983 ~CFX_SortListArray() { Clear(); } | |
984 | |
985 void Clear() { | |
986 for (int32_t i = m_DataLists.GetUpperBound(); i >= 0; i--) { | |
987 DataList list = m_DataLists.ElementAt(i); | |
988 FX_Free(list.data); | |
989 } | |
990 m_DataLists.RemoveAll(); | |
991 m_CurList = 0; | |
992 } | |
993 | |
994 void Append(int32_t nStart, int32_t nCount) { | |
995 if (nStart < 0) { | |
996 return; | |
997 } | |
998 while (nCount > 0) { | |
999 int32_t temp_count = std::min(nCount, FX_DATALIST_LENGTH); | |
1000 DataList list; | |
1001 list.data = FX_Alloc2D(uint8_t, temp_count, unit); | |
1002 list.start = nStart; | |
1003 list.count = temp_count; | |
1004 Append(list); | |
1005 nCount -= temp_count; | |
1006 nStart += temp_count; | |
1007 } | |
1008 } | |
1009 | |
1010 uint8_t* GetAt(int32_t nIndex) { | |
1011 if (nIndex < 0) { | |
1012 return NULL; | |
1013 } | |
1014 if (m_CurList < 0 || m_CurList >= m_DataLists.GetSize()) { | |
1015 return NULL; | |
1016 } | |
1017 DataList* pCurList = m_DataLists.GetDataPtr(m_CurList); | |
1018 if (!pCurList || nIndex < pCurList->start || | |
1019 nIndex >= pCurList->start + pCurList->count) { | |
1020 pCurList = NULL; | |
1021 int32_t iStart = 0; | |
1022 int32_t iEnd = m_DataLists.GetUpperBound(); | |
1023 int32_t iMid = 0; | |
1024 while (iStart <= iEnd) { | |
1025 iMid = (iStart + iEnd) / 2; | |
1026 DataList* list = m_DataLists.GetDataPtr(iMid); | |
1027 if (nIndex < list->start) { | |
1028 iEnd = iMid - 1; | |
1029 } else if (nIndex >= list->start + list->count) { | |
1030 iStart = iMid + 1; | |
1031 } else { | |
1032 pCurList = list; | |
1033 m_CurList = iMid; | |
1034 break; | |
1035 } | |
1036 } | |
1037 } | |
1038 return pCurList ? pCurList->data + (nIndex - pCurList->start) * unit : NULL; | |
1039 } | |
1040 | |
1041 protected: | |
1042 void Append(const DataList& list) { | |
1043 int32_t iStart = 0; | |
1044 int32_t iEnd = m_DataLists.GetUpperBound(); | |
1045 int32_t iFind = 0; | |
1046 while (iStart <= iEnd) { | |
1047 int32_t iMid = (iStart + iEnd) / 2; | |
1048 DataList* cur_list = m_DataLists.GetDataPtr(iMid); | |
1049 if (list.start < cur_list->start + cur_list->count) { | |
1050 iEnd = iMid - 1; | |
1051 } else { | |
1052 if (iMid == iEnd) { | |
1053 iFind = iMid + 1; | |
1054 break; | |
1055 } | |
1056 DataList* next_list = m_DataLists.GetDataPtr(iMid + 1); | |
1057 if (list.start < next_list->start) { | |
1058 iFind = iMid + 1; | |
1059 break; | |
1060 } else { | |
1061 iStart = iMid + 1; | |
1062 } | |
1063 } | |
1064 } | |
1065 m_DataLists.InsertAt(iFind, list); | |
1066 } | |
1067 int32_t m_CurList; | |
1068 CFX_ArrayTemplate<DataList> m_DataLists; | |
1069 }; | |
1070 template <typename T1, typename T2> | |
1071 class CFX_ListArrayTemplate { | |
1072 public: | |
1073 void Clear() { m_Data.Clear(); } | |
1074 | |
1075 void Add(int32_t nStart, int32_t nCount) { m_Data.Append(nStart, nCount); } | |
1076 | |
1077 T2& operator[](int32_t nIndex) { | |
1078 uint8_t* data = m_Data.GetAt(nIndex); | |
1079 FXSYS_assert(data); | |
1080 return (T2&)(*(volatile T2*)data); | |
1081 } | |
1082 | |
1083 T2* GetPtrAt(int32_t nIndex) { return (T2*)m_Data.GetAt(nIndex); } | |
1084 | |
1085 protected: | |
1086 T1 m_Data; | |
1087 }; | |
1088 typedef CFX_ListArrayTemplate<CFX_SortListArray<sizeof(FX_FILESIZE)>, | |
1089 FX_FILESIZE> CFX_FileSizeListArray; | |
1090 | |
1091 #ifdef PDF_ENABLE_XFA | |
1092 class IFX_Unknown { | |
1093 public: | |
1094 virtual ~IFX_Unknown() {} | |
1095 virtual FX_DWORD Release() = 0; | |
1096 virtual FX_DWORD AddRef() = 0; | |
1097 }; | |
1098 #define FX_IsOdd(a) ((a)&1) | |
1099 #endif // PDF_ENABLE_XFA | |
1100 | |
1101 class CFX_Vector_3by1 { | |
1102 public: | |
1103 CFX_Vector_3by1() : a(0.0f), b(0.0f), c(0.0f) {} | |
1104 | |
1105 CFX_Vector_3by1(FX_FLOAT a1, FX_FLOAT b1, FX_FLOAT c1) | |
1106 : a(a1), b(b1), c(c1) {} | |
1107 | |
1108 FX_FLOAT a; | |
1109 FX_FLOAT b; | |
1110 FX_FLOAT c; | |
1111 }; | |
1112 class CFX_Matrix_3by3 { | |
1113 public: | |
1114 CFX_Matrix_3by3() | |
1115 : a(0.0f), | |
1116 b(0.0f), | |
1117 c(0.0f), | |
1118 d(0.0f), | |
1119 e(0.0f), | |
1120 f(0.0f), | |
1121 g(0.0f), | |
1122 h(0.0f), | |
1123 i(0.0f) {} | |
1124 | |
1125 CFX_Matrix_3by3(FX_FLOAT a1, | |
1126 FX_FLOAT b1, | |
1127 FX_FLOAT c1, | |
1128 FX_FLOAT d1, | |
1129 FX_FLOAT e1, | |
1130 FX_FLOAT f1, | |
1131 FX_FLOAT g1, | |
1132 FX_FLOAT h1, | |
1133 FX_FLOAT i1) | |
1134 : a(a1), b(b1), c(c1), d(d1), e(e1), f(f1), g(g1), h(h1), i(i1) {} | |
1135 | |
1136 CFX_Matrix_3by3 Inverse(); | |
1137 | |
1138 CFX_Matrix_3by3 Multiply(const CFX_Matrix_3by3& m); | |
1139 | |
1140 CFX_Vector_3by1 TransformVector(const CFX_Vector_3by1& v); | |
1141 | |
1142 FX_FLOAT a; | |
1143 FX_FLOAT b; | |
1144 FX_FLOAT c; | |
1145 FX_FLOAT d; | |
1146 FX_FLOAT e; | |
1147 FX_FLOAT f; | |
1148 FX_FLOAT g; | |
1149 FX_FLOAT h; | |
1150 FX_FLOAT i; | |
1151 }; | |
1152 | |
1153 #endif // CORE_INCLUDE_FXCRT_FX_BASIC_H_ | |
OLD | NEW |