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" |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 10 | #include "third_party/base/stl_util.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 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 14 | CJS_GlobalVariableArray::CJS_GlobalVariableArray() {} |
| 15 | |
| 16 | CJS_GlobalVariableArray::~CJS_GlobalVariableArray() { |
| 17 | Empty(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 18 | } |
| 19 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 20 | void CJS_GlobalVariableArray::Copy(const CJS_GlobalVariableArray& array) { |
| 21 | Empty(); |
| 22 | for (int i = 0, sz = array.Count(); i < sz; i++) { |
| 23 | CJS_KeyValue* pOldObjData = array.GetAt(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | switch (pOldObjData->nType) { |
| 25 | case JS_GLOBALDATA_TYPE_NUMBER: { |
| 26 | CJS_KeyValue* pNewObjData = new CJS_KeyValue; |
| 27 | pNewObjData->sKey = pOldObjData->sKey; |
| 28 | pNewObjData->nType = pOldObjData->nType; |
| 29 | pNewObjData->dData = pOldObjData->dData; |
| 30 | Add(pNewObjData); |
| 31 | } break; |
| 32 | case JS_GLOBALDATA_TYPE_BOOLEAN: { |
| 33 | CJS_KeyValue* pNewObjData = new CJS_KeyValue; |
| 34 | pNewObjData->sKey = pOldObjData->sKey; |
| 35 | pNewObjData->nType = pOldObjData->nType; |
| 36 | pNewObjData->bData = pOldObjData->bData; |
| 37 | Add(pNewObjData); |
| 38 | } break; |
| 39 | case JS_GLOBALDATA_TYPE_STRING: { |
| 40 | CJS_KeyValue* pNewObjData = new CJS_KeyValue; |
| 41 | pNewObjData->sKey = pOldObjData->sKey; |
| 42 | pNewObjData->nType = pOldObjData->nType; |
| 43 | pNewObjData->sData = pOldObjData->sData; |
| 44 | Add(pNewObjData); |
| 45 | } break; |
| 46 | case JS_GLOBALDATA_TYPE_OBJECT: { |
| 47 | CJS_KeyValue* pNewObjData = new CJS_KeyValue; |
| 48 | pNewObjData->sKey = pOldObjData->sKey; |
| 49 | pNewObjData->nType = pOldObjData->nType; |
| 50 | pNewObjData->objData.Copy(pOldObjData->objData); |
| 51 | Add(pNewObjData); |
Lei Zhang | d77f03f | 2015-12-28 13:12:26 -0800 | [diff] [blame] | 52 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 53 | case JS_GLOBALDATA_TYPE_NULL: { |
| 54 | CJS_KeyValue* pNewObjData = new CJS_KeyValue; |
| 55 | pNewObjData->sKey = pOldObjData->sKey; |
| 56 | pNewObjData->nType = pOldObjData->nType; |
| 57 | Add(pNewObjData); |
Lei Zhang | d77f03f | 2015-12-28 13:12:26 -0800 | [diff] [blame] | 58 | } break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 59 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 60 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | void CJS_GlobalVariableArray::Add(CJS_KeyValue* p) { |
| 64 | array.Add(p); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 67 | int CJS_GlobalVariableArray::Count() const { |
| 68 | return array.GetSize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | CJS_KeyValue* CJS_GlobalVariableArray::GetAt(int index) const { |
| 72 | return array.GetAt(index); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | void CJS_GlobalVariableArray::Empty() { |
| 76 | for (int i = 0, sz = array.GetSize(); i < sz; i++) |
| 77 | delete array.GetAt(i); |
| 78 | array.RemoveAll(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | #define READER_JS_GLOBALDATA_FILENAME L"Reader_JsGlobal.Data" |
| 82 | #define PHANTOM_JS_GLOBALDATA_FILENAME L"Phantom_JsGlobal.Data" |
| 83 | #define SDK_JS_GLOBALDATA_FILENAME L"SDK_JsGlobal.Data" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 84 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | static const uint8_t JS_RC4KEY[] = { |
| 86 | 0x19, 0xa8, 0xe8, 0x01, 0xf6, 0xa8, 0xb6, 0x4d, 0x82, 0x04, 0x45, 0x6d, |
| 87 | 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51, |
| 88 | 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16, |
| 89 | 0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22, |
| 90 | 0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b, |
| 91 | 0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a, |
| 92 | 0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00, |
| 93 | 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0, |
| 94 | 0xf8, 0x77, 0xd5, 0xa3}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 95 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 96 | // Returns true if non-empty, setting sPropName |
| 97 | static bool TrimPropName(const char* propname, CFX_ByteString* sPropName) { |
| 98 | ASSERT(propname); |
| 99 | *sPropName = propname; |
| 100 | sPropName->TrimLeft(); |
| 101 | sPropName->TrimRight(); |
| 102 | return sPropName->GetLength() != 0; |
| 103 | } |
| 104 | |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 105 | CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 106 | |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 107 | // static |
| 108 | CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) { |
| 109 | if (!g_Instance) { |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 110 | g_Instance = new CJS_GlobalData(); |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 111 | } |
| 112 | ++g_Instance->m_RefCount; |
| 113 | return g_Instance; |
| 114 | } |
| 115 | |
| 116 | void CJS_GlobalData::Release() { |
| 117 | if (!--m_RefCount) { |
| 118 | delete g_Instance; |
| 119 | g_Instance = nullptr; |
| 120 | } |
| 121 | } |
| 122 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 123 | CJS_GlobalData::CJS_GlobalData() |
| 124 | : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 125 | LoadGlobalPersistentVariables(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 128 | CJS_GlobalData::~CJS_GlobalData() { |
| 129 | SaveGlobalPersisitentVariables(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 130 | } |
| 131 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 132 | CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable( |
| 133 | const FX_CHAR* propname) { |
| 134 | for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); |
| 135 | ++it) { |
| 136 | if ((*it)->data.sKey == propname) |
| 137 | return it; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 138 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 139 | return m_arrayGlobalData.end(); |
| 140 | } |
| 141 | |
| 142 | CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable( |
| 143 | const FX_CHAR* propname) const { |
| 144 | for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); |
| 145 | ++it) { |
| 146 | if ((*it)->data.sKey == propname) |
| 147 | return it; |
| 148 | } |
| 149 | return m_arrayGlobalData.end(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( |
| 153 | const FX_CHAR* propname) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 154 | auto iter = FindGlobalVariable(propname); |
| 155 | return iter != m_arrayGlobalData.end() ? iter->get() : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname, |
| 159 | double dData) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 160 | CFX_ByteString sPropName; |
| 161 | if (!TrimPropName(propname, &sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 162 | return; |
| 163 | |
| 164 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 165 | pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; |
| 166 | pData->data.dData = dData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 167 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 168 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 169 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 170 | pNewData->data.sKey = sPropName; |
| 171 | pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; |
| 172 | pNewData->data.dData = dData; |
| 173 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname, |
| 177 | bool bData) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 178 | CFX_ByteString sPropName; |
| 179 | if (!TrimPropName(propname, &sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 180 | return; |
| 181 | |
| 182 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 183 | pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; |
| 184 | pData->data.bData = bData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 185 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 187 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 188 | pNewData->data.sKey = sPropName; |
| 189 | pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; |
| 190 | pNewData->data.bData = bData; |
| 191 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname, |
| 195 | const CFX_ByteString& sData) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 196 | CFX_ByteString sPropName; |
| 197 | if (!TrimPropName(propname, &sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 198 | return; |
| 199 | |
| 200 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 201 | pData->data.nType = JS_GLOBALDATA_TYPE_STRING; |
| 202 | pData->data.sData = sData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 203 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 204 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 205 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 206 | pNewData->data.sKey = sPropName; |
| 207 | pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING; |
| 208 | pNewData->data.sData = sData; |
| 209 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void CJS_GlobalData::SetGlobalVariableObject( |
| 213 | const FX_CHAR* propname, |
| 214 | const CJS_GlobalVariableArray& array) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 215 | CFX_ByteString sPropName; |
| 216 | if (!TrimPropName(propname, &sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | return; |
| 218 | |
| 219 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 220 | pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; |
| 221 | pData->data.objData.Copy(array); |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 222 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 223 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 224 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 225 | pNewData->data.sKey = sPropName; |
| 226 | pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; |
| 227 | pNewData->data.objData.Copy(array); |
| 228 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 232 | CFX_ByteString sPropName; |
| 233 | if (!TrimPropName(propname, &sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 234 | return; |
| 235 | |
| 236 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 237 | pData->data.nType = JS_GLOBALDATA_TYPE_NULL; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 238 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 239 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 240 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 241 | pNewData->data.sKey = sPropName; |
| 242 | pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL; |
| 243 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname, |
| 247 | FX_BOOL bPersistent) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 248 | CFX_ByteString sPropName; |
| 249 | if (!TrimPropName(propname, &sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 250 | return FALSE; |
| 251 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 252 | CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName); |
| 253 | if (!pData) |
| 254 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 255 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 256 | pData->bPersistent = bPersistent; |
| 257 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const FX_CHAR* propname) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 261 | CFX_ByteString sPropName; |
| 262 | if (!TrimPropName(propname, &sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 263 | return FALSE; |
| 264 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 265 | auto iter = FindGlobalVariable(sPropName); |
| 266 | if (iter == m_arrayGlobalData.end()) |
| 267 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 268 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 269 | m_arrayGlobalData.erase(iter); |
| 270 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | int32_t CJS_GlobalData::GetSize() const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 274 | return pdfium::CollectionSize<int32_t>(m_arrayGlobalData); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 278 | if (index < 0 || index >= GetSize()) |
| 279 | return nullptr; |
| 280 | return m_arrayGlobalData[index].get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void CJS_GlobalData::LoadGlobalPersistentVariables() { |
| 284 | uint8_t* pBuffer = NULL; |
| 285 | int32_t nLength = 0; |
| 286 | |
| 287 | LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength); |
| 288 | CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY)); |
| 289 | |
| 290 | if (pBuffer) { |
| 291 | uint8_t* p = pBuffer; |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 292 | uint16_t wType = *((uint16_t*)p); |
| 293 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 294 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 295 | // uint16_t wTemp = (uint16_t)(('X' << 8) | 'F'); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 297 | if (wType == (uint16_t)(('X' << 8) | 'F')) { |
| 298 | uint16_t wVersion = *((uint16_t*)p); |
| 299 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 300 | |
| 301 | ASSERT(wVersion <= 2); |
| 302 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 303 | uint32_t dwCount = *((uint32_t*)p); |
| 304 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 305 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 306 | uint32_t dwSize = *((uint32_t*)p); |
| 307 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 308 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 309 | if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 310 | for (int32_t i = 0, sz = dwCount; i < sz; i++) { |
| 311 | if (p > pBuffer + nLength) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 312 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 313 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 314 | uint32_t dwNameLen = *((uint32_t*)p); |
| 315 | p += sizeof(uint32_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 316 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | if (p + dwNameLen > pBuffer + nLength) |
| 318 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 319 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 320 | CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen); |
| 321 | p += sizeof(char) * dwNameLen; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 322 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 323 | uint16_t wDataType = *((uint16_t*)p); |
| 324 | p += sizeof(uint16_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 325 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 326 | switch (wDataType) { |
| 327 | case JS_GLOBALDATA_TYPE_NUMBER: { |
| 328 | double dData = 0; |
| 329 | switch (wVersion) { |
| 330 | case 1: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 331 | uint32_t dwData = *((uint32_t*)p); |
| 332 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 333 | dData = dwData; |
| 334 | } break; |
| 335 | case 2: { |
| 336 | dData = *((double*)p); |
| 337 | p += sizeof(double); |
| 338 | } break; |
| 339 | } |
| 340 | SetGlobalVariableNumber(sEntry, dData); |
| 341 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 342 | } break; |
| 343 | case JS_GLOBALDATA_TYPE_BOOLEAN: { |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 344 | uint16_t wData = *((uint16_t*)p); |
| 345 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 346 | SetGlobalVariableBoolean(sEntry, (bool)(wData == 1)); |
| 347 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 348 | } break; |
| 349 | case JS_GLOBALDATA_TYPE_STRING: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 350 | uint32_t dwLength = *((uint32_t*)p); |
| 351 | p += sizeof(uint32_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 352 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 353 | if (p + dwLength > pBuffer + nLength) |
| 354 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 355 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 356 | SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength)); |
| 357 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 358 | p += sizeof(char) * dwLength; |
| 359 | } break; |
| 360 | case JS_GLOBALDATA_TYPE_NULL: { |
| 361 | SetGlobalVariableNull(sEntry); |
| 362 | SetGlobalVariablePersistent(sEntry, TRUE); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 363 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 364 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 365 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 366 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 367 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 368 | FX_Free(pBuffer); |
| 369 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 372 | void CJS_GlobalData::SaveGlobalPersisitentVariables() { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 373 | uint32_t nCount = 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 374 | CFX_BinaryBuf sData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 375 | for (const auto& pElement : m_arrayGlobalData) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | if (pElement->bPersistent) { |
| 377 | CFX_BinaryBuf sElement; |
| 378 | MakeByteString(pElement->data.sKey, &pElement->data, sElement); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 379 | if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA) |
| 380 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 381 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 382 | sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize()); |
| 383 | nCount++; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 384 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 385 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 386 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 387 | CFX_BinaryBuf sFile; |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 388 | uint16_t wType = (uint16_t)(('X' << 8) | 'F'); |
| 389 | sFile.AppendBlock(&wType, sizeof(uint16_t)); |
| 390 | uint16_t wVersion = 2; |
| 391 | sFile.AppendBlock(&wVersion, sizeof(uint16_t)); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 392 | sFile.AppendBlock(&nCount, sizeof(uint32_t)); |
| 393 | uint32_t dwSize = sData.GetSize(); |
| 394 | sFile.AppendBlock(&dwSize, sizeof(uint32_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 395 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 396 | sFile.AppendBlock(sData.GetBuffer(), sData.GetSize()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 397 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 398 | CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY, |
| 399 | sizeof(JS_RC4KEY)); |
| 400 | WriteFileBuffer(m_sFilePath.c_str(), (const FX_CHAR*)sFile.GetBuffer(), |
| 401 | sFile.GetSize()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 404 | void CJS_GlobalData::LoadFileBuffer(const FX_WCHAR* sFilePath, |
| 405 | uint8_t*& pBuffer, |
| 406 | int32_t& nLength) { |
| 407 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | void CJS_GlobalData::WriteFileBuffer(const FX_WCHAR* sFilePath, |
| 411 | const FX_CHAR* pBuffer, |
| 412 | int32_t nLength) { |
| 413 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 416 | void CJS_GlobalData::MakeByteString(const CFX_ByteString& name, |
| 417 | CJS_KeyValue* pData, |
| 418 | CFX_BinaryBuf& sData) { |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 419 | uint16_t wType = (uint16_t)pData->nType; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 420 | switch (wType) { |
| 421 | case JS_GLOBALDATA_TYPE_NUMBER: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 422 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 423 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 424 | sData.AppendString(name); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 425 | sData.AppendBlock(&wType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 426 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | double dData = pData->dData; |
| 428 | sData.AppendBlock(&dData, sizeof(double)); |
| 429 | } break; |
| 430 | case JS_GLOBALDATA_TYPE_BOOLEAN: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 431 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 432 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 433 | sData.AppendString(name); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 434 | sData.AppendBlock(&wType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 435 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 436 | uint16_t wData = (uint16_t)pData->bData; |
| 437 | sData.AppendBlock(&wData, sizeof(uint16_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 438 | } break; |
| 439 | case JS_GLOBALDATA_TYPE_STRING: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 440 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 441 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | sData.AppendString(name); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 443 | sData.AppendBlock(&wType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 444 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 445 | uint32_t dwDataLen = (uint32_t)pData->sData.GetLength(); |
| 446 | sData.AppendBlock(&dwDataLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 447 | sData.AppendString(pData->sData); |
| 448 | } break; |
| 449 | case JS_GLOBALDATA_TYPE_NULL: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 450 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 451 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 452 | sData.AppendString(name); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 453 | sData.AppendBlock(&wType, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 454 | } break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 455 | default: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 456 | break; |
| 457 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 458 | } |