7#include "fxjs/cfx_globaldata.h"
11#include "core/fdrm/fx_crypt.h"
12#include "core/fxcrt/fx_memcpy_wrappers.h"
13#include "core/fxcrt/numerics/safe_conversions.h"
14#include "core/fxcrt/stl_util.h"
18constexpr size_t kMinGlobalDataBytes = 12;
19constexpr size_t kMaxGlobalDataBytes = 4 * 1024 - 8;
20constexpr uint16_t kMagic = (
'X' << 8) |
'F';
21constexpr uint16_t kMaxVersion = 2;
23const uint8_t kRC4KEY[] = {
24 0x19, 0xa8, 0xe8, 0x01, 0xf6, 0xa8, 0xb6, 0x4d, 0x82, 0x04, 0x45, 0x6d,
25 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51,
26 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16,
27 0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22,
28 0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b,
29 0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a,
30 0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00,
31 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0,
32 0xf8, 0x77, 0xd5, 0xa3};
39 return sPropName->GetLength() != 0;
45 uint32_t dwNameLen =
pdfium::checked_cast<uint32_t>(name.GetLength());
56 MakeNameTypeString(name, pData
.nType, result);
61 MakeNameTypeString(name, pData
.nType, result);
66 MakeNameTypeString(name, pData
.nType, result);
68 pdfium::checked_cast<uint32_t>(pData.sData.GetLength());
74 MakeNameTypeString(name, pData
.nType, result);
91 ++g_pInstance->m_RefCount;
100 g_pInstance =
nullptr;
105 LoadGlobalPersistentVariables();
109 SaveGlobalPersisitentVariables();
114 for (
auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
116 if ((*it)->data.sKey == propname)
119 return m_arrayGlobalData.end();
124 auto iter = FindGlobalVariable(propname);
125 return iter != m_arrayGlobalData.end() ? iter->get() :
nullptr;
130 if (!TrimPropName(&sPropName))
136 pData->data.dData = dData;
140 pNewData->data.sKey =
std::move(sPropName);
142 pNewData->data.dData = dData;
143 m_arrayGlobalData.push_back(std::move(pNewData));
148 if (!TrimPropName(&sPropName))
154 pData->data.bData = bData;
158 pNewData->data.sKey =
std::move(sPropName);
160 pNewData->data.bData = bData;
161 m_arrayGlobalData.push_back(std::move(pNewData));
166 if (!TrimPropName(&sPropName))
172 pData->data.sData = sData;
176 pNewData->data.sKey =
std::move(sPropName);
178 pNewData->data.sData = sData;
179 m_arrayGlobalData.push_back(std::move(pNewData));
184 std::vector<std::unique_ptr<CFX_KeyValue>> array) {
185 if (!TrimPropName(&sPropName))
191 pData->data.objData =
std::move(array);
195 pNewData->data.sKey =
std::move(sPropName);
197 pNewData->data.objData =
std::move(array);
198 m_arrayGlobalData.push_back(std::move(pNewData));
202 if (!TrimPropName(&sPropName))
211 pNewData->data.sKey =
std::move(sPropName);
213 m_arrayGlobalData.push_back(std::move(pNewData));
218 if (!TrimPropName(&sPropName))
230 if (!TrimPropName(&sPropName))
233 auto iter = FindGlobalVariable(sPropName);
234 if (iter == m_arrayGlobalData.end())
237 m_arrayGlobalData.erase(iter);
242 return fxcrt::CollectionSize<int32_t>(m_arrayGlobalData);
248 return m_arrayGlobalData[index].get();
258 std::optional<pdfium::span<uint8_t>> buffer = m_pDelegate->LoadBuffer();
259 if (!buffer.has_value() || buffer.value().empty())
262 ret = LoadGlobalPersistentVariablesFromBuffer(buffer.value());
264 m_pDelegate->BufferDone();
269 pdfium::span<uint8_t> buffer) {
270 if (buffer.size() < kMinGlobalDataBytes)
273 CRYPT_ArcFourCryptBlock(buffer, kRC4KEY);
276 uint8_t* p = buffer.data();
277 uint16_t wType = *((uint16_t*)p);
278 p +=
sizeof(uint16_t);
279 if (wType != kMagic) {
283 uint16_t wVersion = *((uint16_t*)p);
284 p +=
sizeof(uint16_t);
285 if (wVersion > kMaxVersion) {
289 uint32_t dwCount = *((uint32_t*)p);
290 p +=
sizeof(uint32_t);
292 uint32_t dwSize = *((uint32_t*)p);
293 p +=
sizeof(uint32_t);
295 if (dwSize != buffer.size() -
sizeof(uint16_t) * 2 -
sizeof(uint32_t) * 2) {
299 for (int32_t i = 0, sz = dwCount; i < sz; i++) {
300 if (p +
sizeof(uint32_t) >= buffer.end()) {
304 uint32_t dwNameLen = 0;
306 p +=
sizeof(uint32_t);
307 if (p + dwNameLen > buffer.end()) {
312 p +=
sizeof(
char) * dwNameLen;
314 uint16_t wDataType = 0;
316 p +=
sizeof(uint16_t);
328 p +=
sizeof(uint32_t);
343 p +=
sizeof(uint16_t);
348 uint32_t dwLength = 0;
350 p +=
sizeof(uint32_t);
351 if (p + dwLength > buffer.end()) {
356 p +=
sizeof(
char) * dwLength;
377 for (
const auto& pElement : m_arrayGlobalData) {
378 if (!pElement->bPersistent)
381 BinaryBuffer sElement;
382 if (!MakeByteString(pElement->data.sKey, pElement->data, &sElement))
385 if (sData.GetSize() + sElement.GetSize() > kMaxGlobalDataBytes)
388 sData.AppendSpan(sElement.GetSpan());
397 uint32_t dwSize =
pdfium::checked_cast<uint32_t>(sData.GetSize());
399 sFile.AppendSpan(sData.GetSpan());
401 CRYPT_ArcFourCryptBlock(sFile.GetMutableSpan(), kRC4KEY);
402 return m_pDelegate->StoreBuffer(sFile.GetSpan());
fxcrt::ByteString ByteString
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 AppendDouble(double value)
void AppendString(const ByteString &str)
void AppendUint32(uint32_t value)
UNSAFE_BUFFER_USAGE void * FXSYS_memcpy(void *ptr1, const void *ptr2, size_t len)