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