diff options
Diffstat (limited to 'core/src/fxcrt')
26 files changed, 9621 insertions, 9621 deletions
diff --git a/core/src/fxcrt/extension.h b/core/src/fxcrt/extension.h index bf5c5ec6ad..8d9597bfd1 100644 --- a/core/src/fxcrt/extension.h +++ b/core/src/fxcrt/extension.h @@ -1,413 +1,413 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _FXCRT_EXTENSION_IMP_
-#define _FXCRT_EXTENSION_IMP_
-class IFXCRT_FileAccess
-{
-public:
- virtual ~IFXCRT_FileAccess() {}
- virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode) = 0;
- virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode) = 0;
- virtual void Close() = 0;
- virtual void Release(IFX_Allocator* pAllocator = NULL) = 0;
- virtual FX_FILESIZE GetSize() const = 0;
- virtual FX_FILESIZE GetPosition() const = 0;
- virtual FX_FILESIZE SetPosition(FX_FILESIZE pos) = 0;
- virtual size_t Read(void* pBuffer, size_t szBuffer) = 0;
- virtual size_t Write(const void* pBuffer, size_t szBuffer) = 0;
- virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) = 0;
- virtual size_t WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos) = 0;
- virtual FX_BOOL Flush() = 0;
- virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0;
-};
-IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator = NULL);
-class CFX_CRTFileStream : public IFX_FileStream, public CFX_Object
-{
-public:
- CFX_CRTFileStream(IFXCRT_FileAccess* pFA, IFX_Allocator* pAllocator) : m_pAllocator(pAllocator), m_pFile(pFA), m_dwCount(1), m_bUseRange(FALSE), m_nOffset(0), m_nSize(0) {}
- ~CFX_CRTFileStream()
- {
- if (m_pFile) {
- m_pFile->Release(m_pAllocator);
- }
- }
- virtual IFX_FileStream* Retain()
- {
- m_dwCount ++;
- return this;
- }
- virtual void Release()
- {
- FX_DWORD nCount = -- m_dwCount;
- if (!nCount) {
- if (m_pAllocator) {
- FX_DeleteAtAllocator(this, m_pAllocator, CFX_CRTFileStream);
- } else {
- delete this;
- }
- }
- }
- virtual FX_FILESIZE GetSize()
- {
- return m_bUseRange ? m_nSize : m_pFile->GetSize();
- }
- virtual FX_BOOL IsEOF()
- {
- return GetPosition() >= GetSize();
- }
- virtual FX_FILESIZE GetPosition()
- {
- FX_FILESIZE pos = m_pFile->GetPosition();
- if (m_bUseRange) {
- pos -= m_nOffset;
- }
- return pos;
- }
- virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_FILESIZE size)
- {
- if (offset < 0 || offset + size > m_pFile->GetSize()) {
- return FALSE;
- }
- m_nOffset = offset, m_nSize = size;
- m_bUseRange = TRUE;
- m_pFile->SetPosition(m_nOffset);
- return TRUE;
- }
- virtual void ClearRange()
- {
- m_bUseRange = FALSE;
- }
- virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size)
- {
- if (m_bUseRange) {
- if (offset + size > (size_t)GetSize()) {
- return FALSE;
- }
- offset += m_nOffset;
- }
- return (FX_BOOL)m_pFile->ReadPos(buffer, size, offset);
- }
- virtual size_t ReadBlock(void* buffer, size_t size)
- {
- if (m_bUseRange) {
- FX_FILESIZE availSize = m_nOffset + m_nSize - m_pFile->GetPosition();
- if ((size_t)availSize < size) {
- size -= size - (size_t)availSize;
- }
- }
- return m_pFile->Read(buffer, size);
- }
- virtual FX_BOOL WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size)
- {
- if (m_bUseRange) {
- offset += m_nOffset;
- }
- return (FX_BOOL)m_pFile->WritePos(buffer, size, offset);
- }
- virtual FX_BOOL Flush()
- {
- return m_pFile->Flush();
- }
- IFX_Allocator* m_pAllocator;
- IFXCRT_FileAccess* m_pFile;
- FX_DWORD m_dwCount;
- FX_BOOL m_bUseRange;
- FX_FILESIZE m_nOffset;
- FX_FILESIZE m_nSize;
-};
-#define FX_MEMSTREAM_BlockSize (64 * 1024)
-#define FX_MEMSTREAM_Consecutive 0x01
-#define FX_MEMSTREAM_TakeOver 0x02
-class CFX_MemoryStream : public IFX_MemoryStream, public CFX_Object
-{
-public:
- CFX_MemoryStream(FX_BOOL bConsecutive, IFX_Allocator* pAllocator)
- : m_Blocks(pAllocator)
- , m_dwCount(1)
- , m_nTotalSize(0)
- , m_nCurSize(0)
- , m_nCurPos(0)
- , m_nGrowSize(FX_MEMSTREAM_BlockSize)
- , m_bUseRange(FALSE)
- {
- m_dwFlags = FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0);
- }
- CFX_MemoryStream(FX_LPBYTE pBuffer, size_t nSize, FX_BOOL bTakeOver, IFX_Allocator* pAllocator)
- : m_Blocks(pAllocator)
- , m_dwCount(1)
- , m_nTotalSize(nSize)
- , m_nCurSize(nSize)
- , m_nCurPos(0)
- , m_nGrowSize(FX_MEMSTREAM_BlockSize)
- , m_bUseRange(FALSE)
- {
- m_Blocks.Add(pBuffer);
- m_dwFlags = FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0);
- }
- ~CFX_MemoryStream()
- {
- IFX_Allocator* pAllocator = m_Blocks.m_pAllocator;
- if (m_dwFlags & FX_MEMSTREAM_TakeOver) {
- for (FX_INT32 i = 0; i < m_Blocks.GetSize(); i ++) {
- FX_Allocator_Free(pAllocator, (FX_LPBYTE)m_Blocks[i]);
- }
- }
- m_Blocks.RemoveAll();
- }
- virtual IFX_FileStream* Retain()
- {
- m_dwCount ++;
- return this;
- }
- virtual void Release()
- {
- FX_DWORD nCount = -- m_dwCount;
- if (nCount) {
- return;
- }
- IFX_Allocator* pAllocator = m_Blocks.m_pAllocator;
- if (pAllocator) {
- FX_DeleteAtAllocator(this, pAllocator, CFX_MemoryStream);
- } else {
- delete this;
- }
- }
- virtual FX_FILESIZE GetSize()
- {
- return m_bUseRange ? (FX_FILESIZE) m_nSize : (FX_FILESIZE)m_nCurSize;
- }
- virtual FX_BOOL IsEOF()
- {
- return m_nCurPos >= (size_t)GetSize();
- }
- virtual FX_FILESIZE GetPosition()
- {
- FX_FILESIZE pos = (FX_FILESIZE)m_nCurPos;
- if (m_bUseRange) {
- pos -= (FX_FILESIZE)m_nOffset;
- }
- return pos;
- }
- virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_FILESIZE size)
- {
- if (offset < 0 || (size_t)(offset + size) > m_nCurSize) {
- return FALSE;
- }
- m_nOffset = (size_t)offset, m_nSize = (size_t)size;
- m_bUseRange = TRUE;
- m_nCurPos = m_nOffset;
- return TRUE;
- }
- virtual void ClearRange()
- {
- m_bUseRange = FALSE;
- }
- virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size)
- {
- if (!buffer || !size) {
- return FALSE;
- }
- if (m_bUseRange) {
- offset += (FX_FILESIZE)m_nOffset;
- }
- if ((size_t)offset + size > m_nCurSize) {
- return FALSE;
- }
- m_nCurPos = (size_t)offset + size;
- if (m_dwFlags & FX_MEMSTREAM_Consecutive) {
- FXSYS_memcpy32(buffer, (FX_LPBYTE)m_Blocks[0] + (size_t)offset, size);
- return TRUE;
- }
- size_t nStartBlock = (size_t)offset / m_nGrowSize;
- offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize);
- while (size) {
- size_t nRead = m_nGrowSize - (size_t)offset;
- if (nRead > size) {
- nRead = size;
- }
- FXSYS_memcpy32(buffer, (FX_LPBYTE)m_Blocks[(int)nStartBlock] + (size_t)offset, nRead);
- buffer = ((FX_LPBYTE)buffer) + nRead;
- size -= nRead;
- nStartBlock ++;
- offset = 0;
- }
- return TRUE;
- }
- virtual size_t ReadBlock(void* buffer, size_t size)
- {
- if (m_nCurPos >= m_nCurSize) {
- return 0;
- }
- if (m_bUseRange) {
- size_t availSize = m_nOffset + m_nSize - m_nCurPos;
- if (availSize < size) {
- size -= size - (size_t)availSize;
- }
- }
- size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos);
- if (!ReadBlock(buffer, (FX_INT32)m_nCurPos, nRead)) {
- return 0;
- }
- return nRead;
- }
- virtual FX_BOOL WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size)
- {
- if (!buffer || !size) {
- return FALSE;
- }
- if (m_bUseRange) {
- offset += (FX_FILESIZE)m_nOffset;
- }
- if (m_dwFlags & FX_MEMSTREAM_Consecutive) {
- m_nCurPos = (size_t)offset + size;
- if (m_nCurPos > m_nTotalSize) {
- IFX_Allocator* pAllocator = m_Blocks.m_pAllocator;
- m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize;
- if (m_Blocks.GetSize() < 1) {
- void* block = FX_Allocator_Alloc(pAllocator, FX_BYTE, m_nTotalSize);
- m_Blocks.Add(block);
- } else {
- m_Blocks[0] = FX_Allocator_Realloc(pAllocator, FX_BYTE, m_Blocks[0], m_nTotalSize);
- }
- if (!m_Blocks[0]) {
- m_Blocks.RemoveAll();
- return FALSE;
- }
- }
- FXSYS_memcpy32((FX_LPBYTE)m_Blocks[0] + (size_t)offset, buffer, size);
- if (m_nCurSize < m_nCurPos) {
- m_nCurSize = m_nCurPos;
- }
- return TRUE;
- }
- if (!ExpandBlocks((size_t)offset + size)) {
- return FALSE;
- }
- m_nCurPos = (size_t)offset + size;
- size_t nStartBlock = (size_t)offset / m_nGrowSize;
- offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize);
- while (size) {
- size_t nWrite = m_nGrowSize - (size_t)offset;
- if (nWrite > size) {
- nWrite = size;
- }
- FXSYS_memcpy32((FX_LPBYTE)m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite);
- buffer = ((FX_LPBYTE)buffer) + nWrite;
- size -= nWrite;
- nStartBlock ++;
- offset = 0;
- }
- return TRUE;
- }
- virtual FX_BOOL Flush()
- {
- return TRUE;
- }
- virtual FX_BOOL IsConsecutive() const
- {
- return m_dwFlags & FX_MEMSTREAM_Consecutive;
- }
- virtual void EstimateSize(size_t nInitSize, size_t nGrowSize)
- {
- if (m_dwFlags & FX_MEMSTREAM_Consecutive) {
- if (m_Blocks.GetSize() < 1) {
- FX_LPBYTE pBlock = FX_Allocator_Alloc(m_Blocks.m_pAllocator, FX_BYTE, FX_MAX(nInitSize, 4096));
- if (pBlock) {
- m_Blocks.Add(pBlock);
- }
- }
- m_nGrowSize = FX_MAX(nGrowSize, 4096);
- } else if (m_Blocks.GetSize() < 1) {
- m_nGrowSize = FX_MAX(nGrowSize, 4096);
- }
- }
- virtual FX_LPBYTE GetBuffer() const
- {
- return m_Blocks.GetSize() ? (FX_LPBYTE)m_Blocks[0] : NULL;
- }
- virtual void AttachBuffer(FX_LPBYTE pBuffer, size_t nSize, FX_BOOL bTakeOver = FALSE)
- {
- if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) {
- return;
- }
- m_Blocks.RemoveAll();
- m_Blocks.Add(pBuffer);
- m_nTotalSize = m_nCurSize = nSize;
- m_nCurPos = 0;
- m_dwFlags = FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0);
- ClearRange();
- }
- virtual void DetachBuffer()
- {
- if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) {
- return;
- }
- m_Blocks.RemoveAll();
- m_nTotalSize = m_nCurSize = m_nCurPos = 0;
- m_dwFlags = FX_MEMSTREAM_TakeOver;
- ClearRange();
- }
-protected:
- CFX_PtrArray m_Blocks;
- FX_DWORD m_dwCount;
- size_t m_nTotalSize;
- size_t m_nCurSize;
- size_t m_nCurPos;
- size_t m_nGrowSize;
- FX_DWORD m_dwFlags;
- FX_BOOL m_bUseRange;
- size_t m_nOffset;
- size_t m_nSize;
- FX_BOOL ExpandBlocks(size_t size)
- {
- if (m_nCurSize < size) {
- m_nCurSize = size;
- }
- if (size <= m_nTotalSize) {
- return TRUE;
- }
- FX_INT32 iCount = m_Blocks.GetSize();
- size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize;
- m_Blocks.SetSize(m_Blocks.GetSize() + (FX_INT32)size, -1);
- IFX_Allocator* pAllocator = m_Blocks.m_pAllocator;
- while (size --) {
- FX_LPBYTE pBlock = FX_Allocator_Alloc(pAllocator, FX_BYTE, m_nGrowSize);
- if (!pBlock) {
- return FALSE;
- }
- m_Blocks.SetAt(iCount ++, pBlock);
- m_nTotalSize += m_nGrowSize;
- }
- return TRUE;
- }
-};
-#ifdef __cplusplus
-extern "C" {
-#endif
-#define MT_N 848
-#define MT_M 456
-#define MT_Matrix_A 0x9908b0df
-#define MT_Upper_Mask 0x80000000
-#define MT_Lower_Mask 0x7fffffff
-typedef struct _FX_MTRANDOMCONTEXT {
- _FX_MTRANDOMCONTEXT()
- {
- mti = MT_N + 1;
- bHaveSeed = FALSE;
- }
- FX_DWORD mti;
- FX_BOOL bHaveSeed;
- FX_DWORD mt[MT_N];
-} FX_MTRANDOMCONTEXT, * FX_LPMTRANDOMCONTEXT;
-typedef FX_MTRANDOMCONTEXT const * FX_LPCMTRANDOMCONTEXT;
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
-FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount);
-#endif
-#ifdef __cplusplus
-}
-#endif
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _FXCRT_EXTENSION_IMP_ +#define _FXCRT_EXTENSION_IMP_ +class IFXCRT_FileAccess +{ +public: + virtual ~IFXCRT_FileAccess() {} + virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode) = 0; + virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode) = 0; + virtual void Close() = 0; + virtual void Release(IFX_Allocator* pAllocator = NULL) = 0; + virtual FX_FILESIZE GetSize() const = 0; + virtual FX_FILESIZE GetPosition() const = 0; + virtual FX_FILESIZE SetPosition(FX_FILESIZE pos) = 0; + virtual size_t Read(void* pBuffer, size_t szBuffer) = 0; + virtual size_t Write(const void* pBuffer, size_t szBuffer) = 0; + virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) = 0; + virtual size_t WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos) = 0; + virtual FX_BOOL Flush() = 0; + virtual FX_BOOL Truncate(FX_FILESIZE szFile) = 0; +}; +IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator = NULL); +class CFX_CRTFileStream : public IFX_FileStream, public CFX_Object +{ +public: + CFX_CRTFileStream(IFXCRT_FileAccess* pFA, IFX_Allocator* pAllocator) : m_pAllocator(pAllocator), m_pFile(pFA), m_dwCount(1), m_bUseRange(FALSE), m_nOffset(0), m_nSize(0) {} + ~CFX_CRTFileStream() + { + if (m_pFile) { + m_pFile->Release(m_pAllocator); + } + } + virtual IFX_FileStream* Retain() + { + m_dwCount ++; + return this; + } + virtual void Release() + { + FX_DWORD nCount = -- m_dwCount; + if (!nCount) { + if (m_pAllocator) { + FX_DeleteAtAllocator(this, m_pAllocator, CFX_CRTFileStream); + } else { + delete this; + } + } + } + virtual FX_FILESIZE GetSize() + { + return m_bUseRange ? m_nSize : m_pFile->GetSize(); + } + virtual FX_BOOL IsEOF() + { + return GetPosition() >= GetSize(); + } + virtual FX_FILESIZE GetPosition() + { + FX_FILESIZE pos = m_pFile->GetPosition(); + if (m_bUseRange) { + pos -= m_nOffset; + } + return pos; + } + virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_FILESIZE size) + { + if (offset < 0 || offset + size > m_pFile->GetSize()) { + return FALSE; + } + m_nOffset = offset, m_nSize = size; + m_bUseRange = TRUE; + m_pFile->SetPosition(m_nOffset); + return TRUE; + } + virtual void ClearRange() + { + m_bUseRange = FALSE; + } + virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) + { + if (m_bUseRange) { + if (offset + size > (size_t)GetSize()) { + return FALSE; + } + offset += m_nOffset; + } + return (FX_BOOL)m_pFile->ReadPos(buffer, size, offset); + } + virtual size_t ReadBlock(void* buffer, size_t size) + { + if (m_bUseRange) { + FX_FILESIZE availSize = m_nOffset + m_nSize - m_pFile->GetPosition(); + if ((size_t)availSize < size) { + size -= size - (size_t)availSize; + } + } + return m_pFile->Read(buffer, size); + } + virtual FX_BOOL WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) + { + if (m_bUseRange) { + offset += m_nOffset; + } + return (FX_BOOL)m_pFile->WritePos(buffer, size, offset); + } + virtual FX_BOOL Flush() + { + return m_pFile->Flush(); + } + IFX_Allocator* m_pAllocator; + IFXCRT_FileAccess* m_pFile; + FX_DWORD m_dwCount; + FX_BOOL m_bUseRange; + FX_FILESIZE m_nOffset; + FX_FILESIZE m_nSize; +}; +#define FX_MEMSTREAM_BlockSize (64 * 1024) +#define FX_MEMSTREAM_Consecutive 0x01 +#define FX_MEMSTREAM_TakeOver 0x02 +class CFX_MemoryStream : public IFX_MemoryStream, public CFX_Object +{ +public: + CFX_MemoryStream(FX_BOOL bConsecutive, IFX_Allocator* pAllocator) + : m_Blocks(pAllocator) + , m_dwCount(1) + , m_nTotalSize(0) + , m_nCurSize(0) + , m_nCurPos(0) + , m_nGrowSize(FX_MEMSTREAM_BlockSize) + , m_bUseRange(FALSE) + { + m_dwFlags = FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); + } + CFX_MemoryStream(FX_LPBYTE pBuffer, size_t nSize, FX_BOOL bTakeOver, IFX_Allocator* pAllocator) + : m_Blocks(pAllocator) + , m_dwCount(1) + , m_nTotalSize(nSize) + , m_nCurSize(nSize) + , m_nCurPos(0) + , m_nGrowSize(FX_MEMSTREAM_BlockSize) + , m_bUseRange(FALSE) + { + m_Blocks.Add(pBuffer); + m_dwFlags = FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); + } + ~CFX_MemoryStream() + { + IFX_Allocator* pAllocator = m_Blocks.m_pAllocator; + if (m_dwFlags & FX_MEMSTREAM_TakeOver) { + for (FX_INT32 i = 0; i < m_Blocks.GetSize(); i ++) { + FX_Allocator_Free(pAllocator, (FX_LPBYTE)m_Blocks[i]); + } + } + m_Blocks.RemoveAll(); + } + virtual IFX_FileStream* Retain() + { + m_dwCount ++; + return this; + } + virtual void Release() + { + FX_DWORD nCount = -- m_dwCount; + if (nCount) { + return; + } + IFX_Allocator* pAllocator = m_Blocks.m_pAllocator; + if (pAllocator) { + FX_DeleteAtAllocator(this, pAllocator, CFX_MemoryStream); + } else { + delete this; + } + } + virtual FX_FILESIZE GetSize() + { + return m_bUseRange ? (FX_FILESIZE) m_nSize : (FX_FILESIZE)m_nCurSize; + } + virtual FX_BOOL IsEOF() + { + return m_nCurPos >= (size_t)GetSize(); + } + virtual FX_FILESIZE GetPosition() + { + FX_FILESIZE pos = (FX_FILESIZE)m_nCurPos; + if (m_bUseRange) { + pos -= (FX_FILESIZE)m_nOffset; + } + return pos; + } + virtual FX_BOOL SetRange(FX_FILESIZE offset, FX_FILESIZE size) + { + if (offset < 0 || (size_t)(offset + size) > m_nCurSize) { + return FALSE; + } + m_nOffset = (size_t)offset, m_nSize = (size_t)size; + m_bUseRange = TRUE; + m_nCurPos = m_nOffset; + return TRUE; + } + virtual void ClearRange() + { + m_bUseRange = FALSE; + } + virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) + { + if (!buffer || !size) { + return FALSE; + } + if (m_bUseRange) { + offset += (FX_FILESIZE)m_nOffset; + } + if ((size_t)offset + size > m_nCurSize) { + return FALSE; + } + m_nCurPos = (size_t)offset + size; + if (m_dwFlags & FX_MEMSTREAM_Consecutive) { + FXSYS_memcpy32(buffer, (FX_LPBYTE)m_Blocks[0] + (size_t)offset, size); + return TRUE; + } + size_t nStartBlock = (size_t)offset / m_nGrowSize; + offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); + while (size) { + size_t nRead = m_nGrowSize - (size_t)offset; + if (nRead > size) { + nRead = size; + } + FXSYS_memcpy32(buffer, (FX_LPBYTE)m_Blocks[(int)nStartBlock] + (size_t)offset, nRead); + buffer = ((FX_LPBYTE)buffer) + nRead; + size -= nRead; + nStartBlock ++; + offset = 0; + } + return TRUE; + } + virtual size_t ReadBlock(void* buffer, size_t size) + { + if (m_nCurPos >= m_nCurSize) { + return 0; + } + if (m_bUseRange) { + size_t availSize = m_nOffset + m_nSize - m_nCurPos; + if (availSize < size) { + size -= size - (size_t)availSize; + } + } + size_t nRead = FX_MIN(size, m_nCurSize - m_nCurPos); + if (!ReadBlock(buffer, (FX_INT32)m_nCurPos, nRead)) { + return 0; + } + return nRead; + } + virtual FX_BOOL WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) + { + if (!buffer || !size) { + return FALSE; + } + if (m_bUseRange) { + offset += (FX_FILESIZE)m_nOffset; + } + if (m_dwFlags & FX_MEMSTREAM_Consecutive) { + m_nCurPos = (size_t)offset + size; + if (m_nCurPos > m_nTotalSize) { + IFX_Allocator* pAllocator = m_Blocks.m_pAllocator; + m_nTotalSize = (m_nCurPos + m_nGrowSize - 1) / m_nGrowSize * m_nGrowSize; + if (m_Blocks.GetSize() < 1) { + void* block = FX_Allocator_Alloc(pAllocator, FX_BYTE, m_nTotalSize); + m_Blocks.Add(block); + } else { + m_Blocks[0] = FX_Allocator_Realloc(pAllocator, FX_BYTE, m_Blocks[0], m_nTotalSize); + } + if (!m_Blocks[0]) { + m_Blocks.RemoveAll(); + return FALSE; + } + } + FXSYS_memcpy32((FX_LPBYTE)m_Blocks[0] + (size_t)offset, buffer, size); + if (m_nCurSize < m_nCurPos) { + m_nCurSize = m_nCurPos; + } + return TRUE; + } + if (!ExpandBlocks((size_t)offset + size)) { + return FALSE; + } + m_nCurPos = (size_t)offset + size; + size_t nStartBlock = (size_t)offset / m_nGrowSize; + offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); + while (size) { + size_t nWrite = m_nGrowSize - (size_t)offset; + if (nWrite > size) { + nWrite = size; + } + FXSYS_memcpy32((FX_LPBYTE)m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite); + buffer = ((FX_LPBYTE)buffer) + nWrite; + size -= nWrite; + nStartBlock ++; + offset = 0; + } + return TRUE; + } + virtual FX_BOOL Flush() + { + return TRUE; + } + virtual FX_BOOL IsConsecutive() const + { + return m_dwFlags & FX_MEMSTREAM_Consecutive; + } + virtual void EstimateSize(size_t nInitSize, size_t nGrowSize) + { + if (m_dwFlags & FX_MEMSTREAM_Consecutive) { + if (m_Blocks.GetSize() < 1) { + FX_LPBYTE pBlock = FX_Allocator_Alloc(m_Blocks.m_pAllocator, FX_BYTE, FX_MAX(nInitSize, 4096)); + if (pBlock) { + m_Blocks.Add(pBlock); + } + } + m_nGrowSize = FX_MAX(nGrowSize, 4096); + } else if (m_Blocks.GetSize() < 1) { + m_nGrowSize = FX_MAX(nGrowSize, 4096); + } + } + virtual FX_LPBYTE GetBuffer() const + { + return m_Blocks.GetSize() ? (FX_LPBYTE)m_Blocks[0] : NULL; + } + virtual void AttachBuffer(FX_LPBYTE pBuffer, size_t nSize, FX_BOOL bTakeOver = FALSE) + { + if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { + return; + } + m_Blocks.RemoveAll(); + m_Blocks.Add(pBuffer); + m_nTotalSize = m_nCurSize = nSize; + m_nCurPos = 0; + m_dwFlags = FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); + ClearRange(); + } + virtual void DetachBuffer() + { + if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { + return; + } + m_Blocks.RemoveAll(); + m_nTotalSize = m_nCurSize = m_nCurPos = 0; + m_dwFlags = FX_MEMSTREAM_TakeOver; + ClearRange(); + } +protected: + CFX_PtrArray m_Blocks; + FX_DWORD m_dwCount; + size_t m_nTotalSize; + size_t m_nCurSize; + size_t m_nCurPos; + size_t m_nGrowSize; + FX_DWORD m_dwFlags; + FX_BOOL m_bUseRange; + size_t m_nOffset; + size_t m_nSize; + FX_BOOL ExpandBlocks(size_t size) + { + if (m_nCurSize < size) { + m_nCurSize = size; + } + if (size <= m_nTotalSize) { + return TRUE; + } + FX_INT32 iCount = m_Blocks.GetSize(); + size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; + m_Blocks.SetSize(m_Blocks.GetSize() + (FX_INT32)size, -1); + IFX_Allocator* pAllocator = m_Blocks.m_pAllocator; + while (size --) { + FX_LPBYTE pBlock = FX_Allocator_Alloc(pAllocator, FX_BYTE, m_nGrowSize); + if (!pBlock) { + return FALSE; + } + m_Blocks.SetAt(iCount ++, pBlock); + m_nTotalSize += m_nGrowSize; + } + return TRUE; + } +}; +#ifdef __cplusplus +extern "C" { +#endif +#define MT_N 848 +#define MT_M 456 +#define MT_Matrix_A 0x9908b0df +#define MT_Upper_Mask 0x80000000 +#define MT_Lower_Mask 0x7fffffff +typedef struct _FX_MTRANDOMCONTEXT { + _FX_MTRANDOMCONTEXT() + { + mti = MT_N + 1; + bHaveSeed = FALSE; + } + FX_DWORD mti; + FX_BOOL bHaveSeed; + FX_DWORD mt[MT_N]; +} FX_MTRANDOMCONTEXT, * FX_LPMTRANDOMCONTEXT; +typedef FX_MTRANDOMCONTEXT const * FX_LPCMTRANDOMCONTEXT; +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount); +#endif +#ifdef __cplusplus +} +#endif +#endif diff --git a/core/src/fxcrt/fx_basic_array.cpp b/core/src/fxcrt/fx_basic_array.cpp index 36857ce336..93f2b2fec5 100644 --- a/core/src/fxcrt/fx_basic_array.cpp +++ b/core/src/fxcrt/fx_basic_array.cpp @@ -1,365 +1,365 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_basic.h"
-CFX_BasicArray::CFX_BasicArray(int unit_size, IFX_Allocator* pAllocator)
- : m_pAllocator(pAllocator)
- , m_pData(NULL)
- , m_nSize(0)
- , m_nMaxSize(0)
- , m_nGrowBy(0)
-{
- if (unit_size < 0 || unit_size > (1 << 28)) {
- m_nUnitSize = 4;
- } else {
- m_nUnitSize = unit_size;
- }
-}
-CFX_BasicArray::~CFX_BasicArray()
-{
- FX_Allocator_Free(m_pAllocator, m_pData);
-}
-FX_BOOL CFX_BasicArray::SetSize(int nNewSize, int nGrowBy)
-{
- if (nNewSize < 0 || nNewSize > (1 << 28) / m_nUnitSize) {
- m_pData = NULL;
- m_nSize = m_nMaxSize = 0;
- return FALSE;
- }
- if (nGrowBy >= 0) {
- m_nGrowBy = nGrowBy;
- }
- if (nNewSize == 0) {
- if (m_pData != NULL) {
- FX_Allocator_Free(m_pAllocator, m_pData);
- m_pData = NULL;
- }
- m_nSize = m_nMaxSize = 0;
- } else if (m_pData == NULL) {
- m_pData = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, nNewSize * m_nUnitSize);
- if (!m_pData) {
- m_nSize = m_nMaxSize = 0;
- return FALSE;
- }
- FXSYS_memset32(m_pData, 0, nNewSize * m_nUnitSize);
- m_nSize = m_nMaxSize = nNewSize;
- } else if (nNewSize <= m_nMaxSize) {
- if (nNewSize > m_nSize) {
- FXSYS_memset32(m_pData + m_nSize * m_nUnitSize, 0, (nNewSize - m_nSize) * m_nUnitSize);
- }
- m_nSize = nNewSize;
- } else {
- int nGrowBy = m_nGrowBy;
- if (nGrowBy == 0) {
- nGrowBy = m_nSize / 8;
- nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy);
- }
- int nNewMax;
- if (nNewSize < m_nMaxSize + nGrowBy) {
- nNewMax = m_nMaxSize + nGrowBy;
- } else {
- nNewMax = nNewSize;
- }
- FX_LPBYTE pNewData = FX_Allocator_Realloc(m_pAllocator, FX_BYTE, m_pData, nNewMax * m_nUnitSize);
- if (pNewData == NULL) {
- return FALSE;
- }
- FXSYS_memset32(pNewData + m_nSize * m_nUnitSize, 0, (nNewMax - m_nSize) * m_nUnitSize);
- m_pData = pNewData;
- m_nSize = nNewSize;
- m_nMaxSize = nNewMax;
- }
- return TRUE;
-}
-FX_BOOL CFX_BasicArray::Append(const CFX_BasicArray& src)
-{
- int nOldSize = m_nSize;
- if (!SetSize(m_nSize + src.m_nSize, -1)) {
- return FALSE;
- }
- FXSYS_memcpy32(m_pData + nOldSize * m_nUnitSize, src.m_pData, src.m_nSize * m_nUnitSize);
- return TRUE;
-}
-FX_BOOL CFX_BasicArray::Copy(const CFX_BasicArray& src)
-{
- if (!SetSize(src.m_nSize, -1)) {
- return FALSE;
- }
- FXSYS_memcpy32(m_pData, src.m_pData, src.m_nSize * m_nUnitSize);
- return TRUE;
-}
-FX_LPBYTE CFX_BasicArray::InsertSpaceAt(int nIndex, int nCount)
-{
- if (nIndex < 0 || nCount <= 0) {
- return NULL;
- }
- if (nIndex >= m_nSize) {
- if (!SetSize(nIndex + nCount, -1)) {
- return NULL;
- }
- } else {
- int nOldSize = m_nSize;
- if (!SetSize(m_nSize + nCount, -1)) {
- return NULL;
- }
- FXSYS_memmove32(m_pData + (nIndex + nCount)*m_nUnitSize, m_pData + nIndex * m_nUnitSize,
- (nOldSize - nIndex) * m_nUnitSize);
- FXSYS_memset32(m_pData + nIndex * m_nUnitSize, 0, nCount * m_nUnitSize);
- }
- return m_pData + nIndex * m_nUnitSize;
-}
-FX_BOOL CFX_BasicArray::RemoveAt(int nIndex, int nCount)
-{
- if (nIndex < 0 || nCount <= 0 || m_nSize < nIndex + nCount) {
- return FALSE;
- }
- int nMoveCount = m_nSize - (nIndex + nCount);
- if (nMoveCount) {
- FXSYS_memmove32(m_pData + nIndex * m_nUnitSize, m_pData + (nIndex + nCount) * m_nUnitSize, nMoveCount * m_nUnitSize);
- }
- m_nSize -= nCount;
- return TRUE;
-}
-FX_BOOL CFX_BasicArray::InsertAt(int nStartIndex, const CFX_BasicArray* pNewArray)
-{
- if (pNewArray == NULL) {
- return FALSE;
- }
- if (pNewArray->m_nSize == 0) {
- return TRUE;
- }
- if (!InsertSpaceAt(nStartIndex, pNewArray->m_nSize)) {
- return FALSE;
- }
- FXSYS_memcpy32(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData, pNewArray->m_nSize * m_nUnitSize);
- return TRUE;
-}
-const void* CFX_BasicArray::GetDataPtr(int index) const
-{
- if (index < 0 || index >= m_nSize || m_pData == NULL) {
- return NULL;
- }
- return m_pData + index * m_nUnitSize;
-}
-CFX_BaseSegmentedArray::CFX_BaseSegmentedArray(int unit_size, int segment_units, int index_size, IFX_Allocator* pAllocator)
- : m_pAllocator(pAllocator)
- , m_UnitSize(unit_size)
- , m_SegmentSize(segment_units)
- , m_IndexSize(index_size)
- , m_IndexDepth(0)
- , m_DataSize(0)
- , m_pIndex(NULL)
-{
-}
-void CFX_BaseSegmentedArray::SetUnitSize(int unit_size, int segment_units, int index_size)
-{
- ASSERT(m_DataSize == 0);
- m_UnitSize = unit_size;
- m_SegmentSize = segment_units;
- m_IndexSize = index_size;
-}
-CFX_BaseSegmentedArray::~CFX_BaseSegmentedArray()
-{
- RemoveAll();
-}
-static void _ClearIndex(IFX_Allocator* pAllcator, int level, int size, void** pIndex)
-{
- if (level == 0) {
- FX_Allocator_Free(pAllcator, pIndex);
- return;
- }
- for (int i = 0; i < size; i ++) {
- if (pIndex[i] == NULL) {
- continue;
- }
- _ClearIndex(pAllcator, level - 1, size, (void**)pIndex[i]);
- }
- FX_Allocator_Free(pAllcator, pIndex);
-}
-void CFX_BaseSegmentedArray::RemoveAll()
-{
- if (m_pIndex == NULL) {
- return;
- }
- _ClearIndex(m_pAllocator, m_IndexDepth, m_IndexSize, (void**)m_pIndex);
- m_pIndex = NULL;
- m_IndexDepth = 0;
- m_DataSize = 0;
-}
-void* CFX_BaseSegmentedArray::Add()
-{
- if (m_DataSize % m_SegmentSize) {
- return GetAt(m_DataSize ++);
- }
- void* pSegment = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, m_UnitSize * m_SegmentSize);
- if (!pSegment) {
- return NULL;
- }
- if (m_pIndex == NULL) {
- m_pIndex = pSegment;
- m_DataSize ++;
- return pSegment;
- }
- if (m_IndexDepth == 0) {
- void** pIndex = (void**)FX_Allocator_Alloc(m_pAllocator, void*, m_IndexSize);
- if (pIndex == NULL) {
- FX_Allocator_Free(m_pAllocator, pSegment);
- return NULL;
- }
- FXSYS_memset32(pIndex, 0, sizeof(void*) * m_IndexSize);
- pIndex[0] = m_pIndex;
- pIndex[1] = pSegment;
- m_pIndex = pIndex;
- m_DataSize ++;
- m_IndexDepth ++;
- return pSegment;
- }
- int seg_index = m_DataSize / m_SegmentSize;
- if (seg_index % m_IndexSize) {
- void** pIndex = GetIndex(seg_index);
- pIndex[seg_index % m_IndexSize] = pSegment;
- m_DataSize ++;
- return pSegment;
- }
- int tree_size = 1;
- int i;
- for (i = 0; i < m_IndexDepth; i ++) {
- tree_size *= m_IndexSize;
- }
- if (m_DataSize == tree_size * m_SegmentSize) {
- void** pIndex = (void**)FX_Allocator_Alloc(m_pAllocator, void*, m_IndexSize);
- if (pIndex == NULL) {
- FX_Allocator_Free(m_pAllocator, pSegment);
- return NULL;
- }
- FXSYS_memset32(pIndex, 0, sizeof(void*) * m_IndexSize);
- pIndex[0] = m_pIndex;
- m_pIndex = pIndex;
- m_IndexDepth ++;
- } else {
- tree_size /= m_IndexSize;
- }
- void** pSpot = (void**)m_pIndex;
- for (i = 1; i < m_IndexDepth; i ++) {
- if (pSpot[seg_index / tree_size] == NULL) {
- pSpot[seg_index / tree_size] = (void*)FX_Allocator_Alloc(m_pAllocator, void*, m_IndexSize);
- if (pSpot[seg_index / tree_size] == NULL) {
- break;
- }
- FXSYS_memset32(pSpot[seg_index / tree_size], 0, sizeof(void*) * m_IndexSize);
- }
- pSpot = (void**)pSpot[seg_index / tree_size];
- seg_index = seg_index % tree_size;
- tree_size /= m_IndexSize;
- }
- if (i < m_IndexDepth) {
- FX_Allocator_Free(m_pAllocator, pSegment);
- RemoveAll();
- return NULL;
- }
- pSpot[seg_index % m_IndexSize] = pSegment;
- m_DataSize ++;
- return pSegment;
-}
-void** CFX_BaseSegmentedArray::GetIndex(int seg_index) const
-{
- ASSERT(m_IndexDepth != 0);
- if (m_IndexDepth == 1) {
- return (void**)m_pIndex;
- } else if (m_IndexDepth == 2) {
- return (void**)((void**)m_pIndex)[seg_index / m_IndexSize];
- }
- int tree_size = 1;
- int i;
- for (i = 1; i < m_IndexDepth; i ++) {
- tree_size *= m_IndexSize;
- }
- void** pSpot = (void**)m_pIndex;
- for (i = 1; i < m_IndexDepth; i ++) {
- pSpot = (void**)pSpot[seg_index / tree_size];
- seg_index = seg_index % tree_size;
- tree_size /= m_IndexSize;
- }
- return pSpot;
-}
-void* CFX_BaseSegmentedArray::IterateSegment(FX_LPCBYTE pSegment, int count, FX_BOOL (*callback)(void* param, void* pData), void* param) const
-{
- for (int i = 0; i < count; i ++) {
- if (!callback(param, (void*)(pSegment + i * m_UnitSize))) {
- return (void*)(pSegment + i * m_UnitSize);
- }
- }
- return NULL;
-}
-void* CFX_BaseSegmentedArray::IterateIndex(int level, int& start, void** pIndex, FX_BOOL (*callback)(void* param, void* pData), void* param) const
-{
- if (level == 0) {
- int count = m_DataSize - start;
- if (count > m_SegmentSize) {
- count = m_SegmentSize;
- }
- start += count;
- return IterateSegment((FX_LPCBYTE)pIndex, count, callback, param);
- }
- for (int i = 0; i < m_IndexSize; i ++) {
- if (pIndex[i] == NULL) {
- continue;
- }
- void* p = IterateIndex(level - 1, start, (void**)pIndex[i], callback, param);
- if (p) {
- return p;
- }
- }
- return NULL;
-}
-void* CFX_BaseSegmentedArray::Iterate(FX_BOOL (*callback)(void* param, void* pData), void* param) const
-{
- if (m_pIndex == NULL) {
- return NULL;
- }
- int start = 0;
- return IterateIndex(m_IndexDepth, start, (void**)m_pIndex, callback, param);
-}
-void* CFX_BaseSegmentedArray::GetAt(int index) const
-{
- if (index < 0 || index >= m_DataSize) {
- return NULL;
- }
- if (m_IndexDepth == 0) {
- return (FX_LPBYTE)m_pIndex + m_UnitSize * index;
- }
- int seg_index = index / m_SegmentSize;
- return (FX_LPBYTE)GetIndex(seg_index)[seg_index % m_IndexSize] + (index % m_SegmentSize) * m_UnitSize;
-}
-void CFX_BaseSegmentedArray::Delete(int index, int count)
-{
- if(index < 0 || count < 1 || index + count > m_DataSize) {
- return;
- }
- int i;
- for (i = index; i < m_DataSize - count; i ++) {
- FX_BYTE* pSrc = (FX_BYTE*)GetAt(i + count);
- FX_BYTE* pDest = (FX_BYTE*)GetAt(i);
- for (int j = 0; j < m_UnitSize; j ++) {
- pDest[j] = pSrc[j];
- }
- }
- int new_segs = (m_DataSize - count + m_SegmentSize - 1) / m_SegmentSize;
- int old_segs = (m_DataSize + m_SegmentSize - 1) / m_SegmentSize;
- if (new_segs < old_segs) {
- if(m_IndexDepth) {
- for (i = new_segs; i < old_segs; i ++) {
- void** pIndex = GetIndex(i);
- FX_Allocator_Free(m_pAllocator, pIndex[i % m_IndexSize]);
- pIndex[i % m_IndexSize] = NULL;
- }
- } else {
- FX_Allocator_Free(m_pAllocator, m_pIndex);
- m_pIndex = NULL;
- }
- }
- m_DataSize -= count;
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_basic.h" +CFX_BasicArray::CFX_BasicArray(int unit_size, IFX_Allocator* pAllocator) + : m_pAllocator(pAllocator) + , m_pData(NULL) + , m_nSize(0) + , m_nMaxSize(0) + , m_nGrowBy(0) +{ + if (unit_size < 0 || unit_size > (1 << 28)) { + m_nUnitSize = 4; + } else { + m_nUnitSize = unit_size; + } +} +CFX_BasicArray::~CFX_BasicArray() +{ + FX_Allocator_Free(m_pAllocator, m_pData); +} +FX_BOOL CFX_BasicArray::SetSize(int nNewSize, int nGrowBy) +{ + if (nNewSize < 0 || nNewSize > (1 << 28) / m_nUnitSize) { + m_pData = NULL; + m_nSize = m_nMaxSize = 0; + return FALSE; + } + if (nGrowBy >= 0) { + m_nGrowBy = nGrowBy; + } + if (nNewSize == 0) { + if (m_pData != NULL) { + FX_Allocator_Free(m_pAllocator, m_pData); + m_pData = NULL; + } + m_nSize = m_nMaxSize = 0; + } else if (m_pData == NULL) { + m_pData = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, nNewSize * m_nUnitSize); + if (!m_pData) { + m_nSize = m_nMaxSize = 0; + return FALSE; + } + FXSYS_memset32(m_pData, 0, nNewSize * m_nUnitSize); + m_nSize = m_nMaxSize = nNewSize; + } else if (nNewSize <= m_nMaxSize) { + if (nNewSize > m_nSize) { + FXSYS_memset32(m_pData + m_nSize * m_nUnitSize, 0, (nNewSize - m_nSize) * m_nUnitSize); + } + m_nSize = nNewSize; + } else { + int nGrowBy = m_nGrowBy; + if (nGrowBy == 0) { + nGrowBy = m_nSize / 8; + nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy); + } + int nNewMax; + if (nNewSize < m_nMaxSize + nGrowBy) { + nNewMax = m_nMaxSize + nGrowBy; + } else { + nNewMax = nNewSize; + } + FX_LPBYTE pNewData = FX_Allocator_Realloc(m_pAllocator, FX_BYTE, m_pData, nNewMax * m_nUnitSize); + if (pNewData == NULL) { + return FALSE; + } + FXSYS_memset32(pNewData + m_nSize * m_nUnitSize, 0, (nNewMax - m_nSize) * m_nUnitSize); + m_pData = pNewData; + m_nSize = nNewSize; + m_nMaxSize = nNewMax; + } + return TRUE; +} +FX_BOOL CFX_BasicArray::Append(const CFX_BasicArray& src) +{ + int nOldSize = m_nSize; + if (!SetSize(m_nSize + src.m_nSize, -1)) { + return FALSE; + } + FXSYS_memcpy32(m_pData + nOldSize * m_nUnitSize, src.m_pData, src.m_nSize * m_nUnitSize); + return TRUE; +} +FX_BOOL CFX_BasicArray::Copy(const CFX_BasicArray& src) +{ + if (!SetSize(src.m_nSize, -1)) { + return FALSE; + } + FXSYS_memcpy32(m_pData, src.m_pData, src.m_nSize * m_nUnitSize); + return TRUE; +} +FX_LPBYTE CFX_BasicArray::InsertSpaceAt(int nIndex, int nCount) +{ + if (nIndex < 0 || nCount <= 0) { + return NULL; + } + if (nIndex >= m_nSize) { + if (!SetSize(nIndex + nCount, -1)) { + return NULL; + } + } else { + int nOldSize = m_nSize; + if (!SetSize(m_nSize + nCount, -1)) { + return NULL; + } + FXSYS_memmove32(m_pData + (nIndex + nCount)*m_nUnitSize, m_pData + nIndex * m_nUnitSize, + (nOldSize - nIndex) * m_nUnitSize); + FXSYS_memset32(m_pData + nIndex * m_nUnitSize, 0, nCount * m_nUnitSize); + } + return m_pData + nIndex * m_nUnitSize; +} +FX_BOOL CFX_BasicArray::RemoveAt(int nIndex, int nCount) +{ + if (nIndex < 0 || nCount <= 0 || m_nSize < nIndex + nCount) { + return FALSE; + } + int nMoveCount = m_nSize - (nIndex + nCount); + if (nMoveCount) { + FXSYS_memmove32(m_pData + nIndex * m_nUnitSize, m_pData + (nIndex + nCount) * m_nUnitSize, nMoveCount * m_nUnitSize); + } + m_nSize -= nCount; + return TRUE; +} +FX_BOOL CFX_BasicArray::InsertAt(int nStartIndex, const CFX_BasicArray* pNewArray) +{ + if (pNewArray == NULL) { + return FALSE; + } + if (pNewArray->m_nSize == 0) { + return TRUE; + } + if (!InsertSpaceAt(nStartIndex, pNewArray->m_nSize)) { + return FALSE; + } + FXSYS_memcpy32(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData, pNewArray->m_nSize * m_nUnitSize); + return TRUE; +} +const void* CFX_BasicArray::GetDataPtr(int index) const +{ + if (index < 0 || index >= m_nSize || m_pData == NULL) { + return NULL; + } + return m_pData + index * m_nUnitSize; +} +CFX_BaseSegmentedArray::CFX_BaseSegmentedArray(int unit_size, int segment_units, int index_size, IFX_Allocator* pAllocator) + : m_pAllocator(pAllocator) + , m_UnitSize(unit_size) + , m_SegmentSize(segment_units) + , m_IndexSize(index_size) + , m_IndexDepth(0) + , m_DataSize(0) + , m_pIndex(NULL) +{ +} +void CFX_BaseSegmentedArray::SetUnitSize(int unit_size, int segment_units, int index_size) +{ + ASSERT(m_DataSize == 0); + m_UnitSize = unit_size; + m_SegmentSize = segment_units; + m_IndexSize = index_size; +} +CFX_BaseSegmentedArray::~CFX_BaseSegmentedArray() +{ + RemoveAll(); +} +static void _ClearIndex(IFX_Allocator* pAllcator, int level, int size, void** pIndex) +{ + if (level == 0) { + FX_Allocator_Free(pAllcator, pIndex); + return; + } + for (int i = 0; i < size; i ++) { + if (pIndex[i] == NULL) { + continue; + } + _ClearIndex(pAllcator, level - 1, size, (void**)pIndex[i]); + } + FX_Allocator_Free(pAllcator, pIndex); +} +void CFX_BaseSegmentedArray::RemoveAll() +{ + if (m_pIndex == NULL) { + return; + } + _ClearIndex(m_pAllocator, m_IndexDepth, m_IndexSize, (void**)m_pIndex); + m_pIndex = NULL; + m_IndexDepth = 0; + m_DataSize = 0; +} +void* CFX_BaseSegmentedArray::Add() +{ + if (m_DataSize % m_SegmentSize) { + return GetAt(m_DataSize ++); + } + void* pSegment = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, m_UnitSize * m_SegmentSize); + if (!pSegment) { + return NULL; + } + if (m_pIndex == NULL) { + m_pIndex = pSegment; + m_DataSize ++; + return pSegment; + } + if (m_IndexDepth == 0) { + void** pIndex = (void**)FX_Allocator_Alloc(m_pAllocator, void*, m_IndexSize); + if (pIndex == NULL) { + FX_Allocator_Free(m_pAllocator, pSegment); + return NULL; + } + FXSYS_memset32(pIndex, 0, sizeof(void*) * m_IndexSize); + pIndex[0] = m_pIndex; + pIndex[1] = pSegment; + m_pIndex = pIndex; + m_DataSize ++; + m_IndexDepth ++; + return pSegment; + } + int seg_index = m_DataSize / m_SegmentSize; + if (seg_index % m_IndexSize) { + void** pIndex = GetIndex(seg_index); + pIndex[seg_index % m_IndexSize] = pSegment; + m_DataSize ++; + return pSegment; + } + int tree_size = 1; + int i; + for (i = 0; i < m_IndexDepth; i ++) { + tree_size *= m_IndexSize; + } + if (m_DataSize == tree_size * m_SegmentSize) { + void** pIndex = (void**)FX_Allocator_Alloc(m_pAllocator, void*, m_IndexSize); + if (pIndex == NULL) { + FX_Allocator_Free(m_pAllocator, pSegment); + return NULL; + } + FXSYS_memset32(pIndex, 0, sizeof(void*) * m_IndexSize); + pIndex[0] = m_pIndex; + m_pIndex = pIndex; + m_IndexDepth ++; + } else { + tree_size /= m_IndexSize; + } + void** pSpot = (void**)m_pIndex; + for (i = 1; i < m_IndexDepth; i ++) { + if (pSpot[seg_index / tree_size] == NULL) { + pSpot[seg_index / tree_size] = (void*)FX_Allocator_Alloc(m_pAllocator, void*, m_IndexSize); + if (pSpot[seg_index / tree_size] == NULL) { + break; + } + FXSYS_memset32(pSpot[seg_index / tree_size], 0, sizeof(void*) * m_IndexSize); + } + pSpot = (void**)pSpot[seg_index / tree_size]; + seg_index = seg_index % tree_size; + tree_size /= m_IndexSize; + } + if (i < m_IndexDepth) { + FX_Allocator_Free(m_pAllocator, pSegment); + RemoveAll(); + return NULL; + } + pSpot[seg_index % m_IndexSize] = pSegment; + m_DataSize ++; + return pSegment; +} +void** CFX_BaseSegmentedArray::GetIndex(int seg_index) const +{ + ASSERT(m_IndexDepth != 0); + if (m_IndexDepth == 1) { + return (void**)m_pIndex; + } else if (m_IndexDepth == 2) { + return (void**)((void**)m_pIndex)[seg_index / m_IndexSize]; + } + int tree_size = 1; + int i; + for (i = 1; i < m_IndexDepth; i ++) { + tree_size *= m_IndexSize; + } + void** pSpot = (void**)m_pIndex; + for (i = 1; i < m_IndexDepth; i ++) { + pSpot = (void**)pSpot[seg_index / tree_size]; + seg_index = seg_index % tree_size; + tree_size /= m_IndexSize; + } + return pSpot; +} +void* CFX_BaseSegmentedArray::IterateSegment(FX_LPCBYTE pSegment, int count, FX_BOOL (*callback)(void* param, void* pData), void* param) const +{ + for (int i = 0; i < count; i ++) { + if (!callback(param, (void*)(pSegment + i * m_UnitSize))) { + return (void*)(pSegment + i * m_UnitSize); + } + } + return NULL; +} +void* CFX_BaseSegmentedArray::IterateIndex(int level, int& start, void** pIndex, FX_BOOL (*callback)(void* param, void* pData), void* param) const +{ + if (level == 0) { + int count = m_DataSize - start; + if (count > m_SegmentSize) { + count = m_SegmentSize; + } + start += count; + return IterateSegment((FX_LPCBYTE)pIndex, count, callback, param); + } + for (int i = 0; i < m_IndexSize; i ++) { + if (pIndex[i] == NULL) { + continue; + } + void* p = IterateIndex(level - 1, start, (void**)pIndex[i], callback, param); + if (p) { + return p; + } + } + return NULL; +} +void* CFX_BaseSegmentedArray::Iterate(FX_BOOL (*callback)(void* param, void* pData), void* param) const +{ + if (m_pIndex == NULL) { + return NULL; + } + int start = 0; + return IterateIndex(m_IndexDepth, start, (void**)m_pIndex, callback, param); +} +void* CFX_BaseSegmentedArray::GetAt(int index) const +{ + if (index < 0 || index >= m_DataSize) { + return NULL; + } + if (m_IndexDepth == 0) { + return (FX_LPBYTE)m_pIndex + m_UnitSize * index; + } + int seg_index = index / m_SegmentSize; + return (FX_LPBYTE)GetIndex(seg_index)[seg_index % m_IndexSize] + (index % m_SegmentSize) * m_UnitSize; +} +void CFX_BaseSegmentedArray::Delete(int index, int count) +{ + if(index < 0 || count < 1 || index + count > m_DataSize) { + return; + } + int i; + for (i = index; i < m_DataSize - count; i ++) { + FX_BYTE* pSrc = (FX_BYTE*)GetAt(i + count); + FX_BYTE* pDest = (FX_BYTE*)GetAt(i); + for (int j = 0; j < m_UnitSize; j ++) { + pDest[j] = pSrc[j]; + } + } + int new_segs = (m_DataSize - count + m_SegmentSize - 1) / m_SegmentSize; + int old_segs = (m_DataSize + m_SegmentSize - 1) / m_SegmentSize; + if (new_segs < old_segs) { + if(m_IndexDepth) { + for (i = new_segs; i < old_segs; i ++) { + void** pIndex = GetIndex(i); + FX_Allocator_Free(m_pAllocator, pIndex[i % m_IndexSize]); + pIndex[i % m_IndexSize] = NULL; + } + } else { + FX_Allocator_Free(m_pAllocator, m_pIndex); + m_pIndex = NULL; + } + } + m_DataSize -= count; +} diff --git a/core/src/fxcrt/fx_basic_bstring.cpp b/core/src/fxcrt/fx_basic_bstring.cpp index f28b069efc..ef52447771 100644 --- a/core/src/fxcrt/fx_basic_bstring.cpp +++ b/core/src/fxcrt/fx_basic_bstring.cpp @@ -1,1204 +1,1204 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_basic.h"
-static int _Buffer_itoa(char* buf, int i, FX_DWORD flags)
-{
- if (i == 0) {
- buf[0] = '0';
- return 1;
- }
- char buf1[32];
- int buf_pos = 31;
- FX_DWORD u = i;
- if ((flags & FXFORMAT_SIGNED) && i < 0) {
- u = -i;
- }
- int base = 10;
- FX_LPCSTR string = "0123456789abcdef";
- if (flags & FXFORMAT_HEX) {
- base = 16;
- if (flags & FXFORMAT_CAPITAL) {
- string = "0123456789ABCDEF";
- }
- }
- while (u != 0) {
- buf1[buf_pos--] = string[u % base];
- u = u / base;
- }
- if ((flags & FXFORMAT_SIGNED) && i < 0) {
- buf1[buf_pos--] = '-';
- }
- int len = 31 - buf_pos;
- for (int ii = 0; ii < len; ii ++) {
- buf[ii] = buf1[ii + buf_pos + 1];
- }
- return len;
-}
-CFX_ByteString CFX_ByteString::FormatInteger(int i, FX_DWORD flags)
-{
- char buf[32];
- return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags));
-}
-static CFX_StringData* FX_AllocString(int nLen)
-{
- if (nLen == 0) {
- return NULL;
- }
- CFX_StringData* pData = (CFX_StringData*)FX_Alloc(FX_BYTE, sizeof(long) * 3 + (nLen + 1) * sizeof(char));
- if (!pData) {
- return NULL;
- }
- pData->m_nAllocLength = nLen;
- pData->m_nDataLength = nLen;
- pData->m_nRefs = 1;
- pData->m_String[nLen] = 0;
- return pData;
-}
-static void FX_ReleaseString(CFX_StringData* pData)
-{
- if (pData == NULL) {
- return;
- }
- pData->m_nRefs --;
- if (pData->m_nRefs <= 0) {
- FX_Free(pData);
- }
-}
-CFX_ByteString::~CFX_ByteString()
-{
- if (m_pData == NULL) {
- return;
- }
- m_pData->m_nRefs --;
- if (m_pData->m_nRefs < 1) {
- FX_Free(m_pData);
- }
-}
-CFX_ByteString::CFX_ByteString(FX_LPCSTR lpsz, FX_STRSIZE nLen)
-{
- if (nLen < 0) {
- nLen = lpsz ? (FX_STRSIZE)FXSYS_strlen(lpsz) : 0;
- }
- if (nLen) {
- m_pData = FX_AllocString(nLen);
- if (m_pData) {
- FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(char));
- }
- } else {
- m_pData = NULL;
- }
-}
-CFX_ByteString::CFX_ByteString(FX_LPCBYTE lpsz, FX_STRSIZE nLen)
-{
- if (nLen > 0) {
- m_pData = FX_AllocString(nLen);
- if (m_pData) {
- FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(char));
- }
- } else {
- m_pData = NULL;
- }
-}
-CFX_ByteString::CFX_ByteString(char ch)
-{
- m_pData = FX_AllocString(1);
- if (m_pData) {
- m_pData->m_String[0] = ch;
- }
-}
-CFX_ByteString::CFX_ByteString(const CFX_ByteString& stringSrc)
-{
- if (stringSrc.m_pData == NULL) {
- m_pData = NULL;
- return;
- }
- if (stringSrc.m_pData->m_nRefs >= 0) {
- m_pData = stringSrc.m_pData;
- m_pData->m_nRefs ++;
- } else {
- m_pData = NULL;
- *this = stringSrc;
- }
-}
-CFX_ByteString::CFX_ByteString(FX_BSTR stringSrc)
-{
- if (stringSrc.IsEmpty()) {
- m_pData = NULL;
- return;
- } else {
- m_pData = NULL;
- *this = stringSrc;
- }
-}
-CFX_ByteString::CFX_ByteString(FX_BSTR str1, FX_BSTR str2)
-{
- m_pData = NULL;
- int nNewLen = str1.GetLength() + str2.GetLength();
- if (nNewLen == 0) {
- return;
- }
- m_pData = FX_AllocString(nNewLen);
- if (m_pData) {
- FXSYS_memcpy32(m_pData->m_String, str1.GetCStr(), str1.GetLength());
- FXSYS_memcpy32(m_pData->m_String + str1.GetLength(), str2.GetCStr(), str2.GetLength());
- }
-}
-const CFX_ByteString& CFX_ByteString::operator=(FX_LPCSTR lpsz)
-{
- if (lpsz == NULL || lpsz[0] == 0) {
- Empty();
- } else {
- AssignCopy((FX_STRSIZE)FXSYS_strlen(lpsz), lpsz);
- }
- return *this;
-}
-const CFX_ByteString& CFX_ByteString::operator=(FX_BSTR str)
-{
- if (str.IsEmpty()) {
- Empty();
- } else {
- AssignCopy(str.GetLength(), str.GetCStr());
- }
- return *this;
-}
-const CFX_ByteString& CFX_ByteString::operator=(const CFX_ByteString& stringSrc)
-{
- if (m_pData == stringSrc.m_pData) {
- return *this;
- }
- if (stringSrc.IsEmpty()) {
- Empty();
- } else if ((m_pData && m_pData->m_nRefs < 0) ||
- (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) {
- AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String);
- } else {
- Empty();
- m_pData = stringSrc.m_pData;
- if (m_pData) {
- m_pData->m_nRefs ++;
- }
- }
- return *this;
-}
-const CFX_ByteString& CFX_ByteString::operator=(const CFX_BinaryBuf& buf)
-{
- Load(buf.GetBuffer(), buf.GetSize());
- return *this;
-}
-void CFX_ByteString::Load(FX_LPCBYTE buf, FX_STRSIZE len)
-{
- Empty();
- if (len) {
- m_pData = FX_AllocString(len);
- if (m_pData) {
- FXSYS_memcpy32(m_pData->m_String, buf, len * sizeof(char));
- }
- } else {
- m_pData = NULL;
- }
-}
-const CFX_ByteString& CFX_ByteString::operator+=(FX_LPCSTR lpsz)
-{
- if (lpsz) {
- ConcatInPlace((FX_STRSIZE)FXSYS_strlen(lpsz), lpsz);
- }
- return *this;
-}
-const CFX_ByteString& CFX_ByteString::operator+=(char ch)
-{
- ConcatInPlace(1, &ch);
- return *this;
-}
-const CFX_ByteString& CFX_ByteString::operator+=(const CFX_ByteString& string)
-{
- if (string.m_pData == NULL) {
- return *this;
- }
- ConcatInPlace(string.m_pData->m_nDataLength, string.m_pData->m_String);
- return *this;
-}
-const CFX_ByteString& CFX_ByteString::operator+=(FX_BSTR string)
-{
- if (string.IsEmpty()) {
- return *this;
- }
- ConcatInPlace(string.GetLength(), string.GetCStr());
- return *this;
-}
-bool CFX_ByteString::Equal(FX_BSTR str) const
-{
- if (m_pData == NULL) {
- return str.IsEmpty();
- }
- return m_pData->m_nDataLength == str.GetLength() &&
- FXSYS_memcmp32(m_pData->m_String, str.GetCStr(), str.GetLength()) == 0;
-}
-bool CFX_ByteString::operator ==(const CFX_ByteString& s2) const
-{
- if (m_pData == NULL) {
- return s2.IsEmpty();
- }
- if (s2.m_pData == NULL) {
- return false;
- }
- return m_pData->m_nDataLength == s2.m_pData->m_nDataLength &&
- FXSYS_memcmp32(m_pData->m_String, s2.m_pData->m_String, m_pData->m_nDataLength) == 0;
-}
-void CFX_ByteString::Empty()
-{
- if (m_pData == NULL) {
- return;
- }
- if (m_pData->m_nRefs > 1) {
- m_pData->m_nRefs --;
- } else {
- FX_Free(m_pData);
- }
- m_pData = NULL;
-}
-bool CFX_ByteString::EqualNoCase(FX_BSTR str) const
-{
- if (m_pData == NULL) {
- return str.IsEmpty();
- }
- FX_STRSIZE len = str.GetLength();
- if (m_pData->m_nDataLength != len) {
- return false;
- }
- FX_LPCBYTE pThis = (FX_LPCBYTE)m_pData->m_String;
- FX_LPCBYTE pThat = (FX_LPCBYTE)str;
- for (FX_STRSIZE i = 0; i < len; i ++) {
- if ((*pThis) != (*pThat)) {
- FX_BYTE bThis = *pThis;
- if (bThis >= 'A' && bThis <= 'Z') {
- bThis += 'a' - 'A';
- }
- FX_BYTE bThat = *pThat;
- if (bThat >= 'A' && bThat <= 'Z') {
- bThat += 'a' - 'A';
- }
- if (bThis != bThat) {
- return false;
- }
- }
- pThis ++;
- pThat ++;
- }
- return true;
-}
-void CFX_ByteString::AssignCopy(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData)
-{
- AllocBeforeWrite(nSrcLen);
- FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(char));
- m_pData->m_nDataLength = nSrcLen;
- m_pData->m_String[nSrcLen] = 0;
-}
-void CFX_ByteString::CopyBeforeWrite()
-{
- if (m_pData == NULL || m_pData->m_nRefs <= 1) {
- return;
- }
- CFX_StringData* pData = m_pData;
- m_pData->m_nRefs --;
- FX_STRSIZE nDataLength = pData->m_nDataLength;
- m_pData = FX_AllocString(nDataLength);
- if (m_pData != NULL) {
- FXSYS_memcpy32(m_pData->m_String, pData->m_String, (nDataLength + 1) * sizeof(char));
- }
-}
-void CFX_ByteString::AllocBeforeWrite(FX_STRSIZE nLen)
-{
- if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) {
- return;
- }
- Empty();
- m_pData = FX_AllocString(nLen);
-}
-void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength)
-{
- if (m_pData == NULL) {
- return;
- }
- CopyBeforeWrite();
- if (nNewLength == -1) {
- nNewLength = (FX_STRSIZE)FXSYS_strlen((FX_LPCSTR)m_pData->m_String);
- }
- if (nNewLength == 0) {
- Empty();
- return;
- }
- FXSYS_assert(nNewLength <= m_pData->m_nAllocLength);
- m_pData->m_nDataLength = nNewLength;
- m_pData->m_String[nNewLength] = 0;
-}
-FX_LPSTR CFX_ByteString::LockBuffer()
-{
- if (m_pData == NULL) {
- return NULL;
- }
- FX_LPSTR lpsz = GetBuffer(0);
- m_pData->m_nRefs = -1;
- return lpsz;
-}
-void CFX_ByteString::Reserve(FX_STRSIZE len)
-{
- GetBuffer(len);
- ReleaseBuffer(GetLength());
-}
-FX_LPSTR CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength)
-{
- if (m_pData == NULL && nMinBufLength == 0) {
- return NULL;
- }
- if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLength) {
- return m_pData->m_String;
- }
- if (m_pData == NULL) {
- m_pData = FX_AllocString(nMinBufLength);
- if (!m_pData) {
- return NULL;
- }
- m_pData->m_nDataLength = 0;
- m_pData->m_String[0] = 0;
- return m_pData->m_String;
- }
- CFX_StringData* pOldData = m_pData;
- FX_STRSIZE nOldLen = pOldData->m_nDataLength;
- if (nMinBufLength < nOldLen) {
- nMinBufLength = nOldLen;
- }
- m_pData = FX_AllocString(nMinBufLength);
- if (!m_pData) {
- return NULL;
- }
- FXSYS_memcpy32(m_pData->m_String, pOldData->m_String, (nOldLen + 1)*sizeof(char));
- m_pData->m_nDataLength = nOldLen;
- pOldData->m_nRefs --;
- if (pOldData->m_nRefs <= 0) {
- FX_Free(pOldData);
- }
- return m_pData->m_String;
-}
-FX_STRSIZE CFX_ByteString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount)
-{
- if (m_pData == NULL) {
- return 0;
- }
- if (nIndex < 0) {
- nIndex = 0;
- }
- FX_STRSIZE nOldLength = m_pData->m_nDataLength;
- if (nCount > 0 && nIndex < nOldLength) {
- FX_STRSIZE mLength = nIndex + nCount;
- if (mLength >= nOldLength) {
- m_pData->m_nDataLength = nIndex;
- return m_pData->m_nDataLength;
- }
- CopyBeforeWrite();
- int nBytesToCopy = nOldLength - mLength + 1;
- FXSYS_memmove32(m_pData->m_String + nIndex,
- m_pData->m_String + mLength, nBytesToCopy * sizeof(char));
- m_pData->m_nDataLength = nOldLength - nCount;
- }
- return m_pData->m_nDataLength;
-}
-void CFX_ByteString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData)
-{
- if (nSrcLen == 0 || lpszSrcData == NULL) {
- return;
- }
- if (m_pData == NULL) {
- m_pData = FX_AllocString(nSrcLen);
- if (!m_pData) {
- return;
- }
- FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(char));
- return;
- }
- if (m_pData->m_nRefs > 1 || m_pData->m_nDataLength + nSrcLen > m_pData->m_nAllocLength) {
- CFX_StringData* pOldData = m_pData;
- ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcData);
- FX_ReleaseString(pOldData);
- } else {
- FXSYS_memcpy32(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData, nSrcLen * sizeof(char));
- m_pData->m_nDataLength += nSrcLen;
- m_pData->m_String[m_pData->m_nDataLength] = 0;
- }
-}
-void CFX_ByteString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCSTR lpszSrc1Data,
- FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data)
-{
- int nNewLen = nSrc1Len + nSrc2Len;
- if (nNewLen == 0) {
- return;
- }
- m_pData = FX_AllocString(nNewLen);
- if (m_pData) {
- FXSYS_memcpy32(m_pData->m_String, lpszSrc1Data, nSrc1Len * sizeof(char));
- FXSYS_memcpy32(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len * sizeof(char));
- }
-}
-CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const
-{
- if (m_pData == NULL) {
- return CFX_ByteString();
- }
- return Mid(nFirst, m_pData->m_nDataLength - nFirst);
-}
-CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const
-{
- if (nFirst < 0) {
- nFirst = 0;
- }
- if (nCount < 0) {
- nCount = 0;
- }
- if (nFirst + nCount > m_pData->m_nDataLength) {
- nCount = m_pData->m_nDataLength - nFirst;
- }
- if (nFirst > m_pData->m_nDataLength) {
- nCount = 0;
- }
- if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) {
- return *this;
- }
- CFX_ByteString dest;
- AllocCopy(dest, nCount, nFirst, 0);
- return dest;
-}
-void CFX_ByteString::AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex,
- FX_STRSIZE nExtraLen) const
-{
- FX_STRSIZE nNewLen = nCopyLen + nExtraLen;
- if (nNewLen == 0) {
- return;
- }
- ASSERT(dest.m_pData == NULL);
- dest.m_pData = FX_AllocString(nNewLen);
- if (dest.m_pData) {
- FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, nCopyLen * sizeof(char));
- }
-}
-#define FORCE_ANSI 0x10000
-#define FORCE_UNICODE 0x20000
-#define FORCE_INT64 0x40000
-void CFX_ByteString::FormatV(FX_LPCSTR lpszFormat, va_list argList)
-{
- va_list argListSave;
-#if defined(__ARMCC_VERSION) || (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || _FX_CPU_ == _FX_ARM64_)) || defined(__native_client__)
- va_copy(argListSave, argList);
-#else
- argListSave = argList;
-#endif
- int nMaxLen = 0;
- for (FX_LPCSTR lpsz = lpszFormat; *lpsz != 0; lpsz ++) {
- if (*lpsz != '%' || *(lpsz = lpsz + 1) == '%') {
- nMaxLen += (FX_STRSIZE)FXSYS_strlen(lpsz);
- continue;
- }
- int nItemLen = 0;
- int nWidth = 0;
- for (; *lpsz != 0; lpsz ++) {
- if (*lpsz == '#') {
- nMaxLen += 2;
- } else if (*lpsz == '*') {
- nWidth = va_arg(argList, int);
- } else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0' ||
- *lpsz == ' ')
- ;
- else {
- break;
- }
- }
- if (nWidth == 0) {
- nWidth = FXSYS_atoi(lpsz);
- for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz ++)
- ;
- }
- if (nWidth < 0 || nWidth > 128 * 1024) {
- lpszFormat = "Bad width";
- nMaxLen = 10;
- break;
- }
- int nPrecision = 0;
- if (*lpsz == '.') {
- lpsz ++;
- if (*lpsz == '*') {
- nPrecision = va_arg(argList, int);
- lpsz ++;
- } else {
- nPrecision = FXSYS_atoi(lpsz);
- for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz ++)
- ;
- }
- }
- if (nPrecision < 0 || nPrecision > 128 * 1024) {
- lpszFormat = "Bad precision";
- nMaxLen = 14;
- break;
- }
- int nModifier = 0;
- if (FXSYS_strncmp(lpsz, "I64", 3) == 0) {
- lpsz += 3;
- nModifier = FORCE_INT64;
- } else {
- switch (*lpsz) {
- case 'h':
- nModifier = FORCE_ANSI;
- lpsz ++;
- break;
- case 'l':
- nModifier = FORCE_UNICODE;
- lpsz ++;
- break;
- case 'F':
- case 'N':
- case 'L':
- lpsz ++;
- break;
- }
- }
- switch (*lpsz | nModifier) {
- case 'c':
- case 'C':
- nItemLen = 2;
- va_arg(argList, int);
- break;
- case 'c'|FORCE_ANSI:
- case 'C'|FORCE_ANSI:
- nItemLen = 2;
- va_arg(argList, int);
- break;
- case 'c'|FORCE_UNICODE:
- case 'C'|FORCE_UNICODE:
- nItemLen = 2;
- va_arg(argList, int);
- break;
- case 's': {
- FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR);
- if (pstrNextArg == NULL) {
- nItemLen = 6;
- } else {
- nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg);
- if (nItemLen < 1) {
- nItemLen = 1;
- }
- }
- }
- break;
- case 'S': {
- FX_LPWSTR pstrNextArg = va_arg(argList, FX_LPWSTR);
- if (pstrNextArg == NULL) {
- nItemLen = 6;
- } else {
- nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg);
- if (nItemLen < 1) {
- nItemLen = 1;
- }
- }
- }
- break;
- case 's'|FORCE_ANSI:
- case 'S'|FORCE_ANSI: {
- FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR);
- if (pstrNextArg == NULL) {
- nItemLen = 6;
- } else {
- nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg);
- if (nItemLen < 1) {
- nItemLen = 1;
- }
- }
- }
- break;
- case 's'|FORCE_UNICODE:
- case 'S'|FORCE_UNICODE: {
- FX_LPWSTR pstrNextArg = va_arg(argList, FX_LPWSTR);
- if (pstrNextArg == NULL) {
- nItemLen = 6;
- } else {
- nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg);
- if (nItemLen < 1) {
- nItemLen = 1;
- }
- }
- }
- break;
- }
- if (nItemLen != 0) {
- if (nPrecision != 0 && nItemLen > nPrecision) {
- nItemLen = nPrecision;
- }
- if (nItemLen < nWidth) {
- nItemLen = nWidth;
- }
- } else {
- switch (*lpsz) {
- case 'd':
- case 'i':
- case 'u':
- case 'x':
- case 'X':
- case 'o':
- if (nModifier & FORCE_INT64) {
- va_arg(argList, FX_INT64);
- } else {
- va_arg(argList, int);
- }
- nItemLen = 32;
- if (nItemLen < nWidth + nPrecision) {
- nItemLen = nWidth + nPrecision;
- }
- break;
- case 'a':
- case 'A':
- case 'e':
- case 'E':
- case 'g':
- case 'G':
- va_arg(argList, double);
- nItemLen = 128;
- if (nItemLen < nWidth + nPrecision) {
- nItemLen = nWidth + nPrecision;
- }
- break;
- case 'f':
- if (nWidth + nPrecision > 100) {
- nItemLen = nPrecision + nWidth + 128;
- } else {
- double f;
- char pszTemp[256];
- f = va_arg(argList, double);
- FXSYS_sprintf(pszTemp, "%*.*f", nWidth, nPrecision + 6, f );
- nItemLen = (FX_STRSIZE)FXSYS_strlen(pszTemp);
- }
- break;
- case 'p':
- va_arg(argList, void*);
- nItemLen = 32;
- if (nItemLen < nWidth + nPrecision) {
- nItemLen = nWidth + nPrecision;
- }
- break;
- case 'n':
- va_arg(argList, int*);
- break;
- }
- }
- nMaxLen += nItemLen;
- }
- GetBuffer(nMaxLen);
- if (m_pData) {
- FXSYS_vsprintf(m_pData->m_String, lpszFormat, argListSave);
- ReleaseBuffer();
- }
- va_end(argListSave);
-}
-void CFX_ByteString::Format(FX_LPCSTR lpszFormat, ...)
-{
- va_list argList;
- va_start(argList, lpszFormat);
- FormatV(lpszFormat, argList);
- va_end(argList);
-}
-FX_STRSIZE CFX_ByteString::Insert(FX_STRSIZE nIndex, FX_CHAR ch)
-{
- CopyBeforeWrite();
- if (nIndex < 0) {
- nIndex = 0;
- }
- FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0;
- if (nIndex > nNewLength) {
- nIndex = nNewLength;
- }
- nNewLength++;
- if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) {
- CFX_StringData* pOldData = m_pData;
- FX_LPCSTR pstr = m_pData->m_String;
- m_pData = FX_AllocString(nNewLength);
- if (!m_pData) {
- return 0;
- }
- if(pOldData != NULL) {
- FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1)*sizeof(char));
- FX_ReleaseString(pOldData);
- } else {
- m_pData->m_String[0] = 0;
- }
- }
- FXSYS_memmove32(m_pData->m_String + nIndex + 1,
- m_pData->m_String + nIndex, (nNewLength - nIndex)*sizeof(char));
- m_pData->m_String[nIndex] = ch;
- m_pData->m_nDataLength = nNewLength;
- return nNewLength;
-}
-CFX_ByteString CFX_ByteString::Right(FX_STRSIZE nCount) const
-{
- if (m_pData == NULL) {
- return CFX_ByteString();
- }
- if (nCount < 0) {
- nCount = 0;
- }
- if (nCount >= m_pData->m_nDataLength) {
- return *this;
- }
- CFX_ByteString dest;
- AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount, 0);
- return dest;
-}
-CFX_ByteString CFX_ByteString::Left(FX_STRSIZE nCount) const
-{
- if (m_pData == NULL) {
- return CFX_ByteString();
- }
- if (nCount < 0) {
- nCount = 0;
- }
- if (nCount >= m_pData->m_nDataLength) {
- return *this;
- }
- CFX_ByteString dest;
- AllocCopy(dest, nCount, 0, 0);
- return dest;
-}
-FX_STRSIZE CFX_ByteString::Find(FX_CHAR ch, FX_STRSIZE nStart) const
-{
- if (m_pData == NULL) {
- return -1;
- }
- FX_STRSIZE nLength = m_pData->m_nDataLength;
- if (nStart >= nLength) {
- return -1;
- }
- FX_LPCSTR lpsz = FXSYS_strchr(m_pData->m_String + nStart, ch);
- return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
-}
-FX_STRSIZE CFX_ByteString::ReverseFind(FX_CHAR ch) const
-{
- if (m_pData == NULL) {
- return -1;
- }
- FX_STRSIZE nLength = m_pData->m_nDataLength;
- while (nLength) {
- if (m_pData->m_String[nLength - 1] == ch) {
- return nLength - 1;
- }
- nLength --;
- }
- return -1;
-}
-FX_LPCSTR FX_strstr(FX_LPCSTR str1, int len1, FX_LPCSTR str2, int len2)
-{
- if (len2 > len1 || len2 == 0) {
- return NULL;
- }
- FX_LPCSTR end_ptr = str1 + len1 - len2;
- while (str1 <= end_ptr) {
- int i = 0;
- while (1) {
- if (str1[i] != str2[i]) {
- break;
- }
- i ++;
- if (i == len2) {
- return str1;
- }
- }
- str1 ++;
- }
- return NULL;
-}
-FX_STRSIZE CFX_ByteString::Find(FX_BSTR lpszSub, FX_STRSIZE nStart) const
-{
- if (m_pData == NULL) {
- return -1;
- }
- FX_STRSIZE nLength = m_pData->m_nDataLength;
- if (nStart > nLength) {
- return -1;
- }
- FX_LPCSTR lpsz = FX_strstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart,
- lpszSub.GetCStr(), lpszSub.GetLength());
- return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
-}
-void CFX_ByteString::MakeLower()
-{
- if (m_pData == NULL) {
- return;
- }
- CopyBeforeWrite();
- if (GetLength() < 1) {
- return;
- }
- FXSYS_strlwr(m_pData->m_String);
-}
-void CFX_ByteString::MakeUpper()
-{
- if (m_pData == NULL) {
- return;
- }
- CopyBeforeWrite();
- if (GetLength() < 1) {
- return;
- }
- FXSYS_strupr(m_pData->m_String);
-}
-FX_STRSIZE CFX_ByteString::Remove(FX_CHAR chRemove)
-{
- if (m_pData == NULL) {
- return 0;
- }
- CopyBeforeWrite();
- if (GetLength() < 1) {
- return 0;
- }
- FX_LPSTR pstrSource = m_pData->m_String;
- FX_LPSTR pstrDest = m_pData->m_String;
- FX_LPSTR pstrEnd = m_pData->m_String + m_pData->m_nDataLength;
- while (pstrSource < pstrEnd) {
- if (*pstrSource != chRemove) {
- *pstrDest = *pstrSource;
- pstrDest ++;
- }
- pstrSource ++;
- }
- *pstrDest = 0;
- FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest);
- m_pData->m_nDataLength -= nCount;
- return nCount;
-}
-FX_STRSIZE CFX_ByteString::Replace(FX_BSTR lpszOld, FX_BSTR lpszNew)
-{
- if (m_pData == NULL) {
- return 0;
- }
- if (lpszOld.IsEmpty()) {
- return 0;
- }
- FX_STRSIZE nSourceLen = lpszOld.GetLength();
- FX_STRSIZE nReplacementLen = lpszNew.GetLength();
- FX_STRSIZE nCount = 0;
- FX_LPCSTR pStart = m_pData->m_String;
- FX_LPSTR pEnd = m_pData->m_String + m_pData->m_nDataLength;
- while (1) {
- FX_LPCSTR pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), lpszOld.GetCStr(), nSourceLen);
- if (pTarget == NULL) {
- break;
- }
- nCount++;
- pStart = pTarget + nSourceLen;
- }
- if (nCount == 0) {
- return 0;
- }
- FX_STRSIZE nNewLength = m_pData->m_nDataLength + (nReplacementLen - nSourceLen) * nCount;
- if (nNewLength == 0) {
- Empty();
- return nCount;
- }
- CFX_StringData* pNewData = FX_AllocString(nNewLength);
- if (!pNewData) {
- return 0;
- }
- pStart = m_pData->m_String;
- FX_LPSTR pDest = pNewData->m_String;
- for (FX_STRSIZE i = 0; i < nCount; i ++) {
- FX_LPCSTR pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), lpszOld.GetCStr(), nSourceLen);
- FXSYS_memcpy32(pDest, pStart, pTarget - pStart);
- pDest += pTarget - pStart;
- FXSYS_memcpy32(pDest, lpszNew.GetCStr(), lpszNew.GetLength());
- pDest += lpszNew.GetLength();
- pStart = pTarget + nSourceLen;
- }
- FXSYS_memcpy32(pDest, pStart, pEnd - pStart);
- FX_ReleaseString(m_pData);
- m_pData = pNewData;
- return nCount;
-}
-void CFX_ByteString::SetAt(FX_STRSIZE nIndex, FX_CHAR ch)
-{
- if (m_pData == NULL) {
- return;
- }
- FXSYS_assert(nIndex >= 0);
- FXSYS_assert(nIndex < m_pData->m_nDataLength);
- CopyBeforeWrite();
- m_pData->m_String[nIndex] = ch;
-}
-CFX_ByteString CFX_ByteString::LoadFromFile(FX_BSTR filename)
-{
- FXSYS_FILE* file = FXSYS_fopen(CFX_ByteString(filename), "rb");
- if (file == NULL) {
- return CFX_ByteString();
- }
- FXSYS_fseek(file, 0, FXSYS_SEEK_END);
- int len = FXSYS_ftell(file);
- FXSYS_fseek(file, 0, FXSYS_SEEK_SET);
- CFX_ByteString str;
- FX_LPSTR buf = str.GetBuffer(len);
- size_t readCnt = FXSYS_fread(buf, 1, len, file);
- str.ReleaseBuffer(len);
- FXSYS_fclose(file);
- return str;
-}
-CFX_WideString CFX_ByteString::UTF8Decode() const
-{
- CFX_UTF8Decoder decoder;
- for (FX_STRSIZE i = 0; i < GetLength(); i ++) {
- decoder.Input((FX_BYTE)m_pData->m_String[i]);
- }
- return decoder.GetResult();
-}
-CFX_ByteString CFX_ByteString::FromUnicode(FX_LPCWSTR str, FX_STRSIZE len)
-{
- if (len < 0) {
- len = (FX_STRSIZE)FXSYS_wcslen(str);
- }
- CFX_ByteString bstr;
- bstr.ConvertFrom(CFX_WideString(str, len));
- return bstr;
-}
-CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str)
-{
- return FromUnicode((FX_LPCWSTR)str, str.GetLength());
-}
-void CFX_ByteString::ConvertFrom(const CFX_WideString& str, CFX_CharMap* pCharMap)
-{
- if (pCharMap == NULL) {
- pCharMap = CFX_CharMap::GetDefaultMapper();
- }
- *this = (*pCharMap->m_GetByteString)(pCharMap, str);
-}
-int CFX_ByteString::Compare(FX_BSTR str) const
-{
- if (m_pData == NULL) {
- return str.IsEmpty() ? 0 : -1;
- }
- int this_len = m_pData->m_nDataLength;
- int that_len = str.GetLength();
- int min_len = this_len < that_len ? this_len : that_len;
- for (int i = 0; i < min_len; i ++) {
- if ((FX_BYTE)m_pData->m_String[i] < str.GetAt(i)) {
- return -1;
- } else if ((FX_BYTE)m_pData->m_String[i] > str.GetAt(i)) {
- return 1;
- }
- }
- if (this_len < that_len) {
- return -1;
- } else if (this_len > that_len) {
- return 1;
- }
- return 0;
-}
-void CFX_ByteString::TrimRight(FX_BSTR lpszTargets)
-{
- if (m_pData == NULL || lpszTargets.IsEmpty()) {
- return;
- }
- CopyBeforeWrite();
- FX_STRSIZE pos = GetLength();
- if (pos < 1) {
- return;
- }
- while (pos) {
- FX_STRSIZE i = 0;
- while (i < lpszTargets.GetLength() && lpszTargets[i] != m_pData->m_String[pos - 1]) {
- i ++;
- }
- if (i == lpszTargets.GetLength()) {
- break;
- }
- pos --;
- }
- if (pos < m_pData->m_nDataLength) {
- m_pData->m_String[pos] = 0;
- m_pData->m_nDataLength = pos;
- }
-}
-void CFX_ByteString::TrimRight(FX_CHAR chTarget)
-{
- TrimRight(CFX_ByteStringC(chTarget));
-}
-void CFX_ByteString::TrimRight()
-{
- TrimRight(FX_BSTRC("\x09\x0a\x0b\x0c\x0d\x20"));
-}
-void CFX_ByteString::TrimLeft(FX_BSTR lpszTargets)
-{
- if (m_pData == NULL) {
- return;
- }
- if (lpszTargets.IsEmpty()) {
- return;
- }
- CopyBeforeWrite();
- FX_STRSIZE len = GetLength();
- if (len < 1) {
- return;
- }
- FX_STRSIZE pos = 0;
- while (pos < len) {
- FX_STRSIZE i = 0;
- while (i < lpszTargets.GetLength() && lpszTargets[i] != m_pData->m_String[pos]) {
- i ++;
- }
- if (i == lpszTargets.GetLength()) {
- break;
- }
- pos ++;
- }
- if (pos) {
- FX_STRSIZE nDataLength = len - pos;
- FXSYS_memmove32(m_pData->m_String, m_pData->m_String + pos, (nDataLength + 1)*sizeof(FX_CHAR));
- m_pData->m_nDataLength = nDataLength;
- }
-}
-void CFX_ByteString::TrimLeft(FX_CHAR chTarget)
-{
- TrimLeft(CFX_ByteStringC(chTarget));
-}
-void CFX_ByteString::TrimLeft()
-{
- TrimLeft(FX_BSTRC("\x09\x0a\x0b\x0c\x0d\x20"));
-}
-FX_DWORD CFX_ByteString::GetID(FX_STRSIZE start_pos) const
-{
- return CFX_ByteStringC(*this).GetID(start_pos);
-}
-FX_DWORD CFX_ByteStringC::GetID(FX_STRSIZE start_pos) const
-{
- if (m_Length == 0) {
- return 0;
- }
- if (start_pos >= m_Length) {
- return 0;
- }
- FX_DWORD strid = 0;
- if (start_pos + 4 > m_Length) {
- for (FX_STRSIZE i = 0; i < m_Length - start_pos; i ++) {
- strid = strid * 256 + m_Ptr[start_pos + i];
- }
- strid = strid << ((4 - m_Length + start_pos) * 8);
- } else {
- for (int i = 0; i < 4; i ++) {
- strid = strid * 256 + m_Ptr[start_pos + i];
- }
- }
- return strid;
-}
-FX_STRSIZE FX_ftoa(FX_FLOAT d, FX_LPSTR buf)
-{
- buf[0] = '0';
- buf[1] = '\0';
- if (d == 0.0f) {
- return 1;
- }
- FX_BOOL bNegative = FALSE;
- if (d < 0) {
- bNegative = TRUE;
- d = -d;
- }
- int scale = 1;
- int scaled = FXSYS_round(d);
- while (scaled < 100000) {
- if (scale == 1000000) {
- break;
- }
- scale *= 10;
- scaled = FXSYS_round(d * scale);
- }
- if (scaled == 0) {
- return 1;
- }
- char buf2[32];
- int buf_size = 0;
- if (bNegative) {
- buf[buf_size++] = '-';
- }
- int i = scaled / scale;
- FXSYS_itoa(i, buf2, 10);
- FX_STRSIZE len = (FX_STRSIZE)FXSYS_strlen(buf2);
- FXSYS_memcpy32(buf + buf_size, buf2, len);
- buf_size += len;
- int fraction = scaled % scale;
- if (fraction == 0) {
- return buf_size;
- }
- buf[buf_size++] = '.';
- scale /= 10;
- while (fraction) {
- buf[buf_size++] = '0' + fraction / scale;
- fraction %= scale;
- scale /= 10;
- }
- return buf_size;
-}
-CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision)
-{
- FX_CHAR buf[32];
- FX_STRSIZE len = FX_ftoa(d, buf);
- return CFX_ByteString(buf, len);
-}
-void CFX_StringBufBase::Copy(FX_BSTR str)
-{
- m_Size = str.GetLength();
- if (m_Size > m_Limit) {
- m_Size = m_Limit;
- }
- FX_CHAR* pBuffer = (FX_CHAR*)(this + 1);
- FXSYS_memcpy32(pBuffer, str.GetPtr(), m_Size);
-}
-void CFX_StringBufBase::Append(FX_BSTR str)
-{
- int len = str.GetLength();
- if (len > m_Limit - m_Size) {
- len = m_Limit - m_Size;
- }
- FX_CHAR* pBuffer = (FX_CHAR*)(this + 1);
- FXSYS_memcpy32(pBuffer + m_Size, str.GetPtr(), len);
- m_Size += len;
-}
-void CFX_StringBufBase::Append(int i, FX_DWORD flags)
-{
- char buf[32];
- int len = _Buffer_itoa(buf, i, flags);
- Append(CFX_ByteStringC(buf, len));
-}
-void CFX_ByteStringL::Empty(IFX_Allocator* pAllocator)
-{
- if (m_Ptr) {
- FX_Allocator_Free(pAllocator, (FX_LPVOID)m_Ptr);
- }
- m_Ptr = NULL, m_Length = 0;
-}
-FX_LPSTR CFX_ByteStringL::AllocBuffer(FX_STRSIZE length, IFX_Allocator* pAllocator)
-{
- Empty(pAllocator);
- FX_LPSTR str = FX_Allocator_Alloc(pAllocator, FX_CHAR, length + 1);
- if (!str) {
- return NULL;
- }
- *(FX_LPSTR*)(&m_Ptr) = str;
- m_Length = length;
- return str;
-}
-void CFX_ByteStringL::Set(FX_BSTR src, IFX_Allocator* pAllocator)
-{
- Empty(pAllocator);
- if (src.GetCStr() != NULL && src.GetLength() > 0) {
- FX_LPSTR str = FX_Allocator_Alloc(pAllocator, FX_CHAR, src.GetLength() + 1);
- if (!str) {
- return;
- }
- FXSYS_memcpy32(str, src, src.GetLength());
- str[src.GetLength()] = '\0';
- *(FX_LPSTR*)(&m_Ptr) = str;
- m_Length = src.GetLength();
- }
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_basic.h" +static int _Buffer_itoa(char* buf, int i, FX_DWORD flags) +{ + if (i == 0) { + buf[0] = '0'; + return 1; + } + char buf1[32]; + int buf_pos = 31; + FX_DWORD u = i; + if ((flags & FXFORMAT_SIGNED) && i < 0) { + u = -i; + } + int base = 10; + FX_LPCSTR string = "0123456789abcdef"; + if (flags & FXFORMAT_HEX) { + base = 16; + if (flags & FXFORMAT_CAPITAL) { + string = "0123456789ABCDEF"; + } + } + while (u != 0) { + buf1[buf_pos--] = string[u % base]; + u = u / base; + } + if ((flags & FXFORMAT_SIGNED) && i < 0) { + buf1[buf_pos--] = '-'; + } + int len = 31 - buf_pos; + for (int ii = 0; ii < len; ii ++) { + buf[ii] = buf1[ii + buf_pos + 1]; + } + return len; +} +CFX_ByteString CFX_ByteString::FormatInteger(int i, FX_DWORD flags) +{ + char buf[32]; + return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags)); +} +static CFX_StringData* FX_AllocString(int nLen) +{ + if (nLen == 0) { + return NULL; + } + CFX_StringData* pData = (CFX_StringData*)FX_Alloc(FX_BYTE, sizeof(long) * 3 + (nLen + 1) * sizeof(char)); + if (!pData) { + return NULL; + } + pData->m_nAllocLength = nLen; + pData->m_nDataLength = nLen; + pData->m_nRefs = 1; + pData->m_String[nLen] = 0; + return pData; +} +static void FX_ReleaseString(CFX_StringData* pData) +{ + if (pData == NULL) { + return; + } + pData->m_nRefs --; + if (pData->m_nRefs <= 0) { + FX_Free(pData); + } +} +CFX_ByteString::~CFX_ByteString() +{ + if (m_pData == NULL) { + return; + } + m_pData->m_nRefs --; + if (m_pData->m_nRefs < 1) { + FX_Free(m_pData); + } +} +CFX_ByteString::CFX_ByteString(FX_LPCSTR lpsz, FX_STRSIZE nLen) +{ + if (nLen < 0) { + nLen = lpsz ? (FX_STRSIZE)FXSYS_strlen(lpsz) : 0; + } + if (nLen) { + m_pData = FX_AllocString(nLen); + if (m_pData) { + FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(char)); + } + } else { + m_pData = NULL; + } +} +CFX_ByteString::CFX_ByteString(FX_LPCBYTE lpsz, FX_STRSIZE nLen) +{ + if (nLen > 0) { + m_pData = FX_AllocString(nLen); + if (m_pData) { + FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(char)); + } + } else { + m_pData = NULL; + } +} +CFX_ByteString::CFX_ByteString(char ch) +{ + m_pData = FX_AllocString(1); + if (m_pData) { + m_pData->m_String[0] = ch; + } +} +CFX_ByteString::CFX_ByteString(const CFX_ByteString& stringSrc) +{ + if (stringSrc.m_pData == NULL) { + m_pData = NULL; + return; + } + if (stringSrc.m_pData->m_nRefs >= 0) { + m_pData = stringSrc.m_pData; + m_pData->m_nRefs ++; + } else { + m_pData = NULL; + *this = stringSrc; + } +} +CFX_ByteString::CFX_ByteString(FX_BSTR stringSrc) +{ + if (stringSrc.IsEmpty()) { + m_pData = NULL; + return; + } else { + m_pData = NULL; + *this = stringSrc; + } +} +CFX_ByteString::CFX_ByteString(FX_BSTR str1, FX_BSTR str2) +{ + m_pData = NULL; + int nNewLen = str1.GetLength() + str2.GetLength(); + if (nNewLen == 0) { + return; + } + m_pData = FX_AllocString(nNewLen); + if (m_pData) { + FXSYS_memcpy32(m_pData->m_String, str1.GetCStr(), str1.GetLength()); + FXSYS_memcpy32(m_pData->m_String + str1.GetLength(), str2.GetCStr(), str2.GetLength()); + } +} +const CFX_ByteString& CFX_ByteString::operator=(FX_LPCSTR lpsz) +{ + if (lpsz == NULL || lpsz[0] == 0) { + Empty(); + } else { + AssignCopy((FX_STRSIZE)FXSYS_strlen(lpsz), lpsz); + } + return *this; +} +const CFX_ByteString& CFX_ByteString::operator=(FX_BSTR str) +{ + if (str.IsEmpty()) { + Empty(); + } else { + AssignCopy(str.GetLength(), str.GetCStr()); + } + return *this; +} +const CFX_ByteString& CFX_ByteString::operator=(const CFX_ByteString& stringSrc) +{ + if (m_pData == stringSrc.m_pData) { + return *this; + } + if (stringSrc.IsEmpty()) { + Empty(); + } else if ((m_pData && m_pData->m_nRefs < 0) || + (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) { + AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String); + } else { + Empty(); + m_pData = stringSrc.m_pData; + if (m_pData) { + m_pData->m_nRefs ++; + } + } + return *this; +} +const CFX_ByteString& CFX_ByteString::operator=(const CFX_BinaryBuf& buf) +{ + Load(buf.GetBuffer(), buf.GetSize()); + return *this; +} +void CFX_ByteString::Load(FX_LPCBYTE buf, FX_STRSIZE len) +{ + Empty(); + if (len) { + m_pData = FX_AllocString(len); + if (m_pData) { + FXSYS_memcpy32(m_pData->m_String, buf, len * sizeof(char)); + } + } else { + m_pData = NULL; + } +} +const CFX_ByteString& CFX_ByteString::operator+=(FX_LPCSTR lpsz) +{ + if (lpsz) { + ConcatInPlace((FX_STRSIZE)FXSYS_strlen(lpsz), lpsz); + } + return *this; +} +const CFX_ByteString& CFX_ByteString::operator+=(char ch) +{ + ConcatInPlace(1, &ch); + return *this; +} +const CFX_ByteString& CFX_ByteString::operator+=(const CFX_ByteString& string) +{ + if (string.m_pData == NULL) { + return *this; + } + ConcatInPlace(string.m_pData->m_nDataLength, string.m_pData->m_String); + return *this; +} +const CFX_ByteString& CFX_ByteString::operator+=(FX_BSTR string) +{ + if (string.IsEmpty()) { + return *this; + } + ConcatInPlace(string.GetLength(), string.GetCStr()); + return *this; +} +bool CFX_ByteString::Equal(FX_BSTR str) const +{ + if (m_pData == NULL) { + return str.IsEmpty(); + } + return m_pData->m_nDataLength == str.GetLength() && + FXSYS_memcmp32(m_pData->m_String, str.GetCStr(), str.GetLength()) == 0; +} +bool CFX_ByteString::operator ==(const CFX_ByteString& s2) const +{ + if (m_pData == NULL) { + return s2.IsEmpty(); + } + if (s2.m_pData == NULL) { + return false; + } + return m_pData->m_nDataLength == s2.m_pData->m_nDataLength && + FXSYS_memcmp32(m_pData->m_String, s2.m_pData->m_String, m_pData->m_nDataLength) == 0; +} +void CFX_ByteString::Empty() +{ + if (m_pData == NULL) { + return; + } + if (m_pData->m_nRefs > 1) { + m_pData->m_nRefs --; + } else { + FX_Free(m_pData); + } + m_pData = NULL; +} +bool CFX_ByteString::EqualNoCase(FX_BSTR str) const +{ + if (m_pData == NULL) { + return str.IsEmpty(); + } + FX_STRSIZE len = str.GetLength(); + if (m_pData->m_nDataLength != len) { + return false; + } + FX_LPCBYTE pThis = (FX_LPCBYTE)m_pData->m_String; + FX_LPCBYTE pThat = (FX_LPCBYTE)str; + for (FX_STRSIZE i = 0; i < len; i ++) { + if ((*pThis) != (*pThat)) { + FX_BYTE bThis = *pThis; + if (bThis >= 'A' && bThis <= 'Z') { + bThis += 'a' - 'A'; + } + FX_BYTE bThat = *pThat; + if (bThat >= 'A' && bThat <= 'Z') { + bThat += 'a' - 'A'; + } + if (bThis != bThat) { + return false; + } + } + pThis ++; + pThat ++; + } + return true; +} +void CFX_ByteString::AssignCopy(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData) +{ + AllocBeforeWrite(nSrcLen); + FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(char)); + m_pData->m_nDataLength = nSrcLen; + m_pData->m_String[nSrcLen] = 0; +} +void CFX_ByteString::CopyBeforeWrite() +{ + if (m_pData == NULL || m_pData->m_nRefs <= 1) { + return; + } + CFX_StringData* pData = m_pData; + m_pData->m_nRefs --; + FX_STRSIZE nDataLength = pData->m_nDataLength; + m_pData = FX_AllocString(nDataLength); + if (m_pData != NULL) { + FXSYS_memcpy32(m_pData->m_String, pData->m_String, (nDataLength + 1) * sizeof(char)); + } +} +void CFX_ByteString::AllocBeforeWrite(FX_STRSIZE nLen) +{ + if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) { + return; + } + Empty(); + m_pData = FX_AllocString(nLen); +} +void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength) +{ + if (m_pData == NULL) { + return; + } + CopyBeforeWrite(); + if (nNewLength == -1) { + nNewLength = (FX_STRSIZE)FXSYS_strlen((FX_LPCSTR)m_pData->m_String); + } + if (nNewLength == 0) { + Empty(); + return; + } + FXSYS_assert(nNewLength <= m_pData->m_nAllocLength); + m_pData->m_nDataLength = nNewLength; + m_pData->m_String[nNewLength] = 0; +} +FX_LPSTR CFX_ByteString::LockBuffer() +{ + if (m_pData == NULL) { + return NULL; + } + FX_LPSTR lpsz = GetBuffer(0); + m_pData->m_nRefs = -1; + return lpsz; +} +void CFX_ByteString::Reserve(FX_STRSIZE len) +{ + GetBuffer(len); + ReleaseBuffer(GetLength()); +} +FX_LPSTR CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength) +{ + if (m_pData == NULL && nMinBufLength == 0) { + return NULL; + } + if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLength) { + return m_pData->m_String; + } + if (m_pData == NULL) { + m_pData = FX_AllocString(nMinBufLength); + if (!m_pData) { + return NULL; + } + m_pData->m_nDataLength = 0; + m_pData->m_String[0] = 0; + return m_pData->m_String; + } + CFX_StringData* pOldData = m_pData; + FX_STRSIZE nOldLen = pOldData->m_nDataLength; + if (nMinBufLength < nOldLen) { + nMinBufLength = nOldLen; + } + m_pData = FX_AllocString(nMinBufLength); + if (!m_pData) { + return NULL; + } + FXSYS_memcpy32(m_pData->m_String, pOldData->m_String, (nOldLen + 1)*sizeof(char)); + m_pData->m_nDataLength = nOldLen; + pOldData->m_nRefs --; + if (pOldData->m_nRefs <= 0) { + FX_Free(pOldData); + } + return m_pData->m_String; +} +FX_STRSIZE CFX_ByteString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) +{ + if (m_pData == NULL) { + return 0; + } + if (nIndex < 0) { + nIndex = 0; + } + FX_STRSIZE nOldLength = m_pData->m_nDataLength; + if (nCount > 0 && nIndex < nOldLength) { + FX_STRSIZE mLength = nIndex + nCount; + if (mLength >= nOldLength) { + m_pData->m_nDataLength = nIndex; + return m_pData->m_nDataLength; + } + CopyBeforeWrite(); + int nBytesToCopy = nOldLength - mLength + 1; + FXSYS_memmove32(m_pData->m_String + nIndex, + m_pData->m_String + mLength, nBytesToCopy * sizeof(char)); + m_pData->m_nDataLength = nOldLength - nCount; + } + return m_pData->m_nDataLength; +} +void CFX_ByteString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData) +{ + if (nSrcLen == 0 || lpszSrcData == NULL) { + return; + } + if (m_pData == NULL) { + m_pData = FX_AllocString(nSrcLen); + if (!m_pData) { + return; + } + FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(char)); + return; + } + if (m_pData->m_nRefs > 1 || m_pData->m_nDataLength + nSrcLen > m_pData->m_nAllocLength) { + CFX_StringData* pOldData = m_pData; + ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcData); + FX_ReleaseString(pOldData); + } else { + FXSYS_memcpy32(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData, nSrcLen * sizeof(char)); + m_pData->m_nDataLength += nSrcLen; + m_pData->m_String[m_pData->m_nDataLength] = 0; + } +} +void CFX_ByteString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCSTR lpszSrc1Data, + FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data) +{ + int nNewLen = nSrc1Len + nSrc2Len; + if (nNewLen == 0) { + return; + } + m_pData = FX_AllocString(nNewLen); + if (m_pData) { + FXSYS_memcpy32(m_pData->m_String, lpszSrc1Data, nSrc1Len * sizeof(char)); + FXSYS_memcpy32(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len * sizeof(char)); + } +} +CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const +{ + if (m_pData == NULL) { + return CFX_ByteString(); + } + return Mid(nFirst, m_pData->m_nDataLength - nFirst); +} +CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const +{ + if (nFirst < 0) { + nFirst = 0; + } + if (nCount < 0) { + nCount = 0; + } + if (nFirst + nCount > m_pData->m_nDataLength) { + nCount = m_pData->m_nDataLength - nFirst; + } + if (nFirst > m_pData->m_nDataLength) { + nCount = 0; + } + if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) { + return *this; + } + CFX_ByteString dest; + AllocCopy(dest, nCount, nFirst, 0); + return dest; +} +void CFX_ByteString::AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex, + FX_STRSIZE nExtraLen) const +{ + FX_STRSIZE nNewLen = nCopyLen + nExtraLen; + if (nNewLen == 0) { + return; + } + ASSERT(dest.m_pData == NULL); + dest.m_pData = FX_AllocString(nNewLen); + if (dest.m_pData) { + FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, nCopyLen * sizeof(char)); + } +} +#define FORCE_ANSI 0x10000 +#define FORCE_UNICODE 0x20000 +#define FORCE_INT64 0x40000 +void CFX_ByteString::FormatV(FX_LPCSTR lpszFormat, va_list argList) +{ + va_list argListSave; +#if defined(__ARMCC_VERSION) || (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || _FX_CPU_ == _FX_ARM64_)) || defined(__native_client__) + va_copy(argListSave, argList); +#else + argListSave = argList; +#endif + int nMaxLen = 0; + for (FX_LPCSTR lpsz = lpszFormat; *lpsz != 0; lpsz ++) { + if (*lpsz != '%' || *(lpsz = lpsz + 1) == '%') { + nMaxLen += (FX_STRSIZE)FXSYS_strlen(lpsz); + continue; + } + int nItemLen = 0; + int nWidth = 0; + for (; *lpsz != 0; lpsz ++) { + if (*lpsz == '#') { + nMaxLen += 2; + } else if (*lpsz == '*') { + nWidth = va_arg(argList, int); + } else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0' || + *lpsz == ' ') + ; + else { + break; + } + } + if (nWidth == 0) { + nWidth = FXSYS_atoi(lpsz); + for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz ++) + ; + } + if (nWidth < 0 || nWidth > 128 * 1024) { + lpszFormat = "Bad width"; + nMaxLen = 10; + break; + } + int nPrecision = 0; + if (*lpsz == '.') { + lpsz ++; + if (*lpsz == '*') { + nPrecision = va_arg(argList, int); + lpsz ++; + } else { + nPrecision = FXSYS_atoi(lpsz); + for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz ++) + ; + } + } + if (nPrecision < 0 || nPrecision > 128 * 1024) { + lpszFormat = "Bad precision"; + nMaxLen = 14; + break; + } + int nModifier = 0; + if (FXSYS_strncmp(lpsz, "I64", 3) == 0) { + lpsz += 3; + nModifier = FORCE_INT64; + } else { + switch (*lpsz) { + case 'h': + nModifier = FORCE_ANSI; + lpsz ++; + break; + case 'l': + nModifier = FORCE_UNICODE; + lpsz ++; + break; + case 'F': + case 'N': + case 'L': + lpsz ++; + break; + } + } + switch (*lpsz | nModifier) { + case 'c': + case 'C': + nItemLen = 2; + va_arg(argList, int); + break; + case 'c'|FORCE_ANSI: + case 'C'|FORCE_ANSI: + nItemLen = 2; + va_arg(argList, int); + break; + case 'c'|FORCE_UNICODE: + case 'C'|FORCE_UNICODE: + nItemLen = 2; + va_arg(argList, int); + break; + case 's': { + FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR); + if (pstrNextArg == NULL) { + nItemLen = 6; + } else { + nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } + } + break; + case 'S': { + FX_LPWSTR pstrNextArg = va_arg(argList, FX_LPWSTR); + if (pstrNextArg == NULL) { + nItemLen = 6; + } else { + nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } + } + break; + case 's'|FORCE_ANSI: + case 'S'|FORCE_ANSI: { + FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR); + if (pstrNextArg == NULL) { + nItemLen = 6; + } else { + nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } + } + break; + case 's'|FORCE_UNICODE: + case 'S'|FORCE_UNICODE: { + FX_LPWSTR pstrNextArg = va_arg(argList, FX_LPWSTR); + if (pstrNextArg == NULL) { + nItemLen = 6; + } else { + nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } + } + break; + } + if (nItemLen != 0) { + if (nPrecision != 0 && nItemLen > nPrecision) { + nItemLen = nPrecision; + } + if (nItemLen < nWidth) { + nItemLen = nWidth; + } + } else { + switch (*lpsz) { + case 'd': + case 'i': + case 'u': + case 'x': + case 'X': + case 'o': + if (nModifier & FORCE_INT64) { + va_arg(argList, FX_INT64); + } else { + va_arg(argList, int); + } + nItemLen = 32; + if (nItemLen < nWidth + nPrecision) { + nItemLen = nWidth + nPrecision; + } + break; + case 'a': + case 'A': + case 'e': + case 'E': + case 'g': + case 'G': + va_arg(argList, double); + nItemLen = 128; + if (nItemLen < nWidth + nPrecision) { + nItemLen = nWidth + nPrecision; + } + break; + case 'f': + if (nWidth + nPrecision > 100) { + nItemLen = nPrecision + nWidth + 128; + } else { + double f; + char pszTemp[256]; + f = va_arg(argList, double); + FXSYS_sprintf(pszTemp, "%*.*f", nWidth, nPrecision + 6, f ); + nItemLen = (FX_STRSIZE)FXSYS_strlen(pszTemp); + } + break; + case 'p': + va_arg(argList, void*); + nItemLen = 32; + if (nItemLen < nWidth + nPrecision) { + nItemLen = nWidth + nPrecision; + } + break; + case 'n': + va_arg(argList, int*); + break; + } + } + nMaxLen += nItemLen; + } + GetBuffer(nMaxLen); + if (m_pData) { + FXSYS_vsprintf(m_pData->m_String, lpszFormat, argListSave); + ReleaseBuffer(); + } + va_end(argListSave); +} +void CFX_ByteString::Format(FX_LPCSTR lpszFormat, ...) +{ + va_list argList; + va_start(argList, lpszFormat); + FormatV(lpszFormat, argList); + va_end(argList); +} +FX_STRSIZE CFX_ByteString::Insert(FX_STRSIZE nIndex, FX_CHAR ch) +{ + CopyBeforeWrite(); + if (nIndex < 0) { + nIndex = 0; + } + FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0; + if (nIndex > nNewLength) { + nIndex = nNewLength; + } + nNewLength++; + if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) { + CFX_StringData* pOldData = m_pData; + FX_LPCSTR pstr = m_pData->m_String; + m_pData = FX_AllocString(nNewLength); + if (!m_pData) { + return 0; + } + if(pOldData != NULL) { + FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1)*sizeof(char)); + FX_ReleaseString(pOldData); + } else { + m_pData->m_String[0] = 0; + } + } + FXSYS_memmove32(m_pData->m_String + nIndex + 1, + m_pData->m_String + nIndex, (nNewLength - nIndex)*sizeof(char)); + m_pData->m_String[nIndex] = ch; + m_pData->m_nDataLength = nNewLength; + return nNewLength; +} +CFX_ByteString CFX_ByteString::Right(FX_STRSIZE nCount) const +{ + if (m_pData == NULL) { + return CFX_ByteString(); + } + if (nCount < 0) { + nCount = 0; + } + if (nCount >= m_pData->m_nDataLength) { + return *this; + } + CFX_ByteString dest; + AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount, 0); + return dest; +} +CFX_ByteString CFX_ByteString::Left(FX_STRSIZE nCount) const +{ + if (m_pData == NULL) { + return CFX_ByteString(); + } + if (nCount < 0) { + nCount = 0; + } + if (nCount >= m_pData->m_nDataLength) { + return *this; + } + CFX_ByteString dest; + AllocCopy(dest, nCount, 0, 0); + return dest; +} +FX_STRSIZE CFX_ByteString::Find(FX_CHAR ch, FX_STRSIZE nStart) const +{ + if (m_pData == NULL) { + return -1; + } + FX_STRSIZE nLength = m_pData->m_nDataLength; + if (nStart >= nLength) { + return -1; + } + FX_LPCSTR lpsz = FXSYS_strchr(m_pData->m_String + nStart, ch); + return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String); +} +FX_STRSIZE CFX_ByteString::ReverseFind(FX_CHAR ch) const +{ + if (m_pData == NULL) { + return -1; + } + FX_STRSIZE nLength = m_pData->m_nDataLength; + while (nLength) { + if (m_pData->m_String[nLength - 1] == ch) { + return nLength - 1; + } + nLength --; + } + return -1; +} +FX_LPCSTR FX_strstr(FX_LPCSTR str1, int len1, FX_LPCSTR str2, int len2) +{ + if (len2 > len1 || len2 == 0) { + return NULL; + } + FX_LPCSTR end_ptr = str1 + len1 - len2; + while (str1 <= end_ptr) { + int i = 0; + while (1) { + if (str1[i] != str2[i]) { + break; + } + i ++; + if (i == len2) { + return str1; + } + } + str1 ++; + } + return NULL; +} +FX_STRSIZE CFX_ByteString::Find(FX_BSTR lpszSub, FX_STRSIZE nStart) const +{ + if (m_pData == NULL) { + return -1; + } + FX_STRSIZE nLength = m_pData->m_nDataLength; + if (nStart > nLength) { + return -1; + } + FX_LPCSTR lpsz = FX_strstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart, + lpszSub.GetCStr(), lpszSub.GetLength()); + return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String); +} +void CFX_ByteString::MakeLower() +{ + if (m_pData == NULL) { + return; + } + CopyBeforeWrite(); + if (GetLength() < 1) { + return; + } + FXSYS_strlwr(m_pData->m_String); +} +void CFX_ByteString::MakeUpper() +{ + if (m_pData == NULL) { + return; + } + CopyBeforeWrite(); + if (GetLength() < 1) { + return; + } + FXSYS_strupr(m_pData->m_String); +} +FX_STRSIZE CFX_ByteString::Remove(FX_CHAR chRemove) +{ + if (m_pData == NULL) { + return 0; + } + CopyBeforeWrite(); + if (GetLength() < 1) { + return 0; + } + FX_LPSTR pstrSource = m_pData->m_String; + FX_LPSTR pstrDest = m_pData->m_String; + FX_LPSTR pstrEnd = m_pData->m_String + m_pData->m_nDataLength; + while (pstrSource < pstrEnd) { + if (*pstrSource != chRemove) { + *pstrDest = *pstrSource; + pstrDest ++; + } + pstrSource ++; + } + *pstrDest = 0; + FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest); + m_pData->m_nDataLength -= nCount; + return nCount; +} +FX_STRSIZE CFX_ByteString::Replace(FX_BSTR lpszOld, FX_BSTR lpszNew) +{ + if (m_pData == NULL) { + return 0; + } + if (lpszOld.IsEmpty()) { + return 0; + } + FX_STRSIZE nSourceLen = lpszOld.GetLength(); + FX_STRSIZE nReplacementLen = lpszNew.GetLength(); + FX_STRSIZE nCount = 0; + FX_LPCSTR pStart = m_pData->m_String; + FX_LPSTR pEnd = m_pData->m_String + m_pData->m_nDataLength; + while (1) { + FX_LPCSTR pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), lpszOld.GetCStr(), nSourceLen); + if (pTarget == NULL) { + break; + } + nCount++; + pStart = pTarget + nSourceLen; + } + if (nCount == 0) { + return 0; + } + FX_STRSIZE nNewLength = m_pData->m_nDataLength + (nReplacementLen - nSourceLen) * nCount; + if (nNewLength == 0) { + Empty(); + return nCount; + } + CFX_StringData* pNewData = FX_AllocString(nNewLength); + if (!pNewData) { + return 0; + } + pStart = m_pData->m_String; + FX_LPSTR pDest = pNewData->m_String; + for (FX_STRSIZE i = 0; i < nCount; i ++) { + FX_LPCSTR pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), lpszOld.GetCStr(), nSourceLen); + FXSYS_memcpy32(pDest, pStart, pTarget - pStart); + pDest += pTarget - pStart; + FXSYS_memcpy32(pDest, lpszNew.GetCStr(), lpszNew.GetLength()); + pDest += lpszNew.GetLength(); + pStart = pTarget + nSourceLen; + } + FXSYS_memcpy32(pDest, pStart, pEnd - pStart); + FX_ReleaseString(m_pData); + m_pData = pNewData; + return nCount; +} +void CFX_ByteString::SetAt(FX_STRSIZE nIndex, FX_CHAR ch) +{ + if (m_pData == NULL) { + return; + } + FXSYS_assert(nIndex >= 0); + FXSYS_assert(nIndex < m_pData->m_nDataLength); + CopyBeforeWrite(); + m_pData->m_String[nIndex] = ch; +} +CFX_ByteString CFX_ByteString::LoadFromFile(FX_BSTR filename) +{ + FXSYS_FILE* file = FXSYS_fopen(CFX_ByteString(filename), "rb"); + if (file == NULL) { + return CFX_ByteString(); + } + FXSYS_fseek(file, 0, FXSYS_SEEK_END); + int len = FXSYS_ftell(file); + FXSYS_fseek(file, 0, FXSYS_SEEK_SET); + CFX_ByteString str; + FX_LPSTR buf = str.GetBuffer(len); + size_t readCnt = FXSYS_fread(buf, 1, len, file); + str.ReleaseBuffer(len); + FXSYS_fclose(file); + return str; +} +CFX_WideString CFX_ByteString::UTF8Decode() const +{ + CFX_UTF8Decoder decoder; + for (FX_STRSIZE i = 0; i < GetLength(); i ++) { + decoder.Input((FX_BYTE)m_pData->m_String[i]); + } + return decoder.GetResult(); +} +CFX_ByteString CFX_ByteString::FromUnicode(FX_LPCWSTR str, FX_STRSIZE len) +{ + if (len < 0) { + len = (FX_STRSIZE)FXSYS_wcslen(str); + } + CFX_ByteString bstr; + bstr.ConvertFrom(CFX_WideString(str, len)); + return bstr; +} +CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str) +{ + return FromUnicode((FX_LPCWSTR)str, str.GetLength()); +} +void CFX_ByteString::ConvertFrom(const CFX_WideString& str, CFX_CharMap* pCharMap) +{ + if (pCharMap == NULL) { + pCharMap = CFX_CharMap::GetDefaultMapper(); + } + *this = (*pCharMap->m_GetByteString)(pCharMap, str); +} +int CFX_ByteString::Compare(FX_BSTR str) const +{ + if (m_pData == NULL) { + return str.IsEmpty() ? 0 : -1; + } + int this_len = m_pData->m_nDataLength; + int that_len = str.GetLength(); + int min_len = this_len < that_len ? this_len : that_len; + for (int i = 0; i < min_len; i ++) { + if ((FX_BYTE)m_pData->m_String[i] < str.GetAt(i)) { + return -1; + } else if ((FX_BYTE)m_pData->m_String[i] > str.GetAt(i)) { + return 1; + } + } + if (this_len < that_len) { + return -1; + } else if (this_len > that_len) { + return 1; + } + return 0; +} +void CFX_ByteString::TrimRight(FX_BSTR lpszTargets) +{ + if (m_pData == NULL || lpszTargets.IsEmpty()) { + return; + } + CopyBeforeWrite(); + FX_STRSIZE pos = GetLength(); + if (pos < 1) { + return; + } + while (pos) { + FX_STRSIZE i = 0; + while (i < lpszTargets.GetLength() && lpszTargets[i] != m_pData->m_String[pos - 1]) { + i ++; + } + if (i == lpszTargets.GetLength()) { + break; + } + pos --; + } + if (pos < m_pData->m_nDataLength) { + m_pData->m_String[pos] = 0; + m_pData->m_nDataLength = pos; + } +} +void CFX_ByteString::TrimRight(FX_CHAR chTarget) +{ + TrimRight(CFX_ByteStringC(chTarget)); +} +void CFX_ByteString::TrimRight() +{ + TrimRight(FX_BSTRC("\x09\x0a\x0b\x0c\x0d\x20")); +} +void CFX_ByteString::TrimLeft(FX_BSTR lpszTargets) +{ + if (m_pData == NULL) { + return; + } + if (lpszTargets.IsEmpty()) { + return; + } + CopyBeforeWrite(); + FX_STRSIZE len = GetLength(); + if (len < 1) { + return; + } + FX_STRSIZE pos = 0; + while (pos < len) { + FX_STRSIZE i = 0; + while (i < lpszTargets.GetLength() && lpszTargets[i] != m_pData->m_String[pos]) { + i ++; + } + if (i == lpszTargets.GetLength()) { + break; + } + pos ++; + } + if (pos) { + FX_STRSIZE nDataLength = len - pos; + FXSYS_memmove32(m_pData->m_String, m_pData->m_String + pos, (nDataLength + 1)*sizeof(FX_CHAR)); + m_pData->m_nDataLength = nDataLength; + } +} +void CFX_ByteString::TrimLeft(FX_CHAR chTarget) +{ + TrimLeft(CFX_ByteStringC(chTarget)); +} +void CFX_ByteString::TrimLeft() +{ + TrimLeft(FX_BSTRC("\x09\x0a\x0b\x0c\x0d\x20")); +} +FX_DWORD CFX_ByteString::GetID(FX_STRSIZE start_pos) const +{ + return CFX_ByteStringC(*this).GetID(start_pos); +} +FX_DWORD CFX_ByteStringC::GetID(FX_STRSIZE start_pos) const +{ + if (m_Length == 0) { + return 0; + } + if (start_pos >= m_Length) { + return 0; + } + FX_DWORD strid = 0; + if (start_pos + 4 > m_Length) { + for (FX_STRSIZE i = 0; i < m_Length - start_pos; i ++) { + strid = strid * 256 + m_Ptr[start_pos + i]; + } + strid = strid << ((4 - m_Length + start_pos) * 8); + } else { + for (int i = 0; i < 4; i ++) { + strid = strid * 256 + m_Ptr[start_pos + i]; + } + } + return strid; +} +FX_STRSIZE FX_ftoa(FX_FLOAT d, FX_LPSTR buf) +{ + buf[0] = '0'; + buf[1] = '\0'; + if (d == 0.0f) { + return 1; + } + FX_BOOL bNegative = FALSE; + if (d < 0) { + bNegative = TRUE; + d = -d; + } + int scale = 1; + int scaled = FXSYS_round(d); + while (scaled < 100000) { + if (scale == 1000000) { + break; + } + scale *= 10; + scaled = FXSYS_round(d * scale); + } + if (scaled == 0) { + return 1; + } + char buf2[32]; + int buf_size = 0; + if (bNegative) { + buf[buf_size++] = '-'; + } + int i = scaled / scale; + FXSYS_itoa(i, buf2, 10); + FX_STRSIZE len = (FX_STRSIZE)FXSYS_strlen(buf2); + FXSYS_memcpy32(buf + buf_size, buf2, len); + buf_size += len; + int fraction = scaled % scale; + if (fraction == 0) { + return buf_size; + } + buf[buf_size++] = '.'; + scale /= 10; + while (fraction) { + buf[buf_size++] = '0' + fraction / scale; + fraction %= scale; + scale /= 10; + } + return buf_size; +} +CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) +{ + FX_CHAR buf[32]; + FX_STRSIZE len = FX_ftoa(d, buf); + return CFX_ByteString(buf, len); +} +void CFX_StringBufBase::Copy(FX_BSTR str) +{ + m_Size = str.GetLength(); + if (m_Size > m_Limit) { + m_Size = m_Limit; + } + FX_CHAR* pBuffer = (FX_CHAR*)(this + 1); + FXSYS_memcpy32(pBuffer, str.GetPtr(), m_Size); +} +void CFX_StringBufBase::Append(FX_BSTR str) +{ + int len = str.GetLength(); + if (len > m_Limit - m_Size) { + len = m_Limit - m_Size; + } + FX_CHAR* pBuffer = (FX_CHAR*)(this + 1); + FXSYS_memcpy32(pBuffer + m_Size, str.GetPtr(), len); + m_Size += len; +} +void CFX_StringBufBase::Append(int i, FX_DWORD flags) +{ + char buf[32]; + int len = _Buffer_itoa(buf, i, flags); + Append(CFX_ByteStringC(buf, len)); +} +void CFX_ByteStringL::Empty(IFX_Allocator* pAllocator) +{ + if (m_Ptr) { + FX_Allocator_Free(pAllocator, (FX_LPVOID)m_Ptr); + } + m_Ptr = NULL, m_Length = 0; +} +FX_LPSTR CFX_ByteStringL::AllocBuffer(FX_STRSIZE length, IFX_Allocator* pAllocator) +{ + Empty(pAllocator); + FX_LPSTR str = FX_Allocator_Alloc(pAllocator, FX_CHAR, length + 1); + if (!str) { + return NULL; + } + *(FX_LPSTR*)(&m_Ptr) = str; + m_Length = length; + return str; +} +void CFX_ByteStringL::Set(FX_BSTR src, IFX_Allocator* pAllocator) +{ + Empty(pAllocator); + if (src.GetCStr() != NULL && src.GetLength() > 0) { + FX_LPSTR str = FX_Allocator_Alloc(pAllocator, FX_CHAR, src.GetLength() + 1); + if (!str) { + return; + } + FXSYS_memcpy32(str, src, src.GetLength()); + str[src.GetLength()] = '\0'; + *(FX_LPSTR*)(&m_Ptr) = str; + m_Length = src.GetLength(); + } +} diff --git a/core/src/fxcrt/fx_basic_buffer.cpp b/core/src/fxcrt/fx_basic_buffer.cpp index ef3cd21350..4427857b27 100644 --- a/core/src/fxcrt/fx_basic_buffer.cpp +++ b/core/src/fxcrt/fx_basic_buffer.cpp @@ -1,574 +1,574 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_basic.h"
-FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf);
-CFX_BinaryBuf::CFX_BinaryBuf(IFX_Allocator* pAllocator)
- : m_pAllocator(pAllocator)
- , m_AllocStep(0)
- , m_pBuffer(NULL)
- , m_DataSize(0)
- , m_AllocSize(0)
-{
-}
-CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size, IFX_Allocator* pAllocator)
- : m_pAllocator(pAllocator)
- , m_AllocStep(0)
- , m_DataSize(size)
- , m_AllocSize(size)
-{
- m_pBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, size);
-}
-CFX_BinaryBuf::~CFX_BinaryBuf()
-{
- if (m_pBuffer) {
- FX_Allocator_Free(m_pAllocator, m_pBuffer);
- }
-}
-void CFX_BinaryBuf::Delete(int start_index, int count)
-{
- if (!m_pBuffer || start_index < 0 || start_index + count > m_DataSize) {
- return;
- }
- FXSYS_memmove32(m_pBuffer + start_index, m_pBuffer + start_index + count, m_DataSize - start_index - count);
- m_DataSize -= count;
-}
-void CFX_BinaryBuf::Clear()
-{
- m_DataSize = 0;
-}
-void CFX_BinaryBuf::DetachBuffer()
-{
- m_DataSize = 0;
- m_pBuffer = NULL;
- m_AllocSize = 0;
-}
-void CFX_BinaryBuf::AttachData(void* buffer, FX_STRSIZE size)
-{
- if (m_pBuffer) {
- FX_Allocator_Free(m_pAllocator, m_pBuffer);
- }
- m_DataSize = size;
- m_pBuffer = (FX_LPBYTE)buffer;
- m_AllocSize = size;
-}
-void CFX_BinaryBuf::TakeOver(CFX_BinaryBuf& other)
-{
- AttachData(other.GetBuffer(), other.GetSize());
- other.DetachBuffer();
-}
-void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step)
-{
- m_AllocStep = step;
- if (m_AllocSize >= size) {
- return;
- }
- ExpandBuf(size - m_DataSize);
-}
-void CFX_BinaryBuf::ExpandBuf(FX_STRSIZE add_size)
-{
- FX_STRSIZE new_size = add_size + m_DataSize;
- if (m_AllocSize >= new_size) {
- return;
- }
- int alloc_step;
- if (m_AllocStep == 0) {
- alloc_step = m_AllocSize / 4;
- if (alloc_step < 128 ) {
- alloc_step = 128;
- }
- } else {
- alloc_step = m_AllocStep;
- }
- new_size = (new_size + alloc_step - 1) / alloc_step * alloc_step;
- FX_LPBYTE pNewBuffer = m_pBuffer;
- if (pNewBuffer) {
- pNewBuffer = FX_Allocator_Realloc(m_pAllocator, FX_BYTE, m_pBuffer, new_size);
- } else {
- pNewBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, new_size);
- }
- if (pNewBuffer) {
- m_pBuffer = pNewBuffer;
- m_AllocSize = new_size;
- }
-}
-void CFX_BinaryBuf::CopyData(const void* pStr, FX_STRSIZE size)
-{
- if (size == 0) {
- m_DataSize = 0;
- return;
- }
- if (m_AllocSize < size) {
- ExpandBuf(size - m_DataSize);
- }
- if (!m_pBuffer) {
- return;
- }
- FXSYS_memcpy32(m_pBuffer, pStr, size);
- m_DataSize = size;
-}
-void CFX_BinaryBuf::AppendBlock(const void* pBuf, FX_STRSIZE size)
-{
- ExpandBuf(size);
- if (pBuf && m_pBuffer) {
- FXSYS_memcpy32(m_pBuffer + m_DataSize, pBuf, size);
- }
- m_DataSize += size;
-}
-void CFX_BinaryBuf::InsertBlock(FX_STRSIZE pos, const void* pBuf, FX_STRSIZE size)
-{
- ExpandBuf(size);
- if (!m_pBuffer) {
- return;
- }
- FXSYS_memmove32(m_pBuffer + pos + size, m_pBuffer + pos, m_DataSize - pos);
- if (pBuf) {
- FXSYS_memcpy32(m_pBuffer + pos, pBuf, size);
- }
- m_DataSize += size;
-}
-void CFX_BinaryBuf::AppendFill(FX_BYTE byte, FX_STRSIZE count)
-{
- ExpandBuf(count);
- if (!m_pBuffer) {
- return;
- }
- FXSYS_memset8(m_pBuffer + m_DataSize, byte, count);
- m_DataSize += count;
-}
-CFX_ByteStringC CFX_BinaryBuf::GetByteString() const
-{
- return CFX_ByteStringC(m_pBuffer, m_DataSize);
-}
-void CFX_BinaryBuf::GetByteStringL(CFX_ByteStringL &str) const
-{
- str.Set(CFX_ByteStringC(m_pBuffer, m_DataSize), m_pAllocator);
-}
-CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (FX_BSTR lpsz)
-{
- AppendBlock((FX_LPCBYTE)lpsz, lpsz.GetLength());
- return *this;
-}
-CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (int i)
-{
- char buf[32];
- FXSYS_itoa(i, buf, 10);
- AppendBlock(buf, (FX_STRSIZE)FXSYS_strlen(buf));
- return *this;
-}
-CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (FX_DWORD i)
-{
- char buf[32];
- FXSYS_itoa(i, buf, 10);
- AppendBlock(buf, (FX_STRSIZE)FXSYS_strlen(buf));
- return *this;
-}
-CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (double f)
-{
- char buf[32];
- FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf);
- AppendBlock(buf, len);
- return *this;
-}
-CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (const CFX_ByteTextBuf& buf)
-{
- AppendBlock(buf.m_pBuffer, buf.m_DataSize);
- return *this;
-}
-void CFX_ByteTextBuf::operator =(const CFX_ByteStringC& str)
-{
- CopyData((FX_LPCBYTE)str, str.GetLength());
-}
-void CFX_WideTextBuf::AppendChar(FX_WCHAR ch)
-{
- if (m_AllocSize < m_DataSize + (FX_STRSIZE)sizeof(FX_WCHAR)) {
- ExpandBuf(sizeof(FX_WCHAR));
- }
- ASSERT(m_pBuffer != NULL);
- *(FX_WCHAR*)(m_pBuffer + m_DataSize) = ch;
- m_DataSize += sizeof(FX_WCHAR);
-}
-CFX_WideTextBuf& CFX_WideTextBuf::operator << (FX_WSTR str)
-{
- AppendBlock(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR));
- return *this;
-}
-CFX_WideTextBuf& CFX_WideTextBuf::operator << (const CFX_WideString &str)
-{
- AppendBlock((FX_LPCWSTR)str, str.GetLength() * sizeof(FX_WCHAR));
- return *this;
-}
-CFX_WideTextBuf& CFX_WideTextBuf::operator << (int i)
-{
- char buf[32];
- FXSYS_itoa(i, buf, 10);
- FX_STRSIZE len = (FX_STRSIZE)FXSYS_strlen(buf);
- if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) {
- ExpandBuf(len * sizeof(FX_WCHAR));
- }
- ASSERT(m_pBuffer != NULL);
- FX_LPWSTR str = (FX_WCHAR*)(m_pBuffer + m_DataSize);
- for (FX_STRSIZE j = 0; j < len; j ++) {
- *str ++ = buf[j];
- }
- m_DataSize += len * sizeof(FX_WCHAR);
- return *this;
-}
-CFX_WideTextBuf& CFX_WideTextBuf::operator << (double f)
-{
- char buf[32];
- FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf);
- if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) {
- ExpandBuf(len * sizeof(FX_WCHAR));
- }
- ASSERT(m_pBuffer != NULL);
- FX_LPWSTR str = (FX_WCHAR*)(m_pBuffer + m_DataSize);
- for (FX_STRSIZE i = 0; i < len; i ++) {
- *str ++ = buf[i];
- }
- m_DataSize += len * sizeof(FX_WCHAR);
- return *this;
-}
-CFX_WideTextBuf& CFX_WideTextBuf::operator << (FX_LPCWSTR lpsz)
-{
- AppendBlock(lpsz, (FX_STRSIZE)FXSYS_wcslen(lpsz)*sizeof(FX_WCHAR));
- return *this;
-}
-CFX_WideTextBuf& CFX_WideTextBuf::operator << (const CFX_WideTextBuf& buf)
-{
- AppendBlock(buf.m_pBuffer, buf.m_DataSize);
- return *this;
-}
-void CFX_WideTextBuf::operator =(FX_WSTR str)
-{
- CopyData(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR));
-}
-CFX_WideStringC CFX_WideTextBuf::GetWideString() const
-{
- return CFX_WideStringC((FX_LPCWSTR)m_pBuffer, m_DataSize / sizeof(FX_WCHAR));
-}
-void CFX_WideTextBuf::GetWideStringL(CFX_WideStringL& wideText) const
-{
- wideText.Set(CFX_WideStringC((FX_LPCWSTR)m_pBuffer, m_DataSize / sizeof(FX_WCHAR)), m_pAllocator);
-}
-CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_BYTE i)
-{
- if (m_pStream) {
- m_pStream->WriteBlock(&i, 1);
- } else {
- m_SavingBuf.AppendByte(i);
- }
- return *this;
-}
-CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (int i)
-{
- if (m_pStream) {
- m_pStream->WriteBlock(&i, sizeof(int));
- } else {
- m_SavingBuf.AppendBlock(&i, sizeof(int));
- }
- return *this;
-}
-CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_DWORD i)
-{
- if (m_pStream) {
- m_pStream->WriteBlock(&i, sizeof(FX_DWORD));
- } else {
- m_SavingBuf.AppendBlock(&i, sizeof(FX_DWORD));
- }
- return *this;
-}
-CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_FLOAT f)
-{
- if (m_pStream) {
- m_pStream->WriteBlock(&f, sizeof(FX_FLOAT));
- } else {
- m_SavingBuf.AppendBlock(&f, sizeof(FX_FLOAT));
- }
- return *this;
-}
-CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_BSTR bstr)
-{
- int len = bstr.GetLength();
- if (m_pStream) {
- m_pStream->WriteBlock(&len, sizeof(int));
- m_pStream->WriteBlock(bstr, len);
- } else {
- m_SavingBuf.AppendBlock(&len, sizeof(int));
- m_SavingBuf.AppendBlock(bstr, len);
- }
- return *this;
-}
-CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_LPCWSTR wstr)
-{
- FX_STRSIZE len = (FX_STRSIZE)FXSYS_wcslen(wstr);
- if (m_pStream) {
- m_pStream->WriteBlock(&len, sizeof(int));
- m_pStream->WriteBlock(wstr, len);
- } else {
- m_SavingBuf.AppendBlock(&len, sizeof(int));
- m_SavingBuf.AppendBlock(wstr, len);
- }
- return *this;
-}
-CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (const CFX_WideString& wstr)
-{
- CFX_ByteString encoded = wstr.UTF16LE_Encode();
- return operator << (encoded);
-}
-void CFX_ArchiveSaver::Write(const void* pData, FX_STRSIZE dwSize)
-{
- if (m_pStream) {
- m_pStream->WriteBlock(pData, dwSize);
- } else {
- m_SavingBuf.AppendBlock(pData, dwSize);
- }
-}
-CFX_ArchiveLoader::CFX_ArchiveLoader(FX_LPCBYTE pData, FX_DWORD dwSize)
-{
- m_pLoadingBuf = pData;
- m_LoadingPos = 0;
- m_LoadingSize = dwSize;
-}
-FX_BOOL CFX_ArchiveLoader::IsEOF()
-{
- return m_LoadingPos >= m_LoadingSize;
-}
-CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (FX_BYTE& i)
-{
- if (m_LoadingPos >= m_LoadingSize) {
- return *this;
- }
- i = m_pLoadingBuf[m_LoadingPos++];
- return *this;
-}
-CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (int& i)
-{
- Read(&i, sizeof(int));
- return *this;
-}
-CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (FX_DWORD& i)
-{
- Read(&i, sizeof(FX_DWORD));
- return *this;
-}
-CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (FX_FLOAT& i)
-{
- Read(&i, sizeof(FX_FLOAT));
- return *this;
-}
-CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (CFX_ByteString& str)
-{
- if (m_LoadingPos + 4 > m_LoadingSize) {
- return *this;
- }
- int len;
- operator >> (len);
- str.Empty();
- if (len <= 0 || m_LoadingPos + len > m_LoadingSize) {
- return *this;
- }
- FX_LPSTR buffer = str.GetBuffer(len);
- FXSYS_memcpy32(buffer, m_pLoadingBuf + m_LoadingPos, len);
- str.ReleaseBuffer(len);
- m_LoadingPos += len;
- return *this;
-}
-CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (CFX_WideString& str)
-{
- CFX_ByteString encoded;
- operator >> (encoded);
- str = CFX_WideString::FromUTF16LE((const unsigned short*)(FX_LPCSTR)encoded, encoded.GetLength());
- return *this;
-}
-FX_BOOL CFX_ArchiveLoader::Read(void* pBuf, FX_DWORD dwSize)
-{
- if (m_LoadingPos + dwSize > m_LoadingSize) {
- return FALSE;
- }
- FXSYS_memcpy32(pBuf, m_pLoadingBuf + m_LoadingPos, dwSize);
- m_LoadingPos += dwSize;
- return TRUE;
-}
-void CFX_BitStream::Init(FX_LPCBYTE pData, FX_DWORD dwSize)
-{
- m_pData = pData;
- m_BitSize = dwSize * 8;
- m_BitPos = 0;
-}
-void CFX_BitStream::ByteAlign()
-{
- int mod = m_BitPos % 8;
- if (mod == 0) {
- return;
- }
- m_BitPos += 8 - mod;
-}
-FX_DWORD CFX_BitStream::GetBits(FX_DWORD nBits)
-{
- if (nBits > m_BitSize || m_BitPos + nBits > m_BitSize) {
- return 0;
- }
- if (nBits == 1) {
- int bit = (m_pData[m_BitPos / 8] & (1 << (7 - m_BitPos % 8))) ? 1 : 0;
- m_BitPos ++;
- return bit;
- }
- FX_DWORD byte_pos = m_BitPos / 8;
- FX_DWORD bit_pos = m_BitPos % 8, bit_left = nBits;
- FX_DWORD result = 0;
- if (bit_pos) {
- if (8 - bit_pos >= bit_left) {
- result = (m_pData[byte_pos] & (0xff >> bit_pos)) >> (8 - bit_pos - bit_left);
- m_BitPos += bit_left;
- return result;
- }
- bit_left -= 8 - bit_pos;
- result = (m_pData[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left;
- }
- while (bit_left >= 8) {
- bit_left -= 8;
- result |= m_pData[byte_pos++] << bit_left;
- }
- if (bit_left) {
- result |= m_pData[byte_pos] >> (8 - bit_left);
- }
- m_BitPos += nBits;
- return result;
-}
-IFX_BufferArchive::IFX_BufferArchive(FX_STRSIZE size, IFX_Allocator* pAllocator)
- : m_pAllocator(pAllocator)
- , m_BufSize(size)
- , m_pBuffer(NULL)
- , m_Length(0)
-{
-}
-void IFX_BufferArchive::Clear()
-{
- m_Length = 0;
- if (m_pBuffer) {
- FX_Allocator_Free(m_pAllocator, m_pBuffer);
- m_pBuffer = NULL;
- }
-}
-FX_BOOL IFX_BufferArchive::Flush()
-{
- FX_BOOL bRet = DoWork(m_pBuffer, m_Length);
- m_Length = 0;
- return bRet;
-}
-FX_INT32 IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size)
-{
- if (!pBuf || size < 1) {
- return 0;
- }
- if (!m_pBuffer) {
- m_pBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, m_BufSize);
- if (!m_pBuffer) {
- return -1;
- }
- }
- FX_LPBYTE buffer = (FX_LPBYTE)pBuf;
- FX_STRSIZE temp_size = (FX_STRSIZE)size;
- while (temp_size > 0) {
- FX_STRSIZE buf_size = FX_MIN(m_BufSize - m_Length, (FX_STRSIZE)temp_size);
- FXSYS_memcpy32(m_pBuffer + m_Length, buffer, buf_size);
- m_Length += buf_size;
- if (m_Length == m_BufSize) {
- if (!Flush()) {
- return -1;
- }
- }
- temp_size -= buf_size;
- buffer += buf_size;
- }
- return (FX_INT32)size;
-}
-FX_INT32 IFX_BufferArchive::AppendByte(FX_BYTE byte)
-{
- return AppendBlock(&byte, 1);
-}
-FX_INT32 IFX_BufferArchive::AppendDWord(FX_DWORD i)
-{
- char buf[32];
- FXSYS_itoa(i, buf, 10);
- return AppendBlock(buf, (size_t)FXSYS_strlen(buf));
-}
-FX_INT32 IFX_BufferArchive::AppendString(FX_BSTR lpsz)
-{
- return AppendBlock((FX_LPCBYTE)lpsz, lpsz.GetLength());
-}
-CFX_FileBufferArchive::CFX_FileBufferArchive(FX_STRSIZE size, IFX_Allocator* pAllocator)
- : IFX_BufferArchive(size, pAllocator)
- , m_pFile(NULL)
- , m_bTakeover(FALSE)
-{
-}
-CFX_FileBufferArchive::~CFX_FileBufferArchive()
-{
- Clear();
-}
-void CFX_FileBufferArchive::Clear()
-{
- if (m_pFile && m_bTakeover) {
- m_pFile->Release();
- }
- m_pFile = NULL;
- m_bTakeover = FALSE;
- IFX_BufferArchive::Clear();
-}
-FX_BOOL CFX_FileBufferArchive::AttachFile(IFX_StreamWrite *pFile, FX_BOOL bTakeover )
-{
- if (!pFile) {
- return FALSE;
- }
- if (m_pFile && m_bTakeover) {
- m_pFile->Release();
- }
- m_pFile = pFile;
- m_bTakeover = bTakeover;
- return TRUE;
-}
-FX_BOOL CFX_FileBufferArchive::AttachFile(FX_LPCWSTR filename)
-{
- if (!filename) {
- return FALSE;
- }
- if (m_pFile && m_bTakeover) {
- m_pFile->Release();
- }
- m_pFile = FX_CreateFileWrite(filename);
- if (!m_pFile) {
- return FALSE;
- }
- m_bTakeover = TRUE;
- return TRUE;
-}
-FX_BOOL CFX_FileBufferArchive::AttachFile(FX_LPCSTR filename)
-{
- if (!filename) {
- return FALSE;
- }
- if (m_pFile && m_bTakeover) {
- m_pFile->Release();
- }
- m_pFile = FX_CreateFileWrite(filename);
- if (!m_pFile) {
- return FALSE;
- }
- m_bTakeover = TRUE;
- return TRUE;
-}
-FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size)
-{
- if (!m_pFile) {
- return FALSE;
- }
- if (!pBuf || size < 1) {
- return TRUE;
- }
- return m_pFile->WriteBlock(pBuf, size);
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_basic.h" +FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf); +CFX_BinaryBuf::CFX_BinaryBuf(IFX_Allocator* pAllocator) + : m_pAllocator(pAllocator) + , m_AllocStep(0) + , m_pBuffer(NULL) + , m_DataSize(0) + , m_AllocSize(0) +{ +} +CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size, IFX_Allocator* pAllocator) + : m_pAllocator(pAllocator) + , m_AllocStep(0) + , m_DataSize(size) + , m_AllocSize(size) +{ + m_pBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, size); +} +CFX_BinaryBuf::~CFX_BinaryBuf() +{ + if (m_pBuffer) { + FX_Allocator_Free(m_pAllocator, m_pBuffer); + } +} +void CFX_BinaryBuf::Delete(int start_index, int count) +{ + if (!m_pBuffer || start_index < 0 || start_index + count > m_DataSize) { + return; + } + FXSYS_memmove32(m_pBuffer + start_index, m_pBuffer + start_index + count, m_DataSize - start_index - count); + m_DataSize -= count; +} +void CFX_BinaryBuf::Clear() +{ + m_DataSize = 0; +} +void CFX_BinaryBuf::DetachBuffer() +{ + m_DataSize = 0; + m_pBuffer = NULL; + m_AllocSize = 0; +} +void CFX_BinaryBuf::AttachData(void* buffer, FX_STRSIZE size) +{ + if (m_pBuffer) { + FX_Allocator_Free(m_pAllocator, m_pBuffer); + } + m_DataSize = size; + m_pBuffer = (FX_LPBYTE)buffer; + m_AllocSize = size; +} +void CFX_BinaryBuf::TakeOver(CFX_BinaryBuf& other) +{ + AttachData(other.GetBuffer(), other.GetSize()); + other.DetachBuffer(); +} +void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) +{ + m_AllocStep = step; + if (m_AllocSize >= size) { + return; + } + ExpandBuf(size - m_DataSize); +} +void CFX_BinaryBuf::ExpandBuf(FX_STRSIZE add_size) +{ + FX_STRSIZE new_size = add_size + m_DataSize; + if (m_AllocSize >= new_size) { + return; + } + int alloc_step; + if (m_AllocStep == 0) { + alloc_step = m_AllocSize / 4; + if (alloc_step < 128 ) { + alloc_step = 128; + } + } else { + alloc_step = m_AllocStep; + } + new_size = (new_size + alloc_step - 1) / alloc_step * alloc_step; + FX_LPBYTE pNewBuffer = m_pBuffer; + if (pNewBuffer) { + pNewBuffer = FX_Allocator_Realloc(m_pAllocator, FX_BYTE, m_pBuffer, new_size); + } else { + pNewBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, new_size); + } + if (pNewBuffer) { + m_pBuffer = pNewBuffer; + m_AllocSize = new_size; + } +} +void CFX_BinaryBuf::CopyData(const void* pStr, FX_STRSIZE size) +{ + if (size == 0) { + m_DataSize = 0; + return; + } + if (m_AllocSize < size) { + ExpandBuf(size - m_DataSize); + } + if (!m_pBuffer) { + return; + } + FXSYS_memcpy32(m_pBuffer, pStr, size); + m_DataSize = size; +} +void CFX_BinaryBuf::AppendBlock(const void* pBuf, FX_STRSIZE size) +{ + ExpandBuf(size); + if (pBuf && m_pBuffer) { + FXSYS_memcpy32(m_pBuffer + m_DataSize, pBuf, size); + } + m_DataSize += size; +} +void CFX_BinaryBuf::InsertBlock(FX_STRSIZE pos, const void* pBuf, FX_STRSIZE size) +{ + ExpandBuf(size); + if (!m_pBuffer) { + return; + } + FXSYS_memmove32(m_pBuffer + pos + size, m_pBuffer + pos, m_DataSize - pos); + if (pBuf) { + FXSYS_memcpy32(m_pBuffer + pos, pBuf, size); + } + m_DataSize += size; +} +void CFX_BinaryBuf::AppendFill(FX_BYTE byte, FX_STRSIZE count) +{ + ExpandBuf(count); + if (!m_pBuffer) { + return; + } + FXSYS_memset8(m_pBuffer + m_DataSize, byte, count); + m_DataSize += count; +} +CFX_ByteStringC CFX_BinaryBuf::GetByteString() const +{ + return CFX_ByteStringC(m_pBuffer, m_DataSize); +} +void CFX_BinaryBuf::GetByteStringL(CFX_ByteStringL &str) const +{ + str.Set(CFX_ByteStringC(m_pBuffer, m_DataSize), m_pAllocator); +} +CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (FX_BSTR lpsz) +{ + AppendBlock((FX_LPCBYTE)lpsz, lpsz.GetLength()); + return *this; +} +CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (int i) +{ + char buf[32]; + FXSYS_itoa(i, buf, 10); + AppendBlock(buf, (FX_STRSIZE)FXSYS_strlen(buf)); + return *this; +} +CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (FX_DWORD i) +{ + char buf[32]; + FXSYS_itoa(i, buf, 10); + AppendBlock(buf, (FX_STRSIZE)FXSYS_strlen(buf)); + return *this; +} +CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (double f) +{ + char buf[32]; + FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); + AppendBlock(buf, len); + return *this; +} +CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (const CFX_ByteTextBuf& buf) +{ + AppendBlock(buf.m_pBuffer, buf.m_DataSize); + return *this; +} +void CFX_ByteTextBuf::operator =(const CFX_ByteStringC& str) +{ + CopyData((FX_LPCBYTE)str, str.GetLength()); +} +void CFX_WideTextBuf::AppendChar(FX_WCHAR ch) +{ + if (m_AllocSize < m_DataSize + (FX_STRSIZE)sizeof(FX_WCHAR)) { + ExpandBuf(sizeof(FX_WCHAR)); + } + ASSERT(m_pBuffer != NULL); + *(FX_WCHAR*)(m_pBuffer + m_DataSize) = ch; + m_DataSize += sizeof(FX_WCHAR); +} +CFX_WideTextBuf& CFX_WideTextBuf::operator << (FX_WSTR str) +{ + AppendBlock(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); + return *this; +} +CFX_WideTextBuf& CFX_WideTextBuf::operator << (const CFX_WideString &str) +{ + AppendBlock((FX_LPCWSTR)str, str.GetLength() * sizeof(FX_WCHAR)); + return *this; +} +CFX_WideTextBuf& CFX_WideTextBuf::operator << (int i) +{ + char buf[32]; + FXSYS_itoa(i, buf, 10); + FX_STRSIZE len = (FX_STRSIZE)FXSYS_strlen(buf); + if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { + ExpandBuf(len * sizeof(FX_WCHAR)); + } + ASSERT(m_pBuffer != NULL); + FX_LPWSTR str = (FX_WCHAR*)(m_pBuffer + m_DataSize); + for (FX_STRSIZE j = 0; j < len; j ++) { + *str ++ = buf[j]; + } + m_DataSize += len * sizeof(FX_WCHAR); + return *this; +} +CFX_WideTextBuf& CFX_WideTextBuf::operator << (double f) +{ + char buf[32]; + FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); + if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { + ExpandBuf(len * sizeof(FX_WCHAR)); + } + ASSERT(m_pBuffer != NULL); + FX_LPWSTR str = (FX_WCHAR*)(m_pBuffer + m_DataSize); + for (FX_STRSIZE i = 0; i < len; i ++) { + *str ++ = buf[i]; + } + m_DataSize += len * sizeof(FX_WCHAR); + return *this; +} +CFX_WideTextBuf& CFX_WideTextBuf::operator << (FX_LPCWSTR lpsz) +{ + AppendBlock(lpsz, (FX_STRSIZE)FXSYS_wcslen(lpsz)*sizeof(FX_WCHAR)); + return *this; +} +CFX_WideTextBuf& CFX_WideTextBuf::operator << (const CFX_WideTextBuf& buf) +{ + AppendBlock(buf.m_pBuffer, buf.m_DataSize); + return *this; +} +void CFX_WideTextBuf::operator =(FX_WSTR str) +{ + CopyData(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); +} +CFX_WideStringC CFX_WideTextBuf::GetWideString() const +{ + return CFX_WideStringC((FX_LPCWSTR)m_pBuffer, m_DataSize / sizeof(FX_WCHAR)); +} +void CFX_WideTextBuf::GetWideStringL(CFX_WideStringL& wideText) const +{ + wideText.Set(CFX_WideStringC((FX_LPCWSTR)m_pBuffer, m_DataSize / sizeof(FX_WCHAR)), m_pAllocator); +} +CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_BYTE i) +{ + if (m_pStream) { + m_pStream->WriteBlock(&i, 1); + } else { + m_SavingBuf.AppendByte(i); + } + return *this; +} +CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (int i) +{ + if (m_pStream) { + m_pStream->WriteBlock(&i, sizeof(int)); + } else { + m_SavingBuf.AppendBlock(&i, sizeof(int)); + } + return *this; +} +CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_DWORD i) +{ + if (m_pStream) { + m_pStream->WriteBlock(&i, sizeof(FX_DWORD)); + } else { + m_SavingBuf.AppendBlock(&i, sizeof(FX_DWORD)); + } + return *this; +} +CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_FLOAT f) +{ + if (m_pStream) { + m_pStream->WriteBlock(&f, sizeof(FX_FLOAT)); + } else { + m_SavingBuf.AppendBlock(&f, sizeof(FX_FLOAT)); + } + return *this; +} +CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_BSTR bstr) +{ + int len = bstr.GetLength(); + if (m_pStream) { + m_pStream->WriteBlock(&len, sizeof(int)); + m_pStream->WriteBlock(bstr, len); + } else { + m_SavingBuf.AppendBlock(&len, sizeof(int)); + m_SavingBuf.AppendBlock(bstr, len); + } + return *this; +} +CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_LPCWSTR wstr) +{ + FX_STRSIZE len = (FX_STRSIZE)FXSYS_wcslen(wstr); + if (m_pStream) { + m_pStream->WriteBlock(&len, sizeof(int)); + m_pStream->WriteBlock(wstr, len); + } else { + m_SavingBuf.AppendBlock(&len, sizeof(int)); + m_SavingBuf.AppendBlock(wstr, len); + } + return *this; +} +CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (const CFX_WideString& wstr) +{ + CFX_ByteString encoded = wstr.UTF16LE_Encode(); + return operator << (encoded); +} +void CFX_ArchiveSaver::Write(const void* pData, FX_STRSIZE dwSize) +{ + if (m_pStream) { + m_pStream->WriteBlock(pData, dwSize); + } else { + m_SavingBuf.AppendBlock(pData, dwSize); + } +} +CFX_ArchiveLoader::CFX_ArchiveLoader(FX_LPCBYTE pData, FX_DWORD dwSize) +{ + m_pLoadingBuf = pData; + m_LoadingPos = 0; + m_LoadingSize = dwSize; +} +FX_BOOL CFX_ArchiveLoader::IsEOF() +{ + return m_LoadingPos >= m_LoadingSize; +} +CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (FX_BYTE& i) +{ + if (m_LoadingPos >= m_LoadingSize) { + return *this; + } + i = m_pLoadingBuf[m_LoadingPos++]; + return *this; +} +CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (int& i) +{ + Read(&i, sizeof(int)); + return *this; +} +CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (FX_DWORD& i) +{ + Read(&i, sizeof(FX_DWORD)); + return *this; +} +CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (FX_FLOAT& i) +{ + Read(&i, sizeof(FX_FLOAT)); + return *this; +} +CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (CFX_ByteString& str) +{ + if (m_LoadingPos + 4 > m_LoadingSize) { + return *this; + } + int len; + operator >> (len); + str.Empty(); + if (len <= 0 || m_LoadingPos + len > m_LoadingSize) { + return *this; + } + FX_LPSTR buffer = str.GetBuffer(len); + FXSYS_memcpy32(buffer, m_pLoadingBuf + m_LoadingPos, len); + str.ReleaseBuffer(len); + m_LoadingPos += len; + return *this; +} +CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (CFX_WideString& str) +{ + CFX_ByteString encoded; + operator >> (encoded); + str = CFX_WideString::FromUTF16LE((const unsigned short*)(FX_LPCSTR)encoded, encoded.GetLength()); + return *this; +} +FX_BOOL CFX_ArchiveLoader::Read(void* pBuf, FX_DWORD dwSize) +{ + if (m_LoadingPos + dwSize > m_LoadingSize) { + return FALSE; + } + FXSYS_memcpy32(pBuf, m_pLoadingBuf + m_LoadingPos, dwSize); + m_LoadingPos += dwSize; + return TRUE; +} +void CFX_BitStream::Init(FX_LPCBYTE pData, FX_DWORD dwSize) +{ + m_pData = pData; + m_BitSize = dwSize * 8; + m_BitPos = 0; +} +void CFX_BitStream::ByteAlign() +{ + int mod = m_BitPos % 8; + if (mod == 0) { + return; + } + m_BitPos += 8 - mod; +} +FX_DWORD CFX_BitStream::GetBits(FX_DWORD nBits) +{ + if (nBits > m_BitSize || m_BitPos + nBits > m_BitSize) { + return 0; + } + if (nBits == 1) { + int bit = (m_pData[m_BitPos / 8] & (1 << (7 - m_BitPos % 8))) ? 1 : 0; + m_BitPos ++; + return bit; + } + FX_DWORD byte_pos = m_BitPos / 8; + FX_DWORD bit_pos = m_BitPos % 8, bit_left = nBits; + FX_DWORD result = 0; + if (bit_pos) { + if (8 - bit_pos >= bit_left) { + result = (m_pData[byte_pos] & (0xff >> bit_pos)) >> (8 - bit_pos - bit_left); + m_BitPos += bit_left; + return result; + } + bit_left -= 8 - bit_pos; + result = (m_pData[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left; + } + while (bit_left >= 8) { + bit_left -= 8; + result |= m_pData[byte_pos++] << bit_left; + } + if (bit_left) { + result |= m_pData[byte_pos] >> (8 - bit_left); + } + m_BitPos += nBits; + return result; +} +IFX_BufferArchive::IFX_BufferArchive(FX_STRSIZE size, IFX_Allocator* pAllocator) + : m_pAllocator(pAllocator) + , m_BufSize(size) + , m_pBuffer(NULL) + , m_Length(0) +{ +} +void IFX_BufferArchive::Clear() +{ + m_Length = 0; + if (m_pBuffer) { + FX_Allocator_Free(m_pAllocator, m_pBuffer); + m_pBuffer = NULL; + } +} +FX_BOOL IFX_BufferArchive::Flush() +{ + FX_BOOL bRet = DoWork(m_pBuffer, m_Length); + m_Length = 0; + return bRet; +} +FX_INT32 IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size) +{ + if (!pBuf || size < 1) { + return 0; + } + if (!m_pBuffer) { + m_pBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, m_BufSize); + if (!m_pBuffer) { + return -1; + } + } + FX_LPBYTE buffer = (FX_LPBYTE)pBuf; + FX_STRSIZE temp_size = (FX_STRSIZE)size; + while (temp_size > 0) { + FX_STRSIZE buf_size = FX_MIN(m_BufSize - m_Length, (FX_STRSIZE)temp_size); + FXSYS_memcpy32(m_pBuffer + m_Length, buffer, buf_size); + m_Length += buf_size; + if (m_Length == m_BufSize) { + if (!Flush()) { + return -1; + } + } + temp_size -= buf_size; + buffer += buf_size; + } + return (FX_INT32)size; +} +FX_INT32 IFX_BufferArchive::AppendByte(FX_BYTE byte) +{ + return AppendBlock(&byte, 1); +} +FX_INT32 IFX_BufferArchive::AppendDWord(FX_DWORD i) +{ + char buf[32]; + FXSYS_itoa(i, buf, 10); + return AppendBlock(buf, (size_t)FXSYS_strlen(buf)); +} +FX_INT32 IFX_BufferArchive::AppendString(FX_BSTR lpsz) +{ + return AppendBlock((FX_LPCBYTE)lpsz, lpsz.GetLength()); +} +CFX_FileBufferArchive::CFX_FileBufferArchive(FX_STRSIZE size, IFX_Allocator* pAllocator) + : IFX_BufferArchive(size, pAllocator) + , m_pFile(NULL) + , m_bTakeover(FALSE) +{ +} +CFX_FileBufferArchive::~CFX_FileBufferArchive() +{ + Clear(); +} +void CFX_FileBufferArchive::Clear() +{ + if (m_pFile && m_bTakeover) { + m_pFile->Release(); + } + m_pFile = NULL; + m_bTakeover = FALSE; + IFX_BufferArchive::Clear(); +} +FX_BOOL CFX_FileBufferArchive::AttachFile(IFX_StreamWrite *pFile, FX_BOOL bTakeover ) +{ + if (!pFile) { + return FALSE; + } + if (m_pFile && m_bTakeover) { + m_pFile->Release(); + } + m_pFile = pFile; + m_bTakeover = bTakeover; + return TRUE; +} +FX_BOOL CFX_FileBufferArchive::AttachFile(FX_LPCWSTR filename) +{ + if (!filename) { + return FALSE; + } + if (m_pFile && m_bTakeover) { + m_pFile->Release(); + } + m_pFile = FX_CreateFileWrite(filename); + if (!m_pFile) { + return FALSE; + } + m_bTakeover = TRUE; + return TRUE; +} +FX_BOOL CFX_FileBufferArchive::AttachFile(FX_LPCSTR filename) +{ + if (!filename) { + return FALSE; + } + if (m_pFile && m_bTakeover) { + m_pFile->Release(); + } + m_pFile = FX_CreateFileWrite(filename); + if (!m_pFile) { + return FALSE; + } + m_bTakeover = TRUE; + return TRUE; +} +FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) +{ + if (!m_pFile) { + return FALSE; + } + if (!pBuf || size < 1) { + return TRUE; + } + return m_pFile->WriteBlock(pBuf, size); +} diff --git a/core/src/fxcrt/fx_basic_coords.cpp b/core/src/fxcrt/fx_basic_coords.cpp index 47204e0aa3..f55c267da5 100644 --- a/core/src/fxcrt/fx_basic_coords.cpp +++ b/core/src/fxcrt/fx_basic_coords.cpp @@ -1,556 +1,556 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_ext.h"
-void FX_RECT::Normalize()
-{
- if (left > right) {
- int temp = left;
- left = right;
- right = temp;
- }
- if (top > bottom) {
- int temp = top;
- top = bottom;
- bottom = temp;
- }
-}
-void FX_RECT::Intersect(const FX_RECT& src)
-{
- FX_RECT src_n = src;
- src_n.Normalize();
- Normalize();
- left = left > src_n.left ? left : src_n.left;
- top = top > src_n.top ? top : src_n.top;
- right = right < src_n.right ? right : src_n.right;
- bottom = bottom < src_n.bottom ? bottom : src_n.bottom;
- if (left > right || top > bottom) {
- left = top = right = bottom = 0;
- }
-}
-void FX_RECT::Union(const FX_RECT& other_rect)
-{
- Normalize();
- FX_RECT other = other_rect;
- other.Normalize();
- left = left < other.left ? left : other.left;
- right = right > other.right ? right : other.right;
- bottom = bottom > other.bottom ? bottom : other.bottom;
- top = top < other.top ? top : other.top;
-}
-FX_BOOL GetIntersection(FX_FLOAT low1, FX_FLOAT high1, FX_FLOAT low2, FX_FLOAT high2,
- FX_FLOAT& interlow, FX_FLOAT& interhigh)
-{
- if (low1 >= high2 || low2 >= high1) {
- return FALSE;
- }
- interlow = low1 > low2 ? low1 : low2;
- interhigh = high1 > high2 ? high2 : high1;
- return TRUE;
-}
-extern "C" int FXSYS_round(FX_FLOAT d)
-{
- int iRet = 0;
- if (d >= 0.0f) {
- iRet = (int)(d + 0.5f);
- if (iRet >= 0) {
- return iRet;
- }
- return -iRet;
- }
- return (int)(d - 0.5f);
-}
-CFX_FloatRect::CFX_FloatRect(const FX_RECT& rect)
-{
- left = (FX_FLOAT)(rect.left);
- right = (FX_FLOAT)(rect.right);
- bottom = (FX_FLOAT)(rect.top);
- top = (FX_FLOAT)(rect.bottom);
-}
-void CFX_FloatRect::Normalize()
-{
- FX_FLOAT temp;
- if (left > right) {
- temp = left;
- left = right;
- right = temp;
- }
- if (bottom > top) {
- temp = top;
- top = bottom;
- bottom = temp;
- }
-}
-void CFX_FloatRect::Intersect(const CFX_FloatRect& other_rect)
-{
- Normalize();
- CFX_FloatRect other = other_rect;
- other.Normalize();
- left = left > other.left ? left : other.left;
- right = right < other.right ? right : other.right;
- bottom = bottom > other.bottom ? bottom : other.bottom;
- top = top < other.top ? top : other.top;
- if (left > right || bottom > top) {
- left = right = bottom = top = 0;
- }
-}
-void CFX_FloatRect::Union(const CFX_FloatRect& other_rect)
-{
- Normalize();
- CFX_FloatRect other = other_rect;
- other.Normalize();
- left = left < other.left ? left : other.left;
- right = right > other.right ? right : other.right;
- bottom = bottom < other.bottom ? bottom : other.bottom;
- top = top > other.top ? top : other.top;
-}
-void CFX_FloatRect::Transform(const CFX_Matrix* pMatrix)
-{
- pMatrix->TransformRect(left, right, top, bottom);
-}
-int CFX_FloatRect::Substract4(CFX_FloatRect& s, CFX_FloatRect* pRects)
-{
- Normalize();
- s.Normalize();
- int nRects = 0;
- CFX_FloatRect rects[4];
- if (left < s.left) {
- rects[nRects].left = left;
- rects[nRects].right = s.left;
- rects[nRects].bottom = bottom;
- rects[nRects].top = top;
- nRects ++;
- }
- if (s.left < right && s.top < top) {
- rects[nRects].left = s.left;
- rects[nRects].right = right;
- rects[nRects].bottom = s.top;
- rects[nRects].top = top;
- nRects ++;
- }
- if (s.top > bottom && s.right < right) {
- rects[nRects].left = s.right;
- rects[nRects].right = right;
- rects[nRects].bottom = bottom;
- rects[nRects].top = s.top;
- nRects ++;
- }
- if (s.bottom > bottom) {
- rects[nRects].left = s.left;
- rects[nRects].right = s.right;
- rects[nRects].bottom = bottom;
- rects[nRects].top = s.bottom;
- nRects ++;
- }
- if (nRects == 0) {
- return 0;
- }
- for (int i = 0; i < nRects; i ++) {
- pRects[i] = rects[i];
- pRects[i].Intersect(*this);
- }
- return nRects;
-}
-FX_RECT CFX_FloatRect::GetOutterRect() const
-{
- CFX_FloatRect rect1 = *this;
- FX_RECT rect;
- rect.left = (int)FXSYS_floor(rect1.left);
- rect.right = (int)FXSYS_ceil(rect1.right);
- rect.top = (int)FXSYS_floor(rect1.bottom);
- rect.bottom = (int)FXSYS_ceil(rect1.top);
- rect.Normalize();
- return rect;
-}
-FX_RECT CFX_FloatRect::GetInnerRect() const
-{
- CFX_FloatRect rect1 = *this;
- FX_RECT rect;
- rect.left = (int)FXSYS_ceil(rect1.left);
- rect.right = (int)FXSYS_floor(rect1.right);
- rect.top = (int)FXSYS_ceil(rect1.bottom);
- rect.bottom = (int)FXSYS_floor(rect1.top);
- rect.Normalize();
- return rect;
-}
-static void _MatchFloatRange(FX_FLOAT f1, FX_FLOAT f2, int& i1, int& i2)
-{
- int length = (int)FXSYS_ceil(f2 - f1);
- int i1_1 = (int)FXSYS_floor(f1);
- int i1_2 = (int)FXSYS_ceil(f1);
- FX_FLOAT error1 = f1 - i1_1 + (FX_FLOAT)FXSYS_fabs(f2 - i1_1 - length);
- FX_FLOAT error2 = i1_2 - f1 + (FX_FLOAT)FXSYS_fabs(f2 - i1_2 - length);
- i1 = (error1 > error2) ? i1_2 : i1_1;
- i2 = i1 + length;
-}
-FX_RECT CFX_FloatRect::GetClosestRect() const
-{
- CFX_FloatRect rect1 = *this;
- FX_RECT rect;
- _MatchFloatRange(rect1.left, rect1.right, rect.left, rect.right);
- _MatchFloatRange(rect1.bottom, rect1.top, rect.top, rect.bottom);
- rect.Normalize();
- return rect;
-}
-FX_BOOL CFX_FloatRect::Contains(const CFX_FloatRect& other_rect) const
-{
- CFX_FloatRect n1 = *this;
- n1.Normalize();
- CFX_FloatRect n2 = other_rect;
- n2.Normalize();
- if (n2.left >= n1.left && n2.right <= n1.right && n2.bottom >= n1.bottom && n2.top <= n1.top) {
- return TRUE;
- }
- return FALSE;
-}
-FX_BOOL CFX_FloatRect::Contains(FX_FLOAT x, FX_FLOAT y) const
-{
- CFX_FloatRect n1 = *this;
- n1.Normalize();
- return x <= n1.right && x >= n1.left && y <= n1.top && y >= n1.bottom;
-}
-void CFX_FloatRect::UpdateRect(FX_FLOAT x, FX_FLOAT y)
-{
- if (left > x) {
- left = x;
- }
- if (right < x) {
- right = x;
- }
- if (bottom > y) {
- bottom = y;
- }
- if (top < y) {
- top = y;
- }
-}
-CFX_FloatRect CFX_FloatRect::GetBBox(const CFX_FloatPoint* pPoints, int nPoints)
-{
- if (nPoints == 0) {
- return CFX_FloatRect();
- }
- FX_FLOAT min_x = pPoints->x, max_x = pPoints->x, min_y = pPoints->y, max_y = pPoints->y;
- for (int i = 1; i < nPoints; i ++) {
- if (min_x > pPoints[i].x) {
- min_x = pPoints[i].x;
- }
- if (max_x < pPoints[i].x) {
- max_x = pPoints[i].x;
- }
- if (min_y > pPoints[i].y) {
- min_y = pPoints[i].y;
- }
- if (max_y < pPoints[i].y) {
- max_y = pPoints[i].y;
- }
- }
- return CFX_FloatRect(min_x, min_y, max_x, max_y);
-}
-void CFX_Matrix::Set(FX_FLOAT a, FX_FLOAT b, FX_FLOAT c, FX_FLOAT d, FX_FLOAT e, FX_FLOAT f)
-{
- this->a = a;
- this->b = b;
- this->c = c;
- this->d = d;
- this->e = e;
- this->f = f;
-}
-void CFX_Matrix::Set(const FX_FLOAT n[6])
-{
- FXSYS_memcpy32((void*)this, &n, sizeof(CFX_Matrix));
-}
-void CFX_Matrix::SetReverse(const CFX_Matrix &m)
-{
- FX_FLOAT i = m.a * m.d - m.b * m.c;
- if (FXSYS_fabs(i) == 0) {
- return;
- }
- FX_FLOAT j = -i;
- a = m.d / i;
- b = m.b / j;
- c = m.c / j;
- d = m.a / i;
- e = (m.c * m.f - m.d * m.e) / i;
- f = (m.a * m.f - m.b * m.e) / j;
-}
-static void FXCRT_Matrix_Concat(CFX_Matrix &m, const CFX_Matrix &m1, const CFX_Matrix &m2)
-{
- FX_FLOAT aa = m1.a * m2.a + m1.b * m2.c;
- FX_FLOAT bb = m1.a * m2.b + m1.b * m2.d;
- FX_FLOAT cc = m1.c * m2.a + m1.d * m2.c;
- FX_FLOAT dd = m1.c * m2.b + m1.d * m2.d;
- FX_FLOAT ee = m1.e * m2.a + m1.f * m2.c + m2.e;
- FX_FLOAT ff = m1.e * m2.b + m1.f * m2.d + m2.f;
- m.a = aa, m.b = bb, m.c = cc, m.d = dd, m.e = ee, m.f = ff;
-}
-void CFX_Matrix::Concat(FX_FLOAT a, FX_FLOAT b, FX_FLOAT c, FX_FLOAT d, FX_FLOAT e, FX_FLOAT f, FX_BOOL bPrepended)
-{
- CFX_Matrix m;
- m.Set(a, b, c, d, e, f);
- Concat(m, bPrepended);
-}
-void CFX_Matrix::Concat(const CFX_Matrix &m, FX_BOOL bPrepended)
-{
- if (bPrepended) {
- FXCRT_Matrix_Concat(*this, m, *this);
- } else {
- FXCRT_Matrix_Concat(*this, *this, m);
- }
-}
-void CFX_Matrix::ConcatInverse(const CFX_Matrix& src, FX_BOOL bPrepended)
-{
- CFX_Matrix m;
- m.SetReverse(src);
- Concat(m, bPrepended);
-}
-FX_BOOL CFX_Matrix::IsInvertible() const
-{
- return FXSYS_fabs(a * d - b * c) >= 0.0001f;
-}
-FX_BOOL CFX_Matrix::Is90Rotated() const
-{
- return FXSYS_fabs(a * 1000) < FXSYS_fabs(b) && FXSYS_fabs(d * 1000) < FXSYS_fabs(c);
-}
-FX_BOOL CFX_Matrix::IsScaled() const
-{
- return FXSYS_fabs(b * 1000) < FXSYS_fabs(a) && FXSYS_fabs(c * 1000) < FXSYS_fabs(d);
-}
-void CFX_Matrix::Translate(FX_FLOAT x, FX_FLOAT y, FX_BOOL bPrepended)
-{
- if (bPrepended) {
- e += x * a + y * c;
- f += y * d + x * b;
- } else {
- e += x, f += y;
- }
-}
-void CFX_Matrix::Scale(FX_FLOAT sx, FX_FLOAT sy, FX_BOOL bPrepended)
-{
- a *= sx, d *= sy;
- if (bPrepended) {
- b *= sx;
- c *= sy;
- } else {
- b *= sy;
- c *= sx;
- e *= sx;
- f *= sy;
- }
-}
-void CFX_Matrix::Rotate(FX_FLOAT fRadian, FX_BOOL bPrepended)
-{
- FX_FLOAT cosValue = FXSYS_cos(fRadian);
- FX_FLOAT sinValue = FXSYS_sin(fRadian);
- CFX_Matrix m;
- m.Set(cosValue, sinValue, -sinValue, cosValue, 0, 0);
- if (bPrepended) {
- FXCRT_Matrix_Concat(*this, m, *this);
- } else {
- FXCRT_Matrix_Concat(*this, *this, m);
- }
-}
-void CFX_Matrix::RotateAt(FX_FLOAT fRadian, FX_FLOAT dx, FX_FLOAT dy, FX_BOOL bPrepended)
-{
- Translate(dx, dy, bPrepended);
- Rotate(fRadian, bPrepended);
- Translate(-dx, -dy, bPrepended);
-}
-void CFX_Matrix::Shear(FX_FLOAT fAlphaRadian, FX_FLOAT fBetaRadian, FX_BOOL bPrepended)
-{
- CFX_Matrix m;
- m.Set(1, FXSYS_tan(fAlphaRadian), FXSYS_tan(fBetaRadian), 1, 0, 0);
- if (bPrepended) {
- FXCRT_Matrix_Concat(*this, m, *this);
- } else {
- FXCRT_Matrix_Concat(*this, *this, m);
- }
-}
-void CFX_Matrix::MatchRect(const CFX_FloatRect& dest, const CFX_FloatRect& src)
-{
- FX_FLOAT fDiff = src.left - src.right;
- a = FXSYS_fabs(fDiff) < 0.001f ? 1 : (dest.left - dest.right) / fDiff;
- fDiff = src.bottom - src.top;
- d = FXSYS_fabs(fDiff) < 0.001f ? 1 : (dest.bottom - dest.top) / fDiff;
- e = dest.left - src.left * a;
- f = dest.bottom - src.bottom * d;
- b = 0;
- c = 0;
-}
-FX_FLOAT CFX_Matrix::GetXUnit() const
-{
- if (b == 0) {
- return (a > 0 ? a : -a);
- }
- if (a == 0) {
- return (b > 0 ? b : -b);
- }
- return FXSYS_sqrt(a * a + b * b);
-}
-FX_FLOAT CFX_Matrix::GetYUnit() const
-{
- if (c == 0) {
- return (d > 0 ? d : -d);
- }
- if (d == 0) {
- return (c > 0 ? c : -c);
- }
- return FXSYS_sqrt(c * c + d * d);
-}
-void CFX_Matrix::GetUnitRect(CFX_RectF &rect) const
-{
- rect.left = rect.top = 0;
- rect.width = rect.height = 1;
- TransformRect(rect);
-}
-CFX_FloatRect CFX_Matrix::GetUnitRect() const
-{
- CFX_FloatRect rect(0, 0, 1, 1);
- rect.Transform((const CFX_Matrix*)this);
- return rect;
-}
-FX_FLOAT CFX_Matrix::GetUnitArea() const
-{
- FX_FLOAT A = FXSYS_sqrt(a * a + b * b);
- FX_FLOAT B = FXSYS_sqrt(c * c + d * d);
- FX_FLOAT ac = a + c, bd = b + d;
- FX_FLOAT C = FXSYS_sqrt(ac * ac + bd * bd);
- FX_FLOAT P = (A + B + C ) / 2;
- return FXSYS_sqrt(P * (P - A) * (P - B) * (P - C)) * 2;
-}
-FX_FLOAT CFX_Matrix::TransformXDistance(FX_FLOAT dx) const
-{
- FX_FLOAT fx = a * dx, fy = b * dx;
- return FXSYS_sqrt(fx * fx + fy * fy);
-}
-FX_INT32 CFX_Matrix::TransformXDistance(FX_INT32 dx) const
-{
- FX_FLOAT fx = a * dx, fy = b * dx;
- return FXSYS_round(FXSYS_sqrt(fx * fx + fy * fy));
-}
-FX_FLOAT CFX_Matrix::TransformYDistance(FX_FLOAT dy) const
-{
- FX_FLOAT fx = c * dy, fy = d * dy;
- return FXSYS_sqrt(fx * fx + fy * fy);
-}
-FX_INT32 CFX_Matrix::TransformYDistance(FX_INT32 dy) const
-{
- FX_FLOAT fx = c * dy, fy = d * dy;
- return FXSYS_round(FXSYS_sqrt(fx * fx + fy * fy));
-}
-FX_FLOAT CFX_Matrix::TransformDistance(FX_FLOAT dx, FX_FLOAT dy) const
-{
- FX_FLOAT fx = a * dx + c * dy, fy = b * dx + d * dy;
- return FXSYS_sqrt(fx * fx + fy * fy);
-}
-FX_INT32 CFX_Matrix::TransformDistance(FX_INT32 dx, FX_INT32 dy) const
-{
- FX_FLOAT fx = a * dx + c * dy, fy = b * dx + d * dy;
- return FXSYS_round(FXSYS_sqrt(fx * fx + fy * fy));
-}
-FX_FLOAT CFX_Matrix::TransformDistance(FX_FLOAT distance) const
-{
- return distance * (GetXUnit() + GetYUnit()) / 2;
-}
-void CFX_Matrix::TransformVector(CFX_VectorF &v) const
-{
- FX_FLOAT fx = a * v.x + c * v.y;
- FX_FLOAT fy = b * v.x + d * v.y;
- v.x = fx, v.y = fy;
-}
-void CFX_Matrix::TransformVector(CFX_Vector &v) const
-{
- FX_FLOAT fx = a * v.x + c * v.y;
- FX_FLOAT fy = b * v.x + d * v.y;
- v.x = FXSYS_round(fx);
- v.y = FXSYS_round(fy);
-}
-void CFX_Matrix::TransformPoints(CFX_Point *points, FX_INT32 iCount) const
-{
- FXSYS_assert(iCount > 0);
- FX_FLOAT fx, fy;
- for (FX_INT32 i = 0; i < iCount; i ++) {
- fx = a * points->x + c * points->y + e;
- fy = b * points->x + d * points->y + f;
- points->x = FXSYS_round(fx);
- points->y = FXSYS_round(fy);
- points ++;
- }
-}
-void CFX_Matrix::TransformPoints(CFX_PointF *points, FX_INT32 iCount) const
-{
- FXSYS_assert(iCount > 0);
- FX_FLOAT fx, fy;
- for (FX_INT32 i = 0; i < iCount; i ++) {
- fx = a * points->x + c * points->y + e;
- fy = b * points->x + d * points->y + f;
- points->x = fx, points->y = fy;
- points ++;
- }
-}
-void CFX_Matrix::TransformPoint(FX_FLOAT &x, FX_FLOAT &y) const
-{
- FX_FLOAT fx = a * x + c * y + e;
- FX_FLOAT fy = b * x + d * y + f;
- x = fx, y = fy;
-}
-void CFX_Matrix::TransformPoint(FX_INT32 &x, FX_INT32 &y) const
-{
- FX_FLOAT fx = a * x + c * y + e;
- FX_FLOAT fy = b * x + d * y + f;
- x = FXSYS_round(fx);
- y = FXSYS_round(fy);
-}
-void CFX_Matrix::TransformRect(CFX_RectF &rect) const
-{
- FX_FLOAT right = rect.right(), bottom = rect.bottom();
- TransformRect(rect.left, right, bottom, rect.top);
- rect.width = right - rect.left;
- rect.height = bottom - rect.top;
-}
-void CFX_Matrix::TransformRect(CFX_Rect &rect) const
-{
- FX_FLOAT left = (FX_FLOAT)rect.left;
- FX_FLOAT top = (FX_FLOAT)rect.bottom();
- FX_FLOAT right = (FX_FLOAT)rect.right();
- FX_FLOAT bottom = (FX_FLOAT)rect.top;
- TransformRect(left, right, top, bottom);
- rect.left = FXSYS_round(left);
- rect.top = FXSYS_round(bottom);
- rect.width = FXSYS_round(right - left);
- rect.height = FXSYS_round(top - bottom);
-}
-void CFX_Matrix::TransformRect(FX_FLOAT& left, FX_FLOAT& right, FX_FLOAT& top, FX_FLOAT& bottom) const
-{
- FX_FLOAT x[4], y[4];
- x[0] = left;
- y[0] = top;
- x[1] = left;
- y[1] = bottom;
- x[2] = right;
- y[2] = top;
- x[3] = right;
- y[3] = bottom;
- int i;
- for (i = 0; i < 4; i ++) {
- Transform(x[i], y[i], x[i], y[i]);
- }
- right = left = x[0];
- top = bottom = y[0];
- for (i = 1; i < 4; i ++) {
- if (right < x[i]) {
- right = x[i];
- }
- if (left > x[i]) {
- left = x[i];
- }
- if (top < y[i]) {
- top = y[i];
- }
- if (bottom > y[i]) {
- bottom = y[i];
- }
- }
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_ext.h" +void FX_RECT::Normalize() +{ + if (left > right) { + int temp = left; + left = right; + right = temp; + } + if (top > bottom) { + int temp = top; + top = bottom; + bottom = temp; + } +} +void FX_RECT::Intersect(const FX_RECT& src) +{ + FX_RECT src_n = src; + src_n.Normalize(); + Normalize(); + left = left > src_n.left ? left : src_n.left; + top = top > src_n.top ? top : src_n.top; + right = right < src_n.right ? right : src_n.right; + bottom = bottom < src_n.bottom ? bottom : src_n.bottom; + if (left > right || top > bottom) { + left = top = right = bottom = 0; + } +} +void FX_RECT::Union(const FX_RECT& other_rect) +{ + Normalize(); + FX_RECT other = other_rect; + other.Normalize(); + left = left < other.left ? left : other.left; + right = right > other.right ? right : other.right; + bottom = bottom > other.bottom ? bottom : other.bottom; + top = top < other.top ? top : other.top; +} +FX_BOOL GetIntersection(FX_FLOAT low1, FX_FLOAT high1, FX_FLOAT low2, FX_FLOAT high2, + FX_FLOAT& interlow, FX_FLOAT& interhigh) +{ + if (low1 >= high2 || low2 >= high1) { + return FALSE; + } + interlow = low1 > low2 ? low1 : low2; + interhigh = high1 > high2 ? high2 : high1; + return TRUE; +} +extern "C" int FXSYS_round(FX_FLOAT d) +{ + int iRet = 0; + if (d >= 0.0f) { + iRet = (int)(d + 0.5f); + if (iRet >= 0) { + return iRet; + } + return -iRet; + } + return (int)(d - 0.5f); +} +CFX_FloatRect::CFX_FloatRect(const FX_RECT& rect) +{ + left = (FX_FLOAT)(rect.left); + right = (FX_FLOAT)(rect.right); + bottom = (FX_FLOAT)(rect.top); + top = (FX_FLOAT)(rect.bottom); +} +void CFX_FloatRect::Normalize() +{ + FX_FLOAT temp; + if (left > right) { + temp = left; + left = right; + right = temp; + } + if (bottom > top) { + temp = top; + top = bottom; + bottom = temp; + } +} +void CFX_FloatRect::Intersect(const CFX_FloatRect& other_rect) +{ + Normalize(); + CFX_FloatRect other = other_rect; + other.Normalize(); + left = left > other.left ? left : other.left; + right = right < other.right ? right : other.right; + bottom = bottom > other.bottom ? bottom : other.bottom; + top = top < other.top ? top : other.top; + if (left > right || bottom > top) { + left = right = bottom = top = 0; + } +} +void CFX_FloatRect::Union(const CFX_FloatRect& other_rect) +{ + Normalize(); + CFX_FloatRect other = other_rect; + other.Normalize(); + left = left < other.left ? left : other.left; + right = right > other.right ? right : other.right; + bottom = bottom < other.bottom ? bottom : other.bottom; + top = top > other.top ? top : other.top; +} +void CFX_FloatRect::Transform(const CFX_Matrix* pMatrix) +{ + pMatrix->TransformRect(left, right, top, bottom); +} +int CFX_FloatRect::Substract4(CFX_FloatRect& s, CFX_FloatRect* pRects) +{ + Normalize(); + s.Normalize(); + int nRects = 0; + CFX_FloatRect rects[4]; + if (left < s.left) { + rects[nRects].left = left; + rects[nRects].right = s.left; + rects[nRects].bottom = bottom; + rects[nRects].top = top; + nRects ++; + } + if (s.left < right && s.top < top) { + rects[nRects].left = s.left; + rects[nRects].right = right; + rects[nRects].bottom = s.top; + rects[nRects].top = top; + nRects ++; + } + if (s.top > bottom && s.right < right) { + rects[nRects].left = s.right; + rects[nRects].right = right; + rects[nRects].bottom = bottom; + rects[nRects].top = s.top; + nRects ++; + } + if (s.bottom > bottom) { + rects[nRects].left = s.left; + rects[nRects].right = s.right; + rects[nRects].bottom = bottom; + rects[nRects].top = s.bottom; + nRects ++; + } + if (nRects == 0) { + return 0; + } + for (int i = 0; i < nRects; i ++) { + pRects[i] = rects[i]; + pRects[i].Intersect(*this); + } + return nRects; +} +FX_RECT CFX_FloatRect::GetOutterRect() const +{ + CFX_FloatRect rect1 = *this; + FX_RECT rect; + rect.left = (int)FXSYS_floor(rect1.left); + rect.right = (int)FXSYS_ceil(rect1.right); + rect.top = (int)FXSYS_floor(rect1.bottom); + rect.bottom = (int)FXSYS_ceil(rect1.top); + rect.Normalize(); + return rect; +} +FX_RECT CFX_FloatRect::GetInnerRect() const +{ + CFX_FloatRect rect1 = *this; + FX_RECT rect; + rect.left = (int)FXSYS_ceil(rect1.left); + rect.right = (int)FXSYS_floor(rect1.right); + rect.top = (int)FXSYS_ceil(rect1.bottom); + rect.bottom = (int)FXSYS_floor(rect1.top); + rect.Normalize(); + return rect; +} +static void _MatchFloatRange(FX_FLOAT f1, FX_FLOAT f2, int& i1, int& i2) +{ + int length = (int)FXSYS_ceil(f2 - f1); + int i1_1 = (int)FXSYS_floor(f1); + int i1_2 = (int)FXSYS_ceil(f1); + FX_FLOAT error1 = f1 - i1_1 + (FX_FLOAT)FXSYS_fabs(f2 - i1_1 - length); + FX_FLOAT error2 = i1_2 - f1 + (FX_FLOAT)FXSYS_fabs(f2 - i1_2 - length); + i1 = (error1 > error2) ? i1_2 : i1_1; + i2 = i1 + length; +} +FX_RECT CFX_FloatRect::GetClosestRect() const +{ + CFX_FloatRect rect1 = *this; + FX_RECT rect; + _MatchFloatRange(rect1.left, rect1.right, rect.left, rect.right); + _MatchFloatRange(rect1.bottom, rect1.top, rect.top, rect.bottom); + rect.Normalize(); + return rect; +} +FX_BOOL CFX_FloatRect::Contains(const CFX_FloatRect& other_rect) const +{ + CFX_FloatRect n1 = *this; + n1.Normalize(); + CFX_FloatRect n2 = other_rect; + n2.Normalize(); + if (n2.left >= n1.left && n2.right <= n1.right && n2.bottom >= n1.bottom && n2.top <= n1.top) { + return TRUE; + } + return FALSE; +} +FX_BOOL CFX_FloatRect::Contains(FX_FLOAT x, FX_FLOAT y) const +{ + CFX_FloatRect n1 = *this; + n1.Normalize(); + return x <= n1.right && x >= n1.left && y <= n1.top && y >= n1.bottom; +} +void CFX_FloatRect::UpdateRect(FX_FLOAT x, FX_FLOAT y) +{ + if (left > x) { + left = x; + } + if (right < x) { + right = x; + } + if (bottom > y) { + bottom = y; + } + if (top < y) { + top = y; + } +} +CFX_FloatRect CFX_FloatRect::GetBBox(const CFX_FloatPoint* pPoints, int nPoints) +{ + if (nPoints == 0) { + return CFX_FloatRect(); + } + FX_FLOAT min_x = pPoints->x, max_x = pPoints->x, min_y = pPoints->y, max_y = pPoints->y; + for (int i = 1; i < nPoints; i ++) { + if (min_x > pPoints[i].x) { + min_x = pPoints[i].x; + } + if (max_x < pPoints[i].x) { + max_x = pPoints[i].x; + } + if (min_y > pPoints[i].y) { + min_y = pPoints[i].y; + } + if (max_y < pPoints[i].y) { + max_y = pPoints[i].y; + } + } + return CFX_FloatRect(min_x, min_y, max_x, max_y); +} +void CFX_Matrix::Set(FX_FLOAT a, FX_FLOAT b, FX_FLOAT c, FX_FLOAT d, FX_FLOAT e, FX_FLOAT f) +{ + this->a = a; + this->b = b; + this->c = c; + this->d = d; + this->e = e; + this->f = f; +} +void CFX_Matrix::Set(const FX_FLOAT n[6]) +{ + FXSYS_memcpy32((void*)this, &n, sizeof(CFX_Matrix)); +} +void CFX_Matrix::SetReverse(const CFX_Matrix &m) +{ + FX_FLOAT i = m.a * m.d - m.b * m.c; + if (FXSYS_fabs(i) == 0) { + return; + } + FX_FLOAT j = -i; + a = m.d / i; + b = m.b / j; + c = m.c / j; + d = m.a / i; + e = (m.c * m.f - m.d * m.e) / i; + f = (m.a * m.f - m.b * m.e) / j; +} +static void FXCRT_Matrix_Concat(CFX_Matrix &m, const CFX_Matrix &m1, const CFX_Matrix &m2) +{ + FX_FLOAT aa = m1.a * m2.a + m1.b * m2.c; + FX_FLOAT bb = m1.a * m2.b + m1.b * m2.d; + FX_FLOAT cc = m1.c * m2.a + m1.d * m2.c; + FX_FLOAT dd = m1.c * m2.b + m1.d * m2.d; + FX_FLOAT ee = m1.e * m2.a + m1.f * m2.c + m2.e; + FX_FLOAT ff = m1.e * m2.b + m1.f * m2.d + m2.f; + m.a = aa, m.b = bb, m.c = cc, m.d = dd, m.e = ee, m.f = ff; +} +void CFX_Matrix::Concat(FX_FLOAT a, FX_FLOAT b, FX_FLOAT c, FX_FLOAT d, FX_FLOAT e, FX_FLOAT f, FX_BOOL bPrepended) +{ + CFX_Matrix m; + m.Set(a, b, c, d, e, f); + Concat(m, bPrepended); +} +void CFX_Matrix::Concat(const CFX_Matrix &m, FX_BOOL bPrepended) +{ + if (bPrepended) { + FXCRT_Matrix_Concat(*this, m, *this); + } else { + FXCRT_Matrix_Concat(*this, *this, m); + } +} +void CFX_Matrix::ConcatInverse(const CFX_Matrix& src, FX_BOOL bPrepended) +{ + CFX_Matrix m; + m.SetReverse(src); + Concat(m, bPrepended); +} +FX_BOOL CFX_Matrix::IsInvertible() const +{ + return FXSYS_fabs(a * d - b * c) >= 0.0001f; +} +FX_BOOL CFX_Matrix::Is90Rotated() const +{ + return FXSYS_fabs(a * 1000) < FXSYS_fabs(b) && FXSYS_fabs(d * 1000) < FXSYS_fabs(c); +} +FX_BOOL CFX_Matrix::IsScaled() const +{ + return FXSYS_fabs(b * 1000) < FXSYS_fabs(a) && FXSYS_fabs(c * 1000) < FXSYS_fabs(d); +} +void CFX_Matrix::Translate(FX_FLOAT x, FX_FLOAT y, FX_BOOL bPrepended) +{ + if (bPrepended) { + e += x * a + y * c; + f += y * d + x * b; + } else { + e += x, f += y; + } +} +void CFX_Matrix::Scale(FX_FLOAT sx, FX_FLOAT sy, FX_BOOL bPrepended) +{ + a *= sx, d *= sy; + if (bPrepended) { + b *= sx; + c *= sy; + } else { + b *= sy; + c *= sx; + e *= sx; + f *= sy; + } +} +void CFX_Matrix::Rotate(FX_FLOAT fRadian, FX_BOOL bPrepended) +{ + FX_FLOAT cosValue = FXSYS_cos(fRadian); + FX_FLOAT sinValue = FXSYS_sin(fRadian); + CFX_Matrix m; + m.Set(cosValue, sinValue, -sinValue, cosValue, 0, 0); + if (bPrepended) { + FXCRT_Matrix_Concat(*this, m, *this); + } else { + FXCRT_Matrix_Concat(*this, *this, m); + } +} +void CFX_Matrix::RotateAt(FX_FLOAT fRadian, FX_FLOAT dx, FX_FLOAT dy, FX_BOOL bPrepended) +{ + Translate(dx, dy, bPrepended); + Rotate(fRadian, bPrepended); + Translate(-dx, -dy, bPrepended); +} +void CFX_Matrix::Shear(FX_FLOAT fAlphaRadian, FX_FLOAT fBetaRadian, FX_BOOL bPrepended) +{ + CFX_Matrix m; + m.Set(1, FXSYS_tan(fAlphaRadian), FXSYS_tan(fBetaRadian), 1, 0, 0); + if (bPrepended) { + FXCRT_Matrix_Concat(*this, m, *this); + } else { + FXCRT_Matrix_Concat(*this, *this, m); + } +} +void CFX_Matrix::MatchRect(const CFX_FloatRect& dest, const CFX_FloatRect& src) +{ + FX_FLOAT fDiff = src.left - src.right; + a = FXSYS_fabs(fDiff) < 0.001f ? 1 : (dest.left - dest.right) / fDiff; + fDiff = src.bottom - src.top; + d = FXSYS_fabs(fDiff) < 0.001f ? 1 : (dest.bottom - dest.top) / fDiff; + e = dest.left - src.left * a; + f = dest.bottom - src.bottom * d; + b = 0; + c = 0; +} +FX_FLOAT CFX_Matrix::GetXUnit() const +{ + if (b == 0) { + return (a > 0 ? a : -a); + } + if (a == 0) { + return (b > 0 ? b : -b); + } + return FXSYS_sqrt(a * a + b * b); +} +FX_FLOAT CFX_Matrix::GetYUnit() const +{ + if (c == 0) { + return (d > 0 ? d : -d); + } + if (d == 0) { + return (c > 0 ? c : -c); + } + return FXSYS_sqrt(c * c + d * d); +} +void CFX_Matrix::GetUnitRect(CFX_RectF &rect) const +{ + rect.left = rect.top = 0; + rect.width = rect.height = 1; + TransformRect(rect); +} +CFX_FloatRect CFX_Matrix::GetUnitRect() const +{ + CFX_FloatRect rect(0, 0, 1, 1); + rect.Transform((const CFX_Matrix*)this); + return rect; +} +FX_FLOAT CFX_Matrix::GetUnitArea() const +{ + FX_FLOAT A = FXSYS_sqrt(a * a + b * b); + FX_FLOAT B = FXSYS_sqrt(c * c + d * d); + FX_FLOAT ac = a + c, bd = b + d; + FX_FLOAT C = FXSYS_sqrt(ac * ac + bd * bd); + FX_FLOAT P = (A + B + C ) / 2; + return FXSYS_sqrt(P * (P - A) * (P - B) * (P - C)) * 2; +} +FX_FLOAT CFX_Matrix::TransformXDistance(FX_FLOAT dx) const +{ + FX_FLOAT fx = a * dx, fy = b * dx; + return FXSYS_sqrt(fx * fx + fy * fy); +} +FX_INT32 CFX_Matrix::TransformXDistance(FX_INT32 dx) const +{ + FX_FLOAT fx = a * dx, fy = b * dx; + return FXSYS_round(FXSYS_sqrt(fx * fx + fy * fy)); +} +FX_FLOAT CFX_Matrix::TransformYDistance(FX_FLOAT dy) const +{ + FX_FLOAT fx = c * dy, fy = d * dy; + return FXSYS_sqrt(fx * fx + fy * fy); +} +FX_INT32 CFX_Matrix::TransformYDistance(FX_INT32 dy) const +{ + FX_FLOAT fx = c * dy, fy = d * dy; + return FXSYS_round(FXSYS_sqrt(fx * fx + fy * fy)); +} +FX_FLOAT CFX_Matrix::TransformDistance(FX_FLOAT dx, FX_FLOAT dy) const +{ + FX_FLOAT fx = a * dx + c * dy, fy = b * dx + d * dy; + return FXSYS_sqrt(fx * fx + fy * fy); +} +FX_INT32 CFX_Matrix::TransformDistance(FX_INT32 dx, FX_INT32 dy) const +{ + FX_FLOAT fx = a * dx + c * dy, fy = b * dx + d * dy; + return FXSYS_round(FXSYS_sqrt(fx * fx + fy * fy)); +} +FX_FLOAT CFX_Matrix::TransformDistance(FX_FLOAT distance) const +{ + return distance * (GetXUnit() + GetYUnit()) / 2; +} +void CFX_Matrix::TransformVector(CFX_VectorF &v) const +{ + FX_FLOAT fx = a * v.x + c * v.y; + FX_FLOAT fy = b * v.x + d * v.y; + v.x = fx, v.y = fy; +} +void CFX_Matrix::TransformVector(CFX_Vector &v) const +{ + FX_FLOAT fx = a * v.x + c * v.y; + FX_FLOAT fy = b * v.x + d * v.y; + v.x = FXSYS_round(fx); + v.y = FXSYS_round(fy); +} +void CFX_Matrix::TransformPoints(CFX_Point *points, FX_INT32 iCount) const +{ + FXSYS_assert(iCount > 0); + FX_FLOAT fx, fy; + for (FX_INT32 i = 0; i < iCount; i ++) { + fx = a * points->x + c * points->y + e; + fy = b * points->x + d * points->y + f; + points->x = FXSYS_round(fx); + points->y = FXSYS_round(fy); + points ++; + } +} +void CFX_Matrix::TransformPoints(CFX_PointF *points, FX_INT32 iCount) const +{ + FXSYS_assert(iCount > 0); + FX_FLOAT fx, fy; + for (FX_INT32 i = 0; i < iCount; i ++) { + fx = a * points->x + c * points->y + e; + fy = b * points->x + d * points->y + f; + points->x = fx, points->y = fy; + points ++; + } +} +void CFX_Matrix::TransformPoint(FX_FLOAT &x, FX_FLOAT &y) const +{ + FX_FLOAT fx = a * x + c * y + e; + FX_FLOAT fy = b * x + d * y + f; + x = fx, y = fy; +} +void CFX_Matrix::TransformPoint(FX_INT32 &x, FX_INT32 &y) const +{ + FX_FLOAT fx = a * x + c * y + e; + FX_FLOAT fy = b * x + d * y + f; + x = FXSYS_round(fx); + y = FXSYS_round(fy); +} +void CFX_Matrix::TransformRect(CFX_RectF &rect) const +{ + FX_FLOAT right = rect.right(), bottom = rect.bottom(); + TransformRect(rect.left, right, bottom, rect.top); + rect.width = right - rect.left; + rect.height = bottom - rect.top; +} +void CFX_Matrix::TransformRect(CFX_Rect &rect) const +{ + FX_FLOAT left = (FX_FLOAT)rect.left; + FX_FLOAT top = (FX_FLOAT)rect.bottom(); + FX_FLOAT right = (FX_FLOAT)rect.right(); + FX_FLOAT bottom = (FX_FLOAT)rect.top; + TransformRect(left, right, top, bottom); + rect.left = FXSYS_round(left); + rect.top = FXSYS_round(bottom); + rect.width = FXSYS_round(right - left); + rect.height = FXSYS_round(top - bottom); +} +void CFX_Matrix::TransformRect(FX_FLOAT& left, FX_FLOAT& right, FX_FLOAT& top, FX_FLOAT& bottom) const +{ + FX_FLOAT x[4], y[4]; + x[0] = left; + y[0] = top; + x[1] = left; + y[1] = bottom; + x[2] = right; + y[2] = top; + x[3] = right; + y[3] = bottom; + int i; + for (i = 0; i < 4; i ++) { + Transform(x[i], y[i], x[i], y[i]); + } + right = left = x[0]; + top = bottom = y[0]; + for (i = 1; i < 4; i ++) { + if (right < x[i]) { + right = x[i]; + } + if (left > x[i]) { + left = x[i]; + } + if (top < y[i]) { + top = y[i]; + } + if (bottom > y[i]) { + bottom = y[i]; + } + } +} diff --git a/core/src/fxcrt/fx_basic_gcc.cpp b/core/src/fxcrt/fx_basic_gcc.cpp index 20a1e58925..7f5bbade66 100644 --- a/core/src/fxcrt/fx_basic_gcc.cpp +++ b/core/src/fxcrt/fx_basic_gcc.cpp @@ -1,232 +1,232 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_ext.h"
-template <class T, class STR_T>
-T FXSYS_StrToInt(STR_T str)
-{
- FX_BOOL neg = FALSE;
- if (str == NULL) {
- return 0;
- }
- if (*str == '-') {
- neg = TRUE;
- str ++;
- }
- T num = 0;
- while (*str) {
- if ((*str) < '0' || (*str) > '9') {
- break;
- }
- num = num * 10 + (*str) - '0';
- str ++;
- }
- return neg ? -num : num;
-}
-template <typename T, typename STR_T>
-STR_T FXSYS_IntToStr(T value, STR_T string, int radix)
-{
- int i = 0;
- if (value < 0) {
- string[i++] = '-';
- value = -value;
- } else if (value == 0) {
- string[0] = '0';
- string[1] = 0;
- return string;
- }
- int digits = 1;
- T order = value / 10;
- while(order > 0) {
- digits++;
- order = order / 10;
- }
- for (int d = digits - 1; d > -1; d--) {
- string[d + i] = "0123456789abcdef"[value % 10];
- value /= 10;
- }
- string[digits + i] = 0;
- return string;
-}
-#ifdef __cplusplus
-extern "C" {
-#endif
-FX_INT32 FXSYS_atoi(FX_LPCSTR str)
-{
- return FXSYS_StrToInt<FX_INT32, FX_LPCSTR>(str);
-}
-FX_INT32 FXSYS_wtoi(FX_LPCWSTR str)
-{
- return FXSYS_StrToInt<FX_INT32, FX_LPCWSTR>(str);
-}
-FX_INT64 FXSYS_atoi64(FX_LPCSTR str)
-{
- return FXSYS_StrToInt<FX_INT64, FX_LPCSTR>(str);
-}
-FX_INT64 FXSYS_wtoi64(FX_LPCWSTR str)
-{
- return FXSYS_StrToInt<FX_INT64, FX_LPCWSTR>(str);
-}
-FX_LPCSTR FXSYS_i64toa(FX_INT64 value, FX_LPSTR str, int radix)
-{
- return FXSYS_IntToStr<FX_INT64, FX_LPSTR>(value, str, radix);
-}
-FX_LPCWSTR FXSYS_i64tow(FX_INT64 value, FX_LPWSTR str, int radix)
-{
- return FXSYS_IntToStr<FX_INT64, FX_LPWSTR>(value, str, radix);
-}
-#ifdef __cplusplus
-}
-#endif
-#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-#ifdef __cplusplus
-extern "C" {
-#endif
-int FXSYS_GetACP()
-{
- return 0;
-}
-FX_DWORD FXSYS_GetFullPathName(FX_LPCSTR filename, FX_DWORD buflen, FX_LPSTR buf, FX_LPSTR* filepart)
-{
- int srclen = FXSYS_strlen(filename);
- if (buf == NULL || (int)buflen < srclen + 1) {
- return srclen + 1;
- }
- FXSYS_strcpy(buf, filename);
- return srclen;
-}
-FX_DWORD FXSYS_GetModuleFileName(FX_LPVOID hModule, char* buf, FX_DWORD bufsize)
-{
- return (FX_DWORD) - 1;
-}
-#ifdef __cplusplus
-}
-#endif
-#endif
-#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-#ifdef __cplusplus
-extern "C" {
-#endif
-FXSYS_FILE* FXSYS_wfopen(FX_LPCWSTR filename, FX_LPCWSTR mode)
-{
- return FXSYS_fopen(CFX_ByteString::FromUnicode(filename), CFX_ByteString::FromUnicode(mode));
-}
-char* FXSYS_strlwr(char* str)
-{
- if (str == NULL) {
- return NULL;
- }
- char* s = str;
- while (*str) {
- *str = FXSYS_tolower(*str);
- str ++;
- }
- return s;
-}
-char* FXSYS_strupr(char* str)
-{
- if (str == NULL) {
- return NULL;
- }
- char* s = str;
- while (*str) {
- *str = FXSYS_toupper(*str);
- str ++;
- }
- return s;
-}
-FX_WCHAR* FXSYS_wcslwr(FX_WCHAR* str)
-{
- if (str == NULL) {
- return NULL;
- }
- FX_WCHAR* s = str;
- while (*str) {
- *str = FXSYS_tolower(*str);
- str ++;
- }
- return s;
-}
-FX_WCHAR* FXSYS_wcsupr(FX_WCHAR* str)
-{
- if (str == NULL) {
- return NULL;
- }
- FX_WCHAR* s = str;
- while (*str) {
- *str = FXSYS_toupper(*str);
- str ++;
- }
- return s;
-}
-int FXSYS_stricmp(const char*dst, const char*src)
-{
- int f, l;
- do {
- if ( ((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z') ) {
- f -= ('A' - 'a');
- }
- if ( ((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z') ) {
- l -= ('A' - 'a');
- }
- } while ( f && (f == l) );
- return(f - l);
-}
-int FXSYS_wcsicmp(const FX_WCHAR *dst, const FX_WCHAR *src)
-{
- FX_WCHAR f, l;
- do {
- if ( ((f = (FX_WCHAR)(*(dst++))) >= 'A') && (f <= 'Z') ) {
- f -= ('A' - 'a');
- }
- if ( ((l = (FX_WCHAR)(*(src++))) >= 'A') && (l <= 'Z') ) {
- l -= ('A' - 'a');
- }
- } while ( f && (f == l) );
- return(f - l);
-}
-char* FXSYS_itoa(int value, char* string, int radix)
-{
- return FXSYS_IntToStr<FX_INT32, FX_LPSTR>(value, string, radix);
-}
-#ifdef __cplusplus
-}
-#endif
-#endif
-#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-#ifdef __cplusplus
-extern "C" {
-#endif
-int FXSYS_WideCharToMultiByte(FX_DWORD codepage, FX_DWORD dwFlags, FX_LPCWSTR wstr, int wlen,
- FX_LPSTR buf, int buflen, FX_LPCSTR default_str, FX_BOOL* pUseDefault)
-{
- int len = 0;
- for (int i = 0; i < wlen; i ++) {
- if (wstr[i] < 0x100) {
- if (buf && len < buflen) {
- buf[len] = (FX_CHAR)wstr[i];
- }
- len ++;
- }
- }
- return len;
-}
-int FXSYS_MultiByteToWideChar(FX_DWORD codepage, FX_DWORD dwFlags, FX_LPCSTR bstr, int blen,
- FX_LPWSTR buf, int buflen)
-{
- int wlen = 0;
- for (int i = 0; i < blen; i ++) {
- if (buf && wlen < buflen) {
- buf[wlen] = bstr[i];
- }
- wlen ++;
- }
- return wlen;
-}
-#ifdef __cplusplus
-}
-#endif
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_ext.h" +template <class T, class STR_T> +T FXSYS_StrToInt(STR_T str) +{ + FX_BOOL neg = FALSE; + if (str == NULL) { + return 0; + } + if (*str == '-') { + neg = TRUE; + str ++; + } + T num = 0; + while (*str) { + if ((*str) < '0' || (*str) > '9') { + break; + } + num = num * 10 + (*str) - '0'; + str ++; + } + return neg ? -num : num; +} +template <typename T, typename STR_T> +STR_T FXSYS_IntToStr(T value, STR_T string, int radix) +{ + int i = 0; + if (value < 0) { + string[i++] = '-'; + value = -value; + } else if (value == 0) { + string[0] = '0'; + string[1] = 0; + return string; + } + int digits = 1; + T order = value / 10; + while(order > 0) { + digits++; + order = order / 10; + } + for (int d = digits - 1; d > -1; d--) { + string[d + i] = "0123456789abcdef"[value % 10]; + value /= 10; + } + string[digits + i] = 0; + return string; +} +#ifdef __cplusplus +extern "C" { +#endif +FX_INT32 FXSYS_atoi(FX_LPCSTR str) +{ + return FXSYS_StrToInt<FX_INT32, FX_LPCSTR>(str); +} +FX_INT32 FXSYS_wtoi(FX_LPCWSTR str) +{ + return FXSYS_StrToInt<FX_INT32, FX_LPCWSTR>(str); +} +FX_INT64 FXSYS_atoi64(FX_LPCSTR str) +{ + return FXSYS_StrToInt<FX_INT64, FX_LPCSTR>(str); +} +FX_INT64 FXSYS_wtoi64(FX_LPCWSTR str) +{ + return FXSYS_StrToInt<FX_INT64, FX_LPCWSTR>(str); +} +FX_LPCSTR FXSYS_i64toa(FX_INT64 value, FX_LPSTR str, int radix) +{ + return FXSYS_IntToStr<FX_INT64, FX_LPSTR>(value, str, radix); +} +FX_LPCWSTR FXSYS_i64tow(FX_INT64 value, FX_LPWSTR str, int radix) +{ + return FXSYS_IntToStr<FX_INT64, FX_LPWSTR>(value, str, radix); +} +#ifdef __cplusplus +} +#endif +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ +#ifdef __cplusplus +extern "C" { +#endif +int FXSYS_GetACP() +{ + return 0; +} +FX_DWORD FXSYS_GetFullPathName(FX_LPCSTR filename, FX_DWORD buflen, FX_LPSTR buf, FX_LPSTR* filepart) +{ + int srclen = FXSYS_strlen(filename); + if (buf == NULL || (int)buflen < srclen + 1) { + return srclen + 1; + } + FXSYS_strcpy(buf, filename); + return srclen; +} +FX_DWORD FXSYS_GetModuleFileName(FX_LPVOID hModule, char* buf, FX_DWORD bufsize) +{ + return (FX_DWORD) - 1; +} +#ifdef __cplusplus +} +#endif +#endif +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ +#ifdef __cplusplus +extern "C" { +#endif +FXSYS_FILE* FXSYS_wfopen(FX_LPCWSTR filename, FX_LPCWSTR mode) +{ + return FXSYS_fopen(CFX_ByteString::FromUnicode(filename), CFX_ByteString::FromUnicode(mode)); +} +char* FXSYS_strlwr(char* str) +{ + if (str == NULL) { + return NULL; + } + char* s = str; + while (*str) { + *str = FXSYS_tolower(*str); + str ++; + } + return s; +} +char* FXSYS_strupr(char* str) +{ + if (str == NULL) { + return NULL; + } + char* s = str; + while (*str) { + *str = FXSYS_toupper(*str); + str ++; + } + return s; +} +FX_WCHAR* FXSYS_wcslwr(FX_WCHAR* str) +{ + if (str == NULL) { + return NULL; + } + FX_WCHAR* s = str; + while (*str) { + *str = FXSYS_tolower(*str); + str ++; + } + return s; +} +FX_WCHAR* FXSYS_wcsupr(FX_WCHAR* str) +{ + if (str == NULL) { + return NULL; + } + FX_WCHAR* s = str; + while (*str) { + *str = FXSYS_toupper(*str); + str ++; + } + return s; +} +int FXSYS_stricmp(const char*dst, const char*src) +{ + int f, l; + do { + if ( ((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z') ) { + f -= ('A' - 'a'); + } + if ( ((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z') ) { + l -= ('A' - 'a'); + } + } while ( f && (f == l) ); + return(f - l); +} +int FXSYS_wcsicmp(const FX_WCHAR *dst, const FX_WCHAR *src) +{ + FX_WCHAR f, l; + do { + if ( ((f = (FX_WCHAR)(*(dst++))) >= 'A') && (f <= 'Z') ) { + f -= ('A' - 'a'); + } + if ( ((l = (FX_WCHAR)(*(src++))) >= 'A') && (l <= 'Z') ) { + l -= ('A' - 'a'); + } + } while ( f && (f == l) ); + return(f - l); +} +char* FXSYS_itoa(int value, char* string, int radix) +{ + return FXSYS_IntToStr<FX_INT32, FX_LPSTR>(value, string, radix); +} +#ifdef __cplusplus +} +#endif +#endif +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ +#ifdef __cplusplus +extern "C" { +#endif +int FXSYS_WideCharToMultiByte(FX_DWORD codepage, FX_DWORD dwFlags, FX_LPCWSTR wstr, int wlen, + FX_LPSTR buf, int buflen, FX_LPCSTR default_str, FX_BOOL* pUseDefault) +{ + int len = 0; + for (int i = 0; i < wlen; i ++) { + if (wstr[i] < 0x100) { + if (buf && len < buflen) { + buf[len] = (FX_CHAR)wstr[i]; + } + len ++; + } + } + return len; +} +int FXSYS_MultiByteToWideChar(FX_DWORD codepage, FX_DWORD dwFlags, FX_LPCSTR bstr, int blen, + FX_LPWSTR buf, int buflen) +{ + int wlen = 0; + for (int i = 0; i < blen; i ++) { + if (buf && wlen < buflen) { + buf[wlen] = bstr[i]; + } + wlen ++; + } + return wlen; +} +#ifdef __cplusplus +} +#endif +#endif diff --git a/core/src/fxcrt/fx_basic_list.cpp b/core/src/fxcrt/fx_basic_list.cpp index 92b3d2c0c4..bf7091268b 100644 --- a/core/src/fxcrt/fx_basic_list.cpp +++ b/core/src/fxcrt/fx_basic_list.cpp @@ -1,141 +1,141 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_basic.h"
-#include "plex.h"
-CFX_PtrList::CFX_PtrList(int nBlockSize, IFX_Allocator* pAllocator)
- : m_pAllocator(pAllocator)
- , m_pNodeHead(NULL)
- , m_pNodeTail(NULL)
- , m_nCount(0)
- , m_pNodeFree(NULL)
- , m_pBlocks(NULL)
- , m_nBlockSize(nBlockSize)
-{
-}
-FX_POSITION CFX_PtrList::AddTail(void* newElement)
-{
- CNode* pNewNode = NewNode(m_pNodeTail, NULL);
- pNewNode->data = newElement;
- if (m_pNodeTail != NULL) {
- m_pNodeTail->pNext = pNewNode;
- } else {
- m_pNodeHead = pNewNode;
- }
- m_pNodeTail = pNewNode;
- return (FX_POSITION) pNewNode;
-}
-FX_POSITION CFX_PtrList::AddHead(void* newElement)
-{
- CNode* pNewNode = NewNode(NULL, m_pNodeHead);
- pNewNode->data = newElement;
- if (m_pNodeHead != NULL) {
- m_pNodeHead->pPrev = pNewNode;
- } else {
- m_pNodeTail = pNewNode;
- }
- m_pNodeHead = pNewNode;
- return (FX_POSITION) pNewNode;
-}
-FX_POSITION CFX_PtrList::InsertAfter(FX_POSITION position, void* newElement)
-{
- if (position == NULL) {
- return AddTail(newElement);
- }
- CNode* pOldNode = (CNode*) position;
- CNode* pNewNode = NewNode(pOldNode, pOldNode->pNext);
- pNewNode->data = newElement;
- if (pOldNode->pNext != NULL) {
- pOldNode->pNext->pPrev = pNewNode;
- } else {
- m_pNodeTail = pNewNode;
- }
- pOldNode->pNext = pNewNode;
- return (FX_POSITION) pNewNode;
-}
-void CFX_PtrList::RemoveAt(FX_POSITION position)
-{
- CNode* pOldNode = (CNode*) position;
- if (pOldNode == m_pNodeHead) {
- m_pNodeHead = pOldNode->pNext;
- } else {
- pOldNode->pPrev->pNext = pOldNode->pNext;
- }
- if (pOldNode == m_pNodeTail) {
- m_pNodeTail = pOldNode->pPrev;
- } else {
- pOldNode->pNext->pPrev = pOldNode->pPrev;
- }
- FreeNode(pOldNode);
-}
-void CFX_PtrList::FreeNode(CFX_PtrList::CNode* pNode)
-{
- pNode->pNext = m_pNodeFree;
- m_pNodeFree = pNode;
- m_nCount--;
- if (m_nCount == 0) {
- RemoveAll();
- }
-}
-void CFX_PtrList::RemoveAll()
-{
- m_nCount = 0;
- m_pNodeHead = m_pNodeTail = m_pNodeFree = NULL;
- m_pBlocks->FreeDataChain(m_pAllocator);
- m_pBlocks = NULL;
-}
-CFX_PtrList::CNode*
-CFX_PtrList::NewNode(CFX_PtrList::CNode* pPrev, CFX_PtrList::CNode* pNext)
-{
- if (m_pNodeFree == NULL) {
- CFX_Plex* pNewBlock = CFX_Plex::Create(m_pAllocator, m_pBlocks, m_nBlockSize, sizeof(CNode));
- CNode* pNode = (CNode*)pNewBlock->data();
- pNode += m_nBlockSize - 1;
- for (int i = m_nBlockSize - 1; i >= 0; i--, pNode--) {
- pNode->pNext = m_pNodeFree;
- m_pNodeFree = pNode;
- }
- }
- ASSERT(m_pNodeFree != NULL);
- CFX_PtrList::CNode* pNode = m_pNodeFree;
- m_pNodeFree = m_pNodeFree->pNext;
- pNode->pPrev = pPrev;
- pNode->pNext = pNext;
- m_nCount++;
- ASSERT(m_nCount > 0);
- pNode->data = 0;
- return pNode;
-}
-CFX_PtrList::~CFX_PtrList()
-{
- RemoveAll();
- ASSERT(m_nCount == 0);
-}
-FX_POSITION CFX_PtrList::FindIndex(int nIndex) const
-{
- if (nIndex >= m_nCount || nIndex < 0) {
- return NULL;
- }
- CNode* pNode = m_pNodeHead;
- while (nIndex--) {
- pNode = pNode->pNext;
- }
- return (FX_POSITION) pNode;
-}
-FX_POSITION CFX_PtrList::Find(void* searchValue, FX_POSITION startAfter) const
-{
- CNode* pNode = (CNode*) startAfter;
- if (pNode == NULL) {
- pNode = m_pNodeHead;
- } else {
- pNode = pNode->pNext;
- }
- for (; pNode != NULL; pNode = pNode->pNext)
- if (pNode->data == searchValue) {
- return (FX_POSITION) pNode;
- }
- return NULL;
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_basic.h" +#include "plex.h" +CFX_PtrList::CFX_PtrList(int nBlockSize, IFX_Allocator* pAllocator) + : m_pAllocator(pAllocator) + , m_pNodeHead(NULL) + , m_pNodeTail(NULL) + , m_nCount(0) + , m_pNodeFree(NULL) + , m_pBlocks(NULL) + , m_nBlockSize(nBlockSize) +{ +} +FX_POSITION CFX_PtrList::AddTail(void* newElement) +{ + CNode* pNewNode = NewNode(m_pNodeTail, NULL); + pNewNode->data = newElement; + if (m_pNodeTail != NULL) { + m_pNodeTail->pNext = pNewNode; + } else { + m_pNodeHead = pNewNode; + } + m_pNodeTail = pNewNode; + return (FX_POSITION) pNewNode; +} +FX_POSITION CFX_PtrList::AddHead(void* newElement) +{ + CNode* pNewNode = NewNode(NULL, m_pNodeHead); + pNewNode->data = newElement; + if (m_pNodeHead != NULL) { + m_pNodeHead->pPrev = pNewNode; + } else { + m_pNodeTail = pNewNode; + } + m_pNodeHead = pNewNode; + return (FX_POSITION) pNewNode; +} +FX_POSITION CFX_PtrList::InsertAfter(FX_POSITION position, void* newElement) +{ + if (position == NULL) { + return AddTail(newElement); + } + CNode* pOldNode = (CNode*) position; + CNode* pNewNode = NewNode(pOldNode, pOldNode->pNext); + pNewNode->data = newElement; + if (pOldNode->pNext != NULL) { + pOldNode->pNext->pPrev = pNewNode; + } else { + m_pNodeTail = pNewNode; + } + pOldNode->pNext = pNewNode; + return (FX_POSITION) pNewNode; +} +void CFX_PtrList::RemoveAt(FX_POSITION position) +{ + CNode* pOldNode = (CNode*) position; + if (pOldNode == m_pNodeHead) { + m_pNodeHead = pOldNode->pNext; + } else { + pOldNode->pPrev->pNext = pOldNode->pNext; + } + if (pOldNode == m_pNodeTail) { + m_pNodeTail = pOldNode->pPrev; + } else { + pOldNode->pNext->pPrev = pOldNode->pPrev; + } + FreeNode(pOldNode); +} +void CFX_PtrList::FreeNode(CFX_PtrList::CNode* pNode) +{ + pNode->pNext = m_pNodeFree; + m_pNodeFree = pNode; + m_nCount--; + if (m_nCount == 0) { + RemoveAll(); + } +} +void CFX_PtrList::RemoveAll() +{ + m_nCount = 0; + m_pNodeHead = m_pNodeTail = m_pNodeFree = NULL; + m_pBlocks->FreeDataChain(m_pAllocator); + m_pBlocks = NULL; +} +CFX_PtrList::CNode* +CFX_PtrList::NewNode(CFX_PtrList::CNode* pPrev, CFX_PtrList::CNode* pNext) +{ + if (m_pNodeFree == NULL) { + CFX_Plex* pNewBlock = CFX_Plex::Create(m_pAllocator, m_pBlocks, m_nBlockSize, sizeof(CNode)); + CNode* pNode = (CNode*)pNewBlock->data(); + pNode += m_nBlockSize - 1; + for (int i = m_nBlockSize - 1; i >= 0; i--, pNode--) { + pNode->pNext = m_pNodeFree; + m_pNodeFree = pNode; + } + } + ASSERT(m_pNodeFree != NULL); + CFX_PtrList::CNode* pNode = m_pNodeFree; + m_pNodeFree = m_pNodeFree->pNext; + pNode->pPrev = pPrev; + pNode->pNext = pNext; + m_nCount++; + ASSERT(m_nCount > 0); + pNode->data = 0; + return pNode; +} +CFX_PtrList::~CFX_PtrList() +{ + RemoveAll(); + ASSERT(m_nCount == 0); +} +FX_POSITION CFX_PtrList::FindIndex(int nIndex) const +{ + if (nIndex >= m_nCount || nIndex < 0) { + return NULL; + } + CNode* pNode = m_pNodeHead; + while (nIndex--) { + pNode = pNode->pNext; + } + return (FX_POSITION) pNode; +} +FX_POSITION CFX_PtrList::Find(void* searchValue, FX_POSITION startAfter) const +{ + CNode* pNode = (CNode*) startAfter; + if (pNode == NULL) { + pNode = m_pNodeHead; + } else { + pNode = pNode->pNext; + } + for (; pNode != NULL; pNode = pNode->pNext) + if (pNode->data == searchValue) { + return (FX_POSITION) pNode; + } + return NULL; +} diff --git a/core/src/fxcrt/fx_basic_maps.cpp b/core/src/fxcrt/fx_basic_maps.cpp index cb397ee8bd..a0b1788a82 100644 --- a/core/src/fxcrt/fx_basic_maps.cpp +++ b/core/src/fxcrt/fx_basic_maps.cpp @@ -1,654 +1,654 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_ext.h"
-#include "plex.h"
-static void ConstructElement(CFX_ByteString* pNewData)
-{
- new (pNewData) CFX_ByteString();
-}
-static void DestructElement(CFX_ByteString* pOldData)
-{
- pOldData->~CFX_ByteString();
-}
-CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize, IFX_Allocator* pAllocator)
- : m_pAllocator(pAllocator)
- , m_pHashTable(NULL)
- , m_nHashTableSize(17)
- , m_nCount(0)
- , m_pFreeList(NULL)
- , m_pBlocks(NULL)
- , m_nBlockSize(nBlockSize)
-{
- ASSERT(m_nBlockSize > 0);
-}
-void CFX_MapPtrToPtr::RemoveAll()
-{
- if (m_pHashTable) {
- FX_Allocator_Free(m_pAllocator, m_pHashTable);
- m_pHashTable = NULL;
- }
- m_nCount = 0;
- m_pFreeList = NULL;
- m_pBlocks->FreeDataChain(m_pAllocator);
- m_pBlocks = NULL;
-}
-CFX_MapPtrToPtr::~CFX_MapPtrToPtr()
-{
- RemoveAll();
- ASSERT(m_nCount == 0);
-}
-FX_DWORD CFX_MapPtrToPtr::HashKey(void* key) const
-{
- return ((FX_DWORD)(FX_UINTPTR)key) >> 4;
-}
-void CFX_MapPtrToPtr::GetNextAssoc(FX_POSITION& rNextPosition, void*& rKey, void*& rValue) const
-{
- ASSERT(m_pHashTable != NULL);
- CAssoc* pAssocRet = (CAssoc*)rNextPosition;
- ASSERT(pAssocRet != NULL);
- if (pAssocRet == (CAssoc*) - 1) {
- for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++)
- if ((pAssocRet = m_pHashTable[nBucket]) != NULL) {
- break;
- }
- ASSERT(pAssocRet != NULL);
- }
- CAssoc* pAssocNext;
- if ((pAssocNext = pAssocRet->pNext) == NULL) {
- for (FX_DWORD nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1; nBucket < m_nHashTableSize; nBucket ++) {
- if ((pAssocNext = m_pHashTable[nBucket]) != NULL) {
- break;
- }
- }
- }
- rNextPosition = (FX_POSITION) pAssocNext;
- rKey = pAssocRet->key;
- rValue = pAssocRet->value;
-}
-FX_BOOL CFX_MapPtrToPtr::Lookup(void* key, void*& rValue) const
-{
- FX_DWORD nHash;
- CAssoc* pAssoc = GetAssocAt(key, nHash);
- if (pAssoc == NULL) {
- return FALSE;
- }
- rValue = pAssoc->value;
- return TRUE;
-}
-void* CFX_MapPtrToPtr::GetValueAt(void* key) const
-{
- FX_DWORD nHash;
- CAssoc* pAssoc = GetAssocAt(key, nHash);
- if (pAssoc == NULL) {
- return NULL;
- }
- return pAssoc->value;
-}
-void*& CFX_MapPtrToPtr::operator[](void* key)
-{
- FX_DWORD nHash;
- CAssoc* pAssoc;
- if ((pAssoc = GetAssocAt(key, nHash)) == NULL) {
- if (m_pHashTable == NULL) {
- InitHashTable(m_nHashTableSize);
- }
- pAssoc = NewAssoc();
- pAssoc->key = key;
- pAssoc->pNext = m_pHashTable[nHash];
- m_pHashTable[nHash] = pAssoc;
- }
- return pAssoc->value;
-}
-CFX_MapPtrToPtr::CAssoc*
-CFX_MapPtrToPtr::GetAssocAt(void* key, FX_DWORD& nHash) const
-{
- nHash = HashKey(key) % m_nHashTableSize;
- if (m_pHashTable == NULL) {
- return NULL;
- }
- CAssoc* pAssoc;
- for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext) {
- if (pAssoc->key == key) {
- return pAssoc;
- }
- }
- return NULL;
-}
-CFX_MapPtrToPtr::CAssoc*
-CFX_MapPtrToPtr::NewAssoc()
-{
- if (m_pFreeList == NULL) {
- CFX_Plex* newBlock = CFX_Plex::Create(m_pAllocator, m_pBlocks, m_nBlockSize, sizeof(CFX_MapPtrToPtr::CAssoc));
- CFX_MapPtrToPtr::CAssoc* pAssoc = (CFX_MapPtrToPtr::CAssoc*)newBlock->data();
- pAssoc += m_nBlockSize - 1;
- for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) {
- pAssoc->pNext = m_pFreeList;
- m_pFreeList = pAssoc;
- }
- }
- ASSERT(m_pFreeList != NULL);
- CFX_MapPtrToPtr::CAssoc* pAssoc = m_pFreeList;
- m_pFreeList = m_pFreeList->pNext;
- m_nCount++;
- ASSERT(m_nCount > 0);
- pAssoc->key = 0;
- pAssoc->value = 0;
- return pAssoc;
-}
-void CFX_MapPtrToPtr::InitHashTable(
- FX_DWORD nHashSize, FX_BOOL bAllocNow)
-{
- ASSERT(m_nCount == 0);
- ASSERT(nHashSize > 0);
- if (m_pHashTable != NULL) {
- FX_Allocator_Free(m_pAllocator, m_pHashTable);
- m_pHashTable = NULL;
- }
- if (bAllocNow) {
- m_pHashTable = FX_Allocator_Alloc(m_pAllocator, CAssoc*, nHashSize);
- if (m_pHashTable) {
- FXSYS_memset32(m_pHashTable, 0, sizeof(CAssoc*) * nHashSize);
- }
- }
- m_nHashTableSize = nHashSize;
-}
-FX_BOOL CFX_MapPtrToPtr::RemoveKey(void* key)
-{
- if (m_pHashTable == NULL) {
- return FALSE;
- }
- CAssoc** ppAssocPrev;
- ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize];
- CAssoc* pAssoc;
- for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext) {
- if (pAssoc->key == key) {
- *ppAssocPrev = pAssoc->pNext;
- FreeAssoc(pAssoc);
- return TRUE;
- }
- ppAssocPrev = &pAssoc->pNext;
- }
- return FALSE;
-}
-void CFX_MapPtrToPtr::FreeAssoc(CFX_MapPtrToPtr::CAssoc* pAssoc)
-{
- pAssoc->pNext = m_pFreeList;
- m_pFreeList = pAssoc;
- m_nCount--;
- ASSERT(m_nCount >= 0);
- if (m_nCount == 0) {
- RemoveAll();
- }
-}
-CFX_MapByteStringToPtr::CFX_MapByteStringToPtr(int nBlockSize, IFX_Allocator* pAllocator)
- : m_pAllocator(pAllocator)
- , m_pHashTable(NULL)
- , m_nHashTableSize(17)
- , m_nCount(0)
- , m_pFreeList(NULL)
- , m_pBlocks(NULL)
- , m_nBlockSize(nBlockSize)
-{
- ASSERT(m_nBlockSize > 0);
-}
-void CFX_MapByteStringToPtr::RemoveAll()
-{
- if (m_pHashTable != NULL) {
- for (FX_DWORD nHash = 0; nHash < m_nHashTableSize; nHash++) {
- CAssoc* pAssoc;
- for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL;
- pAssoc = pAssoc->pNext) {
- DestructElement(&pAssoc->key);
- }
- }
- FX_Allocator_Free(m_pAllocator, m_pHashTable);
- m_pHashTable = NULL;
- }
- m_nCount = 0;
- m_pFreeList = NULL;
- m_pBlocks->FreeDataChain(m_pAllocator);
- m_pBlocks = NULL;
-}
-CFX_MapByteStringToPtr::~CFX_MapByteStringToPtr()
-{
- RemoveAll();
- ASSERT(m_nCount == 0);
-}
-void CFX_MapByteStringToPtr::GetNextAssoc(FX_POSITION& rNextPosition,
- CFX_ByteString& rKey, void*& rValue) const
-{
- ASSERT(m_pHashTable != NULL);
- CAssoc* pAssocRet = (CAssoc*)rNextPosition;
- ASSERT(pAssocRet != NULL);
- if (pAssocRet == (CAssoc*) - 1) {
- for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++)
- if ((pAssocRet = m_pHashTable[nBucket]) != NULL) {
- break;
- }
- ASSERT(pAssocRet != NULL);
- }
- CAssoc* pAssocNext;
- if ((pAssocNext = pAssocRet->pNext) == NULL) {
- for (FX_DWORD nBucket = pAssocRet->nHashValue + 1;
- nBucket < m_nHashTableSize; nBucket++)
- if ((pAssocNext = m_pHashTable[nBucket]) != NULL) {
- break;
- }
- }
- rNextPosition = (FX_POSITION) pAssocNext;
- rKey = pAssocRet->key;
- rValue = pAssocRet->value;
-}
-FX_LPVOID CFX_MapByteStringToPtr::GetNextValue(FX_POSITION& rNextPosition) const
-{
- ASSERT(m_pHashTable != NULL);
- CAssoc* pAssocRet = (CAssoc*)rNextPosition;
- ASSERT(pAssocRet != NULL);
- if (pAssocRet == (CAssoc*) - 1) {
- for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++)
- if ((pAssocRet = m_pHashTable[nBucket]) != NULL) {
- break;
- }
- ASSERT(pAssocRet != NULL);
- }
- CAssoc* pAssocNext;
- if ((pAssocNext = pAssocRet->pNext) == NULL) {
- for (FX_DWORD nBucket = pAssocRet->nHashValue + 1;
- nBucket < m_nHashTableSize; nBucket++)
- if ((pAssocNext = m_pHashTable[nBucket]) != NULL) {
- break;
- }
- }
- rNextPosition = (FX_POSITION) pAssocNext;
- return pAssocRet->value;
-}
-void*& CFX_MapByteStringToPtr::operator[](FX_BSTR key)
-{
- FX_DWORD nHash;
- CAssoc* pAssoc;
- if ((pAssoc = GetAssocAt(key, nHash)) == NULL) {
- if (m_pHashTable == NULL) {
- InitHashTable(m_nHashTableSize);
- }
- pAssoc = NewAssoc();
- pAssoc->nHashValue = nHash;
- pAssoc->key = key;
- pAssoc->pNext = m_pHashTable[nHash];
- m_pHashTable[nHash] = pAssoc;
- }
- return pAssoc->value;
-}
-CFX_MapByteStringToPtr::CAssoc*
-CFX_MapByteStringToPtr::NewAssoc()
-{
- if (m_pFreeList == NULL) {
- CFX_Plex* newBlock = CFX_Plex::Create(m_pAllocator, m_pBlocks, m_nBlockSize, sizeof(CFX_MapByteStringToPtr::CAssoc));
- CFX_MapByteStringToPtr::CAssoc* pAssoc = (CFX_MapByteStringToPtr::CAssoc*)newBlock->data();
- pAssoc += m_nBlockSize - 1;
- for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) {
- pAssoc->pNext = m_pFreeList;
- m_pFreeList = pAssoc;
- }
- }
- ASSERT(m_pFreeList != NULL);
- CFX_MapByteStringToPtr::CAssoc* pAssoc = m_pFreeList;
- m_pFreeList = m_pFreeList->pNext;
- m_nCount++;
- ASSERT(m_nCount > 0);
- ConstructElement(&pAssoc->key);
- pAssoc->value = 0;
- return pAssoc;
-}
-void CFX_MapByteStringToPtr::FreeAssoc(CFX_MapByteStringToPtr::CAssoc* pAssoc)
-{
- DestructElement(&pAssoc->key);
- pAssoc->pNext = m_pFreeList;
- m_pFreeList = pAssoc;
- m_nCount--;
- ASSERT(m_nCount >= 0);
- if (m_nCount == 0) {
- RemoveAll();
- }
-}
-CFX_MapByteStringToPtr::CAssoc*
-CFX_MapByteStringToPtr::GetAssocAt(FX_BSTR key, FX_DWORD& nHash) const
-{
- nHash = HashKey(key) % m_nHashTableSize;
- if (m_pHashTable == NULL) {
- return NULL;
- }
- CAssoc* pAssoc;
- for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext) {
- if (pAssoc->key == key) {
- return pAssoc;
- }
- }
- return NULL;
-}
-FX_BOOL CFX_MapByteStringToPtr::Lookup(FX_BSTR key, void*& rValue) const
-{
- FX_DWORD nHash;
- CAssoc* pAssoc = GetAssocAt(key, nHash);
- if (pAssoc == NULL) {
- return FALSE;
- }
- rValue = pAssoc->value;
- return TRUE;
-}
-void CFX_MapByteStringToPtr::InitHashTable(
- FX_DWORD nHashSize, FX_BOOL bAllocNow)
-{
- ASSERT(m_nCount == 0);
- ASSERT(nHashSize > 0);
- if (m_pHashTable != NULL) {
- FX_Allocator_Free(m_pAllocator, m_pHashTable);
- m_pHashTable = NULL;
- }
- if (bAllocNow) {
- m_pHashTable = FX_Allocator_Alloc(m_pAllocator, CAssoc*, nHashSize);
- if (m_pHashTable) {
- FXSYS_memset32(m_pHashTable, 0, sizeof(CAssoc*) * nHashSize);
- }
- }
- m_nHashTableSize = nHashSize;
-}
-inline FX_DWORD CFX_MapByteStringToPtr::HashKey(FX_BSTR key) const
-{
- FX_DWORD nHash = 0;
- int len = key.GetLength();
- FX_LPCBYTE buf = key;
- for (int i = 0; i < len; i ++) {
- nHash = (nHash << 5) + nHash + buf[i];
- }
- return nHash;
-}
-FX_BOOL CFX_MapByteStringToPtr::RemoveKey(FX_BSTR key)
-{
- if (m_pHashTable == NULL) {
- return FALSE;
- }
- CAssoc** ppAssocPrev;
- ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize];
- CAssoc* pAssoc;
- for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext) {
- if (pAssoc->key == key) {
- *ppAssocPrev = pAssoc->pNext;
- FreeAssoc(pAssoc);
- return TRUE;
- }
- ppAssocPrev = &pAssoc->pNext;
- }
- return FALSE;
-}
-struct _CompactString {
- FX_BYTE m_CompactLen;
- FX_BYTE m_LenHigh;
- FX_BYTE m_LenLow;
- FX_BYTE m_Unused;
- FX_LPBYTE m_pBuffer;
-};
-static void _CompactStringRelease(IFX_Allocator* pAllocator, _CompactString* pCompact)
-{
- if (pCompact->m_CompactLen == 0xff) {
- FX_Allocator_Free(pAllocator, pCompact->m_pBuffer);
- }
-}
-static FX_BOOL _CompactStringSame(_CompactString* pCompact, FX_LPCBYTE pStr, int len)
-{
- if (len < sizeof(_CompactString)) {
- if (pCompact->m_CompactLen != len) {
- return FALSE;
- }
- return FXSYS_memcmp32(&pCompact->m_LenHigh, pStr, len) == 0;
- }
- if (pCompact->m_CompactLen != 0xff || pCompact->m_LenHigh * 256 + pCompact->m_LenLow != len) {
- return FALSE;
- }
- return FXSYS_memcmp32(pCompact->m_pBuffer, pStr, len) == 0;
-}
-static void _CompactStringStore(IFX_Allocator* pAllocator, _CompactString* pCompact, FX_LPCBYTE pStr, int len)
-{
- if (len < (int)sizeof(_CompactString)) {
- pCompact->m_CompactLen = (FX_BYTE)len;
- FXSYS_memcpy32(&pCompact->m_LenHigh, pStr, len);
- return;
- }
- pCompact->m_CompactLen = 0xff;
- pCompact->m_LenHigh = len / 256;
- pCompact->m_LenLow = len % 256;
- pCompact->m_pBuffer = FX_Allocator_Alloc(pAllocator, FX_BYTE, len);
- if (pCompact->m_pBuffer) {
- FXSYS_memcpy32(pCompact->m_pBuffer, pStr, len);
- }
-}
-static CFX_ByteStringC _CompactStringGet(_CompactString* pCompact)
-{
- if (pCompact->m_CompactLen == 0xff) {
- return CFX_ByteStringC(pCompact->m_pBuffer, pCompact->m_LenHigh * 256 + pCompact->m_LenLow);
- }
- if (pCompact->m_CompactLen == 0xfe) {
- return CFX_ByteStringC();
- }
- return CFX_ByteStringC(&pCompact->m_LenHigh, pCompact->m_CompactLen);
-}
-#define CMAP_ALLOC_STEP 8
-#define CMAP_INDEX_SIZE 8
-CFX_CMapByteStringToPtr::CFX_CMapByteStringToPtr(IFX_Allocator* pAllocator)
- : m_Buffer(sizeof(_CompactString) + sizeof(void*), CMAP_ALLOC_STEP, CMAP_INDEX_SIZE, pAllocator)
-{
-}
-CFX_CMapByteStringToPtr::~CFX_CMapByteStringToPtr()
-{
- RemoveAll();
-}
-void CFX_CMapByteStringToPtr::RemoveAll()
-{
- IFX_Allocator* pAllocator = m_Buffer.m_pAllocator;
- int size = m_Buffer.GetSize();
- for (int i = 0; i < size; i ++) {
- _CompactStringRelease(pAllocator, (_CompactString*)m_Buffer.GetAt(i));
- }
- m_Buffer.RemoveAll();
-}
-FX_POSITION CFX_CMapByteStringToPtr::GetStartPosition() const
-{
- int size = m_Buffer.GetSize();
- for (int i = 0; i < size; i ++) {
- _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i);
- if (pKey->m_CompactLen != 0xfe) {
- return (FX_POSITION)(FX_UINTPTR)(i + 1);
- }
- }
- return NULL;
-}
-void CFX_CMapByteStringToPtr::GetNextAssoc(FX_POSITION& rNextPosition, CFX_ByteString& rKey, void*& rValue) const
-{
- if (rNextPosition == NULL) {
- return;
- }
- int index = (int)(FX_UINTPTR)rNextPosition - 1;
- _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
- rKey = _CompactStringGet(pKey);
- rValue = *(void**)(pKey + 1);
- index ++;
- int size = m_Buffer.GetSize();
- while (index < size) {
- pKey = (_CompactString*)m_Buffer.GetAt(index);
- if (pKey->m_CompactLen != 0xfe) {
- rNextPosition = (FX_POSITION)(FX_UINTPTR)(index + 1);
- return;
- }
- index ++;
- }
- rNextPosition = NULL;
-}
-FX_LPVOID CFX_CMapByteStringToPtr::GetNextValue(FX_POSITION& rNextPosition) const
-{
- if (rNextPosition == NULL) {
- return NULL;
- }
- int index = (int)(FX_UINTPTR)rNextPosition - 1;
- _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
- FX_LPVOID rValue = *(void**)(pKey + 1);
- index ++;
- int size = m_Buffer.GetSize();
- while (index < size) {
- pKey = (_CompactString*)m_Buffer.GetAt(index);
- if (pKey->m_CompactLen != 0xfe) {
- rNextPosition = (FX_POSITION)(FX_UINTPTR)(index + 1);
- return rValue;
- }
- index ++;
- }
- rNextPosition = NULL;
- return rValue;
-}
-FX_BOOL _CMapLookupCallback(void* param, void* pData)
-{
- return !_CompactStringSame((_CompactString*)pData, ((CFX_ByteStringC*)param)->GetPtr(), ((CFX_ByteStringC*)param)->GetLength());
-}
-FX_BOOL CFX_CMapByteStringToPtr::Lookup(FX_BSTR key, void*& rValue) const
-{
- void* p = m_Buffer.Iterate(_CMapLookupCallback, (void*)&key);
- if (!p) {
- return FALSE;
- }
- rValue = *(void**)((_CompactString*)p + 1);
- return TRUE;
-}
-void CFX_CMapByteStringToPtr::SetAt(FX_BSTR key, void* value)
-{
- ASSERT(value != NULL);
- int index, key_len = key.GetLength();
- int size = m_Buffer.GetSize();
- for (index = 0; index < size; index ++) {
- _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
- if (!_CompactStringSame(pKey, (FX_LPCBYTE)key, key_len)) {
- continue;
- }
- *(void**)(pKey + 1) = value;
- return;
- }
- IFX_Allocator* pAllocator = m_Buffer.m_pAllocator;
- for (index = 0; index < size; index ++) {
- _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
- if (pKey->m_CompactLen) {
- continue;
- }
- _CompactStringStore(pAllocator, pKey, (FX_LPCBYTE)key, key_len);
- *(void**)(pKey + 1) = value;
- return;
- }
- _CompactString* pKey = (_CompactString*)m_Buffer.Add();
- _CompactStringStore(pAllocator, pKey, (FX_LPCBYTE)key, key_len);
- *(void**)(pKey + 1) = value;
-}
-void CFX_CMapByteStringToPtr::AddValue(FX_BSTR key, void* value)
-{
- ASSERT(value != NULL);
- _CompactString* pKey = (_CompactString*)m_Buffer.Add();
- _CompactStringStore(m_Buffer.m_pAllocator, pKey, (FX_LPCBYTE)key, key.GetLength());
- *(void**)(pKey + 1) = value;
-}
-void CFX_CMapByteStringToPtr::RemoveKey(FX_BSTR key)
-{
- int key_len = key.GetLength();
- IFX_Allocator* pAllocator = m_Buffer.m_pAllocator;
- int size = m_Buffer.GetSize();
- for (int index = 0; index < size; index ++) {
- _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
- if (!_CompactStringSame(pKey, (FX_LPCBYTE)key, key_len)) {
- continue;
- }
- _CompactStringRelease(pAllocator, pKey);
- pKey->m_CompactLen = 0xfe;
- return;
- }
-}
-int CFX_CMapByteStringToPtr::GetCount() const
-{
- int count = 0;
- int size = m_Buffer.GetSize();
- for (int i = 0; i < size; i ++) {
- _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i);
- if (pKey->m_CompactLen != 0xfe) {
- count ++;
- }
- }
- return count;
-}
-extern "C" {
- static int _CompareDWord(const void* p1, const void* p2)
- {
- return (*(FX_DWORD*)p1) - (*(FX_DWORD*)p2);
- }
-};
-struct _DWordPair {
- FX_DWORD key;
- FX_DWORD value;
-};
-FX_BOOL CFX_CMapDWordToDWord::Lookup(FX_DWORD key, FX_DWORD& value) const
-{
- FX_LPVOID pResult = FXSYS_bsearch(&key, m_Buffer.GetBuffer(), m_Buffer.GetSize() / sizeof(_DWordPair),
- sizeof(_DWordPair), _CompareDWord);
- if (pResult == NULL) {
- return FALSE;
- }
- value = ((FX_DWORD*)pResult)[1];
- return TRUE;
-}
-FX_POSITION CFX_CMapDWordToDWord::GetStartPosition() const
-{
- FX_DWORD count = m_Buffer.GetSize() / sizeof(_DWordPair);
- if (count == 0) {
- return NULL;
- }
- return (FX_POSITION)1;
-}
-void CFX_CMapDWordToDWord::GetNextAssoc(FX_POSITION& pos, FX_DWORD& key, FX_DWORD& value) const
-{
- if (pos == 0) {
- return;
- }
- FX_DWORD index = ((FX_DWORD)(FX_UINTPTR)pos) - 1;
- FX_DWORD count = m_Buffer.GetSize() / sizeof(_DWordPair);
- _DWordPair* buf = (_DWordPair*)m_Buffer.GetBuffer();
- key = buf[index].key;
- value = buf[index].value;
- if (index == count - 1) {
- pos = 0;
- } else {
- pos = (FX_POSITION)((FX_UINTPTR)pos + 1);
- }
-}
-void CFX_CMapDWordToDWord::SetAt(FX_DWORD key, FX_DWORD value)
-{
- FX_DWORD count = m_Buffer.GetSize() / sizeof(_DWordPair);
- _DWordPair* buf = (_DWordPair*)m_Buffer.GetBuffer();
- _DWordPair pair = {key, value};
- if (count == 0 || key > buf[count - 1].key) {
- m_Buffer.AppendBlock(&pair, sizeof(_DWordPair));
- return;
- }
- int low = 0, high = count - 1;
- while (low <= high) {
- int mid = (low + high) / 2;
- if (buf[mid].key < key) {
- low = mid + 1;
- } else if (buf[mid].key > key) {
- high = mid - 1;
- } else {
- buf[mid].value = value;
- return;
- }
- }
- m_Buffer.InsertBlock(low * sizeof(_DWordPair), &pair, sizeof(_DWordPair));
-}
-void CFX_CMapDWordToDWord::EstimateSize(FX_DWORD size, FX_DWORD grow_by)
-{
- m_Buffer.EstimateSize(size, grow_by);
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_ext.h" +#include "plex.h" +static void ConstructElement(CFX_ByteString* pNewData) +{ + new (pNewData) CFX_ByteString(); +} +static void DestructElement(CFX_ByteString* pOldData) +{ + pOldData->~CFX_ByteString(); +} +CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize, IFX_Allocator* pAllocator) + : m_pAllocator(pAllocator) + , m_pHashTable(NULL) + , m_nHashTableSize(17) + , m_nCount(0) + , m_pFreeList(NULL) + , m_pBlocks(NULL) + , m_nBlockSize(nBlockSize) +{ + ASSERT(m_nBlockSize > 0); +} +void CFX_MapPtrToPtr::RemoveAll() +{ + if (m_pHashTable) { + FX_Allocator_Free(m_pAllocator, m_pHashTable); + m_pHashTable = NULL; + } + m_nCount = 0; + m_pFreeList = NULL; + m_pBlocks->FreeDataChain(m_pAllocator); + m_pBlocks = NULL; +} +CFX_MapPtrToPtr::~CFX_MapPtrToPtr() +{ + RemoveAll(); + ASSERT(m_nCount == 0); +} +FX_DWORD CFX_MapPtrToPtr::HashKey(void* key) const +{ + return ((FX_DWORD)(FX_UINTPTR)key) >> 4; +} +void CFX_MapPtrToPtr::GetNextAssoc(FX_POSITION& rNextPosition, void*& rKey, void*& rValue) const +{ + ASSERT(m_pHashTable != NULL); + CAssoc* pAssocRet = (CAssoc*)rNextPosition; + ASSERT(pAssocRet != NULL); + if (pAssocRet == (CAssoc*) - 1) { + for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++) + if ((pAssocRet = m_pHashTable[nBucket]) != NULL) { + break; + } + ASSERT(pAssocRet != NULL); + } + CAssoc* pAssocNext; + if ((pAssocNext = pAssocRet->pNext) == NULL) { + for (FX_DWORD nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1; nBucket < m_nHashTableSize; nBucket ++) { + if ((pAssocNext = m_pHashTable[nBucket]) != NULL) { + break; + } + } + } + rNextPosition = (FX_POSITION) pAssocNext; + rKey = pAssocRet->key; + rValue = pAssocRet->value; +} +FX_BOOL CFX_MapPtrToPtr::Lookup(void* key, void*& rValue) const +{ + FX_DWORD nHash; + CAssoc* pAssoc = GetAssocAt(key, nHash); + if (pAssoc == NULL) { + return FALSE; + } + rValue = pAssoc->value; + return TRUE; +} +void* CFX_MapPtrToPtr::GetValueAt(void* key) const +{ + FX_DWORD nHash; + CAssoc* pAssoc = GetAssocAt(key, nHash); + if (pAssoc == NULL) { + return NULL; + } + return pAssoc->value; +} +void*& CFX_MapPtrToPtr::operator[](void* key) +{ + FX_DWORD nHash; + CAssoc* pAssoc; + if ((pAssoc = GetAssocAt(key, nHash)) == NULL) { + if (m_pHashTable == NULL) { + InitHashTable(m_nHashTableSize); + } + pAssoc = NewAssoc(); + pAssoc->key = key; + pAssoc->pNext = m_pHashTable[nHash]; + m_pHashTable[nHash] = pAssoc; + } + return pAssoc->value; +} +CFX_MapPtrToPtr::CAssoc* +CFX_MapPtrToPtr::GetAssocAt(void* key, FX_DWORD& nHash) const +{ + nHash = HashKey(key) % m_nHashTableSize; + if (m_pHashTable == NULL) { + return NULL; + } + CAssoc* pAssoc; + for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext) { + if (pAssoc->key == key) { + return pAssoc; + } + } + return NULL; +} +CFX_MapPtrToPtr::CAssoc* +CFX_MapPtrToPtr::NewAssoc() +{ + if (m_pFreeList == NULL) { + CFX_Plex* newBlock = CFX_Plex::Create(m_pAllocator, m_pBlocks, m_nBlockSize, sizeof(CFX_MapPtrToPtr::CAssoc)); + CFX_MapPtrToPtr::CAssoc* pAssoc = (CFX_MapPtrToPtr::CAssoc*)newBlock->data(); + pAssoc += m_nBlockSize - 1; + for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) { + pAssoc->pNext = m_pFreeList; + m_pFreeList = pAssoc; + } + } + ASSERT(m_pFreeList != NULL); + CFX_MapPtrToPtr::CAssoc* pAssoc = m_pFreeList; + m_pFreeList = m_pFreeList->pNext; + m_nCount++; + ASSERT(m_nCount > 0); + pAssoc->key = 0; + pAssoc->value = 0; + return pAssoc; +} +void CFX_MapPtrToPtr::InitHashTable( + FX_DWORD nHashSize, FX_BOOL bAllocNow) +{ + ASSERT(m_nCount == 0); + ASSERT(nHashSize > 0); + if (m_pHashTable != NULL) { + FX_Allocator_Free(m_pAllocator, m_pHashTable); + m_pHashTable = NULL; + } + if (bAllocNow) { + m_pHashTable = FX_Allocator_Alloc(m_pAllocator, CAssoc*, nHashSize); + if (m_pHashTable) { + FXSYS_memset32(m_pHashTable, 0, sizeof(CAssoc*) * nHashSize); + } + } + m_nHashTableSize = nHashSize; +} +FX_BOOL CFX_MapPtrToPtr::RemoveKey(void* key) +{ + if (m_pHashTable == NULL) { + return FALSE; + } + CAssoc** ppAssocPrev; + ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize]; + CAssoc* pAssoc; + for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext) { + if (pAssoc->key == key) { + *ppAssocPrev = pAssoc->pNext; + FreeAssoc(pAssoc); + return TRUE; + } + ppAssocPrev = &pAssoc->pNext; + } + return FALSE; +} +void CFX_MapPtrToPtr::FreeAssoc(CFX_MapPtrToPtr::CAssoc* pAssoc) +{ + pAssoc->pNext = m_pFreeList; + m_pFreeList = pAssoc; + m_nCount--; + ASSERT(m_nCount >= 0); + if (m_nCount == 0) { + RemoveAll(); + } +} +CFX_MapByteStringToPtr::CFX_MapByteStringToPtr(int nBlockSize, IFX_Allocator* pAllocator) + : m_pAllocator(pAllocator) + , m_pHashTable(NULL) + , m_nHashTableSize(17) + , m_nCount(0) + , m_pFreeList(NULL) + , m_pBlocks(NULL) + , m_nBlockSize(nBlockSize) +{ + ASSERT(m_nBlockSize > 0); +} +void CFX_MapByteStringToPtr::RemoveAll() +{ + if (m_pHashTable != NULL) { + for (FX_DWORD nHash = 0; nHash < m_nHashTableSize; nHash++) { + CAssoc* pAssoc; + for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; + pAssoc = pAssoc->pNext) { + DestructElement(&pAssoc->key); + } + } + FX_Allocator_Free(m_pAllocator, m_pHashTable); + m_pHashTable = NULL; + } + m_nCount = 0; + m_pFreeList = NULL; + m_pBlocks->FreeDataChain(m_pAllocator); + m_pBlocks = NULL; +} +CFX_MapByteStringToPtr::~CFX_MapByteStringToPtr() +{ + RemoveAll(); + ASSERT(m_nCount == 0); +} +void CFX_MapByteStringToPtr::GetNextAssoc(FX_POSITION& rNextPosition, + CFX_ByteString& rKey, void*& rValue) const +{ + ASSERT(m_pHashTable != NULL); + CAssoc* pAssocRet = (CAssoc*)rNextPosition; + ASSERT(pAssocRet != NULL); + if (pAssocRet == (CAssoc*) - 1) { + for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++) + if ((pAssocRet = m_pHashTable[nBucket]) != NULL) { + break; + } + ASSERT(pAssocRet != NULL); + } + CAssoc* pAssocNext; + if ((pAssocNext = pAssocRet->pNext) == NULL) { + for (FX_DWORD nBucket = pAssocRet->nHashValue + 1; + nBucket < m_nHashTableSize; nBucket++) + if ((pAssocNext = m_pHashTable[nBucket]) != NULL) { + break; + } + } + rNextPosition = (FX_POSITION) pAssocNext; + rKey = pAssocRet->key; + rValue = pAssocRet->value; +} +FX_LPVOID CFX_MapByteStringToPtr::GetNextValue(FX_POSITION& rNextPosition) const +{ + ASSERT(m_pHashTable != NULL); + CAssoc* pAssocRet = (CAssoc*)rNextPosition; + ASSERT(pAssocRet != NULL); + if (pAssocRet == (CAssoc*) - 1) { + for (FX_DWORD nBucket = 0; nBucket < m_nHashTableSize; nBucket++) + if ((pAssocRet = m_pHashTable[nBucket]) != NULL) { + break; + } + ASSERT(pAssocRet != NULL); + } + CAssoc* pAssocNext; + if ((pAssocNext = pAssocRet->pNext) == NULL) { + for (FX_DWORD nBucket = pAssocRet->nHashValue + 1; + nBucket < m_nHashTableSize; nBucket++) + if ((pAssocNext = m_pHashTable[nBucket]) != NULL) { + break; + } + } + rNextPosition = (FX_POSITION) pAssocNext; + return pAssocRet->value; +} +void*& CFX_MapByteStringToPtr::operator[](FX_BSTR key) +{ + FX_DWORD nHash; + CAssoc* pAssoc; + if ((pAssoc = GetAssocAt(key, nHash)) == NULL) { + if (m_pHashTable == NULL) { + InitHashTable(m_nHashTableSize); + } + pAssoc = NewAssoc(); + pAssoc->nHashValue = nHash; + pAssoc->key = key; + pAssoc->pNext = m_pHashTable[nHash]; + m_pHashTable[nHash] = pAssoc; + } + return pAssoc->value; +} +CFX_MapByteStringToPtr::CAssoc* +CFX_MapByteStringToPtr::NewAssoc() +{ + if (m_pFreeList == NULL) { + CFX_Plex* newBlock = CFX_Plex::Create(m_pAllocator, m_pBlocks, m_nBlockSize, sizeof(CFX_MapByteStringToPtr::CAssoc)); + CFX_MapByteStringToPtr::CAssoc* pAssoc = (CFX_MapByteStringToPtr::CAssoc*)newBlock->data(); + pAssoc += m_nBlockSize - 1; + for (int i = m_nBlockSize - 1; i >= 0; i--, pAssoc--) { + pAssoc->pNext = m_pFreeList; + m_pFreeList = pAssoc; + } + } + ASSERT(m_pFreeList != NULL); + CFX_MapByteStringToPtr::CAssoc* pAssoc = m_pFreeList; + m_pFreeList = m_pFreeList->pNext; + m_nCount++; + ASSERT(m_nCount > 0); + ConstructElement(&pAssoc->key); + pAssoc->value = 0; + return pAssoc; +} +void CFX_MapByteStringToPtr::FreeAssoc(CFX_MapByteStringToPtr::CAssoc* pAssoc) +{ + DestructElement(&pAssoc->key); + pAssoc->pNext = m_pFreeList; + m_pFreeList = pAssoc; + m_nCount--; + ASSERT(m_nCount >= 0); + if (m_nCount == 0) { + RemoveAll(); + } +} +CFX_MapByteStringToPtr::CAssoc* +CFX_MapByteStringToPtr::GetAssocAt(FX_BSTR key, FX_DWORD& nHash) const +{ + nHash = HashKey(key) % m_nHashTableSize; + if (m_pHashTable == NULL) { + return NULL; + } + CAssoc* pAssoc; + for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext) { + if (pAssoc->key == key) { + return pAssoc; + } + } + return NULL; +} +FX_BOOL CFX_MapByteStringToPtr::Lookup(FX_BSTR key, void*& rValue) const +{ + FX_DWORD nHash; + CAssoc* pAssoc = GetAssocAt(key, nHash); + if (pAssoc == NULL) { + return FALSE; + } + rValue = pAssoc->value; + return TRUE; +} +void CFX_MapByteStringToPtr::InitHashTable( + FX_DWORD nHashSize, FX_BOOL bAllocNow) +{ + ASSERT(m_nCount == 0); + ASSERT(nHashSize > 0); + if (m_pHashTable != NULL) { + FX_Allocator_Free(m_pAllocator, m_pHashTable); + m_pHashTable = NULL; + } + if (bAllocNow) { + m_pHashTable = FX_Allocator_Alloc(m_pAllocator, CAssoc*, nHashSize); + if (m_pHashTable) { + FXSYS_memset32(m_pHashTable, 0, sizeof(CAssoc*) * nHashSize); + } + } + m_nHashTableSize = nHashSize; +} +inline FX_DWORD CFX_MapByteStringToPtr::HashKey(FX_BSTR key) const +{ + FX_DWORD nHash = 0; + int len = key.GetLength(); + FX_LPCBYTE buf = key; + for (int i = 0; i < len; i ++) { + nHash = (nHash << 5) + nHash + buf[i]; + } + return nHash; +} +FX_BOOL CFX_MapByteStringToPtr::RemoveKey(FX_BSTR key) +{ + if (m_pHashTable == NULL) { + return FALSE; + } + CAssoc** ppAssocPrev; + ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize]; + CAssoc* pAssoc; + for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext) { + if (pAssoc->key == key) { + *ppAssocPrev = pAssoc->pNext; + FreeAssoc(pAssoc); + return TRUE; + } + ppAssocPrev = &pAssoc->pNext; + } + return FALSE; +} +struct _CompactString { + FX_BYTE m_CompactLen; + FX_BYTE m_LenHigh; + FX_BYTE m_LenLow; + FX_BYTE m_Unused; + FX_LPBYTE m_pBuffer; +}; +static void _CompactStringRelease(IFX_Allocator* pAllocator, _CompactString* pCompact) +{ + if (pCompact->m_CompactLen == 0xff) { + FX_Allocator_Free(pAllocator, pCompact->m_pBuffer); + } +} +static FX_BOOL _CompactStringSame(_CompactString* pCompact, FX_LPCBYTE pStr, int len) +{ + if (len < sizeof(_CompactString)) { + if (pCompact->m_CompactLen != len) { + return FALSE; + } + return FXSYS_memcmp32(&pCompact->m_LenHigh, pStr, len) == 0; + } + if (pCompact->m_CompactLen != 0xff || pCompact->m_LenHigh * 256 + pCompact->m_LenLow != len) { + return FALSE; + } + return FXSYS_memcmp32(pCompact->m_pBuffer, pStr, len) == 0; +} +static void _CompactStringStore(IFX_Allocator* pAllocator, _CompactString* pCompact, FX_LPCBYTE pStr, int len) +{ + if (len < (int)sizeof(_CompactString)) { + pCompact->m_CompactLen = (FX_BYTE)len; + FXSYS_memcpy32(&pCompact->m_LenHigh, pStr, len); + return; + } + pCompact->m_CompactLen = 0xff; + pCompact->m_LenHigh = len / 256; + pCompact->m_LenLow = len % 256; + pCompact->m_pBuffer = FX_Allocator_Alloc(pAllocator, FX_BYTE, len); + if (pCompact->m_pBuffer) { + FXSYS_memcpy32(pCompact->m_pBuffer, pStr, len); + } +} +static CFX_ByteStringC _CompactStringGet(_CompactString* pCompact) +{ + if (pCompact->m_CompactLen == 0xff) { + return CFX_ByteStringC(pCompact->m_pBuffer, pCompact->m_LenHigh * 256 + pCompact->m_LenLow); + } + if (pCompact->m_CompactLen == 0xfe) { + return CFX_ByteStringC(); + } + return CFX_ByteStringC(&pCompact->m_LenHigh, pCompact->m_CompactLen); +} +#define CMAP_ALLOC_STEP 8 +#define CMAP_INDEX_SIZE 8 +CFX_CMapByteStringToPtr::CFX_CMapByteStringToPtr(IFX_Allocator* pAllocator) + : m_Buffer(sizeof(_CompactString) + sizeof(void*), CMAP_ALLOC_STEP, CMAP_INDEX_SIZE, pAllocator) +{ +} +CFX_CMapByteStringToPtr::~CFX_CMapByteStringToPtr() +{ + RemoveAll(); +} +void CFX_CMapByteStringToPtr::RemoveAll() +{ + IFX_Allocator* pAllocator = m_Buffer.m_pAllocator; + int size = m_Buffer.GetSize(); + for (int i = 0; i < size; i ++) { + _CompactStringRelease(pAllocator, (_CompactString*)m_Buffer.GetAt(i)); + } + m_Buffer.RemoveAll(); +} +FX_POSITION CFX_CMapByteStringToPtr::GetStartPosition() const +{ + int size = m_Buffer.GetSize(); + for (int i = 0; i < size; i ++) { + _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); + if (pKey->m_CompactLen != 0xfe) { + return (FX_POSITION)(FX_UINTPTR)(i + 1); + } + } + return NULL; +} +void CFX_CMapByteStringToPtr::GetNextAssoc(FX_POSITION& rNextPosition, CFX_ByteString& rKey, void*& rValue) const +{ + if (rNextPosition == NULL) { + return; + } + int index = (int)(FX_UINTPTR)rNextPosition - 1; + _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); + rKey = _CompactStringGet(pKey); + rValue = *(void**)(pKey + 1); + index ++; + int size = m_Buffer.GetSize(); + while (index < size) { + pKey = (_CompactString*)m_Buffer.GetAt(index); + if (pKey->m_CompactLen != 0xfe) { + rNextPosition = (FX_POSITION)(FX_UINTPTR)(index + 1); + return; + } + index ++; + } + rNextPosition = NULL; +} +FX_LPVOID CFX_CMapByteStringToPtr::GetNextValue(FX_POSITION& rNextPosition) const +{ + if (rNextPosition == NULL) { + return NULL; + } + int index = (int)(FX_UINTPTR)rNextPosition - 1; + _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); + FX_LPVOID rValue = *(void**)(pKey + 1); + index ++; + int size = m_Buffer.GetSize(); + while (index < size) { + pKey = (_CompactString*)m_Buffer.GetAt(index); + if (pKey->m_CompactLen != 0xfe) { + rNextPosition = (FX_POSITION)(FX_UINTPTR)(index + 1); + return rValue; + } + index ++; + } + rNextPosition = NULL; + return rValue; +} +FX_BOOL _CMapLookupCallback(void* param, void* pData) +{ + return !_CompactStringSame((_CompactString*)pData, ((CFX_ByteStringC*)param)->GetPtr(), ((CFX_ByteStringC*)param)->GetLength()); +} +FX_BOOL CFX_CMapByteStringToPtr::Lookup(FX_BSTR key, void*& rValue) const +{ + void* p = m_Buffer.Iterate(_CMapLookupCallback, (void*)&key); + if (!p) { + return FALSE; + } + rValue = *(void**)((_CompactString*)p + 1); + return TRUE; +} +void CFX_CMapByteStringToPtr::SetAt(FX_BSTR key, void* value) +{ + ASSERT(value != NULL); + int index, key_len = key.GetLength(); + int size = m_Buffer.GetSize(); + for (index = 0; index < size; index ++) { + _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); + if (!_CompactStringSame(pKey, (FX_LPCBYTE)key, key_len)) { + continue; + } + *(void**)(pKey + 1) = value; + return; + } + IFX_Allocator* pAllocator = m_Buffer.m_pAllocator; + for (index = 0; index < size; index ++) { + _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); + if (pKey->m_CompactLen) { + continue; + } + _CompactStringStore(pAllocator, pKey, (FX_LPCBYTE)key, key_len); + *(void**)(pKey + 1) = value; + return; + } + _CompactString* pKey = (_CompactString*)m_Buffer.Add(); + _CompactStringStore(pAllocator, pKey, (FX_LPCBYTE)key, key_len); + *(void**)(pKey + 1) = value; +} +void CFX_CMapByteStringToPtr::AddValue(FX_BSTR key, void* value) +{ + ASSERT(value != NULL); + _CompactString* pKey = (_CompactString*)m_Buffer.Add(); + _CompactStringStore(m_Buffer.m_pAllocator, pKey, (FX_LPCBYTE)key, key.GetLength()); + *(void**)(pKey + 1) = value; +} +void CFX_CMapByteStringToPtr::RemoveKey(FX_BSTR key) +{ + int key_len = key.GetLength(); + IFX_Allocator* pAllocator = m_Buffer.m_pAllocator; + int size = m_Buffer.GetSize(); + for (int index = 0; index < size; index ++) { + _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); + if (!_CompactStringSame(pKey, (FX_LPCBYTE)key, key_len)) { + continue; + } + _CompactStringRelease(pAllocator, pKey); + pKey->m_CompactLen = 0xfe; + return; + } +} +int CFX_CMapByteStringToPtr::GetCount() const +{ + int count = 0; + int size = m_Buffer.GetSize(); + for (int i = 0; i < size; i ++) { + _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); + if (pKey->m_CompactLen != 0xfe) { + count ++; + } + } + return count; +} +extern "C" { + static int _CompareDWord(const void* p1, const void* p2) + { + return (*(FX_DWORD*)p1) - (*(FX_DWORD*)p2); + } +}; +struct _DWordPair { + FX_DWORD key; + FX_DWORD value; +}; +FX_BOOL CFX_CMapDWordToDWord::Lookup(FX_DWORD key, FX_DWORD& value) const +{ + FX_LPVOID pResult = FXSYS_bsearch(&key, m_Buffer.GetBuffer(), m_Buffer.GetSize() / sizeof(_DWordPair), + sizeof(_DWordPair), _CompareDWord); + if (pResult == NULL) { + return FALSE; + } + value = ((FX_DWORD*)pResult)[1]; + return TRUE; +} +FX_POSITION CFX_CMapDWordToDWord::GetStartPosition() const +{ + FX_DWORD count = m_Buffer.GetSize() / sizeof(_DWordPair); + if (count == 0) { + return NULL; + } + return (FX_POSITION)1; +} +void CFX_CMapDWordToDWord::GetNextAssoc(FX_POSITION& pos, FX_DWORD& key, FX_DWORD& value) const +{ + if (pos == 0) { + return; + } + FX_DWORD index = ((FX_DWORD)(FX_UINTPTR)pos) - 1; + FX_DWORD count = m_Buffer.GetSize() / sizeof(_DWordPair); + _DWordPair* buf = (_DWordPair*)m_Buffer.GetBuffer(); + key = buf[index].key; + value = buf[index].value; + if (index == count - 1) { + pos = 0; + } else { + pos = (FX_POSITION)((FX_UINTPTR)pos + 1); + } +} +void CFX_CMapDWordToDWord::SetAt(FX_DWORD key, FX_DWORD value) +{ + FX_DWORD count = m_Buffer.GetSize() / sizeof(_DWordPair); + _DWordPair* buf = (_DWordPair*)m_Buffer.GetBuffer(); + _DWordPair pair = {key, value}; + if (count == 0 || key > buf[count - 1].key) { + m_Buffer.AppendBlock(&pair, sizeof(_DWordPair)); + return; + } + int low = 0, high = count - 1; + while (low <= high) { + int mid = (low + high) / 2; + if (buf[mid].key < key) { + low = mid + 1; + } else if (buf[mid].key > key) { + high = mid - 1; + } else { + buf[mid].value = value; + return; + } + } + m_Buffer.InsertBlock(low * sizeof(_DWordPair), &pair, sizeof(_DWordPair)); +} +void CFX_CMapDWordToDWord::EstimateSize(FX_DWORD size, FX_DWORD grow_by) +{ + m_Buffer.EstimateSize(size, grow_by); +} diff --git a/core/src/fxcrt/fx_basic_memmgr.cpp b/core/src/fxcrt/fx_basic_memmgr.cpp index 34df829060..5c862a2db5 100644 --- a/core/src/fxcrt/fx_basic_memmgr.cpp +++ b/core/src/fxcrt/fx_basic_memmgr.cpp @@ -1,306 +1,306 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_basic.h"
-#include "mem_int.h"
-void FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr* pFoxitMgr)
-{
- if (pFoxitMgr == NULL) {
- return;
- }
- CFX_MemoryMgr* p = (CFX_MemoryMgr*)pFoxitMgr;
- if (p->m_pSystemMgr->CollectAll) {
- p->m_pSystemMgr->CollectAll(p->m_pSystemMgr);
- }
- if (p->m_bReleaseMgr) {
- p->m_pSystemMgr->Free(p->m_pSystemMgr, p, 0);
- }
- if (p->m_pExternalMemory) {
- free(p->m_pExternalMemory);
- }
-}
-#ifdef __cplusplus
-extern "C" {
-#endif
-static void* _DefAllocDebug(IFX_Allocator* pAllocator, size_t size, FX_LPCSTR filename, int line)
-{
- return ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->AllocDebug(size, 0, filename, line);
-}
-static void* _DefAlloc(IFX_Allocator* pAllocator, size_t size)
-{
- return ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->Alloc(size, 0);
-}
-static void* _DefReallocDebug(IFX_Allocator* pAllocator, void* p, size_t size, FX_LPCSTR filename, int line)
-{
- return ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->ReallocDebug(p, size, 0, filename, line);
-}
-static void* _DefRealloc(IFX_Allocator* pAllocator, void* p, size_t size)
-{
- return ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->Realloc(p, size, 0);
-}
-static void _DefFree(IFX_Allocator* pAllocator, void* p)
-{
- ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->Free(p, 0);
-}
-#ifdef __cplusplus
-}
-#endif
-void CFX_MemoryMgr::Init(FXMEM_SystemMgr* pSystemMgr)
-{
- m_pSystemMgr = pSystemMgr;
- IFX_Allocator &ac = m_DefAllocator.m_Allocator;
- ac.m_Alloc = _DefAlloc;
- ac.m_AllocDebug = _DefAllocDebug;
- ac.m_Realloc = _DefRealloc;
- ac.m_ReallocDebug = _DefReallocDebug;
- ac.m_Free = _DefFree;
- m_DefAllocator.m_pFoxitMgr = this;
- m_pExternalMemory = NULL;
- m_bReleaseMgr = TRUE;
-}
-void CFX_MemoryMgr::PurgeMgr()
-{
- if (m_pSystemMgr->Purge) {
- m_pSystemMgr->Purge(m_pSystemMgr);
- }
-}
-void* CFX_MemoryMgr::Alloc(size_t size, int flags)
-{
- void* p = m_pSystemMgr->Alloc(m_pSystemMgr, size, flags);
- if (p == NULL) {
- return NULL;
- }
- return p;
-}
-void* CFX_MemoryMgr::AllocDebug(size_t size, int flags, FX_LPCSTR file, int line)
-{
- void* p = m_pSystemMgr->AllocDebug(m_pSystemMgr, size, flags, file, line);
- if (p == NULL) {
- return NULL;
- }
- return p;
-}
-void* CFX_MemoryMgr::Realloc(void* p, size_t size, int flags)
-{
- void* p1 = m_pSystemMgr->Realloc(m_pSystemMgr, p, size, flags);
- if (p1 == NULL) {
- return NULL;
- }
- return p1;
-}
-void* CFX_MemoryMgr::ReallocDebug(void* p, size_t size, int flags, FX_LPCSTR file, int line)
-{
- void* p1 = m_pSystemMgr->ReallocDebug(m_pSystemMgr, p, size, flags, file, line);
- if (p1 == NULL) {
- return NULL;
- }
- return p1;
-}
-void CFX_MemoryMgr::Free(void* p, int flags)
-{
- if (p == NULL) {
- return;
- }
- m_pSystemMgr->Free(m_pSystemMgr, p, flags);
-}
-CFX_MemoryMgr* g_pDefFoxitMgr = NULL;
-void* FXMEM_DefaultAlloc(size_t size, int flags)
-{
- return g_pDefFoxitMgr->Alloc(size, flags);
-}
-void* FXMEM_DefaultAlloc2(size_t size, size_t unit, int flags)
-{
- return g_pDefFoxitMgr->Alloc(size * unit, flags);
-}
-void* FXMEM_DefaultRealloc(void* p, size_t size, int flags)
-{
- if (p == NULL) {
- return FXMEM_DefaultAlloc(size, flags);
- }
- return g_pDefFoxitMgr->Realloc(p, size, flags);
-}
-void* FXMEM_DefaultRealloc2(void* p, size_t size, size_t unit, int flags)
-{
- if (p == NULL) {
- return FXMEM_DefaultAlloc2(size, unit, flags);
- }
- return g_pDefFoxitMgr->Realloc(p, size * unit, flags);
-}
-void* FXMEM_DefaultAllocDebug(size_t size, int flags, FX_LPCSTR file, int line)
-{
- return g_pDefFoxitMgr->AllocDebug(size, flags, file, line);
-}
-void* FXMEM_DefaultAllocDebug2(size_t size, size_t unit, int flags, FX_LPCSTR file, int line)
-{
- return g_pDefFoxitMgr->AllocDebug(size * unit, flags, file, line);
-}
-void* FXMEM_DefaultReallocDebug(void* p, size_t size, int flags, FX_LPCSTR file, int line)
-{
- if (p == NULL) {
- return FXMEM_DefaultAllocDebug(size, flags, file, line);
- }
- return g_pDefFoxitMgr->ReallocDebug(p, size, flags, file, line);
-}
-void* FXMEM_DefaultReallocDebug2(void* p, size_t size, size_t unit, int flags, FX_LPCSTR file, int line)
-{
- if (p == NULL) {
- return FXMEM_DefaultAllocDebug2(size, unit, flags, file, line);
- }
- return g_pDefFoxitMgr->ReallocDebug(p, size * unit, flags, file, line);
-}
-void FXMEM_DefaultFree(void* p, int flags)
-{
- g_pDefFoxitMgr->Free(p, flags);
-}
-IFX_Allocator* FXMEM_GetDefAllocator()
-{
- return &g_pDefFoxitMgr->m_DefAllocator.m_Allocator;
-}
-void* CFX_Object::operator new(size_t size)
-{
- return g_pDefFoxitMgr->Alloc(size, 0);
-}
-void* CFX_Object::operator new[](size_t size)
-{
- return g_pDefFoxitMgr->Alloc(size, 0);
-}
-void* CFX_Object::operator new[](size_t size, FX_LPCSTR file, int line)
-{
- return g_pDefFoxitMgr->AllocDebug(size, 0, file, line);
-}
-void* CFX_Object::operator new(size_t size, FX_LPCSTR file, int line)
-{
- return g_pDefFoxitMgr->AllocDebug(size, 0, file, line);
-}
-void CFX_Object::operator delete(void* p)
-{
- g_pDefFoxitMgr->Free(p, 0);
-}
-void CFX_Object::operator delete[](void* p)
-{
- g_pDefFoxitMgr->Free(p, 0);
-}
-void CFX_Object::operator delete(void* p, FX_LPCSTR file, int line)
-{
- g_pDefFoxitMgr->Free(p, 0);
-}
-void CFX_Object::operator delete[](void* p, FX_LPCSTR file, int line)
-{
- g_pDefFoxitMgr->Free(p, 0);
-}
-void* CFX_AllocObject::operator new(size_t size, IFX_Allocator* pAllocator, FX_LPCSTR filename, int line)
-{
- void* p = pAllocator ? pAllocator->m_AllocDebug(pAllocator, size, filename, line) :
- g_pDefFoxitMgr->AllocDebug(size, 0, filename, line);
- ((CFX_AllocObject*)p)->m_pAllocator = pAllocator;
- return p;
-}
-void CFX_AllocObject::operator delete (void* p, IFX_Allocator* pAllocator, FX_LPCSTR filename, int line)
-{
- if (pAllocator) {
- pAllocator->m_Free(pAllocator, p);
- } else {
- g_pDefFoxitMgr->Free(p, 0);
- }
-}
-void* CFX_AllocObject::operator new(size_t size, IFX_Allocator* pAllocator)
-{
- void* p = pAllocator ? pAllocator->m_Alloc(pAllocator, size) : g_pDefFoxitMgr->Alloc(size, 0);
- ((CFX_AllocObject*)p)->m_pAllocator = pAllocator;
- return p;
-}
-void CFX_AllocObject::operator delete(void* p)
-{
- if (((CFX_AllocObject*)p)->m_pAllocator) {
- (((CFX_AllocObject*)p)->m_pAllocator)->m_Free(((CFX_AllocObject*)p)->m_pAllocator, p);
- } else {
- g_pDefFoxitMgr->Free(p, 0);
- }
-}
-void CFX_AllocObject::operator delete(void* p, IFX_Allocator* pAllocator)
-{
- if (pAllocator) {
- pAllocator->m_Free(pAllocator, p);
- } else {
- g_pDefFoxitMgr->Free(p, 0);
- }
-}
-extern "C" {
- static void* _GOPAllocDebug(IFX_Allocator* pAllocator, size_t size, FX_LPCSTR file, int line)
- {
- return ((CFX_GrowOnlyPool*)pAllocator)->Alloc(size);
- }
- static void* _GOPAlloc(IFX_Allocator* pAllocator, size_t size)
- {
- return ((CFX_GrowOnlyPool*)pAllocator)->Alloc(size);
- }
- static void* _GOPReallocDebug(IFX_Allocator* pAllocator, void* p, size_t new_size, FX_LPCSTR file, int line)
- {
- return ((CFX_GrowOnlyPool*)pAllocator)->Realloc(p, new_size);
- }
- static void* _GOPRealloc(IFX_Allocator* pAllocator, void* p, size_t new_size)
- {
- return ((CFX_GrowOnlyPool*)pAllocator)->Realloc(p, new_size);
- }
- static void _GOPFree(IFX_Allocator* pAllocator, void* p)
- {
- }
-};
-CFX_GrowOnlyPool::CFX_GrowOnlyPool(IFX_Allocator* pAllocator, size_t trunk_size)
-{
- m_TrunkSize = trunk_size;
- m_pFirstTrunk = NULL;
- m_pAllocator = pAllocator ? pAllocator : &g_pDefFoxitMgr->m_DefAllocator.m_Allocator;
- m_AllocDebug = _GOPAllocDebug;
- m_Alloc = _GOPAlloc;
- m_ReallocDebug = _GOPReallocDebug;
- m_Realloc = _GOPRealloc;
- m_Free = _GOPFree;
-}
-CFX_GrowOnlyPool::~CFX_GrowOnlyPool()
-{
- FreeAll();
-}
-void CFX_GrowOnlyPool::SetAllocator(IFX_Allocator* pAllocator)
-{
- ASSERT(m_pFirstTrunk == NULL);
- m_pAllocator = pAllocator ? pAllocator : &g_pDefFoxitMgr->m_DefAllocator.m_Allocator;
-}
-struct _FX_GrowOnlyTrunk {
- size_t m_Size;
- size_t m_Allocated;
- _FX_GrowOnlyTrunk* m_pNext;
-};
-void CFX_GrowOnlyPool::FreeAll()
-{
- _FX_GrowOnlyTrunk* pTrunk = (_FX_GrowOnlyTrunk*)m_pFirstTrunk;
- while (pTrunk) {
- _FX_GrowOnlyTrunk* pNext = pTrunk->m_pNext;
- m_pAllocator->m_Free(m_pAllocator, pTrunk);
- pTrunk = pNext;
- }
- m_pFirstTrunk = NULL;
-}
-void* CFX_GrowOnlyPool::Alloc(size_t size)
-{
- size = (size + 3) / 4 * 4;
- _FX_GrowOnlyTrunk* pTrunk = (_FX_GrowOnlyTrunk*)m_pFirstTrunk;
- while (pTrunk) {
- if (pTrunk->m_Size - pTrunk->m_Allocated >= size) {
- void* p = (FX_LPBYTE)(pTrunk + 1) + pTrunk->m_Allocated;
- pTrunk->m_Allocated += size;
- return p;
- }
- pTrunk = pTrunk->m_pNext;
- }
- size_t alloc_size = size > m_TrunkSize ? size : m_TrunkSize;
- pTrunk = (_FX_GrowOnlyTrunk*)m_pAllocator->m_Alloc(m_pAllocator, sizeof(_FX_GrowOnlyTrunk) + alloc_size);
- pTrunk->m_Size = alloc_size;
- pTrunk->m_Allocated = size;
- pTrunk->m_pNext = (_FX_GrowOnlyTrunk*)m_pFirstTrunk;
- m_pFirstTrunk = pTrunk;
- return pTrunk + 1;
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_basic.h" +#include "mem_int.h" +void FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr* pFoxitMgr) +{ + if (pFoxitMgr == NULL) { + return; + } + CFX_MemoryMgr* p = (CFX_MemoryMgr*)pFoxitMgr; + if (p->m_pSystemMgr->CollectAll) { + p->m_pSystemMgr->CollectAll(p->m_pSystemMgr); + } + if (p->m_bReleaseMgr) { + p->m_pSystemMgr->Free(p->m_pSystemMgr, p, 0); + } + if (p->m_pExternalMemory) { + free(p->m_pExternalMemory); + } +} +#ifdef __cplusplus +extern "C" { +#endif +static void* _DefAllocDebug(IFX_Allocator* pAllocator, size_t size, FX_LPCSTR filename, int line) +{ + return ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->AllocDebug(size, 0, filename, line); +} +static void* _DefAlloc(IFX_Allocator* pAllocator, size_t size) +{ + return ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->Alloc(size, 0); +} +static void* _DefReallocDebug(IFX_Allocator* pAllocator, void* p, size_t size, FX_LPCSTR filename, int line) +{ + return ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->ReallocDebug(p, size, 0, filename, line); +} +static void* _DefRealloc(IFX_Allocator* pAllocator, void* p, size_t size) +{ + return ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->Realloc(p, size, 0); +} +static void _DefFree(IFX_Allocator* pAllocator, void* p) +{ + ((FX_DefAllocator*)pAllocator)->m_pFoxitMgr->Free(p, 0); +} +#ifdef __cplusplus +} +#endif +void CFX_MemoryMgr::Init(FXMEM_SystemMgr* pSystemMgr) +{ + m_pSystemMgr = pSystemMgr; + IFX_Allocator &ac = m_DefAllocator.m_Allocator; + ac.m_Alloc = _DefAlloc; + ac.m_AllocDebug = _DefAllocDebug; + ac.m_Realloc = _DefRealloc; + ac.m_ReallocDebug = _DefReallocDebug; + ac.m_Free = _DefFree; + m_DefAllocator.m_pFoxitMgr = this; + m_pExternalMemory = NULL; + m_bReleaseMgr = TRUE; +} +void CFX_MemoryMgr::PurgeMgr() +{ + if (m_pSystemMgr->Purge) { + m_pSystemMgr->Purge(m_pSystemMgr); + } +} +void* CFX_MemoryMgr::Alloc(size_t size, int flags) +{ + void* p = m_pSystemMgr->Alloc(m_pSystemMgr, size, flags); + if (p == NULL) { + return NULL; + } + return p; +} +void* CFX_MemoryMgr::AllocDebug(size_t size, int flags, FX_LPCSTR file, int line) +{ + void* p = m_pSystemMgr->AllocDebug(m_pSystemMgr, size, flags, file, line); + if (p == NULL) { + return NULL; + } + return p; +} +void* CFX_MemoryMgr::Realloc(void* p, size_t size, int flags) +{ + void* p1 = m_pSystemMgr->Realloc(m_pSystemMgr, p, size, flags); + if (p1 == NULL) { + return NULL; + } + return p1; +} +void* CFX_MemoryMgr::ReallocDebug(void* p, size_t size, int flags, FX_LPCSTR file, int line) +{ + void* p1 = m_pSystemMgr->ReallocDebug(m_pSystemMgr, p, size, flags, file, line); + if (p1 == NULL) { + return NULL; + } + return p1; +} +void CFX_MemoryMgr::Free(void* p, int flags) +{ + if (p == NULL) { + return; + } + m_pSystemMgr->Free(m_pSystemMgr, p, flags); +} +CFX_MemoryMgr* g_pDefFoxitMgr = NULL; +void* FXMEM_DefaultAlloc(size_t size, int flags) +{ + return g_pDefFoxitMgr->Alloc(size, flags); +} +void* FXMEM_DefaultAlloc2(size_t size, size_t unit, int flags) +{ + return g_pDefFoxitMgr->Alloc(size * unit, flags); +} +void* FXMEM_DefaultRealloc(void* p, size_t size, int flags) +{ + if (p == NULL) { + return FXMEM_DefaultAlloc(size, flags); + } + return g_pDefFoxitMgr->Realloc(p, size, flags); +} +void* FXMEM_DefaultRealloc2(void* p, size_t size, size_t unit, int flags) +{ + if (p == NULL) { + return FXMEM_DefaultAlloc2(size, unit, flags); + } + return g_pDefFoxitMgr->Realloc(p, size * unit, flags); +} +void* FXMEM_DefaultAllocDebug(size_t size, int flags, FX_LPCSTR file, int line) +{ + return g_pDefFoxitMgr->AllocDebug(size, flags, file, line); +} +void* FXMEM_DefaultAllocDebug2(size_t size, size_t unit, int flags, FX_LPCSTR file, int line) +{ + return g_pDefFoxitMgr->AllocDebug(size * unit, flags, file, line); +} +void* FXMEM_DefaultReallocDebug(void* p, size_t size, int flags, FX_LPCSTR file, int line) +{ + if (p == NULL) { + return FXMEM_DefaultAllocDebug(size, flags, file, line); + } + return g_pDefFoxitMgr->ReallocDebug(p, size, flags, file, line); +} +void* FXMEM_DefaultReallocDebug2(void* p, size_t size, size_t unit, int flags, FX_LPCSTR file, int line) +{ + if (p == NULL) { + return FXMEM_DefaultAllocDebug2(size, unit, flags, file, line); + } + return g_pDefFoxitMgr->ReallocDebug(p, size * unit, flags, file, line); +} +void FXMEM_DefaultFree(void* p, int flags) +{ + g_pDefFoxitMgr->Free(p, flags); +} +IFX_Allocator* FXMEM_GetDefAllocator() +{ + return &g_pDefFoxitMgr->m_DefAllocator.m_Allocator; +} +void* CFX_Object::operator new(size_t size) +{ + return g_pDefFoxitMgr->Alloc(size, 0); +} +void* CFX_Object::operator new[](size_t size) +{ + return g_pDefFoxitMgr->Alloc(size, 0); +} +void* CFX_Object::operator new[](size_t size, FX_LPCSTR file, int line) +{ + return g_pDefFoxitMgr->AllocDebug(size, 0, file, line); +} +void* CFX_Object::operator new(size_t size, FX_LPCSTR file, int line) +{ + return g_pDefFoxitMgr->AllocDebug(size, 0, file, line); +} +void CFX_Object::operator delete(void* p) +{ + g_pDefFoxitMgr->Free(p, 0); +} +void CFX_Object::operator delete[](void* p) +{ + g_pDefFoxitMgr->Free(p, 0); +} +void CFX_Object::operator delete(void* p, FX_LPCSTR file, int line) +{ + g_pDefFoxitMgr->Free(p, 0); +} +void CFX_Object::operator delete[](void* p, FX_LPCSTR file, int line) +{ + g_pDefFoxitMgr->Free(p, 0); +} +void* CFX_AllocObject::operator new(size_t size, IFX_Allocator* pAllocator, FX_LPCSTR filename, int line) +{ + void* p = pAllocator ? pAllocator->m_AllocDebug(pAllocator, size, filename, line) : + g_pDefFoxitMgr->AllocDebug(size, 0, filename, line); + ((CFX_AllocObject*)p)->m_pAllocator = pAllocator; + return p; +} +void CFX_AllocObject::operator delete (void* p, IFX_Allocator* pAllocator, FX_LPCSTR filename, int line) +{ + if (pAllocator) { + pAllocator->m_Free(pAllocator, p); + } else { + g_pDefFoxitMgr->Free(p, 0); + } +} +void* CFX_AllocObject::operator new(size_t size, IFX_Allocator* pAllocator) +{ + void* p = pAllocator ? pAllocator->m_Alloc(pAllocator, size) : g_pDefFoxitMgr->Alloc(size, 0); + ((CFX_AllocObject*)p)->m_pAllocator = pAllocator; + return p; +} +void CFX_AllocObject::operator delete(void* p) +{ + if (((CFX_AllocObject*)p)->m_pAllocator) { + (((CFX_AllocObject*)p)->m_pAllocator)->m_Free(((CFX_AllocObject*)p)->m_pAllocator, p); + } else { + g_pDefFoxitMgr->Free(p, 0); + } +} +void CFX_AllocObject::operator delete(void* p, IFX_Allocator* pAllocator) +{ + if (pAllocator) { + pAllocator->m_Free(pAllocator, p); + } else { + g_pDefFoxitMgr->Free(p, 0); + } +} +extern "C" { + static void* _GOPAllocDebug(IFX_Allocator* pAllocator, size_t size, FX_LPCSTR file, int line) + { + return ((CFX_GrowOnlyPool*)pAllocator)->Alloc(size); + } + static void* _GOPAlloc(IFX_Allocator* pAllocator, size_t size) + { + return ((CFX_GrowOnlyPool*)pAllocator)->Alloc(size); + } + static void* _GOPReallocDebug(IFX_Allocator* pAllocator, void* p, size_t new_size, FX_LPCSTR file, int line) + { + return ((CFX_GrowOnlyPool*)pAllocator)->Realloc(p, new_size); + } + static void* _GOPRealloc(IFX_Allocator* pAllocator, void* p, size_t new_size) + { + return ((CFX_GrowOnlyPool*)pAllocator)->Realloc(p, new_size); + } + static void _GOPFree(IFX_Allocator* pAllocator, void* p) + { + } +}; +CFX_GrowOnlyPool::CFX_GrowOnlyPool(IFX_Allocator* pAllocator, size_t trunk_size) +{ + m_TrunkSize = trunk_size; + m_pFirstTrunk = NULL; + m_pAllocator = pAllocator ? pAllocator : &g_pDefFoxitMgr->m_DefAllocator.m_Allocator; + m_AllocDebug = _GOPAllocDebug; + m_Alloc = _GOPAlloc; + m_ReallocDebug = _GOPReallocDebug; + m_Realloc = _GOPRealloc; + m_Free = _GOPFree; +} +CFX_GrowOnlyPool::~CFX_GrowOnlyPool() +{ + FreeAll(); +} +void CFX_GrowOnlyPool::SetAllocator(IFX_Allocator* pAllocator) +{ + ASSERT(m_pFirstTrunk == NULL); + m_pAllocator = pAllocator ? pAllocator : &g_pDefFoxitMgr->m_DefAllocator.m_Allocator; +} +struct _FX_GrowOnlyTrunk { + size_t m_Size; + size_t m_Allocated; + _FX_GrowOnlyTrunk* m_pNext; +}; +void CFX_GrowOnlyPool::FreeAll() +{ + _FX_GrowOnlyTrunk* pTrunk = (_FX_GrowOnlyTrunk*)m_pFirstTrunk; + while (pTrunk) { + _FX_GrowOnlyTrunk* pNext = pTrunk->m_pNext; + m_pAllocator->m_Free(m_pAllocator, pTrunk); + pTrunk = pNext; + } + m_pFirstTrunk = NULL; +} +void* CFX_GrowOnlyPool::Alloc(size_t size) +{ + size = (size + 3) / 4 * 4; + _FX_GrowOnlyTrunk* pTrunk = (_FX_GrowOnlyTrunk*)m_pFirstTrunk; + while (pTrunk) { + if (pTrunk->m_Size - pTrunk->m_Allocated >= size) { + void* p = (FX_LPBYTE)(pTrunk + 1) + pTrunk->m_Allocated; + pTrunk->m_Allocated += size; + return p; + } + pTrunk = pTrunk->m_pNext; + } + size_t alloc_size = size > m_TrunkSize ? size : m_TrunkSize; + pTrunk = (_FX_GrowOnlyTrunk*)m_pAllocator->m_Alloc(m_pAllocator, sizeof(_FX_GrowOnlyTrunk) + alloc_size); + pTrunk->m_Size = alloc_size; + pTrunk->m_Allocated = size; + pTrunk->m_pNext = (_FX_GrowOnlyTrunk*)m_pFirstTrunk; + m_pFirstTrunk = pTrunk; + return pTrunk + 1; +} diff --git a/core/src/fxcrt/fx_basic_memmgr_mini.cpp b/core/src/fxcrt/fx_basic_memmgr_mini.cpp index f8385e20d2..8d48bab935 100644 --- a/core/src/fxcrt/fx_basic_memmgr_mini.cpp +++ b/core/src/fxcrt/fx_basic_memmgr_mini.cpp @@ -1,822 +1,822 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_ext.h"
-#include "mem_int.h"
-#ifdef _FPDFAPI_MINI_
-static FX_MEMCONFIG g_MemConfig = {
- 1,
- 5,
- 8,
- 4,
- 12,
- 8,
- 2,
- 4,
- 32,
- 64,
-};
-#else
-static FX_MEMCONFIG g_MemConfig = {
- 1,
- 8,
- 24,
- 8,
- 32,
- 16,
- 4,
- 8,
- 128,
- 64,
-};
-#endif
-void FXMEM_SetConfig(const FX_MEMCONFIG* memConfig)
-{
- g_MemConfig = *memConfig;
-}
-#ifdef __cplusplus
-extern "C" {
-#endif
-static void* FixedAlloc(FXMEM_SystemMgr* pMgr, size_t size, int flags)
-{
- return ((CFXMEM_FixedMgr*)pMgr->user)->Alloc(size);
-}
-static void* FixedAllocDebug(FXMEM_SystemMgr* pMgr, size_t size, int flags, FX_LPCSTR file, int line)
-{
- return ((CFXMEM_FixedMgr*)pMgr->user)->Alloc(size);
-}
-static void* FixedRealloc(FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags)
-{
- return ((CFXMEM_FixedMgr*)pMgr->user)->Realloc(pointer, size);
-}
-static void* FixedReallocDebug(FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags, FX_LPCSTR file, int line)
-{
- return ((CFXMEM_FixedMgr*)pMgr->user)->Realloc(pointer, size);
-}
-static void FixedFree(FXMEM_SystemMgr* pMgr, void* pointer, int flags)
-{
- ((CFXMEM_FixedMgr*)pMgr->user)->Free(pointer);
-}
-static void FixedPurge(FXMEM_SystemMgr* pMgr)
-{
- ((CFXMEM_FixedMgr*)pMgr->user)->Purge();
-}
-static void FixedCollectAll(FXMEM_SystemMgr* pMgr)
-{
- ((CFXMEM_FixedMgr*)pMgr->user)->FreeAll();
-}
-#define FIXEDMEM_MINIMUMSIZE (1024 * 1024 * 8)
-FXMEM_FoxitMgr* FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible)
-{
- if (size < FIXEDMEM_MINIMUMSIZE) {
- size = FIXEDMEM_MINIMUMSIZE;
- }
- FX_LPVOID pMemory = malloc(size);
- if (!pMemory) {
- return NULL;
- }
- CFixedMgr_Proxy* pProxy = (CFixedMgr_Proxy*)pMemory;
- size_t offsetSize = (sizeof(CFixedMgr_Proxy) + 15) / 16 * 16;
- FXMEM_FoxitMgr* pFoxitMgr = pProxy->Initialize((FX_LPBYTE)pProxy + offsetSize, size - offsetSize, extensible);
- if (!pFoxitMgr) {
- free(pMemory);
- return NULL;
- }
- g_pDefFoxitMgr = (CFX_MemoryMgr*)pFoxitMgr;
- g_pDefFoxitMgr->m_pExternalMemory = pMemory;
- return pFoxitMgr;
-}
-FXMEM_FoxitMgr* FXMEM_CreateFixedMgr(void* pMemory, size_t size, FXMEM_SystemMgr2* pSystemMgr)
-{
- if (pMemory == NULL || size < FX_FIXEDMEM_PAGESIZE) {
- return NULL;
- }
- if (!pSystemMgr && size >= FIXEDMEM_PROXYSIZE_1) {
- CFixedMgr_Proxy* pProxy = (CFixedMgr_Proxy*)pMemory;
- size_t offsetSize = (sizeof(CFixedMgr_Proxy) + 15) / 16 * 16;
- return pProxy->Initialize((FX_LPBYTE)pProxy + offsetSize, size - offsetSize, FALSE);
- }
- CFXMEM_FixedMgr* pHeader = (CFXMEM_FixedMgr*)pMemory;
- pHeader->Initialize(size);
- pHeader->m_pExtender = pSystemMgr;
- CFX_MemoryMgr* p = (CFX_MemoryMgr*)pHeader->Alloc(sizeof(CFX_MemoryMgr));
- if (p == NULL) {
- return NULL;
- }
- p->Init(&pHeader->m_SystemMgr);
- return (FXMEM_FoxitMgr*)p;
-}
-size_t FXMEM_GetBlockSizeInFixedMgr(FXMEM_FoxitMgr* pFoxitMgr, void* ptr)
-{
- return pFoxitMgr ? ((CFXMEM_FixedMgr*)((CFX_MemoryMgr*)pFoxitMgr)->m_pSystemMgr->user)->GetSize(ptr) : 0;
-}
-#ifdef __cplusplus
-}
-#endif
-const FX_MEMCONFIG g_ProxyMgr_MemConfigs[6] = {
- {1, 2, 4, 0, 2, 2, 2, 0, 0, 0},
- {1, 4, 8, 0, 2, 2, 2, 0, 0, 0},
- {1, 4, 16, 4, 8, 8, 2, 1, 16, 16},
- {1, 8, 24, 4, 12, 12, 4, 2, 32, 16},
- {1, 8, 24, 8, 16, 16, 4, 2, 64, 32},
- {1, 8, 24, 8, 24, 32, 4, 2, 128, 64},
-};
-const FX_MEMCONFIG* FixedMgr_GetConfig(size_t nSize)
-{
- int index = 5;
- if (nSize <= FIXEDMEM_PROXYSIZE_0) {
- index = 0;
- } else if (nSize <= FIXEDMEM_PROXYSIZE_1) {
- index = 1;
- } else if (nSize <= FIXEDMEM_PROXYSIZE_2) {
- index = 2;
- } else if (nSize <= FIXEDMEM_PROXYSIZE_3) {
- index = 3;
- } else if (nSize <= FIXEDMEM_PROXYSIZE_4) {
- index = 4;
- }
- return &g_ProxyMgr_MemConfigs[index];
-}
-FXMEM_FoxitMgr* CFixedMgr_Proxy::Initialize(FX_LPVOID pBuffer, size_t nSize, FX_BOOL bExtensible)
-{
- FXSYS_assert(pBuffer != NULL && nSize >= FIXEDMEM_PROXYSIZE_1 - sizeof(CFixedMgr_Proxy));
- FXMEM_SetConfig(FixedMgr_GetConfig(nSize));
- m_SystemMgr.More = &CFixedMgr_Proxy::Common_More;
- m_SystemMgr.Free = &CFixedMgr_Proxy::Common_Free;
- m_pFixedPage = (CFXMEM_Page*)((FX_LPBYTE)pBuffer + FIXEDMEM_PROXYSIZE_0);
- m_pFixedPage->Initialize(nSize - FIXEDMEM_PROXYSIZE_0);
- m_pBuffer = pBuffer;
- m_nSize = nSize;
- m_bExtensible = bExtensible;
- return FXMEM_CreateFixedMgr(pBuffer, FIXEDMEM_PROXYSIZE_0, &m_SystemMgr);
-}
-FX_BOOL CFixedMgr_Proxy::Common_More(FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size)
-{
- CFixedMgr_Proxy* pProxyMgr = (CFixedMgr_Proxy*)pMgr;
- FXSYS_assert(pProxyMgr != NULL && pProxyMgr->m_pFixedPage != NULL);
- *new_size = alloc_size;
- *new_memory = pProxyMgr->m_pFixedPage->Alloc(alloc_size);
- if (*new_memory == NULL && pProxyMgr->m_bExtensible) {
- *new_memory = malloc(alloc_size);
- }
- return *new_memory != NULL;
-}
-void CFixedMgr_Proxy::Common_Free(FXMEM_SystemMgr2* pMgr, void* memory)
-{
- CFixedMgr_Proxy* pProxyMgr = (CFixedMgr_Proxy*)pMgr;
- FXSYS_assert(pProxyMgr != NULL && pProxyMgr->m_pFixedPage != NULL);
- if (memory > pProxyMgr->m_pBuffer && memory < (FX_LPBYTE)pProxyMgr->m_pBuffer + pProxyMgr->m_nSize) {
- pProxyMgr->m_pFixedPage->Free(memory);
- } else if (pProxyMgr->m_bExtensible) {
- free(memory);
- }
-}
-void CFXMEM_Page::Initialize(size_t size)
-{
- CFXMEM_Block *pFirstBlock = (CFXMEM_Block*)(this + 1);
- m_nAvailSize = size - sizeof(CFXMEM_Page) - sizeof(CFXMEM_Block);
- pFirstBlock->m_nBlockSize = m_nAvailSize;
- pFirstBlock->m_pNextBlock = NULL;
- m_AvailHead.m_nBlockSize = m_nAvailSize;
- m_AvailHead.m_pNextBlock = pFirstBlock;
- m_pLimitPos = (CFXMEM_Block*)((FX_LPBYTE)this + size);
-}
-FX_LPVOID CFXMEM_Page::Alloc(CFXMEM_Block* pPrevBlock, CFXMEM_Block* pNextBlock, size_t size, size_t oldsize)
-{
- size_t gap = pNextBlock->m_nBlockSize - size;
- if (gap <= 64 + sizeof(CFXMEM_Block)) {
- pPrevBlock->m_pNextBlock = pNextBlock->m_pNextBlock;
- m_nAvailSize -= pNextBlock->m_nBlockSize;
- } else {
- m_nAvailSize -= size + sizeof(CFXMEM_Block);
- pNextBlock->m_nBlockSize = size;
- CFXMEM_Block *pNewBlock = (CFXMEM_Block*)((FX_LPBYTE)(pNextBlock + 1) + size);
- pNewBlock->m_nBlockSize = gap - sizeof(CFXMEM_Block);
- pNewBlock->m_pNextBlock = pNextBlock->m_pNextBlock;
- pPrevBlock->m_pNextBlock = pNewBlock;
- }
- return (FX_LPVOID)(pNextBlock + 1);
-}
-FX_LPVOID CFXMEM_Page::Alloc(size_t size)
-{
- size_t oldsize = size;
-#if _FX_WORDSIZE_ == _FX_W64_
- size = (size + 31) / 32 * 32;
-#else
- size = (size + 7) / 8 * 8;
-#endif
- if (m_nAvailSize < size) {
- return NULL;
- }
- CFXMEM_Block *pNextBlock;
- CFXMEM_Block *pPrevBlock = &m_AvailHead;
- while (TRUE) {
- pNextBlock = pPrevBlock->m_pNextBlock;
- if (!pNextBlock) {
- return NULL;
- }
- if (pNextBlock->m_nBlockSize >= size) {
- break;
- }
- pPrevBlock = pNextBlock;
- }
- return Alloc(pPrevBlock, pNextBlock, size, oldsize);
-}
-FX_LPVOID CFXMEM_Page::Realloc(FX_LPVOID p, size_t oldSize, size_t newSize)
-{
- FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos);
- size_t oldnewSize = newSize;
-#if _FX_WORDSIZE_ == _FX_W64_
- newSize = (newSize + 31) / 32 * 32;
-#else
- newSize = (newSize + 7) / 8 * 8;
-#endif
- CFXMEM_Block *pPrevBlock = &m_AvailHead;
- CFXMEM_Block *pNextBlock, *pPrevPrev;
- CFXMEM_Block *pBlock = (CFXMEM_Block*)p - 1;
- pPrevPrev = NULL;
- while (TRUE) {
- pNextBlock = pPrevBlock->m_pNextBlock;
- if (pNextBlock == NULL || pNextBlock > pBlock) {
- break;
- }
- if (pPrevBlock != &m_AvailHead && (FX_LPBYTE)pNextBlock == (FX_LPBYTE)(pPrevBlock + 1) + pPrevBlock->m_nBlockSize) {
- m_nAvailSize += sizeof(CFXMEM_Block);
- pPrevBlock->m_nBlockSize += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block);
- pPrevBlock->m_pNextBlock = pNextBlock->m_pNextBlock;
- } else {
- pPrevPrev = pPrevBlock;
- pPrevBlock = pNextBlock;
- }
- }
- if (pNextBlock) {
- CFXMEM_Block* pCurBlock = pNextBlock->m_pNextBlock;
- while ((FX_LPBYTE)pCurBlock == (FX_LPBYTE)(pNextBlock + 1) + pNextBlock->m_nBlockSize) {
- m_nAvailSize += sizeof(CFXMEM_Block);
- pNextBlock->m_nBlockSize += pCurBlock->m_nBlockSize + sizeof(CFXMEM_Block);
- pCurBlock = pCurBlock->m_pNextBlock;
- pNextBlock->m_pNextBlock = pCurBlock;
- }
- }
- size_t size = 0;
- FX_DWORD dwFlags = 0;
- if (pPrevBlock != &m_AvailHead && (FX_LPBYTE)pBlock == (FX_LPBYTE)(pPrevBlock + 1) + pPrevBlock->m_nBlockSize) {
- size += pPrevBlock->m_nBlockSize + oldSize + sizeof(CFXMEM_Block);
- dwFlags |= 0x10;
- }
- if (pNextBlock && (FX_LPBYTE)pNextBlock == (FX_LPBYTE)p + oldSize) {
- size += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block);
- dwFlags |= 0x01;
- }
- if (size >= newSize) {
- m_nAvailSize += pBlock->m_nBlockSize;
- CFXMEM_Block* pCurBlock = pBlock;
- if (dwFlags & 0x10) {
- pCurBlock = pPrevBlock;
- m_nAvailSize += sizeof(CFXMEM_Block);
- pCurBlock->m_nBlockSize += pBlock->m_nBlockSize + sizeof(CFXMEM_Block);
- pPrevBlock = pPrevPrev;
- }
- if (dwFlags & 0x01) {
- m_nAvailSize += sizeof(CFXMEM_Block);
- pCurBlock->m_nBlockSize += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block);
- pCurBlock->m_pNextBlock = pNextBlock->m_pNextBlock;
- }
- if (pCurBlock != pBlock) {
- FXSYS_memmove32((FX_LPVOID)(pCurBlock + 1), p, oldSize);
- }
- return Alloc(pPrevBlock, pCurBlock, newSize, oldnewSize);
- }
- return NULL;
-}
-void CFXMEM_Page::Free(FX_LPVOID p)
-{
- FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos);
- CFXMEM_Block *pPrevBlock = &m_AvailHead;
- CFXMEM_Block *pNextBlock;
- CFXMEM_Block *pBlock = (CFXMEM_Block*)p - 1;
- m_nAvailSize += pBlock->m_nBlockSize;
- while (TRUE) {
- pNextBlock = pPrevBlock->m_pNextBlock;
- if (pNextBlock == NULL || pNextBlock > pBlock) {
- break;
- }
- if (pPrevBlock != &m_AvailHead && (FX_LPBYTE)pNextBlock == (FX_LPBYTE)(pPrevBlock + 1) + pPrevBlock->m_nBlockSize) {
- m_nAvailSize += sizeof(CFXMEM_Block);
- pPrevBlock->m_nBlockSize += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block);
- pPrevBlock->m_pNextBlock = pNextBlock->m_pNextBlock;
- } else {
- pPrevBlock = pNextBlock;
- }
- }
- while ((FX_LPBYTE)pNextBlock == (FX_LPBYTE)(pBlock + 1) + pBlock->m_nBlockSize) {
- m_nAvailSize += sizeof(CFXMEM_Block);
- pBlock->m_nBlockSize += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block);
- pNextBlock = pNextBlock->m_pNextBlock;
- }
- pBlock->m_pNextBlock = pNextBlock;
- if (pPrevBlock != &m_AvailHead && (FX_LPBYTE)pBlock == (FX_LPBYTE)(pPrevBlock + 1) + pPrevBlock->m_nBlockSize) {
- m_nAvailSize += sizeof(CFXMEM_Block);
- pPrevBlock->m_nBlockSize += pBlock->m_nBlockSize + sizeof(CFXMEM_Block);
- pPrevBlock->m_pNextBlock = pBlock->m_pNextBlock;
- } else {
- FXSYS_assert(pPrevBlock != pBlock);
- pPrevBlock->m_pNextBlock = pBlock;
- }
-}
-void CFXMEM_Pages::Initialize(FX_LPBYTE pStart, size_t pageSize, size_t pages)
-{
- m_pStartPage = m_pCurPage = (CFXMEM_Page*)pStart;
- m_nPageSize = pageSize;
- for (size_t n = 0; n < pages; n++) {
- ((CFXMEM_Page*)pStart)->Initialize(pageSize);
- pStart += pageSize;
- }
- m_pLimitPos = (CFXMEM_Page*)pStart;
-}
-FX_BOOL CFXMEM_Pages::IsEmpty() const
-{
- if (m_pStartPage >= m_pLimitPos) {
- return TRUE;
- }
- FX_LPBYTE pPage = (FX_LPBYTE)m_pStartPage;
- while (pPage < (FX_LPBYTE)m_pLimitPos) {
- if (!((CFXMEM_Page*)pPage)->IsEmpty()) {
- return FALSE;
- }
- pPage += m_nPageSize;
- }
- return TRUE;
-}
-FX_LPVOID CFXMEM_Pages::Alloc(size_t size)
-{
- CFXMEM_Page *pCurPage = m_pCurPage;
- do {
- FX_LPVOID p = m_pCurPage->Alloc(size);
- if (p) {
- return p;
- }
- m_pCurPage = (CFXMEM_Page*)((FX_LPBYTE)m_pCurPage + m_nPageSize);
- if (m_pCurPage == m_pLimitPos) {
- m_pCurPage = m_pStartPage;
- }
- } while (m_pCurPage != pCurPage);
- return NULL;
-}
-FX_LPVOID CFXMEM_Pages::Realloc(FX_LPVOID p, size_t oldSize, size_t newSize)
-{
- FXSYS_assert (p > (FX_LPVOID)m_pStartPage && p < (FX_LPVOID)m_pLimitPos);
- CFXMEM_Page* pPage = (CFXMEM_Page*)((FX_LPBYTE)m_pStartPage + ((FX_LPBYTE)p - (FX_LPBYTE)m_pStartPage) / m_nPageSize * m_nPageSize);
- return pPage->Realloc(p, oldSize, newSize);
-}
-void CFXMEM_Pages::Free(FX_LPVOID p)
-{
- FXSYS_assert (p > (FX_LPVOID)m_pStartPage && p < (FX_LPVOID)m_pLimitPos);
- CFXMEM_Page* pPage = (CFXMEM_Page*)((FX_LPBYTE)m_pStartPage + ((FX_LPBYTE)p - (FX_LPBYTE)m_pStartPage) / m_nPageSize * m_nPageSize);
- pPage->Free(p);
-}
-void CFXMEM_Pool::Initialize(const FX_MEMCONFIG* pMemConfig, size_t size, size_t pageNum8Bytes, size_t pageNum16Bytes, size_t pageNum32Bytes, size_t pageNumMid)
-{
- m_pPrevPool = NULL;
- m_pNextPool = NULL;
- m_bAlone = FALSE;
- FX_LPBYTE pPage = (FX_LPBYTE)this + sizeof(CFXMEM_Pool);
- size -= sizeof(CFXMEM_Pool);
- m_8BytesPages.Initialize(pPage, pageNum8Bytes);
- pPage += pageNum8Bytes * FX_FIXEDMEM_PAGESIZE;
- size -= pageNum8Bytes * FX_FIXEDMEM_PAGESIZE;
- m_16BytesPages.Initialize(pPage, pageNum16Bytes);
- pPage += pageNum16Bytes * FX_FIXEDMEM_PAGESIZE;
- size -= pageNum16Bytes * FX_FIXEDMEM_PAGESIZE;
- m_32BytesPages.Initialize(pPage, pageNum32Bytes);
- pPage += pageNum32Bytes * FX_FIXEDMEM_PAGESIZE;
- size -= pageNum32Bytes * FX_FIXEDMEM_PAGESIZE;
- m_MidPages.Initialize(pPage, pMemConfig->nPageSize_Mid * FX_FIXEDMEM_PAGESIZE, pageNumMid);
- pPage += pageNumMid * pMemConfig->nPageSize_Mid * FX_FIXEDMEM_PAGESIZE;
- size -= pageNumMid * pMemConfig->nPageSize_Mid * FX_FIXEDMEM_PAGESIZE;
- if (size < FX_FIXEDMEM_MIDBLOCKSIZE) {
- m_pLargePage = NULL;
- } else {
- m_pLargePage = (CFXMEM_Page*)pPage;
- m_pLargePage->Initialize(size);
- }
- m_pLimitPos = pPage + size;
-}
-FX_BOOL CFXMEM_Pool::IsEmpty() const
-{
- if (!m_8BytesPages.IsEmpty()) {
- return FALSE;
- }
- if (!m_16BytesPages.IsEmpty()) {
- return FALSE;
- }
- if (!m_32BytesPages.IsEmpty()) {
- return FALSE;
- }
- if (!m_MidPages.IsEmpty()) {
- return FALSE;
- }
- return !m_pLargePage || m_pLargePage->IsEmpty();
-}
-size_t CFXMEM_Pool::GetSize(FX_LPVOID p) const
-{
- FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos);
- if (p < (FX_LPVOID)m_8BytesPages.m_pLimitPos) {
- return 8;
- }
- if (p < (FX_LPVOID)m_16BytesPages.m_pLimitPos) {
- return 16;
- }
- if (p < (FX_LPVOID)m_32BytesPages.m_pLimitPos) {
- return 32;
- }
- return ((CFXMEM_Block*)p - 1)->m_nBlockSize;
-}
-FX_LPVOID CFXMEM_Pool::Realloc(FX_LPVOID p, size_t oldSize, size_t newSize)
-{
- FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos);
- if (p > (FX_LPVOID)m_32BytesPages.m_pLimitPos) {
- if (p < (FX_LPVOID)m_MidPages.m_pLimitPos) {
- return m_MidPages.Realloc(p, oldSize, newSize);
- } else if (m_pLargePage) {
- return m_pLargePage->Realloc(p, oldSize, newSize);
- }
- }
- return NULL;
-}
-void CFXMEM_Pool::Free(FX_LPVOID p)
-{
- FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos);
- if (p < (FX_LPVOID)m_32BytesPages.m_pLimitPos) {
- if (p < (FX_LPVOID)m_8BytesPages.m_pLimitPos) {
- m_8BytesPages.Free(p);
- } else if (p < (FX_LPVOID)m_16BytesPages.m_pLimitPos) {
- m_16BytesPages.Free(p);
- } else {
- m_32BytesPages.Free(p);
- }
- return;
- } else if (p < (FX_LPVOID)m_MidPages.m_pLimitPos) {
- m_MidPages.Free(p);
- } else {
- m_pLargePage->Free(p);
- }
-}
-void CFXMEM_FixedMgr::Initialize(size_t size)
-{
- m_MemConfig = g_MemConfig;
- FXSYS_memset32(&m_SystemMgr, 0, sizeof m_SystemMgr);
- m_SystemMgr.Alloc = FixedAlloc;
- m_SystemMgr.AllocDebug = FixedAllocDebug;
- m_SystemMgr.Free = FixedFree;
- m_SystemMgr.Realloc = FixedRealloc;
- m_SystemMgr.ReallocDebug = FixedReallocDebug;
- m_SystemMgr.CollectAll = FixedCollectAll;
- m_SystemMgr.Purge = FixedPurge;
- m_SystemMgr.user = this;
- size -= sizeof(CFXMEM_FixedMgr);
- size_t nMidPages = 0;
- if (m_MemConfig.nPageSize_Mid) {
- nMidPages = (size - (m_MemConfig.nPageNum_Init8 + m_MemConfig.nPageNum_Init16 + m_MemConfig.nPageNum_Init32) * FX_FIXEDMEM_PAGESIZE) / (m_MemConfig.nPageSize_Mid * FX_FIXEDMEM_PAGESIZE);
- if (nMidPages > m_MemConfig.nPageNum_InitMid) {
- nMidPages = m_MemConfig.nPageNum_InitMid;
- }
- }
- m_FirstPool.Initialize(&m_MemConfig, size, m_MemConfig.nPageNum_Init8, m_MemConfig.nPageNum_Init16, m_MemConfig.nPageNum_Init32, nMidPages);
-}
-FX_LPVOID CFXMEM_FixedMgr::Alloc16(CFXMEM_Pool **pp32Pool, size_t size)
-{
- CFXMEM_Pool *pPool = &m_FirstPool;
- do {
- CFXMEM_16BytesPages &pages = pPool->m_16BytesPages;
- if (pages.HasFreeBlock()) {
- return pages.Alloc(size);
- }
- if (pp32Pool && pPool->m_32BytesPages.HasFreeBlock()) {
- *pp32Pool = pPool;
- }
- pPool = pPool->m_pNextPool;
- } while(pPool);
- return NULL;
-}
-FX_LPVOID CFXMEM_FixedMgr::Alloc32(size_t size)
-{
- if (size <= 8) {
- CFXMEM_8BytesPages &pages = m_FirstPool.m_8BytesPages;
- if (pages.HasFreeBlock()) {
- return pages.Alloc(size);
- }
- }
- CFXMEM_Pool *p32BytesPool;
- if (size <= 16) {
- p32BytesPool = NULL;
- FX_LPVOID p = Alloc16(&p32BytesPool, size);
- if (p) {
- return p;
- }
- } else {
- p32BytesPool = &m_FirstPool;
- }
- while (p32BytesPool) {
- CFXMEM_32BytesPages &pages = p32BytesPool->m_32BytesPages;
- if (pages.HasFreeBlock()) {
- return pages.Alloc(size);
- }
- p32BytesPool = p32BytesPool->m_pNextPool;
- }
- return NULL;
-}
-FX_LPVOID CFXMEM_FixedMgr::AllocSmall(size_t size)
-{
- FX_LPVOID p = Alloc32(size);
- if (p) {
- return p;
- }
- if (!m_pExtender) {
- return NULL;
- }
- size_t requiredSize = (m_MemConfig.nPageNum_More16 + m_MemConfig.nPageNum_More32) * FX_FIXEDMEM_PAGESIZE;
- if (!requiredSize) {
- return NULL;
- }
- CFXMEM_Pool *pNewPool = NULL;
- requiredSize += sizeof(CFXMEM_Pool);
- size_t newSize = requiredSize;
- if (!m_pExtender->More(m_pExtender, newSize, (void**)&pNewPool, &newSize)) {
- return NULL;
- }
- size_t nMidPages = 0;
- if (m_MemConfig.nPageSize_Mid) {
- nMidPages = (newSize - requiredSize) / (m_MemConfig.nPageSize_Mid * FX_FIXEDMEM_PAGESIZE);
- if (nMidPages > m_MemConfig.nPageNum_MoreMid) {
- nMidPages = m_MemConfig.nPageNum_MoreMid;
- }
- }
- pNewPool->Initialize(&m_MemConfig, newSize, 0, m_MemConfig.nPageNum_More16, m_MemConfig.nPageNum_More32, nMidPages);
- pNewPool->m_pPrevPool = &m_FirstPool;
- CFXMEM_Pool *pPool = m_FirstPool.m_pNextPool;
- pNewPool->m_pNextPool = pPool;
- if (pPool) {
- pPool->m_pPrevPool = pNewPool;
- }
- m_FirstPool.m_pNextPool = pNewPool;
- return Alloc32(size);
-}
-FX_LPVOID CFXMEM_FixedMgr::AllocMid(size_t size)
-{
- CFXMEM_Pool *pPool = &m_FirstPool;
- do {
- CFXMEM_Pages &pages = pPool->m_MidPages;
- if (pages.m_pLimitPos > pages.m_pStartPage) {
- FX_LPVOID p = pages.Alloc(size);
- if (p) {
- return p;
- }
- }
- pPool = pPool->m_pNextPool;
- } while(pPool);
- if (!m_pExtender) {
- return NULL;
- }
- size_t newSize = m_MemConfig.nPageSize_Mid * FX_FIXEDMEM_PAGESIZE * m_MemConfig.nPageNum_MoreMid;
- if (!newSize) {
- return NULL;
- }
- CFXMEM_Pool *pNewPool = NULL;
- newSize += sizeof(CFXMEM_Pool);
- if (!m_pExtender->More(m_pExtender, newSize, (void**)&pNewPool, &newSize)) {
- return NULL;
- }
- size_t nMidPages = (newSize - sizeof(CFXMEM_Pool)) / (m_MemConfig.nPageSize_Mid * FX_FIXEDMEM_PAGESIZE);
- if (nMidPages > m_MemConfig.nPageNum_MoreMid) {
- nMidPages = m_MemConfig.nPageNum_MoreMid;
- }
- pNewPool->Initialize(&m_MemConfig, newSize, 0, 0, 0, nMidPages);
- pNewPool->m_pPrevPool = &m_FirstPool;
- pPool = m_FirstPool.m_pNextPool;
- pNewPool->m_pNextPool = pPool;
- if (pPool) {
- pPool->m_pPrevPool = pNewPool;
- }
- m_FirstPool.m_pNextPool = pNewPool;
- return pNewPool->m_MidPages.Alloc(size);
-}
-FX_LPVOID CFXMEM_FixedMgr::AllocLarge(size_t size)
-{
- CFXMEM_Pool *pPool = &m_FirstPool;
- do {
- if (!pPool->m_bAlone && pPool->m_pLargePage) {
- FX_LPVOID p = pPool->m_pLargePage->Alloc(size);
- if (p) {
- return p;
- }
- }
- pPool = pPool->m_pNextPool;
- } while(pPool);
- if (!m_pExtender || !m_MemConfig.nPageSize_Large) {
- return NULL;
- }
- CFXMEM_Pool *pNewPool = NULL;
-#if _FX_WORDSIZE_ == _FX_W64_
- size_t newSize = ((size + 31) / 32 * 32 + sizeof(CFXMEM_Pool) + sizeof(CFXMEM_Page) + sizeof(CFXMEM_Block) + 4095) / 4096 * 4096;
-#else
- size_t newSize = (size + 7) / 8 * 8 + sizeof(CFXMEM_Pool) + sizeof(CFXMEM_Page) + sizeof(CFXMEM_Block);
-#endif
- if (newSize < m_MemConfig.nPageSize_Large * FX_FIXEDMEM_PAGESIZE) {
- newSize = m_MemConfig.nPageSize_Large * FX_FIXEDMEM_PAGESIZE;
- }
- if (!m_pExtender->More(m_pExtender, newSize, (void**)&pNewPool, &newSize)) {
- return NULL;
- }
- pNewPool->Initialize(&m_MemConfig, newSize, 0, 0, 0, 0);
- pNewPool->m_bAlone = size >= m_MemConfig.nPageSize_Alone * FX_FIXEDMEM_PAGESIZE;
- pNewPool->m_pPrevPool = &m_FirstPool;
- pPool = m_FirstPool.m_pNextPool;
- pNewPool->m_pNextPool = pPool;
- if (pPool) {
- pPool->m_pPrevPool = pNewPool;
- }
- m_FirstPool.m_pNextPool = pNewPool;
- return pNewPool->m_pLargePage->Alloc(size);
-}
-size_t CFXMEM_FixedMgr::GetSize(FX_LPVOID p) const
-{
- const CFXMEM_Pool *pFind = &m_FirstPool;
- do {
- if (p > (FX_LPVOID)pFind && p < pFind->m_pLimitPos) {
- return pFind->GetSize(p);
- }
- pFind = pFind->m_pNextPool;
- } while (pFind);
- return 0;
-}
-FX_LPVOID CFXMEM_FixedMgr::Alloc(size_t size)
-{
- FX_LPVOID p;
- if (size <= 32) {
- p = AllocSmall(size);
- if (p) {
- return p;
- }
- }
- if (size <= FX_FIXEDMEM_MIDBLOCKSIZE) {
- p = AllocMid(size);
- if (p) {
- return p;
- }
- }
- p = AllocLarge(size);
- return p;
-}
-FX_LPVOID CFXMEM_FixedMgr::ReallocSmall(CFXMEM_Pool* pPool, FX_LPVOID p, size_t oldSize, size_t newSize)
-{
- FX_LPVOID np = AllocSmall(newSize);
- if (!np) {
- return NULL;
- }
- FXSYS_memcpy32(np, p, oldSize);
- pPool->Free(p);
- return np;
-}
-FX_LPVOID CFXMEM_FixedMgr::Realloc(FX_LPVOID p, size_t newSize)
-{
- if (!p) {
- return Alloc(newSize);
- }
- size_t oldSize = 0;
- CFXMEM_Pool *pFind = &m_FirstPool;
- do {
- if (p > (FX_LPVOID)pFind && p < pFind->m_pLimitPos) {
- oldSize = pFind->GetSize(p);
- if (oldSize >= newSize) {
- return p;
- }
- break;
- }
- pFind = pFind->m_pNextPool;
- } while (pFind);
- if (!oldSize || !pFind) {
- return Alloc(newSize);
- }
- FX_LPVOID np = NULL;
- if (newSize <= 32) {
- np = ReallocSmall(pFind, p, oldSize, newSize);
- if (np) {
- return np;
- }
- }
- if (newSize <= FX_FIXEDMEM_MIDBLOCKSIZE) {
- np = pFind->Realloc(p, oldSize, newSize);
- if (np) {
- return np;
- }
- }
- np = Alloc(newSize);
- if (np) {
- FXSYS_memcpy32(np, p, oldSize);
- pFind->Free(p);
- }
- if (pFind->m_bAlone && pFind->IsEmpty()) {
- FreePool(pFind);
- }
- return np;
-}
-void CFXMEM_FixedMgr::Free(FX_LPVOID p)
-{
- CFXMEM_Pool *pFind = &m_FirstPool;
- do {
- if (p > (FX_LPVOID)pFind && p < pFind->m_pLimitPos) {
- pFind->Free(p);
- if (pFind->m_bAlone && pFind->IsEmpty()) {
- FreePool(pFind);
- }
- return;
- }
- pFind = pFind->m_pNextPool;
- } while (pFind);
-}
-void CFXMEM_FixedMgr::FreePool(CFXMEM_Pool* pPool)
-{
- FXSYS_assert(pPool->m_bAlone && pPool->IsEmpty());
- FXSYS_assert(m_pExtender != NULL);
- CFXMEM_Pool* pPrevPool = pPool->m_pPrevPool;
- CFXMEM_Pool* pNextPool = pPool->m_pNextPool;
- if (pPrevPool) {
- pPrevPool->m_pNextPool = pNextPool;
- }
- if (pNextPool) {
- pNextPool->m_pPrevPool = pPrevPool;
- }
- m_pExtender->Free(m_pExtender, pPool);
-}
-void CFXMEM_FixedMgr::FreeAll()
-{
- if (!m_pExtender) {
- return;
- }
- CFXMEM_Pool* pPool = m_FirstPool.m_pNextPool;
- while (pPool) {
- CFXMEM_Pool* pPrevPool = pPool;
- pPool = pPool->m_pNextPool;
- m_pExtender->Free(m_pExtender, pPrevPool);
- }
- m_FirstPool.m_pNextPool = NULL;
-}
-void CFXMEM_FixedMgr::Purge()
-{
- if (!m_pExtender) {
- return;
- }
- CFXMEM_Pool* pPool = m_FirstPool.m_pNextPool;
- while (pPool) {
- CFXMEM_Pool* pNextPool = pPool->m_pNextPool;
- if (pPool->IsEmpty()) {
- CFXMEM_Pool* pPrevPool = pPool->m_pPrevPool;
- pPrevPool->m_pNextPool = pNextPool;
- if (pNextPool) {
- pNextPool->m_pPrevPool = pPrevPool;
- }
- m_pExtender->Free(m_pExtender, pPool);
- }
- pPool = pNextPool;
- }
-}
-extern const FX_BYTE OneLeadPos[256] = {
- 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
- 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-};
-extern const FX_BYTE ZeroLeadPos[256] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
- 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8,
-};
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_ext.h" +#include "mem_int.h" +#ifdef _FPDFAPI_MINI_ +static FX_MEMCONFIG g_MemConfig = { + 1, + 5, + 8, + 4, + 12, + 8, + 2, + 4, + 32, + 64, +}; +#else +static FX_MEMCONFIG g_MemConfig = { + 1, + 8, + 24, + 8, + 32, + 16, + 4, + 8, + 128, + 64, +}; +#endif +void FXMEM_SetConfig(const FX_MEMCONFIG* memConfig) +{ + g_MemConfig = *memConfig; +} +#ifdef __cplusplus +extern "C" { +#endif +static void* FixedAlloc(FXMEM_SystemMgr* pMgr, size_t size, int flags) +{ + return ((CFXMEM_FixedMgr*)pMgr->user)->Alloc(size); +} +static void* FixedAllocDebug(FXMEM_SystemMgr* pMgr, size_t size, int flags, FX_LPCSTR file, int line) +{ + return ((CFXMEM_FixedMgr*)pMgr->user)->Alloc(size); +} +static void* FixedRealloc(FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags) +{ + return ((CFXMEM_FixedMgr*)pMgr->user)->Realloc(pointer, size); +} +static void* FixedReallocDebug(FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags, FX_LPCSTR file, int line) +{ + return ((CFXMEM_FixedMgr*)pMgr->user)->Realloc(pointer, size); +} +static void FixedFree(FXMEM_SystemMgr* pMgr, void* pointer, int flags) +{ + ((CFXMEM_FixedMgr*)pMgr->user)->Free(pointer); +} +static void FixedPurge(FXMEM_SystemMgr* pMgr) +{ + ((CFXMEM_FixedMgr*)pMgr->user)->Purge(); +} +static void FixedCollectAll(FXMEM_SystemMgr* pMgr) +{ + ((CFXMEM_FixedMgr*)pMgr->user)->FreeAll(); +} +#define FIXEDMEM_MINIMUMSIZE (1024 * 1024 * 8) +FXMEM_FoxitMgr* FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible) +{ + if (size < FIXEDMEM_MINIMUMSIZE) { + size = FIXEDMEM_MINIMUMSIZE; + } + FX_LPVOID pMemory = malloc(size); + if (!pMemory) { + return NULL; + } + CFixedMgr_Proxy* pProxy = (CFixedMgr_Proxy*)pMemory; + size_t offsetSize = (sizeof(CFixedMgr_Proxy) + 15) / 16 * 16; + FXMEM_FoxitMgr* pFoxitMgr = pProxy->Initialize((FX_LPBYTE)pProxy + offsetSize, size - offsetSize, extensible); + if (!pFoxitMgr) { + free(pMemory); + return NULL; + } + g_pDefFoxitMgr = (CFX_MemoryMgr*)pFoxitMgr; + g_pDefFoxitMgr->m_pExternalMemory = pMemory; + return pFoxitMgr; +} +FXMEM_FoxitMgr* FXMEM_CreateFixedMgr(void* pMemory, size_t size, FXMEM_SystemMgr2* pSystemMgr) +{ + if (pMemory == NULL || size < FX_FIXEDMEM_PAGESIZE) { + return NULL; + } + if (!pSystemMgr && size >= FIXEDMEM_PROXYSIZE_1) { + CFixedMgr_Proxy* pProxy = (CFixedMgr_Proxy*)pMemory; + size_t offsetSize = (sizeof(CFixedMgr_Proxy) + 15) / 16 * 16; + return pProxy->Initialize((FX_LPBYTE)pProxy + offsetSize, size - offsetSize, FALSE); + } + CFXMEM_FixedMgr* pHeader = (CFXMEM_FixedMgr*)pMemory; + pHeader->Initialize(size); + pHeader->m_pExtender = pSystemMgr; + CFX_MemoryMgr* p = (CFX_MemoryMgr*)pHeader->Alloc(sizeof(CFX_MemoryMgr)); + if (p == NULL) { + return NULL; + } + p->Init(&pHeader->m_SystemMgr); + return (FXMEM_FoxitMgr*)p; +} +size_t FXMEM_GetBlockSizeInFixedMgr(FXMEM_FoxitMgr* pFoxitMgr, void* ptr) +{ + return pFoxitMgr ? ((CFXMEM_FixedMgr*)((CFX_MemoryMgr*)pFoxitMgr)->m_pSystemMgr->user)->GetSize(ptr) : 0; +} +#ifdef __cplusplus +} +#endif +const FX_MEMCONFIG g_ProxyMgr_MemConfigs[6] = { + {1, 2, 4, 0, 2, 2, 2, 0, 0, 0}, + {1, 4, 8, 0, 2, 2, 2, 0, 0, 0}, + {1, 4, 16, 4, 8, 8, 2, 1, 16, 16}, + {1, 8, 24, 4, 12, 12, 4, 2, 32, 16}, + {1, 8, 24, 8, 16, 16, 4, 2, 64, 32}, + {1, 8, 24, 8, 24, 32, 4, 2, 128, 64}, +}; +const FX_MEMCONFIG* FixedMgr_GetConfig(size_t nSize) +{ + int index = 5; + if (nSize <= FIXEDMEM_PROXYSIZE_0) { + index = 0; + } else if (nSize <= FIXEDMEM_PROXYSIZE_1) { + index = 1; + } else if (nSize <= FIXEDMEM_PROXYSIZE_2) { + index = 2; + } else if (nSize <= FIXEDMEM_PROXYSIZE_3) { + index = 3; + } else if (nSize <= FIXEDMEM_PROXYSIZE_4) { + index = 4; + } + return &g_ProxyMgr_MemConfigs[index]; +} +FXMEM_FoxitMgr* CFixedMgr_Proxy::Initialize(FX_LPVOID pBuffer, size_t nSize, FX_BOOL bExtensible) +{ + FXSYS_assert(pBuffer != NULL && nSize >= FIXEDMEM_PROXYSIZE_1 - sizeof(CFixedMgr_Proxy)); + FXMEM_SetConfig(FixedMgr_GetConfig(nSize)); + m_SystemMgr.More = &CFixedMgr_Proxy::Common_More; + m_SystemMgr.Free = &CFixedMgr_Proxy::Common_Free; + m_pFixedPage = (CFXMEM_Page*)((FX_LPBYTE)pBuffer + FIXEDMEM_PROXYSIZE_0); + m_pFixedPage->Initialize(nSize - FIXEDMEM_PROXYSIZE_0); + m_pBuffer = pBuffer; + m_nSize = nSize; + m_bExtensible = bExtensible; + return FXMEM_CreateFixedMgr(pBuffer, FIXEDMEM_PROXYSIZE_0, &m_SystemMgr); +} +FX_BOOL CFixedMgr_Proxy::Common_More(FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size) +{ + CFixedMgr_Proxy* pProxyMgr = (CFixedMgr_Proxy*)pMgr; + FXSYS_assert(pProxyMgr != NULL && pProxyMgr->m_pFixedPage != NULL); + *new_size = alloc_size; + *new_memory = pProxyMgr->m_pFixedPage->Alloc(alloc_size); + if (*new_memory == NULL && pProxyMgr->m_bExtensible) { + *new_memory = malloc(alloc_size); + } + return *new_memory != NULL; +} +void CFixedMgr_Proxy::Common_Free(FXMEM_SystemMgr2* pMgr, void* memory) +{ + CFixedMgr_Proxy* pProxyMgr = (CFixedMgr_Proxy*)pMgr; + FXSYS_assert(pProxyMgr != NULL && pProxyMgr->m_pFixedPage != NULL); + if (memory > pProxyMgr->m_pBuffer && memory < (FX_LPBYTE)pProxyMgr->m_pBuffer + pProxyMgr->m_nSize) { + pProxyMgr->m_pFixedPage->Free(memory); + } else if (pProxyMgr->m_bExtensible) { + free(memory); + } +} +void CFXMEM_Page::Initialize(size_t size) +{ + CFXMEM_Block *pFirstBlock = (CFXMEM_Block*)(this + 1); + m_nAvailSize = size - sizeof(CFXMEM_Page) - sizeof(CFXMEM_Block); + pFirstBlock->m_nBlockSize = m_nAvailSize; + pFirstBlock->m_pNextBlock = NULL; + m_AvailHead.m_nBlockSize = m_nAvailSize; + m_AvailHead.m_pNextBlock = pFirstBlock; + m_pLimitPos = (CFXMEM_Block*)((FX_LPBYTE)this + size); +} +FX_LPVOID CFXMEM_Page::Alloc(CFXMEM_Block* pPrevBlock, CFXMEM_Block* pNextBlock, size_t size, size_t oldsize) +{ + size_t gap = pNextBlock->m_nBlockSize - size; + if (gap <= 64 + sizeof(CFXMEM_Block)) { + pPrevBlock->m_pNextBlock = pNextBlock->m_pNextBlock; + m_nAvailSize -= pNextBlock->m_nBlockSize; + } else { + m_nAvailSize -= size + sizeof(CFXMEM_Block); + pNextBlock->m_nBlockSize = size; + CFXMEM_Block *pNewBlock = (CFXMEM_Block*)((FX_LPBYTE)(pNextBlock + 1) + size); + pNewBlock->m_nBlockSize = gap - sizeof(CFXMEM_Block); + pNewBlock->m_pNextBlock = pNextBlock->m_pNextBlock; + pPrevBlock->m_pNextBlock = pNewBlock; + } + return (FX_LPVOID)(pNextBlock + 1); +} +FX_LPVOID CFXMEM_Page::Alloc(size_t size) +{ + size_t oldsize = size; +#if _FX_WORDSIZE_ == _FX_W64_ + size = (size + 31) / 32 * 32; +#else + size = (size + 7) / 8 * 8; +#endif + if (m_nAvailSize < size) { + return NULL; + } + CFXMEM_Block *pNextBlock; + CFXMEM_Block *pPrevBlock = &m_AvailHead; + while (TRUE) { + pNextBlock = pPrevBlock->m_pNextBlock; + if (!pNextBlock) { + return NULL; + } + if (pNextBlock->m_nBlockSize >= size) { + break; + } + pPrevBlock = pNextBlock; + } + return Alloc(pPrevBlock, pNextBlock, size, oldsize); +} +FX_LPVOID CFXMEM_Page::Realloc(FX_LPVOID p, size_t oldSize, size_t newSize) +{ + FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos); + size_t oldnewSize = newSize; +#if _FX_WORDSIZE_ == _FX_W64_ + newSize = (newSize + 31) / 32 * 32; +#else + newSize = (newSize + 7) / 8 * 8; +#endif + CFXMEM_Block *pPrevBlock = &m_AvailHead; + CFXMEM_Block *pNextBlock, *pPrevPrev; + CFXMEM_Block *pBlock = (CFXMEM_Block*)p - 1; + pPrevPrev = NULL; + while (TRUE) { + pNextBlock = pPrevBlock->m_pNextBlock; + if (pNextBlock == NULL || pNextBlock > pBlock) { + break; + } + if (pPrevBlock != &m_AvailHead && (FX_LPBYTE)pNextBlock == (FX_LPBYTE)(pPrevBlock + 1) + pPrevBlock->m_nBlockSize) { + m_nAvailSize += sizeof(CFXMEM_Block); + pPrevBlock->m_nBlockSize += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block); + pPrevBlock->m_pNextBlock = pNextBlock->m_pNextBlock; + } else { + pPrevPrev = pPrevBlock; + pPrevBlock = pNextBlock; + } + } + if (pNextBlock) { + CFXMEM_Block* pCurBlock = pNextBlock->m_pNextBlock; + while ((FX_LPBYTE)pCurBlock == (FX_LPBYTE)(pNextBlock + 1) + pNextBlock->m_nBlockSize) { + m_nAvailSize += sizeof(CFXMEM_Block); + pNextBlock->m_nBlockSize += pCurBlock->m_nBlockSize + sizeof(CFXMEM_Block); + pCurBlock = pCurBlock->m_pNextBlock; + pNextBlock->m_pNextBlock = pCurBlock; + } + } + size_t size = 0; + FX_DWORD dwFlags = 0; + if (pPrevBlock != &m_AvailHead && (FX_LPBYTE)pBlock == (FX_LPBYTE)(pPrevBlock + 1) + pPrevBlock->m_nBlockSize) { + size += pPrevBlock->m_nBlockSize + oldSize + sizeof(CFXMEM_Block); + dwFlags |= 0x10; + } + if (pNextBlock && (FX_LPBYTE)pNextBlock == (FX_LPBYTE)p + oldSize) { + size += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block); + dwFlags |= 0x01; + } + if (size >= newSize) { + m_nAvailSize += pBlock->m_nBlockSize; + CFXMEM_Block* pCurBlock = pBlock; + if (dwFlags & 0x10) { + pCurBlock = pPrevBlock; + m_nAvailSize += sizeof(CFXMEM_Block); + pCurBlock->m_nBlockSize += pBlock->m_nBlockSize + sizeof(CFXMEM_Block); + pPrevBlock = pPrevPrev; + } + if (dwFlags & 0x01) { + m_nAvailSize += sizeof(CFXMEM_Block); + pCurBlock->m_nBlockSize += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block); + pCurBlock->m_pNextBlock = pNextBlock->m_pNextBlock; + } + if (pCurBlock != pBlock) { + FXSYS_memmove32((FX_LPVOID)(pCurBlock + 1), p, oldSize); + } + return Alloc(pPrevBlock, pCurBlock, newSize, oldnewSize); + } + return NULL; +} +void CFXMEM_Page::Free(FX_LPVOID p) +{ + FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos); + CFXMEM_Block *pPrevBlock = &m_AvailHead; + CFXMEM_Block *pNextBlock; + CFXMEM_Block *pBlock = (CFXMEM_Block*)p - 1; + m_nAvailSize += pBlock->m_nBlockSize; + while (TRUE) { + pNextBlock = pPrevBlock->m_pNextBlock; + if (pNextBlock == NULL || pNextBlock > pBlock) { + break; + } + if (pPrevBlock != &m_AvailHead && (FX_LPBYTE)pNextBlock == (FX_LPBYTE)(pPrevBlock + 1) + pPrevBlock->m_nBlockSize) { + m_nAvailSize += sizeof(CFXMEM_Block); + pPrevBlock->m_nBlockSize += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block); + pPrevBlock->m_pNextBlock = pNextBlock->m_pNextBlock; + } else { + pPrevBlock = pNextBlock; + } + } + while ((FX_LPBYTE)pNextBlock == (FX_LPBYTE)(pBlock + 1) + pBlock->m_nBlockSize) { + m_nAvailSize += sizeof(CFXMEM_Block); + pBlock->m_nBlockSize += pNextBlock->m_nBlockSize + sizeof(CFXMEM_Block); + pNextBlock = pNextBlock->m_pNextBlock; + } + pBlock->m_pNextBlock = pNextBlock; + if (pPrevBlock != &m_AvailHead && (FX_LPBYTE)pBlock == (FX_LPBYTE)(pPrevBlock + 1) + pPrevBlock->m_nBlockSize) { + m_nAvailSize += sizeof(CFXMEM_Block); + pPrevBlock->m_nBlockSize += pBlock->m_nBlockSize + sizeof(CFXMEM_Block); + pPrevBlock->m_pNextBlock = pBlock->m_pNextBlock; + } else { + FXSYS_assert(pPrevBlock != pBlock); + pPrevBlock->m_pNextBlock = pBlock; + } +} +void CFXMEM_Pages::Initialize(FX_LPBYTE pStart, size_t pageSize, size_t pages) +{ + m_pStartPage = m_pCurPage = (CFXMEM_Page*)pStart; + m_nPageSize = pageSize; + for (size_t n = 0; n < pages; n++) { + ((CFXMEM_Page*)pStart)->Initialize(pageSize); + pStart += pageSize; + } + m_pLimitPos = (CFXMEM_Page*)pStart; +} +FX_BOOL CFXMEM_Pages::IsEmpty() const +{ + if (m_pStartPage >= m_pLimitPos) { + return TRUE; + } + FX_LPBYTE pPage = (FX_LPBYTE)m_pStartPage; + while (pPage < (FX_LPBYTE)m_pLimitPos) { + if (!((CFXMEM_Page*)pPage)->IsEmpty()) { + return FALSE; + } + pPage += m_nPageSize; + } + return TRUE; +} +FX_LPVOID CFXMEM_Pages::Alloc(size_t size) +{ + CFXMEM_Page *pCurPage = m_pCurPage; + do { + FX_LPVOID p = m_pCurPage->Alloc(size); + if (p) { + return p; + } + m_pCurPage = (CFXMEM_Page*)((FX_LPBYTE)m_pCurPage + m_nPageSize); + if (m_pCurPage == m_pLimitPos) { + m_pCurPage = m_pStartPage; + } + } while (m_pCurPage != pCurPage); + return NULL; +} +FX_LPVOID CFXMEM_Pages::Realloc(FX_LPVOID p, size_t oldSize, size_t newSize) +{ + FXSYS_assert (p > (FX_LPVOID)m_pStartPage && p < (FX_LPVOID)m_pLimitPos); + CFXMEM_Page* pPage = (CFXMEM_Page*)((FX_LPBYTE)m_pStartPage + ((FX_LPBYTE)p - (FX_LPBYTE)m_pStartPage) / m_nPageSize * m_nPageSize); + return pPage->Realloc(p, oldSize, newSize); +} +void CFXMEM_Pages::Free(FX_LPVOID p) +{ + FXSYS_assert (p > (FX_LPVOID)m_pStartPage && p < (FX_LPVOID)m_pLimitPos); + CFXMEM_Page* pPage = (CFXMEM_Page*)((FX_LPBYTE)m_pStartPage + ((FX_LPBYTE)p - (FX_LPBYTE)m_pStartPage) / m_nPageSize * m_nPageSize); + pPage->Free(p); +} +void CFXMEM_Pool::Initialize(const FX_MEMCONFIG* pMemConfig, size_t size, size_t pageNum8Bytes, size_t pageNum16Bytes, size_t pageNum32Bytes, size_t pageNumMid) +{ + m_pPrevPool = NULL; + m_pNextPool = NULL; + m_bAlone = FALSE; + FX_LPBYTE pPage = (FX_LPBYTE)this + sizeof(CFXMEM_Pool); + size -= sizeof(CFXMEM_Pool); + m_8BytesPages.Initialize(pPage, pageNum8Bytes); + pPage += pageNum8Bytes * FX_FIXEDMEM_PAGESIZE; + size -= pageNum8Bytes * FX_FIXEDMEM_PAGESIZE; + m_16BytesPages.Initialize(pPage, pageNum16Bytes); + pPage += pageNum16Bytes * FX_FIXEDMEM_PAGESIZE; + size -= pageNum16Bytes * FX_FIXEDMEM_PAGESIZE; + m_32BytesPages.Initialize(pPage, pageNum32Bytes); + pPage += pageNum32Bytes * FX_FIXEDMEM_PAGESIZE; + size -= pageNum32Bytes * FX_FIXEDMEM_PAGESIZE; + m_MidPages.Initialize(pPage, pMemConfig->nPageSize_Mid * FX_FIXEDMEM_PAGESIZE, pageNumMid); + pPage += pageNumMid * pMemConfig->nPageSize_Mid * FX_FIXEDMEM_PAGESIZE; + size -= pageNumMid * pMemConfig->nPageSize_Mid * FX_FIXEDMEM_PAGESIZE; + if (size < FX_FIXEDMEM_MIDBLOCKSIZE) { + m_pLargePage = NULL; + } else { + m_pLargePage = (CFXMEM_Page*)pPage; + m_pLargePage->Initialize(size); + } + m_pLimitPos = pPage + size; +} +FX_BOOL CFXMEM_Pool::IsEmpty() const +{ + if (!m_8BytesPages.IsEmpty()) { + return FALSE; + } + if (!m_16BytesPages.IsEmpty()) { + return FALSE; + } + if (!m_32BytesPages.IsEmpty()) { + return FALSE; + } + if (!m_MidPages.IsEmpty()) { + return FALSE; + } + return !m_pLargePage || m_pLargePage->IsEmpty(); +} +size_t CFXMEM_Pool::GetSize(FX_LPVOID p) const +{ + FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos); + if (p < (FX_LPVOID)m_8BytesPages.m_pLimitPos) { + return 8; + } + if (p < (FX_LPVOID)m_16BytesPages.m_pLimitPos) { + return 16; + } + if (p < (FX_LPVOID)m_32BytesPages.m_pLimitPos) { + return 32; + } + return ((CFXMEM_Block*)p - 1)->m_nBlockSize; +} +FX_LPVOID CFXMEM_Pool::Realloc(FX_LPVOID p, size_t oldSize, size_t newSize) +{ + FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos); + if (p > (FX_LPVOID)m_32BytesPages.m_pLimitPos) { + if (p < (FX_LPVOID)m_MidPages.m_pLimitPos) { + return m_MidPages.Realloc(p, oldSize, newSize); + } else if (m_pLargePage) { + return m_pLargePage->Realloc(p, oldSize, newSize); + } + } + return NULL; +} +void CFXMEM_Pool::Free(FX_LPVOID p) +{ + FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)m_pLimitPos); + if (p < (FX_LPVOID)m_32BytesPages.m_pLimitPos) { + if (p < (FX_LPVOID)m_8BytesPages.m_pLimitPos) { + m_8BytesPages.Free(p); + } else if (p < (FX_LPVOID)m_16BytesPages.m_pLimitPos) { + m_16BytesPages.Free(p); + } else { + m_32BytesPages.Free(p); + } + return; + } else if (p < (FX_LPVOID)m_MidPages.m_pLimitPos) { + m_MidPages.Free(p); + } else { + m_pLargePage->Free(p); + } +} +void CFXMEM_FixedMgr::Initialize(size_t size) +{ + m_MemConfig = g_MemConfig; + FXSYS_memset32(&m_SystemMgr, 0, sizeof m_SystemMgr); + m_SystemMgr.Alloc = FixedAlloc; + m_SystemMgr.AllocDebug = FixedAllocDebug; + m_SystemMgr.Free = FixedFree; + m_SystemMgr.Realloc = FixedRealloc; + m_SystemMgr.ReallocDebug = FixedReallocDebug; + m_SystemMgr.CollectAll = FixedCollectAll; + m_SystemMgr.Purge = FixedPurge; + m_SystemMgr.user = this; + size -= sizeof(CFXMEM_FixedMgr); + size_t nMidPages = 0; + if (m_MemConfig.nPageSize_Mid) { + nMidPages = (size - (m_MemConfig.nPageNum_Init8 + m_MemConfig.nPageNum_Init16 + m_MemConfig.nPageNum_Init32) * FX_FIXEDMEM_PAGESIZE) / (m_MemConfig.nPageSize_Mid * FX_FIXEDMEM_PAGESIZE); + if (nMidPages > m_MemConfig.nPageNum_InitMid) { + nMidPages = m_MemConfig.nPageNum_InitMid; + } + } + m_FirstPool.Initialize(&m_MemConfig, size, m_MemConfig.nPageNum_Init8, m_MemConfig.nPageNum_Init16, m_MemConfig.nPageNum_Init32, nMidPages); +} +FX_LPVOID CFXMEM_FixedMgr::Alloc16(CFXMEM_Pool **pp32Pool, size_t size) +{ + CFXMEM_Pool *pPool = &m_FirstPool; + do { + CFXMEM_16BytesPages &pages = pPool->m_16BytesPages; + if (pages.HasFreeBlock()) { + return pages.Alloc(size); + } + if (pp32Pool && pPool->m_32BytesPages.HasFreeBlock()) { + *pp32Pool = pPool; + } + pPool = pPool->m_pNextPool; + } while(pPool); + return NULL; +} +FX_LPVOID CFXMEM_FixedMgr::Alloc32(size_t size) +{ + if (size <= 8) { + CFXMEM_8BytesPages &pages = m_FirstPool.m_8BytesPages; + if (pages.HasFreeBlock()) { + return pages.Alloc(size); + } + } + CFXMEM_Pool *p32BytesPool; + if (size <= 16) { + p32BytesPool = NULL; + FX_LPVOID p = Alloc16(&p32BytesPool, size); + if (p) { + return p; + } + } else { + p32BytesPool = &m_FirstPool; + } + while (p32BytesPool) { + CFXMEM_32BytesPages &pages = p32BytesPool->m_32BytesPages; + if (pages.HasFreeBlock()) { + return pages.Alloc(size); + } + p32BytesPool = p32BytesPool->m_pNextPool; + } + return NULL; +} +FX_LPVOID CFXMEM_FixedMgr::AllocSmall(size_t size) +{ + FX_LPVOID p = Alloc32(size); + if (p) { + return p; + } + if (!m_pExtender) { + return NULL; + } + size_t requiredSize = (m_MemConfig.nPageNum_More16 + m_MemConfig.nPageNum_More32) * FX_FIXEDMEM_PAGESIZE; + if (!requiredSize) { + return NULL; + } + CFXMEM_Pool *pNewPool = NULL; + requiredSize += sizeof(CFXMEM_Pool); + size_t newSize = requiredSize; + if (!m_pExtender->More(m_pExtender, newSize, (void**)&pNewPool, &newSize)) { + return NULL; + } + size_t nMidPages = 0; + if (m_MemConfig.nPageSize_Mid) { + nMidPages = (newSize - requiredSize) / (m_MemConfig.nPageSize_Mid * FX_FIXEDMEM_PAGESIZE); + if (nMidPages > m_MemConfig.nPageNum_MoreMid) { + nMidPages = m_MemConfig.nPageNum_MoreMid; + } + } + pNewPool->Initialize(&m_MemConfig, newSize, 0, m_MemConfig.nPageNum_More16, m_MemConfig.nPageNum_More32, nMidPages); + pNewPool->m_pPrevPool = &m_FirstPool; + CFXMEM_Pool *pPool = m_FirstPool.m_pNextPool; + pNewPool->m_pNextPool = pPool; + if (pPool) { + pPool->m_pPrevPool = pNewPool; + } + m_FirstPool.m_pNextPool = pNewPool; + return Alloc32(size); +} +FX_LPVOID CFXMEM_FixedMgr::AllocMid(size_t size) +{ + CFXMEM_Pool *pPool = &m_FirstPool; + do { + CFXMEM_Pages &pages = pPool->m_MidPages; + if (pages.m_pLimitPos > pages.m_pStartPage) { + FX_LPVOID p = pages.Alloc(size); + if (p) { + return p; + } + } + pPool = pPool->m_pNextPool; + } while(pPool); + if (!m_pExtender) { + return NULL; + } + size_t newSize = m_MemConfig.nPageSize_Mid * FX_FIXEDMEM_PAGESIZE * m_MemConfig.nPageNum_MoreMid; + if (!newSize) { + return NULL; + } + CFXMEM_Pool *pNewPool = NULL; + newSize += sizeof(CFXMEM_Pool); + if (!m_pExtender->More(m_pExtender, newSize, (void**)&pNewPool, &newSize)) { + return NULL; + } + size_t nMidPages = (newSize - sizeof(CFXMEM_Pool)) / (m_MemConfig.nPageSize_Mid * FX_FIXEDMEM_PAGESIZE); + if (nMidPages > m_MemConfig.nPageNum_MoreMid) { + nMidPages = m_MemConfig.nPageNum_MoreMid; + } + pNewPool->Initialize(&m_MemConfig, newSize, 0, 0, 0, nMidPages); + pNewPool->m_pPrevPool = &m_FirstPool; + pPool = m_FirstPool.m_pNextPool; + pNewPool->m_pNextPool = pPool; + if (pPool) { + pPool->m_pPrevPool = pNewPool; + } + m_FirstPool.m_pNextPool = pNewPool; + return pNewPool->m_MidPages.Alloc(size); +} +FX_LPVOID CFXMEM_FixedMgr::AllocLarge(size_t size) +{ + CFXMEM_Pool *pPool = &m_FirstPool; + do { + if (!pPool->m_bAlone && pPool->m_pLargePage) { + FX_LPVOID p = pPool->m_pLargePage->Alloc(size); + if (p) { + return p; + } + } + pPool = pPool->m_pNextPool; + } while(pPool); + if (!m_pExtender || !m_MemConfig.nPageSize_Large) { + return NULL; + } + CFXMEM_Pool *pNewPool = NULL; +#if _FX_WORDSIZE_ == _FX_W64_ + size_t newSize = ((size + 31) / 32 * 32 + sizeof(CFXMEM_Pool) + sizeof(CFXMEM_Page) + sizeof(CFXMEM_Block) + 4095) / 4096 * 4096; +#else + size_t newSize = (size + 7) / 8 * 8 + sizeof(CFXMEM_Pool) + sizeof(CFXMEM_Page) + sizeof(CFXMEM_Block); +#endif + if (newSize < m_MemConfig.nPageSize_Large * FX_FIXEDMEM_PAGESIZE) { + newSize = m_MemConfig.nPageSize_Large * FX_FIXEDMEM_PAGESIZE; + } + if (!m_pExtender->More(m_pExtender, newSize, (void**)&pNewPool, &newSize)) { + return NULL; + } + pNewPool->Initialize(&m_MemConfig, newSize, 0, 0, 0, 0); + pNewPool->m_bAlone = size >= m_MemConfig.nPageSize_Alone * FX_FIXEDMEM_PAGESIZE; + pNewPool->m_pPrevPool = &m_FirstPool; + pPool = m_FirstPool.m_pNextPool; + pNewPool->m_pNextPool = pPool; + if (pPool) { + pPool->m_pPrevPool = pNewPool; + } + m_FirstPool.m_pNextPool = pNewPool; + return pNewPool->m_pLargePage->Alloc(size); +} +size_t CFXMEM_FixedMgr::GetSize(FX_LPVOID p) const +{ + const CFXMEM_Pool *pFind = &m_FirstPool; + do { + if (p > (FX_LPVOID)pFind && p < pFind->m_pLimitPos) { + return pFind->GetSize(p); + } + pFind = pFind->m_pNextPool; + } while (pFind); + return 0; +} +FX_LPVOID CFXMEM_FixedMgr::Alloc(size_t size) +{ + FX_LPVOID p; + if (size <= 32) { + p = AllocSmall(size); + if (p) { + return p; + } + } + if (size <= FX_FIXEDMEM_MIDBLOCKSIZE) { + p = AllocMid(size); + if (p) { + return p; + } + } + p = AllocLarge(size); + return p; +} +FX_LPVOID CFXMEM_FixedMgr::ReallocSmall(CFXMEM_Pool* pPool, FX_LPVOID p, size_t oldSize, size_t newSize) +{ + FX_LPVOID np = AllocSmall(newSize); + if (!np) { + return NULL; + } + FXSYS_memcpy32(np, p, oldSize); + pPool->Free(p); + return np; +} +FX_LPVOID CFXMEM_FixedMgr::Realloc(FX_LPVOID p, size_t newSize) +{ + if (!p) { + return Alloc(newSize); + } + size_t oldSize = 0; + CFXMEM_Pool *pFind = &m_FirstPool; + do { + if (p > (FX_LPVOID)pFind && p < pFind->m_pLimitPos) { + oldSize = pFind->GetSize(p); + if (oldSize >= newSize) { + return p; + } + break; + } + pFind = pFind->m_pNextPool; + } while (pFind); + if (!oldSize || !pFind) { + return Alloc(newSize); + } + FX_LPVOID np = NULL; + if (newSize <= 32) { + np = ReallocSmall(pFind, p, oldSize, newSize); + if (np) { + return np; + } + } + if (newSize <= FX_FIXEDMEM_MIDBLOCKSIZE) { + np = pFind->Realloc(p, oldSize, newSize); + if (np) { + return np; + } + } + np = Alloc(newSize); + if (np) { + FXSYS_memcpy32(np, p, oldSize); + pFind->Free(p); + } + if (pFind->m_bAlone && pFind->IsEmpty()) { + FreePool(pFind); + } + return np; +} +void CFXMEM_FixedMgr::Free(FX_LPVOID p) +{ + CFXMEM_Pool *pFind = &m_FirstPool; + do { + if (p > (FX_LPVOID)pFind && p < pFind->m_pLimitPos) { + pFind->Free(p); + if (pFind->m_bAlone && pFind->IsEmpty()) { + FreePool(pFind); + } + return; + } + pFind = pFind->m_pNextPool; + } while (pFind); +} +void CFXMEM_FixedMgr::FreePool(CFXMEM_Pool* pPool) +{ + FXSYS_assert(pPool->m_bAlone && pPool->IsEmpty()); + FXSYS_assert(m_pExtender != NULL); + CFXMEM_Pool* pPrevPool = pPool->m_pPrevPool; + CFXMEM_Pool* pNextPool = pPool->m_pNextPool; + if (pPrevPool) { + pPrevPool->m_pNextPool = pNextPool; + } + if (pNextPool) { + pNextPool->m_pPrevPool = pPrevPool; + } + m_pExtender->Free(m_pExtender, pPool); +} +void CFXMEM_FixedMgr::FreeAll() +{ + if (!m_pExtender) { + return; + } + CFXMEM_Pool* pPool = m_FirstPool.m_pNextPool; + while (pPool) { + CFXMEM_Pool* pPrevPool = pPool; + pPool = pPool->m_pNextPool; + m_pExtender->Free(m_pExtender, pPrevPool); + } + m_FirstPool.m_pNextPool = NULL; +} +void CFXMEM_FixedMgr::Purge() +{ + if (!m_pExtender) { + return; + } + CFXMEM_Pool* pPool = m_FirstPool.m_pNextPool; + while (pPool) { + CFXMEM_Pool* pNextPool = pPool->m_pNextPool; + if (pPool->IsEmpty()) { + CFXMEM_Pool* pPrevPool = pPool->m_pPrevPool; + pPrevPool->m_pNextPool = pNextPool; + if (pNextPool) { + pNextPool->m_pPrevPool = pPrevPool; + } + m_pExtender->Free(m_pExtender, pPool); + } + pPool = pNextPool; + } +} +extern const FX_BYTE OneLeadPos[256] = { + 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; +extern const FX_BYTE ZeroLeadPos[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, +}; diff --git a/core/src/fxcrt/fx_basic_plex.cpp b/core/src/fxcrt/fx_basic_plex.cpp index dd3a8ba054..80c014a2b3 100644 --- a/core/src/fxcrt/fx_basic_plex.cpp +++ b/core/src/fxcrt/fx_basic_plex.cpp @@ -1,28 +1,28 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_basic.h"
-#include "plex.h"
-CFX_Plex* CFX_Plex::Create(IFX_Allocator* pAllocator, CFX_Plex*& pHead, FX_DWORD nMax, FX_DWORD cbElement)
-{
- CFX_Plex* p = (CFX_Plex*)FX_Allocator_Alloc(pAllocator, FX_BYTE, sizeof(CFX_Plex) + nMax * cbElement);
- if (!p) {
- return NULL;
- }
- p->pNext = pHead;
- pHead = p;
- return p;
-}
-void CFX_Plex::FreeDataChain(IFX_Allocator* pAllocator)
-{
- CFX_Plex* p = this;
- while (p != NULL) {
- FX_BYTE* bytes = (FX_BYTE*) p;
- CFX_Plex* pNext = p->pNext;
- FX_Allocator_Free(pAllocator, bytes);
- p = pNext;
- }
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_basic.h" +#include "plex.h" +CFX_Plex* CFX_Plex::Create(IFX_Allocator* pAllocator, CFX_Plex*& pHead, FX_DWORD nMax, FX_DWORD cbElement) +{ + CFX_Plex* p = (CFX_Plex*)FX_Allocator_Alloc(pAllocator, FX_BYTE, sizeof(CFX_Plex) + nMax * cbElement); + if (!p) { + return NULL; + } + p->pNext = pHead; + pHead = p; + return p; +} +void CFX_Plex::FreeDataChain(IFX_Allocator* pAllocator) +{ + CFX_Plex* p = this; + while (p != NULL) { + FX_BYTE* bytes = (FX_BYTE*) p; + CFX_Plex* pNext = p->pNext; + FX_Allocator_Free(pAllocator, bytes); + p = pNext; + } +} diff --git a/core/src/fxcrt/fx_basic_utf.cpp b/core/src/fxcrt/fx_basic_utf.cpp index b94e08df1d..c5aa360e6c 100644 --- a/core/src/fxcrt/fx_basic_utf.cpp +++ b/core/src/fxcrt/fx_basic_utf.cpp @@ -1,102 +1,102 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_basic.h"
-void CFX_UTF8Decoder::Clear()
-{
- m_Buffer.Clear();
- m_PendingBytes = 0;
-}
-void CFX_UTF8Decoder::AppendChar(FX_DWORD ch)
-{
- m_Buffer.AppendChar((FX_WCHAR)ch);
-}
-void CFX_UTF8Decoder::Input(FX_BYTE byte)
-{
- if (byte < 0x80) {
- m_PendingBytes = 0;
- m_Buffer.AppendChar(byte);
- } else if (byte < 0xc0) {
- if (m_PendingBytes == 0) {
- return;
- }
- m_PendingBytes --;
- m_PendingChar |= (byte & 0x3f) << (m_PendingBytes * 6);
- if (m_PendingBytes == 0) {
- AppendChar(m_PendingChar);
- }
- } else if (byte < 0xe0) {
- m_PendingBytes = 1;
- m_PendingChar = (byte & 0x1f) << 6;
- } else if (byte < 0xf0) {
- m_PendingBytes = 2;
- m_PendingChar = (byte & 0x0f) << 12;
- } else if (byte < 0xf8) {
- m_PendingBytes = 3;
- m_PendingChar = (byte & 0x07) << 18;
- } else if (byte < 0xfc) {
- m_PendingBytes = 4;
- m_PendingChar = (byte & 0x03) << 24;
- } else if (byte < 0xfe) {
- m_PendingBytes = 5;
- m_PendingChar = (byte & 0x01) << 30;
- }
-}
-void CFX_UTF8Encoder::Input(FX_WCHAR unicode)
-{
- if ((FX_DWORD)unicode < 0x80) {
- m_Buffer.AppendChar(unicode);
- } else {
- if ((FX_DWORD)unicode >= 0x80000000) {
- return;
- }
- int nbytes = 0;
- if ((FX_DWORD)unicode < 0x800) {
- nbytes = 2;
- } else if ((FX_DWORD)unicode < 0x10000) {
- nbytes = 3;
- } else if ((FX_DWORD)unicode < 0x200000) {
- nbytes = 4;
- } else if ((FX_DWORD)unicode < 0x4000000) {
- nbytes = 5;
- } else {
- nbytes = 6;
- }
- static FX_BYTE prefix[] = {0xc0, 0xe0, 0xf0, 0xf8, 0xfc};
- int order = 1 << ((nbytes - 1) * 6);
- int code = unicode;
- m_Buffer.AppendChar(prefix[nbytes - 2] | (code / order));
- for (int i = 0; i < nbytes - 1; i ++) {
- code = code % order;
- order >>= 6;
- m_Buffer.AppendChar(0x80 | (code / order));
- }
- }
-}
-CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len)
-{
- FXSYS_assert(pwsStr != NULL);
- if (len < 0) {
- len = (FX_STRSIZE)FXSYS_wcslen(pwsStr);
- }
- CFX_UTF8Encoder encoder;
- while (len -- > 0) {
- encoder.Input(*pwsStr ++);
- }
- return encoder.GetResult();
-}
-void FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len, CFX_ByteStringL &utf8Str, IFX_Allocator* pAllocator)
-{
- FXSYS_assert(pwsStr != NULL);
- if (len < 0) {
- len = (FX_STRSIZE)FXSYS_wcslen(pwsStr);
- }
- CFX_UTF8Encoder encoder(pAllocator);
- while (len -- > 0) {
- encoder.Input(*pwsStr ++);
- }
- encoder.GetResult(utf8Str);
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_basic.h" +void CFX_UTF8Decoder::Clear() +{ + m_Buffer.Clear(); + m_PendingBytes = 0; +} +void CFX_UTF8Decoder::AppendChar(FX_DWORD ch) +{ + m_Buffer.AppendChar((FX_WCHAR)ch); +} +void CFX_UTF8Decoder::Input(FX_BYTE byte) +{ + if (byte < 0x80) { + m_PendingBytes = 0; + m_Buffer.AppendChar(byte); + } else if (byte < 0xc0) { + if (m_PendingBytes == 0) { + return; + } + m_PendingBytes --; + m_PendingChar |= (byte & 0x3f) << (m_PendingBytes * 6); + if (m_PendingBytes == 0) { + AppendChar(m_PendingChar); + } + } else if (byte < 0xe0) { + m_PendingBytes = 1; + m_PendingChar = (byte & 0x1f) << 6; + } else if (byte < 0xf0) { + m_PendingBytes = 2; + m_PendingChar = (byte & 0x0f) << 12; + } else if (byte < 0xf8) { + m_PendingBytes = 3; + m_PendingChar = (byte & 0x07) << 18; + } else if (byte < 0xfc) { + m_PendingBytes = 4; + m_PendingChar = (byte & 0x03) << 24; + } else if (byte < 0xfe) { + m_PendingBytes = 5; + m_PendingChar = (byte & 0x01) << 30; + } +} +void CFX_UTF8Encoder::Input(FX_WCHAR unicode) +{ + if ((FX_DWORD)unicode < 0x80) { + m_Buffer.AppendChar(unicode); + } else { + if ((FX_DWORD)unicode >= 0x80000000) { + return; + } + int nbytes = 0; + if ((FX_DWORD)unicode < 0x800) { + nbytes = 2; + } else if ((FX_DWORD)unicode < 0x10000) { + nbytes = 3; + } else if ((FX_DWORD)unicode < 0x200000) { + nbytes = 4; + } else if ((FX_DWORD)unicode < 0x4000000) { + nbytes = 5; + } else { + nbytes = 6; + } + static FX_BYTE prefix[] = {0xc0, 0xe0, 0xf0, 0xf8, 0xfc}; + int order = 1 << ((nbytes - 1) * 6); + int code = unicode; + m_Buffer.AppendChar(prefix[nbytes - 2] | (code / order)); + for (int i = 0; i < nbytes - 1; i ++) { + code = code % order; + order >>= 6; + m_Buffer.AppendChar(0x80 | (code / order)); + } + } +} +CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len) +{ + FXSYS_assert(pwsStr != NULL); + if (len < 0) { + len = (FX_STRSIZE)FXSYS_wcslen(pwsStr); + } + CFX_UTF8Encoder encoder; + while (len -- > 0) { + encoder.Input(*pwsStr ++); + } + return encoder.GetResult(); +} +void FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len, CFX_ByteStringL &utf8Str, IFX_Allocator* pAllocator) +{ + FXSYS_assert(pwsStr != NULL); + if (len < 0) { + len = (FX_STRSIZE)FXSYS_wcslen(pwsStr); + } + CFX_UTF8Encoder encoder(pAllocator); + while (len -- > 0) { + encoder.Input(*pwsStr ++); + } + encoder.GetResult(utf8Str); +} diff --git a/core/src/fxcrt/fx_basic_util.cpp b/core/src/fxcrt/fx_basic_util.cpp index af4d42c0ae..1d947d3fed 100644 --- a/core/src/fxcrt/fx_basic_util.cpp +++ b/core/src/fxcrt/fx_basic_util.cpp @@ -1,444 +1,444 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_basic.h"
-#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
-#include <sys/types.h>
-#include <dirent.h>
-#else
-#include <direct.h>
-#endif
-CFX_PrivateData::~CFX_PrivateData()
-{
- ClearAll();
-}
-void FX_PRIVATEDATA::FreeData()
-{
- if (m_pData == NULL) {
- return;
- }
- if (m_bSelfDestruct) {
- delete (CFX_DestructObject*)m_pData;
- } else if (m_pCallback) {
- m_pCallback(m_pData);
- }
-}
-void CFX_PrivateData::AddData(FX_LPVOID pModuleId, FX_LPVOID pData, PD_CALLBACK_FREEDATA callback, FX_BOOL bSelfDestruct)
-{
- if (pModuleId == NULL) {
- return;
- }
- FX_PRIVATEDATA* pList = m_DataList.GetData();
- int count = m_DataList.GetSize();
- for (int i = 0; i < count; i ++) {
- if (pList[i].m_pModuleId == pModuleId) {
- pList[i].FreeData();
- pList[i].m_pData = pData;
- pList[i].m_pCallback = callback;
- return;
- }
- }
- FX_PRIVATEDATA data = {pModuleId, pData, callback, bSelfDestruct};
- m_DataList.Add(data);
-}
-void CFX_PrivateData::SetPrivateData(FX_LPVOID pModuleId, FX_LPVOID pData, PD_CALLBACK_FREEDATA callback)
-{
- AddData(pModuleId, pData, callback, FALSE);
-}
-void CFX_PrivateData::SetPrivateObj(FX_LPVOID pModuleId, CFX_DestructObject* pObj)
-{
- AddData(pModuleId, pObj, NULL, TRUE);
-}
-FX_BOOL CFX_PrivateData::RemovePrivateData(FX_LPVOID pModuleId)
-{
- if (pModuleId == NULL) {
- return FALSE;
- }
- FX_PRIVATEDATA* pList = m_DataList.GetData();
- int count = m_DataList.GetSize();
- for (int i = 0; i < count; i ++) {
- if (pList[i].m_pModuleId == pModuleId) {
- m_DataList.RemoveAt(i);
- return TRUE;
- }
- }
- return FALSE;
-}
-FX_LPVOID CFX_PrivateData::GetPrivateData(FX_LPVOID pModuleId)
-{
- if (pModuleId == NULL) {
- return NULL;
- }
- FX_PRIVATEDATA* pList = m_DataList.GetData();
- int count = m_DataList.GetSize();
- for (int i = 0; i < count; i ++) {
- if (pList[i].m_pModuleId == pModuleId) {
- return pList[i].m_pData;
- }
- }
- return NULL;
-}
-void CFX_PrivateData::ClearAll()
-{
- FX_PRIVATEDATA* pList = m_DataList.GetData();
- int count = m_DataList.GetSize();
- for (int i = 0; i < count; i ++) {
- pList[i].FreeData();
- }
- m_DataList.RemoveAll();
-}
-void FX_atonum(FX_BSTR strc, FX_BOOL& bInteger, void* pData)
-{
- if (FXSYS_memchr(strc.GetPtr(), '.', strc.GetLength()) == NULL) {
- bInteger = TRUE;
- int cc = 0, integer = 0;
- FX_LPCSTR str = strc.GetCStr();
- int len = strc.GetLength();
- FX_BOOL bNegative = FALSE;
- if (str[0] == '+') {
- cc++;
- } else if (str[0] == '-') {
- bNegative = TRUE;
- cc++;
- }
- while (cc < len) {
- if (str[cc] < '0' || str[cc] > '9') {
- break;
- }
- integer = integer * 10 + str[cc] - '0';
- if (integer < 0) {
- break;
- }
- cc ++;
- }
- if (bNegative) {
- integer = -integer;
- }
- *(int*)pData = integer;
- } else {
- bInteger = FALSE;
- *(FX_FLOAT*)pData = FX_atof(strc);
- }
-}
-FX_FLOAT FX_atof(FX_BSTR strc)
-{
- if (strc.GetLength() == 0) {
- return 0.0;
- }
- int cc = 0;
- FX_BOOL bNegative = FALSE;
- FX_LPCSTR str = strc.GetCStr();
- int len = strc.GetLength();
- if (str[0] == '+') {
- cc++;
- } else if (str[0] == '-') {
- bNegative = TRUE;
- cc++;
- }
- while (cc < len) {
- if (str[cc] != '+' && str[cc] != '-') {
- break;
- }
- cc ++;
- }
- FX_FLOAT value = 0;
- while (cc < len) {
- if (str[cc] == '.') {
- break;
- }
- value = value * 10 + str[cc] - '0';
- cc ++;
- }
- static const FX_FLOAT fraction_scales[] = {0.1f, 0.01f, 0.001f, 0.0001f, 0.00001f, 0.000001f,
- 0.0000001f, 0.00000001f, 0.000000001f, 0.0000000001f, 0.00000000001f
- };
- int scale = 0;
- if (cc < len && str[cc] == '.') {
- cc ++;
- while (cc < len) {
- value += fraction_scales[scale] * (str[cc] - '0');
- scale ++;
- if (scale == sizeof fraction_scales / sizeof(FX_FLOAT)) {
- break;
- }
- cc ++;
- }
- }
- return bNegative ? -value : value;
-}
-static FX_BOOL FX_IsDigit(FX_BYTE ch)
-{
- return (ch >= '0' && ch <= '9') ? TRUE : FALSE;
-}
-static FX_BOOL FX_IsXDigit(FX_BYTE ch)
-{
- return (FX_IsDigit(ch) || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) ? TRUE : FALSE;
-}
-static FX_BYTE FX_MakeUpper(FX_BYTE ch)
-{
- if (ch < 'a' || ch > 'z') {
- return ch;
- }
- return ch - 32;
-}
-static int FX_HexToI(FX_BYTE ch)
-{
- ch = FX_MakeUpper(ch);
- return FX_IsDigit(ch) ? (ch - '0') : (ch - 55);
-}
-static const unsigned char url_encodeTable[128] = {
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 0, 1, 1, 0, 1, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 1, 0, 1, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 0,
- 1, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1,
-};
-CFX_ByteString FX_UrlEncode(const CFX_WideString& wsUrl)
-{
- const char arDigits[] = "0123456789ABCDEF";
- CFX_ByteString rUrl;
- int nLength = wsUrl.GetLength();
- for (int i = 0; i < nLength; i++) {
- FX_DWORD word = wsUrl.GetAt(i);
- if (word > 0x7F || url_encodeTable[word] == 1) {
- CFX_ByteString bsUri = CFX_ByteString::FromUnicode((FX_WORD)word);
- int nByte = bsUri.GetLength();
- for (int j = 0; j < nByte; j++) {
- rUrl += '%';
- FX_BYTE code = bsUri.GetAt(j);
- rUrl += arDigits[code >> 4];
- rUrl += arDigits[code & 0x0F];
- }
- } else {
- rUrl += CFX_ByteString::FromUnicode((FX_WORD)word);
- }
- }
- return rUrl;
-}
-CFX_WideString FX_UrlDecode(const CFX_ByteString& bsUrl)
-{
- CFX_ByteString rUrl;
- int nLength = bsUrl.GetLength();
- for (int i = 0; i < nLength; i++) {
- if (i < nLength - 2 && bsUrl[i] == '%' && FX_IsXDigit(bsUrl[i + 1]) && FX_IsXDigit(bsUrl[i + 2])) {
- rUrl += (FX_HexToI(bsUrl[i + 1]) << 4 | FX_HexToI(bsUrl[i + 2]));
- i += 2;
- } else {
- rUrl += bsUrl[i];
- }
- }
- return CFX_WideString::FromLocal(rUrl);
-}
-CFX_ByteString FX_EncodeURI(const CFX_WideString& wsURI)
-{
- const char arDigits[] = "0123456789ABCDEF";
- CFX_ByteString rURI;
- CFX_ByteString bsUri = wsURI.UTF8Encode();
- int nLength = bsUri.GetLength();
- for (int i = 0; i < nLength; i++) {
- FX_BYTE code = bsUri.GetAt(i);
- if (code > 0x7F || url_encodeTable[code] == 1) {
- rURI += '%';
- rURI += arDigits[code >> 4];
- rURI += arDigits[code & 0x0F];
- } else {
- rURI += code;
- }
- }
- return rURI;
-}
-CFX_WideString FX_DecodeURI(const CFX_ByteString& bsURI)
-{
- CFX_ByteString rURI;
- int nLength = bsURI.GetLength();
- for (int i = 0; i < nLength; i++) {
- if (i < nLength - 2 && bsURI[i] == '%' && FX_IsXDigit(bsURI[i + 1]) && FX_IsXDigit(bsURI[i + 2])) {
- rURI += (FX_HexToI(bsURI[i + 1]) << 4 | FX_HexToI(bsURI[i + 2]));
- i += 2;
- } else {
- rURI += bsURI[i];
- }
- }
- return CFX_WideString::FromUTF8(rURI);
-}
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
-class CFindFileData : public CFX_Object
-{
-public:
- virtual ~CFindFileData() {}
- HANDLE m_Handle;
- FX_BOOL m_bEnd;
-};
-class CFindFileDataA : public CFindFileData
-{
-public:
- virtual ~CFindFileDataA() {}
- WIN32_FIND_DATAA m_FindData;
-};
-class CFindFileDataW : public CFindFileData
-{
-public:
- virtual ~CFindFileDataW() {}
- WIN32_FIND_DATAW m_FindData;
-};
-#endif
-void* FX_OpenFolder(FX_LPCSTR path)
-{
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
-#ifndef _WIN32_WCE
- CFindFileDataA* pData = FX_NEW CFindFileDataA;
- if (!pData) {
- return NULL;
- }
-#ifdef _FX_WINAPI_PARTITION_DESKTOP_
- pData->m_Handle = FindFirstFileA(CFX_ByteString(path) + "/*.*", &pData->m_FindData);
-#else
- pData->m_Handle = FindFirstFileExA(CFX_ByteString(path) + "/*.*", FindExInfoStandard, &pData->m_FindData, FindExSearchNameMatch, NULL, 0);
-#endif
-#else
- CFindFileDataW* pData = FX_NEW CFindFileDataW;
- if (!pData) {
- return NULL;
- }
- pData->m_Handle = FindFirstFileW(CFX_WideString::FromLocal(path) + L"/*.*", &pData->m_FindData);
-#endif
- if (pData->m_Handle == INVALID_HANDLE_VALUE) {
- delete pData;
- return NULL;
- }
- pData->m_bEnd = FALSE;
- return pData;
-#else
- DIR* dir = opendir(path);
- return dir;
-#endif
-}
-void* FX_OpenFolder(FX_LPCWSTR path)
-{
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
- CFindFileDataW* pData = FX_NEW CFindFileDataW;
- if (!pData) {
- return NULL;
- }
-#ifdef _FX_WINAPI_PARTITION_DESKTOP_
- pData->m_Handle = FindFirstFileW(CFX_WideString(path) + L"/*.*", &pData->m_FindData);
-#else
- pData->m_Handle = FindFirstFileExW(CFX_WideString(path) + L"/*.*", FindExInfoStandard, &pData->m_FindData, FindExSearchNameMatch, NULL, 0);
-#endif
- if (pData->m_Handle == INVALID_HANDLE_VALUE) {
- delete pData;
- return NULL;
- }
- pData->m_bEnd = FALSE;
- return pData;
-#else
- DIR* dir = opendir(CFX_ByteString::FromUnicode(path));
- return dir;
-#endif
-}
-FX_BOOL FX_GetNextFile(void* handle, CFX_ByteString& filename, FX_BOOL& bFolder)
-{
- if (handle == NULL) {
- return FALSE;
- }
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
-#ifndef _WIN32_WCE
- CFindFileDataA* pData = (CFindFileDataA*)handle;
- if (pData->m_bEnd) {
- return FALSE;
- }
- filename = pData->m_FindData.cFileName;
- bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
- if (!FindNextFileA(pData->m_Handle, &pData->m_FindData)) {
- pData->m_bEnd = TRUE;
- }
- return TRUE;
-#else
- CFindFileDataW* pData = (CFindFileDataW*)handle;
- if (pData->m_bEnd) {
- return FALSE;
- }
- filename = CFX_ByteString::FromUnicode(pData->m_FindData.cFileName);
- bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
- if (!FindNextFileW(pData->m_Handle, &pData->m_FindData)) {
- pData->m_bEnd = TRUE;
- }
- return TRUE;
-#endif
-#elif defined(__native_client__)
- abort();
- return FALSE;
-#else
- struct dirent *de = readdir((DIR*)handle);
- if (de == NULL) {
- return FALSE;
- }
- filename = de->d_name;
- bFolder = de->d_type == DT_DIR;
- return TRUE;
-#endif
-}
-FX_BOOL FX_GetNextFile(void* handle, CFX_WideString& filename, FX_BOOL& bFolder)
-{
- if (handle == NULL) {
- return FALSE;
- }
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
- CFindFileDataW* pData = (CFindFileDataW*)handle;
- if (pData->m_bEnd) {
- return FALSE;
- }
- filename = pData->m_FindData.cFileName;
- bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
- if (!FindNextFileW(pData->m_Handle, &pData->m_FindData)) {
- pData->m_bEnd = TRUE;
- }
- return TRUE;
-#elif defined(__native_client__)
- abort();
- return FALSE;
-#else
- struct dirent *de = readdir((DIR*)handle);
- if (de == NULL) {
- return FALSE;
- }
- filename = CFX_WideString::FromLocal(de->d_name);
- bFolder = de->d_type == DT_DIR;
- return TRUE;
-#endif
-}
-void FX_CloseFolder(void* handle)
-{
- if (handle == NULL) {
- return;
- }
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
- CFindFileData* pData = (CFindFileData*)handle;
- FindClose(pData->m_Handle);
- delete pData;
-#else
- closedir((DIR*)handle);
-#endif
-}
-FX_WCHAR FX_GetFolderSeparator()
-{
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
- return '\\';
-#else
- return '/';
-#endif
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_basic.h" +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ +#include <sys/types.h> +#include <dirent.h> +#else +#include <direct.h> +#endif +CFX_PrivateData::~CFX_PrivateData() +{ + ClearAll(); +} +void FX_PRIVATEDATA::FreeData() +{ + if (m_pData == NULL) { + return; + } + if (m_bSelfDestruct) { + delete (CFX_DestructObject*)m_pData; + } else if (m_pCallback) { + m_pCallback(m_pData); + } +} +void CFX_PrivateData::AddData(FX_LPVOID pModuleId, FX_LPVOID pData, PD_CALLBACK_FREEDATA callback, FX_BOOL bSelfDestruct) +{ + if (pModuleId == NULL) { + return; + } + FX_PRIVATEDATA* pList = m_DataList.GetData(); + int count = m_DataList.GetSize(); + for (int i = 0; i < count; i ++) { + if (pList[i].m_pModuleId == pModuleId) { + pList[i].FreeData(); + pList[i].m_pData = pData; + pList[i].m_pCallback = callback; + return; + } + } + FX_PRIVATEDATA data = {pModuleId, pData, callback, bSelfDestruct}; + m_DataList.Add(data); +} +void CFX_PrivateData::SetPrivateData(FX_LPVOID pModuleId, FX_LPVOID pData, PD_CALLBACK_FREEDATA callback) +{ + AddData(pModuleId, pData, callback, FALSE); +} +void CFX_PrivateData::SetPrivateObj(FX_LPVOID pModuleId, CFX_DestructObject* pObj) +{ + AddData(pModuleId, pObj, NULL, TRUE); +} +FX_BOOL CFX_PrivateData::RemovePrivateData(FX_LPVOID pModuleId) +{ + if (pModuleId == NULL) { + return FALSE; + } + FX_PRIVATEDATA* pList = m_DataList.GetData(); + int count = m_DataList.GetSize(); + for (int i = 0; i < count; i ++) { + if (pList[i].m_pModuleId == pModuleId) { + m_DataList.RemoveAt(i); + return TRUE; + } + } + return FALSE; +} +FX_LPVOID CFX_PrivateData::GetPrivateData(FX_LPVOID pModuleId) +{ + if (pModuleId == NULL) { + return NULL; + } + FX_PRIVATEDATA* pList = m_DataList.GetData(); + int count = m_DataList.GetSize(); + for (int i = 0; i < count; i ++) { + if (pList[i].m_pModuleId == pModuleId) { + return pList[i].m_pData; + } + } + return NULL; +} +void CFX_PrivateData::ClearAll() +{ + FX_PRIVATEDATA* pList = m_DataList.GetData(); + int count = m_DataList.GetSize(); + for (int i = 0; i < count; i ++) { + pList[i].FreeData(); + } + m_DataList.RemoveAll(); +} +void FX_atonum(FX_BSTR strc, FX_BOOL& bInteger, void* pData) +{ + if (FXSYS_memchr(strc.GetPtr(), '.', strc.GetLength()) == NULL) { + bInteger = TRUE; + int cc = 0, integer = 0; + FX_LPCSTR str = strc.GetCStr(); + int len = strc.GetLength(); + FX_BOOL bNegative = FALSE; + if (str[0] == '+') { + cc++; + } else if (str[0] == '-') { + bNegative = TRUE; + cc++; + } + while (cc < len) { + if (str[cc] < '0' || str[cc] > '9') { + break; + } + integer = integer * 10 + str[cc] - '0'; + if (integer < 0) { + break; + } + cc ++; + } + if (bNegative) { + integer = -integer; + } + *(int*)pData = integer; + } else { + bInteger = FALSE; + *(FX_FLOAT*)pData = FX_atof(strc); + } +} +FX_FLOAT FX_atof(FX_BSTR strc) +{ + if (strc.GetLength() == 0) { + return 0.0; + } + int cc = 0; + FX_BOOL bNegative = FALSE; + FX_LPCSTR str = strc.GetCStr(); + int len = strc.GetLength(); + if (str[0] == '+') { + cc++; + } else if (str[0] == '-') { + bNegative = TRUE; + cc++; + } + while (cc < len) { + if (str[cc] != '+' && str[cc] != '-') { + break; + } + cc ++; + } + FX_FLOAT value = 0; + while (cc < len) { + if (str[cc] == '.') { + break; + } + value = value * 10 + str[cc] - '0'; + cc ++; + } + static const FX_FLOAT fraction_scales[] = {0.1f, 0.01f, 0.001f, 0.0001f, 0.00001f, 0.000001f, + 0.0000001f, 0.00000001f, 0.000000001f, 0.0000000001f, 0.00000000001f + }; + int scale = 0; + if (cc < len && str[cc] == '.') { + cc ++; + while (cc < len) { + value += fraction_scales[scale] * (str[cc] - '0'); + scale ++; + if (scale == sizeof fraction_scales / sizeof(FX_FLOAT)) { + break; + } + cc ++; + } + } + return bNegative ? -value : value; +} +static FX_BOOL FX_IsDigit(FX_BYTE ch) +{ + return (ch >= '0' && ch <= '9') ? TRUE : FALSE; +} +static FX_BOOL FX_IsXDigit(FX_BYTE ch) +{ + return (FX_IsDigit(ch) || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) ? TRUE : FALSE; +} +static FX_BYTE FX_MakeUpper(FX_BYTE ch) +{ + if (ch < 'a' || ch > 'z') { + return ch; + } + return ch - 32; +} +static int FX_HexToI(FX_BYTE ch) +{ + ch = FX_MakeUpper(ch); + return FX_IsDigit(ch) ? (ch - '0') : (ch - 55); +} +static const unsigned char url_encodeTable[128] = { + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 1, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 1, 1, 1, 0, + 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 1, 1, 1, 1, +}; +CFX_ByteString FX_UrlEncode(const CFX_WideString& wsUrl) +{ + const char arDigits[] = "0123456789ABCDEF"; + CFX_ByteString rUrl; + int nLength = wsUrl.GetLength(); + for (int i = 0; i < nLength; i++) { + FX_DWORD word = wsUrl.GetAt(i); + if (word > 0x7F || url_encodeTable[word] == 1) { + CFX_ByteString bsUri = CFX_ByteString::FromUnicode((FX_WORD)word); + int nByte = bsUri.GetLength(); + for (int j = 0; j < nByte; j++) { + rUrl += '%'; + FX_BYTE code = bsUri.GetAt(j); + rUrl += arDigits[code >> 4]; + rUrl += arDigits[code & 0x0F]; + } + } else { + rUrl += CFX_ByteString::FromUnicode((FX_WORD)word); + } + } + return rUrl; +} +CFX_WideString FX_UrlDecode(const CFX_ByteString& bsUrl) +{ + CFX_ByteString rUrl; + int nLength = bsUrl.GetLength(); + for (int i = 0; i < nLength; i++) { + if (i < nLength - 2 && bsUrl[i] == '%' && FX_IsXDigit(bsUrl[i + 1]) && FX_IsXDigit(bsUrl[i + 2])) { + rUrl += (FX_HexToI(bsUrl[i + 1]) << 4 | FX_HexToI(bsUrl[i + 2])); + i += 2; + } else { + rUrl += bsUrl[i]; + } + } + return CFX_WideString::FromLocal(rUrl); +} +CFX_ByteString FX_EncodeURI(const CFX_WideString& wsURI) +{ + const char arDigits[] = "0123456789ABCDEF"; + CFX_ByteString rURI; + CFX_ByteString bsUri = wsURI.UTF8Encode(); + int nLength = bsUri.GetLength(); + for (int i = 0; i < nLength; i++) { + FX_BYTE code = bsUri.GetAt(i); + if (code > 0x7F || url_encodeTable[code] == 1) { + rURI += '%'; + rURI += arDigits[code >> 4]; + rURI += arDigits[code & 0x0F]; + } else { + rURI += code; + } + } + return rURI; +} +CFX_WideString FX_DecodeURI(const CFX_ByteString& bsURI) +{ + CFX_ByteString rURI; + int nLength = bsURI.GetLength(); + for (int i = 0; i < nLength; i++) { + if (i < nLength - 2 && bsURI[i] == '%' && FX_IsXDigit(bsURI[i + 1]) && FX_IsXDigit(bsURI[i + 2])) { + rURI += (FX_HexToI(bsURI[i + 1]) << 4 | FX_HexToI(bsURI[i + 2])); + i += 2; + } else { + rURI += bsURI[i]; + } + } + return CFX_WideString::FromUTF8(rURI); +} +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +class CFindFileData : public CFX_Object +{ +public: + virtual ~CFindFileData() {} + HANDLE m_Handle; + FX_BOOL m_bEnd; +}; +class CFindFileDataA : public CFindFileData +{ +public: + virtual ~CFindFileDataA() {} + WIN32_FIND_DATAA m_FindData; +}; +class CFindFileDataW : public CFindFileData +{ +public: + virtual ~CFindFileDataW() {} + WIN32_FIND_DATAW m_FindData; +}; +#endif +void* FX_OpenFolder(FX_LPCSTR path) +{ +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +#ifndef _WIN32_WCE + CFindFileDataA* pData = FX_NEW CFindFileDataA; + if (!pData) { + return NULL; + } +#ifdef _FX_WINAPI_PARTITION_DESKTOP_ + pData->m_Handle = FindFirstFileA(CFX_ByteString(path) + "/*.*", &pData->m_FindData); +#else + pData->m_Handle = FindFirstFileExA(CFX_ByteString(path) + "/*.*", FindExInfoStandard, &pData->m_FindData, FindExSearchNameMatch, NULL, 0); +#endif +#else + CFindFileDataW* pData = FX_NEW CFindFileDataW; + if (!pData) { + return NULL; + } + pData->m_Handle = FindFirstFileW(CFX_WideString::FromLocal(path) + L"/*.*", &pData->m_FindData); +#endif + if (pData->m_Handle == INVALID_HANDLE_VALUE) { + delete pData; + return NULL; + } + pData->m_bEnd = FALSE; + return pData; +#else + DIR* dir = opendir(path); + return dir; +#endif +} +void* FX_OpenFolder(FX_LPCWSTR path) +{ +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + CFindFileDataW* pData = FX_NEW CFindFileDataW; + if (!pData) { + return NULL; + } +#ifdef _FX_WINAPI_PARTITION_DESKTOP_ + pData->m_Handle = FindFirstFileW(CFX_WideString(path) + L"/*.*", &pData->m_FindData); +#else + pData->m_Handle = FindFirstFileExW(CFX_WideString(path) + L"/*.*", FindExInfoStandard, &pData->m_FindData, FindExSearchNameMatch, NULL, 0); +#endif + if (pData->m_Handle == INVALID_HANDLE_VALUE) { + delete pData; + return NULL; + } + pData->m_bEnd = FALSE; + return pData; +#else + DIR* dir = opendir(CFX_ByteString::FromUnicode(path)); + return dir; +#endif +} +FX_BOOL FX_GetNextFile(void* handle, CFX_ByteString& filename, FX_BOOL& bFolder) +{ + if (handle == NULL) { + return FALSE; + } +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +#ifndef _WIN32_WCE + CFindFileDataA* pData = (CFindFileDataA*)handle; + if (pData->m_bEnd) { + return FALSE; + } + filename = pData->m_FindData.cFileName; + bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; + if (!FindNextFileA(pData->m_Handle, &pData->m_FindData)) { + pData->m_bEnd = TRUE; + } + return TRUE; +#else + CFindFileDataW* pData = (CFindFileDataW*)handle; + if (pData->m_bEnd) { + return FALSE; + } + filename = CFX_ByteString::FromUnicode(pData->m_FindData.cFileName); + bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; + if (!FindNextFileW(pData->m_Handle, &pData->m_FindData)) { + pData->m_bEnd = TRUE; + } + return TRUE; +#endif +#elif defined(__native_client__) + abort(); + return FALSE; +#else + struct dirent *de = readdir((DIR*)handle); + if (de == NULL) { + return FALSE; + } + filename = de->d_name; + bFolder = de->d_type == DT_DIR; + return TRUE; +#endif +} +FX_BOOL FX_GetNextFile(void* handle, CFX_WideString& filename, FX_BOOL& bFolder) +{ + if (handle == NULL) { + return FALSE; + } +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + CFindFileDataW* pData = (CFindFileDataW*)handle; + if (pData->m_bEnd) { + return FALSE; + } + filename = pData->m_FindData.cFileName; + bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; + if (!FindNextFileW(pData->m_Handle, &pData->m_FindData)) { + pData->m_bEnd = TRUE; + } + return TRUE; +#elif defined(__native_client__) + abort(); + return FALSE; +#else + struct dirent *de = readdir((DIR*)handle); + if (de == NULL) { + return FALSE; + } + filename = CFX_WideString::FromLocal(de->d_name); + bFolder = de->d_type == DT_DIR; + return TRUE; +#endif +} +void FX_CloseFolder(void* handle) +{ + if (handle == NULL) { + return; + } +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + CFindFileData* pData = (CFindFileData*)handle; + FindClose(pData->m_Handle); + delete pData; +#else + closedir((DIR*)handle); +#endif +} +FX_WCHAR FX_GetFolderSeparator() +{ +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + return '\\'; +#else + return '/'; +#endif +} diff --git a/core/src/fxcrt/fx_basic_wstring.cpp b/core/src/fxcrt/fx_basic_wstring.cpp index 900f05846a..7af3303ca2 100644 --- a/core/src/fxcrt/fx_basic_wstring.cpp +++ b/core/src/fxcrt/fx_basic_wstring.cpp @@ -1,1168 +1,1168 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_basic.h"
-static CFX_StringDataW* FX_AllocStringW(int nLen)
-{
- if (nLen == 0) {
- return NULL;
- }
- CFX_StringDataW* pData = (CFX_StringDataW*)FX_Alloc(FX_BYTE, sizeof(long) * 3 + (nLen + 1) * sizeof(FX_WCHAR));
- if (!pData) {
- return NULL;
- }
- pData->m_nAllocLength = nLen;
- pData->m_nDataLength = nLen;
- pData->m_nRefs = 1;
- pData->m_String[nLen] = 0;
- return pData;
-}
-static void FX_ReleaseStringW(CFX_StringDataW* pData)
-{
- if (pData == NULL) {
- return;
- }
- pData->m_nRefs --;
- if (pData->m_nRefs <= 0) {
- FX_Free(pData);
- }
-}
-CFX_WideString::~CFX_WideString()
-{
- if (m_pData == NULL) {
- return;
- }
- m_pData->m_nRefs --;
- if (m_pData->m_nRefs < 1) {
- FX_Free(m_pData);
- }
-}
-void CFX_WideString::InitStr(FX_LPCWSTR lpsz, FX_STRSIZE nLen)
-{
- if (nLen < 0) {
- nLen = lpsz ? (FX_STRSIZE)FXSYS_wcslen(lpsz) : 0;
- }
- if (nLen) {
- m_pData = FX_AllocStringW(nLen);
- if (!m_pData) {
- return;
- }
- FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(FX_WCHAR));
- } else {
- m_pData = NULL;
- }
-}
-CFX_WideString::CFX_WideString(const CFX_WideString& stringSrc)
-{
- if (stringSrc.m_pData == NULL) {
- m_pData = NULL;
- return;
- }
- if (stringSrc.m_pData->m_nRefs >= 0) {
- m_pData = stringSrc.m_pData;
- m_pData->m_nRefs ++;
- } else {
- m_pData = NULL;
- *this = stringSrc;
- }
-}
-CFX_WideString::CFX_WideString(FX_WCHAR ch)
-{
- m_pData = FX_AllocStringW(1);
- if (m_pData) {
- m_pData->m_String[0] = ch;
- }
-}
-CFX_WideString::CFX_WideString(const CFX_WideStringC& str)
-{
- if (str.IsEmpty()) {
- m_pData = NULL;
- return;
- }
- m_pData = FX_AllocStringW(str.GetLength());
- if (m_pData) {
- FXSYS_memcpy32(m_pData->m_String, str.GetPtr(), str.GetLength()*sizeof(FX_WCHAR));
- }
-}
-CFX_WideString::CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2)
-{
- m_pData = NULL;
- int nNewLen = str1.GetLength() + str2.GetLength();
- if (nNewLen == 0) {
- return;
- }
- m_pData = FX_AllocStringW(nNewLen);
- if (m_pData) {
- FXSYS_memcpy32(m_pData->m_String, str1.GetPtr(), str1.GetLength()*sizeof(FX_WCHAR));
- FXSYS_memcpy32(m_pData->m_String + str1.GetLength(), str2.GetPtr(), str2.GetLength()*sizeof(FX_WCHAR));
- }
-}
-void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength)
-{
- if (m_pData == NULL) {
- return;
- }
- CopyBeforeWrite();
- if (nNewLength == -1) {
- nNewLength = m_pData ? (FX_STRSIZE)FXSYS_wcslen(m_pData->m_String) : 0;
- }
- if (nNewLength == 0) {
- Empty();
- return;
- }
- FXSYS_assert(nNewLength <= m_pData->m_nAllocLength);
- m_pData->m_nDataLength = nNewLength;
- m_pData->m_String[nNewLength] = 0;
-}
-const CFX_WideString& CFX_WideString::operator=(FX_LPCWSTR lpsz)
-{
- if (lpsz == NULL || lpsz[0] == 0) {
- Empty();
- } else {
- AssignCopy((FX_STRSIZE)FXSYS_wcslen(lpsz), lpsz);
- }
- return *this;
-}
-const CFX_WideString& CFX_WideString::operator=(const CFX_WideStringC& stringSrc)
-{
- if (stringSrc.IsEmpty()) {
- Empty();
- } else {
- AssignCopy(stringSrc.GetLength(), stringSrc.GetPtr());
- }
- return *this;
-}
-const CFX_WideString& CFX_WideString::operator=(const CFX_WideString& stringSrc)
-{
- if (m_pData == stringSrc.m_pData) {
- return *this;
- }
- if (stringSrc.IsEmpty()) {
- Empty();
- } else if ((m_pData && m_pData->m_nRefs < 0) ||
- (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) {
- AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String);
- } else {
- Empty();
- m_pData = stringSrc.m_pData;
- if (m_pData) {
- m_pData->m_nRefs ++;
- }
- }
- return *this;
-}
-const CFX_WideString& CFX_WideString::operator+=(FX_WCHAR ch)
-{
- ConcatInPlace(1, &ch);
- return *this;
-}
-const CFX_WideString& CFX_WideString::operator+=(FX_LPCWSTR lpsz)
-{
- if (lpsz) {
- ConcatInPlace((FX_STRSIZE)FXSYS_wcslen(lpsz), lpsz);
- }
- return *this;
-}
-const CFX_WideString& CFX_WideString::operator+=(const CFX_WideString& string)
-{
- if (string.m_pData == NULL) {
- return *this;
- }
- ConcatInPlace(string.m_pData->m_nDataLength, string.m_pData->m_String);
- return *this;
-}
-const CFX_WideString& CFX_WideString::operator+=(const CFX_WideStringC& string)
-{
- if (string.IsEmpty()) {
- return *this;
- }
- ConcatInPlace(string.GetLength(), string.GetPtr());
- return *this;
-}
-bool operator==(const CFX_WideString& s1, FX_LPCWSTR s2)
-{
- return s1.Equal(s2);
-}
-bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2)
-{
- return s2.Equal(s1);
-}
-bool operator==(const CFX_WideString& s1, const CFX_WideString& s2)
-{
- return s1.Equal(s2);
-}
-bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2)
-{
- return s1.Equal(s2);
-}
-bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2)
-{
- return s2.Equal(s1);
-}
-bool operator != (const CFX_WideString& s1, FX_LPCWSTR s2)
-{
- return !s1.Equal(s2);
-}
-bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2)
-{
- return !s1.Equal(s2);
-}
-bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2)
-{
- return !s1.Equal(s2);
-}
-bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2)
-{
- return !s2.Equal(s1);
-}
-bool CFX_WideString::Equal(const CFX_WideStringC& str) const
-{
- if (m_pData == NULL) {
- return str.IsEmpty();
- }
- return str.GetLength() == m_pData->m_nDataLength &&
- FXSYS_memcmp32(str.GetPtr(), m_pData->m_String, m_pData->m_nDataLength * sizeof(FX_WCHAR)) == 0;
-}
-void CFX_WideString::Empty()
-{
- if (m_pData == NULL) {
- return;
- }
- if (m_pData->m_nRefs > 1) {
- m_pData->m_nRefs --;
- } else {
- FX_Free(m_pData);
- }
- m_pData = NULL;
-}
-void CFX_WideString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData)
-{
- if (nSrcLen == 0 || lpszSrcData == NULL) {
- return;
- }
- if (m_pData == NULL) {
- m_pData = FX_AllocStringW(nSrcLen);
- if (m_pData) {
- FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
- }
- return;
- }
- if (m_pData->m_nRefs > 1 || m_pData->m_nDataLength + nSrcLen > m_pData->m_nAllocLength) {
- CFX_StringDataW* pOldData = m_pData;
- ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcData);
- FX_ReleaseStringW(pOldData);
- } else {
- FXSYS_memcpy32(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
- m_pData->m_nDataLength += nSrcLen;
- m_pData->m_String[m_pData->m_nDataLength] = 0;
- }
-}
-void CFX_WideString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data,
- FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data)
-{
- FX_STRSIZE nNewLen = nSrc1Len + nSrc2Len;
- if (nNewLen == 0) {
- return;
- }
- m_pData = FX_AllocStringW(nNewLen);
- if (m_pData) {
- FXSYS_memcpy32(m_pData->m_String, lpszSrc1Data, nSrc1Len * sizeof(FX_WCHAR));
- FXSYS_memcpy32(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len * sizeof(FX_WCHAR));
- }
-}
-void CFX_WideString::CopyBeforeWrite()
-{
- if (m_pData == NULL || m_pData->m_nRefs <= 1) {
- return;
- }
- CFX_StringDataW* pData = m_pData;
- m_pData->m_nRefs --;
- FX_STRSIZE nDataLength = pData->m_nDataLength;
- m_pData = FX_AllocStringW(nDataLength);
- if (m_pData != NULL) {
- FXSYS_memcpy32(m_pData->m_String, pData->m_String, (nDataLength + 1) * sizeof(FX_WCHAR));
- }
-}
-void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nLen)
-{
- if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) {
- return;
- }
- Empty();
- m_pData = FX_AllocStringW(nLen);
-}
-void CFX_WideString::AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData)
-{
- AllocBeforeWrite(nSrcLen);
- FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
- m_pData->m_nDataLength = nSrcLen;
- m_pData->m_String[nSrcLen] = 0;
-}
-int CFX_WideString::Compare(FX_LPCWSTR lpsz) const
-{
- if (m_pData == NULL) {
- return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1;
- }
- return FXSYS_wcscmp(m_pData->m_String, lpsz);
-}
-CFX_ByteString CFX_WideString::UTF8Encode() const
-{
- return FX_UTF8Encode(*this);
-}
-CFX_ByteString CFX_WideString::UTF16LE_Encode(FX_BOOL bTerminate) const
-{
- if (m_pData == NULL) {
- return bTerminate ? CFX_ByteString(FX_BSTRC("\0\0")) : CFX_ByteString();
- }
- int len = m_pData->m_nDataLength;
- CFX_ByteString result;
- FX_LPSTR buffer = result.GetBuffer(len * 2 + (bTerminate ? 2 : 0));
- for (int i = 0; i < len; i ++) {
- buffer[i * 2] = m_pData->m_String[i] & 0xff;
- buffer[i * 2 + 1] = m_pData->m_String[i] >> 8;
- }
- if (bTerminate) {
- buffer[len * 2] = 0;
- buffer[len * 2 + 1] = 0;
- result.ReleaseBuffer(len * 2 + 2);
- } else {
- result.ReleaseBuffer(len * 2);
- }
- return result;
-}
-void CFX_WideString::ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap)
-{
- if (pCharMap == NULL) {
- pCharMap = CFX_CharMap::GetDefaultMapper();
- }
- *this = pCharMap->m_GetWideString(pCharMap, str);
-}
-void CFX_WideString::Reserve(FX_STRSIZE len)
-{
- GetBuffer(len);
- ReleaseBuffer(GetLength());
-}
-FX_LPWSTR CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength)
-{
- if (m_pData == NULL && nMinBufLength == 0) {
- return NULL;
- }
- if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLength) {
- return m_pData->m_String;
- }
- if (m_pData == NULL) {
- m_pData = FX_AllocStringW(nMinBufLength);
- if (!m_pData) {
- return NULL;
- }
- m_pData->m_nDataLength = 0;
- m_pData->m_String[0] = 0;
- return m_pData->m_String;
- }
- CFX_StringDataW* pOldData = m_pData;
- FX_STRSIZE nOldLen = pOldData->m_nDataLength;
- if (nMinBufLength < nOldLen) {
- nMinBufLength = nOldLen;
- }
- m_pData = FX_AllocStringW(nMinBufLength);
- if (!m_pData) {
- return NULL;
- }
- FXSYS_memcpy32(m_pData->m_String, pOldData->m_String, (nOldLen + 1)*sizeof(FX_WCHAR));
- m_pData->m_nDataLength = nOldLen;
- pOldData->m_nRefs --;
- if (pOldData->m_nRefs <= 0) {
- FX_Free(pOldData);
- }
- return m_pData->m_String;
-}
-CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len)
-{
- CFX_WideString result;
- result.ConvertFrom(CFX_ByteString(str, len));
- return result;
-}
-CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len)
-{
- if (!str) {
- return CFX_WideString();
- }
- if (len < 0) {
- len = 0;
- while (str[len]) {
- len ++;
- }
- }
- CFX_UTF8Decoder decoder;
- for (FX_STRSIZE i = 0; i < len; i ++) {
- decoder.Input(str[i]);
- }
- return decoder.GetResult();
-}
-CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, FX_STRSIZE wlen)
-{
- if (!wstr || !wlen) {
- return CFX_WideString();
- }
- if (wlen < 0) {
- wlen = 0;
- while (wstr[wlen]) {
- wlen ++;
- }
- }
- CFX_WideString result;
- FX_WCHAR* buf = result.GetBuffer(wlen);
- for (int i = 0; i < wlen; i ++) {
- buf[i] = wstr[i];
- }
- result.ReleaseBuffer(wlen);
- return result;
-}
-void CFX_WideString::AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex,
- FX_STRSIZE nExtraLen) const
-{
- FX_STRSIZE nNewLen = nCopyLen + nExtraLen;
- if (nNewLen == 0) {
- return;
- }
- ASSERT(dest.m_pData == NULL);
- dest.m_pData = FX_AllocStringW(nNewLen);
- if (dest.m_pData) {
- FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, nCopyLen * sizeof(FX_WCHAR));
- }
-}
-CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const
-{
- if (m_pData == NULL) {
- return CFX_WideString();
- }
- if (nCount < 0) {
- nCount = 0;
- }
- if (nCount >= m_pData->m_nDataLength) {
- return *this;
- }
- CFX_WideString dest;
- AllocCopy(dest, nCount, 0, 0);
- return dest;
-}
-CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const
-{
- return Mid(nFirst, m_pData->m_nDataLength - nFirst);
-}
-CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const
-{
- if (m_pData == NULL) {
- return CFX_WideString();
- }
- if (nFirst < 0) {
- nFirst = 0;
- }
- if (nCount < 0) {
- nCount = 0;
- }
- if (nFirst + nCount > m_pData->m_nDataLength) {
- nCount = m_pData->m_nDataLength - nFirst;
- }
- if (nFirst > m_pData->m_nDataLength) {
- nCount = 0;
- }
- if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) {
- return *this;
- }
- CFX_WideString dest;
- AllocCopy(dest, nCount, nFirst, 0);
- return dest;
-}
-CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const
-{
- if (m_pData == NULL) {
- return CFX_WideString();
- }
- if (nCount < 0) {
- nCount = 0;
- }
- if (nCount >= m_pData->m_nDataLength) {
- return *this;
- }
- CFX_WideString dest;
- AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount, 0);
- return dest;
-}
-int CFX_WideString::CompareNoCase(FX_LPCWSTR lpsz) const
-{
- if (m_pData == NULL) {
- return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1;
- }
- return FXSYS_wcsicmp(m_pData->m_String, lpsz);
-}
-int CFX_WideString::Compare(const CFX_WideString& str) const
-{
- if (m_pData == NULL) {
- if (str.m_pData == NULL) {
- return 0;
- }
- return -1;
- } else if (str.m_pData == NULL) {
- return 1;
- }
- int this_len = m_pData->m_nDataLength;
- int that_len = str.m_pData->m_nDataLength;
- int min_len = this_len < that_len ? this_len : that_len;
- for (int i = 0; i < min_len; i ++) {
- if (m_pData->m_String[i] < str.m_pData->m_String[i]) {
- return -1;
- } else if (m_pData->m_String[i] > str.m_pData->m_String[i]) {
- return 1;
- }
- }
- if (this_len < that_len) {
- return -1;
- } else if (this_len > that_len) {
- return 1;
- }
- return 0;
-}
-FX_LPWSTR CFX_WideString::LockBuffer()
-{
- if (m_pData == NULL) {
- return NULL;
- }
- FX_LPWSTR lpsz = GetBuffer(0);
- m_pData->m_nRefs = -1;
- return lpsz;
-}
-void CFX_WideString::SetAt(FX_STRSIZE nIndex, FX_WCHAR ch)
-{
- if (m_pData == NULL) {
- return;
- }
- ASSERT(nIndex >= 0);
- ASSERT(nIndex < m_pData->m_nDataLength);
- CopyBeforeWrite();
- m_pData->m_String[nIndex] = ch;
-}
-void CFX_WideString::MakeLower()
-{
- if (m_pData == NULL) {
- return;
- }
- CopyBeforeWrite();
- if (GetLength() < 1) {
- return;
- }
- FXSYS_wcslwr(m_pData->m_String);
-}
-void CFX_WideString::MakeUpper()
-{
- if (m_pData == NULL) {
- return;
- }
- CopyBeforeWrite();
- if (GetLength() < 1) {
- return;
- }
- FXSYS_wcsupr(m_pData->m_String);
-}
-FX_STRSIZE CFX_WideString::Find(FX_LPCWSTR lpszSub, FX_STRSIZE nStart) const
-{
- FX_STRSIZE nLength = GetLength();
- if (nLength < 1 || nStart > nLength) {
- return -1;
- }
- FX_LPCWSTR lpsz = (FX_LPCWSTR)FXSYS_wcsstr(m_pData->m_String + nStart, lpszSub);
- return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
-}
-FX_STRSIZE CFX_WideString::Find(FX_WCHAR ch, FX_STRSIZE nStart) const
-{
- if (m_pData == NULL) {
- return -1;
- }
- FX_STRSIZE nLength = m_pData->m_nDataLength;
- if (nStart >= nLength) {
- return -1;
- }
- FX_LPCWSTR lpsz = (FX_LPCWSTR)FXSYS_wcschr(m_pData->m_String + nStart, ch);
- return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
-}
-void CFX_WideString::TrimRight(FX_LPCWSTR lpszTargetList)
-{
- FXSYS_assert(lpszTargetList != NULL);
- if (m_pData == NULL || *lpszTargetList == 0) {
- return;
- }
- CopyBeforeWrite();
- FX_STRSIZE len = GetLength();
- if (len < 1) {
- return;
- }
- FX_STRSIZE pos = len;
- while (pos) {
- if (FXSYS_wcschr(lpszTargetList, m_pData->m_String[pos - 1]) == NULL) {
- break;
- }
- pos --;
- }
- if (pos < len) {
- m_pData->m_String[pos] = 0;
- m_pData->m_nDataLength = pos;
- }
-}
-void CFX_WideString::TrimRight(FX_WCHAR chTarget)
-{
- FX_WCHAR str[2] = {chTarget, 0};
- TrimRight(str);
-}
-void CFX_WideString::TrimRight()
-{
- TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20");
-}
-void CFX_WideString::TrimLeft(FX_LPCWSTR lpszTargets)
-{
- FXSYS_assert(lpszTargets != NULL);
- if (m_pData == NULL || *lpszTargets == 0) {
- return;
- }
- CopyBeforeWrite();
- if (GetLength() < 1) {
- return;
- }
- FX_LPCWSTR lpsz = m_pData->m_String;
- while (*lpsz != 0) {
- if (FXSYS_wcschr(lpszTargets, *lpsz) == NULL) {
- break;
- }
- lpsz ++;
- }
- if (lpsz != m_pData->m_String) {
- int nDataLength = m_pData->m_nDataLength - (FX_STRSIZE)(lpsz - m_pData->m_String);
- FXSYS_memmove32(m_pData->m_String, lpsz, (nDataLength + 1)*sizeof(FX_WCHAR));
- m_pData->m_nDataLength = nDataLength;
- }
-}
-void CFX_WideString::TrimLeft(FX_WCHAR chTarget)
-{
- FX_WCHAR str[2] = {chTarget, 0};
- TrimLeft(str);
-}
-void CFX_WideString::TrimLeft()
-{
- TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20");
-}
-FX_STRSIZE CFX_WideString::Replace(FX_LPCWSTR lpszOld, FX_LPCWSTR lpszNew)
-{
- if (GetLength() < 1) {
- return 0;
- }
- if (lpszOld == NULL) {
- return 0;
- }
- FX_STRSIZE nSourceLen = (FX_STRSIZE)FXSYS_wcslen(lpszOld);
- if (nSourceLen == 0) {
- return 0;
- }
- FX_STRSIZE nReplacementLen = lpszNew ? (FX_STRSIZE)FXSYS_wcslen(lpszNew) : 0;
- FX_STRSIZE nCount = 0;
- FX_LPWSTR lpszStart = m_pData->m_String;
- FX_LPWSTR lpszEnd = m_pData->m_String + m_pData->m_nDataLength;
- FX_LPWSTR lpszTarget;
- {
- while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL && lpszStart < lpszEnd) {
- nCount++;
- lpszStart = lpszTarget + nSourceLen;
- }
- }
- if (nCount > 0) {
- CopyBeforeWrite();
- FX_STRSIZE nOldLength = m_pData->m_nDataLength;
- FX_STRSIZE nNewLength = nOldLength + (nReplacementLen - nSourceLen) * nCount;
- if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) {
- CFX_StringDataW* pOldData = m_pData;
- FX_LPCWSTR pstr = m_pData->m_String;
- m_pData = FX_AllocStringW(nNewLength);
- if (!m_pData) {
- return 0;
- }
- FXSYS_memcpy32(m_pData->m_String, pstr, pOldData->m_nDataLength * sizeof(FX_WCHAR));
- FX_ReleaseStringW(pOldData);
- }
- lpszStart = m_pData->m_String;
- lpszEnd = m_pData->m_String + FX_MAX(m_pData->m_nDataLength, nNewLength);
- {
- while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL && lpszStart < lpszEnd) {
- FX_STRSIZE nBalance = nOldLength - (FX_STRSIZE)(lpszTarget - m_pData->m_String + nSourceLen);
- FXSYS_memmove32(lpszTarget + nReplacementLen, lpszTarget + nSourceLen, nBalance * sizeof(FX_WCHAR));
- FXSYS_memcpy32(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_WCHAR));
- lpszStart = lpszTarget + nReplacementLen;
- lpszStart[nBalance] = 0;
- nOldLength += (nReplacementLen - nSourceLen);
- }
- }
- ASSERT(m_pData->m_String[nNewLength] == 0);
- m_pData->m_nDataLength = nNewLength;
- }
- return nCount;
-}
-FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, FX_WCHAR ch)
-{
- CopyBeforeWrite();
- if (nIndex < 0) {
- nIndex = 0;
- }
- FX_STRSIZE nNewLength = GetLength();
- if (nIndex > nNewLength) {
- nIndex = nNewLength;
- }
- nNewLength++;
- if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) {
- CFX_StringDataW* pOldData = m_pData;
- FX_LPCWSTR pstr = m_pData->m_String;
- m_pData = FX_AllocStringW(nNewLength);
- if (!m_pData) {
- return 0;
- }
- if(pOldData != NULL) {
- FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1)*sizeof(FX_WCHAR));
- FX_ReleaseStringW(pOldData);
- } else {
- m_pData->m_String[0] = 0;
- }
- }
- FXSYS_memmove32(m_pData->m_String + nIndex + 1,
- m_pData->m_String + nIndex, (nNewLength - nIndex)*sizeof(FX_WCHAR));
- m_pData->m_String[nIndex] = ch;
- m_pData->m_nDataLength = nNewLength;
- return nNewLength;
-}
-FX_STRSIZE CFX_WideString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount)
-{
- if (GetLength() < 1) {
- return 0;
- }
- if (nIndex < 0) {
- nIndex = 0;
- }
- FX_STRSIZE nOldLength = m_pData->m_nDataLength;
- if (nCount > 0 && nIndex < nOldLength) {
- CopyBeforeWrite();
- int nBytesToCopy = nOldLength - (nIndex + nCount) + 1;
- FXSYS_memmove32(m_pData->m_String + nIndex,
- m_pData->m_String + nIndex + nCount, nBytesToCopy * sizeof(FX_WCHAR));
- m_pData->m_nDataLength = nOldLength - nCount;
- }
- return m_pData->m_nDataLength;
-}
-FX_STRSIZE CFX_WideString::Remove(FX_WCHAR chRemove)
-{
- if (m_pData == NULL) {
- return 0;
- }
- CopyBeforeWrite();
- if (GetLength() < 1) {
- return 0;
- }
- FX_LPWSTR pstrSource = m_pData->m_String;
- FX_LPWSTR pstrDest = m_pData->m_String;
- FX_LPWSTR pstrEnd = m_pData->m_String + m_pData->m_nDataLength;
- while (pstrSource < pstrEnd) {
- if (*pstrSource != chRemove) {
- *pstrDest = *pstrSource;
- pstrDest ++;
- }
- pstrSource ++;
- }
- *pstrDest = 0;
- FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest);
- m_pData->m_nDataLength -= nCount;
- return nCount;
-}
-#define FORCE_ANSI 0x10000
-#define FORCE_UNICODE 0x20000
-#define FORCE_INT64 0x40000
-void CFX_WideString::FormatV(FX_LPCWSTR lpszFormat, va_list argList)
-{
- va_list argListSave;
-#if defined(__ARMCC_VERSION) || (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || _FX_CPU_ == _FX_ARM64_)) || defined(__native_client__)
- va_copy(argListSave, argList);
-#else
- argListSave = argList;
-#endif
- int nMaxLen = 0;
- for (FX_LPCWSTR lpsz = lpszFormat; *lpsz != 0; lpsz ++) {
- if (*lpsz != '%' || *(lpsz = lpsz + 1) == '%') {
- nMaxLen += (FX_STRSIZE)FXSYS_wcslen(lpsz);
- continue;
- }
- int nItemLen = 0;
- int nWidth = 0;
- for (; *lpsz != 0; lpsz ++) {
- if (*lpsz == '#') {
- nMaxLen += 2;
- } else if (*lpsz == '*') {
- nWidth = va_arg(argList, int);
- } else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0' ||
- *lpsz == ' ')
- ;
- else {
- break;
- }
- }
- if (nWidth == 0) {
- nWidth = FXSYS_wtoi(lpsz);
- for (; *lpsz != 0 && (*lpsz) <= '9' && (*lpsz) >= '0'; lpsz ++)
- ;
- }
- if (nWidth < 0 || nWidth > 128 * 1024) {
- lpszFormat = (FX_LPCWSTR)L"Bad width";
- nMaxLen = 10;
- break;
- }
- int nPrecision = 0;
- if (*lpsz == '.') {
- lpsz ++;
- if (*lpsz == '*') {
- nPrecision = va_arg(argList, int);
- lpsz ++;
- } else {
- nPrecision = FXSYS_wtoi(lpsz);
- for (; *lpsz != 0 && (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz ++)
- ;
- }
- }
- if (nPrecision < 0 || nPrecision > 128 * 1024) {
- lpszFormat = (FX_LPCWSTR)L"Bad precision";
- nMaxLen = 14;
- break;
- }
- int nModifier = 0;
- if (*lpsz == L'I' && *(lpsz + 1) == L'6' && *(lpsz + 2) == L'4') {
- lpsz += 3;
- nModifier = FORCE_INT64;
- } else {
- switch (*lpsz) {
- case 'h':
- nModifier = FORCE_ANSI;
- lpsz ++;
- break;
- case 'l':
- nModifier = FORCE_UNICODE;
- lpsz ++;
- break;
- case 'F':
- case 'N':
- case 'L':
- lpsz ++;
- break;
- }
- }
- switch (*lpsz | nModifier) {
- case 'c':
- case 'C':
- nItemLen = 2;
- va_arg(argList, int);
- break;
- case 'c'|FORCE_ANSI:
- case 'C'|FORCE_ANSI:
- nItemLen = 2;
- va_arg(argList, int);
- break;
- case 'c'|FORCE_UNICODE:
- case 'C'|FORCE_UNICODE:
- nItemLen = 2;
- va_arg(argList, int);
- break;
- case 's': {
- FX_LPCWSTR pstrNextArg = va_arg(argList, FX_LPCWSTR);
- if (pstrNextArg == NULL) {
- nItemLen = 6;
- } else {
- nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg);
- if (nItemLen < 1) {
- nItemLen = 1;
- }
- }
- }
- break;
- case 'S': {
- FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR);
- if (pstrNextArg == NULL) {
- nItemLen = 6;
- } else {
- nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg);
- if (nItemLen < 1) {
- nItemLen = 1;
- }
- }
- }
- break;
- case 's'|FORCE_ANSI:
- case 'S'|FORCE_ANSI: {
- FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR);
- if (pstrNextArg == NULL) {
- nItemLen = 6;
- } else {
- nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg);
- if (nItemLen < 1) {
- nItemLen = 1;
- }
- }
- }
- break;
- case 's'|FORCE_UNICODE:
- case 'S'|FORCE_UNICODE: {
- FX_LPWSTR pstrNextArg = va_arg(argList, FX_LPWSTR);
- if (pstrNextArg == NULL) {
- nItemLen = 6;
- } else {
- nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg);
- if (nItemLen < 1) {
- nItemLen = 1;
- }
- }
- }
- break;
- }
- if (nItemLen != 0) {
- if (nPrecision != 0 && nItemLen > nPrecision) {
- nItemLen = nPrecision;
- }
- if (nItemLen < nWidth) {
- nItemLen = nWidth;
- }
- } else {
- switch (*lpsz) {
- case 'd':
- case 'i':
- case 'u':
- case 'x':
- case 'X':
- case 'o':
- if (nModifier & FORCE_INT64) {
- va_arg(argList, FX_INT64);
- } else {
- va_arg(argList, int);
- }
- nItemLen = 32;
- if (nItemLen < nWidth + nPrecision) {
- nItemLen = nWidth + nPrecision;
- }
- break;
- case 'a':
- case 'A':
- case 'e':
- case 'E':
- case 'g':
- case 'G':
- va_arg(argList, double);
- nItemLen = 128;
- if (nItemLen < nWidth + nPrecision) {
- nItemLen = nWidth + nPrecision;
- }
- break;
- case 'f':
- if (nWidth + nPrecision > 100) {
- nItemLen = nPrecision + nWidth + 128;
- } else {
- double f;
- char pszTemp[256];
- f = va_arg(argList, double);
- FXSYS_snprintf(pszTemp, sizeof(pszTemp), "%*.*f", nWidth, nPrecision + 6, f );
- nItemLen = (FX_STRSIZE)FXSYS_strlen(pszTemp);
- }
- break;
- case 'p':
- va_arg(argList, void*);
- nItemLen = 32;
- if (nItemLen < nWidth + nPrecision) {
- nItemLen = nWidth + nPrecision;
- }
- break;
- case 'n':
- va_arg(argList, int*);
- break;
- }
- }
- nMaxLen += nItemLen;
- }
- GetBuffer(nMaxLen);
- if (m_pData) {
- FXSYS_vswprintf((wchar_t*)m_pData->m_String, nMaxLen + 1, (const wchar_t*)lpszFormat, argListSave);
- ReleaseBuffer();
- }
- va_end(argListSave);
-}
-void CFX_WideString::Format(FX_LPCWSTR lpszFormat, ...)
-{
- va_list argList;
- va_start(argList, lpszFormat);
- FormatV(lpszFormat, argList);
- va_end(argList);
-}
-FX_FLOAT FX_wtof(FX_LPCWSTR str, int len)
-{
- if (len == 0) {
- return 0.0;
- }
- int cc = 0;
- FX_BOOL bNegative = FALSE;
- if (str[0] == '+') {
- cc++;
- } else if (str[0] == '-') {
- bNegative = TRUE;
- cc++;
- }
- int integer = 0;
- while (cc < len) {
- if (str[cc] == '.') {
- break;
- }
- integer = integer * 10 + str[cc] - '0';
- cc ++;
- }
- FX_FLOAT fraction = 0;
- if (str[cc] == '.') {
- cc ++;
- FX_FLOAT scale = 0.1f;
- while (cc < len) {
- fraction += scale * (str[cc] - '0');
- scale *= 0.1f;
- cc ++;
- }
- }
- fraction += (FX_FLOAT)integer;
- return bNegative ? -fraction : fraction;
-}
-int CFX_WideString::GetInteger() const
-{
- if (m_pData == NULL) {
- return 0;
- }
- return FXSYS_wtoi(m_pData->m_String);
-}
-FX_FLOAT CFX_WideString::GetFloat() const
-{
- if (m_pData == NULL) {
- return 0.0;
- }
- return FX_wtof(m_pData->m_String, m_pData->m_nDataLength);
-}
-void CFX_WideStringL::Empty(IFX_Allocator* pAllocator)
-{
- if (m_Ptr) {
- FX_Allocator_Free(pAllocator, (FX_LPVOID)m_Ptr);
- }
- m_Ptr = NULL, m_Length = 0;
-}
-void CFX_WideStringL::Set(FX_WSTR src, IFX_Allocator* pAllocator)
-{
- Empty(pAllocator);
- if (src.GetPtr() != NULL && src.GetLength() > 0) {
- FX_LPWSTR str = FX_Allocator_Alloc(pAllocator, FX_WCHAR, src.GetLength() + 1);
- if (!str) {
- return;
- }
- FXSYS_memcpy32(str, src.GetPtr(), src.GetLength()*sizeof(FX_WCHAR));
- str[src.GetLength()] = '\0';
- *(FX_LPWSTR*)(&m_Ptr) = str;
- m_Length = src.GetLength();
- }
-}
-int CFX_WideStringL::GetInteger() const
-{
- if (!m_Ptr) {
- return 0;
- }
- return FXSYS_wtoi(m_Ptr);
-}
-FX_FLOAT CFX_WideStringL::GetFloat() const
-{
- if (!m_Ptr) {
- return 0.0f;
- }
- return FX_wtof(m_Ptr, m_Length);
-}
-void CFX_WideStringL::TrimRight(FX_LPCWSTR lpszTargets)
-{
- if (!lpszTargets || *lpszTargets == 0 || !m_Ptr || m_Length < 1) {
- return;
- }
- FX_STRSIZE pos = m_Length;
- while (pos) {
- if (FXSYS_wcschr(lpszTargets, m_Ptr[pos - 1]) == NULL) {
- break;
- }
- pos --;
- }
- if (pos < m_Length) {
- (*(FX_LPWSTR*)(&m_Ptr))[pos] = 0;
- m_Length = pos;
- }
-}
-static CFX_ByteString _DefMap_GetByteString(CFX_CharMap* pCharMap, const CFX_WideString& widestr)
-{
- int src_len = widestr.GetLength();
- int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0;
- int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, widestr, src_len, NULL, 0, NULL, NULL);
- if (dest_len == 0) {
- return CFX_ByteString();
- }
- CFX_ByteString bytestr;
- FX_LPSTR dest_buf = bytestr.GetBuffer(dest_len);
- FXSYS_WideCharToMultiByte(codepage, 0, widestr, src_len, dest_buf, dest_len, NULL, NULL);
- bytestr.ReleaseBuffer(dest_len);
- return bytestr;
-}
-static CFX_WideString _DefMap_GetWideString(CFX_CharMap* pCharMap, const CFX_ByteString& bytestr)
-{
- int src_len = bytestr.GetLength();
- int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0;
- int dest_len = FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, NULL, 0);
- if (dest_len == 0) {
- return CFX_WideString();
- }
- CFX_WideString widestr;
- FX_LPWSTR dest_buf = widestr.GetBuffer(dest_len);
- FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, dest_buf, dest_len);
- widestr.ReleaseBuffer(dest_len);
- return widestr;
-}
-static int _DefMap_GetGBKCodePage()
-{
- return 936;
-}
-static int _DefMap_GetUHCCodePage()
-{
- return 949;
-}
-static int _DefMap_GetJISCodePage()
-{
- return 932;
-}
-static int _DefMap_GetBig5CodePage()
-{
- return 950;
-}
-static const CFX_CharMap g_DefaultMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, NULL};
-static const CFX_CharMap g_DefaultGBKMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetGBKCodePage};
-static const CFX_CharMap g_DefaultJISMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetJISCodePage};
-static const CFX_CharMap g_DefaultUHCMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetUHCCodePage};
-static const CFX_CharMap g_DefaultBig5Mapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetBig5CodePage};
-CFX_CharMap* CFX_CharMap::GetDefaultMapper(FX_INT32 codepage)
-{
- switch (codepage) {
- case 0:
- return (CFX_CharMap*)&g_DefaultMapper;
- case 932:
- return (CFX_CharMap*)&g_DefaultJISMapper;
- case 936:
- return (CFX_CharMap*)&g_DefaultGBKMapper;
- case 949:
- return (CFX_CharMap*)&g_DefaultUHCMapper;
- case 950:
- return (CFX_CharMap*)&g_DefaultBig5Mapper;
- }
- return NULL;
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_basic.h" +static CFX_StringDataW* FX_AllocStringW(int nLen) +{ + if (nLen == 0) { + return NULL; + } + CFX_StringDataW* pData = (CFX_StringDataW*)FX_Alloc(FX_BYTE, sizeof(long) * 3 + (nLen + 1) * sizeof(FX_WCHAR)); + if (!pData) { + return NULL; + } + pData->m_nAllocLength = nLen; + pData->m_nDataLength = nLen; + pData->m_nRefs = 1; + pData->m_String[nLen] = 0; + return pData; +} +static void FX_ReleaseStringW(CFX_StringDataW* pData) +{ + if (pData == NULL) { + return; + } + pData->m_nRefs --; + if (pData->m_nRefs <= 0) { + FX_Free(pData); + } +} +CFX_WideString::~CFX_WideString() +{ + if (m_pData == NULL) { + return; + } + m_pData->m_nRefs --; + if (m_pData->m_nRefs < 1) { + FX_Free(m_pData); + } +} +void CFX_WideString::InitStr(FX_LPCWSTR lpsz, FX_STRSIZE nLen) +{ + if (nLen < 0) { + nLen = lpsz ? (FX_STRSIZE)FXSYS_wcslen(lpsz) : 0; + } + if (nLen) { + m_pData = FX_AllocStringW(nLen); + if (!m_pData) { + return; + } + FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(FX_WCHAR)); + } else { + m_pData = NULL; + } +} +CFX_WideString::CFX_WideString(const CFX_WideString& stringSrc) +{ + if (stringSrc.m_pData == NULL) { + m_pData = NULL; + return; + } + if (stringSrc.m_pData->m_nRefs >= 0) { + m_pData = stringSrc.m_pData; + m_pData->m_nRefs ++; + } else { + m_pData = NULL; + *this = stringSrc; + } +} +CFX_WideString::CFX_WideString(FX_WCHAR ch) +{ + m_pData = FX_AllocStringW(1); + if (m_pData) { + m_pData->m_String[0] = ch; + } +} +CFX_WideString::CFX_WideString(const CFX_WideStringC& str) +{ + if (str.IsEmpty()) { + m_pData = NULL; + return; + } + m_pData = FX_AllocStringW(str.GetLength()); + if (m_pData) { + FXSYS_memcpy32(m_pData->m_String, str.GetPtr(), str.GetLength()*sizeof(FX_WCHAR)); + } +} +CFX_WideString::CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2) +{ + m_pData = NULL; + int nNewLen = str1.GetLength() + str2.GetLength(); + if (nNewLen == 0) { + return; + } + m_pData = FX_AllocStringW(nNewLen); + if (m_pData) { + FXSYS_memcpy32(m_pData->m_String, str1.GetPtr(), str1.GetLength()*sizeof(FX_WCHAR)); + FXSYS_memcpy32(m_pData->m_String + str1.GetLength(), str2.GetPtr(), str2.GetLength()*sizeof(FX_WCHAR)); + } +} +void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) +{ + if (m_pData == NULL) { + return; + } + CopyBeforeWrite(); + if (nNewLength == -1) { + nNewLength = m_pData ? (FX_STRSIZE)FXSYS_wcslen(m_pData->m_String) : 0; + } + if (nNewLength == 0) { + Empty(); + return; + } + FXSYS_assert(nNewLength <= m_pData->m_nAllocLength); + m_pData->m_nDataLength = nNewLength; + m_pData->m_String[nNewLength] = 0; +} +const CFX_WideString& CFX_WideString::operator=(FX_LPCWSTR lpsz) +{ + if (lpsz == NULL || lpsz[0] == 0) { + Empty(); + } else { + AssignCopy((FX_STRSIZE)FXSYS_wcslen(lpsz), lpsz); + } + return *this; +} +const CFX_WideString& CFX_WideString::operator=(const CFX_WideStringC& stringSrc) +{ + if (stringSrc.IsEmpty()) { + Empty(); + } else { + AssignCopy(stringSrc.GetLength(), stringSrc.GetPtr()); + } + return *this; +} +const CFX_WideString& CFX_WideString::operator=(const CFX_WideString& stringSrc) +{ + if (m_pData == stringSrc.m_pData) { + return *this; + } + if (stringSrc.IsEmpty()) { + Empty(); + } else if ((m_pData && m_pData->m_nRefs < 0) || + (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) { + AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String); + } else { + Empty(); + m_pData = stringSrc.m_pData; + if (m_pData) { + m_pData->m_nRefs ++; + } + } + return *this; +} +const CFX_WideString& CFX_WideString::operator+=(FX_WCHAR ch) +{ + ConcatInPlace(1, &ch); + return *this; +} +const CFX_WideString& CFX_WideString::operator+=(FX_LPCWSTR lpsz) +{ + if (lpsz) { + ConcatInPlace((FX_STRSIZE)FXSYS_wcslen(lpsz), lpsz); + } + return *this; +} +const CFX_WideString& CFX_WideString::operator+=(const CFX_WideString& string) +{ + if (string.m_pData == NULL) { + return *this; + } + ConcatInPlace(string.m_pData->m_nDataLength, string.m_pData->m_String); + return *this; +} +const CFX_WideString& CFX_WideString::operator+=(const CFX_WideStringC& string) +{ + if (string.IsEmpty()) { + return *this; + } + ConcatInPlace(string.GetLength(), string.GetPtr()); + return *this; +} +bool operator==(const CFX_WideString& s1, FX_LPCWSTR s2) +{ + return s1.Equal(s2); +} +bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2) +{ + return s2.Equal(s1); +} +bool operator==(const CFX_WideString& s1, const CFX_WideString& s2) +{ + return s1.Equal(s2); +} +bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2) +{ + return s1.Equal(s2); +} +bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2) +{ + return s2.Equal(s1); +} +bool operator != (const CFX_WideString& s1, FX_LPCWSTR s2) +{ + return !s1.Equal(s2); +} +bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2) +{ + return !s1.Equal(s2); +} +bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2) +{ + return !s1.Equal(s2); +} +bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2) +{ + return !s2.Equal(s1); +} +bool CFX_WideString::Equal(const CFX_WideStringC& str) const +{ + if (m_pData == NULL) { + return str.IsEmpty(); + } + return str.GetLength() == m_pData->m_nDataLength && + FXSYS_memcmp32(str.GetPtr(), m_pData->m_String, m_pData->m_nDataLength * sizeof(FX_WCHAR)) == 0; +} +void CFX_WideString::Empty() +{ + if (m_pData == NULL) { + return; + } + if (m_pData->m_nRefs > 1) { + m_pData->m_nRefs --; + } else { + FX_Free(m_pData); + } + m_pData = NULL; +} +void CFX_WideString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData) +{ + if (nSrcLen == 0 || lpszSrcData == NULL) { + return; + } + if (m_pData == NULL) { + m_pData = FX_AllocStringW(nSrcLen); + if (m_pData) { + FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR)); + } + return; + } + if (m_pData->m_nRefs > 1 || m_pData->m_nDataLength + nSrcLen > m_pData->m_nAllocLength) { + CFX_StringDataW* pOldData = m_pData; + ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcData); + FX_ReleaseStringW(pOldData); + } else { + FXSYS_memcpy32(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData, nSrcLen * sizeof(FX_WCHAR)); + m_pData->m_nDataLength += nSrcLen; + m_pData->m_String[m_pData->m_nDataLength] = 0; + } +} +void CFX_WideString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data, + FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data) +{ + FX_STRSIZE nNewLen = nSrc1Len + nSrc2Len; + if (nNewLen == 0) { + return; + } + m_pData = FX_AllocStringW(nNewLen); + if (m_pData) { + FXSYS_memcpy32(m_pData->m_String, lpszSrc1Data, nSrc1Len * sizeof(FX_WCHAR)); + FXSYS_memcpy32(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len * sizeof(FX_WCHAR)); + } +} +void CFX_WideString::CopyBeforeWrite() +{ + if (m_pData == NULL || m_pData->m_nRefs <= 1) { + return; + } + CFX_StringDataW* pData = m_pData; + m_pData->m_nRefs --; + FX_STRSIZE nDataLength = pData->m_nDataLength; + m_pData = FX_AllocStringW(nDataLength); + if (m_pData != NULL) { + FXSYS_memcpy32(m_pData->m_String, pData->m_String, (nDataLength + 1) * sizeof(FX_WCHAR)); + } +} +void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nLen) +{ + if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) { + return; + } + Empty(); + m_pData = FX_AllocStringW(nLen); +} +void CFX_WideString::AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData) +{ + AllocBeforeWrite(nSrcLen); + FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR)); + m_pData->m_nDataLength = nSrcLen; + m_pData->m_String[nSrcLen] = 0; +} +int CFX_WideString::Compare(FX_LPCWSTR lpsz) const +{ + if (m_pData == NULL) { + return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1; + } + return FXSYS_wcscmp(m_pData->m_String, lpsz); +} +CFX_ByteString CFX_WideString::UTF8Encode() const +{ + return FX_UTF8Encode(*this); +} +CFX_ByteString CFX_WideString::UTF16LE_Encode(FX_BOOL bTerminate) const +{ + if (m_pData == NULL) { + return bTerminate ? CFX_ByteString(FX_BSTRC("\0\0")) : CFX_ByteString(); + } + int len = m_pData->m_nDataLength; + CFX_ByteString result; + FX_LPSTR buffer = result.GetBuffer(len * 2 + (bTerminate ? 2 : 0)); + for (int i = 0; i < len; i ++) { + buffer[i * 2] = m_pData->m_String[i] & 0xff; + buffer[i * 2 + 1] = m_pData->m_String[i] >> 8; + } + if (bTerminate) { + buffer[len * 2] = 0; + buffer[len * 2 + 1] = 0; + result.ReleaseBuffer(len * 2 + 2); + } else { + result.ReleaseBuffer(len * 2); + } + return result; +} +void CFX_WideString::ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap) +{ + if (pCharMap == NULL) { + pCharMap = CFX_CharMap::GetDefaultMapper(); + } + *this = pCharMap->m_GetWideString(pCharMap, str); +} +void CFX_WideString::Reserve(FX_STRSIZE len) +{ + GetBuffer(len); + ReleaseBuffer(GetLength()); +} +FX_LPWSTR CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) +{ + if (m_pData == NULL && nMinBufLength == 0) { + return NULL; + } + if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLength) { + return m_pData->m_String; + } + if (m_pData == NULL) { + m_pData = FX_AllocStringW(nMinBufLength); + if (!m_pData) { + return NULL; + } + m_pData->m_nDataLength = 0; + m_pData->m_String[0] = 0; + return m_pData->m_String; + } + CFX_StringDataW* pOldData = m_pData; + FX_STRSIZE nOldLen = pOldData->m_nDataLength; + if (nMinBufLength < nOldLen) { + nMinBufLength = nOldLen; + } + m_pData = FX_AllocStringW(nMinBufLength); + if (!m_pData) { + return NULL; + } + FXSYS_memcpy32(m_pData->m_String, pOldData->m_String, (nOldLen + 1)*sizeof(FX_WCHAR)); + m_pData->m_nDataLength = nOldLen; + pOldData->m_nRefs --; + if (pOldData->m_nRefs <= 0) { + FX_Free(pOldData); + } + return m_pData->m_String; +} +CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len) +{ + CFX_WideString result; + result.ConvertFrom(CFX_ByteString(str, len)); + return result; +} +CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len) +{ + if (!str) { + return CFX_WideString(); + } + if (len < 0) { + len = 0; + while (str[len]) { + len ++; + } + } + CFX_UTF8Decoder decoder; + for (FX_STRSIZE i = 0; i < len; i ++) { + decoder.Input(str[i]); + } + return decoder.GetResult(); +} +CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, FX_STRSIZE wlen) +{ + if (!wstr || !wlen) { + return CFX_WideString(); + } + if (wlen < 0) { + wlen = 0; + while (wstr[wlen]) { + wlen ++; + } + } + CFX_WideString result; + FX_WCHAR* buf = result.GetBuffer(wlen); + for (int i = 0; i < wlen; i ++) { + buf[i] = wstr[i]; + } + result.ReleaseBuffer(wlen); + return result; +} +void CFX_WideString::AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex, + FX_STRSIZE nExtraLen) const +{ + FX_STRSIZE nNewLen = nCopyLen + nExtraLen; + if (nNewLen == 0) { + return; + } + ASSERT(dest.m_pData == NULL); + dest.m_pData = FX_AllocStringW(nNewLen); + if (dest.m_pData) { + FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, nCopyLen * sizeof(FX_WCHAR)); + } +} +CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const +{ + if (m_pData == NULL) { + return CFX_WideString(); + } + if (nCount < 0) { + nCount = 0; + } + if (nCount >= m_pData->m_nDataLength) { + return *this; + } + CFX_WideString dest; + AllocCopy(dest, nCount, 0, 0); + return dest; +} +CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const +{ + return Mid(nFirst, m_pData->m_nDataLength - nFirst); +} +CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const +{ + if (m_pData == NULL) { + return CFX_WideString(); + } + if (nFirst < 0) { + nFirst = 0; + } + if (nCount < 0) { + nCount = 0; + } + if (nFirst + nCount > m_pData->m_nDataLength) { + nCount = m_pData->m_nDataLength - nFirst; + } + if (nFirst > m_pData->m_nDataLength) { + nCount = 0; + } + if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) { + return *this; + } + CFX_WideString dest; + AllocCopy(dest, nCount, nFirst, 0); + return dest; +} +CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const +{ + if (m_pData == NULL) { + return CFX_WideString(); + } + if (nCount < 0) { + nCount = 0; + } + if (nCount >= m_pData->m_nDataLength) { + return *this; + } + CFX_WideString dest; + AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount, 0); + return dest; +} +int CFX_WideString::CompareNoCase(FX_LPCWSTR lpsz) const +{ + if (m_pData == NULL) { + return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1; + } + return FXSYS_wcsicmp(m_pData->m_String, lpsz); +} +int CFX_WideString::Compare(const CFX_WideString& str) const +{ + if (m_pData == NULL) { + if (str.m_pData == NULL) { + return 0; + } + return -1; + } else if (str.m_pData == NULL) { + return 1; + } + int this_len = m_pData->m_nDataLength; + int that_len = str.m_pData->m_nDataLength; + int min_len = this_len < that_len ? this_len : that_len; + for (int i = 0; i < min_len; i ++) { + if (m_pData->m_String[i] < str.m_pData->m_String[i]) { + return -1; + } else if (m_pData->m_String[i] > str.m_pData->m_String[i]) { + return 1; + } + } + if (this_len < that_len) { + return -1; + } else if (this_len > that_len) { + return 1; + } + return 0; +} +FX_LPWSTR CFX_WideString::LockBuffer() +{ + if (m_pData == NULL) { + return NULL; + } + FX_LPWSTR lpsz = GetBuffer(0); + m_pData->m_nRefs = -1; + return lpsz; +} +void CFX_WideString::SetAt(FX_STRSIZE nIndex, FX_WCHAR ch) +{ + if (m_pData == NULL) { + return; + } + ASSERT(nIndex >= 0); + ASSERT(nIndex < m_pData->m_nDataLength); + CopyBeforeWrite(); + m_pData->m_String[nIndex] = ch; +} +void CFX_WideString::MakeLower() +{ + if (m_pData == NULL) { + return; + } + CopyBeforeWrite(); + if (GetLength() < 1) { + return; + } + FXSYS_wcslwr(m_pData->m_String); +} +void CFX_WideString::MakeUpper() +{ + if (m_pData == NULL) { + return; + } + CopyBeforeWrite(); + if (GetLength() < 1) { + return; + } + FXSYS_wcsupr(m_pData->m_String); +} +FX_STRSIZE CFX_WideString::Find(FX_LPCWSTR lpszSub, FX_STRSIZE nStart) const +{ + FX_STRSIZE nLength = GetLength(); + if (nLength < 1 || nStart > nLength) { + return -1; + } + FX_LPCWSTR lpsz = (FX_LPCWSTR)FXSYS_wcsstr(m_pData->m_String + nStart, lpszSub); + return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String); +} +FX_STRSIZE CFX_WideString::Find(FX_WCHAR ch, FX_STRSIZE nStart) const +{ + if (m_pData == NULL) { + return -1; + } + FX_STRSIZE nLength = m_pData->m_nDataLength; + if (nStart >= nLength) { + return -1; + } + FX_LPCWSTR lpsz = (FX_LPCWSTR)FXSYS_wcschr(m_pData->m_String + nStart, ch); + return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String); +} +void CFX_WideString::TrimRight(FX_LPCWSTR lpszTargetList) +{ + FXSYS_assert(lpszTargetList != NULL); + if (m_pData == NULL || *lpszTargetList == 0) { + return; + } + CopyBeforeWrite(); + FX_STRSIZE len = GetLength(); + if (len < 1) { + return; + } + FX_STRSIZE pos = len; + while (pos) { + if (FXSYS_wcschr(lpszTargetList, m_pData->m_String[pos - 1]) == NULL) { + break; + } + pos --; + } + if (pos < len) { + m_pData->m_String[pos] = 0; + m_pData->m_nDataLength = pos; + } +} +void CFX_WideString::TrimRight(FX_WCHAR chTarget) +{ + FX_WCHAR str[2] = {chTarget, 0}; + TrimRight(str); +} +void CFX_WideString::TrimRight() +{ + TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20"); +} +void CFX_WideString::TrimLeft(FX_LPCWSTR lpszTargets) +{ + FXSYS_assert(lpszTargets != NULL); + if (m_pData == NULL || *lpszTargets == 0) { + return; + } + CopyBeforeWrite(); + if (GetLength() < 1) { + return; + } + FX_LPCWSTR lpsz = m_pData->m_String; + while (*lpsz != 0) { + if (FXSYS_wcschr(lpszTargets, *lpsz) == NULL) { + break; + } + lpsz ++; + } + if (lpsz != m_pData->m_String) { + int nDataLength = m_pData->m_nDataLength - (FX_STRSIZE)(lpsz - m_pData->m_String); + FXSYS_memmove32(m_pData->m_String, lpsz, (nDataLength + 1)*sizeof(FX_WCHAR)); + m_pData->m_nDataLength = nDataLength; + } +} +void CFX_WideString::TrimLeft(FX_WCHAR chTarget) +{ + FX_WCHAR str[2] = {chTarget, 0}; + TrimLeft(str); +} +void CFX_WideString::TrimLeft() +{ + TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20"); +} +FX_STRSIZE CFX_WideString::Replace(FX_LPCWSTR lpszOld, FX_LPCWSTR lpszNew) +{ + if (GetLength() < 1) { + return 0; + } + if (lpszOld == NULL) { + return 0; + } + FX_STRSIZE nSourceLen = (FX_STRSIZE)FXSYS_wcslen(lpszOld); + if (nSourceLen == 0) { + return 0; + } + FX_STRSIZE nReplacementLen = lpszNew ? (FX_STRSIZE)FXSYS_wcslen(lpszNew) : 0; + FX_STRSIZE nCount = 0; + FX_LPWSTR lpszStart = m_pData->m_String; + FX_LPWSTR lpszEnd = m_pData->m_String + m_pData->m_nDataLength; + FX_LPWSTR lpszTarget; + { + while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL && lpszStart < lpszEnd) { + nCount++; + lpszStart = lpszTarget + nSourceLen; + } + } + if (nCount > 0) { + CopyBeforeWrite(); + FX_STRSIZE nOldLength = m_pData->m_nDataLength; + FX_STRSIZE nNewLength = nOldLength + (nReplacementLen - nSourceLen) * nCount; + if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) { + CFX_StringDataW* pOldData = m_pData; + FX_LPCWSTR pstr = m_pData->m_String; + m_pData = FX_AllocStringW(nNewLength); + if (!m_pData) { + return 0; + } + FXSYS_memcpy32(m_pData->m_String, pstr, pOldData->m_nDataLength * sizeof(FX_WCHAR)); + FX_ReleaseStringW(pOldData); + } + lpszStart = m_pData->m_String; + lpszEnd = m_pData->m_String + FX_MAX(m_pData->m_nDataLength, nNewLength); + { + while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL && lpszStart < lpszEnd) { + FX_STRSIZE nBalance = nOldLength - (FX_STRSIZE)(lpszTarget - m_pData->m_String + nSourceLen); + FXSYS_memmove32(lpszTarget + nReplacementLen, lpszTarget + nSourceLen, nBalance * sizeof(FX_WCHAR)); + FXSYS_memcpy32(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_WCHAR)); + lpszStart = lpszTarget + nReplacementLen; + lpszStart[nBalance] = 0; + nOldLength += (nReplacementLen - nSourceLen); + } + } + ASSERT(m_pData->m_String[nNewLength] == 0); + m_pData->m_nDataLength = nNewLength; + } + return nCount; +} +FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, FX_WCHAR ch) +{ + CopyBeforeWrite(); + if (nIndex < 0) { + nIndex = 0; + } + FX_STRSIZE nNewLength = GetLength(); + if (nIndex > nNewLength) { + nIndex = nNewLength; + } + nNewLength++; + if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) { + CFX_StringDataW* pOldData = m_pData; + FX_LPCWSTR pstr = m_pData->m_String; + m_pData = FX_AllocStringW(nNewLength); + if (!m_pData) { + return 0; + } + if(pOldData != NULL) { + FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1)*sizeof(FX_WCHAR)); + FX_ReleaseStringW(pOldData); + } else { + m_pData->m_String[0] = 0; + } + } + FXSYS_memmove32(m_pData->m_String + nIndex + 1, + m_pData->m_String + nIndex, (nNewLength - nIndex)*sizeof(FX_WCHAR)); + m_pData->m_String[nIndex] = ch; + m_pData->m_nDataLength = nNewLength; + return nNewLength; +} +FX_STRSIZE CFX_WideString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) +{ + if (GetLength() < 1) { + return 0; + } + if (nIndex < 0) { + nIndex = 0; + } + FX_STRSIZE nOldLength = m_pData->m_nDataLength; + if (nCount > 0 && nIndex < nOldLength) { + CopyBeforeWrite(); + int nBytesToCopy = nOldLength - (nIndex + nCount) + 1; + FXSYS_memmove32(m_pData->m_String + nIndex, + m_pData->m_String + nIndex + nCount, nBytesToCopy * sizeof(FX_WCHAR)); + m_pData->m_nDataLength = nOldLength - nCount; + } + return m_pData->m_nDataLength; +} +FX_STRSIZE CFX_WideString::Remove(FX_WCHAR chRemove) +{ + if (m_pData == NULL) { + return 0; + } + CopyBeforeWrite(); + if (GetLength() < 1) { + return 0; + } + FX_LPWSTR pstrSource = m_pData->m_String; + FX_LPWSTR pstrDest = m_pData->m_String; + FX_LPWSTR pstrEnd = m_pData->m_String + m_pData->m_nDataLength; + while (pstrSource < pstrEnd) { + if (*pstrSource != chRemove) { + *pstrDest = *pstrSource; + pstrDest ++; + } + pstrSource ++; + } + *pstrDest = 0; + FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest); + m_pData->m_nDataLength -= nCount; + return nCount; +} +#define FORCE_ANSI 0x10000 +#define FORCE_UNICODE 0x20000 +#define FORCE_INT64 0x40000 +void CFX_WideString::FormatV(FX_LPCWSTR lpszFormat, va_list argList) +{ + va_list argListSave; +#if defined(__ARMCC_VERSION) || (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || _FX_CPU_ == _FX_ARM64_)) || defined(__native_client__) + va_copy(argListSave, argList); +#else + argListSave = argList; +#endif + int nMaxLen = 0; + for (FX_LPCWSTR lpsz = lpszFormat; *lpsz != 0; lpsz ++) { + if (*lpsz != '%' || *(lpsz = lpsz + 1) == '%') { + nMaxLen += (FX_STRSIZE)FXSYS_wcslen(lpsz); + continue; + } + int nItemLen = 0; + int nWidth = 0; + for (; *lpsz != 0; lpsz ++) { + if (*lpsz == '#') { + nMaxLen += 2; + } else if (*lpsz == '*') { + nWidth = va_arg(argList, int); + } else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0' || + *lpsz == ' ') + ; + else { + break; + } + } + if (nWidth == 0) { + nWidth = FXSYS_wtoi(lpsz); + for (; *lpsz != 0 && (*lpsz) <= '9' && (*lpsz) >= '0'; lpsz ++) + ; + } + if (nWidth < 0 || nWidth > 128 * 1024) { + lpszFormat = (FX_LPCWSTR)L"Bad width"; + nMaxLen = 10; + break; + } + int nPrecision = 0; + if (*lpsz == '.') { + lpsz ++; + if (*lpsz == '*') { + nPrecision = va_arg(argList, int); + lpsz ++; + } else { + nPrecision = FXSYS_wtoi(lpsz); + for (; *lpsz != 0 && (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz ++) + ; + } + } + if (nPrecision < 0 || nPrecision > 128 * 1024) { + lpszFormat = (FX_LPCWSTR)L"Bad precision"; + nMaxLen = 14; + break; + } + int nModifier = 0; + if (*lpsz == L'I' && *(lpsz + 1) == L'6' && *(lpsz + 2) == L'4') { + lpsz += 3; + nModifier = FORCE_INT64; + } else { + switch (*lpsz) { + case 'h': + nModifier = FORCE_ANSI; + lpsz ++; + break; + case 'l': + nModifier = FORCE_UNICODE; + lpsz ++; + break; + case 'F': + case 'N': + case 'L': + lpsz ++; + break; + } + } + switch (*lpsz | nModifier) { + case 'c': + case 'C': + nItemLen = 2; + va_arg(argList, int); + break; + case 'c'|FORCE_ANSI: + case 'C'|FORCE_ANSI: + nItemLen = 2; + va_arg(argList, int); + break; + case 'c'|FORCE_UNICODE: + case 'C'|FORCE_UNICODE: + nItemLen = 2; + va_arg(argList, int); + break; + case 's': { + FX_LPCWSTR pstrNextArg = va_arg(argList, FX_LPCWSTR); + if (pstrNextArg == NULL) { + nItemLen = 6; + } else { + nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } + } + break; + case 'S': { + FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR); + if (pstrNextArg == NULL) { + nItemLen = 6; + } else { + nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } + } + break; + case 's'|FORCE_ANSI: + case 'S'|FORCE_ANSI: { + FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR); + if (pstrNextArg == NULL) { + nItemLen = 6; + } else { + nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } + } + break; + case 's'|FORCE_UNICODE: + case 'S'|FORCE_UNICODE: { + FX_LPWSTR pstrNextArg = va_arg(argList, FX_LPWSTR); + if (pstrNextArg == NULL) { + nItemLen = 6; + } else { + nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } + } + break; + } + if (nItemLen != 0) { + if (nPrecision != 0 && nItemLen > nPrecision) { + nItemLen = nPrecision; + } + if (nItemLen < nWidth) { + nItemLen = nWidth; + } + } else { + switch (*lpsz) { + case 'd': + case 'i': + case 'u': + case 'x': + case 'X': + case 'o': + if (nModifier & FORCE_INT64) { + va_arg(argList, FX_INT64); + } else { + va_arg(argList, int); + } + nItemLen = 32; + if (nItemLen < nWidth + nPrecision) { + nItemLen = nWidth + nPrecision; + } + break; + case 'a': + case 'A': + case 'e': + case 'E': + case 'g': + case 'G': + va_arg(argList, double); + nItemLen = 128; + if (nItemLen < nWidth + nPrecision) { + nItemLen = nWidth + nPrecision; + } + break; + case 'f': + if (nWidth + nPrecision > 100) { + nItemLen = nPrecision + nWidth + 128; + } else { + double f; + char pszTemp[256]; + f = va_arg(argList, double); + FXSYS_snprintf(pszTemp, sizeof(pszTemp), "%*.*f", nWidth, nPrecision + 6, f ); + nItemLen = (FX_STRSIZE)FXSYS_strlen(pszTemp); + } + break; + case 'p': + va_arg(argList, void*); + nItemLen = 32; + if (nItemLen < nWidth + nPrecision) { + nItemLen = nWidth + nPrecision; + } + break; + case 'n': + va_arg(argList, int*); + break; + } + } + nMaxLen += nItemLen; + } + GetBuffer(nMaxLen); + if (m_pData) { + FXSYS_vswprintf((wchar_t*)m_pData->m_String, nMaxLen + 1, (const wchar_t*)lpszFormat, argListSave); + ReleaseBuffer(); + } + va_end(argListSave); +} +void CFX_WideString::Format(FX_LPCWSTR lpszFormat, ...) +{ + va_list argList; + va_start(argList, lpszFormat); + FormatV(lpszFormat, argList); + va_end(argList); +} +FX_FLOAT FX_wtof(FX_LPCWSTR str, int len) +{ + if (len == 0) { + return 0.0; + } + int cc = 0; + FX_BOOL bNegative = FALSE; + if (str[0] == '+') { + cc++; + } else if (str[0] == '-') { + bNegative = TRUE; + cc++; + } + int integer = 0; + while (cc < len) { + if (str[cc] == '.') { + break; + } + integer = integer * 10 + str[cc] - '0'; + cc ++; + } + FX_FLOAT fraction = 0; + if (str[cc] == '.') { + cc ++; + FX_FLOAT scale = 0.1f; + while (cc < len) { + fraction += scale * (str[cc] - '0'); + scale *= 0.1f; + cc ++; + } + } + fraction += (FX_FLOAT)integer; + return bNegative ? -fraction : fraction; +} +int CFX_WideString::GetInteger() const +{ + if (m_pData == NULL) { + return 0; + } + return FXSYS_wtoi(m_pData->m_String); +} +FX_FLOAT CFX_WideString::GetFloat() const +{ + if (m_pData == NULL) { + return 0.0; + } + return FX_wtof(m_pData->m_String, m_pData->m_nDataLength); +} +void CFX_WideStringL::Empty(IFX_Allocator* pAllocator) +{ + if (m_Ptr) { + FX_Allocator_Free(pAllocator, (FX_LPVOID)m_Ptr); + } + m_Ptr = NULL, m_Length = 0; +} +void CFX_WideStringL::Set(FX_WSTR src, IFX_Allocator* pAllocator) +{ + Empty(pAllocator); + if (src.GetPtr() != NULL && src.GetLength() > 0) { + FX_LPWSTR str = FX_Allocator_Alloc(pAllocator, FX_WCHAR, src.GetLength() + 1); + if (!str) { + return; + } + FXSYS_memcpy32(str, src.GetPtr(), src.GetLength()*sizeof(FX_WCHAR)); + str[src.GetLength()] = '\0'; + *(FX_LPWSTR*)(&m_Ptr) = str; + m_Length = src.GetLength(); + } +} +int CFX_WideStringL::GetInteger() const +{ + if (!m_Ptr) { + return 0; + } + return FXSYS_wtoi(m_Ptr); +} +FX_FLOAT CFX_WideStringL::GetFloat() const +{ + if (!m_Ptr) { + return 0.0f; + } + return FX_wtof(m_Ptr, m_Length); +} +void CFX_WideStringL::TrimRight(FX_LPCWSTR lpszTargets) +{ + if (!lpszTargets || *lpszTargets == 0 || !m_Ptr || m_Length < 1) { + return; + } + FX_STRSIZE pos = m_Length; + while (pos) { + if (FXSYS_wcschr(lpszTargets, m_Ptr[pos - 1]) == NULL) { + break; + } + pos --; + } + if (pos < m_Length) { + (*(FX_LPWSTR*)(&m_Ptr))[pos] = 0; + m_Length = pos; + } +} +static CFX_ByteString _DefMap_GetByteString(CFX_CharMap* pCharMap, const CFX_WideString& widestr) +{ + int src_len = widestr.GetLength(); + int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0; + int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, widestr, src_len, NULL, 0, NULL, NULL); + if (dest_len == 0) { + return CFX_ByteString(); + } + CFX_ByteString bytestr; + FX_LPSTR dest_buf = bytestr.GetBuffer(dest_len); + FXSYS_WideCharToMultiByte(codepage, 0, widestr, src_len, dest_buf, dest_len, NULL, NULL); + bytestr.ReleaseBuffer(dest_len); + return bytestr; +} +static CFX_WideString _DefMap_GetWideString(CFX_CharMap* pCharMap, const CFX_ByteString& bytestr) +{ + int src_len = bytestr.GetLength(); + int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0; + int dest_len = FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, NULL, 0); + if (dest_len == 0) { + return CFX_WideString(); + } + CFX_WideString widestr; + FX_LPWSTR dest_buf = widestr.GetBuffer(dest_len); + FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, dest_buf, dest_len); + widestr.ReleaseBuffer(dest_len); + return widestr; +} +static int _DefMap_GetGBKCodePage() +{ + return 936; +} +static int _DefMap_GetUHCCodePage() +{ + return 949; +} +static int _DefMap_GetJISCodePage() +{ + return 932; +} +static int _DefMap_GetBig5CodePage() +{ + return 950; +} +static const CFX_CharMap g_DefaultMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, NULL}; +static const CFX_CharMap g_DefaultGBKMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetGBKCodePage}; +static const CFX_CharMap g_DefaultJISMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetJISCodePage}; +static const CFX_CharMap g_DefaultUHCMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetUHCCodePage}; +static const CFX_CharMap g_DefaultBig5Mapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetBig5CodePage}; +CFX_CharMap* CFX_CharMap::GetDefaultMapper(FX_INT32 codepage) +{ + switch (codepage) { + case 0: + return (CFX_CharMap*)&g_DefaultMapper; + case 932: + return (CFX_CharMap*)&g_DefaultJISMapper; + case 936: + return (CFX_CharMap*)&g_DefaultGBKMapper; + case 949: + return (CFX_CharMap*)&g_DefaultUHCMapper; + case 950: + return (CFX_CharMap*)&g_DefaultBig5Mapper; + } + return NULL; +} diff --git a/core/src/fxcrt/fx_extension.cpp b/core/src/fxcrt/fx_extension.cpp index 79d300668f..e7272cff0b 100644 --- a/core/src/fxcrt/fx_extension.cpp +++ b/core/src/fxcrt/fx_extension.cpp @@ -1,401 +1,401 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_ext.h"
-#include "extension.h"
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
-#include <wincrypt.h>
-#else
-#include <ctime>
-#endif
-FX_HFILE FX_File_Open(FX_BSTR fileName, FX_DWORD dwMode, IFX_Allocator* pAllocator)
-{
- IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(pAllocator);
- if (pFA && !pFA->Open(fileName, dwMode)) {
- pFA->Release(pAllocator);
- return NULL;
- }
- return (FX_HFILE)pFA;
-}
-FX_HFILE FX_File_Open(FX_WSTR fileName, FX_DWORD dwMode, IFX_Allocator* pAllocator)
-{
- IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(pAllocator);
- if (pFA && !pFA->Open(fileName, dwMode)) {
- pFA->Release(pAllocator);
- return NULL;
- }
- return (FX_HFILE)pFA;
-}
-void FX_File_Close(FX_HFILE hFile, IFX_Allocator* pAllocator)
-{
- FXSYS_assert(hFile != NULL);
- ((IFXCRT_FileAccess*)hFile)->Close();
- ((IFXCRT_FileAccess*)hFile)->Release(pAllocator);
-}
-FX_FILESIZE FX_File_GetSize(FX_HFILE hFile)
-{
- FXSYS_assert(hFile != NULL);
- return ((IFXCRT_FileAccess*)hFile)->GetSize();
-}
-FX_FILESIZE FX_File_GetPosition(FX_HFILE hFile)
-{
- FXSYS_assert(hFile != NULL);
- return ((IFXCRT_FileAccess*)hFile)->GetPosition();
-}
-FX_FILESIZE FX_File_SetPosition(FX_HFILE hFile, FX_FILESIZE pos)
-{
- FXSYS_assert(hFile != NULL);
- return ((IFXCRT_FileAccess*)hFile)->SetPosition(pos);
-}
-size_t FX_File_Read(FX_HFILE hFile, void* pBuffer, size_t szBuffer)
-{
- FXSYS_assert(hFile != NULL);
- return ((IFXCRT_FileAccess*)hFile)->Read(pBuffer, szBuffer);
-}
-size_t FX_File_ReadPos(FX_HFILE hFile, void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
-{
- FXSYS_assert(hFile != NULL);
- return ((IFXCRT_FileAccess*)hFile)->ReadPos(pBuffer, szBuffer, pos);
-}
-size_t FX_File_Write(FX_HFILE hFile, const void* pBuffer, size_t szBuffer)
-{
- FXSYS_assert(hFile != NULL);
- return ((IFXCRT_FileAccess*)hFile)->Write(pBuffer, szBuffer);
-}
-size_t FX_File_WritePos(FX_HFILE hFile, const void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
-{
- FXSYS_assert(hFile != NULL);
- return ((IFXCRT_FileAccess*)hFile)->WritePos(pBuffer, szBuffer, pos);
-}
-FX_BOOL FX_File_Flush(FX_HFILE hFile)
-{
- FXSYS_assert(hFile != NULL);
- return ((IFXCRT_FileAccess*)hFile)->Flush();
-}
-FX_BOOL FX_File_Truncate(FX_HFILE hFile, FX_FILESIZE szFile)
-{
- FXSYS_assert(hFile != NULL);
- return ((IFXCRT_FileAccess*)hFile)->Truncate(szFile);
-}
-IFX_FileStream* FX_CreateFileStream(FX_LPCSTR filename, FX_DWORD dwModes, IFX_Allocator* pAllocator)
-{
- IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(pAllocator);
- if (!pFA) {
- return NULL;
- }
- if (!pFA->Open(filename, dwModes)) {
- pFA->Release(pAllocator);
- return NULL;
- }
- if (pAllocator) {
- return FX_NewAtAllocator(pAllocator) CFX_CRTFileStream(pFA, pAllocator);
- } else {
- return FX_NEW CFX_CRTFileStream(pFA, pAllocator);
- }
-}
-IFX_FileStream* FX_CreateFileStream(FX_LPCWSTR filename, FX_DWORD dwModes, IFX_Allocator* pAllocator)
-{
- IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(pAllocator);
- if (!pFA) {
- return NULL;
- }
- if (!pFA->Open(filename, dwModes)) {
- pFA->Release(pAllocator);
- return NULL;
- }
- if (pAllocator) {
- return FX_NewAtAllocator(pAllocator) CFX_CRTFileStream(pFA, pAllocator);
- } else {
- return FX_NEW CFX_CRTFileStream(pFA, pAllocator);
- }
-}
-IFX_FileWrite* FX_CreateFileWrite(FX_LPCSTR filename, IFX_Allocator* pAllocator)
-{
- return FX_CreateFileStream(filename, FX_FILEMODE_Truncate, pAllocator);
-}
-IFX_FileWrite* FX_CreateFileWrite(FX_LPCWSTR filename, IFX_Allocator* pAllocator)
-{
- return FX_CreateFileStream(filename, FX_FILEMODE_Truncate, pAllocator);
-}
-IFX_FileRead* FX_CreateFileRead(FX_LPCSTR filename, IFX_Allocator* pAllocator)
-{
- return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly, pAllocator);
-}
-IFX_FileRead* FX_CreateFileRead(FX_LPCWSTR filename, IFX_Allocator* pAllocator)
-{
- return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly, pAllocator);
-}
-IFX_MemoryStream* FX_CreateMemoryStream(FX_LPBYTE pBuffer, size_t dwSize, FX_BOOL bTakeOver, IFX_Allocator* pAllocator)
-{
- if (pAllocator) {
- return FX_NewAtAllocator(pAllocator)CFX_MemoryStream(pBuffer, dwSize, bTakeOver, pAllocator);
- } else {
- return FX_NEW CFX_MemoryStream(pBuffer, dwSize, bTakeOver, NULL);
- }
-}
-IFX_MemoryStream* FX_CreateMemoryStream(FX_BOOL bConsecutive, IFX_Allocator* pAllocator)
-{
- if (pAllocator) {
- return FX_NewAtAllocator(pAllocator)CFX_MemoryStream(bConsecutive, pAllocator);
- } else {
- return FX_NEW CFX_MemoryStream(bConsecutive, NULL);
- }
-}
-#ifdef __cplusplus
-extern "C" {
-#endif
-FX_FLOAT FXSYS_tan(FX_FLOAT a)
-{
- return (FX_FLOAT)tan(a);
-}
-FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x)
-{
- return FXSYS_log(x) / FXSYS_log(b);
-}
-FX_FLOAT FXSYS_strtof(FX_LPCSTR pcsStr, FX_INT32 iLength, FX_INT32 *pUsedLen)
-{
- FXSYS_assert(pcsStr != NULL);
- if (iLength < 0) {
- iLength = (FX_INT32)FXSYS_strlen(pcsStr);
- }
- CFX_WideString ws = CFX_WideString::FromLocal(pcsStr, iLength);
- return FXSYS_wcstof((FX_LPCWSTR)ws, iLength, pUsedLen);
-}
-FX_FLOAT FXSYS_wcstof(FX_LPCWSTR pwsStr, FX_INT32 iLength, FX_INT32 *pUsedLen)
-{
- FXSYS_assert(pwsStr != NULL);
- if (iLength < 0) {
- iLength = (FX_INT32)FXSYS_wcslen(pwsStr);
- }
- if (iLength == 0) {
- return 0.0f;
- }
- FX_INT32 iUsedLen = 0;
- FX_BOOL bNegtive = FALSE;
- switch (pwsStr[iUsedLen]) {
- case '-':
- bNegtive = TRUE;
- case '+':
- iUsedLen++;
- break;
- }
- FX_FLOAT fValue = 0.0f;
- while (iUsedLen < iLength) {
- FX_WCHAR wch = pwsStr[iUsedLen];
- if (wch >= L'0' && wch <= L'9') {
- fValue = fValue * 10.0f + (wch - L'0');
- } else {
- break;
- }
- iUsedLen++;
- }
- if (iUsedLen < iLength && pwsStr[iUsedLen] == L'.') {
- FX_FLOAT fPrecise = 0.1f;
- while (++iUsedLen < iLength) {
- FX_WCHAR wch = pwsStr[iUsedLen];
- if (wch >= L'0' && wch <= L'9') {
- fValue += (wch - L'0') * fPrecise;
- fPrecise *= 0.1f;
- } else {
- break;
- }
- }
- }
- if (pUsedLen) {
- *pUsedLen = iUsedLen;
- }
- return bNegtive ? -fValue : fValue;
-}
-FX_LPWSTR FXSYS_wcsncpy(FX_LPWSTR dstStr, FX_LPCWSTR srcStr, size_t count)
-{
- FXSYS_assert(dstStr != NULL && srcStr != NULL && count > 0);
- for (size_t i = 0; i < count; ++i)
- if ((dstStr[i] = srcStr[i]) == L'\0') {
- break;
- }
- return dstStr;
-}
-FX_INT32 FXSYS_wcsnicmp(FX_LPCWSTR s1, FX_LPCWSTR s2, size_t count)
-{
- FXSYS_assert(s1 != NULL && s2 != NULL && count > 0);
- FX_WCHAR wch1 = 0, wch2 = 0;
- while (count-- > 0) {
- wch1 = (FX_WCHAR)FXSYS_tolower(*s1++);
- wch2 = (FX_WCHAR)FXSYS_tolower(*s2++);
- if (wch1 != wch2) {
- break;
- }
- }
- return wch1 - wch2;
-}
-FX_INT32 FXSYS_strnicmp(FX_LPCSTR s1, FX_LPCSTR s2, size_t count)
-{
- FXSYS_assert(s1 != NULL && s2 != NULL && count > 0);
- FX_CHAR ch1 = 0, ch2 = 0;
- while (count-- > 0) {
- ch1 = (FX_CHAR)FXSYS_tolower(*s1++);
- ch2 = (FX_CHAR)FXSYS_tolower(*s2++);
- if (ch1 != ch2) {
- break;
- }
- }
- return ch1 - ch2;
-}
-FX_DWORD FX_HashCode_String_GetA(FX_LPCSTR pStr, FX_INT32 iLength, FX_BOOL bIgnoreCase)
-{
- FXSYS_assert(pStr != NULL);
- if (iLength < 0) {
- iLength = (FX_INT32)FXSYS_strlen(pStr);
- }
- FX_LPCSTR pStrEnd = pStr + iLength;
- FX_DWORD dwHashCode = 0;
- if (bIgnoreCase) {
- while (pStr < pStrEnd) {
- dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++);
- }
- } else {
- while (pStr < pStrEnd) {
- dwHashCode = 31 * dwHashCode + *pStr ++;
- }
- }
- return dwHashCode;
-}
-FX_DWORD FX_HashCode_String_GetW(FX_LPCWSTR pStr, FX_INT32 iLength, FX_BOOL bIgnoreCase)
-{
- FXSYS_assert(pStr != NULL);
- if (iLength < 0) {
- iLength = (FX_INT32)FXSYS_wcslen(pStr);
- }
- FX_LPCWSTR pStrEnd = pStr + iLength;
- FX_DWORD dwHashCode = 0;
- if (bIgnoreCase) {
- while (pStr < pStrEnd) {
- dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++);
- }
- } else {
- while (pStr < pStrEnd) {
- dwHashCode = 1313 * dwHashCode + *pStr ++;
- }
- }
- return dwHashCode;
-}
-#ifdef __cplusplus
-}
-#endif
-#ifdef __cplusplus
-extern "C" {
-#endif
-FX_LPVOID FX_Random_MT_Start(FX_DWORD dwSeed)
-{
- FX_LPMTRANDOMCONTEXT pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1);
- if (!pContext) {
- return NULL;
- }
- pContext->mt[0] = dwSeed;
- FX_DWORD &i = pContext->mti;
- FX_LPDWORD pBuf = pContext->mt;
- for (i = 1; i < MT_N; i ++) {
- pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i);
- }
- pContext->bHaveSeed = TRUE;
- return pContext;
-}
-FX_DWORD FX_Random_MT_Generate(FX_LPVOID pContext)
-{
- FXSYS_assert(pContext != NULL);
- FX_LPMTRANDOMCONTEXT pMTC = (FX_LPMTRANDOMCONTEXT)pContext;
- FX_DWORD v;
- static FX_DWORD mag[2] = {0, MT_Matrix_A};
- FX_DWORD &mti = pMTC->mti;
- FX_LPDWORD pBuf = pMTC->mt;
- if ((int)mti < 0 || mti >= MT_N) {
- if (mti > MT_N && !pMTC->bHaveSeed) {
- return 0;
- }
- FX_DWORD kk;
- for (kk = 0; kk < MT_N - MT_M; kk ++) {
- v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask);
- pBuf[kk] = pBuf[kk + MT_M] ^ (v >> 1) ^ mag[v & 1];
- }
- for (; kk < MT_N - 1; kk ++) {
- v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask);
- pBuf[kk] = pBuf[kk + (MT_M - MT_N)] ^ (v >> 1) ^ mag[v & 1];
- }
- v = (pBuf[MT_N - 1] & MT_Upper_Mask) | (pBuf[0] & MT_Lower_Mask);
- pBuf[MT_N - 1] = pBuf[MT_M - 1] ^ (v >> 1) ^ mag[v & 1];
- mti = 0;
- }
- v = pBuf[mti ++];
- v ^= (v >> 11);
- v ^= (v << 7) & 0x9d2c5680UL;
- v ^= (v << 15) & 0xefc60000UL;
- v ^= (v >> 18);
- return v;
-}
-void FX_Random_MT_Close(FX_LPVOID pContext)
-{
- FXSYS_assert(pContext != NULL);
- FX_Free(pContext);
-}
-void FX_Random_GenerateMT(FX_LPDWORD pBuffer, FX_INT32 iCount)
-{
- FX_DWORD dwSeed;
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
- if (!FX_GenerateCryptoRandom(&dwSeed, 1)) {
- FX_Random_GenerateBase(&dwSeed, 1);
- }
-#else
- FX_Random_GenerateBase(&dwSeed, 1);
-#endif
- FX_LPVOID pContext = FX_Random_MT_Start(dwSeed);
- while (iCount -- > 0) {
- *pBuffer ++ = FX_Random_MT_Generate(pContext);
- }
- FX_Random_MT_Close(pContext);
-}
-void FX_Random_GenerateBase(FX_LPDWORD pBuffer, FX_INT32 iCount)
-{
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
- SYSTEMTIME st1, st2;
- ::GetSystemTime(&st1);
- do {
- ::GetSystemTime(&st2);
- } while (FXSYS_memcmp32(&st1, &st2, sizeof(SYSTEMTIME)) == 0);
- FX_DWORD dwHash1 = FX_HashCode_String_GetA((FX_LPCSTR)&st1, sizeof(st1), TRUE);
- FX_DWORD dwHash2 = FX_HashCode_String_GetA((FX_LPCSTR)&st2, sizeof(st2), TRUE);
- ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2);
-#else
- time_t tmLast = time(NULL), tmCur;
- while ((tmCur = time(NULL)) == tmLast);
- ::srand((tmCur << 16) | (tmLast & 0xFFFF));
-#endif
- while (iCount -- > 0) {
- *pBuffer ++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF));
- }
-}
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
-FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount)
-{
- HCRYPTPROV hCP = NULL;
- if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || hCP == NULL) {
- return FALSE;
- }
- ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (FX_LPBYTE)pBuffer);
- ::CryptReleaseContext(hCP, 0);
- return TRUE;
-}
-#endif
-void FX_Random_GenerateCrypto(FX_LPDWORD pBuffer, FX_INT32 iCount)
-{
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
- FX_GenerateCryptoRandom(pBuffer, iCount);
-#else
- FX_Random_GenerateBase(pBuffer, iCount);
-#endif
-}
-#ifdef __cplusplus
-}
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_ext.h" +#include "extension.h" +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +#include <wincrypt.h> +#else +#include <ctime> +#endif +FX_HFILE FX_File_Open(FX_BSTR fileName, FX_DWORD dwMode, IFX_Allocator* pAllocator) +{ + IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(pAllocator); + if (pFA && !pFA->Open(fileName, dwMode)) { + pFA->Release(pAllocator); + return NULL; + } + return (FX_HFILE)pFA; +} +FX_HFILE FX_File_Open(FX_WSTR fileName, FX_DWORD dwMode, IFX_Allocator* pAllocator) +{ + IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(pAllocator); + if (pFA && !pFA->Open(fileName, dwMode)) { + pFA->Release(pAllocator); + return NULL; + } + return (FX_HFILE)pFA; +} +void FX_File_Close(FX_HFILE hFile, IFX_Allocator* pAllocator) +{ + FXSYS_assert(hFile != NULL); + ((IFXCRT_FileAccess*)hFile)->Close(); + ((IFXCRT_FileAccess*)hFile)->Release(pAllocator); +} +FX_FILESIZE FX_File_GetSize(FX_HFILE hFile) +{ + FXSYS_assert(hFile != NULL); + return ((IFXCRT_FileAccess*)hFile)->GetSize(); +} +FX_FILESIZE FX_File_GetPosition(FX_HFILE hFile) +{ + FXSYS_assert(hFile != NULL); + return ((IFXCRT_FileAccess*)hFile)->GetPosition(); +} +FX_FILESIZE FX_File_SetPosition(FX_HFILE hFile, FX_FILESIZE pos) +{ + FXSYS_assert(hFile != NULL); + return ((IFXCRT_FileAccess*)hFile)->SetPosition(pos); +} +size_t FX_File_Read(FX_HFILE hFile, void* pBuffer, size_t szBuffer) +{ + FXSYS_assert(hFile != NULL); + return ((IFXCRT_FileAccess*)hFile)->Read(pBuffer, szBuffer); +} +size_t FX_File_ReadPos(FX_HFILE hFile, void* pBuffer, size_t szBuffer, FX_FILESIZE pos) +{ + FXSYS_assert(hFile != NULL); + return ((IFXCRT_FileAccess*)hFile)->ReadPos(pBuffer, szBuffer, pos); +} +size_t FX_File_Write(FX_HFILE hFile, const void* pBuffer, size_t szBuffer) +{ + FXSYS_assert(hFile != NULL); + return ((IFXCRT_FileAccess*)hFile)->Write(pBuffer, szBuffer); +} +size_t FX_File_WritePos(FX_HFILE hFile, const void* pBuffer, size_t szBuffer, FX_FILESIZE pos) +{ + FXSYS_assert(hFile != NULL); + return ((IFXCRT_FileAccess*)hFile)->WritePos(pBuffer, szBuffer, pos); +} +FX_BOOL FX_File_Flush(FX_HFILE hFile) +{ + FXSYS_assert(hFile != NULL); + return ((IFXCRT_FileAccess*)hFile)->Flush(); +} +FX_BOOL FX_File_Truncate(FX_HFILE hFile, FX_FILESIZE szFile) +{ + FXSYS_assert(hFile != NULL); + return ((IFXCRT_FileAccess*)hFile)->Truncate(szFile); +} +IFX_FileStream* FX_CreateFileStream(FX_LPCSTR filename, FX_DWORD dwModes, IFX_Allocator* pAllocator) +{ + IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(pAllocator); + if (!pFA) { + return NULL; + } + if (!pFA->Open(filename, dwModes)) { + pFA->Release(pAllocator); + return NULL; + } + if (pAllocator) { + return FX_NewAtAllocator(pAllocator) CFX_CRTFileStream(pFA, pAllocator); + } else { + return FX_NEW CFX_CRTFileStream(pFA, pAllocator); + } +} +IFX_FileStream* FX_CreateFileStream(FX_LPCWSTR filename, FX_DWORD dwModes, IFX_Allocator* pAllocator) +{ + IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(pAllocator); + if (!pFA) { + return NULL; + } + if (!pFA->Open(filename, dwModes)) { + pFA->Release(pAllocator); + return NULL; + } + if (pAllocator) { + return FX_NewAtAllocator(pAllocator) CFX_CRTFileStream(pFA, pAllocator); + } else { + return FX_NEW CFX_CRTFileStream(pFA, pAllocator); + } +} +IFX_FileWrite* FX_CreateFileWrite(FX_LPCSTR filename, IFX_Allocator* pAllocator) +{ + return FX_CreateFileStream(filename, FX_FILEMODE_Truncate, pAllocator); +} +IFX_FileWrite* FX_CreateFileWrite(FX_LPCWSTR filename, IFX_Allocator* pAllocator) +{ + return FX_CreateFileStream(filename, FX_FILEMODE_Truncate, pAllocator); +} +IFX_FileRead* FX_CreateFileRead(FX_LPCSTR filename, IFX_Allocator* pAllocator) +{ + return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly, pAllocator); +} +IFX_FileRead* FX_CreateFileRead(FX_LPCWSTR filename, IFX_Allocator* pAllocator) +{ + return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly, pAllocator); +} +IFX_MemoryStream* FX_CreateMemoryStream(FX_LPBYTE pBuffer, size_t dwSize, FX_BOOL bTakeOver, IFX_Allocator* pAllocator) +{ + if (pAllocator) { + return FX_NewAtAllocator(pAllocator)CFX_MemoryStream(pBuffer, dwSize, bTakeOver, pAllocator); + } else { + return FX_NEW CFX_MemoryStream(pBuffer, dwSize, bTakeOver, NULL); + } +} +IFX_MemoryStream* FX_CreateMemoryStream(FX_BOOL bConsecutive, IFX_Allocator* pAllocator) +{ + if (pAllocator) { + return FX_NewAtAllocator(pAllocator)CFX_MemoryStream(bConsecutive, pAllocator); + } else { + return FX_NEW CFX_MemoryStream(bConsecutive, NULL); + } +} +#ifdef __cplusplus +extern "C" { +#endif +FX_FLOAT FXSYS_tan(FX_FLOAT a) +{ + return (FX_FLOAT)tan(a); +} +FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) +{ + return FXSYS_log(x) / FXSYS_log(b); +} +FX_FLOAT FXSYS_strtof(FX_LPCSTR pcsStr, FX_INT32 iLength, FX_INT32 *pUsedLen) +{ + FXSYS_assert(pcsStr != NULL); + if (iLength < 0) { + iLength = (FX_INT32)FXSYS_strlen(pcsStr); + } + CFX_WideString ws = CFX_WideString::FromLocal(pcsStr, iLength); + return FXSYS_wcstof((FX_LPCWSTR)ws, iLength, pUsedLen); +} +FX_FLOAT FXSYS_wcstof(FX_LPCWSTR pwsStr, FX_INT32 iLength, FX_INT32 *pUsedLen) +{ + FXSYS_assert(pwsStr != NULL); + if (iLength < 0) { + iLength = (FX_INT32)FXSYS_wcslen(pwsStr); + } + if (iLength == 0) { + return 0.0f; + } + FX_INT32 iUsedLen = 0; + FX_BOOL bNegtive = FALSE; + switch (pwsStr[iUsedLen]) { + case '-': + bNegtive = TRUE; + case '+': + iUsedLen++; + break; + } + FX_FLOAT fValue = 0.0f; + while (iUsedLen < iLength) { + FX_WCHAR wch = pwsStr[iUsedLen]; + if (wch >= L'0' && wch <= L'9') { + fValue = fValue * 10.0f + (wch - L'0'); + } else { + break; + } + iUsedLen++; + } + if (iUsedLen < iLength && pwsStr[iUsedLen] == L'.') { + FX_FLOAT fPrecise = 0.1f; + while (++iUsedLen < iLength) { + FX_WCHAR wch = pwsStr[iUsedLen]; + if (wch >= L'0' && wch <= L'9') { + fValue += (wch - L'0') * fPrecise; + fPrecise *= 0.1f; + } else { + break; + } + } + } + if (pUsedLen) { + *pUsedLen = iUsedLen; + } + return bNegtive ? -fValue : fValue; +} +FX_LPWSTR FXSYS_wcsncpy(FX_LPWSTR dstStr, FX_LPCWSTR srcStr, size_t count) +{ + FXSYS_assert(dstStr != NULL && srcStr != NULL && count > 0); + for (size_t i = 0; i < count; ++i) + if ((dstStr[i] = srcStr[i]) == L'\0') { + break; + } + return dstStr; +} +FX_INT32 FXSYS_wcsnicmp(FX_LPCWSTR s1, FX_LPCWSTR s2, size_t count) +{ + FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); + FX_WCHAR wch1 = 0, wch2 = 0; + while (count-- > 0) { + wch1 = (FX_WCHAR)FXSYS_tolower(*s1++); + wch2 = (FX_WCHAR)FXSYS_tolower(*s2++); + if (wch1 != wch2) { + break; + } + } + return wch1 - wch2; +} +FX_INT32 FXSYS_strnicmp(FX_LPCSTR s1, FX_LPCSTR s2, size_t count) +{ + FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); + FX_CHAR ch1 = 0, ch2 = 0; + while (count-- > 0) { + ch1 = (FX_CHAR)FXSYS_tolower(*s1++); + ch2 = (FX_CHAR)FXSYS_tolower(*s2++); + if (ch1 != ch2) { + break; + } + } + return ch1 - ch2; +} +FX_DWORD FX_HashCode_String_GetA(FX_LPCSTR pStr, FX_INT32 iLength, FX_BOOL bIgnoreCase) +{ + FXSYS_assert(pStr != NULL); + if (iLength < 0) { + iLength = (FX_INT32)FXSYS_strlen(pStr); + } + FX_LPCSTR pStrEnd = pStr + iLength; + FX_DWORD dwHashCode = 0; + if (bIgnoreCase) { + while (pStr < pStrEnd) { + dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); + } + } else { + while (pStr < pStrEnd) { + dwHashCode = 31 * dwHashCode + *pStr ++; + } + } + return dwHashCode; +} +FX_DWORD FX_HashCode_String_GetW(FX_LPCWSTR pStr, FX_INT32 iLength, FX_BOOL bIgnoreCase) +{ + FXSYS_assert(pStr != NULL); + if (iLength < 0) { + iLength = (FX_INT32)FXSYS_wcslen(pStr); + } + FX_LPCWSTR pStrEnd = pStr + iLength; + FX_DWORD dwHashCode = 0; + if (bIgnoreCase) { + while (pStr < pStrEnd) { + dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); + } + } else { + while (pStr < pStrEnd) { + dwHashCode = 1313 * dwHashCode + *pStr ++; + } + } + return dwHashCode; +} +#ifdef __cplusplus +} +#endif +#ifdef __cplusplus +extern "C" { +#endif +FX_LPVOID FX_Random_MT_Start(FX_DWORD dwSeed) +{ + FX_LPMTRANDOMCONTEXT pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1); + if (!pContext) { + return NULL; + } + pContext->mt[0] = dwSeed; + FX_DWORD &i = pContext->mti; + FX_LPDWORD pBuf = pContext->mt; + for (i = 1; i < MT_N; i ++) { + pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); + } + pContext->bHaveSeed = TRUE; + return pContext; +} +FX_DWORD FX_Random_MT_Generate(FX_LPVOID pContext) +{ + FXSYS_assert(pContext != NULL); + FX_LPMTRANDOMCONTEXT pMTC = (FX_LPMTRANDOMCONTEXT)pContext; + FX_DWORD v; + static FX_DWORD mag[2] = {0, MT_Matrix_A}; + FX_DWORD &mti = pMTC->mti; + FX_LPDWORD pBuf = pMTC->mt; + if ((int)mti < 0 || mti >= MT_N) { + if (mti > MT_N && !pMTC->bHaveSeed) { + return 0; + } + FX_DWORD kk; + for (kk = 0; kk < MT_N - MT_M; kk ++) { + v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); + pBuf[kk] = pBuf[kk + MT_M] ^ (v >> 1) ^ mag[v & 1]; + } + for (; kk < MT_N - 1; kk ++) { + v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); + pBuf[kk] = pBuf[kk + (MT_M - MT_N)] ^ (v >> 1) ^ mag[v & 1]; + } + v = (pBuf[MT_N - 1] & MT_Upper_Mask) | (pBuf[0] & MT_Lower_Mask); + pBuf[MT_N - 1] = pBuf[MT_M - 1] ^ (v >> 1) ^ mag[v & 1]; + mti = 0; + } + v = pBuf[mti ++]; + v ^= (v >> 11); + v ^= (v << 7) & 0x9d2c5680UL; + v ^= (v << 15) & 0xefc60000UL; + v ^= (v >> 18); + return v; +} +void FX_Random_MT_Close(FX_LPVOID pContext) +{ + FXSYS_assert(pContext != NULL); + FX_Free(pContext); +} +void FX_Random_GenerateMT(FX_LPDWORD pBuffer, FX_INT32 iCount) +{ + FX_DWORD dwSeed; +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + if (!FX_GenerateCryptoRandom(&dwSeed, 1)) { + FX_Random_GenerateBase(&dwSeed, 1); + } +#else + FX_Random_GenerateBase(&dwSeed, 1); +#endif + FX_LPVOID pContext = FX_Random_MT_Start(dwSeed); + while (iCount -- > 0) { + *pBuffer ++ = FX_Random_MT_Generate(pContext); + } + FX_Random_MT_Close(pContext); +} +void FX_Random_GenerateBase(FX_LPDWORD pBuffer, FX_INT32 iCount) +{ +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + SYSTEMTIME st1, st2; + ::GetSystemTime(&st1); + do { + ::GetSystemTime(&st2); + } while (FXSYS_memcmp32(&st1, &st2, sizeof(SYSTEMTIME)) == 0); + FX_DWORD dwHash1 = FX_HashCode_String_GetA((FX_LPCSTR)&st1, sizeof(st1), TRUE); + FX_DWORD dwHash2 = FX_HashCode_String_GetA((FX_LPCSTR)&st2, sizeof(st2), TRUE); + ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); +#else + time_t tmLast = time(NULL), tmCur; + while ((tmCur = time(NULL)) == tmLast); + ::srand((tmCur << 16) | (tmLast & 0xFFFF)); +#endif + while (iCount -- > 0) { + *pBuffer ++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); + } +} +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount) +{ + HCRYPTPROV hCP = NULL; + if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || hCP == NULL) { + return FALSE; + } + ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (FX_LPBYTE)pBuffer); + ::CryptReleaseContext(hCP, 0); + return TRUE; +} +#endif +void FX_Random_GenerateCrypto(FX_LPDWORD pBuffer, FX_INT32 iCount) +{ +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + FX_GenerateCryptoRandom(pBuffer, iCount); +#else + FX_Random_GenerateBase(pBuffer, iCount); +#endif +} +#ifdef __cplusplus +} +#endif diff --git a/core/src/fxcrt/fx_xml_composer.cpp b/core/src/fxcrt/fx_xml_composer.cpp index d68dd1ff27..1fce9009db 100644 --- a/core/src/fxcrt/fx_xml_composer.cpp +++ b/core/src/fxcrt/fx_xml_composer.cpp @@ -1,42 +1,42 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_xml.h"
-#include "xml_int.h"
-void FX_XML_SplitQualifiedName(FX_BSTR bsFullName, CFX_ByteStringC &bsSpace, CFX_ByteStringC &bsName)
-{
- if (bsFullName.IsEmpty()) {
- return;
- }
- FX_INT32 iStart = 0;
- for (; iStart < bsFullName.GetLength(); iStart ++) {
- if (bsFullName.GetAt(iStart) == ':') {
- break;
- }
- }
- if (iStart >= bsFullName.GetLength()) {
- bsName = bsFullName;
- } else {
- bsSpace = CFX_ByteStringC(bsFullName.GetCStr(), iStart);
- iStart ++;
- bsName = CFX_ByteStringC(bsFullName.GetCStr() + iStart, bsFullName.GetLength() - iStart);
- }
-}
-void CXML_Element::SetTag(FX_BSTR qSpace, FX_BSTR tagname)
-{
- IFX_Allocator* pAllocator = m_Children.m_pAllocator;
- m_QSpaceName.Set(qSpace, pAllocator);
- m_TagName.Set(tagname, pAllocator);
-}
-void CXML_Element::SetTag(FX_BSTR qTagName)
-{
- ASSERT(!qTagName.IsEmpty());
- IFX_Allocator* pAllocator = m_Children.m_pAllocator;
- CFX_ByteStringC bsSpace, bsName;
- FX_XML_SplitQualifiedName(qTagName, bsSpace, bsName);
- m_QSpaceName.Set(bsSpace, pAllocator);
- m_TagName.Set(bsName, pAllocator);
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_xml.h" +#include "xml_int.h" +void FX_XML_SplitQualifiedName(FX_BSTR bsFullName, CFX_ByteStringC &bsSpace, CFX_ByteStringC &bsName) +{ + if (bsFullName.IsEmpty()) { + return; + } + FX_INT32 iStart = 0; + for (; iStart < bsFullName.GetLength(); iStart ++) { + if (bsFullName.GetAt(iStart) == ':') { + break; + } + } + if (iStart >= bsFullName.GetLength()) { + bsName = bsFullName; + } else { + bsSpace = CFX_ByteStringC(bsFullName.GetCStr(), iStart); + iStart ++; + bsName = CFX_ByteStringC(bsFullName.GetCStr() + iStart, bsFullName.GetLength() - iStart); + } +} +void CXML_Element::SetTag(FX_BSTR qSpace, FX_BSTR tagname) +{ + IFX_Allocator* pAllocator = m_Children.m_pAllocator; + m_QSpaceName.Set(qSpace, pAllocator); + m_TagName.Set(tagname, pAllocator); +} +void CXML_Element::SetTag(FX_BSTR qTagName) +{ + ASSERT(!qTagName.IsEmpty()); + IFX_Allocator* pAllocator = m_Children.m_pAllocator; + CFX_ByteStringC bsSpace, bsName; + FX_XML_SplitQualifiedName(qTagName, bsSpace, bsName); + m_QSpaceName.Set(bsSpace, pAllocator); + m_TagName.Set(bsName, pAllocator); +} diff --git a/core/src/fxcrt/fx_xml_parser.cpp b/core/src/fxcrt/fx_xml_parser.cpp index 85e9544342..c3d4b9c506 100644 --- a/core/src/fxcrt/fx_xml_parser.cpp +++ b/core/src/fxcrt/fx_xml_parser.cpp @@ -1,1017 +1,1017 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_xml.h"
-#include "xml_int.h"
-CXML_Parser::~CXML_Parser()
-{
- if (m_bOwnedStream) {
- m_pDataAcc->Release();
- }
-}
-FX_BOOL CXML_Parser::Init(FX_LPBYTE pBuffer, size_t size)
-{
- if (m_pAllocator) {
- m_pDataAcc = FX_NewAtAllocator(m_pAllocator)CXML_DataBufAcc(pBuffer, size, m_pAllocator);
- } else {
- m_pDataAcc = FX_NEW CXML_DataBufAcc(pBuffer, size, NULL);
- }
- if (!m_pDataAcc) {
- return FALSE;
- }
- return Init(TRUE);
-}
-FX_BOOL CXML_Parser::Init(IFX_FileRead *pFileRead)
-{
- if (m_pAllocator) {
- m_pDataAcc = FX_NewAtAllocator(m_pAllocator)CXML_DataStmAcc(pFileRead, m_pAllocator);
- } else {
- m_pDataAcc = FX_NEW CXML_DataStmAcc(pFileRead, NULL);
- }
- if (!m_pDataAcc) {
- return FALSE;
- }
- return Init(TRUE);
-}
-FX_BOOL CXML_Parser::Init(IFX_BufferRead *pBuffer)
-{
- if (!pBuffer) {
- return FALSE;
- }
- m_pDataAcc = pBuffer;
- return Init(FALSE);
-}
-FX_BOOL CXML_Parser::Init(FX_BOOL bOwndedStream)
-{
- m_bOwnedStream = bOwndedStream;
- m_nOffset = 0;
- return ReadNextBlock();
-}
-FX_BOOL CXML_Parser::ReadNextBlock()
-{
- if (!m_pDataAcc->ReadNextBlock()) {
- return FALSE;
- }
- m_pBuffer = m_pDataAcc->GetBlockBuffer();
- m_dwBufferSize = m_pDataAcc->GetBlockSize();
- m_nBufferOffset = m_pDataAcc->GetBlockOffset();
- m_dwIndex = 0;
- return m_dwBufferSize > 0;
-}
-FX_BOOL CXML_Parser::IsEOF()
-{
- if (!m_pDataAcc->IsEOF()) {
- return FALSE;
- }
- return m_dwIndex >= m_dwBufferSize;
-}
-#define FXCRTM_XML_CHARTYPE_Normal 0x00
-#define FXCRTM_XML_CHARTYPE_SpaceChar 0x01
-#define FXCRTM_XML_CHARTYPE_Letter 0x02
-#define FXCRTM_XML_CHARTYPE_Digital 0x04
-#define FXCRTM_XML_CHARTYPE_NameIntro 0x08
-#define FXCRTM_XML_CHARTYPE_NameChar 0x10
-#define FXCRTM_XML_CHARTYPE_HexDigital 0x20
-#define FXCRTM_XML_CHARTYPE_HexLowerLetter 0x40
-#define FXCRTM_XML_CHARTYPE_HexUpperLetter 0x60
-#define FXCRTM_XML_CHARTYPE_HexChar 0x60
-FX_BYTE g_FXCRT_XML_ByteTypes[256] = {
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,
- 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x7A, 0x7A, 0x7A, 0x7A, 0x7A, 0x7A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x18,
- 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A,
- 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x01, 0x01,
-};
-FX_BOOL g_FXCRT_XML_IsWhiteSpace(FX_BYTE ch)
-{
- return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_SpaceChar) != 0;
-}
-FX_BOOL g_FXCRT_XML_IsLetter(FX_BYTE ch)
-{
- return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_Letter) != 0;
-}
-FX_BOOL g_FXCRT_XML_IsDigital(FX_BYTE ch)
-{
- return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_Digital) != 0;
-}
-FX_BOOL g_FXCRT_XML_IsNameIntro(FX_BYTE ch)
-{
- return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_NameIntro) != 0;
-}
-FX_BOOL g_FXCRT_XML_IsNameChar(FX_BYTE ch)
-{
- return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_NameChar) != 0;
-}
-FX_BOOL g_FXCRT_XML_IsHexChar(FX_BYTE ch)
-{
- return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_HexChar) != 0;
-}
-void CXML_Parser::SkipWhiteSpaces()
-{
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (IsEOF()) {
- return;
- }
- do {
- while (m_dwIndex < m_dwBufferSize && g_FXCRT_XML_IsWhiteSpace(m_pBuffer[m_dwIndex])) {
- m_dwIndex ++;
- }
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (m_dwIndex < m_dwBufferSize || IsEOF()) {
- break;
- }
- } while (ReadNextBlock());
-}
-void CXML_Parser::GetName(CFX_ByteStringL &space, CFX_ByteStringL &name)
-{
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (IsEOF()) {
- return;
- }
- CFX_ByteTextBuf buf(m_pAllocator);
- FX_BYTE ch;
- do {
- while (m_dwIndex < m_dwBufferSize) {
- ch = m_pBuffer[m_dwIndex];
- if (ch == ':') {
- buf.GetByteStringL(space);
- buf.Clear();
- } else if (g_FXCRT_XML_IsNameChar(ch)) {
- buf.AppendChar(ch);
- } else {
- break;
- }
- m_dwIndex ++;
- }
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (m_dwIndex < m_dwBufferSize || IsEOF()) {
- break;
- }
- } while (ReadNextBlock());
- buf.GetByteStringL(name);
-}
-void CXML_Parser::SkipLiterals(FX_BSTR str)
-{
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (IsEOF()) {
- return;
- }
- FX_INT32 i = 0, iLen = str.GetLength();
- do {
- while (m_dwIndex < m_dwBufferSize) {
- if (str.GetAt(i) != m_pBuffer[m_dwIndex ++]) {
- i = 0;
- } else {
- i ++;
- if (i == iLen) {
- break;
- }
- }
- }
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (i == iLen) {
- return;
- }
- if (m_dwIndex < m_dwBufferSize || IsEOF()) {
- break;
- }
- } while (ReadNextBlock());
- while (!m_pDataAcc->IsEOF()) {
- ReadNextBlock();
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwBufferSize;
- }
- m_dwIndex = m_dwBufferSize;
-}
-FX_DWORD CXML_Parser::GetCharRef()
-{
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (IsEOF()) {
- return 0;
- }
- FX_BYTE ch;
- FX_INT32 iState = 0;
- CFX_ByteTextBuf buf(m_pAllocator);
- FX_DWORD code = 0;
- do {
- while (m_dwIndex < m_dwBufferSize) {
- ch = m_pBuffer[m_dwIndex];
- switch (iState) {
- case 0:
- if (ch == '#') {
- m_dwIndex ++;
- iState = 2;
- break;
- }
- iState = 1;
- case 1:
- m_dwIndex ++;
- if (ch == ';') {
- CFX_ByteStringC ref = buf.GetByteString();
- if (ref == FX_BSTRC("gt")) {
- code = '>';
- } else if (ref == FX_BSTRC("lt")) {
- code = '<';
- } else if (ref == FX_BSTRC("amp")) {
- code = '&';
- } else if (ref == FX_BSTRC("apos")) {
- code = '\'';
- } else if (ref == FX_BSTRC("quot")) {
- code = '"';
- }
- iState = 10;
- break;
- }
- buf.AppendByte(ch);
- break;
- case 2:
- if (ch == 'x') {
- m_dwIndex ++;
- iState = 4;
- break;
- }
- iState = 3;
- case 3:
- m_dwIndex ++;
- if (ch == ';') {
- iState = 10;
- break;
- }
- if (g_FXCRT_XML_IsDigital(ch)) {
- code = code * 10 + ch - '0';
- }
- break;
- case 4:
- m_dwIndex ++;
- if (ch == ';') {
- iState = 10;
- break;
- }
- FX_BYTE nHex = g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_HexChar;
- if (nHex) {
- if (nHex == FXCRTM_XML_CHARTYPE_HexDigital) {
- code = (code << 4) + ch - '0';
- } else if (nHex == FXCRTM_XML_CHARTYPE_HexLowerLetter) {
- code = (code << 4) + ch - 87;
- } else {
- code = (code << 4) + ch - 55;
- }
- }
- break;
- }
- if (iState == 10) {
- break;
- }
- }
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (iState == 10 || m_dwIndex < m_dwBufferSize || IsEOF()) {
- break;
- }
- } while (ReadNextBlock());
- return code;
-}
-void CXML_Parser::GetAttrValue(CFX_WideStringL &value)
-{
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (IsEOF()) {
- return;
- }
- CFX_UTF8Decoder decoder(m_pAllocator);
- FX_BYTE mark = 0, ch;
- do {
- while (m_dwIndex < m_dwBufferSize) {
- ch = m_pBuffer[m_dwIndex];
- if (mark == 0) {
- if (ch != '\'' && ch != '"') {
- return;
- }
- mark = ch;
- m_dwIndex ++;
- ch = 0;
- continue;
- }
- m_dwIndex ++;
- if (ch == mark) {
- break;
- }
- if (ch == '&') {
- decoder.AppendChar(GetCharRef());
- if (IsEOF()) {
- decoder.GetResult(value);
- return;
- }
- } else {
- decoder.Input(ch);
- }
- }
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (ch == mark || m_dwIndex < m_dwBufferSize || IsEOF()) {
- break;
- }
- } while (ReadNextBlock());
- decoder.GetResult(value);
-}
-void CXML_Parser::GetTagName(CFX_ByteStringL &space, CFX_ByteStringL &name, FX_BOOL &bEndTag, FX_BOOL bStartTag)
-{
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (IsEOF()) {
- return;
- }
- bEndTag = FALSE;
- FX_BYTE ch;
- FX_INT32 iState = bStartTag ? 1 : 0;
- do {
- while (m_dwIndex < m_dwBufferSize) {
- ch = m_pBuffer[m_dwIndex];
- switch (iState) {
- case 0:
- m_dwIndex ++;
- if (ch != '<') {
- break;
- }
- iState = 1;
- break;
- case 1:
- if (ch == '?') {
- m_dwIndex ++;
- SkipLiterals(FX_BSTRC("?>"));
- iState = 0;
- break;
- } else if (ch == '!') {
- m_dwIndex ++;
- SkipLiterals(FX_BSTRC("-->"));
- iState = 0;
- break;
- }
- if (ch == '/') {
- m_dwIndex ++;
- GetName(space, name);
- bEndTag = TRUE;
- } else {
- GetName(space, name);
- bEndTag = FALSE;
- }
- return;
- }
- }
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (m_dwIndex < m_dwBufferSize || IsEOF()) {
- break;
- }
- } while (ReadNextBlock());
-}
-CXML_Element* CXML_Parser::ParseElement(CXML_Element* pParent, FX_BOOL bStartTag)
-{
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (IsEOF()) {
- return NULL;
- }
- CFX_ByteStringL tag_name, tag_space;
- FX_BOOL bEndTag;
- GetTagName(tag_space, tag_name, bEndTag, bStartTag);
- if (tag_name.IsEmpty() || bEndTag) {
- tag_space.Empty(m_pAllocator);
- return NULL;
- }
- CXML_Element* pElement;
- if (m_pAllocator) {
- pElement = FX_NewAtAllocator(m_pAllocator)CXML_Element(m_pAllocator);
- } else {
- pElement = FX_NEW CXML_Element;
- }
- if (pElement) {
- pElement->m_pParent = pParent;
- pElement->SetTag(tag_space, tag_name);
- }
- tag_space.Empty(m_pAllocator);
- tag_name.Empty(m_pAllocator);
- if (!pElement) {
- return NULL;
- }
- do {
- CFX_ByteStringL attr_space, attr_name;
- while (m_dwIndex < m_dwBufferSize) {
- SkipWhiteSpaces();
- if (IsEOF()) {
- break;
- }
- if (!g_FXCRT_XML_IsNameIntro(m_pBuffer[m_dwIndex])) {
- break;
- }
- attr_space.Empty(m_pAllocator);
- attr_name.Empty(m_pAllocator);
- GetName(attr_space, attr_name);
- SkipWhiteSpaces();
- if (IsEOF()) {
- break;
- }
- if (m_pBuffer[m_dwIndex] != '=') {
- break;
- }
- m_dwIndex ++;
- SkipWhiteSpaces();
- if (IsEOF()) {
- break;
- }
- CFX_WideStringL attr_value;
- GetAttrValue(attr_value);
- pElement->m_AttrMap.SetAt(attr_space, attr_name, attr_value, m_pAllocator);
- attr_value.Empty(m_pAllocator);
- }
- attr_space.Empty(m_pAllocator);
- attr_name.Empty(m_pAllocator);
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (m_dwIndex < m_dwBufferSize || IsEOF()) {
- break;
- }
- } while (ReadNextBlock());
- SkipWhiteSpaces();
- if (IsEOF()) {
- return pElement;
- }
- FX_BYTE ch = m_pBuffer[m_dwIndex ++];
- if (ch == '/') {
- m_dwIndex ++;
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- return pElement;
- }
- if (ch != '>') {
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (m_pAllocator) {
- FX_DeleteAtAllocator(pElement, m_pAllocator, CXML_Element);
- } else {
- delete pElement;
- }
- return NULL;
- }
- SkipWhiteSpaces();
- if (IsEOF()) {
- return pElement;
- }
- CFX_UTF8Decoder decoder(m_pAllocator);
- CFX_WideTextBuf content(m_pAllocator);
- FX_BOOL bCDATA = FALSE;
- FX_INT32 iState = 0;
- do {
- while (m_dwIndex < m_dwBufferSize) {
- ch = m_pBuffer[m_dwIndex ++];
- switch (iState) {
- case 0:
- if (ch == '<') {
- iState = 1;
- } else if (ch == '&') {
- decoder.ClearStatus();
- decoder.AppendChar(GetCharRef());
- } else {
- decoder.Input(ch);
- }
- break;
- case 1:
- if (ch == '!') {
- iState = 2;
- } else if (ch == '?') {
- SkipLiterals(FX_BSTRC("?>"));
- SkipWhiteSpaces();
- iState = 0;
- } else if (ch == '/') {
- CFX_ByteStringL space, name;
- GetName(space, name);
- space.Empty(m_pAllocator);
- name.Empty(m_pAllocator);
- SkipWhiteSpaces();
- m_dwIndex ++;
- iState = 10;
- } else {
- content << decoder.GetResult();
- CFX_WideStringL dataStr;
- content.GetWideStringL(dataStr);
- if (!bCDATA && !m_bSaveSpaceChars) {
- dataStr.TrimRight((FX_LPCWSTR)L" \t\r\n");
- }
- InsertContentSegment(bCDATA, dataStr, pElement);
- dataStr.Empty(m_pAllocator);
- content.Clear();
- decoder.Clear();
- bCDATA = FALSE;
- iState = 0;
- m_dwIndex --;
- CXML_Element* pSubElement = ParseElement(pElement, TRUE);
- if (pSubElement == NULL) {
- break;
- }
- pSubElement->m_pParent = pElement;
- pElement->m_Children.Add((FX_LPVOID)CXML_Element::Element);
- pElement->m_Children.Add(pSubElement);
- SkipWhiteSpaces();
- }
- break;
- case 2:
- if (ch == '[') {
- SkipLiterals(FX_BSTRC("]]>"));
- } else if (ch == '-') {
- m_dwIndex ++;
- SkipLiterals(FX_BSTRC("-->"));
- } else {
- SkipLiterals(FX_BSTRC(">"));
- }
- decoder.Clear();
- SkipWhiteSpaces();
- iState = 0;
- break;
- }
- if (iState == 10) {
- break;
- }
- }
- m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex;
- if (iState == 10 || m_dwIndex < m_dwBufferSize || IsEOF()) {
- break;
- }
- } while (ReadNextBlock());
- content << decoder.GetResult();
- CFX_WideStringL dataStr;
- content.GetWideStringL(dataStr);
- if (!m_bSaveSpaceChars) {
- dataStr.TrimRight((FX_LPCWSTR)L" \t\r\n");
- }
- InsertContentSegment(bCDATA, dataStr, pElement);
- dataStr.Empty(m_pAllocator);
- content.Clear();
- decoder.Clear();
- bCDATA = FALSE;
- return pElement;
-}
-void CXML_Parser::InsertContentSegment(FX_BOOL bCDATA, FX_WSTR content, CXML_Element* pElement)
-{
- if (content.IsEmpty()) {
- return;
- }
- CXML_Content* pContent;
- if (m_pAllocator) {
- pContent = FX_NewAtAllocator(m_pAllocator)CXML_Content;
- } else {
- pContent = FX_NEW CXML_Content;
- }
- if (!pContent) {
- return;
- }
- pContent->Set(bCDATA, content, m_pAllocator);
- pElement->m_Children.Add((FX_LPVOID)CXML_Element::Content);
- pElement->m_Children.Add(pContent);
-}
-static CXML_Element* XML_ContinueParse(CXML_Parser &parser, FX_BOOL bSaveSpaceChars, FX_FILESIZE* pParsedSize)
-{
- parser.m_bSaveSpaceChars = bSaveSpaceChars;
- CXML_Element* pElement = parser.ParseElement(NULL, FALSE);
- if (pParsedSize) {
- *pParsedSize = parser.m_nOffset;
- }
- return pElement;
-}
-CXML_Element* CXML_Element::Parse(const void* pBuffer, size_t size, FX_BOOL bSaveSpaceChars, FX_FILESIZE* pParsedSize, IFX_Allocator* pAllocator)
-{
- CXML_Parser parser(pAllocator);
- if (!parser.Init((FX_LPBYTE)pBuffer, size)) {
- return NULL;
- }
- return XML_ContinueParse(parser, bSaveSpaceChars, pParsedSize);
-}
-CXML_Element* CXML_Element::Parse(IFX_FileRead *pFile, FX_BOOL bSaveSpaceChars, FX_FILESIZE* pParsedSize, IFX_Allocator* pAllocator)
-{
- CXML_Parser parser(pAllocator);
- if (!parser.Init(pFile)) {
- return NULL;
- }
- return XML_ContinueParse(parser, bSaveSpaceChars, pParsedSize);
-}
-CXML_Element* CXML_Element::Parse(IFX_BufferRead *pBuffer, FX_BOOL bSaveSpaceChars, FX_FILESIZE* pParsedSize, IFX_Allocator* pAllocator)
-{
- CXML_Parser parser(pAllocator);
- if (!parser.Init(pBuffer)) {
- return NULL;
- }
- return XML_ContinueParse(parser, bSaveSpaceChars, pParsedSize);
-}
-CXML_Element::CXML_Element(IFX_Allocator* pAllocator)
- : m_pParent(NULL)
- , m_QSpaceName()
- , m_TagName()
- , m_AttrMap()
- , m_Children(pAllocator)
-{
-}
-CXML_Element::CXML_Element(FX_BSTR qSpace, FX_BSTR tagName, IFX_Allocator* pAllocator)
- : m_pParent(NULL)
- , m_QSpaceName()
- , m_TagName()
- , m_AttrMap()
- , m_Children(pAllocator)
-{
- m_QSpaceName.Set(qSpace, pAllocator);
- m_TagName.Set(tagName, pAllocator);
-}
-CXML_Element::CXML_Element(FX_BSTR qTagName, IFX_Allocator* pAllocator)
- : m_pParent(NULL)
- , m_QSpaceName()
- , m_TagName()
- , m_AttrMap()
- , m_Children(pAllocator)
-{
- SetTag(qTagName);
-}
-CXML_Element::~CXML_Element()
-{
- Empty();
-}
-void CXML_Element::Empty()
-{
- IFX_Allocator* pAllocator = m_Children.m_pAllocator;
- m_QSpaceName.Empty(pAllocator);
- m_TagName.Empty(pAllocator);
- m_AttrMap.RemoveAll(pAllocator);
- RemoveChildren();
-}
-void CXML_Element::RemoveChildren()
-{
- IFX_Allocator* pAllocator = m_Children.m_pAllocator;
- for (int i = 0; i < m_Children.GetSize(); i += 2) {
- ChildType type = (ChildType)(FX_UINTPTR)m_Children.GetAt(i);
- if (type == Content) {
- CXML_Content* content = (CXML_Content*)m_Children.GetAt(i + 1);
- if (pAllocator) {
- FX_DeleteAtAllocator(content, pAllocator, CXML_Content);
- } else {
- delete content;
- }
- } else if (type == Element) {
- CXML_Element* child = (CXML_Element*)m_Children.GetAt(i + 1);
- child->RemoveChildren();
- if (pAllocator) {
- FX_DeleteAtAllocator(child, pAllocator, CXML_Element);
- } else {
- delete child;
- }
- }
- }
- m_Children.RemoveAll();
-}
-CFX_ByteString CXML_Element::GetTagName(FX_BOOL bQualified) const
-{
- if (!bQualified || m_QSpaceName.IsEmpty()) {
- return m_TagName;
- }
- CFX_ByteString bsTag = m_QSpaceName;
- bsTag += ":";
- bsTag += m_TagName;
- return bsTag;
-}
-void CXML_Element::GetTagName(CFX_ByteStringL &tagName, FX_BOOL bQualified) const
-{
- IFX_Allocator* pAllocator = m_Children.m_pAllocator;
- if (!bQualified || m_QSpaceName.IsEmpty()) {
- tagName.Set(m_TagName, pAllocator);
- return;
- }
- FX_LPSTR str = tagName.AllocBuffer(m_QSpaceName.GetLength() + m_TagName.GetLength() + 2, pAllocator);
- if (!str) {
- return;
- }
- FXSYS_memcpy32(str, m_QSpaceName.GetCStr(), m_QSpaceName.GetLength());
- str += m_QSpaceName.GetLength();
- *str = ':';
- str ++;
- FXSYS_memcpy32(str, m_TagName.GetCStr(), m_TagName.GetLength());
- str += m_TagName.GetLength();
- *str = '\0';
-}
-CFX_ByteString CXML_Element::GetNamespace(FX_BOOL bQualified) const
-{
- if (bQualified) {
- return m_QSpaceName;
- }
- return GetNamespaceURI(m_QSpaceName);
-}
-void CXML_Element::GetNamespace(CFX_ByteStringL &nameSpace, FX_BOOL bQualified) const
-{
- IFX_Allocator* pAllocator = m_Children.m_pAllocator;
- if (bQualified) {
- nameSpace.Set(m_QSpaceName, pAllocator);
- return;
- }
- GetNamespaceURI(m_QSpaceName, nameSpace);
-}
-CFX_ByteString CXML_Element::GetNamespaceURI(FX_BSTR qName) const
-{
- const CFX_WideStringL* pwsSpace;
- const CXML_Element *pElement = this;
- do {
- if (qName.IsEmpty()) {
- pwsSpace = pElement->m_AttrMap.Lookup(FX_BSTRC(""), FX_BSTRC("xmlns"));
- } else {
- pwsSpace = pElement->m_AttrMap.Lookup(FX_BSTRC("xmlns"), qName);
- }
- if (pwsSpace) {
- break;
- }
- pElement = pElement->GetParent();
- } while(pElement);
- return pwsSpace ? FX_UTF8Encode(*pwsSpace) : CFX_ByteString();
-}
-void CXML_Element::GetNamespaceURI(FX_BSTR qName, CFX_ByteStringL &uri) const
-{
- IFX_Allocator* pAllocator = m_Children.m_pAllocator;
- const CFX_WideStringL* pwsSpace;
- const CXML_Element *pElement = this;
- do {
- if (qName.IsEmpty()) {
- pwsSpace = pElement->m_AttrMap.Lookup(FX_BSTRC(""), FX_BSTRC("xmlns"));
- } else {
- pwsSpace = pElement->m_AttrMap.Lookup(FX_BSTRC("xmlns"), qName);
- }
- if (pwsSpace) {
- break;
- }
- pElement = pElement->GetParent();
- } while(pElement);
- if (pwsSpace) {
- FX_UTF8Encode(pwsSpace->GetPtr(), pwsSpace->GetLength(), uri, pAllocator);
- }
-}
-void CXML_Element::GetAttrByIndex(int index, CFX_ByteString& space, CFX_ByteString& name, CFX_WideString& value) const
-{
- if (index < 0 || index >= m_AttrMap.GetSize()) {
- return;
- }
- CXML_AttrItem& item = m_AttrMap.GetAt(index);
- space = item.m_QSpaceName;
- name = item.m_AttrName;
- value = item.m_Value;
-}
-void CXML_Element::GetAttrByIndex(int index, CFX_ByteStringL &space, CFX_ByteStringL &name, CFX_WideStringL &value) const
-{
- if (index < 0 || index >= m_AttrMap.GetSize()) {
- return;
- }
- IFX_Allocator* pAllocator = m_Children.m_pAllocator;
- CXML_AttrItem& item = m_AttrMap.GetAt(index);
- space.Set(item.m_QSpaceName, pAllocator);
- name.Set(item.m_AttrName, pAllocator);
- value.Set(item.m_Value, pAllocator);
-}
-FX_BOOL CXML_Element::HasAttr(FX_BSTR name) const
-{
- CFX_ByteStringC bsSpace, bsName;
- FX_XML_SplitQualifiedName(name, bsSpace, bsName);
- return m_AttrMap.Lookup(bsSpace, bsName) != NULL;
-}
-FX_BOOL CXML_Element::GetAttrValue(FX_BSTR name, CFX_WideString& attribute) const
-{
- CFX_ByteStringC bsSpace, bsName;
- FX_XML_SplitQualifiedName(name, bsSpace, bsName);
- const CFX_WideStringL* pValue = m_AttrMap.Lookup(bsSpace, bsName);
- if (pValue) {
- attribute = CFX_WideString(pValue->GetPtr(), pValue->GetLength());
- return TRUE;
- }
- return FALSE;
-}
-const CFX_WideStringL* CXML_Element::GetAttrValuePtr(FX_BSTR name) const
-{
- CFX_ByteStringC bsSpace, bsName;
- FX_XML_SplitQualifiedName(name, bsSpace, bsName);
- return m_AttrMap.Lookup(bsSpace, bsName);
-}
-FX_BOOL CXML_Element::GetAttrValue(FX_BSTR space, FX_BSTR name, CFX_WideString& attribute) const
-{
- const CFX_WideStringL* pValue = m_AttrMap.Lookup(space, name);
- if (pValue) {
- attribute = CFX_WideString(pValue->GetPtr(), pValue->GetLength());
- return TRUE;
- }
- return FALSE;
-}
-const CFX_WideStringL* CXML_Element::GetAttrValuePtr(FX_BSTR space, FX_BSTR name) const
-{
- return m_AttrMap.Lookup(space, name);
-}
-FX_BOOL CXML_Element::GetAttrInteger(FX_BSTR name, int& attribute) const
-{
- CFX_ByteStringC bsSpace, bsName;
- FX_XML_SplitQualifiedName(name, bsSpace, bsName);
- const CFX_WideStringL* pwsValue = m_AttrMap.Lookup(bsSpace, bsName);
- if (pwsValue) {
- attribute = pwsValue->GetInteger();
- return TRUE;
- }
- return FALSE;
-}
-FX_BOOL CXML_Element::GetAttrInteger(FX_BSTR space, FX_BSTR name, int& attribute) const
-{
- const CFX_WideStringL* pwsValue = m_AttrMap.Lookup(space, name);
- if (pwsValue) {
- attribute = pwsValue->GetInteger();
- return TRUE;
- }
- return FALSE;
-}
-FX_BOOL CXML_Element::GetAttrFloat(FX_BSTR name, FX_FLOAT& attribute) const
-{
- CFX_ByteStringC bsSpace, bsName;
- FX_XML_SplitQualifiedName(name, bsSpace, bsName);
- return GetAttrFloat(bsSpace, bsName, attribute);
-}
-FX_BOOL CXML_Element::GetAttrFloat(FX_BSTR space, FX_BSTR name, FX_FLOAT& attribute) const
-{
- CFX_WideString value;
- const CFX_WideStringL* pValue = m_AttrMap.Lookup(space, name);
- if (pValue) {
- attribute = pValue->GetFloat();
- return TRUE;
- }
- return FALSE;
-}
-FX_DWORD CXML_Element::CountChildren() const
-{
- return m_Children.GetSize() / 2;
-}
-CXML_Element::ChildType CXML_Element::GetChildType(FX_DWORD index) const
-{
- index <<= 1;
- if (index >= (FX_DWORD)m_Children.GetSize()) {
- return Invalid;
- }
- return (ChildType)(FX_UINTPTR)m_Children.GetAt(index);
-}
-CFX_WideString CXML_Element::GetContent(FX_DWORD index) const
-{
- index <<= 1;
- if (index >= (FX_DWORD)m_Children.GetSize() ||
- (ChildType)(FX_UINTPTR)m_Children.GetAt(index) != Content) {
- return CFX_WideString();
- }
- CXML_Content* pContent = (CXML_Content*)m_Children.GetAt(index + 1);
- if (pContent) {
- return pContent->m_Content;
- }
- return CFX_WideString();
-}
-const CFX_WideStringL* CXML_Element::GetContentPtr(FX_DWORD index) const
-{
- index <<= 1;
- if (index >= (FX_DWORD)m_Children.GetSize() ||
- (ChildType)(FX_UINTPTR)m_Children.GetAt(index) != Content) {
- return NULL;
- }
- CXML_Content* pContent = (CXML_Content*)m_Children.GetAt(index + 1);
- if (pContent) {
- return &pContent->m_Content;
- }
- return NULL;
-}
-CXML_Element* CXML_Element::GetElement(FX_DWORD index) const
-{
- index <<= 1;
- if (index >= (FX_DWORD)m_Children.GetSize() ||
- (ChildType)(FX_UINTPTR)m_Children.GetAt(index) != Element) {
- return NULL;
- }
- return (CXML_Element*)m_Children.GetAt(index + 1);
-}
-FX_DWORD CXML_Element::CountElements(FX_BSTR space, FX_BSTR tag) const
-{
- int count = 0;
- for (int i = 0; i < m_Children.GetSize(); i += 2) {
- ChildType type = (ChildType)(FX_UINTPTR)m_Children.GetAt(i);
- if (type != Element) {
- continue;
- }
- CXML_Element* pKid = (CXML_Element*)m_Children.GetAt(i + 1);
- if ((space.IsEmpty() || pKid->m_QSpaceName == space) && pKid->m_TagName == tag) {
- count ++;
- }
- }
- return count;
-}
-CXML_Element* CXML_Element::GetElement(FX_BSTR space, FX_BSTR tag, int index) const
-{
- if (index < 0) {
- return NULL;
- }
- for (int i = 0; i < m_Children.GetSize(); i += 2) {
- ChildType type = (ChildType)(FX_UINTPTR)m_Children.GetAt(i);
- if (type != Element) {
- continue;
- }
- CXML_Element* pKid = (CXML_Element*)m_Children.GetAt(i + 1);
- if ((!space.IsEmpty() && pKid->m_QSpaceName != space) || pKid->m_TagName != tag) {
- continue;
- }
- if (index -- == 0) {
- return pKid;
- }
- }
- return NULL;
-}
-FX_DWORD CXML_Element::FindElement(CXML_Element *pChild) const
-{
- for (int i = 0; i < m_Children.GetSize(); i += 2) {
- if ((ChildType)(FX_UINTPTR)m_Children.GetAt(i) == Element &&
- (CXML_Element*)m_Children.GetAt(i + 1) == pChild) {
- return (FX_DWORD)(i >> 1);
- }
- }
- return (FX_DWORD) - 1;
-}
-const CFX_WideStringL* CXML_AttrMap::Lookup(FX_BSTR space, FX_BSTR name) const
-{
- if (m_pMap == NULL) {
- return NULL;
- }
- for (int i = 0; i < m_pMap->GetSize(); i ++) {
- CXML_AttrItem& item = GetAt(i);
- if ((space.IsEmpty() || item.m_QSpaceName == space) && item.m_AttrName == name) {
- return &item.m_Value;
- }
- }
- return NULL;
-}
-void CXML_AttrMap::SetAt(FX_BSTR space, FX_BSTR name, FX_WSTR value, IFX_Allocator* pAllocator)
-{
- for (int i = 0; i < GetSize(); i ++) {
- CXML_AttrItem& item = GetAt(i);
- if ((space.IsEmpty() || item.m_QSpaceName == space) && item.m_AttrName == name) {
- item.m_Value.Set(value, pAllocator);
- return;
- }
- }
- if (!m_pMap) {
- if (pAllocator) {
- m_pMap = FX_NewAtAllocator(pAllocator)CFX_ObjectArray<CXML_AttrItem>(pAllocator);
- } else {
- m_pMap = FX_NEW CFX_ObjectArray<CXML_AttrItem>;
- }
- }
- if (!m_pMap) {
- return;
- }
- CXML_AttrItem* pItem = (CXML_AttrItem*)m_pMap->AddSpace();
- if (!pItem) {
- return;
- }
- pItem->m_QSpaceName.Set(space, pAllocator);
- pItem->m_AttrName.Set(name, pAllocator);
- pItem->m_Value.Set(value, pAllocator);
-}
-void CXML_AttrMap::RemoveAt(FX_BSTR space, FX_BSTR name, IFX_Allocator* pAllocator)
-{
- if (m_pMap == NULL) {
- return;
- }
- for (int i = 0; i < m_pMap->GetSize(); i ++) {
- CXML_AttrItem& item = GetAt(i);
- if ((space.IsEmpty() || item.m_QSpaceName == space) && item.m_AttrName == name) {
- item.Empty(pAllocator);
- m_pMap->RemoveAt(i);
- return;
- }
- }
-}
-int CXML_AttrMap::GetSize() const
-{
- return m_pMap == NULL ? 0 : m_pMap->GetSize();
-}
-CXML_AttrItem& CXML_AttrMap::GetAt(int index) const
-{
- ASSERT(m_pMap != NULL);
- return (*m_pMap)[index];
-}
-void CXML_AttrMap::RemoveAll(IFX_Allocator* pAllocator)
-{
- if (!m_pMap) {
- return;
- }
- for (int i = 0; i < m_pMap->GetSize(); i ++) {
- CXML_AttrItem& item = (*m_pMap)[i];
- item.Empty(pAllocator);
- }
- m_pMap->RemoveAll();
- if (pAllocator) {
- FX_DeleteAtAllocator(m_pMap, pAllocator, CFX_ObjectArray<CXML_AttrItem>);
- } else {
- delete m_pMap;
- }
- m_pMap = NULL;
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_xml.h" +#include "xml_int.h" +CXML_Parser::~CXML_Parser() +{ + if (m_bOwnedStream) { + m_pDataAcc->Release(); + } +} +FX_BOOL CXML_Parser::Init(FX_LPBYTE pBuffer, size_t size) +{ + if (m_pAllocator) { + m_pDataAcc = FX_NewAtAllocator(m_pAllocator)CXML_DataBufAcc(pBuffer, size, m_pAllocator); + } else { + m_pDataAcc = FX_NEW CXML_DataBufAcc(pBuffer, size, NULL); + } + if (!m_pDataAcc) { + return FALSE; + } + return Init(TRUE); +} +FX_BOOL CXML_Parser::Init(IFX_FileRead *pFileRead) +{ + if (m_pAllocator) { + m_pDataAcc = FX_NewAtAllocator(m_pAllocator)CXML_DataStmAcc(pFileRead, m_pAllocator); + } else { + m_pDataAcc = FX_NEW CXML_DataStmAcc(pFileRead, NULL); + } + if (!m_pDataAcc) { + return FALSE; + } + return Init(TRUE); +} +FX_BOOL CXML_Parser::Init(IFX_BufferRead *pBuffer) +{ + if (!pBuffer) { + return FALSE; + } + m_pDataAcc = pBuffer; + return Init(FALSE); +} +FX_BOOL CXML_Parser::Init(FX_BOOL bOwndedStream) +{ + m_bOwnedStream = bOwndedStream; + m_nOffset = 0; + return ReadNextBlock(); +} +FX_BOOL CXML_Parser::ReadNextBlock() +{ + if (!m_pDataAcc->ReadNextBlock()) { + return FALSE; + } + m_pBuffer = m_pDataAcc->GetBlockBuffer(); + m_dwBufferSize = m_pDataAcc->GetBlockSize(); + m_nBufferOffset = m_pDataAcc->GetBlockOffset(); + m_dwIndex = 0; + return m_dwBufferSize > 0; +} +FX_BOOL CXML_Parser::IsEOF() +{ + if (!m_pDataAcc->IsEOF()) { + return FALSE; + } + return m_dwIndex >= m_dwBufferSize; +} +#define FXCRTM_XML_CHARTYPE_Normal 0x00 +#define FXCRTM_XML_CHARTYPE_SpaceChar 0x01 +#define FXCRTM_XML_CHARTYPE_Letter 0x02 +#define FXCRTM_XML_CHARTYPE_Digital 0x04 +#define FXCRTM_XML_CHARTYPE_NameIntro 0x08 +#define FXCRTM_XML_CHARTYPE_NameChar 0x10 +#define FXCRTM_XML_CHARTYPE_HexDigital 0x20 +#define FXCRTM_XML_CHARTYPE_HexLowerLetter 0x40 +#define FXCRTM_XML_CHARTYPE_HexUpperLetter 0x60 +#define FXCRTM_XML_CHARTYPE_HexChar 0x60 +FX_BYTE g_FXCRT_XML_ByteTypes[256] = { + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, + 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x7A, 0x7A, 0x7A, 0x7A, 0x7A, 0x7A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x18, + 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, + 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A, 0x01, 0x01, +}; +FX_BOOL g_FXCRT_XML_IsWhiteSpace(FX_BYTE ch) +{ + return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_SpaceChar) != 0; +} +FX_BOOL g_FXCRT_XML_IsLetter(FX_BYTE ch) +{ + return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_Letter) != 0; +} +FX_BOOL g_FXCRT_XML_IsDigital(FX_BYTE ch) +{ + return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_Digital) != 0; +} +FX_BOOL g_FXCRT_XML_IsNameIntro(FX_BYTE ch) +{ + return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_NameIntro) != 0; +} +FX_BOOL g_FXCRT_XML_IsNameChar(FX_BYTE ch) +{ + return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_NameChar) != 0; +} +FX_BOOL g_FXCRT_XML_IsHexChar(FX_BYTE ch) +{ + return (g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_HexChar) != 0; +} +void CXML_Parser::SkipWhiteSpaces() +{ + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (IsEOF()) { + return; + } + do { + while (m_dwIndex < m_dwBufferSize && g_FXCRT_XML_IsWhiteSpace(m_pBuffer[m_dwIndex])) { + m_dwIndex ++; + } + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (m_dwIndex < m_dwBufferSize || IsEOF()) { + break; + } + } while (ReadNextBlock()); +} +void CXML_Parser::GetName(CFX_ByteStringL &space, CFX_ByteStringL &name) +{ + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (IsEOF()) { + return; + } + CFX_ByteTextBuf buf(m_pAllocator); + FX_BYTE ch; + do { + while (m_dwIndex < m_dwBufferSize) { + ch = m_pBuffer[m_dwIndex]; + if (ch == ':') { + buf.GetByteStringL(space); + buf.Clear(); + } else if (g_FXCRT_XML_IsNameChar(ch)) { + buf.AppendChar(ch); + } else { + break; + } + m_dwIndex ++; + } + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (m_dwIndex < m_dwBufferSize || IsEOF()) { + break; + } + } while (ReadNextBlock()); + buf.GetByteStringL(name); +} +void CXML_Parser::SkipLiterals(FX_BSTR str) +{ + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (IsEOF()) { + return; + } + FX_INT32 i = 0, iLen = str.GetLength(); + do { + while (m_dwIndex < m_dwBufferSize) { + if (str.GetAt(i) != m_pBuffer[m_dwIndex ++]) { + i = 0; + } else { + i ++; + if (i == iLen) { + break; + } + } + } + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (i == iLen) { + return; + } + if (m_dwIndex < m_dwBufferSize || IsEOF()) { + break; + } + } while (ReadNextBlock()); + while (!m_pDataAcc->IsEOF()) { + ReadNextBlock(); + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwBufferSize; + } + m_dwIndex = m_dwBufferSize; +} +FX_DWORD CXML_Parser::GetCharRef() +{ + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (IsEOF()) { + return 0; + } + FX_BYTE ch; + FX_INT32 iState = 0; + CFX_ByteTextBuf buf(m_pAllocator); + FX_DWORD code = 0; + do { + while (m_dwIndex < m_dwBufferSize) { + ch = m_pBuffer[m_dwIndex]; + switch (iState) { + case 0: + if (ch == '#') { + m_dwIndex ++; + iState = 2; + break; + } + iState = 1; + case 1: + m_dwIndex ++; + if (ch == ';') { + CFX_ByteStringC ref = buf.GetByteString(); + if (ref == FX_BSTRC("gt")) { + code = '>'; + } else if (ref == FX_BSTRC("lt")) { + code = '<'; + } else if (ref == FX_BSTRC("amp")) { + code = '&'; + } else if (ref == FX_BSTRC("apos")) { + code = '\''; + } else if (ref == FX_BSTRC("quot")) { + code = '"'; + } + iState = 10; + break; + } + buf.AppendByte(ch); + break; + case 2: + if (ch == 'x') { + m_dwIndex ++; + iState = 4; + break; + } + iState = 3; + case 3: + m_dwIndex ++; + if (ch == ';') { + iState = 10; + break; + } + if (g_FXCRT_XML_IsDigital(ch)) { + code = code * 10 + ch - '0'; + } + break; + case 4: + m_dwIndex ++; + if (ch == ';') { + iState = 10; + break; + } + FX_BYTE nHex = g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_HexChar; + if (nHex) { + if (nHex == FXCRTM_XML_CHARTYPE_HexDigital) { + code = (code << 4) + ch - '0'; + } else if (nHex == FXCRTM_XML_CHARTYPE_HexLowerLetter) { + code = (code << 4) + ch - 87; + } else { + code = (code << 4) + ch - 55; + } + } + break; + } + if (iState == 10) { + break; + } + } + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (iState == 10 || m_dwIndex < m_dwBufferSize || IsEOF()) { + break; + } + } while (ReadNextBlock()); + return code; +} +void CXML_Parser::GetAttrValue(CFX_WideStringL &value) +{ + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (IsEOF()) { + return; + } + CFX_UTF8Decoder decoder(m_pAllocator); + FX_BYTE mark = 0, ch; + do { + while (m_dwIndex < m_dwBufferSize) { + ch = m_pBuffer[m_dwIndex]; + if (mark == 0) { + if (ch != '\'' && ch != '"') { + return; + } + mark = ch; + m_dwIndex ++; + ch = 0; + continue; + } + m_dwIndex ++; + if (ch == mark) { + break; + } + if (ch == '&') { + decoder.AppendChar(GetCharRef()); + if (IsEOF()) { + decoder.GetResult(value); + return; + } + } else { + decoder.Input(ch); + } + } + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (ch == mark || m_dwIndex < m_dwBufferSize || IsEOF()) { + break; + } + } while (ReadNextBlock()); + decoder.GetResult(value); +} +void CXML_Parser::GetTagName(CFX_ByteStringL &space, CFX_ByteStringL &name, FX_BOOL &bEndTag, FX_BOOL bStartTag) +{ + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (IsEOF()) { + return; + } + bEndTag = FALSE; + FX_BYTE ch; + FX_INT32 iState = bStartTag ? 1 : 0; + do { + while (m_dwIndex < m_dwBufferSize) { + ch = m_pBuffer[m_dwIndex]; + switch (iState) { + case 0: + m_dwIndex ++; + if (ch != '<') { + break; + } + iState = 1; + break; + case 1: + if (ch == '?') { + m_dwIndex ++; + SkipLiterals(FX_BSTRC("?>")); + iState = 0; + break; + } else if (ch == '!') { + m_dwIndex ++; + SkipLiterals(FX_BSTRC("-->")); + iState = 0; + break; + } + if (ch == '/') { + m_dwIndex ++; + GetName(space, name); + bEndTag = TRUE; + } else { + GetName(space, name); + bEndTag = FALSE; + } + return; + } + } + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (m_dwIndex < m_dwBufferSize || IsEOF()) { + break; + } + } while (ReadNextBlock()); +} +CXML_Element* CXML_Parser::ParseElement(CXML_Element* pParent, FX_BOOL bStartTag) +{ + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (IsEOF()) { + return NULL; + } + CFX_ByteStringL tag_name, tag_space; + FX_BOOL bEndTag; + GetTagName(tag_space, tag_name, bEndTag, bStartTag); + if (tag_name.IsEmpty() || bEndTag) { + tag_space.Empty(m_pAllocator); + return NULL; + } + CXML_Element* pElement; + if (m_pAllocator) { + pElement = FX_NewAtAllocator(m_pAllocator)CXML_Element(m_pAllocator); + } else { + pElement = FX_NEW CXML_Element; + } + if (pElement) { + pElement->m_pParent = pParent; + pElement->SetTag(tag_space, tag_name); + } + tag_space.Empty(m_pAllocator); + tag_name.Empty(m_pAllocator); + if (!pElement) { + return NULL; + } + do { + CFX_ByteStringL attr_space, attr_name; + while (m_dwIndex < m_dwBufferSize) { + SkipWhiteSpaces(); + if (IsEOF()) { + break; + } + if (!g_FXCRT_XML_IsNameIntro(m_pBuffer[m_dwIndex])) { + break; + } + attr_space.Empty(m_pAllocator); + attr_name.Empty(m_pAllocator); + GetName(attr_space, attr_name); + SkipWhiteSpaces(); + if (IsEOF()) { + break; + } + if (m_pBuffer[m_dwIndex] != '=') { + break; + } + m_dwIndex ++; + SkipWhiteSpaces(); + if (IsEOF()) { + break; + } + CFX_WideStringL attr_value; + GetAttrValue(attr_value); + pElement->m_AttrMap.SetAt(attr_space, attr_name, attr_value, m_pAllocator); + attr_value.Empty(m_pAllocator); + } + attr_space.Empty(m_pAllocator); + attr_name.Empty(m_pAllocator); + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (m_dwIndex < m_dwBufferSize || IsEOF()) { + break; + } + } while (ReadNextBlock()); + SkipWhiteSpaces(); + if (IsEOF()) { + return pElement; + } + FX_BYTE ch = m_pBuffer[m_dwIndex ++]; + if (ch == '/') { + m_dwIndex ++; + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + return pElement; + } + if (ch != '>') { + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (m_pAllocator) { + FX_DeleteAtAllocator(pElement, m_pAllocator, CXML_Element); + } else { + delete pElement; + } + return NULL; + } + SkipWhiteSpaces(); + if (IsEOF()) { + return pElement; + } + CFX_UTF8Decoder decoder(m_pAllocator); + CFX_WideTextBuf content(m_pAllocator); + FX_BOOL bCDATA = FALSE; + FX_INT32 iState = 0; + do { + while (m_dwIndex < m_dwBufferSize) { + ch = m_pBuffer[m_dwIndex ++]; + switch (iState) { + case 0: + if (ch == '<') { + iState = 1; + } else if (ch == '&') { + decoder.ClearStatus(); + decoder.AppendChar(GetCharRef()); + } else { + decoder.Input(ch); + } + break; + case 1: + if (ch == '!') { + iState = 2; + } else if (ch == '?') { + SkipLiterals(FX_BSTRC("?>")); + SkipWhiteSpaces(); + iState = 0; + } else if (ch == '/') { + CFX_ByteStringL space, name; + GetName(space, name); + space.Empty(m_pAllocator); + name.Empty(m_pAllocator); + SkipWhiteSpaces(); + m_dwIndex ++; + iState = 10; + } else { + content << decoder.GetResult(); + CFX_WideStringL dataStr; + content.GetWideStringL(dataStr); + if (!bCDATA && !m_bSaveSpaceChars) { + dataStr.TrimRight((FX_LPCWSTR)L" \t\r\n"); + } + InsertContentSegment(bCDATA, dataStr, pElement); + dataStr.Empty(m_pAllocator); + content.Clear(); + decoder.Clear(); + bCDATA = FALSE; + iState = 0; + m_dwIndex --; + CXML_Element* pSubElement = ParseElement(pElement, TRUE); + if (pSubElement == NULL) { + break; + } + pSubElement->m_pParent = pElement; + pElement->m_Children.Add((FX_LPVOID)CXML_Element::Element); + pElement->m_Children.Add(pSubElement); + SkipWhiteSpaces(); + } + break; + case 2: + if (ch == '[') { + SkipLiterals(FX_BSTRC("]]>")); + } else if (ch == '-') { + m_dwIndex ++; + SkipLiterals(FX_BSTRC("-->")); + } else { + SkipLiterals(FX_BSTRC(">")); + } + decoder.Clear(); + SkipWhiteSpaces(); + iState = 0; + break; + } + if (iState == 10) { + break; + } + } + m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; + if (iState == 10 || m_dwIndex < m_dwBufferSize || IsEOF()) { + break; + } + } while (ReadNextBlock()); + content << decoder.GetResult(); + CFX_WideStringL dataStr; + content.GetWideStringL(dataStr); + if (!m_bSaveSpaceChars) { + dataStr.TrimRight((FX_LPCWSTR)L" \t\r\n"); + } + InsertContentSegment(bCDATA, dataStr, pElement); + dataStr.Empty(m_pAllocator); + content.Clear(); + decoder.Clear(); + bCDATA = FALSE; + return pElement; +} +void CXML_Parser::InsertContentSegment(FX_BOOL bCDATA, FX_WSTR content, CXML_Element* pElement) +{ + if (content.IsEmpty()) { + return; + } + CXML_Content* pContent; + if (m_pAllocator) { + pContent = FX_NewAtAllocator(m_pAllocator)CXML_Content; + } else { + pContent = FX_NEW CXML_Content; + } + if (!pContent) { + return; + } + pContent->Set(bCDATA, content, m_pAllocator); + pElement->m_Children.Add((FX_LPVOID)CXML_Element::Content); + pElement->m_Children.Add(pContent); +} +static CXML_Element* XML_ContinueParse(CXML_Parser &parser, FX_BOOL bSaveSpaceChars, FX_FILESIZE* pParsedSize) +{ + parser.m_bSaveSpaceChars = bSaveSpaceChars; + CXML_Element* pElement = parser.ParseElement(NULL, FALSE); + if (pParsedSize) { + *pParsedSize = parser.m_nOffset; + } + return pElement; +} +CXML_Element* CXML_Element::Parse(const void* pBuffer, size_t size, FX_BOOL bSaveSpaceChars, FX_FILESIZE* pParsedSize, IFX_Allocator* pAllocator) +{ + CXML_Parser parser(pAllocator); + if (!parser.Init((FX_LPBYTE)pBuffer, size)) { + return NULL; + } + return XML_ContinueParse(parser, bSaveSpaceChars, pParsedSize); +} +CXML_Element* CXML_Element::Parse(IFX_FileRead *pFile, FX_BOOL bSaveSpaceChars, FX_FILESIZE* pParsedSize, IFX_Allocator* pAllocator) +{ + CXML_Parser parser(pAllocator); + if (!parser.Init(pFile)) { + return NULL; + } + return XML_ContinueParse(parser, bSaveSpaceChars, pParsedSize); +} +CXML_Element* CXML_Element::Parse(IFX_BufferRead *pBuffer, FX_BOOL bSaveSpaceChars, FX_FILESIZE* pParsedSize, IFX_Allocator* pAllocator) +{ + CXML_Parser parser(pAllocator); + if (!parser.Init(pBuffer)) { + return NULL; + } + return XML_ContinueParse(parser, bSaveSpaceChars, pParsedSize); +} +CXML_Element::CXML_Element(IFX_Allocator* pAllocator) + : m_pParent(NULL) + , m_QSpaceName() + , m_TagName() + , m_AttrMap() + , m_Children(pAllocator) +{ +} +CXML_Element::CXML_Element(FX_BSTR qSpace, FX_BSTR tagName, IFX_Allocator* pAllocator) + : m_pParent(NULL) + , m_QSpaceName() + , m_TagName() + , m_AttrMap() + , m_Children(pAllocator) +{ + m_QSpaceName.Set(qSpace, pAllocator); + m_TagName.Set(tagName, pAllocator); +} +CXML_Element::CXML_Element(FX_BSTR qTagName, IFX_Allocator* pAllocator) + : m_pParent(NULL) + , m_QSpaceName() + , m_TagName() + , m_AttrMap() + , m_Children(pAllocator) +{ + SetTag(qTagName); +} +CXML_Element::~CXML_Element() +{ + Empty(); +} +void CXML_Element::Empty() +{ + IFX_Allocator* pAllocator = m_Children.m_pAllocator; + m_QSpaceName.Empty(pAllocator); + m_TagName.Empty(pAllocator); + m_AttrMap.RemoveAll(pAllocator); + RemoveChildren(); +} +void CXML_Element::RemoveChildren() +{ + IFX_Allocator* pAllocator = m_Children.m_pAllocator; + for (int i = 0; i < m_Children.GetSize(); i += 2) { + ChildType type = (ChildType)(FX_UINTPTR)m_Children.GetAt(i); + if (type == Content) { + CXML_Content* content = (CXML_Content*)m_Children.GetAt(i + 1); + if (pAllocator) { + FX_DeleteAtAllocator(content, pAllocator, CXML_Content); + } else { + delete content; + } + } else if (type == Element) { + CXML_Element* child = (CXML_Element*)m_Children.GetAt(i + 1); + child->RemoveChildren(); + if (pAllocator) { + FX_DeleteAtAllocator(child, pAllocator, CXML_Element); + } else { + delete child; + } + } + } + m_Children.RemoveAll(); +} +CFX_ByteString CXML_Element::GetTagName(FX_BOOL bQualified) const +{ + if (!bQualified || m_QSpaceName.IsEmpty()) { + return m_TagName; + } + CFX_ByteString bsTag = m_QSpaceName; + bsTag += ":"; + bsTag += m_TagName; + return bsTag; +} +void CXML_Element::GetTagName(CFX_ByteStringL &tagName, FX_BOOL bQualified) const +{ + IFX_Allocator* pAllocator = m_Children.m_pAllocator; + if (!bQualified || m_QSpaceName.IsEmpty()) { + tagName.Set(m_TagName, pAllocator); + return; + } + FX_LPSTR str = tagName.AllocBuffer(m_QSpaceName.GetLength() + m_TagName.GetLength() + 2, pAllocator); + if (!str) { + return; + } + FXSYS_memcpy32(str, m_QSpaceName.GetCStr(), m_QSpaceName.GetLength()); + str += m_QSpaceName.GetLength(); + *str = ':'; + str ++; + FXSYS_memcpy32(str, m_TagName.GetCStr(), m_TagName.GetLength()); + str += m_TagName.GetLength(); + *str = '\0'; +} +CFX_ByteString CXML_Element::GetNamespace(FX_BOOL bQualified) const +{ + if (bQualified) { + return m_QSpaceName; + } + return GetNamespaceURI(m_QSpaceName); +} +void CXML_Element::GetNamespace(CFX_ByteStringL &nameSpace, FX_BOOL bQualified) const +{ + IFX_Allocator* pAllocator = m_Children.m_pAllocator; + if (bQualified) { + nameSpace.Set(m_QSpaceName, pAllocator); + return; + } + GetNamespaceURI(m_QSpaceName, nameSpace); +} +CFX_ByteString CXML_Element::GetNamespaceURI(FX_BSTR qName) const +{ + const CFX_WideStringL* pwsSpace; + const CXML_Element *pElement = this; + do { + if (qName.IsEmpty()) { + pwsSpace = pElement->m_AttrMap.Lookup(FX_BSTRC(""), FX_BSTRC("xmlns")); + } else { + pwsSpace = pElement->m_AttrMap.Lookup(FX_BSTRC("xmlns"), qName); + } + if (pwsSpace) { + break; + } + pElement = pElement->GetParent(); + } while(pElement); + return pwsSpace ? FX_UTF8Encode(*pwsSpace) : CFX_ByteString(); +} +void CXML_Element::GetNamespaceURI(FX_BSTR qName, CFX_ByteStringL &uri) const +{ + IFX_Allocator* pAllocator = m_Children.m_pAllocator; + const CFX_WideStringL* pwsSpace; + const CXML_Element *pElement = this; + do { + if (qName.IsEmpty()) { + pwsSpace = pElement->m_AttrMap.Lookup(FX_BSTRC(""), FX_BSTRC("xmlns")); + } else { + pwsSpace = pElement->m_AttrMap.Lookup(FX_BSTRC("xmlns"), qName); + } + if (pwsSpace) { + break; + } + pElement = pElement->GetParent(); + } while(pElement); + if (pwsSpace) { + FX_UTF8Encode(pwsSpace->GetPtr(), pwsSpace->GetLength(), uri, pAllocator); + } +} +void CXML_Element::GetAttrByIndex(int index, CFX_ByteString& space, CFX_ByteString& name, CFX_WideString& value) const +{ + if (index < 0 || index >= m_AttrMap.GetSize()) { + return; + } + CXML_AttrItem& item = m_AttrMap.GetAt(index); + space = item.m_QSpaceName; + name = item.m_AttrName; + value = item.m_Value; +} +void CXML_Element::GetAttrByIndex(int index, CFX_ByteStringL &space, CFX_ByteStringL &name, CFX_WideStringL &value) const +{ + if (index < 0 || index >= m_AttrMap.GetSize()) { + return; + } + IFX_Allocator* pAllocator = m_Children.m_pAllocator; + CXML_AttrItem& item = m_AttrMap.GetAt(index); + space.Set(item.m_QSpaceName, pAllocator); + name.Set(item.m_AttrName, pAllocator); + value.Set(item.m_Value, pAllocator); +} +FX_BOOL CXML_Element::HasAttr(FX_BSTR name) const +{ + CFX_ByteStringC bsSpace, bsName; + FX_XML_SplitQualifiedName(name, bsSpace, bsName); + return m_AttrMap.Lookup(bsSpace, bsName) != NULL; +} +FX_BOOL CXML_Element::GetAttrValue(FX_BSTR name, CFX_WideString& attribute) const +{ + CFX_ByteStringC bsSpace, bsName; + FX_XML_SplitQualifiedName(name, bsSpace, bsName); + const CFX_WideStringL* pValue = m_AttrMap.Lookup(bsSpace, bsName); + if (pValue) { + attribute = CFX_WideString(pValue->GetPtr(), pValue->GetLength()); + return TRUE; + } + return FALSE; +} +const CFX_WideStringL* CXML_Element::GetAttrValuePtr(FX_BSTR name) const +{ + CFX_ByteStringC bsSpace, bsName; + FX_XML_SplitQualifiedName(name, bsSpace, bsName); + return m_AttrMap.Lookup(bsSpace, bsName); +} +FX_BOOL CXML_Element::GetAttrValue(FX_BSTR space, FX_BSTR name, CFX_WideString& attribute) const +{ + const CFX_WideStringL* pValue = m_AttrMap.Lookup(space, name); + if (pValue) { + attribute = CFX_WideString(pValue->GetPtr(), pValue->GetLength()); + return TRUE; + } + return FALSE; +} +const CFX_WideStringL* CXML_Element::GetAttrValuePtr(FX_BSTR space, FX_BSTR name) const +{ + return m_AttrMap.Lookup(space, name); +} +FX_BOOL CXML_Element::GetAttrInteger(FX_BSTR name, int& attribute) const +{ + CFX_ByteStringC bsSpace, bsName; + FX_XML_SplitQualifiedName(name, bsSpace, bsName); + const CFX_WideStringL* pwsValue = m_AttrMap.Lookup(bsSpace, bsName); + if (pwsValue) { + attribute = pwsValue->GetInteger(); + return TRUE; + } + return FALSE; +} +FX_BOOL CXML_Element::GetAttrInteger(FX_BSTR space, FX_BSTR name, int& attribute) const +{ + const CFX_WideStringL* pwsValue = m_AttrMap.Lookup(space, name); + if (pwsValue) { + attribute = pwsValue->GetInteger(); + return TRUE; + } + return FALSE; +} +FX_BOOL CXML_Element::GetAttrFloat(FX_BSTR name, FX_FLOAT& attribute) const +{ + CFX_ByteStringC bsSpace, bsName; + FX_XML_SplitQualifiedName(name, bsSpace, bsName); + return GetAttrFloat(bsSpace, bsName, attribute); +} +FX_BOOL CXML_Element::GetAttrFloat(FX_BSTR space, FX_BSTR name, FX_FLOAT& attribute) const +{ + CFX_WideString value; + const CFX_WideStringL* pValue = m_AttrMap.Lookup(space, name); + if (pValue) { + attribute = pValue->GetFloat(); + return TRUE; + } + return FALSE; +} +FX_DWORD CXML_Element::CountChildren() const +{ + return m_Children.GetSize() / 2; +} +CXML_Element::ChildType CXML_Element::GetChildType(FX_DWORD index) const +{ + index <<= 1; + if (index >= (FX_DWORD)m_Children.GetSize()) { + return Invalid; + } + return (ChildType)(FX_UINTPTR)m_Children.GetAt(index); +} +CFX_WideString CXML_Element::GetContent(FX_DWORD index) const +{ + index <<= 1; + if (index >= (FX_DWORD)m_Children.GetSize() || + (ChildType)(FX_UINTPTR)m_Children.GetAt(index) != Content) { + return CFX_WideString(); + } + CXML_Content* pContent = (CXML_Content*)m_Children.GetAt(index + 1); + if (pContent) { + return pContent->m_Content; + } + return CFX_WideString(); +} +const CFX_WideStringL* CXML_Element::GetContentPtr(FX_DWORD index) const +{ + index <<= 1; + if (index >= (FX_DWORD)m_Children.GetSize() || + (ChildType)(FX_UINTPTR)m_Children.GetAt(index) != Content) { + return NULL; + } + CXML_Content* pContent = (CXML_Content*)m_Children.GetAt(index + 1); + if (pContent) { + return &pContent->m_Content; + } + return NULL; +} +CXML_Element* CXML_Element::GetElement(FX_DWORD index) const +{ + index <<= 1; + if (index >= (FX_DWORD)m_Children.GetSize() || + (ChildType)(FX_UINTPTR)m_Children.GetAt(index) != Element) { + return NULL; + } + return (CXML_Element*)m_Children.GetAt(index + 1); +} +FX_DWORD CXML_Element::CountElements(FX_BSTR space, FX_BSTR tag) const +{ + int count = 0; + for (int i = 0; i < m_Children.GetSize(); i += 2) { + ChildType type = (ChildType)(FX_UINTPTR)m_Children.GetAt(i); + if (type != Element) { + continue; + } + CXML_Element* pKid = (CXML_Element*)m_Children.GetAt(i + 1); + if ((space.IsEmpty() || pKid->m_QSpaceName == space) && pKid->m_TagName == tag) { + count ++; + } + } + return count; +} +CXML_Element* CXML_Element::GetElement(FX_BSTR space, FX_BSTR tag, int index) const +{ + if (index < 0) { + return NULL; + } + for (int i = 0; i < m_Children.GetSize(); i += 2) { + ChildType type = (ChildType)(FX_UINTPTR)m_Children.GetAt(i); + if (type != Element) { + continue; + } + CXML_Element* pKid = (CXML_Element*)m_Children.GetAt(i + 1); + if ((!space.IsEmpty() && pKid->m_QSpaceName != space) || pKid->m_TagName != tag) { + continue; + } + if (index -- == 0) { + return pKid; + } + } + return NULL; +} +FX_DWORD CXML_Element::FindElement(CXML_Element *pChild) const +{ + for (int i = 0; i < m_Children.GetSize(); i += 2) { + if ((ChildType)(FX_UINTPTR)m_Children.GetAt(i) == Element && + (CXML_Element*)m_Children.GetAt(i + 1) == pChild) { + return (FX_DWORD)(i >> 1); + } + } + return (FX_DWORD) - 1; +} +const CFX_WideStringL* CXML_AttrMap::Lookup(FX_BSTR space, FX_BSTR name) const +{ + if (m_pMap == NULL) { + return NULL; + } + for (int i = 0; i < m_pMap->GetSize(); i ++) { + CXML_AttrItem& item = GetAt(i); + if ((space.IsEmpty() || item.m_QSpaceName == space) && item.m_AttrName == name) { + return &item.m_Value; + } + } + return NULL; +} +void CXML_AttrMap::SetAt(FX_BSTR space, FX_BSTR name, FX_WSTR value, IFX_Allocator* pAllocator) +{ + for (int i = 0; i < GetSize(); i ++) { + CXML_AttrItem& item = GetAt(i); + if ((space.IsEmpty() || item.m_QSpaceName == space) && item.m_AttrName == name) { + item.m_Value.Set(value, pAllocator); + return; + } + } + if (!m_pMap) { + if (pAllocator) { + m_pMap = FX_NewAtAllocator(pAllocator)CFX_ObjectArray<CXML_AttrItem>(pAllocator); + } else { + m_pMap = FX_NEW CFX_ObjectArray<CXML_AttrItem>; + } + } + if (!m_pMap) { + return; + } + CXML_AttrItem* pItem = (CXML_AttrItem*)m_pMap->AddSpace(); + if (!pItem) { + return; + } + pItem->m_QSpaceName.Set(space, pAllocator); + pItem->m_AttrName.Set(name, pAllocator); + pItem->m_Value.Set(value, pAllocator); +} +void CXML_AttrMap::RemoveAt(FX_BSTR space, FX_BSTR name, IFX_Allocator* pAllocator) +{ + if (m_pMap == NULL) { + return; + } + for (int i = 0; i < m_pMap->GetSize(); i ++) { + CXML_AttrItem& item = GetAt(i); + if ((space.IsEmpty() || item.m_QSpaceName == space) && item.m_AttrName == name) { + item.Empty(pAllocator); + m_pMap->RemoveAt(i); + return; + } + } +} +int CXML_AttrMap::GetSize() const +{ + return m_pMap == NULL ? 0 : m_pMap->GetSize(); +} +CXML_AttrItem& CXML_AttrMap::GetAt(int index) const +{ + ASSERT(m_pMap != NULL); + return (*m_pMap)[index]; +} +void CXML_AttrMap::RemoveAll(IFX_Allocator* pAllocator) +{ + if (!m_pMap) { + return; + } + for (int i = 0; i < m_pMap->GetSize(); i ++) { + CXML_AttrItem& item = (*m_pMap)[i]; + item.Empty(pAllocator); + } + m_pMap->RemoveAll(); + if (pAllocator) { + FX_DeleteAtAllocator(m_pMap, pAllocator, CFX_ObjectArray<CXML_AttrItem>); + } else { + delete m_pMap; + } + m_pMap = NULL; +} diff --git a/core/src/fxcrt/fxcrt_platforms.cpp b/core/src/fxcrt/fxcrt_platforms.cpp index 1479b7bcf0..7fb3adae40 100644 --- a/core/src/fxcrt/fxcrt_platforms.cpp +++ b/core/src/fxcrt/fxcrt_platforms.cpp @@ -1,203 +1,203 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_ext.h"
-#include "fxcrt_platforms.h"
-#if (_FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ && _FXM_PLATFORM_ != _FXM_PLATFORM_LINUX_ && _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ && _FXM_PLATFORM_ != _FXM_PLATFORM_ANDROID_)
-IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator)
-{
- if (pAllocator) {
- return FX_NewAtAllocator(pAllocator) CFXCRT_FileAccess_CRT;
- } else {
- return FX_NEW CFXCRT_FileAccess_CRT;
- }
-}
-void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_ByteString &bsMode)
-{
- if (dwModes & FX_FILEMODE_ReadOnly) {
- bsMode = FX_BSTRC("rb");
- } else if (dwModes & FX_FILEMODE_Truncate) {
- bsMode = FX_BSTRC("w+b");
- } else {
- bsMode = FX_BSTRC("a+b");
- }
-}
-void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_WideString &wsMode)
-{
- if (dwModes & FX_FILEMODE_ReadOnly) {
- wsMode = FX_WSTRC(L"rb");
- } else if (dwModes & FX_FILEMODE_Truncate) {
- wsMode = FX_WSTRC(L"w+b");
- } else {
- wsMode = FX_WSTRC(L"a+b");
- }
-}
-CFXCRT_FileAccess_CRT::CFXCRT_FileAccess_CRT()
- : m_hFile(NULL)
-{
-}
-CFXCRT_FileAccess_CRT::~CFXCRT_FileAccess_CRT()
-{
- Close();
-}
-FX_BOOL CFXCRT_FileAccess_CRT::Open(FX_BSTR fileName, FX_DWORD dwMode)
-{
- if (m_hFile) {
- return FALSE;
- }
- CFX_ByteString strMode;
- FXCRT_GetFileModeString(dwMode, strMode);
- m_hFile = FXSYS_fopen(fileName.GetCStr(), (FX_LPCSTR)strMode);
- return m_hFile != NULL;
-}
-FX_BOOL CFXCRT_FileAccess_CRT::Open(FX_WSTR fileName, FX_DWORD dwMode)
-{
- if (m_hFile) {
- return FALSE;
- }
- CFX_WideString strMode;
- FXCRT_GetFileModeString(dwMode, strMode);
- m_hFile = FXSYS_wfopen(fileName.GetPtr(), (FX_LPCWSTR)strMode);
- return m_hFile != NULL;
-}
-void CFXCRT_FileAccess_CRT::Close()
-{
- if (!m_hFile) {
- return;
- }
- FXSYS_fclose(m_hFile);
- m_hFile = NULL;
-}
-void CFXCRT_FileAccess_CRT::Release(IFX_Allocator* pAllocator)
-{
- if (pAllocator) {
- FX_DeleteAtAllocator(this, pAllocator, CFXCRT_FileAccess_CRT);
- } else {
- delete this;
- }
-}
-FX_FILESIZE CFXCRT_FileAccess_CRT::GetSize() const
-{
- if (!m_hFile) {
- return 0;
- }
- FX_FILESIZE pos = (FX_FILESIZE)FXSYS_ftell(m_hFile);
- FXSYS_fseek(m_hFile, 0, FXSYS_SEEK_END);
- FX_FILESIZE size = (FX_FILESIZE)FXSYS_ftell(m_hFile);
- FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
- return size;
-}
-FX_FILESIZE CFXCRT_FileAccess_CRT::GetPosition() const
-{
- if (!m_hFile) {
- return (FX_FILESIZE) - 1;
- }
- return (FX_FILESIZE)FXSYS_ftell(m_hFile);
-}
-FX_FILESIZE CFXCRT_FileAccess_CRT::SetPosition(FX_FILESIZE pos)
-{
- if (!m_hFile) {
- return (FX_FILESIZE) - 1;
- }
- FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
- return (FX_FILESIZE)FXSYS_ftell(m_hFile);
-}
-size_t CFXCRT_FileAccess_CRT::Read(void* pBuffer, size_t szBuffer)
-{
- if (!m_hFile) {
- return 0;
- }
- return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile);
-}
-size_t CFXCRT_FileAccess_CRT::Write(const void* pBuffer, size_t szBuffer)
-{
- if (!m_hFile) {
- return 0;
- }
- return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile);
-}
-size_t CFXCRT_FileAccess_CRT::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
-{
- if (!m_hFile) {
- return (FX_FILESIZE) - 1;
- }
- FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
- return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile);
-}
-size_t CFXCRT_FileAccess_CRT::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
-{
- if (!m_hFile) {
- return (FX_FILESIZE) - 1;
- }
- FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET);
- return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile);
-}
-FX_BOOL CFXCRT_FileAccess_CRT::Flush()
-{
- if (!m_hFile) {
- return FALSE;
- }
- return !FXSYS_fflush(m_hFile);
-}
-FX_BOOL CFXCRT_FileAccess_CRT::Truncate(FX_FILESIZE szFile)
-{
- return FALSE;
-}
-FX_BOOL FX_File_Exist(FX_BSTR fileName)
-{
- return access(fileName.GetCStr(), F_OK) > -1;
-}
-FX_BOOL FX_File_Exist(FX_WSTR fileName)
-{
- return FX_File_Exist(FX_UTF8Encode(fileName));
-}
-FX_BOOL FX_File_Delete(FX_BSTR fileName)
-{
- return remove(fileName.GetCStr()) > -1;
-}
-FX_BOOL FX_File_Delete(FX_WSTR fileName)
-{
- return FX_File_Delete(FX_UTF8Encode(fileName));
-}
-FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
-{
- CFXCRT_FileAccess_CRT src, dst;
- if (!src.Open(fileNameSrc, FX_FILEMODE_ReadOnly)) {
- return FALSE;
- }
- FX_FILESIZE size = src.GetSize();
- if (!size) {
- return FALSE;
- }
- if (!dst.Open(fileNameDst, FX_FILEMODE_Truncate)) {
- return FALSE;
- }
- FX_FILESIZE num = 0;
- FX_LPBYTE pBuffer = FX_Alloc(FX_BYTE, 32768);
- if (!pBuffer) {
- return FALSE;
- }
- while (num = src.Read(pBuffer, 32768)) {
- if (dst.Write(pBuffer, num) != num) {
- break;
- }
- }
- FX_Free(pBuffer);
- return TRUE;
-}
-FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
-{
- return FX_File_Copy(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst));
-}
-FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
-{
- return rename(fileNameSrc.GetCStr(), fileNameDst.GetCStr());
-}
-FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
-{
- return FX_File_Move(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst));
-}
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_ext.h" +#include "fxcrt_platforms.h" +#if (_FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ && _FXM_PLATFORM_ != _FXM_PLATFORM_LINUX_ && _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ && _FXM_PLATFORM_ != _FXM_PLATFORM_ANDROID_) +IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator) +{ + if (pAllocator) { + return FX_NewAtAllocator(pAllocator) CFXCRT_FileAccess_CRT; + } else { + return FX_NEW CFXCRT_FileAccess_CRT; + } +} +void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_ByteString &bsMode) +{ + if (dwModes & FX_FILEMODE_ReadOnly) { + bsMode = FX_BSTRC("rb"); + } else if (dwModes & FX_FILEMODE_Truncate) { + bsMode = FX_BSTRC("w+b"); + } else { + bsMode = FX_BSTRC("a+b"); + } +} +void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_WideString &wsMode) +{ + if (dwModes & FX_FILEMODE_ReadOnly) { + wsMode = FX_WSTRC(L"rb"); + } else if (dwModes & FX_FILEMODE_Truncate) { + wsMode = FX_WSTRC(L"w+b"); + } else { + wsMode = FX_WSTRC(L"a+b"); + } +} +CFXCRT_FileAccess_CRT::CFXCRT_FileAccess_CRT() + : m_hFile(NULL) +{ +} +CFXCRT_FileAccess_CRT::~CFXCRT_FileAccess_CRT() +{ + Close(); +} +FX_BOOL CFXCRT_FileAccess_CRT::Open(FX_BSTR fileName, FX_DWORD dwMode) +{ + if (m_hFile) { + return FALSE; + } + CFX_ByteString strMode; + FXCRT_GetFileModeString(dwMode, strMode); + m_hFile = FXSYS_fopen(fileName.GetCStr(), (FX_LPCSTR)strMode); + return m_hFile != NULL; +} +FX_BOOL CFXCRT_FileAccess_CRT::Open(FX_WSTR fileName, FX_DWORD dwMode) +{ + if (m_hFile) { + return FALSE; + } + CFX_WideString strMode; + FXCRT_GetFileModeString(dwMode, strMode); + m_hFile = FXSYS_wfopen(fileName.GetPtr(), (FX_LPCWSTR)strMode); + return m_hFile != NULL; +} +void CFXCRT_FileAccess_CRT::Close() +{ + if (!m_hFile) { + return; + } + FXSYS_fclose(m_hFile); + m_hFile = NULL; +} +void CFXCRT_FileAccess_CRT::Release(IFX_Allocator* pAllocator) +{ + if (pAllocator) { + FX_DeleteAtAllocator(this, pAllocator, CFXCRT_FileAccess_CRT); + } else { + delete this; + } +} +FX_FILESIZE CFXCRT_FileAccess_CRT::GetSize() const +{ + if (!m_hFile) { + return 0; + } + FX_FILESIZE pos = (FX_FILESIZE)FXSYS_ftell(m_hFile); + FXSYS_fseek(m_hFile, 0, FXSYS_SEEK_END); + FX_FILESIZE size = (FX_FILESIZE)FXSYS_ftell(m_hFile); + FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); + return size; +} +FX_FILESIZE CFXCRT_FileAccess_CRT::GetPosition() const +{ + if (!m_hFile) { + return (FX_FILESIZE) - 1; + } + return (FX_FILESIZE)FXSYS_ftell(m_hFile); +} +FX_FILESIZE CFXCRT_FileAccess_CRT::SetPosition(FX_FILESIZE pos) +{ + if (!m_hFile) { + return (FX_FILESIZE) - 1; + } + FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); + return (FX_FILESIZE)FXSYS_ftell(m_hFile); +} +size_t CFXCRT_FileAccess_CRT::Read(void* pBuffer, size_t szBuffer) +{ + if (!m_hFile) { + return 0; + } + return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile); +} +size_t CFXCRT_FileAccess_CRT::Write(const void* pBuffer, size_t szBuffer) +{ + if (!m_hFile) { + return 0; + } + return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile); +} +size_t CFXCRT_FileAccess_CRT::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) +{ + if (!m_hFile) { + return (FX_FILESIZE) - 1; + } + FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); + return FXSYS_fread(pBuffer, 1, szBuffer, m_hFile); +} +size_t CFXCRT_FileAccess_CRT::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos) +{ + if (!m_hFile) { + return (FX_FILESIZE) - 1; + } + FXSYS_fseek(m_hFile, pos, FXSYS_SEEK_SET); + return FXSYS_fwrite(pBuffer, 1, szBuffer, m_hFile); +} +FX_BOOL CFXCRT_FileAccess_CRT::Flush() +{ + if (!m_hFile) { + return FALSE; + } + return !FXSYS_fflush(m_hFile); +} +FX_BOOL CFXCRT_FileAccess_CRT::Truncate(FX_FILESIZE szFile) +{ + return FALSE; +} +FX_BOOL FX_File_Exist(FX_BSTR fileName) +{ + return access(fileName.GetCStr(), F_OK) > -1; +} +FX_BOOL FX_File_Exist(FX_WSTR fileName) +{ + return FX_File_Exist(FX_UTF8Encode(fileName)); +} +FX_BOOL FX_File_Delete(FX_BSTR fileName) +{ + return remove(fileName.GetCStr()) > -1; +} +FX_BOOL FX_File_Delete(FX_WSTR fileName) +{ + return FX_File_Delete(FX_UTF8Encode(fileName)); +} +FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) +{ + CFXCRT_FileAccess_CRT src, dst; + if (!src.Open(fileNameSrc, FX_FILEMODE_ReadOnly)) { + return FALSE; + } + FX_FILESIZE size = src.GetSize(); + if (!size) { + return FALSE; + } + if (!dst.Open(fileNameDst, FX_FILEMODE_Truncate)) { + return FALSE; + } + FX_FILESIZE num = 0; + FX_LPBYTE pBuffer = FX_Alloc(FX_BYTE, 32768); + if (!pBuffer) { + return FALSE; + } + while (num = src.Read(pBuffer, 32768)) { + if (dst.Write(pBuffer, num) != num) { + break; + } + } + FX_Free(pBuffer); + return TRUE; +} +FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) +{ + return FX_File_Copy(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); +} +FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) +{ + return rename(fileNameSrc.GetCStr(), fileNameDst.GetCStr()); +} +FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) +{ + return FX_File_Move(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); +} +#endif diff --git a/core/src/fxcrt/fxcrt_platforms.h b/core/src/fxcrt/fxcrt_platforms.h index 633b888fe7..4cb839961d 100644 --- a/core/src/fxcrt/fxcrt_platforms.h +++ b/core/src/fxcrt/fxcrt_platforms.h @@ -1,35 +1,35 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _FXCRT_PLATFORMS_
-#define _FXCRT_PLATFORMS_
-#include "extension.h"
-#if _FX_OS_ == _FX_ANDROID_
-void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_ByteString &bsMode);
-void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_WideString &wsMode);
-class CFXCRT_FileAccess_CRT : public IFXCRT_FileAccess, public CFX_Object
-{
-public:
- CFXCRT_FileAccess_CRT();
- virtual ~CFXCRT_FileAccess_CRT();
- virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode);
- virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode);
- virtual void Close();
- virtual void Release(IFX_Allocator* pAllocator = NULL);
- virtual FX_FILESIZE GetSize() const;
- virtual FX_FILESIZE GetPosition() const;
- virtual FX_FILESIZE SetPosition(FX_FILESIZE pos);
- virtual size_t Read(void* pBuffer, size_t szBuffer);
- virtual size_t Write(const void* pBuffer, size_t szBuffer);
- virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos);
- virtual size_t WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos);
- virtual FX_BOOL Flush();
- virtual FX_BOOL Truncate(FX_FILESIZE szFile);
-protected:
- FXSYS_FILE* m_hFile;
-};
-#endif
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _FXCRT_PLATFORMS_ +#define _FXCRT_PLATFORMS_ +#include "extension.h" +#if _FX_OS_ == _FX_ANDROID_ +void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_ByteString &bsMode); +void FXCRT_GetFileModeString(FX_DWORD dwModes, CFX_WideString &wsMode); +class CFXCRT_FileAccess_CRT : public IFXCRT_FileAccess, public CFX_Object +{ +public: + CFXCRT_FileAccess_CRT(); + virtual ~CFXCRT_FileAccess_CRT(); + virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode); + virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode); + virtual void Close(); + virtual void Release(IFX_Allocator* pAllocator = NULL); + virtual FX_FILESIZE GetSize() const; + virtual FX_FILESIZE GetPosition() const; + virtual FX_FILESIZE SetPosition(FX_FILESIZE pos); + virtual size_t Read(void* pBuffer, size_t szBuffer); + virtual size_t Write(const void* pBuffer, size_t szBuffer); + virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos); + virtual size_t WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos); + virtual FX_BOOL Flush(); + virtual FX_BOOL Truncate(FX_FILESIZE szFile); +protected: + FXSYS_FILE* m_hFile; +}; +#endif +#endif diff --git a/core/src/fxcrt/fxcrt_posix.cpp b/core/src/fxcrt/fxcrt_posix.cpp index 6752e0a117..558f33466b 100644 --- a/core/src/fxcrt/fxcrt_posix.cpp +++ b/core/src/fxcrt/fxcrt_posix.cpp @@ -1,201 +1,201 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_ext.h"
-#include "fxcrt_posix.h"
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_
-IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator)
-{
- if (pAllocator) {
- return FX_NewAtAllocator(pAllocator) CFXCRT_FileAccess_Posix();
- } else {
- return FX_NEW CFXCRT_FileAccess_Posix;
- }
-}
-void FXCRT_Posix_GetFileMode(FX_DWORD dwModes, FX_INT32 &nFlags, FX_INT32 &nMasks)
-{
- nFlags = O_BINARY | O_LARGEFILE;
- if (dwModes & FX_FILEMODE_ReadOnly) {
- nFlags |= O_RDONLY;
- nMasks = 0;
- } else {
- nFlags |= O_RDWR | O_CREAT;
- if (dwModes & FX_FILEMODE_Truncate) {
- nFlags |= O_TRUNC;
- }
- nMasks = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
- }
-}
-CFXCRT_FileAccess_Posix::CFXCRT_FileAccess_Posix()
- : m_nFD(-1)
-{
-}
-CFXCRT_FileAccess_Posix::~CFXCRT_FileAccess_Posix()
-{
- Close();
-}
-FX_BOOL CFXCRT_FileAccess_Posix::Open(FX_BSTR fileName, FX_DWORD dwMode)
-{
- if (m_nFD > -1) {
- return FALSE;
- }
- FX_INT32 nFlags, nMasks;
- FXCRT_Posix_GetFileMode(dwMode, nFlags, nMasks);
- m_nFD = open(fileName.GetCStr(), nFlags, nMasks);
- return m_nFD > -1;
-}
-FX_BOOL CFXCRT_FileAccess_Posix::Open(FX_WSTR fileName, FX_DWORD dwMode)
-{
- return Open(FX_UTF8Encode(fileName), dwMode);
-}
-void CFXCRT_FileAccess_Posix::Close()
-{
- if (m_nFD < 0) {
- return;
- }
- close(m_nFD);
- m_nFD = -1;
-}
-void CFXCRT_FileAccess_Posix::Release(IFX_Allocator* pAllocator)
-{
- if (pAllocator) {
- FX_DeleteAtAllocator(this, pAllocator, CFXCRT_FileAccess_Posix);
- } else {
- delete this;
- }
-}
-FX_FILESIZE CFXCRT_FileAccess_Posix::GetSize() const
-{
- if (m_nFD < 0) {
- return 0;
- }
- struct stat s;
- FXSYS_memset32(&s, 0, sizeof(s));
- fstat(m_nFD, &s);
- return s.st_size;
-}
-FX_FILESIZE CFXCRT_FileAccess_Posix::GetPosition() const
-{
- if (m_nFD < 0) {
- return (FX_FILESIZE) - 1;
- }
- return lseek(m_nFD, 0, SEEK_CUR);
-}
-FX_FILESIZE CFXCRT_FileAccess_Posix::SetPosition(FX_FILESIZE pos)
-{
- if (m_nFD < 0) {
- return (FX_FILESIZE) - 1;
- }
- return lseek(m_nFD, pos, SEEK_SET);
-}
-size_t CFXCRT_FileAccess_Posix::Read(void* pBuffer, size_t szBuffer)
-{
- if (m_nFD < 0) {
- return 0;
- }
- return read(m_nFD, pBuffer, szBuffer);
-}
-size_t CFXCRT_FileAccess_Posix::Write(const void* pBuffer, size_t szBuffer)
-{
- if (m_nFD < 0) {
- return 0;
- }
- return write(m_nFD, pBuffer, szBuffer);
-}
-size_t CFXCRT_FileAccess_Posix::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
-{
- if (m_nFD < 0) {
- return 0;
- }
- if (pos >= GetSize()) {
- return 0;
- }
- if (SetPosition(pos) == (FX_FILESIZE) - 1) {
- return 0;
- }
- return Read(pBuffer, szBuffer);
-}
-size_t CFXCRT_FileAccess_Posix::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
-{
- if (m_nFD < 0) {
- return 0;
- }
- if (SetPosition(pos) == (FX_FILESIZE) - 1) {
- return 0;
- }
- return Write(pBuffer, szBuffer);
-}
-FX_BOOL CFXCRT_FileAccess_Posix::Flush()
-{
- if (m_nFD < 0) {
- return FALSE;
- }
- return fsync(m_nFD) > -1;
-}
-FX_BOOL CFXCRT_FileAccess_Posix::Truncate(FX_FILESIZE szFile)
-{
- if (m_nFD < 0) {
- return FALSE;
- }
- return !ftruncate(m_nFD, szFile);
-}
-FX_BOOL FX_File_Exist(FX_BSTR fileName)
-{
- return access(fileName.GetCStr(), F_OK) > -1;
-}
-FX_BOOL FX_File_Exist(FX_WSTR fileName)
-{
- return FX_File_Exist(FX_UTF8Encode(fileName));
-}
-FX_BOOL FX_File_Delete(FX_BSTR fileName)
-{
- return remove(fileName.GetCStr()) > -1;
-}
-FX_BOOL FX_File_Delete(FX_WSTR fileName)
-{
- return FX_File_Delete(FX_UTF8Encode(fileName));
-}
-FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
-{
- CFXCRT_FileAccess_Posix src, dst;
- if (!src.Open(fileNameSrc, FX_FILEMODE_ReadOnly)) {
- return FALSE;
- }
- FX_FILESIZE size = src.GetSize();
- if (!size) {
- return FALSE;
- }
- if (!dst.Open(fileNameDst, FX_FILEMODE_Truncate)) {
- return FALSE;
- }
- size_t num = 0;
- FX_LPBYTE pBuffer = FX_Alloc(FX_BYTE, 32768);
- if (!pBuffer) {
- return FALSE;
- }
- num = src.Read(pBuffer, 32768);
- while (num) {
- if (dst.Write(pBuffer, num) != num) {
- break;
- }
- num = src.Read(pBuffer, 32768);
- }
- FX_Free(pBuffer);
- return TRUE;
-}
-FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
-{
- return FX_File_Copy(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst));
-}
-FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
-{
- return rename(fileNameSrc.GetCStr(), fileNameDst.GetCStr());
-}
-FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
-{
- return FX_File_Move(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst));
-}
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_ext.h" +#include "fxcrt_posix.h" +#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ +IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator) +{ + if (pAllocator) { + return FX_NewAtAllocator(pAllocator) CFXCRT_FileAccess_Posix(); + } else { + return FX_NEW CFXCRT_FileAccess_Posix; + } +} +void FXCRT_Posix_GetFileMode(FX_DWORD dwModes, FX_INT32 &nFlags, FX_INT32 &nMasks) +{ + nFlags = O_BINARY | O_LARGEFILE; + if (dwModes & FX_FILEMODE_ReadOnly) { + nFlags |= O_RDONLY; + nMasks = 0; + } else { + nFlags |= O_RDWR | O_CREAT; + if (dwModes & FX_FILEMODE_Truncate) { + nFlags |= O_TRUNC; + } + nMasks = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; + } +} +CFXCRT_FileAccess_Posix::CFXCRT_FileAccess_Posix() + : m_nFD(-1) +{ +} +CFXCRT_FileAccess_Posix::~CFXCRT_FileAccess_Posix() +{ + Close(); +} +FX_BOOL CFXCRT_FileAccess_Posix::Open(FX_BSTR fileName, FX_DWORD dwMode) +{ + if (m_nFD > -1) { + return FALSE; + } + FX_INT32 nFlags, nMasks; + FXCRT_Posix_GetFileMode(dwMode, nFlags, nMasks); + m_nFD = open(fileName.GetCStr(), nFlags, nMasks); + return m_nFD > -1; +} +FX_BOOL CFXCRT_FileAccess_Posix::Open(FX_WSTR fileName, FX_DWORD dwMode) +{ + return Open(FX_UTF8Encode(fileName), dwMode); +} +void CFXCRT_FileAccess_Posix::Close() +{ + if (m_nFD < 0) { + return; + } + close(m_nFD); + m_nFD = -1; +} +void CFXCRT_FileAccess_Posix::Release(IFX_Allocator* pAllocator) +{ + if (pAllocator) { + FX_DeleteAtAllocator(this, pAllocator, CFXCRT_FileAccess_Posix); + } else { + delete this; + } +} +FX_FILESIZE CFXCRT_FileAccess_Posix::GetSize() const +{ + if (m_nFD < 0) { + return 0; + } + struct stat s; + FXSYS_memset32(&s, 0, sizeof(s)); + fstat(m_nFD, &s); + return s.st_size; +} +FX_FILESIZE CFXCRT_FileAccess_Posix::GetPosition() const +{ + if (m_nFD < 0) { + return (FX_FILESIZE) - 1; + } + return lseek(m_nFD, 0, SEEK_CUR); +} +FX_FILESIZE CFXCRT_FileAccess_Posix::SetPosition(FX_FILESIZE pos) +{ + if (m_nFD < 0) { + return (FX_FILESIZE) - 1; + } + return lseek(m_nFD, pos, SEEK_SET); +} +size_t CFXCRT_FileAccess_Posix::Read(void* pBuffer, size_t szBuffer) +{ + if (m_nFD < 0) { + return 0; + } + return read(m_nFD, pBuffer, szBuffer); +} +size_t CFXCRT_FileAccess_Posix::Write(const void* pBuffer, size_t szBuffer) +{ + if (m_nFD < 0) { + return 0; + } + return write(m_nFD, pBuffer, szBuffer); +} +size_t CFXCRT_FileAccess_Posix::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) +{ + if (m_nFD < 0) { + return 0; + } + if (pos >= GetSize()) { + return 0; + } + if (SetPosition(pos) == (FX_FILESIZE) - 1) { + return 0; + } + return Read(pBuffer, szBuffer); +} +size_t CFXCRT_FileAccess_Posix::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos) +{ + if (m_nFD < 0) { + return 0; + } + if (SetPosition(pos) == (FX_FILESIZE) - 1) { + return 0; + } + return Write(pBuffer, szBuffer); +} +FX_BOOL CFXCRT_FileAccess_Posix::Flush() +{ + if (m_nFD < 0) { + return FALSE; + } + return fsync(m_nFD) > -1; +} +FX_BOOL CFXCRT_FileAccess_Posix::Truncate(FX_FILESIZE szFile) +{ + if (m_nFD < 0) { + return FALSE; + } + return !ftruncate(m_nFD, szFile); +} +FX_BOOL FX_File_Exist(FX_BSTR fileName) +{ + return access(fileName.GetCStr(), F_OK) > -1; +} +FX_BOOL FX_File_Exist(FX_WSTR fileName) +{ + return FX_File_Exist(FX_UTF8Encode(fileName)); +} +FX_BOOL FX_File_Delete(FX_BSTR fileName) +{ + return remove(fileName.GetCStr()) > -1; +} +FX_BOOL FX_File_Delete(FX_WSTR fileName) +{ + return FX_File_Delete(FX_UTF8Encode(fileName)); +} +FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) +{ + CFXCRT_FileAccess_Posix src, dst; + if (!src.Open(fileNameSrc, FX_FILEMODE_ReadOnly)) { + return FALSE; + } + FX_FILESIZE size = src.GetSize(); + if (!size) { + return FALSE; + } + if (!dst.Open(fileNameDst, FX_FILEMODE_Truncate)) { + return FALSE; + } + size_t num = 0; + FX_LPBYTE pBuffer = FX_Alloc(FX_BYTE, 32768); + if (!pBuffer) { + return FALSE; + } + num = src.Read(pBuffer, 32768); + while (num) { + if (dst.Write(pBuffer, num) != num) { + break; + } + num = src.Read(pBuffer, 32768); + } + FX_Free(pBuffer); + return TRUE; +} +FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) +{ + return FX_File_Copy(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); +} +FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) +{ + return rename(fileNameSrc.GetCStr(), fileNameDst.GetCStr()); +} +FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) +{ + return FX_File_Move(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); +} +#endif diff --git a/core/src/fxcrt/fxcrt_posix.h b/core/src/fxcrt/fxcrt_posix.h index 29c84adb1f..f76e35cdd1 100644 --- a/core/src/fxcrt/fxcrt_posix.h +++ b/core/src/fxcrt/fxcrt_posix.h @@ -1,33 +1,33 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _FXCRT_LINUX_
-#define _FXCRT_LINUX_
-#include "extension.h"
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_
-class CFXCRT_FileAccess_Posix : public IFXCRT_FileAccess, public CFX_Object
-{
-public:
- CFXCRT_FileAccess_Posix();
- virtual ~CFXCRT_FileAccess_Posix();
- virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode);
- virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode);
- virtual void Close();
- virtual void Release(IFX_Allocator* pAllocator = NULL);
- virtual FX_FILESIZE GetSize() const;
- virtual FX_FILESIZE GetPosition() const;
- virtual FX_FILESIZE SetPosition(FX_FILESIZE pos);
- virtual size_t Read(void* pBuffer, size_t szBuffer);
- virtual size_t Write(const void* pBuffer, size_t szBuffer);
- virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos);
- virtual size_t WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos);
- virtual FX_BOOL Flush();
- virtual FX_BOOL Truncate(FX_FILESIZE szFile);
-protected:
- FX_INT32 m_nFD;
-};
-#endif
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _FXCRT_LINUX_ +#define _FXCRT_LINUX_ +#include "extension.h" +#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ +class CFXCRT_FileAccess_Posix : public IFXCRT_FileAccess, public CFX_Object +{ +public: + CFXCRT_FileAccess_Posix(); + virtual ~CFXCRT_FileAccess_Posix(); + virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode); + virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode); + virtual void Close(); + virtual void Release(IFX_Allocator* pAllocator = NULL); + virtual FX_FILESIZE GetSize() const; + virtual FX_FILESIZE GetPosition() const; + virtual FX_FILESIZE SetPosition(FX_FILESIZE pos); + virtual size_t Read(void* pBuffer, size_t szBuffer); + virtual size_t Write(const void* pBuffer, size_t szBuffer); + virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos); + virtual size_t WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos); + virtual FX_BOOL Flush(); + virtual FX_BOOL Truncate(FX_FILESIZE szFile); +protected: + FX_INT32 m_nFD; +}; +#endif +#endif diff --git a/core/src/fxcrt/fxcrt_windows.cpp b/core/src/fxcrt/fxcrt_windows.cpp index e03bfda354..9b77b33ca2 100644 --- a/core/src/fxcrt/fxcrt_windows.cpp +++ b/core/src/fxcrt/fxcrt_windows.cpp @@ -1,222 +1,222 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../include/fxcrt/fx_ext.h"
-#include "fxcrt_windows.h"
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
-FX_BOOL FX_File_Exist(FX_BSTR fileName)
-{
- FX_DWORD dwAttri = ::GetFileAttributesA(fileName.GetCStr());
- if (dwAttri == -1) {
- return FALSE;
- }
- return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0;
-}
-FX_BOOL FX_File_Exist(FX_WSTR fileName)
-{
- FX_DWORD dwAttri = ::GetFileAttributesW((LPCWSTR)fileName.GetPtr());
- if (dwAttri == -1) {
- return FALSE;
- }
- return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0;
-}
-IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator)
-{
- if (pAllocator) {
- return FX_NewAtAllocator(pAllocator) CFXCRT_FileAccess_Win64;
- } else {
- return FX_NEW CFXCRT_FileAccess_Win64;
- }
-}
-void FXCRT_Windows_GetFileMode(FX_DWORD dwMode, FX_DWORD &dwAccess, FX_DWORD &dwShare, FX_DWORD &dwCreation)
-{
- dwAccess = GENERIC_READ;
- dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
- if (!(dwMode & FX_FILEMODE_ReadOnly)) {
- dwAccess |= GENERIC_WRITE;
- dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS;
- } else {
- dwCreation = OPEN_EXISTING;
- }
-}
-#ifdef __cplusplus
-extern "C" {
-#endif
-WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize);
-WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod);
-#ifdef __cplusplus
-}
-#endif
-CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64()
- : m_hFile(NULL)
-{
-}
-CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64()
-{
- Close();
-}
-FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_BSTR fileName, FX_DWORD dwMode)
-{
- if (m_hFile) {
- return FALSE;
- }
- FX_DWORD dwAccess, dwShare, dwCreation;
- FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
- m_hFile = ::CreateFileA(fileName.GetCStr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL);
- if (m_hFile == INVALID_HANDLE_VALUE) {
- m_hFile = NULL;
- }
- return m_hFile != NULL;
-}
-FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_WSTR fileName, FX_DWORD dwMode)
-{
- if (m_hFile) {
- return FALSE;
- }
- FX_DWORD dwAccess, dwShare, dwCreation;
- FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation);
- m_hFile = ::CreateFileW((LPCWSTR)fileName.GetPtr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL);
- if (m_hFile == INVALID_HANDLE_VALUE) {
- m_hFile = NULL;
- }
- return m_hFile != NULL;
-}
-void CFXCRT_FileAccess_Win64::Close()
-{
- if (!m_hFile) {
- return;
- }
- ::CloseHandle(m_hFile);
- m_hFile = NULL;
-}
-void CFXCRT_FileAccess_Win64::Release(IFX_Allocator* pAllocator)
-{
- if (pAllocator) {
- FX_DeleteAtAllocator(this, pAllocator, CFXCRT_FileAccess_Win64);
- } else {
- delete this;
- }
-}
-FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const
-{
- if (!m_hFile) {
- return 0;
- }
- LARGE_INTEGER size = {0, 0};
- if (!::GetFileSizeEx(m_hFile, &size)) {
- return 0;
- }
- return (FX_FILESIZE)size.QuadPart;
-}
-FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const
-{
- if (!m_hFile) {
- return (FX_FILESIZE) - 1;
- }
- LARGE_INTEGER dist = {0, 0};
- LARGE_INTEGER newPos = {0, 0};
- if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) {
- return (FX_FILESIZE) - 1;
- }
- return (FX_FILESIZE)newPos.QuadPart;
-}
-FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos)
-{
- if (!m_hFile) {
- return (FX_FILESIZE) - 1;
- }
- LARGE_INTEGER dist;
- dist.QuadPart = pos;
- LARGE_INTEGER newPos = {0, 0};
- if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) {
- return (FX_FILESIZE) - 1;
- }
- return (FX_FILESIZE)newPos.QuadPart;
-}
-size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer)
-{
- if (!m_hFile) {
- return 0;
- }
- size_t szRead = 0;
- if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, NULL)) {
- return 0;
- }
- return szRead;
-}
-size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer)
-{
- if (!m_hFile) {
- return 0;
- }
- size_t szWrite = 0;
- if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, NULL)) {
- return 0;
- }
- return szWrite;
-}
-size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
-{
- if (!m_hFile) {
- return 0;
- }
- if (pos >= GetSize()) {
- return 0;
- }
- if (SetPosition(pos) == (FX_FILESIZE) - 1) {
- return 0;
- }
- return Read(pBuffer, szBuffer);
-}
-size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos)
-{
- if (!m_hFile) {
- return 0;
- }
- if (SetPosition(pos) == (FX_FILESIZE) - 1) {
- return 0;
- }
- return Write(pBuffer, szBuffer);
-}
-FX_BOOL CFXCRT_FileAccess_Win64::Flush()
-{
- if (!m_hFile) {
- return FALSE;
- }
- return ::FlushFileBuffers(m_hFile);
-}
-FX_BOOL CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile)
-{
- if (SetPosition(szFile) == (FX_FILESIZE) - 1) {
- return FALSE;
- }
- return ::SetEndOfFile(m_hFile);
-}
-FX_BOOL FX_File_Delete(FX_BSTR fileName)
-{
- return ::DeleteFileA(fileName.GetCStr());
-}
-FX_BOOL FX_File_Delete(FX_WSTR fileName)
-{
- return ::DeleteFileW((LPCWSTR)fileName.GetPtr());
-}
-FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
-{
- return ::CopyFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr(), FALSE);
-}
-FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
-{
- return ::CopyFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr(), FALSE);
-}
-FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst)
-{
- return ::MoveFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr());
-}
-FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst)
-{
- return ::MoveFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr());
-}
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "../../include/fxcrt/fx_ext.h" +#include "fxcrt_windows.h" +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +FX_BOOL FX_File_Exist(FX_BSTR fileName) +{ + FX_DWORD dwAttri = ::GetFileAttributesA(fileName.GetCStr()); + if (dwAttri == -1) { + return FALSE; + } + return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0; +} +FX_BOOL FX_File_Exist(FX_WSTR fileName) +{ + FX_DWORD dwAttri = ::GetFileAttributesW((LPCWSTR)fileName.GetPtr()); + if (dwAttri == -1) { + return FALSE; + } + return (dwAttri & FILE_ATTRIBUTE_DIRECTORY) == 0; +} +IFXCRT_FileAccess* FXCRT_FileAccess_Create(IFX_Allocator* pAllocator) +{ + if (pAllocator) { + return FX_NewAtAllocator(pAllocator) CFXCRT_FileAccess_Win64; + } else { + return FX_NEW CFXCRT_FileAccess_Win64; + } +} +void FXCRT_Windows_GetFileMode(FX_DWORD dwMode, FX_DWORD &dwAccess, FX_DWORD &dwShare, FX_DWORD &dwCreation) +{ + dwAccess = GENERIC_READ; + dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE; + if (!(dwMode & FX_FILEMODE_ReadOnly)) { + dwAccess |= GENERIC_WRITE; + dwCreation = (dwMode & FX_FILEMODE_Truncate) ? CREATE_ALWAYS : OPEN_ALWAYS; + } else { + dwCreation = OPEN_EXISTING; + } +} +#ifdef __cplusplus +extern "C" { +#endif +WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize); +WINBASEAPI BOOL WINAPI SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod); +#ifdef __cplusplus +} +#endif +CFXCRT_FileAccess_Win64::CFXCRT_FileAccess_Win64() + : m_hFile(NULL) +{ +} +CFXCRT_FileAccess_Win64::~CFXCRT_FileAccess_Win64() +{ + Close(); +} +FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_BSTR fileName, FX_DWORD dwMode) +{ + if (m_hFile) { + return FALSE; + } + FX_DWORD dwAccess, dwShare, dwCreation; + FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); + m_hFile = ::CreateFileA(fileName.GetCStr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL); + if (m_hFile == INVALID_HANDLE_VALUE) { + m_hFile = NULL; + } + return m_hFile != NULL; +} +FX_BOOL CFXCRT_FileAccess_Win64::Open(FX_WSTR fileName, FX_DWORD dwMode) +{ + if (m_hFile) { + return FALSE; + } + FX_DWORD dwAccess, dwShare, dwCreation; + FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); + m_hFile = ::CreateFileW((LPCWSTR)fileName.GetPtr(), dwAccess, dwShare, NULL, dwCreation, FILE_ATTRIBUTE_NORMAL, NULL); + if (m_hFile == INVALID_HANDLE_VALUE) { + m_hFile = NULL; + } + return m_hFile != NULL; +} +void CFXCRT_FileAccess_Win64::Close() +{ + if (!m_hFile) { + return; + } + ::CloseHandle(m_hFile); + m_hFile = NULL; +} +void CFXCRT_FileAccess_Win64::Release(IFX_Allocator* pAllocator) +{ + if (pAllocator) { + FX_DeleteAtAllocator(this, pAllocator, CFXCRT_FileAccess_Win64); + } else { + delete this; + } +} +FX_FILESIZE CFXCRT_FileAccess_Win64::GetSize() const +{ + if (!m_hFile) { + return 0; + } + LARGE_INTEGER size = {0, 0}; + if (!::GetFileSizeEx(m_hFile, &size)) { + return 0; + } + return (FX_FILESIZE)size.QuadPart; +} +FX_FILESIZE CFXCRT_FileAccess_Win64::GetPosition() const +{ + if (!m_hFile) { + return (FX_FILESIZE) - 1; + } + LARGE_INTEGER dist = {0, 0}; + LARGE_INTEGER newPos = {0, 0}; + if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_CURRENT)) { + return (FX_FILESIZE) - 1; + } + return (FX_FILESIZE)newPos.QuadPart; +} +FX_FILESIZE CFXCRT_FileAccess_Win64::SetPosition(FX_FILESIZE pos) +{ + if (!m_hFile) { + return (FX_FILESIZE) - 1; + } + LARGE_INTEGER dist; + dist.QuadPart = pos; + LARGE_INTEGER newPos = {0, 0}; + if (!::SetFilePointerEx(m_hFile, dist, &newPos, FILE_BEGIN)) { + return (FX_FILESIZE) - 1; + } + return (FX_FILESIZE)newPos.QuadPart; +} +size_t CFXCRT_FileAccess_Win64::Read(void* pBuffer, size_t szBuffer) +{ + if (!m_hFile) { + return 0; + } + size_t szRead = 0; + if (!::ReadFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szRead, NULL)) { + return 0; + } + return szRead; +} +size_t CFXCRT_FileAccess_Win64::Write(const void* pBuffer, size_t szBuffer) +{ + if (!m_hFile) { + return 0; + } + size_t szWrite = 0; + if (!::WriteFile(m_hFile, pBuffer, (DWORD)szBuffer, (LPDWORD)&szWrite, NULL)) { + return 0; + } + return szWrite; +} +size_t CFXCRT_FileAccess_Win64::ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos) +{ + if (!m_hFile) { + return 0; + } + if (pos >= GetSize()) { + return 0; + } + if (SetPosition(pos) == (FX_FILESIZE) - 1) { + return 0; + } + return Read(pBuffer, szBuffer); +} +size_t CFXCRT_FileAccess_Win64::WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos) +{ + if (!m_hFile) { + return 0; + } + if (SetPosition(pos) == (FX_FILESIZE) - 1) { + return 0; + } + return Write(pBuffer, szBuffer); +} +FX_BOOL CFXCRT_FileAccess_Win64::Flush() +{ + if (!m_hFile) { + return FALSE; + } + return ::FlushFileBuffers(m_hFile); +} +FX_BOOL CFXCRT_FileAccess_Win64::Truncate(FX_FILESIZE szFile) +{ + if (SetPosition(szFile) == (FX_FILESIZE) - 1) { + return FALSE; + } + return ::SetEndOfFile(m_hFile); +} +FX_BOOL FX_File_Delete(FX_BSTR fileName) +{ + return ::DeleteFileA(fileName.GetCStr()); +} +FX_BOOL FX_File_Delete(FX_WSTR fileName) +{ + return ::DeleteFileW((LPCWSTR)fileName.GetPtr()); +} +FX_BOOL FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) +{ + return ::CopyFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr(), FALSE); +} +FX_BOOL FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) +{ + return ::CopyFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr(), FALSE); +} +FX_BOOL FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst) +{ + return ::MoveFileA(fileNameSrc.GetCStr(), fileNameDst.GetCStr()); +} +FX_BOOL FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst) +{ + return ::MoveFileW((LPCWSTR)fileNameSrc.GetPtr(), (LPCWSTR)fileNameDst.GetPtr()); +} +#endif diff --git a/core/src/fxcrt/fxcrt_windows.h b/core/src/fxcrt/fxcrt_windows.h index beb684d067..9c2e428782 100644 --- a/core/src/fxcrt/fxcrt_windows.h +++ b/core/src/fxcrt/fxcrt_windows.h @@ -1,33 +1,33 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _FXCRT_WINDOWS_
-#define _FXCRT_WINDOWS_
-#include "extension.h"
-#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
-class CFXCRT_FileAccess_Win64 : public IFXCRT_FileAccess, public CFX_Object
-{
-public:
- CFXCRT_FileAccess_Win64();
- virtual ~CFXCRT_FileAccess_Win64();
- virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode);
- virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode);
- virtual void Close();
- virtual void Release(IFX_Allocator* pAllocator = NULL);
- virtual FX_FILESIZE GetSize() const;
- virtual FX_FILESIZE GetPosition() const;
- virtual FX_FILESIZE SetPosition(FX_FILESIZE pos);
- virtual size_t Read(void* pBuffer, size_t szBuffer);
- virtual size_t Write(const void* pBuffer, size_t szBuffer);
- virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos);
- virtual size_t WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos);
- virtual FX_BOOL Flush();
- virtual FX_BOOL Truncate(FX_FILESIZE szFile);
-protected:
- FX_LPVOID m_hFile;
-};
-#endif
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _FXCRT_WINDOWS_ +#define _FXCRT_WINDOWS_ +#include "extension.h" +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +class CFXCRT_FileAccess_Win64 : public IFXCRT_FileAccess, public CFX_Object +{ +public: + CFXCRT_FileAccess_Win64(); + virtual ~CFXCRT_FileAccess_Win64(); + virtual FX_BOOL Open(FX_BSTR fileName, FX_DWORD dwMode); + virtual FX_BOOL Open(FX_WSTR fileName, FX_DWORD dwMode); + virtual void Close(); + virtual void Release(IFX_Allocator* pAllocator = NULL); + virtual FX_FILESIZE GetSize() const; + virtual FX_FILESIZE GetPosition() const; + virtual FX_FILESIZE SetPosition(FX_FILESIZE pos); + virtual size_t Read(void* pBuffer, size_t szBuffer); + virtual size_t Write(const void* pBuffer, size_t szBuffer); + virtual size_t ReadPos(void* pBuffer, size_t szBuffer, FX_FILESIZE pos); + virtual size_t WritePos(const void* pBuffer, size_t szBuffer, FX_FILESIZE pos); + virtual FX_BOOL Flush(); + virtual FX_BOOL Truncate(FX_FILESIZE szFile); +protected: + FX_LPVOID m_hFile; +}; +#endif +#endif diff --git a/core/src/fxcrt/mem_int.h b/core/src/fxcrt/mem_int.h index da43023bb5..3e3d25a7ec 100644 --- a/core/src/fxcrt/mem_int.h +++ b/core/src/fxcrt/mem_int.h @@ -1,232 +1,232 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _FXM_MEM_INT_H_
-#define _FXM_MEM_INT_H_
-struct FX_DefAllocator {
- IFX_Allocator m_Allocator;
- struct CFX_MemoryMgr* m_pFoxitMgr;
-};
-struct CFX_MemoryMgr {
-public:
- FXMEM_SystemMgr* m_pSystemMgr;
- FX_DefAllocator m_DefAllocator;
- FX_LPVOID m_pExternalMemory;
- FX_BOOL m_bReleaseMgr;
- void Init(FXMEM_SystemMgr* pSystemMgr);
- void* Alloc(size_t size, int flags);
- void* AllocDebug(size_t size, int flags, FX_LPCSTR file, int line);
- void* Realloc(void* p, size_t size, int flags);
- void* ReallocDebug(void* p, size_t size, int flags, FX_LPCSTR file, int line);
- void Free(void* p, int flags);
- void PurgeMgr();
-};
-extern CFX_MemoryMgr* g_pDefFoxitMgr;
-#define FIXEDMEM_PAGE_EXTRASPACE sizeof(size_t)
-#define FIXEDMEM_BLOCKNUM(bs) (8 * (FX_FIXEDMEM_PAGESIZE - FIXEDMEM_PAGE_EXTRASPACE) / (8 * bs + 1))
-#define FIXEDMEM_8BYTES_BLOCKNUM FIXEDMEM_BLOCKNUM(8)
-#define FIXEDMEM_16BYTES_BLOCKNUM FIXEDMEM_BLOCKNUM(16)
-#define FIXEDMEM_32BYTES_BLOCKNUM FIXEDMEM_BLOCKNUM(32)
-extern const FX_BYTE ZeroLeadPos[256];
-extern const FX_BYTE OneLeadPos[256];
-template <size_t blockNum, size_t blockSize>
-class CFXMEM_FixedPage
-{
-public:
- void Initialize()
- {
- m_nAvailCount = blockNum;
- FXSYS_memset32(m_BusyMap, 0, (blockNum + 7) / 8);
- }
- FX_BOOL HasFreeBlock() const
- {
- return (FX_BOOL)m_nAvailCount;
- }
- FX_LPVOID Alloc(size_t size)
- {
- FXSYS_assert(m_nAvailCount);
- FX_LPDWORD pFind = (FX_LPDWORD)m_BusyMap;
- size_t i = 0;
- while (i < (blockNum + 7) / 8 / 4 && pFind[i] == 0xFFFFFFFF) {
- i ++;
- }
- i *= 4;
- while (m_BusyMap[i] == 0xFF) {
- i ++;
- }
- size_t pos = ZeroLeadPos[m_BusyMap[i]];
- m_BusyMap[i] |= 1 << (7 - pos);
- m_nAvailCount --;
- return (FX_LPBYTE)(this + 1) + (i * 8 + pos) * blockSize;
- }
- void Free(FX_LPVOID p)
- {
- FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)((FX_LPBYTE)this + FX_FIXEDMEM_PAGESIZE));
- size_t pos = ((FX_LPBYTE)p - (FX_LPBYTE)(this + 1)) / blockSize;
- m_BusyMap[pos / 8] &= ~(1 << (7 - (pos % 8)));
- m_nAvailCount ++;
- }
- volatile size_t m_nAvailCount;
- FX_BYTE m_BusyMap[(blockNum + 7) / 8];
-};
-typedef CFXMEM_FixedPage<FIXEDMEM_8BYTES_BLOCKNUM, 8> CFXMEM_8BytesPage;
-typedef CFXMEM_FixedPage<FIXEDMEM_16BYTES_BLOCKNUM, 16> CFXMEM_16BytesPage;
-typedef CFXMEM_FixedPage<FIXEDMEM_32BYTES_BLOCKNUM, 32> CFXMEM_32BytesPage;
-template <size_t blockNum, size_t blockSize>
-class CFXMEM_FixedPages
-{
-public:
- typedef CFXMEM_FixedPage<blockNum, blockSize> T;
- FX_LPBYTE m_pStartPage;
- FX_LPBYTE m_pLimitPos;
- FX_LPBYTE m_pCurPage;
- volatile size_t m_nAvailBlocks;
- void Initialize(FX_LPBYTE pStart, size_t pages)
- {
- m_pStartPage = m_pCurPage = pStart;
- m_nAvailBlocks = pages * blockNum;
- for (size_t n = 0; n < pages; n ++) {
- ((T*)pStart)->Initialize();
- pStart += FX_FIXEDMEM_PAGESIZE;
- }
- m_pLimitPos = pStart;
- }
- FX_BOOL IsEmpty() const
- {
- return m_nAvailBlocks == (m_pLimitPos - m_pStartPage) / FX_FIXEDMEM_PAGESIZE * blockNum;
- }
- FX_BOOL HasFreeBlock() const
- {
- return (FX_BOOL)m_nAvailBlocks;
- }
- FX_LPVOID Alloc(size_t size)
- {
- FXSYS_assert(m_nAvailBlocks);
- do {
- if (((T*)m_pCurPage)->HasFreeBlock()) {
- m_nAvailBlocks --;
- return ((T*)m_pCurPage)->Alloc(size);
- }
- m_pCurPage += FX_FIXEDMEM_PAGESIZE;
- if (m_pCurPage == m_pLimitPos) {
- m_pCurPage = m_pStartPage;
- }
- } while (TRUE);
- return NULL;
- }
- void Free(FX_LPVOID p)
- {
- FXSYS_assert(p > (FX_LPVOID)m_pStartPage && p < (FX_LPVOID)m_pLimitPos);
- ((T*)(m_pStartPage + ((FX_LPBYTE)p - m_pStartPage) / FX_FIXEDMEM_PAGESIZE * FX_FIXEDMEM_PAGESIZE))->Free(p);
- m_nAvailBlocks ++;
- }
-};
-typedef CFXMEM_FixedPages<FIXEDMEM_8BYTES_BLOCKNUM, 8> CFXMEM_8BytesPages;
-typedef CFXMEM_FixedPages<FIXEDMEM_16BYTES_BLOCKNUM, 16> CFXMEM_16BytesPages;
-typedef CFXMEM_FixedPages<FIXEDMEM_32BYTES_BLOCKNUM, 32> CFXMEM_32BytesPages;
-class CFXMEM_Block
-{
-public:
- size_t m_nBlockSize;
- CFXMEM_Block* m_pNextBlock;
-};
-class CFXMEM_Page
-{
-public:
- size_t m_nAvailSize;
- CFXMEM_Block* m_pLimitPos;
- CFXMEM_Block m_AvailHead;
- void Initialize(size_t size);
- FX_BOOL IsEmpty() const
- {
- return m_AvailHead.m_pNextBlock && m_AvailHead.m_nBlockSize == m_AvailHead.m_pNextBlock->m_nBlockSize;
- }
- FX_LPVOID Alloc(size_t size);
- FX_LPVOID Realloc(FX_LPVOID p, size_t oldSize, size_t newSize);
- void Free(FX_LPVOID p);
-protected:
- FX_LPVOID Alloc(CFXMEM_Block* pPrevBlock, CFXMEM_Block* pNextBlock, size_t size, size_t oldsize);
-};
-class CFXMEM_Pages
-{
-public:
- CFXMEM_Page* m_pStartPage;
- CFXMEM_Page* m_pLimitPos;
- CFXMEM_Page* m_pCurPage;
- size_t m_nPageSize;
- void Initialize(FX_LPBYTE pStart, size_t pageSize, size_t pages);
- FX_BOOL IsEmpty() const;
- FX_LPVOID Alloc(size_t size);
- FX_LPVOID Realloc(FX_LPVOID p, size_t oldSize, size_t newSize);
- void Free(FX_LPVOID p);
-};
-class CFXMEM_Pool
-{
-public:
- CFXMEM_Pool* m_pPrevPool;
- CFXMEM_Pool* m_pNextPool;
- CFXMEM_8BytesPages m_8BytesPages;
- CFXMEM_16BytesPages m_16BytesPages;
- CFXMEM_32BytesPages m_32BytesPages;
- CFXMEM_Pages m_MidPages;
- FX_BOOL m_bAlone;
- FX_DWORD m_dwReserved[3];
- FX_LPVOID m_pLimitPos;
- CFXMEM_Page* m_pLargePage;
- void Initialize(const FX_MEMCONFIG* pMemConfig, size_t size, size_t pageNum8Bytes, size_t pageNum16Bytes, size_t pageNum32Bytes, size_t pageNumMid);
- FX_BOOL IsEmpty() const;
- size_t GetSize(FX_LPVOID p) const;
- FX_LPVOID Realloc(FX_LPVOID p, size_t oldSize, size_t newSize);
- void Free(FX_LPVOID p);
-};
-class CFXMEM_FixedMgr
-{
-public:
- void Initialize(size_t size);
- FX_LPVOID Alloc(size_t size);
- FX_LPVOID Realloc(FX_LPVOID p, size_t newSize);
- void Free(FX_LPVOID p);
- void FreeAll();
- void Purge();
- CFXMEM_Pool* GetFirstPool()
- {
- return &m_FirstPool;
- }
- size_t GetSize(FX_LPVOID p) const;
- FXMEM_SystemMgr m_SystemMgr;
- FXMEM_SystemMgr2* m_pExtender;
- FX_LPVOID m_pReserved;
- FX_MEMCONFIG m_MemConfig;
-protected:
- FX_LPVOID Alloc16(CFXMEM_Pool **pp32Pool = NULL, size_t size = 0);
- FX_LPVOID Alloc32(size_t size);
- FX_LPVOID AllocSmall(size_t size);
- FX_LPVOID AllocMid(size_t size);
- FX_LPVOID AllocLarge(size_t size);
- FX_LPVOID ReallocSmall(CFXMEM_Pool* pPool, FX_LPVOID p, size_t oldSize, size_t newSize);
- void FreePool(CFXMEM_Pool* pPool);
- CFXMEM_Pool m_FirstPool;
-};
-#define FIXEDMEM_PROXYSIZE_0 (1024 * 1024 * 8)
-#define FIXEDMEM_PROXYSIZE_1 (1024 * 1024 * 16)
-#define FIXEDMEM_PROXYSIZE_2 (1024 * 1024 * 32)
-#define FIXEDMEM_PROXYSIZE_3 (1024 * 1024 * 64)
-#define FIXEDMEM_PROXYSIZE_4 (1024 * 1024 * 128)
-#define FIXEDMEM_PROXYSIZE_5 (1024 * 1024 * 256)
-const FX_MEMCONFIG* FixedMgr_GetConfig(size_t nSize);
-class CFixedMgr_Proxy
-{
-public:
- FXMEM_FoxitMgr* Initialize(FX_LPVOID pBuffer, size_t nSize, FX_BOOL bExtensible);
- static FX_BOOL Common_More(FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size);
- static void Common_Free(FXMEM_SystemMgr2* pMgr, void* memory);
- FXMEM_SystemMgr2 m_SystemMgr;
- CFXMEM_Page* m_pFixedPage;
- FX_LPVOID m_pBuffer;
- size_t m_nSize;
- FX_BOOL m_bExtensible;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _FXM_MEM_INT_H_ +#define _FXM_MEM_INT_H_ +struct FX_DefAllocator { + IFX_Allocator m_Allocator; + struct CFX_MemoryMgr* m_pFoxitMgr; +}; +struct CFX_MemoryMgr { +public: + FXMEM_SystemMgr* m_pSystemMgr; + FX_DefAllocator m_DefAllocator; + FX_LPVOID m_pExternalMemory; + FX_BOOL m_bReleaseMgr; + void Init(FXMEM_SystemMgr* pSystemMgr); + void* Alloc(size_t size, int flags); + void* AllocDebug(size_t size, int flags, FX_LPCSTR file, int line); + void* Realloc(void* p, size_t size, int flags); + void* ReallocDebug(void* p, size_t size, int flags, FX_LPCSTR file, int line); + void Free(void* p, int flags); + void PurgeMgr(); +}; +extern CFX_MemoryMgr* g_pDefFoxitMgr; +#define FIXEDMEM_PAGE_EXTRASPACE sizeof(size_t) +#define FIXEDMEM_BLOCKNUM(bs) (8 * (FX_FIXEDMEM_PAGESIZE - FIXEDMEM_PAGE_EXTRASPACE) / (8 * bs + 1)) +#define FIXEDMEM_8BYTES_BLOCKNUM FIXEDMEM_BLOCKNUM(8) +#define FIXEDMEM_16BYTES_BLOCKNUM FIXEDMEM_BLOCKNUM(16) +#define FIXEDMEM_32BYTES_BLOCKNUM FIXEDMEM_BLOCKNUM(32) +extern const FX_BYTE ZeroLeadPos[256]; +extern const FX_BYTE OneLeadPos[256]; +template <size_t blockNum, size_t blockSize> +class CFXMEM_FixedPage +{ +public: + void Initialize() + { + m_nAvailCount = blockNum; + FXSYS_memset32(m_BusyMap, 0, (blockNum + 7) / 8); + } + FX_BOOL HasFreeBlock() const + { + return (FX_BOOL)m_nAvailCount; + } + FX_LPVOID Alloc(size_t size) + { + FXSYS_assert(m_nAvailCount); + FX_LPDWORD pFind = (FX_LPDWORD)m_BusyMap; + size_t i = 0; + while (i < (blockNum + 7) / 8 / 4 && pFind[i] == 0xFFFFFFFF) { + i ++; + } + i *= 4; + while (m_BusyMap[i] == 0xFF) { + i ++; + } + size_t pos = ZeroLeadPos[m_BusyMap[i]]; + m_BusyMap[i] |= 1 << (7 - pos); + m_nAvailCount --; + return (FX_LPBYTE)(this + 1) + (i * 8 + pos) * blockSize; + } + void Free(FX_LPVOID p) + { + FXSYS_assert(p > (FX_LPVOID)this && p < (FX_LPVOID)((FX_LPBYTE)this + FX_FIXEDMEM_PAGESIZE)); + size_t pos = ((FX_LPBYTE)p - (FX_LPBYTE)(this + 1)) / blockSize; + m_BusyMap[pos / 8] &= ~(1 << (7 - (pos % 8))); + m_nAvailCount ++; + } + volatile size_t m_nAvailCount; + FX_BYTE m_BusyMap[(blockNum + 7) / 8]; +}; +typedef CFXMEM_FixedPage<FIXEDMEM_8BYTES_BLOCKNUM, 8> CFXMEM_8BytesPage; +typedef CFXMEM_FixedPage<FIXEDMEM_16BYTES_BLOCKNUM, 16> CFXMEM_16BytesPage; +typedef CFXMEM_FixedPage<FIXEDMEM_32BYTES_BLOCKNUM, 32> CFXMEM_32BytesPage; +template <size_t blockNum, size_t blockSize> +class CFXMEM_FixedPages +{ +public: + typedef CFXMEM_FixedPage<blockNum, blockSize> T; + FX_LPBYTE m_pStartPage; + FX_LPBYTE m_pLimitPos; + FX_LPBYTE m_pCurPage; + volatile size_t m_nAvailBlocks; + void Initialize(FX_LPBYTE pStart, size_t pages) + { + m_pStartPage = m_pCurPage = pStart; + m_nAvailBlocks = pages * blockNum; + for (size_t n = 0; n < pages; n ++) { + ((T*)pStart)->Initialize(); + pStart += FX_FIXEDMEM_PAGESIZE; + } + m_pLimitPos = pStart; + } + FX_BOOL IsEmpty() const + { + return m_nAvailBlocks == (m_pLimitPos - m_pStartPage) / FX_FIXEDMEM_PAGESIZE * blockNum; + } + FX_BOOL HasFreeBlock() const + { + return (FX_BOOL)m_nAvailBlocks; + } + FX_LPVOID Alloc(size_t size) + { + FXSYS_assert(m_nAvailBlocks); + do { + if (((T*)m_pCurPage)->HasFreeBlock()) { + m_nAvailBlocks --; + return ((T*)m_pCurPage)->Alloc(size); + } + m_pCurPage += FX_FIXEDMEM_PAGESIZE; + if (m_pCurPage == m_pLimitPos) { + m_pCurPage = m_pStartPage; + } + } while (TRUE); + return NULL; + } + void Free(FX_LPVOID p) + { + FXSYS_assert(p > (FX_LPVOID)m_pStartPage && p < (FX_LPVOID)m_pLimitPos); + ((T*)(m_pStartPage + ((FX_LPBYTE)p - m_pStartPage) / FX_FIXEDMEM_PAGESIZE * FX_FIXEDMEM_PAGESIZE))->Free(p); + m_nAvailBlocks ++; + } +}; +typedef CFXMEM_FixedPages<FIXEDMEM_8BYTES_BLOCKNUM, 8> CFXMEM_8BytesPages; +typedef CFXMEM_FixedPages<FIXEDMEM_16BYTES_BLOCKNUM, 16> CFXMEM_16BytesPages; +typedef CFXMEM_FixedPages<FIXEDMEM_32BYTES_BLOCKNUM, 32> CFXMEM_32BytesPages; +class CFXMEM_Block +{ +public: + size_t m_nBlockSize; + CFXMEM_Block* m_pNextBlock; +}; +class CFXMEM_Page +{ +public: + size_t m_nAvailSize; + CFXMEM_Block* m_pLimitPos; + CFXMEM_Block m_AvailHead; + void Initialize(size_t size); + FX_BOOL IsEmpty() const + { + return m_AvailHead.m_pNextBlock && m_AvailHead.m_nBlockSize == m_AvailHead.m_pNextBlock->m_nBlockSize; + } + FX_LPVOID Alloc(size_t size); + FX_LPVOID Realloc(FX_LPVOID p, size_t oldSize, size_t newSize); + void Free(FX_LPVOID p); +protected: + FX_LPVOID Alloc(CFXMEM_Block* pPrevBlock, CFXMEM_Block* pNextBlock, size_t size, size_t oldsize); +}; +class CFXMEM_Pages +{ +public: + CFXMEM_Page* m_pStartPage; + CFXMEM_Page* m_pLimitPos; + CFXMEM_Page* m_pCurPage; + size_t m_nPageSize; + void Initialize(FX_LPBYTE pStart, size_t pageSize, size_t pages); + FX_BOOL IsEmpty() const; + FX_LPVOID Alloc(size_t size); + FX_LPVOID Realloc(FX_LPVOID p, size_t oldSize, size_t newSize); + void Free(FX_LPVOID p); +}; +class CFXMEM_Pool +{ +public: + CFXMEM_Pool* m_pPrevPool; + CFXMEM_Pool* m_pNextPool; + CFXMEM_8BytesPages m_8BytesPages; + CFXMEM_16BytesPages m_16BytesPages; + CFXMEM_32BytesPages m_32BytesPages; + CFXMEM_Pages m_MidPages; + FX_BOOL m_bAlone; + FX_DWORD m_dwReserved[3]; + FX_LPVOID m_pLimitPos; + CFXMEM_Page* m_pLargePage; + void Initialize(const FX_MEMCONFIG* pMemConfig, size_t size, size_t pageNum8Bytes, size_t pageNum16Bytes, size_t pageNum32Bytes, size_t pageNumMid); + FX_BOOL IsEmpty() const; + size_t GetSize(FX_LPVOID p) const; + FX_LPVOID Realloc(FX_LPVOID p, size_t oldSize, size_t newSize); + void Free(FX_LPVOID p); +}; +class CFXMEM_FixedMgr +{ +public: + void Initialize(size_t size); + FX_LPVOID Alloc(size_t size); + FX_LPVOID Realloc(FX_LPVOID p, size_t newSize); + void Free(FX_LPVOID p); + void FreeAll(); + void Purge(); + CFXMEM_Pool* GetFirstPool() + { + return &m_FirstPool; + } + size_t GetSize(FX_LPVOID p) const; + FXMEM_SystemMgr m_SystemMgr; + FXMEM_SystemMgr2* m_pExtender; + FX_LPVOID m_pReserved; + FX_MEMCONFIG m_MemConfig; +protected: + FX_LPVOID Alloc16(CFXMEM_Pool **pp32Pool = NULL, size_t size = 0); + FX_LPVOID Alloc32(size_t size); + FX_LPVOID AllocSmall(size_t size); + FX_LPVOID AllocMid(size_t size); + FX_LPVOID AllocLarge(size_t size); + FX_LPVOID ReallocSmall(CFXMEM_Pool* pPool, FX_LPVOID p, size_t oldSize, size_t newSize); + void FreePool(CFXMEM_Pool* pPool); + CFXMEM_Pool m_FirstPool; +}; +#define FIXEDMEM_PROXYSIZE_0 (1024 * 1024 * 8) +#define FIXEDMEM_PROXYSIZE_1 (1024 * 1024 * 16) +#define FIXEDMEM_PROXYSIZE_2 (1024 * 1024 * 32) +#define FIXEDMEM_PROXYSIZE_3 (1024 * 1024 * 64) +#define FIXEDMEM_PROXYSIZE_4 (1024 * 1024 * 128) +#define FIXEDMEM_PROXYSIZE_5 (1024 * 1024 * 256) +const FX_MEMCONFIG* FixedMgr_GetConfig(size_t nSize); +class CFixedMgr_Proxy +{ +public: + FXMEM_FoxitMgr* Initialize(FX_LPVOID pBuffer, size_t nSize, FX_BOOL bExtensible); + static FX_BOOL Common_More(FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size); + static void Common_Free(FXMEM_SystemMgr2* pMgr, void* memory); + FXMEM_SystemMgr2 m_SystemMgr; + CFXMEM_Page* m_pFixedPage; + FX_LPVOID m_pBuffer; + size_t m_nSize; + FX_BOOL m_bExtensible; +}; +#endif diff --git a/core/src/fxcrt/plex.h b/core/src/fxcrt/plex.h index 32a27cbf1d..e982fd67a4 100644 --- a/core/src/fxcrt/plex.h +++ b/core/src/fxcrt/plex.h @@ -1,15 +1,15 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-struct CFX_Plex {
- CFX_Plex* pNext;
- void* data()
- {
- return this + 1;
- }
- static CFX_Plex* Create(IFX_Allocator* pAllocator, CFX_Plex*& head, FX_DWORD nMax, FX_DWORD cbElement);
- void FreeDataChain(IFX_Allocator* pAllocator);
-};
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +struct CFX_Plex { + CFX_Plex* pNext; + void* data() + { + return this + 1; + } + static CFX_Plex* Create(IFX_Allocator* pAllocator, CFX_Plex*& head, FX_DWORD nMax, FX_DWORD cbElement); + void FreeDataChain(IFX_Allocator* pAllocator); +}; diff --git a/core/src/fxcrt/xml_int.h b/core/src/fxcrt/xml_int.h index 8ad8ef4b88..09737893af 100644 --- a/core/src/fxcrt/xml_int.h +++ b/core/src/fxcrt/xml_int.h @@ -1,178 +1,178 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _FXCRT_XML_INT_
-#define _FXCRT_XML_INT_
-class CXML_DataBufAcc : public IFX_BufferRead, public CFX_Object
-{
-public:
- CXML_DataBufAcc(FX_LPCBYTE pBuffer, size_t size, IFX_Allocator* pAllocator = NULL)
- : m_pAllocator(pAllocator)
- , m_pBuffer(pBuffer)
- , m_dwSize(size)
- , m_dwCurPos(0)
- {
- }
- virtual ~CXML_DataBufAcc() {}
- virtual void Release()
- {
- if (m_pAllocator) {
- FX_DeleteAtAllocator(this, m_pAllocator, CXML_DataBufAcc);
- } else {
- delete this;
- }
- }
- virtual FX_BOOL IsEOF()
- {
- return m_dwCurPos >= m_dwSize;
- }
- virtual FX_FILESIZE GetPosition()
- {
- return (FX_FILESIZE)m_dwCurPos;
- }
- virtual size_t ReadBlock(void* buffer, size_t size)
- {
- return 0;
- }
- virtual FX_BOOL ReadNextBlock(FX_BOOL bRestart = FALSE)
- {
- if (bRestart) {
- m_dwCurPos = 0;
- }
- if (m_dwCurPos < m_dwSize) {
- m_dwCurPos = m_dwSize;
- return TRUE;
- }
- return FALSE;
- }
- virtual FX_LPCBYTE GetBlockBuffer()
- {
- return m_pBuffer;
- }
- virtual size_t GetBlockSize()
- {
- return m_dwSize;
- }
- virtual FX_FILESIZE GetBlockOffset()
- {
- return 0;
- }
-protected:
- IFX_Allocator* m_pAllocator;
- FX_LPCBYTE m_pBuffer;
- size_t m_dwSize;
- size_t m_dwCurPos;
-};
-#define FX_XMLDATASTREAM_BufferSize (32 * 1024)
-class CXML_DataStmAcc : public IFX_BufferRead, public CFX_Object
-{
-public:
- CXML_DataStmAcc(IFX_FileRead *pFileRead, IFX_Allocator* pAllocator = NULL)
- : m_pAllocator(pAllocator)
- , m_pFileRead(pFileRead)
- , m_pBuffer(NULL)
- , m_nStart(0)
- , m_dwSize(0)
- {
- FXSYS_assert(m_pFileRead != NULL);
- }
- virtual ~CXML_DataStmAcc()
- {
- if (m_pBuffer) {
- FX_Allocator_Free(m_pAllocator, m_pBuffer);
- }
- }
- virtual void Release()
- {
- if (m_pAllocator) {
- FX_DeleteAtAllocator(this, m_pAllocator, CXML_DataStmAcc);
- } else {
- delete this;
- }
- }
- virtual FX_BOOL IsEOF()
- {
- return m_nStart + (FX_FILESIZE)m_dwSize >= m_pFileRead->GetSize();
- }
- virtual FX_FILESIZE GetPosition()
- {
- return m_nStart + (FX_FILESIZE)m_dwSize;
- }
- virtual size_t ReadBlock(void* buffer, size_t size)
- {
- return 0;
- }
- virtual FX_BOOL ReadNextBlock(FX_BOOL bRestart = FALSE)
- {
- if (bRestart) {
- m_nStart = 0;
- }
- FX_FILESIZE nLength = m_pFileRead->GetSize();
- m_nStart += (FX_FILESIZE)m_dwSize;
- if (m_nStart >= nLength) {
- return FALSE;
- }
- m_dwSize = (size_t)FX_MIN(FX_XMLDATASTREAM_BufferSize, nLength - m_nStart);
- if (!m_pBuffer) {
- m_pBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, m_dwSize);
- if (!m_pBuffer) {
- return FALSE;
- }
- }
- return m_pFileRead->ReadBlock(m_pBuffer, m_nStart, m_dwSize);
- }
- virtual FX_LPCBYTE GetBlockBuffer()
- {
- return (FX_LPCBYTE)m_pBuffer;
- }
- virtual size_t GetBlockSize()
- {
- return m_dwSize;
- }
- virtual FX_FILESIZE GetBlockOffset()
- {
- return m_nStart;
- }
-protected:
- IFX_Allocator* m_pAllocator;
- IFX_FileRead *m_pFileRead;
- FX_LPBYTE m_pBuffer;
- FX_FILESIZE m_nStart;
- size_t m_dwSize;
-};
-class CXML_Parser
-{
-public:
- CXML_Parser(IFX_Allocator* pAllocator = NULL) : m_pAllocator(pAllocator) {}
- ~CXML_Parser();
- IFX_Allocator* m_pAllocator;
- IFX_BufferRead* m_pDataAcc;
- FX_BOOL m_bOwnedStream;
- FX_FILESIZE m_nOffset;
- FX_BOOL m_bSaveSpaceChars;
- FX_LPCBYTE m_pBuffer;
- size_t m_dwBufferSize;
- FX_FILESIZE m_nBufferOffset;
- size_t m_dwIndex;
- FX_BOOL Init(FX_LPBYTE pBuffer, size_t size);
- FX_BOOL Init(IFX_FileRead *pFileRead);
- FX_BOOL Init(IFX_BufferRead *pBuffer);
- FX_BOOL Init(FX_BOOL bOwndedStream);
- FX_BOOL ReadNextBlock();
- FX_BOOL IsEOF();
- FX_BOOL HaveAvailData();
- void SkipWhiteSpaces();
- void GetName(CFX_ByteStringL &space, CFX_ByteStringL &name);
- void GetAttrValue(CFX_WideStringL &value);
- FX_DWORD GetCharRef();
- void GetTagName(CFX_ByteStringL &space, CFX_ByteStringL &name, FX_BOOL &bEndTag, FX_BOOL bStartTag = FALSE);
- void SkipLiterals(FX_BSTR str);
- CXML_Element* ParseElement(CXML_Element* pParent, FX_BOOL bStartTag = FALSE);
- void InsertContentSegment(FX_BOOL bCDATA, FX_WSTR content, CXML_Element* pElement);
- void InsertCDATASegment(CFX_UTF8Decoder& decoder, CXML_Element* pElement);
-};
-void FX_XML_SplitQualifiedName(FX_BSTR bsFullName, CFX_ByteStringC &bsSpace, CFX_ByteStringC &bsName);
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _FXCRT_XML_INT_ +#define _FXCRT_XML_INT_ +class CXML_DataBufAcc : public IFX_BufferRead, public CFX_Object +{ +public: + CXML_DataBufAcc(FX_LPCBYTE pBuffer, size_t size, IFX_Allocator* pAllocator = NULL) + : m_pAllocator(pAllocator) + , m_pBuffer(pBuffer) + , m_dwSize(size) + , m_dwCurPos(0) + { + } + virtual ~CXML_DataBufAcc() {} + virtual void Release() + { + if (m_pAllocator) { + FX_DeleteAtAllocator(this, m_pAllocator, CXML_DataBufAcc); + } else { + delete this; + } + } + virtual FX_BOOL IsEOF() + { + return m_dwCurPos >= m_dwSize; + } + virtual FX_FILESIZE GetPosition() + { + return (FX_FILESIZE)m_dwCurPos; + } + virtual size_t ReadBlock(void* buffer, size_t size) + { + return 0; + } + virtual FX_BOOL ReadNextBlock(FX_BOOL bRestart = FALSE) + { + if (bRestart) { + m_dwCurPos = 0; + } + if (m_dwCurPos < m_dwSize) { + m_dwCurPos = m_dwSize; + return TRUE; + } + return FALSE; + } + virtual FX_LPCBYTE GetBlockBuffer() + { + return m_pBuffer; + } + virtual size_t GetBlockSize() + { + return m_dwSize; + } + virtual FX_FILESIZE GetBlockOffset() + { + return 0; + } +protected: + IFX_Allocator* m_pAllocator; + FX_LPCBYTE m_pBuffer; + size_t m_dwSize; + size_t m_dwCurPos; +}; +#define FX_XMLDATASTREAM_BufferSize (32 * 1024) +class CXML_DataStmAcc : public IFX_BufferRead, public CFX_Object +{ +public: + CXML_DataStmAcc(IFX_FileRead *pFileRead, IFX_Allocator* pAllocator = NULL) + : m_pAllocator(pAllocator) + , m_pFileRead(pFileRead) + , m_pBuffer(NULL) + , m_nStart(0) + , m_dwSize(0) + { + FXSYS_assert(m_pFileRead != NULL); + } + virtual ~CXML_DataStmAcc() + { + if (m_pBuffer) { + FX_Allocator_Free(m_pAllocator, m_pBuffer); + } + } + virtual void Release() + { + if (m_pAllocator) { + FX_DeleteAtAllocator(this, m_pAllocator, CXML_DataStmAcc); + } else { + delete this; + } + } + virtual FX_BOOL IsEOF() + { + return m_nStart + (FX_FILESIZE)m_dwSize >= m_pFileRead->GetSize(); + } + virtual FX_FILESIZE GetPosition() + { + return m_nStart + (FX_FILESIZE)m_dwSize; + } + virtual size_t ReadBlock(void* buffer, size_t size) + { + return 0; + } + virtual FX_BOOL ReadNextBlock(FX_BOOL bRestart = FALSE) + { + if (bRestart) { + m_nStart = 0; + } + FX_FILESIZE nLength = m_pFileRead->GetSize(); + m_nStart += (FX_FILESIZE)m_dwSize; + if (m_nStart >= nLength) { + return FALSE; + } + m_dwSize = (size_t)FX_MIN(FX_XMLDATASTREAM_BufferSize, nLength - m_nStart); + if (!m_pBuffer) { + m_pBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, m_dwSize); + if (!m_pBuffer) { + return FALSE; + } + } + return m_pFileRead->ReadBlock(m_pBuffer, m_nStart, m_dwSize); + } + virtual FX_LPCBYTE GetBlockBuffer() + { + return (FX_LPCBYTE)m_pBuffer; + } + virtual size_t GetBlockSize() + { + return m_dwSize; + } + virtual FX_FILESIZE GetBlockOffset() + { + return m_nStart; + } +protected: + IFX_Allocator* m_pAllocator; + IFX_FileRead *m_pFileRead; + FX_LPBYTE m_pBuffer; + FX_FILESIZE m_nStart; + size_t m_dwSize; +}; +class CXML_Parser +{ +public: + CXML_Parser(IFX_Allocator* pAllocator = NULL) : m_pAllocator(pAllocator) {} + ~CXML_Parser(); + IFX_Allocator* m_pAllocator; + IFX_BufferRead* m_pDataAcc; + FX_BOOL m_bOwnedStream; + FX_FILESIZE m_nOffset; + FX_BOOL m_bSaveSpaceChars; + FX_LPCBYTE m_pBuffer; + size_t m_dwBufferSize; + FX_FILESIZE m_nBufferOffset; + size_t m_dwIndex; + FX_BOOL Init(FX_LPBYTE pBuffer, size_t size); + FX_BOOL Init(IFX_FileRead *pFileRead); + FX_BOOL Init(IFX_BufferRead *pBuffer); + FX_BOOL Init(FX_BOOL bOwndedStream); + FX_BOOL ReadNextBlock(); + FX_BOOL IsEOF(); + FX_BOOL HaveAvailData(); + void SkipWhiteSpaces(); + void GetName(CFX_ByteStringL &space, CFX_ByteStringL &name); + void GetAttrValue(CFX_WideStringL &value); + FX_DWORD GetCharRef(); + void GetTagName(CFX_ByteStringL &space, CFX_ByteStringL &name, FX_BOOL &bEndTag, FX_BOOL bStartTag = FALSE); + void SkipLiterals(FX_BSTR str); + CXML_Element* ParseElement(CXML_Element* pParent, FX_BOOL bStartTag = FALSE); + void InsertContentSegment(FX_BOOL bCDATA, FX_WSTR content, CXML_Element* pElement); + void InsertCDATASegment(CFX_UTF8Decoder& decoder, CXML_Element* pElement); +}; +void FX_XML_SplitQualifiedName(FX_BSTR bsFullName, CFX_ByteStringC &bsSpace, CFX_ByteStringC &bsName); +#endif |