1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "xfa/fxfa/parser/xfa_utils.h"
#include "core/fxcrt/fx_ext.h"
#include "xfa/fde/xml/fde_xml_imp.h"
#include "xfa/fxfa/parser/cxfa_document.h"
#include "xfa/fxfa/parser/cxfa_localemgr.h"
#include "xfa/fxfa/parser/cxfa_localevalue.h"
#include "xfa/fxfa/parser/cxfa_measurement.h"
#include "xfa/fxfa/parser/cxfa_node.h"
#include "xfa/fxfa/parser/xfa_basic_data.h"
namespace {
const double fraction_scales[] = {0.1,
0.01,
0.001,
0.0001,
0.00001,
0.000001,
0.0000001,
0.00000001,
0.000000001,
0.0000000001,
0.00000000001,
0.000000000001,
0.0000000000001,
0.00000000000001,
0.000000000000001,
0.0000000000000001};
double WideStringToDouble(const CFX_WideString& wsStringVal) {
CFX_WideString wsValue = wsStringVal;
wsValue.TrimLeft();
wsValue.TrimRight();
int64_t nIntegral = 0;
uint32_t dwFractional = 0;
int32_t nExponent = 0;
int32_t cc = 0;
bool bNegative = false;
bool bExpSign = false;
const wchar_t* str = wsValue.c_str();
int32_t len = wsValue.GetLength();
if (str[0] == '+') {
cc++;
} else if (str[0] == '-') {
bNegative = true;
cc++;
}
int32_t nIntegralLen = 0;
while (cc < len) {
if (str[cc] == '.' || str[cc] == 'E' || str[cc] == 'e' ||
nIntegralLen > 17) {
break;
}
if (!FXSYS_isDecimalDigit(str[cc])) {
return 0;
}
nIntegral = nIntegral * 10 + str[cc] - '0';
cc++;
nIntegralLen++;
}
nIntegral = bNegative ? -nIntegral : nIntegral;
int32_t scale = 0;
double fraction = 0.0;
if (cc < len && str[cc] == '.') {
cc++;
while (cc < len) {
fraction += XFA_GetFractionalScale(scale) * (str[cc] - '0');
scale++;
cc++;
if (cc == len)
break;
if (scale == XFA_GetMaxFractionalScale() || str[cc] == 'E' ||
str[cc] == 'e') {
break;
}
if (!FXSYS_isDecimalDigit(str[cc]))
return 0;
}
dwFractional = static_cast<uint32_t>(fraction * 4294967296.0);
}
if (cc < len && (str[cc] == 'E' || str[cc] == 'e')) {
cc++;
if (cc < len) {
if (str[cc] == '+') {
cc++;
} else if (str[cc] == '-') {
bExpSign = true;
cc++;
}
}
while (cc < len) {
if (str[cc] == '.' || !FXSYS_isDecimalDigit(str[cc]))
return 0;
nExponent = nExponent * 10 + str[cc] - '0';
cc++;
}
nExponent = bExpSign ? -nExponent : nExponent;
}
double dValue = dwFractional / 4294967296.0;
dValue = nIntegral + (nIntegral >= 0 ? dValue : -dValue);
if (nExponent != 0)
dValue *= FXSYS_pow(10, static_cast<float>(nExponent));
return dValue;
}
} // namespace
double XFA_GetFractionalScale(uint32_t idx) {
return fraction_scales[idx];
}
int XFA_GetMaxFractionalScale() {
return FX_ArraySize(fraction_scales);
}
CXFA_LocaleValue XFA_GetLocaleValue(CXFA_WidgetData* pWidgetData) {
CXFA_Node* pNodeValue =
pWidgetData->GetNode()->GetChild(0, XFA_Element::Value);
if (!pNodeValue) {
return CXFA_LocaleValue();
}
CXFA_Node* pValueChild = pNodeValue->GetNodeItem(XFA_NODEITEM_FirstChild);
if (!pValueChild) {
return CXFA_LocaleValue();
}
int32_t iVTType = XFA_VT_NULL;
switch (pValueChild->GetElementType()) {
case XFA_Element::Decimal:
iVTType = XFA_VT_DECIMAL;
break;
case XFA_Element::Float:
iVTType = XFA_VT_FLOAT;
break;
case XFA_Element::Date:
iVTType = XFA_VT_DATE;
break;
case XFA_Element::Time:
iVTType = XFA_VT_TIME;
break;
case XFA_Element::DateTime:
iVTType = XFA_VT_DATETIME;
break;
case XFA_Element::Boolean:
iVTType = XFA_VT_BOOLEAN;
break;
case XFA_Element::Integer:
iVTType = XFA_VT_INTEGER;
break;
case XFA_Element::Text:
iVTType = XFA_VT_TEXT;
break;
default:
iVTType = XFA_VT_NULL;
break;
}
return CXFA_LocaleValue(iVTType, pWidgetData->GetRawValue(),
pWidgetData->GetNode()->GetDocument()->GetLocalMgr());
}
void XFA_GetPlainTextFromRichText(CFDE_XMLNode* pXMLNode,
CFX_WideString& wsPlainText) {
if (!pXMLNode) {
return;
}
switch (pXMLNode->GetType()) {
case FDE_XMLNODE_Element: {
CFDE_XMLElement* pXMLElement = static_cast<CFDE_XMLElement*>(pXMLNode);
CFX_WideString wsTag;
pXMLElement->GetLocalTagName(wsTag);
uint32_t uTag = FX_HashCode_GetW(wsTag.AsStringC(), true);
if (uTag == 0x0001f714) {
wsPlainText += L"\n";
} else if (uTag == 0x00000070) {
if (!wsPlainText.IsEmpty()) {
wsPlainText += L"\n";
}
} else if (uTag == 0xa48ac63) {
if (!wsPlainText.IsEmpty() &&
wsPlainText[wsPlainText.GetLength() - 1] != '\n') {
wsPlainText += L"\n";
}
}
} break;
case FDE_XMLNODE_Text: {
CFX_WideString wsContent;
static_cast<CFDE_XMLText*>(pXMLNode)->GetText(wsContent);
wsPlainText += wsContent;
} break;
case FDE_XMLNODE_CharData: {
CFX_WideString wsCharData;
static_cast<CFDE_XMLCharData*>(pXMLNode)->GetCharData(wsCharData);
wsPlainText += wsCharData;
} break;
default:
break;
}
for (CFDE_XMLNode* pChildXML =
pXMLNode->GetNodeItem(CFDE_XMLNode::FirstChild);
pChildXML;
pChildXML = pChildXML->GetNodeItem(CFDE_XMLNode::NextSibling)) {
XFA_GetPlainTextFromRichText(pChildXML, wsPlainText);
}
}
bool XFA_FieldIsMultiListBox(CXFA_Node* pFieldNode) {
bool bRet = false;
if (!pFieldNode)
return bRet;
CXFA_Node* pUIChild = pFieldNode->GetChild(0, XFA_Element::Ui);
if (pUIChild) {
CXFA_Node* pFirstChild = pUIChild->GetNodeItem(XFA_NODEITEM_FirstChild);
if (pFirstChild &&
pFirstChild->GetElementType() == XFA_Element::ChoiceList) {
bRet = pFirstChild->GetEnum(XFA_ATTRIBUTE_Open) ==
XFA_ATTRIBUTEENUM_MultiSelect;
}
}
return bRet;
}
double XFA_ByteStringToDouble(const CFX_ByteStringC& szStringVal) {
CFX_WideString wsValue = CFX_WideString::FromUTF8(szStringVal);
return WideStringToDouble(wsValue);
}
int32_t XFA_MapRotation(int32_t nRotation) {
nRotation = nRotation % 360;
nRotation = nRotation < 0 ? nRotation + 360 : nRotation;
return nRotation;
}
const XFA_SCRIPTATTRIBUTEINFO* XFA_GetScriptAttributeByName(
XFA_Element eElement,
const CFX_WideStringC& wsAttributeName) {
if (wsAttributeName.IsEmpty())
return nullptr;
int32_t iElementIndex = static_cast<int32_t>(eElement);
while (iElementIndex != -1) {
const XFA_SCRIPTHIERARCHY* scriptIndex = g_XFAScriptIndex + iElementIndex;
int32_t icount = scriptIndex->wAttributeCount;
if (icount == 0) {
iElementIndex = scriptIndex->wParentIndex;
continue;
}
uint32_t uHash = FX_HashCode_GetW(wsAttributeName, false);
int32_t iStart = scriptIndex->wAttributeStart, iEnd = iStart + icount - 1;
do {
int32_t iMid = (iStart + iEnd) / 2;
const XFA_SCRIPTATTRIBUTEINFO* pInfo = g_SomAttributeData + iMid;
if (uHash == pInfo->uHash)
return pInfo;
if (uHash < pInfo->uHash)
iEnd = iMid - 1;
else
iStart = iMid + 1;
} while (iStart <= iEnd);
iElementIndex = scriptIndex->wParentIndex;
}
return nullptr;
}
const XFA_NOTSUREATTRIBUTE* XFA_GetNotsureAttribute(XFA_Element eElement,
XFA_ATTRIBUTE eAttribute,
XFA_ATTRIBUTETYPE eType) {
int32_t iStart = 0, iEnd = g_iXFANotsureCount - 1;
do {
int32_t iMid = (iStart + iEnd) / 2;
const XFA_NOTSUREATTRIBUTE* pAttr = g_XFANotsureAttributes + iMid;
if (eElement == pAttr->eElement) {
if (pAttr->eAttribute == eAttribute) {
if (eType == XFA_ATTRIBUTETYPE_NOTSURE || eType == pAttr->eType)
return pAttr;
return nullptr;
}
int32_t iBefore = iMid - 1;
if (iBefore >= 0) {
pAttr = g_XFANotsureAttributes + iBefore;
while (eElement == pAttr->eElement) {
if (pAttr->eAttribute == eAttribute) {
if (eType == XFA_ATTRIBUTETYPE_NOTSURE || eType == pAttr->eType)
return pAttr;
return nullptr;
}
iBefore--;
if (iBefore < 0)
break;
pAttr = g_XFANotsureAttributes + iBefore;
}
}
int32_t iAfter = iMid + 1;
if (iAfter <= g_iXFANotsureCount - 1) {
pAttr = g_XFANotsureAttributes + iAfter;
while (eElement == pAttr->eElement) {
if (pAttr->eAttribute == eAttribute) {
if (eType == XFA_ATTRIBUTETYPE_NOTSURE || eType == pAttr->eType)
return pAttr;
return nullptr;
}
iAfter++;
if (iAfter > g_iXFANotsureCount - 1)
break;
pAttr = g_XFANotsureAttributes + iAfter;
}
}
return nullptr;
}
if (eElement < pAttr->eElement)
iEnd = iMid - 1;
else
iStart = iMid + 1;
} while (iStart <= iEnd);
return nullptr;
}
const XFA_PROPERTY* XFA_GetPropertyOfElement(XFA_Element eElement,
XFA_Element eProperty,
uint32_t dwPacket) {
int32_t iCount = 0;
const XFA_PROPERTY* pProperties = XFA_GetElementProperties(eElement, iCount);
if (!pProperties || iCount < 1)
return nullptr;
auto* it = std::find_if(pProperties, pProperties + iCount,
[eProperty](const XFA_PROPERTY& prop) {
return prop.eName == eProperty;
});
if (it == pProperties + iCount)
return nullptr;
const XFA_ELEMENTINFO* pInfo = XFA_GetElementByID(eProperty);
ASSERT(pInfo);
if (dwPacket != XFA_XDPPACKET_UNKNOWN && !(dwPacket & pInfo->dwPackets))
return nullptr;
return it;
}
const XFA_PROPERTY* XFA_GetElementProperties(XFA_Element eElement,
int32_t& iCount) {
if (eElement == XFA_Element::Unknown)
return nullptr;
const XFA_ELEMENTHIERARCHY* pElement =
g_XFAElementPropertyIndex + static_cast<int32_t>(eElement);
iCount = pElement->wCount;
return g_XFAElementPropertyData + pElement->wStart;
}
const uint8_t* XFA_GetElementAttributes(XFA_Element eElement, int32_t& iCount) {
if (eElement == XFA_Element::Unknown)
return nullptr;
const XFA_ELEMENTHIERARCHY* pElement =
g_XFAElementAttributeIndex + static_cast<int32_t>(eElement);
iCount = pElement->wCount;
return g_XFAElementAttributeData + pElement->wStart;
}
const XFA_ELEMENTINFO* XFA_GetElementByID(XFA_Element eName) {
return eName != XFA_Element::Unknown
? g_XFAElementData + static_cast<int32_t>(eName)
: nullptr;
}
XFA_Element XFA_GetElementTypeForName(const CFX_WideStringC& wsName) {
if (wsName.IsEmpty())
return XFA_Element::Unknown;
uint32_t uHash = FX_HashCode_GetW(wsName, false);
const XFA_ELEMENTINFO* pEnd = g_XFAElementData + g_iXFAElementCount;
auto* pInfo =
std::lower_bound(g_XFAElementData, pEnd, uHash,
[](const XFA_ELEMENTINFO& info, uint32_t hash) {
return info.uHash < hash;
});
if (pInfo < pEnd && pInfo->uHash == uHash)
return pInfo->eName;
return XFA_Element::Unknown;
}
CXFA_Measurement XFA_GetAttributeDefaultValue_Measure(XFA_Element eElement,
XFA_ATTRIBUTE eAttribute,
uint32_t dwPacket) {
void* pValue;
if (XFA_GetAttributeDefaultValue(pValue, eElement, eAttribute,
XFA_ATTRIBUTETYPE_Measure, dwPacket)) {
return *(CXFA_Measurement*)pValue;
}
return CXFA_Measurement();
}
bool XFA_GetAttributeDefaultValue(void*& pValue,
XFA_Element eElement,
XFA_ATTRIBUTE eAttribute,
XFA_ATTRIBUTETYPE eType,
uint32_t dwPacket) {
const XFA_ATTRIBUTEINFO* pInfo = XFA_GetAttributeByID(eAttribute);
if (!pInfo)
return false;
if (dwPacket && (dwPacket & pInfo->dwPackets) == 0)
return false;
if (pInfo->eType == eType) {
pValue = pInfo->pDefValue;
return true;
}
if (pInfo->eType == XFA_ATTRIBUTETYPE_NOTSURE) {
const XFA_NOTSUREATTRIBUTE* pAttr =
XFA_GetNotsureAttribute(eElement, eAttribute, eType);
if (pAttr) {
pValue = pAttr->pValue;
return true;
}
}
return false;
}
const XFA_ATTRIBUTEINFO* XFA_GetAttributeByName(const CFX_WideStringC& wsName) {
if (wsName.IsEmpty())
return nullptr;
uint32_t uHash = FX_HashCode_GetW(wsName, false);
int32_t iStart = 0;
int32_t iEnd = g_iXFAAttributeCount - 1;
do {
int32_t iMid = (iStart + iEnd) / 2;
const XFA_ATTRIBUTEINFO* pInfo = g_XFAAttributeData + iMid;
if (uHash == pInfo->uHash)
return pInfo;
if (uHash < pInfo->uHash)
iEnd = iMid - 1;
else
iStart = iMid + 1;
} while (iStart <= iEnd);
return nullptr;
}
const XFA_ATTRIBUTEINFO* XFA_GetAttributeByID(XFA_ATTRIBUTE eName) {
return (eName < g_iXFAAttributeCount) ? (g_XFAAttributeData + eName)
: nullptr;
}
const XFA_ATTRIBUTEENUMINFO* XFA_GetAttributeEnumByName(
const CFX_WideStringC& wsName) {
if (wsName.IsEmpty())
return nullptr;
uint32_t uHash = FX_HashCode_GetW(wsName, false);
int32_t iStart = 0;
int32_t iEnd = g_iXFAEnumCount - 1;
do {
int32_t iMid = (iStart + iEnd) / 2;
const XFA_ATTRIBUTEENUMINFO* pInfo = g_XFAEnumData + iMid;
if (uHash == pInfo->uHash)
return pInfo;
if (uHash < pInfo->uHash)
iEnd = iMid - 1;
else
iStart = iMid + 1;
} while (iStart <= iEnd);
return nullptr;
}
const XFA_PACKETINFO* XFA_GetPacketByIndex(XFA_PACKET ePacket) {
return g_XFAPacketData + ePacket;
}
const XFA_PACKETINFO* XFA_GetPacketByID(uint32_t dwPacket) {
int32_t iStart = 0, iEnd = g_iXFAPacketCount - 1;
do {
int32_t iMid = (iStart + iEnd) / 2;
uint32_t dwFind = (g_XFAPacketData + iMid)->eName;
if (dwPacket == dwFind)
return g_XFAPacketData + iMid;
if (dwPacket < dwFind)
iEnd = iMid - 1;
else
iStart = iMid + 1;
} while (iStart <= iEnd);
return nullptr;
}
|