zip_io.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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;
  70. fname.parse_utf8(p_fname);
  71. int file_access_mode = 0;
  72. if (mode & ZLIB_FILEFUNC_MODE_READ) {
  73. file_access_mode |= FileAccess::READ;
  74. }
  75. if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
  76. file_access_mode |= FileAccess::WRITE;
  77. }
  78. if (mode & ZLIB_FILEFUNC_MODE_CREATE) {
  79. file_access_mode |= FileAccess::WRITE_READ;
  80. }
  81. (*fa) = FileAccess::open(fname, file_access_mode);
  82. if (fa->is_null()) {
  83. return nullptr;
  84. }
  85. return opaque;
  86. }
  87. uLong zipio_read(voidpf opaque, voidpf stream, void *buf, uLong size) {
  88. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  89. ERR_FAIL_NULL_V(fa, 0);
  90. ERR_FAIL_COND_V(fa->is_null(), 0);
  91. return (*fa)->get_buffer((uint8_t *)buf, size);
  92. }
  93. uLong zipio_write(voidpf opaque, voidpf stream, const void *buf, uLong size) {
  94. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  95. ERR_FAIL_NULL_V(fa, 0);
  96. ERR_FAIL_COND_V(fa->is_null(), 0);
  97. (*fa)->store_buffer((uint8_t *)buf, size);
  98. return size;
  99. }
  100. long zipio_tell(voidpf opaque, voidpf stream) {
  101. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  102. ERR_FAIL_NULL_V(fa, 0);
  103. ERR_FAIL_COND_V(fa->is_null(), 0);
  104. return (*fa)->get_position();
  105. }
  106. long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
  107. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  108. ERR_FAIL_NULL_V(fa, 0);
  109. ERR_FAIL_COND_V(fa->is_null(), 0);
  110. uint64_t pos = offset;
  111. switch (origin) {
  112. case ZLIB_FILEFUNC_SEEK_CUR:
  113. pos = (*fa)->get_position() + offset;
  114. break;
  115. case ZLIB_FILEFUNC_SEEK_END:
  116. pos = (*fa)->get_length() + offset;
  117. break;
  118. default:
  119. break;
  120. }
  121. (*fa)->seek(pos);
  122. return 0;
  123. }
  124. int zipio_close(voidpf opaque, voidpf stream) {
  125. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  126. ERR_FAIL_NULL_V(fa, 0);
  127. ERR_FAIL_COND_V(fa->is_null(), 0);
  128. fa->unref();
  129. return 0;
  130. }
  131. int zipio_testerror(voidpf opaque, voidpf stream) {
  132. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  133. ERR_FAIL_NULL_V(fa, 1);
  134. ERR_FAIL_COND_V(fa->is_null(), 0);
  135. return (fa->is_valid() && (*fa)->get_error() != OK) ? 1 : 0;
  136. }
  137. voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) {
  138. voidpf ptr = memalloc((size_t)items * size);
  139. memset(ptr, 0, items * size);
  140. return ptr;
  141. }
  142. void zipio_free(voidpf opaque, voidpf address) {
  143. memfree(address);
  144. }
  145. zlib_filefunc_def zipio_create_io(Ref<FileAccess> *p_data) {
  146. zlib_filefunc_def io;
  147. io.opaque = (void *)p_data;
  148. io.zopen_file = zipio_open;
  149. io.zread_file = zipio_read;
  150. io.zwrite_file = zipio_write;
  151. io.ztell_file = zipio_tell;
  152. io.zseek_file = zipio_seek;
  153. io.zclose_file = zipio_close;
  154. io.zerror_file = zipio_testerror;
  155. io.alloc_mem = zipio_alloc;
  156. io.free_mem = zipio_free;
  157. return io;
  158. }