zip_source_crc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. zip_source_crc.c -- pass-through source that calculates CRC32 and size
  3. Copyright (c) 2009-2017 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 <limits.h>
  33. #include "zipint.h"
  34. struct crc_context {
  35. int validate; /* whether to check CRC on EOF and return error on mismatch */
  36. int crc_complete; /* whether CRC was computed for complete file */
  37. zip_error_t error;
  38. zip_uint64_t size;
  39. zip_uint64_t position; /* current reading position */
  40. zip_uint64_t crc_position; /* how far we've computed the CRC */
  41. zip_uint32_t crc;
  42. };
  43. static zip_int64_t crc_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
  44. zip_source_t *
  45. zip_source_crc(zip_t *za, zip_source_t *src, int validate)
  46. {
  47. struct crc_context *ctx;
  48. if (src == NULL) {
  49. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  50. return NULL;
  51. }
  52. if ((ctx=(struct crc_context *)malloc(sizeof(*ctx))) == NULL) {
  53. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  54. return NULL;
  55. }
  56. zip_error_init(&ctx->error);
  57. ctx->validate = validate;
  58. ctx->crc_complete = 0;
  59. ctx->crc_position = 0;
  60. ctx->crc = (zip_uint32_t)crc32(0, NULL, 0);
  61. ctx->size = 0;
  62. return zip_source_layered(za, src, crc_read, ctx);
  63. }
  64. static zip_int64_t
  65. crc_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd)
  66. {
  67. struct crc_context *ctx;
  68. zip_int64_t n;
  69. ctx = (struct crc_context *)_ctx;
  70. switch (cmd) {
  71. case ZIP_SOURCE_OPEN:
  72. ctx->position = 0;
  73. return 0;
  74. case ZIP_SOURCE_READ:
  75. if ((n = zip_source_read(src, data, len)) < 0) {
  76. _zip_error_set_from_source(&ctx->error, src);
  77. return -1;
  78. }
  79. if (n == 0) {
  80. if (ctx->crc_position == ctx->position) {
  81. ctx->crc_complete = 1;
  82. ctx->size = ctx->position;
  83. if (ctx->validate) {
  84. struct zip_stat st;
  85. if (zip_source_stat(src, &st) < 0) {
  86. _zip_error_set_from_source(&ctx->error, src);
  87. return -1;
  88. }
  89. if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) {
  90. zip_error_set(&ctx->error, ZIP_ER_CRC, 0);
  91. return -1;
  92. }
  93. if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) {
  94. zip_error_set(&ctx->error, ZIP_ER_INCONS, 0);
  95. return -1;
  96. }
  97. }
  98. }
  99. }
  100. else if (!ctx->crc_complete && ctx->position <= ctx->crc_position) {
  101. zip_uint64_t i, nn;
  102. for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) {
  103. nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n-i);
  104. ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data+i, (uInt)nn);
  105. ctx->crc_position += nn;
  106. }
  107. }
  108. ctx->position += (zip_uint64_t)n;
  109. return n;
  110. case ZIP_SOURCE_CLOSE:
  111. return 0;
  112. case ZIP_SOURCE_STAT:
  113. {
  114. zip_stat_t *st;
  115. st = (zip_stat_t *)data;
  116. if (ctx->crc_complete) {
  117. /* TODO: Set comp_size, comp_method, encryption_method?
  118. After all, this only works for uncompressed data. */
  119. st->size = ctx->size;
  120. st->crc = ctx->crc;
  121. st->comp_size = ctx->size;
  122. st->comp_method = ZIP_CM_STORE;
  123. st->encryption_method = ZIP_EM_NONE;
  124. st->valid |= ZIP_STAT_SIZE|ZIP_STAT_CRC|ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_ENCRYPTION_METHOD;
  125. }
  126. return 0;
  127. }
  128. case ZIP_SOURCE_ERROR:
  129. return zip_error_to_data(&ctx->error, data, len);
  130. case ZIP_SOURCE_FREE:
  131. free(ctx);
  132. return 0;
  133. case ZIP_SOURCE_SUPPORTS:
  134. {
  135. zip_int64_t mask = zip_source_supports(src);
  136. if (mask < 0) {
  137. _zip_error_set_from_source(&ctx->error, src);
  138. return -1;
  139. }
  140. return mask & ~zip_source_make_command_bitmap(ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_REMOVE, -1);
  141. }
  142. case ZIP_SOURCE_SEEK:
  143. {
  144. zip_int64_t new_position;
  145. zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
  146. if (args == NULL) {
  147. return -1;
  148. }
  149. if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) {
  150. _zip_error_set_from_source(&ctx->error, src);
  151. return -1;
  152. }
  153. ctx->position = (zip_uint64_t)new_position;
  154. return 0;
  155. }
  156. case ZIP_SOURCE_TELL:
  157. return (zip_int64_t)ctx->position;
  158. default:
  159. zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
  160. return -1;
  161. }
  162. }