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 #include <string>
18 #include <vector>
19 #include "zip_utils.h"
20 #include "cj_zip.h"
21 #include "cj_common_ffi.h"
22 #include "common_func.h"
23 #include "app_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace LIBZIP {
28 
29 extern "C" {
FfiBundleManagerCompressFile(CArrUI8 inFile, CArrUI8 outFile, RetOptions options)30     int32_t FfiBundleManagerCompressFile(CArrUI8 inFile, CArrUI8 outFile, RetOptions options)
31     {
32         std::vector<uint8_t> inBytesVec;
33         for (int i = 0; i < inFile.len; i++) {
34             inBytesVec.push_back(inFile.data[i]);
35         }
36         std::string strInFile(inBytesVec.begin(), inBytesVec.end());
37         std::vector<uint8_t> outBytesVec;
38         for (int i = 0; i < outFile.len; i++) {
39             outBytesVec.push_back(outFile.data[i]);
40         }
41         std::string strOutFile(outBytesVec.begin(), outBytesVec.end());
42         int32_t code = ERROR_CODE_ERRNO;
43         OPTIONS cOptions;
44         cOptions.level = static_cast<COMPRESS_LEVEL>(options.level);
45         cOptions.memLevel = static_cast<MEMORY_LEVEL>(options.memLevel);
46         cOptions.strategy = static_cast<COMPRESS_STRATEGY>(options.strategy);
47 
48         code = Zip(strInFile, strOutFile, cOptions);
49         int32_t err = CommonFunc::ConvertErrCode(code);
50 
51         return err;
52     }
53 
54 
FfiBundleManagerDeCompressFileOptions(CArrUI8 inFile, CArrUI8 outFile, RetOptions options)55     int32_t FfiBundleManagerDeCompressFileOptions(CArrUI8 inFile, CArrUI8 outFile, RetOptions options)
56     {
57         std::vector<uint8_t> inBytesVec;
58         for (int i = 0; i < inFile.len; i++) {
59             inBytesVec.push_back(inFile.data[i]);
60         }
61         std::string strInFile(inBytesVec.begin(), inBytesVec.end());
62         std::vector<uint8_t> outBytesVec;
63         for (int i = 0; i < outFile.len; i++) {
64             outBytesVec.push_back(outFile.data[i]);
65         }
66         std::string strOutFile(outBytesVec.begin(), outBytesVec.end());
67         int32_t code = ERROR_CODE_ERRNO;
68         OPTIONS cOptions;
69         cOptions.level = static_cast<COMPRESS_LEVEL>(options.level);
70         cOptions.memLevel = static_cast<MEMORY_LEVEL>(options.memLevel);
71         cOptions.strategy = static_cast<COMPRESS_STRATEGY>(options.strategy);
72 
73         code = UnZip(strInFile, strOutFile, cOptions);
74         int32_t err = CommonFunc::ConvertErrCode(code);
75 
76         return err;
77     }
78 
FfiBundleManagerDeCompressFile(CArrUI8 inFile, CArrUI8 outFile)79     int32_t FfiBundleManagerDeCompressFile(CArrUI8 inFile, CArrUI8 outFile)
80     {
81         std::vector<uint8_t> inBytesVec;
82         for (int i = 0; i < inFile.len; i++) {
83             inBytesVec.push_back(inFile.data[i]);
84         }
85         std::string strInFile(inBytesVec.begin(), inBytesVec.end());
86         std::vector<uint8_t> outBytesVec;
87         for (int i = 0; i < outFile.len; i++) {
88             outBytesVec.push_back(outFile.data[i]);
89         }
90         std::string strOutFile(outBytesVec.begin(), outBytesVec.end());
91         int32_t code = ERROR_CODE_ERRNO;
92         OPTIONS cOptions;
93 
94         code = UnZip(strInFile, strOutFile, cOptions);
95         int32_t err = CommonFunc::ConvertErrCode(code);
96 
97         return err;
98     }
99 }
100 
101 } // LIBZIP
102 } // AppExecFwk
103 } // OHOS
104