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) { |
weili | 9896339 | 2016-06-07 11:28:31 -0700 | [diff] [blame] | 64 | m_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 { |
weili | 9896339 | 2016-06-07 11:28:31 -0700 | [diff] [blame] | 68 | return m_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 { |
weili | 9896339 | 2016-06-07 11:28:31 -0700 | [diff] [blame] | 72 | return m_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() { |
weili | 9896339 | 2016-06-07 11:28:31 -0700 | [diff] [blame] | 76 | for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) |
| 77 | delete m_Array.GetAt(i); |
| 78 | m_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 |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 97 | static bool TrimPropName(CFX_ByteString* sPropName) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 98 | sPropName->TrimLeft(); |
| 99 | sPropName->TrimRight(); |
| 100 | return sPropName->GetLength() != 0; |
| 101 | } |
| 102 | |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 103 | CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 104 | |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 105 | // static |
| 106 | CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) { |
| 107 | if (!g_Instance) { |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 108 | g_Instance = new CJS_GlobalData(); |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 109 | } |
| 110 | ++g_Instance->m_RefCount; |
| 111 | return g_Instance; |
| 112 | } |
| 113 | |
| 114 | void CJS_GlobalData::Release() { |
| 115 | if (!--m_RefCount) { |
| 116 | delete g_Instance; |
| 117 | g_Instance = nullptr; |
| 118 | } |
| 119 | } |
| 120 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 121 | CJS_GlobalData::CJS_GlobalData() |
| 122 | : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | LoadGlobalPersistentVariables(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | CJS_GlobalData::~CJS_GlobalData() { |
| 127 | SaveGlobalPersisitentVariables(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 128 | } |
| 129 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 130 | CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable( |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 131 | const CFX_ByteString& propname) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 132 | for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); |
| 133 | ++it) { |
| 134 | if ((*it)->data.sKey == propname) |
| 135 | return it; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 137 | return m_arrayGlobalData.end(); |
| 138 | } |
| 139 | |
| 140 | CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable( |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 141 | const CFX_ByteString& propname) const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 142 | for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); |
| 143 | ++it) { |
| 144 | if ((*it)->data.sKey == propname) |
| 145 | return it; |
| 146 | } |
| 147 | return m_arrayGlobalData.end(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 151 | const CFX_ByteString& propname) { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 152 | auto iter = FindGlobalVariable(propname); |
| 153 | return iter != m_arrayGlobalData.end() ? iter->get() : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | } |
| 155 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 156 | void CJS_GlobalData::SetGlobalVariableNumber(const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 157 | double dData) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 158 | CFX_ByteString sPropName(propname); |
| 159 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 160 | return; |
| 161 | |
| 162 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 163 | pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; |
| 164 | pData->data.dData = dData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 165 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 166 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 167 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 168 | pNewData->data.sKey = sPropName; |
| 169 | pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; |
| 170 | pNewData->data.dData = dData; |
| 171 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | } |
| 173 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 174 | void CJS_GlobalData::SetGlobalVariableBoolean(const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | bool bData) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 176 | CFX_ByteString sPropName(propname); |
| 177 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | return; |
| 179 | |
| 180 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 181 | pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; |
| 182 | pData->data.bData = bData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 183 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 184 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 185 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 186 | pNewData->data.sKey = sPropName; |
| 187 | pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; |
| 188 | pNewData->data.bData = bData; |
| 189 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 190 | } |
| 191 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 192 | void CJS_GlobalData::SetGlobalVariableString(const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | const CFX_ByteString& sData) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 194 | CFX_ByteString sPropName(propname); |
| 195 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | return; |
| 197 | |
| 198 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 199 | pData->data.nType = JS_GLOBALDATA_TYPE_STRING; |
| 200 | pData->data.sData = sData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 201 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 203 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 204 | pNewData->data.sKey = sPropName; |
| 205 | pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING; |
| 206 | pNewData->data.sData = sData; |
| 207 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void CJS_GlobalData::SetGlobalVariableObject( |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 211 | const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 212 | const CJS_GlobalVariableArray& array) { |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 213 | CFX_ByteString sPropName(propname); |
| 214 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 215 | return; |
| 216 | |
| 217 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 218 | pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; |
| 219 | pData->data.objData.Copy(array); |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 220 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 222 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 223 | pNewData->data.sKey = sPropName; |
| 224 | pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; |
| 225 | pNewData->data.objData.Copy(array); |
| 226 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 227 | } |
| 228 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 229 | void CJS_GlobalData::SetGlobalVariableNull(const CFX_ByteString& propname) { |
| 230 | CFX_ByteString sPropName(propname); |
| 231 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 232 | return; |
| 233 | |
| 234 | if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 235 | pData->data.nType = JS_GLOBALDATA_TYPE_NULL; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 236 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 237 | } |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 238 | std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 239 | pNewData->data.sKey = sPropName; |
| 240 | pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL; |
| 241 | m_arrayGlobalData.push_back(std::move(pNewData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 242 | } |
| 243 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 244 | FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent( |
| 245 | const CFX_ByteString& propname, |
| 246 | FX_BOOL bPersistent) { |
| 247 | CFX_ByteString sPropName(propname); |
| 248 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 249 | return FALSE; |
| 250 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 251 | CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName); |
| 252 | if (!pData) |
| 253 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 254 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 255 | pData->bPersistent = bPersistent; |
| 256 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | } |
| 258 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 259 | FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const CFX_ByteString& propname) { |
| 260 | CFX_ByteString sPropName(propname); |
| 261 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 262 | return FALSE; |
| 263 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 264 | auto iter = FindGlobalVariable(sPropName); |
| 265 | if (iter == m_arrayGlobalData.end()) |
| 266 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 267 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 268 | m_arrayGlobalData.erase(iter); |
| 269 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | int32_t CJS_GlobalData::GetSize() const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 273 | return pdfium::CollectionSize<int32_t>(m_arrayGlobalData); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 277 | if (index < 0 || index >= GetSize()) |
| 278 | return nullptr; |
| 279 | return m_arrayGlobalData[index].get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void CJS_GlobalData::LoadGlobalPersistentVariables() { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 283 | uint8_t* pBuffer = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | int32_t nLength = 0; |
| 285 | |
| 286 | LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength); |
| 287 | CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY)); |
| 288 | |
| 289 | if (pBuffer) { |
| 290 | uint8_t* p = pBuffer; |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 291 | uint16_t wType = *((uint16_t*)p); |
| 292 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 293 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 294 | if (wType == (uint16_t)(('X' << 8) | 'F')) { |
| 295 | uint16_t wVersion = *((uint16_t*)p); |
| 296 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 297 | |
| 298 | ASSERT(wVersion <= 2); |
| 299 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 300 | uint32_t dwCount = *((uint32_t*)p); |
| 301 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 303 | uint32_t dwSize = *((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 | if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 307 | for (int32_t i = 0, sz = dwCount; i < sz; i++) { |
| 308 | if (p > pBuffer + nLength) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 309 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 310 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 311 | uint32_t dwNameLen = *((uint32_t*)p); |
| 312 | p += sizeof(uint32_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 313 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | if (p + dwNameLen > pBuffer + nLength) |
| 315 | break; |
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 | CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen); |
| 318 | p += sizeof(char) * dwNameLen; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 319 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 320 | uint16_t wDataType = *((uint16_t*)p); |
| 321 | p += sizeof(uint16_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 322 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 323 | switch (wDataType) { |
| 324 | case JS_GLOBALDATA_TYPE_NUMBER: { |
| 325 | double dData = 0; |
| 326 | switch (wVersion) { |
| 327 | case 1: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 328 | uint32_t dwData = *((uint32_t*)p); |
| 329 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 330 | dData = dwData; |
| 331 | } break; |
| 332 | case 2: { |
| 333 | dData = *((double*)p); |
| 334 | p += sizeof(double); |
| 335 | } break; |
| 336 | } |
| 337 | SetGlobalVariableNumber(sEntry, dData); |
| 338 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 339 | } break; |
| 340 | case JS_GLOBALDATA_TYPE_BOOLEAN: { |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 341 | uint16_t wData = *((uint16_t*)p); |
| 342 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 343 | SetGlobalVariableBoolean(sEntry, (bool)(wData == 1)); |
| 344 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 345 | } break; |
| 346 | case JS_GLOBALDATA_TYPE_STRING: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 347 | uint32_t dwLength = *((uint32_t*)p); |
| 348 | p += sizeof(uint32_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 349 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 350 | if (p + dwLength > pBuffer + nLength) |
| 351 | break; |
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 | SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength)); |
| 354 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 355 | p += sizeof(char) * dwLength; |
| 356 | } break; |
| 357 | case JS_GLOBALDATA_TYPE_NULL: { |
| 358 | SetGlobalVariableNull(sEntry); |
| 359 | SetGlobalVariablePersistent(sEntry, TRUE); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 360 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 361 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 362 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 363 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 364 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 365 | FX_Free(pBuffer); |
| 366 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 369 | void CJS_GlobalData::SaveGlobalPersisitentVariables() { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 370 | uint32_t nCount = 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 371 | CFX_BinaryBuf sData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 372 | for (const auto& pElement : m_arrayGlobalData) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 373 | if (pElement->bPersistent) { |
| 374 | CFX_BinaryBuf sElement; |
| 375 | MakeByteString(pElement->data.sKey, &pElement->data, sElement); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA) |
| 377 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 378 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 379 | sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize()); |
| 380 | nCount++; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 381 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 382 | } |
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 | CFX_BinaryBuf sFile; |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 385 | uint16_t wType = (uint16_t)(('X' << 8) | 'F'); |
| 386 | sFile.AppendBlock(&wType, sizeof(uint16_t)); |
| 387 | uint16_t wVersion = 2; |
| 388 | sFile.AppendBlock(&wVersion, sizeof(uint16_t)); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 389 | sFile.AppendBlock(&nCount, sizeof(uint32_t)); |
| 390 | uint32_t dwSize = sData.GetSize(); |
| 391 | sFile.AppendBlock(&dwSize, sizeof(uint32_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 392 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 393 | sFile.AppendBlock(sData.GetBuffer(), sData.GetSize()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 394 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 395 | CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY, |
| 396 | sizeof(JS_RC4KEY)); |
| 397 | WriteFileBuffer(m_sFilePath.c_str(), (const FX_CHAR*)sFile.GetBuffer(), |
| 398 | sFile.GetSize()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 401 | void CJS_GlobalData::LoadFileBuffer(const FX_WCHAR* sFilePath, |
| 402 | uint8_t*& pBuffer, |
| 403 | int32_t& nLength) { |
| 404 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 407 | void CJS_GlobalData::WriteFileBuffer(const FX_WCHAR* sFilePath, |
| 408 | const FX_CHAR* pBuffer, |
| 409 | int32_t nLength) { |
| 410 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 413 | void CJS_GlobalData::MakeByteString(const CFX_ByteString& name, |
| 414 | CJS_KeyValue* pData, |
| 415 | CFX_BinaryBuf& sData) { |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 416 | uint16_t wType = (uint16_t)pData->nType; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 417 | switch (wType) { |
| 418 | case JS_GLOBALDATA_TYPE_NUMBER: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 419 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 420 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 421 | sData.AppendString(name); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 422 | sData.AppendBlock(&wType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 423 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 424 | double dData = pData->dData; |
| 425 | sData.AppendBlock(&dData, sizeof(double)); |
| 426 | } break; |
| 427 | case JS_GLOBALDATA_TYPE_BOOLEAN: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 428 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 429 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 430 | sData.AppendString(name); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 431 | sData.AppendBlock(&wType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 432 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 433 | uint16_t wData = (uint16_t)pData->bData; |
| 434 | sData.AppendBlock(&wData, sizeof(uint16_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 435 | } break; |
| 436 | case JS_GLOBALDATA_TYPE_STRING: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 437 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 438 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 439 | sData.AppendString(name); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 440 | sData.AppendBlock(&wType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 441 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 442 | uint32_t dwDataLen = (uint32_t)pData->sData.GetLength(); |
| 443 | sData.AppendBlock(&dwDataLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 444 | sData.AppendString(pData->sData); |
| 445 | } break; |
| 446 | case JS_GLOBALDATA_TYPE_NULL: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 447 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 448 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 449 | sData.AppendString(name); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 450 | sData.AppendBlock(&wType, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 451 | } break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 452 | default: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 453 | break; |
| 454 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 455 | } |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 456 | |
| 457 | CJS_KeyValue::CJS_KeyValue() {} |
| 458 | |
| 459 | CJS_KeyValue::~CJS_KeyValue() {} |