zip_io.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*************************************************************************/
  2. /* zip_io.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  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 */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef ZIP_IO_H
  31. #define ZIP_IO_H
  32. #include "os/copymem.h"
  33. #include "os/file_access.h"
  34. #include "thirdparty/minizip/unzip.h"
  35. #include "thirdparty/minizip/zip.h"
  36. static void *zipio_open(void *data, const char *p_fname, int mode) {
  37. FileAccess *&f = *(FileAccess **)data;
  38. String fname;
  39. fname.parse_utf8(p_fname);
  40. if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
  41. f = FileAccess::open(fname, FileAccess::WRITE);
  42. } else {
  43. f = FileAccess::open(fname, FileAccess::READ);
  44. }
  45. if (!f)
  46. return NULL;
  47. return data;
  48. };
  49. static uLong zipio_read(void *data, void *fdata, void *buf, uLong size) {
  50. FileAccess *f = *(FileAccess **)data;
  51. return f->get_buffer((uint8_t *)buf, size);
  52. };
  53. static uLong zipio_write(voidpf opaque, voidpf stream, const void *buf, uLong size) {
  54. FileAccess *f = *(FileAccess **)opaque;
  55. f->store_buffer((uint8_t *)buf, size);
  56. return size;
  57. };
  58. static long zipio_tell(voidpf opaque, voidpf stream) {
  59. FileAccess *f = *(FileAccess **)opaque;
  60. return f->get_pos();
  61. };
  62. static long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
  63. FileAccess *f = *(FileAccess **)opaque;
  64. int pos = offset;
  65. switch (origin) {
  66. case ZLIB_FILEFUNC_SEEK_CUR:
  67. pos = f->get_pos() + offset;
  68. break;
  69. case ZLIB_FILEFUNC_SEEK_END:
  70. pos = f->get_len() + offset;
  71. break;
  72. default:
  73. break;
  74. };
  75. f->seek(pos);
  76. return 0;
  77. };
  78. static int zipio_close(voidpf opaque, voidpf stream) {
  79. FileAccess *&f = *(FileAccess **)opaque;
  80. if (f) {
  81. f->close();
  82. f = NULL;
  83. }
  84. return 0;
  85. };
  86. static int zipio_testerror(voidpf opaque, voidpf stream) {
  87. FileAccess *f = *(FileAccess **)opaque;
  88. return (f && f->get_error() != OK) ? 1 : 0;
  89. };
  90. static voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) {
  91. voidpf ptr = memalloc(items * size);
  92. zeromem(ptr, items * size);
  93. return ptr;
  94. }
  95. static void zipio_free(voidpf opaque, voidpf address) {
  96. memfree(address);
  97. }
  98. static zlib_filefunc_def zipio_create_io_from_file(FileAccess **p_file) {
  99. zlib_filefunc_def io;
  100. io.opaque = p_file;
  101. io.zopen_file = zipio_open;
  102. io.zread_file = zipio_read;
  103. io.zwrite_file = zipio_write;
  104. io.ztell_file = zipio_tell;
  105. io.zseek_file = zipio_seek;
  106. io.zclose_file = zipio_close;
  107. io.zerror_file = zipio_testerror;
  108. io.alloc_mem = zipio_alloc;
  109. io.free_mem = zipio_free;
  110. return io;
  111. }
  112. #endif // ZIP_IO_H