blob: 2c2ec1d5c1c4eec1d9a546696ef2bfb485fd9127 [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 Sinclair13ee55a2016-03-14 15:56:00 -04009#include "core/fdrm/crypto/include/fx_crypt.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080010#include "fpdfsdk/include/javascript/IJavaScript.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070011
Nico Weber9d8ec5a2015-08-04 13:00:21 -070012#define JS_MAXGLOBALDATA (1024 * 4 - 8)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013
14/* --------------------- CJS_GlobalVariableArray --------------------- */
15
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016CJS_GlobalVariableArray::CJS_GlobalVariableArray() {}
17
18CJS_GlobalVariableArray::~CJS_GlobalVariableArray() {
19 Empty();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020}
21
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array) {
23 Empty();
24 for (int i = 0, sz = array.Count(); i < sz; i++) {
25 CJS_KeyValue* pOldObjData = array.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 switch (pOldObjData->nType) {
27 case JS_GLOBALDATA_TYPE_NUMBER: {
28 CJS_KeyValue* pNewObjData = new CJS_KeyValue;
29 pNewObjData->sKey = pOldObjData->sKey;
30 pNewObjData->nType = pOldObjData->nType;
31 pNewObjData->dData = pOldObjData->dData;
32 Add(pNewObjData);
33 } break;
34 case JS_GLOBALDATA_TYPE_BOOLEAN: {
35 CJS_KeyValue* pNewObjData = new CJS_KeyValue;
36 pNewObjData->sKey = pOldObjData->sKey;
37 pNewObjData->nType = pOldObjData->nType;
38 pNewObjData->bData = pOldObjData->bData;
39 Add(pNewObjData);
40 } break;
41 case JS_GLOBALDATA_TYPE_STRING: {
42 CJS_KeyValue* pNewObjData = new CJS_KeyValue;
43 pNewObjData->sKey = pOldObjData->sKey;
44 pNewObjData->nType = pOldObjData->nType;
45 pNewObjData->sData = pOldObjData->sData;
46 Add(pNewObjData);
47 } break;
48 case JS_GLOBALDATA_TYPE_OBJECT: {
49 CJS_KeyValue* pNewObjData = new CJS_KeyValue;
50 pNewObjData->sKey = pOldObjData->sKey;
51 pNewObjData->nType = pOldObjData->nType;
52 pNewObjData->objData.Copy(pOldObjData->objData);
53 Add(pNewObjData);
Lei Zhangd77f03f2015-12-28 13:12:26 -080054 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 case JS_GLOBALDATA_TYPE_NULL: {
56 CJS_KeyValue* pNewObjData = new CJS_KeyValue;
57 pNewObjData->sKey = pOldObjData->sKey;
58 pNewObjData->nType = pOldObjData->nType;
59 Add(pNewObjData);
Lei Zhangd77f03f2015-12-28 13:12:26 -080060 } break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070061 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063}
64
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065void CJS_GlobalVariableArray::Add(CJS_KeyValue* p) {
66 array.Add(p);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067}
68
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069int CJS_GlobalVariableArray::Count() const {
70 return array.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071}
72
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073CJS_KeyValue* CJS_GlobalVariableArray::GetAt(int index) const {
74 return array.GetAt(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075}
76
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077void CJS_GlobalVariableArray::Empty() {
78 for (int i = 0, sz = array.GetSize(); i < sz; i++)
79 delete array.GetAt(i);
80 array.RemoveAll();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081}
82
83/* -------------------------- CJS_GlobalData -------------------------- */
84
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085#define READER_JS_GLOBALDATA_FILENAME L"Reader_JsGlobal.Data"
86#define PHANTOM_JS_GLOBALDATA_FILENAME L"Phantom_JsGlobal.Data"
87#define SDK_JS_GLOBALDATA_FILENAME L"SDK_JsGlobal.Data"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089static const uint8_t JS_RC4KEY[] = {
90 0x19, 0xa8, 0xe8, 0x01, 0xf6, 0xa8, 0xb6, 0x4d, 0x82, 0x04, 0x45, 0x6d,
91 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51,
92 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16,
93 0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22,
94 0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b,
95 0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a,
96 0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00,
97 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0,
98 0xf8, 0x77, 0xd5, 0xa3};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Tom Sepezf4583622015-09-14 15:06:53 -0700100CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Tom Sepezf4583622015-09-14 15:06:53 -0700102// static
103CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) {
104 if (!g_Instance) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700105 g_Instance = new CJS_GlobalData();
Tom Sepezf4583622015-09-14 15:06:53 -0700106 }
107 ++g_Instance->m_RefCount;
108 return g_Instance;
109}
110
111void CJS_GlobalData::Release() {
112 if (!--m_RefCount) {
113 delete g_Instance;
114 g_Instance = nullptr;
115 }
116}
117
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700118CJS_GlobalData::CJS_GlobalData() : m_RefCount(0) {
Tom Sepezf4583622015-09-14 15:06:53 -0700119 m_sFilePath += SDK_JS_GLOBALDATA_FILENAME;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 LoadGlobalPersistentVariables();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121}
122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123CJS_GlobalData::~CJS_GlobalData() {
124 SaveGlobalPersisitentVariables();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++)
126 delete m_arrayGlobalData.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 m_arrayGlobalData.RemoveAll();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131int CJS_GlobalData::FindGlobalVariable(const FX_CHAR* propname) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) {
133 CJS_GlobalData_Element* pTemp = m_arrayGlobalData.GetAt(i);
Tom Sepezf4583622015-09-14 15:06:53 -0700134 if (pTemp->data.sKey[0] == *propname && pTemp->data.sKey == propname)
135 return i;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 }
Tom Sepezf4583622015-09-14 15:06:53 -0700137 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138}
139
140CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable(
141 const FX_CHAR* propname) {
Lei Zhang96660d62015-12-14 18:27:25 -0800142 ASSERT(propname);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143
144 int nFind = FindGlobalVariable(propname);
Lei Zhang96660d62015-12-14 18:27:25 -0800145 return nFind >= 0 ? m_arrayGlobalData.GetAt(nFind) : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146}
147
148void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname,
149 double dData) {
Lei Zhang96660d62015-12-14 18:27:25 -0800150 ASSERT(propname);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 CFX_ByteString sPropName = propname;
152 sPropName.TrimLeft();
153 sPropName.TrimRight();
154 if (sPropName.GetLength() == 0)
155 return;
156
157 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
158 pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;
159 pData->data.dData = dData;
160 } else {
161 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
162 pNewData->data.sKey = sPropName;
163 pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;
164 pNewData->data.dData = dData;
165 m_arrayGlobalData.Add(pNewData);
166 }
167}
168
169void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname,
170 bool bData) {
Lei Zhang96660d62015-12-14 18:27:25 -0800171 ASSERT(propname);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 CFX_ByteString sPropName = propname;
173
174 sPropName.TrimLeft();
175 sPropName.TrimRight();
176
177 if (sPropName.GetLength() == 0)
178 return;
179
180 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
181 pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;
182 pData->data.bData = bData;
183 } else {
184 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
185 pNewData->data.sKey = sPropName;
186 pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;
187 pNewData->data.bData = bData;
188
189 m_arrayGlobalData.Add(pNewData);
190 }
191}
192
193void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname,
194 const CFX_ByteString& sData) {
Lei Zhang96660d62015-12-14 18:27:25 -0800195 ASSERT(propname);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 CFX_ByteString sPropName = propname;
197
198 sPropName.TrimLeft();
199 sPropName.TrimRight();
200
201 if (sPropName.GetLength() == 0)
202 return;
203
204 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
205 pData->data.nType = JS_GLOBALDATA_TYPE_STRING;
206 pData->data.sData = sData;
207 } else {
208 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
209 pNewData->data.sKey = sPropName;
210 pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING;
211 pNewData->data.sData = sData;
212
213 m_arrayGlobalData.Add(pNewData);
214 }
215}
216
217void CJS_GlobalData::SetGlobalVariableObject(
218 const FX_CHAR* propname,
219 const CJS_GlobalVariableArray& array) {
Lei Zhang96660d62015-12-14 18:27:25 -0800220 ASSERT(propname);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 CFX_ByteString sPropName = propname;
222
223 sPropName.TrimLeft();
224 sPropName.TrimRight();
225
226 if (sPropName.GetLength() == 0)
227 return;
228
229 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
230 pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;
231 pData->data.objData.Copy(array);
232 } else {
233 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
234 pNewData->data.sKey = sPropName;
235 pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;
236 pNewData->data.objData.Copy(array);
237
238 m_arrayGlobalData.Add(pNewData);
239 }
240}
241
242void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) {
Lei Zhang96660d62015-12-14 18:27:25 -0800243 ASSERT(propname);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 CFX_ByteString sPropName = propname;
245
246 sPropName.TrimLeft();
247 sPropName.TrimRight();
248
249 if (sPropName.GetLength() == 0)
250 return;
251
252 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
253 pData->data.nType = JS_GLOBALDATA_TYPE_NULL;
254 } else {
255 CJS_GlobalData_Element* pNewData = new CJS_GlobalData_Element;
256 pNewData->data.sKey = sPropName;
257 pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL;
258
259 m_arrayGlobalData.Add(pNewData);
260 }
261}
262
263FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname,
264 FX_BOOL bPersistent) {
Lei Zhang96660d62015-12-14 18:27:25 -0800265 ASSERT(propname);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 CFX_ByteString sPropName = propname;
267
268 sPropName.TrimLeft();
269 sPropName.TrimRight();
270
271 if (sPropName.GetLength() == 0)
272 return FALSE;
273
274 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
275 pData->bPersistent = bPersistent;
276 return TRUE;
277 }
278
279 return FALSE;
280}
281
282FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const FX_CHAR* propname) {
Lei Zhang96660d62015-12-14 18:27:25 -0800283 ASSERT(propname);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 CFX_ByteString sPropName = propname;
285
286 sPropName.TrimLeft();
287 sPropName.TrimRight();
288
289 if (sPropName.GetLength() == 0)
290 return FALSE;
291
292 int nFind = FindGlobalVariable(sPropName);
293
294 if (nFind >= 0) {
295 delete m_arrayGlobalData.GetAt(nFind);
296 m_arrayGlobalData.RemoveAt(nFind);
297 return TRUE;
298 }
299
300 return FALSE;
301}
302
303int32_t CJS_GlobalData::GetSize() const {
304 return m_arrayGlobalData.GetSize();
305}
306
307CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const {
308 return m_arrayGlobalData.GetAt(index);
309}
310
311void CJS_GlobalData::LoadGlobalPersistentVariables() {
312 uint8_t* pBuffer = NULL;
313 int32_t nLength = 0;
314
315 LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength);
316 CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY));
317
318 if (pBuffer) {
319 uint8_t* p = pBuffer;
Tom Sepez62a70f92016-03-21 15:00:20 -0700320 uint16_t wType = *((uint16_t*)p);
321 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322
Tom Sepez62a70f92016-03-21 15:00:20 -0700323 // uint16_t wTemp = (uint16_t)(('X' << 8) | 'F');
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324
Tom Sepez62a70f92016-03-21 15:00:20 -0700325 if (wType == (uint16_t)(('X' << 8) | 'F')) {
326 uint16_t wVersion = *((uint16_t*)p);
327 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328
329 ASSERT(wVersion <= 2);
330
tsepezc3255f52016-03-25 14:52:27 -0700331 uint32_t dwCount = *((uint32_t*)p);
332 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333
tsepezc3255f52016-03-25 14:52:27 -0700334 uint32_t dwSize = *((uint32_t*)p);
335 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336
tsepezc3255f52016-03-25 14:52:27 -0700337 if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 for (int32_t i = 0, sz = dwCount; i < sz; i++) {
339 if (p > pBuffer + nLength)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700340 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700341
tsepezc3255f52016-03-25 14:52:27 -0700342 uint32_t dwNameLen = *((uint32_t*)p);
343 p += sizeof(uint32_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700344
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 if (p + dwNameLen > pBuffer + nLength)
346 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen);
349 p += sizeof(char) * dwNameLen;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700350
Tom Sepez62a70f92016-03-21 15:00:20 -0700351 uint16_t wDataType = *((uint16_t*)p);
352 p += sizeof(uint16_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700353
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 switch (wDataType) {
355 case JS_GLOBALDATA_TYPE_NUMBER: {
356 double dData = 0;
357 switch (wVersion) {
358 case 1: {
tsepezc3255f52016-03-25 14:52:27 -0700359 uint32_t dwData = *((uint32_t*)p);
360 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 dData = dwData;
362 } break;
363 case 2: {
364 dData = *((double*)p);
365 p += sizeof(double);
366 } break;
367 }
368 SetGlobalVariableNumber(sEntry, dData);
369 SetGlobalVariablePersistent(sEntry, TRUE);
370 } break;
371 case JS_GLOBALDATA_TYPE_BOOLEAN: {
Tom Sepez62a70f92016-03-21 15:00:20 -0700372 uint16_t wData = *((uint16_t*)p);
373 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 SetGlobalVariableBoolean(sEntry, (bool)(wData == 1));
375 SetGlobalVariablePersistent(sEntry, TRUE);
376 } break;
377 case JS_GLOBALDATA_TYPE_STRING: {
tsepezc3255f52016-03-25 14:52:27 -0700378 uint32_t dwLength = *((uint32_t*)p);
379 p += sizeof(uint32_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700380
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 if (p + dwLength > pBuffer + nLength)
382 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength));
385 SetGlobalVariablePersistent(sEntry, TRUE);
386 p += sizeof(char) * dwLength;
387 } break;
388 case JS_GLOBALDATA_TYPE_NULL: {
389 SetGlobalVariableNull(sEntry);
390 SetGlobalVariablePersistent(sEntry, TRUE);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700391 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700393 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700395 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 FX_Free(pBuffer);
397 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700398}
399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400void CJS_GlobalData::SaveGlobalPersisitentVariables() {
tsepezc3255f52016-03-25 14:52:27 -0700401 uint32_t nCount = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 CFX_BinaryBuf sData;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700403
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) {
405 CJS_GlobalData_Element* pElement = m_arrayGlobalData.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 if (pElement->bPersistent) {
407 CFX_BinaryBuf sElement;
408 MakeByteString(pElement->data.sKey, &pElement->data, sElement);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700409
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA)
411 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700412
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413 sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize());
414 nCount++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700415 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700417
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 CFX_BinaryBuf sFile;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700419
Tom Sepez62a70f92016-03-21 15:00:20 -0700420 uint16_t wType = (uint16_t)(('X' << 8) | 'F');
421 sFile.AppendBlock(&wType, sizeof(uint16_t));
422 uint16_t wVersion = 2;
423 sFile.AppendBlock(&wVersion, sizeof(uint16_t));
tsepezc3255f52016-03-25 14:52:27 -0700424 sFile.AppendBlock(&nCount, sizeof(uint32_t));
425 uint32_t dwSize = sData.GetSize();
426 sFile.AppendBlock(&dwSize, sizeof(uint32_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700427
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 sFile.AppendBlock(sData.GetBuffer(), sData.GetSize());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY,
431 sizeof(JS_RC4KEY));
432 WriteFileBuffer(m_sFilePath.c_str(), (const FX_CHAR*)sFile.GetBuffer(),
433 sFile.GetSize());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700434}
435
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436void CJS_GlobalData::LoadFileBuffer(const FX_WCHAR* sFilePath,
437 uint8_t*& pBuffer,
438 int32_t& nLength) {
439 // UnSupport.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700440}
441
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442void CJS_GlobalData::WriteFileBuffer(const FX_WCHAR* sFilePath,
443 const FX_CHAR* pBuffer,
444 int32_t nLength) {
445 // UnSupport.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700446}
447
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448void CJS_GlobalData::MakeByteString(const CFX_ByteString& name,
449 CJS_KeyValue* pData,
450 CFX_BinaryBuf& sData) {
Tom Sepez62a70f92016-03-21 15:00:20 -0700451 uint16_t wType = (uint16_t)pData->nType;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 switch (wType) {
453 case JS_GLOBALDATA_TYPE_NUMBER: {
tsepezc3255f52016-03-25 14:52:27 -0700454 uint32_t dwNameLen = (uint32_t)name.GetLength();
455 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 sData.AppendString(name);
Tom Sepez62a70f92016-03-21 15:00:20 -0700457 sData.AppendBlock(&wType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700458
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 double dData = pData->dData;
460 sData.AppendBlock(&dData, sizeof(double));
461 } break;
462 case JS_GLOBALDATA_TYPE_BOOLEAN: {
tsepezc3255f52016-03-25 14:52:27 -0700463 uint32_t dwNameLen = (uint32_t)name.GetLength();
464 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 sData.AppendString(name);
Tom Sepez62a70f92016-03-21 15:00:20 -0700466 sData.AppendBlock(&wType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700467
Tom Sepez62a70f92016-03-21 15:00:20 -0700468 uint16_t wData = (uint16_t)pData->bData;
469 sData.AppendBlock(&wData, sizeof(uint16_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 } break;
471 case JS_GLOBALDATA_TYPE_STRING: {
tsepezc3255f52016-03-25 14:52:27 -0700472 uint32_t dwNameLen = (uint32_t)name.GetLength();
473 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 sData.AppendString(name);
Tom Sepez62a70f92016-03-21 15:00:20 -0700475 sData.AppendBlock(&wType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700476
tsepezc3255f52016-03-25 14:52:27 -0700477 uint32_t dwDataLen = (uint32_t)pData->sData.GetLength();
478 sData.AppendBlock(&dwDataLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 sData.AppendString(pData->sData);
480 } break;
481 case JS_GLOBALDATA_TYPE_NULL: {
tsepezc3255f52016-03-25 14:52:27 -0700482 uint32_t dwNameLen = (uint32_t)name.GetLength();
483 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 sData.AppendString(name);
tsepezc3255f52016-03-25 14:52:27 -0700485 sData.AppendBlock(&wType, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 } break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700487 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 break;
489 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700490}