• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "code_protect_bundle_info.h"
17 
18 #include "nlohmann/json.hpp"
19 #include "string_ex.h"
20 
21 #include "json_util.h"
22 #include "parcel_macro.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::string VERSION_CODE = "versionCode";
28 const std::string APPLICATION_RESERVED_FLAG = "applicationReservedFlag";
29 const std::string APP_IDENTIFIER = "appIdentifier";
30 }
31 
ReadFromParcel(Parcel & parcel)32 bool CodeProtectBundleInfo::ReadFromParcel(Parcel &parcel)
33 {
34     bundleName = Str16ToStr8(parcel.ReadString16());
35     uid = parcel.ReadInt32();
36     appIndex = parcel.ReadInt32();
37     versionCode = parcel.ReadUint32();
38     applicationReservedFlag = parcel.ReadUint32();
39     appIdentifier = Str16ToStr8(parcel.ReadString16());
40     return true;
41 }
42 
Unmarshalling(Parcel & parcel)43 CodeProtectBundleInfo *CodeProtectBundleInfo::Unmarshalling(Parcel &parcel)
44 {
45     CodeProtectBundleInfo *info = new (std::nothrow) CodeProtectBundleInfo();
46     if (info && !info->ReadFromParcel(parcel)) {
47         APP_LOGW("read from parcel failed");
48         delete info;
49         info = nullptr;
50     }
51     return info;
52 }
53 
Marshalling(Parcel & parcel) const54 bool CodeProtectBundleInfo::Marshalling(Parcel &parcel) const
55 {
56     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
57     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, uid);
58     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex);
59     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
60     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, applicationReservedFlag);
61     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(appIdentifier));
62     return true;
63 }
64 
to_json(nlohmann::json & jsonObject,const CodeProtectBundleInfo & CodeProtectBundleInfo)65 void to_json(nlohmann::json &jsonObject, const CodeProtectBundleInfo &CodeProtectBundleInfo)
66 {
67     jsonObject = nlohmann::json {
68         {Constants::BUNDLE_NAME, CodeProtectBundleInfo.bundleName},
69         {Constants::UID, CodeProtectBundleInfo.uid},
70         {Constants::APP_INDEX, CodeProtectBundleInfo.appIndex},
71         {VERSION_CODE, CodeProtectBundleInfo.versionCode},
72         {APPLICATION_RESERVED_FLAG, CodeProtectBundleInfo.applicationReservedFlag},
73         {APP_IDENTIFIER, CodeProtectBundleInfo.appIdentifier}
74     };
75 }
76 
from_json(const nlohmann::json & jsonObject,CodeProtectBundleInfo & CodeProtectBundleInfo)77 void from_json(const nlohmann::json &jsonObject, CodeProtectBundleInfo &CodeProtectBundleInfo)
78 {
79     const auto &jsonObjectEnd = jsonObject.end();
80     int32_t parseResult = ERR_OK;
81     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, Constants::BUNDLE_NAME,
82         CodeProtectBundleInfo.bundleName, false, parseResult);
83     GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, Constants::UID,
84         CodeProtectBundleInfo.uid, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
85     GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, Constants::APP_INDEX,
86         CodeProtectBundleInfo.appIndex, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
87     GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, VERSION_CODE,
88         CodeProtectBundleInfo.versionCode, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
89     GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, APPLICATION_RESERVED_FLAG,
90         CodeProtectBundleInfo.applicationReservedFlag, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
91     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, APP_IDENTIFIER,
92         CodeProtectBundleInfo.appIdentifier, false, parseResult);
93     if (parseResult != ERR_OK) {
94         APP_LOGE("read CodeProtectBundleInfo jsonObject error : %{public}d", parseResult);
95     }
96 }
97 }  // namespace AppExecFwk
98 }  // namespace OHOS
99