More master side changes for convergence with XFA.
[pdfium.git] / fpdfsdk / src / javascript / JS_GlobalData.cpp
index 97bba8d..cf5b997 100644 (file)
-// Copyright 2014 PDFium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
\r
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
-\r
-#include "../../include/javascript/JavaScript.h"\r
-#include "../../include/javascript/IJavaScript.h"\r
-#include "../../include/javascript/JS_GlobalData.h"\r
-\r
-#define JS_MAXGLOBALDATA                       (1024 * 4 - 8)\r
-\r
-/* --------------------- CJS_GlobalVariableArray --------------------- */\r
-\r
-CJS_GlobalVariableArray::CJS_GlobalVariableArray()\r
-{\r
-}\r
-\r
-CJS_GlobalVariableArray::~CJS_GlobalVariableArray()\r
-{\r
-       Empty();\r
-}\r
-\r
-void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array)\r
-{\r
-       Empty();\r
-       for (int i=0,sz=array.Count(); i<sz; i++)\r
-       {\r
-               CJS_KeyValue* pOldObjData = array.GetAt(i);\r
-               ASSERT(pOldObjData != NULL);\r
-\r
-               switch (pOldObjData->nType)\r
-               {\r
-               case JS_GLOBALDATA_TYPE_NUMBER:\r
-                       {\r
-                               CJS_KeyValue* pNewObjData = new CJS_KeyValue;\r
-                               pNewObjData->sKey = pOldObjData->sKey;\r
-                               pNewObjData->nType = pOldObjData->nType;\r
-                               pNewObjData->dData = pOldObjData->dData;\r
-                               Add(pNewObjData);\r
-                       }\r
-                       break;\r
-               case JS_GLOBALDATA_TYPE_BOOLEAN:\r
-                       {\r
-                               CJS_KeyValue* pNewObjData = new CJS_KeyValue;\r
-                               pNewObjData->sKey = pOldObjData->sKey;\r
-                               pNewObjData->nType = pOldObjData->nType;\r
-                               pNewObjData->bData = pOldObjData->bData;\r
-                               Add(pNewObjData);\r
-                       }\r
-                       break;\r
-               case JS_GLOBALDATA_TYPE_STRING:\r
-                       {\r
-                               CJS_KeyValue* pNewObjData = new CJS_KeyValue;\r
-                               pNewObjData->sKey = pOldObjData->sKey;\r
-                               pNewObjData->nType = pOldObjData->nType;\r
-                               pNewObjData->sData = pOldObjData->sData;\r
-                               Add(pNewObjData);\r
-                       }\r
-                       break;\r
-               case JS_GLOBALDATA_TYPE_OBJECT:\r
-                       {\r
-                               CJS_KeyValue* pNewObjData = new CJS_KeyValue;\r
-                               pNewObjData->sKey = pOldObjData->sKey;\r
-                               pNewObjData->nType = pOldObjData->nType;\r
-                               pNewObjData->objData.Copy(pOldObjData->objData);\r
-                               Add(pNewObjData);\r
-                       }\r
-               case JS_GLOBALDATA_TYPE_NULL:\r
-                       {\r
-                               CJS_KeyValue* pNewObjData = new CJS_KeyValue;\r
-                               pNewObjData->sKey = pOldObjData->sKey;\r
-                               pNewObjData->nType = pOldObjData->nType;\r
-                               Add(pNewObjData);\r
-                       }\r
-               }\r
-       }\r
-}\r
-\r
-void CJS_GlobalVariableArray::Add(CJS_KeyValue* p)\r
-{\r
-       array.Add(p);\r
-}\r
-\r
-int CJS_GlobalVariableArray::Count() const\r
-{\r
-       return array.GetSize();\r
-}\r
-\r
-CJS_KeyValue* CJS_GlobalVariableArray::GetAt(int index) const\r
-{\r
-       return array.GetAt(index);\r
-}\r
-\r
-void CJS_GlobalVariableArray::Empty()\r
-{\r
-       for (int i=0,sz=array.GetSize(); i<sz; i++)\r
-               delete array.GetAt(i);\r
-       array.RemoveAll();\r
-}\r
-\r
-/* -------------------------- CJS_GlobalData -------------------------- */\r
-\r
-#define READER_JS_GLOBALDATA_FILENAME                          L"Reader_JsGlobal.Data"\r
-#define PHANTOM_JS_GLOBALDATA_FILENAME                         L"Phantom_JsGlobal.Data"\r
-#define SDK_JS_GLOBALDATA_FILENAME                                     L"SDK_JsGlobal.Data"\r
-\r
-static const FX_BYTE JS_RC4KEY[] = {0x19,0xa8,0xe8,0x01,0xf6,0xa8,0xb6,0x4d,0x82,0x04,\r
-                                                       0x45,0x6d,0xb4,0xcf,0xd7,0x77,0x67,0xf9,0x75,0x9f,\r
-                                                       0xf0,0xe0,0x1e,0x51,0xee,0x46,0xfd,0x0b,0xc9,0x93,\r
-                                                       0x25,0x55,0x4a,0xee,0xe0,0x16,0xd0,0xdf,0x8c,0xfa,\r
-                                                       0x2a,0xa9,0x49,0xfd,0x97,0x1c,0x0e,0x22,0x13,0x28,\r
-                                                       0x7c,0xaf,0xc4,0xfc,0x9c,0x12,0x65,0x8c,0x4e,0x5b,\r
-                                                       0x04,0x75,0x89,0xc9,0xb1,0xed,0x50,0xca,0x96,0x6f,\r
-                                                       0x1a,0x7a,0xfe,0x58,0x5d,0xec,0x19,0x4a,0xf6,0x35,\r
-                                                       0x6a,0x97,0x14,0x00,0x0e,0xd0,0x6b,0xbb,0xd5,0x75,\r
-                                                       0x55,0x8b,0x6e,0x6b,0x19,0xa0,0xf8,0x77,0xd5,0xa3\r
-                                                       };\r
-\r
-CJS_GlobalData::CJS_GlobalData(CPDFDoc_Environment* pApp) : m_pApp(pApp)\r
-{\r
-//     IBaseAnnot* pBaseAnnot = IBaseAnnot::GetBaseAnnot(m_pApp);\r
-//     ASSERT(pBaseAnnot != NULL);\r
-// \r
-//     m_sFilePath = pBaseAnnot->GetUserPath();\r
-       m_sFilePath += SDK_JS_GLOBALDATA_FILENAME;\r
-\r
-       LoadGlobalPersistentVariables();\r
-}\r
-\r
-CJS_GlobalData::~CJS_GlobalData()\r
-{\r
-       SaveGlobalPersisitentVariables();\r
-\r
-       for (int i=0,sz=m_arrayGlobalData.GetSize(); i<sz; i++)\r
-               delete m_arrayGlobalData.GetAt(i);\r
-\r
-       m_arrayGlobalData.RemoveAll();\r
-}\r
-\r
-int    CJS_GlobalData::FindGlobalVariable(FX_LPCSTR propname)\r
-{\r
-       ASSERT(propname != NULL);\r
-\r
-       int nRet = -1;\r
-\r
-       for (int i=0,sz=m_arrayGlobalData.GetSize(); i<sz; i++)\r
-       {\r
-               CJS_GlobalData_Element* pTemp = m_arrayGlobalData.GetAt(i);\r
-               if (pTemp->data.sKey[0] == *propname && pTemp->data.sKey == propname)\r
-               {\r
-                       nRet = i;\r
-                       break;\r
-               }\r
-       }\r
-\r
-       return nRet;\r
-}\r
-\r
-CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable(FX_LPCSTR propname)\r
-{\r
-       ASSERT(propname != NULL);\r
-\r
-       int     nFind = FindGlobalVariable(propname);\r
-\r
-       if (nFind >= 0)\r
-               return m_arrayGlobalData.GetAt(nFind);\r
-       else\r
-               return NULL;\r
-}\r
-\r
-void CJS_GlobalData::SetGlobalVariableNumber(FX_LPCSTR propname, double dData)\r
-{\r
-       ASSERT(propname != NULL);\r
-       CFX_ByteString sPropName = propname;\r
-\r
-       sPropName.TrimLeft();\r
-       sPropName.TrimRight();\r
-\r
-       if (sPropName.GetLength() == 0) return;\r
-\r
-       if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName))\r
-       {\r
-               pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;\r
-               pData->data.dData = dData;\r
-       }\r
-       else\r
-       {\r
-               CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;\r
-               pNewData->data.sKey = sPropName;\r
-               pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;\r
-               pNewData->data.dData = dData;\r
-\r
-               m_arrayGlobalData.Add(pNewData);\r
-       }\r
-}\r
-\r
-void CJS_GlobalData::SetGlobalVariableBoolean(FX_LPCSTR propname, bool bData)\r
-{\r
-       ASSERT(propname != NULL);\r
-       CFX_ByteString sPropName = propname;\r
-\r
-       sPropName.TrimLeft();\r
-       sPropName.TrimRight();\r
-\r
-       if (sPropName.GetLength() == 0) return;\r
-\r
-       if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName))\r
-       {\r
-               pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;\r
-               pData->data.bData = bData;\r
-       }\r
-       else\r
-       {\r
-               CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;\r
-               pNewData->data.sKey = sPropName;\r
-               pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;\r
-               pNewData->data.bData = bData;\r
-\r
-               m_arrayGlobalData.Add(pNewData);\r
-       }\r
-}\r
-\r
-void CJS_GlobalData::SetGlobalVariableString(FX_LPCSTR propname, const CFX_ByteString& sData)\r
-{\r
-       ASSERT(propname != NULL);\r
-       CFX_ByteString sPropName = propname;\r
-\r
-       sPropName.TrimLeft();\r
-       sPropName.TrimRight();\r
-\r
-       if (sPropName.GetLength() == 0) return;\r
-\r
-       if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName))\r
-       {\r
-               pData->data.nType = JS_GLOBALDATA_TYPE_STRING;\r
-               pData->data.sData = sData;\r
-       }\r
-       else\r
-       {\r
-               CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;\r
-               pNewData->data.sKey = sPropName;\r
-               pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING;\r
-               pNewData->data.sData = sData;\r
-\r
-               m_arrayGlobalData.Add(pNewData);\r
-       }\r
-}\r
-\r
-void CJS_GlobalData::SetGlobalVariableObject(FX_LPCSTR propname, const CJS_GlobalVariableArray& array)\r
-{\r
-       ASSERT(propname != NULL);\r
-       CFX_ByteString sPropName = propname;\r
-\r
-       sPropName.TrimLeft();\r
-       sPropName.TrimRight();\r
-\r
-       if (sPropName.GetLength() == 0) return;\r
-\r
-       if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName))\r
-       {\r
-               pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;\r
-               pData->data.objData.Copy(array);\r
-       }\r
-       else\r
-       {\r
-               CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;\r
-               pNewData->data.sKey = sPropName;\r
-               pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;\r
-               pNewData->data.objData.Copy(array);\r
-               \r
-               m_arrayGlobalData.Add(pNewData);\r
-       }\r
-}\r
-\r
-void CJS_GlobalData::SetGlobalVariableNull(FX_LPCSTR propname)\r
-{\r
-       ASSERT(propname != NULL);\r
-       CFX_ByteString sPropName = propname;\r
-       \r
-       sPropName.TrimLeft();\r
-       sPropName.TrimRight();\r
-       \r
-       if (sPropName.GetLength() == 0) return;\r
-       \r
-       if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName))\r
-       {\r
-               pData->data.nType = JS_GLOBALDATA_TYPE_NULL;\r
-       }\r
-       else\r
-       {\r
-               CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;\r
-               pNewData->data.sKey = sPropName;\r
-               pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL;\r
-               \r
-               m_arrayGlobalData.Add(pNewData);\r
-       }\r
-}\r
-\r
-FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(FX_LPCSTR propname, FX_BOOL bPersistent)\r
-{\r
-       ASSERT(propname != NULL);\r
-       CFX_ByteString sPropName = propname;\r
-\r
-       sPropName.TrimLeft();\r
-       sPropName.TrimRight();\r
-\r
-       if (sPropName.GetLength() == 0) return FALSE;\r
-\r
-       if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName))\r
-       {\r
-               pData->bPersistent = bPersistent;\r
-               return TRUE;\r
-       }\r
-\r
-       return FALSE;\r
-}\r
-\r
-FX_BOOL CJS_GlobalData::DeleteGlobalVariable(FX_LPCSTR propname)\r
-{\r
-       ASSERT(propname != NULL);\r
-       CFX_ByteString sPropName = propname;\r
-\r
-       sPropName.TrimLeft();\r
-       sPropName.TrimRight();\r
-\r
-       if (sPropName.GetLength() == 0) return FALSE;\r
-\r
-       int     nFind = FindGlobalVariable(sPropName);\r
-\r
-       if (nFind >= 0)\r
-       {\r
-               delete m_arrayGlobalData.GetAt(nFind);\r
-               m_arrayGlobalData.RemoveAt(nFind);\r
-               return TRUE;\r
-       }\r
-\r
-       return FALSE;\r
-}\r
-\r
-FX_INT32 CJS_GlobalData::GetSize() const\r
-{\r
-       return m_arrayGlobalData.GetSize();\r
-}\r
-\r
-CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const\r
-{\r
-       return m_arrayGlobalData.GetAt(index);\r
-}\r
-\r
-void CJS_GlobalData::LoadGlobalPersistentVariables()\r
-{\r
-       FX_LPBYTE pBuffer = NULL;\r
-       FX_INT32 nLength = 0;\r
-\r
-       LoadFileBuffer(m_sFilePath, pBuffer, nLength);\r
-\r
-       CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY));\r
-\r
-       if (pBuffer)\r
-       {\r
-               FX_LPBYTE p = pBuffer;\r
-               FX_WORD wType = *((FX_WORD*)p);\r
-               p += sizeof(FX_WORD);\r
-\r
-               //FX_WORD wTemp = (FX_WORD)(('X' << 8) | 'F');\r
-\r
-               if (wType == (FX_WORD)(('X' << 8) | 'F'))\r
-               {\r
-                       FX_WORD wVersion = *((FX_WORD*)p);\r
-                       p += sizeof(FX_WORD);\r
-\r
-                       ASSERT(wVersion <= 2);\r
-\r
-                       FX_DWORD dwCount = *((FX_DWORD*)p);\r
-                       p += sizeof(FX_DWORD);\r
-\r
-                       FX_DWORD dwSize = *((FX_DWORD*)p);\r
-                       p += sizeof(FX_DWORD);\r
-\r
-                       if (dwSize == nLength - sizeof(FX_WORD) * 2 - sizeof(FX_DWORD)* 2)\r
-                       {\r
-                               for (FX_INT32 i=0,sz=dwCount; i<sz; i++)\r
-                               {\r
-                                       if (p > pBuffer + nLength)\r
-                                               break;\r
-\r
-                                       FX_DWORD dwNameLen = *((FX_DWORD*)p);\r
-                                       p += sizeof(FX_DWORD);\r
-\r
-                                       if (p + dwNameLen > pBuffer + nLength)\r
-                                               break;\r
-\r
-                                       CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen);\r
-                                       p += sizeof(char) * dwNameLen;\r
-\r
-                                       FX_WORD wDataType = *((FX_WORD*)p);\r
-                                       p += sizeof(FX_WORD);\r
-\r
-                                       switch (wDataType)\r
-                                       {\r
-                                       case JS_GLOBALDATA_TYPE_NUMBER:\r
-                                               {\r
-                                                       double dData = 0;\r
-                                                       switch (wVersion)\r
-                                                       {\r
-                                                       case 1:\r
-                                                               {\r
-                                                                       FX_DWORD dwData = *((FX_DWORD*)p);\r
-                                                                       p += sizeof(FX_DWORD);\r
-                                                                       dData = dwData;\r
-                                                               }\r
-                                                               break;\r
-                                                       case 2:\r
-                                                               {\r
-                                                                       dData = *((double*)p);\r
-                                                                       p += sizeof(double);\r
-                                                               }\r
-                                                               break;\r
-                                                       }\r
-                                                       SetGlobalVariableNumber(sEntry, dData);\r
-                                                       SetGlobalVariablePersistent(sEntry, TRUE);\r
-                                               }\r
-                                               break;\r
-                                       case JS_GLOBALDATA_TYPE_BOOLEAN:\r
-                                               {\r
-                                                       FX_WORD wData = *((FX_WORD*)p);\r
-                                                       p += sizeof(FX_WORD);\r
-                                                       SetGlobalVariableBoolean(sEntry, (bool)(wData == 1));\r
-                                                       SetGlobalVariablePersistent(sEntry, TRUE);\r
-                                               }\r
-                                               break;\r
-                                       case JS_GLOBALDATA_TYPE_STRING:\r
-                                               {\r
-                                                       FX_DWORD dwLength = *((FX_DWORD*)p);\r
-                                                       p += sizeof(FX_DWORD);\r
-\r
-                                                       if (p + dwLength > pBuffer + nLength)\r
-                                                               break;\r
-\r
-                                                       SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength));\r
-                                                       SetGlobalVariablePersistent(sEntry, TRUE);\r
-                                                       p += sizeof(char) * dwLength;\r
-                                               }\r
-                                               break;\r
-                                       case JS_GLOBALDATA_TYPE_NULL:\r
-                                               {\r
-                                                       SetGlobalVariableNull(sEntry);\r
-                                                       SetGlobalVariablePersistent(sEntry, TRUE);\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-               FX_Free(pBuffer);\r
-       }\r
-}\r
-\r
-/*\r
-struct js_global_datafile_header\r
-{\r
-       FX_WORD type; //FX ('X' << 8) | 'F'\r
-       FX_WORD version; //1.0\r
-       FX_DWORD datacount;\r
-};\r
-struct js_global_datafile_data\r
-{\r
-       FX_WORD type;\r
-       FX_DWORD nData;\r
-       FX_WORD bData;\r
-       FX_DWORD nStrLen;\r
-       char* pStr;\r
-};\r
-*/\r
-\r
-void CJS_GlobalData::SaveGlobalPersisitentVariables()\r
-{\r
-       FX_DWORD nCount = 0;\r
-       CFX_BinaryBuf sData;\r
-\r
-       for (int i=0,sz=m_arrayGlobalData.GetSize(); i<sz; i++)\r
-       {\r
-               CJS_GlobalData_Element* pElement = m_arrayGlobalData.GetAt(i);\r
-               ASSERT(pElement != NULL);\r
-\r
-               if (pElement->bPersistent)\r
-               {\r
-                       CFX_BinaryBuf sElement;\r
-                       MakeByteString(pElement->data.sKey, &pElement->data, sElement);\r
-\r
-                       if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA)\r
-                               break;\r
-\r
-                       sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize());\r
-                       nCount++;\r
-               }\r
-       }\r
-\r
-       CFX_BinaryBuf sFile;\r
-\r
-       FX_WORD wType = (FX_WORD)(('X' << 8) | 'F');\r
-       sFile.AppendBlock(&wType, sizeof(FX_WORD));\r
-       FX_WORD wVersion = 2;\r
-       sFile.AppendBlock(&wVersion, sizeof(FX_WORD));\r
-       sFile.AppendBlock(&nCount, sizeof(FX_DWORD));\r
-       FX_DWORD dwSize = sData.GetSize();\r
-       sFile.AppendBlock(&dwSize, sizeof(FX_DWORD));\r
-\r
-       sFile.AppendBlock(sData.GetBuffer(), sData.GetSize());\r
-\r
-       CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY, sizeof(JS_RC4KEY));\r
-       WriteFileBuffer(m_sFilePath, (FX_LPCSTR)sFile.GetBuffer(), sFile.GetSize());\r
-}\r
-\r
-void CJS_GlobalData::LoadFileBuffer(FX_LPCWSTR sFilePath, FX_LPBYTE& pBuffer, FX_INT32& nLength)\r
-{\r
-//UnSupport.\r
-}\r
-\r
-void CJS_GlobalData::WriteFileBuffer(FX_LPCWSTR sFilePath, FX_LPCSTR pBuffer, FX_INT32 nLength)\r
-{\r
-//UnSupport.\r
-}\r
-\r
-void CJS_GlobalData::MakeByteString(const CFX_ByteString& name, CJS_KeyValue* pData, CFX_BinaryBuf& sData)\r
-{\r
-       ASSERT(pData != NULL);\r
-\r
-       FX_WORD wType = (FX_WORD)pData->nType;\r
-\r
-       switch (wType)\r
-       {\r
-       case JS_GLOBALDATA_TYPE_NUMBER:\r
-               {\r
-                       FX_DWORD dwNameLen = (FX_DWORD)name.GetLength();\r
-                       sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD));\r
-                       sData.AppendString(name);\r
-\r
-                       sData.AppendBlock(&wType, sizeof(FX_WORD));\r
-                       double dData = pData->dData;\r
-                       sData.AppendBlock(&dData, sizeof(double));\r
-               }\r
-               break;\r
-       case JS_GLOBALDATA_TYPE_BOOLEAN:\r
-               {\r
-                       FX_DWORD dwNameLen = (FX_DWORD)name.GetLength();\r
-                       sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD));\r
-                       sData.AppendString(name);\r
-\r
-                       sData.AppendBlock(&wType, sizeof(FX_WORD));\r
-                       FX_WORD wData = (FX_WORD)pData->bData;\r
-                       sData.AppendBlock(&wData, sizeof(FX_WORD));\r
-               }\r
-               break;\r
-       case JS_GLOBALDATA_TYPE_STRING:\r
-               {\r
-                       FX_DWORD dwNameLen = (FX_DWORD)name.GetLength();\r
-                       sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD));\r
-                       sData.AppendString(name);\r
-\r
-                       sData.AppendBlock(&wType, sizeof(FX_WORD));\r
-\r
-                       FX_DWORD dwDataLen = (FX_DWORD)pData->sData.GetLength();\r
-                       sData.AppendBlock(&dwDataLen, sizeof(FX_DWORD));\r
-                       sData.AppendString(pData->sData);\r
-               }\r
-               break;\r
-       case JS_GLOBALDATA_TYPE_NULL:\r
-               {\r
-                       FX_DWORD dwNameLen = (FX_DWORD)name.GetLength();\r
-                       sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD));\r
-                       sData.AppendString(name);\r
-\r
-                       sData.AppendBlock(&wType, sizeof(FX_DWORD));\r
-               }\r
-               break;\r
-       default:\r
-               break;\r
-       }\r
-}\r
-\r
+// 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 "JS_GlobalData.h"
+
+#include "../../../core/include/fdrm/fx_crypt.h"
+#include "../../include/javascript/IJavaScript.h"
+
+#define JS_MAXGLOBALDATA (1024 * 4 - 8)
+
+/* --------------------- CJS_GlobalVariableArray --------------------- */
+
+CJS_GlobalVariableArray::CJS_GlobalVariableArray() {}
+
+CJS_GlobalVariableArray::~CJS_GlobalVariableArray() {
+  Empty();
+}
+
+void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array) {
+  Empty();
+  for (int i = 0, sz = array.Count(); i < sz; i++) {
+    CJS_KeyValue* pOldObjData = array.GetAt(i);
+    ASSERT(pOldObjData != NULL);
+
+    switch (pOldObjData->nType) {
+      case JS_GLOBALDATA_TYPE_NUMBER: {
+        CJS_KeyValue* pNewObjData = new CJS_KeyValue;
+        pNewObjData->sKey = pOldObjData->sKey;
+        pNewObjData->nType = pOldObjData->nType;
+        pNewObjData->dData = pOldObjData->dData;
+        Add(pNewObjData);
+      } break;
+      case JS_GLOBALDATA_TYPE_BOOLEAN: {
+        CJS_KeyValue* pNewObjData = new CJS_KeyValue;
+        pNewObjData->sKey = pOldObjData->sKey;
+        pNewObjData->nType = pOldObjData->nType;
+        pNewObjData->bData = pOldObjData->bData;
+        Add(pNewObjData);
+      } break;
+      case JS_GLOBALDATA_TYPE_STRING: {
+        CJS_KeyValue* pNewObjData = new CJS_KeyValue;
+        pNewObjData->sKey = pOldObjData->sKey;
+        pNewObjData->nType = pOldObjData->nType;
+        pNewObjData->sData = pOldObjData->sData;
+        Add(pNewObjData);
+      } break;
+      case JS_GLOBALDATA_TYPE_OBJECT: {
+        CJS_KeyValue* pNewObjData = new CJS_KeyValue;
+        pNewObjData->sKey = pOldObjData->sKey;
+        pNewObjData->nType = pOldObjData->nType;
+        pNewObjData->objData.Copy(pOldObjData->objData);
+        Add(pNewObjData);
+      }
+      case JS_GLOBALDATA_TYPE_NULL: {
+        CJS_KeyValue* pNewObjData = new CJS_KeyValue;
+        pNewObjData->sKey = pOldObjData->sKey;
+        pNewObjData->nType = pOldObjData->nType;
+        Add(pNewObjData);
+      }
+    }
+  }
+}
+
+void CJS_GlobalVariableArray::Add(CJS_KeyValue* p) {
+  array.Add(p);
+}
+
+int CJS_GlobalVariableArray::Count() const {
+  return array.GetSize();
+}
+
+CJS_KeyValue* CJS_GlobalVariableArray::GetAt(int index) const {
+  return array.GetAt(index);
+}
+
+void CJS_GlobalVariableArray::Empty() {
+  for (int i = 0, sz = array.GetSize(); i < sz; i++)
+    delete array.GetAt(i);
+  array.RemoveAll();
+}
+
+/* -------------------------- CJS_GlobalData -------------------------- */
+
+#define READER_JS_GLOBALDATA_FILENAME L"Reader_JsGlobal.Data"
+#define PHANTOM_JS_GLOBALDATA_FILENAME L"Phantom_JsGlobal.Data"
+#define SDK_JS_GLOBALDATA_FILENAME L"SDK_JsGlobal.Data"
+
+static const uint8_t JS_RC4KEY[] = {
+    0x19, 0xa8, 0xe8, 0x01, 0xf6, 0xa8, 0xb6, 0x4d, 0x82, 0x04, 0x45, 0x6d,
+    0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51,
+    0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16,
+    0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22,
+    0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b,
+    0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a,
+    0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00,
+    0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0,
+    0xf8, 0x77, 0xd5, 0xa3};
+
+CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr;
+
+// static
+CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) {
+  if (!g_Instance) {
+    g_Instance = new CJS_GlobalData();
+  }
+  ++g_Instance->m_RefCount;
+  return g_Instance;
+}
+
+void CJS_GlobalData::Release() {
+  if (!--m_RefCount) {
+    delete g_Instance;
+    g_Instance = nullptr;
+  }
+}
+
+CJS_GlobalData::CJS_GlobalData() : m_RefCount(0) {
+  m_sFilePath += SDK_JS_GLOBALDATA_FILENAME;
+  LoadGlobalPersistentVariables();
+}
+
+CJS_GlobalData::~CJS_GlobalData() {
+  SaveGlobalPersisitentVariables();
+  for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++)
+    delete m_arrayGlobalData.GetAt(i);
+
+  m_arrayGlobalData.RemoveAll();
+}
+
+int CJS_GlobalData::FindGlobalVariable(const FX_CHAR* propname) {
+  for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) {
+    CJS_GlobalData_Element* pTemp = m_arrayGlobalData.GetAt(i);
+    if (pTemp->data.sKey[0] == *propname && pTemp->data.sKey == propname)
+      return i;
+  }
+  return -1;
+}
+
+CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable(
+    const FX_CHAR* propname) {
+  ASSERT(propname != NULL);
+
+  int nFind = FindGlobalVariable(propname);
+  if (nFind >= 0)
+    return m_arrayGlobalData.GetAt(nFind);
+
+  return NULL;
+}
+
+void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname,
+                                             double dData) {
+  ASSERT(propname != NULL);
+
+  CFX_ByteString sPropName = propname;
+  sPropName.TrimLeft();
+  sPropName.TrimRight();
+  if (sPropName.GetLength() == 0)
+    return;
+
+  if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
+    pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;
+    pData->data.dData = dData;
+  } else {
+    CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
+    pNewData->data.sKey = sPropName;
+    pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;
+    pNewData->data.dData = dData;
+    m_arrayGlobalData.Add(pNewData);
+  }
+}
+
+void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname,
+                                              bool bData) {
+  ASSERT(propname != NULL);
+  CFX_ByteString sPropName = propname;
+
+  sPropName.TrimLeft();
+  sPropName.TrimRight();
+
+  if (sPropName.GetLength() == 0)
+    return;
+
+  if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
+    pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;
+    pData->data.bData = bData;
+  } else {
+    CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
+    pNewData->data.sKey = sPropName;
+    pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;
+    pNewData->data.bData = bData;
+
+    m_arrayGlobalData.Add(pNewData);
+  }
+}
+
+void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname,
+                                             const CFX_ByteString& sData) {
+  ASSERT(propname != NULL);
+  CFX_ByteString sPropName = propname;
+
+  sPropName.TrimLeft();
+  sPropName.TrimRight();
+
+  if (sPropName.GetLength() == 0)
+    return;
+
+  if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
+    pData->data.nType = JS_GLOBALDATA_TYPE_STRING;
+    pData->data.sData = sData;
+  } else {
+    CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
+    pNewData->data.sKey = sPropName;
+    pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING;
+    pNewData->data.sData = sData;
+
+    m_arrayGlobalData.Add(pNewData);
+  }
+}
+
+void CJS_GlobalData::SetGlobalVariableObject(
+    const FX_CHAR* propname,
+    const CJS_GlobalVariableArray& array) {
+  ASSERT(propname != NULL);
+  CFX_ByteString sPropName = propname;
+
+  sPropName.TrimLeft();
+  sPropName.TrimRight();
+
+  if (sPropName.GetLength() == 0)
+    return;
+
+  if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
+    pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;
+    pData->data.objData.Copy(array);
+  } else {
+    CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
+    pNewData->data.sKey = sPropName;
+    pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;
+    pNewData->data.objData.Copy(array);
+
+    m_arrayGlobalData.Add(pNewData);
+  }
+}
+
+void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) {
+  ASSERT(propname != NULL);
+  CFX_ByteString sPropName = propname;
+
+  sPropName.TrimLeft();
+  sPropName.TrimRight();
+
+  if (sPropName.GetLength() == 0)
+    return;
+
+  if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
+    pData->data.nType = JS_GLOBALDATA_TYPE_NULL;
+  } else {
+    CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
+    pNewData->data.sKey = sPropName;
+    pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL;
+
+    m_arrayGlobalData.Add(pNewData);
+  }
+}
+
+FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname,
+                                                    FX_BOOL bPersistent) {
+  ASSERT(propname != NULL);
+  CFX_ByteString sPropName = propname;
+
+  sPropName.TrimLeft();
+  sPropName.TrimRight();
+
+  if (sPropName.GetLength() == 0)
+    return FALSE;
+
+  if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
+    pData->bPersistent = bPersistent;
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const FX_CHAR* propname) {
+  ASSERT(propname != NULL);
+  CFX_ByteString sPropName = propname;
+
+  sPropName.TrimLeft();
+  sPropName.TrimRight();
+
+  if (sPropName.GetLength() == 0)
+    return FALSE;
+
+  int nFind = FindGlobalVariable(sPropName);
+
+  if (nFind >= 0) {
+    delete m_arrayGlobalData.GetAt(nFind);
+    m_arrayGlobalData.RemoveAt(nFind);
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+int32_t CJS_GlobalData::GetSize() const {
+  return m_arrayGlobalData.GetSize();
+}
+
+CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const {
+  return m_arrayGlobalData.GetAt(index);
+}
+
+void CJS_GlobalData::LoadGlobalPersistentVariables() {
+  uint8_t* pBuffer = NULL;
+  int32_t nLength = 0;
+
+  LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength);
+  CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY));
+
+  if (pBuffer) {
+    uint8_t* p = pBuffer;
+    FX_WORD wType = *((FX_WORD*)p);
+    p += sizeof(FX_WORD);
+
+    // FX_WORD wTemp = (FX_WORD)(('X' << 8) | 'F');
+
+    if (wType == (FX_WORD)(('X' << 8) | 'F')) {
+      FX_WORD wVersion = *((FX_WORD*)p);
+      p += sizeof(FX_WORD);
+
+      ASSERT(wVersion <= 2);
+
+      FX_DWORD dwCount = *((FX_DWORD*)p);
+      p += sizeof(FX_DWORD);
+
+      FX_DWORD dwSize = *((FX_DWORD*)p);
+      p += sizeof(FX_DWORD);
+
+      if (dwSize == nLength - sizeof(FX_WORD) * 2 - sizeof(FX_DWORD) * 2) {
+        for (int32_t i = 0, sz = dwCount; i < sz; i++) {
+          if (p > pBuffer + nLength)
+            break;
+
+          FX_DWORD dwNameLen = *((FX_DWORD*)p);
+          p += sizeof(FX_DWORD);
+
+          if (p + dwNameLen > pBuffer + nLength)
+            break;
+
+          CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen);
+          p += sizeof(char) * dwNameLen;
+
+          FX_WORD wDataType = *((FX_WORD*)p);
+          p += sizeof(FX_WORD);
+
+          switch (wDataType) {
+            case JS_GLOBALDATA_TYPE_NUMBER: {
+              double dData = 0;
+              switch (wVersion) {
+                case 1: {
+                  FX_DWORD dwData = *((FX_DWORD*)p);
+                  p += sizeof(FX_DWORD);
+                  dData = dwData;
+                } break;
+                case 2: {
+                  dData = *((double*)p);
+                  p += sizeof(double);
+                } break;
+              }
+              SetGlobalVariableNumber(sEntry, dData);
+              SetGlobalVariablePersistent(sEntry, TRUE);
+            } break;
+            case JS_GLOBALDATA_TYPE_BOOLEAN: {
+              FX_WORD wData = *((FX_WORD*)p);
+              p += sizeof(FX_WORD);
+              SetGlobalVariableBoolean(sEntry, (bool)(wData == 1));
+              SetGlobalVariablePersistent(sEntry, TRUE);
+            } break;
+            case JS_GLOBALDATA_TYPE_STRING: {
+              FX_DWORD dwLength = *((FX_DWORD*)p);
+              p += sizeof(FX_DWORD);
+
+              if (p + dwLength > pBuffer + nLength)
+                break;
+
+              SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength));
+              SetGlobalVariablePersistent(sEntry, TRUE);
+              p += sizeof(char) * dwLength;
+            } break;
+            case JS_GLOBALDATA_TYPE_NULL: {
+              SetGlobalVariableNull(sEntry);
+              SetGlobalVariablePersistent(sEntry, TRUE);
+            }
+          }
+        }
+      }
+    }
+    FX_Free(pBuffer);
+  }
+}
+
+void CJS_GlobalData::SaveGlobalPersisitentVariables() {
+  FX_DWORD nCount = 0;
+  CFX_BinaryBuf sData;
+
+  for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) {
+    CJS_GlobalData_Element* pElement = m_arrayGlobalData.GetAt(i);
+    ASSERT(pElement != NULL);
+
+    if (pElement->bPersistent) {
+      CFX_BinaryBuf sElement;
+      MakeByteString(pElement->data.sKey, &pElement->data, sElement);
+
+      if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA)
+        break;
+
+      sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize());
+      nCount++;
+    }
+  }
+
+  CFX_BinaryBuf sFile;
+
+  FX_WORD wType = (FX_WORD)(('X' << 8) | 'F');
+  sFile.AppendBlock(&wType, sizeof(FX_WORD));
+  FX_WORD wVersion = 2;
+  sFile.AppendBlock(&wVersion, sizeof(FX_WORD));
+  sFile.AppendBlock(&nCount, sizeof(FX_DWORD));
+  FX_DWORD dwSize = sData.GetSize();
+  sFile.AppendBlock(&dwSize, sizeof(FX_DWORD));
+
+  sFile.AppendBlock(sData.GetBuffer(), sData.GetSize());
+
+  CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY,
+                          sizeof(JS_RC4KEY));
+  WriteFileBuffer(m_sFilePath.c_str(), (const FX_CHAR*)sFile.GetBuffer(),
+                  sFile.GetSize());
+}
+
+void CJS_GlobalData::LoadFileBuffer(const FX_WCHAR* sFilePath,
+                                    uint8_t*& pBuffer,
+                                    int32_t& nLength) {
+  // UnSupport.
+}
+
+void CJS_GlobalData::WriteFileBuffer(const FX_WCHAR* sFilePath,
+                                     const FX_CHAR* pBuffer,
+                                     int32_t nLength) {
+  // UnSupport.
+}
+
+void CJS_GlobalData::MakeByteString(const CFX_ByteString& name,
+                                    CJS_KeyValue* pData,
+                                    CFX_BinaryBuf& sData) {
+  FX_WORD wType = (FX_WORD)pData->nType;
+  switch (wType) {
+    case JS_GLOBALDATA_TYPE_NUMBER: {
+      FX_DWORD dwNameLen = (FX_DWORD)name.GetLength();
+      sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD));
+      sData.AppendString(name);
+      sData.AppendBlock(&wType, sizeof(FX_WORD));
+
+      double dData = pData->dData;
+      sData.AppendBlock(&dData, sizeof(double));
+    } break;
+    case JS_GLOBALDATA_TYPE_BOOLEAN: {
+      FX_DWORD dwNameLen = (FX_DWORD)name.GetLength();
+      sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD));
+      sData.AppendString(name);
+      sData.AppendBlock(&wType, sizeof(FX_WORD));
+
+      FX_WORD wData = (FX_WORD)pData->bData;
+      sData.AppendBlock(&wData, sizeof(FX_WORD));
+    } break;
+    case JS_GLOBALDATA_TYPE_STRING: {
+      FX_DWORD dwNameLen = (FX_DWORD)name.GetLength();
+      sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD));
+      sData.AppendString(name);
+      sData.AppendBlock(&wType, sizeof(FX_WORD));
+
+      FX_DWORD dwDataLen = (FX_DWORD)pData->sData.GetLength();
+      sData.AppendBlock(&dwDataLen, sizeof(FX_DWORD));
+      sData.AppendString(pData->sData);
+    } break;
+    case JS_GLOBALDATA_TYPE_NULL: {
+      FX_DWORD dwNameLen = (FX_DWORD)name.GetLength();
+      sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD));
+      sData.AppendString(name);
+      sData.AppendBlock(&wType, sizeof(FX_DWORD));
+    } break;
+    default:
+      break;
+  }
+}