7#include "fxjs/cfx_globaldata.h"
11#include "core/fdrm/fx_crypt.h"
12#include "core/fxcrt/stl_util.h"
13#include "third_party/base/numerics/safe_conversions.h"
17constexpr size_t kMinGlobalDataBytes = 12;
18constexpr size_t kMaxGlobalDataBytes = 4 * 1024 - 8;
19constexpr uint16_t kMagic = (
'X' << 8) |
'F';
20constexpr uint16_t kMaxVersion = 2;
22const uint8_t kRC4KEY[] = {
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};
36bool TrimPropName(ByteString* sPropName) {
38 return sPropName->GetLength() != 0;
41void MakeNameTypeString(
const ByteString& name,
44 uint32_t dwNameLen = pdfium::base::checked_cast<uint32_t>(name.GetLength());
50bool MakeByteString(
const ByteString& name,
55 MakeNameTypeString(name, pData
.nType, result);
60 MakeNameTypeString(name, pData
.nType, result);
65 MakeNameTypeString(name, pData
.nType, result);
67 pdfium::base::checked_cast<uint32_t>(pData.sData.GetLength());
73 MakeNameTypeString(name, pData
.nType, result);
90 ++g_pInstance->m_RefCount;
99 g_pInstance =
nullptr;
104 LoadGlobalPersistentVariables();
108 SaveGlobalPersisitentVariables();
112 const ByteString& propname) {
113 for (
auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
115 if ((*it)->data.sKey == propname)
118 return m_arrayGlobalData.end();
122 const ByteString& propname) {
123 auto iter = FindGlobalVariable(propname);
124 return iter != m_arrayGlobalData.end() ? iter->get() :
nullptr;
129 if (!TrimPropName(&sPropName))
135 pData->data.dData = dData;
139 pNewData->data.sKey =
std::move(sPropName);
141 pNewData->data.dData = dData;
142 m_arrayGlobalData.push_back(std::move(pNewData));
147 if (!TrimPropName(&sPropName))
153 pData->data.bData = bData;
157 pNewData->data.sKey =
std::move(sPropName);
159 pNewData->data.bData = bData;
160 m_arrayGlobalData.push_back(std::move(pNewData));
164 const ByteString& sData) {
165 if (!TrimPropName(&sPropName))
171 pData->data.sData = sData;
175 pNewData->data.sKey =
std::move(sPropName);
177 pNewData->data.sData = sData;
178 m_arrayGlobalData.push_back(std::move(pNewData));
182 ByteString sPropName,
183 std::vector<std::unique_ptr<CFX_KeyValue>> array) {
184 if (!TrimPropName(&sPropName))
190 pData->data.objData =
std::move(array);
194 pNewData->data.sKey =
std::move(sPropName);
196 pNewData->data.objData =
std::move(array);
197 m_arrayGlobalData.push_back(std::move(pNewData));
201 if (!TrimPropName(&sPropName))
210 pNewData->data.sKey =
std::move(sPropName);
212 m_arrayGlobalData.push_back(std::move(pNewData));
217 if (!TrimPropName(&sPropName))
229 if (!TrimPropName(&sPropName))
232 auto iter = FindGlobalVariable(sPropName);
233 if (iter == m_arrayGlobalData.end())
236 m_arrayGlobalData.erase(iter);
241 return fxcrt::CollectionSize<int32_t>(m_arrayGlobalData);
247 return m_arrayGlobalData[index].get();
257 absl::optional<pdfium::span<uint8_t>> buffer = m_pDelegate->LoadBuffer();
258 if (!buffer.has_value() || buffer.value().empty())
261 ret = LoadGlobalPersistentVariablesFromBuffer(buffer.value());
263 m_pDelegate->BufferDone();
268 pdfium::span<uint8_t> buffer) {
269 if (buffer.size() < kMinGlobalDataBytes)
272 CRYPT_ArcFourCryptBlock(buffer, kRC4KEY);
274 uint8_t* p = buffer.data();
275 uint16_t wType = *((uint16_t*)p);
276 p +=
sizeof(uint16_t);
280 uint16_t wVersion = *((uint16_t*)p);
281 p +=
sizeof(uint16_t);
282 if (wVersion > kMaxVersion)
285 uint32_t dwCount = *((uint32_t*)p);
286 p +=
sizeof(uint32_t);
288 uint32_t dwSize = *((uint32_t*)p);
289 p +=
sizeof(uint32_t);
291 if (dwSize != buffer.size() -
sizeof(uint16_t) * 2 -
sizeof(uint32_t) * 2)
294 for (int32_t i = 0, sz = dwCount; i < sz; i++) {
295 if (p +
sizeof(uint32_t) >= buffer.end()) {
299 uint32_t dwNameLen = 0;
300 memcpy(&dwNameLen, p,
sizeof(uint32_t));
301 p +=
sizeof(uint32_t);
302 if (p + dwNameLen > buffer.end())
305 ByteString sEntry = ByteString(p, dwNameLen);
306 p +=
sizeof(
char) * dwNameLen;
308 uint16_t wDataType = 0;
309 memcpy(&wDataType, p,
sizeof(uint16_t));
310 p +=
sizeof(uint16_t);
320 memcpy(&dwData, p,
sizeof(uint32_t));
321 p +=
sizeof(uint32_t);
326 memcpy(&dData, p,
sizeof(
double));
335 memcpy(&wData, p,
sizeof(uint16_t));
336 p +=
sizeof(uint16_t);
341 uint32_t dwLength = 0;
342 memcpy(&dwLength, p,
sizeof(uint32_t));
343 p +=
sizeof(uint32_t);
344 if (p + dwLength > buffer.end())
349 p +=
sizeof(
char) * dwLength;
369 for (
const auto& pElement : m_arrayGlobalData) {
370 if (!pElement->bPersistent)
373 BinaryBuffer sElement;
374 if (!MakeByteString(pElement->data.sKey, pElement->data, &sElement))
377 if (sData.GetSize() + sElement.GetSize() > kMaxGlobalDataBytes)
380 sData.AppendSpan(sElement.GetSpan());
389 uint32_t dwSize = pdfium::base::checked_cast<uint32_t>(sData.GetSize());
394 return m_pDelegate->StoreBuffer(sFile.GetSpan());
bool SetGlobalVariablePersistent(ByteString propname, bool bPersistent)
Element * GetAt(int index)
void SetGlobalVariableObject(ByteString propname, std::vector< std::unique_ptr< CFX_KeyValue > > array)
void SetGlobalVariableNull(ByteString propname)
void SetGlobalVariableNumber(ByteString propname, double dData)
bool DeleteGlobalVariable(ByteString propname)
static CFX_GlobalData * GetRetainedInstance(Delegate *pDelegate)
void SetGlobalVariableBoolean(ByteString propname, bool bData)
Element * GetGlobalVariable(const ByteString &sPropname)
void SetGlobalVariableString(ByteString propname, const ByteString &sData)
void AppendUint16(uint16_t value)
void AppendSpan(pdfium::span< const uint8_t > span)
pdfium::span< const uint8_t > GetSpan() const
void AppendDouble(double value)
void AppendString(const ByteString &str)
void AppendUint32(uint32_t value)
pdfium::span< uint8_t > GetMutableSpan()