| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "fpdfsdk/javascript/JS_GlobalData.h" | 7 #include "fpdfsdk/javascript/JS_GlobalData.h" |
| 8 | 8 |
| 9 #include "core/fdrm/crypto/include/fx_crypt.h" | 9 #include "core/fdrm/crypto/include/fx_crypt.h" |
| 10 #include "third_party/base/stl_util.h" | 10 #include "third_party/base/stl_util.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51, | 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, | 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, | 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, | 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, | 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, | 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, | 93 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0, |
| 94 0xf8, 0x77, 0xd5, 0xa3}; | 94 0xf8, 0x77, 0xd5, 0xa3}; |
| 95 | 95 |
| 96 // Returns true if non-empty, setting sPropName | 96 // Returns true if non-empty, setting sPropName |
| 97 static bool TrimPropName(const char* propname, CFX_ByteString* sPropName) { | 97 static bool TrimPropName(CFX_ByteString* sPropName) { |
| 98 ASSERT(propname); | |
| 99 *sPropName = propname; | |
| 100 sPropName->TrimLeft(); | 98 sPropName->TrimLeft(); |
| 101 sPropName->TrimRight(); | 99 sPropName->TrimRight(); |
| 102 return sPropName->GetLength() != 0; | 100 return sPropName->GetLength() != 0; |
| 103 } | 101 } |
| 104 | 102 |
| 105 CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr; | 103 CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr; |
| 106 | 104 |
| 107 // static | 105 // static |
| 108 CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) { | 106 CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) { |
| 109 if (!g_Instance) { | 107 if (!g_Instance) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 123 CJS_GlobalData::CJS_GlobalData() | 121 CJS_GlobalData::CJS_GlobalData() |
| 124 : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) { | 122 : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) { |
| 125 LoadGlobalPersistentVariables(); | 123 LoadGlobalPersistentVariables(); |
| 126 } | 124 } |
| 127 | 125 |
| 128 CJS_GlobalData::~CJS_GlobalData() { | 126 CJS_GlobalData::~CJS_GlobalData() { |
| 129 SaveGlobalPersisitentVariables(); | 127 SaveGlobalPersisitentVariables(); |
| 130 } | 128 } |
| 131 | 129 |
| 132 CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable( | 130 CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable( |
| 133 const FX_CHAR* propname) { | 131 const CFX_ByteString& propname) { |
| 134 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); | 132 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); |
| 135 ++it) { | 133 ++it) { |
| 136 if ((*it)->data.sKey == propname) | 134 if ((*it)->data.sKey == propname) |
| 137 return it; | 135 return it; |
| 138 } | 136 } |
| 139 return m_arrayGlobalData.end(); | 137 return m_arrayGlobalData.end(); |
| 140 } | 138 } |
| 141 | 139 |
| 142 CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable( | 140 CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable( |
| 143 const FX_CHAR* propname) const { | 141 const CFX_ByteString& propname) const { |
| 144 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); | 142 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); |
| 145 ++it) { | 143 ++it) { |
| 146 if ((*it)->data.sKey == propname) | 144 if ((*it)->data.sKey == propname) |
| 147 return it; | 145 return it; |
| 148 } | 146 } |
| 149 return m_arrayGlobalData.end(); | 147 return m_arrayGlobalData.end(); |
| 150 } | 148 } |
| 151 | 149 |
| 152 CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( | 150 CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( |
| 153 const FX_CHAR* propname) { | 151 const CFX_ByteString& propname) { |
| 154 auto iter = FindGlobalVariable(propname); | 152 auto iter = FindGlobalVariable(propname); |
| 155 return iter != m_arrayGlobalData.end() ? iter->get() : nullptr; | 153 return iter != m_arrayGlobalData.end() ? iter->get() : nullptr; |
| 156 } | 154 } |
| 157 | 155 |
| 158 void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname, | 156 void CJS_GlobalData::SetGlobalVariableNumber(const CFX_ByteString& propname, |
| 159 double dData) { | 157 double dData) { |
| 160 CFX_ByteString sPropName; | 158 CFX_ByteString sPropName(propname); |
| 161 if (!TrimPropName(propname, &sPropName)) | 159 if (!TrimPropName(&sPropName)) |
| 162 return; | 160 return; |
| 163 | 161 |
| 164 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { | 162 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 165 pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; | 163 pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; |
| 166 pData->data.dData = dData; | 164 pData->data.dData = dData; |
| 167 return; | 165 return; |
| 168 } | 166 } |
| 169 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); | 167 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 170 pNewData->data.sKey = sPropName; | 168 pNewData->data.sKey = sPropName; |
| 171 pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; | 169 pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; |
| 172 pNewData->data.dData = dData; | 170 pNewData->data.dData = dData; |
| 173 m_arrayGlobalData.push_back(std::move(pNewData)); | 171 m_arrayGlobalData.push_back(std::move(pNewData)); |
| 174 } | 172 } |
| 175 | 173 |
| 176 void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname, | 174 void CJS_GlobalData::SetGlobalVariableBoolean(const CFX_ByteString& propname, |
| 177 bool bData) { | 175 bool bData) { |
| 178 CFX_ByteString sPropName; | 176 CFX_ByteString sPropName(propname); |
| 179 if (!TrimPropName(propname, &sPropName)) | 177 if (!TrimPropName(&sPropName)) |
| 180 return; | 178 return; |
| 181 | 179 |
| 182 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { | 180 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 183 pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; | 181 pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; |
| 184 pData->data.bData = bData; | 182 pData->data.bData = bData; |
| 185 return; | 183 return; |
| 186 } | 184 } |
| 187 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); | 185 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 188 pNewData->data.sKey = sPropName; | 186 pNewData->data.sKey = sPropName; |
| 189 pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; | 187 pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; |
| 190 pNewData->data.bData = bData; | 188 pNewData->data.bData = bData; |
| 191 m_arrayGlobalData.push_back(std::move(pNewData)); | 189 m_arrayGlobalData.push_back(std::move(pNewData)); |
| 192 } | 190 } |
| 193 | 191 |
| 194 void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname, | 192 void CJS_GlobalData::SetGlobalVariableString(const CFX_ByteString& propname, |
| 195 const CFX_ByteString& sData) { | 193 const CFX_ByteString& sData) { |
| 196 CFX_ByteString sPropName; | 194 CFX_ByteString sPropName(propname); |
| 197 if (!TrimPropName(propname, &sPropName)) | 195 if (!TrimPropName(&sPropName)) |
| 198 return; | 196 return; |
| 199 | 197 |
| 200 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { | 198 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 201 pData->data.nType = JS_GLOBALDATA_TYPE_STRING; | 199 pData->data.nType = JS_GLOBALDATA_TYPE_STRING; |
| 202 pData->data.sData = sData; | 200 pData->data.sData = sData; |
| 203 return; | 201 return; |
| 204 } | 202 } |
| 205 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); | 203 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 206 pNewData->data.sKey = sPropName; | 204 pNewData->data.sKey = sPropName; |
| 207 pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING; | 205 pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING; |
| 208 pNewData->data.sData = sData; | 206 pNewData->data.sData = sData; |
| 209 m_arrayGlobalData.push_back(std::move(pNewData)); | 207 m_arrayGlobalData.push_back(std::move(pNewData)); |
| 210 } | 208 } |
| 211 | 209 |
| 212 void CJS_GlobalData::SetGlobalVariableObject( | 210 void CJS_GlobalData::SetGlobalVariableObject( |
| 213 const FX_CHAR* propname, | 211 const CFX_ByteString& propname, |
| 214 const CJS_GlobalVariableArray& array) { | 212 const CJS_GlobalVariableArray& array) { |
| 215 CFX_ByteString sPropName; | 213 CFX_ByteString sPropName(propname); |
| 216 if (!TrimPropName(propname, &sPropName)) | 214 if (!TrimPropName(&sPropName)) |
| 217 return; | 215 return; |
| 218 | 216 |
| 219 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { | 217 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 220 pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; | 218 pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; |
| 221 pData->data.objData.Copy(array); | 219 pData->data.objData.Copy(array); |
| 222 return; | 220 return; |
| 223 } | 221 } |
| 224 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); | 222 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 225 pNewData->data.sKey = sPropName; | 223 pNewData->data.sKey = sPropName; |
| 226 pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; | 224 pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; |
| 227 pNewData->data.objData.Copy(array); | 225 pNewData->data.objData.Copy(array); |
| 228 m_arrayGlobalData.push_back(std::move(pNewData)); | 226 m_arrayGlobalData.push_back(std::move(pNewData)); |
| 229 } | 227 } |
| 230 | 228 |
| 231 void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) { | 229 void CJS_GlobalData::SetGlobalVariableNull(const CFX_ByteString& propname) { |
| 232 CFX_ByteString sPropName; | 230 CFX_ByteString sPropName(propname); |
| 233 if (!TrimPropName(propname, &sPropName)) | 231 if (!TrimPropName(&sPropName)) |
| 234 return; | 232 return; |
| 235 | 233 |
| 236 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { | 234 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { |
| 237 pData->data.nType = JS_GLOBALDATA_TYPE_NULL; | 235 pData->data.nType = JS_GLOBALDATA_TYPE_NULL; |
| 238 return; | 236 return; |
| 239 } | 237 } |
| 240 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); | 238 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); |
| 241 pNewData->data.sKey = sPropName; | 239 pNewData->data.sKey = sPropName; |
| 242 pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL; | 240 pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL; |
| 243 m_arrayGlobalData.push_back(std::move(pNewData)); | 241 m_arrayGlobalData.push_back(std::move(pNewData)); |
| 244 } | 242 } |
| 245 | 243 |
| 246 FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname, | 244 FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent( |
| 247 FX_BOOL bPersistent) { | 245 const CFX_ByteString& propname, |
| 248 CFX_ByteString sPropName; | 246 FX_BOOL bPersistent) { |
| 249 if (!TrimPropName(propname, &sPropName)) | 247 CFX_ByteString sPropName(propname); |
| 248 if (!TrimPropName(&sPropName)) |
| 250 return FALSE; | 249 return FALSE; |
| 251 | 250 |
| 252 CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName); | 251 CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName); |
| 253 if (!pData) | 252 if (!pData) |
| 254 return FALSE; | 253 return FALSE; |
| 255 | 254 |
| 256 pData->bPersistent = bPersistent; | 255 pData->bPersistent = bPersistent; |
| 257 return TRUE; | 256 return TRUE; |
| 258 } | 257 } |
| 259 | 258 |
| 260 FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const FX_CHAR* propname) { | 259 FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const CFX_ByteString& propname) { |
| 261 CFX_ByteString sPropName; | 260 CFX_ByteString sPropName(propname); |
| 262 if (!TrimPropName(propname, &sPropName)) | 261 if (!TrimPropName(&sPropName)) |
| 263 return FALSE; | 262 return FALSE; |
| 264 | 263 |
| 265 auto iter = FindGlobalVariable(sPropName); | 264 auto iter = FindGlobalVariable(sPropName); |
| 266 if (iter == m_arrayGlobalData.end()) | 265 if (iter == m_arrayGlobalData.end()) |
| 267 return FALSE; | 266 return FALSE; |
| 268 | 267 |
| 269 m_arrayGlobalData.erase(iter); | 268 m_arrayGlobalData.erase(iter); |
| 270 return TRUE; | 269 return TRUE; |
| 271 } | 270 } |
| 272 | 271 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 285 int32_t nLength = 0; | 284 int32_t nLength = 0; |
| 286 | 285 |
| 287 LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength); | 286 LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength); |
| 288 CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY)); | 287 CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY)); |
| 289 | 288 |
| 290 if (pBuffer) { | 289 if (pBuffer) { |
| 291 uint8_t* p = pBuffer; | 290 uint8_t* p = pBuffer; |
| 292 uint16_t wType = *((uint16_t*)p); | 291 uint16_t wType = *((uint16_t*)p); |
| 293 p += sizeof(uint16_t); | 292 p += sizeof(uint16_t); |
| 294 | 293 |
| 295 // uint16_t wTemp = (uint16_t)(('X' << 8) | 'F'); | |
| 296 | |
| 297 if (wType == (uint16_t)(('X' << 8) | 'F')) { | 294 if (wType == (uint16_t)(('X' << 8) | 'F')) { |
| 298 uint16_t wVersion = *((uint16_t*)p); | 295 uint16_t wVersion = *((uint16_t*)p); |
| 299 p += sizeof(uint16_t); | 296 p += sizeof(uint16_t); |
| 300 | 297 |
| 301 ASSERT(wVersion <= 2); | 298 ASSERT(wVersion <= 2); |
| 302 | 299 |
| 303 uint32_t dwCount = *((uint32_t*)p); | 300 uint32_t dwCount = *((uint32_t*)p); |
| 304 p += sizeof(uint32_t); | 301 p += sizeof(uint32_t); |
| 305 | 302 |
| 306 uint32_t dwSize = *((uint32_t*)p); | 303 uint32_t dwSize = *((uint32_t*)p); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 case JS_GLOBALDATA_TYPE_NULL: { | 446 case JS_GLOBALDATA_TYPE_NULL: { |
| 450 uint32_t dwNameLen = (uint32_t)name.GetLength(); | 447 uint32_t dwNameLen = (uint32_t)name.GetLength(); |
| 451 sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); | 448 sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); |
| 452 sData.AppendString(name); | 449 sData.AppendString(name); |
| 453 sData.AppendBlock(&wType, sizeof(uint32_t)); | 450 sData.AppendBlock(&wType, sizeof(uint32_t)); |
| 454 } break; | 451 } break; |
| 455 default: | 452 default: |
| 456 break; | 453 break; |
| 457 } | 454 } |
| 458 } | 455 } |
| OLD | NEW |