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