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