xmlfile.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
  3. See the file copying.txt for copying permission.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stddef.h>
  8. #include <string.h>
  9. #include <fcntl.h>
  10. #include "xmlparse.h"
  11. #include "xmlfile.h"
  12. #include "xmltchar.h"
  13. #include "filemap.h"
  14. #if MSVCRT
  15. #include <io.h>
  16. #endif
  17. #ifdef _XOPEN_SOURCE 600
  18. #include <unistd.h>
  19. #endif
  20. #ifndef O_BINARY
  21. #ifdef _O_BINARY
  22. #define O_BINARY _O_BINARY
  23. #else
  24. #define O_BINARY 0
  25. #endif
  26. #endif
  27. #ifdef _DEBUG
  28. #define READ_SIZE 16
  29. #else
  30. #define READ_SIZE (1024*8)
  31. #endif
  32. typedef struct {
  33. XML_Parser parser;
  34. int *retPtr;
  35. } PROCESS_ARGS;
  36. static
  37. void reportError(XML_Parser parser, const XML_Char *filename)
  38. {
  39. int code = XML_GetErrorCode(parser);
  40. const XML_Char *message = XML_ErrorString(code);
  41. if (message)
  42. ftprintf(stdout, T("%s:%d:%d: %s\n"),
  43. filename,
  44. XML_GetErrorLineNumber(parser),
  45. XML_GetErrorColumnNumber(parser),
  46. message);
  47. else
  48. ftprintf(stderr, T("%s: (unknown message %d)\n"), filename, code);
  49. }
  50. static
  51. void processFile(const void *data,
  52. size_t size,
  53. const XML_Char *filename,
  54. void *args)
  55. {
  56. XML_Parser parser = ((PROCESS_ARGS *)args)->parser;
  57. int *retPtr = ((PROCESS_ARGS *)args)->retPtr;
  58. if (!XML_Parse(parser, data, size, 1)) {
  59. reportError(parser, filename);
  60. *retPtr = 0;
  61. }
  62. else
  63. *retPtr = 1;
  64. }
  65. #ifdef WIN32
  66. static
  67. int isAsciiLetter(XML_Char c)
  68. {
  69. return (T('a') <= c && c <= T('z')) || (T('A') <= c && c <= T('Z'));
  70. }
  71. #endif /* WIN32 */
  72. static
  73. const XML_Char *resolveSystemId(const XML_Char *base, const XML_Char *systemId, XML_Char **toFree)
  74. {
  75. XML_Char *s;
  76. *toFree = 0;
  77. if (!base
  78. || *systemId == T('/')
  79. #ifdef WIN32
  80. || *systemId == T('\\')
  81. || (isAsciiLetter(systemId[0]) && systemId[1] == T(':'))
  82. #endif
  83. )
  84. return systemId;
  85. *toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2)*sizeof(XML_Char));
  86. if (!*toFree)
  87. return systemId;
  88. tcscpy(*toFree, base);
  89. s = *toFree;
  90. if (tcsrchr(s, T('/')))
  91. s = tcsrchr(s, T('/')) + 1;
  92. #ifdef WIN32
  93. if (tcsrchr(s, T('\\')))
  94. s = tcsrchr(s, T('\\')) + 1;
  95. #endif
  96. tcscpy(s, systemId);
  97. return *toFree;
  98. }
  99. static
  100. int externalEntityRefFilemap(XML_Parser parser,
  101. const XML_Char *context,
  102. const XML_Char *base,
  103. const XML_Char *systemId,
  104. const XML_Char *publicId)
  105. {
  106. int result;
  107. XML_Char *s;
  108. const XML_Char *filename;
  109. XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
  110. PROCESS_ARGS args;
  111. args.retPtr = &result;
  112. args.parser = entParser;
  113. filename = resolveSystemId(base, systemId, &s);
  114. XML_SetBase(entParser, filename);
  115. if (!filemap(filename, processFile, &args))
  116. result = 0;
  117. free(s);
  118. XML_ParserFree(entParser);
  119. return result;
  120. }
  121. static
  122. int processStream(const XML_Char *filename, XML_Parser parser)
  123. {
  124. int fd = topen(filename, O_BINARY|O_RDONLY);
  125. if (fd < 0) {
  126. tperror(filename);
  127. return 0;
  128. }
  129. for (;;) {
  130. int nread;
  131. char *buf = XML_GetBuffer(parser, READ_SIZE);
  132. if (!buf) {
  133. close(fd);
  134. ftprintf(stderr, T("%s: out of memory\n"), filename);
  135. return 0;
  136. }
  137. nread = read(fd, buf, READ_SIZE);
  138. if (nread < 0) {
  139. tperror(filename);
  140. close(fd);
  141. return 0;
  142. }
  143. if (!XML_ParseBuffer(parser, nread, nread == 0)) {
  144. reportError(parser, filename);
  145. close(fd);
  146. return 0;
  147. }
  148. if (nread == 0) {
  149. close(fd);
  150. break;;
  151. }
  152. }
  153. return 1;
  154. }
  155. static
  156. int externalEntityRefStream(XML_Parser parser,
  157. const XML_Char *context,
  158. const XML_Char *base,
  159. const XML_Char *systemId,
  160. const XML_Char *publicId)
  161. {
  162. XML_Char *s;
  163. const XML_Char *filename;
  164. int ret;
  165. XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
  166. filename = resolveSystemId(base, systemId, &s);
  167. XML_SetBase(entParser, filename);
  168. ret = processStream(filename, entParser);
  169. free(s);
  170. XML_ParserFree(entParser);
  171. return ret;
  172. }
  173. int XML_ProcessFile(XML_Parser parser,
  174. const XML_Char *filename,
  175. unsigned flags)
  176. {
  177. int result;
  178. if (!XML_SetBase(parser, filename)) {
  179. ftprintf(stderr, T("%s: out of memory"), filename);
  180. exit(1);
  181. }
  182. if (flags & XML_EXTERNAL_ENTITIES)
  183. XML_SetExternalEntityRefHandler(parser,
  184. (flags & XML_MAP_FILE)
  185. ? externalEntityRefFilemap
  186. : externalEntityRefStream);
  187. if (flags & XML_MAP_FILE) {
  188. PROCESS_ARGS args;
  189. args.retPtr = &result;
  190. args.parser = parser;
  191. if (!filemap(filename, processFile, &args))
  192. result = 0;
  193. }
  194. else
  195. result = processStream(filename, parser);
  196. return result;
  197. }