1 /*
2 * Copyright (c) 2024-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 #include "hap_info_segment.h"
16
17 namespace OHOS {
18 namespace SignatureTools {
19
HapInfoSegment()20 HapInfoSegment::HapInfoSegment()
21 {
22 std::vector<int8_t> emptyVector;
23 magic = HapInfoSegment::MAGIC_NUM;
24 signInfo = SignInfo(0, 0, 0, emptyVector, emptyVector);
25 }
26
HapInfoSegment(int32_t magic,const SignInfo & signInfo)27 HapInfoSegment::HapInfoSegment(int32_t magic, const SignInfo& signInfo)
28 {
29 this->magic = magic;
30 this->signInfo = signInfo;
31 }
32
SetSignInfo(const SignInfo & signInfo)33 void HapInfoSegment::SetSignInfo(const SignInfo& signInfo)
34 {
35 this->signInfo = signInfo;
36 }
37
GetSignInfo()38 SignInfo& HapInfoSegment::GetSignInfo()
39 {
40 return signInfo;
41 }
42
GetSize()43 int32_t HapInfoSegment::GetSize()
44 {
45 return HapInfoSegment::MAGIC_NUM_BYTES + signInfo.GetSize();
46 }
47
ToByteArray(std::vector<int8_t> & ret)48 void HapInfoSegment::ToByteArray(std::vector<int8_t> &ret)
49 {
50 std::vector<int8_t> hapSignInfoByteArray;
51 signInfo.ToByteArray(hapSignInfoByteArray);
52 std::unique_ptr<ByteBuffer> bf = std::make_unique<ByteBuffer>
53 (ByteBuffer(HapInfoSegment::MAGIC_NUM_BYTES + hapSignInfoByteArray.size()));
54 bf->PutInt32(magic);
55 bf->PutData(hapSignInfoByteArray.data(), hapSignInfoByteArray.size());
56 ret = std::vector<int8_t>(bf->GetBufferPtr(), bf->GetBufferPtr() + bf->GetPosition());
57 return;
58 }
59
FromByteArray(std::vector<int8_t> & bytes)60 HapInfoSegment HapInfoSegment::FromByteArray(std::vector<int8_t>& bytes)
61 {
62 std::unique_ptr<ByteBuffer> bf = std::make_unique<ByteBuffer>(ByteBuffer(bytes.size()));
63 bf->PutData(bytes.data(), bytes.size());
64 bf->Flip();
65 int32_t inMagic = 0;
66 bf->GetInt32(inMagic);
67 if (inMagic != HapInfoSegment::MAGIC_NUM) {
68 PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
69 "The magic number of HapInfoSegment is incorrect.");
70 return HapInfoSegment();
71 }
72 if (bytes.size() <= HapInfoSegment::MAGIC_NUM_BYTES) {
73 PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
74 "The bytes size of HapInfoSegment is incorrect.");
75 return HapInfoSegment();
76 }
77 std::vector<int8_t> hapSignInfoByteArray(bytes.size() - HapInfoSegment::MAGIC_NUM_BYTES);
78 bf->GetByte(hapSignInfoByteArray.data(), hapSignInfoByteArray.size());
79 SignInfo inHapSignInfo = SignInfo::FromByteArray(hapSignInfoByteArray);
80 if (inHapSignInfo.GetDataSize() % HapInfoSegment::CHUNK_SIZE != 0) {
81 PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
82 "Invalid dataSize, the dataSize must be an integer multiple of 4096.");
83 return HapInfoSegment();
84 }
85 if (inHapSignInfo.GetExtensionNum() != SignInfo::MAX_EXTENSION_NUM) {
86 PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
87 "The extension number of HapInfoSegment is incorrect.");
88 return HapInfoSegment();
89 }
90 return HapInfoSegment(inMagic, inHapSignInfo);
91 }
92
93 }
94 }