1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef _ZIP_ARCHIVE_H
17#define _ZIP_ARCHIVE_H
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#include <stdint.h>
24
25#define PATH_BUF_SIZE 512
26/* used for read zip file */
27static const uint32_t LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
28static const uint32_t CENTRAL_SIGNATURE = 0x02014b50;
29static const uint32_t EOCD_SIGNATURE = 0x06054b50;
30static const uint16_t COMPRESS_STORED = 0;
31static const char* ZIP_FILE_PATH_SEPARATOR = "!/";
32
33struct zip_info {
34    int fd;
35    bool found;
36    uint64_t file_offset;
37    uint16_t file_path_index;
38    char path_buf[PATH_BUF_SIZE];
39};
40
41/* Zip Format:
42 * -------------------------------------------------------
43 * | Local file header 1             |                   |
44 * -----------------------------------                   |
45 * | File data 1                     |                   |
46 * -----------------------------------                   |
47 * | Data descriptor 1               |                   |
48 * -----------------------------------    File Entry     |
49 * | ...                             |                   |
50 * -----------------------------------                   |
51 * | Local file header n             |                   |
52 * -----------------------------------                   |
53 * | File data n                     |                   |
54 * -----------------------------------                   |
55 * | Data descriptor n               |                   |
56 * -------------------------------------------------------
57 * | Central dir entry 1             |                   |
58 * -----------------------------------                   |
59 * | ...                             | Central Directory |
60 * -----------------------------------                   |
61 * | Central dir entry n             |                   |
62 * -------------------------------------------------------
63 * | End of central directory record |       EOCD        |
64 * ------------------------------------------------------- */
65
66// Local file header
67struct __attribute__((packed)) local_file_header {
68    uint32_t signature;
69    uint16_t version_needed;
70    uint16_t flags;
71    uint16_t compression_method;
72    uint16_t modified_time;
73    uint16_t modified_date;
74    uint32_t crc;
75    uint32_t compressed_size;
76    uint32_t uncompressed_size;
77    uint16_t name_size;
78    uint16_t extra_size;
79};
80
81// Central dir entry
82struct __attribute__((packed)) central_dir_entry {
83    uint32_t signature;
84    uint16_t version_made;
85    uint16_t version_needed;
86    uint16_t flags;
87    uint16_t compression_method;
88    uint16_t modified_time;
89    uint16_t modified_date;
90    uint32_t crc;
91    uint32_t compressed_size;
92    uint32_t uncompressed_size;
93    uint16_t name_size;
94    uint16_t extra_size;
95    uint16_t comment_size;
96    uint16_t disk_num_start;
97    uint16_t internal_attr;
98    uint32_t external_attr;
99    uint32_t local_header_offset;
100};
101
102// End of central directory record
103struct __attribute__((packed)) zip_end_locator {
104    uint32_t signature;
105    uint16_t num_disk;
106    uint16_t start_disk_of_central_dir;
107    uint16_t total_entries_in_disk;
108    uint16_t total_entries; /* Total number of central directory entrys. */
109    uint32_t size_of_central_dir;
110    uint32_t offset;  /* Offset of start of central directory entry. */
111    uint16_t comment_len;
112};
113
114int open_uncompressed_library_in_zipfile(const char *path, struct zip_info *z_info, char *separator);
115
116#ifdef __cplusplus
117}
118#endif
119
120#endif
121