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