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