ioapi_mem.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* ioapi_mem.c -- IO base function header for compress/uncompress .zip
  2. files using zlib + zip or unzip API
  3. This version of ioapi is designed to access memory rather than files.
  4. We do use a region of memory to put data in to and take it out of. We do
  5. not have auto-extending buffers and do not inform anyone else that the
  6. data has been written. It is really intended for accessing a zip archive
  7. embedded in an application such that I can write an installer with no
  8. external files. Creation of archives has not been attempted, although
  9. parts of the framework are present.
  10. Based on Unzip ioapi.c version 0.22, May 19th, 2003
  11. Copyright (C) 1998-2003 Gilles Vollant
  12. (C) 2003 Justin Fletcher
  13. This file is under the same license as the Unzip tool it is distributed
  14. with.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "zlib.h"
  20. #include "ioapi_mem.h"
  21. #ifdef _MSC_VER
  22. #define strtoull _strtoui64
  23. #endif
  24. namespace cocos2d {
  25. voidpf ZCALLBACK fopen_mem_func (voidpf opaque,
  26. const char* filename,
  27. int mode)
  28. {
  29. ourmemory_t *mem = (ourmemory_t*)malloc(sizeof(ourmemory_t));
  30. if (mem==NULL) return NULL; /* Can't allocate space, so failed */
  31. /* Filenames are specified in the form :
  32. * <hex base of zip file>+<hex size of zip file>
  33. * This may not work where memory addresses are longer than the
  34. * size of an int and therefore may need addressing for 64bit
  35. * architectures
  36. */
  37. memset(mem, 0, sizeof(ourmemory_t));
  38. char *pEnd = NULL;
  39. void *base = NULL;
  40. unsigned long size = 0;
  41. base = (void*)strtoull(filename, &pEnd, 16);
  42. if (!base) return NULL;
  43. size = strtoul(pEnd, &pEnd, 16);
  44. if (!size) return NULL;
  45. mem->base = base;
  46. mem->size = size;
  47. if (mode & ZLIB_FILEFUNC_MODE_CREATE) {
  48. mem->limit = 0; /* When writing we start with 0 bytes written */
  49. } else {
  50. mem->limit = mem->size;
  51. }
  52. mem->cur_offset = 0;
  53. return mem;
  54. }
  55. voidpf ZCALLBACK fopen_mem_func64_32 (voidpf opaque,
  56. const void* filename,
  57. int mode)
  58. {
  59. ourmemory_t *mem = (ourmemory_t*)malloc(sizeof(ourmemory_t));
  60. if (mem==NULL) return NULL; /* Can't allocate space, so failed */
  61. /* Filenames are specified in the form :
  62. * <hex base of zip file>+<hex size of zip file>
  63. * This may not work where memory addresses are longer than the
  64. * size of an int and therefore may need addressing for 64bit
  65. * architectures
  66. */
  67. memset(mem, 0, sizeof(ourmemory_t));
  68. char *pEnd = NULL;
  69. void *base = NULL;
  70. unsigned long size = 0;
  71. base = (void*)strtoull((const char*)filename, &pEnd, 16);
  72. if (!base) return NULL;
  73. size = strtoul(pEnd, &pEnd, 16);
  74. if (!size) return NULL;
  75. mem->base = base;
  76. mem->size = size;
  77. if (mode & ZLIB_FILEFUNC_MODE_CREATE) {
  78. mem->limit=0; /* When writing we start with 0 bytes written */
  79. } else {
  80. mem->limit=mem->size;
  81. }
  82. mem->cur_offset = 0;
  83. return mem;
  84. }
  85. uLong ZCALLBACK fread_mem_func (voidpf opaque, voidpf stream, void* buf, uLong size)
  86. {
  87. ourmemory_t *mem = (ourmemory_t *)stream;
  88. if (size > mem->size - mem->cur_offset) {
  89. size = mem->size - mem->cur_offset;
  90. }
  91. memcpy(buf, (char*)mem->base + mem->cur_offset, size);
  92. mem->cur_offset+=size;
  93. return size;
  94. }
  95. uLong ZCALLBACK fwrite_mem_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
  96. {
  97. ourmemory_t *mem = (ourmemory_t *)stream;
  98. if (size > mem->size - mem->cur_offset) {
  99. size = mem->size - mem->cur_offset;
  100. }
  101. memcpy((char*)mem->base + mem->cur_offset, buf, size);
  102. mem->cur_offset+=size;
  103. if (mem->cur_offset > mem->limit) {
  104. mem->limit = mem->cur_offset;
  105. }
  106. return size;
  107. }
  108. long ZCALLBACK ftell_mem_func (voidpf opaque, voidpf stream)
  109. {
  110. ourmemory_t *mem = (ourmemory_t *)stream;
  111. return mem->cur_offset;
  112. }
  113. long ZCALLBACK fseek_mem_func (voidpf opaque, voidpf stream, uLong offset, int origin)
  114. {
  115. ourmemory_t *mem = (ourmemory_t *)stream;
  116. uLong new_pos;
  117. switch (origin) {
  118. case ZLIB_FILEFUNC_SEEK_CUR : {
  119. new_pos = mem->cur_offset + offset;
  120. break;
  121. }
  122. case ZLIB_FILEFUNC_SEEK_END : {
  123. new_pos = mem->limit + offset;
  124. break;
  125. }
  126. case ZLIB_FILEFUNC_SEEK_SET : {
  127. new_pos = offset;
  128. break;
  129. }
  130. default: {
  131. return -1;
  132. }
  133. }
  134. if (new_pos > mem->size) return 1; /* Failed to seek that far */
  135. if (new_pos > mem->limit) {
  136. memset((char*)mem->base + mem->limit, 0, new_pos - mem->limit);
  137. }
  138. mem->cur_offset = new_pos;
  139. return 0;
  140. }
  141. int ZCALLBACK fclose_mem_func (voidpf opaque, voidpf stream)
  142. {
  143. ourmemory_t *mem = (ourmemory_t *)stream;
  144. /* Note that once we've written to the buffer we don't tell anyone
  145. about it here. Probably the opaque handle could be used to inform
  146. some other component of how much data was written.
  147. This, and other aspects of writing through this interface, has
  148. not been tested.
  149. */
  150. free (mem);
  151. return 0;
  152. }
  153. int ZCALLBACK ferror_mem_func (voidpf opaque, voidpf stream)
  154. {
  155. /* We never return errors */
  156. return 0;
  157. }
  158. void fill_memory_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
  159. {
  160. pzlib_filefunc_def->zopen_file = fopen_mem_func;
  161. pzlib_filefunc_def->zread_file = fread_mem_func;
  162. pzlib_filefunc_def->zwrite_file = fwrite_mem_func;
  163. pzlib_filefunc_def->ztell_file = ftell_mem_func;
  164. pzlib_filefunc_def->zseek_file = fseek_mem_func;
  165. pzlib_filefunc_def->zclose_file = fclose_mem_func;
  166. pzlib_filefunc_def->zerror_file = ferror_mem_func;
  167. pzlib_filefunc_def->opaque = NULL;
  168. }
  169. void fill_memory_filefunc64_32 (zlib_filefunc64_32_def* pzlib_filefunc_def)
  170. {
  171. pzlib_filefunc_def->zopen32_file = fopen_mem_func;
  172. pzlib_filefunc_def->zfile_func64.zopen64_file = fopen_mem_func64_32;
  173. pzlib_filefunc_def->zfile_func64.zread_file = fread_mem_func;
  174. pzlib_filefunc_def->zfile_func64.zwrite_file = fwrite_mem_func;
  175. pzlib_filefunc_def->ztell32_file = ftell_mem_func;
  176. pzlib_filefunc_def->zseek32_file = fseek_mem_func;
  177. pzlib_filefunc_def->zfile_func64.zseek64_file = NULL;
  178. pzlib_filefunc_def->zfile_func64.zclose_file = fclose_mem_func;
  179. pzlib_filefunc_def->zfile_func64.zerror_file = ferror_mem_func;
  180. pzlib_filefunc_def->zfile_func64.opaque = NULL;
  181. }
  182. }; // namespace cocos2d