Coverage Report

Created: 2025-11-04 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.05/xpdf/XFAScanner.cc
Line
Count
Source
1
//========================================================================
2
//
3
// XFAScanner.cc
4
//
5
// Copyright 2020 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include "GString.h"
12
#include "GHash.h"
13
#include "Object.h"
14
#include "Error.h"
15
#include "Zoox.h"
16
#include "XFAScanner.h"
17
18
//------------------------------------------------------------------------
19
20
// fields have two names:
21
//
22
// name:
23
//   - nodes with bind=global set the index to 0 ("foo[0]") regardless
24
//     of the number of nodes with the same name
25
//   - nodes with bind=none are dropped from the name
26
//   - <area> nodes are dropped from the name
27
//   - used for field value lookup in <xfa:datasets>
28
//
29
// fullName:
30
//   - all named nodes are treated the same, regardless of bind=global
31
//     or bind=none
32
//   - <area> nodes are included in the name, but don't reset the
33
//     numbering (i.e., <area> nodes are "transparent" with respect to
34
//     node numbering)
35
//   - used for field value lookup in <form>
36
//   - used for matching with AcroForm names
37
//
38
// Both names use indexes on all nodes, even if there's only one node
39
// with the name -- this isn't correct for XFA naming, but it matches
40
// the AcroForm behavior.
41
42
//------------------------------------------------------------------------
43
44
XFAFieldLayoutInfo::XFAFieldLayoutInfo(XFAFieldLayoutHAlign hAlignA,
45
15
               XFAFieldLayoutVAlign vAlignA) {
46
15
  hAlign = hAlignA;
47
15
  vAlign = vAlignA;
48
15
}
49
50
//------------------------------------------------------------------------
51
52
XFAFieldPictureInfo::XFAFieldPictureInfo(XFAFieldPictureSubtype subtypeA,
53
0
           GString *formatA) {
54
0
  subtype = subtypeA;
55
0
  format = formatA;
56
0
}
57
58
0
XFAFieldPictureInfo::~XFAFieldPictureInfo() {
59
0
  delete format;
60
0
}
61
62
//------------------------------------------------------------------------
63
64
XFAFieldBarcodeInfo::XFAFieldBarcodeInfo(GString *barcodeTypeA,
65
           double wideNarrowRatioA,
66
           double moduleWidthA,
67
           double moduleHeightA,
68
           int dataLengthA,
69
           int errorCorrectionLevelA,
70
0
           GString *textLocationA) {
71
0
  barcodeType = barcodeTypeA;
72
0
  wideNarrowRatio = wideNarrowRatioA;
73
0
  moduleWidth = moduleWidthA;
74
0
  moduleHeight = moduleHeightA;
75
0
  dataLength = dataLengthA;
76
0
  errorCorrectionLevel = errorCorrectionLevelA;
77
0
  textLocation = textLocationA;
78
0
}
79
80
0
XFAFieldBarcodeInfo::~XFAFieldBarcodeInfo() {
81
0
  delete barcodeType;
82
0
  delete textLocation;
83
0
}
84
85
//------------------------------------------------------------------------
86
87
XFAField::XFAField(GString *nameA, GString *fullNameA, GString *valueA,
88
       XFAFieldLayoutInfo *layoutInfoA,
89
       XFAFieldPictureInfo *pictureInfoA,
90
       XFAFieldBarcodeInfo *barcodeInfoA)
91
573
  : name(nameA)
92
573
  , fullName(fullNameA)
93
573
  , value(valueA)
94
573
  , layoutInfo(layoutInfoA)
95
573
  , pictureInfo(pictureInfoA)
96
573
  , barcodeInfo(barcodeInfoA)
97
573
{
98
573
}
99
100
573
XFAField::~XFAField() {
101
573
  delete name;
102
573
  delete fullName;
103
573
  delete value;
104
573
  delete layoutInfo;
105
573
  delete pictureInfo;
106
573
  delete barcodeInfo;
107
573
}
108
109
//------------------------------------------------------------------------
110
111
59
XFAScanner *XFAScanner::load(Object *xfaObj) {
112
59
  GString *xfaData = readXFAStreams(xfaObj);
113
59
  if (!xfaData) {
114
2
    return NULL;
115
2
  }
116
57
  ZxDoc *xml = ZxDoc::loadMem(xfaData->getCString(), xfaData->getLength());
117
57
  delete xfaData;
118
57
  if (!xml) {
119
1
    error(errSyntaxError, -1, "Invalid XML in XFA form");
120
1
    return NULL;
121
1
  }
122
123
56
  XFAScanner *scanner = new XFAScanner();
124
125
56
  if (xml->getRoot()) {
126
56
    GHash *formValues = scanner->scanFormValues(xml->getRoot());
127
56
    ZxElement *dataElem = NULL;
128
56
    ZxElement *datasets =
129
56
        xml->getRoot()->findFirstChildElement("xfa:datasets");
130
56
    if (datasets) {
131
2
      dataElem = datasets->findFirstChildElement("xfa:data");
132
2
    }
133
56
    ZxElement *tmpl = xml->getRoot()->findFirstChildElement("template");
134
56
    if (tmpl) {
135
26
      scanner->scanNode(tmpl, NULL, NULL, NULL, NULL, NULL,
136
26
      dataElem, formValues);
137
26
    }
138
56
    deleteGHash(formValues, GString);
139
56
  }
140
141
56
  delete xml;
142
143
56
  return scanner;
144
57
}
145
146
56
XFAScanner::XFAScanner() {
147
56
  fields = new GHash();
148
56
}
149
150
56
XFAScanner::~XFAScanner() {
151
56
  deleteGHash(fields, XFAField);
152
56
}
153
154
80
XFAField *XFAScanner::findField(GString *acroFormFieldName) {
155
80
  return (XFAField *)fields->lookup(acroFormFieldName);
156
80
}
157
158
59
GString *XFAScanner::readXFAStreams(Object *xfaObj) {
159
59
  GString *data = new GString();
160
59
  char buf[4096];
161
59
  int n;
162
59
  if (xfaObj->isStream()) {
163
0
    xfaObj->streamReset();
164
0
    while ((n = xfaObj->getStream()->getBlock(buf, sizeof(buf))) > 0) {
165
0
      data->append(buf, n);
166
0
    }
167
59
  } else if (xfaObj->isArray()) {
168
499
    for (int i = 1; i < xfaObj->arrayGetLength(); i += 2) {
169
442
      Object obj;
170
442
      if (!xfaObj->arrayGet(i, &obj)->isStream()) {
171
2
  error(errSyntaxError, -1, "XFA array element is wrong type");
172
2
  obj.free();
173
2
  delete data;
174
2
  return NULL;
175
2
      }
176
440
      obj.streamReset();
177
4.06k
      while ((n = obj.getStream()->getBlock(buf, sizeof(buf))) > 0) {
178
3.62k
  data->append(buf, n);
179
3.62k
      }
180
440
      obj.free();
181
440
    }
182
59
  } else {
183
0
    error(errSyntaxError, -1, "XFA object is wrong type");
184
0
    return NULL;
185
0
  }
186
57
  return data;
187
59
}
188
189
56
GHash *XFAScanner::scanFormValues(ZxElement *xmlRoot) {
190
56
  GHash *formValues = new GHash(gTrue);
191
56
  ZxElement *formElem = xmlRoot->findFirstChildElement("form");
192
56
  if (formElem) {
193
1
    scanFormNode(formElem, NULL, formValues);
194
1
  }
195
56
  return formValues;
196
56
}
197
198
void XFAScanner::scanFormNode(ZxElement *elem, GString *fullName,
199
1
            GHash *formValues) {
200
1
  GHash *fullNameIdx = new GHash();
201
1
  for (ZxNode *node = elem->getFirstChild();
202
1
       node;
203
1
       node = node->getNextChild()) {
204
0
    if (node->isElement("value")) {
205
0
      if (fullName) {
206
0
  ZxNode *child1Node = ((ZxElement *)node)->getFirstChild();
207
0
  if (child1Node && child1Node->isElement()) {
208
0
    ZxNode *child2Node = ((ZxElement *)child1Node)->getFirstChild();
209
0
    if (child2Node && child2Node->isCharData()) {
210
0
      formValues->add(fullName->copy(),
211
0
          ((ZxCharData *)child2Node)->getData()->copy());
212
0
    }
213
0
  }
214
0
      }
215
0
    } else if (node->isElement()) {
216
0
      ZxAttr *nameAttr = ((ZxElement *)node)->findAttr("name");
217
0
      if (nameAttr && (node->isElement("subform") ||
218
0
           node->isElement("field"))) {
219
0
  GString *nodeName = nameAttr->getValue();
220
0
  GString *childFullName;
221
0
  if (fullName) {
222
0
    childFullName = GString::format("{0:t}.{1:t}", fullName, nodeName);
223
0
  } else {
224
0
    childFullName = nodeName->copy();
225
0
  }
226
0
  int idx = fullNameIdx->lookupInt(nodeName);
227
0
  childFullName->appendf("[{0:d}]", idx);
228
0
  fullNameIdx->replace(nodeName, idx + 1);
229
0
  scanFormNode((ZxElement *)node, childFullName, formValues);
230
0
  delete childFullName;
231
0
      } else if (node->isElement("subform")) {
232
0
  scanFormNode((ZxElement *)node, fullName, formValues);
233
0
      }
234
0
    }
235
0
  }
236
1
  delete fullNameIdx;
237
1
}
238
239
void XFAScanner::scanNode(ZxElement *elem,
240
        GString *parentName, GString *parentFullName,
241
        GHash *nameIdx, GHash *fullNameIdx,
242
        GString *exclGroupName, ZxElement *dataElem,
243
5.51k
        GHash *formValues) {
244
5.51k
  GString *nodeName = getNodeName(elem);
245
5.51k
  GHash *childNameIdx;
246
5.51k
  if (!nameIdx || nodeName) {
247
208
    childNameIdx = new GHash();
248
5.30k
  } else {
249
5.30k
    childNameIdx = nameIdx;
250
5.30k
  }
251
5.51k
  GString *nodeFullName = getNodeFullName(elem);
252
5.51k
  GHash *childFullNameIdx;
253
5.51k
  if (!fullNameIdx || (nodeFullName && !elem->isElement("area"))) {
254
208
    childFullNameIdx = new GHash();
255
5.30k
  } else {
256
5.30k
    childFullNameIdx = fullNameIdx;
257
5.30k
  }
258
259
5.51k
  GString *childName;
260
5.51k
  if (nodeName) {
261
182
    if (parentName) {
262
157
      childName = GString::format("{0:t}.{1:t}", parentName, nodeName);
263
157
    } else {
264
25
      childName = nodeName->copy();
265
25
    }
266
182
    int idx = nameIdx->lookupInt(nodeName);
267
182
    nameIdx->replace(nodeName, idx + 1);
268
182
    if (nodeIsBindGlobal(elem)) {
269
0
      childName->appendf("[0]");
270
182
    } else {
271
182
      childName->appendf("[{0:d}]", idx);
272
182
    }
273
5.33k
  } else {
274
5.33k
    childName = parentName;
275
5.33k
  }
276
5.51k
  GString *childFullName;
277
5.51k
  if (nodeFullName) {
278
182
    if (parentFullName) {
279
157
      childFullName = GString::format("{0:t}.{1:t}",
280
157
              parentFullName, nodeFullName);
281
157
    } else {
282
25
      childFullName = nodeFullName->copy();
283
25
    }
284
182
    int idx = fullNameIdx->lookupInt(nodeFullName);
285
182
    fullNameIdx->replace(nodeFullName, idx + 1);
286
182
    childFullName->appendf("[{0:d}]", idx);
287
5.33k
  } else {
288
5.33k
    childFullName = parentFullName;
289
5.33k
  }
290
291
5.51k
  if (elem->isElement("field")) {
292
573
    if (childName && childFullName) {
293
573
      scanField(elem, childName, childFullName, exclGroupName,
294
573
    dataElem, formValues);
295
573
    }
296
4.94k
  } else {
297
4.94k
    GString *childExclGroupName;
298
4.94k
    if (elem->isElement("exclGroup")) {
299
10
      childExclGroupName = childName;
300
4.93k
    } else {
301
4.93k
      childExclGroupName = NULL;
302
4.93k
    }
303
4.94k
    for (ZxNode *child = elem->getFirstChild();
304
14.2k
   child;
305
9.33k
   child = child->getNextChild()) {
306
9.33k
      if (child->isElement()) {
307
5.49k
  scanNode((ZxElement *)child, childName, childFullName,
308
5.49k
     childNameIdx, childFullNameIdx, childExclGroupName,
309
5.49k
     dataElem, formValues);
310
5.49k
      }
311
9.33k
    }
312
4.94k
  }
313
314
5.51k
  if (childName != parentName) {
315
182
    delete childName;
316
182
  }
317
5.51k
  if (childFullName != parentFullName) {
318
182
    delete childFullName;
319
182
  }
320
5.51k
  if (childNameIdx != nameIdx) {
321
208
    delete childNameIdx;
322
208
  }
323
5.51k
  if (childFullNameIdx != fullNameIdx) {
324
208
    delete childFullNameIdx;
325
208
  }
326
5.51k
}
327
328
void XFAScanner::scanField(ZxElement *elem, GString *name, GString *fullName,
329
         GString *exclGroupName, ZxElement *dataElem,
330
573
         GHash *formValues) {
331
573
  GString *value = getFieldValue(elem, name, fullName, exclGroupName,
332
573
         dataElem, formValues);
333
573
  XFAFieldLayoutInfo *layoutInfo = getFieldLayoutInfo(elem);
334
573
  XFAFieldPictureInfo *pictureInfo = getFieldPictureInfo(elem);
335
573
  XFAFieldBarcodeInfo *barcodeInfo = getFieldBarcodeInfo(elem);
336
573
  XFAField *field = new XFAField(name->copy(), fullName->copy(), value,
337
573
         layoutInfo, pictureInfo, barcodeInfo);
338
573
  fields->add(field->fullName, field);
339
573
}
340
341
GString *XFAScanner::getFieldValue(ZxElement *elem, GString *name,
342
           GString *fullName, GString *exclGroupName,
343
573
           ZxElement *dataElem, GHash *formValues) {
344
573
  GString *val = NULL;
345
346
  //--- check the <xfa:datasets> packet
347
573
  val = getDatasetsValue(name->getCString(), dataElem);
348
573
  if (!val && exclGroupName) {
349
0
    val = (GString *)getDatasetsValue(exclGroupName->getCString(), dataElem);
350
0
  }
351
352
  //--- check the <form> element
353
573
  if (!val) {
354
571
    val = (GString *)formValues->lookup(fullName);
355
571
  }
356
357
  //--- check the <value> element within the field
358
573
  if (!val) {
359
571
    ZxElement *valueElem = elem->findFirstChildElement("value");
360
571
    if (valueElem) {
361
13
      ZxNode *child1Node = valueElem->getFirstChild();
362
13
      if (child1Node && child1Node->isElement()) {
363
0
  ZxNode *child2Node = ((ZxElement *)child1Node)->getFirstChild();
364
0
  if (child2Node && child2Node->isCharData()) {
365
0
    val = ((ZxCharData *)child2Node)->getData();
366
0
  }
367
0
      }
368
13
    }
369
571
  }
370
371
  //--- get the checkbutton item value
372
573
  GString *checkbuttonItem = NULL;
373
573
  ZxElement *uiElem = elem->findFirstChildElement("ui");
374
573
  if (uiElem) {
375
37
    ZxNode *uiChild = uiElem->getFirstChild();
376
37
    if (uiChild && uiChild->isElement("checkButton")) {
377
14
      ZxElement *itemsElem = elem->findFirstChildElement("items");
378
14
      if (itemsElem) {
379
10
  ZxNode *node1 = itemsElem->getFirstChild();
380
10
  if (node1 && node1->isElement()) {
381
5
    ZxNode *node2 = ((ZxElement *)node1)->getFirstChild();
382
5
    if (node2 && node2->isCharData()) {
383
5
      checkbuttonItem = ((ZxCharData *)node2)->getData();
384
5
    }
385
5
  }
386
10
      }
387
14
    }
388
37
  }
389
  // convert XFA checkbutton value to AcroForm-style On/Off value
390
573
  if (checkbuttonItem && val) {
391
0
    if (val->cmp(checkbuttonItem)) {
392
0
      val = new GString("Off");
393
0
    } else {
394
0
      val = new GString("On");
395
0
    }
396
573
  } else if (val) {
397
2
    val = val->copy();
398
2
  }
399
400
573
  return val;
401
573
}
402
403
775
GString *XFAScanner::getDatasetsValue(char *partName, ZxElement *elem) {
404
775
  if (!elem) {
405
545
    return NULL;
406
545
  }
407
408
  // partName = xxxx[nn].yyyy----
409
230
  char *p = strchr(partName, '[');
410
230
  if (!p) {
411
10
    return NULL;
412
10
  }
413
220
  int partLen = (int)(p - partName);
414
220
  int idx = atoi(p + 1);
415
220
  p = strchr(p + 1, '.');
416
220
  if (p) {
417
163
    ++p;
418
163
  }
419
420
220
  int curIdx = 0;
421
220
  for (ZxNode *node = elem->getFirstChild();
422
912
       node;
423
735
       node = node->getNextChild()) {
424
735
    if (!node->isElement()) {
425
0
      continue;
426
0
    }
427
735
    GString *nodeName = ((ZxElement *)node)->getType();
428
735
    if (nodeName->getLength() != partLen ||
429
686
  strncmp(nodeName->getCString(), partName, partLen)) {
430
686
      continue;
431
686
    }
432
49
    if (curIdx != idx) {
433
6
      ++curIdx;
434
6
      continue;
435
6
    }
436
43
    if (p) {
437
41
      GString *val = getDatasetsValue(p, (ZxElement *)node);
438
41
      if (val) {
439
2
  return val;
440
2
      }
441
39
      break;
442
41
    } else {
443
2
      ZxNode *child = ((ZxElement *)node)->getFirstChild();
444
2
      if (!child || !child->isCharData()) {
445
0
  return NULL;
446
0
      }
447
2
      return ((ZxCharData *)child)->getData();
448
2
    }
449
43
  }
450
451
  // search for an 'ancestor match'
452
216
  if (p) {
453
161
    return getDatasetsValue(p, elem);
454
161
  }
455
456
55
  return NULL;
457
216
}
458
459
573
XFAFieldLayoutInfo *XFAScanner::getFieldLayoutInfo(ZxElement *elem) {
460
573
  ZxElement *paraElem = elem->findFirstChildElement("para");
461
573
  if (!paraElem) {
462
558
    return NULL;
463
558
  }
464
15
  XFAFieldLayoutHAlign hAlign = xfaFieldLayoutHAlignLeft;
465
15
  ZxAttr *hAlignAttr = paraElem->findAttr("hAlign");
466
15
  if (hAlignAttr) {
467
13
    if (!hAlignAttr->getValue()->cmp("left")) {
468
0
      hAlign = xfaFieldLayoutHAlignLeft;
469
13
    } else if (!hAlignAttr->getValue()->cmp("center")) {
470
0
      hAlign = xfaFieldLayoutHAlignCenter;
471
13
    } else if (!hAlignAttr->getValue()->cmp("right")) {
472
0
      hAlign = xfaFieldLayoutHAlignRight;
473
0
    }
474
13
  }
475
15
  XFAFieldLayoutVAlign vAlign = xfaFieldLayoutVAlignTop;
476
15
  ZxAttr *vAlignAttr = paraElem->findAttr("vAlign");
477
15
  if (vAlignAttr) {
478
0
    if (!vAlignAttr->getValue()->cmp("top")) {
479
0
      vAlign = xfaFieldLayoutVAlignTop;
480
0
    } else if (!vAlignAttr->getValue()->cmp("middle")) {
481
0
      vAlign = xfaFieldLayoutVAlignMiddle;
482
0
    } else if (!vAlignAttr->getValue()->cmp("bottom")) {
483
0
      vAlign = xfaFieldLayoutVAlignBottom;
484
0
    }
485
0
  }
486
15
  return new XFAFieldLayoutInfo(hAlign, vAlign);
487
573
}
488
489
573
XFAFieldPictureInfo *XFAScanner::getFieldPictureInfo(ZxElement *elem) {
490
573
  ZxElement *uiElem = elem->findFirstChildElement("ui");
491
573
  if (!uiElem) {
492
536
    return NULL;
493
536
  }
494
37
  XFAFieldPictureSubtype subtype;
495
37
  if (uiElem->findFirstChildElement("dateTimeEdit")) {
496
0
    subtype = xfaFieldPictureDateTime;
497
37
  } else if (uiElem->findFirstChildElement("numericEdit")) {
498
0
    subtype = xfaFieldPictureNumeric;
499
37
  } else if (uiElem->findFirstChildElement("textEdit")) {
500
0
    subtype = xfaFieldPictureText;
501
37
  } else {
502
37
    return NULL;
503
37
  }
504
505
0
  ZxElement *formatElem, *pictureElem;
506
0
  ZxNode *pictureChildNode;
507
0
  if (!(formatElem = elem->findFirstChildElement("format")) ||
508
0
      !(pictureElem = formatElem->findFirstChildElement("picture")) ||
509
0
      !(pictureChildNode = pictureElem->getFirstChild()) ||
510
0
      !pictureChildNode->isCharData()) {
511
0
    return NULL;
512
0
  }
513
0
  GString *format = ((ZxCharData *)pictureChildNode)->getData()->copy();
514
515
0
  return new XFAFieldPictureInfo(subtype, format);
516
0
}
517
518
573
XFAFieldBarcodeInfo *XFAScanner::getFieldBarcodeInfo(ZxElement *elem) {
519
573
  ZxElement *uiElem, *barcodeElem;
520
573
  if (!(uiElem = elem->findFirstChildElement("ui")) ||
521
573
      !(barcodeElem = uiElem->findFirstChildElement("barcode"))) {
522
573
    return NULL;
523
573
  }
524
525
0
  ZxAttr *attr;
526
0
  if (!(attr = barcodeElem->findAttr("type"))) {
527
0
    return NULL;
528
0
  }
529
0
  GString *barcodeType = attr->getValue()->copy();
530
531
0
  double wideNarrowRatio = 3;
532
0
  if ((attr = barcodeElem->findAttr("wideNarrowRatio"))) {
533
0
    char *s = attr->getValue()->getCString();
534
0
    char *colon = strchr(s, ':');
535
0
    if (colon) {
536
0
      GString *numStr = new GString(s, (int)(colon - s));
537
0
      double num = atof(numStr->getCString());
538
0
      delete numStr;
539
0
      double den = atof(colon + 1);
540
0
      if (den == 0) {
541
0
  wideNarrowRatio = num;
542
0
      } else {
543
0
  wideNarrowRatio = num / den;
544
0
      }
545
0
    } else {
546
0
      wideNarrowRatio = atof(s);
547
0
    }
548
0
  }
549
550
0
  double moduleWidth = (0.25 / 25.4) * 72.0; // 0.25mm
551
0
  if ((attr = barcodeElem->findAttr("moduleWidth"))) {
552
0
    moduleWidth = getMeasurement(attr->getValue());
553
0
  }
554
555
0
  double moduleHeight = (5.0 / 25.4) * 72.0; // 5mm
556
0
  if ((attr = barcodeElem->findAttr("moduleHeight"))) {
557
0
    moduleHeight = getMeasurement(attr->getValue());
558
0
  }
559
560
0
  int dataLength = 0;
561
0
  if ((attr = barcodeElem->findAttr("dataLength"))) {
562
0
    dataLength = atoi(attr->getValue()->getCString());
563
0
  }
564
565
0
  int errorCorrectionLevel = 0;
566
0
  if ((attr = barcodeElem->findAttr("errorCorrectionLevel"))) {
567
0
    errorCorrectionLevel = atoi(attr->getValue()->getCString());
568
0
  }
569
570
0
  GString *textLocation;
571
0
  if ((attr = barcodeElem->findAttr("textLocation"))) {
572
0
    textLocation = attr->getValue()->copy();
573
0
  } else {
574
0
    textLocation = new GString("below");
575
0
  }
576
577
0
  return new XFAFieldBarcodeInfo(barcodeType, wideNarrowRatio,
578
0
         moduleWidth, moduleHeight, dataLength,
579
0
         errorCorrectionLevel, textLocation);
580
0
}
581
582
0
double XFAScanner::getMeasurement(GString *s) {
583
0
  int i = 0;
584
0
  GBool neg = gFalse;
585
0
  if (i < s->getLength() && s->getChar(i) == '+') {
586
0
    ++i;
587
0
  } else if (i < s->getLength() && s->getChar(i) == '-') {
588
0
    neg = gTrue;
589
0
    ++i;
590
0
  }
591
0
  double val = 0;
592
0
  while (i < s->getLength() && s->getChar(i) >= '0' && s->getChar(i) <= '9') {
593
0
    val = val * 10 + s->getChar(i) - '0';
594
0
    ++i;
595
0
  }
596
0
  if (i < s->getLength() && s->getChar(i) == '.') {
597
0
    ++i;
598
0
    double mul = 0.1;
599
0
    while (i < s->getLength() &&
600
0
     s->getChar(i) >= '0' && s->getChar(i) <= '9') {
601
0
      val += mul * (s->getChar(i) - '0');
602
0
      mul *= 0.1;
603
0
      ++i;
604
0
    }
605
0
  }
606
0
  if (neg) {
607
0
    val = -val;
608
0
  }
609
0
  if (i+1 < s->getLength()) {
610
0
    if (s->getChar(i) == 'i' && s->getChar(i+1) == 'n') {
611
0
      val *= 72;
612
0
    } else if (s->getChar(i) == 'p' && s->getChar(i+1) == 't') {
613
      // no change
614
0
    } else if (s->getChar(i) == 'c' && s->getChar(i+1) == 'm') {
615
0
      val *= 72 / 2.54;
616
0
    } else if (s->getChar(i) == 'm' && s->getChar(i+1) == 'm') {
617
0
      val *= 72 / 25.4;
618
0
    } else {
619
      // default to inches
620
0
      val *= 72;
621
0
    }
622
0
  } else {
623
    // default to inches
624
0
    val *= 72;
625
0
  }
626
0
  return val;
627
0
}
628
629
5.51k
GString *XFAScanner::getNodeName(ZxElement *elem) {
630
5.51k
  if (elem->isElement("template") ||
631
5.25k
      elem->isElement("area") ||
632
5.25k
      elem->isElement("draw")) {
633
265
    return NULL;
634
265
  }
635
5.25k
  if (!elem->isElement("field") && nodeIsBindNone(elem)) {
636
0
    return NULL;
637
0
  }
638
5.25k
  ZxAttr *nameAttr = elem->findAttr("name");
639
5.25k
  if (!nameAttr) {
640
5.07k
    return NULL;
641
5.07k
  }
642
182
  return nameAttr->getValue();
643
5.25k
}
644
645
5.51k
GString *XFAScanner::getNodeFullName(ZxElement *elem) {
646
5.51k
  if (elem->isElement("template") ||
647
5.25k
      elem->isElement("draw")) {
648
265
    return NULL;
649
265
  }
650
5.25k
  ZxAttr *nameAttr = elem->findAttr("name");
651
5.25k
  if (!nameAttr) {
652
5.07k
    return NULL;
653
5.07k
  }
654
182
  return nameAttr->getValue();
655
5.25k
}
656
657
182
GBool XFAScanner::nodeIsBindGlobal(ZxElement *elem) {
658
182
  ZxElement *bindElem = elem->findFirstChildElement("bind");
659
182
  if (!bindElem) {
660
176
    return gFalse;
661
176
  }
662
6
  ZxAttr *attr = bindElem->findAttr("match");
663
6
  return attr && !attr->getValue()->cmp("global");
664
182
}
665
666
4.67k
GBool XFAScanner::nodeIsBindNone(ZxElement *elem) {
667
4.67k
  ZxElement *bindElem = elem->findFirstChildElement("bind");
668
4.67k
  if (!bindElem) {
669
4.67k
    return gFalse;
670
4.67k
  }
671
9
  ZxAttr *attr = bindElem->findAttr("match");
672
9
  return attr && !attr->getValue()->cmp("none");
673
4.67k
}