Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
5.9 KiB

  1. /*
  2. The contents of this file are subject to the Mozilla Public License
  3. Version 1.1 (the "License"); you may not use this file except in
  4. compliance with the License. You may obtain a copy of the License at
  5. http://www.mozilla.org/MPL/
  6. Software distributed under the License is distributed on an "AS IS"
  7. basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  8. License for the specific language governing rights and limitations
  9. under the License.
  10. The Original Code is expat.
  11. The Initial Developer of the Original Code is James Clark.
  12. Portions created by James Clark are Copyright (C) 1998, 1999
  13. James Clark. All Rights Reserved.
  14. Contributor(s):
  15. Alternatively, the contents of this file may be used under the terms
  16. of the GNU General Public License (the "GPL"), in which case the
  17. provisions of the GPL are applicable instead of those above. If you
  18. wish to allow use of your version of this file only under the terms of
  19. the GPL and not to allow others to use your version of this file under
  20. the MPL, indicate your decision by deleting the provisions above and
  21. replace them with the notice and other provisions required by the
  22. GPL. If you do not delete the provisions above, a recipient may use
  23. your version of this file under either the MPL or the GPL.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <stddef.h>
  28. #include <string.h>
  29. #include <fcntl.h>
  30. #include "xmlparse.h"
  31. #include "xmlfile.h"
  32. #include "xmltchar.h"
  33. #include "filemap.h"
  34. #ifdef _MSC_VER
  35. #include <io.h>
  36. #endif
  37. #ifdef _POSIX_SOURCE
  38. #include <unistd.h>
  39. #endif
  40. #ifndef O_BINARY
  41. #ifdef _O_BINARY
  42. #define O_BINARY _O_BINARY
  43. #else
  44. #define O_BINARY 0
  45. #endif
  46. #endif
  47. #ifdef _DEBUG
  48. #define READ_SIZE 16
  49. #else
  50. #define READ_SIZE (1024*8)
  51. #endif
  52. typedef struct {
  53. XML_Parser parser;
  54. int *retPtr;
  55. } PROCESS_ARGS;
  56. static
  57. void reportError(XML_Parser parser, const XML_Char *filename)
  58. {
  59. int code = XML_GetErrorCode(parser);
  60. const XML_Char *message = XML_ErrorString(code);
  61. if (message)
  62. ftprintf(stdout, T("%s:%d:%ld: %s\n"),
  63. filename,
  64. XML_GetErrorLineNumber(parser),
  65. XML_GetErrorColumnNumber(parser),
  66. message);
  67. else
  68. ftprintf(stderr, T("%s: (unknown message %d)\n"), filename, code);
  69. }
  70. static
  71. void processFile(const void *data,
  72. size_t size,
  73. const XML_Char *filename,
  74. void *args)
  75. {
  76. XML_Parser parser = ((PROCESS_ARGS *)args)->parser;
  77. int *retPtr = ((PROCESS_ARGS *)args)->retPtr;
  78. if (!XML_Parse(parser, data, size, 1)) {
  79. reportError(parser, filename);
  80. *retPtr = 0;
  81. }
  82. else
  83. *retPtr = 1;
  84. }
  85. static
  86. int isAsciiLetter(XML_Char c)
  87. {
  88. return (T('a') <= c && c <= T('z')) || (T('A') <= c && c <= T('Z'));
  89. }
  90. static
  91. const XML_Char *resolveSystemId(const XML_Char *base, const XML_Char *systemId, XML_Char **toFree)
  92. {
  93. XML_Char *s;
  94. *toFree = 0;
  95. if (!base
  96. || *systemId == T('/')
  97. #ifdef WIN32
  98. || *systemId == T('\\')
  99. || (isAsciiLetter(systemId[0]) && systemId[1] == T(':'))
  100. #endif
  101. )
  102. return systemId;
  103. *toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2)*sizeof(XML_Char));
  104. if (!*toFree)
  105. return systemId;
  106. tcscpy(*toFree, base);
  107. s = *toFree;
  108. if (tcsrchr(s, T('/')))
  109. s = tcsrchr(s, T('/')) + 1;
  110. #ifdef WIN32
  111. if (tcsrchr(s, T('\\')))
  112. s = tcsrchr(s, T('\\')) + 1;
  113. #endif
  114. tcscpy(s, systemId);
  115. return *toFree;
  116. }
  117. static
  118. int externalEntityRefFilemap(XML_Parser parser,
  119. const XML_Char *context,
  120. const XML_Char *base,
  121. const XML_Char *systemId,
  122. const XML_Char *publicId)
  123. {
  124. int result;
  125. XML_Char *s;
  126. const XML_Char *filename;
  127. XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
  128. PROCESS_ARGS args;
  129. args.retPtr = &result;
  130. args.parser = entParser;
  131. filename = resolveSystemId(base, systemId, &s);
  132. XML_SetBase(entParser, filename);
  133. if (!filemap(filename, processFile, &args))
  134. result = 0;
  135. free(s);
  136. XML_ParserFree(entParser);
  137. return result;
  138. }
  139. static
  140. int processStream(const XML_Char *filename, XML_Parser parser)
  141. {
  142. int fd = topen(filename, O_BINARY|O_RDONLY);
  143. if (fd < 0) {
  144. tperror(filename);
  145. return 0;
  146. }
  147. for (;;) {
  148. int nread;
  149. char *buf = XML_GetBuffer(parser, READ_SIZE);
  150. if (!buf) {
  151. close(fd);
  152. ftprintf(stderr, T("%s: out of memory\n"), filename);
  153. return 0;
  154. }
  155. nread = read(fd, buf, READ_SIZE);
  156. if (nread < 0) {
  157. tperror(filename);
  158. close(fd);
  159. return 0;
  160. }
  161. if (!XML_ParseBuffer(parser, nread, nread == 0)) {
  162. reportError(parser, filename);
  163. close(fd);
  164. return 0;
  165. }
  166. if (nread == 0) {
  167. close(fd);
  168. break;;
  169. }
  170. }
  171. return 1;
  172. }
  173. static
  174. int externalEntityRefStream(XML_Parser parser,
  175. const XML_Char *context,
  176. const XML_Char *base,
  177. const XML_Char *systemId,
  178. const XML_Char *publicId)
  179. {
  180. XML_Char *s;
  181. const XML_Char *filename;
  182. int ret;
  183. XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
  184. filename = resolveSystemId(base, systemId, &s);
  185. XML_SetBase(entParser, filename);
  186. ret = processStream(filename, entParser);
  187. free(s);
  188. XML_ParserFree(entParser);
  189. return ret;
  190. }
  191. int XML_ProcessFile(XML_Parser parser,
  192. const XML_Char *filename,
  193. unsigned flags)
  194. {
  195. int result;
  196. if (!XML_SetBase(parser, filename)) {
  197. ftprintf(stderr, T("%s: out of memory"), filename);
  198. exit(1);
  199. }
  200. if (flags & XML_EXTERNAL_ENTITIES)
  201. XML_SetExternalEntityRefHandler(parser,
  202. (flags & XML_MAP_FILE)
  203. ? externalEntityRefFilemap
  204. : externalEntityRefStream);
  205. if (flags & XML_MAP_FILE) {
  206. PROCESS_ARGS args;
  207. args.retPtr = &result;
  208. args.parser = parser;
  209. if (!filemap(filename, processFile, &args))
  210. result = 0;
  211. }
  212. else
  213. result = processStream(filename, parser);
  214. return result;
  215. }