1
0

win32filemap.c 3.7 KB

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