xps-resource.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "xps-imp.h"
  24. #include <string.h>
  25. static fz_xml *
  26. xps_lookup_resource(fz_context *ctx, xps_document *doc, xps_resource *dict, char *name, char **urip)
  27. {
  28. xps_resource *head, *node;
  29. for (head = dict; head; head = head->parent)
  30. {
  31. for (node = head; node; node = node->next)
  32. {
  33. if (!strcmp(node->name, name))
  34. {
  35. if (urip && head->base_uri)
  36. *urip = head->base_uri;
  37. return node->data;
  38. }
  39. }
  40. }
  41. return NULL;
  42. }
  43. static fz_xml *
  44. xps_parse_resource_reference(fz_context *ctx, xps_document *doc, xps_resource *dict, char *att, char **urip)
  45. {
  46. char name[1024];
  47. char *s;
  48. if (strstr(att, "{StaticResource ") != att)
  49. return NULL;
  50. fz_strlcpy(name, att + 16, sizeof name);
  51. s = strrchr(name, '}');
  52. if (s)
  53. *s = 0;
  54. return xps_lookup_resource(ctx, doc, dict, name, urip);
  55. }
  56. void
  57. xps_resolve_resource_reference(fz_context *ctx, xps_document *doc, xps_resource *dict,
  58. char **attp, fz_xml **tagp, char **urip)
  59. {
  60. if (*attp)
  61. {
  62. fz_xml *rsrc = xps_parse_resource_reference(ctx, doc, dict, *attp, urip);
  63. if (rsrc)
  64. {
  65. *attp = NULL;
  66. *tagp = rsrc;
  67. }
  68. }
  69. }
  70. static xps_resource *
  71. xps_parse_remote_resource_dictionary(fz_context *ctx, xps_document *doc, char *base_uri, char *source_att)
  72. {
  73. char part_name[1024];
  74. char part_uri[1024];
  75. xps_part *part;
  76. xps_resource *dict = NULL;
  77. fz_xml_doc *xml = NULL;
  78. char *s;
  79. fz_var(xml);
  80. /* External resource dictionaries MUST NOT reference other resource dictionaries */
  81. xps_resolve_url(ctx, doc, part_name, base_uri, source_att, sizeof part_name);
  82. part = xps_read_part(ctx, doc, part_name);
  83. fz_try(ctx)
  84. {
  85. xml = fz_parse_xml(ctx, part->data, 0);
  86. if (!fz_xml_is_tag(fz_xml_root(xml), "ResourceDictionary"))
  87. fz_throw(ctx, FZ_ERROR_FORMAT, "expected ResourceDictionary element");
  88. fz_strlcpy(part_uri, part_name, sizeof part_uri);
  89. s = strrchr(part_uri, '/');
  90. if (s)
  91. s[1] = 0;
  92. dict = xps_parse_resource_dictionary(ctx, doc, part_uri, fz_xml_root(xml));
  93. if (dict)
  94. {
  95. dict->base_xml = xml; /* pass on ownership */
  96. xml = NULL;
  97. }
  98. }
  99. fz_always(ctx)
  100. {
  101. xps_drop_part(ctx, doc, part);
  102. fz_drop_xml(ctx, xml);
  103. }
  104. fz_catch(ctx)
  105. {
  106. fz_rethrow(ctx);
  107. }
  108. return dict;
  109. }
  110. xps_resource *
  111. xps_parse_resource_dictionary(fz_context *ctx, xps_document *doc, char *base_uri, fz_xml *root)
  112. {
  113. xps_resource *head;
  114. xps_resource *entry;
  115. fz_xml *node;
  116. char *source;
  117. char *key;
  118. source = fz_xml_att(root, "Source");
  119. if (source)
  120. return xps_parse_remote_resource_dictionary(ctx, doc, base_uri, source);
  121. head = NULL;
  122. for (node = fz_xml_down(root); node; node = fz_xml_next(node))
  123. {
  124. key = fz_xml_att(node, "x:Key");
  125. if (key)
  126. {
  127. entry = fz_malloc_struct(ctx, xps_resource);
  128. entry->name = key;
  129. entry->base_uri = NULL;
  130. entry->base_xml = NULL;
  131. entry->data = node;
  132. entry->next = head;
  133. entry->parent = NULL;
  134. head = entry;
  135. }
  136. }
  137. if (head)
  138. {
  139. fz_try(ctx)
  140. head->base_uri = fz_strdup(ctx, base_uri);
  141. fz_catch(ctx)
  142. {
  143. fz_free(ctx, entry);
  144. fz_rethrow(ctx);
  145. }
  146. }
  147. return head;
  148. }
  149. void
  150. xps_drop_resource_dictionary(fz_context *ctx, xps_document *doc, xps_resource *dict)
  151. {
  152. xps_resource *next;
  153. while (dict)
  154. {
  155. next = dict->next;
  156. fz_drop_xml(ctx, dict->base_xml);
  157. fz_free(ctx, dict->base_uri);
  158. fz_free(ctx, dict);
  159. dict = next;
  160. }
  161. }