zip_io.cpp 4.2 KB

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