1 /**
2 * Copyright (c) 2021-2022 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 #include "zip_archive.h"
17 #include "utils/logger.h"
18
19 namespace panda {
20
21 constexpr size_t ZIP_MAGIC_MASK = 0xff;
22 constexpr size_t ZIP_MAGIC_OFFSET = 8U;
23
IsZipMagic(uint32_t magic)24 bool IsZipMagic(uint32_t magic)
25 {
26 return ((((magic >> 0U) & ZIP_MAGIC_MASK) == 'P') && (((magic >> ZIP_MAGIC_OFFSET) & ZIP_MAGIC_MASK) == 'K'));
27 }
28
OpenArchive(ZipArchiveHandle & handle,const char * path)29 int OpenArchive(ZipArchiveHandle &handle, const char *path)
30 {
31 handle = unzOpen(path);
32 if (handle == nullptr) {
33 LOG(ERROR, ZIPARCHIVE) << "OpenArchive failed, filename is " << path;
34 return ZIPARCHIVE_ERR;
35 }
36 return ZIPARCHIVE_OK;
37 }
38
OpenArchiveFile(ZipArchiveHandle & handle,FILE * fp)39 int OpenArchiveFile(ZipArchiveHandle &handle, FILE *fp)
40 {
41 handle = unzOpenFile(fp);
42 if (handle == nullptr) {
43 LOG(ERROR, ZIPARCHIVE) << "OpenArchive failed from FILE *fp";
44 return ZIPARCHIVE_ERR;
45 }
46 return ZIPARCHIVE_OK;
47 }
48
CloseArchive(ZipArchiveHandle & handle)49 int CloseArchive(ZipArchiveHandle &handle)
50 {
51 if (handle == nullptr) {
52 LOG(ERROR, ZIPARCHIVE) << "ZipArchiveHandle handle should not be nullptr";
53 return ZIPARCHIVE_ERR;
54 }
55 int err = unzClose(handle);
56 if (err != UNZ_OK) {
57 LOG(ERROR, ZIPARCHIVE) << "unzClose with error: " << err;
58 return ZIPARCHIVE_ERR;
59 }
60 return ZIPARCHIVE_OK;
61 }
62
CloseArchiveFile(ZipArchiveHandle & handle)63 int CloseArchiveFile(ZipArchiveHandle &handle)
64 {
65 if (handle == nullptr) {
66 LOG(ERROR, ZIPARCHIVE) << "ZipArchiveHandle handle should not be nullptr";
67 return ZIPARCHIVE_ERR;
68 }
69 int err = unzCloseFile(handle);
70 if (err != UNZ_OK) {
71 LOG(ERROR, ZIPARCHIVE) << "unzCloseFile with error: " << err;
72 return ZIPARCHIVE_ERR;
73 }
74 return ZIPARCHIVE_OK;
75 }
76
GetGlobalFileInfo(ZipArchiveHandle & handle,GlobalStat * gstat)77 int GetGlobalFileInfo(ZipArchiveHandle &handle, GlobalStat *gstat)
78 {
79 int err = unzGetGlobalInfo(handle, &gstat->ginfo);
80 if (err != UNZ_OK) {
81 LOG(ERROR, ZIPARCHIVE) << "GetGlobalFileInfo with error: " << err;
82 return ZIPARCHIVE_ERR;
83 }
84 return ZIPARCHIVE_OK;
85 }
86
GoToNextFile(ZipArchiveHandle & handle)87 int GoToNextFile(ZipArchiveHandle &handle)
88 {
89 int err = unzGoToNextFile(handle);
90 if (err != UNZ_OK) {
91 LOG(ERROR, ZIPARCHIVE) << "GoToNextFile with error: " << err;
92 return ZIPARCHIVE_ERR;
93 }
94 return ZIPARCHIVE_OK;
95 }
96
LocateFile(ZipArchiveHandle & handle,const char * filename)97 int LocateFile(ZipArchiveHandle &handle, const char *filename)
98 {
99 int err = unzLocateFile2(handle, filename, 0);
100 if (err != UNZ_OK) {
101 LOG(ERROR, ZIPARCHIVE) << filename << " is not found in the zipfile";
102 return ZIPARCHIVE_ERR;
103 }
104 return ZIPARCHIVE_OK;
105 }
106
GetCurrentFileInfo(ZipArchiveHandle & handle,EntryFileStat * entry)107 int GetCurrentFileInfo(ZipArchiveHandle &handle, EntryFileStat *entry)
108 {
109 int err = unzGetCurrentFileInfo(handle, &entry->file_stat, nullptr, 0, nullptr, 0, nullptr, 0);
110 if (err != UNZ_OK) {
111 LOG(ERROR, ZIPARCHIVE) << "unzGetCurrentFileInfo failed!";
112 return ZIPARCHIVE_ERR;
113 }
114 return ZIPARCHIVE_OK;
115 }
116
OpenCurrentFile(ZipArchiveHandle & handle)117 int OpenCurrentFile(ZipArchiveHandle &handle)
118 {
119 int err = unzOpenCurrentFile(handle);
120 if (err != UNZ_OK) {
121 LOG(ERROR, ZIPARCHIVE) << "OpenCurrentFile failed!";
122 return ZIPARCHIVE_ERR;
123 }
124 return ZIPARCHIVE_OK;
125 }
126
GetCurrentFileOffset(ZipArchiveHandle & handle,EntryFileStat * entry)127 void GetCurrentFileOffset(ZipArchiveHandle &handle, EntryFileStat *entry)
128 {
129 entry->offset = static_cast<uint32_t>(unzGetCurrentFileZStreamPos64(handle));
130 }
131
CloseCurrentFile(ZipArchiveHandle & handle)132 int CloseCurrentFile(ZipArchiveHandle &handle)
133 {
134 int err = unzCloseCurrentFile(handle);
135 if (err != UNZ_OK) {
136 LOG(ERROR, ZIPARCHIVE) << "CloseCurrentFile failed!";
137 return ZIPARCHIVE_ERR;
138 }
139 return ZIPARCHIVE_OK;
140 }
141
ExtractToMemory(ZipArchiveHandle & handle,void * buf,size_t buf_size)142 int ExtractToMemory(ZipArchiveHandle &handle, void *buf, size_t buf_size)
143 {
144 int size = unzReadCurrentFile(handle, buf, buf_size);
145 if (size < 0) {
146 LOG(ERROR, ZIPARCHIVE) << "ExtractToMemory failed!";
147 return ZIPARCHIVE_ERR;
148 }
149 LOG(INFO, ZIPARCHIVE) << "ExtractToMemory size is " << size;
150 return ZIPARCHIVE_OK;
151 }
152
CreateOrAddFileIntoZip(const char * zipname,const char * filename,const void * pbuf,size_t buf_size,int append,int level)153 int CreateOrAddFileIntoZip(const char *zipname, const char *filename, const void *pbuf, size_t buf_size, int append,
154 int level)
155 {
156 zipFile zfile = nullptr;
157 zfile = zipOpen(zipname, append);
158 if (zfile == nullptr) {
159 LOG(ERROR, ZIPARCHIVE) << "CreateArchive failed, zipname is " << zipname;
160 return ZIPARCHIVE_ERR;
161 }
162 int success = ZIPARCHIVE_OK;
163 int err = zipOpenNewFileInZip(zfile, filename, nullptr, nullptr, 0, nullptr, 0, nullptr,
164 (level != 0) ? Z_DEFLATED : 0, level);
165 if (err != UNZ_OK) {
166 LOG(ERROR, ZIPARCHIVE) << "zipOpenNewFileInZip failed!, zipname is" << zipname << ", filename is " << filename;
167 return ZIPARCHIVE_ERR;
168 }
169 err = zipWriteInFileInZip(zfile, pbuf, buf_size);
170 if (err != UNZ_OK) {
171 LOG(ERROR, ZIPARCHIVE) << "zipWriteInFileInZip failed!, zipname is" << zipname << ", filename is " << filename;
172 success = ZIPARCHIVE_ERR;
173 }
174 err = zipCloseFileInZip(zfile);
175 if (err != UNZ_OK) {
176 LOG(ERROR, ZIPARCHIVE) << "zipCloseFileInZip failed!, zipname is" << zipname << ", filename is " << filename;
177 }
178 err = zipClose(zfile, nullptr);
179 if (err != UNZ_OK) {
180 LOG(ERROR, ZIPARCHIVE) << "CloseArcive failed!, zipname is" << zipname;
181 }
182 return success;
183 }
184 } // namespace panda
185