root/ext/zip/lib/zip_string.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _zip_string_crc32
  2. _zip_string_equal
  3. _zip_string_free
  4. _zip_string_get
  5. _zip_string_length
  6. _zip_string_new
  7. _zip_string_write

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

/* [<][>][^][v][top][bottom][index][help] */