Apache HTTPD
xmlfile.c
Go to the documentation of this file.
1/*
2 __ __ _
3 ___\ \/ /_ __ __ _| |_
4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
7 |_| XML parser
8
9 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10 Copyright (c) 2000 Clark Cooper <[email protected]>
11 Copyright (c) 2002-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-2023 Sebastian Pipping <[email protected]>
15 Copyright (c) 2017 Rhodri James <[email protected]>
16 Copyright (c) 2019 David Loffredo <[email protected]>
17 Copyright (c) 2021 Donghee Na <[email protected]>
18 Copyright (c) 2024 Hanno Böck <[email protected]>
19 Licensed under the MIT license:
20
21 Permission is hereby granted, free of charge, to any person obtaining
22 a copy of this software and associated documentation files (the
23 "Software"), to deal in the Software without restriction, including
24 without limitation the rights to use, copy, modify, merge, publish,
25 distribute, sublicense, and/or sell copies of the Software, and to permit
26 persons to whom the Software is furnished to do so, subject to the
27 following conditions:
28
29 The above copyright notice and this permission notice shall be included
30 in all copies or substantial portions of the Software.
31
32 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
35 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
36 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
37 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
38 USE OR OTHER DEALINGS IN THE SOFTWARE.
39*/
40
41#include "expat_config.h"
42
43#include <stdio.h>
44#include <stdlib.h>
45#include <stddef.h>
46#include <string.h>
47#include <fcntl.h>
48
49#ifdef _WIN32
50# include "winconfig.h"
51#endif
52
53#include "expat.h"
54#include "internal.h" /* for UNUSED_P only */
55#include "xmlfile.h"
56#include "xmltchar.h"
57#include "filemap.h"
58
59#if defined(_MSC_VER)
60# include <io.h>
61#endif
62
63#ifdef HAVE_UNISTD_H
64# include <unistd.h>
65#endif
66
67#ifndef O_BINARY
68# ifdef _O_BINARY
69# define O_BINARY _O_BINARY
70# else
71# define O_BINARY 0
72# endif
73#endif
74
75int g_read_size_bytes = 1024 * 8;
76
77typedef struct {
79 int *retPtr;
81
83
84static void
88 if (message)
90 T("%s") T(":%") T(XML_FMT_INT_MOD) T("u") T(":%")
91 T(XML_FMT_INT_MOD) T("u") T(": %s\n"),
94 else
95 ftprintf(stderr, T("%s: (unknown message %u)\n"), filename,
96 (unsigned int)code);
97}
98
99/* This implementation will give problems on files larger than INT_MAX. */
100static void
101processFile(const void *data, size_t size, const XML_Char *filename,
102 void *args) {
103 XML_Parser parser = ((PROCESS_ARGS *)args)->parser;
104 int *retPtr = ((PROCESS_ARGS *)args)->retPtr;
105 if (XML_Parse(parser, (const char *)data, (int)size, 1) == XML_STATUS_ERROR) {
107 *retPtr = 0;
108 } else
109 *retPtr = 1;
110}
111
112#if defined(_WIN32)
113
114static int
116 return (T('a') <= c && c <= T('z')) || (T('A') <= c && c <= T('Z'));
117}
118
119#endif /* _WIN32 */
120
121static const XML_Char *
122resolveSystemId(const XML_Char *base, const XML_Char *systemId,
123 XML_Char **toFree) {
124 XML_Char *s;
125 *toFree = 0;
126 if (! base || *systemId == T('/')
127#if defined(_WIN32)
128 || *systemId == T('\\')
129 || (isAsciiLetter(systemId[0]) && systemId[1] == T(':'))
130#endif
131 )
132 return systemId;
133 *toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2)
134 * sizeof(XML_Char));
135 if (! *toFree)
136 return systemId;
137 tcscpy(*toFree, base);
138 s = *toFree;
139 if (tcsrchr(s, T('/')))
140 s = tcsrchr(s, T('/')) + 1;
141#if defined(_WIN32)
142 if (tcsrchr(s, T('\\')))
143 s = tcsrchr(s, T('\\')) + 1;
144#endif
145 tcscpy(s, systemId);
146 return *toFree;
147}
148
149static int
151 const XML_Char *base, const XML_Char *systemId,
152 const XML_Char *publicId) {
153 int result;
154 XML_Char *s;
155 const XML_Char *filename;
157 int filemapRes;
159 UNUSED_P(publicId);
160 args.retPtr = &result;
161 args.parser = entParser;
162 filename = resolveSystemId(base, systemId, &s);
165 switch (filemapRes) {
166 case 0:
167 result = 0;
168 break;
169 case 2:
171 T("%s: file too large for memory-mapping")
172 T(", switching to streaming\n"),
173 filename);
175 break;
176 }
177 free(s);
179 return result;
180}
181
182static int
184 /* passing NULL for filename means read input from stdin */
185 int fd = 0; /* 0 is the fileno for stdin */
186
187 if (filename != NULL) {
189 if (fd < 0) {
191 return 0;
192 }
193 }
194 for (;;) {
195 int nread;
196 char *buf = (char *)XML_GetBuffer(parser, g_read_size_bytes);
197 if (! buf) {
198 if (filename != NULL)
199 close(fd);
200 ftprintf(stderr, T("%s: out of memory\n"),
201 filename != NULL ? filename : T("xmlwf"));
202 return 0;
203 }
204 nread = read(fd, buf, g_read_size_bytes);
205 if (nread < 0) {
206 tperror(filename != NULL ? filename : T("STDIN"));
207 if (filename != NULL)
208 close(fd);
209 return 0;
210 }
212 reportError(parser, filename != NULL ? filename : T("STDIN"));
213 if (filename != NULL)
214 close(fd);
215 return 0;
216 }
217 if (nread == 0) {
218 if (filename != NULL)
219 close(fd);
220 break;
221 ;
222 }
223 }
224 return 1;
225}
226
227static int
229 const XML_Char *base, const XML_Char *systemId,
230 const XML_Char *publicId) {
231 XML_Char *s;
232 const XML_Char *filename;
233 int ret;
235 UNUSED_P(publicId);
236 filename = resolveSystemId(base, systemId, &s);
239 free(s);
241 return ret;
242}
243
244int
246 int result;
247
248 if (! XML_SetBase(parser, filename)) {
249 ftprintf(stderr, T("%s: out of memory"), filename);
250 exit(1);
251 }
252
257 if (flags & XML_MAP_FILE) {
258 int filemapRes;
260 args.retPtr = &result;
261 args.parser = parser;
263 switch (filemapRes) {
264 case 0:
265 result = 0;
266 break;
267 case 2:
269 T("%s: file too large for memory-mapping")
270 T(", switching to streaming\n"),
271 filename);
273 break;
274 }
275 } else
277 return result;
278}
#define XML_FMT_INT_MOD
const XML_LChar * XML_ErrorString(enum XML_Error code)
Definition xmlparse.c:2429
#define XML_STATUS_ERROR
Definition expat.h:76
XML_Error
Definition expat.h:83
enum XML_Status XML_Parse(XML_Parser parser, const char *s, int len, int isFinal)
Definition xmlparse.c:1926
enum XML_Error XML_GetErrorCode(XML_Parser parser)
Definition xmlparse.c:2318
void XML_ParserFree(XML_Parser parser)
Definition xmlparse.c:1537
enum XML_Status XML_SetBase(XML_Parser parser, const XML_Char *base)
Definition xmlparse.c:1647
enum XML_Status XML_ParseBuffer(XML_Parser parser, int len, int isFinal)
Definition xmlparse.c:2037
void XML_SetExternalEntityRefHandler(XML_Parser parser, XML_ExternalEntityRefHandler handler)
Definition xmlparse.c:1838
#define XML_GetErrorColumnNumber
Definition expat.h:974
void * XML_GetBuffer(XML_Parser parser, int len)
Definition xmlparse.c:2108
XML_Parser XML_ExternalEntityParserCreate(XML_Parser parser, const XML_Char *context, const XML_Char *encoding)
Definition xmlparse.c:1354
#define XML_GetErrorLineNumber
Definition expat.h:973
char XML_Char
int filemap(const char *name, void(*processor)(const void *, size_t, const char *, void *arg), void *arg)
Definition readfilemap.c:86
ap_conf_vector_t * base
const unsigned char * buf
Definition util_md5.h:50
apr_md5_ctx_t * context
Definition util_md5.h:58
apr_file_t * fd
const char apr_ssize_t int flags
Definition apr_encode.h:168
apr_xml_parser ** parser
Definition apr_xml.h:228
apr_size_t size
void * data
apr_array_header_t ** result
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_size_t const char * filename
Definition apr_shm.h:72
const char * s
Definition apr_strings.h:95
const char const char *const * args
#define UNUSED_P(p)
Definition internal.h:137
return NULL
Definition mod_so.c:359
#define O_BINARY
Definition readfilemap.c:78
int * retPtr
Definition xmlfile.c:79
XML_Parser parser
Definition xmlfile.c:78
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray
static void processFile(const void *data, size_t size, const XML_Char *filename, void *args)
Definition xmlfile.c:101
static int processStream(const XML_Char *filename, XML_Parser parser)
Definition xmlfile.c:183
static void reportError(XML_Parser parser, const XML_Char *filename)
Definition xmlfile.c:85
int XML_ProcessFile(XML_Parser parser, const XML_Char *filename, unsigned flags)
Definition xmlfile.c:245
static int externalEntityRefFilemap(XML_Parser parser, const XML_Char *context, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId)
Definition xmlfile.c:150
int g_read_size_bytes
Definition xmlfile.c:75
static const XML_Char * resolveSystemId(const XML_Char *base, const XML_Char *systemId, XML_Char **toFree)
Definition xmlfile.c:122
static int externalEntityRefStream(XML_Parser parser, const XML_Char *context, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId)
Definition xmlfile.c:228
#define XML_EXTERNAL_ENTITIES
Definition xmlfile.h:37
#define XML_MAP_FILE
Definition xmlfile.h:36
#define tperror
Definition xmltchar.h:72
#define T(x)
Definition xmltchar.h:61
#define ftprintf
Definition xmltchar.h:62
#define tcscpy
Definition xmltchar.h:67
#define tcsrchr
Definition xmltchar.h:70
#define topen
Definition xmltchar.h:73
#define tcslen
Definition xmltchar.h:71