blob: 5738aab87f6d7d219b73f98931291d4df0be0d46 [file] [log] [blame]
Dieter Baron16d2e632012-01-16 15:02:32 +01001/*
2 zip_set_file_compression.c -- set compression for file in archive
Thomas Klausnerc36a6f42017-12-14 11:57:47 +01003 Copyright (C) 2012-2017 Dieter Baron and Thomas Klausner
Dieter Baron16d2e632012-01-16 15:02:32 +01004
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
Dieter Baron16d2e632012-01-16 15:02:32 +010034
35#include "zipint.h"
36
Dieter Baron16d2e632012-01-16 15:02:32 +010037
38ZIP_EXTERN int
Dieter Baronc548a182014-10-10 18:27:50 +020039zip_set_file_compression(zip_t *za, zip_uint64_t idx, zip_int32_t method, zip_uint32_t flags)
Dieter Baron16d2e632012-01-16 15:02:32 +010040{
Dieter Baron1d9dfeb2014-09-28 23:02:54 +020041 zip_entry_t *e;
Thomas Klausner71f5ebd2013-04-29 08:19:33 +020042 zip_int32_t old_method;
Dieter Baron0e5eeab2012-04-24 18:47:12 +020043
Thomas Klausner025d07e2017-04-06 12:33:07 +020044 if (idx >= za->nentry || flags > 9) {
Thomas Klausnerea8ba492014-09-23 16:54:47 +020045 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
Dieter Baron16d2e632012-01-16 15:02:32 +010046 return -1;
47 }
48
Dieter Baron16d2e632012-01-16 15:02:32 +010049 if (ZIP_IS_RDONLY(za)) {
Thomas Klausnerea8ba492014-09-23 16:54:47 +020050 zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
Dieter Baron16d2e632012-01-16 15:02:32 +010051 return -1;
52 }
53
Thomas Klausnercc5b86f2017-03-30 15:48:45 +020054 if (!zip_compression_method_supported(method, true)) {
Thomas Klausnerea8ba492014-09-23 16:54:47 +020055 zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
Dieter Baron0e5eeab2012-04-24 18:47:12 +020056 return -1;
57 }
Dieter Baron16d2e632012-01-16 15:02:32 +010058
Dieter Baron0e5eeab2012-04-24 18:47:12 +020059 e = za->entry+idx;
60
Thomas Klausner71f5ebd2013-04-29 08:19:33 +020061 old_method = (e->orig == NULL ? ZIP_CM_DEFAULT : e->orig->comp_method);
Dieter Baronb99ed002013-04-27 12:24:01 +020062
Thomas Klausner025d07e2017-04-06 12:33:07 +020063 /* TODO: do we want to recompress if level is set? Only if it's
64 * different than what bit flags tell us, but those are not
65 * defined for all compression methods, or not directly mappable
66 * to levels */
Dieter Baronb99ed002013-04-27 12:24:01 +020067
68 if (method == old_method) {
Thomas Klausner7dec8172012-06-05 14:58:34 +020069 if (e->changes) {
70 e->changes->changed &= ~ZIP_DIRENT_COMP_METHOD;
Thomas Klausner025d07e2017-04-06 12:33:07 +020071 e->changes->compression_level = 0;
Thomas Klausner7dec8172012-06-05 14:58:34 +020072 if (e->changes->changed == 0) {
73 _zip_dirent_free(e->changes);
74 e->changes = NULL;
75 }
76 }
77 }
78 else {
Dieter Baron6fad8b62012-10-12 15:18:04 +020079 if (e->changes == NULL) {
80 if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) {
Thomas Klausnerea8ba492014-09-23 16:54:47 +020081 zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
Dieter Baron6fad8b62012-10-12 15:18:04 +020082 return -1;
83 }
Dieter Baron6fad8b62012-10-12 15:18:04 +020084 }
Dieter Baron70c46242013-04-17 10:59:04 +020085
86 e->changes->comp_method = method;
Thomas Klausner025d07e2017-04-06 12:33:07 +020087 e->changes->compression_level = (zip_uint16_t)flags;
Dieter Baron70c46242013-04-17 10:59:04 +020088 e->changes->changed |= ZIP_DIRENT_COMP_METHOD;
Dieter Baron0e5eeab2012-04-24 18:47:12 +020089 }
Dieter Baron16d2e632012-01-16 15:02:32 +010090
91 return 0;
92}