root/ext/zip/lib/zip_set_name.c

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

DEFINITIONS

This source file includes following definitions.
  1. _zip_set_name

   1 /*
   2   zip_set_name.c -- rename helper function
   3   Copyright (C) 1999-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 int
  44 _zip_set_name(struct zip *za, zip_uint64_t idx, const char *name, zip_flags_t flags)
  45 {
  46     struct zip_entry *e;
  47     struct zip_string *str;
  48     int changed;
  49     zip_int64_t i;
  50 
  51     if (idx >= za->nentry) {
  52         _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  53         return -1;
  54     }
  55 
  56     if (ZIP_IS_RDONLY(za)) {
  57         _zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
  58         return -1;
  59     }
  60 
  61     if (name && strlen(name) > 0) {
  62         /* TODO: check for string too long */
  63         if ((str=_zip_string_new((const zip_uint8_t *)name, (zip_uint16_t)strlen(name), flags, &za->error)) == NULL)
  64             return -1;
  65         if ((flags & ZIP_FL_ENCODING_ALL) == ZIP_FL_ENC_GUESS && _zip_guess_encoding(str, ZIP_ENCODING_UNKNOWN) == ZIP_ENCODING_UTF8_GUESSED)
  66             str->encoding = ZIP_ENCODING_UTF8_KNOWN;
  67     }
  68     else
  69         str = NULL;
  70 
  71     /* TODO: encoding flags needed for CP437? */
  72     if ((i=_zip_name_locate(za, name, 0, NULL)) >= 0 && (zip_uint64_t)i != idx) {
  73         _zip_string_free(str);
  74         _zip_error_set(&za->error, ZIP_ER_EXISTS, 0);
  75         return -1;
  76     }
  77 
  78     /* no effective name change */
  79     if (i>=0 && (zip_uint64_t)i == idx) {
  80         _zip_string_free(str);
  81         return 0;
  82     }
  83 
  84     e = za->entry+idx;
  85 
  86     if (e->changes) {
  87         _zip_string_free(e->changes->filename);
  88         e->changes->filename = NULL;
  89         e->changes->changed &= ~ZIP_DIRENT_FILENAME;
  90     }
  91 
  92     if (e->orig)
  93         changed = !_zip_string_equal(e->orig->filename, str);
  94     else
  95         changed = 1;
  96         
  97     if (changed) {
  98         if (e->changes == NULL) {
  99             if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) {
 100                 _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
 101                 _zip_string_free(str);
 102                 return -1;
 103             }
 104         }
 105         e->changes->filename = str;
 106         e->changes->changed |= ZIP_DIRENT_FILENAME;
 107     }
 108     else {
 109         _zip_string_free(str);
 110         if (e->changes && e->changes->changed == 0) {
 111             _zip_dirent_free(e->changes);
 112             e->changes = NULL;
 113         }
 114     }
 115 
 116     return 0;
 117 }

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