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 | 13ee55a | 2016-03-14 15:56:00 -0400 | [diff] [blame] | 9 | #include "core/fdrm/crypto/include/fx_crypt.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 10 | #include "fpdfsdk/include/javascript/IJavaScript.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 11 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 12 | #define JS_MAXGLOBALDATA (1024 * 4 - 8) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 13 | |
| 14 | /* --------------------- CJS_GlobalVariableArray --------------------- */ |
| 15 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 16 | CJS_GlobalVariableArray::CJS_GlobalVariableArray() {} |
| 17 | |
| 18 | CJS_GlobalVariableArray::~CJS_GlobalVariableArray() { |
| 19 | Empty(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | } |
| 21 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 22 | void 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 26 | 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 Zhang | d77f03f | 2015-12-28 13:12:26 -0800 | [diff] [blame] | 54 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | 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 Zhang | d77f03f | 2015-12-28 13:12:26 -0800 | [diff] [blame] | 60 | } break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 61 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 62 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 65 | void CJS_GlobalVariableArray::Add(CJS_KeyValue* p) { |
| 66 | array.Add(p); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 69 | int CJS_GlobalVariableArray::Count() const { |
| 70 | return array.GetSize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | CJS_KeyValue* CJS_GlobalVariableArray::GetAt(int index) const { |
| 74 | return array.GetAt(index); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | void 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* -------------------------- CJS_GlobalData -------------------------- */ |
| 84 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | #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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 88 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | static 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 99 | |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 100 | CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 101 | |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 102 | // static |
| 103 | CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) { |
| 104 | if (!g_Instance) { |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 105 | g_Instance = new CJS_GlobalData(); |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 106 | } |
| 107 | ++g_Instance->m_RefCount; |
| 108 | return g_Instance; |
| 109 | } |
| 110 | |
| 111 | void CJS_GlobalData::Release() { |
| 112 | if (!--m_RefCount) { |
| 113 | delete g_Instance; |
| 114 | g_Instance = nullptr; |
| 115 | } |
| 116 | } |
| 117 | |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 118 | CJS_GlobalData::CJS_GlobalData() : m_RefCount(0) { |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 119 | m_sFilePath += SDK_JS_GLOBALDATA_FILENAME; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | LoadGlobalPersistentVariables(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | CJS_GlobalData::~CJS_GlobalData() { |
| 124 | SaveGlobalPersisitentVariables(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 125 | for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) |
| 126 | delete m_arrayGlobalData.GetAt(i); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 127 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 128 | m_arrayGlobalData.RemoveAll(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 131 | int CJS_GlobalData::FindGlobalVariable(const FX_CHAR* propname) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) { |
| 133 | CJS_GlobalData_Element* pTemp = m_arrayGlobalData.GetAt(i); |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 134 | if (pTemp->data.sKey[0] == *propname && pTemp->data.sKey == propname) |
| 135 | return i; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | } |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 137 | return -1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( |
| 141 | const FX_CHAR* propname) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 142 | ASSERT(propname); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 143 | |
| 144 | int nFind = FindGlobalVariable(propname); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 145 | return nFind >= 0 ? m_arrayGlobalData.GetAt(nFind) : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname, |
| 149 | double dData) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 150 | ASSERT(propname); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 151 | 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 | |
| 169 | void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname, |
| 170 | bool bData) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 171 | ASSERT(propname); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | 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 | |
| 193 | void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname, |
| 194 | const CFX_ByteString& sData) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 195 | ASSERT(propname); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | 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 | |
| 217 | void CJS_GlobalData::SetGlobalVariableObject( |
| 218 | const FX_CHAR* propname, |
| 219 | const CJS_GlobalVariableArray& array) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 220 | ASSERT(propname); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | 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 | |
| 242 | void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 243 | ASSERT(propname); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 244 | 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 | |
| 263 | FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname, |
| 264 | FX_BOOL bPersistent) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 265 | ASSERT(propname); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 266 | 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 | |
| 282 | FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const FX_CHAR* propname) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 283 | ASSERT(propname); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | 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 | |
| 303 | int32_t CJS_GlobalData::GetSize() const { |
| 304 | return m_arrayGlobalData.GetSize(); |
| 305 | } |
| 306 | |
| 307 | CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const { |
| 308 | return m_arrayGlobalData.GetAt(index); |
| 309 | } |
| 310 | |
| 311 | void 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 Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 320 | uint16_t wType = *((uint16_t*)p); |
| 321 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 322 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 323 | // uint16_t wTemp = (uint16_t)(('X' << 8) | 'F'); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 324 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 325 | if (wType == (uint16_t)(('X' << 8) | 'F')) { |
| 326 | uint16_t wVersion = *((uint16_t*)p); |
| 327 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 328 | |
| 329 | ASSERT(wVersion <= 2); |
| 330 | |
| 331 | FX_DWORD dwCount = *((FX_DWORD*)p); |
| 332 | p += sizeof(FX_DWORD); |
| 333 | |
| 334 | FX_DWORD dwSize = *((FX_DWORD*)p); |
| 335 | p += sizeof(FX_DWORD); |
| 336 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 337 | if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(FX_DWORD) * 2) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | for (int32_t i = 0, sz = dwCount; i < sz; i++) { |
| 339 | if (p > pBuffer + nLength) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 340 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 341 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 342 | FX_DWORD dwNameLen = *((FX_DWORD*)p); |
| 343 | p += sizeof(FX_DWORD); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 344 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 345 | if (p + dwNameLen > pBuffer + nLength) |
| 346 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 347 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 348 | CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen); |
| 349 | p += sizeof(char) * dwNameLen; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 350 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 351 | uint16_t wDataType = *((uint16_t*)p); |
| 352 | p += sizeof(uint16_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 353 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 354 | switch (wDataType) { |
| 355 | case JS_GLOBALDATA_TYPE_NUMBER: { |
| 356 | double dData = 0; |
| 357 | switch (wVersion) { |
| 358 | case 1: { |
| 359 | FX_DWORD dwData = *((FX_DWORD*)p); |
| 360 | p += sizeof(FX_DWORD); |
| 361 | 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 Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 372 | uint16_t wData = *((uint16_t*)p); |
| 373 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 374 | SetGlobalVariableBoolean(sEntry, (bool)(wData == 1)); |
| 375 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 376 | } break; |
| 377 | case JS_GLOBALDATA_TYPE_STRING: { |
| 378 | FX_DWORD dwLength = *((FX_DWORD*)p); |
| 379 | p += sizeof(FX_DWORD); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 380 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 381 | if (p + dwLength > pBuffer + nLength) |
| 382 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 383 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | 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 Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 391 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 392 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 393 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 394 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 395 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 396 | FX_Free(pBuffer); |
| 397 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 400 | void CJS_GlobalData::SaveGlobalPersisitentVariables() { |
| 401 | FX_DWORD nCount = 0; |
| 402 | CFX_BinaryBuf sData; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 403 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 404 | for (int i = 0, sz = m_arrayGlobalData.GetSize(); i < sz; i++) { |
| 405 | CJS_GlobalData_Element* pElement = m_arrayGlobalData.GetAt(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 406 | if (pElement->bPersistent) { |
| 407 | CFX_BinaryBuf sElement; |
| 408 | MakeByteString(pElement->data.sKey, &pElement->data, sElement); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 409 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA) |
| 411 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 412 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 413 | sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize()); |
| 414 | nCount++; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 415 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 416 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 417 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 418 | CFX_BinaryBuf sFile; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 419 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 420 | 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)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 424 | sFile.AppendBlock(&nCount, sizeof(FX_DWORD)); |
| 425 | FX_DWORD dwSize = sData.GetSize(); |
| 426 | sFile.AppendBlock(&dwSize, sizeof(FX_DWORD)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 427 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 428 | sFile.AppendBlock(sData.GetBuffer(), sData.GetSize()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 429 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 430 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 436 | void CJS_GlobalData::LoadFileBuffer(const FX_WCHAR* sFilePath, |
| 437 | uint8_t*& pBuffer, |
| 438 | int32_t& nLength) { |
| 439 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | void CJS_GlobalData::WriteFileBuffer(const FX_WCHAR* sFilePath, |
| 443 | const FX_CHAR* pBuffer, |
| 444 | int32_t nLength) { |
| 445 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 448 | void CJS_GlobalData::MakeByteString(const CFX_ByteString& name, |
| 449 | CJS_KeyValue* pData, |
| 450 | CFX_BinaryBuf& sData) { |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 451 | uint16_t wType = (uint16_t)pData->nType; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 452 | switch (wType) { |
| 453 | case JS_GLOBALDATA_TYPE_NUMBER: { |
| 454 | FX_DWORD dwNameLen = (FX_DWORD)name.GetLength(); |
| 455 | sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD)); |
| 456 | sData.AppendString(name); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 457 | sData.AppendBlock(&wType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 458 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 459 | double dData = pData->dData; |
| 460 | sData.AppendBlock(&dData, sizeof(double)); |
| 461 | } break; |
| 462 | case JS_GLOBALDATA_TYPE_BOOLEAN: { |
| 463 | FX_DWORD dwNameLen = (FX_DWORD)name.GetLength(); |
| 464 | sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD)); |
| 465 | sData.AppendString(name); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 466 | sData.AppendBlock(&wType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 467 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 468 | uint16_t wData = (uint16_t)pData->bData; |
| 469 | sData.AppendBlock(&wData, sizeof(uint16_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 470 | } break; |
| 471 | case JS_GLOBALDATA_TYPE_STRING: { |
| 472 | FX_DWORD dwNameLen = (FX_DWORD)name.GetLength(); |
| 473 | sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD)); |
| 474 | sData.AppendString(name); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 475 | sData.AppendBlock(&wType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 476 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 477 | FX_DWORD dwDataLen = (FX_DWORD)pData->sData.GetLength(); |
| 478 | sData.AppendBlock(&dwDataLen, sizeof(FX_DWORD)); |
| 479 | sData.AppendString(pData->sData); |
| 480 | } break; |
| 481 | case JS_GLOBALDATA_TYPE_NULL: { |
| 482 | FX_DWORD dwNameLen = (FX_DWORD)name.GetLength(); |
| 483 | sData.AppendBlock(&dwNameLen, sizeof(FX_DWORD)); |
| 484 | sData.AppendString(name); |
| 485 | sData.AppendBlock(&wType, sizeof(FX_DWORD)); |
| 486 | } break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 487 | default: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 488 | break; |
| 489 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 490 | } |