win32filemap.c 3.5 KB

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