xref: /freebsd/contrib/expat/xmlwf/unixfilemap.c (revision 646a7fea)
15bb6a25fSPoul-Henning Kamp /*
25bb6a25fSPoul-Henning Kamp                             __  __            _
35bb6a25fSPoul-Henning Kamp                          ___\ \/ /_ __   __ _| |_
45bb6a25fSPoul-Henning Kamp                         / _ \\  /| '_ \ / _` | __|
55bb6a25fSPoul-Henning Kamp                        |  __//  \| |_) | (_| | |_
65bb6a25fSPoul-Henning Kamp                         \___/_/\_\ .__/ \__,_|\__|
75bb6a25fSPoul-Henning Kamp                                  |_| XML parser
85bb6a25fSPoul-Henning Kamp 
95bb6a25fSPoul-Henning Kamp    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
105bb6a25fSPoul-Henning Kamp    Copyright (c) 2000      Clark Cooper <coopercc@users.sourceforge.net>
115bb6a25fSPoul-Henning Kamp    Copyright (c) 2001-2002 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
125bb6a25fSPoul-Henning Kamp    Copyright (c) 2006      Karl Waclawek <karl@waclawek.net>
135bb6a25fSPoul-Henning Kamp    Copyright (c) 2016-2017 Sebastian Pipping <sebastian@pipping.org>
145bb6a25fSPoul-Henning Kamp    Copyright (c) 2017      Rhodri James <rhodri@wildebeest.org.uk>
155bb6a25fSPoul-Henning Kamp    Licensed under the MIT license:
165bb6a25fSPoul-Henning Kamp 
175bb6a25fSPoul-Henning Kamp    Permission is  hereby granted,  free of charge,  to any  person obtaining
185bb6a25fSPoul-Henning Kamp    a  copy  of  this  software   and  associated  documentation  files  (the
195bb6a25fSPoul-Henning Kamp    "Software"),  to  deal in  the  Software  without restriction,  including
205bb6a25fSPoul-Henning Kamp    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
215bb6a25fSPoul-Henning Kamp    distribute, sublicense, and/or sell copies of the Software, and to permit
225bb6a25fSPoul-Henning Kamp    persons  to whom  the Software  is  furnished to  do so,  subject to  the
235bb6a25fSPoul-Henning Kamp    following conditions:
245bb6a25fSPoul-Henning Kamp 
255bb6a25fSPoul-Henning Kamp    The above copyright  notice and this permission notice  shall be included
265bb6a25fSPoul-Henning Kamp    in all copies or substantial portions of the Software.
275bb6a25fSPoul-Henning Kamp 
285bb6a25fSPoul-Henning Kamp    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
295bb6a25fSPoul-Henning Kamp    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
305bb6a25fSPoul-Henning Kamp    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
315bb6a25fSPoul-Henning Kamp    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
325bb6a25fSPoul-Henning Kamp    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
335bb6a25fSPoul-Henning Kamp    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
345bb6a25fSPoul-Henning Kamp    USE OR OTHER DEALINGS IN THE SOFTWARE.
355bb6a25fSPoul-Henning Kamp */
365bb6a25fSPoul-Henning Kamp 
375bb6a25fSPoul-Henning Kamp #include <sys/types.h>
385bb6a25fSPoul-Henning Kamp #include <sys/mman.h>
395bb6a25fSPoul-Henning Kamp #include <sys/stat.h>
405bb6a25fSPoul-Henning Kamp #include <fcntl.h>
415bb6a25fSPoul-Henning Kamp #include <errno.h>
425bb6a25fSPoul-Henning Kamp #include <string.h>
435bb6a25fSPoul-Henning Kamp #include <stdio.h>
445bb6a25fSPoul-Henning Kamp #include <unistd.h>
455bb6a25fSPoul-Henning Kamp 
465bb6a25fSPoul-Henning Kamp #ifndef MAP_FILE
47220ed979SColeman Kane #  define MAP_FILE 0
48220ed979SColeman Kane #endif
49220ed979SColeman Kane 
50220ed979SColeman Kane #include "xmltchar.h"
51220ed979SColeman Kane #include "filemap.h"
52220ed979SColeman Kane 
53220ed979SColeman Kane #ifdef XML_UNICODE_WCHAR_T
545bb6a25fSPoul-Henning Kamp #  define XML_FMT_STR "ls"
555bb6a25fSPoul-Henning Kamp #else
565bb6a25fSPoul-Henning Kamp #  define XML_FMT_STR "s"
575bb6a25fSPoul-Henning Kamp #endif
585bb6a25fSPoul-Henning Kamp 
595bb6a25fSPoul-Henning Kamp int
filemap(const tchar * name,void (* processor)(const void *,size_t,const tchar *,void * arg),void * arg)605bb6a25fSPoul-Henning Kamp filemap(const tchar *name,
615bb6a25fSPoul-Henning Kamp         void (*processor)(const void *, size_t, const tchar *, void *arg),
625bb6a25fSPoul-Henning Kamp         void *arg) {
635bb6a25fSPoul-Henning Kamp   int fd;
645bb6a25fSPoul-Henning Kamp   size_t nbytes;
655bb6a25fSPoul-Henning Kamp   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