blob: 4e3782ec2037c5f5f4ae56bc3047f453740502ec [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
dsinclair735606d2016-10-05 15:47:02 -070043CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(
44 CPDFSDK_FormFillEnvironment* pApp) {
weili47228ac2016-07-20 10:35:31 -070045 if (!g_pInstance) {
46 g_pInstance = new CJS_GlobalData();
Tom Sepezf4583622015-09-14 15:06:53 -070047 }
weili47228ac2016-07-20 10:35:31 -070048 ++g_pInstance->m_RefCount;
49 return g_pInstance;
Tom Sepezf4583622015-09-14 15:06:53 -070050}
51
52void CJS_GlobalData::Release() {
53 if (!--m_RefCount) {
weili47228ac2016-07-20 10:35:31 -070054 delete g_pInstance;
55 g_pInstance = nullptr;
Tom Sepezf4583622015-09-14 15:06:53 -070056 }
57}
58
tsepez41a53ad2016-03-28 16:59:30 -070059CJS_GlobalData::CJS_GlobalData()
60 : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 LoadGlobalPersistentVariables();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062}
63
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064CJS_GlobalData::~CJS_GlobalData() {
65 SaveGlobalPersisitentVariables();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066}
67
tsepez41a53ad2016-03-28 16:59:30 -070068CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable(
tsepez24a48882016-04-11 15:18:40 -070069 const CFX_ByteString& propname) {
tsepez41a53ad2016-03-28 16:59:30 -070070 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
71 ++it) {
72 if ((*it)->data.sKey == propname)
73 return it;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 }
tsepez41a53ad2016-03-28 16:59:30 -070075 return m_arrayGlobalData.end();
76}
77
78CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable(
tsepez24a48882016-04-11 15:18:40 -070079 const CFX_ByteString& propname) const {
tsepez41a53ad2016-03-28 16:59:30 -070080 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
81 ++it) {
82 if ((*it)->data.sKey == propname)
83 return it;
84 }
85 return m_arrayGlobalData.end();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086}
87
88CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable(
tsepez24a48882016-04-11 15:18:40 -070089 const CFX_ByteString& propname) {
tsepez41a53ad2016-03-28 16:59:30 -070090 auto iter = FindGlobalVariable(propname);
91 return iter != m_arrayGlobalData.end() ? iter->get() : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092}
93
tsepez24a48882016-04-11 15:18:40 -070094void CJS_GlobalData::SetGlobalVariableNumber(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 double dData) {
tsepez24a48882016-04-11 15:18:40 -070096 CFX_ByteString sPropName(propname);
97 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 return;
99
100 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700101 pData->data.nType = JS_GlobalDataType::NUMBER;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 pData->data.dData = dData;
tsepez41a53ad2016-03-28 16:59:30 -0700103 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 }
tsepez41a53ad2016-03-28 16:59:30 -0700105 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
106 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700107 pNewData->data.nType = JS_GlobalDataType::NUMBER;
tsepez41a53ad2016-03-28 16:59:30 -0700108 pNewData->data.dData = dData;
109 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110}
111
tsepez24a48882016-04-11 15:18:40 -0700112void CJS_GlobalData::SetGlobalVariableBoolean(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 bool bData) {
tsepez24a48882016-04-11 15:18:40 -0700114 CFX_ByteString sPropName(propname);
115 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 return;
117
118 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700119 pData->data.nType = JS_GlobalDataType::BOOLEAN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 pData->data.bData = bData;
tsepez41a53ad2016-03-28 16:59:30 -0700121 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 }
tsepez41a53ad2016-03-28 16:59:30 -0700123 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
124 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700125 pNewData->data.nType = JS_GlobalDataType::BOOLEAN;
tsepez41a53ad2016-03-28 16:59:30 -0700126 pNewData->data.bData = bData;
127 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128}
129
tsepez24a48882016-04-11 15:18:40 -0700130void CJS_GlobalData::SetGlobalVariableString(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 const CFX_ByteString& sData) {
tsepez24a48882016-04-11 15:18:40 -0700132 CFX_ByteString sPropName(propname);
133 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 return;
135
136 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700137 pData->data.nType = JS_GlobalDataType::STRING;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 pData->data.sData = sData;
tsepez41a53ad2016-03-28 16:59:30 -0700139 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 }
tsepez41a53ad2016-03-28 16:59:30 -0700141 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
142 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700143 pNewData->data.nType = JS_GlobalDataType::STRING;
tsepez41a53ad2016-03-28 16:59:30 -0700144 pNewData->data.sData = sData;
145 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146}
147
148void CJS_GlobalData::SetGlobalVariableObject(
tsepez24a48882016-04-11 15:18:40 -0700149 const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 const CJS_GlobalVariableArray& array) {
tsepez24a48882016-04-11 15:18:40 -0700151 CFX_ByteString sPropName(propname);
152 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 return;
154
155 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700156 pData->data.nType = JS_GlobalDataType::OBJECT;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 pData->data.objData.Copy(array);
tsepez41a53ad2016-03-28 16:59:30 -0700158 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 }
tsepez41a53ad2016-03-28 16:59:30 -0700160 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
161 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700162 pNewData->data.nType = JS_GlobalDataType::OBJECT;
tsepez41a53ad2016-03-28 16:59:30 -0700163 pNewData->data.objData.Copy(array);
164 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165}
166
tsepez24a48882016-04-11 15:18:40 -0700167void CJS_GlobalData::SetGlobalVariableNull(const CFX_ByteString& propname) {
168 CFX_ByteString sPropName(propname);
169 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 return;
171
172 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
weili47228ac2016-07-20 10:35:31 -0700173 pData->data.nType = JS_GlobalDataType::NULLOBJ;
tsepez41a53ad2016-03-28 16:59:30 -0700174 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 }
tsepez41a53ad2016-03-28 16:59:30 -0700176 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
177 pNewData->data.sKey = sPropName;
weili47228ac2016-07-20 10:35:31 -0700178 pNewData->data.nType = JS_GlobalDataType::NULLOBJ;
tsepez41a53ad2016-03-28 16:59:30 -0700179 m_arrayGlobalData.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180}
181
tsepez24a48882016-04-11 15:18:40 -0700182FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(
183 const CFX_ByteString& propname,
184 FX_BOOL bPersistent) {
185 CFX_ByteString sPropName(propname);
186 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 return FALSE;
188
tsepez41a53ad2016-03-28 16:59:30 -0700189 CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName);
190 if (!pData)
191 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192
tsepez41a53ad2016-03-28 16:59:30 -0700193 pData->bPersistent = bPersistent;
194 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195}
196
tsepez24a48882016-04-11 15:18:40 -0700197FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const CFX_ByteString& propname) {
198 CFX_ByteString sPropName(propname);
199 if (!TrimPropName(&sPropName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 return FALSE;
201
tsepez41a53ad2016-03-28 16:59:30 -0700202 auto iter = FindGlobalVariable(sPropName);
203 if (iter == m_arrayGlobalData.end())
204 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205
tsepez41a53ad2016-03-28 16:59:30 -0700206 m_arrayGlobalData.erase(iter);
207 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208}
209
210int32_t CJS_GlobalData::GetSize() const {
tsepez41a53ad2016-03-28 16:59:30 -0700211 return pdfium::CollectionSize<int32_t>(m_arrayGlobalData);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212}
213
214CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const {
tsepez41a53ad2016-03-28 16:59:30 -0700215 if (index < 0 || index >= GetSize())
216 return nullptr;
217 return m_arrayGlobalData[index].get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218}
219
220void CJS_GlobalData::LoadGlobalPersistentVariables() {
thestig1cd352e2016-06-07 17:53:06 -0700221 uint8_t* pBuffer = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 int32_t nLength = 0;
223
224 LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength);
225 CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY));
226
227 if (pBuffer) {
228 uint8_t* p = pBuffer;
Tom Sepez62a70f92016-03-21 15:00:20 -0700229 uint16_t wType = *((uint16_t*)p);
230 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231
Tom Sepez62a70f92016-03-21 15:00:20 -0700232 if (wType == (uint16_t)(('X' << 8) | 'F')) {
233 uint16_t wVersion = *((uint16_t*)p);
234 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235
236 ASSERT(wVersion <= 2);
237
tsepezc3255f52016-03-25 14:52:27 -0700238 uint32_t dwCount = *((uint32_t*)p);
239 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240
tsepezc3255f52016-03-25 14:52:27 -0700241 uint32_t dwSize = *((uint32_t*)p);
242 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243
tsepezc3255f52016-03-25 14:52:27 -0700244 if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 for (int32_t i = 0, sz = dwCount; i < sz; i++) {
246 if (p > pBuffer + nLength)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700247 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248
tsepezc3255f52016-03-25 14:52:27 -0700249 uint32_t dwNameLen = *((uint32_t*)p);
250 p += sizeof(uint32_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 if (p + dwNameLen > pBuffer + nLength)
253 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen);
256 p += sizeof(char) * dwNameLen;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700257
weili47228ac2016-07-20 10:35:31 -0700258 JS_GlobalDataType wDataType =
259 static_cast<JS_GlobalDataType>(*((uint16_t*)p));
Tom Sepez62a70f92016-03-21 15:00:20 -0700260 p += sizeof(uint16_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 switch (wDataType) {
weili47228ac2016-07-20 10:35:31 -0700263 case JS_GlobalDataType::NUMBER: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 double dData = 0;
265 switch (wVersion) {
266 case 1: {
tsepezc3255f52016-03-25 14:52:27 -0700267 uint32_t dwData = *((uint32_t*)p);
268 p += sizeof(uint32_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 dData = dwData;
270 } break;
271 case 2: {
272 dData = *((double*)p);
273 p += sizeof(double);
274 } break;
275 }
276 SetGlobalVariableNumber(sEntry, dData);
277 SetGlobalVariablePersistent(sEntry, TRUE);
278 } break;
weili47228ac2016-07-20 10:35:31 -0700279 case JS_GlobalDataType::BOOLEAN: {
Tom Sepez62a70f92016-03-21 15:00:20 -0700280 uint16_t wData = *((uint16_t*)p);
281 p += sizeof(uint16_t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 SetGlobalVariableBoolean(sEntry, (bool)(wData == 1));
283 SetGlobalVariablePersistent(sEntry, TRUE);
284 } break;
weili47228ac2016-07-20 10:35:31 -0700285 case JS_GlobalDataType::STRING: {
tsepezc3255f52016-03-25 14:52:27 -0700286 uint32_t dwLength = *((uint32_t*)p);
287 p += sizeof(uint32_t);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 if (p + dwLength > pBuffer + nLength)
290 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength));
293 SetGlobalVariablePersistent(sEntry, TRUE);
294 p += sizeof(char) * dwLength;
295 } break;
weili47228ac2016-07-20 10:35:31 -0700296 case JS_GlobalDataType::NULLOBJ: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 SetGlobalVariableNull(sEntry);
298 SetGlobalVariablePersistent(sEntry, TRUE);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700299 }
weili47228ac2016-07-20 10:35:31 -0700300 case JS_GlobalDataType::OBJECT:
301 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700303 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700305 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306 FX_Free(pBuffer);
307 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308}
309
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310void CJS_GlobalData::SaveGlobalPersisitentVariables() {
tsepezc3255f52016-03-25 14:52:27 -0700311 uint32_t nCount = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 CFX_BinaryBuf sData;
tsepez41a53ad2016-03-28 16:59:30 -0700313 for (const auto& pElement : m_arrayGlobalData) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 if (pElement->bPersistent) {
315 CFX_BinaryBuf sElement;
316 MakeByteString(pElement->data.sKey, &pElement->data, sElement);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA)
318 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize());
321 nCount++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700322 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 CFX_BinaryBuf sFile;
Tom Sepez62a70f92016-03-21 15:00:20 -0700326 uint16_t wType = (uint16_t)(('X' << 8) | 'F');
327 sFile.AppendBlock(&wType, sizeof(uint16_t));
328 uint16_t wVersion = 2;
329 sFile.AppendBlock(&wVersion, sizeof(uint16_t));
tsepezc3255f52016-03-25 14:52:27 -0700330 sFile.AppendBlock(&nCount, sizeof(uint32_t));
331 uint32_t dwSize = sData.GetSize();
332 sFile.AppendBlock(&dwSize, sizeof(uint32_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 sFile.AppendBlock(sData.GetBuffer(), sData.GetSize());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700335
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY,
337 sizeof(JS_RC4KEY));
338 WriteFileBuffer(m_sFilePath.c_str(), (const FX_CHAR*)sFile.GetBuffer(),
339 sFile.GetSize());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340}
341
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342void CJS_GlobalData::LoadFileBuffer(const FX_WCHAR* sFilePath,
343 uint8_t*& pBuffer,
344 int32_t& nLength) {
345 // UnSupport.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346}
347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348void CJS_GlobalData::WriteFileBuffer(const FX_WCHAR* sFilePath,
349 const FX_CHAR* pBuffer,
350 int32_t nLength) {
351 // UnSupport.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352}
353
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354void CJS_GlobalData::MakeByteString(const CFX_ByteString& name,
355 CJS_KeyValue* pData,
356 CFX_BinaryBuf& sData) {
weili47228ac2016-07-20 10:35:31 -0700357 switch (pData->nType) {
358 case JS_GlobalDataType::NUMBER: {
tsepezc3255f52016-03-25 14:52:27 -0700359 uint32_t dwNameLen = (uint32_t)name.GetLength();
360 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700362 sData.AppendBlock(&pData->nType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 double dData = pData->dData;
365 sData.AppendBlock(&dData, sizeof(double));
366 } break;
weili47228ac2016-07-20 10:35:31 -0700367 case JS_GlobalDataType::BOOLEAN: {
tsepezc3255f52016-03-25 14:52:27 -0700368 uint32_t dwNameLen = (uint32_t)name.GetLength();
369 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700371 sData.AppendBlock(&pData->nType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700372
Tom Sepez62a70f92016-03-21 15:00:20 -0700373 uint16_t wData = (uint16_t)pData->bData;
374 sData.AppendBlock(&wData, sizeof(uint16_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 } break;
weili47228ac2016-07-20 10:35:31 -0700376 case JS_GlobalDataType::STRING: {
tsepezc3255f52016-03-25 14:52:27 -0700377 uint32_t dwNameLen = (uint32_t)name.GetLength();
378 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700380 sData.AppendBlock(&pData->nType, sizeof(uint16_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700381
tsepezc3255f52016-03-25 14:52:27 -0700382 uint32_t dwDataLen = (uint32_t)pData->sData.GetLength();
383 sData.AppendBlock(&dwDataLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 sData.AppendString(pData->sData);
385 } break;
weili47228ac2016-07-20 10:35:31 -0700386 case JS_GlobalDataType::NULLOBJ: {
tsepezc3255f52016-03-25 14:52:27 -0700387 uint32_t dwNameLen = (uint32_t)name.GetLength();
388 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 sData.AppendString(name);
weili47228ac2016-07-20 10:35:31 -0700390 sData.AppendBlock(&pData->nType, sizeof(uint32_t));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 } break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700392 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 break;
394 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700395}