win32filemap.c 3.7 KB

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