"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/libzip/zip_source_crc.c" (10 Oct 2018, 5844 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "zip_source_crc.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.21_Source_vs_1.22_Source.

    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 
    5   This file is part of libzip, a library to manipulate ZIP archives.
    6   The authors can be contacted at <libzip@nih.at>
    7 
    8   Redistribution and use in source and binary forms, with or without
    9   modification, are permitted provided that the following conditions
   10   are met:
   11   1. Redistributions of source code must retain the above copyright
   12      notice, this list of conditions and the following disclaimer.
   13   2. Redistributions in binary form must reproduce the above copyright
   14      notice, this list of conditions and the following disclaimer in
   15      the documentation and/or other materials provided with the
   16      distribution.
   17   3. The names of the authors may not be used to endorse or promote
   18      products derived from this software without specific prior
   19      written permission.
   20 
   21   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
   22   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   23   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
   25   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   27   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   28   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
   29   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   30   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
   31   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   32 */
   33 
   34 
   35 #include <limits.h>
   36 #include <stdlib.h>
   37 #include <string.h>
   38 
   39 #include "zipint.h"
   40 
   41 struct crc_context {
   42     int validate;     /* whether to check CRC on EOF and return error on mismatch */
   43     int crc_complete; /* whether CRC was computed for complete file */
   44     zip_error_t error;
   45     zip_uint64_t size;
   46     zip_uint64_t position;     /* current reading position */
   47     zip_uint64_t crc_position; /* how far we've computed the CRC */
   48     zip_uint32_t crc;
   49 };
   50 
   51 static zip_int64_t crc_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
   52 
   53 
   54 zip_source_t *
   55 zip_source_crc(zip_t *za, zip_source_t *src, int validate) {
   56     struct crc_context *ctx;
   57 
   58     if (src == NULL) {
   59     zip_error_set(&za->error, ZIP_ER_INVAL, 0);
   60     return NULL;
   61     }
   62 
   63     if ((ctx = (struct crc_context *)malloc(sizeof(*ctx))) == NULL) {
   64     zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
   65     return NULL;
   66     }
   67 
   68     zip_error_init(&ctx->error);
   69     ctx->validate = validate;
   70     ctx->crc_complete = 0;
   71     ctx->crc_position = 0;
   72     ctx->crc = (zip_uint32_t)crc32(0, NULL, 0);
   73     ctx->size = 0;
   74 
   75     return zip_source_layered(za, src, crc_read, ctx);
   76 }
   77 
   78 
   79 static zip_int64_t
   80 crc_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
   81     struct crc_context *ctx;
   82     zip_int64_t n;
   83 
   84     ctx = (struct crc_context *)_ctx;
   85 
   86     switch (cmd) {
   87     case ZIP_SOURCE_OPEN:
   88     ctx->position = 0;
   89     return 0;
   90 
   91     case ZIP_SOURCE_READ:
   92     if ((n = zip_source_read(src, data, len)) < 0) {
   93         _zip_error_set_from_source(&ctx->error, src);
   94         return -1;
   95     }
   96 
   97     if (n == 0) {
   98         if (ctx->crc_position == ctx->position) {
   99         ctx->crc_complete = 1;
  100         ctx->size = ctx->position;
  101 
  102         if (ctx->validate) {
  103             struct zip_stat st;
  104 
  105             if (zip_source_stat(src, &st) < 0) {
  106             _zip_error_set_from_source(&ctx->error, src);
  107             return -1;
  108             }
  109 
  110             if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) {
  111             zip_error_set(&ctx->error, ZIP_ER_CRC, 0);
  112             return -1;
  113             }
  114             if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) {
  115             zip_error_set(&ctx->error, ZIP_ER_INCONS, 0);
  116             return -1;
  117             }
  118         }
  119         }
  120     }
  121     else if (!ctx->crc_complete && ctx->position <= ctx->crc_position) {
  122         zip_uint64_t i, nn;
  123 
  124         for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) {
  125         nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n - i);
  126 
  127         ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data + i, (uInt)nn);
  128         ctx->crc_position += nn;
  129         }
  130     }
  131     ctx->position += (zip_uint64_t)n;
  132     return n;
  133 
  134     case ZIP_SOURCE_CLOSE:
  135     return 0;
  136 
  137     case ZIP_SOURCE_STAT: {
  138     zip_stat_t *st;
  139 
  140     st = (zip_stat_t *)data;
  141 
  142     if (ctx->crc_complete) {
  143         /* TODO: Set comp_size, comp_method, encryption_method?
  144             After all, this only works for uncompressed data. */
  145         st->size = ctx->size;
  146         st->crc = ctx->crc;
  147         st->comp_size = ctx->size;
  148         st->comp_method = ZIP_CM_STORE;
  149         st->encryption_method = ZIP_EM_NONE;
  150         st->valid |= ZIP_STAT_SIZE | ZIP_STAT_CRC | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD;
  151         ;
  152     }
  153     return 0;
  154     }
  155 
  156     case ZIP_SOURCE_ERROR:
  157     return zip_error_to_data(&ctx->error, data, len);
  158 
  159     case ZIP_SOURCE_FREE:
  160     free(ctx);
  161     return 0;
  162 
  163     case ZIP_SOURCE_SUPPORTS: {
  164     zip_int64_t mask = zip_source_supports(src);
  165 
  166     if (mask < 0) {
  167         _zip_error_set_from_source(&ctx->error, src);
  168         return -1;
  169     }
  170 
  171     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, ZIP_SOURCE_GET_COMPRESSION_FLAGS, -1);
  172     }
  173 
  174     case ZIP_SOURCE_SEEK: {
  175     zip_int64_t new_position;
  176     zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
  177 
  178     if (args == NULL) {
  179         return -1;
  180     }
  181     if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) {
  182         _zip_error_set_from_source(&ctx->error, src);
  183         return -1;
  184     }
  185 
  186     ctx->position = (zip_uint64_t)new_position;
  187 
  188     return 0;
  189     }
  190 
  191     case ZIP_SOURCE_TELL:
  192     return (zip_int64_t)ctx->position;
  193 
  194     default:
  195     zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
  196     return -1;
  197     }
  198 }