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