mz_strm_os_posix.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* mz_strm_posix.c -- Stream for filesystem access for posix/linux
  2. Version 2.9.2, February 12, 2020
  3. part of the MiniZip project
  4. Copyright (C) 2010-2020 Nathan Moinvaziri
  5. https://github.com/nmoinvaz/minizip
  6. Modifications for Zip64 support
  7. Copyright (C) 2009-2010 Mathias Svensson
  8. http://result42.com
  9. Copyright (C) 1998-2010 Gilles Vollant
  10. https://www.winimage.com/zLibDll/minizip.html
  11. This program is distributed under the terms of the same license as zlib.
  12. See the accompanying LICENSE file for the full text of the license.
  13. */
  14. #include "mz.h"
  15. #include "mz_strm.h"
  16. #include "mz_strm_os.h"
  17. #include <stdio.h> /* fopen, fread.. */
  18. #include <errno.h>
  19. /***************************************************************************/
  20. #define fopen64 fopen
  21. #ifndef MZ_FILE32_API
  22. # ifndef NO_FSEEKO
  23. # define ftello64 ftello
  24. # define fseeko64 fseeko
  25. # elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  26. # define ftello64 _ftelli64
  27. # define fseeko64 _fseeki64
  28. # endif
  29. #endif
  30. #ifndef ftello64
  31. # define ftello64 ftell
  32. #endif
  33. #ifndef fseeko64
  34. # define fseeko64 fseek
  35. #endif
  36. /***************************************************************************/
  37. static mz_stream_vtbl mz_stream_os_vtbl = {
  38. mz_stream_os_open,
  39. mz_stream_os_is_open,
  40. mz_stream_os_read,
  41. mz_stream_os_write,
  42. mz_stream_os_tell,
  43. mz_stream_os_seek,
  44. mz_stream_os_close,
  45. mz_stream_os_error,
  46. mz_stream_os_create,
  47. mz_stream_os_delete,
  48. NULL,
  49. NULL
  50. };
  51. /***************************************************************************/
  52. typedef struct mz_stream_posix_s
  53. {
  54. mz_stream stream;
  55. int32_t error;
  56. FILE *handle;
  57. } mz_stream_posix;
  58. /***************************************************************************/
  59. int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode)
  60. {
  61. mz_stream_posix *posix = (mz_stream_posix *)stream;
  62. const char *mode_fopen = NULL;
  63. if (path == NULL)
  64. return MZ_PARAM_ERROR;
  65. if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
  66. mode_fopen = "rb";
  67. else if (mode & MZ_OPEN_MODE_APPEND)
  68. mode_fopen = "r+b";
  69. else if (mode & MZ_OPEN_MODE_CREATE)
  70. mode_fopen = "wb";
  71. else
  72. return MZ_OPEN_ERROR;
  73. posix->handle = fopen64(path, mode_fopen);
  74. if (posix->handle == NULL)
  75. {
  76. posix->error = errno;
  77. return MZ_OPEN_ERROR;
  78. }
  79. if (mode & MZ_OPEN_MODE_APPEND)
  80. return mz_stream_os_seek(stream, 0, MZ_SEEK_END);
  81. return MZ_OK;
  82. }
  83. int32_t mz_stream_os_is_open(void *stream)
  84. {
  85. mz_stream_posix *posix = (mz_stream_posix*)stream;
  86. if (posix->handle == NULL)
  87. return MZ_OPEN_ERROR;
  88. return MZ_OK;
  89. }
  90. int32_t mz_stream_os_read(void *stream, void *buf, int32_t size)
  91. {
  92. mz_stream_posix *posix = (mz_stream_posix*)stream;
  93. int32_t read = (int32_t)fread(buf, 1, (size_t)size, posix->handle);
  94. if (read < size && ferror(posix->handle))
  95. {
  96. posix->error = errno;
  97. return MZ_READ_ERROR;
  98. }
  99. return read;
  100. }
  101. int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size)
  102. {
  103. mz_stream_posix *posix = (mz_stream_posix*)stream;
  104. int32_t written = (int32_t)fwrite(buf, 1, (size_t)size, posix->handle);
  105. if (written < size && ferror(posix->handle))
  106. {
  107. posix->error = errno;
  108. return MZ_WRITE_ERROR;
  109. }
  110. return written;
  111. }
  112. int64_t mz_stream_os_tell(void *stream)
  113. {
  114. mz_stream_posix *posix = (mz_stream_posix*)stream;
  115. int64_t position = ftello64(posix->handle);
  116. if (position == -1)
  117. {
  118. posix->error = errno;
  119. return MZ_TELL_ERROR;
  120. }
  121. return position;
  122. }
  123. int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin)
  124. {
  125. mz_stream_posix *posix = (mz_stream_posix*)stream;
  126. int32_t fseek_origin = 0;
  127. switch (origin)
  128. {
  129. case MZ_SEEK_CUR:
  130. fseek_origin = SEEK_CUR;
  131. break;
  132. case MZ_SEEK_END:
  133. fseek_origin = SEEK_END;
  134. break;
  135. case MZ_SEEK_SET:
  136. fseek_origin = SEEK_SET;
  137. break;
  138. default:
  139. return MZ_SEEK_ERROR;
  140. }
  141. if (fseeko64(posix->handle, offset, fseek_origin) != 0)
  142. {
  143. posix->error = errno;
  144. return MZ_SEEK_ERROR;
  145. }
  146. return MZ_OK;
  147. }
  148. int32_t mz_stream_os_close(void *stream)
  149. {
  150. mz_stream_posix *posix = (mz_stream_posix*)stream;
  151. int32_t closed = 0;
  152. if (posix->handle != NULL)
  153. {
  154. closed = fclose(posix->handle);
  155. posix->handle = NULL;
  156. }
  157. if (closed != 0)
  158. {
  159. posix->error = errno;
  160. return MZ_CLOSE_ERROR;
  161. }
  162. return MZ_OK;
  163. }
  164. int32_t mz_stream_os_error(void *stream)
  165. {
  166. mz_stream_posix *posix = (mz_stream_posix*)stream;
  167. return posix->error;
  168. }
  169. void *mz_stream_os_create(void **stream)
  170. {
  171. mz_stream_posix *posix = NULL;
  172. posix = (mz_stream_posix *)MZ_ALLOC(sizeof(mz_stream_posix));
  173. if (posix != NULL)
  174. {
  175. memset(posix, 0, sizeof(mz_stream_posix));
  176. posix->stream.vtbl = &mz_stream_os_vtbl;
  177. }
  178. if (stream != NULL)
  179. *stream = posix;
  180. return posix;
  181. }
  182. void mz_stream_os_delete(void **stream)
  183. {
  184. mz_stream_posix *posix = NULL;
  185. if (stream == NULL)
  186. return;
  187. posix = (mz_stream_posix *)*stream;
  188. if (posix != NULL)
  189. MZ_FREE(posix);
  190. *stream = NULL;
  191. }
  192. void *mz_stream_os_get_interface(void)
  193. {
  194. return (void *)&mz_stream_os_vtbl;
  195. }