readfilemap.c 4.3 KB

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