readfilemap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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) 2001-2004 Fred L. Drake, Jr. <[email protected]>
  11. Copyright (c) 2002-2009 Karl Waclawek <[email protected]>
  12. Copyright (c) 2016-2017 Sebastian Pipping <[email protected]>
  13. Copyright (c) 2017 Rhodri James <[email protected]>
  14. Copyright (c) 2017 Franek Korta <[email protected]>
  15. Licensed under the MIT license:
  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. The above copyright notice and this permission notice shall be included
  24. in all copies or substantial portions of the Software.
  25. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  28. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  29. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  30. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  31. USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. /* Functions close(2) and read(2) */
  39. #if ! defined(_WIN32) && ! defined(_WIN64)
  40. # include <unistd.h>
  41. #endif
  42. /* Function "read": */
  43. #if defined(_MSC_VER)
  44. # include <io.h>
  45. /* https://msdn.microsoft.com/en-us/library/wyssk1bs(v=vs.100).aspx */
  46. # define _EXPAT_read _read
  47. # define _EXPAT_read_count_t int
  48. # define _EXPAT_read_req_t unsigned int
  49. #else /* POSIX */
  50. /* http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html */
  51. # define _EXPAT_read read
  52. # define _EXPAT_read_count_t ssize_t
  53. # define _EXPAT_read_req_t size_t
  54. #endif
  55. #ifndef S_ISREG
  56. # ifndef S_IFREG
  57. # define S_IFREG _S_IFREG
  58. # endif
  59. # ifndef S_IFMT
  60. # define S_IFMT _S_IFMT
  61. # endif
  62. # define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
  63. #endif /* not S_ISREG */
  64. #ifndef O_BINARY
  65. # ifdef _O_BINARY
  66. # define O_BINARY _O_BINARY
  67. # else
  68. # define O_BINARY 0
  69. # endif
  70. #endif
  71. #include "xmltchar.h"
  72. #include "filemap.h"
  73. int
  74. filemap(const tchar *name,
  75. void (*processor)(const void *, size_t, const tchar *, void *arg),
  76. void *arg) {
  77. size_t nbytes;
  78. int fd;
  79. _EXPAT_read_count_t n;
  80. struct stat sb;
  81. void *p;
  82. fd = topen(name, O_RDONLY | O_BINARY);
  83. if (fd < 0) {
  84. tperror(name);
  85. return 0;
  86. }
  87. if (fstat(fd, &sb) < 0) {
  88. tperror(name);
  89. close(fd);
  90. return 0;
  91. }
  92. if (! S_ISREG(sb.st_mode)) {
  93. ftprintf(stderr, T("%s: not a regular file\n"), name);
  94. close(fd);
  95. return 0;
  96. }
  97. if (sb.st_size > XML_MAX_CHUNK_LEN) {
  98. close(fd);
  99. return 2; /* Cannot be passed to XML_Parse in one go */
  100. }
  101. nbytes = sb.st_size;
  102. /* malloc will return NULL with nbytes == 0, handle files with size 0 */
  103. if (nbytes == 0) {
  104. static const char c = '\0';
  105. processor(&c, 0, name, arg);
  106. close(fd);
  107. return 1;
  108. }
  109. p = malloc(nbytes);
  110. if (! p) {
  111. ftprintf(stderr, T("%s: out of memory\n"), name);
  112. close(fd);
  113. return 0;
  114. }
  115. n = _EXPAT_read(fd, p, (_EXPAT_read_req_t)nbytes);
  116. if (n < 0) {
  117. tperror(name);
  118. free(p);
  119. close(fd);
  120. return 0;
  121. }
  122. if (n != (_EXPAT_read_count_t)nbytes) {
  123. ftprintf(stderr, T("%s: read unexpected number of bytes\n"), name);
  124. free(p);
  125. close(fd);
  126. return 0;
  127. }
  128. processor(p, nbytes, name, arg);
  129. free(p);
  130. close(fd);
  131. return 1;
  132. }