readfilemap.c 4.2 KB

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