Coverage Report

Created: 2026-12-01 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pupnp/ixml/src/document.c
Line
Count
Source
1
/*******************************************************************************
2
 *
3
 * Copyright (c) 2000-2003 Intel Corporation
4
 * All rights reserved.
5
 * Copyright (c) 2012 France Telecom All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright notice,
11
 * this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright notice,
13
 * this list of conditions and the following disclaimer in the documentation
14
 * and/or other materials provided with the distribution.
15
 * - Neither name of Intel Corporation nor the names of its contributors
16
 * may be used to endorse or promote products derived from this software
17
 * without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
 *
31
 ******************************************************************************/
32
33
/*!
34
 * \file
35
 */
36
37
#include "ixml.h"
38
#include "ixmldebug.h"
39
#include "ixmlparser.h"
40
41
#include <stdlib.h>
42
#include <string.h>
43
44
#include "posix_overwrites.h" // IWYU pragma: keep
45
46
void ixmlDocument_init(IXML_Document *doc)
47
0
{
48
0
  memset(doc, 0, sizeof(IXML_Document));
49
0
}
50
51
void ixmlDocument_free(IXML_Document *doc)
52
0
{
53
0
  if (doc) {
54
0
    ixmlNode_free((IXML_Node *)doc);
55
0
  }
56
0
}
57
58
/*!
59
 * Sets the owner document of nodeptr, of its descendants and of its following
60
 * siblings. Iterative, so a deeply nested tree cannot overflow the stack.
61
 *
62
 * Internal function called by ixmlDocument_importNode
63
 */
64
static void ixmlDocument_setOwnerDocument(
65
  /*! [in] The document node. */
66
  IXML_Document *doc,
67
  /*! [in] The first node to update. */
68
  IXML_Node *nodeptr)
69
0
{
70
0
  IXML_Node *fence;
71
0
  IXML_Node *node;
72
73
0
  if (!nodeptr)
74
0
    return;
75
0
  fence = nodeptr->parentNode;
76
0
  node = nodeptr;
77
0
  while (node) {
78
0
    node->ownerDocument = doc;
79
0
    if (node->firstChild) {
80
0
      node = node->firstChild;
81
0
    } else {
82
0
      while (node && node != fence && !node->nextSibling)
83
0
        node = node->parentNode;
84
0
      if (!node || node == fence)
85
0
        return;
86
0
      node = node->nextSibling;
87
0
    }
88
0
  }
89
0
}
90
91
int ixmlDocument_importNode(
92
  IXML_Document *doc, IXML_Node *importNode, int deep, IXML_Node **rtNode)
93
0
{
94
0
  unsigned short nodeType;
95
0
  IXML_Node *newNode;
96
97
0
  *rtNode = NULL;
98
99
0
  if (!doc || !importNode) {
100
0
    return IXML_INVALID_PARAMETER;
101
0
  }
102
103
0
  nodeType = ixmlNode_getNodeType(importNode);
104
0
  if (nodeType == eDOCUMENT_NODE) {
105
0
    return IXML_NOT_SUPPORTED_ERR;
106
0
  }
107
108
0
  newNode = ixmlNode_cloneNode(importNode, deep);
109
0
  if (!newNode) {
110
0
    return IXML_FAILED;
111
0
  }
112
113
0
  ixmlDocument_setOwnerDocument(doc, newNode);
114
0
  *rtNode = newNode;
115
116
0
  return IXML_SUCCESS;
117
0
}
118
119
int ixmlDocument_createElementEx(
120
  IXML_Document *doc, const DOMString tagName, IXML_Element **rtElement)
121
0
{
122
0
  int errCode = IXML_SUCCESS;
123
0
  IXML_Element *newElement = NULL;
124
125
0
  if (!doc || !tagName) {
126
0
    errCode = IXML_INVALID_PARAMETER;
127
0
    goto ErrorHandler;
128
0
  }
129
130
0
  newElement = (IXML_Element *)malloc(sizeof(IXML_Element));
131
0
  if (!newElement) {
132
0
    errCode = IXML_INSUFFICIENT_MEMORY;
133
0
    goto ErrorHandler;
134
0
  }
135
136
0
  ixmlElement_init(newElement);
137
0
  newElement->tagName = strdup(tagName);
138
0
  if (!newElement->tagName) {
139
0
    ixmlElement_free(newElement);
140
0
    newElement = NULL;
141
0
    errCode = IXML_INSUFFICIENT_MEMORY;
142
0
    goto ErrorHandler;
143
0
  }
144
  /* set the node fields */
145
0
  newElement->n.nodeType = eELEMENT_NODE;
146
0
  newElement->n.nodeName = strdup(tagName);
147
0
  if (!newElement->n.nodeName) {
148
0
    ixmlElement_free(newElement);
149
0
    newElement = NULL;
150
0
    errCode = IXML_INSUFFICIENT_MEMORY;
151
0
    goto ErrorHandler;
152
0
  }
153
154
0
  newElement->n.ownerDocument = doc;
155
156
0
ErrorHandler:
157
0
  *rtElement = newElement;
158
159
0
  return errCode;
160
0
}
161
162
IXML_Element *ixmlDocument_createElement(
163
  IXML_Document *doc, const DOMString tagName)
164
0
{
165
0
  IXML_Element *newElement = NULL;
166
0
  int ret = IXML_SUCCESS;
167
168
0
  ret = ixmlDocument_createElementEx(doc, tagName, &newElement);
169
0
  if (ret != IXML_SUCCESS) {
170
0
    IxmlPrintf(__FILE__,
171
0
      __LINE__,
172
0
      "ixmlDocument_createElement",
173
0
      "Error %d\n",
174
0
      ret);
175
0
    return NULL;
176
0
  }
177
0
  return newElement;
178
0
}
179
180
int ixmlDocument_createDocumentEx(IXML_Document **rtDoc)
181
0
{
182
0
  IXML_Document *doc;
183
0
  int errCode = IXML_SUCCESS;
184
185
0
  doc = NULL;
186
0
  doc = (IXML_Document *)malloc(sizeof(IXML_Document));
187
0
  if (!doc) {
188
0
    errCode = IXML_INSUFFICIENT_MEMORY;
189
0
    goto ErrorHandler;
190
0
  }
191
192
0
  ixmlDocument_init(doc);
193
194
0
  doc->n.nodeName = strdup((const char *)DOCUMENTNODENAME);
195
0
  if (!doc->n.nodeName) {
196
0
    ixmlDocument_free(doc);
197
0
    doc = NULL;
198
0
    errCode = IXML_INSUFFICIENT_MEMORY;
199
0
    goto ErrorHandler;
200
0
  }
201
202
0
  doc->n.nodeType = eDOCUMENT_NODE;
203
0
  doc->n.ownerDocument = doc;
204
205
0
ErrorHandler:
206
  /* doc is NULL on failure */
207
0
  *rtDoc = doc;
208
0
  return errCode;
209
0
}
210
211
IXML_Document *ixmlDocument_createDocument(void)
212
0
{
213
0
  IXML_Document *doc = NULL;
214
215
0
  ixmlDocument_createDocumentEx(&doc);
216
217
0
  return doc;
218
0
}
219
220
int ixmlDocument_createTextNodeEx(
221
  IXML_Document *doc, const DOMString data, IXML_Node **textNode)
222
0
{
223
0
  IXML_Node *returnNode;
224
0
  int rc = IXML_SUCCESS;
225
226
0
  returnNode = NULL;
227
0
  if (!doc || !data) {
228
0
    rc = IXML_INVALID_PARAMETER;
229
0
    goto ErrorHandler;
230
0
  }
231
232
0
  returnNode = (IXML_Node *)malloc(sizeof(IXML_Node));
233
0
  if (!returnNode) {
234
0
    rc = IXML_INSUFFICIENT_MEMORY;
235
0
    goto ErrorHandler;
236
0
  }
237
  /* initialize the node */
238
0
  ixmlNode_init(returnNode);
239
240
0
  returnNode->nodeName = strdup((const char *)TEXTNODENAME);
241
0
  if (!returnNode->nodeName) {
242
0
    ixmlNode_free(returnNode);
243
0
    returnNode = NULL;
244
0
    rc = IXML_INSUFFICIENT_MEMORY;
245
0
    goto ErrorHandler;
246
0
  }
247
  /* add in node value */
248
0
  if (data) {
249
0
    returnNode->nodeValue = strdup(data);
250
0
    if (!returnNode->nodeValue) {
251
0
      ixmlNode_free(returnNode);
252
0
      returnNode = NULL;
253
0
      rc = IXML_INSUFFICIENT_MEMORY;
254
0
      goto ErrorHandler;
255
0
    }
256
0
  }
257
258
0
  returnNode->nodeType = eTEXT_NODE;
259
0
  returnNode->ownerDocument = doc;
260
261
0
ErrorHandler:
262
0
  *textNode = returnNode;
263
0
  return rc;
264
0
}
265
266
IXML_Node *ixmlDocument_createTextNode(IXML_Document *doc, const DOMString data)
267
0
{
268
0
  IXML_Node *returnNode = NULL;
269
270
0
  ixmlDocument_createTextNodeEx(doc, data, &returnNode);
271
272
0
  return returnNode;
273
0
}
274
275
int ixmlDocument_createAttributeEx(
276
  IXML_Document *doc, const DOMString name, IXML_Attr **rtAttr)
277
0
{
278
0
  int errCode = IXML_SUCCESS;
279
0
  IXML_Attr *attrNode = NULL;
280
281
0
  if (!doc || !name) {
282
0
    errCode = IXML_INVALID_PARAMETER;
283
0
    goto ErrorHandler;
284
0
  }
285
0
  attrNode = (IXML_Attr *)malloc(sizeof(IXML_Attr));
286
0
  if (!attrNode) {
287
0
    errCode = IXML_INSUFFICIENT_MEMORY;
288
0
    goto ErrorHandler;
289
0
  }
290
0
  ixmlAttr_init(attrNode);
291
0
  attrNode->n.nodeType = eATTRIBUTE_NODE;
292
  /* set the node fields */
293
0
  attrNode->n.nodeName = strdup(name);
294
0
  if (!attrNode->n.nodeName) {
295
0
    ixmlAttr_free(attrNode);
296
0
    attrNode = NULL;
297
0
    errCode = IXML_INSUFFICIENT_MEMORY;
298
0
    goto ErrorHandler;
299
0
  }
300
0
  attrNode->n.ownerDocument = doc;
301
302
0
ErrorHandler:
303
0
  *rtAttr = attrNode;
304
0
  return errCode;
305
0
}
306
307
IXML_Attr *ixmlDocument_createAttribute(
308
  IXML_Document *doc, const DOMString name)
309
0
{
310
0
  IXML_Attr *attrNode = NULL;
311
312
0
  if (ixmlDocument_createAttributeEx(doc, name, &attrNode) !=
313
0
    IXML_SUCCESS)
314
0
    return NULL;
315
316
0
  return attrNode;
317
0
}
318
319
int ixmlDocument_createAttributeNSEx(IXML_Document *doc,
320
  const DOMString namespaceURI,
321
  const DOMString qualifiedName,
322
  IXML_Attr **rtAttr)
323
0
{
324
0
  int errCode = IXML_SUCCESS;
325
0
  IXML_Attr *attrNode = NULL;
326
327
0
  if (!doc || !namespaceURI || !qualifiedName) {
328
0
    errCode = IXML_INVALID_PARAMETER;
329
0
    goto ErrorHandler;
330
0
  }
331
332
0
  errCode = ixmlDocument_createAttributeEx(doc, qualifiedName, &attrNode);
333
0
  if (errCode != IXML_SUCCESS) {
334
0
    goto ErrorHandler;
335
0
  }
336
  /* set the namespaceURI field */
337
0
  attrNode->n.namespaceURI = strdup(namespaceURI);
338
0
  if (!attrNode->n.namespaceURI) {
339
0
    ixmlAttr_free(attrNode);
340
0
    attrNode = NULL;
341
0
    errCode = IXML_INSUFFICIENT_MEMORY;
342
0
    goto ErrorHandler;
343
0
  }
344
  /* set the localName and prefix */
345
0
  errCode = ixmlNode_setNodeName((IXML_Node *)attrNode, qualifiedName);
346
0
  if (errCode != IXML_SUCCESS) {
347
0
    ixmlAttr_free(attrNode);
348
0
    attrNode = NULL;
349
0
    goto ErrorHandler;
350
0
  }
351
352
0
ErrorHandler:
353
0
  *rtAttr = attrNode;
354
0
  return errCode;
355
0
}
356
357
IXML_Attr *ixmlDocument_createAttributeNS(IXML_Document *doc,
358
  const DOMString namespaceURI,
359
  const DOMString qualifiedName)
360
0
{
361
0
  IXML_Attr *attrNode = NULL;
362
363
0
  ixmlDocument_createAttributeNSEx(
364
0
    doc, namespaceURI, qualifiedName, &attrNode);
365
366
0
  return attrNode;
367
0
}
368
369
int ixmlDocument_createCDATASectionEx(
370
  IXML_Document *doc, const DOMString data, IXML_CDATASection **rtCD)
371
0
{
372
0
  int errCode = IXML_SUCCESS;
373
0
  IXML_CDATASection *cDSectionNode = NULL;
374
375
0
  if (!doc || !data) {
376
0
    errCode = IXML_INVALID_PARAMETER;
377
0
    goto ErrorHandler;
378
0
  }
379
380
0
  cDSectionNode = (IXML_CDATASection *)malloc(sizeof(IXML_CDATASection));
381
0
  if (!cDSectionNode) {
382
0
    errCode = IXML_INSUFFICIENT_MEMORY;
383
0
    goto ErrorHandler;
384
0
  }
385
386
0
  ixmlCDATASection_init(cDSectionNode);
387
0
  cDSectionNode->n.nodeType = eCDATA_SECTION_NODE;
388
0
  cDSectionNode->n.nodeName = strdup((const char *)CDATANODENAME);
389
0
  if (!cDSectionNode->n.nodeName) {
390
0
    ixmlCDATASection_free(cDSectionNode);
391
0
    cDSectionNode = NULL;
392
0
    errCode = IXML_INSUFFICIENT_MEMORY;
393
0
    goto ErrorHandler;
394
0
  }
395
396
0
  cDSectionNode->n.nodeValue = strdup(data);
397
0
  if (!cDSectionNode->n.nodeValue) {
398
0
    ixmlCDATASection_free(cDSectionNode);
399
0
    cDSectionNode = NULL;
400
0
    errCode = IXML_INSUFFICIENT_MEMORY;
401
0
    goto ErrorHandler;
402
0
  }
403
404
0
  cDSectionNode->n.ownerDocument = doc;
405
406
0
ErrorHandler:
407
0
  *rtCD = cDSectionNode;
408
0
  return errCode;
409
0
}
410
411
IXML_CDATASection *ixmlDocument_createCDATASection(
412
  IXML_Document *doc, const DOMString data)
413
0
{
414
0
  IXML_CDATASection *cDSectionNode = NULL;
415
416
0
  ixmlDocument_createCDATASectionEx(doc, data, &cDSectionNode);
417
418
0
  return cDSectionNode;
419
0
}
420
421
int ixmlDocument_createElementNSEx(IXML_Document *doc,
422
  const DOMString namespaceURI,
423
  const DOMString qualifiedName,
424
  IXML_Element **rtElement)
425
0
{
426
0
  IXML_Element *newElement = NULL;
427
0
  int ret = IXML_SUCCESS;
428
0
  int line = 0;
429
430
0
  if (!doc || !namespaceURI || !qualifiedName) {
431
0
    line = __LINE__;
432
0
    ret = IXML_INVALID_PARAMETER;
433
0
    goto ErrorHandler;
434
0
  }
435
436
0
  ret = ixmlDocument_createElementEx(doc, qualifiedName, &newElement);
437
0
  if (ret != IXML_SUCCESS) {
438
0
    line = __LINE__;
439
0
    goto ErrorHandler;
440
0
  }
441
  /* set the namespaceURI field */
442
0
  newElement->n.namespaceURI = strdup(namespaceURI);
443
0
  if (!newElement->n.namespaceURI) {
444
0
    line = __LINE__;
445
0
    ixmlElement_free(newElement);
446
0
    newElement = NULL;
447
0
    ret = IXML_INSUFFICIENT_MEMORY;
448
0
    goto ErrorHandler;
449
0
  }
450
  /* set the localName and prefix */
451
0
  ret = ixmlNode_setNodeName((IXML_Node *)newElement, qualifiedName);
452
0
  if (ret != IXML_SUCCESS) {
453
0
    line = __LINE__;
454
0
    ixmlElement_free(newElement);
455
0
    newElement = NULL;
456
0
    ret = IXML_INSUFFICIENT_MEMORY;
457
0
    goto ErrorHandler;
458
0
  }
459
460
0
  newElement->n.nodeValue = NULL;
461
462
0
ErrorHandler:
463
0
  *rtElement = newElement;
464
0
  if (ret != IXML_SUCCESS) {
465
0
    IxmlPrintf(__FILE__,
466
0
      line,
467
0
      "ixmlDocument_createElementNSEx",
468
0
      "Error %d\n",
469
0
      ret);
470
0
  }
471
472
0
  return ret;
473
0
}
474
475
IXML_Element *ixmlDocument_createElementNS(IXML_Document *doc,
476
  const DOMString namespaceURI,
477
  const DOMString qualifiedName)
478
0
{
479
0
  IXML_Element *newElement = NULL;
480
481
0
  ixmlDocument_createElementNSEx(
482
0
    doc, namespaceURI, qualifiedName, &newElement);
483
484
0
  return newElement;
485
0
}
486
487
IXML_NodeList *ixmlDocument_getElementsByTagName(
488
  IXML_Document *doc, const DOMString tagName)
489
0
{
490
0
  IXML_NodeList *returnNodeList = NULL;
491
492
0
  if (!doc || !tagName) {
493
0
    return NULL;
494
0
  }
495
496
0
  ixmlNode_getElementsByTagName(
497
0
    (IXML_Node *)doc, tagName, &returnNodeList);
498
499
0
  return returnNodeList;
500
0
}
501
502
IXML_NodeList *ixmlDocument_getElementsByTagNameNS(IXML_Document *doc,
503
  const DOMString namespaceURI,
504
  const DOMString localName)
505
0
{
506
0
  IXML_NodeList *returnNodeList = NULL;
507
508
0
  if (!doc || !namespaceURI || !localName) {
509
0
    return NULL;
510
0
  }
511
512
0
  ixmlNode_getElementsByTagNameNS(
513
0
    (IXML_Node *)doc, namespaceURI, localName, &returnNodeList);
514
515
0
  return returnNodeList;
516
0
}
517
518
IXML_Element *ixmlDocument_getElementById(
519
  IXML_Document *doc, const DOMString tagName)
520
0
{
521
0
  IXML_Node *fence;
522
0
  IXML_Node *node = (IXML_Node *)doc;
523
0
  const char *name;
524
525
0
  if (!node || !tagName) {
526
0
    return NULL;
527
0
  }
528
  /* Iterative pre-order walk: a deeply nested document from the network
529
   * must not be able to overflow the stack. */
530
0
  fence = node->parentNode;
531
0
  while (node) {
532
0
    if (ixmlNode_getNodeType(node) == eELEMENT_NODE) {
533
0
      name = ixmlNode_getNodeName(node);
534
0
      if (name && strcmp(tagName, name) == 0) {
535
0
        return (IXML_Element *)node;
536
0
      }
537
0
    }
538
0
    if (node->firstChild) {
539
0
      node = node->firstChild;
540
0
    } else {
541
0
      while (node && node != fence && !node->nextSibling)
542
0
        node = node->parentNode;
543
0
      if (!node || node == fence)
544
0
        return NULL;
545
0
      node = node->nextSibling;
546
0
    }
547
0
  }
548
549
0
  return NULL;
550
0
}