1 /*
2 * Copyright (c) 2024 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_ffi.h"
17
18 #include <fcntl.h>
19 #include <string>
20 #include <vector>
21
22 #include "app_log_wrapper.h"
23 #include "bundle_error.h"
24 #include "cj_common_ffi.h"
25 #include "cj_zip.h"
26 #include "common_func.h"
27 #include "directory_ex.h"
28 #include "file_path.h"
29 #include "zip_reader.h"
30 #include "zip_utils.h"
31 #include "zlib.h"
32
33 using namespace OHOS::CJSystemapi::BundleManager;
34
35 namespace OHOS {
36 namespace AppExecFwk {
37 namespace LIBZIP {
ZlibBusinessError(int32_t errCode)38 static inline ErrCode ZlibBusinessError(int32_t errCode)
39 {
40 if (errCode == Z_STREAM_ERROR) {
41 return ERR_ZLIB_ZSTREAM_ERROR;
42 } else if (errCode == Z_MEM_ERROR) {
43 return ERR_ZLIB_MEMORY_ALLOC_FAILED;
44 } else if (errCode == Z_BUF_ERROR) {
45 return ERR_ZLIB_BUFFER_ERROR;
46 } else {
47 return ERR_ZLIB_INTERNAL_STRUCT_ERROR;
48 }
49 }
50
51 struct CjZipEntity {
52 int32_t Compress(ZipCompressParam* param);
53 int32_t Compress2(ZipCompressParam* param);
54 int32_t CompressBound(int32_t sourceLen);
55 int32_t UnCompress(ZipCompressParam* param);
56
57 void GetZStream(CZStream* cStrm, int32_t* errCode);
58 int32_t Deflate(CZStream* cStrm, int32_t flush, int32_t* errCode);
59 int32_t DeflateEnd(CZStream* cStrm, int32_t* errCode);
60 int32_t DeflateInit2(CZStream* cStrm, DeflateInit2Param* param, int32_t* errCode);
61
62 int32_t Inflate(CZStream* cStrm, int32_t flush, int32_t* errCode);
63 int32_t InflateEnd(CZStream* cStrm, int32_t* errCode);
64 int32_t InflateInit(CZStream* cStrm, int32_t* errCode);
65 int32_t InflateInit2(CZStream* cStrm, int32_t windowBits, int32_t* errCode);
66
67 void InitialZStream(CZStream* cStrm);
68 void SetZStreamValue(CZStream* cStrm);
69
70 std::unique_ptr<z_stream> zs = {};
71 };
72
Compress(ZipCompressParam * param)73 int32_t CjZipEntity::Compress(ZipCompressParam* param)
74 {
75 if (param->destBuf == nullptr || param->srcBuf == nullptr) {
76 APP_LOGE("destBuf or srcBuf is nullptr");
77 return ERR_ZLIB_INTERNAL_STRUCT_ERROR;
78 }
79 uLong destLen = static_cast<uLong>(param->destBufLen);
80 int32_t status =
81 compress(static_cast<Bytef*>(param->destBuf), &destLen, static_cast<Bytef*>(param->srcBuf), param->srcBufLen);
82 if (status < 0) {
83 APP_LOGE("compress return error: %{public}d", status);
84 return ZlibBusinessError(status);
85 }
86 param->outStatus = status;
87 param->outDestLen = static_cast<int64_t>(destLen);
88 return 0;
89 }
90
Compress2(ZipCompressParam * param)91 int32_t CjZipEntity::Compress2(ZipCompressParam* param)
92 {
93 if (param->destBuf == nullptr || param->srcBuf == nullptr) {
94 APP_LOGE("destBuf or srcBuf is nullptr");
95 return ERR_ZLIB_INTERNAL_STRUCT_ERROR;
96 }
97 uLong destLen = static_cast<uLong>(param->destBufLen);
98 int32_t status = compress2(static_cast<Bytef*>(param->destBuf), &destLen, static_cast<Bytef*>(param->srcBuf),
99 param->srcBufLen, param->level);
100 if (status < 0) {
101 APP_LOGE("compress2 return error: %{public}d", status);
102 return ZlibBusinessError(status);
103 }
104 param->outStatus = status;
105 param->outDestLen = static_cast<int64_t>(destLen);
106 return 0;
107 }
108
CompressBound(int32_t sourceLen)109 int32_t CjZipEntity::CompressBound(int32_t sourceLen)
110 {
111 return static_cast<int32_t>(compressBound(sourceLen));
112 }
113
UnCompress(ZipCompressParam * param)114 int32_t CjZipEntity::UnCompress(ZipCompressParam* param)
115 {
116 if (param->destBuf == nullptr || param->srcBuf == nullptr) {
117 APP_LOGE("destBuf or srcBuf is nullptr");
118 return ERR_ZLIB_INTERNAL_STRUCT_ERROR;
119 }
120 uLong destLen = static_cast<uLong>(param->destBufLen);
121 int32_t status =
122 uncompress(static_cast<Bytef*>(param->destBuf), &destLen, static_cast<Bytef*>(param->srcBuf), param->srcBufLen);
123 if (status < 0) {
124 APP_LOGE("uncompress return error: %{public}d", status);
125 return ZlibBusinessError(status);
126 }
127 param->outStatus = status;
128 param->outDestLen = static_cast<int64_t>(destLen);
129 return 0;
130 }
131
GetZStream(CZStream * cStrm,int32_t * errCode)132 void CjZipEntity::GetZStream(CZStream* cStrm, int32_t* errCode)
133 {
134 if (zs == nullptr) {
135 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
136 APP_LOGE("GetZStream zs is nullptr");
137 return;
138 }
139 cStrm->availableIn = zs->avail_in;
140 cStrm->totalIn = zs->total_in;
141 cStrm->availableOut = zs->avail_out;
142 cStrm->totalOut = zs->total_out;
143 cStrm->dataType = zs->data_type;
144 cStrm->adler = zs->adler;
145 }
146
Deflate(CZStream * cStrm,int32_t flush,int32_t * errCode)147 int32_t CjZipEntity::Deflate(CZStream* cStrm, int32_t flush, int32_t* errCode)
148 {
149 SetZStreamValue(cStrm);
150 int32_t status = deflate(zs.get(), flush);
151 if (status < 0) {
152 APP_LOGE("deflate return error: %{public}d", status);
153 *errCode = ZlibBusinessError(status);
154 }
155 return status;
156 }
157
DeflateEnd(CZStream * cStrm,int32_t * errCode)158 int32_t CjZipEntity::DeflateEnd(CZStream* cStrm, int32_t* errCode)
159 {
160 SetZStreamValue(cStrm);
161 int32_t status = deflateEnd(zs.get());
162 if (status < 0) {
163 APP_LOGE("deflateEnd return error: %{public}d", status);
164 *errCode = ZlibBusinessError(status);
165 }
166 return status;
167 }
168
DeflateInit2(CZStream * cStrm,DeflateInit2Param * param,int32_t * errCode)169 int32_t CjZipEntity::DeflateInit2(CZStream* cStrm, DeflateInit2Param* param, int32_t* errCode)
170 {
171 zs = std::make_unique<z_stream>();
172 InitialZStream(cStrm);
173 int32_t status =
174 deflateInit2(zs.get(), param->level, param->method, param->windowBits, param->memLevel, param->strategy);
175 if (status < 0) {
176 APP_LOGE("deflateInit2 return error: %{public}d", status);
177 *errCode = ZlibBusinessError(status);
178 }
179 return status;
180 }
181
Inflate(CZStream * cStrm,int32_t flush,int32_t * errCode)182 int32_t CjZipEntity::Inflate(CZStream* cStrm, int32_t flush, int32_t* errCode)
183 {
184 SetZStreamValue(cStrm);
185 int32_t status = inflate(zs.get(), flush);
186 if (status < 0) {
187 APP_LOGE("inflate return error: %{public}d", status);
188 *errCode = ZlibBusinessError(status);
189 }
190 return status;
191 }
192
InflateEnd(CZStream * cStrm,int32_t * errCode)193 int32_t CjZipEntity::InflateEnd(CZStream* cStrm, int32_t* errCode)
194 {
195 SetZStreamValue(cStrm);
196 int32_t status = inflateEnd(zs.get());
197 if (status < 0) {
198 APP_LOGE("inflateEnd return error: %{public}d", status);
199 *errCode = ZlibBusinessError(status);
200 }
201 return status;
202 }
203
InflateInit(CZStream * cStrm,int32_t * errCode)204 int32_t CjZipEntity::InflateInit(CZStream* cStrm, int32_t* errCode)
205 {
206 zs = std::make_unique<z_stream>();
207 InitialZStream(cStrm);
208 int32_t status = inflateInit(zs.get());
209 if (status < 0) {
210 APP_LOGE("inflateInit return error: %{public}d", status);
211 *errCode = ZlibBusinessError(status);
212 }
213 return status;
214 }
215
InflateInit2(CZStream * cStrm,int32_t windowBits,int32_t * errCode)216 int32_t CjZipEntity::InflateInit2(CZStream* cStrm, int32_t windowBits, int32_t* errCode)
217 {
218 zs = std::make_unique<z_stream>();
219 InitialZStream(cStrm);
220 int32_t status = inflateInit2(zs.get(), windowBits);
221 if (status < 0) {
222 APP_LOGE("inflateInit2 return error: %{public}d", status);
223 *errCode = ZlibBusinessError(status);
224 }
225 return status;
226 }
227
InitialZStream(CZStream * cStrm)228 void CjZipEntity::InitialZStream(CZStream* cStrm)
229 {
230 if (cStrm->hasNextIn) {
231 zs->next_in = reinterpret_cast<Bytef*>(cStrm->nextIn);
232 }
233 if (cStrm->hasAvailableIn) {
234 zs->avail_in = static_cast<uInt>(cStrm->availableIn);
235 }
236 if (cStrm->hasTotalIn) {
237 zs->total_in = static_cast<uLong>(cStrm->totalIn);
238 }
239 if (cStrm->hasNextOut) {
240 zs->next_out = reinterpret_cast<Bytef*>(cStrm->nextOut);
241 }
242 if (cStrm->hasAvailableOut) {
243 zs->avail_out = static_cast<uInt>(cStrm->availableOut);
244 }
245 if (cStrm->hasTotalOut) {
246 zs->total_out = static_cast<uLong>(cStrm->totalOut);
247 }
248 if (cStrm->hasDataType) {
249 zs->data_type = cStrm->dataType;
250 }
251 if (cStrm->hasAdler) {
252 zs->adler = static_cast<uLong>(cStrm->adler);
253 }
254 }
255
SetZStreamValue(CZStream * cStrm)256 void CjZipEntity::SetZStreamValue(CZStream* cStrm)
257 {
258 if (!zs) {
259 zs = std::make_unique<z_stream>();
260 }
261 InitialZStream(cStrm);
262 }
263
GetOriginalSize(PlatformFile zipFd,int64_t & originalSize)264 ErrCode GetOriginalSize(PlatformFile zipFd, int64_t& originalSize)
265 {
266 ZipReader reader;
267 if (!reader.OpenFromPlatformFile(zipFd)) {
268 APP_LOGE("Failed to open, not ZIP format or damaged");
269 return ERR_ZLIB_SRC_FILE_FORMAT_ERROR;
270 }
271 int64_t totalSize = 0;
272 while (reader.HasMore()) {
273 if (!reader.OpenCurrentEntryInZip()) {
274 APP_LOGE("Failed to open the current file in zip");
275 return ERR_ZLIB_SRC_FILE_FORMAT_ERROR;
276 }
277 const FilePath& constEntryPath = reader.CurrentEntryInfo()->GetFilePath();
278 FilePath entryPath = constEntryPath;
279 if (reader.CurrentEntryInfo()->IsUnsafe()) {
280 APP_LOGE("Found an unsafe file in zip");
281 return ERR_ZLIB_SRC_FILE_FORMAT_ERROR;
282 }
283 totalSize += reader.CurrentEntryInfo()->GetOriginalSize();
284 if (!reader.AdvanceToNextEntry()) {
285 APP_LOGE("Failed to advance to the next file");
286 return ERR_ZLIB_SRC_FILE_FORMAT_ERROR;
287 }
288 }
289 originalSize = totalSize;
290 return ERR_OK;
291 }
292
ZipFileIsValid(const std::string & srcFile)293 bool ZipFileIsValid(const std::string& srcFile)
294 {
295 if ((srcFile.size() == 0) || FilePath::HasRelativePathBaseOnAPIVersion(srcFile)) {
296 APP_LOGE("srcFile len is 0 or ../");
297 return false;
298 }
299 if (!FilePathCheckValid(srcFile)) {
300 APP_LOGE("FilePathCheckValid return false");
301 return false;
302 }
303 FilePath srcFileDir(srcFile);
304 if (!FilePath::PathIsValid(srcFileDir)) {
305 APP_LOGE("PathIsValid return false");
306 return false;
307 }
308 if (!FilePath::PathIsReadable(srcFileDir)) {
309 APP_LOGE("PathIsReadable return false");
310 return false;
311 }
312 return true;
313 }
314
GetOriginalSize(const std::string & srcFile,int64_t & originalSize)315 ErrCode GetOriginalSize(const std::string& srcFile, int64_t& originalSize)
316 {
317 if (!ZipFileIsValid(srcFile)) {
318 return ERR_ZLIB_SRC_FILE_DISABLED;
319 }
320 PlatformFile zipFd = open(srcFile.c_str(), S_IREAD, O_CREAT);
321 if (zipFd == kInvalidPlatformFile) {
322 APP_LOGE("Failed to open file, errno: %{public}d, %{public}s", errno, strerror(errno));
323 return ERR_ZLIB_SRC_FILE_DISABLED;
324 }
325 ErrCode ret = GetOriginalSize(zipFd, originalSize);
326 close(zipFd);
327 return ret;
328 }
329
330 extern "C" {
FfiBundleManagerCompressFile(const char * inFile,const char * outFile,RetOptions options)331 FFI_EXPORT int32_t FfiBundleManagerCompressFile(const char* inFile, const char* outFile, RetOptions options)
332 {
333 std::string strInFile(inFile);
334 std::string strOutFile(outFile);
335 int32_t code = ERROR_CODE_ERRNO;
336 OPTIONS cOptions;
337 cOptions.level = static_cast<COMPRESS_LEVEL>(options.level);
338 cOptions.memLevel = static_cast<MEMORY_LEVEL>(options.memLevel);
339 cOptions.strategy = static_cast<COMPRESS_STRATEGY>(options.strategy);
340
341 code = Zip(strInFile, strOutFile, cOptions);
342 int32_t err = CommonFunc::ConvertErrCode(code);
343
344 return err;
345 }
346
FfiBundleManagerDeCompressFileOptions(const char * inFile,const char * outFile,RetOptions options)347 FFI_EXPORT int32_t FfiBundleManagerDeCompressFileOptions(const char* inFile, const char* outFile, RetOptions options)
348 {
349 std::string strInFile(inFile);
350 std::string strOutFile(outFile);
351 int32_t code = ERROR_CODE_ERRNO;
352 OPTIONS cOptions;
353 cOptions.level = static_cast<COMPRESS_LEVEL>(options.level);
354 cOptions.memLevel = static_cast<MEMORY_LEVEL>(options.memLevel);
355 cOptions.strategy = static_cast<COMPRESS_STRATEGY>(options.strategy);
356
357 code = UnZip(strInFile, strOutFile, cOptions);
358 int32_t err = CommonFunc::ConvertErrCode(code);
359
360 return err;
361 }
362
FfiBundleManagerDeCompressFile(const char * inFile,const char * outFile)363 FFI_EXPORT int32_t FfiBundleManagerDeCompressFile(const char* inFile, const char* outFile)
364 {
365 std::string strInFile(inFile);
366 std::string strOutFile(outFile);
367 int32_t code = ERROR_CODE_ERRNO;
368 OPTIONS cOptions;
369
370 code = UnZip(strInFile, strOutFile, cOptions);
371 int32_t err = CommonFunc::ConvertErrCode(code);
372
373 return err;
374 }
375
FfiBundleManagerZipInstCreate()376 FFI_EXPORT void* FfiBundleManagerZipInstCreate()
377 {
378 return new CjZipEntity();
379 }
380
FfiBundleManagerZipInstDestroy(void * ffiInst)381 FFI_EXPORT void FfiBundleManagerZipInstDestroy(void* ffiInst)
382 {
383 delete static_cast<CjZipEntity*>(ffiInst);
384 }
385
FfiBundleManagerZipCompress(void * ffiInst,ZipCompressParam * param,int32_t * errCode)386 FFI_EXPORT void FfiBundleManagerZipCompress(void* ffiInst, ZipCompressParam* param, int32_t* errCode)
387 {
388 *errCode = 0;
389 if (ffiInst == nullptr || param == nullptr) {
390 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
391 APP_LOGE("FfiBundleManagerZipCompress param check failed");
392 return;
393 }
394 auto entity = static_cast<CjZipEntity*>(ffiInst);
395 *errCode = entity->Compress(param);
396 return;
397 }
398
FfiBundleManagerZipCompress2(void * ffiInst,ZipCompressParam * param,int32_t * errCode)399 FFI_EXPORT void FfiBundleManagerZipCompress2(void* ffiInst, ZipCompressParam* param, int32_t* errCode)
400 {
401 *errCode = 0;
402 if (ffiInst == nullptr || param == nullptr) {
403 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
404 APP_LOGE("FfiBundleManagerZipCompress2 param check failed");
405 return;
406 }
407 auto entity = static_cast<CjZipEntity*>(ffiInst);
408 *errCode = entity->Compress2(param);
409 return;
410 }
411
FfiBundleManagerZipCompressBound(void * ffiInst,int32_t sourceLen,int32_t * errCode)412 FFI_EXPORT int32_t FfiBundleManagerZipCompressBound(void* ffiInst, int32_t sourceLen, int32_t* errCode)
413 {
414 *errCode = 0;
415 if (ffiInst == nullptr) {
416 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
417 APP_LOGE("FfiBundleManagerZipCompressBound param check failed");
418 return 0;
419 }
420 auto entity = static_cast<CjZipEntity*>(ffiInst);
421 return entity->CompressBound(sourceLen);
422 }
423
FfiBundleManagerZipDeflate(void * ffiInst,CZStream * cStrm,int32_t flush,int32_t * errCode)424 FFI_EXPORT int32_t FfiBundleManagerZipDeflate(void* ffiInst, CZStream* cStrm, int32_t flush, int32_t* errCode)
425 {
426 *errCode = 0;
427 if (ffiInst == nullptr || cStrm == nullptr) {
428 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
429 APP_LOGE("FfiBundleManagerZipDeflate param check failed");
430 return 0;
431 }
432 auto entity = static_cast<CjZipEntity*>(ffiInst);
433 return entity->Deflate(cStrm, flush, errCode);
434 }
435
FfiBundleManagerZipDeflateEnd(void * ffiInst,CZStream * cStrm,int32_t * errCode)436 FFI_EXPORT int32_t FfiBundleManagerZipDeflateEnd(void* ffiInst, CZStream* cStrm, int32_t* errCode)
437 {
438 *errCode = 0;
439 if (ffiInst == nullptr || cStrm == nullptr) {
440 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
441 APP_LOGE("FfiBundleManagerZipDeflateEnd param check failed");
442 return 0;
443 }
444 auto entity = static_cast<CjZipEntity*>(ffiInst);
445 return entity->DeflateEnd(cStrm, errCode);
446 }
447
FfiBundleManagerZipDeflateInit2(void * ffiInst,CZStream * cStrm,DeflateInit2Param * param,int32_t * errCode)448 FFI_EXPORT int32_t FfiBundleManagerZipDeflateInit2(
449 void* ffiInst, CZStream* cStrm, DeflateInit2Param* param, int32_t* errCode)
450 {
451 *errCode = 0;
452 if (ffiInst == nullptr || cStrm == nullptr || param == nullptr) {
453 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
454 APP_LOGE("FfiBundleManagerZipDeflateInit2 param check failed");
455 return 0;
456 }
457 auto entity = static_cast<CjZipEntity*>(ffiInst);
458 return entity->DeflateInit2(cStrm, param, errCode);
459 }
460
FfiBundleManagerZipGetZStream(void * ffiInst,CZStream * cStrm,int32_t * errCode)461 FFI_EXPORT void FfiBundleManagerZipGetZStream(void* ffiInst, CZStream* cStrm, int32_t* errCode)
462 {
463 *errCode = 0;
464 if (ffiInst == nullptr || cStrm == nullptr) {
465 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
466 APP_LOGE("FfiBundleManagerZipGetZStream param check failed");
467 return;
468 }
469 auto entity = static_cast<CjZipEntity*>(ffiInst);
470 return entity->GetZStream(cStrm, errCode);
471 }
472
FfiBundleManagerZipInflate(void * ffiInst,CZStream * cStrm,int32_t flush,int32_t * errCode)473 FFI_EXPORT int32_t FfiBundleManagerZipInflate(void* ffiInst, CZStream* cStrm, int32_t flush, int32_t* errCode)
474 {
475 *errCode = 0;
476 if (ffiInst == nullptr || cStrm == nullptr) {
477 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
478 APP_LOGE("FfiBundleManagerZipInflate param check failed");
479 return 0;
480 }
481 auto entity = static_cast<CjZipEntity*>(ffiInst);
482 return entity->Inflate(cStrm, flush, errCode);
483 }
484
FfiBundleManagerZipInflateEnd(void * ffiInst,CZStream * cStrm,int32_t * errCode)485 FFI_EXPORT int32_t FfiBundleManagerZipInflateEnd(void* ffiInst, CZStream* cStrm, int32_t* errCode)
486 {
487 *errCode = 0;
488 if (ffiInst == nullptr || cStrm == nullptr) {
489 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
490 APP_LOGE("FfiBundleManagerZipInflateEnd param check failed");
491 return 0;
492 }
493 auto entity = static_cast<CjZipEntity*>(ffiInst);
494 return entity->InflateEnd(cStrm, errCode);
495 }
496
FfiBundleManagerZipInflateInit(void * ffiInst,CZStream * cStrm,int32_t * errCode)497 FFI_EXPORT int32_t FfiBundleManagerZipInflateInit(void* ffiInst, CZStream* cStrm, int32_t* errCode)
498 {
499 *errCode = 0;
500 if (ffiInst == nullptr || cStrm == nullptr) {
501 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
502 APP_LOGE("FfiBundleManagerZipInflateInit param check failed");
503 return 0;
504 }
505 auto entity = static_cast<CjZipEntity*>(ffiInst);
506 return entity->InflateInit(cStrm, errCode);
507 }
508
FfiBundleManagerZipInflateInit2(void * ffiInst,CZStream * cStrm,int32_t windowBits,int32_t * errCode)509 FFI_EXPORT int32_t FfiBundleManagerZipInflateInit2(void* ffiInst, CZStream* cStrm, int32_t windowBits, int32_t* errCode)
510 {
511 *errCode = 0;
512 if (ffiInst == nullptr || cStrm == nullptr) {
513 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
514 APP_LOGE("FfiBundleManagerZipInflateInit2 param check failed");
515 return 0;
516 }
517 auto entity = static_cast<CjZipEntity*>(ffiInst);
518 return entity->InflateInit2(cStrm, windowBits, errCode);
519 }
520
FfiBundleManagerZipUnCompress(void * ffiInst,ZipCompressParam * param,int32_t * errCode)521 FFI_EXPORT void FfiBundleManagerZipUnCompress(void* ffiInst, ZipCompressParam* param, int32_t* errCode)
522 {
523 *errCode = 0;
524 if (ffiInst == nullptr || param == nullptr) {
525 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
526 APP_LOGE("FfiBundleManagerZipUnCompress param check failed");
527 return;
528 }
529 auto entity = static_cast<CjZipEntity*>(ffiInst);
530 *errCode = entity->UnCompress(param);
531 return;
532 }
533
FfiBundleManagerCompressFiles(CArrString cInFiles,const char * cOutFile,RetOptions cOptions)534 FFI_EXPORT int32_t FfiBundleManagerCompressFiles(CArrString cInFiles, const char* cOutFile, RetOptions cOptions)
535 {
536 if (cInFiles.head == nullptr || cInFiles.size <= 0 || cOutFile == nullptr) {
537 APP_LOGE("FfiBundleManagerCompressFiles param check failed");
538 return ERR_ZLIB_INTERNAL_STRUCT_ERROR;
539 }
540 std::vector<std::string> inFiles;
541 inFiles.reserve(cInFiles.size);
542 for (int64_t i = 0; i < cInFiles.size; i++) {
543 if (cInFiles.head[i] == nullptr) {
544 APP_LOGE("FfiBundleManagerCompressFiles param check failed");
545 return ERR_ZLIB_INTERNAL_STRUCT_ERROR;
546 }
547 inFiles.push_back(cInFiles.head[i]);
548 }
549 OPTIONS options;
550 options.level = static_cast<COMPRESS_LEVEL>(cOptions.level);
551 options.memLevel = static_cast<MEMORY_LEVEL>(cOptions.memLevel);
552 options.strategy = static_cast<COMPRESS_STRATEGY>(cOptions.strategy);
553 int32_t code = Zips(inFiles, cOutFile, options);
554 return CommonFunc::ConvertErrCode(code);
555 }
556
FfiBundleManagerGetOriginalSize(const char * compressedFile,int32_t * errCode)557 FFI_EXPORT int64_t FfiBundleManagerGetOriginalSize(const char* compressedFile, int32_t* errCode)
558 {
559 *errCode = 0;
560 if (compressedFile == nullptr) {
561 APP_LOGE("FfiBundleManagerGetOriginalSize param check failed");
562 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
563 return 0;
564 }
565
566 int64_t originalSize = 0;
567 int32_t err = GetOriginalSize(compressedFile, originalSize);
568 if (err != ERR_OK) {
569 *errCode = CommonFunc::ConvertErrCode(err);
570 }
571 return originalSize;
572 }
573 }
574 } // LIBZIP
575 } // AppExecFwk
576 } // OHOS
577