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