element_declarations.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Read an XML document from standard input and print
  2. element declarations (if any) to standard output.
  3. It must be used with Expat compiled for UTF-8 output.
  4. __ __ _
  5. ___\ \/ /_ __ __ _| |_
  6. / _ \\ /| '_ \ / _` | __|
  7. | __// \| |_) | (_| | |_
  8. \___/_/\_\ .__/ \__,_|\__|
  9. |_| XML parser
  10. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  11. Copyright (c) 2001-2003 Fred L. Drake, Jr. <[email protected]>
  12. Copyright (c) 2004-2006 Karl Waclawek <[email protected]>
  13. Copyright (c) 2005-2007 Steven Solie <[email protected]>
  14. Copyright (c) 2016-2024 Sebastian Pipping <[email protected]>
  15. Copyright (c) 2017 Rhodri James <[email protected]>
  16. Copyright (c) 2019 Zhongyuan Zhou <[email protected]>
  17. Copyright (c) 2024 Hanno Böck <[email protected]>
  18. Licensed under the MIT license:
  19. Permission is hereby granted, free of charge, to any person obtaining
  20. a copy of this software and associated documentation files (the
  21. "Software"), to deal in the Software without restriction, including
  22. without limitation the rights to use, copy, modify, merge, publish,
  23. distribute, sublicense, and/or sell copies of the Software, and to permit
  24. persons to whom the Software is furnished to do so, subject to the
  25. following conditions:
  26. The above copyright notice and this permission notice shall be included
  27. in all copies or substantial portions of the Software.
  28. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  31. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  32. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  33. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  34. USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. */
  36. #include <stdbool.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <expat.h>
  40. #ifdef XML_LARGE_SIZE
  41. # define XML_FMT_INT_MOD "ll"
  42. #else
  43. # define XML_FMT_INT_MOD "l"
  44. #endif
  45. #ifdef XML_UNICODE_WCHAR_T
  46. # define XML_FMT_STR "ls"
  47. #else
  48. # define XML_FMT_STR "s"
  49. #endif
  50. // While traversing the XML_Content tree, we avoid recursion
  51. // to not be vulnerable to a denial of service attack.
  52. typedef struct StackStruct {
  53. const XML_Content *model;
  54. unsigned level;
  55. struct StackStruct *prev;
  56. } Stack;
  57. static Stack *
  58. stackPushMalloc(Stack *stackTop, const XML_Content *model, unsigned level) {
  59. Stack *const newStackTop = malloc(sizeof(Stack));
  60. if (! newStackTop) {
  61. return NULL;
  62. }
  63. newStackTop->model = model;
  64. newStackTop->level = level;
  65. newStackTop->prev = stackTop;
  66. return newStackTop;
  67. }
  68. static Stack *
  69. stackPopFree(Stack *stackTop) {
  70. Stack *const newStackTop = stackTop->prev;
  71. free(stackTop);
  72. return newStackTop;
  73. }
  74. static char *
  75. contentTypeName(enum XML_Content_Type contentType) {
  76. switch (contentType) {
  77. case XML_CTYPE_EMPTY:
  78. return "EMPTY";
  79. case XML_CTYPE_ANY:
  80. return "ANY";
  81. case XML_CTYPE_MIXED:
  82. return "MIXED";
  83. case XML_CTYPE_NAME:
  84. return "NAME";
  85. case XML_CTYPE_CHOICE:
  86. return "CHOICE";
  87. case XML_CTYPE_SEQ:
  88. return "SEQ";
  89. default:
  90. return "???";
  91. }
  92. }
  93. static char *
  94. contentQuantName(enum XML_Content_Quant contentQuant) {
  95. switch (contentQuant) {
  96. case XML_CQUANT_NONE:
  97. return "NONE";
  98. case XML_CQUANT_OPT:
  99. return "OPT";
  100. case XML_CQUANT_REP:
  101. return "REP";
  102. case XML_CQUANT_PLUS:
  103. return "PLUS";
  104. default:
  105. return "???";
  106. }
  107. }
  108. static void
  109. dumpContentModelElement(const XML_Content *model, unsigned level,
  110. const XML_Content *root) {
  111. // Indent
  112. unsigned u = 0;
  113. for (; u < level; u++) {
  114. printf(" ");
  115. }
  116. // Node
  117. printf("[%u] type=%s(%u), quant=%s(%u)", (unsigned)(model - root),
  118. contentTypeName(model->type), (unsigned int)model->type,
  119. contentQuantName(model->quant), (unsigned int)model->quant);
  120. if (model->name) {
  121. printf(", name=\"%" XML_FMT_STR "\"", model->name);
  122. } else {
  123. printf(", name=NULL");
  124. }
  125. printf(", numchildren=%u", model->numchildren);
  126. printf("\n");
  127. }
  128. static bool
  129. dumpContentModel(const XML_Char *name, const XML_Content *root) {
  130. printf("Element \"%" XML_FMT_STR "\":\n", name);
  131. Stack *stackTop = stackPushMalloc(NULL, root, 1);
  132. if (! stackTop) {
  133. return false;
  134. }
  135. while (stackTop) {
  136. const XML_Content *const model = stackTop->model;
  137. const unsigned level = stackTop->level;
  138. dumpContentModelElement(model, level, root);
  139. stackTop = stackPopFree(stackTop);
  140. for (size_t u = model->numchildren; u >= 1; u--) {
  141. Stack *const newStackTop
  142. = stackPushMalloc(stackTop, model->children + (u - 1), level + 1);
  143. if (! newStackTop) {
  144. // We ran out of memory, so let's free all memory allocated
  145. // earlier in this function, to be leak-clean:
  146. while (stackTop != NULL) {
  147. stackTop = stackPopFree(stackTop);
  148. }
  149. return false;
  150. }
  151. stackTop = newStackTop;
  152. }
  153. }
  154. printf("\n");
  155. return true;
  156. }
  157. static void XMLCALL
  158. handleElementDeclaration(void *userData, const XML_Char *name,
  159. XML_Content *model) {
  160. XML_Parser parser = (XML_Parser)userData;
  161. const bool success = dumpContentModel(name, model);
  162. XML_FreeContentModel(parser, model);
  163. if (! success) {
  164. XML_StopParser(parser, /* resumable= */ XML_FALSE);
  165. }
  166. }
  167. int
  168. main(void) {
  169. XML_Parser parser = XML_ParserCreate(NULL);
  170. int done;
  171. if (! parser) {
  172. fprintf(stderr, "Couldn't allocate memory for parser\n");
  173. return 1;
  174. }
  175. XML_SetUserData(parser, parser);
  176. XML_SetElementDeclHandler(parser, handleElementDeclaration);
  177. do {
  178. void *const buf = XML_GetBuffer(parser, BUFSIZ);
  179. if (! buf) {
  180. fprintf(stderr, "Couldn't allocate memory for buffer\n");
  181. XML_ParserFree(parser);
  182. return 1;
  183. }
  184. const size_t len = fread(buf, 1, BUFSIZ, stdin);
  185. if (ferror(stdin)) {
  186. fprintf(stderr, "Read error\n");
  187. XML_ParserFree(parser);
  188. return 1;
  189. }
  190. done = feof(stdin);
  191. if (XML_ParseBuffer(parser, (int)len, done) == XML_STATUS_ERROR) {
  192. enum XML_Error errorCode = XML_GetErrorCode(parser);
  193. if (errorCode == XML_ERROR_ABORTED) {
  194. errorCode = XML_ERROR_NO_MEMORY;
  195. }
  196. fprintf(stderr,
  197. "Parse error at line %" XML_FMT_INT_MOD "u:\n%" XML_FMT_STR "\n",
  198. XML_GetCurrentLineNumber(parser), XML_ErrorString(errorCode));
  199. XML_ParserFree(parser);
  200. return 1;
  201. }
  202. } while (! done);
  203. XML_ParserFree(parser);
  204. return 0;
  205. }