xref: /freebsd/contrib/expat/xmlwf/unixfilemap.c (revision 3078531de10dcae44b253a35125c949ff4235284)
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-2002 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
12    Copyright (c) 2006      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    Licensed under the MIT license:
16 
17    Permission is  hereby granted,  free of charge,  to any  person obtaining
18    a  copy  of  this  software   and  associated  documentation  files  (the
19    "Software"),  to  deal in  the  Software  without restriction,  including
20    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
21    distribute, sublicense, and/or sell copies of the Software, and to permit
22    persons  to whom  the Software  is  furnished to  do so,  subject to  the
23    following conditions:
24 
25    The above copyright  notice and this permission notice  shall be included
26    in all copies or substantial portions of the Software.
27 
28    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
29    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
30    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
31    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
32    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
33    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
34    USE OR OTHER DEALINGS IN THE SOFTWARE.
35 */
36 
37 #include <sys/types.h>
38 #include <sys/mman.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 
46 #ifndef MAP_FILE
47 #  define MAP_FILE 0
48 #endif
49 
50 #include "xmltchar.h"
51 #include "filemap.h"
52 
53 #ifdef XML_UNICODE_WCHAR_T
54 #  define XML_FMT_STR "ls"
55 #else
56 #  define XML_FMT_STR "s"
57 #endif
58 
59 int
60 filemap(const tchar *name,
61         void (*processor)(const void *, size_t, const tchar *, void *arg),
62         void *arg) {
63   int fd;
64   size_t nbytes;
65   struct stat sb;
66   void *p;
67 
68   fd = topen(name, O_RDONLY);
69   if (fd < 0) {
70     tperror(name);
71     return 0;
72   }
73   if (fstat(fd, &sb) < 0) {
74     tperror(name);
75     close(fd);
76     return 0;
77   }
78   if (! S_ISREG(sb.st_mode)) {
79     close(fd);
80     fprintf(stderr, "%" XML_FMT_STR ": not a regular file\n", name);
81     return 0;
82   }
83   if (sb.st_size > XML_MAX_CHUNK_LEN) {
84     close(fd);
85     return 2; /* Cannot be passed to XML_Parse in one go */
86   }
87 
88   nbytes = sb.st_size;
89   /* mmap fails for zero length files */
90   if (nbytes == 0) {
91     static const char c = '\0';
92     processor(&c, 0, name, arg);
93     close(fd);
94     return 1;
95   }
96   p = (void *)mmap((void *)0, (size_t)nbytes, PROT_READ, MAP_FILE | MAP_PRIVATE,
97                    fd, (off_t)0);
98   if (p == (void *)-1) {
99     tperror(name);
100     close(fd);
101     return 0;
102   }
103   processor(p, nbytes, name, arg);
104   munmap((void *)p, nbytes);
105   close(fd);
106   return 1;
107 }
108