Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | zip_set_file_compression.c -- set compression for file in archive |
Thomas Klausner | c36a6f4 | 2017-12-14 11:57:47 +0100 | [diff] [blame] | 3 | Copyright (C) 2012-2017 Dieter Baron and Thomas Klausner |
Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 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 | |
Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 34 | |
| 35 | #include "zipint.h" |
| 36 | |
Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 37 | |
| 38 | ZIP_EXTERN int |
Dieter Baron | c548a18 | 2014-10-10 18:27:50 +0200 | [diff] [blame] | 39 | zip_set_file_compression(zip_t *za, zip_uint64_t idx, zip_int32_t method, zip_uint32_t flags) |
Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 40 | { |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 41 | zip_entry_t *e; |
Thomas Klausner | 71f5ebd | 2013-04-29 08:19:33 +0200 | [diff] [blame] | 42 | zip_int32_t old_method; |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 43 | |
Thomas Klausner | 025d07e | 2017-04-06 12:33:07 +0200 | [diff] [blame] | 44 | if (idx >= za->nentry || flags > 9) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 45 | zip_error_set(&za->error, ZIP_ER_INVAL, 0); |
Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 46 | return -1; |
| 47 | } |
| 48 | |
Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 49 | if (ZIP_IS_RDONLY(za)) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 50 | zip_error_set(&za->error, ZIP_ER_RDONLY, 0); |
Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 51 | return -1; |
| 52 | } |
| 53 | |
Thomas Klausner | cc5b86f | 2017-03-30 15:48:45 +0200 | [diff] [blame] | 54 | if (!zip_compression_method_supported(method, true)) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 55 | zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 56 | return -1; |
| 57 | } |
Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 58 | |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 59 | e = za->entry+idx; |
| 60 | |
Thomas Klausner | 71f5ebd | 2013-04-29 08:19:33 +0200 | [diff] [blame] | 61 | old_method = (e->orig == NULL ? ZIP_CM_DEFAULT : e->orig->comp_method); |
Dieter Baron | b99ed00 | 2013-04-27 12:24:01 +0200 | [diff] [blame] | 62 | |
Thomas Klausner | 025d07e | 2017-04-06 12:33:07 +0200 | [diff] [blame] | 63 | /* 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 Baron | b99ed00 | 2013-04-27 12:24:01 +0200 | [diff] [blame] | 67 | |
| 68 | if (method == old_method) { |
Thomas Klausner | 7dec817 | 2012-06-05 14:58:34 +0200 | [diff] [blame] | 69 | if (e->changes) { |
| 70 | e->changes->changed &= ~ZIP_DIRENT_COMP_METHOD; |
Thomas Klausner | 025d07e | 2017-04-06 12:33:07 +0200 | [diff] [blame] | 71 | e->changes->compression_level = 0; |
Thomas Klausner | 7dec817 | 2012-06-05 14:58:34 +0200 | [diff] [blame] | 72 | if (e->changes->changed == 0) { |
| 73 | _zip_dirent_free(e->changes); |
| 74 | e->changes = NULL; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | else { |
Dieter Baron | 6fad8b6 | 2012-10-12 15:18:04 +0200 | [diff] [blame] | 79 | if (e->changes == NULL) { |
| 80 | if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 81 | zip_error_set(&za->error, ZIP_ER_MEMORY, 0); |
Dieter Baron | 6fad8b6 | 2012-10-12 15:18:04 +0200 | [diff] [blame] | 82 | return -1; |
| 83 | } |
Dieter Baron | 6fad8b6 | 2012-10-12 15:18:04 +0200 | [diff] [blame] | 84 | } |
Dieter Baron | 70c4624 | 2013-04-17 10:59:04 +0200 | [diff] [blame] | 85 | |
| 86 | e->changes->comp_method = method; |
Thomas Klausner | 025d07e | 2017-04-06 12:33:07 +0200 | [diff] [blame] | 87 | e->changes->compression_level = (zip_uint16_t)flags; |
Dieter Baron | 70c4624 | 2013-04-17 10:59:04 +0200 | [diff] [blame] | 88 | e->changes->changed |= ZIP_DIRENT_COMP_METHOD; |
Dieter Baron | 0e5eeab | 2012-04-24 18:47:12 +0200 | [diff] [blame] | 89 | } |
Dieter Baron | 16d2e63 | 2012-01-16 15:02:32 +0100 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |