ioapi_mem.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.h"
  21. voidpf ZCALLBACK fopen_mem_func OF((
  22. voidpf opaque,
  23. const char* filename,
  24. int mode));
  25. uLong ZCALLBACK fread_mem_func OF((
  26. voidpf opaque,
  27. voidpf stream,
  28. void* buf,
  29. uLong size));
  30. uLong ZCALLBACK fwrite_mem_func OF((
  31. voidpf opaque,
  32. voidpf stream,
  33. const void* buf,
  34. uLong size));
  35. long ZCALLBACK ftell_mem_func OF((
  36. voidpf opaque,
  37. voidpf stream));
  38. long ZCALLBACK fseek_mem_func OF((
  39. voidpf opaque,
  40. voidpf stream,
  41. uLong offset,
  42. int origin));
  43. int ZCALLBACK fclose_mem_func OF((
  44. voidpf opaque,
  45. voidpf stream));
  46. int ZCALLBACK ferror_mem_func OF((
  47. voidpf opaque,
  48. voidpf stream));
  49. typedef struct ourmemory_s {
  50. char *base; /* Base of the region of memory we're using */
  51. uLong size; /* Size of the region of memory we're using */
  52. uLong limit; /* Furthest we've written */
  53. uLong cur_offset; /* Current offset in the area */
  54. } ourmemory_t;
  55. typedef struct zmemdata_s
  56. {
  57. char* data;
  58. uLong size;
  59. } zmemdata_t;
  60. voidpf ZCALLBACK fopen_mem_func (opaque, filename, mode)
  61. voidpf opaque;
  62. const char* filename;
  63. int mode;
  64. {
  65. ourmemory_t *mem = malloc(sizeof(*mem));
  66. zmemdata_t *data = (zmemdata_t*)(filename);
  67. if (mem==NULL)
  68. return NULL; /* Can't allocate space, so failed */
  69. /* Filenames are specified in the form :
  70. * <hex base of zip file>+<hex size of zip file>
  71. * This may not work where memory addresses are longer than the
  72. * size of an int and therefore may need addressing for 64bit
  73. * architectures
  74. */
  75. //if (sscanf(filename,"%ld+%ld", (size_t*)&mem->base, (size_t*)&mem->size)!=2)
  76. // return NULL;
  77. mem->base = data->data;
  78. mem->size = data->size;
  79. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  80. mem->limit=0; /* When writing we start with 0 bytes written */
  81. else
  82. mem->limit=mem->size;
  83. mem->cur_offset = 0;
  84. return mem;
  85. }
  86. uLong ZCALLBACK fread_mem_func (opaque, stream, buf, size)
  87. voidpf opaque;
  88. voidpf stream;
  89. void* buf;
  90. uLong size;
  91. {
  92. ourmemory_t *mem = (ourmemory_t *)stream;
  93. if (size > mem->size - mem->cur_offset)
  94. size = mem->size - mem->cur_offset;
  95. memcpy(buf, mem->base + mem->cur_offset, size);
  96. mem->cur_offset+=size;
  97. return size;
  98. }
  99. uLong ZCALLBACK fwrite_mem_func (opaque, stream, buf, size)
  100. voidpf opaque;
  101. voidpf stream;
  102. const void* buf;
  103. uLong size;
  104. {
  105. ourmemory_t *mem = (ourmemory_t *)stream;
  106. if (size > mem->size - mem->cur_offset)
  107. size = mem->size - mem->cur_offset;
  108. memcpy(mem->base + mem->cur_offset, buf, size);
  109. mem->cur_offset+=size;
  110. if (mem->cur_offset > mem->limit)
  111. mem->limit = mem->cur_offset;
  112. return size;
  113. }
  114. long ZCALLBACK ftell_mem_func (opaque, stream)
  115. voidpf opaque;
  116. voidpf stream;
  117. {
  118. ourmemory_t *mem = (ourmemory_t *)stream;
  119. return mem->cur_offset;
  120. }
  121. long ZCALLBACK fseek_mem_func (opaque, stream, offset, origin)
  122. voidpf opaque;
  123. voidpf stream;
  124. uLong offset;
  125. int origin;
  126. {
  127. ourmemory_t *mem = (ourmemory_t *)stream;
  128. uLong new_pos;
  129. switch (origin)
  130. {
  131. case ZLIB_FILEFUNC_SEEK_CUR :
  132. new_pos = mem->cur_offset + offset;
  133. break;
  134. case ZLIB_FILEFUNC_SEEK_END :
  135. new_pos = mem->limit + offset;
  136. break;
  137. case ZLIB_FILEFUNC_SEEK_SET :
  138. new_pos = offset;
  139. break;
  140. default: return -1;
  141. }
  142. if (new_pos > mem->size)
  143. return 1; /* Failed to seek that far */
  144. if (new_pos > mem->limit)
  145. memset(mem->base + mem->limit, 0, new_pos - mem->limit);
  146. mem->cur_offset = new_pos;
  147. return 0;
  148. }
  149. int ZCALLBACK fclose_mem_func (opaque, stream)
  150. voidpf opaque;
  151. voidpf stream;
  152. {
  153. ourmemory_t *mem = (ourmemory_t *)stream;
  154. /* Note that once we've written to the buffer we don't tell anyone
  155. about it here. Probably the opaque handle could be used to inform
  156. some other component of how much data was written.
  157. This, and other aspects of writing through this interface, has
  158. not been tested.
  159. */
  160. free (mem);
  161. return 0;
  162. }
  163. int ZCALLBACK ferror_mem_func (opaque, stream)
  164. voidpf opaque;
  165. voidpf stream;
  166. {
  167. ourmemory_t *mem = (ourmemory_t *)stream;
  168. /* We never return errors */
  169. return 0;
  170. }
  171. void fill_memory_filefunc (pzlib_filefunc_def)
  172. zlib_filefunc_def* pzlib_filefunc_def;
  173. {
  174. pzlib_filefunc_def->zopen_file = fopen_mem_func;
  175. pzlib_filefunc_def->zread_file = fread_mem_func;
  176. pzlib_filefunc_def->zwrite_file = fwrite_mem_func;
  177. pzlib_filefunc_def->ztell_file = ftell_mem_func;
  178. pzlib_filefunc_def->zseek_file = fseek_mem_func;
  179. pzlib_filefunc_def->zclose_file = fclose_mem_func;
  180. pzlib_filefunc_def->zerror_file = ferror_mem_func;
  181. pzlib_filefunc_def->opaque = NULL;
  182. }