Rizin
unix-like reverse engineering framework and cli tools
zip_source_get_file_attributes.c
Go to the documentation of this file.
1 /*
2  zip_source_get_file_attributes.c -- get attributes for file from source
3  Copyright (C) 2020 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 <info@libzip.org>
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 #include "zipint.h"
35 
36 ZIP_EXTERN void
38  attributes->valid = 0;
39  attributes->version = 1;
40 }
41 
42 int
44  if (src->source_closed) {
45  return -1;
46  }
47  if (attributes == NULL) {
48  zip_error_set(&src->error, ZIP_ER_INVAL, 0);
49  return -1;
50  }
51 
52  zip_file_attributes_init(attributes);
53 
55  if (_zip_source_call(src, attributes, sizeof(*attributes), ZIP_SOURCE_GET_FILE_ATTRIBUTES) < 0) {
56  return -1;
57  }
58  }
59 
61  zip_file_attributes_t lower_attributes;
62 
63  if (zip_source_get_file_attributes(src->src, &lower_attributes) < 0) {
64  _zip_error_set_from_source(&src->error, src->src);
65  return -1;
66  }
67 
68  if ((lower_attributes.valid & ZIP_FILE_ATTRIBUTES_HOST_SYSTEM) && (attributes->valid & ZIP_FILE_ATTRIBUTES_HOST_SYSTEM) == 0) {
69  attributes->host_system = lower_attributes.host_system;
71  }
72  if ((lower_attributes.valid & ZIP_FILE_ATTRIBUTES_ASCII) && (attributes->valid & ZIP_FILE_ATTRIBUTES_ASCII) == 0) {
73  attributes->ascii = lower_attributes.ascii;
74  attributes->valid |= ZIP_FILE_ATTRIBUTES_ASCII;
75  }
76  if ((lower_attributes.valid & ZIP_FILE_ATTRIBUTES_VERSION_NEEDED)) {
77  if (attributes->valid & ZIP_FILE_ATTRIBUTES_VERSION_NEEDED) {
78  attributes->version_needed = ZIP_MAX(lower_attributes.version_needed, attributes->version_needed);
79  }
80  else {
81  attributes->version_needed = lower_attributes.version_needed;
83  }
84  }
86  attributes->external_file_attributes = lower_attributes.external_file_attributes;
88  }
89  if ((lower_attributes.valid & ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS)) {
91  /* only take from lower level what is not defined at current level */
92  lower_attributes.general_purpose_bit_mask &= ~attributes->general_purpose_bit_mask;
93 
94  attributes->general_purpose_bit_flags |= lower_attributes.general_purpose_bit_flags & lower_attributes.general_purpose_bit_mask;
95  attributes->general_purpose_bit_mask |= lower_attributes.general_purpose_bit_mask;
96  }
97  else {
99  attributes->general_purpose_bit_flags = lower_attributes.general_purpose_bit_flags;
100  attributes->general_purpose_bit_mask = lower_attributes.general_purpose_bit_mask;
101  }
102  }
103  }
104 
105  return 0;
106 }
lzma_index * src
Definition: index.h:567
#define NULL
Definition: cris-opc.c:27
#define ZIP_FILE_ATTRIBUTES_ASCII
Definition: zip.h:330
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_FILE_ATTRIBUTES_HOST_SYSTEM
Definition: zip.h:329
#define ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES
Definition: zip.h:332
#define ZIP_EXTERN
Definition: zip.h:54
#define ZIP_SOURCE_MAKE_COMMAND_BITMASK(cmd)
Definition: zip.h:243
@ ZIP_SOURCE_GET_FILE_ATTRIBUTES
Definition: zip.h:239
#define ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS
Definition: zip.h:333
#define ZIP_ER_INVAL
Definition: zip.h:123
#define ZIP_FILE_ATTRIBUTES_VERSION_NEEDED
Definition: zip.h:331
zip_uint8_t host_system
Definition: zip.h:321
zip_uint16_t general_purpose_bit_mask
Definition: zip.h:326
zip_uint8_t ascii
Definition: zip.h:322
zip_uint16_t general_purpose_bit_flags
Definition: zip.h:325
zip_uint8_t version_needed
Definition: zip.h:323
zip_uint32_t external_file_attributes
Definition: zip.h:324
zip_uint64_t valid
Definition: zip.h:319
zip_uint8_t version
Definition: zip.h:320
void _zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Definition: zip_error.c:135
zip_int64_t _zip_source_call(zip_source_t *src, void *data, zip_uint64_t length, zip_source_cmd_t command)
int zip_source_get_file_attributes(zip_source_t *src, zip_file_attributes_t *attributes)
ZIP_EXTERN void zip_file_attributes_init(zip_file_attributes_t *attributes)
#define ZIP_MAX(a, b)
Definition: zipint.h:472
#define ZIP_SOURCE_IS_LAYERED(src)
Definition: zipint.h:404