Debian Codegraph @

libgupnp-av/fragment-util.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2012 Intel Corporation
3 *
4 * Authors: Krzesimir Nowak <[email protected]>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-or-later
7 *
8 */
9
10 #include <config.h>
11
12 #include <stdarg.h>
13 #include <libxml/parserInternals.h>
14
15 #include "fragment-util.h"
16 #include "xml-util.h"
17
18 typedef struct {
19 gchar *node_name;
20 gchar *attribute_name;
21 } NodeDiff;
22
23 static NodeDiff *
24 node_diff_new (const xmlChar *node_name,
25 const xmlChar *attribute_name)
26 {
27 NodeDiff *diff = g_slice_new (NodeDiff);
28
29 diff->node_name = g_strdup ((gchar *) node_name);
30 diff->attribute_name = g_strdup ((gchar *) attribute_name);
31
32 return diff;
33 }
34
35 static void
36 node_diff_free (NodeDiff *diff)
37 {
38 if (diff != NULL) {
39 g_free (diff->node_name);
40 g_free (diff->attribute_name);
41 g_slice_free (NodeDiff, diff);
42 }
43 }
44
45 static GList *
46 get_toplevel_changes (xmlNodePtr current_node,
47 xmlNodePtr new_node)
48 {
49 xmlAttrPtr attribute;
50 GHashTable *current_attributes = av_xml_util_get_attributes_map
51 (current_node);
52 GList *changes = NULL;
53 const xmlChar *name = new_node->name;
54
55 /* compare attributes */
56 for (attribute = new_node->properties;
57 attribute != NULL;
58 attribute = attribute->next) {
59 const xmlChar *value = NULL;
60 const xmlChar *key = attribute->name;
61 gboolean differs = FALSE;
62
63 if (g_hash_table_lookup_extended (current_attributes,
64 key,
65 NULL,
66 (gpointer *) &value)) {
67 if (xmlStrcmp (value, attribute->children->content))
68 differs = TRUE;
69 g_hash_table_remove (current_attributes, key);
70 } else
71 differs = TRUE;
72 if (differs)
73 changes = g_list_prepend (changes,
74 node_diff_new (name,
75 key));
76 }
77
78 if (g_hash_table_size (current_attributes) > 0) {
79 GHashTableIter iter;
80 xmlChar *key = NULL;
81
82 g_hash_table_iter_init (&iter, current_attributes);
83 while (g_hash_table_iter_next (&iter,
84 (gpointer *) &key,
85 NULL))
86 changes = g_list_prepend (changes, node_diff_new (name,
87 key));
88 }
89
90 g_hash_table_unref (current_attributes);
91
92 return changes;
93 }
94
95 static gboolean
96 is_read_only (const gchar *changed_element,
97 const gchar *changed_attribute)
98 {
99 static GHashTable *readonly_props = NULL;
100 static gsize readonly_props_loaded = 0;
101
102 if (g_once_init_enter (&readonly_props_loaded)) {
103 readonly_props = g_hash_table_new (g_str_hash,
104 g_str_equal);
105
106 g_hash_table_add (readonly_props, (gpointer) "@id");
107 g_hash_table_add (readonly_props, (gpointer) "@parentID");
108 g_hash_table_add (readonly_props, (gpointer) "@refID");
109 g_hash_table_add (readonly_props, (gpointer) "@restricted");
110 g_hash_table_add (readonly_props, (gpointer) "@searchable");
111 g_hash_table_add (readonly_props, (gpointer) "@childCount");
112 g_hash_table_add (readonly_props, (gpointer) "searchClass");
113 g_hash_table_add (readonly_props, (gpointer) "searchClass@name");
114 g_hash_table_add (readonly_props, (gpointer) "searchClass@includeDerived");
115 g_hash_table_add (readonly_props, (gpointer) "createClass");
116 g_hash_table_add (readonly_props, (gpointer) "createClass@name");
117 g_hash_table_add (readonly_props, (gpointer) "createClass@includeDerived");
118 g_hash_table_add (readonly_props, (gpointer) "writeStatus");
119 g_hash_table_add (readonly_props, (gpointer) "res@importUri");
120 g_hash_table_add (readonly_props, (gpointer) "storageTotal");
121 g_hash_table_add (readonly_props, (gpointer) "storageUsed");
122 g_hash_table_add (readonly_props, (gpointer) "storageFree");
123 g_hash_table_add (readonly_props, (gpointer) "storageMaxPartition");
124 g_hash_table_add (readonly_props, (gpointer) "storageMedium");
125 g_hash_table_add (readonly_props, (gpointer) "playbackCount");
126 g_hash_table_add (readonly_props, (gpointer) "srsRecordScheduleID");
127 g_hash_table_add (readonly_props, (gpointer) "srsRecordTaskID");
128 g_hash_table_add (readonly_props, (gpointer) "price");
129 g_hash_table_add (readonly_props, (gpointer) "price@currency");
130 g_hash_table_add (readonly_props, (gpointer) "payPerView");
131 g_hash_table_add (readonly_props, (gpointer) "dateTimeRange");
132 g_hash_table_add (readonly_props, (gpointer)
133 "dateTimeRange@daylightSaving");
134 g_hash_table_add (readonly_props, (gpointer) "signalStrength");
135 g_hash_table_add (readonly_props, (gpointer) "signalLocked");
136 g_hash_table_add (readonly_props, (gpointer) "tuned");
137 g_hash_table_add (readonly_props, (gpointer) "containerUpdateID");
138 g_hash_table_add (readonly_props, (gpointer) "objectUpdateID");
139 g_hash_table_add (readonly_props, (gpointer) "totalDeletedChildCount");
140 g_hash_table_add (readonly_props, (gpointer) "res@updateCount");
141 g_once_init_leave (&readonly_props_loaded, 1);
142 }
143 if (changed_element != NULL) {
144 if (changed_attribute != NULL) {
145 gchar *test_prop = g_strdup_printf ("%s@%s",
146 changed_element,
147 changed_attribute);
148 gboolean result = g_hash_table_contains (readonly_props,
149 test_prop);
150
151 g_free (test_prop);
152 if (result)
153 return TRUE;
154 test_prop = g_strdup_printf ("@%s", changed_attribute);
155 result = g_hash_table_contains (readonly_props,
156 test_prop);
157 g_free (test_prop);
158 if (result)
159 return TRUE;
160 }
161
162 return g_hash_table_contains (readonly_props, changed_element);
163 }
164
165 return FALSE;
166 }
167
168 static gboolean
169 is_any_change_read_only (xmlNodePtr current_node,
170 xmlNodePtr new_node)
171 {
172 GList *changes = get_toplevel_changes (current_node, new_node);
173 GList *iter;
174 gboolean read_only = FALSE;
175
176 for (iter = changes; iter != NULL; iter = iter->next) {
177 NodeDiff *diff = (NodeDiff *) iter->data;
178
179 if (is_read_only (diff->node_name,
180 diff->attribute_name)) {
181 read_only = TRUE;
182
183 break;
184 }
185 }
186
187 if (changes != NULL)
188 g_list_free_full (changes, (GDestroyNotify) node_diff_free);
189 return read_only;
190 }
191
192 static GUPnPDIDLLiteFragmentResult
193 apply_temporary_modification (DocNode *modified,
194 xmlNodePtr current_node,
195 xmlNodePtr new_node,
196 XSDData *xsd_data)
197 {
198 xmlNodePtr mod_cur_node = av_xml_util_find_node (modified->node,
199 current_node);
200 xmlNodePtr new_node_copy = av_xml_util_copy_node (new_node);
201
202 if (mod_cur_node == NULL) {
203 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_UNKNOWN_ERROR;
204 }
205
206 xmlUnlinkNode (new_node_copy);
207 mod_cur_node = xmlReplaceNode (mod_cur_node, new_node_copy);
208 xmlUnlinkNode (mod_cur_node);
209 xmlFreeNode (mod_cur_node);
210
211 if (!xsd_data_validate_doc (xsd_data, modified->doc))
212 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_NEW_INVALID;
213
214 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_OK;
215 }
216
217 static GUPnPDIDLLiteFragmentResult
218 apply_temporary_addition (DocNode *modified,
219 xmlNodePtr sibling,
220 xmlNodePtr new_node,
221 XSDData *xsd_data)
222 {
223 xmlNodePtr mod_sibling;
224 xmlNodePtr new_node_copy = av_xml_util_copy_node (new_node);
225
226 if (sibling->doc == modified->doc)
227 mod_sibling = sibling;
228 else
229 mod_sibling = av_xml_util_find_node (modified->node, sibling);
230
231 if (mod_sibling == NULL)
232 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_UNKNOWN_ERROR;
233
234 xmlUnlinkNode (new_node_copy);
235
236 if (xmlAddNextSibling (mod_sibling, new_node_copy) == NULL) {
237 xmlFreeNode (new_node_copy);
238
239 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_UNKNOWN_ERROR;
240 }
241
242 if (!xsd_data_validate_doc (xsd_data, modified->doc))
243 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_NEW_INVALID;
244
245 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_OK;
246 }
247
248 static GUPnPDIDLLiteFragmentResult
249 apply_temporary_removal (DocNode *modified,
250 xmlNodePtr current_node,
251 XSDData *xsd_data)
252 {
253 xmlNodePtr mod_cur_node = av_xml_util_find_node (modified->node,
254 current_node);
255
256 if (mod_cur_node == NULL)
257 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_UNKNOWN_ERROR;
258
259 xmlUnlinkNode (mod_cur_node);
260 xmlFreeNode (mod_cur_node);
261 if (!xsd_data_validate_doc (xsd_data, modified->doc))
262 /* not sure if this is correct */
263 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_REQUIRED_TAG;
264
265 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_OK;
266 }
267
268 typedef struct {
269 gboolean required;
270 GHashTable* required_dep_props; /* string set */
271 GHashTable* required_indep_props; /* string to indep prop */
272 } IndependentProperty;
273
274 static void
275 independent_property_free (IndependentProperty *indep)
276 {
277 if (indep != NULL) {
278 g_hash_table_unref (indep->required_dep_props);
279 g_hash_table_unref (indep->required_indep_props);
280 g_slice_free (IndependentProperty, indep);
281 }
282 }
283
284 static IndependentProperty *
285 independent_property_new (gboolean required)
286 {
287 IndependentProperty *indep = g_slice_new (IndependentProperty);
288
289 indep->required = required;
290 indep->required_dep_props = g_hash_table_new_full (g_str_hash,
291 g_str_equal,
292 g_free,
293 NULL);
294 indep->required_indep_props = g_hash_table_new_full
295 (g_str_hash,
296 g_str_equal,
297 g_free,
298 (GDestroyNotify) independent_property_free);
299
300 return indep;
301 }
302
303 static void
304 insert_indep_prop (GHashTable *props,
305 const gchar *name,
306 IndependentProperty *prop)
307 {
308 g_hash_table_insert (props, g_strdup (name), prop);
309 }
310
311 static void
312 insert_indep_prop_to_indep (IndependentProperty *prop,
313 const gchar *name,
314 IndependentProperty *req_prop)
315 {
316 insert_indep_prop (prop->required_indep_props, name, req_prop);
317 }
318
319 static void
320 add_dep_prop (IndependentProperty *indep,
321 const gchar *name)
322 {
323 g_hash_table_add (indep->required_dep_props, g_strdup (name));
324 }
325
326 static IndependentProperty *
327 create_prop_with_required_dep_props (gboolean required,
328 const gchar *dep_prop,
329 ...)
330 {
331 IndependentProperty *indep = independent_property_new (required);
332
333 if (dep_prop != NULL) {
334 va_list var_args;
335 const gchar *name = dep_prop;
336
337 va_start (var_args, dep_prop);
338 do {
339 add_dep_prop (indep, name);
340 name = va_arg (var_args, gchar *);
341 } while (name != NULL);
342 va_end (var_args);
343 }
344
345 return indep;
346 }
347
348 static IndependentProperty *
349 create_foreign_metadata_props (void)
350 {
351 IndependentProperty *fm = independent_property_new (FALSE);
352 IndependentProperty *other;
353
354 add_dep_prop (fm, "type");
355
356 other = independent_property_new (TRUE);
357 insert_indep_prop_to_indep (fm, "fmId", other);
358
359 other = independent_property_new (TRUE);
360 insert_indep_prop_to_indep (fm, "fmClass", other);
361
362 other = independent_property_new (TRUE);
363 insert_indep_prop_to_indep (fm, "fmProvider", other);
364
365 other = independent_property_new (TRUE);
366 add_dep_prop (other, "xmlFlag");
367 insert_indep_prop_to_indep (fm, "fmBody", other);
368
369 return fm;
370 }
371
372 static GHashTable *
373 get_required_properties (void)
374 {
375 static GHashTable *required_props = NULL;
376 static gsize required_props_loaded = 0;
377
378 if (g_once_init_enter (&required_props_loaded)) {
379 required_props = g_hash_table_new_full
380 (g_str_hash,
381 g_str_equal,
382 g_free,
383 (GDestroyNotify) independent_property_free);
384
385 insert_indep_prop (required_props,
386 "",
387 create_prop_with_required_dep_props
388 (FALSE,
389 "id",
390 "parentID",
391 "restricted",
392 NULL));
393
394 insert_indep_prop (required_props,
395 "title",
396 independent_property_new (TRUE));
397 insert_indep_prop (required_props,
398 "class",
399 independent_property_new (TRUE));
400
401 insert_indep_prop (required_props,
402 "res",
403 create_prop_with_required_dep_props
404 (FALSE,
405 "protocolInfo",
406 NULL));
407 insert_indep_prop (required_props,
408 "programID",
409 create_prop_with_required_dep_props
410 (FALSE,
411 "type",
412 NULL));
413 insert_indep_prop (required_props,
414 "seriesID",
415 create_prop_with_required_dep_props
416 (FALSE,
417 "type",
418 NULL));
419 insert_indep_prop (required_props,
420 "channelID",
421 create_prop_with_required_dep_props
422 (FALSE,
423 "type",
424 NULL));
425 insert_indep_prop (required_props,
426 "programCode",
427 create_prop_with_required_dep_props
428 (FALSE,
429 "type",
430 NULL));
431 insert_indep_prop (required_props,
432 "channelGroupName",
433 create_prop_with_required_dep_props
434 (FALSE,
435 "id",
436 NULL));
437 insert_indep_prop (required_props,
438 "price",
439 create_prop_with_required_dep_props
440 (FALSE,
441 "currency",
442 NULL));
443 insert_indep_prop (required_props,
444 "desc",
445 create_prop_with_required_dep_props
446 (FALSE,
447 "nameSpace",
448 NULL));
449 insert_indep_prop (required_props,
450 "deviceUDN",
451 create_prop_with_required_dep_props
452 (FALSE,
453 "serviceType",
454 "serviceId",
455 NULL));
456 insert_indep_prop (required_props,
457 "stateVariableCollection",
458 create_prop_with_required_dep_props
459 (FALSE,
460 "serviceName",
461 "rcsInstanceType",
462 NULL));
463 insert_indep_prop (required_props,
464 "foreignMetadata",
465 create_foreign_metadata_props ());
466 g_once_init_leave (&required_props_loaded, 1);
467 }
468
469 return required_props;
470 }
471
472 static gboolean
473 is_required (const xmlChar *changed_element,
474 const xmlChar *changed_attribute)
475 {
476 GHashTable *required_props = get_required_properties ();
477
478 if (changed_element != NULL) {
479 IndependentProperty *toplevel_prop = g_hash_table_lookup
480 (required_props,
481 "");
482 IndependentProperty *this_prop = g_hash_table_lookup
483 (required_props,
484 (gpointer) changed_element);
485
486 if (changed_attribute != NULL) {
487 if (g_hash_table_contains
488 (toplevel_prop->required_dep_props,
489 changed_attribute))
490 return TRUE;
491 if (g_hash_table_contains (this_prop->required_dep_props,
492 changed_attribute))
493 return TRUE;
494 }
495 if (g_hash_table_contains (toplevel_prop->required_indep_props,
496 changed_element))
497 return TRUE;
498 /* TODO: check if changed element is not a required
499 * property of its parent element. That needs some
500 * additions in IndepependentProperty.
501 */
502 }
503
504 return FALSE;
505 }
506
507 static GUPnPDIDLLiteFragmentResult
508 new_doc_is_valid_modification (DocNode *modified,
509 xmlDocPtr current_doc,
510 xmlDocPtr new_doc,
511 XSDData *xsd_data)
512 {
513 xmlNodePtr current_node = current_doc->children->children;
514 xmlNodePtr new_node = new_doc->children->children;
515 xmlNodePtr last_sibling = NULL;
516
517 while (current_node != NULL && new_node != NULL) {
518 GUPnPDIDLLiteFragmentResult result;
519 xmlNodePtr temp_current_node = current_node;
520 xmlNodePtr temp_new_node = new_node;
521
522 last_sibling = new_node;
523 /* We can't put this line into for instruction,
524 * because new_node could be unlinked from its
525 * document and put into another one in
526 * apply_temporary_modification. We have to get its
527 * sibling before that could happen.
528 */
529 new_node = new_node->next;
530 current_node = current_node->next;
531 if (av_xml_util_node_deep_equal (temp_current_node,
532 temp_new_node))
533 /* This is just a context, skip the checks. */
534 continue;
535 if (xmlStrcmp (temp_current_node->name, temp_new_node->name))
536 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_NEW_INVALID;
537 if (is_any_change_read_only (temp_current_node, temp_new_node))
538 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_READONLY_TAG;
539 result = apply_temporary_modification (modified,
540 temp_current_node,
541 temp_new_node,
542 xsd_data);
543 if (result != GUPNP_DIDL_LITE_FRAGMENT_RESULT_OK)
544 return result;
545 }
546 if (last_sibling == NULL) {
547 if (modified->node->children != NULL)
548 last_sibling = modified->node->last;
549 else
550 /* We expect that modified object has some
551 * required tags like <upnp:class> or
552 * <dc:title>.
553 */
554 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_UNKNOWN_ERROR;
555 }
556 /* If there are some more nodes in current fragment then it
557 * means they are going to be removed. Check against required
558 * or read-only tag removal.
559 */
560 while (current_node != NULL) {
561 GUPnPDIDLLiteFragmentResult result;
562 xmlNodePtr temp_node = current_node;
563
564 current_node = current_node->next;
565 /* TODO: should we check if there are some readonly
566 * attributes when we remove whole element?
567 */
568 if (is_read_only ((gchar *) temp_node->name, NULL))
569 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_READONLY_TAG;
570 /* We don't check for required attributes or
571 * subelements, because most of them are required only
572 * when the element exists. And we are removing this
573 * one.
574 */
575 if (is_required (temp_node->name, NULL))
576 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_REQUIRED_TAG;
577 result = apply_temporary_removal (modified,
578 temp_node,
579 xsd_data);
580
581 if (result != GUPNP_DIDL_LITE_FRAGMENT_RESULT_OK)
582 return result;
583 }
584 /* If there are some more nodes in new fragment then it means
585 * they are going to be added. Check against read-only tags
586 * addition and general sanity check.
587 */
588 while (new_node != NULL) {
589 GUPnPDIDLLiteFragmentResult result;
590 xmlNodePtr temp_node;
591
592 if (is_read_only ((gchar *) new_node->name, NULL))
593 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_READONLY_TAG;
594 /* TODO: We probably should check if newly added node
595 * has all required properties. Maybe XSD check could
596 * do that for us.
597 */
598 temp_node = new_node;
599 new_node = new_node->next;
600 result = apply_temporary_addition (modified,
601 last_sibling,
602 temp_node,
603 xsd_data);
604 if (result != GUPNP_DIDL_LITE_FRAGMENT_RESULT_OK)
605 return result;
606 }
607
608 return GUPNP_DIDL_LITE_FRAGMENT_RESULT_OK;
609 }
610
611 static gchar *
612 fix_fragment (const gchar *fragment)
613 {
614 return g_strdup_printf
615 ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
616 "<DIDLLiteFragment\n"
617 "xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n"
618 "xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\"\n"
619 "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\"\n"
620 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
621 ">%s</DIDLLiteFragment>\n",
622 fragment);
623 }
624
625 static gboolean
626 is_current_doc_part_of_original_doc (DocNode *original,
627 xmlDocPtr current_doc)
628 {
629 xmlNodePtr current_node = current_doc->children->children;
630 xmlNodePtr this_node;
631
632 /* No current node means that we want to add new elements to
633 the document. */
634 if (current_node == NULL)
635 return TRUE;
636
637 this_node = av_xml_util_find_node (original->node, current_node);
638
639 if (this_node == NULL)
640 return FALSE;
641
642 for (current_node = current_node->next, this_node = this_node->next;
643 current_node != NULL && this_node != NULL;
644 current_node = current_node->next, this_node = this_node->next)
645 if (!av_xml_util_node_deep_equal (current_node, this_node))
646 return FALSE;
647
648 return TRUE;
649 }
650
651 GUPnPDIDLLiteFragmentResult
652 fragment_util_check_fragments (DocNode *original,
653 DocNode *modified,
654 const gchar *current_fragment,
655 const gchar *new_fragment,
656 XSDData *xsd_data)
657 {
658 gchar *fixed_current_fragment = fix_fragment (current_fragment);
659 gchar *fixed_new_fragment = fix_fragment (new_fragment);
660 xmlDocPtr current_doc = xmlReadDoc (BAD_CAST (fixed_current_fragment),
661 NULL,
662 NULL,
663 XML_PARSE_NONET);
664 xmlDocPtr new_doc = xmlReadDoc (BAD_CAST (fixed_new_fragment),
665 NULL,
666 NULL,
667 XML_PARSE_NONET);
668 GUPnPDIDLLiteFragmentResult result;
669
670 if (current_doc == NULL || current_doc->children == NULL) {
671 result = GUPNP_DIDL_LITE_FRAGMENT_RESULT_CURRENT_BAD_XML;
672
673 goto out;
674 }
675
676 if (new_doc == NULL || new_doc->children == NULL) {
677 result = GUPNP_DIDL_LITE_FRAGMENT_RESULT_NEW_BAD_XML;
678
679 goto out;
680 }
681
682 /* Assuming current_doc->children is non-NULL. */
683 if (current_doc->children->children != NULL) {
684 /* If the child element is title or class,
685 * it must not be set to empty or removed.
686 */
687 if (g_strrstr ((const char *) current_doc->children->children->name,
688 "title") != NULL ||
689 g_strrstr ((const char *) current_doc->children->children->name,
690 "class") != NULL) {
691 /* If the new tag has no corresponding title or class element */
692 if (new_doc->children->children == NULL) {
693 result = GUPNP_DIDL_LITE_FRAGMENT_RESULT_REQUIRED_TAG;
694
695 goto out;
696 }
697 /* If the new tag has an empty value for title or class */
698 if (new_doc->children->children->children == NULL) {
699 result = GUPNP_DIDL_LITE_FRAGMENT_RESULT_REQUIRED_TAG;
700
701 goto out;
702 }
703 }
704 }
705
706 if (!is_current_doc_part_of_original_doc (original, current_doc)) {
707 result = GUPNP_DIDL_LITE_FRAGMENT_RESULT_CURRENT_INVALID;
708
709 goto out;
710 }
711
712 result = new_doc_is_valid_modification (modified,
713 current_doc,
714 new_doc,
715 xsd_data);
716
717 out:
718 if (new_doc != NULL)
719 xmlFreeDoc (new_doc);
720 if (current_doc != NULL)
721 xmlFreeDoc (current_doc);
722 g_free (fixed_new_fragment);
723 g_free (fixed_current_fragment);
724
725 return result;
726 }
727
728 static const gchar *
729 get_data_dir (void)
730 {
731 const gchar *datadir = g_getenv ("GUPNP_AV_DATADIR");
732
733 if (datadir == NULL)
734 /* that's a macro defined by -DDATADIR=foo */
735 datadir = DATADIR;
736
737 return datadir;
738 }
739
740 static gchar *
741 get_xsd_path (const gchar *path)
742 {
743 return g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s",
744 get_data_dir (),
745 path);
746 }
747
748 static xmlParserInputPtr
749 our_own_loader (const char *url,
750 const char *id,
751 xmlParserCtxtPtr context)
752 {
753 gchar *basename;
754 gchar *path;
755 xmlParserInputPtr input;
756
757 g_debug ("URL: %s, ID: %s.", url, id);
758
759 basename = g_path_get_basename (url);
760 path = get_xsd_path (basename);
761 g_debug ("BASENAME: %s, PATH: %s", basename, path);
762 input = xmlNewInputFromFile (context, path);
763
764 g_free (basename);
765 g_free (path);
766
767 return input;
768 }
769
770 XSDData *
771 fragment_util_get_didl_lite_xsd_data (void)
772 {
773 gchar *path = get_xsd_path ("didl-lite-v2.xsd");
774 xmlExternalEntityLoader original_loader = xmlGetExternalEntityLoader ();
775 XSDData *xsd_data;
776
777 xmlSetExternalEntityLoader (our_own_loader);
778 xsd_data = xsd_data_new (path);
779 xmlSetExternalEntityLoader (original_loader);
780
781 g_free (path);
782
783 return xsd_data;
784 }
785
786 gboolean
787 fragment_util_apply_modification (xmlNodePtr *node_ptr,
788 DocNode *modified)
789 {
790 xmlNodePtr node_copy;
791 xmlNodePtr old;
792
793 if (node_ptr == NULL || *node_ptr == NULL)
794 return FALSE;
795
796 node_copy = av_xml_util_copy_node (modified->node);
797
798 if (node_copy == NULL)
799 return FALSE;
800
801 old = xmlReplaceNode (*node_ptr, node_copy);
802
803 if (old == NULL)
804 return FALSE;
805
806 *node_ptr = node_copy;
807 xmlFreeNode (old);
808
809 return TRUE;
810 }