PageRenderTime 148ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/MagickCore/xml-tree.c

https://gitlab.com/ImageMagick/ImageMagick
C | 2865 lines | 2508 code | 46 blank | 311 comment | 364 complexity | 151dcbc69a8f9b6ae831a00e3291f68f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % X X M M L %
  7. % X X MM MM L %
  8. % X M M M L %
  9. % X X M M L %
  10. % X X M M LLLLL %
  11. % %
  12. % TTTTT RRRR EEEEE EEEEE %
  13. % T R R E E %
  14. % T RRRR EEE EEE %
  15. % T R R E E %
  16. % T R R EEEEE EEEEE %
  17. % %
  18. % %
  19. % XML Tree Methods %
  20. % %
  21. % Software Design %
  22. % Cristy %
  23. % December 2004 %
  24. % %
  25. % %
  26. % Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization %
  27. % dedicated to making software imaging solutions freely available. %
  28. % %
  29. % You may not use this file except in compliance with the License. You may %
  30. % obtain a copy of the License at %
  31. % %
  32. % https://imagemagick.org/script/license.php %
  33. % %
  34. % Unless required by applicable law or agreed to in writing, software %
  35. % distributed under the License is distributed on an "AS IS" BASIS, %
  36. % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
  37. % See the License for the specific language governing permissions and %
  38. % limitations under the License. %
  39. % %
  40. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  41. %
  42. % This module implements the standard handy xml-tree methods for storing and
  43. % retrieving nodes and attributes from an XML string.
  44. %
  45. */
  46. /*
  47. Include declarations.
  48. */
  49. #include "MagickCore/studio.h"
  50. #include "MagickCore/blob.h"
  51. #include "MagickCore/blob-private.h"
  52. #include "MagickCore/exception.h"
  53. #include "MagickCore/exception-private.h"
  54. #include "MagickCore/image-private.h"
  55. #include "MagickCore/log.h"
  56. #include "MagickCore/memory_.h"
  57. #include "MagickCore/memory-private.h"
  58. #include "MagickCore/semaphore.h"
  59. #include "MagickCore/string_.h"
  60. #include "MagickCore/string-private.h"
  61. #include "MagickCore/token-private.h"
  62. #include "MagickCore/xml-tree.h"
  63. #include "MagickCore/xml-tree-private.h"
  64. #include "MagickCore/utility.h"
  65. #include "MagickCore/utility-private.h"
  66. /*
  67. Define declarations.
  68. */
  69. #define NumberPredefinedEntities 10
  70. #define XMLWhitespace "\t\r\n "
  71. /*
  72. Typedef declarations.
  73. */
  74. struct _XMLTreeInfo
  75. {
  76. char
  77. *tag,
  78. **attributes,
  79. *content;
  80. size_t
  81. offset;
  82. XMLTreeInfo
  83. *parent,
  84. *next,
  85. *sibling,
  86. *ordered,
  87. *child;
  88. MagickBooleanType
  89. debug;
  90. SemaphoreInfo
  91. *semaphore;
  92. size_t
  93. signature;
  94. };
  95. typedef struct _XMLTreeRoot
  96. XMLTreeRoot;
  97. struct _XMLTreeRoot
  98. {
  99. struct _XMLTreeInfo
  100. root;
  101. XMLTreeInfo
  102. *node;
  103. MagickBooleanType
  104. standalone;
  105. char
  106. ***processing_instructions,
  107. **entities,
  108. ***attributes;
  109. MagickBooleanType
  110. debug;
  111. SemaphoreInfo
  112. *semaphore;
  113. size_t
  114. signature;
  115. };
  116. /*
  117. Global declarations.
  118. */
  119. static char
  120. *sentinel[] = { (char *) NULL };
  121. /*
  122. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  123. % %
  124. % %
  125. % %
  126. % A d d C h i l d T o X M L T r e e %
  127. % %
  128. % %
  129. % %
  130. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  131. %
  132. % AddChildToXMLTree() adds a child tag at an offset relative to the start of
  133. % the parent tag's character content. Return the child tag.
  134. %
  135. % The format of the AddChildToXMLTree method is:
  136. %
  137. % XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,const char *tag,
  138. % const size_t offset)
  139. %
  140. % A description of each parameter follows:
  141. %
  142. % o xml_info: the xml info.
  143. %
  144. % o tag: the tag.
  145. %
  146. % o offset: the tag offset.
  147. %
  148. */
  149. MagickExport XMLTreeInfo *AddChildToXMLTree(XMLTreeInfo *xml_info,
  150. const char *tag,const size_t offset)
  151. {
  152. XMLTreeInfo
  153. *child;
  154. if (xml_info == (XMLTreeInfo *) NULL)
  155. return((XMLTreeInfo *) NULL);
  156. child=(XMLTreeInfo *) AcquireMagickMemory(sizeof(*child));
  157. if (child == (XMLTreeInfo *) NULL)
  158. return((XMLTreeInfo *) NULL);
  159. (void) memset(child,0,sizeof(*child));
  160. child->tag=ConstantString(tag);
  161. child->attributes=sentinel;
  162. child->content=ConstantString("");
  163. child->debug=IsEventLogging();
  164. child->signature=MagickCoreSignature;
  165. return(InsertTagIntoXMLTree(xml_info,child,offset));
  166. }
  167. /*
  168. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  169. % %
  170. % %
  171. % %
  172. % A d d P a t h T o X M L T r e e %
  173. % %
  174. % %
  175. % %
  176. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  177. %
  178. % AddPathToXMLTree() adds a child tag at an offset relative to the start of
  179. % the parent tag's character content. This method returns the child tag.
  180. %
  181. % The format of the AddPathToXMLTree method is:
  182. %
  183. % XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,const char *path,
  184. % const size_t offset)
  185. %
  186. % A description of each parameter follows:
  187. %
  188. % o xml_info: the xml info.
  189. %
  190. % o path: the path.
  191. %
  192. % o offset: the tag offset.
  193. %
  194. */
  195. MagickPrivate XMLTreeInfo *AddPathToXMLTree(XMLTreeInfo *xml_info,
  196. const char *path,const size_t offset)
  197. {
  198. char
  199. **components,
  200. subnode[MagickPathExtent],
  201. tag[MagickPathExtent];
  202. register ssize_t
  203. i;
  204. size_t
  205. number_components;
  206. ssize_t
  207. j;
  208. XMLTreeInfo
  209. *child,
  210. *node;
  211. assert(xml_info != (XMLTreeInfo *) NULL);
  212. assert((xml_info->signature == MagickCoreSignature) ||
  213. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  214. if (xml_info->debug != MagickFalse)
  215. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  216. node=xml_info;
  217. components=GetPathComponents(path,&number_components);
  218. if (components == (char **) NULL)
  219. return((XMLTreeInfo *) NULL);
  220. for (i=0; i < (ssize_t) number_components; i++)
  221. {
  222. GetPathComponent(components[i],SubimagePath,subnode);
  223. GetPathComponent(components[i],CanonicalPath,tag);
  224. child=GetXMLTreeChild(node,tag);
  225. if (child == (XMLTreeInfo *) NULL)
  226. child=AddChildToXMLTree(node,tag,offset);
  227. node=child;
  228. if (node == (XMLTreeInfo *) NULL)
  229. break;
  230. for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
  231. {
  232. node=GetXMLTreeOrdered(node);
  233. if (node == (XMLTreeInfo *) NULL)
  234. break;
  235. }
  236. if (node == (XMLTreeInfo *) NULL)
  237. break;
  238. components[i]=DestroyString(components[i]);
  239. }
  240. for ( ; i < (ssize_t) number_components; i++)
  241. components[i]=DestroyString(components[i]);
  242. components=(char **) RelinquishMagickMemory(components);
  243. return(node);
  244. }
  245. /*
  246. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  247. % %
  248. % %
  249. % %
  250. % C a n o n i c a l X M L C o n t e n t %
  251. % %
  252. % %
  253. % %
  254. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  255. %
  256. % CanonicalXMLContent() converts text to canonical XML content by converting
  257. % to UTF-8, substituting predefined entities, wrapping as CDATA, or encoding
  258. % as base-64 as required.
  259. %
  260. % The format of the CanonicalXMLContent method is:
  261. %
  262. %
  263. % char *CanonicalXMLContent(const char *content,
  264. % const MagickBooleanType pedantic)
  265. %
  266. % A description of each parameter follows:
  267. %
  268. % o content: the content.
  269. %
  270. % o pedantic: if true, replace newlines and tabs with their respective
  271. % entities.
  272. %
  273. */
  274. MagickPrivate char *CanonicalXMLContent(const char *content,
  275. const MagickBooleanType pedantic)
  276. {
  277. char
  278. *base64,
  279. *canonical_content;
  280. register const unsigned char
  281. *p;
  282. register ssize_t
  283. i;
  284. size_t
  285. extent,
  286. length;
  287. unsigned char
  288. *utf8;
  289. utf8=ConvertLatin1ToUTF8((const unsigned char *) content);
  290. if (utf8 == (unsigned char *) NULL)
  291. return((char *) NULL);
  292. for (p=utf8; *p != '\0'; p++)
  293. if ((*p < 0x20) && (*p != 0x09) && (*p != 0x0a) && (*p != 0x0d))
  294. break;
  295. if (*p != '\0')
  296. {
  297. /*
  298. String is binary, base64-encode it.
  299. */
  300. base64=Base64Encode(utf8,strlen((char *) utf8),&length);
  301. utf8=(unsigned char *) RelinquishMagickMemory(utf8);
  302. if (base64 == (char *) NULL)
  303. return((char *) NULL);
  304. canonical_content=AcquireString("<base64>");
  305. (void) ConcatenateString(&canonical_content,base64);
  306. base64=DestroyString(base64);
  307. (void) ConcatenateString(&canonical_content,"</base64>");
  308. return(canonical_content);
  309. }
  310. /*
  311. Substitute predefined entities.
  312. */
  313. i=0;
  314. canonical_content=AcquireString((char *) NULL);
  315. extent=MagickPathExtent;
  316. for (p=utf8; *p != '\0'; p++)
  317. {
  318. if ((i+MagickPathExtent) > (ssize_t) extent)
  319. {
  320. extent+=MagickPathExtent;
  321. canonical_content=(char *) ResizeQuantumMemory(canonical_content,extent,
  322. sizeof(*canonical_content));
  323. if (canonical_content == (char *) NULL)
  324. return(canonical_content);
  325. }
  326. switch (*p)
  327. {
  328. case '&':
  329. {
  330. i+=FormatLocaleString(canonical_content+i,extent,"&amp;");
  331. break;
  332. }
  333. case '<':
  334. {
  335. i+=FormatLocaleString(canonical_content+i,extent,"&lt;");
  336. break;
  337. }
  338. case '>':
  339. {
  340. i+=FormatLocaleString(canonical_content+i,extent,"&gt;");
  341. break;
  342. }
  343. case '"':
  344. {
  345. i+=FormatLocaleString(canonical_content+i,extent,"&quot;");
  346. break;
  347. }
  348. case '\n':
  349. {
  350. if (pedantic == MagickFalse)
  351. {
  352. canonical_content[i++]=(char) (*p);
  353. break;
  354. }
  355. i+=FormatLocaleString(canonical_content+i,extent,"&#xA;");
  356. break;
  357. }
  358. case '\t':
  359. {
  360. if (pedantic == MagickFalse)
  361. {
  362. canonical_content[i++]=(char) (*p);
  363. break;
  364. }
  365. i+=FormatLocaleString(canonical_content+i,extent,"&#x9;");
  366. break;
  367. }
  368. case '\r':
  369. {
  370. i+=FormatLocaleString(canonical_content+i,extent,"&#xD;");
  371. break;
  372. }
  373. default:
  374. {
  375. canonical_content[i++]=(char) (*p);
  376. break;
  377. }
  378. }
  379. }
  380. canonical_content[i]='\0';
  381. utf8=(unsigned char *) RelinquishMagickMemory(utf8);
  382. return(canonical_content);
  383. }
  384. /*
  385. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  386. % %
  387. % %
  388. % %
  389. % D e s t r o y X M L T r e e %
  390. % %
  391. % %
  392. % %
  393. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  394. %
  395. % DestroyXMLTree() destroys the xml-tree.
  396. %
  397. % The format of the DestroyXMLTree method is:
  398. %
  399. % XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
  400. %
  401. % A description of each parameter follows:
  402. %
  403. % o xml_info: the xml info.
  404. %
  405. */
  406. static char **DestroyXMLTreeAttributes(char **attributes)
  407. {
  408. register ssize_t
  409. i;
  410. /*
  411. Destroy a tag attribute list.
  412. */
  413. if ((attributes == (char **) NULL) || (attributes == sentinel))
  414. return((char **) NULL);
  415. for (i=0; attributes[i] != (char *) NULL; i+=2)
  416. {
  417. /*
  418. Destroy attribute tag and value.
  419. */
  420. if (attributes[i] != (char *) NULL)
  421. attributes[i]=DestroyString(attributes[i]);
  422. if (attributes[i+1] != (char *) NULL)
  423. attributes[i+1]=DestroyString(attributes[i+1]);
  424. }
  425. attributes=(char **) RelinquishMagickMemory(attributes);
  426. return((char **) NULL);
  427. }
  428. static void DestroyXMLTreeChild(XMLTreeInfo *xml_info)
  429. {
  430. XMLTreeInfo
  431. *child,
  432. *node;
  433. child=xml_info->child;
  434. while(child != (XMLTreeInfo *) NULL)
  435. {
  436. node=child;
  437. child=node->child;
  438. node->child=(XMLTreeInfo *) NULL;
  439. (void) DestroyXMLTree(node);
  440. }
  441. }
  442. static void DestroyXMLTreeOrdered(XMLTreeInfo *xml_info)
  443. {
  444. XMLTreeInfo
  445. *node,
  446. *ordered;
  447. ordered=xml_info->ordered;
  448. while(ordered != (XMLTreeInfo *) NULL)
  449. {
  450. node=ordered;
  451. ordered=node->ordered;
  452. node->ordered=(XMLTreeInfo *) NULL;
  453. (void) DestroyXMLTree(node);
  454. }
  455. }
  456. static void DestroyXMLTreeRoot(XMLTreeInfo *xml_info)
  457. {
  458. char
  459. **attributes;
  460. register ssize_t
  461. i;
  462. ssize_t
  463. j;
  464. XMLTreeRoot
  465. *root;
  466. assert(xml_info != (XMLTreeInfo *) NULL);
  467. assert((xml_info->signature == MagickCoreSignature) ||
  468. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  469. if (xml_info->debug != MagickFalse)
  470. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  471. if (xml_info->parent != (XMLTreeInfo *) NULL)
  472. return;
  473. /*
  474. Free root tag allocations.
  475. */
  476. root=(XMLTreeRoot *) xml_info;
  477. for (i=NumberPredefinedEntities; root->entities[i] != (char *) NULL; i+=2)
  478. root->entities[i+1]=DestroyString(root->entities[i+1]);
  479. root->entities=(char **) RelinquishMagickMemory(root->entities);
  480. for (i=0; root->attributes[i] != (char **) NULL; i++)
  481. {
  482. attributes=root->attributes[i];
  483. if (attributes[0] != (char *) NULL)
  484. attributes[0]=DestroyString(attributes[0]);
  485. for (j=1; attributes[j] != (char *) NULL; j+=3)
  486. {
  487. if (attributes[j] != (char *) NULL)
  488. attributes[j]=DestroyString(attributes[j]);
  489. if (attributes[j+1] != (char *) NULL)
  490. attributes[j+1]=DestroyString(attributes[j+1]);
  491. if (attributes[j+2] != (char *) NULL)
  492. attributes[j+2]=DestroyString(attributes[j+2]);
  493. }
  494. attributes=(char **) RelinquishMagickMemory(attributes);
  495. }
  496. if (root->attributes[0] != (char **) NULL)
  497. root->attributes=(char ***) RelinquishMagickMemory(root->attributes);
  498. if (root->processing_instructions[0] != (char **) NULL)
  499. {
  500. for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
  501. {
  502. for (j=0; root->processing_instructions[i][j] != (char *) NULL; j++)
  503. root->processing_instructions[i][j]=DestroyString(
  504. root->processing_instructions[i][j]);
  505. root->processing_instructions[i][j+1]=DestroyString(
  506. root->processing_instructions[i][j+1]);
  507. root->processing_instructions[i]=(char **) RelinquishMagickMemory(
  508. root->processing_instructions[i]);
  509. }
  510. root->processing_instructions=(char ***) RelinquishMagickMemory(
  511. root->processing_instructions);
  512. }
  513. }
  514. MagickExport XMLTreeInfo *DestroyXMLTree(XMLTreeInfo *xml_info)
  515. {
  516. assert(xml_info != (XMLTreeInfo *) NULL);
  517. assert((xml_info->signature == MagickCoreSignature) ||
  518. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  519. if (xml_info->debug != MagickFalse)
  520. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  521. DestroyXMLTreeChild(xml_info);
  522. DestroyXMLTreeOrdered(xml_info);
  523. DestroyXMLTreeRoot(xml_info);
  524. xml_info->attributes=DestroyXMLTreeAttributes(xml_info->attributes);
  525. xml_info->content=DestroyString(xml_info->content);
  526. xml_info->tag=DestroyString(xml_info->tag);
  527. xml_info=(XMLTreeInfo *) RelinquishMagickMemory(xml_info);
  528. return((XMLTreeInfo *) NULL);
  529. }
  530. /*
  531. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  532. % %
  533. % %
  534. % %
  535. % F i l e T o X M L %
  536. % %
  537. % %
  538. % %
  539. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  540. %
  541. % FileToXML() returns the contents of a file as a XML string.
  542. %
  543. % The format of the FileToXML method is:
  544. %
  545. % char *FileToXML(const char *filename,const size_t extent)
  546. %
  547. % A description of each parameter follows:
  548. %
  549. % o filename: the filename.
  550. %
  551. % o extent: Maximum length of the string.
  552. %
  553. */
  554. MagickPrivate char *FileToXML(const char *filename,const size_t extent)
  555. {
  556. char
  557. *xml;
  558. int
  559. file;
  560. MagickOffsetType
  561. offset;
  562. register size_t
  563. i;
  564. size_t
  565. length;
  566. ssize_t
  567. count;
  568. void
  569. *map;
  570. assert(filename != (const char *) NULL);
  571. length=0;
  572. file=fileno(stdin);
  573. if (LocaleCompare(filename,"-") != 0)
  574. file=open_utf8(filename,O_RDONLY | O_BINARY,0);
  575. if (file == -1)
  576. return((char *) NULL);
  577. offset=(MagickOffsetType) lseek(file,0,SEEK_END);
  578. count=0;
  579. if ((file == fileno(stdin)) || (offset < 0) ||
  580. (offset != (MagickOffsetType) ((ssize_t) offset)))
  581. {
  582. size_t
  583. quantum;
  584. struct stat
  585. file_stats;
  586. /*
  587. Stream is not seekable.
  588. */
  589. offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
  590. quantum=(size_t) MagickMaxBufferExtent;
  591. if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
  592. quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
  593. xml=(char *) AcquireQuantumMemory(quantum,sizeof(*xml));
  594. for (i=0; xml != (char *) NULL; i+=count)
  595. {
  596. count=read(file,xml+i,quantum);
  597. if (count <= 0)
  598. {
  599. count=0;
  600. if (errno != EINTR)
  601. break;
  602. }
  603. if (~((size_t) i) < (quantum+1))
  604. {
  605. xml=(char *) RelinquishMagickMemory(xml);
  606. break;
  607. }
  608. xml=(char *) ResizeQuantumMemory(xml,i+quantum+1,sizeof(*xml));
  609. if ((size_t) (i+count) >= extent)
  610. break;
  611. }
  612. if (LocaleCompare(filename,"-") != 0)
  613. file=close(file);
  614. if (xml == (char *) NULL)
  615. return((char *) NULL);
  616. if (file == -1)
  617. {
  618. xml=(char *) RelinquishMagickMemory(xml);
  619. return((char *) NULL);
  620. }
  621. length=(size_t) MagickMin(i+count,extent);
  622. xml[length]='\0';
  623. return(xml);
  624. }
  625. length=(size_t) MagickMin(offset,(MagickOffsetType) extent);
  626. xml=(char *) NULL;
  627. if (~length >= (MagickPathExtent-1))
  628. xml=(char *) AcquireQuantumMemory(length+MagickPathExtent,sizeof(*xml));
  629. if (xml == (char *) NULL)
  630. {
  631. file=close(file);
  632. return((char *) NULL);
  633. }
  634. map=MapBlob(file,ReadMode,0,length);
  635. if (map != (char *) NULL)
  636. {
  637. (void) memcpy(xml,map,length);
  638. (void) UnmapBlob(map,length);
  639. }
  640. else
  641. {
  642. (void) lseek(file,0,SEEK_SET);
  643. for (i=0; i < length; i+=count)
  644. {
  645. count=read(file,xml+i,(size_t) MagickMin(length-i,(ssize_t) SSIZE_MAX));
  646. if (count <= 0)
  647. {
  648. count=0;
  649. if (errno != EINTR)
  650. break;
  651. }
  652. }
  653. if (i < length)
  654. {
  655. file=close(file)-1;
  656. xml=(char *) RelinquishMagickMemory(xml);
  657. return((char *) NULL);
  658. }
  659. }
  660. xml[length]='\0';
  661. if (LocaleCompare(filename,"-") != 0)
  662. file=close(file);
  663. if (file == -1)
  664. xml=(char *) RelinquishMagickMemory(xml);
  665. return(xml);
  666. }
  667. /*
  668. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  669. % %
  670. % %
  671. % %
  672. % G e t N e x t X M L T r e e T a g %
  673. % %
  674. % %
  675. % %
  676. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  677. %
  678. % GetNextXMLTreeTag() returns the next tag or NULL if not found.
  679. %
  680. % The format of the GetNextXMLTreeTag method is:
  681. %
  682. % XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
  683. %
  684. % A description of each parameter follows:
  685. %
  686. % o xml_info: the xml info.
  687. %
  688. */
  689. MagickExport XMLTreeInfo *GetNextXMLTreeTag(XMLTreeInfo *xml_info)
  690. {
  691. assert(xml_info != (XMLTreeInfo *) NULL);
  692. assert((xml_info->signature == MagickCoreSignature) ||
  693. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  694. if (xml_info->debug != MagickFalse)
  695. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  696. return(xml_info->next);
  697. }
  698. /*
  699. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  700. % %
  701. % %
  702. % %
  703. % G e t X M L T r e e A t t r i b u t e %
  704. % %
  705. % %
  706. % %
  707. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  708. %
  709. % GetXMLTreeAttribute() returns the value of the attribute tag with the
  710. % specified tag if found, otherwise NULL.
  711. %
  712. % The format of the GetXMLTreeAttribute method is:
  713. %
  714. % const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag)
  715. %
  716. % A description of each parameter follows:
  717. %
  718. % o xml_info: the xml info.
  719. %
  720. % o tag: the attribute tag.
  721. %
  722. */
  723. MagickExport const char *GetXMLTreeAttribute(XMLTreeInfo *xml_info,
  724. const char *tag)
  725. {
  726. register ssize_t
  727. i;
  728. ssize_t
  729. j;
  730. XMLTreeRoot
  731. *root;
  732. assert(xml_info != (XMLTreeInfo *) NULL);
  733. assert((xml_info->signature == MagickCoreSignature) ||
  734. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  735. if (xml_info->debug != MagickFalse)
  736. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  737. if (xml_info->attributes == (char **) NULL)
  738. return((const char *) NULL);
  739. i=0;
  740. while ((xml_info->attributes[i] != (char *) NULL) &&
  741. (strcmp(xml_info->attributes[i],tag) != 0))
  742. i+=2;
  743. if (xml_info->attributes[i] != (char *) NULL)
  744. return(xml_info->attributes[i+1]);
  745. root=(XMLTreeRoot*) xml_info;
  746. while (root->root.parent != (XMLTreeInfo *) NULL)
  747. root=(XMLTreeRoot *) root->root.parent;
  748. i=0;
  749. while ((root->attributes[i] != (char **) NULL) &&
  750. (strcmp(root->attributes[i][0],xml_info->tag) != 0))
  751. i++;
  752. if (root->attributes[i] == (char **) NULL)
  753. return((const char *) NULL);
  754. j=1;
  755. while ((root->attributes[i][j] != (char *) NULL) &&
  756. (strcmp(root->attributes[i][j],tag) != 0))
  757. j+=3;
  758. if (root->attributes[i][j] == (char *) NULL)
  759. return((const char *) NULL);
  760. return(root->attributes[i][j+1]);
  761. }
  762. /*
  763. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  764. % %
  765. % %
  766. % %
  767. % G e t X M L T r e e A t t r i b u t e s %
  768. % %
  769. % %
  770. % %
  771. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  772. %
  773. % GetXMLTreeAttributes() injects all attributes associated with the current
  774. % tag in the specified splay-tree.
  775. %
  776. % The format of the GetXMLTreeAttributes method is:
  777. %
  778. % MagickBooleanType GetXMLTreeAttributes(const XMLTreeInfo *xml_info,
  779. % SplayTreeInfo *attributes)
  780. %
  781. % A description of each parameter follows:
  782. %
  783. % o xml_info: the xml info.
  784. %
  785. % o attributes: the attribute splay-tree.
  786. %
  787. */
  788. MagickPrivate MagickBooleanType GetXMLTreeAttributes(
  789. const XMLTreeInfo *xml_info,SplayTreeInfo *attributes)
  790. {
  791. register ssize_t
  792. i;
  793. assert(xml_info != (XMLTreeInfo *) NULL);
  794. assert((xml_info->signature == MagickCoreSignature) ||
  795. (((const XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  796. if (xml_info->debug != MagickFalse)
  797. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  798. assert(attributes != (SplayTreeInfo *) NULL);
  799. if (xml_info->attributes == (char **) NULL)
  800. return(MagickTrue);
  801. i=0;
  802. while (xml_info->attributes[i] != (char *) NULL)
  803. {
  804. (void) AddValueToSplayTree(attributes,
  805. ConstantString(xml_info->attributes[i]),
  806. ConstantString(xml_info->attributes[i+1]));
  807. i+=2;
  808. }
  809. return(MagickTrue);
  810. }
  811. /*
  812. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  813. % %
  814. % %
  815. % %
  816. % G e t X M L T r e e C h i l d %
  817. % %
  818. % %
  819. % %
  820. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  821. %
  822. % GetXMLTreeChild() returns the first child tag with the specified tag if
  823. % found, otherwise NULL.
  824. %
  825. % The format of the GetXMLTreeChild method is:
  826. %
  827. % XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
  828. %
  829. % A description of each parameter follows:
  830. %
  831. % o xml_info: the xml info.
  832. %
  833. */
  834. MagickExport XMLTreeInfo *GetXMLTreeChild(XMLTreeInfo *xml_info,const char *tag)
  835. {
  836. XMLTreeInfo
  837. *child;
  838. assert(xml_info != (XMLTreeInfo *) NULL);
  839. assert((xml_info->signature == MagickCoreSignature) ||
  840. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  841. if (xml_info->debug != MagickFalse)
  842. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  843. child=xml_info->child;
  844. if (tag != (const char *) NULL)
  845. while ((child != (XMLTreeInfo *) NULL) && (strcmp(child->tag,tag) != 0))
  846. child=child->sibling;
  847. return(child);
  848. }
  849. /*
  850. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  851. % %
  852. % %
  853. % %
  854. % G e t X M L T r e e C o n t e n t %
  855. % %
  856. % %
  857. % %
  858. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  859. %
  860. % GetXMLTreeContent() returns any content associated with specified
  861. % xml-tree node.
  862. %
  863. % The format of the GetXMLTreeContent method is:
  864. %
  865. % const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
  866. %
  867. % A description of each parameter follows:
  868. %
  869. % o xml_info: the xml info.
  870. %
  871. */
  872. MagickExport const char *GetXMLTreeContent(XMLTreeInfo *xml_info)
  873. {
  874. assert(xml_info != (XMLTreeInfo *) NULL);
  875. assert((xml_info->signature == MagickCoreSignature) ||
  876. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  877. if (xml_info->debug != MagickFalse)
  878. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  879. return(xml_info->content);
  880. }
  881. /*
  882. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  883. % %
  884. % %
  885. % %
  886. % G e t X M L T r e e O r d e r e d %
  887. % %
  888. % %
  889. % %
  890. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  891. %
  892. % GetXMLTreeOrdered() returns the next ordered node if found, otherwise NULL.
  893. %
  894. % The format of the GetXMLTreeOrdered method is:
  895. %
  896. % XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
  897. %
  898. % A description of each parameter follows:
  899. %
  900. % o xml_info: the xml info.
  901. %
  902. */
  903. MagickPrivate XMLTreeInfo *GetXMLTreeOrdered(XMLTreeInfo *xml_info)
  904. {
  905. assert(xml_info != (XMLTreeInfo *) NULL);
  906. assert((xml_info->signature == MagickCoreSignature) ||
  907. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  908. if (xml_info->debug != MagickFalse)
  909. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  910. return(xml_info->ordered);
  911. }
  912. /*
  913. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  914. % %
  915. % %
  916. % %
  917. % G e t X M L T r e e P a t h %
  918. % %
  919. % %
  920. % %
  921. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  922. %
  923. % GetXMLTreePath() traverses the XML-tree as defined by the specified path
  924. % and returns the node if found, otherwise NULL.
  925. %
  926. % The format of the GetXMLTreePath method is:
  927. %
  928. % XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,const char *path)
  929. %
  930. % A description of each parameter follows:
  931. %
  932. % o xml_info: the xml info.
  933. %
  934. % o path: the path (e.g. property/elapsed-time).
  935. %
  936. */
  937. MagickPrivate XMLTreeInfo *GetXMLTreePath(XMLTreeInfo *xml_info,
  938. const char *path)
  939. {
  940. char
  941. **components,
  942. subnode[MagickPathExtent],
  943. tag[MagickPathExtent];
  944. register ssize_t
  945. i;
  946. size_t
  947. number_components;
  948. ssize_t
  949. j;
  950. XMLTreeInfo
  951. *node;
  952. assert(xml_info != (XMLTreeInfo *) NULL);
  953. assert((xml_info->signature == MagickCoreSignature) ||
  954. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  955. if (xml_info->debug != MagickFalse)
  956. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  957. node=xml_info;
  958. components=GetPathComponents(path,&number_components);
  959. if (components == (char **) NULL)
  960. return((XMLTreeInfo *) NULL);
  961. for (i=0; i < (ssize_t) number_components; i++)
  962. {
  963. GetPathComponent(components[i],SubimagePath,subnode);
  964. GetPathComponent(components[i],CanonicalPath,tag);
  965. node=GetXMLTreeChild(node,tag);
  966. if (node == (XMLTreeInfo *) NULL)
  967. break;
  968. for (j=(ssize_t) StringToLong(subnode)-1; j > 0; j--)
  969. {
  970. node=GetXMLTreeOrdered(node);
  971. if (node == (XMLTreeInfo *) NULL)
  972. break;
  973. }
  974. if (node == (XMLTreeInfo *) NULL)
  975. break;
  976. components[i]=DestroyString(components[i]);
  977. }
  978. for ( ; i < (ssize_t) number_components; i++)
  979. components[i]=DestroyString(components[i]);
  980. components=(char **) RelinquishMagickMemory(components);
  981. return(node);
  982. }
  983. /*
  984. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  985. % %
  986. % %
  987. % %
  988. % G e t X M L T r e e P r o c e s s i n g I n s t r u c t i o n s %
  989. % %
  990. % %
  991. % %
  992. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  993. %
  994. % GetXMLTreeProcessingInstructions() returns a null terminated array of
  995. % processing instructions for the given target.
  996. %
  997. % The format of the GetXMLTreeProcessingInstructions method is:
  998. %
  999. % const char **GetXMLTreeProcessingInstructions(XMLTreeInfo *xml_info,
  1000. % const char *target)
  1001. %
  1002. % A description of each parameter follows:
  1003. %
  1004. % o xml_info: the xml info.
  1005. %
  1006. */
  1007. MagickPrivate const char **GetXMLTreeProcessingInstructions(
  1008. XMLTreeInfo *xml_info,const char *target)
  1009. {
  1010. register ssize_t
  1011. i;
  1012. XMLTreeRoot
  1013. *root;
  1014. assert(xml_info != (XMLTreeInfo *) NULL);
  1015. assert((xml_info->signature == MagickCoreSignature) ||
  1016. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  1017. if (xml_info->debug != MagickFalse)
  1018. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  1019. root=(XMLTreeRoot *) xml_info;
  1020. while (root->root.parent != (XMLTreeInfo *) NULL)
  1021. root=(XMLTreeRoot *) root->root.parent;
  1022. i=0;
  1023. while ((root->processing_instructions[i] != (char **) NULL) &&
  1024. (strcmp(root->processing_instructions[i][0],target) != 0))
  1025. i++;
  1026. if (root->processing_instructions[i] == (char **) NULL)
  1027. return((const char **) sentinel);
  1028. return((const char **) (root->processing_instructions[i]+1));
  1029. }
  1030. /*
  1031. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1032. % %
  1033. % %
  1034. % %
  1035. % G e t X M L T r e e S i b l i n g %
  1036. % %
  1037. % %
  1038. % %
  1039. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1040. %
  1041. % GetXMLTreeSibling() returns the node sibling if found, otherwise NULL.
  1042. %
  1043. % The format of the GetXMLTreeSibling method is:
  1044. %
  1045. % XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
  1046. %
  1047. % A description of each parameter follows:
  1048. %
  1049. % o xml_info: the xml info.
  1050. %
  1051. */
  1052. MagickExport XMLTreeInfo *GetXMLTreeSibling(XMLTreeInfo *xml_info)
  1053. {
  1054. assert(xml_info != (XMLTreeInfo *) NULL);
  1055. assert((xml_info->signature == MagickCoreSignature) ||
  1056. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  1057. if (xml_info->debug != MagickFalse)
  1058. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  1059. return(xml_info->sibling);
  1060. }
  1061. /*
  1062. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1063. % %
  1064. % %
  1065. % %
  1066. % G e t X M L T r e e T a g %
  1067. % %
  1068. % %
  1069. % %
  1070. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1071. %
  1072. % GetXMLTreeTag() returns the tag associated with specified xml-tree node.
  1073. %
  1074. % The format of the GetXMLTreeTag method is:
  1075. %
  1076. % const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
  1077. %
  1078. % A description of each parameter follows:
  1079. %
  1080. % o xml_info: the xml info.
  1081. %
  1082. */
  1083. MagickExport const char *GetXMLTreeTag(XMLTreeInfo *xml_info)
  1084. {
  1085. assert(xml_info != (XMLTreeInfo *) NULL);
  1086. assert((xml_info->signature == MagickCoreSignature) ||
  1087. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  1088. if (xml_info->debug != MagickFalse)
  1089. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  1090. return(xml_info->tag);
  1091. }
  1092. /*
  1093. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1094. % %
  1095. % %
  1096. % %
  1097. % I n s e r t I n t o T a g X M L T r e e %
  1098. % %
  1099. % %
  1100. % %
  1101. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1102. %
  1103. % InsertTagIntoXMLTree() inserts a tag at an offset relative to the start of
  1104. % the parent tag's character content. This method returns the child tag.
  1105. %
  1106. % The format of the InsertTagIntoXMLTree method is:
  1107. %
  1108. % XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
  1109. % XMLTreeInfo *child,const size_t offset)
  1110. %
  1111. % A description of each parameter follows:
  1112. %
  1113. % o xml_info: the xml info.
  1114. %
  1115. % o child: the child tag.
  1116. %
  1117. % o offset: the tag offset.
  1118. %
  1119. */
  1120. MagickPrivate XMLTreeInfo *InsertTagIntoXMLTree(XMLTreeInfo *xml_info,
  1121. XMLTreeInfo *child,const size_t offset)
  1122. {
  1123. XMLTreeInfo
  1124. *head,
  1125. *node,
  1126. *previous;
  1127. child->ordered=(XMLTreeInfo *) NULL;
  1128. child->sibling=(XMLTreeInfo *) NULL;
  1129. child->next=(XMLTreeInfo *) NULL;
  1130. child->offset=offset;
  1131. child->parent=xml_info;
  1132. if (xml_info->child == (XMLTreeInfo *) NULL)
  1133. {
  1134. xml_info->child=child;
  1135. return(child);
  1136. }
  1137. head=xml_info->child;
  1138. if (head->offset > offset)
  1139. {
  1140. child->ordered=head;
  1141. xml_info->child=child;
  1142. }
  1143. else
  1144. {
  1145. node=head;
  1146. while ((node->ordered != (XMLTreeInfo *) NULL) &&
  1147. (node->ordered->offset <= offset))
  1148. node=node->ordered;
  1149. child->ordered=node->ordered;
  1150. node->ordered=child;
  1151. }
  1152. previous=(XMLTreeInfo *) NULL;
  1153. node=head;
  1154. while ((node != (XMLTreeInfo *) NULL) && (strcmp(node->tag,child->tag) != 0))
  1155. {
  1156. previous=node;
  1157. node=node->sibling;
  1158. }
  1159. if ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
  1160. {
  1161. while ((node->next != (XMLTreeInfo *) NULL) &&
  1162. (node->next->offset <= offset))
  1163. node=node->next;
  1164. child->next=node->next;
  1165. node->next=child;
  1166. }
  1167. else
  1168. {
  1169. if ((previous != (XMLTreeInfo *) NULL) && (node != (XMLTreeInfo *) NULL))
  1170. previous->sibling=node->sibling;
  1171. child->next=node;
  1172. previous=(XMLTreeInfo *) NULL;
  1173. node=head;
  1174. while ((node != (XMLTreeInfo *) NULL) && (node->offset <= offset))
  1175. {
  1176. previous=node;
  1177. node=node->sibling;
  1178. }
  1179. child->sibling=node;
  1180. if (previous != (XMLTreeInfo *) NULL)
  1181. previous->sibling=child;
  1182. }
  1183. return(child);
  1184. }
  1185. /*
  1186. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1187. % %
  1188. % %
  1189. % %
  1190. % N e w X M L T r e e %
  1191. % %
  1192. % %
  1193. % %
  1194. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1195. %
  1196. % NewXMLTree() returns a XMLTreeInfo xml-tree as defined by the specified
  1197. % XML string.
  1198. %
  1199. % The format of the NewXMLTree method is:
  1200. %
  1201. % XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
  1202. %
  1203. % A description of each parameter follows:
  1204. %
  1205. % o xml: A null-terminated XML string.
  1206. %
  1207. % o exception: return any errors or warnings in this structure.
  1208. %
  1209. */
  1210. static char *ConvertUTF16ToUTF8(const char *content,size_t *length)
  1211. {
  1212. char
  1213. *utf8;
  1214. int
  1215. bits,
  1216. byte,
  1217. c,
  1218. encoding;
  1219. register ssize_t
  1220. i;
  1221. size_t
  1222. extent;
  1223. ssize_t
  1224. j;
  1225. utf8=(char *) AcquireQuantumMemory(*length+1,sizeof(*utf8));
  1226. if (utf8 == (char *) NULL)
  1227. return((char *) NULL);
  1228. encoding=(*content == '\xFE') ? 1 : (*content == '\xFF') ? 0 : -1;
  1229. if (encoding == -1)
  1230. {
  1231. /*
  1232. Already UTF-8.
  1233. */
  1234. (void) memcpy(utf8,content,*length*sizeof(*utf8));
  1235. utf8[*length]='\0';
  1236. return(utf8);
  1237. }
  1238. j=0;
  1239. extent=(*length);
  1240. for (i=2; i < (ssize_t) (*length-1); i+=2)
  1241. {
  1242. c=(encoding != 0) ? ((content[i] & 0xff) << 8) | (content[i+1] & 0xff) :
  1243. ((content[i+1] & 0xff) << 8) | (content[i] & 0xff);
  1244. if ((c >= 0xd800) && (c <= 0xdfff) && ((i+=2) < (ssize_t) (*length-1)))
  1245. {
  1246. byte=(encoding != 0) ? ((content[i] & 0xff) << 8) |
  1247. (content[i+1] & 0xff) : ((content[i+1] & 0xff) << 8) |
  1248. (content[i] & 0xff);
  1249. c=(((c & 0x3ff) << 10) | (byte & 0x3ff))+0x10000;
  1250. }
  1251. if ((size_t) (j+MagickPathExtent) > extent)
  1252. {
  1253. extent=(size_t) j+MagickPathExtent;
  1254. utf8=(char *) ResizeQuantumMemory(utf8,extent,sizeof(*utf8));
  1255. if (utf8 == (char *) NULL)
  1256. return(utf8);
  1257. }
  1258. if (c < 0x80)
  1259. {
  1260. utf8[j]=c;
  1261. j++;
  1262. continue;
  1263. }
  1264. /*
  1265. Multi-byte UTF-8 sequence.
  1266. */
  1267. byte=c;
  1268. for (bits=0; byte != 0; byte/=2)
  1269. bits++;
  1270. bits=(bits-2)/5;
  1271. utf8[j++]=(0xFF << (7-bits)) | (c >> (6*bits));
  1272. while (bits != 0)
  1273. {
  1274. bits--;
  1275. utf8[j]=0x80 | ((c >> (6*bits)) & 0x3f);
  1276. j++;
  1277. }
  1278. }
  1279. *length=(size_t) j;
  1280. utf8=(char *) ResizeQuantumMemory(utf8,*length,sizeof(*utf8));
  1281. if (utf8 != (char *) NULL)
  1282. utf8[*length]='\0';
  1283. return(utf8);
  1284. }
  1285. static char *ParseEntities(char *xml,char **entities,int state)
  1286. {
  1287. char
  1288. *entity;
  1289. int
  1290. byte,
  1291. c;
  1292. register char
  1293. *p,
  1294. *q;
  1295. register ssize_t
  1296. i;
  1297. size_t
  1298. extent,
  1299. length;
  1300. ssize_t
  1301. offset;
  1302. /*
  1303. Normalize line endings.
  1304. */
  1305. p=xml;
  1306. q=xml;
  1307. for ( ; *xml != '\0'; xml++)
  1308. while (*xml == '\r')
  1309. {
  1310. *(xml++)='\n';
  1311. if (*xml == '\n')
  1312. (void) memmove(xml,xml+1,strlen(xml));
  1313. }
  1314. for (xml=p; ; )
  1315. {
  1316. while ((*xml != '\0') && (*xml != '&') && ((*xml != '%') ||
  1317. (state != '%')) && (isspace((int) ((unsigned char) *xml) == 0)))
  1318. xml++;
  1319. if (*xml == '\0')
  1320. break;
  1321. /*
  1322. States include:
  1323. '&' for general entity decoding
  1324. '%' for parameter entity decoding
  1325. 'c' for CDATA sections
  1326. ' ' for attributes normalization
  1327. '*' for non-CDATA attributes normalization
  1328. */
  1329. if ((state != 'c') && (strncmp(xml,"&#",2) == 0))
  1330. {
  1331. /*
  1332. Character reference.
  1333. */
  1334. if (xml[2] != 'x')
  1335. c=strtol(xml+2,&entity,10); /* base 10 */
  1336. else
  1337. c=strtol(xml+3,&entity,16); /* base 16 */
  1338. if ((c == 0) || (*entity != ';'))
  1339. {
  1340. /*
  1341. Not a character reference.
  1342. */
  1343. xml++;
  1344. continue;
  1345. }
  1346. if (c < 0x80)
  1347. *(xml++)=c;
  1348. else
  1349. {
  1350. /*
  1351. Multi-byte UTF-8 sequence.
  1352. */
  1353. byte=c;
  1354. for (i=0; byte != 0; byte/=2)
  1355. i++;
  1356. i=(i-2)/5;
  1357. *xml=(char) ((0xFF << (7-i)) | (c >> (6*i)));
  1358. xml++;
  1359. while (i != 0)
  1360. {
  1361. i--;
  1362. *xml=(char) (0x80 | ((c >> (6*i)) & 0x3F));
  1363. xml++;
  1364. }
  1365. }
  1366. (void) memmove(xml,strchr(xml,';')+1,strlen(strchr(xml,';')));
  1367. }
  1368. else
  1369. if (((*xml == '&') && ((state == '&') || (state == ' ') ||
  1370. (state == '*'))) || ((state == '%') && (*xml == '%')))
  1371. {
  1372. /*
  1373. Find entity in the list.
  1374. */
  1375. i=0;
  1376. while ((entities[i] != (char *) NULL) &&
  1377. (strncmp(xml+1,entities[i],strlen(entities[i])) != 0))
  1378. i+=2;
  1379. if (entities[i++] == (char *) NULL)
  1380. xml++;
  1381. else
  1382. if (entities[i] != (char *) NULL)
  1383. {
  1384. /*
  1385. Found a match.
  1386. */
  1387. length=strlen(entities[i]);
  1388. entity=strchr(xml,';');
  1389. if ((entity != (char *) NULL) &&
  1390. ((length-1L) >= (size_t) (entity-xml)))
  1391. {
  1392. offset=(ssize_t) (xml-p);
  1393. extent=(size_t) (offset+length+strlen(entity));
  1394. if (p != q)
  1395. {
  1396. p=(char *) ResizeQuantumMemory(p,extent+1,sizeof(*p));
  1397. p[extent]='\0';
  1398. }
  1399. else
  1400. {
  1401. char
  1402. *extent_xml;
  1403. extent_xml=(char *) AcquireQuantumMemory(extent+1,
  1404. sizeof(*extent_xml));
  1405. if (extent_xml != (char *) NULL)
  1406. {
  1407. memset(extent_xml,0,extent*sizeof(*extent_xml));
  1408. (void) CopyMagickString(extent_xml,p,extent*
  1409. sizeof(*extent_xml));
  1410. }
  1411. p=extent_xml;
  1412. }
  1413. if (p == (char *) NULL)
  1414. ThrowFatalException(ResourceLimitFatalError,
  1415. "MemoryAllocationFailed");
  1416. xml=p+offset;
  1417. entity=strchr(xml,';');
  1418. }
  1419. if (entity != (char *) NULL)
  1420. (void) memmove(xml+length,entity+1,strlen(entity));
  1421. (void) strncpy(xml,entities[i],length);
  1422. }
  1423. }
  1424. else
  1425. if (((state == ' ') || (state == '*')) &&
  1426. (isspace((int) ((unsigned char) *xml) != 0)))
  1427. *(xml++)=' ';
  1428. else
  1429. xml++;
  1430. }
  1431. if (state == '*')
  1432. {
  1433. /*
  1434. Normalize spaces for non-CDATA attributes.
  1435. */
  1436. for (xml=p; *xml != '\0'; xml++)
  1437. {
  1438. char
  1439. accept[] = " ";
  1440. i=(ssize_t) strspn(xml,accept);
  1441. if (i != 0)
  1442. (void) memmove(xml,xml+i,strlen(xml+i)+1);
  1443. while ((*xml != '\0') && (*xml != ' '))
  1444. xml++;
  1445. if (*xml == '\0')
  1446. break;
  1447. }
  1448. xml--;
  1449. if ((xml >= p) && (*xml == ' '))
  1450. *xml='\0';
  1451. }
  1452. return(p == q ? ConstantString(p) : p);
  1453. }
  1454. static void ParseCharacterContent(XMLTreeRoot *root,char *xml,
  1455. const size_t length,const char state)
  1456. {
  1457. XMLTreeInfo
  1458. *xml_info;
  1459. xml_info=root->node;
  1460. if ((xml_info == (XMLTreeInfo *) NULL) || (xml_info->tag == (char *) NULL) ||
  1461. (length == 0))
  1462. return;
  1463. xml[length]='\0';
  1464. xml=ParseEntities(xml,root->entities,state);
  1465. if ((xml_info->content != (char *) NULL) && (*xml_info->content != '\0'))
  1466. {
  1467. (void) ConcatenateString(&xml_info->content,xml);
  1468. xml=DestroyString(xml);
  1469. }
  1470. else
  1471. {
  1472. if (xml_info->content != (char *) NULL)
  1473. xml_info->content=DestroyString(xml_info->content);
  1474. xml_info->content=xml;
  1475. }
  1476. }
  1477. static XMLTreeInfo *ParseCloseTag(XMLTreeRoot *root,char *tag,
  1478. ExceptionInfo *exception)
  1479. {
  1480. if ((root->node == (XMLTreeInfo *) NULL) ||
  1481. (root->node->tag == (char *) NULL) || (strcmp(tag,root->node->tag) != 0))
  1482. {
  1483. (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
  1484. "ParseError","unexpected closing tag </%s>",tag);
  1485. return(&root->root);
  1486. }
  1487. root->node=root->node->parent;
  1488. return((XMLTreeInfo *) NULL);
  1489. }
  1490. static MagickBooleanType ValidateEntities(char *tag,char *xml,
  1491. const size_t depth,char **entities)
  1492. {
  1493. register ssize_t
  1494. i;
  1495. /*
  1496. Check for circular entity references.
  1497. */
  1498. if (depth > MagickMaxRecursionDepth)
  1499. return(MagickFalse);
  1500. for ( ; ; xml++)
  1501. {
  1502. while ((*xml != '\0') && (*xml != '&'))
  1503. xml++;
  1504. if (*xml == '\0')
  1505. return(MagickTrue);
  1506. if (strncmp(xml+1,tag,strlen(tag)) == 0)
  1507. return(MagickFalse);
  1508. i=0;
  1509. while ((entities[i] != (char *) NULL) &&
  1510. (strncmp(entities[i],xml+1,strlen(entities[i])) == 0))
  1511. i+=2;
  1512. if ((entities[i] != (char *) NULL) &&
  1513. (ValidateEntities(tag,entities[i+1],depth+1,entities) == 0))
  1514. return(MagickFalse);
  1515. }
  1516. }
  1517. static void ParseProcessingInstructions(XMLTreeRoot *root,char *xml,
  1518. size_t length)
  1519. {
  1520. char
  1521. *target;
  1522. register ssize_t
  1523. i;
  1524. ssize_t
  1525. j;
  1526. target=xml;
  1527. xml[length]='\0';
  1528. xml+=strcspn(xml,XMLWhitespace);
  1529. if (*xml != '\0')
  1530. {
  1531. *xml='\0';
  1532. xml+=strspn(xml+1,XMLWhitespace)+1;
  1533. }
  1534. if (strcmp(target,"xml") == 0)
  1535. {
  1536. xml=strstr(xml,"standalone");
  1537. if ((xml != (char *) NULL) &&
  1538. (strncmp(xml+strspn(xml+10,XMLWhitespace "='\"")+10,"yes",3) == 0))
  1539. root->standalone=MagickTrue;
  1540. return;
  1541. }
  1542. if (root->processing_instructions[0] == (char **) NULL)
  1543. {
  1544. root->processing_instructions=(char ***) AcquireCriticalMemory(sizeof(
  1545. *root->processing_instructions));
  1546. *root->processing_instructions=(char **) NULL;
  1547. }
  1548. i=0;
  1549. while ((root->processing_instructions[i] != (char **) NULL) &&
  1550. (strcmp(target,root->processing_instructions[i][0]) != 0))
  1551. i++;
  1552. if (root->processing_instructions[i] == (char **) NULL)
  1553. {
  1554. root->processing_instructions=(char ***) ResizeQuantumMemory(
  1555. root->processing_instructions,(size_t) (i+2),
  1556. sizeof(*root->processing_instructions));
  1557. if (root->processing_instructions == (char ***) NULL)
  1558. ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
  1559. root->processing_instructions[i]=(char **) AcquireQuantumMemory(3,
  1560. sizeof(**root->processing_instructions));
  1561. if (root->processing_instructions[i] == (char **) NULL)
  1562. ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
  1563. root->processing_instructions[i+1]=(char **) NULL;
  1564. root->processing_instructions[i][0]=ConstantString(target);
  1565. root->processing_instructions[i][1]=(char *)
  1566. root->processing_instructions[i+1];
  1567. root->processing_instructions[i+1]=(char **) NULL;
  1568. root->processing_instructions[i][2]=ConstantString("");
  1569. }
  1570. j=1;
  1571. while (root->processing_instructions[i][j] != (char *) NULL)
  1572. j++;
  1573. root->processing_instructions[i]=(char **) ResizeQuantumMemory(
  1574. root->processing_instructions[i],(size_t) (j+3),
  1575. sizeof(**root->processing_instructions));
  1576. if (root->processing_instructions[i] == (char **) NULL)
  1577. ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
  1578. root->processing_instructions[i][j+2]=(char *) ResizeQuantumMemory(
  1579. root->processing_instructions[i][j+1],(size_t) (j+1),
  1580. sizeof(***root->processing_instructions));
  1581. if (root->processing_instructions[i][j+2] == (char *) NULL)
  1582. ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
  1583. (void) CopyMagickString(root->processing_instructions[i][j+2]+j-1,
  1584. root->root.tag != (char *) NULL ? ">" : "<",2);
  1585. root->processing_instructions[i][j]=ConstantString(xml);
  1586. root->processing_instructions[i][j+1]=(char *) NULL;
  1587. }
  1588. static MagickBooleanType ParseInternalDoctype(XMLTreeRoot *root,char *xml,
  1589. size_t length,ExceptionInfo *exception)
  1590. {
  1591. char
  1592. *c,
  1593. **entities,
  1594. *n,
  1595. **predefined_entitites,
  1596. q,
  1597. *t,
  1598. *v;
  1599. register ssize_t
  1600. i;
  1601. ssize_t
  1602. j;
  1603. n=(char *) NULL;
  1604. predefined_entitites=(char **) AcquireMagickMemory(sizeof(sentinel));
  1605. if (predefined_entitites == (char **) NULL)
  1606. ThrowFatalException(ResourceLimitError,"MemoryAllocationFailed");
  1607. (void) memcpy(predefined_entitites,sentinel,sizeof(sentinel));
  1608. for (xml[length]='\0'; xml != (char *) NULL; )
  1609. {
  1610. while ((*xml != '\0') && (*xml != '<') && (*xml != '%'))
  1611. xml++;
  1612. if (*xml == '\0')
  1613. break;
  1614. if ((strlen(xml) > 9) && (strncmp(xml,"<!ENTITY",8) == 0))
  1615. {
  1616. /*
  1617. Parse entity definitions.
  1618. */
  1619. if (strspn(xml+8,XMLWhitespace) == 0)
  1620. break;
  1621. xml+=strspn(xml+8,XMLWhitespace)+8;
  1622. c=xml;
  1623. n=xml+strspn(xml,XMLWhitespace "%");
  1624. if ((isalpha((int) ((unsigned char) *n)) == 0) && (*n != '_'))
  1625. break;
  1626. xml=n+strcspn(n,XMLWhitespace);
  1627. if (*xml == '\0')
  1628. break;
  1629. *xml=';';
  1630. v=xml+strspn(xml+1,XMLWhitespace)+1;
  1631. q=(*v);
  1632. v++;
  1633. if ((q != '"') && (q != '\''))
  1634. {
  1635. /*
  1636. Skip externals.
  1637. */
  1638. xml=strchr(xml,'>');
  1639. continue;
  1640. }
  1641. entities=(*c == '%') ? predefined_entitites : root->entities;
  1642. for (i=0; entities[i] != (char *) NULL; i++) ;
  1643. entities=(char **) ResizeQuantumMemory(entities,(size_t) (i+3),
  1644. sizeof(*entities));
  1645. if (entities == (char **) NULL)
  1646. ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
  1647. if (*c == '%')
  1648. predefined_entitites=entities;
  1649. else
  1650. root->entities=entities;
  1651. xml++;
  1652. *xml='\0';
  1653. xml=strchr(v,q);
  1654. if (xml != (char *) NULL)
  1655. {
  1656. *xml='\0';
  1657. xml++;
  1658. }
  1659. entities[i+1]=ParseEntities(v,predefined_entitites,'%');
  1660. entities[i+2]=(char *) NULL;
  1661. if (ValidateEntities(n,entities[i+1],0,entities) != MagickFalse)
  1662. entities[i]=n;
  1663. else
  1664. {
  1665. if (entities[i+1] != v)
  1666. entities[i+1]=DestroyString(entities[i+1]);
  1667. (void) ThrowMagickException(exception,GetMagickModule(),
  1668. OptionWarning,"ParseError","circular entity declaration &%s",n);
  1669. predefined_entitites=(char **) RelinquishMagickMemory(
  1670. predefined_entitites);
  1671. return(MagickFalse);
  1672. }
  1673. }
  1674. else
  1675. if (strncmp(xml,"<!ATTLIST",9) == 0)
  1676. {
  1677. /*
  1678. Parse default attributes.
  1679. */
  1680. t=xml+strspn(xml+9,XMLWhitespace)+9;
  1681. if (*t == '\0')
  1682. {
  1683. (void) ThrowMagickException(exception,GetMagickModule(),
  1684. OptionWarning,"ParseError","unclosed <!ATTLIST");
  1685. predefined_entitites=(char **) RelinquishMagickMemory(
  1686. predefined_entitites);
  1687. return(MagickFalse);
  1688. }
  1689. xml=t+strcspn(t,XMLWhitespace ">");
  1690. if (*xml == '>')
  1691. continue;
  1692. *xml='\0';
  1693. i=0;
  1694. while ((root->attributes[i] != (char **) NULL) &&
  1695. (n != (char *) NULL) &&
  1696. (strcmp(n,root->attributes[i][0]) != 0))
  1697. i++;
  1698. while ((*(n=xml+strspn(xml+1,XMLWhitespace)+1) != '\0') &&
  1699. (*n != '>'))
  1700. {
  1701. xml=n+strcspn(n,XMLWhitespace);
  1702. if (*xml != '\0')
  1703. *xml='\0';
  1704. else
  1705. {
  1706. (void) ThrowMagickException(exception,GetMagickModule(),
  1707. OptionWarning,"ParseError","malformed <!ATTLIST");
  1708. predefined_entitites=(char **) RelinquishMagickMemory(
  1709. predefined_entitites);
  1710. return(MagickFalse);
  1711. }
  1712. xml+=strspn(xml+1,XMLWhitespace)+1;
  1713. c=(char *) (strncmp(xml,"CDATA",5) != 0 ? "*" : " ");
  1714. if (strncmp(xml,"NOTATION",8) == 0)
  1715. xml+=strspn(xml+8,XMLWhitespace)+8;
  1716. xml=(*xml == '(') ? strchr(xml,')') : xml+
  1717. strcspn(xml,XMLWhitespace);
  1718. if (xml == (char *) NULL)
  1719. {
  1720. (void) ThrowMagickException(exception,GetMagickModule(),
  1721. OptionWarning,"ParseError","malformed <!ATTLIST");
  1722. predefined_entitites=(char **) RelinquishMagickMemory(
  1723. predefined_entitites);
  1724. return(MagickFalse);
  1725. }
  1726. xml+=strspn(xml,XMLWhitespace ")");
  1727. if (strncmp(xml,"#FIXED",6) == 0)
  1728. xml+=strspn(xml+6,XMLWhitespace)+6;
  1729. if (*xml == '#')
  1730. {
  1731. xml+=strcspn(xml,XMLWhitespace ">")-1;
  1732. if (*c == ' ')
  1733. continue;
  1734. v=(char *) NULL;
  1735. }
  1736. else
  1737. if (((*xml == '"') || (*xml == '\'')) &&
  1738. ((xml=strchr(v=xml+1,*xml)) != (char *) NULL))
  1739. *xml='\0';
  1740. else
  1741. {
  1742. (void) ThrowMagickException(exception,GetMagickModule(),
  1743. OptionWarning,"ParseError","malformed <!ATTLIST");
  1744. predefined_entitites=(char **) RelinquishMagickMemory(
  1745. predefined_entitites);
  1746. return(MagickFalse);
  1747. }
  1748. if (root->attributes[i] == (char **) NULL)
  1749. {
  1750. /*
  1751. New attribute tag.
  1752. */
  1753. if (i == 0)
  1754. root->attributes=(char ***) AcquireQuantumMemory(2,
  1755. sizeof(*root->attributes));
  1756. else
  1757. root->attributes=(char ***) ResizeQuantumMemory(
  1758. root->attributes,(size_t) (i+2),
  1759. sizeof(*root->attributes));
  1760. if (root->attributes == (char ***) NULL)
  1761. ThrowFatalException(ResourceLimitFatalError,
  1762. "MemoryAllocationFailed");
  1763. root->attributes[i]=(char **) AcquireQuantumMemory(2,
  1764. sizeof(**root->attributes));
  1765. if (root->attributes[i] == (char **) NULL)
  1766. ThrowFatalException(ResourceLimitFatalError,
  1767. "MemoryAllocationFailed");
  1768. root->attributes[i][0]=ConstantString(t);
  1769. root->attributes[i][1]=(char *) NULL;
  1770. root->attributes[i+1]=(char **) NULL;
  1771. }
  1772. for (j=1; root->attributes[i][j] != (char *) NULL; j+=3) ;
  1773. root->attributes[i]=(char **) ResizeQuantumMemory(
  1774. root->attributes[i],(size_t) (j+4),sizeof(**root->attributes));
  1775. if (root->attributes[i] == (char **) NULL)
  1776. ThrowFatalException(ResourceLimitFatalError,
  1777. "MemoryAllocationFailed");
  1778. root->attributes[i][j+3]=(char *) NULL;
  1779. root->attributes[i][j+2]=ConstantString(c);
  1780. root->attributes[i][j+1]=(char *) NULL;
  1781. if (v != (char *) NULL)
  1782. root->attributes[i][j+1]=ParseEntities(v,root->entities,*c);
  1783. root->attributes[i][j]=ConstantString(n);
  1784. }
  1785. }
  1786. else
  1787. if (strncmp(xml, "<!--", 4) == 0)
  1788. xml=strstr(xml+4,"-->");
  1789. else
  1790. if (strncmp(xml,"<?", 2) == 0)
  1791. {
  1792. c=xml+2;
  1793. xml=strstr(c,"?>");
  1794. if (xml != (char *) NULL)
  1795. {
  1796. ParseProcessingInstructions(root,c,(size_t) (xml-c));
  1797. xml++;
  1798. }
  1799. }
  1800. else
  1801. if (*xml == '<')
  1802. xml=strchr(xml,'>');
  1803. else
  1804. if ((*(xml++) == '%') && (root->standalone == MagickFalse))
  1805. break;
  1806. }
  1807. predefined_entitites=(char **) RelinquishMagickMemory(predefined_entitites);
  1808. return(MagickTrue);
  1809. }
  1810. static void ParseOpenTag(XMLTreeRoot *root,char *tag,char **attributes)
  1811. {
  1812. XMLTreeInfo
  1813. *xml_info;
  1814. xml_info=root->node;
  1815. if (xml_info->tag == (char *) NULL)
  1816. xml_info->tag=ConstantString(tag);
  1817. else
  1818. xml_info=AddChildToXMLTree(xml_info,tag,strlen(xml_info->content));
  1819. if (xml_info != (XMLTreeInfo *) NULL)
  1820. xml_info->attributes=attributes;
  1821. root->node=xml_info;
  1822. }
  1823. static const char
  1824. *ignore_tags[3] =
  1825. {
  1826. "rdf:Bag",
  1827. "rdf:Seq",
  1828. (const char *) NULL
  1829. };
  1830. static inline MagickBooleanType IsSkipTag(const char *tag)
  1831. {
  1832. register ssize_t
  1833. i;
  1834. i=0;
  1835. while (ignore_tags[i] != (const char *) NULL)
  1836. {
  1837. if (LocaleCompare(tag,ignore_tags[i]) == 0)
  1838. return(MagickTrue);
  1839. i++;
  1840. }
  1841. return(MagickFalse);
  1842. }
  1843. MagickExport XMLTreeInfo *NewXMLTree(const char *xml,ExceptionInfo *exception)
  1844. {
  1845. char
  1846. **attribute,
  1847. **attributes,
  1848. *tag,
  1849. *utf8;
  1850. int
  1851. c,
  1852. terminal;
  1853. MagickBooleanType
  1854. status;
  1855. register char
  1856. *p;
  1857. register ssize_t
  1858. i;
  1859. size_t
  1860. ignore_depth,
  1861. length;
  1862. ssize_t
  1863. j,
  1864. l;
  1865. XMLTreeRoot
  1866. *root;
  1867. /*
  1868. Convert xml-string to UTF8.
  1869. */
  1870. if ((xml == (const char *) NULL) || (strlen(xml) == 0))
  1871. {
  1872. (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
  1873. "ParseError","root tag missing");
  1874. return((XMLTreeInfo *) NULL);
  1875. }
  1876. root=(XMLTreeRoot *) NewXMLTreeTag((char *) NULL);
  1877. length=strlen(xml);
  1878. utf8=ConvertUTF16ToUTF8(xml,&length);
  1879. if (utf8 == (char *) NULL)
  1880. {
  1881. (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
  1882. "ParseError","UTF16 to UTF8 failed");
  1883. return((XMLTreeInfo *) NULL);
  1884. }
  1885. terminal=utf8[length-1];
  1886. utf8[length-1]='\0';
  1887. p=utf8;
  1888. while ((*p != '\0') && (*p != '<'))
  1889. p++;
  1890. if (*p == '\0')
  1891. {
  1892. (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
  1893. "ParseError","root tag missing");
  1894. utf8=DestroyString(utf8);
  1895. return((XMLTreeInfo *) NULL);
  1896. }
  1897. attribute=(char **) NULL;
  1898. l=0;
  1899. ignore_depth=0;
  1900. for (p++; ; p++)
  1901. {
  1902. attributes=(char **) sentinel;
  1903. tag=p;
  1904. c=(*p);
  1905. if ((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_') ||
  1906. (*p == ':') || (c < '\0'))
  1907. {
  1908. /*
  1909. Tag.
  1910. */
  1911. if (root->node == (XMLTreeInfo *) NULL)
  1912. {
  1913. (void) ThrowMagickException(exception,GetMagickModule(),
  1914. OptionWarning,"ParseError","root tag missing");
  1915. utf8=DestroyString(utf8);
  1916. return(&root->root);
  1917. }
  1918. p+=strcspn(p,XMLWhitespace "/>");
  1919. while (isspace((int) ((unsigned char) *p)) != 0)
  1920. *p++='\0';
  1921. if (((isalpha((int) ((unsigned char) *p)) != 0) || (*p == '_')) &&
  1922. (ignore_depth == 0))
  1923. {
  1924. if ((*p != '\0') && (*p != '/') && (*p != '>'))
  1925. {
  1926. /*
  1927. Find tag in default attributes list.
  1928. */
  1929. i=0;
  1930. while ((root->attributes[i] != (char **) NULL) &&
  1931. (strcmp(root->attributes[i][0],tag) != 0))
  1932. i++;
  1933. attribute=root->attributes[i];
  1934. }
  1935. for (l=0; (*p != '\0') && (*p != '/') && (*p != '>'); l+=2)
  1936. {
  1937. /*
  1938. Attribute.
  1939. */
  1940. if (l == 0)
  1941. attributes=(char **) AcquireQuantumMemory(4,
  1942. sizeof(*attributes));
  1943. else
  1944. attributes=(char **) ResizeQuantumMemory(attributes,(size_t)
  1945. (l+4),sizeof(*attributes));
  1946. if (attributes == (char **) NULL)
  1947. {
  1948. (void) ThrowMagickException(exception,GetMagickModule(),
  1949. ResourceLimitError,"MemoryAllocationFailed","`%s'","");
  1950. utf8=DestroyString(utf8);
  1951. return(&root->root);
  1952. }
  1953. attributes[l+2]=(char *) NULL;
  1954. attributes[l+1]=(char *) NULL;
  1955. attributes[l]=p;
  1956. p+=strcspn(p,XMLWhitespace "=/>");
  1957. if ((*p != '=') && (isspace((int) ((unsigned char) *p)) == 0))
  1958. attributes[l]=ConstantString("");
  1959. else
  1960. {
  1961. *p++='\0';
  1962. p+=strspn(p,XMLWhitespace "=");
  1963. c=(*p);
  1964. if ((c == '"') || (c == '\''))
  1965. {
  1966. /*
  1967. Attributes value.
  1968. */
  1969. p++;
  1970. attributes[l+1]=p;
  1971. while ((*p != '\0') && (*p != c))
  1972. p++;
  1973. if (*p != '\0')
  1974. *p++='\0';
  1975. else
  1976. {
  1977. attributes[l]=ConstantString("");
  1978. attributes[l+1]=ConstantString("");
  1979. (void) DestroyXMLTreeAttributes(attributes);
  1980. (void) ThrowMagickException(exception,
  1981. GetMagickModule(),OptionWarning,"ParseError",
  1982. "missing %c",c);
  1983. utf8=DestroyString(utf8);
  1984. return(&root->root);
  1985. }
  1986. j=1;
  1987. while ((attribute != (char **) NULL) &&
  1988. (attribute[j] != (char *) NULL) &&
  1989. (strcmp(attribute[j],attributes[l]) != 0))
  1990. j+=3;
  1991. attributes[l+1]=ParseEntities(attributes[l+1],
  1992. root->entities,(attribute != (char **) NULL) &&
  1993. (attribute[j] != (char *) NULL) ? *attribute[j+2] :
  1994. ' ');
  1995. }
  1996. attributes[l]=ConstantString(attributes[l]);
  1997. }
  1998. while (isspace((int) ((unsigned char) *p)) != 0)
  1999. p++;
  2000. }
  2001. }
  2002. else
  2003. {
  2004. while((*p != '\0') && (*p != '/') && (*p != '>'))
  2005. p++;
  2006. }
  2007. if (*p == '/')
  2008. {
  2009. /*
  2010. Self closing tag.
  2011. */
  2012. *p++='\0';
  2013. if (((*p != '\0') && (*p != '>')) ||
  2014. ((*p == '\0') && (terminal != '>')))
  2015. {
  2016. if (l != 0)
  2017. (void) DestroyXMLTreeAttributes(attributes);
  2018. (void) ThrowMagickException(exception,GetMagickModule(),
  2019. OptionWarning,"ParseError","missing >");
  2020. utf8=DestroyString(utf8);
  2021. return(&root->root);
  2022. }
  2023. if ((ignore_depth != 0) || (IsSkipTag(tag) != MagickFalse))
  2024. (void) DestroyXMLTreeAttributes(attributes);
  2025. else
  2026. {
  2027. ParseOpenTag(root,tag,attributes);
  2028. (void) ParseCloseTag(root,tag,exception);
  2029. }
  2030. }
  2031. else
  2032. {
  2033. c=(*p);
  2034. if ((*p == '>') || ((*p == '\0') && (terminal == '>')))
  2035. {
  2036. *p='\0';
  2037. if ((ignore_depth == 0) && (IsSkipTag(tag) == MagickFalse))
  2038. ParseOpenTag(root,tag,attributes);
  2039. else
  2040. {
  2041. ignore_depth++;
  2042. (void) DestroyXMLTreeAttributes(attributes);
  2043. }
  2044. *p=c;
  2045. }
  2046. else
  2047. {
  2048. if (l != 0)
  2049. (void) DestroyXMLTreeAttributes(attributes);
  2050. (void) ThrowMagickException(exception,GetMagickModule(),
  2051. OptionWarning,"ParseError","missing >");
  2052. utf8=DestroyString(utf8);
  2053. return(&root->root);
  2054. }
  2055. }
  2056. }
  2057. else
  2058. if (*p == '/')
  2059. {
  2060. /*
  2061. Close tag.
  2062. */
  2063. tag=p+1;
  2064. p+=strcspn(tag,XMLWhitespace ">")+1;
  2065. c=(*p);
  2066. if ((c == '\0') && (terminal != '>'))
  2067. {
  2068. (void) ThrowMagickException(exception,GetMagickModule(),
  2069. OptionWarning,"ParseError","missing >");
  2070. utf8=DestroyString(utf8);
  2071. return(&root->root);
  2072. }
  2073. *p='\0';
  2074. if ((ignore_depth == 0) &&
  2075. (ParseCloseTag(root,tag,exception) != (XMLTreeInfo *) NULL))
  2076. {
  2077. utf8=DestroyString(utf8);
  2078. return(&root->root);
  2079. }
  2080. if (ignore_depth > 0)
  2081. ignore_depth--;
  2082. *p=c;
  2083. if (isspace((int) ((unsigned char) *p)) != 0)
  2084. p+=strspn(p,XMLWhitespace);
  2085. }
  2086. else
  2087. if (strncmp(p,"!--",3) == 0)
  2088. {
  2089. /*
  2090. Comment.
  2091. */
  2092. p=strstr(p+3,"--");
  2093. if ((p == (char *) NULL) || ((*(p+=2) != '>') && (*p != '\0')) ||
  2094. ((*p == '\0') && (terminal != '>')))
  2095. {
  2096. (void) ThrowMagickException(exception,GetMagickModule(),
  2097. OptionWarning,"ParseError","unclosed <!--");
  2098. utf8=DestroyString(utf8);
  2099. return(&root->root);
  2100. }
  2101. }
  2102. else
  2103. if (strncmp(p,"![CDATA[",8) == 0)
  2104. {
  2105. /*
  2106. Cdata.
  2107. */
  2108. p=strstr(p,"]]>");
  2109. if (p != (char *) NULL)
  2110. {
  2111. p+=2;
  2112. if (ignore_depth == 0)
  2113. ParseCharacterContent(root,tag+8,(size_t) (p-tag-10),'c');
  2114. }
  2115. else
  2116. {
  2117. (void) ThrowMagickException(exception,GetMagickModule(),
  2118. OptionWarning,"ParseError","unclosed <![CDATA[");
  2119. utf8=DestroyString(utf8);
  2120. return(&root->root);
  2121. }
  2122. }
  2123. else
  2124. if (strncmp(p,"!DOCTYPE",8) == 0)
  2125. {
  2126. /*
  2127. DTD.
  2128. */
  2129. for (l=0; (*p != '\0') && (((l == 0) && (*p != '>')) ||
  2130. ((l != 0) && ((*p != ']') ||
  2131. (*(p+strspn(p+1,XMLWhitespace)+1) != '>'))));
  2132. l=(ssize_t) ((*p == '[') ? 1 : l))
  2133. p+=strcspn(p+1,"[]>")+1;
  2134. if ((*p == '\0') && (terminal != '>'))
  2135. {
  2136. (void) ThrowMagickException(exception,GetMagickModule(),
  2137. OptionWarning,"ParseError","unclosed <!DOCTYPE");
  2138. utf8=DestroyString(utf8);
  2139. return(&root->root);
  2140. }
  2141. if (l != 0)
  2142. tag=strchr(tag,'[')+1;
  2143. if (l != 0)
  2144. {
  2145. status=ParseInternalDoctype(root,tag,(size_t) (p-tag),
  2146. exception);
  2147. if (status == MagickFalse)
  2148. {
  2149. utf8=DestroyString(utf8);
  2150. return(&root->root);
  2151. }
  2152. p++;
  2153. }
  2154. }
  2155. else
  2156. if (*p == '?')
  2157. {
  2158. /*
  2159. Processing instructions.
  2160. */
  2161. do
  2162. {
  2163. p=strchr(p,'?');
  2164. if (p == (char *) NULL)
  2165. break;
  2166. p++;
  2167. } while ((*p != '\0') && (*p != '>'));
  2168. if ((p == (char *) NULL) || ((*p == '\0') &&
  2169. (terminal != '>')))
  2170. {
  2171. (void) ThrowMagickException(exception,GetMagickModule(),
  2172. OptionWarning,"ParseError","unclosed <?");
  2173. utf8=DestroyString(utf8);
  2174. return(&root->root);
  2175. }
  2176. ParseProcessingInstructions(root,tag+1,(size_t) (p-tag-2));
  2177. }
  2178. else
  2179. {
  2180. (void) ThrowMagickException(exception,GetMagickModule(),
  2181. OptionWarning,"ParseError","unexpected <");
  2182. utf8=DestroyString(utf8);
  2183. return(&root->root);
  2184. }
  2185. if ((p == (char *) NULL) || (*p == '\0'))
  2186. break;
  2187. *p++='\0';
  2188. tag=p;
  2189. if ((*p != '\0') && (*p != '<'))
  2190. {
  2191. /*
  2192. Tag character content.
  2193. */
  2194. while ((*p != '\0') && (*p != '<'))
  2195. p++;
  2196. if (*p == '\0')
  2197. break;
  2198. if (ignore_depth == 0)
  2199. ParseCharacterContent(root,tag,(size_t) (p-tag),'&');
  2200. }
  2201. else
  2202. if (*p == '\0')
  2203. break;
  2204. }
  2205. utf8=DestroyString(utf8);
  2206. if (root->node == (XMLTreeInfo *) NULL)
  2207. return(&root->root);
  2208. if (root->node->tag == (char *) NULL)
  2209. {
  2210. (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
  2211. "ParseError","root tag missing");
  2212. return(&root->root);
  2213. }
  2214. (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
  2215. "ParseError","unclosed tag: '%s'",root->node->tag);
  2216. return(&root->root);
  2217. }
  2218. /*
  2219. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2220. % %
  2221. % %
  2222. % %
  2223. % N e w X M L T r e e T a g %
  2224. % %
  2225. % %
  2226. % %
  2227. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2228. %
  2229. % NewXMLTreeTag() returns a new empty xml structure for the xml-tree tag.
  2230. %
  2231. % The format of the NewXMLTreeTag method is:
  2232. %
  2233. % XMLTreeInfo *NewXMLTreeTag(const char *tag)
  2234. %
  2235. % A description of each parameter follows:
  2236. %
  2237. % o tag: the tag.
  2238. %
  2239. */
  2240. MagickExport XMLTreeInfo *NewXMLTreeTag(const char *tag)
  2241. {
  2242. static const char
  2243. *predefined_entities[NumberPredefinedEntities+1] =
  2244. {
  2245. "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
  2246. "apos;", "&#39;", "amp;", "&#38;", (char *) NULL
  2247. };
  2248. XMLTreeRoot
  2249. *root;
  2250. root=(XMLTreeRoot *) AcquireMagickMemory(sizeof(*root));
  2251. if (root == (XMLTreeRoot *) NULL)
  2252. return((XMLTreeInfo *) NULL);
  2253. (void) memset(root,0,sizeof(*root));
  2254. root->root.tag=(char *) NULL;
  2255. if (tag != (char *) NULL)
  2256. root->root.tag=ConstantString(tag);
  2257. root->node=(&root->root);
  2258. root->root.content=ConstantString("");
  2259. root->entities=(char **) AcquireMagickMemory(sizeof(predefined_entities));
  2260. if (root->entities == (char **) NULL)
  2261. return((XMLTreeInfo *) NULL);
  2262. (void) memcpy(root->entities,predefined_entities,sizeof(predefined_entities));
  2263. root->root.attributes=sentinel;
  2264. root->attributes=(char ***) root->root.attributes;
  2265. root->processing_instructions=(char ***) root->root.attributes;
  2266. root->debug=IsEventLogging();
  2267. root->signature=MagickCoreSignature;
  2268. return(&root->root);
  2269. }
  2270. /*
  2271. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2272. % %
  2273. % %
  2274. % %
  2275. % P r u n e T a g F r o m X M L T r e e %
  2276. % %
  2277. % %
  2278. % %
  2279. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2280. %
  2281. % PruneTagFromXMLTree() prunes a tag from the xml-tree along with all its
  2282. % subtags.
  2283. %
  2284. % The format of the PruneTagFromXMLTree method is:
  2285. %
  2286. % XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
  2287. %
  2288. % A description of each parameter follows:
  2289. %
  2290. % o xml_info: the xml info.
  2291. %
  2292. */
  2293. MagickPrivate XMLTreeInfo *PruneTagFromXMLTree(XMLTreeInfo *xml_info)
  2294. {
  2295. XMLTreeInfo
  2296. *node;
  2297. assert(xml_info != (XMLTreeInfo *) NULL);
  2298. assert((xml_info->signature == MagickCoreSignature) ||
  2299. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  2300. if (xml_info->debug != MagickFalse)
  2301. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  2302. if (xml_info->next != (XMLTreeInfo *) NULL)
  2303. xml_info->next->sibling=xml_info->sibling;
  2304. if (xml_info->parent != (XMLTreeInfo *) NULL)
  2305. {
  2306. node=xml_info->parent->child;
  2307. if (node == xml_info)
  2308. xml_info->parent->child=xml_info->ordered;
  2309. else
  2310. {
  2311. while (node->ordered != xml_info)
  2312. node=node->ordered;
  2313. node->ordered=node->ordered->ordered;
  2314. node=xml_info->parent->child;
  2315. if (strcmp(node->tag,xml_info->tag) != 0)
  2316. {
  2317. while (strcmp(node->sibling->tag,xml_info->tag) != 0)
  2318. node=node->sibling;
  2319. if (node->sibling != xml_info)
  2320. node=node->sibling;
  2321. else
  2322. node->sibling=(xml_info->next != (XMLTreeInfo *) NULL) ?
  2323. xml_info->next : node->sibling->sibling;
  2324. }
  2325. while ((node->next != (XMLTreeInfo *) NULL) &&
  2326. (node->next != xml_info))
  2327. node=node->next;
  2328. if (node->next != (XMLTreeInfo *) NULL)
  2329. node->next=node->next->next;
  2330. }
  2331. }
  2332. xml_info->ordered=(XMLTreeInfo *) NULL;
  2333. xml_info->sibling=(XMLTreeInfo *) NULL;
  2334. xml_info->next=(XMLTreeInfo *) NULL;
  2335. return(xml_info);
  2336. }
  2337. /*
  2338. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2339. % %
  2340. % %
  2341. % %
  2342. % S e t X M L T r e e A t t r i b u t e %
  2343. % %
  2344. % %
  2345. % %
  2346. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2347. %
  2348. % SetXMLTreeAttribute() sets the tag attributes or adds a new attribute if not
  2349. % found. A value of NULL removes the specified attribute.
  2350. %
  2351. % The format of the SetXMLTreeAttribute method is:
  2352. %
  2353. % XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,const char *tag,
  2354. % const char *value)
  2355. %
  2356. % A description of each parameter follows:
  2357. %
  2358. % o xml_info: the xml info.
  2359. %
  2360. % o tag: The attribute tag.
  2361. %
  2362. % o value: The attribute value.
  2363. %
  2364. */
  2365. MagickPrivate XMLTreeInfo *SetXMLTreeAttribute(XMLTreeInfo *xml_info,
  2366. const char *tag,const char *value)
  2367. {
  2368. register ssize_t
  2369. i;
  2370. ssize_t
  2371. j;
  2372. assert(xml_info != (XMLTreeInfo *) NULL);
  2373. assert((xml_info->signature == MagickCoreSignature) ||
  2374. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  2375. if (xml_info->debug != MagickFalse)
  2376. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  2377. i=0;
  2378. while ((xml_info->attributes[i] != (char *) NULL) &&
  2379. (strcmp(xml_info->attributes[i],tag) != 0))
  2380. i+=2;
  2381. if (xml_info->attributes[i] == (char *) NULL)
  2382. {
  2383. /*
  2384. Add new attribute tag.
  2385. */
  2386. if (value == (const char *) NULL)
  2387. return(xml_info);
  2388. if (xml_info->attributes != sentinel)
  2389. xml_info->attributes=(char **) ResizeQuantumMemory(
  2390. xml_info->attributes,(size_t) (i+4),sizeof(*xml_info->attributes));
  2391. else
  2392. {
  2393. xml_info->attributes=(char **) AcquireQuantumMemory(4,
  2394. sizeof(*xml_info->attributes));
  2395. if (xml_info->attributes != (char **) NULL)
  2396. xml_info->attributes[1]=ConstantString("");
  2397. }
  2398. if (xml_info->attributes == (char **) NULL)
  2399. ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
  2400. xml_info->attributes[i]=ConstantString(tag);
  2401. xml_info->attributes[i+2]=(char *) NULL;
  2402. (void) strlen(xml_info->attributes[i+1]);
  2403. }
  2404. /*
  2405. Add new value to an existing attribute.
  2406. */
  2407. for (j=i; xml_info->attributes[j] != (char *) NULL; j+=2) ;
  2408. if (xml_info->attributes[i+1] != (char *) NULL)
  2409. xml_info->attributes[i+1]=DestroyString(xml_info->attributes[i+1]);
  2410. if (value != (const char *) NULL)
  2411. {
  2412. xml_info->attributes[i+1]=ConstantString(value);
  2413. return(xml_info);
  2414. }
  2415. if (xml_info->attributes[i] != (char *) NULL)
  2416. xml_info->attributes[i]=DestroyString(xml_info->attributes[i]);
  2417. (void) memmove(xml_info->attributes+i,xml_info->attributes+i+2,(size_t)
  2418. (j-i)*sizeof(*xml_info->attributes));
  2419. xml_info->attributes=(char **) ResizeQuantumMemory(xml_info->attributes,
  2420. (size_t) (j+2),sizeof(*xml_info->attributes));
  2421. if (xml_info->attributes == (char **) NULL)
  2422. ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
  2423. j-=2;
  2424. (void) memmove(xml_info->attributes[j+1]+(i/2),xml_info->attributes[j+1]+
  2425. (i/2)+1,(size_t) (((j+2)/2)-(i/2))*sizeof(**xml_info->attributes));
  2426. return(xml_info);
  2427. }
  2428. /*
  2429. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2430. % %
  2431. % %
  2432. % %
  2433. % S e t X M L T r e e C o n t e n t %
  2434. % %
  2435. % %
  2436. % %
  2437. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2438. %
  2439. % SetXMLTreeContent() sets the character content for the given tag and
  2440. % returns the tag.
  2441. %
  2442. % The format of the SetXMLTreeContent method is:
  2443. %
  2444. % XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
  2445. % const char *content)
  2446. %
  2447. % A description of each parameter follows:
  2448. %
  2449. % o xml_info: the xml info.
  2450. %
  2451. % o content: The content.
  2452. %
  2453. */
  2454. MagickExport XMLTreeInfo *SetXMLTreeContent(XMLTreeInfo *xml_info,
  2455. const char *content)
  2456. {
  2457. assert(xml_info != (XMLTreeInfo *) NULL);
  2458. assert((xml_info->signature == MagickCoreSignature) ||
  2459. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  2460. if (xml_info->debug != MagickFalse)
  2461. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  2462. if (xml_info->content != (char *) NULL)
  2463. xml_info->content=DestroyString(xml_info->content);
  2464. xml_info->content=(char *) ConstantString(content);
  2465. return(xml_info);
  2466. }
  2467. /*
  2468. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2469. % %
  2470. % %
  2471. % %
  2472. % X M L T r e e I n f o T o X M L %
  2473. % %
  2474. % %
  2475. % %
  2476. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2477. %
  2478. % XMLTreeInfoToXML() converts an xml-tree to an XML string.
  2479. %
  2480. % The format of the XMLTreeInfoToXML method is:
  2481. %
  2482. % char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
  2483. %
  2484. % A description of each parameter follows:
  2485. %
  2486. % o xml_info: the xml info.
  2487. %
  2488. */
  2489. static char *EncodePredefinedEntities(const char *source,ssize_t offset,
  2490. char **destination,size_t *length,size_t *extent,MagickBooleanType pedantic)
  2491. {
  2492. char
  2493. *canonical_content;
  2494. if (offset < 0)
  2495. canonical_content=CanonicalXMLContent(source,pedantic);
  2496. else
  2497. {
  2498. char
  2499. *content;
  2500. content=AcquireString(source);
  2501. content[offset]='\0';
  2502. canonical_content=CanonicalXMLContent(content,pedantic);
  2503. content=DestroyString(content);
  2504. }
  2505. if (canonical_content == (char *) NULL)
  2506. return(*destination);
  2507. if ((*length+strlen(canonical_content)+MagickPathExtent) > *extent)
  2508. {
  2509. *extent=(*length)+strlen(canonical_content)+MagickPathExtent;
  2510. *destination=(char *) ResizeQuantumMemory(*destination,*extent,
  2511. sizeof(**destination));
  2512. if (*destination == (char *) NULL)
  2513. return(*destination);
  2514. }
  2515. *length+=FormatLocaleString(*destination+(*length),*extent,"%s",
  2516. canonical_content);
  2517. canonical_content=DestroyString(canonical_content);
  2518. return(*destination);
  2519. }
  2520. static char *XMLTreeTagToXML(XMLTreeInfo *xml_info,char **source,size_t *length,
  2521. size_t *extent,size_t start,char ***attributes)
  2522. {
  2523. char
  2524. *content;
  2525. const char
  2526. *attribute;
  2527. register ssize_t
  2528. i;
  2529. size_t
  2530. offset;
  2531. ssize_t
  2532. j;
  2533. content=(char *) "";
  2534. if (xml_info->parent != (XMLTreeInfo *) NULL)
  2535. content=xml_info->parent->content;
  2536. offset=0;
  2537. *source=EncodePredefinedEntities(content+start,(ssize_t) (xml_info->offset-
  2538. start),source,length,extent,MagickFalse);
  2539. if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
  2540. {
  2541. *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
  2542. *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
  2543. if (*source == (char *) NULL)
  2544. return(*source);
  2545. }
  2546. *length+=FormatLocaleString(*source+(*length),*extent,"<%s",xml_info->tag);
  2547. for (i=0; xml_info->attributes[i]; i+=2)
  2548. {
  2549. attribute=GetXMLTreeAttribute(xml_info,xml_info->attributes[i]);
  2550. if (attribute != xml_info->attributes[i+1])
  2551. continue;
  2552. if ((*length+strlen(xml_info->attributes[i])+MagickPathExtent) > *extent)
  2553. {
  2554. *extent=(*length)+strlen(xml_info->attributes[i])+MagickPathExtent;
  2555. *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
  2556. if (*source == (char *) NULL)
  2557. return((char *) NULL);
  2558. }
  2559. *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
  2560. xml_info->attributes[i]);
  2561. (void) EncodePredefinedEntities(xml_info->attributes[i+1],-1,source,length,
  2562. extent,MagickTrue);
  2563. *length+=FormatLocaleString(*source+(*length),*extent,"\"");
  2564. }
  2565. i=0;
  2566. while ((attributes[i] != (char **) NULL) &&
  2567. (strcmp(attributes[i][0],xml_info->tag) != 0))
  2568. i++;
  2569. j=1;
  2570. while ((attributes[i] != (char **) NULL) &&
  2571. (attributes[i][j] != (char *) NULL))
  2572. {
  2573. if ((attributes[i][j+1] == (char *) NULL) ||
  2574. (GetXMLTreeAttribute(xml_info,attributes[i][j]) != attributes[i][j+1]))
  2575. {
  2576. j+=3;
  2577. continue;
  2578. }
  2579. if ((*length+strlen(attributes[i][j])+MagickPathExtent) > *extent)
  2580. {
  2581. *extent=(*length)+strlen(attributes[i][j])+MagickPathExtent;
  2582. *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
  2583. if (*source == (char *) NULL)
  2584. return((char *) NULL);
  2585. }
  2586. *length+=FormatLocaleString(*source+(*length),*extent," %s=\"",
  2587. attributes[i][j]);
  2588. (void) EncodePredefinedEntities(attributes[i][j+1],-1,source,length,extent,
  2589. MagickTrue);
  2590. *length+=FormatLocaleString(*source+(*length),*extent,"\"");
  2591. j+=3;
  2592. }
  2593. *length+=FormatLocaleString(*source+(*length),*extent,*xml_info->content ?
  2594. ">" : "/>");
  2595. if (xml_info->child != (XMLTreeInfo *) NULL)
  2596. *source=XMLTreeTagToXML(xml_info->child,source,length,extent,0,attributes);
  2597. else
  2598. *source=EncodePredefinedEntities(xml_info->content,-1,source,length,extent,
  2599. MagickFalse);
  2600. if ((*length+strlen(xml_info->tag)+MagickPathExtent) > *extent)
  2601. {
  2602. *extent=(*length)+strlen(xml_info->tag)+MagickPathExtent;
  2603. *source=(char *) ResizeQuantumMemory(*source,*extent,sizeof(**source));
  2604. if (*source == (char *) NULL)
  2605. return((char *) NULL);
  2606. }
  2607. if (*xml_info->content != '\0')
  2608. *length+=FormatLocaleString(*source+(*length),*extent,"</%s>",
  2609. xml_info->tag);
  2610. while ((offset < xml_info->offset) && (content[offset] != '\0'))
  2611. offset++;
  2612. if (xml_info->ordered != (XMLTreeInfo *) NULL)
  2613. content=XMLTreeTagToXML(xml_info->ordered,source,length,extent,offset,
  2614. attributes);
  2615. else
  2616. content=EncodePredefinedEntities(content+offset,-1,source,length,extent,
  2617. MagickFalse);
  2618. return(content);
  2619. }
  2620. MagickExport char *XMLTreeInfoToXML(XMLTreeInfo *xml_info)
  2621. {
  2622. char
  2623. *xml;
  2624. register char
  2625. *p,
  2626. *q;
  2627. register ssize_t
  2628. i;
  2629. size_t
  2630. extent,
  2631. length;
  2632. ssize_t
  2633. j,
  2634. k;
  2635. XMLTreeInfo
  2636. *ordered,
  2637. *parent;
  2638. XMLTreeRoot
  2639. *root;
  2640. assert(xml_info != (XMLTreeInfo *) NULL);
  2641. assert((xml_info->signature == MagickCoreSignature) ||
  2642. (((XMLTreeRoot *) xml_info)->signature == MagickCoreSignature));
  2643. if (xml_info->debug != MagickFalse)
  2644. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  2645. if (xml_info->tag == (char *) NULL)
  2646. return((char *) NULL);
  2647. xml=AcquireString((char *) NULL);
  2648. length=0;
  2649. extent=MagickPathExtent;
  2650. root=(XMLTreeRoot *) xml_info;
  2651. while (root->root.parent != (XMLTreeInfo *) NULL)
  2652. root=(XMLTreeRoot *) root->root.parent;
  2653. parent=xml_info->parent;
  2654. if (parent == (XMLTreeInfo *) NULL)
  2655. for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
  2656. {
  2657. /*
  2658. Pre-root processing instructions.
  2659. */
  2660. for (k=2; root->processing_instructions[i][k-1]; k++) ;
  2661. p=root->processing_instructions[i][1];
  2662. for (j=1; p != (char *) NULL; j++)
  2663. {
  2664. if (root->processing_instructions[i][k][j-1] == '>')
  2665. {
  2666. p=root->processing_instructions[i][j];
  2667. continue;
  2668. }
  2669. q=root->processing_instructions[i][0];
  2670. if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
  2671. {
  2672. extent=length+strlen(p)+strlen(q)+MagickPathExtent;
  2673. xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
  2674. if (xml == (char *) NULL)
  2675. return(xml);
  2676. }
  2677. length+=FormatLocaleString(xml+length,extent,"<?%s%s%s?>\n",q,
  2678. *p != '\0' ? " " : "",p);
  2679. p=root->processing_instructions[i][j];
  2680. }
  2681. }
  2682. ordered=xml_info->ordered;
  2683. xml_info->parent=(XMLTreeInfo *) NULL;
  2684. xml_info->ordered=(XMLTreeInfo *) NULL;
  2685. xml=XMLTreeTagToXML(xml_info,&xml,&length,&extent,0,root->attributes);
  2686. xml_info->parent=parent;
  2687. xml_info->ordered=ordered;
  2688. if (parent == (XMLTreeInfo *) NULL)
  2689. for (i=0; root->processing_instructions[i] != (char **) NULL; i++)
  2690. {
  2691. /*
  2692. Post-root processing instructions.
  2693. */
  2694. for (k=2; root->processing_instructions[i][k-1]; k++) ;
  2695. p=root->processing_instructions[i][1];
  2696. for (j=1; p != (char *) NULL; j++)
  2697. {
  2698. if (root->processing_instructions[i][k][j-1] == '<')
  2699. {
  2700. p=root->processing_instructions[i][j];
  2701. continue;
  2702. }
  2703. q=root->processing_instructions[i][0];
  2704. if ((length+strlen(p)+strlen(q)+MagickPathExtent) > extent)
  2705. {
  2706. extent=length+strlen(p)+strlen(q)+MagickPathExtent;
  2707. xml=(char *) ResizeQuantumMemory(xml,extent,sizeof(*xml));
  2708. if (xml == (char *) NULL)
  2709. return(xml);
  2710. }
  2711. length+=FormatLocaleString(xml+length,extent,"\n<?%s%s%s?>",q,
  2712. *p != '\0' ? " " : "",p);
  2713. p=root->processing_instructions[i][j];
  2714. }
  2715. }
  2716. return((char *) ResizeQuantumMemory(xml,length+1,sizeof(*xml)));
  2717. }