zip_io.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**************************************************************************/
  2. /* zip_io.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "zip_io.h"
  31. #include "core/templates/local_vector.h"
  32. int godot_unzip_get_current_file_info(unzFile p_zip_file, unz_file_info64 &r_file_info, String &r_filepath) {
  33. const uLong short_file_path_buffer_size = 16384ul;
  34. char short_file_path_buffer[short_file_path_buffer_size];
  35. int err = unzGetCurrentFileInfo64(p_zip_file, &r_file_info, short_file_path_buffer, short_file_path_buffer_size, nullptr, 0, nullptr, 0);
  36. if (unlikely((err != UNZ_OK) || (r_file_info.size_filename > short_file_path_buffer_size))) {
  37. LocalVector<char> long_file_path_buffer;
  38. long_file_path_buffer.resize(r_file_info.size_filename);
  39. err = unzGetCurrentFileInfo64(p_zip_file, &r_file_info, long_file_path_buffer.ptr(), long_file_path_buffer.size(), nullptr, 0, nullptr, 0);
  40. if (err != UNZ_OK) {
  41. return err;
  42. }
  43. r_filepath = String::utf8(long_file_path_buffer.ptr(), r_file_info.size_filename);
  44. } else {
  45. r_filepath = String::utf8(short_file_path_buffer, r_file_info.size_filename);
  46. }
  47. return err;
  48. }
  49. int godot_unzip_locate_file(unzFile p_zip_file, const String &p_filepath, bool p_case_sensitive) {
  50. int err = unzGoToFirstFile(p_zip_file);
  51. while (err == UNZ_OK) {
  52. unz_file_info64 current_file_info;
  53. String current_filepath;
  54. err = godot_unzip_get_current_file_info(p_zip_file, current_file_info, current_filepath);
  55. if (err == UNZ_OK) {
  56. bool filepaths_are_equal = p_case_sensitive ? (p_filepath == current_filepath) : (p_filepath.nocasecmp_to(current_filepath) == 0);
  57. if (filepaths_are_equal) {
  58. return UNZ_OK;
  59. }
  60. err = unzGoToNextFile(p_zip_file);
  61. }
  62. }
  63. return err;
  64. }
  65. //
  66. void *zipio_open(voidpf opaque, const char *p_fname, int mode) {
  67. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  68. ERR_FAIL_NULL_V(fa, nullptr);
  69. String fname = String::utf8(p_fname);
  70. int file_access_mode = 0;
  71. if (mode & ZLIB_FILEFUNC_MODE_READ) {
  72. file_access_mode |= FileAccess::READ;
  73. }
  74. if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
  75. file_access_mode |= FileAccess::WRITE;
  76. }
  77. if (mode & ZLIB_FILEFUNC_MODE_CREATE) {
  78. file_access_mode |= FileAccess::WRITE_READ;
  79. }
  80. (*fa) = FileAccess::open(fname, file_access_mode);
  81. if (fa->is_null()) {
  82. return nullptr;
  83. }
  84. return opaque;
  85. }
  86. uLong zipio_read(voidpf opaque, voidpf stream, void *buf, uLong size) {
  87. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  88. ERR_FAIL_NULL_V(fa, 0);
  89. ERR_FAIL_COND_V(fa->is_null(), 0);
  90. return (*fa)->get_buffer((uint8_t *)buf, size);
  91. }
  92. uLong zipio_write(voidpf opaque, voidpf stream, const void *buf, uLong size) {
  93. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  94. ERR_FAIL_NULL_V(fa, 0);
  95. ERR_FAIL_COND_V(fa->is_null(), 0);
  96. (*fa)->store_buffer((uint8_t *)buf, size);
  97. return size;
  98. }
  99. long zipio_tell(voidpf opaque, voidpf stream) {
  100. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  101. ERR_FAIL_NULL_V(fa, 0);
  102. ERR_FAIL_COND_V(fa->is_null(), 0);
  103. return (*fa)->get_position();
  104. }
  105. long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
  106. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  107. ERR_FAIL_NULL_V(fa, 0);
  108. ERR_FAIL_COND_V(fa->is_null(), 0);
  109. uint64_t pos = offset;
  110. switch (origin) {
  111. case ZLIB_FILEFUNC_SEEK_CUR:
  112. pos = (*fa)->get_position() + offset;
  113. break;
  114. case ZLIB_FILEFUNC_SEEK_END:
  115. pos = (*fa)->get_length() + offset;
  116. break;
  117. default:
  118. break;
  119. }
  120. (*fa)->seek(pos);
  121. return 0;
  122. }
  123. int zipio_close(voidpf opaque, voidpf stream) {
  124. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  125. ERR_FAIL_NULL_V(fa, 0);
  126. ERR_FAIL_COND_V(fa->is_null(), 0);
  127. fa->unref();
  128. return 0;
  129. }
  130. int zipio_testerror(voidpf opaque, voidpf stream) {
  131. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  132. ERR_FAIL_NULL_V(fa, 1);
  133. ERR_FAIL_COND_V(fa->is_null(), 0);
  134. return (fa->is_valid() && (*fa)->get_error() != OK) ? 1 : 0;
  135. }
  136. voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) {
  137. voidpf ptr = memalloc((size_t)items * size);
  138. memset(ptr, 0, items * size);
  139. return ptr;
  140. }
  141. void zipio_free(voidpf opaque, voidpf address) {
  142. memfree(address);
  143. }
  144. zlib_filefunc_def zipio_create_io(Ref<FileAccess> *p_data) {
  145. zlib_filefunc_def io;
  146. io.opaque = (void *)p_data;
  147. io.zopen_file = zipio_open;
  148. io.zread_file = zipio_read;
  149. io.zwrite_file = zipio_write;
  150. io.ztell_file = zipio_tell;
  151. io.zseek_file = zipio_seek;
  152. io.zclose_file = zipio_close;
  153. io.zerror_file = zipio_testerror;
  154. io.alloc_mem = zipio_alloc;
  155. io.free_mem = zipio_free;
  156. return io;
  157. }