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 <coopercc@users.sourceforge.net>
11 Copyright (c) 2001-2004 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
12 Copyright (c) 2002-2009 Karl Waclawek <karl@waclawek.net>
13 Copyright (c) 2016-2017 Sebastian Pipping <sebastian@pipping.org>
14 Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
15 Copyright (c) 2017 Franek Korta <fkorta@gmail.com>
16 Copyright (c) 2022 Sean McBride <sean@rogue-research.com>
17 Copyright (c) 2025 Hanno Böck <hanno@gentoo.org>
18 Licensed under the MIT license:
19
20 Permission is hereby granted, free of charge, to any person obtaining
21 a copy of this software and associated documentation files (the
22 "Software"), to deal in the Software without restriction, including
23 without limitation the rights to use, copy, modify, merge, publish,
24 distribute, sublicense, and/or sell copies of the Software, and to permit
25 persons to whom the Software is furnished to do so, subject to the
26 following conditions:
27
28 The above copyright notice and this permission notice shall be included
29 in all copies or substantial portions of the Software.
30
31 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
34 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
35 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
36 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
37 USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45
46 /* Functions close(2) and read(2) */
47 #if ! defined(_WIN32) && ! defined(_WIN64)
48 # include <unistd.h>
49 #endif
50
51 /* Function "read": */
52 #if defined(_MSC_VER)
53 # include <io.h>
54 /* https://msdn.microsoft.com/en-us/library/wyssk1bs(v=vs.100).aspx */
55 # define EXPAT_read _read
56 # define EXPAT_read_count_t int
57 # define EXPAT_read_req_t unsigned int
58 #else /* POSIX */
59 /* https://pubs.opengroup.org/onlinepubs/009695399/functions/read.html */
60 # define EXPAT_read read
61 # define EXPAT_read_count_t ssize_t
62 # define EXPAT_read_req_t size_t
63 #endif
64
65 #ifndef S_ISREG
66 # ifndef S_IFREG
67 # define S_IFREG _S_IFREG
68 # endif
69 # ifndef S_IFMT
70 # define S_IFMT _S_IFMT
71 # endif
72 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
73 #endif /* not S_ISREG */
74
75 #ifndef O_BINARY
76 # ifdef _O_BINARY
77 # define O_BINARY _O_BINARY
78 # else
79 # define O_BINARY 0
80 # endif
81 #endif
82
83 #include "xmltchar.h"
84 #include "filemap.h"
85
86 int
filemap(const tchar * name,void (* processor)(const void *,size_t,const tchar *,void * arg),void * arg)87 filemap(const tchar *name,
88 void (*processor)(const void *, size_t, const tchar *, void *arg),
89 void *arg) {
90 size_t nbytes;
91 int fd;
92 EXPAT_read_count_t n;
93 struct stat sb;
94 void *p;
95
96 fd = topen(name, O_RDONLY | O_BINARY);
97 if (fd < 0) {
98 tperror(name);
99 return 0;
100 }
101 if (fstat(fd, &sb) < 0) {
102 tperror(name);
103 close(fd);
104 return 0;
105 }
106 if (! S_ISREG(sb.st_mode)) {
107 ftprintf(stderr, T("%s: not a regular file\n"), name);
108 close(fd);
109 return 0;
110 }
111 if (sb.st_size > XML_MAX_CHUNK_LEN) {
112 close(fd);
113 return 2; /* Cannot be passed to XML_Parse in one go */
114 }
115
116 nbytes = sb.st_size;
117 /* malloc will return NULL with nbytes == 0, handle files with size 0 */
118 if (nbytes == 0) {
119 static const char c = '\0';
120 processor(&c, 0, name, arg);
121 close(fd);
122 return 1;
123 }
124 p = malloc(nbytes);
125 if (! p) {
126 ftprintf(stderr, T("%s: out of memory\n"), name);
127 close(fd);
128 return 0;
129 }
130 n = EXPAT_read(fd, p, (EXPAT_read_req_t)nbytes);
131 if (n < 0) {
132 tperror(name);
133 free(p);
134 close(fd);
135 return 0;
136 }
137 if (n != (EXPAT_read_count_t)nbytes) {
138 ftprintf(stderr, T("%s: read unexpected number of bytes\n"), name);
139 free(p);
140 close(fd);
141 return 0;
142 }
143 processor(p, nbytes, name, arg);
144 free(p);
145 close(fd);
146 return 1;
147 }
148