"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/libzip/zip_string.c" (10 Oct 2018, 4675 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_string.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_string.c -- string handling (with encoding)
    3   Copyright (C) 2012-2014 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 #include <string.h>
   37 
   38 #include "zipint.h"
   39 
   40 
   41 zip_uint32_t
   42 _zip_string_crc32(const zip_string_t *s) {
   43     zip_uint32_t crc;
   44 
   45     crc = (zip_uint32_t)crc32(0L, Z_NULL, 0);
   46 
   47     if (s != NULL)
   48     crc = (zip_uint32_t)crc32(crc, s->raw, s->length);
   49 
   50     return crc;
   51 }
   52 
   53 
   54 int
   55 _zip_string_equal(const zip_string_t *a, const zip_string_t *b) {
   56     if (a == NULL || b == NULL)
   57     return a == b;
   58 
   59     if (a->length != b->length)
   60     return 0;
   61 
   62     /* TODO: encoding */
   63 
   64     return (memcmp(a->raw, b->raw, a->length) == 0);
   65 }
   66 
   67 
   68 void
   69 _zip_string_free(zip_string_t *s) {
   70     if (s == NULL)
   71     return;
   72 
   73     free(s->raw);
   74     free(s->converted);
   75     free(s);
   76 }
   77 
   78 
   79 const zip_uint8_t *
   80 _zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error) {
   81     static const zip_uint8_t empty[1] = "";
   82 
   83     if (string == NULL) {
   84     if (lenp)
   85         *lenp = 0;
   86     return empty;
   87     }
   88 
   89     if ((flags & ZIP_FL_ENC_RAW) == 0) {
   90     /* start guessing */
   91     if (string->encoding == ZIP_ENCODING_UNKNOWN)
   92         _zip_guess_encoding(string, ZIP_ENCODING_UNKNOWN);
   93 
   94     if (((flags & ZIP_FL_ENC_STRICT) && string->encoding != ZIP_ENCODING_ASCII && string->encoding != ZIP_ENCODING_UTF8_KNOWN) || (string->encoding == ZIP_ENCODING_CP437)) {
   95         if (string->converted == NULL) {
   96         if ((string->converted = _zip_cp437_to_utf8(string->raw, string->length, &string->converted_length, error)) == NULL)
   97             return NULL;
   98         }
   99         if (lenp)
  100         *lenp = string->converted_length;
  101         return string->converted;
  102     }
  103     }
  104 
  105     if (lenp)
  106     *lenp = string->length;
  107     return string->raw;
  108 }
  109 
  110 
  111 zip_uint16_t
  112 _zip_string_length(const zip_string_t *s) {
  113     if (s == NULL)
  114     return 0;
  115 
  116     return s->length;
  117 }
  118 
  119 
  120 zip_string_t *
  121 _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error) {
  122     zip_string_t *s;
  123     zip_encoding_type_t expected_encoding;
  124 
  125     if (length == 0)
  126     return NULL;
  127 
  128     switch (flags & ZIP_FL_ENCODING_ALL) {
  129     case ZIP_FL_ENC_GUESS:
  130     expected_encoding = ZIP_ENCODING_UNKNOWN;
  131     break;
  132     case ZIP_FL_ENC_UTF_8:
  133     expected_encoding = ZIP_ENCODING_UTF8_KNOWN;
  134     break;
  135     case ZIP_FL_ENC_CP437:
  136     expected_encoding = ZIP_ENCODING_CP437;
  137     break;
  138     default:
  139     zip_error_set(error, ZIP_ER_INVAL, 0);
  140     return NULL;
  141     }
  142 
  143     if ((s = (zip_string_t *)malloc(sizeof(*s))) == NULL) {
  144     zip_error_set(error, ZIP_ER_MEMORY, 0);
  145     return NULL;
  146     }
  147 
  148     if ((s->raw = (zip_uint8_t *)malloc((size_t)(length + 1))) == NULL) {
  149     free(s);
  150     return NULL;
  151     }
  152 
  153     memcpy(s->raw, raw, length);
  154     s->raw[length] = '\0';
  155     s->length = length;
  156     s->encoding = ZIP_ENCODING_UNKNOWN;
  157     s->converted = NULL;
  158     s->converted_length = 0;
  159 
  160     if (expected_encoding != ZIP_ENCODING_UNKNOWN) {
  161     if (_zip_guess_encoding(s, expected_encoding) == ZIP_ENCODING_ERROR) {
  162         _zip_string_free(s);
  163         zip_error_set(error, ZIP_ER_INVAL, 0);
  164         return NULL;
  165     }
  166     }
  167 
  168     return s;
  169 }
  170 
  171 
  172 int
  173 _zip_string_write(zip_t *za, const zip_string_t *s) {
  174     if (s == NULL)
  175     return 0;
  176 
  177     return _zip_write(za, s->raw, s->length);
  178 }