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