zip_io.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. void *zipio_open(voidpf opaque, const char *p_fname, int mode) {
  32. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  33. ERR_FAIL_COND_V(fa == nullptr, nullptr);
  34. String fname;
  35. fname.parse_utf8(p_fname);
  36. int file_access_mode = 0;
  37. if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
  38. file_access_mode |= FileAccess::WRITE;
  39. }
  40. if (mode & ZLIB_FILEFUNC_MODE_READ) {
  41. file_access_mode |= FileAccess::READ;
  42. }
  43. if (mode & ZLIB_FILEFUNC_MODE_CREATE) {
  44. file_access_mode |= FileAccess::WRITE_READ;
  45. }
  46. (*fa) = FileAccess::open(fname, file_access_mode);
  47. if (fa->is_null()) {
  48. return nullptr;
  49. }
  50. return opaque;
  51. }
  52. uLong zipio_read(voidpf opaque, voidpf stream, void *buf, uLong size) {
  53. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  54. ERR_FAIL_COND_V(fa == nullptr, 0);
  55. ERR_FAIL_COND_V(fa->is_null(), 0);
  56. return (*fa)->get_buffer((uint8_t *)buf, size);
  57. }
  58. uLong zipio_write(voidpf opaque, voidpf stream, const void *buf, uLong size) {
  59. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  60. ERR_FAIL_COND_V(fa == nullptr, 0);
  61. ERR_FAIL_COND_V(fa->is_null(), 0);
  62. (*fa)->store_buffer((uint8_t *)buf, size);
  63. return size;
  64. }
  65. long zipio_tell(voidpf opaque, voidpf stream) {
  66. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  67. ERR_FAIL_COND_V(fa == nullptr, 0);
  68. ERR_FAIL_COND_V(fa->is_null(), 0);
  69. return (*fa)->get_position();
  70. }
  71. long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
  72. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  73. ERR_FAIL_COND_V(fa == nullptr, 0);
  74. ERR_FAIL_COND_V(fa->is_null(), 0);
  75. uint64_t pos = offset;
  76. switch (origin) {
  77. case ZLIB_FILEFUNC_SEEK_CUR:
  78. pos = (*fa)->get_position() + offset;
  79. break;
  80. case ZLIB_FILEFUNC_SEEK_END:
  81. pos = (*fa)->get_length() + offset;
  82. break;
  83. default:
  84. break;
  85. }
  86. (*fa)->seek(pos);
  87. return 0;
  88. }
  89. int zipio_close(voidpf opaque, voidpf stream) {
  90. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  91. ERR_FAIL_COND_V(fa == nullptr, 0);
  92. ERR_FAIL_COND_V(fa->is_null(), 0);
  93. fa->unref();
  94. return 0;
  95. }
  96. int zipio_testerror(voidpf opaque, voidpf stream) {
  97. Ref<FileAccess> *fa = reinterpret_cast<Ref<FileAccess> *>(opaque);
  98. ERR_FAIL_COND_V(fa == nullptr, 1);
  99. ERR_FAIL_COND_V(fa->is_null(), 0);
  100. return (fa->is_valid() && (*fa)->get_error() != OK) ? 1 : 0;
  101. }
  102. voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) {
  103. voidpf ptr = memalloc((size_t)items * size);
  104. memset(ptr, 0, items * size);
  105. return ptr;
  106. }
  107. void zipio_free(voidpf opaque, voidpf address) {
  108. memfree(address);
  109. }
  110. zlib_filefunc_def zipio_create_io(Ref<FileAccess> *p_data) {
  111. zlib_filefunc_def io;
  112. io.opaque = (void *)p_data;
  113. io.zopen_file = zipio_open;
  114. io.zread_file = zipio_read;
  115. io.zwrite_file = zipio_write;
  116. io.ztell_file = zipio_tell;
  117. io.zseek_file = zipio_seek;
  118. io.zclose_file = zipio_close;
  119. io.zerror_file = zipio_testerror;
  120. io.alloc_mem = zipio_alloc;
  121. io.free_mem = zipio_free;
  122. return io;
  123. }