zip_source_window.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. zip_source_window.c -- return part of lower source
  3. Copyright (C) 2012-2014 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <libzip@nih.at>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "zipint.h"
  33. struct window {
  34. zip_uint64_t start;
  35. zip_uint64_t end;
  36. zip_uint64_t offset;
  37. zip_stat_t stat;
  38. zip_error_t error;
  39. zip_int64_t supports;
  40. bool needs_seek;
  41. };
  42. static zip_int64_t window_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
  43. zip_source_t *
  44. zip_source_window(zip_t *za, zip_source_t *src, zip_uint64_t start, zip_uint64_t len)
  45. {
  46. return _zip_source_window_new(src, start, len, NULL, &za->error);
  47. }
  48. zip_source_t *
  49. _zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_uint64_t length, zip_stat_t *st, zip_error_t *error)
  50. {
  51. struct window *ctx;
  52. if (src == NULL || start + length < start) {
  53. zip_error_set(error, ZIP_ER_INVAL, 0);
  54. return NULL;
  55. }
  56. if ((ctx=(struct window *)malloc(sizeof(*ctx))) == NULL) {
  57. zip_error_set(error, ZIP_ER_MEMORY, 0);
  58. return NULL;
  59. }
  60. ctx->start = start;
  61. ctx->end = start + length;
  62. zip_stat_init(&ctx->stat);
  63. zip_error_init(&ctx->error);
  64. ctx->supports = (zip_source_supports(src) & ZIP_SOURCE_SUPPORTS_SEEKABLE) | (zip_source_make_command_bitmap(ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, -1));
  65. ctx->needs_seek = (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_SEEK)) ? true : false;
  66. if (st) {
  67. if (_zip_stat_merge(&ctx->stat, st, error) < 0) {
  68. free(ctx);
  69. return NULL;
  70. }
  71. }
  72. return zip_source_layered_create(src, window_read, ctx, error);
  73. }
  74. int
  75. _zip_source_set_source_archive(zip_source_t *src, zip_t *za)
  76. {
  77. src->source_archive = za;
  78. return _zip_register_source(za, src);
  79. }
  80. /* called by zip_discard to avoid operating on file from closed archive */
  81. void
  82. _zip_source_invalidate(zip_source_t *src)
  83. {
  84. src->source_closed = 1;
  85. if (zip_error_code_zip(&src->error) == ZIP_ER_OK) {
  86. zip_error_set(&src->error, ZIP_ER_ZIPCLOSED, 0);
  87. }
  88. }
  89. static zip_int64_t
  90. window_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd)
  91. {
  92. struct window *ctx;
  93. zip_int64_t ret;
  94. zip_uint64_t n, i;
  95. char b[8192];
  96. ctx = (struct window *)_ctx;
  97. switch (cmd) {
  98. case ZIP_SOURCE_CLOSE:
  99. return 0;
  100. case ZIP_SOURCE_ERROR:
  101. return zip_error_to_data(&ctx->error, data, len);
  102. case ZIP_SOURCE_FREE:
  103. free(ctx);
  104. return 0;
  105. case ZIP_SOURCE_OPEN:
  106. if (!ctx->needs_seek) {
  107. for (n=0; n<ctx->start; n+=(zip_uint64_t)ret) {
  108. i = (ctx->start-n > sizeof(b) ? sizeof(b) : ctx->start-n);
  109. if ((ret=zip_source_read(src, b, i)) < 0) {
  110. _zip_error_set_from_source(&ctx->error, src);
  111. return -1;
  112. }
  113. if (ret==0) {
  114. zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
  115. return -1;
  116. }
  117. }
  118. }
  119. ctx->offset = ctx->start;
  120. return 0;
  121. case ZIP_SOURCE_READ:
  122. if (len > ctx->end - ctx->offset)
  123. len = ctx->end - ctx->offset;
  124. if (len == 0)
  125. return 0;
  126. if (ctx->needs_seek) {
  127. if (zip_source_seek(src, (zip_int64_t)ctx->offset, SEEK_SET) < 0) {
  128. _zip_error_set_from_source(&ctx->error, src);
  129. return -1;
  130. }
  131. }
  132. if ((ret=zip_source_read(src, data, len)) < 0) {
  133. zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
  134. return -1;
  135. }
  136. ctx->offset += (zip_uint64_t)ret;
  137. if (ret == 0) {
  138. if (ctx->offset < ctx->end) {
  139. zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
  140. return -1;
  141. }
  142. }
  143. return ret;
  144. case ZIP_SOURCE_SEEK:
  145. {
  146. zip_int64_t new_offset = zip_source_seek_compute_offset(ctx->offset - ctx->start, ctx->end - ctx->start, data, len, &ctx->error);
  147. if (new_offset < 0) {
  148. return -1;
  149. }
  150. ctx->offset = (zip_uint64_t)new_offset + ctx->start;
  151. return 0;
  152. }
  153. case ZIP_SOURCE_STAT:
  154. {
  155. zip_stat_t *st;
  156. st = (zip_stat_t *)data;
  157. if (_zip_stat_merge(st, &ctx->stat, &ctx->error) < 0) {
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. case ZIP_SOURCE_SUPPORTS:
  163. return ctx->supports;
  164. case ZIP_SOURCE_TELL:
  165. return (zip_int64_t)(ctx->offset - ctx->start);
  166. default:
  167. zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
  168. return -1;
  169. }
  170. }
  171. void
  172. _zip_deregister_source(zip_t *za, zip_source_t *src)
  173. {
  174. unsigned int i;
  175. for (i=0; i<za->nopen_source; i++) {
  176. if (za->open_source[i] == src) {
  177. za->open_source[i] = za->open_source[za->nopen_source-1];
  178. za->nopen_source--;
  179. break;
  180. }
  181. }
  182. }
  183. int
  184. _zip_register_source(zip_t *za, zip_source_t *src)
  185. {
  186. zip_source_t **open_source;
  187. if (za->nopen_source+1 >= za->nopen_source_alloc) {
  188. unsigned int n;
  189. n = za->nopen_source_alloc + 10;
  190. open_source = (zip_source_t **)realloc(za->open_source, n*sizeof(zip_source_t *));
  191. if (open_source == NULL) {
  192. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  193. return -1;
  194. }
  195. za->nopen_source_alloc = n;
  196. za->open_source = open_source;
  197. }
  198. za->open_source[za->nopen_source++] = src;
  199. return 0;
  200. }