xref: /freebsd/contrib/expat/xmlwf/unixfilemap.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
10a48773fSEric van Gyzen /*
20a48773fSEric van Gyzen                             __  __            _
30a48773fSEric van Gyzen                          ___\ \/ /_ __   __ _| |_
40a48773fSEric van Gyzen                         / _ \\  /| '_ \ / _` | __|
50a48773fSEric van Gyzen                        |  __//  \| |_) | (_| | |_
60a48773fSEric van Gyzen                         \___/_/\_\ .__/ \__,_|\__|
70a48773fSEric van Gyzen                                  |_| XML parser
80a48773fSEric van Gyzen 
90a48773fSEric van Gyzen    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10*cc68614dSXin LI    Copyright (c) 2000      Clark Cooper <coopercc@users.sourceforge.net>
11*cc68614dSXin LI    Copyright (c) 2001-2002 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
12*cc68614dSXin LI    Copyright (c) 2006      Karl Waclawek <karl@waclawek.net>
13*cc68614dSXin LI    Copyright (c) 2016-2017 Sebastian Pipping <sebastian@pipping.org>
14*cc68614dSXin LI    Copyright (c) 2017      Rhodri James <rhodri@wildebeest.org.uk>
150a48773fSEric van Gyzen    Licensed under the MIT license:
160a48773fSEric van Gyzen 
170a48773fSEric van Gyzen    Permission is  hereby granted,  free of charge,  to any  person obtaining
180a48773fSEric van Gyzen    a  copy  of  this  software   and  associated  documentation  files  (the
190a48773fSEric van Gyzen    "Software"),  to  deal in  the  Software  without restriction,  including
200a48773fSEric van Gyzen    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
210a48773fSEric van Gyzen    distribute, sublicense, and/or sell copies of the Software, and to permit
220a48773fSEric van Gyzen    persons  to whom  the Software  is  furnished to  do so,  subject to  the
230a48773fSEric van Gyzen    following conditions:
240a48773fSEric van Gyzen 
250a48773fSEric van Gyzen    The above copyright  notice and this permission notice  shall be included
260a48773fSEric van Gyzen    in all copies or substantial portions of the Software.
270a48773fSEric van Gyzen 
280a48773fSEric van Gyzen    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
290a48773fSEric van Gyzen    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
300a48773fSEric van Gyzen    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
310a48773fSEric van Gyzen    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
320a48773fSEric van Gyzen    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
330a48773fSEric van Gyzen    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
340a48773fSEric van Gyzen    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
475bb6a25fSPoul-Henning Kamp #  define MAP_FILE 0
485bb6a25fSPoul-Henning Kamp #endif
495bb6a25fSPoul-Henning Kamp 
500a48773fSEric van Gyzen #include "xmltchar.h"
515bb6a25fSPoul-Henning Kamp #include "filemap.h"
525bb6a25fSPoul-Henning Kamp 
530a48773fSEric van Gyzen #ifdef XML_UNICODE_WCHAR_T
540a48773fSEric van Gyzen #  define XML_FMT_STR "ls"
550a48773fSEric van Gyzen #else
560a48773fSEric van Gyzen #  define XML_FMT_STR "s"
570a48773fSEric van Gyzen #endif
580a48773fSEric van Gyzen 
595bb6a25fSPoul-Henning Kamp int
filemap(const tchar * name,void (* processor)(const void *,size_t,const tchar *,void * arg),void * arg)600a48773fSEric van Gyzen filemap(const tchar *name,
610a48773fSEric van Gyzen         void (*processor)(const void *, size_t, const tchar *, void *arg),
626b2c1e49SXin LI         void *arg) {
635bb6a25fSPoul-Henning Kamp   int fd;
645bb6a25fSPoul-Henning Kamp   size_t nbytes;
655bb6a25fSPoul-Henning Kamp   struct stat sb;
665bb6a25fSPoul-Henning Kamp   void *p;
675bb6a25fSPoul-Henning Kamp 
680a48773fSEric van Gyzen   fd = topen(name, O_RDONLY);
695bb6a25fSPoul-Henning Kamp   if (fd < 0) {
700a48773fSEric van Gyzen     tperror(name);
715bb6a25fSPoul-Henning Kamp     return 0;
725bb6a25fSPoul-Henning Kamp   }
735bb6a25fSPoul-Henning Kamp   if (fstat(fd, &sb) < 0) {
740a48773fSEric van Gyzen     tperror(name);
755bb6a25fSPoul-Henning Kamp     close(fd);
765bb6a25fSPoul-Henning Kamp     return 0;
775bb6a25fSPoul-Henning Kamp   }
785bb6a25fSPoul-Henning Kamp   if (! S_ISREG(sb.st_mode)) {
795bb6a25fSPoul-Henning Kamp     close(fd);
800a48773fSEric van Gyzen     fprintf(stderr, "%" XML_FMT_STR ": not a regular file\n", name);
815bb6a25fSPoul-Henning Kamp     return 0;
825bb6a25fSPoul-Henning Kamp   }
830a48773fSEric van Gyzen   if (sb.st_size > XML_MAX_CHUNK_LEN) {
840a48773fSEric van Gyzen     close(fd);
850a48773fSEric van Gyzen     return 2; /* Cannot be passed to XML_Parse in one go */
860a48773fSEric van Gyzen   }
875bb6a25fSPoul-Henning Kamp 
885bb6a25fSPoul-Henning Kamp   nbytes = sb.st_size;
89220ed979SColeman Kane   /* mmap fails for zero length files */
90220ed979SColeman Kane   if (nbytes == 0) {
91220ed979SColeman Kane     static const char c = '\0';
92220ed979SColeman Kane     processor(&c, 0, name, arg);
93220ed979SColeman Kane     close(fd);
94220ed979SColeman Kane     return 1;
95220ed979SColeman Kane   }
966b2c1e49SXin LI   p = (void *)mmap((void *)0, (size_t)nbytes, PROT_READ, MAP_FILE | MAP_PRIVATE,
976b2c1e49SXin LI                    fd, (off_t)0);
985bb6a25fSPoul-Henning Kamp   if (p == (void *)-1) {
990a48773fSEric van Gyzen     tperror(name);
1005bb6a25fSPoul-Henning Kamp     close(fd);
1015bb6a25fSPoul-Henning Kamp     return 0;
1025bb6a25fSPoul-Henning Kamp   }
1035bb6a25fSPoul-Henning Kamp   processor(p, nbytes, name, arg);
104be8aff81SXin LI   munmap((void *)p, nbytes);
1055bb6a25fSPoul-Henning Kamp   close(fd);
1065bb6a25fSPoul-Henning Kamp   return 1;
1075bb6a25fSPoul-Henning Kamp }
108