4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
9 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10 Copyright (c) 2000-2017 Expat development team
11 Licensed under the MIT license:
13 Permission is hereby granted, free of charge, to any person obtaining
14 a copy of this software and associated documentation files (the
15 "Software"), to deal in the Software without restriction, including
16 without limitation the rights to use, copy, modify, merge, publish,
17 distribute, sublicense, and/or sell copies of the Software, and to permit
18 persons to whom the Software is furnished to do so, subject to the
21 The above copyright notice and this permission notice shall be included
22 in all copies or substantial portions of the Software.
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
29 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30 USE OR OTHER DEALINGS IN THE SOFTWARE.
40 # include "winconfig.h"
41 #elif defined(HAVE_EXPAT_CONFIG_H)
42 # include <expat_config.h>
43 #endif /* ndef _WIN32 */
46 #include "internal.h" /* for UNUSED_P only */
61 # define O_BINARY _O_BINARY
70 # define READ_SIZE (1024 * 8)
78 static int processStream(const XML_Char *filename, XML_Parser parser);
81 reportError(XML_Parser parser, const XML_Char *filename) {
82 enum XML_Error code = XML_GetErrorCode(parser);
83 const XML_Char *message = XML_ErrorString(code);
86 T("%s") T(":%") T(XML_FMT_INT_MOD) T("u") T(":%")
87 T(XML_FMT_INT_MOD) T("u") T(": %s\n"),
88 filename, XML_GetErrorLineNumber(parser),
89 XML_GetErrorColumnNumber(parser), message);
91 ftprintf(stderr, T("%s: (unknown message %d)\n"), filename, code);
94 /* This implementation will give problems on files larger than INT_MAX. */
96 processFile(const void *data, size_t size, const XML_Char *filename,
98 XML_Parser parser = ((PROCESS_ARGS *)args)->parser;
99 int *retPtr = ((PROCESS_ARGS *)args)->retPtr;
100 if (XML_Parse(parser, (const char *)data, (int)size, 1) == XML_STATUS_ERROR) {
101 reportError(parser, filename);
110 isAsciiLetter(XML_Char c) {
111 return (T('a') <= c && c <= T('z')) || (T('A') <= c && c <= T('Z'));
116 static const XML_Char *
117 resolveSystemId(const XML_Char *base, const XML_Char *systemId,
121 if (! base || *systemId == T('/')
123 || *systemId == T('\\')
124 || (isAsciiLetter(systemId[0]) && systemId[1] == T(':'))
128 *toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2)
132 tcscpy(*toFree, base);
134 if (tcsrchr(s, T('/')))
135 s = tcsrchr(s, T('/')) + 1;
137 if (tcsrchr(s, T('\\')))
138 s = tcsrchr(s, T('\\')) + 1;
145 externalEntityRefFilemap(XML_Parser parser, const XML_Char *context,
146 const XML_Char *base, const XML_Char *systemId,
147 const XML_Char *publicId) {
150 const XML_Char *filename;
151 XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
155 args.retPtr = &result;
156 args.parser = entParser;
157 filename = resolveSystemId(base, systemId, &s);
158 XML_SetBase(entParser, filename);
159 filemapRes = filemap(filename, processFile, &args);
160 switch (filemapRes) {
166 T("%s: file too large for memory-mapping")
167 T(", switching to streaming\n"),
169 result = processStream(filename, entParser);
173 XML_ParserFree(entParser);
178 processStream(const XML_Char *filename, XML_Parser parser) {
179 /* passing NULL for filename means read intput from stdin */
180 int fd = 0; /* 0 is the fileno for stdin */
182 if (filename != NULL) {
183 fd = topen(filename, O_BINARY | O_RDONLY);
191 char *buf = (char *)XML_GetBuffer(parser, READ_SIZE);
193 if (filename != NULL)
195 ftprintf(stderr, T("%s: out of memory\n"),
196 filename != NULL ? filename : T("xmlwf"));
199 nread = read(fd, buf, READ_SIZE);
201 tperror(filename != NULL ? filename : T("STDIN"));
202 if (filename != NULL)
206 if (XML_ParseBuffer(parser, nread, nread == 0) == XML_STATUS_ERROR) {
207 reportError(parser, filename != NULL ? filename : T("STDIN"));
208 if (filename != NULL)
213 if (filename != NULL)
223 externalEntityRefStream(XML_Parser parser, const XML_Char *context,
224 const XML_Char *base, const XML_Char *systemId,
225 const XML_Char *publicId) {
227 const XML_Char *filename;
229 XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
231 filename = resolveSystemId(base, systemId, &s);
232 XML_SetBase(entParser, filename);
233 ret = processStream(filename, entParser);
235 XML_ParserFree(entParser);
240 XML_ProcessFile(XML_Parser parser, const XML_Char *filename, unsigned flags) {
243 if (! XML_SetBase(parser, filename)) {
244 ftprintf(stderr, T("%s: out of memory"), filename);
248 if (flags & XML_EXTERNAL_ENTITIES)
249 XML_SetExternalEntityRefHandler(parser, (flags & XML_MAP_FILE)
250 ? externalEntityRefFilemap
251 : externalEntityRefStream);
252 if (flags & XML_MAP_FILE) {
255 args.retPtr = &result;
256 args.parser = parser;
257 filemapRes = filemap(filename, processFile, &args);
258 switch (filemapRes) {
264 T("%s: file too large for memory-mapping")
265 T(", switching to streaming\n"),
267 result = processStream(filename, parser);
271 result = processStream(filename, parser);