xmlfile.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
  2. See the file COPYING for copying permission.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stddef.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #ifdef _WIN32
  10. #include "winconfig.h"
  11. #elif defined(HAVE_EXPAT_CONFIG_H)
  12. #include <expat_config.h>
  13. #endif /* ndef _WIN32 */
  14. #include "expat.h"
  15. #include "internal.h" /* for UNUSED_P only */
  16. #include "xmlfile.h"
  17. #include "xmltchar.h"
  18. #include "filemap.h"
  19. #if defined(_MSC_VER)
  20. #include <io.h>
  21. #endif
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #ifndef O_BINARY
  26. #ifdef _O_BINARY
  27. #define O_BINARY _O_BINARY
  28. #else
  29. #define O_BINARY 0
  30. #endif
  31. #endif
  32. #ifdef _DEBUG
  33. #define READ_SIZE 16
  34. #else
  35. #define READ_SIZE (1024*8)
  36. #endif
  37. typedef struct {
  38. XML_Parser parser;
  39. int *retPtr;
  40. } PROCESS_ARGS;
  41. static int
  42. processStream(const XML_Char *filename, XML_Parser parser);
  43. static void
  44. reportError(XML_Parser parser, const XML_Char *filename)
  45. {
  46. enum XML_Error code = XML_GetErrorCode(parser);
  47. const XML_Char *message = XML_ErrorString(code);
  48. if (message)
  49. ftprintf(stdout, T("%s:%" XML_FMT_INT_MOD "u:%" XML_FMT_INT_MOD "u: %s\n"),
  50. filename,
  51. XML_GetErrorLineNumber(parser),
  52. XML_GetErrorColumnNumber(parser),
  53. message);
  54. else
  55. ftprintf(stderr, T("%s: (unknown message %d)\n"), filename, code);
  56. }
  57. /* This implementation will give problems on files larger than INT_MAX. */
  58. static void
  59. processFile(const void *data, size_t size,
  60. const XML_Char *filename, void *args)
  61. {
  62. XML_Parser parser = ((PROCESS_ARGS *)args)->parser;
  63. int *retPtr = ((PROCESS_ARGS *)args)->retPtr;
  64. if (XML_Parse(parser, (const char *)data, (int)size, 1) == XML_STATUS_ERROR) {
  65. reportError(parser, filename);
  66. *retPtr = 0;
  67. }
  68. else
  69. *retPtr = 1;
  70. }
  71. #if defined(_WIN32)
  72. static int
  73. isAsciiLetter(XML_Char c)
  74. {
  75. return (T('a') <= c && c <= T('z')) || (T('A') <= c && c <= T('Z'));
  76. }
  77. #endif /* _WIN32 */
  78. static const XML_Char *
  79. resolveSystemId(const XML_Char *base, const XML_Char *systemId,
  80. XML_Char **toFree)
  81. {
  82. XML_Char *s;
  83. *toFree = 0;
  84. if (!base
  85. || *systemId == T('/')
  86. #if defined(_WIN32)
  87. || *systemId == T('\\')
  88. || (isAsciiLetter(systemId[0]) && systemId[1] == T(':'))
  89. #endif
  90. )
  91. return systemId;
  92. *toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2)
  93. * sizeof(XML_Char));
  94. if (!*toFree)
  95. return systemId;
  96. tcscpy(*toFree, base);
  97. s = *toFree;
  98. if (tcsrchr(s, T('/')))
  99. s = tcsrchr(s, T('/')) + 1;
  100. #if defined(_WIN32)
  101. if (tcsrchr(s, T('\\')))
  102. s = tcsrchr(s, T('\\')) + 1;
  103. #endif
  104. tcscpy(s, systemId);
  105. return *toFree;
  106. }
  107. static int
  108. externalEntityRefFilemap(XML_Parser parser,
  109. const XML_Char *context,
  110. const XML_Char *base,
  111. const XML_Char *systemId,
  112. const XML_Char *UNUSED_P(publicId))
  113. {
  114. int result;
  115. XML_Char *s;
  116. const XML_Char *filename;
  117. XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
  118. int filemapRes;
  119. PROCESS_ARGS args;
  120. args.retPtr = &result;
  121. args.parser = entParser;
  122. filename = resolveSystemId(base, systemId, &s);
  123. XML_SetBase(entParser, filename);
  124. filemapRes = filemap(filename, processFile, &args);
  125. switch (filemapRes) {
  126. case 0:
  127. result = 0;
  128. break;
  129. case 2:
  130. ftprintf(stderr, T("%s: file too large for memory-mapping")
  131. T(", switching to streaming\n"), filename);
  132. result = processStream(filename, entParser);
  133. break;
  134. }
  135. free(s);
  136. XML_ParserFree(entParser);
  137. return result;
  138. }
  139. static int
  140. processStream(const XML_Char *filename, XML_Parser parser)
  141. {
  142. /* passing NULL for filename means read intput from stdin */
  143. int fd = 0; /* 0 is the fileno for stdin */
  144. if (filename != NULL) {
  145. fd = topen(filename, O_BINARY|O_RDONLY);
  146. if (fd < 0) {
  147. tperror(filename);
  148. return 0;
  149. }
  150. }
  151. for (;;) {
  152. int nread;
  153. char *buf = (char *)XML_GetBuffer(parser, READ_SIZE);
  154. if (!buf) {
  155. if (filename != NULL)
  156. close(fd);
  157. ftprintf(stderr, T("%s: out of memory\n"),
  158. filename != NULL ? filename : "xmlwf");
  159. return 0;
  160. }
  161. nread = read(fd, buf, READ_SIZE);
  162. if (nread < 0) {
  163. tperror(filename != NULL ? filename : "STDIN");
  164. if (filename != NULL)
  165. close(fd);
  166. return 0;
  167. }
  168. if (XML_ParseBuffer(parser, nread, nread == 0) == XML_STATUS_ERROR) {
  169. reportError(parser, filename != NULL ? filename : "STDIN");
  170. if (filename != NULL)
  171. close(fd);
  172. return 0;
  173. }
  174. if (nread == 0) {
  175. if (filename != NULL)
  176. close(fd);
  177. break;;
  178. }
  179. }
  180. return 1;
  181. }
  182. static int
  183. externalEntityRefStream(XML_Parser parser,
  184. const XML_Char *context,
  185. const XML_Char *base,
  186. const XML_Char *systemId,
  187. const XML_Char *UNUSED_P(publicId))
  188. {
  189. XML_Char *s;
  190. const XML_Char *filename;
  191. int ret;
  192. XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
  193. filename = resolveSystemId(base, systemId, &s);
  194. XML_SetBase(entParser, filename);
  195. ret = processStream(filename, entParser);
  196. free(s);
  197. XML_ParserFree(entParser);
  198. return ret;
  199. }
  200. int
  201. XML_ProcessFile(XML_Parser parser,
  202. const XML_Char *filename,
  203. unsigned flags)
  204. {
  205. int result;
  206. if (!XML_SetBase(parser, filename)) {
  207. ftprintf(stderr, T("%s: out of memory"), filename);
  208. exit(1);
  209. }
  210. if (flags & XML_EXTERNAL_ENTITIES)
  211. XML_SetExternalEntityRefHandler(parser,
  212. (flags & XML_MAP_FILE)
  213. ? externalEntityRefFilemap
  214. : externalEntityRefStream);
  215. if (flags & XML_MAP_FILE) {
  216. int filemapRes;
  217. PROCESS_ARGS args;
  218. args.retPtr = &result;
  219. args.parser = parser;
  220. filemapRes = filemap(filename, processFile, &args);
  221. switch (filemapRes) {
  222. case 0:
  223. result = 0;
  224. break;
  225. case 2:
  226. ftprintf(stderr, T("%s: file too large for memory-mapping")
  227. T(", switching to streaming\n"), filename);
  228. result = processStream(filename, parser);
  229. break;
  230. }
  231. }
  232. else
  233. result = processStream(filename, parser);
  234. return result;
  235. }