• 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-2017 Expat development team
11    Licensed under the MIT license:
12 
13    Permission is  hereby granted,  free of charge,  to any  person obtaining
14    a  copy  of  this  software   and  associated  documentation  files  (the
15    "Software"),  to  deal in  the  Software  without restriction,  including
16    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17    distribute, sublicense, and/or sell copies of the Software, and to permit
18    persons  to whom  the Software  is  furnished to  do so,  subject to  the
19    following conditions:
20 
21    The above copyright  notice and this permission notice  shall be included
22    in all copies or substantial portions of the Software.
23 
24    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30    USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32 
33 #define STRICT 1
34 #define WIN32_LEAN_AND_MEAN 1
35 
36 #ifdef XML_UNICODE_WCHAR_T
37 #  ifndef XML_UNICODE
38 #    define XML_UNICODE
39 #  endif
40 #endif
41 
42 #ifdef XML_UNICODE
43 #  define UNICODE
44 #  define _UNICODE
45 #endif /* XML_UNICODE */
46 #include <windows.h>
47 #include <stdio.h>
48 #include <tchar.h>
49 #include "filemap.h"
50 
51 static void win32perror(const TCHAR *);
52 
53 int
filemap(const TCHAR * name,void (* processor)(const void *,size_t,const TCHAR *,void * arg),void * arg)54 filemap(const TCHAR *name,
55         void (*processor)(const void *, size_t, const TCHAR *, void *arg),
56         void *arg) {
57   HANDLE f;
58   HANDLE m;
59   DWORD size;
60   DWORD sizeHi;
61   void *p;
62 
63   f = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
64                  FILE_FLAG_SEQUENTIAL_SCAN, NULL);
65   if (f == INVALID_HANDLE_VALUE) {
66     win32perror(name);
67     return 0;
68   }
69   size = GetFileSize(f, &sizeHi);
70   if (size == (DWORD)-1) {
71     win32perror(name);
72     CloseHandle(f);
73     return 0;
74   }
75   if (sizeHi || (size > XML_MAX_CHUNK_LEN)) {
76     CloseHandle(f);
77     return 2; /* Cannot be passed to XML_Parse in one go */
78   }
79   /* CreateFileMapping barfs on zero length files */
80   if (size == 0) {
81     static const char c = '\0';
82     processor(&c, 0, name, arg);
83     CloseHandle(f);
84     return 1;
85   }
86   m = CreateFileMapping(f, NULL, PAGE_READONLY, 0, 0, NULL);
87   if (m == NULL) {
88     win32perror(name);
89     CloseHandle(f);
90     return 0;
91   }
92   p = MapViewOfFile(m, FILE_MAP_READ, 0, 0, 0);
93   if (p == NULL) {
94     win32perror(name);
95     CloseHandle(m);
96     CloseHandle(f);
97     return 0;
98   }
99   processor(p, size, name, arg);
100   UnmapViewOfFile(p);
101   CloseHandle(m);
102   CloseHandle(f);
103   return 1;
104 }
105 
106 static void
win32perror(const TCHAR * s)107 win32perror(const TCHAR *s) {
108   LPVOID buf;
109   if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
110                     NULL, GetLastError(),
111                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0,
112                     NULL)) {
113     _ftprintf(stderr, _T("%s: %s"), s, buf);
114     fflush(stderr);
115     LocalFree(buf);
116   } else
117     _ftprintf(stderr, _T("%s: unknown Windows error\n"), s);
118 }
119