Fixed sdps crash for some size flash.bin
[mfgtools.git] / libuuu / zip.h
blob3a5e7a9f99429b5f41e48138dd99ae3b883c3bfd
1 /*
2 * Copyright 2018 NXP.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
14 * Neither the name of the NXP Semiconductor nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #pragma once
33 #include "backfile.h"
35 #include "zlib.h"
37 #include <cstdint>
38 #include <map>
39 #include <memory>
41 /* Allow opportunistic use of the C++17 fall-through attribute . */
42 #if defined(__cplusplus) && __cplusplus >= 201703L
43 #define FALLTHROUGH [[fallthrough]]
44 #else
45 #define FALLTHROUGH
46 #endif
48 class FileBuffer;
50 #pragma pack(1)
51 struct Zip_data_desc
53 uint32_t sign;
54 uint32_t crc;
55 uint32_t compressed_size;
56 uint32_t uncompressed_size;
59 struct Zip_file_desc
61 uint32_t sign;
62 uint16_t version_mini_extract;
63 uint16_t flags;
64 uint16_t compress_method;
65 uint16_t last_modify_time;
66 uint16_t last_modify_date;
67 uint32_t crc;
68 uint32_t compressed_size;
69 uint32_t uncompressed_size;
70 uint16_t file_name_length;
71 uint16_t extrafield_length;
72 uint8_t filename[0];
74 struct Zip_central_dir
76 uint32_t sign;
77 uint16_t version;
78 uint16_t version_mini_extract;
79 uint16_t flags;
80 uint16_t compress_method;
81 uint16_t last_modify_time;
82 uint16_t last_modify_date;
83 uint32_t crc;
84 uint32_t compressed_size;
85 uint32_t uncompressed_size;
86 uint16_t file_name_length;
87 uint16_t extrafield_length;
88 uint16_t file_comment_length;
89 uint16_t disk_number;
90 uint16_t internal_file_attr;
91 uint32_t external_file_attr;
92 uint32_t offset;
93 uint8_t filename[0];
96 struct Zip64_central_dir
98 uint32_t sign;
99 uint16_t version;
100 uint16_t version_mini_extract;
101 uint16_t flags;
102 uint16_t compress_method;
103 uint16_t last_modify_time;
104 uint16_t last_modify_date;
105 uint32_t crc;
106 uint32_t compressed_size;
107 uint32_t uncompressed_size;
108 uint16_t file_name_length;
109 uint16_t extrafield_length;
110 uint16_t file_comment_length;
111 uint16_t disk_number;
112 uint16_t internal_file_attr;
113 uint32_t external_file_attr;
114 uint32_t offset;
115 uint8_t filename[0];
117 struct Zip_eocd
119 uint32_t sign;
120 uint16_t num_of_thisdisk;
121 uint16_t start_disk_of_dir;
122 uint16_t num_of_dir_ondisk;
123 uint16_t num_of_dir;
124 uint32_t size_of_central_dir;
125 uint32_t offset_of_central_dir;
126 uint16_t length_of_comment;
127 uint8_t comment[0];
130 struct Zip64_eocd_locator
132 uint32_t sign;
133 uint32_t num_of_thisdisk;
134 uint64_t offset_of_eocd;
135 uint32_t total_num_disks;
138 struct Zip64_eocd
140 uint32_t sign;
141 uint64_t size_of_eocd;
142 uint16_t version;
143 uint16_t version_mini_extract;
144 uint32_t num_of_dir_ondisk;
145 uint32_t num_of_disk;
146 uint64_t total_ondisk;
147 uint64_t total;
148 uint64_t size;
149 uint64_t offset;
152 struct Zip_ext
154 uint16_t tag;
155 uint16_t size;
158 #define EOCD_SIGNATURE 0x06054b50
159 #define DIR_SIGNATURE 0x02014b50
160 #define DATA_SIGNATURE 0x08074b50
161 #define FILE_SIGNATURE 0x04034b50
162 #define EOCD64_LOCATOR_SIGNATURE 0x07064b50
163 #define EOCD64_SIGNATURE 0x06064b50
165 class Zip;
167 class Zip_file_Info
169 public:
170 Zip_file_Info();
171 ~Zip_file_Info();
173 int decompress(Zip *pZip, std::shared_ptr<FileBuffer> p);
175 private:
176 std::string m_filename;
177 uint32_t m_timestamp;
178 size_t m_filesize;
179 size_t m_compressedsize;
180 size_t m_offset;
181 z_stream m_strm;
183 friend Zip;
186 class Zip : public Backfile
188 public:
189 int BuildDirInfo();
190 bool check_file_exist(std::string filename);
191 int get_file_buff(std::string filename, std::shared_ptr<FileBuffer>p);
192 int Open(std::string filename);
194 std::map<std::string, Zip_file_Info> m_filemap;
198 #pragma pack()