readfilemap.c 3.7 KB

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