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 | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 182 | FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent( |
| 183 | const CFX_ByteString& propname, |
| 184 | FX_BOOL bPersistent) { |
| 185 | CFX_ByteString sPropName(propname); |
| 186 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 187 | return FALSE; |
| 188 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 189 | CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName); |
| 190 | if (!pData) |
| 191 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 193 | pData->bPersistent = bPersistent; |
| 194 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | } |
| 196 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 197 | FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const CFX_ByteString& propname) { |
| 198 | CFX_ByteString sPropName(propname); |
| 199 | if (!TrimPropName(&sPropName)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 200 | return FALSE; |
| 201 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 202 | auto iter = FindGlobalVariable(sPropName); |
| 203 | if (iter == m_arrayGlobalData.end()) |
| 204 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 205 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 206 | m_arrayGlobalData.erase(iter); |
| 207 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | int32_t CJS_GlobalData::GetSize() const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 211 | return pdfium::CollectionSize<int32_t>(m_arrayGlobalData); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | CJS_GlobalData_Element* CJS_GlobalData::GetAt(int index) const { |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 215 | if (index < 0 || index >= GetSize()) |
| 216 | return nullptr; |
| 217 | return m_arrayGlobalData[index].get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void CJS_GlobalData::LoadGlobalPersistentVariables() { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 221 | uint8_t* pBuffer = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 222 | int32_t nLength = 0; |
| 223 | |
| 224 | LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength); |
| 225 | CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY)); |
| 226 | |
| 227 | if (pBuffer) { |
| 228 | uint8_t* p = pBuffer; |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 229 | uint16_t wType = *((uint16_t*)p); |
| 230 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 231 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 232 | if (wType == (uint16_t)(('X' << 8) | 'F')) { |
| 233 | uint16_t wVersion = *((uint16_t*)p); |
| 234 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 235 | |
| 236 | ASSERT(wVersion <= 2); |
| 237 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 238 | uint32_t dwCount = *((uint32_t*)p); |
| 239 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 241 | uint32_t dwSize = *((uint32_t*)p); |
| 242 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 243 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 244 | if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 245 | for (int32_t i = 0, sz = dwCount; i < sz; i++) { |
| 246 | if (p > pBuffer + nLength) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 247 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 248 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 249 | uint32_t dwNameLen = *((uint32_t*)p); |
| 250 | p += sizeof(uint32_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 251 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | if (p + dwNameLen > pBuffer + nLength) |
| 253 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 254 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 255 | CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen); |
| 256 | p += sizeof(char) * dwNameLen; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 257 | |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 258 | JS_GlobalDataType wDataType = |
| 259 | static_cast<JS_GlobalDataType>(*((uint16_t*)p)); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 260 | p += sizeof(uint16_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 261 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 262 | switch (wDataType) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 263 | case JS_GlobalDataType::NUMBER: { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 264 | double dData = 0; |
| 265 | switch (wVersion) { |
| 266 | case 1: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 267 | uint32_t dwData = *((uint32_t*)p); |
| 268 | p += sizeof(uint32_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 269 | dData = dwData; |
| 270 | } break; |
| 271 | case 2: { |
| 272 | dData = *((double*)p); |
| 273 | p += sizeof(double); |
| 274 | } break; |
| 275 | } |
| 276 | SetGlobalVariableNumber(sEntry, dData); |
| 277 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 278 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 279 | case JS_GlobalDataType::BOOLEAN: { |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 280 | uint16_t wData = *((uint16_t*)p); |
| 281 | p += sizeof(uint16_t); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 282 | SetGlobalVariableBoolean(sEntry, (bool)(wData == 1)); |
| 283 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 284 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 285 | case JS_GlobalDataType::STRING: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 286 | uint32_t dwLength = *((uint32_t*)p); |
| 287 | p += sizeof(uint32_t); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 288 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 289 | if (p + dwLength > pBuffer + nLength) |
| 290 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 291 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength)); |
| 293 | SetGlobalVariablePersistent(sEntry, TRUE); |
| 294 | p += sizeof(char) * dwLength; |
| 295 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 296 | case JS_GlobalDataType::NULLOBJ: { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 297 | SetGlobalVariableNull(sEntry); |
| 298 | SetGlobalVariablePersistent(sEntry, TRUE); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 299 | } |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 300 | case JS_GlobalDataType::OBJECT: |
| 301 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 303 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 305 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 306 | FX_Free(pBuffer); |
| 307 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 310 | void CJS_GlobalData::SaveGlobalPersisitentVariables() { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 311 | uint32_t nCount = 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 312 | CFX_BinaryBuf sData; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 313 | for (const auto& pElement : m_arrayGlobalData) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | if (pElement->bPersistent) { |
| 315 | CFX_BinaryBuf sElement; |
| 316 | MakeByteString(pElement->data.sKey, &pElement->data, sElement); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | if (sData.GetSize() + sElement.GetSize() > JS_MAXGLOBALDATA) |
| 318 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 319 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 320 | sData.AppendBlock(sElement.GetBuffer(), sElement.GetSize()); |
| 321 | nCount++; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 322 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 323 | } |
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_BinaryBuf sFile; |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 326 | uint16_t wType = (uint16_t)(('X' << 8) | 'F'); |
| 327 | sFile.AppendBlock(&wType, sizeof(uint16_t)); |
| 328 | uint16_t wVersion = 2; |
| 329 | sFile.AppendBlock(&wVersion, sizeof(uint16_t)); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 330 | sFile.AppendBlock(&nCount, sizeof(uint32_t)); |
| 331 | uint32_t dwSize = sData.GetSize(); |
| 332 | sFile.AppendBlock(&dwSize, sizeof(uint32_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 333 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 334 | sFile.AppendBlock(sData.GetBuffer(), sData.GetSize()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 335 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 336 | CRYPT_ArcFourCryptBlock(sFile.GetBuffer(), sFile.GetSize(), JS_RC4KEY, |
| 337 | sizeof(JS_RC4KEY)); |
| 338 | WriteFileBuffer(m_sFilePath.c_str(), (const FX_CHAR*)sFile.GetBuffer(), |
| 339 | sFile.GetSize()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 342 | void CJS_GlobalData::LoadFileBuffer(const FX_WCHAR* sFilePath, |
| 343 | uint8_t*& pBuffer, |
| 344 | int32_t& nLength) { |
| 345 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 348 | void CJS_GlobalData::WriteFileBuffer(const FX_WCHAR* sFilePath, |
| 349 | const FX_CHAR* pBuffer, |
| 350 | int32_t nLength) { |
| 351 | // UnSupport. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 354 | void CJS_GlobalData::MakeByteString(const CFX_ByteString& name, |
| 355 | CJS_KeyValue* pData, |
| 356 | CFX_BinaryBuf& sData) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 357 | switch (pData->nType) { |
| 358 | case JS_GlobalDataType::NUMBER: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 359 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 360 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 361 | sData.AppendString(name); |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 362 | sData.AppendBlock(&pData->nType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 363 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 364 | double dData = pData->dData; |
| 365 | sData.AppendBlock(&dData, sizeof(double)); |
| 366 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 367 | case JS_GlobalDataType::BOOLEAN: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 368 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 369 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 370 | sData.AppendString(name); |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 371 | sData.AppendBlock(&pData->nType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 372 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 373 | uint16_t wData = (uint16_t)pData->bData; |
| 374 | sData.AppendBlock(&wData, sizeof(uint16_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 375 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 376 | case JS_GlobalDataType::STRING: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 377 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 378 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 379 | sData.AppendString(name); |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 380 | sData.AppendBlock(&pData->nType, sizeof(uint16_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 381 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 382 | uint32_t dwDataLen = (uint32_t)pData->sData.GetLength(); |
| 383 | sData.AppendBlock(&dwDataLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | sData.AppendString(pData->sData); |
| 385 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 386 | case JS_GlobalDataType::NULLOBJ: { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 387 | uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 388 | sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 389 | sData.AppendString(name); |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 390 | sData.AppendBlock(&pData->nType, sizeof(uint32_t)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 391 | } break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 392 | default: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 393 | break; |
| 394 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 395 | } |