blob: 048fca05913975ddc7c435d76c00380869598cea [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/JS_GlobalData.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009#include <utility>
10
dsinclairb1469a22016-09-29 10:00:05 -070011#include "core/fdrm/crypto/fx_crypt.h"
tsepez41a53ad2016-03-28 16:59:30 -070012#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013
Nico Weber9d8ec5a2015-08-04 13:00:21 -070014#define JS_MAXGLOBALDATA (1024 * 4 - 8)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016#define READER_JS_GLOBALDATA_FILENAME L"Reader_JsGlobal.Data"
17#define PHANTOM_JS_GLOBALDATA_FILENAME L"Phantom_JsGlobal.Data"
18#define SDK_JS_GLOBALDATA_FILENAME L"SDK_JsGlobal.Data"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
weili47228ac2016-07-20 10:35:31 -070020namespace {
21
22const uint8_t JS_RC4KEY[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023 0x19, 0xa8, 0xe8, 0x01, 0xf6, 0xa8, 0xb6, 0x4d, 0x82, 0x04, 0x45, 0x6d,
24 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51,
25 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16,
26 0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22,
27 0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b,
28 0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a,
29 0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00,
30 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0,
31 0xf8, 0x77, 0xd5, 0xa3};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
tsepez41a53ad2016-03-28 16:59:30 -070033// Returns true if non-empty, setting sPropName
weili47228ac2016-07-20 10:35:31 -070034bool TrimPropName(CFX_ByteString* sPropName) {
tsepez41a53ad2016-03-28 16:59:30 -070035 sPropName->TrimLeft();
36 sPropName->TrimRight();
37 return sPropName->GetLength() != 0;
38}
39
weili47228ac2016-07-20 10:35:31 -070040CJS_GlobalData* g_pInstance = nullptr;
41
42} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
Tom Sepezf4583622015-09-14 15:06:53 -070044// static
dsinclair735606d2016-10-05 15:47:02 -070045CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(
46 CPDFSDK_FormFillEnvironment* pApp) {
weili47228ac2016-07-20 10:35:31 -070047 if (!g_pInstance) {
48 g_pInstance = new CJS_GlobalData();
Tom Sepezf4583622015-09-14 15:06:53 -070049 }
weili47228ac2016-07-20 10:35:31 -070050 ++g_pInstance->m_RefCount;
51 return g_pInstance;
Tom Sepezf4583622015-09-14 15:06:53 -070052}
53
54void CJS_GlobalData::Release() {
55 if (!--m_RefCount) {
weili47228ac2016-07-20 10:35:31 -070056 delete g_pInstance;
57 g_pInstance = nullptr;
Tom Sepezf4583622015-09-14 15:06:53 -070058 }
59}
60
tsepez41a53ad2016-03-28 16:59:30 -070061CJS_GlobalData::CJS_GlobalData()
62 : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 LoadGlobalPersistentVariables();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064}
65
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066CJS_GlobalData::~CJS_GlobalData() {
67 SaveGlobalPersisitentVariables();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068}
69
tsepez41a53ad2016-03-28 16:59:30 -070070CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable(
tsepez24a48882016-04-11 15:18:40 -070071 const CFX_ByteString& propname) {
tsepez41a53ad2016-03-28 16:59:30 -070072 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
73 ++it) {
74 if ((*it)->data.sKey == propname)
75 return it;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 }
tsepez41a53ad2016-03-28 16:59:30 -070077 return m_arrayGlobalData.end();
78}
79
80CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable(
tsepez24a48882016-04-11 15:18:40 -070081 const CFX_ByteString& propname) const {
tsepez41a53ad2016-03-28 16:59:30 -070082 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
83 ++it) {
84 if ((*it)->data.sKey == propname)
85 return it;
86 }
87 return m_arrayGlobalData.end();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088}
89
90CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable(
tsepez24a48882016-04-11 15:18:40 -070091 const CFX_ByteString& propname) {
tsepez41a53ad2016-03-28 16:59:30 -070092 auto iter = FindGlobalVariable(propname);
93 return iter != m_arrayGlobalData.end() ? iter->get() : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094}
95
tsepez24a48882016-04-11 15:18:40 -070096void CJS_GlobalData::SetGlobalVariableNumber(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 double dData) {
tsepez24a48882016-04-11 15:18:40 -070098 CFX_ByteString sPropName(propname);
99 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 return;
101
102 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700103 pData->data.nType = JS_GlobalDataType::NUMBER;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 pData->data.dData = dData;
tsepez41a53ad2016-03-28 16:59:30 -0700105 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 }
tsepez41a53ad2016-03-28 16:59:30 -0700107 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
108 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700109 pNewData->data.nType = JS_GlobalDataType::NUMBER;
tsepez41a53ad2016-03-28 16:59:30 -0700110 pNewData->data.dData = dData;
111 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112}
113
tsepez24a48882016-04-11 15:18:40 -0700114void CJS_GlobalData::SetGlobalVariableBoolean(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 bool bData) {
tsepez24a48882016-04-11 15:18:40 -0700116 CFX_ByteString sPropName(propname);
117 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 return;
119
120 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700121 pData->data.nType = JS_GlobalDataType::BOOLEAN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 pData->data.bData = bData;
tsepez41a53ad2016-03-28 16:59:30 -0700123 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 }
tsepez41a53ad2016-03-28 16:59:30 -0700125 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
126 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700127 pNewData->data.nType = JS_GlobalDataType::BOOLEAN;
tsepez41a53ad2016-03-28 16:59:30 -0700128 pNewData->data.bData = bData;
129 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130}
131
tsepez24a48882016-04-11 15:18:40 -0700132void CJS_GlobalData::SetGlobalVariableString(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 const CFX_ByteString& sData) {
tsepez24a48882016-04-11 15:18:40 -0700134 CFX_ByteString sPropName(propname);
135 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 return;
137
138 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700139 pData->data.nType = JS_GlobalDataType::STRING;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 pData->data.sData = sData;
tsepez41a53ad2016-03-28 16:59:30 -0700141 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 }
tsepez41a53ad2016-03-28 16:59:30 -0700143 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
144 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700145 pNewData->data.nType = JS_GlobalDataType::STRING;
tsepez41a53ad2016-03-28 16:59:30 -0700146 pNewData->data.sData = sData;
147 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148}
149
150void CJS_GlobalData::SetGlobalVariableObject(
tsepez24a48882016-04-11 15:18:40 -0700151 const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 const CJS_GlobalVariableArray& array) {
tsepez24a48882016-04-11 15:18:40 -0700153 CFX_ByteString sPropName(propname);
154 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 return;
156
157 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700158 pData->data.nType = JS_GlobalDataType::OBJECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 pData->data.objData.Copy(array);
tsepez41a53ad2016-03-28 16:59:30 -0700160 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 }
tsepez41a53ad2016-03-28 16:59:30 -0700162 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
163 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700164 pNewData->data.nType = JS_GlobalDataType::OBJECT;
tsepez41a53ad2016-03-28 16:59:30 -0700165 pNewData->data.objData.Copy(array);
166 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167}
168
tsepez24a48882016-04-11 15:18:40 -0700169void CJS_GlobalData::SetGlobalVariableNull(const CFX_ByteString& propname) {
170 CFX_ByteString sPropName(propname);
171 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 return;
173
174 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700175 pData->data.nType = JS_GlobalDataType::NULLOBJ;
tsepez41a53ad2016-03-28 16:59:30 -0700176 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 }
tsepez41a53ad2016-03-28 16:59:30 -0700178 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
179 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700180 pNewData->data.nType = JS_GlobalDataType::NULLOBJ;
tsepez41a53ad2016-03-28 16:59:30 -0700181 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182}
183
tsepez4cf55152016-11-02 14:37:54 -0700184bool CJS_GlobalData::SetGlobalVariablePersistent(const CFX_ByteString& propname,
185 bool bPersistent) {
tsepez24a48882016-04-11 15:18:40 -0700186 CFX_ByteString sPropName(propname);
187 if (!TrimPropName(&sPropName))
tsepez4cf55152016-11-02 14:37:54 -0700188 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189
tsepez41a53ad2016-03-28 16:59:30 -0700190 CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName);
191 if (!pData)
tsepez4cf55152016-11-02 14:37:54 -0700192 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193
tsepez41a53ad2016-03-28 16:59:30 -0700194 pData->bPersistent = bPersistent;
tsepez4cf55152016-11-02 14:37:54 -0700195 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196}
197
tsepez4cf55152016-11-02 14:37:54 -0700198bool CJS_GlobalData::DeleteGlobalVariable(const CFX_ByteString& propname) {
tsepez24a48882016-04-11 15:18:40 -0700199 CFX_ByteString sPropName(propname);
200 if (!TrimPropName(&sPropName))
tsepez4cf55152016-11-02 14:37:54 -0700201 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202
tsepez41a53ad2016-03-28 16:59:30 -0700203 auto iter = FindGlobalVariable(sPropName);
204 if (iter == m_arrayGlobalData.end())
tsepez4cf55152016-11-02 14:37:54 -0700205 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206
tsepez41a53ad2016-03-28 16:59:30 -0700207 m_arrayGlobalData.erase(iter);
tsepez4cf55152016-11-02 14:37:54 -0700208 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209}
210
211int32_t CJS_GlobalData::GetSize() const {
tsepez41a53ad2016-03-28 16:59:30 -0700212 return pdfium::CollectionSize<int32_t>(m_arrayGlobalData);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213}
214
215CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const {
tsepez41a53ad2016-03-28 16:59:30 -0700216 if (index < 0 || index >= GetSize())
217 return nullptr;
218 return m_arrayGlobalData[index].get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219}
220
221void CJS_GlobalData::LoadGlobalPersistentVariables() {
thestig1cd352e2016-06-07 17:53:06 -0700222 uint8_t* pBuffer = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 int32_t nLength = 0;
224
225 LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength);
226 CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY));
227
228 if (pBuffer) {
229 uint8_t* p = pBuffer;
Tom Sepez62a70f92016-03-21 15:00:20 -0700230 uint16_t wType = *((uint16_t*)p);
231 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232
Tom Sepez62a70f92016-03-21 15:00:20 -0700233 if (wType == (uint16_t)(('X' << 8) | 'F')) {
234 uint16_t wVersion = *((uint16_t*)p);
235 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236
237 ASSERT(wVersion <= 2);
238
tsepezc3255f52016-03-25 14:52:27 -0700239 uint32_t dwCount = *((uint32_t*)p);
240 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241
tsepezc3255f52016-03-25 14:52:27 -0700242 uint32_t dwSize = *((uint32_t*)p);
243 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244
tsepezc3255f52016-03-25 14:52:27 -0700245 if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 for (int32_t i = 0, sz = dwCount; i < sz; i++) {
247 if (p > pBuffer + nLength)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700248 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249
tsepezc3255f52016-03-25 14:52:27 -0700250 uint32_t dwNameLen = *((uint32_t*)p);
251 p += sizeof(uint32_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 if (p + dwNameLen > pBuffer + nLength)
254 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen);
257 p += sizeof(char) * dwNameLen;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258
weili47228ac2016-07-20 10:35:31 -0700259 JS_GlobalDataType wDataType =
260 static_cast<JS_GlobalDataType>(*((uint16_t*)p));
Tom Sepez62a70f92016-03-21 15:00:20 -0700261 p += sizeof(uint16_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 switch (wDataType) {
weili47228ac2016-07-20 10:35:31 -0700264 case JS_GlobalDataType::NUMBER: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 double dData = 0;
266 switch (wVersion) {
267 case 1: {
tsepezc3255f52016-03-25 14:52:27 -0700268 uint32_t dwData = *((uint32_t*)p);
269 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 dData = dwData;
271 } break;
272 case 2: {
273 dData = *((double*)p);
274 p += sizeof(double);
275 } break;
276 }
277 SetGlobalVariableNumber(sEntry, dData);
tsepez4cf55152016-11-02 14:37:54 -0700278 SetGlobalVariablePersistent(sEntry, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 } break;
weili47228ac2016-07-20 10:35:31 -0700280 case JS_GlobalDataType::BOOLEAN: {
Tom Sepez62a70f92016-03-21 15:00:20 -0700281 uint16_t wData = *((uint16_t*)p);
282 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 SetGlobalVariableBoolean(sEntry, (bool)(wData == 1));
tsepez4cf55152016-11-02 14:37:54 -0700284 SetGlobalVariablePersistent(sEntry, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 } break;
weili47228ac2016-07-20 10:35:31 -0700286 case JS_GlobalDataType::STRING: {
tsepezc3255f52016-03-25 14:52:27 -0700287 uint32_t dwLength = *((uint32_t*)p);
288 p += sizeof(uint32_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 if (p + dwLength > pBuffer + nLength)
291 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength));
tsepez4cf55152016-11-02 14:37:54 -0700294 SetGlobalVariablePersistent(sEntry, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 p += sizeof(char) * dwLength;
296 } break;
weili47228ac2016-07-20 10:35:31 -0700297 case JS_GlobalDataType::NULLOBJ: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 SetGlobalVariableNull(sEntry);
tsepez4cf55152016-11-02 14:37:54 -0700299 SetGlobalVariablePersistent(sEntry, true);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700300 }
weili47228ac2016-07-20 10:35:31 -0700301 case JS_GlobalDataType::OBJECT:
302 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700304 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700306 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 FX_Free(pBuffer);
308 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309}
310
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311void CJS_GlobalData::SaveGlobalPersisitentVariables() {
tsepezc3255f52016-03-25 14:52:27 -0700312 uint32_t nCount = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 CFX_BinaryBuf sData;
tsepez41a53ad2016-03-28 16:59:30 -0700314 for (const auto& pElement : m_arrayGlobalData) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 if (pElement->bPersistent) {
316 CFX_BinaryBuf sElement;
317 MakeByteString(pElement->data.sKey, &pElement->data, sElement);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA)
319 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700320
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize());
322 nCount++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700323 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700325
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 CFX_BinaryBuf sFile;
Tom Sepez62a70f92016-03-21 15:00:20 -0700327 uint16_t wType = (uint16_t)(('X' << 8) | 'F');
328 sFile.AppendBlock(&wType, sizeof(uint16_t));
329 uint16_t wVersion = 2;
330 sFile.AppendBlock(&wVersion, sizeof(uint16_t));
tsepezc3255f52016-03-25 14:52:27 -0700331 sFile.AppendBlock(&nCount, sizeof(uint32_t));
332 uint32_t dwSize = sData.GetSize();
333 sFile.AppendBlock(&dwSize, sizeof(uint32_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 sFile.AppendBlock(sData.GetBuffer(), sData.GetSize());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY,
338 sizeof(JS_RC4KEY));
Dan Sinclair812e96c2017-03-13 16:43:37 -0400339 WriteFileBuffer(m_sFilePath.c_str(), (const char*)sFile.GetBuffer(),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 sFile.GetSize());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700341}
342
Dan Sinclair812e96c2017-03-13 16:43:37 -0400343void CJS_GlobalData::LoadFileBuffer(const wchar_t* sFilePath,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 uint8_t*& pBuffer,
345 int32_t& nLength) {
346 // UnSupport.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347}
348
Dan Sinclair812e96c2017-03-13 16:43:37 -0400349void CJS_GlobalData::WriteFileBuffer(const wchar_t* sFilePath,
350 const char* pBuffer,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 int32_t nLength) {
352 // UnSupport.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700353}
354
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355void CJS_GlobalData::MakeByteString(const CFX_ByteString& name,
356 CJS_KeyValue* pData,
357 CFX_BinaryBuf& sData) {
weili47228ac2016-07-20 10:35:31 -0700358 switch (pData->nType) {
359 case JS_GlobalDataType::NUMBER: {
tsepezc3255f52016-03-25 14:52:27 -0700360 uint32_t dwNameLen = (uint32_t)name.GetLength();
361 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700363 sData.AppendBlock(&pData->nType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 double dData = pData->dData;
366 sData.AppendBlock(&dData, sizeof(double));
367 } break;
weili47228ac2016-07-20 10:35:31 -0700368 case JS_GlobalDataType::BOOLEAN: {
tsepezc3255f52016-03-25 14:52:27 -0700369 uint32_t dwNameLen = (uint32_t)name.GetLength();
370 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700372 sData.AppendBlock(&pData->nType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373
Tom Sepez62a70f92016-03-21 15:00:20 -0700374 uint16_t wData = (uint16_t)pData->bData;
375 sData.AppendBlock(&wData, sizeof(uint16_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 } break;
weili47228ac2016-07-20 10:35:31 -0700377 case JS_GlobalDataType::STRING: {
tsepezc3255f52016-03-25 14:52:27 -0700378 uint32_t dwNameLen = (uint32_t)name.GetLength();
379 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700381 sData.AppendBlock(&pData->nType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700382
tsepezc3255f52016-03-25 14:52:27 -0700383 uint32_t dwDataLen = (uint32_t)pData->sData.GetLength();
384 sData.AppendBlock(&dwDataLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 sData.AppendString(pData->sData);
386 } break;
weili47228ac2016-07-20 10:35:31 -0700387 case JS_GlobalDataType::NULLOBJ: {
tsepezc3255f52016-03-25 14:52:27 -0700388 uint32_t dwNameLen = (uint32_t)name.GetLength();
389 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700391 sData.AppendBlock(&pData->nType, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 } break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700393 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 break;
395 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700396}