blob: c07fe24741c1af7781270f959bbbacf742a2ea49 [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
dsinclairb1469a22016-09-29 10:00:05 -07009#include "core/fdrm/crypto/fx_crypt.h"
tsepez41a53ad2016-03-28 16:59:30 -070010#include "third_party/base/stl_util.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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070014#define READER_JS_GLOBALDATA_FILENAME L"Reader_JsGlobal.Data"
15#define PHANTOM_JS_GLOBALDATA_FILENAME L"Phantom_JsGlobal.Data"
16#define SDK_JS_GLOBALDATA_FILENAME L"SDK_JsGlobal.Data"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
weili47228ac2016-07-20 10:35:31 -070018namespace {
19
20const uint8_t JS_RC4KEY[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021 0x19, 0xa8, 0xe8, 0x01, 0xf6, 0xa8, 0xb6, 0x4d, 0x82, 0x04, 0x45, 0x6d,
22 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51,
23 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16,
24 0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22,
25 0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b,
26 0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a,
27 0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00,
28 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0,
29 0xf8, 0x77, 0xd5, 0xa3};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
tsepez41a53ad2016-03-28 16:59:30 -070031// Returns true if non-empty, setting sPropName
weili47228ac2016-07-20 10:35:31 -070032bool TrimPropName(CFX_ByteString* sPropName) {
tsepez41a53ad2016-03-28 16:59:30 -070033 sPropName->TrimLeft();
34 sPropName->TrimRight();
35 return sPropName->GetLength() != 0;
36}
37
weili47228ac2016-07-20 10:35:31 -070038CJS_GlobalData* g_pInstance = nullptr;
39
40} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Tom Sepezf4583622015-09-14 15:06:53 -070042// static
dsinclair79db6092016-09-14 07:27:21 -070043CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFSDK_Environment* pApp) {
weili47228ac2016-07-20 10:35:31 -070044 if (!g_pInstance) {
45 g_pInstance = new CJS_GlobalData();
Tom Sepezf4583622015-09-14 15:06:53 -070046 }
weili47228ac2016-07-20 10:35:31 -070047 ++g_pInstance->m_RefCount;
48 return g_pInstance;
Tom Sepezf4583622015-09-14 15:06:53 -070049}
50
51void CJS_GlobalData::Release() {
52 if (!--m_RefCount) {
weili47228ac2016-07-20 10:35:31 -070053 delete g_pInstance;
54 g_pInstance = nullptr;
Tom Sepezf4583622015-09-14 15:06:53 -070055 }
56}
57
tsepez41a53ad2016-03-28 16:59:30 -070058CJS_GlobalData::CJS_GlobalData()
59 : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 LoadGlobalPersistentVariables();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061}
62
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063CJS_GlobalData::~CJS_GlobalData() {
64 SaveGlobalPersisitentVariables();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065}
66
tsepez41a53ad2016-03-28 16:59:30 -070067CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable(
tsepez24a48882016-04-11 15:18:40 -070068 const CFX_ByteString& propname) {
tsepez41a53ad2016-03-28 16:59:30 -070069 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
70 ++it) {
71 if ((*it)->data.sKey == propname)
72 return it;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 }
tsepez41a53ad2016-03-28 16:59:30 -070074 return m_arrayGlobalData.end();
75}
76
77CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable(
tsepez24a48882016-04-11 15:18:40 -070078 const CFX_ByteString& propname) const {
tsepez41a53ad2016-03-28 16:59:30 -070079 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
80 ++it) {
81 if ((*it)->data.sKey == propname)
82 return it;
83 }
84 return m_arrayGlobalData.end();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085}
86
87CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable(
tsepez24a48882016-04-11 15:18:40 -070088 const CFX_ByteString& propname) {
tsepez41a53ad2016-03-28 16:59:30 -070089 auto iter = FindGlobalVariable(propname);
90 return iter != m_arrayGlobalData.end() ? iter->get() : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091}
92
tsepez24a48882016-04-11 15:18:40 -070093void CJS_GlobalData::SetGlobalVariableNumber(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 double dData) {
tsepez24a48882016-04-11 15:18:40 -070095 CFX_ByteString sPropName(propname);
96 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 return;
98
99 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700100 pData->data.nType = JS_GlobalDataType::NUMBER;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 pData->data.dData = dData;
tsepez41a53ad2016-03-28 16:59:30 -0700102 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 }
tsepez41a53ad2016-03-28 16:59:30 -0700104 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
105 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700106 pNewData->data.nType = JS_GlobalDataType::NUMBER;
tsepez41a53ad2016-03-28 16:59:30 -0700107 pNewData->data.dData = dData;
108 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109}
110
tsepez24a48882016-04-11 15:18:40 -0700111void CJS_GlobalData::SetGlobalVariableBoolean(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 bool bData) {
tsepez24a48882016-04-11 15:18:40 -0700113 CFX_ByteString sPropName(propname);
114 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 return;
116
117 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700118 pData->data.nType = JS_GlobalDataType::BOOLEAN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 pData->data.bData = bData;
tsepez41a53ad2016-03-28 16:59:30 -0700120 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 }
tsepez41a53ad2016-03-28 16:59:30 -0700122 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
123 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700124 pNewData->data.nType = JS_GlobalDataType::BOOLEAN;
tsepez41a53ad2016-03-28 16:59:30 -0700125 pNewData->data.bData = bData;
126 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127}
128
tsepez24a48882016-04-11 15:18:40 -0700129void CJS_GlobalData::SetGlobalVariableString(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 const CFX_ByteString& sData) {
tsepez24a48882016-04-11 15:18:40 -0700131 CFX_ByteString sPropName(propname);
132 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 return;
134
135 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700136 pData->data.nType = JS_GlobalDataType::STRING;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 pData->data.sData = sData;
tsepez41a53ad2016-03-28 16:59:30 -0700138 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 }
tsepez41a53ad2016-03-28 16:59:30 -0700140 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
141 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700142 pNewData->data.nType = JS_GlobalDataType::STRING;
tsepez41a53ad2016-03-28 16:59:30 -0700143 pNewData->data.sData = sData;
144 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145}
146
147void CJS_GlobalData::SetGlobalVariableObject(
tsepez24a48882016-04-11 15:18:40 -0700148 const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 const CJS_GlobalVariableArray& array) {
tsepez24a48882016-04-11 15:18:40 -0700150 CFX_ByteString sPropName(propname);
151 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 return;
153
154 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700155 pData->data.nType = JS_GlobalDataType::OBJECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 pData->data.objData.Copy(array);
tsepez41a53ad2016-03-28 16:59:30 -0700157 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 }
tsepez41a53ad2016-03-28 16:59:30 -0700159 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
160 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700161 pNewData->data.nType = JS_GlobalDataType::OBJECT;
tsepez41a53ad2016-03-28 16:59:30 -0700162 pNewData->data.objData.Copy(array);
163 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164}
165
tsepez24a48882016-04-11 15:18:40 -0700166void CJS_GlobalData::SetGlobalVariableNull(const CFX_ByteString& propname) {
167 CFX_ByteString sPropName(propname);
168 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 return;
170
171 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700172 pData->data.nType = JS_GlobalDataType::NULLOBJ;
tsepez41a53ad2016-03-28 16:59:30 -0700173 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 }
tsepez41a53ad2016-03-28 16:59:30 -0700175 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
176 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700177 pNewData->data.nType = JS_GlobalDataType::NULLOBJ;
tsepez41a53ad2016-03-28 16:59:30 -0700178 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179}
180
tsepez24a48882016-04-11 15:18:40 -0700181FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(
182 const CFX_ByteString& propname,
183 FX_BOOL bPersistent) {
184 CFX_ByteString sPropName(propname);
185 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 return FALSE;
187
tsepez41a53ad2016-03-28 16:59:30 -0700188 CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName);
189 if (!pData)
190 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191
tsepez41a53ad2016-03-28 16:59:30 -0700192 pData->bPersistent = bPersistent;
193 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194}
195
tsepez24a48882016-04-11 15:18:40 -0700196FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const CFX_ByteString& propname) {
197 CFX_ByteString sPropName(propname);
198 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 return FALSE;
200
tsepez41a53ad2016-03-28 16:59:30 -0700201 auto iter = FindGlobalVariable(sPropName);
202 if (iter == m_arrayGlobalData.end())
203 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204
tsepez41a53ad2016-03-28 16:59:30 -0700205 m_arrayGlobalData.erase(iter);
206 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207}
208
209int32_t CJS_GlobalData::GetSize() const {
tsepez41a53ad2016-03-28 16:59:30 -0700210 return pdfium::CollectionSize<int32_t>(m_arrayGlobalData);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211}
212
213CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const {
tsepez41a53ad2016-03-28 16:59:30 -0700214 if (index < 0 || index >= GetSize())
215 return nullptr;
216 return m_arrayGlobalData[index].get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217}
218
219void CJS_GlobalData::LoadGlobalPersistentVariables() {
thestig1cd352e2016-06-07 17:53:06 -0700220 uint8_t* pBuffer = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 int32_t nLength = 0;
222
223 LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength);
224 CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY));
225
226 if (pBuffer) {
227 uint8_t* p = pBuffer;
Tom Sepez62a70f92016-03-21 15:00:20 -0700228 uint16_t wType = *((uint16_t*)p);
229 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230
Tom Sepez62a70f92016-03-21 15:00:20 -0700231 if (wType == (uint16_t)(('X' << 8) | 'F')) {
232 uint16_t wVersion = *((uint16_t*)p);
233 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234
235 ASSERT(wVersion <= 2);
236
tsepezc3255f52016-03-25 14:52:27 -0700237 uint32_t dwCount = *((uint32_t*)p);
238 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239
tsepezc3255f52016-03-25 14:52:27 -0700240 uint32_t dwSize = *((uint32_t*)p);
241 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242
tsepezc3255f52016-03-25 14:52:27 -0700243 if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 for (int32_t i = 0, sz = dwCount; i < sz; i++) {
245 if (p > pBuffer + nLength)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700246 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
tsepezc3255f52016-03-25 14:52:27 -0700248 uint32_t dwNameLen = *((uint32_t*)p);
249 p += sizeof(uint32_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 if (p + dwNameLen > pBuffer + nLength)
252 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen);
255 p += sizeof(char) * dwNameLen;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256
weili47228ac2016-07-20 10:35:31 -0700257 JS_GlobalDataType wDataType =
258 static_cast<JS_GlobalDataType>(*((uint16_t*)p));
Tom Sepez62a70f92016-03-21 15:00:20 -0700259 p += sizeof(uint16_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 switch (wDataType) {
weili47228ac2016-07-20 10:35:31 -0700262 case JS_GlobalDataType::NUMBER: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 double dData = 0;
264 switch (wVersion) {
265 case 1: {
tsepezc3255f52016-03-25 14:52:27 -0700266 uint32_t dwData = *((uint32_t*)p);
267 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 dData = dwData;
269 } break;
270 case 2: {
271 dData = *((double*)p);
272 p += sizeof(double);
273 } break;
274 }
275 SetGlobalVariableNumber(sEntry, dData);
276 SetGlobalVariablePersistent(sEntry, TRUE);
277 } break;
weili47228ac2016-07-20 10:35:31 -0700278 case JS_GlobalDataType::BOOLEAN: {
Tom Sepez62a70f92016-03-21 15:00:20 -0700279 uint16_t wData = *((uint16_t*)p);
280 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 SetGlobalVariableBoolean(sEntry, (bool)(wData == 1));
282 SetGlobalVariablePersistent(sEntry, TRUE);
283 } break;
weili47228ac2016-07-20 10:35:31 -0700284 case JS_GlobalDataType::STRING: {
tsepezc3255f52016-03-25 14:52:27 -0700285 uint32_t dwLength = *((uint32_t*)p);
286 p += sizeof(uint32_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 if (p + dwLength > pBuffer + nLength)
289 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength));
292 SetGlobalVariablePersistent(sEntry, TRUE);
293 p += sizeof(char) * dwLength;
294 } break;
weili47228ac2016-07-20 10:35:31 -0700295 case JS_GlobalDataType::NULLOBJ: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 SetGlobalVariableNull(sEntry);
297 SetGlobalVariablePersistent(sEntry, TRUE);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700298 }
weili47228ac2016-07-20 10:35:31 -0700299 case JS_GlobalDataType::OBJECT:
300 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700302 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700304 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 FX_Free(pBuffer);
306 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307}
308
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309void CJS_GlobalData::SaveGlobalPersisitentVariables() {
tsepezc3255f52016-03-25 14:52:27 -0700310 uint32_t nCount = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 CFX_BinaryBuf sData;
tsepez41a53ad2016-03-28 16:59:30 -0700312 for (const auto& pElement : m_arrayGlobalData) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 if (pElement->bPersistent) {
314 CFX_BinaryBuf sElement;
315 MakeByteString(pElement->data.sKey, &pElement->data, sElement);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA)
317 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize());
320 nCount++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700321 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700323
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 CFX_BinaryBuf sFile;
Tom Sepez62a70f92016-03-21 15:00:20 -0700325 uint16_t wType = (uint16_t)(('X' << 8) | 'F');
326 sFile.AppendBlock(&wType, sizeof(uint16_t));
327 uint16_t wVersion = 2;
328 sFile.AppendBlock(&wVersion, sizeof(uint16_t));
tsepezc3255f52016-03-25 14:52:27 -0700329 sFile.AppendBlock(&nCount, sizeof(uint32_t));
330 uint32_t dwSize = sData.GetSize();
331 sFile.AppendBlock(&dwSize, sizeof(uint32_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700332
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 sFile.AppendBlock(sData.GetBuffer(), sData.GetSize());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY,
336 sizeof(JS_RC4KEY));
337 WriteFileBuffer(m_sFilePath.c_str(), (const FX_CHAR*)sFile.GetBuffer(),
338 sFile.GetSize());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700339}
340
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341void CJS_GlobalData::LoadFileBuffer(const FX_WCHAR* sFilePath,
342 uint8_t*& pBuffer,
343 int32_t& nLength) {
344 // UnSupport.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700345}
346
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347void CJS_GlobalData::WriteFileBuffer(const FX_WCHAR* sFilePath,
348 const FX_CHAR* pBuffer,
349 int32_t nLength) {
350 // UnSupport.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351}
352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353void CJS_GlobalData::MakeByteString(const CFX_ByteString& name,
354 CJS_KeyValue* pData,
355 CFX_BinaryBuf& sData) {
weili47228ac2016-07-20 10:35:31 -0700356 switch (pData->nType) {
357 case JS_GlobalDataType::NUMBER: {
tsepezc3255f52016-03-25 14:52:27 -0700358 uint32_t dwNameLen = (uint32_t)name.GetLength();
359 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700361 sData.AppendBlock(&pData->nType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700362
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 double dData = pData->dData;
364 sData.AppendBlock(&dData, sizeof(double));
365 } break;
weili47228ac2016-07-20 10:35:31 -0700366 case JS_GlobalDataType::BOOLEAN: {
tsepezc3255f52016-03-25 14:52:27 -0700367 uint32_t dwNameLen = (uint32_t)name.GetLength();
368 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700370 sData.AppendBlock(&pData->nType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700371
Tom Sepez62a70f92016-03-21 15:00:20 -0700372 uint16_t wData = (uint16_t)pData->bData;
373 sData.AppendBlock(&wData, sizeof(uint16_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 } break;
weili47228ac2016-07-20 10:35:31 -0700375 case JS_GlobalDataType::STRING: {
tsepezc3255f52016-03-25 14:52:27 -0700376 uint32_t dwNameLen = (uint32_t)name.GetLength();
377 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700379 sData.AppendBlock(&pData->nType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700380
tsepezc3255f52016-03-25 14:52:27 -0700381 uint32_t dwDataLen = (uint32_t)pData->sData.GetLength();
382 sData.AppendBlock(&dwDataLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 sData.AppendString(pData->sData);
384 } break;
weili47228ac2016-07-20 10:35:31 -0700385 case JS_GlobalDataType::NULLOBJ: {
tsepezc3255f52016-03-25 14:52:27 -0700386 uint32_t dwNameLen = (uint32_t)name.GetLength();
387 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700389 sData.AppendBlock(&pData->nType, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 } break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700391 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 break;
393 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700394}