xref: /freebsd/contrib/expat/xmlwf/readfilemap.c (revision 207d96dabfec14d7b3699747abb539ab3c1118ab)
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    SPDX-License-Identifier: MIT
40 */
41 
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 
48 /* Functions close(2) and read(2) */
49 #if ! defined(_WIN32) && ! defined(_WIN64)
50 #  include <unistd.h>
51 #endif
52 
53 /* Function "read": */
54 #if defined(_MSC_VER)
55 #  include <io.h>
56 /* https://msdn.microsoft.com/en-us/library/wyssk1bs(v=vs.100).aspx */
57 #  define EXPAT_read _read
58 #  define EXPAT_read_count_t int
59 #  define EXPAT_read_req_t unsigned int
60 #else /* POSIX */
61 /* https://pubs.opengroup.org/onlinepubs/009695399/functions/read.html */
62 #  define EXPAT_read read
63 #  define EXPAT_read_count_t ssize_t
64 #  define EXPAT_read_req_t size_t
65 #endif
66 
67 #ifndef S_ISREG
68 #  ifndef S_IFREG
69 #    define S_IFREG _S_IFREG
70 #  endif
71 #  ifndef S_IFMT
72 #    define S_IFMT _S_IFMT
73 #  endif
74 #  define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
75 #endif /* not S_ISREG */
76 
77 #ifndef O_BINARY
78 #  ifdef _O_BINARY
79 #    define O_BINARY _O_BINARY
80 #  else
81 #    define O_BINARY 0
82 #  endif
83 #endif
84 
85 #include "xmltchar.h"
86 #include "filemap.h"
87 
88 int
filemap(const tchar * name,void (* processor)(const void *,size_t,const tchar *,void * arg),void * arg)89 filemap(const tchar *name,
90         void (*processor)(const void *, size_t, const tchar *, void *arg),
91         void *arg) {
92   size_t nbytes;
93   int fd;
94   EXPAT_read_count_t n;
95   struct stat sb;
96   void *p;
97 
98   fd = topen(name, O_RDONLY | O_BINARY);
99   if (fd < 0) {
100     tperror(name);
101     return 0;
102   }
103   if (fstat(fd, &sb) < 0) {
104     tperror(name);
105     close(fd);
106     return 0;
107   }
108   if (! S_ISREG(sb.st_mode)) {
109     ftprintf(stderr, T("%s: not a regular file\n"), name);
110     close(fd);
111     return 0;
112   }
113   if (sb.st_size > XML_MAX_CHUNK_LEN) {
114     close(fd);
115     return 2; /* Cannot be passed to XML_Parse in one go */
116   }
117 
118   nbytes = sb.st_size;
119   /* malloc will return NULL with nbytes == 0, handle files with size 0 */
120   if (nbytes == 0) {
121     static const char c = '\0';
122     processor(&c, 0, name, arg);
123     close(fd);
124     return 1;
125   }
126   p = malloc(nbytes);
127   if (! p) {
128     ftprintf(stderr, T("%s: out of memory\n"), name);
129     close(fd);
130     return 0;
131   }
132   n = EXPAT_read(fd, p, (EXPAT_read_req_t)nbytes);
133   if (n < 0) {
134     tperror(name);
135     free(p);
136     close(fd);
137     return 0;
138   }
139   if (n != (EXPAT_read_count_t)nbytes) {
140     ftprintf(stderr, T("%s: read unexpected number of bytes\n"), name);
141     free(p);
142     close(fd);
143     return 0;
144   }
145   processor(p, nbytes, name, arg);
146   free(p);
147   close(fd);
148   return 1;
149 }
150