readfilemap.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. /* Functions close(2) and read(2) */
  34. #if ! defined(_WIN32) && ! defined(_WIN64)
  35. # include <unistd.h>
  36. #endif
  37. /* Function "read": */
  38. #if defined(_MSC_VER)
  39. # include <io.h>
  40. /* https://msdn.microsoft.com/en-us/library/wyssk1bs(v=vs.100).aspx */
  41. # define _EXPAT_read _read
  42. # define _EXPAT_read_count_t int
  43. # define _EXPAT_read_req_t unsigned int
  44. #else /* POSIX */
  45. /* http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html */
  46. # define _EXPAT_read read
  47. # define _EXPAT_read_count_t ssize_t
  48. # define _EXPAT_read_req_t size_t
  49. #endif
  50. #ifndef S_ISREG
  51. # ifndef S_IFREG
  52. # define S_IFREG _S_IFREG
  53. # endif
  54. # ifndef S_IFMT
  55. # define S_IFMT _S_IFMT
  56. # endif
  57. # define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
  58. #endif /* not S_ISREG */
  59. #ifndef O_BINARY
  60. # ifdef _O_BINARY
  61. # define O_BINARY _O_BINARY
  62. # else
  63. # define O_BINARY 0
  64. # endif
  65. #endif
  66. #include "xmltchar.h"
  67. #include "filemap.h"
  68. int
  69. filemap(const tchar *name,
  70. void (*processor)(const void *, size_t, const tchar *, void *arg),
  71. void *arg) {
  72. size_t nbytes;
  73. int fd;
  74. _EXPAT_read_count_t n;
  75. struct stat sb;
  76. void *p;
  77. fd = topen(name, O_RDONLY | O_BINARY);
  78. if (fd < 0) {
  79. tperror(name);
  80. return 0;
  81. }
  82. if (fstat(fd, &sb) < 0) {
  83. tperror(name);
  84. close(fd);
  85. return 0;
  86. }
  87. if (! S_ISREG(sb.st_mode)) {
  88. ftprintf(stderr, T("%s: not a regular file\n"), name);
  89. close(fd);
  90. return 0;
  91. }
  92. if (sb.st_size > XML_MAX_CHUNK_LEN) {
  93. close(fd);
  94. return 2; /* Cannot be passed to XML_Parse in one go */
  95. }
  96. nbytes = sb.st_size;
  97. /* malloc will return NULL with nbytes == 0, handle files with size 0 */
  98. if (nbytes == 0) {
  99. static const char c = '\0';
  100. processor(&c, 0, name, arg);
  101. close(fd);
  102. return 1;
  103. }
  104. p = malloc(nbytes);
  105. if (! p) {
  106. ftprintf(stderr, T("%s: out of memory\n"), name);
  107. close(fd);
  108. return 0;
  109. }
  110. n = _EXPAT_read(fd, p, (_EXPAT_read_req_t)nbytes);
  111. if (n < 0) {
  112. tperror(name);
  113. free(p);
  114. close(fd);
  115. return 0;
  116. }
  117. if (n != (_EXPAT_read_count_t)nbytes) {
  118. ftprintf(stderr, T("%s: read unexpected number of bytes\n"), name);
  119. free(p);
  120. close(fd);
  121. return 0;
  122. }
  123. processor(p, nbytes, name, arg);
  124. free(p);
  125. close(fd);
  126. return 1;
  127. }