zip_io.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*************************************************************************/
  2. /* zip_io.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "zip_io.h"
  31. #include "core/os/copymem.h"
  32. void *zipio_open(void *data, const char *p_fname, int mode) {
  33. FileAccess *&f = *(FileAccess **)data;
  34. String fname;
  35. fname.parse_utf8(p_fname);
  36. if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
  37. f = FileAccess::open(fname, FileAccess::WRITE);
  38. } else {
  39. f = FileAccess::open(fname, FileAccess::READ);
  40. }
  41. if (!f) {
  42. return nullptr;
  43. }
  44. return data;
  45. }
  46. uLong zipio_read(void *data, void *fdata, void *buf, uLong size) {
  47. FileAccess *f = *(FileAccess **)data;
  48. return f->get_buffer((uint8_t *)buf, size);
  49. }
  50. uLong zipio_write(voidpf opaque, voidpf stream, const void *buf, uLong size) {
  51. FileAccess *f = *(FileAccess **)opaque;
  52. f->store_buffer((uint8_t *)buf, size);
  53. return size;
  54. }
  55. long zipio_tell(voidpf opaque, voidpf stream) {
  56. FileAccess *f = *(FileAccess **)opaque;
  57. return f->get_position();
  58. }
  59. long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
  60. FileAccess *f = *(FileAccess **)opaque;
  61. int pos = offset;
  62. switch (origin) {
  63. case ZLIB_FILEFUNC_SEEK_CUR:
  64. pos = f->get_position() + offset;
  65. break;
  66. case ZLIB_FILEFUNC_SEEK_END:
  67. pos = f->get_len() + offset;
  68. break;
  69. default:
  70. break;
  71. }
  72. f->seek(pos);
  73. return 0;
  74. }
  75. int zipio_close(voidpf opaque, voidpf stream) {
  76. FileAccess *&f = *(FileAccess **)opaque;
  77. if (f) {
  78. f->close();
  79. memdelete(f);
  80. f = nullptr;
  81. }
  82. return 0;
  83. }
  84. int zipio_testerror(voidpf opaque, voidpf stream) {
  85. FileAccess *f = *(FileAccess **)opaque;
  86. return (f && f->get_error() != OK) ? 1 : 0;
  87. }
  88. voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) {
  89. voidpf ptr = memalloc(items * size);
  90. zeromem(ptr, items * size);
  91. return ptr;
  92. }
  93. void zipio_free(voidpf opaque, voidpf address) {
  94. memfree(address);
  95. }
  96. zlib_filefunc_def zipio_create_io_from_file(FileAccess **p_file) {
  97. zlib_filefunc_def io;
  98. io.opaque = p_file;
  99. io.zopen_file = zipio_open;
  100. io.zread_file = zipio_read;
  101. io.zwrite_file = zipio_write;
  102. io.ztell_file = zipio_tell;
  103. io.zseek_file = zipio_seek;
  104. io.zclose_file = zipio_close;
  105. io.zerror_file = zipio_testerror;
  106. io.alloc_mem = zipio_alloc;
  107. io.free_mem = zipio_free;
  108. return io;
  109. }