mz_strm_mem.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* mz_strm_mem.c -- Stream for memory access
  2. Version 2.9.2, February 12, 2020
  3. part of the MiniZip project
  4. This interface is designed to access memory rather than files.
  5. We do use a region of memory to put data in to and take it out of.
  6. Based on Unzip ioapi.c version 0.22, May 19th, 2003
  7. Copyright (C) 2010-2020 Nathan Moinvaziri
  8. https://github.com/nmoinvaz/minizip
  9. Copyright (C) 2003 Justin Fletcher
  10. Copyright (C) 1998-2003 Gilles Vollant
  11. https://www.winimage.com/zLibDll/minizip.html
  12. This program is distributed under the terms of the same license as zlib.
  13. See the accompanying LICENSE file for the full text of the license.
  14. */
  15. #include "mz.h"
  16. #include "mz_strm.h"
  17. #include "mz_strm_mem.h"
  18. /***************************************************************************/
  19. static mz_stream_vtbl mz_stream_mem_vtbl = {
  20. mz_stream_mem_open,
  21. mz_stream_mem_is_open,
  22. mz_stream_mem_read,
  23. mz_stream_mem_write,
  24. mz_stream_mem_tell,
  25. mz_stream_mem_seek,
  26. mz_stream_mem_close,
  27. mz_stream_mem_error,
  28. mz_stream_mem_create,
  29. mz_stream_mem_delete,
  30. NULL,
  31. NULL
  32. };
  33. /***************************************************************************/
  34. typedef struct mz_stream_mem_s {
  35. mz_stream stream;
  36. int32_t mode;
  37. uint8_t *buffer; /* Memory buffer pointer */
  38. int32_t size; /* Size of the memory buffer */
  39. int32_t limit; /* Furthest we've written */
  40. int32_t position; /* Current position in the memory */
  41. int32_t grow_size; /* Size to grow when full */
  42. } mz_stream_mem;
  43. /***************************************************************************/
  44. static int32_t mz_stream_mem_set_size(void *stream, int32_t size)
  45. {
  46. mz_stream_mem *mem = (mz_stream_mem *)stream;
  47. int32_t new_size = size;
  48. uint8_t *new_buf = NULL;
  49. new_buf = (uint8_t *)MZ_ALLOC((uint32_t)new_size);
  50. if (new_buf == NULL)
  51. return MZ_BUF_ERROR;
  52. if (mem->buffer)
  53. {
  54. memcpy(new_buf, mem->buffer, mem->size);
  55. MZ_FREE(mem->buffer);
  56. }
  57. mem->buffer = new_buf;
  58. mem->size = new_size;
  59. return MZ_OK;
  60. }
  61. int32_t mz_stream_mem_open(void *stream, const char *path, int32_t mode)
  62. {
  63. mz_stream_mem *mem = (mz_stream_mem *)stream;
  64. int32_t err = MZ_OK;
  65. MZ_UNUSED(path);
  66. mem->mode = mode;
  67. mem->limit = 0;
  68. mem->position = 0;
  69. if (mem->mode & MZ_OPEN_MODE_CREATE)
  70. err = mz_stream_mem_set_size(stream, mem->grow_size);
  71. else
  72. mem->limit = mem->size;
  73. return err;
  74. }
  75. int32_t mz_stream_mem_is_open(void *stream)
  76. {
  77. mz_stream_mem *mem = (mz_stream_mem *)stream;
  78. if (mem->buffer == NULL)
  79. return MZ_OPEN_ERROR;
  80. return MZ_OK;
  81. }
  82. int32_t mz_stream_mem_read(void *stream, void *buf, int32_t size)
  83. {
  84. mz_stream_mem *mem = (mz_stream_mem *)stream;
  85. if (size > mem->size - mem->position)
  86. size = mem->size - mem->position;
  87. if (mem->position + size > mem->limit)
  88. size = mem->limit - mem->position;
  89. if (size <= 0)
  90. return 0;
  91. memcpy(buf, mem->buffer + mem->position, size);
  92. mem->position += size;
  93. return size;
  94. }
  95. int32_t mz_stream_mem_write(void *stream, const void *buf, int32_t size)
  96. {
  97. mz_stream_mem *mem = (mz_stream_mem *)stream;
  98. int32_t new_size = 0;
  99. int32_t err = MZ_OK;
  100. if (size == 0)
  101. return size;
  102. if (size > mem->size - mem->position)
  103. {
  104. if (mem->mode & MZ_OPEN_MODE_CREATE)
  105. {
  106. new_size = mem->size;
  107. if (size < mem->grow_size)
  108. new_size += mem->grow_size;
  109. else
  110. new_size += size;
  111. err = mz_stream_mem_set_size(stream, new_size);
  112. if (err != MZ_OK)
  113. return err;
  114. }
  115. else
  116. {
  117. size = mem->size - mem->position;
  118. }
  119. }
  120. memcpy(mem->buffer + mem->position, buf, size);
  121. mem->position += size;
  122. if (mem->position > mem->limit)
  123. mem->limit = mem->position;
  124. return size;
  125. }
  126. int64_t mz_stream_mem_tell(void *stream)
  127. {
  128. mz_stream_mem *mem = (mz_stream_mem *)stream;
  129. return mem->position;
  130. }
  131. int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin)
  132. {
  133. mz_stream_mem *mem = (mz_stream_mem *)stream;
  134. int64_t new_pos = 0;
  135. int32_t err = MZ_OK;
  136. switch (origin)
  137. {
  138. case MZ_SEEK_CUR:
  139. new_pos = mem->position + offset;
  140. break;
  141. case MZ_SEEK_END:
  142. new_pos = mem->limit + offset;
  143. break;
  144. case MZ_SEEK_SET:
  145. new_pos = offset;
  146. break;
  147. default:
  148. return MZ_SEEK_ERROR;
  149. }
  150. if (new_pos > mem->size)
  151. {
  152. if ((mem->mode & MZ_OPEN_MODE_CREATE) == 0)
  153. return MZ_SEEK_ERROR;
  154. err = mz_stream_mem_set_size(stream, (int32_t)new_pos);
  155. if (err != MZ_OK)
  156. return err;
  157. }
  158. else if (new_pos < 0)
  159. {
  160. return MZ_SEEK_ERROR;
  161. }
  162. mem->position = (int32_t)new_pos;
  163. return MZ_OK;
  164. }
  165. int32_t mz_stream_mem_close(void *stream)
  166. {
  167. MZ_UNUSED(stream);
  168. /* We never return errors */
  169. return MZ_OK;
  170. }
  171. int32_t mz_stream_mem_error(void *stream)
  172. {
  173. MZ_UNUSED(stream);
  174. /* We never return errors */
  175. return MZ_OK;
  176. }
  177. void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size)
  178. {
  179. mz_stream_mem *mem = (mz_stream_mem *)stream;
  180. mem->buffer = (uint8_t *)buf;
  181. mem->size = size;
  182. mem->limit = size;
  183. }
  184. int32_t mz_stream_mem_get_buffer(void *stream, const void **buf)
  185. {
  186. return mz_stream_mem_get_buffer_at(stream, 0, buf);
  187. }
  188. int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void **buf)
  189. {
  190. mz_stream_mem *mem = (mz_stream_mem *)stream;
  191. if (buf == NULL || position < 0 || mem->size < position || mem->buffer == NULL)
  192. return MZ_SEEK_ERROR;
  193. *buf = mem->buffer + position;
  194. return MZ_OK;
  195. }
  196. int32_t mz_stream_mem_get_buffer_at_current(void *stream, const void **buf)
  197. {
  198. mz_stream_mem *mem = (mz_stream_mem *)stream;
  199. return mz_stream_mem_get_buffer_at(stream, mem->position, buf);
  200. }
  201. void mz_stream_mem_get_buffer_length(void *stream, int32_t *length)
  202. {
  203. mz_stream_mem *mem = (mz_stream_mem *)stream;
  204. *length = mem->limit;
  205. }
  206. void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit)
  207. {
  208. mz_stream_mem *mem = (mz_stream_mem *)stream;
  209. mem->limit = limit;
  210. }
  211. void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size)
  212. {
  213. mz_stream_mem *mem = (mz_stream_mem *)stream;
  214. mem->grow_size = grow_size;
  215. }
  216. void *mz_stream_mem_create(void **stream)
  217. {
  218. mz_stream_mem *mem = NULL;
  219. mem = (mz_stream_mem *)MZ_ALLOC(sizeof(mz_stream_mem));
  220. if (mem != NULL)
  221. {
  222. memset(mem, 0, sizeof(mz_stream_mem));
  223. mem->stream.vtbl = &mz_stream_mem_vtbl;
  224. mem->grow_size = 4096;
  225. }
  226. if (stream != NULL)
  227. *stream = mem;
  228. return mem;
  229. }
  230. void mz_stream_mem_delete(void **stream)
  231. {
  232. mz_stream_mem *mem = NULL;
  233. if (stream == NULL)
  234. return;
  235. mem = (mz_stream_mem *)*stream;
  236. if (mem != NULL)
  237. {
  238. if ((mem->mode & MZ_OPEN_MODE_CREATE) && (mem->buffer != NULL))
  239. MZ_FREE(mem->buffer);
  240. MZ_FREE(mem);
  241. }
  242. *stream = NULL;
  243. }
  244. void *mz_stream_mem_get_interface(void)
  245. {
  246. return (void *)&mz_stream_mem_vtbl;
  247. }