1 /*
2 * Copyright (c) 2025 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 "app_log_wrapper.h"
17 #include "bundle_error.h"
18 #include "zip_ffi.h"
19 #include "zlib.h"
20
21 using namespace OHOS::CJSystemapi::BundleManager;
22
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace LIBZIP {
26 struct CjCheckSumEntity {
27 uint64_t Crc32(uint64_t crc, uint8_t* buf, uint64_t len);
28 };
29
Crc32(uint64_t crc,uint8_t * buf,uint64_t len)30 uint64_t CjCheckSumEntity::Crc32(uint64_t crc, uint8_t* buf, uint64_t len)
31 {
32 return crc32(static_cast<uLong>(crc), reinterpret_cast<Bytef*>(buf), static_cast<uInt>(len));
33 }
34
35 extern "C" {
FfiBundleManagerChecksumInstCreate()36 FFI_EXPORT void* FfiBundleManagerChecksumInstCreate()
37 {
38 return new CjCheckSumEntity();
39 }
40
FfiBundleManagerChecksumInstDestroy(void * ffiInst)41 FFI_EXPORT void FfiBundleManagerChecksumInstDestroy(void* ffiInst)
42 {
43 delete static_cast<CjCheckSumEntity*>(ffiInst);
44 }
45
FfiBundleManagerChecksumCrc32(void * ffiInst,uint64_t crc,uint8_t * buf,uint64_t len,int32_t * errCode)46 FFI_EXPORT uint64_t FfiBundleManagerChecksumCrc32(
47 void* ffiInst, uint64_t crc, uint8_t* buf, uint64_t len, int32_t* errCode)
48 {
49 *errCode = 0;
50 if (ffiInst == nullptr || buf == nullptr) {
51 *errCode = ERR_ZLIB_INTERNAL_STRUCT_ERROR;
52 APP_LOGE("FfiBundleManagerChecksumCrc32 param check failed");
53 return 0;
54 }
55 auto entity = static_cast<CjCheckSumEntity*>(ffiInst);
56 return entity->Crc32(crc, buf, len);
57 }
58 } // extern "C"
59 } // namespace LIBZIP
60 } // namespace AppExecFwk
61 } // namespace OHOS
62