"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/libzip/zip_source_zip_new.c" (10 Oct 2018, 5352 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_zip_new.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_zip_new.c -- prepare data structures for zip_fopen/zip_source_zip
    3   Copyright (C) 2012-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 <stdlib.h>
   36 
   37 #include "zipint.h"
   38 
   39 
   40 zip_source_t *
   41 _zip_source_zip_new(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t flags, zip_uint64_t start, zip_uint64_t len, const char *password) {
   42     zip_source_t *src, *s2;
   43     struct zip_stat st;
   44     bool partial_data, needs_crc, needs_decrypt, needs_decompress;
   45 
   46     if (za == NULL)
   47     return NULL;
   48 
   49     if (srcza == NULL || srcidx >= srcza->nentry) {
   50     zip_error_set(&za->error, ZIP_ER_INVAL, 0);
   51     return NULL;
   52     }
   53 
   54     if ((flags & ZIP_FL_UNCHANGED) == 0 && (ZIP_ENTRY_DATA_CHANGED(srcza->entry + srcidx) || srcza->entry[srcidx].deleted)) {
   55     zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
   56     return NULL;
   57     }
   58 
   59     if (zip_stat_index(srcza, srcidx, flags | ZIP_FL_UNCHANGED, &st) < 0) {
   60     zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
   61     return NULL;
   62     }
   63 
   64     if (flags & ZIP_FL_ENCRYPTED)
   65     flags |= ZIP_FL_COMPRESSED;
   66 
   67     if ((start > 0 || len > 0) && (flags & ZIP_FL_COMPRESSED)) {
   68     zip_error_set(&za->error, ZIP_ER_INVAL, 0);
   69     return NULL;
   70     }
   71 
   72     /* overflow or past end of file */
   73     if ((start > 0 || len > 0) && (start + len < start || start + len > st.size)) {
   74     zip_error_set(&za->error, ZIP_ER_INVAL, 0);
   75     return NULL;
   76     }
   77 
   78     if (len == 0) {
   79     len = st.size - start;
   80     }
   81 
   82     partial_data = len < st.size;
   83     needs_decrypt = ((flags & ZIP_FL_ENCRYPTED) == 0) && (st.encryption_method != ZIP_EM_NONE);
   84     needs_decompress = ((flags & ZIP_FL_COMPRESSED) == 0) && (st.comp_method != ZIP_CM_STORE);
   85     /* when reading the whole file, check for CRC errors */
   86     needs_crc = ((flags & ZIP_FL_COMPRESSED) == 0 || st.comp_method == ZIP_CM_STORE) && !partial_data;
   87 
   88     if (needs_decrypt) {
   89     if (password == NULL) {
   90         password = za->default_password;
   91     }
   92     if (password == NULL) {
   93         zip_error_set(&za->error, ZIP_ER_NOPASSWD, 0);
   94         return NULL;
   95     }
   96     }
   97 
   98     if (st.comp_size == 0) {
   99     return zip_source_buffer(za, NULL, 0, 0);
  100     }
  101 
  102     if (partial_data && !needs_decrypt && !needs_decompress) {
  103     struct zip_stat st2;
  104 
  105     st2.size = len;
  106     st2.comp_size = len;
  107     st2.comp_method = ZIP_CM_STORE;
  108     st2.mtime = st.mtime;
  109     st2.valid = ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_MTIME;
  110 
  111     if ((src = _zip_source_window_new(srcza->src, start, len, &st2, 0, srcza, srcidx, &za->error)) == NULL) {
  112         return NULL;
  113     }
  114     }
  115     else {
  116     zip_dirent_t *de;
  117 
  118     if ((de = _zip_get_dirent(srcza, srcidx, flags, &za->error)) == NULL) {
  119         return NULL;
  120     }
  121     if ((src = _zip_source_window_new(srcza->src, 0, st.comp_size, &st, (de->bitflags >> 1) & 3, srcza, srcidx, &za->error)) == NULL) {
  122         return NULL;
  123     }
  124     }
  125 
  126     if (_zip_source_set_source_archive(src, srcza) < 0) {
  127     zip_source_free(src);
  128     return NULL;
  129     }
  130 
  131     /* creating a layered source calls zip_keep() on the lower layer, so we free it */
  132 
  133     if (needs_decrypt) {
  134     zip_encryption_implementation enc_impl;
  135 
  136     if ((enc_impl = _zip_get_encryption_implementation(st.encryption_method, ZIP_CODEC_DECODE)) == NULL) {
  137         zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0);
  138         return NULL;
  139     }
  140 
  141     s2 = enc_impl(za, src, st.encryption_method, 0, password);
  142     zip_source_free(src);
  143     if (s2 == NULL) {
  144         return NULL;
  145     }
  146     src = s2;
  147     }
  148     if (needs_decompress) {
  149     s2 = zip_source_decompress(za, src, st.comp_method);
  150     zip_source_free(src);
  151     if (s2 == NULL) {
  152         return NULL;
  153     }
  154     src = s2;
  155     }
  156     if (needs_crc) {
  157     s2 = zip_source_crc(za, src, 1);
  158     zip_source_free(src);
  159     if (s2 == NULL) {
  160         return NULL;
  161     }
  162     src = s2;
  163     }
  164 
  165     if (partial_data && (needs_decrypt || needs_decompress)) {
  166     s2 = zip_source_window(za, src, start, len);
  167     zip_source_free(src);
  168     if (s2 == NULL) {
  169         return NULL;
  170     }
  171     src = s2;
  172     }
  173 
  174     return src;
  175 }