John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/javascript/JS_GlobalData.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
dsinclair | b1469a2 | 2016-09-29 10:00:05 -0700 | [diff] [blame] | 11 | #include "core/fdrm/crypto/fx_crypt.h" |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 12 | #include "third_party/base/ptr_util.h" |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 13 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 14 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 15 | #define JS_MAXGLOBALDATA (1024 * 4 - 8) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 17 | #define READER_JS_GLOBALDATA_FILENAME L"Reader_JsGlobal.Data" |
| 18 | #define PHANTOM_JS_GLOBALDATA_FILENAME L"Phantom_JsGlobal.Data" |
| 19 | #define SDK_JS_GLOBALDATA_FILENAME L"SDK_JsGlobal.Data" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | const uint8_t JS_RC4KEY[] = { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | 0x19, 0xa8, 0xe8, 0x01, 0xf6, 0xa8, 0xb6, 0x4d, 0x82, 0x04, 0x45, 0x6d, |
| 25 | 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51, |
| 26 | 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16, |
| 27 | 0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22, |
| 28 | 0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b, |
| 29 | 0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a, |
| 30 | 0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00, |
| 31 | 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0, |
| 32 | 0xf8, 0x77, 0xd5, 0xa3}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 34 | // Returns true if non-empty, setting sPropName |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 35 | bool TrimPropName(CFX_ByteString* sPropName) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 36 | sPropName->TrimLeft(); |
| 37 | sPropName->TrimRight(); |
| 38 | return sPropName->GetLength() != 0; |
| 39 | } |
| 40 | |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 41 | CJS_GlobalData* g_pInstance = nullptr; |
| 42 | |
| 43 | } // namespace |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 45 | // static |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 46 | CJS_GlobalData* CJS_GlobalData::GetRetainedInstance( |
| 47 | CPDFSDK_FormFillEnvironment* pApp) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 48 | if (!g_pInstance) { |
| 49 | g_pInstance = new CJS_GlobalData(); |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 50 | } |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 51 | ++g_pInstance->m_RefCount; |
| 52 | return g_pInstance; |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void CJS_GlobalData::Release() { |
| 56 | if (!--m_RefCount) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 57 | delete g_pInstance; |
| 58 | g_pInstance = nullptr; |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 62 | CJS_GlobalData::CJS_GlobalData() |
| 63 | : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | LoadGlobalPersistentVariables(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 67 | CJS_GlobalData::~CJS_GlobalData() { |
| 68 | SaveGlobalPersisitentVariables(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 69 | } |
| 70 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 71 | CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable( |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 72 | const CFX_ByteString& propname) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 73 | for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); |
| 74 | ++it) { |
| 75 | if ((*it)->data.sKey == propname) |
| 76 | return it; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 78 | return m_arrayGlobalData.end(); |
| 79 | } |
| 80 | |
| 81 | CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable( |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 82 | const CFX_ByteString& propname) const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 83 | for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); |
| 84 | ++it) { |
| 85 | if ((*it)->data.sKey == propname) |
| 86 | return it; |
| 87 | } |
| 88 | return m_arrayGlobalData.end(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 92 | const CFX_ByteString& propname) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 93 | auto iter = FindGlobalVariable(propname); |
| 94 | return iter != m_arrayGlobalData.end() ? iter->get() : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | } |
| 96 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 97 | void CJS_GlobalData::SetGlobalVariableNumber(const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | double dData) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 99 | CFX_ByteString sPropName(propname); |
| 100 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | return; |
| 102 | |
| 103 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 104 | pData->data.nType = JS_GlobalDataType::NUMBER; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | pData->data.dData = dData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 106 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 107 | } |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 108 | auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 109 | pNewData->data.sKey = sPropName; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 110 | pNewData->data.nType = JS_GlobalDataType::NUMBER; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 111 | pNewData->data.dData = dData; |
| 112 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 113 | } |
| 114 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 115 | void CJS_GlobalData::SetGlobalVariableBoolean(const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 116 | bool bData) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 117 | CFX_ByteString sPropName(propname); |
| 118 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 119 | return; |
| 120 | |
| 121 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 122 | pData->data.nType = JS_GlobalDataType::BOOLEAN; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | pData->data.bData = bData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 124 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 125 | } |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 126 | auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 127 | pNewData->data.sKey = sPropName; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 128 | pNewData->data.nType = JS_GlobalDataType::BOOLEAN; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 129 | pNewData->data.bData = bData; |
| 130 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 131 | } |
| 132 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 133 | void CJS_GlobalData::SetGlobalVariableString(const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 134 | const CFX_ByteString& sData) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 135 | CFX_ByteString sPropName(propname); |
| 136 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | return; |
| 138 | |
| 139 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 140 | pData->data.nType = JS_GlobalDataType::STRING; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 141 | pData->data.sData = sData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 142 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 143 | } |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 144 | auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 145 | pNewData->data.sKey = sPropName; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 146 | pNewData->data.nType = JS_GlobalDataType::STRING; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 147 | pNewData->data.sData = sData; |
| 148 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void CJS_GlobalData::SetGlobalVariableObject( |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 152 | const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 153 | const CJS_GlobalVariableArray& array) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 154 | CFX_ByteString sPropName(propname); |
| 155 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | return; |
| 157 | |
| 158 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 159 | pData->data.nType = JS_GlobalDataType::OBJECT; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 160 | pData->data.objData.Copy(array); |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 161 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 162 | } |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 163 | auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 164 | pNewData->data.sKey = sPropName; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 165 | pNewData->data.nType = JS_GlobalDataType::OBJECT; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 166 | pNewData->data.objData.Copy(array); |
| 167 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 168 | } |
| 169 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 170 | void CJS_GlobalData::SetGlobalVariableNull(const CFX_ByteString& propname) { |
| 171 | CFX_ByteString sPropName(propname); |
| 172 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 173 | return; |
| 174 | |
| 175 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 176 | pData->data.nType = JS_GlobalDataType::NULLOBJ; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 177 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | } |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 179 | auto pNewData = pdfium::MakeUnique<CJS_GlobalData_Element>(); |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 180 | pNewData->data.sKey = sPropName; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 181 | pNewData->data.nType = JS_GlobalDataType::NULLOBJ; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 182 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | } |
| 184 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 185 | bool CJS_GlobalData::SetGlobalVariablePersistent(const CFX_ByteString& propname, |
| 186 | bool bPersistent) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 187 | CFX_ByteString sPropName(propname); |
| 188 | if (!TrimPropName(&sPropName)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 189 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 190 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 191 | CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName); |
| 192 | if (!pData) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 193 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 194 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 195 | pData->bPersistent = bPersistent; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 196 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 197 | } |
| 198 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 199 | bool CJS_GlobalData::DeleteGlobalVariable(const CFX_ByteString& propname) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 200 | CFX_ByteString sPropName(propname); |
| 201 | if (!TrimPropName(&sPropName)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 202 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 204 | auto iter = FindGlobalVariable(sPropName); |
| 205 | if (iter == m_arrayGlobalData.end()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 206 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 207 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 208 | m_arrayGlobalData.erase(iter); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 209 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | int32_t CJS_GlobalData::GetSize() const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 213 | return pdfium::CollectionSize<int32_t>(m_arrayGlobalData); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 217 | if (index < 0 || index >= GetSize()) |
| 218 | return nullptr; |
| 219 | return m_arrayGlobalData[index].get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void CJS_GlobalData::LoadGlobalPersistentVariables() { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 223 | uint8_t* pBuffer = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 224 | int32_t nLength = 0; |
| 225 | |
| 226 | LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength); |
| 227 | CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY)); |
| 228 | |
| 229 | if (pBuffer) { |
| 230 | uint8_t* p = pBuffer; |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 231 | uint16_t wType = *((uint16_t*)p); |
| 232 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 233 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 234 | if (wType == (uint16_t)(('X' << 8) | 'F')) { |
| 235 | uint16_t wVersion = *((uint16_t*)p); |
| 236 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 237 | |
| 238 | ASSERT(wVersion <= 2); |
| 239 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 240 | uint32_t dwCount = *((uint32_t*)p); |
| 241 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 242 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 243 | uint32_t dwSize = *((uint32_t*)p); |
| 244 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 245 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 246 | if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 247 | for (int32_t i = 0, sz = dwCount; i < sz; i++) { |
| 248 | if (p > pBuffer + nLength) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 249 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 250 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 251 | uint32_t dwNameLen = *((uint32_t*)p); |
| 252 | p += sizeof(uint32_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 253 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 254 | if (p + dwNameLen > pBuffer + nLength) |
| 255 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 256 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen); |
| 258 | p += sizeof(char) * dwNameLen; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 259 | |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 260 | JS_GlobalDataType wDataType = |
| 261 | static_cast<JS_GlobalDataType>(*((uint16_t*)p)); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 262 | p += sizeof(uint16_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 263 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 264 | switch (wDataType) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 265 | case JS_GlobalDataType::NUMBER: { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 266 | double dData = 0; |
| 267 | switch (wVersion) { |
| 268 | case 1: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 269 | uint32_t dwData = *((uint32_t*)p); |
| 270 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | dData = dwData; |
| 272 | } break; |
| 273 | case 2: { |
| 274 | dData = *((double*)p); |
| 275 | p += sizeof(double); |
| 276 | } break; |
| 277 | } |
| 278 | SetGlobalVariableNumber(sEntry, dData); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 279 | SetGlobalVariablePersistent(sEntry, true); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 280 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 281 | case JS_GlobalDataType::BOOLEAN: { |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 282 | uint16_t wData = *((uint16_t*)p); |
| 283 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | SetGlobalVariableBoolean(sEntry, (bool)(wData == 1)); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 285 | SetGlobalVariablePersistent(sEntry, true); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 286 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 287 | case JS_GlobalDataType::STRING: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 288 | uint32_t dwLength = *((uint32_t*)p); |
| 289 | p += sizeof(uint32_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 290 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 291 | if (p + dwLength > pBuffer + nLength) |
| 292 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 293 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 294 | SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength)); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 295 | SetGlobalVariablePersistent(sEntry, true); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | p += sizeof(char) * dwLength; |
| 297 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 298 | case JS_GlobalDataType::NULLOBJ: { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 299 | SetGlobalVariableNull(sEntry); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 300 | SetGlobalVariablePersistent(sEntry, true); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 301 | } |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 302 | case JS_GlobalDataType::OBJECT: |
| 303 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 305 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 306 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 307 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 308 | FX_Free(pBuffer); |
| 309 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 312 | void CJS_GlobalData::SaveGlobalPersisitentVariables() { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 313 | uint32_t nCount = 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | CFX_BinaryBuf sData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 315 | for (const auto& pElement : m_arrayGlobalData) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 316 | if (pElement->bPersistent) { |
| 317 | CFX_BinaryBuf sElement; |
| 318 | MakeByteString(pElement->data.sKey, &pElement->data, sElement); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA) |
| 320 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 321 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 322 | sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize()); |
| 323 | nCount++; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 324 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 325 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 326 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 327 | CFX_BinaryBuf sFile; |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 328 | uint16_t wType = (uint16_t)(('X' << 8) | 'F'); |
| 329 | sFile.AppendBlock(&wType, sizeof(uint16_t)); |
| 330 | uint16_t wVersion = 2; |
| 331 | sFile.AppendBlock(&wVersion, sizeof(uint16_t)); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 332 | sFile.AppendBlock(&nCount, sizeof(uint32_t)); |
| 333 | uint32_t dwSize = sData.GetSize(); |
| 334 | sFile.AppendBlock(&dwSize, sizeof(uint32_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 335 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 336 | sFile.AppendBlock(sData.GetBuffer(), sData.GetSize()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 337 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY, |
| 339 | sizeof(JS_RC4KEY)); |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 340 | WriteFileBuffer(m_sFilePath.c_str(), (const char*)sFile.GetBuffer(), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 341 | sFile.GetSize()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 344 | void CJS_GlobalData::LoadFileBuffer(const wchar_t* sFilePath, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 345 | uint8_t*& pBuffer, |
| 346 | int32_t& nLength) { |
| 347 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 350 | void CJS_GlobalData::WriteFileBuffer(const wchar_t* sFilePath, |
| 351 | const char* pBuffer, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 352 | int32_t nLength) { |
| 353 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 356 | void CJS_GlobalData::MakeByteString(const CFX_ByteString& name, |
| 357 | CJS_KeyValue* pData, |
| 358 | CFX_BinaryBuf& sData) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 359 | switch (pData->nType) { |
| 360 | case JS_GlobalDataType::NUMBER: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 361 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 362 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 363 | sData.AppendString(name); |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 364 | sData.AppendBlock(&pData->nType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 365 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 366 | double dData = pData->dData; |
| 367 | sData.AppendBlock(&dData, sizeof(double)); |
| 368 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 369 | case JS_GlobalDataType::BOOLEAN: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 370 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 371 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 372 | sData.AppendString(name); |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 373 | sData.AppendBlock(&pData->nType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 374 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 375 | uint16_t wData = (uint16_t)pData->bData; |
| 376 | sData.AppendBlock(&wData, sizeof(uint16_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 377 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 378 | case JS_GlobalDataType::STRING: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 379 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 380 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 381 | sData.AppendString(name); |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 382 | sData.AppendBlock(&pData->nType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 383 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 384 | uint32_t dwDataLen = (uint32_t)pData->sData.GetLength(); |
| 385 | sData.AppendBlock(&dwDataLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 386 | sData.AppendString(pData->sData); |
| 387 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 388 | case JS_GlobalDataType::NULLOBJ: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 389 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 390 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 391 | sData.AppendString(name); |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 392 | sData.AppendBlock(&pData->nType, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 393 | } break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 394 | default: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 395 | break; |
| 396 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 397 | } |