Imported Upstream version 2.2.5
[platform/upstream/expat.git] / xmlwf / win32filemap.c
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
54 filemap(const TCHAR *name,
55         void (*processor)(const void *, size_t, const TCHAR *, void *arg),
56         void *arg)
57 {
58   HANDLE f;
59   HANDLE m;
60   DWORD size;
61   DWORD sizeHi;
62   void *p;
63
64   f = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
65                           FILE_FLAG_SEQUENTIAL_SCAN, NULL);
66   if (f == INVALID_HANDLE_VALUE) {
67     win32perror(name);
68     return 0;
69   }
70   size = GetFileSize(f, &sizeHi);
71   if (size == (DWORD)-1) {
72     win32perror(name);
73     CloseHandle(f);
74     return 0;
75   }
76   if (sizeHi || (size > XML_MAX_CHUNK_LEN)) {
77     CloseHandle(f);
78     return 2;  /* Cannot be passed to XML_Parse in one go */
79   }
80   /* CreateFileMapping barfs on zero length files */
81   if (size == 0) {
82     static const char c = '\0';
83     processor(&c, 0, name, arg);
84     CloseHandle(f);
85     return 1;
86   }
87   m = CreateFileMapping(f, NULL, PAGE_READONLY, 0, 0, NULL);
88   if (m == NULL) {
89     win32perror(name);
90     CloseHandle(f);
91     return 0;
92   }
93   p = MapViewOfFile(m, FILE_MAP_READ, 0, 0, 0);
94   if (p == NULL) {
95     win32perror(name);
96     CloseHandle(m);
97     CloseHandle(f);
98     return 0;
99   }
100   processor(p, size, name, arg); 
101   UnmapViewOfFile(p);
102   CloseHandle(m);
103   CloseHandle(f);
104   return 1;
105 }
106
107 static void
108 win32perror(const TCHAR *s)
109 {
110   LPVOID buf;
111   if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
112                     | FORMAT_MESSAGE_FROM_SYSTEM,
113                     NULL,
114                     GetLastError(),
115                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
116                     (LPTSTR) &buf,
117                     0,
118                     NULL)) {
119     _ftprintf(stderr, _T("%s: %s"), s, buf);
120     fflush(stderr);
121     LocalFree(buf);
122   }
123   else
124     _ftprintf(stderr, _T("%s: unknown Windows error\n"), s);
125 }