file_access_zip.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**************************************************************************/
  2. /* file_access_zip.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. #ifdef MINIZIP_ENABLED
  31. #include "file_access_zip.h"
  32. #include "core/io/file_access.h"
  33. ZipArchive *ZipArchive::instance = nullptr;
  34. extern "C" {
  35. struct ZipData {
  36. Ref<FileAccess> f;
  37. };
  38. static void *godot_open(voidpf opaque, const char *p_fname, int mode) {
  39. if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
  40. return nullptr;
  41. }
  42. Ref<FileAccess> f = FileAccess::open(String::utf8(p_fname), FileAccess::READ);
  43. ERR_FAIL_COND_V(f.is_null(), nullptr);
  44. ZipData *zd = memnew(ZipData);
  45. zd->f = f;
  46. return zd;
  47. }
  48. static uLong godot_read(voidpf opaque, voidpf stream, void *buf, uLong size) {
  49. ZipData *zd = (ZipData *)stream;
  50. zd->f->get_buffer((uint8_t *)buf, size);
  51. return size;
  52. }
  53. static uLong godot_write(voidpf opaque, voidpf stream, const void *buf, uLong size) {
  54. return 0;
  55. }
  56. static long godot_tell(voidpf opaque, voidpf stream) {
  57. ZipData *zd = (ZipData *)stream;
  58. return zd->f->get_position();
  59. }
  60. static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
  61. ZipData *zd = (ZipData *)stream;
  62. uint64_t pos = offset;
  63. switch (origin) {
  64. case ZLIB_FILEFUNC_SEEK_CUR:
  65. pos = zd->f->get_position() + offset;
  66. break;
  67. case ZLIB_FILEFUNC_SEEK_END:
  68. pos = zd->f->get_length() + offset;
  69. break;
  70. default:
  71. break;
  72. }
  73. zd->f->seek(pos);
  74. return 0;
  75. }
  76. static int godot_close(voidpf opaque, voidpf stream) {
  77. ZipData *zd = (ZipData *)stream;
  78. memdelete(zd);
  79. return 0;
  80. }
  81. static int godot_testerror(voidpf opaque, voidpf stream) {
  82. ZipData *zd = (ZipData *)stream;
  83. return zd->f->get_error() != OK ? 1 : 0;
  84. }
  85. static voidpf godot_alloc(voidpf opaque, uInt items, uInt size) {
  86. return memalloc((size_t)items * size);
  87. }
  88. static void godot_free(voidpf opaque, voidpf address) {
  89. memfree(address);
  90. }
  91. } // extern "C"
  92. void ZipArchive::close_handle(unzFile p_file) const {
  93. ERR_FAIL_NULL_MSG(p_file, "Cannot close a file if none is open.");
  94. unzCloseCurrentFile(p_file);
  95. unzClose(p_file);
  96. }
  97. unzFile ZipArchive::get_file_handle(const String &p_file) const {
  98. ERR_FAIL_COND_V_MSG(!file_exists(p_file), nullptr, "File '" + p_file + " doesn't exist.");
  99. File file = files[p_file];
  100. zlib_filefunc_def io;
  101. memset(&io, 0, sizeof(io));
  102. io.opaque = nullptr;
  103. io.zopen_file = godot_open;
  104. io.zread_file = godot_read;
  105. io.zwrite_file = godot_write;
  106. io.ztell_file = godot_tell;
  107. io.zseek_file = godot_seek;
  108. io.zclose_file = godot_close;
  109. io.zerror_file = godot_testerror;
  110. io.alloc_mem = godot_alloc;
  111. io.free_mem = godot_free;
  112. unzFile pkg = unzOpen2(packages[file.package].filename.utf8().get_data(), &io);
  113. ERR_FAIL_NULL_V_MSG(pkg, nullptr, "Cannot open file '" + packages[file.package].filename + "'.");
  114. int unz_err = unzGoToFilePos(pkg, &file.file_pos);
  115. if (unz_err != UNZ_OK || unzOpenCurrentFile(pkg) != UNZ_OK) {
  116. unzClose(pkg);
  117. ERR_FAIL_V(nullptr);
  118. }
  119. return pkg;
  120. }
  121. bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset = 0) {
  122. // load with offset feature only supported for PCK files
  123. ERR_FAIL_COND_V_MSG(p_offset != 0, false, "Invalid PCK data. Note that loading files with a non-zero offset isn't supported with ZIP archives.");
  124. if (p_path.get_extension().nocasecmp_to("zip") != 0 && p_path.get_extension().nocasecmp_to("pcz") != 0) {
  125. return false;
  126. }
  127. zlib_filefunc_def io;
  128. memset(&io, 0, sizeof(io));
  129. io.opaque = nullptr;
  130. io.zopen_file = godot_open;
  131. io.zread_file = godot_read;
  132. io.zwrite_file = godot_write;
  133. io.ztell_file = godot_tell;
  134. io.zseek_file = godot_seek;
  135. io.zclose_file = godot_close;
  136. io.zerror_file = godot_testerror;
  137. unzFile zfile = unzOpen2(p_path.utf8().get_data(), &io);
  138. ERR_FAIL_NULL_V(zfile, false);
  139. unz_global_info64 gi;
  140. int err = unzGetGlobalInfo64(zfile, &gi);
  141. ERR_FAIL_COND_V(err != UNZ_OK, false);
  142. Package pkg;
  143. pkg.filename = p_path;
  144. pkg.zfile = zfile;
  145. packages.push_back(pkg);
  146. int pkg_num = packages.size() - 1;
  147. for (uint64_t i = 0; i < gi.number_entry; i++) {
  148. char filename_inzip[256];
  149. unz_file_info64 file_info;
  150. err = unzGetCurrentFileInfo64(zfile, &file_info, filename_inzip, sizeof(filename_inzip), nullptr, 0, nullptr, 0);
  151. ERR_CONTINUE(err != UNZ_OK);
  152. File f;
  153. f.package = pkg_num;
  154. unzGetFilePos(zfile, &f.file_pos);
  155. String fname = String("res://") + String::utf8(filename_inzip);
  156. files[fname] = f;
  157. uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  158. PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files, false);
  159. //printf("packed data add path %s, %s\n", p_name.utf8().get_data(), fname.utf8().get_data());
  160. if ((i + 1) < gi.number_entry) {
  161. unzGoToNextFile(zfile);
  162. }
  163. }
  164. return true;
  165. }
  166. bool ZipArchive::file_exists(const String &p_name) const {
  167. return files.has(p_name);
  168. }
  169. Ref<FileAccess> ZipArchive::get_file(const String &p_path, PackedData::PackedFile *p_file) {
  170. return memnew(FileAccessZip(p_path, *p_file));
  171. }
  172. ZipArchive *ZipArchive::get_singleton() {
  173. if (instance == nullptr) {
  174. instance = memnew(ZipArchive);
  175. }
  176. return instance;
  177. }
  178. ZipArchive::ZipArchive() {
  179. instance = this;
  180. }
  181. ZipArchive::~ZipArchive() {
  182. for (int i = 0; i < packages.size(); i++) {
  183. unzClose(packages[i].zfile);
  184. }
  185. packages.clear();
  186. }
  187. Error FileAccessZip::open_internal(const String &p_path, int p_mode_flags) {
  188. _close();
  189. ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, FAILED);
  190. ZipArchive *arch = ZipArchive::get_singleton();
  191. ERR_FAIL_NULL_V(arch, FAILED);
  192. zfile = arch->get_file_handle(p_path);
  193. ERR_FAIL_NULL_V(zfile, FAILED);
  194. int err = unzGetCurrentFileInfo64(zfile, &file_info, nullptr, 0, nullptr, 0, nullptr, 0);
  195. ERR_FAIL_COND_V(err != UNZ_OK, FAILED);
  196. return OK;
  197. }
  198. void FileAccessZip::_close() {
  199. if (!zfile) {
  200. return;
  201. }
  202. ZipArchive *arch = ZipArchive::get_singleton();
  203. ERR_FAIL_NULL(arch);
  204. arch->close_handle(zfile);
  205. zfile = nullptr;
  206. }
  207. bool FileAccessZip::is_open() const {
  208. return zfile != nullptr;
  209. }
  210. void FileAccessZip::seek(uint64_t p_position) {
  211. ERR_FAIL_NULL(zfile);
  212. unzSeekCurrentFile(zfile, p_position);
  213. }
  214. void FileAccessZip::seek_end(int64_t p_position) {
  215. ERR_FAIL_NULL(zfile);
  216. unzSeekCurrentFile(zfile, get_length() + p_position);
  217. }
  218. uint64_t FileAccessZip::get_position() const {
  219. ERR_FAIL_NULL_V(zfile, 0);
  220. return unztell64(zfile);
  221. }
  222. uint64_t FileAccessZip::get_length() const {
  223. ERR_FAIL_NULL_V(zfile, 0);
  224. return file_info.uncompressed_size;
  225. }
  226. bool FileAccessZip::eof_reached() const {
  227. ERR_FAIL_NULL_V(zfile, true);
  228. return at_eof;
  229. }
  230. uint8_t FileAccessZip::get_8() const {
  231. uint8_t ret = 0;
  232. get_buffer(&ret, 1);
  233. return ret;
  234. }
  235. uint64_t FileAccessZip::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  236. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  237. ERR_FAIL_NULL_V(zfile, -1);
  238. at_eof = unzeof(zfile);
  239. if (at_eof) {
  240. return 0;
  241. }
  242. int64_t read = unzReadCurrentFile(zfile, p_dst, p_length);
  243. ERR_FAIL_COND_V(read < 0, read);
  244. if ((uint64_t)read < p_length) {
  245. at_eof = true;
  246. }
  247. return read;
  248. }
  249. Error FileAccessZip::get_error() const {
  250. if (!zfile) {
  251. return ERR_UNCONFIGURED;
  252. }
  253. if (eof_reached()) {
  254. return ERR_FILE_EOF;
  255. }
  256. return OK;
  257. }
  258. void FileAccessZip::flush() {
  259. ERR_FAIL();
  260. }
  261. void FileAccessZip::store_8(uint8_t p_dest) {
  262. ERR_FAIL();
  263. }
  264. bool FileAccessZip::file_exists(const String &p_name) {
  265. return false;
  266. }
  267. void FileAccessZip::close() {
  268. _close();
  269. }
  270. FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) {
  271. open_internal(p_path, FileAccess::READ);
  272. }
  273. FileAccessZip::~FileAccessZip() {
  274. _close();
  275. }
  276. #endif // MINIZIP_ENABLED